blob: b86273fdf48ee5bdc9cb8379947a2a49219c198e [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
Jens Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
Jens Axboeffc771b2014-05-09 09:42:02 -060034#include <linux/blk-mq.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060035#include <linux/bio.h>
36#include <linux/dma-mapping.h>
37#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010038#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060039#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020040#include <linux/export.h>
Asai Thambi S P7b421d22012-06-04 12:44:02 -070041#include <linux/debugfs.h>
Sam Bradshawf45c40a2014-06-06 13:28:48 -060042#include <linux/prefetch.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060043#include "mtip32xx.h"
44
45#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
Sam Bradshaw188b9f42014-01-15 10:14:57 -080046
47/* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48#define AHCI_RX_FIS_SZ 0x100
49#define AHCI_RX_FIS_OFFSET 0x0
50#define AHCI_IDFY_SZ ATA_SECT_SIZE
51#define AHCI_IDFY_OFFSET 0x400
52#define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53#define AHCI_SECTBUF_OFFSET 0x800
54#define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55#define AHCI_SMARTBUF_OFFSET 0xC00
56/* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57#define BLOCK_DMA_ALLOC_SZ 4096
58
59/* DMA region containing command table (should be 8192 bytes) */
60#define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61#define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62#define AHCI_CMD_TBL_OFFSET 0x0
63
64/* DMA region per command (contains header and SGL) */
65#define AHCI_CMD_TBL_HDR_SZ 0x80
66#define AHCI_CMD_TBL_HDR_OFFSET 0x0
67#define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68#define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69#define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
70
Sam Bradshaw88523a62011-08-30 08:34:26 -060071
Asai Thambi S P45038362012-04-09 08:35:38 +020072#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060073#define HOST_HSORG 0xFC
74#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76#define HSORG_HWREV 0xFF00
77#define HSORG_STYLE 0x8
78#define HSORG_SLOTGROUPS 0x7
79
80#define PORT_COMMAND_ISSUE 0x38
81#define PORT_SDBV 0x7C
82
83#define PORT_OFFSET 0x100
84#define PORT_MEM_SIZE 0x80
85
86#define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
90 PORT_IRQ_OVERFLOW)
91#define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93#define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97#define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
99
100/* product numbers */
101#define MTIP_PRODUCT_UNKNOWN 0x00
102#define MTIP_PRODUCT_ASICFPGA 0x11
103
104/* Device instance number, incremented each time a device is probed. */
105static int instance;
106
Zhu Yanjun9e35fdc2016-01-05 18:39:04 +0800107static struct list_head online_list;
108static struct list_head removing_list;
109static spinlock_t dev_lock;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530110
Sam Bradshaw88523a62011-08-30 08:34:26 -0600111/*
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
114 */
Jens Axboe3ff147d2011-09-27 21:33:53 -0600115static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -0700116static struct dentry *dfs_parent;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530117static struct dentry *dfs_device_status;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600118
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800119static u32 cpu_use[NR_CPUS];
120
Sam Bradshaw88523a62011-08-30 08:34:26 -0600121static DEFINE_SPINLOCK(rssd_index_lock);
122static DEFINE_IDA(rssd_index_ida);
123
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100124static int mtip_block_initialize(struct driver_data *dd);
125
Jens Axboe16d02c02011-09-27 15:50:01 -0600126#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600127struct mtip_compat_ide_task_request_s {
128 __u8 io_ports[8];
129 __u8 hob_ports[8];
130 ide_reg_valid_t out_flags;
131 ide_reg_valid_t in_flags;
132 int data_phase;
133 int req_cmd;
134 compat_ulong_t out_size;
135 compat_ulong_t in_size;
136};
Jens Axboe16d02c02011-09-27 15:50:01 -0600137#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600138
Sam Bradshaw88523a62011-08-30 08:34:26 -0600139/*
Jens Axboe63166682011-09-27 21:27:43 -0600140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
143 *
144 * @pdev Pointer to the pci_dev structure.
145 *
146 * return value
147 * true if device removed, else false
148 */
149static bool mtip_check_surprise_removal(struct pci_dev *pdev)
150{
151 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600152 struct driver_data *dd = pci_get_drvdata(pdev);
153
154 if (dd->sr)
155 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600156
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600159 if (vendor_id == 0xFFFF) {
160 dd->sr = true;
161 if (dd->queue)
162 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
163 else
164 dev_warn(&dd->pdev->dev,
165 "%s: dd->queue is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600166 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600167 }
168
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600169 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600170}
171
Ming Leidb54fac2017-04-27 07:45:18 -0600172/* we have to use runtime tag to setup command header */
173static void mtip_init_cmd_header(struct request *rq)
174{
175 struct driver_data *dd = rq->q->queuedata;
176 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
177 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
178
179 /* Point the command headers at the command tables. */
180 cmd->command_header = dd->port->command_list +
181 (sizeof(struct mtip_cmd_hdr) * rq->tag);
182 cmd->command_header_dma = dd->port->command_list_dma +
183 (sizeof(struct mtip_cmd_hdr) * rq->tag);
184
185 if (host_cap_64)
186 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
187
188 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
189}
190
Jens Axboeffc771b2014-05-09 09:42:02 -0600191static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600192{
Jens Axboeffc771b2014-05-09 09:42:02 -0600193 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600194
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800195 if (mtip_check_surprise_removal(dd->pdev))
196 return NULL;
197
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100198 rq = blk_mq_alloc_request(dd->queue, 0, BLK_MQ_REQ_RESERVED);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800199 if (IS_ERR(rq))
200 return NULL;
201
Ming Leidb54fac2017-04-27 07:45:18 -0600202 /* Internal cmd isn't submitted via .queue_rq */
203 mtip_init_cmd_header(rq);
204
Jens Axboeffc771b2014-05-09 09:42:02 -0600205 return blk_mq_rq_to_pdu(rq);
206}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600207
Jens Axboeffc771b2014-05-09 09:42:02 -0600208static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
209{
210 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600211}
212
213/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600214 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600215 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600216static struct request *mtip_rq_from_tag(struct driver_data *dd,
217 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600218{
Jens Axboe0e62f512014-06-04 10:23:49 -0600219 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
220
221 return blk_mq_tag_to_rq(hctx->tags, tag);
Jens Axboeffc771b2014-05-09 09:42:02 -0600222}
223
224static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
225 unsigned int tag)
226{
227 struct request *rq = mtip_rq_from_tag(dd, tag);
228
229 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600230}
231
232/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600233 * IO completion function.
234 *
235 * This completion function is called by the driver ISR when a
236 * command that was issued by the kernel completes. It first calls the
237 * asynchronous completion function which normally calls back into the block
238 * layer passing the asynchronous callback data, then unmaps the
239 * scatter list associated with the completed command, and finally
240 * clears the allocated bit associated with the completed command.
241 *
242 * @port Pointer to the port data structure.
243 * @tag Tag of the command.
244 * @data Pointer to driver_data.
245 * @status Completion status.
246 *
247 * return value
248 * None
249 */
250static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600251 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600252{
Jens Axboeffc771b2014-05-09 09:42:02 -0600253 struct driver_data *dd = port->dd;
254 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600255
256 if (unlikely(!dd) || unlikely(!port))
257 return;
258
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600259 if (unlikely(status == PORT_IRQ_TF_ERR)) {
260 dev_warn(&port->dd->pdev->dev,
261 "Command tag %d failed due to TFE\n", tag);
262 }
263
Jens Axboeffc771b2014-05-09 09:42:02 -0600264 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600265
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -0800266 blk_mq_complete_request(rq, status);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600267}
268
269/*
Jens Axboe63166682011-09-27 21:27:43 -0600270 * Reset the HBA (without sleeping)
271 *
Jens Axboe63166682011-09-27 21:27:43 -0600272 * @dd Pointer to the driver data structure.
273 *
274 * return value
275 * 0 The reset was successful.
276 * -1 The HBA Reset bit did not clear.
277 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530278static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600279{
280 unsigned long timeout;
281
Jens Axboe63166682011-09-27 21:27:43 -0600282 /* Set the reset bit */
283 writel(HOST_RESET, dd->mmio + HOST_CTL);
284
285 /* Flush */
286 readl(dd->mmio + HOST_CTL);
287
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700288 /*
289 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
290 * is 1 sec but in LUN failure conditions, up to 10 secs are required
291 */
292 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530293 do {
294 mdelay(10);
295 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
296 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600297
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530298 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
299 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200300
Jens Axboe63166682011-09-27 21:27:43 -0600301 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
302 return -1;
303
304 return 0;
305}
306
307/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600308 * Issue a command to the hardware.
309 *
310 * Set the appropriate bit in the s_active and Command Issue hardware
311 * registers, causing hardware command processing to begin.
312 *
313 * @port Pointer to the port structure.
314 * @tag The tag of the command to be issued.
315 *
316 * return value
317 * None
318 */
319static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
320{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800321 int group = tag >> 5;
322
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800323 /* guard SACT and CI registers */
324 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600325 writel((1 << MTIP_TAG_BIT(tag)),
326 port->s_active[MTIP_TAG_INDEX(tag)]);
327 writel((1 << MTIP_TAG_BIT(tag)),
328 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800329 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600330}
331
332/*
Jens Axboe63166682011-09-27 21:27:43 -0600333 * Enable/disable the reception of FIS
334 *
335 * @port Pointer to the port data structure
336 * @enable 1 to enable, 0 to disable
337 *
338 * return value
339 * Previous state: 1 enabled, 0 disabled
340 */
341static int mtip_enable_fis(struct mtip_port *port, int enable)
342{
343 u32 tmp;
344
345 /* enable FIS reception */
346 tmp = readl(port->mmio + PORT_CMD);
347 if (enable)
348 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
349 else
350 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
351
352 /* Flush */
353 readl(port->mmio + PORT_CMD);
354
355 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
356}
357
358/*
359 * Enable/disable the DMA engine
360 *
361 * @port Pointer to the port data structure
362 * @enable 1 to enable, 0 to disable
363 *
364 * return value
365 * Previous state: 1 enabled, 0 disabled.
366 */
367static int mtip_enable_engine(struct mtip_port *port, int enable)
368{
369 u32 tmp;
370
371 /* enable FIS reception */
372 tmp = readl(port->mmio + PORT_CMD);
373 if (enable)
374 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
375 else
376 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
377
378 readl(port->mmio + PORT_CMD);
379 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
380}
381
382/*
383 * Enables the port DMA engine and FIS reception.
384 *
385 * return value
386 * None
387 */
388static inline void mtip_start_port(struct mtip_port *port)
389{
390 /* Enable FIS reception */
391 mtip_enable_fis(port, 1);
392
393 /* Enable the DMA engine */
394 mtip_enable_engine(port, 1);
395}
396
397/*
398 * Deinitialize a port by disabling port interrupts, the DMA engine,
399 * and FIS reception.
400 *
401 * @port Pointer to the port structure
402 *
403 * return value
404 * None
405 */
406static inline void mtip_deinit_port(struct mtip_port *port)
407{
408 /* Disable interrupts on this port */
409 writel(0, port->mmio + PORT_IRQ_MASK);
410
411 /* Disable the DMA engine */
412 mtip_enable_engine(port, 0);
413
414 /* Disable FIS reception */
415 mtip_enable_fis(port, 0);
416}
417
418/*
419 * Initialize a port.
420 *
421 * This function deinitializes the port by calling mtip_deinit_port() and
422 * then initializes it by setting the command header and RX FIS addresses,
423 * clearing the SError register and any pending port interrupts before
424 * re-enabling the default set of port interrupts.
425 *
426 * @port Pointer to the port structure.
427 *
428 * return value
429 * None
430 */
431static void mtip_init_port(struct mtip_port *port)
432{
433 int i;
434 mtip_deinit_port(port);
435
436 /* Program the command list base and FIS base addresses */
437 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
438 writel((port->command_list_dma >> 16) >> 16,
439 port->mmio + PORT_LST_ADDR_HI);
440 writel((port->rxfis_dma >> 16) >> 16,
441 port->mmio + PORT_FIS_ADDR_HI);
442 }
443
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100444 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600445 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100446 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600447
448 /* Clear SError */
449 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
450
451 /* reset the completed registers.*/
452 for (i = 0; i < port->dd->slot_groups; i++)
453 writel(0xFFFFFFFF, port->completed[i]);
454
455 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700456 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600457
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100458 /* Clear any pending interrupts on the HBA. */
459 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
460 port->dd->mmio + HOST_IRQ_STAT);
461
Jens Axboe63166682011-09-27 21:27:43 -0600462 /* Enable port interrupts */
463 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
464}
465
466/*
467 * Restart a port
468 *
469 * @port Pointer to the port data structure.
470 *
471 * return value
472 * None
473 */
474static void mtip_restart_port(struct mtip_port *port)
475{
476 unsigned long timeout;
477
478 /* Disable the DMA engine */
479 mtip_enable_engine(port, 0);
480
481 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
482 timeout = jiffies + msecs_to_jiffies(500);
483 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
484 && time_before(jiffies, timeout))
485 ;
486
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200487 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200488 return;
489
Jens Axboe63166682011-09-27 21:27:43 -0600490 /*
491 * Chip quirk: escalate to hba reset if
492 * PxCMD.CR not clear after 500 ms
493 */
494 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
495 dev_warn(&port->dd->pdev->dev,
496 "PxCMD.CR not clear, escalating reset\n");
497
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530498 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600499 dev_err(&port->dd->pdev->dev,
500 "HBA reset escalation failed.\n");
501
502 /* 30 ms delay before com reset to quiesce chip */
503 mdelay(30);
504 }
505
506 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
507
508 /* Set PxSCTL.DET */
509 writel(readl(port->mmio + PORT_SCR_CTL) |
510 1, port->mmio + PORT_SCR_CTL);
511 readl(port->mmio + PORT_SCR_CTL);
512
513 /* Wait 1 ms to quiesce chip function */
514 timeout = jiffies + msecs_to_jiffies(1);
515 while (time_before(jiffies, timeout))
516 ;
517
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200518 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200519 return;
520
Jens Axboe63166682011-09-27 21:27:43 -0600521 /* Clear PxSCTL.DET */
522 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
523 port->mmio + PORT_SCR_CTL);
524 readl(port->mmio + PORT_SCR_CTL);
525
526 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
527 timeout = jiffies + msecs_to_jiffies(500);
528 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
529 && time_before(jiffies, timeout))
530 ;
531
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200532 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200533 return;
534
Jens Axboe63166682011-09-27 21:27:43 -0600535 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
536 dev_warn(&port->dd->pdev->dev,
537 "COM reset failed\n");
538
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100539 mtip_init_port(port);
540 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600541
Jens Axboe63166682011-09-27 21:27:43 -0600542}
543
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530544static int mtip_device_reset(struct driver_data *dd)
545{
546 int rv = 0;
547
548 if (mtip_check_surprise_removal(dd->pdev))
549 return 0;
550
551 if (mtip_hba_reset(dd) < 0)
552 rv = -EFAULT;
553
554 mdelay(1);
555 mtip_init_port(dd->port);
556 mtip_start_port(dd->port);
557
558 /* Enable interrupts on the HBA. */
559 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
560 dd->mmio + HOST_CTL);
561 return rv;
562}
563
Jens Axboe63166682011-09-27 21:27:43 -0600564/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200565 * Helper function for tag logging
566 */
567static void print_tags(struct driver_data *dd,
568 char *msg,
569 unsigned long *tagbits,
570 int cnt)
571{
572 unsigned char tagmap[128];
573 int group, tagmap_len = 0;
574
575 memset(tagmap, 0, sizeof(tagmap));
576 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600577 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200578 tagbits[group-1]);
579 dev_warn(&dd->pdev->dev,
580 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
581}
582
583/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600584 * Internal command completion callback function.
585 *
586 * This function is normally called by the driver ISR when an internal
587 * command completed. This function signals the command completion by
588 * calling complete().
589 *
590 * @port Pointer to the port data structure.
591 * @tag Tag of the command that has completed.
592 * @data Pointer to a completion structure.
593 * @status Completion status.
594 *
595 * return value
596 * None
597 */
598static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600599 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600600{
Jens Axboeffc771b2014-05-09 09:42:02 -0600601 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600602 if (unlikely(status == PORT_IRQ_TF_ERR))
603 dev_warn(&port->dd->pdev->dev,
604 "Internal command %d completed with TFE\n", tag);
605
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800606 command->comp_func = NULL;
607 command->comp_data = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600608 complete(waiting);
609}
610
Asai Thambi S P8182b492012-04-09 08:35:38 +0200611static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600612 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200613{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200614}
615
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200616static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
617 dma_addr_t buffer_dma, unsigned int sectors);
618static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
619 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600620/*
621 * Handle an error.
622 *
623 * @dd Pointer to the DRIVER_DATA structure.
624 *
625 * return value
626 * None
627 */
628static void mtip_handle_tfe(struct driver_data *dd)
629{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200630 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600631 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200632 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600633 u32 completed;
634 struct host_to_dev_fis *fis;
635 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200636 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200637 unsigned char *buf;
638 char *fail_reason = NULL;
639 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600640
641 dev_warn(&dd->pdev->dev, "Taskfile error\n");
642
643 port = dd->port;
644
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700645 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600646 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700647 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
648
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700649 if (cmd->comp_data && cmd->comp_func) {
650 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600651 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700652 }
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800653 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700654 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600655
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200656 /* clear the tag accumulator */
657 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
658
Sam Bradshaw88523a62011-08-30 08:34:26 -0600659 /* Loop through all the groups */
660 for (group = 0; group < dd->slot_groups; group++) {
661 completed = readl(port->completed[group]);
662
Jens Axboeffc771b2014-05-09 09:42:02 -0600663 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
664
Sam Bradshaw88523a62011-08-30 08:34:26 -0600665 /* clear completed status register in the hardware.*/
666 writel(completed, port->completed[group]);
667
Sam Bradshaw88523a62011-08-30 08:34:26 -0600668 /* Process successfully completed commands */
669 for (bit = 0; bit < 32 && completed; bit++) {
670 if (!(completed & (1<<bit)))
671 continue;
672 tag = (group << 5) + bit;
673
674 /* Skip the internal command slot */
675 if (tag == MTIP_TAG_INTERNAL)
676 continue;
677
Jens Axboeffc771b2014-05-09 09:42:02 -0600678 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200679 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600680 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200681 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600682 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600683 } else {
684 dev_err(&port->dd->pdev->dev,
685 "Missing completion func for tag %d",
686 tag);
687 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600688 /* don't proceed further */
689 return;
690 }
691 }
692 }
693 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200694
695 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600696
697 /* Restart the port */
698 mdelay(20);
699 mtip_restart_port(port);
700
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200701 /* Trying to determine the cause of the error */
702 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
703 dd->port->log_buf,
704 dd->port->log_buf_dma, 1);
705 if (rv) {
706 dev_warn(&dd->pdev->dev,
707 "Error in READ LOG EXT (10h) command\n");
708 /* non-critical error, don't fail the load */
709 } else {
710 buf = (unsigned char *)dd->port->log_buf;
711 if (buf[259] & 0x1) {
712 dev_info(&dd->pdev->dev,
713 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200714 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200715 fail_all_ncq_write = 1;
716 fail_reason = "write protect";
717 }
718 if (buf[288] == 0xF7) {
719 dev_info(&dd->pdev->dev,
720 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200721 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200722 fail_all_ncq_cmds = 1;
723 fail_reason = "thermal shutdown";
724 }
725 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800726 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200727 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700728 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200729 fail_all_ncq_cmds = 1;
730 fail_reason = "rebuild failed";
731 }
732 }
733
Sam Bradshaw88523a62011-08-30 08:34:26 -0600734 /* clear the tag accumulator */
735 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
736
737 /* Loop through all the groups */
738 for (group = 0; group < dd->slot_groups; group++) {
739 for (bit = 0; bit < 32; bit++) {
740 reissue = 1;
741 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600742 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600743
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200744 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600745
746 /* Should re-issue? */
747 if (tag == MTIP_TAG_INTERNAL ||
748 fis->command == ATA_CMD_SET_FEATURES)
749 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200750 else {
751 if (fail_all_ncq_cmds ||
752 (fail_all_ncq_write &&
753 fis->command == ATA_CMD_FPDMA_WRITE)) {
754 dev_warn(&dd->pdev->dev,
755 " Fail: %s w/tag %d [%s].\n",
756 fis->command == ATA_CMD_FPDMA_WRITE ?
757 "write" : "read",
758 tag,
759 fail_reason != NULL ?
760 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200761 if (cmd->comp_func) {
762 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600763 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200764 }
765 continue;
766 }
767 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600768
769 /*
770 * First check if this command has
771 * exceeded its retries.
772 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200773 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600774
775 set_bit(tag, tagaccum);
776
Sam Bradshaw88523a62011-08-30 08:34:26 -0600777 /* Re-issue the command. */
778 mtip_issue_ncq_command(port, tag);
779
780 continue;
781 }
782
783 /* Retire a command that will not be reissued */
784 dev_warn(&port->dd->pdev->dev,
785 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600786
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200787 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600788 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600789 else
790 dev_warn(&port->dd->pdev->dev,
791 "Bad completion for tag %d\n",
792 tag);
793 }
794 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200795 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600796}
797
798/*
799 * Handle a set device bits interrupt
800 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800801static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
802 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600803{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800804 struct driver_data *dd = port->dd;
805 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600806 struct mtip_cmd *command;
807
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800808 if (!completed) {
809 WARN_ON_ONCE(!completed);
810 return;
811 }
812 /* clear completed status register in the hardware.*/
813 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600814
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800815 /* Process completed commands. */
816 for (bit = 0; (bit < 32) && completed; bit++) {
817 if (completed & 0x01) {
818 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600819
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800820 /* skip internal command slot. */
821 if (unlikely(tag == MTIP_TAG_INTERNAL))
822 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600823
Jens Axboeffc771b2014-05-09 09:42:02 -0600824 command = mtip_cmd_from_tag(dd, tag);
825 if (likely(command->comp_func))
826 command->comp_func(port, tag, command, 0);
827 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600828 dev_dbg(&dd->pdev->dev,
829 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800830 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800832 if (mtip_check_surprise_removal(
833 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800834 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600835 }
836 }
837 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800838 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600839 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800840
841 /* If last, re-enable interrupts */
842 if (atomic_dec_return(&dd->irq_workers_active) == 0)
843 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600844}
845
846/*
847 * Process legacy pio and d2h interrupts
848 */
849static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
850{
851 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600852 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600853
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200854 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100855 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600856 & (1 << MTIP_TAG_INTERNAL))) {
857 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600858 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600859 return;
860 }
861 }
862
Sam Bradshaw88523a62011-08-30 08:34:26 -0600863 return;
864}
865
866/*
867 * Demux and handle errors
868 */
869static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
870{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600871
872 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
873 dev_warn(&dd->pdev->dev,
874 "Clearing PxSERR.DIAG.x\n");
875 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
876 }
877
878 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
879 dev_warn(&dd->pdev->dev,
880 "Clearing PxSERR.DIAG.n\n");
881 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
882 }
883
884 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
885 dev_warn(&dd->pdev->dev,
886 "Port stat errors %x unhandled\n",
887 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700888 if (mtip_check_surprise_removal(dd->pdev))
889 return;
890 }
891 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
892 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
893 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600894 }
895}
896
897static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
898{
899 struct driver_data *dd = (struct driver_data *) data;
900 struct mtip_port *port = dd->port;
901 u32 hba_stat, port_stat;
902 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800903 int do_irq_enable = 1, i, workers;
904 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600905
906 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
907 if (hba_stat) {
908 rv = IRQ_HANDLED;
909
910 /* Acknowledge the interrupt status on the port.*/
911 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700912 if (unlikely(port_stat == 0xFFFFFFFF)) {
913 mtip_check_surprise_removal(dd->pdev);
914 return IRQ_HANDLED;
915 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600916 writel(port_stat, port->mmio + PORT_IRQ_STAT);
917
918 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800919 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
920 do_irq_enable = 0;
921 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
922
923 /* Start at 1: group zero is always local? */
924 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
925 i++) {
926 twork = &dd->work[i];
927 twork->completed = readl(port->completed[i]);
928 if (twork->completed)
929 workers++;
930 }
931
932 atomic_set(&dd->irq_workers_active, workers);
933 if (workers) {
934 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
935 twork = &dd->work[i];
936 if (twork->completed)
937 queue_work_on(
938 twork->cpu_binding,
939 dd->isr_workq,
940 &twork->work);
941 }
942
943 if (likely(dd->work[0].completed))
944 mtip_workq_sdbfx(port, 0,
945 dd->work[0].completed);
946
947 } else {
948 /*
949 * Chip quirk: SDB interrupt but nothing
950 * to complete
951 */
952 do_irq_enable = 1;
953 }
954 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600955
956 if (unlikely(port_stat & PORT_IRQ_ERR)) {
957 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600958 /* don't proceed further */
959 return IRQ_HANDLED;
960 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200961 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200962 &dd->dd_flag))
963 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600964
965 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
966 }
967
968 if (unlikely(port_stat & PORT_IRQ_LEGACY))
969 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
970 }
971
972 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800973 if (unlikely(do_irq_enable))
974 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600975
976 return rv;
977}
978
979/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600980 * HBA interrupt subroutine.
981 *
982 * @irq IRQ number.
983 * @instance Pointer to the driver data structure.
984 *
985 * return value
986 * IRQ_HANDLED A HBA interrupt was pending and handled.
987 * IRQ_NONE This interrupt was not for the HBA.
988 */
989static irqreturn_t mtip_irq_handler(int irq, void *instance)
990{
991 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800992
993 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600994}
995
996static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
997{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600998 writel(1 << MTIP_TAG_BIT(tag),
999 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1000}
1001
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001002static bool mtip_pause_ncq(struct mtip_port *port,
1003 struct host_to_dev_fis *fis)
1004{
1005 struct host_to_dev_fis *reply;
1006 unsigned long task_file_data;
1007
1008 reply = port->rxfis + RX_FIS_D2H_REG;
1009 task_file_data = readl(port->mmio+PORT_TFDATA);
1010
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301011 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001012 return false;
1013
1014 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001015 port->ic_pause_timer = jiffies;
1016 return true;
1017 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1018 (fis->features == 0x03)) {
1019 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1020 port->ic_pause_timer = jiffies;
1021 return true;
1022 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1023 ((fis->command == 0xFC) &&
1024 (fis->features == 0x27 || fis->features == 0x72 ||
1025 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001026 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001027 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001028 /* Com reset after secure erase or lowlevel format */
1029 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001030 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001031 return false;
1032 }
1033
1034 return false;
1035}
1036
Sam Bradshaw88523a62011-08-30 08:34:26 -06001037/*
1038 * Wait for port to quiesce
1039 *
1040 * @port Pointer to port data structure
1041 * @timeout Max duration to wait (ms)
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001042 * @atomic gfp_t flag to indicate blockable context or not
Sam Bradshaw88523a62011-08-30 08:34:26 -06001043 *
1044 * return value
1045 * 0 Success
1046 * -EBUSY Commands still active
1047 */
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001048static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout,
1049 gfp_t atomic)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050{
1051 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001052 unsigned int n;
1053 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001054
Jens Axboe9acf03c2014-05-14 08:22:56 -06001055 blk_mq_stop_hw_queues(port->dd->queue);
1056
Sam Bradshaw88523a62011-08-30 08:34:26 -06001057 to = jiffies + msecs_to_jiffies(timeout);
1058 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001059 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001060 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags) &&
1061 atomic == GFP_KERNEL) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001062 msleep(20);
1063 continue; /* svc thd is actively issuing commands */
1064 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001065
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001066 if (atomic == GFP_KERNEL)
1067 msleep(100);
1068 else {
1069 cpu_relax();
1070 udelay(100);
1071 }
1072
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001073 if (mtip_check_surprise_removal(port->dd->pdev))
1074 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001075
Sam Bradshaw88523a62011-08-30 08:34:26 -06001076 /*
1077 * Ignore s_active bit 0 of array element 0.
1078 * This bit will always be set
1079 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001080 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 for (n = 1; n < port->dd->slot_groups; n++)
1082 active |= readl(port->s_active[n]);
1083
1084 if (!active)
1085 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001086 } while (time_before(jiffies, to));
1087
Jens Axboe9acf03c2014-05-14 08:22:56 -06001088 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001089 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001090err_fault:
1091 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1092 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001093}
1094
1095/*
1096 * Execute an internal command and wait for the completion.
1097 *
1098 * @port Pointer to the port data structure.
1099 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001100 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001101 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001102 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001103 * @opts Command header options, excluding the FIS length
1104 * and the number of PRD entries.
1105 * @timeout Time in ms to wait for the command to complete.
1106 *
1107 * return value
1108 * 0 Command completed successfully.
1109 * -EFAULT The buffer address is not correctly aligned.
1110 * -EBUSY Internal command or other IO in progress.
1111 * -EAGAIN Time out waiting for command to complete.
1112 */
1113static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001114 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001115 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001116 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001117 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001118 u32 opts,
1119 gfp_t atomic,
1120 unsigned long timeout)
1121{
1122 struct mtip_cmd_sg *command_sg;
1123 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001124 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301125 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001126 int rv = 0;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001127 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001128
1129 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1130 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301131 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001132 return -EFAULT;
1133 }
1134
Jens Axboeffc771b2014-05-09 09:42:02 -06001135 int_cmd = mtip_get_int_command(dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001136 if (!int_cmd) {
1137 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
1138 return -EFAULT;
1139 }
Jens Axboeffc771b2014-05-09 09:42:02 -06001140
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001141 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001142
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001143 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1144 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1145
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301146 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001147
1148 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001149 if (fis->command != ATA_CMD_STANDBYNOW1) {
1150 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001151 if (mtip_quiesce_io(port,
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001152 MTIP_QUIESCE_IO_TIMEOUT_MS, atomic) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301153 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001154 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001155 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001156 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001157 wake_up_interruptible(&port->svc_wait);
1158 return -EBUSY;
1159 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001160 }
1161
1162 /* Set the completion function and data for the command. */
1163 int_cmd->comp_data = &wait;
1164 int_cmd->comp_func = mtip_completion;
1165
1166 } else {
1167 /* Clear completion - we're going to poll */
1168 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001169 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001170 }
1171
1172 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001173 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174
1175 /* Populate the SG list */
1176 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001177 __force_bit2int cpu_to_le32(opts | fis_len);
1178 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001179 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1180
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001181 command_sg->info =
1182 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1183 command_sg->dba =
1184 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1185 command_sg->dba_upper =
1186 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001187
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001188 int_cmd->command_header->opts |=
1189 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001190 }
1191
1192 /* Populate the command header */
1193 int_cmd->command_header->byte_count = 0;
1194
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001195 start = jiffies;
1196
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 /* Issue the command to the hardware */
1198 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1199
Sam Bradshaw88523a62011-08-30 08:34:26 -06001200 if (atomic == GFP_KERNEL) {
1201 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001202 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001203 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001204 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001205
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301206 if (rv == -ERESTARTSYS) { /* interrupted */
1207 dev_err(&dd->pdev->dev,
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001208 "Internal command [%02X] was interrupted after %u ms\n",
1209 fis->command,
1210 jiffies_to_msecs(jiffies - start));
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301211 rv = -EINTR;
1212 goto exec_ic_exit;
1213 } else if (rv == 0) /* timeout */
1214 dev_err(&dd->pdev->dev,
1215 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1216 fis->command, timeout);
1217 else
1218 dev_err(&dd->pdev->dev,
1219 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1220 fis->command, rv, timeout);
1221
1222 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001223 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301224 &dd->dd_flag)) {
1225 dev_err(&dd->pdev->dev,
1226 "Internal command [%02X] wait returned due to SR\n",
1227 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001228 rv = -ENXIO;
1229 goto exec_ic_exit;
1230 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301231 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001232 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301233 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001234 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001235 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301236 u32 hba_stat, port_stat;
1237
Sam Bradshaw88523a62011-08-30 08:34:26 -06001238 /* Spin for <timeout> checking if command still outstanding */
1239 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001240 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1241 & (1 << MTIP_TAG_INTERNAL))
1242 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301243 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001244 rv = -ENXIO;
1245 goto exec_ic_exit;
1246 }
1247 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001248 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301249 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001250 rv = -ENXIO;
1251 goto exec_ic_exit;
1252 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301253 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1254 if (!port_stat)
1255 continue;
1256
1257 if (port_stat & PORT_IRQ_ERR) {
1258 dev_err(&dd->pdev->dev,
1259 "Internal command [%02X] failed\n",
1260 fis->command);
1261 mtip_device_reset(dd);
1262 rv = -EIO;
1263 goto exec_ic_exit;
1264 } else {
1265 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1266 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1267 if (hba_stat)
1268 writel(hba_stat,
1269 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001270 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301271 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001272 }
1273 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001274
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001275 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1276 & (1 << MTIP_TAG_INTERNAL)) {
1277 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301278 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1279 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001280 rv = -EAGAIN;
1281 }
1282 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001283exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001284 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001285 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001286 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001287 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1288 /* NCQ paused */
1289 return rv;
1290 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001291 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001292
1293 return rv;
1294}
1295
1296/*
1297 * Byte-swap ATA ID strings.
1298 *
1299 * ATA identify data contains strings in byte-swapped 16-bit words.
1300 * They must be swapped (on all architectures) to be usable as C strings.
1301 * This function swaps bytes in-place.
1302 *
1303 * @buf The buffer location of the string
1304 * @len The number of bytes to swap
1305 *
1306 * return value
1307 * None
1308 */
1309static inline void ata_swap_string(u16 *buf, unsigned int len)
1310{
1311 int i;
1312 for (i = 0; i < (len/2); i++)
1313 be16_to_cpus(&buf[i]);
1314}
1315
Asai Thambi S P670a6412014-04-16 20:27:54 -07001316static void mtip_set_timeout(struct driver_data *dd,
1317 struct host_to_dev_fis *fis,
1318 unsigned int *timeout, u8 erasemode)
1319{
1320 switch (fis->command) {
1321 case ATA_CMD_DOWNLOAD_MICRO:
1322 *timeout = 120000; /* 2 minutes */
1323 break;
1324 case ATA_CMD_SEC_ERASE_UNIT:
1325 case 0xFC:
1326 if (erasemode)
1327 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1328 else
1329 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1330 break;
1331 case ATA_CMD_STANDBYNOW1:
1332 *timeout = 120000; /* 2 minutes */
1333 break;
1334 case 0xF7:
1335 case 0xFA:
1336 *timeout = 60000; /* 60 seconds */
1337 break;
1338 case ATA_CMD_SMART:
1339 *timeout = 15000; /* 15 seconds */
1340 break;
1341 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001342 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001343 break;
1344 }
1345}
1346
Sam Bradshaw88523a62011-08-30 08:34:26 -06001347/*
1348 * Request the device identity information.
1349 *
1350 * If a user space buffer is not specified, i.e. is NULL, the
1351 * identify information is still read from the drive and placed
1352 * into the identify data buffer (@e port->identify) in the
1353 * port data structure.
1354 * When the identify buffer contains valid identify information @e
1355 * port->identify_valid is non-zero.
1356 *
1357 * @port Pointer to the port structure.
1358 * @user_buffer A user space buffer where the identify data should be
1359 * copied.
1360 *
1361 * return value
1362 * 0 Command completed successfully.
1363 * -EFAULT An error occurred while coping data to the user buffer.
1364 * -1 Command failed.
1365 */
1366static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1367{
1368 int rv = 0;
1369 struct host_to_dev_fis fis;
1370
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001371 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001372 return -EFAULT;
1373
Sam Bradshaw88523a62011-08-30 08:34:26 -06001374 /* Build the FIS. */
1375 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1376 fis.type = 0x27;
1377 fis.opts = 1 << 7;
1378 fis.command = ATA_CMD_ID_ATA;
1379
1380 /* Set the identify information as invalid. */
1381 port->identify_valid = 0;
1382
1383 /* Clear the identify information. */
1384 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1385
1386 /* Execute the command. */
1387 if (mtip_exec_internal_command(port,
1388 &fis,
1389 5,
1390 port->identify_dma,
1391 sizeof(u16) * ATA_ID_WORDS,
1392 0,
1393 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001394 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001395 < 0) {
1396 rv = -1;
1397 goto out;
1398 }
1399
1400 /*
1401 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1402 * perform field-sensitive swapping on the string fields.
1403 * See the kernel use of ata_id_string() for proof of this.
1404 */
1405#ifdef __LITTLE_ENDIAN
1406 ata_swap_string(port->identify + 27, 40); /* model string*/
1407 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1408 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1409#else
1410 {
1411 int i;
1412 for (i = 0; i < ATA_ID_WORDS; i++)
1413 port->identify[i] = le16_to_cpu(port->identify[i]);
1414 }
1415#endif
1416
Sam Bradshaw26d58052013-10-03 10:18:05 -07001417 /* Check security locked state */
1418 if (port->identify[128] & 0x4)
1419 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1420 else
1421 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1422
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301423#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001424 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1425 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1426 port->dd->trim_supp = true;
1427 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301428#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001429 port->dd->trim_supp = false;
1430
Sam Bradshaw88523a62011-08-30 08:34:26 -06001431 /* Set the identify buffer as valid. */
1432 port->identify_valid = 1;
1433
1434 if (user_buffer) {
1435 if (copy_to_user(
1436 user_buffer,
1437 port->identify,
1438 ATA_ID_WORDS * sizeof(u16))) {
1439 rv = -EFAULT;
1440 goto out;
1441 }
1442 }
1443
1444out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001445 return rv;
1446}
1447
1448/*
1449 * Issue a standby immediate command to the device.
1450 *
1451 * @port Pointer to the port structure.
1452 *
1453 * return value
1454 * 0 Command was executed successfully.
1455 * -1 An error occurred while executing the command.
1456 */
1457static int mtip_standby_immediate(struct mtip_port *port)
1458{
1459 int rv;
1460 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001461 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001462 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001463
Sam Bradshaw88523a62011-08-30 08:34:26 -06001464 /* Build the FIS. */
1465 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1466 fis.type = 0x27;
1467 fis.opts = 1 << 7;
1468 fis.command = ATA_CMD_STANDBYNOW1;
1469
Asai Thambi S P670a6412014-04-16 20:27:54 -07001470 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1471
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001472 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001473 rv = mtip_exec_internal_command(port,
1474 &fis,
1475 5,
1476 0,
1477 0,
1478 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001479 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001480 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001481 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1482 jiffies_to_msecs(jiffies - start));
1483 if (rv)
1484 dev_warn(&port->dd->pdev->dev,
1485 "STANDBY IMMEDIATE command failed.\n");
1486
1487 return rv;
1488}
1489
1490/*
1491 * Issue a READ LOG EXT command to the device.
1492 *
1493 * @port pointer to the port structure.
1494 * @page page number to fetch
1495 * @buffer pointer to buffer
1496 * @buffer_dma dma address corresponding to @buffer
1497 * @sectors page length to fetch, in sectors
1498 *
1499 * return value
1500 * @rv return value from mtip_exec_internal_command()
1501 */
1502static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1503 dma_addr_t buffer_dma, unsigned int sectors)
1504{
1505 struct host_to_dev_fis fis;
1506
1507 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1508 fis.type = 0x27;
1509 fis.opts = 1 << 7;
1510 fis.command = ATA_CMD_READ_LOG_EXT;
1511 fis.sect_count = sectors & 0xFF;
1512 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1513 fis.lba_low = page;
1514 fis.lba_mid = 0;
1515 fis.device = ATA_DEVICE_OBS;
1516
1517 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1518
1519 return mtip_exec_internal_command(port,
1520 &fis,
1521 5,
1522 buffer_dma,
1523 sectors * ATA_SECT_SIZE,
1524 0,
1525 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001526 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001527}
1528
1529/*
1530 * Issue a SMART READ DATA command to the device.
1531 *
1532 * @port pointer to the port structure.
1533 * @buffer pointer to buffer
1534 * @buffer_dma dma address corresponding to @buffer
1535 *
1536 * return value
1537 * @rv return value from mtip_exec_internal_command()
1538 */
1539static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1540 dma_addr_t buffer_dma)
1541{
1542 struct host_to_dev_fis fis;
1543
1544 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1545 fis.type = 0x27;
1546 fis.opts = 1 << 7;
1547 fis.command = ATA_CMD_SMART;
1548 fis.features = 0xD0;
1549 fis.sect_count = 1;
1550 fis.lba_mid = 0x4F;
1551 fis.lba_hi = 0xC2;
1552 fis.device = ATA_DEVICE_OBS;
1553
1554 return mtip_exec_internal_command(port,
1555 &fis,
1556 5,
1557 buffer_dma,
1558 ATA_SECT_SIZE,
1559 0,
1560 GFP_ATOMIC,
1561 15000);
1562}
1563
1564/*
1565 * Get the value of a smart attribute
1566 *
1567 * @port pointer to the port structure
1568 * @id attribute number
1569 * @attrib pointer to return attrib information corresponding to @id
1570 *
1571 * return value
1572 * -EINVAL NULL buffer passed or unsupported attribute @id.
1573 * -EPERM Identify data not valid, SMART not supported or not enabled
1574 */
1575static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1576 struct smart_attr *attrib)
1577{
1578 int rv, i;
1579 struct smart_attr *pattr;
1580
1581 if (!attrib)
1582 return -EINVAL;
1583
1584 if (!port->identify_valid) {
1585 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1586 return -EPERM;
1587 }
1588 if (!(port->identify[82] & 0x1)) {
1589 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1590 return -EPERM;
1591 }
1592 if (!(port->identify[85] & 0x1)) {
1593 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1594 return -EPERM;
1595 }
1596
1597 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1598 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1599 if (rv) {
1600 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1601 return rv;
1602 }
1603
1604 pattr = (struct smart_attr *)(port->smart_buf + 2);
1605 for (i = 0; i < 29; i++, pattr++)
1606 if (pattr->attr_id == id) {
1607 memcpy(attrib, pattr, sizeof(struct smart_attr));
1608 break;
1609 }
1610
1611 if (i == 29) {
1612 dev_warn(&port->dd->pdev->dev,
1613 "Query for invalid SMART attribute ID\n");
1614 rv = -EINVAL;
1615 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001616
Sam Bradshaw88523a62011-08-30 08:34:26 -06001617 return rv;
1618}
1619
1620/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001621 * Trim unused sectors
1622 *
1623 * @dd pointer to driver_data structure
1624 * @lba starting lba
1625 * @len # of 512b sectors to trim
1626 *
1627 * return value
1628 * -ENOMEM Out of dma memory
1629 * -EINVAL Invalid parameters passed in, trim not supported
1630 * -EIO Error submitting trim request to hw
1631 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301632static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1633 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001634{
1635 int i, rv = 0;
1636 u64 tlba, tlen, sect_left;
1637 struct mtip_trim_entry *buf;
1638 dma_addr_t dma_addr;
1639 struct host_to_dev_fis fis;
1640
1641 if (!len || dd->trim_supp == false)
1642 return -EINVAL;
1643
1644 /* Trim request too big */
1645 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1646
1647 /* Trim request not aligned on 4k boundary */
1648 WARN_ON(len % 8 != 0);
1649
1650 /* Warn if vu_trim structure is too big */
1651 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1652
1653 /* Allocate a DMA buffer for the trim structure */
1654 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1655 GFP_KERNEL);
1656 if (!buf)
1657 return -ENOMEM;
1658 memset(buf, 0, ATA_SECT_SIZE);
1659
1660 for (i = 0, sect_left = len, tlba = lba;
1661 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1662 i++) {
1663 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1664 MTIP_MAX_TRIM_ENTRY_LEN :
1665 sect_left);
1666 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1667 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1668 tlba += tlen;
1669 sect_left -= tlen;
1670 }
1671 WARN_ON(sect_left != 0);
1672
1673 /* Build the fis */
1674 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1675 fis.type = 0x27;
1676 fis.opts = 1 << 7;
1677 fis.command = 0xfb;
1678 fis.features = 0x60;
1679 fis.sect_count = 1;
1680 fis.device = ATA_DEVICE_OBS;
1681
1682 if (mtip_exec_internal_command(dd->port,
1683 &fis,
1684 5,
1685 dma_addr,
1686 ATA_SECT_SIZE,
1687 0,
1688 GFP_KERNEL,
1689 MTIP_TRIM_TIMEOUT_MS) < 0)
1690 rv = -EIO;
1691
1692 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1693 return rv;
1694}
1695
1696/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001697 * Get the drive capacity.
1698 *
1699 * @dd Pointer to the device data structure.
1700 * @sectors Pointer to the variable that will receive the sector count.
1701 *
1702 * return value
1703 * 1 Capacity was returned successfully.
1704 * 0 The identify information is invalid.
1705 */
Jens Axboe63166682011-09-27 21:27:43 -06001706static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001707{
1708 struct mtip_port *port = dd->port;
1709 u64 total, raw0, raw1, raw2, raw3;
1710 raw0 = port->identify[100];
1711 raw1 = port->identify[101];
1712 raw2 = port->identify[102];
1713 raw3 = port->identify[103];
1714 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1715 *sectors = total;
1716 return (bool) !!port->identify_valid;
1717}
1718
1719/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001720 * Display the identify command data.
1721 *
1722 * @port Pointer to the port data structure.
1723 *
1724 * return value
1725 * None
1726 */
1727static void mtip_dump_identify(struct mtip_port *port)
1728{
1729 sector_t sectors;
1730 unsigned short revid;
1731 char cbuf[42];
1732
1733 if (!port->identify_valid)
1734 return;
1735
1736 strlcpy(cbuf, (char *)(port->identify+10), 21);
1737 dev_info(&port->dd->pdev->dev,
1738 "Serial No.: %s\n", cbuf);
1739
1740 strlcpy(cbuf, (char *)(port->identify+23), 9);
1741 dev_info(&port->dd->pdev->dev,
1742 "Firmware Ver.: %s\n", cbuf);
1743
1744 strlcpy(cbuf, (char *)(port->identify+27), 41);
1745 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1746
Sam Bradshaw26d58052013-10-03 10:18:05 -07001747 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1748 port->identify[128],
1749 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1750
Sam Bradshaw88523a62011-08-30 08:34:26 -06001751 if (mtip_hw_get_capacity(port->dd, &sectors))
1752 dev_info(&port->dd->pdev->dev,
1753 "Capacity: %llu sectors (%llu MB)\n",
1754 (u64)sectors,
1755 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1756
1757 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001758 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001759 case 0x1:
1760 strlcpy(cbuf, "A0", 3);
1761 break;
1762 case 0x3:
1763 strlcpy(cbuf, "A2", 3);
1764 break;
1765 default:
1766 strlcpy(cbuf, "?", 2);
1767 break;
1768 }
1769 dev_info(&port->dd->pdev->dev,
1770 "Card Type: %s\n", cbuf);
1771}
1772
1773/*
1774 * Map the commands scatter list into the command table.
1775 *
1776 * @command Pointer to the command.
1777 * @nents Number of scatter list entries.
1778 *
1779 * return value
1780 * None
1781 */
1782static inline void fill_command_sg(struct driver_data *dd,
1783 struct mtip_cmd *command,
1784 int nents)
1785{
1786 int n;
1787 unsigned int dma_len;
1788 struct mtip_cmd_sg *command_sg;
1789 struct scatterlist *sg = command->sg;
1790
1791 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1792
1793 for (n = 0; n < nents; n++) {
1794 dma_len = sg_dma_len(sg);
1795 if (dma_len > 0x400000)
1796 dev_err(&dd->pdev->dev,
1797 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001798 command_sg->info = __force_bit2int
1799 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1800 command_sg->dba = __force_bit2int
1801 cpu_to_le32(sg_dma_address(sg));
1802 command_sg->dba_upper = __force_bit2int
1803 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001804 command_sg++;
1805 sg++;
1806 }
1807}
1808
1809/*
1810 * @brief Execute a drive command.
1811 *
1812 * return value 0 The command completed successfully.
1813 * return value -1 An error occurred while executing the command.
1814 */
Jens Axboe63166682011-09-27 21:27:43 -06001815static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001816{
1817 struct host_to_dev_fis fis;
1818 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001819 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001820
Sam Bradshaw88523a62011-08-30 08:34:26 -06001821 /* Build the FIS. */
1822 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1823 fis.type = 0x27;
1824 fis.opts = 1 << 7;
1825 fis.command = command[0];
1826 fis.features = command[1];
1827 fis.sect_count = command[2];
1828 fis.sector = command[3];
1829 fis.cyl_low = command[4];
1830 fis.cyl_hi = command[5];
1831 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1832
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001833 mtip_set_timeout(port->dd, &fis, &to, 0);
1834
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001835 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 -06001836 __func__,
1837 command[0],
1838 command[1],
1839 command[2],
1840 command[3],
1841 command[4],
1842 command[5],
1843 command[6]);
1844
1845 /* Execute the command. */
1846 if (mtip_exec_internal_command(port,
1847 &fis,
1848 5,
1849 0,
1850 0,
1851 0,
1852 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001853 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001854 return -1;
1855 }
1856
1857 command[0] = reply->command; /* Status*/
1858 command[1] = reply->features; /* Error*/
1859 command[4] = reply->cyl_low;
1860 command[5] = reply->cyl_hi;
1861
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001862 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 -06001863 __func__,
1864 command[0],
1865 command[1],
1866 command[4],
1867 command[5]);
1868
Sam Bradshaw88523a62011-08-30 08:34:26 -06001869 return 0;
1870}
1871
1872/*
1873 * @brief Execute a drive command.
1874 *
1875 * @param port Pointer to the port data structure.
1876 * @param command Pointer to the user specified command parameters.
1877 * @param user_buffer Pointer to the user space buffer where read sector
1878 * data should be copied.
1879 *
1880 * return value 0 The command completed successfully.
1881 * return value -EFAULT An error occurred while copying the completion
1882 * data to the user space buffer.
1883 * return value -1 An error occurred while executing the command.
1884 */
Jens Axboe63166682011-09-27 21:27:43 -06001885static int exec_drive_command(struct mtip_port *port, u8 *command,
1886 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001887{
1888 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001889 struct host_to_dev_fis *reply;
1890 u8 *buf = NULL;
1891 dma_addr_t dma_addr = 0;
1892 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001893 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001894
1895 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001896 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001897 return -EFAULT;
1898
1899 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1900 ATA_SECT_SIZE * xfer_sz,
1901 &dma_addr,
1902 GFP_KERNEL);
1903 if (!buf) {
1904 dev_err(&port->dd->pdev->dev,
1905 "Memory allocation failed (%d bytes)\n",
1906 ATA_SECT_SIZE * xfer_sz);
1907 return -ENOMEM;
1908 }
1909 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1910 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001911
Sam Bradshaw88523a62011-08-30 08:34:26 -06001912 /* Build the FIS. */
1913 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001914 fis.type = 0x27;
1915 fis.opts = 1 << 7;
1916 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001917 fis.features = command[2];
1918 fis.sect_count = command[3];
1919 if (fis.command == ATA_CMD_SMART) {
1920 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001921 fis.cyl_low = 0x4F;
1922 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001923 }
1924
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001925 mtip_set_timeout(port->dd, &fis, &to, 0);
1926
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001927 if (xfer_sz)
1928 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1929 else
1930 reply = (port->rxfis + RX_FIS_D2H_REG);
1931
Sam Bradshaw88523a62011-08-30 08:34:26 -06001932 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001933 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001934 "feat %x, sectcnt %x\n",
1935 __func__,
1936 command[0],
1937 command[1],
1938 command[2],
1939 command[3]);
1940
Sam Bradshaw88523a62011-08-30 08:34:26 -06001941 /* Execute the command. */
1942 if (mtip_exec_internal_command(port,
1943 &fis,
1944 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001945 (xfer_sz ? dma_addr : 0),
1946 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001947 0,
1948 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001949 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001950 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001951 rv = -EFAULT;
1952 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001953 }
1954
1955 /* Collect the completion status. */
1956 command[0] = reply->command; /* Status*/
1957 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001958 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001959
1960 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001961 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001962 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001963 __func__,
1964 command[0],
1965 command[1],
1966 command[2]);
1967
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001968 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001969 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001970 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001971 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001972 rv = -EFAULT;
1973 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001974 }
1975 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001976exit_drive_command:
1977 if (buf)
1978 dmam_free_coherent(&port->dd->pdev->dev,
1979 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1980 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001981}
1982
1983/*
1984 * Indicates whether a command has a single sector payload.
1985 *
1986 * @command passed to the device to perform the certain event.
1987 * @features passed to the device to perform the certain event.
1988 *
1989 * return value
1990 * 1 command is one that always has a single sector payload,
1991 * regardless of the value in the Sector Count field.
1992 * 0 otherwise
1993 *
1994 */
1995static unsigned int implicit_sector(unsigned char command,
1996 unsigned char features)
1997{
1998 unsigned int rv = 0;
1999
2000 /* list of commands that have an implicit sector count of 1 */
2001 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002002 case ATA_CMD_SEC_SET_PASS:
2003 case ATA_CMD_SEC_UNLOCK:
2004 case ATA_CMD_SEC_ERASE_PREP:
2005 case ATA_CMD_SEC_ERASE_UNIT:
2006 case ATA_CMD_SEC_FREEZE_LOCK:
2007 case ATA_CMD_SEC_DISABLE_PASS:
2008 case ATA_CMD_PMP_READ:
2009 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010 rv = 1;
2011 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002012 case ATA_CMD_SET_MAX:
2013 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002014 rv = 1;
2015 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002016 case ATA_CMD_SMART:
2017 if ((features == ATA_SMART_READ_VALUES) ||
2018 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002019 rv = 1;
2020 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002021 case ATA_CMD_CONF_OVERLAY:
2022 if ((features == ATA_DCO_IDENTIFY) ||
2023 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002024 rv = 1;
2025 break;
2026 }
2027 return rv;
2028}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002029
Sam Bradshaw88523a62011-08-30 08:34:26 -06002030/*
2031 * Executes a taskfile
2032 * See ide_taskfile_ioctl() for derivation
2033 */
2034static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002035 void __user *buf,
2036 ide_task_request_t *req_task,
2037 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002038{
2039 struct host_to_dev_fis fis;
2040 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002041 u8 *outbuf = NULL;
2042 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002043 dma_addr_t outbuf_dma = 0;
2044 dma_addr_t inbuf_dma = 0;
2045 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002046 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002047 unsigned int taskin = 0;
2048 unsigned int taskout = 0;
2049 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002050 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002051 unsigned int force_single_sector;
2052 unsigned int transfer_size;
2053 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002054 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002055 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002056
2057 taskout = req_task->out_size;
2058 taskin = req_task->in_size;
2059 /* 130560 = 512 * 0xFF*/
2060 if (taskin > 130560 || taskout > 130560) {
2061 err = -EINVAL;
2062 goto abort;
2063 }
2064
2065 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002066 outbuf = memdup_user(buf + outtotal, taskout);
2067 if (IS_ERR(outbuf)) {
2068 err = PTR_ERR(outbuf);
2069 outbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002070 goto abort;
2071 }
2072 outbuf_dma = pci_map_single(dd->pdev,
2073 outbuf,
2074 taskout,
2075 DMA_TO_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002076 if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002077 err = -ENOMEM;
2078 goto abort;
2079 }
2080 dma_buffer = outbuf_dma;
2081 }
2082
2083 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002084 inbuf = memdup_user(buf + intotal, taskin);
2085 if (IS_ERR(inbuf)) {
2086 err = PTR_ERR(inbuf);
2087 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002088 goto abort;
2089 }
2090 inbuf_dma = pci_map_single(dd->pdev,
2091 inbuf,
2092 taskin, DMA_FROM_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002093 if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002094 err = -ENOMEM;
2095 goto abort;
2096 }
2097 dma_buffer = inbuf_dma;
2098 }
2099
2100 /* only supports PIO and non-data commands from this ioctl. */
2101 switch (req_task->data_phase) {
2102 case TASKFILE_OUT:
2103 nsect = taskout / ATA_SECT_SIZE;
2104 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2105 break;
2106 case TASKFILE_IN:
2107 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2108 break;
2109 case TASKFILE_NO_DATA:
2110 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2111 break;
2112 default:
2113 err = -EINVAL;
2114 goto abort;
2115 }
2116
Sam Bradshaw88523a62011-08-30 08:34:26 -06002117 /* Build the FIS. */
2118 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2119
2120 fis.type = 0x27;
2121 fis.opts = 1 << 7;
2122 fis.command = req_task->io_ports[7];
2123 fis.features = req_task->io_ports[1];
2124 fis.sect_count = req_task->io_ports[2];
2125 fis.lba_low = req_task->io_ports[3];
2126 fis.lba_mid = req_task->io_ports[4];
2127 fis.lba_hi = req_task->io_ports[5];
2128 /* Clear the dev bit*/
2129 fis.device = req_task->io_ports[6] & ~0x10;
2130
2131 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2132 req_task->in_flags.all =
2133 IDE_TASKFILE_STD_IN_FLAGS |
2134 (IDE_HOB_STD_IN_FLAGS << 8);
2135 fis.lba_low_ex = req_task->hob_ports[3];
2136 fis.lba_mid_ex = req_task->hob_ports[4];
2137 fis.lba_hi_ex = req_task->hob_ports[5];
2138 fis.features_ex = req_task->hob_ports[1];
2139 fis.sect_cnt_ex = req_task->hob_ports[2];
2140
2141 } else {
2142 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2143 }
2144
2145 force_single_sector = implicit_sector(fis.command, fis.features);
2146
2147 if ((taskin || taskout) && (!fis.sect_count)) {
2148 if (nsect)
2149 fis.sect_count = nsect;
2150 else {
2151 if (!force_single_sector) {
2152 dev_warn(&dd->pdev->dev,
2153 "data movement but "
2154 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002155 err = -EINVAL;
2156 goto abort;
2157 }
2158 }
2159 }
2160
2161 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002162 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002163 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2164 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002165 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002166 fis.command,
2167 fis.features,
2168 fis.sect_count,
2169 fis.lba_low,
2170 fis.lba_mid,
2171 fis.lba_hi,
2172 fis.device);
2173
Selvan Mani4453bc82012-09-27 14:36:43 +02002174 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002175 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2176 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002177 erasemode = 1;
2178 }
2179
2180 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002181
2182 /* Determine the correct transfer size.*/
2183 if (force_single_sector)
2184 transfer_size = ATA_SECT_SIZE;
2185 else
2186 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2187
2188 /* Execute the command.*/
2189 if (mtip_exec_internal_command(dd->port,
2190 &fis,
2191 5,
2192 dma_buffer,
2193 transfer_size,
2194 0,
2195 GFP_KERNEL,
2196 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002197 err = -EIO;
2198 goto abort;
2199 }
2200
2201 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2202
2203 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2204 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2205 req_task->io_ports[7] = reply->control;
2206 } else {
2207 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2208 req_task->io_ports[7] = reply->command;
2209 }
2210
2211 /* reclaim the DMA buffers.*/
2212 if (inbuf_dma)
2213 pci_unmap_single(dd->pdev, inbuf_dma,
2214 taskin, DMA_FROM_DEVICE);
2215 if (outbuf_dma)
2216 pci_unmap_single(dd->pdev, outbuf_dma,
2217 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002218 inbuf_dma = 0;
2219 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002220
2221 /* return the ATA registers to the caller.*/
2222 req_task->io_ports[1] = reply->features;
2223 req_task->io_ports[2] = reply->sect_count;
2224 req_task->io_ports[3] = reply->lba_low;
2225 req_task->io_ports[4] = reply->lba_mid;
2226 req_task->io_ports[5] = reply->lba_hi;
2227 req_task->io_ports[6] = reply->device;
2228
2229 if (req_task->out_flags.all & 1) {
2230
2231 req_task->hob_ports[3] = reply->lba_low_ex;
2232 req_task->hob_ports[4] = reply->lba_mid_ex;
2233 req_task->hob_ports[5] = reply->lba_hi_ex;
2234 req_task->hob_ports[1] = reply->features_ex;
2235 req_task->hob_ports[2] = reply->sect_cnt_ex;
2236 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002237 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002238 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002239 "err %x, sect_cnt %x, lbalo %x,"
2240 "lbamid %x, lbahi %x, dev %x\n",
2241 __func__,
2242 req_task->io_ports[7],
2243 req_task->io_ports[1],
2244 req_task->io_ports[2],
2245 req_task->io_ports[3],
2246 req_task->io_ports[4],
2247 req_task->io_ports[5],
2248 req_task->io_ports[6]);
2249
Sam Bradshaw88523a62011-08-30 08:34:26 -06002250 if (taskout) {
2251 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2252 err = -EFAULT;
2253 goto abort;
2254 }
2255 }
2256 if (taskin) {
2257 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2258 err = -EFAULT;
2259 goto abort;
2260 }
2261 }
2262abort:
2263 if (inbuf_dma)
2264 pci_unmap_single(dd->pdev, inbuf_dma,
2265 taskin, DMA_FROM_DEVICE);
2266 if (outbuf_dma)
2267 pci_unmap_single(dd->pdev, outbuf_dma,
2268 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002269 kfree(outbuf);
2270 kfree(inbuf);
2271
2272 return err;
2273}
2274
2275/*
2276 * Handle IOCTL calls from the Block Layer.
2277 *
2278 * This function is called by the Block Layer when it receives an IOCTL
2279 * command that it does not understand. If the IOCTL command is not supported
2280 * this function returns -ENOTTY.
2281 *
2282 * @dd Pointer to the driver data structure.
2283 * @cmd IOCTL command passed from the Block Layer.
2284 * @arg IOCTL argument passed from the Block Layer.
2285 *
2286 * return value
2287 * 0 The IOCTL completed successfully.
2288 * -ENOTTY The specified command is not supported.
2289 * -EFAULT An error occurred copying data to a user space buffer.
2290 * -EIO An error occurred while executing the command.
2291 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002292static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2293 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002294{
2295 switch (cmd) {
2296 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002297 {
2298 if (copy_to_user((void __user *)arg, dd->port->identify,
2299 sizeof(u16) * ATA_ID_WORDS))
2300 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002301 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002302 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002303 case HDIO_DRIVE_CMD:
2304 {
2305 u8 drive_command[4];
2306
2307 /* Copy the user command info to our buffer. */
2308 if (copy_from_user(drive_command,
2309 (void __user *) arg,
2310 sizeof(drive_command)))
2311 return -EFAULT;
2312
2313 /* Execute the drive command. */
2314 if (exec_drive_command(dd->port,
2315 drive_command,
2316 (void __user *) (arg+4)))
2317 return -EIO;
2318
2319 /* Copy the status back to the users buffer. */
2320 if (copy_to_user((void __user *) arg,
2321 drive_command,
2322 sizeof(drive_command)))
2323 return -EFAULT;
2324
2325 break;
2326 }
2327 case HDIO_DRIVE_TASK:
2328 {
2329 u8 drive_command[7];
2330
2331 /* Copy the user command info to our buffer. */
2332 if (copy_from_user(drive_command,
2333 (void __user *) arg,
2334 sizeof(drive_command)))
2335 return -EFAULT;
2336
2337 /* Execute the drive command. */
2338 if (exec_drive_task(dd->port, drive_command))
2339 return -EIO;
2340
2341 /* Copy the status back to the users buffer. */
2342 if (copy_to_user((void __user *) arg,
2343 drive_command,
2344 sizeof(drive_command)))
2345 return -EFAULT;
2346
2347 break;
2348 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002349 case HDIO_DRIVE_TASKFILE: {
2350 ide_task_request_t req_task;
2351 int ret, outtotal;
2352
2353 if (copy_from_user(&req_task, (void __user *) arg,
2354 sizeof(req_task)))
2355 return -EFAULT;
2356
2357 outtotal = sizeof(req_task);
2358
2359 ret = exec_drive_taskfile(dd, (void __user *) arg,
2360 &req_task, outtotal);
2361
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002362 if (copy_to_user((void __user *) arg, &req_task,
2363 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002364 return -EFAULT;
2365
2366 return ret;
2367 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002368
2369 default:
2370 return -EINVAL;
2371 }
2372 return 0;
2373}
2374
2375/*
2376 * Submit an IO to the hw
2377 *
2378 * This function is called by the block layer to issue an io
2379 * to the device. Upon completion, the callback function will
2380 * be called with the data parameter passed as the callback data.
2381 *
2382 * @dd Pointer to the driver data structure.
2383 * @start First sector to read.
2384 * @nsect Number of sectors to read.
2385 * @nents Number of entries in scatter list for the read command.
2386 * @tag The tag of this read command.
2387 * @callback Pointer to the function that should be called
2388 * when the read completes.
2389 * @data Callback data passed to the callback function
2390 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002391 * @dir Direction (read or write)
2392 *
2393 * return value
2394 * None
2395 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002396static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2397 struct mtip_cmd *command, int nents,
2398 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002399{
2400 struct host_to_dev_fis *fis;
2401 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002402 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2403 u64 start = blk_rq_pos(rq);
2404 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405
2406 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002407 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002408
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002409 prefetch(&port->flags);
2410
Sam Bradshaw88523a62011-08-30 08:34:26 -06002411 command->scatter_ents = nents;
2412
2413 /*
2414 * The number of retries for this command before it is
2415 * reported as a failure to the upper layers.
2416 */
2417 command->retries = MTIP_MAX_RETRIES;
2418
2419 /* Fill out fis */
2420 fis = command->command;
2421 fis->type = 0x27;
2422 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002423 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002424 fis->command = ATA_CMD_FPDMA_READ;
2425 else
2426 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002427 fis->lba_low = start & 0xFF;
2428 fis->lba_mid = (start >> 8) & 0xFF;
2429 fis->lba_hi = (start >> 16) & 0xFF;
2430 fis->lba_low_ex = (start >> 24) & 0xFF;
2431 fis->lba_mid_ex = (start >> 32) & 0xFF;
2432 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002433 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002434 fis->features = nsect & 0xFF;
2435 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002436 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002437 fis->sect_cnt_ex = 0;
2438 fis->control = 0;
2439 fis->res2 = 0;
2440 fis->res3 = 0;
2441 fill_command_sg(dd, command, nents);
2442
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002443 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002444 fis->device |= 1 << 7;
2445
Sam Bradshaw88523a62011-08-30 08:34:26 -06002446 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002447 command->command_header->opts =
2448 __force_bit2int cpu_to_le32(
2449 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002450 command->command_header->byte_count = 0;
2451
2452 /*
2453 * Set the completion function and data for the command
2454 * within this layer.
2455 */
2456 command->comp_data = dd;
2457 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002458 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002459
2460 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002461 * To prevent this command from being issued
2462 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002463 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002464 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002465 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002466 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002467 return;
2468 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002469
2470 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002471 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002472}
2473
2474/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002475 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002476 *
2477 * @dev Pointer to the device structure, passed by the kernrel.
2478 * @attr Pointer to the device_attribute structure passed by the kernel.
2479 * @buf Pointer to the char buffer that will receive the stats info.
2480 *
2481 * return value
2482 * The size, in bytes, of the data copied into buf.
2483 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002484static ssize_t mtip_hw_show_status(struct device *dev,
2485 struct device_attribute *attr,
2486 char *buf)
2487{
2488 struct driver_data *dd = dev_to_disk(dev)->private_data;
2489 int size = 0;
2490
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002491 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002492 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002493 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002494 size += sprintf(buf, "%s", "write_protect\n");
2495 else
2496 size += sprintf(buf, "%s", "online\n");
2497
2498 return size;
2499}
2500
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002501static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002502
Asai Thambi S P0caff002013-04-03 19:56:21 +05302503/* debugsfs entries */
2504
2505static ssize_t show_device_status(struct device_driver *drv, char *buf)
2506{
2507 int size = 0;
2508 struct driver_data *dd, *tmp;
2509 unsigned long flags;
2510 char id_buf[42];
2511 u16 status = 0;
2512
2513 spin_lock_irqsave(&dev_lock, flags);
2514 size += sprintf(&buf[size], "Devices Present:\n");
2515 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002516 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302517 if (dd->port &&
2518 dd->port->identify &&
2519 dd->port->identify_valid) {
2520 strlcpy(id_buf,
2521 (char *) (dd->port->identify + 10), 21);
2522 status = *(dd->port->identify + 141);
2523 } else {
2524 memset(id_buf, 0, 42);
2525 status = 0;
2526 }
2527
2528 if (dd->port &&
2529 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2530 size += sprintf(&buf[size],
2531 " device %s %s (ftl rebuild %d %%)\n",
2532 dev_name(&dd->pdev->dev),
2533 id_buf,
2534 status);
2535 } else {
2536 size += sprintf(&buf[size],
2537 " device %s %s\n",
2538 dev_name(&dd->pdev->dev),
2539 id_buf);
2540 }
2541 }
2542 }
2543
2544 size += sprintf(&buf[size], "Devices Being Removed:\n");
2545 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002546 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302547 if (dd->port &&
2548 dd->port->identify &&
2549 dd->port->identify_valid) {
2550 strlcpy(id_buf,
2551 (char *) (dd->port->identify+10), 21);
2552 status = *(dd->port->identify + 141);
2553 } else {
2554 memset(id_buf, 0, 42);
2555 status = 0;
2556 }
2557
2558 if (dd->port &&
2559 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2560 size += sprintf(&buf[size],
2561 " device %s %s (ftl rebuild %d %%)\n",
2562 dev_name(&dd->pdev->dev),
2563 id_buf,
2564 status);
2565 } else {
2566 size += sprintf(&buf[size],
2567 " device %s %s\n",
2568 dev_name(&dd->pdev->dev),
2569 id_buf);
2570 }
2571 }
2572 }
2573 spin_unlock_irqrestore(&dev_lock, flags);
2574
2575 return size;
2576}
2577
2578static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2579 size_t len, loff_t *offset)
2580{
David Milburnc8afd0d2013-05-23 16:23:45 -05002581 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302582 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002583 char *buf;
2584 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302585
2586 if (!len || *offset)
2587 return 0;
2588
David Milburnc8afd0d2013-05-23 16:23:45 -05002589 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2590 if (!buf) {
2591 dev_err(&dd->pdev->dev,
2592 "Memory allocation: status buffer\n");
2593 return -ENOMEM;
2594 }
2595
Asai Thambi S P0caff002013-04-03 19:56:21 +05302596 size += show_device_status(NULL, buf);
2597
2598 *offset = size <= len ? size : len;
2599 size = copy_to_user(ubuf, buf, *offset);
2600 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002601 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302602
David Milburnc8afd0d2013-05-23 16:23:45 -05002603 kfree(buf);
2604 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302605}
2606
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002607static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2608 size_t len, loff_t *offset)
2609{
2610 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002611 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002612 u32 group_allocated;
2613 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002614 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002615
2616 if (!len || size)
2617 return 0;
2618
David Milburnc8afd0d2013-05-23 16:23:45 -05002619 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2620 if (!buf) {
2621 dev_err(&dd->pdev->dev,
2622 "Memory allocation: register buffer\n");
2623 return -ENOMEM;
2624 }
2625
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002626 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2627
2628 for (n = dd->slot_groups-1; n >= 0; n--)
2629 size += sprintf(&buf[size], "%08X ",
2630 readl(dd->port->s_active[n]));
2631
2632 size += sprintf(&buf[size], "]\n");
2633 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2634
2635 for (n = dd->slot_groups-1; n >= 0; n--)
2636 size += sprintf(&buf[size], "%08X ",
2637 readl(dd->port->cmd_issue[n]));
2638
2639 size += sprintf(&buf[size], "]\n");
2640 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2641
2642 for (n = dd->slot_groups-1; n >= 0; n--)
2643 size += sprintf(&buf[size], "%08X ",
2644 readl(dd->port->completed[n]));
2645
2646 size += sprintf(&buf[size], "]\n");
2647 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2648 readl(dd->port->mmio + PORT_IRQ_STAT));
2649 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2650 readl(dd->mmio + HOST_IRQ_STAT));
2651 size += sprintf(&buf[size], "\n");
2652
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002653 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2654
2655 for (n = dd->slot_groups-1; n >= 0; n--) {
2656 if (sizeof(long) > sizeof(u32))
2657 group_allocated =
2658 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2659 else
2660 group_allocated = dd->port->cmds_to_issue[n];
2661 size += sprintf(&buf[size], "%08X ", group_allocated);
2662 }
2663 size += sprintf(&buf[size], "]\n");
2664
2665 *offset = size <= len ? size : len;
2666 size = copy_to_user(ubuf, buf, *offset);
2667 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002668 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002669
David Milburnc8afd0d2013-05-23 16:23:45 -05002670 kfree(buf);
2671 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002672}
2673
2674static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2675 size_t len, loff_t *offset)
2676{
2677 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002678 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002679 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002680 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002681
2682 if (!len || size)
2683 return 0;
2684
David Milburnc8afd0d2013-05-23 16:23:45 -05002685 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2686 if (!buf) {
2687 dev_err(&dd->pdev->dev,
2688 "Memory allocation: flag buffer\n");
2689 return -ENOMEM;
2690 }
2691
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002692 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2693 dd->port->flags);
2694 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2695 dd->dd_flag);
2696
2697 *offset = size <= len ? size : len;
2698 size = copy_to_user(ubuf, buf, *offset);
2699 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002700 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002701
David Milburnc8afd0d2013-05-23 16:23:45 -05002702 kfree(buf);
2703 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002704}
2705
Asai Thambi S P0caff002013-04-03 19:56:21 +05302706static const struct file_operations mtip_device_status_fops = {
2707 .owner = THIS_MODULE,
2708 .open = simple_open,
2709 .read = mtip_hw_read_device_status,
2710 .llseek = no_llseek,
2711};
2712
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002713static const struct file_operations mtip_regs_fops = {
2714 .owner = THIS_MODULE,
2715 .open = simple_open,
2716 .read = mtip_hw_read_registers,
2717 .llseek = no_llseek,
2718};
2719
2720static const struct file_operations mtip_flags_fops = {
2721 .owner = THIS_MODULE,
2722 .open = simple_open,
2723 .read = mtip_hw_read_flags,
2724 .llseek = no_llseek,
2725};
2726
Sam Bradshaw88523a62011-08-30 08:34:26 -06002727/*
2728 * Create the sysfs related attributes.
2729 *
2730 * @dd Pointer to the driver data structure.
2731 * @kobj Pointer to the kobj for the block device.
2732 *
2733 * return value
2734 * 0 Operation completed successfully.
2735 * -EINVAL Invalid parameter.
2736 */
Jens Axboe63166682011-09-27 21:27:43 -06002737static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002738{
2739 if (!kobj || !dd)
2740 return -EINVAL;
2741
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002742 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2743 dev_warn(&dd->pdev->dev,
2744 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002745 return 0;
2746}
2747
2748/*
2749 * Remove the sysfs related attributes.
2750 *
2751 * @dd Pointer to the driver data structure.
2752 * @kobj Pointer to the kobj for the block device.
2753 *
2754 * return value
2755 * 0 Operation completed successfully.
2756 * -EINVAL Invalid parameter.
2757 */
Jens Axboe63166682011-09-27 21:27:43 -06002758static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002759{
2760 if (!kobj || !dd)
2761 return -EINVAL;
2762
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002763 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002764
2765 return 0;
2766}
2767
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002768static int mtip_hw_debugfs_init(struct driver_data *dd)
2769{
2770 if (!dfs_parent)
2771 return -1;
2772
2773 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2774 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2775 dev_warn(&dd->pdev->dev,
2776 "Error creating node %s under debugfs\n",
2777 dd->disk->disk_name);
2778 dd->dfs_node = NULL;
2779 return -1;
2780 }
2781
2782 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2783 &mtip_flags_fops);
2784 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2785 &mtip_regs_fops);
2786
2787 return 0;
2788}
2789
2790static void mtip_hw_debugfs_exit(struct driver_data *dd)
2791{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002792 if (dd->dfs_node)
2793 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002794}
2795
Sam Bradshaw88523a62011-08-30 08:34:26 -06002796/*
2797 * Perform any init/resume time hardware setup
2798 *
2799 * @dd Pointer to the driver data structure.
2800 *
2801 * return value
2802 * None
2803 */
2804static inline void hba_setup(struct driver_data *dd)
2805{
2806 u32 hwdata;
2807 hwdata = readl(dd->mmio + HOST_HSORG);
2808
2809 /* interrupt bug workaround: use only 1 IS bit.*/
2810 writel(hwdata |
2811 HSORG_DISABLE_SLOTGRP_INTR |
2812 HSORG_DISABLE_SLOTGRP_PXIS,
2813 dd->mmio + HOST_HSORG);
2814}
2815
Asai Thambi S P2077d942013-04-29 21:19:49 +02002816static int mtip_device_unaligned_constrained(struct driver_data *dd)
2817{
2818 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2819}
2820
Sam Bradshaw88523a62011-08-30 08:34:26 -06002821/*
2822 * Detect the details of the product, and store anything needed
2823 * into the driver data structure. This includes product type and
2824 * version and number of slot groups.
2825 *
2826 * @dd Pointer to the driver data structure.
2827 *
2828 * return value
2829 * None
2830 */
2831static void mtip_detect_product(struct driver_data *dd)
2832{
2833 u32 hwdata;
2834 unsigned int rev, slotgroups;
2835
2836 /*
2837 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2838 * info register:
2839 * [15:8] hardware/software interface rev#
2840 * [ 3] asic-style interface
2841 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2842 */
2843 hwdata = readl(dd->mmio + HOST_HSORG);
2844
2845 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2846 dd->slot_groups = 1;
2847
2848 if (hwdata & 0x8) {
2849 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2850 rev = (hwdata & HSORG_HWREV) >> 8;
2851 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2852 dev_info(&dd->pdev->dev,
2853 "ASIC-FPGA design, HS rev 0x%x, "
2854 "%i slot groups [%i slots]\n",
2855 rev,
2856 slotgroups,
2857 slotgroups * 32);
2858
2859 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2860 dev_warn(&dd->pdev->dev,
2861 "Warning: driver only supports "
2862 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2863 slotgroups = MTIP_MAX_SLOT_GROUPS;
2864 }
2865 dd->slot_groups = slotgroups;
2866 return;
2867 }
2868
2869 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2870}
2871
2872/*
2873 * Blocking wait for FTL rebuild to complete
2874 *
2875 * @dd Pointer to the DRIVER_DATA structure.
2876 *
2877 * return value
2878 * 0 FTL rebuild completed successfully
2879 * -EFAULT FTL rebuild error/timeout/interruption
2880 */
2881static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2882{
2883 unsigned long timeout, cnt = 0, start;
2884
2885 dev_warn(&dd->pdev->dev,
2886 "FTL rebuild in progress. Polling for completion.\n");
2887
2888 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002889 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2890
2891 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002892 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002893 &dd->dd_flag)))
2894 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002895 if (mtip_check_surprise_removal(dd->pdev))
2896 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002897
Sam Bradshaw88523a62011-08-30 08:34:26 -06002898 if (mtip_get_identify(dd->port, NULL) < 0)
2899 return -EFAULT;
2900
2901 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2902 MTIP_FTL_REBUILD_MAGIC) {
2903 ssleep(1);
2904 /* Print message every 3 minutes */
2905 if (cnt++ >= 180) {
2906 dev_warn(&dd->pdev->dev,
2907 "FTL rebuild in progress (%d secs).\n",
2908 jiffies_to_msecs(jiffies - start) / 1000);
2909 cnt = 0;
2910 }
2911 } else {
2912 dev_warn(&dd->pdev->dev,
2913 "FTL rebuild complete (%d secs).\n",
2914 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002915 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002916 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002917 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002918 } while (time_before(jiffies, timeout));
2919
2920 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002921 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002922 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2923 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002924 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002925}
2926
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002927static void mtip_softirq_done_fn(struct request *rq)
2928{
2929 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2930 struct driver_data *dd = rq->q->queuedata;
2931
2932 /* Unmap the DMA scatter list entries */
2933 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2934 cmd->direction);
2935
2936 if (unlikely(cmd->unaligned))
2937 up(&dd->port->cmd_slot_unal);
2938
2939 blk_mq_end_request(rq, rq->errors);
2940}
2941
2942static void mtip_abort_cmd(struct request *req, void *data,
2943 bool reserved)
2944{
2945 struct driver_data *dd = data;
2946
2947 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2948
2949 clear_bit(req->tag, dd->port->cmds_to_issue);
2950 req->errors = -EIO;
2951 mtip_softirq_done_fn(req);
2952}
2953
2954static void mtip_queue_cmd(struct request *req, void *data,
2955 bool reserved)
2956{
2957 struct driver_data *dd = data;
2958
2959 set_bit(req->tag, dd->port->cmds_to_issue);
2960 blk_abort_request(req);
2961}
2962
Sam Bradshaw88523a62011-08-30 08:34:26 -06002963/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002964 * service thread to issue queued commands
2965 *
2966 * @data Pointer to the driver data structure.
2967 *
2968 * return value
2969 * 0
2970 */
2971
2972static int mtip_service_thread(void *data)
2973{
2974 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002975 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002976 unsigned int num_cmd_slots = dd->slot_groups * 32;
2977 struct mtip_port *port = dd->port;
2978
2979 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002980 if (kthread_should_stop() ||
2981 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2982 goto st_out;
2983 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2984
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002985 /*
2986 * the condition is to check neither an internal command is
2987 * is in progress nor error handling is active
2988 */
2989 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002990 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002991
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002992 if (kthread_should_stop() ||
2993 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2994 goto st_out;
2995
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002996 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002997 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002998 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002999
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08003000 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3001
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003002restart_eh:
3003 /* Demux bits: start with error handling */
3004 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
3005 mtip_handle_tfe(dd);
3006 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
3007 }
3008
3009 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
3010 goto restart_eh;
3011
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003012 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
3013 to = jiffies + msecs_to_jiffies(5000);
3014
3015 do {
3016 mdelay(100);
3017 } while (atomic_read(&dd->irq_workers_active) != 0 &&
3018 time_before(jiffies, to));
3019
3020 if (atomic_read(&dd->irq_workers_active) != 0)
3021 dev_warn(&dd->pdev->dev,
3022 "Completion workers still active!");
3023
3024 spin_lock(dd->queue->queue_lock);
Keith Busch6d125de2016-03-10 13:58:48 +02003025 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003026 mtip_queue_cmd, dd);
3027 spin_unlock(dd->queue->queue_lock);
3028
3029 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
3030
3031 if (mtip_device_reset(dd))
Keith Busch6d125de2016-03-10 13:58:48 +02003032 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003033 mtip_abort_cmd, dd);
3034
3035 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
3036 }
3037
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003038 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003039 slot = 1;
3040 /* used to restrict the loop to one iteration */
3041 slot_start = num_cmd_slots;
3042 slot_wrap = 0;
3043 while (1) {
3044 slot = find_next_bit(port->cmds_to_issue,
3045 num_cmd_slots, slot);
3046 if (slot_wrap == 1) {
3047 if ((slot_start >= slot) ||
3048 (slot >= num_cmd_slots))
3049 break;
3050 }
3051 if (unlikely(slot_start == num_cmd_slots))
3052 slot_start = slot;
3053
3054 if (unlikely(slot == num_cmd_slots)) {
3055 slot = 1;
3056 slot_wrap = 1;
3057 continue;
3058 }
3059
3060 /* Issue the command to the hardware */
3061 mtip_issue_ncq_command(port, slot);
3062
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003063 clear_bit(slot, port->cmds_to_issue);
3064 }
3065
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003066 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003067 }
3068
3069 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003070 if (mtip_ftl_rebuild_poll(dd) == 0)
3071 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003072 }
3073 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003074
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003075st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003076 return 0;
3077}
3078
3079/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003080 * DMA region teardown
3081 *
3082 * @dd Pointer to driver_data structure
3083 *
3084 * return value
3085 * None
3086 */
3087static void mtip_dma_free(struct driver_data *dd)
3088{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003089 struct mtip_port *port = dd->port;
3090
3091 if (port->block1)
3092 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3093 port->block1, port->block1_dma);
3094
3095 if (port->command_list) {
3096 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3097 port->command_list, port->command_list_dma);
3098 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003099}
3100
3101/*
3102 * DMA region setup
3103 *
3104 * @dd Pointer to driver_data structure
3105 *
3106 * return value
3107 * -ENOMEM Not enough free DMA region space to initialize driver
3108 */
3109static int mtip_dma_alloc(struct driver_data *dd)
3110{
3111 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003112
3113 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3114 port->block1 =
3115 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3116 &port->block1_dma, GFP_KERNEL);
3117 if (!port->block1)
3118 return -ENOMEM;
3119 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3120
3121 /* Allocate dma memory for command list */
3122 port->command_list =
3123 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3124 &port->command_list_dma, GFP_KERNEL);
3125 if (!port->command_list) {
3126 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3127 port->block1, port->block1_dma);
3128 port->block1 = NULL;
3129 port->block1_dma = 0;
3130 return -ENOMEM;
3131 }
3132 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3133
3134 /* Setup all pointers into first DMA region */
3135 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3136 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3137 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3138 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3139 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3140 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3141 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3142 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3143
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003144 return 0;
3145}
3146
Jens Axboeffc771b2014-05-09 09:42:02 -06003147static int mtip_hw_get_identify(struct driver_data *dd)
3148{
3149 struct smart_attr attr242;
3150 unsigned char *buf;
3151 int rv;
3152
3153 if (mtip_get_identify(dd->port, NULL) < 0)
3154 return -EFAULT;
3155
3156 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3157 MTIP_FTL_REBUILD_MAGIC) {
3158 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3159 return MTIP_FTL_REBUILD_MAGIC;
3160 }
3161 mtip_dump_identify(dd->port);
3162
3163 /* check write protect, over temp and rebuild statuses */
3164 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3165 dd->port->log_buf,
3166 dd->port->log_buf_dma, 1);
3167 if (rv) {
3168 dev_warn(&dd->pdev->dev,
3169 "Error in READ LOG EXT (10h) command\n");
3170 /* non-critical error, don't fail the load */
3171 } else {
3172 buf = (unsigned char *)dd->port->log_buf;
3173 if (buf[259] & 0x1) {
3174 dev_info(&dd->pdev->dev,
3175 "Write protect bit is set.\n");
3176 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3177 }
3178 if (buf[288] == 0xF7) {
3179 dev_info(&dd->pdev->dev,
3180 "Exceeded Tmax, drive in thermal shutdown.\n");
3181 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3182 }
3183 if (buf[288] == 0xBF) {
3184 dev_info(&dd->pdev->dev,
3185 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003186 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06003187 }
3188 }
3189
3190 /* get write protect progess */
3191 memset(&attr242, 0, sizeof(struct smart_attr));
3192 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3193 dev_warn(&dd->pdev->dev,
3194 "Unable to check write protect progress\n");
3195 else
3196 dev_info(&dd->pdev->dev,
3197 "Write protect progress: %u%% (%u blocks)\n",
3198 attr242.cur, le32_to_cpu(attr242.data));
3199
3200 return rv;
3201}
3202
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003203/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003204 * Called once for each card.
3205 *
3206 * @dd Pointer to the driver data structure.
3207 *
3208 * return value
3209 * 0 on success, else an error code.
3210 */
Jens Axboe63166682011-09-27 21:27:43 -06003211static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003212{
3213 int i;
3214 int rv;
3215 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003216 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003217
3218 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3219
3220 mtip_detect_product(dd);
3221 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3222 rv = -EIO;
3223 goto out1;
3224 }
3225 num_command_slots = dd->slot_groups * 32;
3226
3227 hba_setup(dd);
3228
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003229 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3230 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003231 if (!dd->port) {
3232 dev_err(&dd->pdev->dev,
3233 "Memory allocation: port structure\n");
3234 return -ENOMEM;
3235 }
3236
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003237 /* Continue workqueue setup */
3238 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3239 dd->work[i].port = dd->port;
3240
Asai Thambi S P2077d942013-04-29 21:19:49 +02003241 /* Enable unaligned IO constraints for some devices */
3242 if (mtip_device_unaligned_constrained(dd))
3243 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3244 else
3245 dd->unal_qdepth = 0;
3246
Asai Thambi S P2077d942013-04-29 21:19:49 +02003247 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003248
3249 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003250 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3251 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003252
3253 /* Set the port mmio base address. */
3254 dd->port->mmio = dd->mmio + PORT_OFFSET;
3255 dd->port->dd = dd;
3256
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003257 /* DMA allocations */
3258 rv = mtip_dma_alloc(dd);
3259 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003260 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003261
3262 /* Setup the pointers to the extended s_active and CI registers. */
3263 for (i = 0; i < dd->slot_groups; i++) {
3264 dd->port->s_active[i] =
3265 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3266 dd->port->cmd_issue[i] =
3267 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3268 dd->port->completed[i] =
3269 dd->port->mmio + i*0x80 + PORT_SDBV;
3270 }
3271
Asai Thambi S P45038362012-04-09 08:35:38 +02003272 timetaken = jiffies;
3273 timeout = jiffies + msecs_to_jiffies(30000);
3274 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3275 time_before(jiffies, timeout)) {
3276 mdelay(100);
3277 }
3278 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3279 timetaken = jiffies - timetaken;
3280 dev_warn(&dd->pdev->dev,
3281 "Surprise removal detected at %u ms\n",
3282 jiffies_to_msecs(timetaken));
3283 rv = -ENODEV;
3284 goto out2 ;
3285 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003286 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003287 timetaken = jiffies - timetaken;
3288 dev_warn(&dd->pdev->dev,
3289 "Removal detected at %u ms\n",
3290 jiffies_to_msecs(timetaken));
3291 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003292 goto out2;
3293 }
3294
Asai Thambi S P45038362012-04-09 08:35:38 +02003295 /* Conditionally reset the HBA. */
3296 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3297 if (mtip_hba_reset(dd) < 0) {
3298 dev_err(&dd->pdev->dev,
3299 "Card did not reset within timeout\n");
3300 rv = -EIO;
3301 goto out2;
3302 }
3303 } else {
3304 /* Clear any pending interrupts on the HBA */
3305 writel(readl(dd->mmio + HOST_IRQ_STAT),
3306 dd->mmio + HOST_IRQ_STAT);
3307 }
3308
Sam Bradshaw88523a62011-08-30 08:34:26 -06003309 mtip_init_port(dd->port);
3310 mtip_start_port(dd->port);
3311
3312 /* Setup the ISR and enable interrupts. */
3313 rv = devm_request_irq(&dd->pdev->dev,
3314 dd->pdev->irq,
3315 mtip_irq_handler,
3316 IRQF_SHARED,
3317 dev_driver_string(&dd->pdev->dev),
3318 dd);
3319
3320 if (rv) {
3321 dev_err(&dd->pdev->dev,
3322 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3323 goto out2;
3324 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003325 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003326
3327 /* Enable interrupts on the HBA. */
3328 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3329 dd->mmio + HOST_CTL);
3330
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003331 init_waitqueue_head(&dd->port->svc_wait);
3332
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003333 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003334 rv = -EFAULT;
3335 goto out3;
3336 }
3337
Sam Bradshaw88523a62011-08-30 08:34:26 -06003338 return rv;
3339
3340out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003341 /* Disable interrupts on the HBA. */
3342 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3343 dd->mmio + HOST_CTL);
3344
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003345 /* Release the IRQ. */
3346 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003347 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3348
3349out2:
3350 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003351 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003352
Sam Bradshaw88523a62011-08-30 08:34:26 -06003353out1:
3354 /* Free the memory allocated for the for structure. */
3355 kfree(dd->port);
3356
3357 return rv;
3358}
3359
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003360static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003361{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003362 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003363
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003364 if (dd->sr || !dd->port)
3365 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003366 /*
3367 * Send standby immediate (E0h) to the drive so that it
3368 * saves its state.
3369 */
3370 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003371 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3372 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3373 rv = mtip_standby_immediate(dd->port);
3374 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003375 dev_warn(&dd->pdev->dev,
3376 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003377 }
3378 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003379}
3380
Sam Bradshaw88523a62011-08-30 08:34:26 -06003381/*
3382 * Called to deinitialize an interface.
3383 *
3384 * @dd Pointer to the driver data structure.
3385 *
3386 * return value
3387 * 0
3388 */
Jens Axboe63166682011-09-27 21:27:43 -06003389static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003390{
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003391 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003392 /* de-initialize the port. */
3393 mtip_deinit_port(dd->port);
3394
3395 /* Disable interrupts on the HBA. */
3396 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3397 dd->mmio + HOST_CTL);
3398 }
3399
Sam Bradshaw88523a62011-08-30 08:34:26 -06003400 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003401 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003402 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003403 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003404
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003405 /* Free dma regions */
3406 mtip_dma_free(dd);
3407
Sam Bradshaw88523a62011-08-30 08:34:26 -06003408 /* Free the memory allocated for the for structure. */
3409 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003410 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003411
3412 return 0;
3413}
3414
3415/*
3416 * Issue a Standby Immediate command to the device.
3417 *
3418 * This function is called by the Block Layer just before the
3419 * system powers off during a shutdown.
3420 *
3421 * @dd Pointer to the driver data structure.
3422 *
3423 * return value
3424 * 0
3425 */
Jens Axboe63166682011-09-27 21:27:43 -06003426static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003427{
3428 /*
3429 * Send standby immediate (E0h) to the drive so that it
3430 * saves its state.
3431 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003432 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003433
3434 return 0;
3435}
3436
3437/*
3438 * Suspend function
3439 *
3440 * This function is called by the Block Layer just before the
3441 * system hibernates.
3442 *
3443 * @dd Pointer to the driver data structure.
3444 *
3445 * return value
3446 * 0 Suspend was successful
3447 * -EFAULT Suspend was not successful
3448 */
Jens Axboe63166682011-09-27 21:27:43 -06003449static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003450{
3451 /*
3452 * Send standby immediate (E0h) to the drive
3453 * so that it saves its state.
3454 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003455 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003456 dev_err(&dd->pdev->dev,
3457 "Failed standby-immediate command\n");
3458 return -EFAULT;
3459 }
3460
3461 /* Disable interrupts on the HBA.*/
3462 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3463 dd->mmio + HOST_CTL);
3464 mtip_deinit_port(dd->port);
3465
3466 return 0;
3467}
3468
3469/*
3470 * Resume function
3471 *
3472 * This function is called by the Block Layer as the
3473 * system resumes.
3474 *
3475 * @dd Pointer to the driver data structure.
3476 *
3477 * return value
3478 * 0 Resume was successful
3479 * -EFAULT Resume was not successful
3480 */
Jens Axboe63166682011-09-27 21:27:43 -06003481static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003482{
3483 /* Perform any needed hardware setup steps */
3484 hba_setup(dd);
3485
3486 /* Reset the HBA */
3487 if (mtip_hba_reset(dd) != 0) {
3488 dev_err(&dd->pdev->dev,
3489 "Unable to reset the HBA\n");
3490 return -EFAULT;
3491 }
3492
3493 /*
3494 * Enable the port, DMA engine, and FIS reception specific
3495 * h/w in controller.
3496 */
3497 mtip_init_port(dd->port);
3498 mtip_start_port(dd->port);
3499
3500 /* Enable interrupts on the HBA.*/
3501 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3502 dd->mmio + HOST_CTL);
3503
3504 return 0;
3505}
3506
3507/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003508 * Helper function for reusing disk name
3509 * upon hot insertion.
3510 */
3511static int rssd_disk_name_format(char *prefix,
3512 int index,
3513 char *buf,
3514 int buflen)
3515{
3516 const int base = 'z' - 'a' + 1;
3517 char *begin = buf + strlen(prefix);
3518 char *end = buf + buflen;
3519 char *p;
3520 int unit;
3521
3522 p = end - 1;
3523 *p = '\0';
3524 unit = base;
3525 do {
3526 if (p == begin)
3527 return -EINVAL;
3528 *--p = 'a' + (index % unit);
3529 index = (index / unit) - 1;
3530 } while (index >= 0);
3531
3532 memmove(begin, p, end - p);
3533 memcpy(buf, prefix, strlen(prefix));
3534
3535 return 0;
3536}
3537
3538/*
3539 * Block layer IOCTL handler.
3540 *
3541 * @dev Pointer to the block_device structure.
3542 * @mode ignored
3543 * @cmd IOCTL command passed from the user application.
3544 * @arg Argument passed from the user application.
3545 *
3546 * return value
3547 * 0 IOCTL completed successfully.
3548 * -ENOTTY IOCTL not supported or invalid driver data
3549 * structure pointer.
3550 */
3551static int mtip_block_ioctl(struct block_device *dev,
3552 fmode_t mode,
3553 unsigned cmd,
3554 unsigned long arg)
3555{
3556 struct driver_data *dd = dev->bd_disk->private_data;
3557
3558 if (!capable(CAP_SYS_ADMIN))
3559 return -EACCES;
3560
3561 if (!dd)
3562 return -ENOTTY;
3563
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003564 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003565 return -ENOTTY;
3566
Sam Bradshaw88523a62011-08-30 08:34:26 -06003567 switch (cmd) {
3568 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003569 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003570 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003571 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003572 }
3573}
3574
Jens Axboe16d02c02011-09-27 15:50:01 -06003575#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003576/*
3577 * Block layer compat IOCTL handler.
3578 *
3579 * @dev Pointer to the block_device structure.
3580 * @mode ignored
3581 * @cmd IOCTL command passed from the user application.
3582 * @arg Argument passed from the user application.
3583 *
3584 * return value
3585 * 0 IOCTL completed successfully.
3586 * -ENOTTY IOCTL not supported or invalid driver data
3587 * structure pointer.
3588 */
3589static int mtip_block_compat_ioctl(struct block_device *dev,
3590 fmode_t mode,
3591 unsigned cmd,
3592 unsigned long arg)
3593{
3594 struct driver_data *dd = dev->bd_disk->private_data;
3595
3596 if (!capable(CAP_SYS_ADMIN))
3597 return -EACCES;
3598
3599 if (!dd)
3600 return -ENOTTY;
3601
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003602 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003603 return -ENOTTY;
3604
Sam Bradshaw88523a62011-08-30 08:34:26 -06003605 switch (cmd) {
3606 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003607 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003608 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003609 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003610 ide_task_request_t req_task;
3611 int compat_tasksize, outtotal, ret;
3612
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003613 compat_tasksize =
3614 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003615
3616 compat_req_task =
3617 (struct mtip_compat_ide_task_request_s __user *) arg;
3618
3619 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003620 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003621 return -EFAULT;
3622
3623 if (get_user(req_task.out_size, &compat_req_task->out_size))
3624 return -EFAULT;
3625
3626 if (get_user(req_task.in_size, &compat_req_task->in_size))
3627 return -EFAULT;
3628
3629 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3630
3631 ret = exec_drive_taskfile(dd, (void __user *) arg,
3632 &req_task, outtotal);
3633
3634 if (copy_to_user((void __user *) arg, &req_task,
3635 compat_tasksize -
3636 (2 * sizeof(compat_long_t))))
3637 return -EFAULT;
3638
3639 if (put_user(req_task.out_size, &compat_req_task->out_size))
3640 return -EFAULT;
3641
3642 if (put_user(req_task.in_size, &compat_req_task->in_size))
3643 return -EFAULT;
3644
3645 return ret;
3646 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003647 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003648 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003649 }
3650}
Jens Axboe16d02c02011-09-27 15:50:01 -06003651#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003652
3653/*
3654 * Obtain the geometry of the device.
3655 *
3656 * You may think that this function is obsolete, but some applications,
3657 * fdisk for example still used CHS values. This function describes the
3658 * device as having 224 heads and 56 sectors per cylinder. These values are
3659 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3660 * partition is described in terms of a start and end cylinder this means
3661 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3662 * affects performance.
3663 *
3664 * @dev Pointer to the block_device strucutre.
3665 * @geo Pointer to a hd_geometry structure.
3666 *
3667 * return value
3668 * 0 Operation completed successfully.
3669 * -ENOTTY An error occurred while reading the drive capacity.
3670 */
3671static int mtip_block_getgeo(struct block_device *dev,
3672 struct hd_geometry *geo)
3673{
3674 struct driver_data *dd = dev->bd_disk->private_data;
3675 sector_t capacity;
3676
3677 if (!dd)
3678 return -ENOTTY;
3679
3680 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3681 dev_warn(&dd->pdev->dev,
3682 "Could not get drive capacity.\n");
3683 return -ENOTTY;
3684 }
3685
3686 geo->heads = 224;
3687 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003688 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003689 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003690 return 0;
3691}
3692
Asai Thambi SP51c65702016-02-24 21:18:10 -08003693static int mtip_block_open(struct block_device *dev, fmode_t mode)
3694{
3695 struct driver_data *dd;
3696
3697 if (dev && dev->bd_disk) {
3698 dd = (struct driver_data *) dev->bd_disk->private_data;
3699
3700 if (dd) {
3701 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3702 &dd->dd_flag)) {
3703 return -ENODEV;
3704 }
3705 return 0;
3706 }
3707 }
3708 return -ENODEV;
3709}
3710
Baoyou Xie99e6b872016-08-26 14:08:53 +08003711static void mtip_block_release(struct gendisk *disk, fmode_t mode)
Asai Thambi SP51c65702016-02-24 21:18:10 -08003712{
3713}
3714
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715/*
3716 * Block device operation function.
3717 *
3718 * This structure contains pointers to the functions required by the block
3719 * layer.
3720 */
3721static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003722 .open = mtip_block_open,
3723 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003724 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003725#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003726 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003727#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003728 .getgeo = mtip_block_getgeo,
3729 .owner = THIS_MODULE
3730};
3731
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003732static inline bool is_se_active(struct driver_data *dd)
3733{
3734 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3735 if (dd->port->ic_pause_timer) {
3736 unsigned long to = dd->port->ic_pause_timer +
3737 msecs_to_jiffies(1000);
3738 if (time_after(jiffies, to)) {
3739 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3740 &dd->port->flags);
3741 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3742 dd->port->ic_pause_timer = 0;
3743 wake_up_interruptible(&dd->port->svc_wait);
3744 return false;
3745 }
3746 }
3747 return true;
3748 }
3749 return false;
3750}
3751
Sam Bradshaw88523a62011-08-30 08:34:26 -06003752/*
3753 * Block layer make request function.
3754 *
3755 * This function is called by the kernel to process a BIO for
3756 * the P320 device.
3757 *
3758 * @queue Pointer to the request queue. Unused other than to obtain
3759 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003760 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003761 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003762 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003763static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003764{
Jens Axboeffc771b2014-05-09 09:42:02 -06003765 struct driver_data *dd = hctx->queue->queuedata;
3766 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3767 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003768
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003769 if (is_se_active(dd))
3770 return -ENODATA;
3771
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003772 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3773 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3774 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003775 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003776 }
3777 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003778 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003779 }
3780 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3781 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003782 rq_data_dir(rq))) {
3783 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003784 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003785 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3786 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003787 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003788 }
3789
Mike Christiec2df40d2016-06-05 14:32:17 -05003790 if (req_op(rq) == REQ_OP_DISCARD) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003791 int err;
3792
3793 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003794 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003795 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003796 }
3797
Jens Axboeffc771b2014-05-09 09:42:02 -06003798 /* Create the scatter list for this request. */
3799 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003800
Jens Axboeffc771b2014-05-09 09:42:02 -06003801 /* Issue the read/write. */
3802 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3803 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003804}
3805
Jens Axboeffc771b2014-05-09 09:42:02 -06003806static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3807 struct request *rq)
3808{
3809 struct driver_data *dd = hctx->queue->queuedata;
3810 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3811
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003812 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003813 return false;
3814
3815 /*
3816 * If unaligned depth must be limited on this controller, mark it
3817 * as unaligned if the IO isn't on a 4k boundary (start of length).
3818 */
3819 if (blk_rq_sectors(rq) <= 64) {
3820 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3821 cmd->unaligned = 1;
3822 }
3823
3824 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3825 return true;
3826
3827 return false;
3828}
3829
Jens Axboe74c45052014-10-29 11:14:52 -06003830static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3831 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003832{
Jens Axboe74c45052014-10-29 11:14:52 -06003833 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003834 int ret;
3835
Ming Leidb54fac2017-04-27 07:45:18 -06003836 mtip_init_cmd_header(rq);
3837
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003838 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003839 return BLK_MQ_RQ_QUEUE_BUSY;
3840
Christoph Hellwige2490072014-09-13 16:40:09 -07003841 blk_mq_start_request(rq);
3842
Jens Axboeffc771b2014-05-09 09:42:02 -06003843 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003844 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003845 return BLK_MQ_RQ_QUEUE_OK;
3846
3847 rq->errors = ret;
3848 return BLK_MQ_RQ_QUEUE_ERROR;
3849}
3850
3851static void mtip_free_cmd(void *data, struct request *rq,
3852 unsigned int hctx_idx, unsigned int request_idx)
3853{
3854 struct driver_data *dd = data;
3855 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3856
3857 if (!cmd->command)
3858 return;
3859
3860 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3861 cmd->command, cmd->command_dma);
3862}
3863
3864static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3865 unsigned int request_idx, unsigned int numa_node)
3866{
3867 struct driver_data *dd = data;
3868 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003869
Jeff Moyer74c9c912015-07-29 10:22:50 -04003870 /*
3871 * For flush requests, request_idx starts at the end of the
3872 * tag space. Since we don't support FLUSH/FUA, simply return
3873 * 0 as there's nothing to be done.
3874 */
3875 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3876 return 0;
3877
Jens Axboeffc771b2014-05-09 09:42:02 -06003878 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3879 &cmd->command_dma, GFP_KERNEL);
3880 if (!cmd->command)
3881 return -ENOMEM;
3882
3883 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3884
Jens Axboeffc771b2014-05-09 09:42:02 -06003885 sg_init_table(cmd->sg, MTIP_MAX_SG);
3886 return 0;
3887}
3888
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003889static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3890 bool reserved)
3891{
3892 struct driver_data *dd = req->q->queuedata;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003893
3894 if (reserved)
3895 goto exit_handler;
3896
3897 if (test_bit(req->tag, dd->port->cmds_to_issue))
3898 goto exit_handler;
3899
3900 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3901 goto exit_handler;
3902
3903 wake_up_interruptible(&dd->port->svc_wait);
3904exit_handler:
Jens Axboe90beb2e2016-03-04 08:15:48 -07003905 return BLK_EH_RESET_TIMER;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003906}
3907
Jens Axboeffc771b2014-05-09 09:42:02 -06003908static struct blk_mq_ops mtip_mq_ops = {
3909 .queue_rq = mtip_queue_rq,
Jens Axboeffc771b2014-05-09 09:42:02 -06003910 .init_request = mtip_init_cmd,
3911 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003912 .complete = mtip_softirq_done_fn,
3913 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003914};
3915
Sam Bradshaw88523a62011-08-30 08:34:26 -06003916/*
3917 * Block layer initialization function.
3918 *
3919 * This function is called once by the PCI layer for each P320
3920 * device that is connected to the system.
3921 *
3922 * @dd Pointer to the driver data structure.
3923 *
3924 * return value
3925 * 0 on success else an error code.
3926 */
Jens Axboe63166682011-09-27 21:27:43 -06003927static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003928{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003929 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003930 sector_t capacity;
3931 unsigned int index = 0;
3932 struct kobject *kobj;
3933
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003934 if (dd->disk)
3935 goto skip_create_disk; /* hw init done, before rebuild */
3936
Jens Axboeffc771b2014-05-09 09:42:02 -06003937 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003938 rv = -EINVAL;
3939 goto protocol_init_error;
3940 }
3941
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003942 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003943 if (dd->disk == NULL) {
3944 dev_err(&dd->pdev->dev,
3945 "Unable to allocate gendisk structure\n");
3946 rv = -EINVAL;
3947 goto alloc_disk_error;
3948 }
3949
3950 /* Generate the disk name, implemented same as in sd.c */
3951 do {
3952 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3953 goto ida_get_error;
3954
3955 spin_lock(&rssd_index_lock);
3956 rv = ida_get_new(&rssd_index_ida, &index);
3957 spin_unlock(&rssd_index_lock);
3958 } while (rv == -EAGAIN);
3959
3960 if (rv)
3961 goto ida_get_error;
3962
3963 rv = rssd_disk_name_format("rssd",
3964 index,
3965 dd->disk->disk_name,
3966 DISK_NAME_LEN);
3967 if (rv)
3968 goto disk_index_error;
3969
Sam Bradshaw88523a62011-08-30 08:34:26 -06003970 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003971 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3972 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003973 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003974 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003975 dd->index = index;
3976
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003977 mtip_hw_debugfs_init(dd);
3978
Jens Axboeffc771b2014-05-09 09:42:02 -06003979 memset(&dd->tags, 0, sizeof(dd->tags));
3980 dd->tags.ops = &mtip_mq_ops;
3981 dd->tags.nr_hw_queues = 1;
3982 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3983 dd->tags.reserved_tags = 1;
3984 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3985 dd->tags.numa_node = dd->numa_node;
3986 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3987 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003988 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003989
3990 rv = blk_mq_alloc_tag_set(&dd->tags);
3991 if (rv) {
3992 dev_err(&dd->pdev->dev,
3993 "Unable to allocate request queue\n");
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003994 goto block_queue_alloc_tag_error;
Jens Axboeffc771b2014-05-09 09:42:02 -06003995 }
3996
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003997 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003998 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003999 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004000 dev_err(&dd->pdev->dev,
4001 "Unable to allocate request queue\n");
4002 rv = -ENOMEM;
4003 goto block_queue_alloc_init_error;
4004 }
4005
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004006 dd->disk->queue = dd->queue;
4007 dd->queue->queuedata = dd;
4008
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004009skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06004010 /* Initialize the protocol layer. */
4011 wait_for_rebuild = mtip_hw_get_identify(dd);
4012 if (wait_for_rebuild < 0) {
4013 dev_err(&dd->pdev->dev,
4014 "Protocol layer initialization failed\n");
4015 rv = -EINVAL;
4016 goto init_hw_cmds_error;
4017 }
4018
4019 /*
4020 * if rebuild pending, start the service thread, and delay the block
Dan Williams0d52c7562016-06-15 19:44:20 -07004021 * queue creation and device_add_disk()
Jens Axboeffc771b2014-05-09 09:42:02 -06004022 */
4023 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4024 goto start_service_thread;
4025
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004026 /* Set device limits. */
4027 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06004028 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004029 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4030 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004031 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4032 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004033 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00004034 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004035
Asai Thambi S P15283462013-01-11 14:41:34 +01004036 /* Signal trim support */
4037 if (dd->trim_supp == true) {
4038 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4039 dd->queue->limits.discard_granularity = 4096;
4040 blk_queue_max_discard_sectors(dd->queue,
4041 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4042 dd->queue->limits.discard_zeroes_data = 0;
4043 }
4044
Sam Bradshaw88523a62011-08-30 08:34:26 -06004045 /* Set the capacity of the device in 512 byte sectors. */
4046 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4047 dev_warn(&dd->pdev->dev,
4048 "Could not read drive capacity\n");
4049 rv = -EIO;
4050 goto read_capacity_error;
4051 }
4052 set_capacity(dd->disk, capacity);
4053
4054 /* Enable the block device and add it to /dev */
Dan Williams0d52c7562016-06-15 19:44:20 -07004055 device_add_disk(&dd->pdev->dev, dd->disk);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004056
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004057 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004058 /*
4059 * Now that the disk is active, initialize any sysfs attributes
4060 * managed by the protocol layer.
4061 */
4062 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4063 if (kobj) {
4064 mtip_hw_sysfs_init(dd, kobj);
4065 kobject_put(kobj);
4066 }
4067
Asai Thambi S P45038362012-04-09 08:35:38 +02004068 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004069 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004070 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004071 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004072
4073start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004074 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01004075 dd, dd->numa_node,
4076 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004077
4078 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004079 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004080 dd->mtip_svc_handler = NULL;
4081 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004082 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004083 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004084 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004085 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4086 rv = wait_for_rebuild;
4087
Sam Bradshaw88523a62011-08-30 08:34:26 -06004088 return rv;
4089
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004090kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004091 bdput(dd->bdev);
4092 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004093
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004094 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004095 del_gendisk(dd->disk);
4096
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004097read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004098init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004099 blk_cleanup_queue(dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004100block_queue_alloc_init_error:
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004101 blk_mq_free_tag_set(&dd->tags);
4102block_queue_alloc_tag_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004103 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004104disk_index_error:
4105 spin_lock(&rssd_index_lock);
4106 ida_remove(&rssd_index_ida, index);
4107 spin_unlock(&rssd_index_lock);
4108
4109ida_get_error:
4110 put_disk(dd->disk);
4111
4112alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004113 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004114
4115protocol_init_error:
4116 return rv;
4117}
4118
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004119static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
4120{
4121 struct driver_data *dd = (struct driver_data *)data;
4122 struct mtip_cmd *cmd;
4123
4124 if (likely(!reserv))
4125 blk_mq_complete_request(rq, -ENODEV);
4126 else if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &dd->port->flags)) {
4127
4128 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
4129 if (cmd->comp_func)
4130 cmd->comp_func(dd->port, MTIP_TAG_INTERNAL,
4131 cmd, -ENODEV);
4132 }
4133}
4134
Sam Bradshaw88523a62011-08-30 08:34:26 -06004135/*
4136 * Block layer deinitialization function.
4137 *
4138 * Called by the PCI layer as each P320 device is removed.
4139 *
4140 * @dd Pointer to the driver data structure.
4141 *
4142 * return value
4143 * 0
4144 */
Jens Axboe63166682011-09-27 21:27:43 -06004145static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004146{
4147 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004148
Asai Thambi SP2132a542015-05-11 15:53:18 -07004149 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004150
Asai Thambi SP2132a542015-05-11 15:53:18 -07004151 if (dd->mtip_svc_handler) {
4152 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4153 wake_up_interruptible(&dd->port->svc_wait);
4154 kthread_stop(dd->mtip_svc_handler);
4155 }
4156
4157 /* Clean up the sysfs attributes, if created */
4158 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4159 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4160 if (kobj) {
4161 mtip_hw_sysfs_exit(dd, kobj);
4162 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004163 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004164 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004165
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004166 if (!dd->sr) {
4167 /*
4168 * Explicitly wait here for IOs to quiesce,
4169 * as mtip_standby_drive usually won't wait for IOs.
4170 */
4171 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS,
4172 GFP_KERNEL))
4173 mtip_standby_drive(dd);
4174 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004175 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004176 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4177 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004178
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004179 blk_mq_freeze_queue_start(dd->queue);
4180 blk_mq_stop_hw_queues(dd->queue);
Keith Busch6d125de2016-03-10 13:58:48 +02004181 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004182
Asai Thambi SP2132a542015-05-11 15:53:18 -07004183 /*
4184 * Delete our gendisk structure. This also removes the device
4185 * from /dev
4186 */
4187 if (dd->bdev) {
4188 bdput(dd->bdev);
4189 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004190 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004191 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004192 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4193 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004194 if (dd->disk->queue) {
4195 blk_cleanup_queue(dd->queue);
4196 blk_mq_free_tag_set(&dd->tags);
4197 dd->queue = NULL;
4198 }
4199 put_disk(dd->disk);
4200 }
4201 dd->disk = NULL;
4202
4203 spin_lock(&rssd_index_lock);
4204 ida_remove(&rssd_index_ida, dd->index);
4205 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004206
4207 /* De-initialize the protocol layer. */
4208 mtip_hw_exit(dd);
4209
4210 return 0;
4211}
4212
4213/*
4214 * Function called by the PCI layer when just before the
4215 * machine shuts down.
4216 *
4217 * If a protocol layer shutdown function is present it will be called
4218 * by this function.
4219 *
4220 * @dd Pointer to the driver data structure.
4221 *
4222 * return value
4223 * 0
4224 */
Jens Axboe63166682011-09-27 21:27:43 -06004225static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004226{
Jens Axboeffc771b2014-05-09 09:42:02 -06004227 mtip_hw_shutdown(dd);
4228
Sam Bradshaw88523a62011-08-30 08:34:26 -06004229 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304230 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304231 dev_info(&dd->pdev->dev,
4232 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304233
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004234 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4235 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304236 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304237 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004238 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004239 }
4240 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304241 dd->disk = NULL;
4242 dd->queue = NULL;
4243 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004244
4245 spin_lock(&rssd_index_lock);
4246 ida_remove(&rssd_index_ida, dd->index);
4247 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004248 return 0;
4249}
4250
Jens Axboe63166682011-09-27 21:27:43 -06004251static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004252{
4253 dev_info(&dd->pdev->dev,
4254 "Suspending %s ...\n", dd->disk->disk_name);
4255 mtip_hw_suspend(dd);
4256 return 0;
4257}
4258
Jens Axboe63166682011-09-27 21:27:43 -06004259static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004260{
4261 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4262 dd->disk->disk_name);
4263 mtip_hw_resume(dd);
4264 return 0;
4265}
4266
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004267static void drop_cpu(int cpu)
4268{
4269 cpu_use[cpu]--;
4270}
4271
4272static int get_least_used_cpu_on_node(int node)
4273{
4274 int cpu, least_used_cpu, least_cnt;
4275 const struct cpumask *node_mask;
4276
4277 node_mask = cpumask_of_node(node);
4278 least_used_cpu = cpumask_first(node_mask);
4279 least_cnt = cpu_use[least_used_cpu];
4280 cpu = least_used_cpu;
4281
4282 for_each_cpu(cpu, node_mask) {
4283 if (cpu_use[cpu] < least_cnt) {
4284 least_used_cpu = cpu;
4285 least_cnt = cpu_use[cpu];
4286 }
4287 }
4288 cpu_use[least_used_cpu]++;
4289 return least_used_cpu;
4290}
4291
4292/* Helper for selecting a node in round robin mode */
4293static inline int mtip_get_next_rr_node(void)
4294{
4295 static int next_node = -1;
4296
4297 if (next_node == -1) {
4298 next_node = first_online_node;
4299 return next_node;
4300 }
4301
4302 next_node = next_online_node(next_node);
4303 if (next_node == MAX_NUMNODES)
4304 next_node = first_online_node;
4305 return next_node;
4306}
4307
Fengguang Wu25bac122013-01-12 15:31:40 +08004308static DEFINE_HANDLER(0);
4309static DEFINE_HANDLER(1);
4310static DEFINE_HANDLER(2);
4311static DEFINE_HANDLER(3);
4312static DEFINE_HANDLER(4);
4313static DEFINE_HANDLER(5);
4314static DEFINE_HANDLER(6);
4315static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004316
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004317static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4318{
4319 int pos;
4320 unsigned short pcie_dev_ctrl;
4321
4322 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4323 if (pos) {
4324 pci_read_config_word(pdev,
4325 pos + PCI_EXP_DEVCTL,
4326 &pcie_dev_ctrl);
4327 if (pcie_dev_ctrl & (1 << 11) ||
4328 pcie_dev_ctrl & (1 << 4)) {
4329 dev_info(&dd->pdev->dev,
4330 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4331 pdev->vendor, pdev->device);
4332 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4333 PCI_EXP_DEVCTL_RELAX_EN);
4334 pci_write_config_word(pdev,
4335 pos + PCI_EXP_DEVCTL,
4336 pcie_dev_ctrl);
4337 }
4338 }
4339}
4340
4341static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4342{
4343 /*
4344 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4345 * device with device id 0x5aXX
4346 */
4347 if (pdev->bus && pdev->bus->self) {
4348 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4349 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4350 mtip_disable_link_opts(dd, pdev->bus->self);
4351 } else {
4352 /* Check further up the topology */
4353 struct pci_dev *parent_dev = pdev->bus->self;
4354 if (parent_dev->bus &&
4355 parent_dev->bus->parent &&
4356 parent_dev->bus->parent->self &&
4357 parent_dev->bus->parent->self->vendor ==
4358 PCI_VENDOR_ID_ATI &&
4359 (parent_dev->bus->parent->self->device &
4360 0xff00) == 0x5a00) {
4361 mtip_disable_link_opts(dd,
4362 parent_dev->bus->parent->self);
4363 }
4364 }
4365 }
4366}
4367
Sam Bradshaw88523a62011-08-30 08:34:26 -06004368/*
4369 * Called for each supported PCI device detected.
4370 *
4371 * This function allocates the private data structure, enables the
4372 * PCI device and then calls the block layer initialization function.
4373 *
4374 * return value
4375 * 0 on success else an error code.
4376 */
4377static int mtip_pci_probe(struct pci_dev *pdev,
4378 const struct pci_device_id *ent)
4379{
4380 int rv = 0;
4381 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004382 char cpu_list[256];
4383 const struct cpumask *node_mask;
4384 int cpu, i = 0, j = 0;
4385 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304386 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004387
4388 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004389 my_node = pcibus_to_node(pdev->bus);
4390 if (my_node != NUMA_NO_NODE) {
4391 if (!node_online(my_node))
4392 my_node = mtip_get_next_rr_node();
4393 } else {
4394 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4395 my_node = mtip_get_next_rr_node();
4396 }
4397 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4398 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004399 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004400
4401 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004402 if (dd == NULL) {
4403 dev_err(&pdev->dev,
4404 "Unable to allocate memory for driver data\n");
4405 return -ENOMEM;
4406 }
4407
Sam Bradshaw88523a62011-08-30 08:34:26 -06004408 /* Attach the private data to this PCI device. */
4409 pci_set_drvdata(pdev, dd);
4410
4411 rv = pcim_enable_device(pdev);
4412 if (rv < 0) {
4413 dev_err(&pdev->dev, "Unable to enable device\n");
4414 goto iomap_err;
4415 }
4416
4417 /* Map BAR5 to memory. */
4418 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4419 if (rv < 0) {
4420 dev_err(&pdev->dev, "Unable to map regions\n");
4421 goto iomap_err;
4422 }
4423
4424 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4425 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4426
4427 if (rv) {
4428 rv = pci_set_consistent_dma_mask(pdev,
4429 DMA_BIT_MASK(32));
4430 if (rv) {
4431 dev_warn(&pdev->dev,
4432 "64-bit DMA enable failed\n");
4433 goto setmask_err;
4434 }
4435 }
4436 }
4437
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004438 /* Copy the info we may need later into the private data structure. */
4439 dd->major = mtip_major;
4440 dd->instance = instance;
4441 dd->pdev = pdev;
4442 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004443
Asai Thambi S P0caff002013-04-03 19:56:21 +05304444 INIT_LIST_HEAD(&dd->online_list);
4445 INIT_LIST_HEAD(&dd->remove_list);
4446
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004447 memset(dd->workq_name, 0, 32);
4448 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4449
4450 dd->isr_workq = create_workqueue(dd->workq_name);
4451 if (!dd->isr_workq) {
4452 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004453 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004454 goto block_initialize_err;
4455 }
4456
4457 memset(cpu_list, 0, sizeof(cpu_list));
4458
4459 node_mask = cpumask_of_node(dd->numa_node);
4460 if (!cpumask_empty(node_mask)) {
4461 for_each_cpu(cpu, node_mask)
4462 {
4463 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4464 j = strlen(cpu_list);
4465 }
4466
4467 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4468 dd->numa_node,
4469 topology_physical_package_id(cpumask_first(node_mask)),
4470 nr_cpus_node(dd->numa_node),
4471 cpu_list);
4472 } else
4473 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4474
4475 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4476 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4477 cpu_to_node(dd->isr_binding), dd->isr_binding);
4478
4479 /* first worker context always runs in ISR */
4480 dd->work[0].cpu_binding = dd->isr_binding;
4481 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4482 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4483 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4484 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4485 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4486 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4487 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4488
4489 /* Log the bindings */
4490 for_each_present_cpu(cpu) {
4491 memset(cpu_list, 0, sizeof(cpu_list));
4492 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4493 if (dd->work[i].cpu_binding == cpu) {
4494 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4495 j = strlen(cpu_list);
4496 }
4497 }
4498 if (j)
4499 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4500 }
4501
4502 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4503 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4504 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4505 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4506 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4507 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4508 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4509 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4510
4511 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004512 rv = pci_enable_msi(pdev);
4513 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004514 dev_warn(&pdev->dev,
4515 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004516 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004517 }
4518
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004519 mtip_fix_ero_nosnoop(dd, pdev);
4520
Sam Bradshaw88523a62011-08-30 08:34:26 -06004521 /* Initialize the block layer. */
4522 rv = mtip_block_initialize(dd);
4523 if (rv < 0) {
4524 dev_err(&pdev->dev,
4525 "Unable to initialize block layer\n");
4526 goto block_initialize_err;
4527 }
4528
4529 /*
4530 * Increment the instance count so that each device has a unique
4531 * instance number.
4532 */
4533 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004534 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004535 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304536 else
4537 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304538
4539 /* Add to online list even if in ftl rebuild */
4540 spin_lock_irqsave(&dev_lock, flags);
4541 list_add(&dd->online_list, &online_list);
4542 spin_unlock_irqrestore(&dev_lock, flags);
4543
Sam Bradshaw88523a62011-08-30 08:34:26 -06004544 goto done;
4545
4546block_initialize_err:
4547 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004548
4549msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004550 if (dd->isr_workq) {
4551 flush_workqueue(dd->isr_workq);
4552 destroy_workqueue(dd->isr_workq);
4553 drop_cpu(dd->work[0].cpu_binding);
4554 drop_cpu(dd->work[1].cpu_binding);
4555 drop_cpu(dd->work[2].cpu_binding);
4556 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004557setmask_err:
4558 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4559
4560iomap_err:
4561 kfree(dd);
4562 pci_set_drvdata(pdev, NULL);
4563 return rv;
4564done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004565 return rv;
4566}
4567
4568/*
4569 * Called for each probed device when the device is removed or the
4570 * driver is unloaded.
4571 *
4572 * return value
4573 * None
4574 */
4575static void mtip_pci_remove(struct pci_dev *pdev)
4576{
4577 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004578 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004579
Asai Thambi SP51c65702016-02-24 21:18:10 -08004580 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004581
Asai Thambi S P0caff002013-04-03 19:56:21 +05304582 spin_lock_irqsave(&dev_lock, flags);
4583 list_del_init(&dd->online_list);
4584 list_add(&dd->remove_list, &removing_list);
4585 spin_unlock_irqrestore(&dev_lock, flags);
4586
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004587 mtip_check_surprise_removal(pdev);
4588 synchronize_irq(dd->pdev->irq);
4589
4590 /* Spin until workers are done */
4591 to = jiffies + msecs_to_jiffies(4000);
4592 do {
4593 msleep(20);
4594 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4595 time_before(jiffies, to));
4596
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004597 if (!dd->sr)
4598 fsync_bdev(dd->bdev);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004599
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004600 if (atomic_read(&dd->irq_workers_active) != 0) {
4601 dev_warn(&dd->pdev->dev,
4602 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004603 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004604
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004605 blk_set_queue_dying(dd->queue);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004606 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4607
Sam Bradshaw88523a62011-08-30 08:34:26 -06004608 /* Clean up the block layer. */
4609 mtip_block_remove(dd);
4610
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004611 if (dd->isr_workq) {
4612 flush_workqueue(dd->isr_workq);
4613 destroy_workqueue(dd->isr_workq);
4614 drop_cpu(dd->work[0].cpu_binding);
4615 drop_cpu(dd->work[1].cpu_binding);
4616 drop_cpu(dd->work[2].cpu_binding);
4617 }
4618
Sam Bradshaw88523a62011-08-30 08:34:26 -06004619 pci_disable_msi(pdev);
4620
Asai Thambi S P0caff002013-04-03 19:56:21 +05304621 spin_lock_irqsave(&dev_lock, flags);
4622 list_del_init(&dd->remove_list);
4623 spin_unlock_irqrestore(&dev_lock, flags);
4624
Asai Thambi SP2132a542015-05-11 15:53:18 -07004625 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004626
Sam Bradshaw88523a62011-08-30 08:34:26 -06004627 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004628 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004629}
4630
4631/*
4632 * Called for each probed device when the device is suspended.
4633 *
4634 * return value
4635 * 0 Success
4636 * <0 Error
4637 */
4638static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4639{
4640 int rv = 0;
4641 struct driver_data *dd = pci_get_drvdata(pdev);
4642
4643 if (!dd) {
4644 dev_err(&pdev->dev,
4645 "Driver private datastructure is NULL\n");
4646 return -EFAULT;
4647 }
4648
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004649 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004650
4651 /* Disable ports & interrupts then send standby immediate */
4652 rv = mtip_block_suspend(dd);
4653 if (rv < 0) {
4654 dev_err(&pdev->dev,
4655 "Failed to suspend controller\n");
4656 return rv;
4657 }
4658
4659 /*
4660 * Save the pci config space to pdev structure &
4661 * disable the device
4662 */
4663 pci_save_state(pdev);
4664 pci_disable_device(pdev);
4665
4666 /* Move to Low power state*/
4667 pci_set_power_state(pdev, PCI_D3hot);
4668
4669 return rv;
4670}
4671
4672/*
4673 * Called for each probed device when the device is resumed.
4674 *
4675 * return value
4676 * 0 Success
4677 * <0 Error
4678 */
4679static int mtip_pci_resume(struct pci_dev *pdev)
4680{
4681 int rv = 0;
4682 struct driver_data *dd;
4683
4684 dd = pci_get_drvdata(pdev);
4685 if (!dd) {
4686 dev_err(&pdev->dev,
4687 "Driver private datastructure is NULL\n");
4688 return -EFAULT;
4689 }
4690
4691 /* Move the device to active State */
4692 pci_set_power_state(pdev, PCI_D0);
4693
4694 /* Restore PCI configuration space */
4695 pci_restore_state(pdev);
4696
4697 /* Enable the PCI device*/
4698 rv = pcim_enable_device(pdev);
4699 if (rv < 0) {
4700 dev_err(&pdev->dev,
4701 "Failed to enable card during resume\n");
4702 goto err;
4703 }
4704 pci_set_master(pdev);
4705
4706 /*
4707 * Calls hbaReset, initPort, & startPort function
4708 * then enables interrupts
4709 */
4710 rv = mtip_block_resume(dd);
4711 if (rv < 0)
4712 dev_err(&pdev->dev, "Unable to resume\n");
4713
4714err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004715 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004716
4717 return rv;
4718}
4719
4720/*
4721 * Shutdown routine
4722 *
4723 * return value
4724 * None
4725 */
4726static void mtip_pci_shutdown(struct pci_dev *pdev)
4727{
4728 struct driver_data *dd = pci_get_drvdata(pdev);
4729 if (dd)
4730 mtip_block_shutdown(dd);
4731}
4732
Sam Bradshaw88523a62011-08-30 08:34:26 -06004733/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004734static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304735 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4736 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4737 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4738 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4739 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4740 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4741 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004742 { 0 }
4743};
4744
4745/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004746static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004747 .name = MTIP_DRV_NAME,
4748 .id_table = mtip_pci_tbl,
4749 .probe = mtip_pci_probe,
4750 .remove = mtip_pci_remove,
4751 .suspend = mtip_pci_suspend,
4752 .resume = mtip_pci_resume,
4753 .shutdown = mtip_pci_shutdown,
4754};
4755
4756MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4757
4758/*
4759 * Module initialization function.
4760 *
4761 * Called once when the module is loaded. This function allocates a major
4762 * block device number to the Cyclone devices and registers the PCI layer
4763 * of the driver.
4764 *
4765 * Return value
4766 * 0 on success else error code.
4767 */
4768static int __init mtip_init(void)
4769{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004770 int error;
4771
Asai Thambi S P45422e72012-09-05 22:03:56 +05304772 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004773
Asai Thambi S P0caff002013-04-03 19:56:21 +05304774 spin_lock_init(&dev_lock);
4775
4776 INIT_LIST_HEAD(&online_list);
4777 INIT_LIST_HEAD(&removing_list);
4778
Sam Bradshaw88523a62011-08-30 08:34:26 -06004779 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004780 error = register_blkdev(0, MTIP_DRV_NAME);
4781 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304782 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004783 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004784 return -EBUSY;
4785 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004786 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004787
Asai Thambi S P0caff002013-04-03 19:56:21 +05304788 dfs_parent = debugfs_create_dir("rssd", NULL);
4789 if (IS_ERR_OR_NULL(dfs_parent)) {
4790 pr_warn("Error creating debugfs parent\n");
4791 dfs_parent = NULL;
4792 }
4793 if (dfs_parent) {
4794 dfs_device_status = debugfs_create_file("device_status",
4795 S_IRUGO, dfs_parent, NULL,
4796 &mtip_device_status_fops);
4797 if (IS_ERR_OR_NULL(dfs_device_status)) {
4798 pr_err("Error creating device_status node\n");
4799 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004800 }
4801 }
4802
Sam Bradshaw88523a62011-08-30 08:34:26 -06004803 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004804 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004805 if (error) {
4806 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004807 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004808 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004809
4810 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004811}
4812
4813/*
4814 * Module de-initialization function.
4815 *
4816 * Called once when the module is unloaded. This function deallocates
4817 * the major block device number allocated by mtip_init() and
4818 * unregisters the PCI layer of the driver.
4819 *
4820 * Return value
4821 * none
4822 */
4823static void __exit mtip_exit(void)
4824{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004825 /* Release the allocated major block device number. */
4826 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4827
4828 /* Unregister the PCI driver. */
4829 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004830
4831 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004832}
4833
4834MODULE_AUTHOR("Micron Technology, Inc");
4835MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4836MODULE_LICENSE("GPL");
4837MODULE_VERSION(MTIP_DRV_VERSION);
4838
4839module_init(mtip_init);
4840module_exit(mtip_exit);