blob: c8f5d8c393f7cec52eeae19f2ff7068ed5470578 [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
Jens Axboeffc771b2014-05-09 09:42:02 -0600172static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600173{
Jens Axboeffc771b2014-05-09 09:42:02 -0600174 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600175
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100176 rq = blk_mq_alloc_request(dd->queue, 0, BLK_MQ_REQ_RESERVED);
Jens Axboeffc771b2014-05-09 09:42:02 -0600177 return blk_mq_rq_to_pdu(rq);
178}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600179
Jens Axboeffc771b2014-05-09 09:42:02 -0600180static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
181{
182 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600183}
184
185/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600186 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600187 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600188static struct request *mtip_rq_from_tag(struct driver_data *dd,
189 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600190{
Jens Axboe0e62f512014-06-04 10:23:49 -0600191 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
192
193 return blk_mq_tag_to_rq(hctx->tags, tag);
Jens Axboeffc771b2014-05-09 09:42:02 -0600194}
195
196static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
197 unsigned int tag)
198{
199 struct request *rq = mtip_rq_from_tag(dd, tag);
200
201 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600202}
203
204/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600205 * IO completion function.
206 *
207 * This completion function is called by the driver ISR when a
208 * command that was issued by the kernel completes. It first calls the
209 * asynchronous completion function which normally calls back into the block
210 * layer passing the asynchronous callback data, then unmaps the
211 * scatter list associated with the completed command, and finally
212 * clears the allocated bit associated with the completed command.
213 *
214 * @port Pointer to the port data structure.
215 * @tag Tag of the command.
216 * @data Pointer to driver_data.
217 * @status Completion status.
218 *
219 * return value
220 * None
221 */
222static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600223 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600224{
Jens Axboeffc771b2014-05-09 09:42:02 -0600225 struct driver_data *dd = port->dd;
226 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600227
228 if (unlikely(!dd) || unlikely(!port))
229 return;
230
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600231 if (unlikely(status == PORT_IRQ_TF_ERR)) {
232 dev_warn(&port->dd->pdev->dev,
233 "Command tag %d failed due to TFE\n", tag);
234 }
235
Jens Axboeffc771b2014-05-09 09:42:02 -0600236 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600237
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -0800238 blk_mq_complete_request(rq, status);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600239}
240
241/*
Jens Axboe63166682011-09-27 21:27:43 -0600242 * Reset the HBA (without sleeping)
243 *
Jens Axboe63166682011-09-27 21:27:43 -0600244 * @dd Pointer to the driver data structure.
245 *
246 * return value
247 * 0 The reset was successful.
248 * -1 The HBA Reset bit did not clear.
249 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530250static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600251{
252 unsigned long timeout;
253
Jens Axboe63166682011-09-27 21:27:43 -0600254 /* Set the reset bit */
255 writel(HOST_RESET, dd->mmio + HOST_CTL);
256
257 /* Flush */
258 readl(dd->mmio + HOST_CTL);
259
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700260 /*
261 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
262 * is 1 sec but in LUN failure conditions, up to 10 secs are required
263 */
264 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530265 do {
266 mdelay(10);
267 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
268 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600269
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530270 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
271 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200272
Jens Axboe63166682011-09-27 21:27:43 -0600273 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
274 return -1;
275
276 return 0;
277}
278
279/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600280 * Issue a command to the hardware.
281 *
282 * Set the appropriate bit in the s_active and Command Issue hardware
283 * registers, causing hardware command processing to begin.
284 *
285 * @port Pointer to the port structure.
286 * @tag The tag of the command to be issued.
287 *
288 * return value
289 * None
290 */
291static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
292{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800293 int group = tag >> 5;
294
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800295 /* guard SACT and CI registers */
296 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600297 writel((1 << MTIP_TAG_BIT(tag)),
298 port->s_active[MTIP_TAG_INDEX(tag)]);
299 writel((1 << MTIP_TAG_BIT(tag)),
300 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800301 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600302}
303
304/*
Jens Axboe63166682011-09-27 21:27:43 -0600305 * Enable/disable the reception of FIS
306 *
307 * @port Pointer to the port data structure
308 * @enable 1 to enable, 0 to disable
309 *
310 * return value
311 * Previous state: 1 enabled, 0 disabled
312 */
313static int mtip_enable_fis(struct mtip_port *port, int enable)
314{
315 u32 tmp;
316
317 /* enable FIS reception */
318 tmp = readl(port->mmio + PORT_CMD);
319 if (enable)
320 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
321 else
322 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
323
324 /* Flush */
325 readl(port->mmio + PORT_CMD);
326
327 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
328}
329
330/*
331 * Enable/disable the DMA engine
332 *
333 * @port Pointer to the port data structure
334 * @enable 1 to enable, 0 to disable
335 *
336 * return value
337 * Previous state: 1 enabled, 0 disabled.
338 */
339static int mtip_enable_engine(struct mtip_port *port, int enable)
340{
341 u32 tmp;
342
343 /* enable FIS reception */
344 tmp = readl(port->mmio + PORT_CMD);
345 if (enable)
346 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
347 else
348 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
349
350 readl(port->mmio + PORT_CMD);
351 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
352}
353
354/*
355 * Enables the port DMA engine and FIS reception.
356 *
357 * return value
358 * None
359 */
360static inline void mtip_start_port(struct mtip_port *port)
361{
362 /* Enable FIS reception */
363 mtip_enable_fis(port, 1);
364
365 /* Enable the DMA engine */
366 mtip_enable_engine(port, 1);
367}
368
369/*
370 * Deinitialize a port by disabling port interrupts, the DMA engine,
371 * and FIS reception.
372 *
373 * @port Pointer to the port structure
374 *
375 * return value
376 * None
377 */
378static inline void mtip_deinit_port(struct mtip_port *port)
379{
380 /* Disable interrupts on this port */
381 writel(0, port->mmio + PORT_IRQ_MASK);
382
383 /* Disable the DMA engine */
384 mtip_enable_engine(port, 0);
385
386 /* Disable FIS reception */
387 mtip_enable_fis(port, 0);
388}
389
390/*
391 * Initialize a port.
392 *
393 * This function deinitializes the port by calling mtip_deinit_port() and
394 * then initializes it by setting the command header and RX FIS addresses,
395 * clearing the SError register and any pending port interrupts before
396 * re-enabling the default set of port interrupts.
397 *
398 * @port Pointer to the port structure.
399 *
400 * return value
401 * None
402 */
403static void mtip_init_port(struct mtip_port *port)
404{
405 int i;
406 mtip_deinit_port(port);
407
408 /* Program the command list base and FIS base addresses */
409 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
410 writel((port->command_list_dma >> 16) >> 16,
411 port->mmio + PORT_LST_ADDR_HI);
412 writel((port->rxfis_dma >> 16) >> 16,
413 port->mmio + PORT_FIS_ADDR_HI);
414 }
415
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100416 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600417 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100418 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600419
420 /* Clear SError */
421 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
422
423 /* reset the completed registers.*/
424 for (i = 0; i < port->dd->slot_groups; i++)
425 writel(0xFFFFFFFF, port->completed[i]);
426
427 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700428 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600429
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100430 /* Clear any pending interrupts on the HBA. */
431 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
432 port->dd->mmio + HOST_IRQ_STAT);
433
Jens Axboe63166682011-09-27 21:27:43 -0600434 /* Enable port interrupts */
435 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
436}
437
438/*
439 * Restart a port
440 *
441 * @port Pointer to the port data structure.
442 *
443 * return value
444 * None
445 */
446static void mtip_restart_port(struct mtip_port *port)
447{
448 unsigned long timeout;
449
450 /* Disable the DMA engine */
451 mtip_enable_engine(port, 0);
452
453 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
454 timeout = jiffies + msecs_to_jiffies(500);
455 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
456 && time_before(jiffies, timeout))
457 ;
458
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200459 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200460 return;
461
Jens Axboe63166682011-09-27 21:27:43 -0600462 /*
463 * Chip quirk: escalate to hba reset if
464 * PxCMD.CR not clear after 500 ms
465 */
466 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
467 dev_warn(&port->dd->pdev->dev,
468 "PxCMD.CR not clear, escalating reset\n");
469
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530470 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600471 dev_err(&port->dd->pdev->dev,
472 "HBA reset escalation failed.\n");
473
474 /* 30 ms delay before com reset to quiesce chip */
475 mdelay(30);
476 }
477
478 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
479
480 /* Set PxSCTL.DET */
481 writel(readl(port->mmio + PORT_SCR_CTL) |
482 1, port->mmio + PORT_SCR_CTL);
483 readl(port->mmio + PORT_SCR_CTL);
484
485 /* Wait 1 ms to quiesce chip function */
486 timeout = jiffies + msecs_to_jiffies(1);
487 while (time_before(jiffies, timeout))
488 ;
489
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200490 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200491 return;
492
Jens Axboe63166682011-09-27 21:27:43 -0600493 /* Clear PxSCTL.DET */
494 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
495 port->mmio + PORT_SCR_CTL);
496 readl(port->mmio + PORT_SCR_CTL);
497
498 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
499 timeout = jiffies + msecs_to_jiffies(500);
500 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
501 && time_before(jiffies, timeout))
502 ;
503
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200504 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200505 return;
506
Jens Axboe63166682011-09-27 21:27:43 -0600507 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
508 dev_warn(&port->dd->pdev->dev,
509 "COM reset failed\n");
510
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100511 mtip_init_port(port);
512 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600513
Jens Axboe63166682011-09-27 21:27:43 -0600514}
515
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530516static int mtip_device_reset(struct driver_data *dd)
517{
518 int rv = 0;
519
520 if (mtip_check_surprise_removal(dd->pdev))
521 return 0;
522
523 if (mtip_hba_reset(dd) < 0)
524 rv = -EFAULT;
525
526 mdelay(1);
527 mtip_init_port(dd->port);
528 mtip_start_port(dd->port);
529
530 /* Enable interrupts on the HBA. */
531 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
532 dd->mmio + HOST_CTL);
533 return rv;
534}
535
Jens Axboe63166682011-09-27 21:27:43 -0600536/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200537 * Helper function for tag logging
538 */
539static void print_tags(struct driver_data *dd,
540 char *msg,
541 unsigned long *tagbits,
542 int cnt)
543{
544 unsigned char tagmap[128];
545 int group, tagmap_len = 0;
546
547 memset(tagmap, 0, sizeof(tagmap));
548 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600549 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200550 tagbits[group-1]);
551 dev_warn(&dd->pdev->dev,
552 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
553}
554
555/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600556 * Internal command completion callback function.
557 *
558 * This function is normally called by the driver ISR when an internal
559 * command completed. This function signals the command completion by
560 * calling complete().
561 *
562 * @port Pointer to the port data structure.
563 * @tag Tag of the command that has completed.
564 * @data Pointer to a completion structure.
565 * @status Completion status.
566 *
567 * return value
568 * None
569 */
570static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600571 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600572{
Jens Axboeffc771b2014-05-09 09:42:02 -0600573 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600574 if (unlikely(status == PORT_IRQ_TF_ERR))
575 dev_warn(&port->dd->pdev->dev,
576 "Internal command %d completed with TFE\n", tag);
577
Sam Bradshaw88523a62011-08-30 08:34:26 -0600578 complete(waiting);
579}
580
Asai Thambi S P8182b492012-04-09 08:35:38 +0200581static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600582 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200583{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200584}
585
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200586static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
587 dma_addr_t buffer_dma, unsigned int sectors);
588static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
589 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600590/*
591 * Handle an error.
592 *
593 * @dd Pointer to the DRIVER_DATA structure.
594 *
595 * return value
596 * None
597 */
598static void mtip_handle_tfe(struct driver_data *dd)
599{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200600 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600601 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200602 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600603 u32 completed;
604 struct host_to_dev_fis *fis;
605 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200606 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200607 unsigned char *buf;
608 char *fail_reason = NULL;
609 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600610
611 dev_warn(&dd->pdev->dev, "Taskfile error\n");
612
613 port = dd->port;
614
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700615 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600616 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700617 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
618
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700619 if (cmd->comp_data && cmd->comp_func) {
620 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600621 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700622 }
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800623 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700624 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600625
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200626 /* clear the tag accumulator */
627 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
628
Sam Bradshaw88523a62011-08-30 08:34:26 -0600629 /* Loop through all the groups */
630 for (group = 0; group < dd->slot_groups; group++) {
631 completed = readl(port->completed[group]);
632
Jens Axboeffc771b2014-05-09 09:42:02 -0600633 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
634
Sam Bradshaw88523a62011-08-30 08:34:26 -0600635 /* clear completed status register in the hardware.*/
636 writel(completed, port->completed[group]);
637
Sam Bradshaw88523a62011-08-30 08:34:26 -0600638 /* Process successfully completed commands */
639 for (bit = 0; bit < 32 && completed; bit++) {
640 if (!(completed & (1<<bit)))
641 continue;
642 tag = (group << 5) + bit;
643
644 /* Skip the internal command slot */
645 if (tag == MTIP_TAG_INTERNAL)
646 continue;
647
Jens Axboeffc771b2014-05-09 09:42:02 -0600648 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200649 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600650 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200651 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600652 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600653 } else {
654 dev_err(&port->dd->pdev->dev,
655 "Missing completion func for tag %d",
656 tag);
657 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600658 /* don't proceed further */
659 return;
660 }
661 }
662 }
663 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200664
665 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600666
667 /* Restart the port */
668 mdelay(20);
669 mtip_restart_port(port);
670
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200671 /* Trying to determine the cause of the error */
672 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
673 dd->port->log_buf,
674 dd->port->log_buf_dma, 1);
675 if (rv) {
676 dev_warn(&dd->pdev->dev,
677 "Error in READ LOG EXT (10h) command\n");
678 /* non-critical error, don't fail the load */
679 } else {
680 buf = (unsigned char *)dd->port->log_buf;
681 if (buf[259] & 0x1) {
682 dev_info(&dd->pdev->dev,
683 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200684 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200685 fail_all_ncq_write = 1;
686 fail_reason = "write protect";
687 }
688 if (buf[288] == 0xF7) {
689 dev_info(&dd->pdev->dev,
690 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200691 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200692 fail_all_ncq_cmds = 1;
693 fail_reason = "thermal shutdown";
694 }
695 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800696 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200697 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700698 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200699 fail_all_ncq_cmds = 1;
700 fail_reason = "rebuild failed";
701 }
702 }
703
Sam Bradshaw88523a62011-08-30 08:34:26 -0600704 /* clear the tag accumulator */
705 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
706
707 /* Loop through all the groups */
708 for (group = 0; group < dd->slot_groups; group++) {
709 for (bit = 0; bit < 32; bit++) {
710 reissue = 1;
711 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600712 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600713
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200714 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600715
716 /* Should re-issue? */
717 if (tag == MTIP_TAG_INTERNAL ||
718 fis->command == ATA_CMD_SET_FEATURES)
719 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200720 else {
721 if (fail_all_ncq_cmds ||
722 (fail_all_ncq_write &&
723 fis->command == ATA_CMD_FPDMA_WRITE)) {
724 dev_warn(&dd->pdev->dev,
725 " Fail: %s w/tag %d [%s].\n",
726 fis->command == ATA_CMD_FPDMA_WRITE ?
727 "write" : "read",
728 tag,
729 fail_reason != NULL ?
730 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200731 if (cmd->comp_func) {
732 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600733 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200734 }
735 continue;
736 }
737 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600738
739 /*
740 * First check if this command has
741 * exceeded its retries.
742 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200743 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600744
745 set_bit(tag, tagaccum);
746
Sam Bradshaw88523a62011-08-30 08:34:26 -0600747 /* Re-issue the command. */
748 mtip_issue_ncq_command(port, tag);
749
750 continue;
751 }
752
753 /* Retire a command that will not be reissued */
754 dev_warn(&port->dd->pdev->dev,
755 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600756
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200757 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600758 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600759 else
760 dev_warn(&port->dd->pdev->dev,
761 "Bad completion for tag %d\n",
762 tag);
763 }
764 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200765 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600766}
767
768/*
769 * Handle a set device bits interrupt
770 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800771static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
772 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600773{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800774 struct driver_data *dd = port->dd;
775 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600776 struct mtip_cmd *command;
777
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800778 if (!completed) {
779 WARN_ON_ONCE(!completed);
780 return;
781 }
782 /* clear completed status register in the hardware.*/
783 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600784
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800785 /* Process completed commands. */
786 for (bit = 0; (bit < 32) && completed; bit++) {
787 if (completed & 0x01) {
788 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600789
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800790 /* skip internal command slot. */
791 if (unlikely(tag == MTIP_TAG_INTERNAL))
792 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600793
Jens Axboeffc771b2014-05-09 09:42:02 -0600794 command = mtip_cmd_from_tag(dd, tag);
795 if (likely(command->comp_func))
796 command->comp_func(port, tag, command, 0);
797 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600798 dev_dbg(&dd->pdev->dev,
799 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800800 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600801
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800802 if (mtip_check_surprise_removal(
803 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800804 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600805 }
806 }
807 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800808 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600809 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800810
811 /* If last, re-enable interrupts */
812 if (atomic_dec_return(&dd->irq_workers_active) == 0)
813 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600814}
815
816/*
817 * Process legacy pio and d2h interrupts
818 */
819static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
820{
821 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600822 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600823
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200824 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100825 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600826 & (1 << MTIP_TAG_INTERNAL))) {
827 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600828 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600829 return;
830 }
831 }
832
Sam Bradshaw88523a62011-08-30 08:34:26 -0600833 return;
834}
835
836/*
837 * Demux and handle errors
838 */
839static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
840{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600841
842 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
843 dev_warn(&dd->pdev->dev,
844 "Clearing PxSERR.DIAG.x\n");
845 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
846 }
847
848 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
849 dev_warn(&dd->pdev->dev,
850 "Clearing PxSERR.DIAG.n\n");
851 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
852 }
853
854 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
855 dev_warn(&dd->pdev->dev,
856 "Port stat errors %x unhandled\n",
857 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700858 if (mtip_check_surprise_removal(dd->pdev))
859 return;
860 }
861 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
862 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
863 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600864 }
865}
866
867static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
868{
869 struct driver_data *dd = (struct driver_data *) data;
870 struct mtip_port *port = dd->port;
871 u32 hba_stat, port_stat;
872 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800873 int do_irq_enable = 1, i, workers;
874 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600875
876 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
877 if (hba_stat) {
878 rv = IRQ_HANDLED;
879
880 /* Acknowledge the interrupt status on the port.*/
881 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700882 if (unlikely(port_stat == 0xFFFFFFFF)) {
883 mtip_check_surprise_removal(dd->pdev);
884 return IRQ_HANDLED;
885 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600886 writel(port_stat, port->mmio + PORT_IRQ_STAT);
887
888 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800889 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
890 do_irq_enable = 0;
891 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
892
893 /* Start at 1: group zero is always local? */
894 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
895 i++) {
896 twork = &dd->work[i];
897 twork->completed = readl(port->completed[i]);
898 if (twork->completed)
899 workers++;
900 }
901
902 atomic_set(&dd->irq_workers_active, workers);
903 if (workers) {
904 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
905 twork = &dd->work[i];
906 if (twork->completed)
907 queue_work_on(
908 twork->cpu_binding,
909 dd->isr_workq,
910 &twork->work);
911 }
912
913 if (likely(dd->work[0].completed))
914 mtip_workq_sdbfx(port, 0,
915 dd->work[0].completed);
916
917 } else {
918 /*
919 * Chip quirk: SDB interrupt but nothing
920 * to complete
921 */
922 do_irq_enable = 1;
923 }
924 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600925
926 if (unlikely(port_stat & PORT_IRQ_ERR)) {
927 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600928 /* don't proceed further */
929 return IRQ_HANDLED;
930 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200931 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200932 &dd->dd_flag))
933 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600934
935 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
936 }
937
938 if (unlikely(port_stat & PORT_IRQ_LEGACY))
939 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
940 }
941
942 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800943 if (unlikely(do_irq_enable))
944 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600945
946 return rv;
947}
948
949/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600950 * HBA interrupt subroutine.
951 *
952 * @irq IRQ number.
953 * @instance Pointer to the driver data structure.
954 *
955 * return value
956 * IRQ_HANDLED A HBA interrupt was pending and handled.
957 * IRQ_NONE This interrupt was not for the HBA.
958 */
959static irqreturn_t mtip_irq_handler(int irq, void *instance)
960{
961 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800962
963 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600964}
965
966static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
967{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600968 writel(1 << MTIP_TAG_BIT(tag),
969 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
970}
971
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200972static bool mtip_pause_ncq(struct mtip_port *port,
973 struct host_to_dev_fis *fis)
974{
975 struct host_to_dev_fis *reply;
976 unsigned long task_file_data;
977
978 reply = port->rxfis + RX_FIS_D2H_REG;
979 task_file_data = readl(port->mmio+PORT_TFDATA);
980
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530981 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200982 return false;
983
984 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200985 port->ic_pause_timer = jiffies;
986 return true;
987 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
988 (fis->features == 0x03)) {
989 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
990 port->ic_pause_timer = jiffies;
991 return true;
992 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
993 ((fis->command == 0xFC) &&
994 (fis->features == 0x27 || fis->features == 0x72 ||
995 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -0700996 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800997 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200998 /* Com reset after secure erase or lowlevel format */
999 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001000 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001001 return false;
1002 }
1003
1004 return false;
1005}
1006
Sam Bradshaw88523a62011-08-30 08:34:26 -06001007/*
1008 * Wait for port to quiesce
1009 *
1010 * @port Pointer to port data structure
1011 * @timeout Max duration to wait (ms)
1012 *
1013 * return value
1014 * 0 Success
1015 * -EBUSY Commands still active
1016 */
1017static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1018{
1019 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001020 unsigned int n;
1021 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001022
Jens Axboe9acf03c2014-05-14 08:22:56 -06001023 blk_mq_stop_hw_queues(port->dd->queue);
1024
Sam Bradshaw88523a62011-08-30 08:34:26 -06001025 to = jiffies + msecs_to_jiffies(timeout);
1026 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001027 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1028 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001029 msleep(20);
1030 continue; /* svc thd is actively issuing commands */
1031 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001032
1033 msleep(100);
1034 if (mtip_check_surprise_removal(port->dd->pdev))
1035 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001036 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001037 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001038
Sam Bradshaw88523a62011-08-30 08:34:26 -06001039 /*
1040 * Ignore s_active bit 0 of array element 0.
1041 * This bit will always be set
1042 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001043 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001044 for (n = 1; n < port->dd->slot_groups; n++)
1045 active |= readl(port->s_active[n]);
1046
1047 if (!active)
1048 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001049 } while (time_before(jiffies, to));
1050
Jens Axboe9acf03c2014-05-14 08:22:56 -06001051 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001052 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001053err_fault:
1054 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1055 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001056}
1057
1058/*
1059 * Execute an internal command and wait for the completion.
1060 *
1061 * @port Pointer to the port data structure.
1062 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001063 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001064 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001065 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001066 * @opts Command header options, excluding the FIS length
1067 * and the number of PRD entries.
1068 * @timeout Time in ms to wait for the command to complete.
1069 *
1070 * return value
1071 * 0 Command completed successfully.
1072 * -EFAULT The buffer address is not correctly aligned.
1073 * -EBUSY Internal command or other IO in progress.
1074 * -EAGAIN Time out waiting for command to complete.
1075 */
1076static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001077 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001078 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001079 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001080 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 u32 opts,
1082 gfp_t atomic,
1083 unsigned long timeout)
1084{
1085 struct mtip_cmd_sg *command_sg;
1086 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001087 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301088 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001089 int rv = 0;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001090 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001091
1092 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1093 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301094 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001095 return -EFAULT;
1096 }
1097
Jens Axboeffc771b2014-05-09 09:42:02 -06001098 int_cmd = mtip_get_int_command(dd);
1099
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001100 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001101
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001102 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1103 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1104
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301105 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001106
1107 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001108 if (fis->command != ATA_CMD_STANDBYNOW1) {
1109 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001110 if (mtip_quiesce_io(port,
1111 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301112 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001113 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001114 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001115 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001116 wake_up_interruptible(&port->svc_wait);
1117 return -EBUSY;
1118 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001119 }
1120
1121 /* Set the completion function and data for the command. */
1122 int_cmd->comp_data = &wait;
1123 int_cmd->comp_func = mtip_completion;
1124
1125 } else {
1126 /* Clear completion - we're going to poll */
1127 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001128 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001129 }
1130
1131 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001132 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133
1134 /* Populate the SG list */
1135 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001136 __force_bit2int cpu_to_le32(opts | fis_len);
1137 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001138 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1139
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001140 command_sg->info =
1141 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1142 command_sg->dba =
1143 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1144 command_sg->dba_upper =
1145 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001146
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001147 int_cmd->command_header->opts |=
1148 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001149 }
1150
1151 /* Populate the command header */
1152 int_cmd->command_header->byte_count = 0;
1153
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001154 start = jiffies;
1155
Sam Bradshaw88523a62011-08-30 08:34:26 -06001156 /* Issue the command to the hardware */
1157 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1158
Sam Bradshaw88523a62011-08-30 08:34:26 -06001159 if (atomic == GFP_KERNEL) {
1160 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001161 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001162 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001163 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001164
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301165 if (rv == -ERESTARTSYS) { /* interrupted */
1166 dev_err(&dd->pdev->dev,
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001167 "Internal command [%02X] was interrupted after %u ms\n",
1168 fis->command,
1169 jiffies_to_msecs(jiffies - start));
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301170 rv = -EINTR;
1171 goto exec_ic_exit;
1172 } else if (rv == 0) /* timeout */
1173 dev_err(&dd->pdev->dev,
1174 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1175 fis->command, timeout);
1176 else
1177 dev_err(&dd->pdev->dev,
1178 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1179 fis->command, rv, timeout);
1180
1181 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001182 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301183 &dd->dd_flag)) {
1184 dev_err(&dd->pdev->dev,
1185 "Internal command [%02X] wait returned due to SR\n",
1186 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001187 rv = -ENXIO;
1188 goto exec_ic_exit;
1189 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301190 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001191 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301192 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001193 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001194 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301195 u32 hba_stat, port_stat;
1196
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 /* Spin for <timeout> checking if command still outstanding */
1198 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001199 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1200 & (1 << MTIP_TAG_INTERNAL))
1201 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301202 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001203 rv = -ENXIO;
1204 goto exec_ic_exit;
1205 }
1206 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001207 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301208 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001209 rv = -ENXIO;
1210 goto exec_ic_exit;
1211 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301212 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1213 if (!port_stat)
1214 continue;
1215
1216 if (port_stat & PORT_IRQ_ERR) {
1217 dev_err(&dd->pdev->dev,
1218 "Internal command [%02X] failed\n",
1219 fis->command);
1220 mtip_device_reset(dd);
1221 rv = -EIO;
1222 goto exec_ic_exit;
1223 } else {
1224 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1225 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1226 if (hba_stat)
1227 writel(hba_stat,
1228 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001229 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301230 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001231 }
1232 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001233
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001234 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1235 & (1 << MTIP_TAG_INTERNAL)) {
1236 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301237 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1238 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001239 rv = -EAGAIN;
1240 }
1241 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001242exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001243 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001244 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001245 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001246 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1247 /* NCQ paused */
1248 return rv;
1249 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001250 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001251
1252 return rv;
1253}
1254
1255/*
1256 * Byte-swap ATA ID strings.
1257 *
1258 * ATA identify data contains strings in byte-swapped 16-bit words.
1259 * They must be swapped (on all architectures) to be usable as C strings.
1260 * This function swaps bytes in-place.
1261 *
1262 * @buf The buffer location of the string
1263 * @len The number of bytes to swap
1264 *
1265 * return value
1266 * None
1267 */
1268static inline void ata_swap_string(u16 *buf, unsigned int len)
1269{
1270 int i;
1271 for (i = 0; i < (len/2); i++)
1272 be16_to_cpus(&buf[i]);
1273}
1274
Asai Thambi S P670a6412014-04-16 20:27:54 -07001275static void mtip_set_timeout(struct driver_data *dd,
1276 struct host_to_dev_fis *fis,
1277 unsigned int *timeout, u8 erasemode)
1278{
1279 switch (fis->command) {
1280 case ATA_CMD_DOWNLOAD_MICRO:
1281 *timeout = 120000; /* 2 minutes */
1282 break;
1283 case ATA_CMD_SEC_ERASE_UNIT:
1284 case 0xFC:
1285 if (erasemode)
1286 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1287 else
1288 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1289 break;
1290 case ATA_CMD_STANDBYNOW1:
1291 *timeout = 120000; /* 2 minutes */
1292 break;
1293 case 0xF7:
1294 case 0xFA:
1295 *timeout = 60000; /* 60 seconds */
1296 break;
1297 case ATA_CMD_SMART:
1298 *timeout = 15000; /* 15 seconds */
1299 break;
1300 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001301 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001302 break;
1303 }
1304}
1305
Sam Bradshaw88523a62011-08-30 08:34:26 -06001306/*
1307 * Request the device identity information.
1308 *
1309 * If a user space buffer is not specified, i.e. is NULL, the
1310 * identify information is still read from the drive and placed
1311 * into the identify data buffer (@e port->identify) in the
1312 * port data structure.
1313 * When the identify buffer contains valid identify information @e
1314 * port->identify_valid is non-zero.
1315 *
1316 * @port Pointer to the port structure.
1317 * @user_buffer A user space buffer where the identify data should be
1318 * copied.
1319 *
1320 * return value
1321 * 0 Command completed successfully.
1322 * -EFAULT An error occurred while coping data to the user buffer.
1323 * -1 Command failed.
1324 */
1325static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1326{
1327 int rv = 0;
1328 struct host_to_dev_fis fis;
1329
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001330 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001331 return -EFAULT;
1332
Sam Bradshaw88523a62011-08-30 08:34:26 -06001333 /* Build the FIS. */
1334 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1335 fis.type = 0x27;
1336 fis.opts = 1 << 7;
1337 fis.command = ATA_CMD_ID_ATA;
1338
1339 /* Set the identify information as invalid. */
1340 port->identify_valid = 0;
1341
1342 /* Clear the identify information. */
1343 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1344
1345 /* Execute the command. */
1346 if (mtip_exec_internal_command(port,
1347 &fis,
1348 5,
1349 port->identify_dma,
1350 sizeof(u16) * ATA_ID_WORDS,
1351 0,
1352 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001353 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001354 < 0) {
1355 rv = -1;
1356 goto out;
1357 }
1358
1359 /*
1360 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1361 * perform field-sensitive swapping on the string fields.
1362 * See the kernel use of ata_id_string() for proof of this.
1363 */
1364#ifdef __LITTLE_ENDIAN
1365 ata_swap_string(port->identify + 27, 40); /* model string*/
1366 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1367 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1368#else
1369 {
1370 int i;
1371 for (i = 0; i < ATA_ID_WORDS; i++)
1372 port->identify[i] = le16_to_cpu(port->identify[i]);
1373 }
1374#endif
1375
Sam Bradshaw26d58052013-10-03 10:18:05 -07001376 /* Check security locked state */
1377 if (port->identify[128] & 0x4)
1378 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1379 else
1380 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1381
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301382#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001383 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1384 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1385 port->dd->trim_supp = true;
1386 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301387#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001388 port->dd->trim_supp = false;
1389
Sam Bradshaw88523a62011-08-30 08:34:26 -06001390 /* Set the identify buffer as valid. */
1391 port->identify_valid = 1;
1392
1393 if (user_buffer) {
1394 if (copy_to_user(
1395 user_buffer,
1396 port->identify,
1397 ATA_ID_WORDS * sizeof(u16))) {
1398 rv = -EFAULT;
1399 goto out;
1400 }
1401 }
1402
1403out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001404 return rv;
1405}
1406
1407/*
1408 * Issue a standby immediate command to the device.
1409 *
1410 * @port Pointer to the port structure.
1411 *
1412 * return value
1413 * 0 Command was executed successfully.
1414 * -1 An error occurred while executing the command.
1415 */
1416static int mtip_standby_immediate(struct mtip_port *port)
1417{
1418 int rv;
1419 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001420 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001421 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001422
Sam Bradshaw88523a62011-08-30 08:34:26 -06001423 /* Build the FIS. */
1424 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1425 fis.type = 0x27;
1426 fis.opts = 1 << 7;
1427 fis.command = ATA_CMD_STANDBYNOW1;
1428
Asai Thambi S P670a6412014-04-16 20:27:54 -07001429 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1430
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001431 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001432 rv = mtip_exec_internal_command(port,
1433 &fis,
1434 5,
1435 0,
1436 0,
1437 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001438 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001439 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001440 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1441 jiffies_to_msecs(jiffies - start));
1442 if (rv)
1443 dev_warn(&port->dd->pdev->dev,
1444 "STANDBY IMMEDIATE command failed.\n");
1445
1446 return rv;
1447}
1448
1449/*
1450 * Issue a READ LOG EXT command to the device.
1451 *
1452 * @port pointer to the port structure.
1453 * @page page number to fetch
1454 * @buffer pointer to buffer
1455 * @buffer_dma dma address corresponding to @buffer
1456 * @sectors page length to fetch, in sectors
1457 *
1458 * return value
1459 * @rv return value from mtip_exec_internal_command()
1460 */
1461static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1462 dma_addr_t buffer_dma, unsigned int sectors)
1463{
1464 struct host_to_dev_fis fis;
1465
1466 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1467 fis.type = 0x27;
1468 fis.opts = 1 << 7;
1469 fis.command = ATA_CMD_READ_LOG_EXT;
1470 fis.sect_count = sectors & 0xFF;
1471 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1472 fis.lba_low = page;
1473 fis.lba_mid = 0;
1474 fis.device = ATA_DEVICE_OBS;
1475
1476 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1477
1478 return mtip_exec_internal_command(port,
1479 &fis,
1480 5,
1481 buffer_dma,
1482 sectors * ATA_SECT_SIZE,
1483 0,
1484 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001485 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001486}
1487
1488/*
1489 * Issue a SMART READ DATA command to the device.
1490 *
1491 * @port pointer to the port structure.
1492 * @buffer pointer to buffer
1493 * @buffer_dma dma address corresponding to @buffer
1494 *
1495 * return value
1496 * @rv return value from mtip_exec_internal_command()
1497 */
1498static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1499 dma_addr_t buffer_dma)
1500{
1501 struct host_to_dev_fis fis;
1502
1503 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1504 fis.type = 0x27;
1505 fis.opts = 1 << 7;
1506 fis.command = ATA_CMD_SMART;
1507 fis.features = 0xD0;
1508 fis.sect_count = 1;
1509 fis.lba_mid = 0x4F;
1510 fis.lba_hi = 0xC2;
1511 fis.device = ATA_DEVICE_OBS;
1512
1513 return mtip_exec_internal_command(port,
1514 &fis,
1515 5,
1516 buffer_dma,
1517 ATA_SECT_SIZE,
1518 0,
1519 GFP_ATOMIC,
1520 15000);
1521}
1522
1523/*
1524 * Get the value of a smart attribute
1525 *
1526 * @port pointer to the port structure
1527 * @id attribute number
1528 * @attrib pointer to return attrib information corresponding to @id
1529 *
1530 * return value
1531 * -EINVAL NULL buffer passed or unsupported attribute @id.
1532 * -EPERM Identify data not valid, SMART not supported or not enabled
1533 */
1534static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1535 struct smart_attr *attrib)
1536{
1537 int rv, i;
1538 struct smart_attr *pattr;
1539
1540 if (!attrib)
1541 return -EINVAL;
1542
1543 if (!port->identify_valid) {
1544 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1545 return -EPERM;
1546 }
1547 if (!(port->identify[82] & 0x1)) {
1548 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1549 return -EPERM;
1550 }
1551 if (!(port->identify[85] & 0x1)) {
1552 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1553 return -EPERM;
1554 }
1555
1556 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1557 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1558 if (rv) {
1559 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1560 return rv;
1561 }
1562
1563 pattr = (struct smart_attr *)(port->smart_buf + 2);
1564 for (i = 0; i < 29; i++, pattr++)
1565 if (pattr->attr_id == id) {
1566 memcpy(attrib, pattr, sizeof(struct smart_attr));
1567 break;
1568 }
1569
1570 if (i == 29) {
1571 dev_warn(&port->dd->pdev->dev,
1572 "Query for invalid SMART attribute ID\n");
1573 rv = -EINVAL;
1574 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001575
Sam Bradshaw88523a62011-08-30 08:34:26 -06001576 return rv;
1577}
1578
1579/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001580 * Trim unused sectors
1581 *
1582 * @dd pointer to driver_data structure
1583 * @lba starting lba
1584 * @len # of 512b sectors to trim
1585 *
1586 * return value
1587 * -ENOMEM Out of dma memory
1588 * -EINVAL Invalid parameters passed in, trim not supported
1589 * -EIO Error submitting trim request to hw
1590 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301591static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1592 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001593{
1594 int i, rv = 0;
1595 u64 tlba, tlen, sect_left;
1596 struct mtip_trim_entry *buf;
1597 dma_addr_t dma_addr;
1598 struct host_to_dev_fis fis;
1599
1600 if (!len || dd->trim_supp == false)
1601 return -EINVAL;
1602
1603 /* Trim request too big */
1604 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1605
1606 /* Trim request not aligned on 4k boundary */
1607 WARN_ON(len % 8 != 0);
1608
1609 /* Warn if vu_trim structure is too big */
1610 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1611
1612 /* Allocate a DMA buffer for the trim structure */
1613 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1614 GFP_KERNEL);
1615 if (!buf)
1616 return -ENOMEM;
1617 memset(buf, 0, ATA_SECT_SIZE);
1618
1619 for (i = 0, sect_left = len, tlba = lba;
1620 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1621 i++) {
1622 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1623 MTIP_MAX_TRIM_ENTRY_LEN :
1624 sect_left);
1625 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1626 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1627 tlba += tlen;
1628 sect_left -= tlen;
1629 }
1630 WARN_ON(sect_left != 0);
1631
1632 /* Build the fis */
1633 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1634 fis.type = 0x27;
1635 fis.opts = 1 << 7;
1636 fis.command = 0xfb;
1637 fis.features = 0x60;
1638 fis.sect_count = 1;
1639 fis.device = ATA_DEVICE_OBS;
1640
1641 if (mtip_exec_internal_command(dd->port,
1642 &fis,
1643 5,
1644 dma_addr,
1645 ATA_SECT_SIZE,
1646 0,
1647 GFP_KERNEL,
1648 MTIP_TRIM_TIMEOUT_MS) < 0)
1649 rv = -EIO;
1650
1651 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1652 return rv;
1653}
1654
1655/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001656 * Get the drive capacity.
1657 *
1658 * @dd Pointer to the device data structure.
1659 * @sectors Pointer to the variable that will receive the sector count.
1660 *
1661 * return value
1662 * 1 Capacity was returned successfully.
1663 * 0 The identify information is invalid.
1664 */
Jens Axboe63166682011-09-27 21:27:43 -06001665static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001666{
1667 struct mtip_port *port = dd->port;
1668 u64 total, raw0, raw1, raw2, raw3;
1669 raw0 = port->identify[100];
1670 raw1 = port->identify[101];
1671 raw2 = port->identify[102];
1672 raw3 = port->identify[103];
1673 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1674 *sectors = total;
1675 return (bool) !!port->identify_valid;
1676}
1677
1678/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679 * Display the identify command data.
1680 *
1681 * @port Pointer to the port data structure.
1682 *
1683 * return value
1684 * None
1685 */
1686static void mtip_dump_identify(struct mtip_port *port)
1687{
1688 sector_t sectors;
1689 unsigned short revid;
1690 char cbuf[42];
1691
1692 if (!port->identify_valid)
1693 return;
1694
1695 strlcpy(cbuf, (char *)(port->identify+10), 21);
1696 dev_info(&port->dd->pdev->dev,
1697 "Serial No.: %s\n", cbuf);
1698
1699 strlcpy(cbuf, (char *)(port->identify+23), 9);
1700 dev_info(&port->dd->pdev->dev,
1701 "Firmware Ver.: %s\n", cbuf);
1702
1703 strlcpy(cbuf, (char *)(port->identify+27), 41);
1704 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1705
Sam Bradshaw26d58052013-10-03 10:18:05 -07001706 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1707 port->identify[128],
1708 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1709
Sam Bradshaw88523a62011-08-30 08:34:26 -06001710 if (mtip_hw_get_capacity(port->dd, &sectors))
1711 dev_info(&port->dd->pdev->dev,
1712 "Capacity: %llu sectors (%llu MB)\n",
1713 (u64)sectors,
1714 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1715
1716 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001717 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001718 case 0x1:
1719 strlcpy(cbuf, "A0", 3);
1720 break;
1721 case 0x3:
1722 strlcpy(cbuf, "A2", 3);
1723 break;
1724 default:
1725 strlcpy(cbuf, "?", 2);
1726 break;
1727 }
1728 dev_info(&port->dd->pdev->dev,
1729 "Card Type: %s\n", cbuf);
1730}
1731
1732/*
1733 * Map the commands scatter list into the command table.
1734 *
1735 * @command Pointer to the command.
1736 * @nents Number of scatter list entries.
1737 *
1738 * return value
1739 * None
1740 */
1741static inline void fill_command_sg(struct driver_data *dd,
1742 struct mtip_cmd *command,
1743 int nents)
1744{
1745 int n;
1746 unsigned int dma_len;
1747 struct mtip_cmd_sg *command_sg;
1748 struct scatterlist *sg = command->sg;
1749
1750 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1751
1752 for (n = 0; n < nents; n++) {
1753 dma_len = sg_dma_len(sg);
1754 if (dma_len > 0x400000)
1755 dev_err(&dd->pdev->dev,
1756 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001757 command_sg->info = __force_bit2int
1758 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1759 command_sg->dba = __force_bit2int
1760 cpu_to_le32(sg_dma_address(sg));
1761 command_sg->dba_upper = __force_bit2int
1762 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001763 command_sg++;
1764 sg++;
1765 }
1766}
1767
1768/*
1769 * @brief Execute a drive command.
1770 *
1771 * return value 0 The command completed successfully.
1772 * return value -1 An error occurred while executing the command.
1773 */
Jens Axboe63166682011-09-27 21:27:43 -06001774static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001775{
1776 struct host_to_dev_fis fis;
1777 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001778 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001779
Sam Bradshaw88523a62011-08-30 08:34:26 -06001780 /* Build the FIS. */
1781 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1782 fis.type = 0x27;
1783 fis.opts = 1 << 7;
1784 fis.command = command[0];
1785 fis.features = command[1];
1786 fis.sect_count = command[2];
1787 fis.sector = command[3];
1788 fis.cyl_low = command[4];
1789 fis.cyl_hi = command[5];
1790 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1791
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001792 mtip_set_timeout(port->dd, &fis, &to, 0);
1793
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001794 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 -06001795 __func__,
1796 command[0],
1797 command[1],
1798 command[2],
1799 command[3],
1800 command[4],
1801 command[5],
1802 command[6]);
1803
1804 /* Execute the command. */
1805 if (mtip_exec_internal_command(port,
1806 &fis,
1807 5,
1808 0,
1809 0,
1810 0,
1811 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001812 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001813 return -1;
1814 }
1815
1816 command[0] = reply->command; /* Status*/
1817 command[1] = reply->features; /* Error*/
1818 command[4] = reply->cyl_low;
1819 command[5] = reply->cyl_hi;
1820
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001821 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 -06001822 __func__,
1823 command[0],
1824 command[1],
1825 command[4],
1826 command[5]);
1827
Sam Bradshaw88523a62011-08-30 08:34:26 -06001828 return 0;
1829}
1830
1831/*
1832 * @brief Execute a drive command.
1833 *
1834 * @param port Pointer to the port data structure.
1835 * @param command Pointer to the user specified command parameters.
1836 * @param user_buffer Pointer to the user space buffer where read sector
1837 * data should be copied.
1838 *
1839 * return value 0 The command completed successfully.
1840 * return value -EFAULT An error occurred while copying the completion
1841 * data to the user space buffer.
1842 * return value -1 An error occurred while executing the command.
1843 */
Jens Axboe63166682011-09-27 21:27:43 -06001844static int exec_drive_command(struct mtip_port *port, u8 *command,
1845 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001846{
1847 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001848 struct host_to_dev_fis *reply;
1849 u8 *buf = NULL;
1850 dma_addr_t dma_addr = 0;
1851 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001852 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001853
1854 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001855 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001856 return -EFAULT;
1857
1858 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1859 ATA_SECT_SIZE * xfer_sz,
1860 &dma_addr,
1861 GFP_KERNEL);
1862 if (!buf) {
1863 dev_err(&port->dd->pdev->dev,
1864 "Memory allocation failed (%d bytes)\n",
1865 ATA_SECT_SIZE * xfer_sz);
1866 return -ENOMEM;
1867 }
1868 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1869 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001870
Sam Bradshaw88523a62011-08-30 08:34:26 -06001871 /* Build the FIS. */
1872 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001873 fis.type = 0x27;
1874 fis.opts = 1 << 7;
1875 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001876 fis.features = command[2];
1877 fis.sect_count = command[3];
1878 if (fis.command == ATA_CMD_SMART) {
1879 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001880 fis.cyl_low = 0x4F;
1881 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 }
1883
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001884 mtip_set_timeout(port->dd, &fis, &to, 0);
1885
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001886 if (xfer_sz)
1887 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1888 else
1889 reply = (port->rxfis + RX_FIS_D2H_REG);
1890
Sam Bradshaw88523a62011-08-30 08:34:26 -06001891 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001892 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001893 "feat %x, sectcnt %x\n",
1894 __func__,
1895 command[0],
1896 command[1],
1897 command[2],
1898 command[3]);
1899
Sam Bradshaw88523a62011-08-30 08:34:26 -06001900 /* Execute the command. */
1901 if (mtip_exec_internal_command(port,
1902 &fis,
1903 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001904 (xfer_sz ? dma_addr : 0),
1905 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001906 0,
1907 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001908 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001909 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001910 rv = -EFAULT;
1911 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001912 }
1913
1914 /* Collect the completion status. */
1915 command[0] = reply->command; /* Status*/
1916 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001917 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001918
1919 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001920 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001921 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922 __func__,
1923 command[0],
1924 command[1],
1925 command[2]);
1926
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001927 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001929 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001930 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001931 rv = -EFAULT;
1932 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001933 }
1934 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001935exit_drive_command:
1936 if (buf)
1937 dmam_free_coherent(&port->dd->pdev->dev,
1938 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1939 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001940}
1941
1942/*
1943 * Indicates whether a command has a single sector payload.
1944 *
1945 * @command passed to the device to perform the certain event.
1946 * @features passed to the device to perform the certain event.
1947 *
1948 * return value
1949 * 1 command is one that always has a single sector payload,
1950 * regardless of the value in the Sector Count field.
1951 * 0 otherwise
1952 *
1953 */
1954static unsigned int implicit_sector(unsigned char command,
1955 unsigned char features)
1956{
1957 unsigned int rv = 0;
1958
1959 /* list of commands that have an implicit sector count of 1 */
1960 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001961 case ATA_CMD_SEC_SET_PASS:
1962 case ATA_CMD_SEC_UNLOCK:
1963 case ATA_CMD_SEC_ERASE_PREP:
1964 case ATA_CMD_SEC_ERASE_UNIT:
1965 case ATA_CMD_SEC_FREEZE_LOCK:
1966 case ATA_CMD_SEC_DISABLE_PASS:
1967 case ATA_CMD_PMP_READ:
1968 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001969 rv = 1;
1970 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001971 case ATA_CMD_SET_MAX:
1972 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001973 rv = 1;
1974 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001975 case ATA_CMD_SMART:
1976 if ((features == ATA_SMART_READ_VALUES) ||
1977 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978 rv = 1;
1979 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001980 case ATA_CMD_CONF_OVERLAY:
1981 if ((features == ATA_DCO_IDENTIFY) ||
1982 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001983 rv = 1;
1984 break;
1985 }
1986 return rv;
1987}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001988
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989/*
1990 * Executes a taskfile
1991 * See ide_taskfile_ioctl() for derivation
1992 */
1993static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001994 void __user *buf,
1995 ide_task_request_t *req_task,
1996 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001997{
1998 struct host_to_dev_fis fis;
1999 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002000 u8 *outbuf = NULL;
2001 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002002 dma_addr_t outbuf_dma = 0;
2003 dma_addr_t inbuf_dma = 0;
2004 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002006 unsigned int taskin = 0;
2007 unsigned int taskout = 0;
2008 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002009 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010 unsigned int force_single_sector;
2011 unsigned int transfer_size;
2012 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002013 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002014 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002015
2016 taskout = req_task->out_size;
2017 taskin = req_task->in_size;
2018 /* 130560 = 512 * 0xFF*/
2019 if (taskin > 130560 || taskout > 130560) {
2020 err = -EINVAL;
2021 goto abort;
2022 }
2023
2024 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002025 outbuf = memdup_user(buf + outtotal, taskout);
2026 if (IS_ERR(outbuf)) {
2027 err = PTR_ERR(outbuf);
2028 outbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002029 goto abort;
2030 }
2031 outbuf_dma = pci_map_single(dd->pdev,
2032 outbuf,
2033 taskout,
2034 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002035 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002036 err = -ENOMEM;
2037 goto abort;
2038 }
2039 dma_buffer = outbuf_dma;
2040 }
2041
2042 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002043 inbuf = memdup_user(buf + intotal, taskin);
2044 if (IS_ERR(inbuf)) {
2045 err = PTR_ERR(inbuf);
2046 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002047 goto abort;
2048 }
2049 inbuf_dma = pci_map_single(dd->pdev,
2050 inbuf,
2051 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002052 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002053 err = -ENOMEM;
2054 goto abort;
2055 }
2056 dma_buffer = inbuf_dma;
2057 }
2058
2059 /* only supports PIO and non-data commands from this ioctl. */
2060 switch (req_task->data_phase) {
2061 case TASKFILE_OUT:
2062 nsect = taskout / ATA_SECT_SIZE;
2063 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2064 break;
2065 case TASKFILE_IN:
2066 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2067 break;
2068 case TASKFILE_NO_DATA:
2069 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2070 break;
2071 default:
2072 err = -EINVAL;
2073 goto abort;
2074 }
2075
Sam Bradshaw88523a62011-08-30 08:34:26 -06002076 /* Build the FIS. */
2077 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2078
2079 fis.type = 0x27;
2080 fis.opts = 1 << 7;
2081 fis.command = req_task->io_ports[7];
2082 fis.features = req_task->io_ports[1];
2083 fis.sect_count = req_task->io_ports[2];
2084 fis.lba_low = req_task->io_ports[3];
2085 fis.lba_mid = req_task->io_ports[4];
2086 fis.lba_hi = req_task->io_ports[5];
2087 /* Clear the dev bit*/
2088 fis.device = req_task->io_ports[6] & ~0x10;
2089
2090 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2091 req_task->in_flags.all =
2092 IDE_TASKFILE_STD_IN_FLAGS |
2093 (IDE_HOB_STD_IN_FLAGS << 8);
2094 fis.lba_low_ex = req_task->hob_ports[3];
2095 fis.lba_mid_ex = req_task->hob_ports[4];
2096 fis.lba_hi_ex = req_task->hob_ports[5];
2097 fis.features_ex = req_task->hob_ports[1];
2098 fis.sect_cnt_ex = req_task->hob_ports[2];
2099
2100 } else {
2101 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2102 }
2103
2104 force_single_sector = implicit_sector(fis.command, fis.features);
2105
2106 if ((taskin || taskout) && (!fis.sect_count)) {
2107 if (nsect)
2108 fis.sect_count = nsect;
2109 else {
2110 if (!force_single_sector) {
2111 dev_warn(&dd->pdev->dev,
2112 "data movement but "
2113 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002114 err = -EINVAL;
2115 goto abort;
2116 }
2117 }
2118 }
2119
2120 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002121 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002122 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2123 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002124 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002125 fis.command,
2126 fis.features,
2127 fis.sect_count,
2128 fis.lba_low,
2129 fis.lba_mid,
2130 fis.lba_hi,
2131 fis.device);
2132
Selvan Mani4453bc82012-09-27 14:36:43 +02002133 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002134 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2135 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002136 erasemode = 1;
2137 }
2138
2139 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002140
2141 /* Determine the correct transfer size.*/
2142 if (force_single_sector)
2143 transfer_size = ATA_SECT_SIZE;
2144 else
2145 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2146
2147 /* Execute the command.*/
2148 if (mtip_exec_internal_command(dd->port,
2149 &fis,
2150 5,
2151 dma_buffer,
2152 transfer_size,
2153 0,
2154 GFP_KERNEL,
2155 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002156 err = -EIO;
2157 goto abort;
2158 }
2159
2160 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2161
2162 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2163 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2164 req_task->io_ports[7] = reply->control;
2165 } else {
2166 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2167 req_task->io_ports[7] = reply->command;
2168 }
2169
2170 /* reclaim the DMA buffers.*/
2171 if (inbuf_dma)
2172 pci_unmap_single(dd->pdev, inbuf_dma,
2173 taskin, DMA_FROM_DEVICE);
2174 if (outbuf_dma)
2175 pci_unmap_single(dd->pdev, outbuf_dma,
2176 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002177 inbuf_dma = 0;
2178 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002179
2180 /* return the ATA registers to the caller.*/
2181 req_task->io_ports[1] = reply->features;
2182 req_task->io_ports[2] = reply->sect_count;
2183 req_task->io_ports[3] = reply->lba_low;
2184 req_task->io_ports[4] = reply->lba_mid;
2185 req_task->io_ports[5] = reply->lba_hi;
2186 req_task->io_ports[6] = reply->device;
2187
2188 if (req_task->out_flags.all & 1) {
2189
2190 req_task->hob_ports[3] = reply->lba_low_ex;
2191 req_task->hob_ports[4] = reply->lba_mid_ex;
2192 req_task->hob_ports[5] = reply->lba_hi_ex;
2193 req_task->hob_ports[1] = reply->features_ex;
2194 req_task->hob_ports[2] = reply->sect_cnt_ex;
2195 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002197 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002198 "err %x, sect_cnt %x, lbalo %x,"
2199 "lbamid %x, lbahi %x, dev %x\n",
2200 __func__,
2201 req_task->io_ports[7],
2202 req_task->io_ports[1],
2203 req_task->io_ports[2],
2204 req_task->io_ports[3],
2205 req_task->io_ports[4],
2206 req_task->io_ports[5],
2207 req_task->io_ports[6]);
2208
Sam Bradshaw88523a62011-08-30 08:34:26 -06002209 if (taskout) {
2210 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2211 err = -EFAULT;
2212 goto abort;
2213 }
2214 }
2215 if (taskin) {
2216 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2217 err = -EFAULT;
2218 goto abort;
2219 }
2220 }
2221abort:
2222 if (inbuf_dma)
2223 pci_unmap_single(dd->pdev, inbuf_dma,
2224 taskin, DMA_FROM_DEVICE);
2225 if (outbuf_dma)
2226 pci_unmap_single(dd->pdev, outbuf_dma,
2227 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002228 kfree(outbuf);
2229 kfree(inbuf);
2230
2231 return err;
2232}
2233
2234/*
2235 * Handle IOCTL calls from the Block Layer.
2236 *
2237 * This function is called by the Block Layer when it receives an IOCTL
2238 * command that it does not understand. If the IOCTL command is not supported
2239 * this function returns -ENOTTY.
2240 *
2241 * @dd Pointer to the driver data structure.
2242 * @cmd IOCTL command passed from the Block Layer.
2243 * @arg IOCTL argument passed from the Block Layer.
2244 *
2245 * return value
2246 * 0 The IOCTL completed successfully.
2247 * -ENOTTY The specified command is not supported.
2248 * -EFAULT An error occurred copying data to a user space buffer.
2249 * -EIO An error occurred while executing the command.
2250 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002251static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2252 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002253{
2254 switch (cmd) {
2255 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002256 {
2257 if (copy_to_user((void __user *)arg, dd->port->identify,
2258 sizeof(u16) * ATA_ID_WORDS))
2259 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002260 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002261 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002262 case HDIO_DRIVE_CMD:
2263 {
2264 u8 drive_command[4];
2265
2266 /* Copy the user command info to our buffer. */
2267 if (copy_from_user(drive_command,
2268 (void __user *) arg,
2269 sizeof(drive_command)))
2270 return -EFAULT;
2271
2272 /* Execute the drive command. */
2273 if (exec_drive_command(dd->port,
2274 drive_command,
2275 (void __user *) (arg+4)))
2276 return -EIO;
2277
2278 /* Copy the status back to the users buffer. */
2279 if (copy_to_user((void __user *) arg,
2280 drive_command,
2281 sizeof(drive_command)))
2282 return -EFAULT;
2283
2284 break;
2285 }
2286 case HDIO_DRIVE_TASK:
2287 {
2288 u8 drive_command[7];
2289
2290 /* Copy the user command info to our buffer. */
2291 if (copy_from_user(drive_command,
2292 (void __user *) arg,
2293 sizeof(drive_command)))
2294 return -EFAULT;
2295
2296 /* Execute the drive command. */
2297 if (exec_drive_task(dd->port, drive_command))
2298 return -EIO;
2299
2300 /* Copy the status back to the users buffer. */
2301 if (copy_to_user((void __user *) arg,
2302 drive_command,
2303 sizeof(drive_command)))
2304 return -EFAULT;
2305
2306 break;
2307 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002308 case HDIO_DRIVE_TASKFILE: {
2309 ide_task_request_t req_task;
2310 int ret, outtotal;
2311
2312 if (copy_from_user(&req_task, (void __user *) arg,
2313 sizeof(req_task)))
2314 return -EFAULT;
2315
2316 outtotal = sizeof(req_task);
2317
2318 ret = exec_drive_taskfile(dd, (void __user *) arg,
2319 &req_task, outtotal);
2320
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002321 if (copy_to_user((void __user *) arg, &req_task,
2322 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002323 return -EFAULT;
2324
2325 return ret;
2326 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002327
2328 default:
2329 return -EINVAL;
2330 }
2331 return 0;
2332}
2333
2334/*
2335 * Submit an IO to the hw
2336 *
2337 * This function is called by the block layer to issue an io
2338 * to the device. Upon completion, the callback function will
2339 * be called with the data parameter passed as the callback data.
2340 *
2341 * @dd Pointer to the driver data structure.
2342 * @start First sector to read.
2343 * @nsect Number of sectors to read.
2344 * @nents Number of entries in scatter list for the read command.
2345 * @tag The tag of this read command.
2346 * @callback Pointer to the function that should be called
2347 * when the read completes.
2348 * @data Callback data passed to the callback function
2349 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002350 * @dir Direction (read or write)
2351 *
2352 * return value
2353 * None
2354 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002355static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2356 struct mtip_cmd *command, int nents,
2357 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002358{
2359 struct host_to_dev_fis *fis;
2360 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002361 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2362 u64 start = blk_rq_pos(rq);
2363 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002364
2365 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002366 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002367
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002368 prefetch(&port->flags);
2369
Sam Bradshaw88523a62011-08-30 08:34:26 -06002370 command->scatter_ents = nents;
2371
2372 /*
2373 * The number of retries for this command before it is
2374 * reported as a failure to the upper layers.
2375 */
2376 command->retries = MTIP_MAX_RETRIES;
2377
2378 /* Fill out fis */
2379 fis = command->command;
2380 fis->type = 0x27;
2381 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002382 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002383 fis->command = ATA_CMD_FPDMA_READ;
2384 else
2385 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002386 fis->lba_low = start & 0xFF;
2387 fis->lba_mid = (start >> 8) & 0xFF;
2388 fis->lba_hi = (start >> 16) & 0xFF;
2389 fis->lba_low_ex = (start >> 24) & 0xFF;
2390 fis->lba_mid_ex = (start >> 32) & 0xFF;
2391 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002392 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002393 fis->features = nsect & 0xFF;
2394 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002395 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002396 fis->sect_cnt_ex = 0;
2397 fis->control = 0;
2398 fis->res2 = 0;
2399 fis->res3 = 0;
2400 fill_command_sg(dd, command, nents);
2401
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002402 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002403 fis->device |= 1 << 7;
2404
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002406 command->command_header->opts =
2407 __force_bit2int cpu_to_le32(
2408 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 command->command_header->byte_count = 0;
2410
2411 /*
2412 * Set the completion function and data for the command
2413 * within this layer.
2414 */
2415 command->comp_data = dd;
2416 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002417 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418
2419 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002420 * To prevent this command from being issued
2421 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002423 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002424 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002425 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002426 return;
2427 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002428
2429 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002430 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431}
2432
2433/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002434 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435 *
2436 * @dev Pointer to the device structure, passed by the kernrel.
2437 * @attr Pointer to the device_attribute structure passed by the kernel.
2438 * @buf Pointer to the char buffer that will receive the stats info.
2439 *
2440 * return value
2441 * The size, in bytes, of the data copied into buf.
2442 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002443static ssize_t mtip_hw_show_status(struct device *dev,
2444 struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct driver_data *dd = dev_to_disk(dev)->private_data;
2448 int size = 0;
2449
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002450 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002451 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002452 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002453 size += sprintf(buf, "%s", "write_protect\n");
2454 else
2455 size += sprintf(buf, "%s", "online\n");
2456
2457 return size;
2458}
2459
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002460static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002461
Asai Thambi S P0caff002013-04-03 19:56:21 +05302462/* debugsfs entries */
2463
2464static ssize_t show_device_status(struct device_driver *drv, char *buf)
2465{
2466 int size = 0;
2467 struct driver_data *dd, *tmp;
2468 unsigned long flags;
2469 char id_buf[42];
2470 u16 status = 0;
2471
2472 spin_lock_irqsave(&dev_lock, flags);
2473 size += sprintf(&buf[size], "Devices Present:\n");
2474 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002475 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302476 if (dd->port &&
2477 dd->port->identify &&
2478 dd->port->identify_valid) {
2479 strlcpy(id_buf,
2480 (char *) (dd->port->identify + 10), 21);
2481 status = *(dd->port->identify + 141);
2482 } else {
2483 memset(id_buf, 0, 42);
2484 status = 0;
2485 }
2486
2487 if (dd->port &&
2488 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2489 size += sprintf(&buf[size],
2490 " device %s %s (ftl rebuild %d %%)\n",
2491 dev_name(&dd->pdev->dev),
2492 id_buf,
2493 status);
2494 } else {
2495 size += sprintf(&buf[size],
2496 " device %s %s\n",
2497 dev_name(&dd->pdev->dev),
2498 id_buf);
2499 }
2500 }
2501 }
2502
2503 size += sprintf(&buf[size], "Devices Being Removed:\n");
2504 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002505 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302506 if (dd->port &&
2507 dd->port->identify &&
2508 dd->port->identify_valid) {
2509 strlcpy(id_buf,
2510 (char *) (dd->port->identify+10), 21);
2511 status = *(dd->port->identify + 141);
2512 } else {
2513 memset(id_buf, 0, 42);
2514 status = 0;
2515 }
2516
2517 if (dd->port &&
2518 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2519 size += sprintf(&buf[size],
2520 " device %s %s (ftl rebuild %d %%)\n",
2521 dev_name(&dd->pdev->dev),
2522 id_buf,
2523 status);
2524 } else {
2525 size += sprintf(&buf[size],
2526 " device %s %s\n",
2527 dev_name(&dd->pdev->dev),
2528 id_buf);
2529 }
2530 }
2531 }
2532 spin_unlock_irqrestore(&dev_lock, flags);
2533
2534 return size;
2535}
2536
2537static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2538 size_t len, loff_t *offset)
2539{
David Milburnc8afd0d2013-05-23 16:23:45 -05002540 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302541 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002542 char *buf;
2543 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302544
2545 if (!len || *offset)
2546 return 0;
2547
David Milburnc8afd0d2013-05-23 16:23:45 -05002548 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2549 if (!buf) {
2550 dev_err(&dd->pdev->dev,
2551 "Memory allocation: status buffer\n");
2552 return -ENOMEM;
2553 }
2554
Asai Thambi S P0caff002013-04-03 19:56:21 +05302555 size += show_device_status(NULL, buf);
2556
2557 *offset = size <= len ? size : len;
2558 size = copy_to_user(ubuf, buf, *offset);
2559 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002560 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561
David Milburnc8afd0d2013-05-23 16:23:45 -05002562 kfree(buf);
2563 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302564}
2565
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002566static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2567 size_t len, loff_t *offset)
2568{
2569 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002570 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002571 u32 group_allocated;
2572 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002573 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002574
2575 if (!len || size)
2576 return 0;
2577
David Milburnc8afd0d2013-05-23 16:23:45 -05002578 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2579 if (!buf) {
2580 dev_err(&dd->pdev->dev,
2581 "Memory allocation: register buffer\n");
2582 return -ENOMEM;
2583 }
2584
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002585 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2586
2587 for (n = dd->slot_groups-1; n >= 0; n--)
2588 size += sprintf(&buf[size], "%08X ",
2589 readl(dd->port->s_active[n]));
2590
2591 size += sprintf(&buf[size], "]\n");
2592 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2593
2594 for (n = dd->slot_groups-1; n >= 0; n--)
2595 size += sprintf(&buf[size], "%08X ",
2596 readl(dd->port->cmd_issue[n]));
2597
2598 size += sprintf(&buf[size], "]\n");
2599 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2600
2601 for (n = dd->slot_groups-1; n >= 0; n--)
2602 size += sprintf(&buf[size], "%08X ",
2603 readl(dd->port->completed[n]));
2604
2605 size += sprintf(&buf[size], "]\n");
2606 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2607 readl(dd->port->mmio + PORT_IRQ_STAT));
2608 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2609 readl(dd->mmio + HOST_IRQ_STAT));
2610 size += sprintf(&buf[size], "\n");
2611
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002612 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--) {
2615 if (sizeof(long) > sizeof(u32))
2616 group_allocated =
2617 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2618 else
2619 group_allocated = dd->port->cmds_to_issue[n];
2620 size += sprintf(&buf[size], "%08X ", group_allocated);
2621 }
2622 size += sprintf(&buf[size], "]\n");
2623
2624 *offset = size <= len ? size : len;
2625 size = copy_to_user(ubuf, buf, *offset);
2626 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002627 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002628
David Milburnc8afd0d2013-05-23 16:23:45 -05002629 kfree(buf);
2630 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002631}
2632
2633static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2634 size_t len, loff_t *offset)
2635{
2636 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002637 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002638 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002639 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002640
2641 if (!len || size)
2642 return 0;
2643
David Milburnc8afd0d2013-05-23 16:23:45 -05002644 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2645 if (!buf) {
2646 dev_err(&dd->pdev->dev,
2647 "Memory allocation: flag buffer\n");
2648 return -ENOMEM;
2649 }
2650
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002651 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2652 dd->port->flags);
2653 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2654 dd->dd_flag);
2655
2656 *offset = size <= len ? size : len;
2657 size = copy_to_user(ubuf, buf, *offset);
2658 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002659 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002660
David Milburnc8afd0d2013-05-23 16:23:45 -05002661 kfree(buf);
2662 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002663}
2664
Asai Thambi S P0caff002013-04-03 19:56:21 +05302665static const struct file_operations mtip_device_status_fops = {
2666 .owner = THIS_MODULE,
2667 .open = simple_open,
2668 .read = mtip_hw_read_device_status,
2669 .llseek = no_llseek,
2670};
2671
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002672static const struct file_operations mtip_regs_fops = {
2673 .owner = THIS_MODULE,
2674 .open = simple_open,
2675 .read = mtip_hw_read_registers,
2676 .llseek = no_llseek,
2677};
2678
2679static const struct file_operations mtip_flags_fops = {
2680 .owner = THIS_MODULE,
2681 .open = simple_open,
2682 .read = mtip_hw_read_flags,
2683 .llseek = no_llseek,
2684};
2685
Sam Bradshaw88523a62011-08-30 08:34:26 -06002686/*
2687 * Create the sysfs related attributes.
2688 *
2689 * @dd Pointer to the driver data structure.
2690 * @kobj Pointer to the kobj for the block device.
2691 *
2692 * return value
2693 * 0 Operation completed successfully.
2694 * -EINVAL Invalid parameter.
2695 */
Jens Axboe63166682011-09-27 21:27:43 -06002696static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002697{
2698 if (!kobj || !dd)
2699 return -EINVAL;
2700
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002701 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2702 dev_warn(&dd->pdev->dev,
2703 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002704 return 0;
2705}
2706
2707/*
2708 * Remove the sysfs related attributes.
2709 *
2710 * @dd Pointer to the driver data structure.
2711 * @kobj Pointer to the kobj for the block device.
2712 *
2713 * return value
2714 * 0 Operation completed successfully.
2715 * -EINVAL Invalid parameter.
2716 */
Jens Axboe63166682011-09-27 21:27:43 -06002717static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002718{
2719 if (!kobj || !dd)
2720 return -EINVAL;
2721
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002722 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002723
2724 return 0;
2725}
2726
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002727static int mtip_hw_debugfs_init(struct driver_data *dd)
2728{
2729 if (!dfs_parent)
2730 return -1;
2731
2732 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2733 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2734 dev_warn(&dd->pdev->dev,
2735 "Error creating node %s under debugfs\n",
2736 dd->disk->disk_name);
2737 dd->dfs_node = NULL;
2738 return -1;
2739 }
2740
2741 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2742 &mtip_flags_fops);
2743 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2744 &mtip_regs_fops);
2745
2746 return 0;
2747}
2748
2749static void mtip_hw_debugfs_exit(struct driver_data *dd)
2750{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002751 if (dd->dfs_node)
2752 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002753}
2754
Sam Bradshaw88523a62011-08-30 08:34:26 -06002755/*
2756 * Perform any init/resume time hardware setup
2757 *
2758 * @dd Pointer to the driver data structure.
2759 *
2760 * return value
2761 * None
2762 */
2763static inline void hba_setup(struct driver_data *dd)
2764{
2765 u32 hwdata;
2766 hwdata = readl(dd->mmio + HOST_HSORG);
2767
2768 /* interrupt bug workaround: use only 1 IS bit.*/
2769 writel(hwdata |
2770 HSORG_DISABLE_SLOTGRP_INTR |
2771 HSORG_DISABLE_SLOTGRP_PXIS,
2772 dd->mmio + HOST_HSORG);
2773}
2774
Asai Thambi S P2077d942013-04-29 21:19:49 +02002775static int mtip_device_unaligned_constrained(struct driver_data *dd)
2776{
2777 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2778}
2779
Sam Bradshaw88523a62011-08-30 08:34:26 -06002780/*
2781 * Detect the details of the product, and store anything needed
2782 * into the driver data structure. This includes product type and
2783 * version and number of slot groups.
2784 *
2785 * @dd Pointer to the driver data structure.
2786 *
2787 * return value
2788 * None
2789 */
2790static void mtip_detect_product(struct driver_data *dd)
2791{
2792 u32 hwdata;
2793 unsigned int rev, slotgroups;
2794
2795 /*
2796 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2797 * info register:
2798 * [15:8] hardware/software interface rev#
2799 * [ 3] asic-style interface
2800 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2801 */
2802 hwdata = readl(dd->mmio + HOST_HSORG);
2803
2804 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2805 dd->slot_groups = 1;
2806
2807 if (hwdata & 0x8) {
2808 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2809 rev = (hwdata & HSORG_HWREV) >> 8;
2810 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2811 dev_info(&dd->pdev->dev,
2812 "ASIC-FPGA design, HS rev 0x%x, "
2813 "%i slot groups [%i slots]\n",
2814 rev,
2815 slotgroups,
2816 slotgroups * 32);
2817
2818 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2819 dev_warn(&dd->pdev->dev,
2820 "Warning: driver only supports "
2821 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2822 slotgroups = MTIP_MAX_SLOT_GROUPS;
2823 }
2824 dd->slot_groups = slotgroups;
2825 return;
2826 }
2827
2828 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2829}
2830
2831/*
2832 * Blocking wait for FTL rebuild to complete
2833 *
2834 * @dd Pointer to the DRIVER_DATA structure.
2835 *
2836 * return value
2837 * 0 FTL rebuild completed successfully
2838 * -EFAULT FTL rebuild error/timeout/interruption
2839 */
2840static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2841{
2842 unsigned long timeout, cnt = 0, start;
2843
2844 dev_warn(&dd->pdev->dev,
2845 "FTL rebuild in progress. Polling for completion.\n");
2846
2847 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002848 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2849
2850 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002851 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002852 &dd->dd_flag)))
2853 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002854 if (mtip_check_surprise_removal(dd->pdev))
2855 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002856
Sam Bradshaw88523a62011-08-30 08:34:26 -06002857 if (mtip_get_identify(dd->port, NULL) < 0)
2858 return -EFAULT;
2859
2860 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2861 MTIP_FTL_REBUILD_MAGIC) {
2862 ssleep(1);
2863 /* Print message every 3 minutes */
2864 if (cnt++ >= 180) {
2865 dev_warn(&dd->pdev->dev,
2866 "FTL rebuild in progress (%d secs).\n",
2867 jiffies_to_msecs(jiffies - start) / 1000);
2868 cnt = 0;
2869 }
2870 } else {
2871 dev_warn(&dd->pdev->dev,
2872 "FTL rebuild complete (%d secs).\n",
2873 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002874 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002875 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002876 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002877 } while (time_before(jiffies, timeout));
2878
2879 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002880 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002881 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2882 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002883 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002884}
2885
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002886static void mtip_softirq_done_fn(struct request *rq)
2887{
2888 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2889 struct driver_data *dd = rq->q->queuedata;
2890
2891 /* Unmap the DMA scatter list entries */
2892 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2893 cmd->direction);
2894
2895 if (unlikely(cmd->unaligned))
2896 up(&dd->port->cmd_slot_unal);
2897
2898 blk_mq_end_request(rq, rq->errors);
2899}
2900
2901static void mtip_abort_cmd(struct request *req, void *data,
2902 bool reserved)
2903{
2904 struct driver_data *dd = data;
2905
2906 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2907
2908 clear_bit(req->tag, dd->port->cmds_to_issue);
2909 req->errors = -EIO;
2910 mtip_softirq_done_fn(req);
2911}
2912
2913static void mtip_queue_cmd(struct request *req, void *data,
2914 bool reserved)
2915{
2916 struct driver_data *dd = data;
2917
2918 set_bit(req->tag, dd->port->cmds_to_issue);
2919 blk_abort_request(req);
2920}
2921
Sam Bradshaw88523a62011-08-30 08:34:26 -06002922/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002923 * service thread to issue queued commands
2924 *
2925 * @data Pointer to the driver data structure.
2926 *
2927 * return value
2928 * 0
2929 */
2930
2931static int mtip_service_thread(void *data)
2932{
2933 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002934 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002935 unsigned int num_cmd_slots = dd->slot_groups * 32;
2936 struct mtip_port *port = dd->port;
2937
2938 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002939 if (kthread_should_stop() ||
2940 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2941 goto st_out;
2942 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2943
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002944 /*
2945 * the condition is to check neither an internal command is
2946 * is in progress nor error handling is active
2947 */
2948 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002949 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002950
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002951 if (kthread_should_stop() ||
2952 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2953 goto st_out;
2954
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002955 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002956 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002957 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002958
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002959 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2960
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002961restart_eh:
2962 /* Demux bits: start with error handling */
2963 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2964 mtip_handle_tfe(dd);
2965 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2966 }
2967
2968 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2969 goto restart_eh;
2970
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002971 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2972 to = jiffies + msecs_to_jiffies(5000);
2973
2974 do {
2975 mdelay(100);
2976 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2977 time_before(jiffies, to));
2978
2979 if (atomic_read(&dd->irq_workers_active) != 0)
2980 dev_warn(&dd->pdev->dev,
2981 "Completion workers still active!");
2982
2983 spin_lock(dd->queue->queue_lock);
2984 blk_mq_all_tag_busy_iter(*dd->tags.tags,
2985 mtip_queue_cmd, dd);
2986 spin_unlock(dd->queue->queue_lock);
2987
2988 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2989
2990 if (mtip_device_reset(dd))
2991 blk_mq_all_tag_busy_iter(*dd->tags.tags,
2992 mtip_abort_cmd, dd);
2993
2994 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
2995 }
2996
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002997 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002998 slot = 1;
2999 /* used to restrict the loop to one iteration */
3000 slot_start = num_cmd_slots;
3001 slot_wrap = 0;
3002 while (1) {
3003 slot = find_next_bit(port->cmds_to_issue,
3004 num_cmd_slots, slot);
3005 if (slot_wrap == 1) {
3006 if ((slot_start >= slot) ||
3007 (slot >= num_cmd_slots))
3008 break;
3009 }
3010 if (unlikely(slot_start == num_cmd_slots))
3011 slot_start = slot;
3012
3013 if (unlikely(slot == num_cmd_slots)) {
3014 slot = 1;
3015 slot_wrap = 1;
3016 continue;
3017 }
3018
3019 /* Issue the command to the hardware */
3020 mtip_issue_ncq_command(port, slot);
3021
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003022 clear_bit(slot, port->cmds_to_issue);
3023 }
3024
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003025 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003026 }
3027
3028 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003029 if (mtip_ftl_rebuild_poll(dd) == 0)
3030 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003031 }
3032 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003033
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003034st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003035 return 0;
3036}
3037
3038/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003039 * DMA region teardown
3040 *
3041 * @dd Pointer to driver_data structure
3042 *
3043 * return value
3044 * None
3045 */
3046static void mtip_dma_free(struct driver_data *dd)
3047{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003048 struct mtip_port *port = dd->port;
3049
3050 if (port->block1)
3051 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3052 port->block1, port->block1_dma);
3053
3054 if (port->command_list) {
3055 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3056 port->command_list, port->command_list_dma);
3057 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003058}
3059
3060/*
3061 * DMA region setup
3062 *
3063 * @dd Pointer to driver_data structure
3064 *
3065 * return value
3066 * -ENOMEM Not enough free DMA region space to initialize driver
3067 */
3068static int mtip_dma_alloc(struct driver_data *dd)
3069{
3070 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003071
3072 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3073 port->block1 =
3074 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3075 &port->block1_dma, GFP_KERNEL);
3076 if (!port->block1)
3077 return -ENOMEM;
3078 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3079
3080 /* Allocate dma memory for command list */
3081 port->command_list =
3082 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3083 &port->command_list_dma, GFP_KERNEL);
3084 if (!port->command_list) {
3085 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3086 port->block1, port->block1_dma);
3087 port->block1 = NULL;
3088 port->block1_dma = 0;
3089 return -ENOMEM;
3090 }
3091 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3092
3093 /* Setup all pointers into first DMA region */
3094 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3095 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3096 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3097 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3098 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3099 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3100 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3101 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3102
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003103 return 0;
3104}
3105
Jens Axboeffc771b2014-05-09 09:42:02 -06003106static int mtip_hw_get_identify(struct driver_data *dd)
3107{
3108 struct smart_attr attr242;
3109 unsigned char *buf;
3110 int rv;
3111
3112 if (mtip_get_identify(dd->port, NULL) < 0)
3113 return -EFAULT;
3114
3115 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3116 MTIP_FTL_REBUILD_MAGIC) {
3117 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3118 return MTIP_FTL_REBUILD_MAGIC;
3119 }
3120 mtip_dump_identify(dd->port);
3121
3122 /* check write protect, over temp and rebuild statuses */
3123 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3124 dd->port->log_buf,
3125 dd->port->log_buf_dma, 1);
3126 if (rv) {
3127 dev_warn(&dd->pdev->dev,
3128 "Error in READ LOG EXT (10h) command\n");
3129 /* non-critical error, don't fail the load */
3130 } else {
3131 buf = (unsigned char *)dd->port->log_buf;
3132 if (buf[259] & 0x1) {
3133 dev_info(&dd->pdev->dev,
3134 "Write protect bit is set.\n");
3135 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3136 }
3137 if (buf[288] == 0xF7) {
3138 dev_info(&dd->pdev->dev,
3139 "Exceeded Tmax, drive in thermal shutdown.\n");
3140 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3141 }
3142 if (buf[288] == 0xBF) {
3143 dev_info(&dd->pdev->dev,
3144 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003145 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06003146 }
3147 }
3148
3149 /* get write protect progess */
3150 memset(&attr242, 0, sizeof(struct smart_attr));
3151 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3152 dev_warn(&dd->pdev->dev,
3153 "Unable to check write protect progress\n");
3154 else
3155 dev_info(&dd->pdev->dev,
3156 "Write protect progress: %u%% (%u blocks)\n",
3157 attr242.cur, le32_to_cpu(attr242.data));
3158
3159 return rv;
3160}
3161
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003162/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003163 * Called once for each card.
3164 *
3165 * @dd Pointer to the driver data structure.
3166 *
3167 * return value
3168 * 0 on success, else an error code.
3169 */
Jens Axboe63166682011-09-27 21:27:43 -06003170static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003171{
3172 int i;
3173 int rv;
3174 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003175 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003176
3177 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3178
3179 mtip_detect_product(dd);
3180 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3181 rv = -EIO;
3182 goto out1;
3183 }
3184 num_command_slots = dd->slot_groups * 32;
3185
3186 hba_setup(dd);
3187
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003188 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3189 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003190 if (!dd->port) {
3191 dev_err(&dd->pdev->dev,
3192 "Memory allocation: port structure\n");
3193 return -ENOMEM;
3194 }
3195
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003196 /* Continue workqueue setup */
3197 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3198 dd->work[i].port = dd->port;
3199
Asai Thambi S P2077d942013-04-29 21:19:49 +02003200 /* Enable unaligned IO constraints for some devices */
3201 if (mtip_device_unaligned_constrained(dd))
3202 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3203 else
3204 dd->unal_qdepth = 0;
3205
Asai Thambi S P2077d942013-04-29 21:19:49 +02003206 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003207
3208 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003209 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3210 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003211
3212 /* Set the port mmio base address. */
3213 dd->port->mmio = dd->mmio + PORT_OFFSET;
3214 dd->port->dd = dd;
3215
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003216 /* DMA allocations */
3217 rv = mtip_dma_alloc(dd);
3218 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003219 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003220
3221 /* Setup the pointers to the extended s_active and CI registers. */
3222 for (i = 0; i < dd->slot_groups; i++) {
3223 dd->port->s_active[i] =
3224 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3225 dd->port->cmd_issue[i] =
3226 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3227 dd->port->completed[i] =
3228 dd->port->mmio + i*0x80 + PORT_SDBV;
3229 }
3230
Asai Thambi S P45038362012-04-09 08:35:38 +02003231 timetaken = jiffies;
3232 timeout = jiffies + msecs_to_jiffies(30000);
3233 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3234 time_before(jiffies, timeout)) {
3235 mdelay(100);
3236 }
3237 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3238 timetaken = jiffies - timetaken;
3239 dev_warn(&dd->pdev->dev,
3240 "Surprise removal detected at %u ms\n",
3241 jiffies_to_msecs(timetaken));
3242 rv = -ENODEV;
3243 goto out2 ;
3244 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003245 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003246 timetaken = jiffies - timetaken;
3247 dev_warn(&dd->pdev->dev,
3248 "Removal detected at %u ms\n",
3249 jiffies_to_msecs(timetaken));
3250 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003251 goto out2;
3252 }
3253
Asai Thambi S P45038362012-04-09 08:35:38 +02003254 /* Conditionally reset the HBA. */
3255 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3256 if (mtip_hba_reset(dd) < 0) {
3257 dev_err(&dd->pdev->dev,
3258 "Card did not reset within timeout\n");
3259 rv = -EIO;
3260 goto out2;
3261 }
3262 } else {
3263 /* Clear any pending interrupts on the HBA */
3264 writel(readl(dd->mmio + HOST_IRQ_STAT),
3265 dd->mmio + HOST_IRQ_STAT);
3266 }
3267
Sam Bradshaw88523a62011-08-30 08:34:26 -06003268 mtip_init_port(dd->port);
3269 mtip_start_port(dd->port);
3270
3271 /* Setup the ISR and enable interrupts. */
3272 rv = devm_request_irq(&dd->pdev->dev,
3273 dd->pdev->irq,
3274 mtip_irq_handler,
3275 IRQF_SHARED,
3276 dev_driver_string(&dd->pdev->dev),
3277 dd);
3278
3279 if (rv) {
3280 dev_err(&dd->pdev->dev,
3281 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3282 goto out2;
3283 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003284 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003285
3286 /* Enable interrupts on the HBA. */
3287 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3288 dd->mmio + HOST_CTL);
3289
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003290 init_waitqueue_head(&dd->port->svc_wait);
3291
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003292 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003293 rv = -EFAULT;
3294 goto out3;
3295 }
3296
Sam Bradshaw88523a62011-08-30 08:34:26 -06003297 return rv;
3298
3299out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003300 /* Disable interrupts on the HBA. */
3301 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3302 dd->mmio + HOST_CTL);
3303
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003304 /* Release the IRQ. */
3305 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003306 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3307
3308out2:
3309 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003310 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003311
Sam Bradshaw88523a62011-08-30 08:34:26 -06003312out1:
3313 /* Free the memory allocated for the for structure. */
3314 kfree(dd->port);
3315
3316 return rv;
3317}
3318
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003319static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003320{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003321 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003322
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003323 if (dd->sr || !dd->port)
3324 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003325 /*
3326 * Send standby immediate (E0h) to the drive so that it
3327 * saves its state.
3328 */
3329 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003330 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3331 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3332 rv = mtip_standby_immediate(dd->port);
3333 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003334 dev_warn(&dd->pdev->dev,
3335 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003336 }
3337 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003338}
3339
Sam Bradshaw88523a62011-08-30 08:34:26 -06003340/*
3341 * Called to deinitialize an interface.
3342 *
3343 * @dd Pointer to the driver data structure.
3344 *
3345 * return value
3346 * 0
3347 */
Jens Axboe63166682011-09-27 21:27:43 -06003348static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003349{
3350 /*
3351 * Send standby immediate (E0h) to the drive so that it
3352 * saves its state.
3353 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003354 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003355 /* de-initialize the port. */
3356 mtip_deinit_port(dd->port);
3357
3358 /* Disable interrupts on the HBA. */
3359 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3360 dd->mmio + HOST_CTL);
3361 }
3362
Sam Bradshaw88523a62011-08-30 08:34:26 -06003363 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003364 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003365 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003366 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003367
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003368 /* Free dma regions */
3369 mtip_dma_free(dd);
3370
Sam Bradshaw88523a62011-08-30 08:34:26 -06003371 /* Free the memory allocated for the for structure. */
3372 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003373 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003374
3375 return 0;
3376}
3377
3378/*
3379 * Issue a Standby Immediate command to the device.
3380 *
3381 * This function is called by the Block Layer just before the
3382 * system powers off during a shutdown.
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_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003390{
3391 /*
3392 * Send standby immediate (E0h) to the drive so that it
3393 * saves its state.
3394 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003395 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003396
3397 return 0;
3398}
3399
3400/*
3401 * Suspend function
3402 *
3403 * This function is called by the Block Layer just before the
3404 * system hibernates.
3405 *
3406 * @dd Pointer to the driver data structure.
3407 *
3408 * return value
3409 * 0 Suspend was successful
3410 * -EFAULT Suspend was not successful
3411 */
Jens Axboe63166682011-09-27 21:27:43 -06003412static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003413{
3414 /*
3415 * Send standby immediate (E0h) to the drive
3416 * so that it saves its state.
3417 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003418 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003419 dev_err(&dd->pdev->dev,
3420 "Failed standby-immediate command\n");
3421 return -EFAULT;
3422 }
3423
3424 /* Disable interrupts on the HBA.*/
3425 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3426 dd->mmio + HOST_CTL);
3427 mtip_deinit_port(dd->port);
3428
3429 return 0;
3430}
3431
3432/*
3433 * Resume function
3434 *
3435 * This function is called by the Block Layer as the
3436 * system resumes.
3437 *
3438 * @dd Pointer to the driver data structure.
3439 *
3440 * return value
3441 * 0 Resume was successful
3442 * -EFAULT Resume was not successful
3443 */
Jens Axboe63166682011-09-27 21:27:43 -06003444static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003445{
3446 /* Perform any needed hardware setup steps */
3447 hba_setup(dd);
3448
3449 /* Reset the HBA */
3450 if (mtip_hba_reset(dd) != 0) {
3451 dev_err(&dd->pdev->dev,
3452 "Unable to reset the HBA\n");
3453 return -EFAULT;
3454 }
3455
3456 /*
3457 * Enable the port, DMA engine, and FIS reception specific
3458 * h/w in controller.
3459 */
3460 mtip_init_port(dd->port);
3461 mtip_start_port(dd->port);
3462
3463 /* Enable interrupts on the HBA.*/
3464 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3465 dd->mmio + HOST_CTL);
3466
3467 return 0;
3468}
3469
3470/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003471 * Helper function for reusing disk name
3472 * upon hot insertion.
3473 */
3474static int rssd_disk_name_format(char *prefix,
3475 int index,
3476 char *buf,
3477 int buflen)
3478{
3479 const int base = 'z' - 'a' + 1;
3480 char *begin = buf + strlen(prefix);
3481 char *end = buf + buflen;
3482 char *p;
3483 int unit;
3484
3485 p = end - 1;
3486 *p = '\0';
3487 unit = base;
3488 do {
3489 if (p == begin)
3490 return -EINVAL;
3491 *--p = 'a' + (index % unit);
3492 index = (index / unit) - 1;
3493 } while (index >= 0);
3494
3495 memmove(begin, p, end - p);
3496 memcpy(buf, prefix, strlen(prefix));
3497
3498 return 0;
3499}
3500
3501/*
3502 * Block layer IOCTL handler.
3503 *
3504 * @dev Pointer to the block_device structure.
3505 * @mode ignored
3506 * @cmd IOCTL command passed from the user application.
3507 * @arg Argument passed from the user application.
3508 *
3509 * return value
3510 * 0 IOCTL completed successfully.
3511 * -ENOTTY IOCTL not supported or invalid driver data
3512 * structure pointer.
3513 */
3514static int mtip_block_ioctl(struct block_device *dev,
3515 fmode_t mode,
3516 unsigned cmd,
3517 unsigned long arg)
3518{
3519 struct driver_data *dd = dev->bd_disk->private_data;
3520
3521 if (!capable(CAP_SYS_ADMIN))
3522 return -EACCES;
3523
3524 if (!dd)
3525 return -ENOTTY;
3526
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003527 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003528 return -ENOTTY;
3529
Sam Bradshaw88523a62011-08-30 08:34:26 -06003530 switch (cmd) {
3531 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003532 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003533 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003534 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003535 }
3536}
3537
Jens Axboe16d02c02011-09-27 15:50:01 -06003538#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003539/*
3540 * Block layer compat IOCTL handler.
3541 *
3542 * @dev Pointer to the block_device structure.
3543 * @mode ignored
3544 * @cmd IOCTL command passed from the user application.
3545 * @arg Argument passed from the user application.
3546 *
3547 * return value
3548 * 0 IOCTL completed successfully.
3549 * -ENOTTY IOCTL not supported or invalid driver data
3550 * structure pointer.
3551 */
3552static int mtip_block_compat_ioctl(struct block_device *dev,
3553 fmode_t mode,
3554 unsigned cmd,
3555 unsigned long arg)
3556{
3557 struct driver_data *dd = dev->bd_disk->private_data;
3558
3559 if (!capable(CAP_SYS_ADMIN))
3560 return -EACCES;
3561
3562 if (!dd)
3563 return -ENOTTY;
3564
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003565 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003566 return -ENOTTY;
3567
Sam Bradshaw88523a62011-08-30 08:34:26 -06003568 switch (cmd) {
3569 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003570 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003571 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003572 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003573 ide_task_request_t req_task;
3574 int compat_tasksize, outtotal, ret;
3575
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003576 compat_tasksize =
3577 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003578
3579 compat_req_task =
3580 (struct mtip_compat_ide_task_request_s __user *) arg;
3581
3582 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003583 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003584 return -EFAULT;
3585
3586 if (get_user(req_task.out_size, &compat_req_task->out_size))
3587 return -EFAULT;
3588
3589 if (get_user(req_task.in_size, &compat_req_task->in_size))
3590 return -EFAULT;
3591
3592 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3593
3594 ret = exec_drive_taskfile(dd, (void __user *) arg,
3595 &req_task, outtotal);
3596
3597 if (copy_to_user((void __user *) arg, &req_task,
3598 compat_tasksize -
3599 (2 * sizeof(compat_long_t))))
3600 return -EFAULT;
3601
3602 if (put_user(req_task.out_size, &compat_req_task->out_size))
3603 return -EFAULT;
3604
3605 if (put_user(req_task.in_size, &compat_req_task->in_size))
3606 return -EFAULT;
3607
3608 return ret;
3609 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003610 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003611 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003612 }
3613}
Jens Axboe16d02c02011-09-27 15:50:01 -06003614#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003615
3616/*
3617 * Obtain the geometry of the device.
3618 *
3619 * You may think that this function is obsolete, but some applications,
3620 * fdisk for example still used CHS values. This function describes the
3621 * device as having 224 heads and 56 sectors per cylinder. These values are
3622 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3623 * partition is described in terms of a start and end cylinder this means
3624 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3625 * affects performance.
3626 *
3627 * @dev Pointer to the block_device strucutre.
3628 * @geo Pointer to a hd_geometry structure.
3629 *
3630 * return value
3631 * 0 Operation completed successfully.
3632 * -ENOTTY An error occurred while reading the drive capacity.
3633 */
3634static int mtip_block_getgeo(struct block_device *dev,
3635 struct hd_geometry *geo)
3636{
3637 struct driver_data *dd = dev->bd_disk->private_data;
3638 sector_t capacity;
3639
3640 if (!dd)
3641 return -ENOTTY;
3642
3643 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3644 dev_warn(&dd->pdev->dev,
3645 "Could not get drive capacity.\n");
3646 return -ENOTTY;
3647 }
3648
3649 geo->heads = 224;
3650 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003651 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003652 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003653 return 0;
3654}
3655
Asai Thambi SP51c65702016-02-24 21:18:10 -08003656static int mtip_block_open(struct block_device *dev, fmode_t mode)
3657{
3658 struct driver_data *dd;
3659
3660 if (dev && dev->bd_disk) {
3661 dd = (struct driver_data *) dev->bd_disk->private_data;
3662
3663 if (dd) {
3664 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3665 &dd->dd_flag)) {
3666 return -ENODEV;
3667 }
3668 return 0;
3669 }
3670 }
3671 return -ENODEV;
3672}
3673
3674void mtip_block_release(struct gendisk *disk, fmode_t mode)
3675{
3676}
3677
Sam Bradshaw88523a62011-08-30 08:34:26 -06003678/*
3679 * Block device operation function.
3680 *
3681 * This structure contains pointers to the functions required by the block
3682 * layer.
3683 */
3684static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003685 .open = mtip_block_open,
3686 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003687 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003688#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003689 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003690#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003691 .getgeo = mtip_block_getgeo,
3692 .owner = THIS_MODULE
3693};
3694
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003695static inline bool is_se_active(struct driver_data *dd)
3696{
3697 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3698 if (dd->port->ic_pause_timer) {
3699 unsigned long to = dd->port->ic_pause_timer +
3700 msecs_to_jiffies(1000);
3701 if (time_after(jiffies, to)) {
3702 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3703 &dd->port->flags);
3704 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3705 dd->port->ic_pause_timer = 0;
3706 wake_up_interruptible(&dd->port->svc_wait);
3707 return false;
3708 }
3709 }
3710 return true;
3711 }
3712 return false;
3713}
3714
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715/*
3716 * Block layer make request function.
3717 *
3718 * This function is called by the kernel to process a BIO for
3719 * the P320 device.
3720 *
3721 * @queue Pointer to the request queue. Unused other than to obtain
3722 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003723 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003724 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003725 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003726static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003727{
Jens Axboeffc771b2014-05-09 09:42:02 -06003728 struct driver_data *dd = hctx->queue->queuedata;
3729 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3730 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003731
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003732 if (is_se_active(dd))
3733 return -ENODATA;
3734
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003735 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3736 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3737 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003738 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003739 }
3740 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003741 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003742 }
3743 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3744 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003745 rq_data_dir(rq))) {
3746 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003747 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003748 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3749 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003750 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003751 }
3752
Jens Axboeffc771b2014-05-09 09:42:02 -06003753 if (rq->cmd_flags & REQ_DISCARD) {
3754 int err;
3755
3756 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003757 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003758 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003759 }
3760
Jens Axboeffc771b2014-05-09 09:42:02 -06003761 /* Create the scatter list for this request. */
3762 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003763
Jens Axboeffc771b2014-05-09 09:42:02 -06003764 /* Issue the read/write. */
3765 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3766 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003767}
3768
Jens Axboeffc771b2014-05-09 09:42:02 -06003769static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3770 struct request *rq)
3771{
3772 struct driver_data *dd = hctx->queue->queuedata;
3773 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3774
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003775 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003776 return false;
3777
3778 /*
3779 * If unaligned depth must be limited on this controller, mark it
3780 * as unaligned if the IO isn't on a 4k boundary (start of length).
3781 */
3782 if (blk_rq_sectors(rq) <= 64) {
3783 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3784 cmd->unaligned = 1;
3785 }
3786
3787 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3788 return true;
3789
3790 return false;
3791}
3792
Jens Axboe74c45052014-10-29 11:14:52 -06003793static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3794 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003795{
Jens Axboe74c45052014-10-29 11:14:52 -06003796 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003797 int ret;
3798
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003799 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003800 return BLK_MQ_RQ_QUEUE_BUSY;
3801
Christoph Hellwige2490072014-09-13 16:40:09 -07003802 blk_mq_start_request(rq);
3803
Jens Axboeffc771b2014-05-09 09:42:02 -06003804 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003805 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003806 return BLK_MQ_RQ_QUEUE_OK;
3807
3808 rq->errors = ret;
3809 return BLK_MQ_RQ_QUEUE_ERROR;
3810}
3811
3812static void mtip_free_cmd(void *data, struct request *rq,
3813 unsigned int hctx_idx, unsigned int request_idx)
3814{
3815 struct driver_data *dd = data;
3816 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3817
3818 if (!cmd->command)
3819 return;
3820
3821 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3822 cmd->command, cmd->command_dma);
3823}
3824
3825static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3826 unsigned int request_idx, unsigned int numa_node)
3827{
3828 struct driver_data *dd = data;
3829 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3830 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3831
Jeff Moyer74c9c912015-07-29 10:22:50 -04003832 /*
3833 * For flush requests, request_idx starts at the end of the
3834 * tag space. Since we don't support FLUSH/FUA, simply return
3835 * 0 as there's nothing to be done.
3836 */
3837 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3838 return 0;
3839
Jens Axboeffc771b2014-05-09 09:42:02 -06003840 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3841 &cmd->command_dma, GFP_KERNEL);
3842 if (!cmd->command)
3843 return -ENOMEM;
3844
3845 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3846
3847 /* Point the command headers at the command tables. */
3848 cmd->command_header = dd->port->command_list +
3849 (sizeof(struct mtip_cmd_hdr) * request_idx);
3850 cmd->command_header_dma = dd->port->command_list_dma +
3851 (sizeof(struct mtip_cmd_hdr) * request_idx);
3852
3853 if (host_cap_64)
3854 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3855
3856 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3857
3858 sg_init_table(cmd->sg, MTIP_MAX_SG);
3859 return 0;
3860}
3861
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003862static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3863 bool reserved)
3864{
3865 struct driver_data *dd = req->q->queuedata;
3866 int ret = BLK_EH_RESET_TIMER;
3867
3868 if (reserved)
3869 goto exit_handler;
3870
3871 if (test_bit(req->tag, dd->port->cmds_to_issue))
3872 goto exit_handler;
3873
3874 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3875 goto exit_handler;
3876
3877 wake_up_interruptible(&dd->port->svc_wait);
3878exit_handler:
3879 return ret;
3880}
3881
Jens Axboeffc771b2014-05-09 09:42:02 -06003882static struct blk_mq_ops mtip_mq_ops = {
3883 .queue_rq = mtip_queue_rq,
3884 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003885 .init_request = mtip_init_cmd,
3886 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003887 .complete = mtip_softirq_done_fn,
3888 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003889};
3890
Sam Bradshaw88523a62011-08-30 08:34:26 -06003891/*
3892 * Block layer initialization function.
3893 *
3894 * This function is called once by the PCI layer for each P320
3895 * device that is connected to the system.
3896 *
3897 * @dd Pointer to the driver data structure.
3898 *
3899 * return value
3900 * 0 on success else an error code.
3901 */
Jens Axboe63166682011-09-27 21:27:43 -06003902static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003903{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003904 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003905 sector_t capacity;
3906 unsigned int index = 0;
3907 struct kobject *kobj;
3908
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003909 if (dd->disk)
3910 goto skip_create_disk; /* hw init done, before rebuild */
3911
Jens Axboeffc771b2014-05-09 09:42:02 -06003912 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003913 rv = -EINVAL;
3914 goto protocol_init_error;
3915 }
3916
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003917 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003918 if (dd->disk == NULL) {
3919 dev_err(&dd->pdev->dev,
3920 "Unable to allocate gendisk structure\n");
3921 rv = -EINVAL;
3922 goto alloc_disk_error;
3923 }
3924
3925 /* Generate the disk name, implemented same as in sd.c */
3926 do {
3927 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3928 goto ida_get_error;
3929
3930 spin_lock(&rssd_index_lock);
3931 rv = ida_get_new(&rssd_index_ida, &index);
3932 spin_unlock(&rssd_index_lock);
3933 } while (rv == -EAGAIN);
3934
3935 if (rv)
3936 goto ida_get_error;
3937
3938 rv = rssd_disk_name_format("rssd",
3939 index,
3940 dd->disk->disk_name,
3941 DISK_NAME_LEN);
3942 if (rv)
3943 goto disk_index_error;
3944
3945 dd->disk->driverfs_dev = &dd->pdev->dev;
3946 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003947 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3948 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003949 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003950 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003951 dd->index = index;
3952
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003953 mtip_hw_debugfs_init(dd);
3954
Jens Axboeffc771b2014-05-09 09:42:02 -06003955 memset(&dd->tags, 0, sizeof(dd->tags));
3956 dd->tags.ops = &mtip_mq_ops;
3957 dd->tags.nr_hw_queues = 1;
3958 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3959 dd->tags.reserved_tags = 1;
3960 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3961 dd->tags.numa_node = dd->numa_node;
3962 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3963 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003964 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003965
3966 rv = blk_mq_alloc_tag_set(&dd->tags);
3967 if (rv) {
3968 dev_err(&dd->pdev->dev,
3969 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003970 goto block_queue_alloc_init_error;
3971 }
3972
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003973 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003974 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003975 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003976 dev_err(&dd->pdev->dev,
3977 "Unable to allocate request queue\n");
3978 rv = -ENOMEM;
3979 goto block_queue_alloc_init_error;
3980 }
3981
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003982 dd->disk->queue = dd->queue;
3983 dd->queue->queuedata = dd;
3984
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003985skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003986 /* Initialize the protocol layer. */
3987 wait_for_rebuild = mtip_hw_get_identify(dd);
3988 if (wait_for_rebuild < 0) {
3989 dev_err(&dd->pdev->dev,
3990 "Protocol layer initialization failed\n");
3991 rv = -EINVAL;
3992 goto init_hw_cmds_error;
3993 }
3994
3995 /*
3996 * if rebuild pending, start the service thread, and delay the block
3997 * queue creation and add_disk()
3998 */
3999 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4000 goto start_service_thread;
4001
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004002 /* Set device limits. */
4003 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06004004 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004005 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4006 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004007 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4008 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004009 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00004010 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004011
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01004012 /*
4013 * write back cache is not supported in the device. FUA depends on
4014 * write back cache support, hence setting flush support to zero.
4015 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004016 blk_queue_flush(dd->queue, 0);
4017
Asai Thambi S P15283462013-01-11 14:41:34 +01004018 /* Signal trim support */
4019 if (dd->trim_supp == true) {
4020 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4021 dd->queue->limits.discard_granularity = 4096;
4022 blk_queue_max_discard_sectors(dd->queue,
4023 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4024 dd->queue->limits.discard_zeroes_data = 0;
4025 }
4026
Sam Bradshaw88523a62011-08-30 08:34:26 -06004027 /* Set the capacity of the device in 512 byte sectors. */
4028 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4029 dev_warn(&dd->pdev->dev,
4030 "Could not read drive capacity\n");
4031 rv = -EIO;
4032 goto read_capacity_error;
4033 }
4034 set_capacity(dd->disk, capacity);
4035
4036 /* Enable the block device and add it to /dev */
4037 add_disk(dd->disk);
4038
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004039 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004040 /*
4041 * Now that the disk is active, initialize any sysfs attributes
4042 * managed by the protocol layer.
4043 */
4044 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4045 if (kobj) {
4046 mtip_hw_sysfs_init(dd, kobj);
4047 kobject_put(kobj);
4048 }
4049
Asai Thambi S P45038362012-04-09 08:35:38 +02004050 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004051 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004052 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004053 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004054
4055start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004056 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01004057 dd, dd->numa_node,
4058 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004059
4060 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004061 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004062 dd->mtip_svc_handler = NULL;
4063 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004064 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004065 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004066 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004067 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4068 rv = wait_for_rebuild;
4069
Sam Bradshaw88523a62011-08-30 08:34:26 -06004070 return rv;
4071
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004072kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004073 bdput(dd->bdev);
4074 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004075
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004076 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004077 del_gendisk(dd->disk);
4078
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004079read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004080init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004081 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004082 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004083block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004084 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004085disk_index_error:
4086 spin_lock(&rssd_index_lock);
4087 ida_remove(&rssd_index_ida, index);
4088 spin_unlock(&rssd_index_lock);
4089
4090ida_get_error:
4091 put_disk(dd->disk);
4092
4093alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004094 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004095
4096protocol_init_error:
4097 return rv;
4098}
4099
4100/*
4101 * Block layer deinitialization function.
4102 *
4103 * Called by the PCI layer as each P320 device is removed.
4104 *
4105 * @dd Pointer to the driver data structure.
4106 *
4107 * return value
4108 * 0
4109 */
Jens Axboe63166682011-09-27 21:27:43 -06004110static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004111{
4112 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004113
Asai Thambi SP2132a542015-05-11 15:53:18 -07004114 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004115
Asai Thambi SP2132a542015-05-11 15:53:18 -07004116 if (dd->mtip_svc_handler) {
4117 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4118 wake_up_interruptible(&dd->port->svc_wait);
4119 kthread_stop(dd->mtip_svc_handler);
4120 }
4121
4122 /* Clean up the sysfs attributes, if created */
4123 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4124 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4125 if (kobj) {
4126 mtip_hw_sysfs_exit(dd, kobj);
4127 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004128 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004129 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004130
Asai Thambi SP2132a542015-05-11 15:53:18 -07004131 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004132 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004133 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004134 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4135 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004136
4137 /*
4138 * Delete our gendisk structure. This also removes the device
4139 * from /dev
4140 */
4141 if (dd->bdev) {
4142 bdput(dd->bdev);
4143 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004144 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004145 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004146 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4147 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004148 if (dd->disk->queue) {
4149 blk_cleanup_queue(dd->queue);
4150 blk_mq_free_tag_set(&dd->tags);
4151 dd->queue = NULL;
4152 }
4153 put_disk(dd->disk);
4154 }
4155 dd->disk = NULL;
4156
4157 spin_lock(&rssd_index_lock);
4158 ida_remove(&rssd_index_ida, dd->index);
4159 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004160
4161 /* De-initialize the protocol layer. */
4162 mtip_hw_exit(dd);
4163
4164 return 0;
4165}
4166
4167/*
4168 * Function called by the PCI layer when just before the
4169 * machine shuts down.
4170 *
4171 * If a protocol layer shutdown function is present it will be called
4172 * by this function.
4173 *
4174 * @dd Pointer to the driver data structure.
4175 *
4176 * return value
4177 * 0
4178 */
Jens Axboe63166682011-09-27 21:27:43 -06004179static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004180{
Jens Axboeffc771b2014-05-09 09:42:02 -06004181 mtip_hw_shutdown(dd);
4182
Sam Bradshaw88523a62011-08-30 08:34:26 -06004183 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304184 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304185 dev_info(&dd->pdev->dev,
4186 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304187
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004188 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4189 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304190 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304191 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004192 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004193 }
4194 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304195 dd->disk = NULL;
4196 dd->queue = NULL;
4197 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004198
4199 spin_lock(&rssd_index_lock);
4200 ida_remove(&rssd_index_ida, dd->index);
4201 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004202 return 0;
4203}
4204
Jens Axboe63166682011-09-27 21:27:43 -06004205static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004206{
4207 dev_info(&dd->pdev->dev,
4208 "Suspending %s ...\n", dd->disk->disk_name);
4209 mtip_hw_suspend(dd);
4210 return 0;
4211}
4212
Jens Axboe63166682011-09-27 21:27:43 -06004213static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004214{
4215 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4216 dd->disk->disk_name);
4217 mtip_hw_resume(dd);
4218 return 0;
4219}
4220
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004221static void drop_cpu(int cpu)
4222{
4223 cpu_use[cpu]--;
4224}
4225
4226static int get_least_used_cpu_on_node(int node)
4227{
4228 int cpu, least_used_cpu, least_cnt;
4229 const struct cpumask *node_mask;
4230
4231 node_mask = cpumask_of_node(node);
4232 least_used_cpu = cpumask_first(node_mask);
4233 least_cnt = cpu_use[least_used_cpu];
4234 cpu = least_used_cpu;
4235
4236 for_each_cpu(cpu, node_mask) {
4237 if (cpu_use[cpu] < least_cnt) {
4238 least_used_cpu = cpu;
4239 least_cnt = cpu_use[cpu];
4240 }
4241 }
4242 cpu_use[least_used_cpu]++;
4243 return least_used_cpu;
4244}
4245
4246/* Helper for selecting a node in round robin mode */
4247static inline int mtip_get_next_rr_node(void)
4248{
4249 static int next_node = -1;
4250
4251 if (next_node == -1) {
4252 next_node = first_online_node;
4253 return next_node;
4254 }
4255
4256 next_node = next_online_node(next_node);
4257 if (next_node == MAX_NUMNODES)
4258 next_node = first_online_node;
4259 return next_node;
4260}
4261
Fengguang Wu25bac122013-01-12 15:31:40 +08004262static DEFINE_HANDLER(0);
4263static DEFINE_HANDLER(1);
4264static DEFINE_HANDLER(2);
4265static DEFINE_HANDLER(3);
4266static DEFINE_HANDLER(4);
4267static DEFINE_HANDLER(5);
4268static DEFINE_HANDLER(6);
4269static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004270
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004271static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4272{
4273 int pos;
4274 unsigned short pcie_dev_ctrl;
4275
4276 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4277 if (pos) {
4278 pci_read_config_word(pdev,
4279 pos + PCI_EXP_DEVCTL,
4280 &pcie_dev_ctrl);
4281 if (pcie_dev_ctrl & (1 << 11) ||
4282 pcie_dev_ctrl & (1 << 4)) {
4283 dev_info(&dd->pdev->dev,
4284 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4285 pdev->vendor, pdev->device);
4286 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4287 PCI_EXP_DEVCTL_RELAX_EN);
4288 pci_write_config_word(pdev,
4289 pos + PCI_EXP_DEVCTL,
4290 pcie_dev_ctrl);
4291 }
4292 }
4293}
4294
4295static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4296{
4297 /*
4298 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4299 * device with device id 0x5aXX
4300 */
4301 if (pdev->bus && pdev->bus->self) {
4302 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4303 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4304 mtip_disable_link_opts(dd, pdev->bus->self);
4305 } else {
4306 /* Check further up the topology */
4307 struct pci_dev *parent_dev = pdev->bus->self;
4308 if (parent_dev->bus &&
4309 parent_dev->bus->parent &&
4310 parent_dev->bus->parent->self &&
4311 parent_dev->bus->parent->self->vendor ==
4312 PCI_VENDOR_ID_ATI &&
4313 (parent_dev->bus->parent->self->device &
4314 0xff00) == 0x5a00) {
4315 mtip_disable_link_opts(dd,
4316 parent_dev->bus->parent->self);
4317 }
4318 }
4319 }
4320}
4321
Sam Bradshaw88523a62011-08-30 08:34:26 -06004322/*
4323 * Called for each supported PCI device detected.
4324 *
4325 * This function allocates the private data structure, enables the
4326 * PCI device and then calls the block layer initialization function.
4327 *
4328 * return value
4329 * 0 on success else an error code.
4330 */
4331static int mtip_pci_probe(struct pci_dev *pdev,
4332 const struct pci_device_id *ent)
4333{
4334 int rv = 0;
4335 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004336 char cpu_list[256];
4337 const struct cpumask *node_mask;
4338 int cpu, i = 0, j = 0;
4339 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304340 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004341
4342 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004343 my_node = pcibus_to_node(pdev->bus);
4344 if (my_node != NUMA_NO_NODE) {
4345 if (!node_online(my_node))
4346 my_node = mtip_get_next_rr_node();
4347 } else {
4348 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4349 my_node = mtip_get_next_rr_node();
4350 }
4351 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4352 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004353 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004354
4355 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004356 if (dd == NULL) {
4357 dev_err(&pdev->dev,
4358 "Unable to allocate memory for driver data\n");
4359 return -ENOMEM;
4360 }
4361
Sam Bradshaw88523a62011-08-30 08:34:26 -06004362 /* Attach the private data to this PCI device. */
4363 pci_set_drvdata(pdev, dd);
4364
4365 rv = pcim_enable_device(pdev);
4366 if (rv < 0) {
4367 dev_err(&pdev->dev, "Unable to enable device\n");
4368 goto iomap_err;
4369 }
4370
4371 /* Map BAR5 to memory. */
4372 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4373 if (rv < 0) {
4374 dev_err(&pdev->dev, "Unable to map regions\n");
4375 goto iomap_err;
4376 }
4377
4378 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4379 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4380
4381 if (rv) {
4382 rv = pci_set_consistent_dma_mask(pdev,
4383 DMA_BIT_MASK(32));
4384 if (rv) {
4385 dev_warn(&pdev->dev,
4386 "64-bit DMA enable failed\n");
4387 goto setmask_err;
4388 }
4389 }
4390 }
4391
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004392 /* Copy the info we may need later into the private data structure. */
4393 dd->major = mtip_major;
4394 dd->instance = instance;
4395 dd->pdev = pdev;
4396 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004397
Asai Thambi S P0caff002013-04-03 19:56:21 +05304398 INIT_LIST_HEAD(&dd->online_list);
4399 INIT_LIST_HEAD(&dd->remove_list);
4400
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004401 memset(dd->workq_name, 0, 32);
4402 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4403
4404 dd->isr_workq = create_workqueue(dd->workq_name);
4405 if (!dd->isr_workq) {
4406 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004407 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004408 goto block_initialize_err;
4409 }
4410
4411 memset(cpu_list, 0, sizeof(cpu_list));
4412
4413 node_mask = cpumask_of_node(dd->numa_node);
4414 if (!cpumask_empty(node_mask)) {
4415 for_each_cpu(cpu, node_mask)
4416 {
4417 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4418 j = strlen(cpu_list);
4419 }
4420
4421 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4422 dd->numa_node,
4423 topology_physical_package_id(cpumask_first(node_mask)),
4424 nr_cpus_node(dd->numa_node),
4425 cpu_list);
4426 } else
4427 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4428
4429 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4430 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4431 cpu_to_node(dd->isr_binding), dd->isr_binding);
4432
4433 /* first worker context always runs in ISR */
4434 dd->work[0].cpu_binding = dd->isr_binding;
4435 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4436 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4437 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4438 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4439 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4440 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4441 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4442
4443 /* Log the bindings */
4444 for_each_present_cpu(cpu) {
4445 memset(cpu_list, 0, sizeof(cpu_list));
4446 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4447 if (dd->work[i].cpu_binding == cpu) {
4448 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4449 j = strlen(cpu_list);
4450 }
4451 }
4452 if (j)
4453 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4454 }
4455
4456 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4457 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4458 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4459 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4460 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4461 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4462 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4463 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4464
4465 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004466 rv = pci_enable_msi(pdev);
4467 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004468 dev_warn(&pdev->dev,
4469 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004470 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004471 }
4472
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004473 mtip_fix_ero_nosnoop(dd, pdev);
4474
Sam Bradshaw88523a62011-08-30 08:34:26 -06004475 /* Initialize the block layer. */
4476 rv = mtip_block_initialize(dd);
4477 if (rv < 0) {
4478 dev_err(&pdev->dev,
4479 "Unable to initialize block layer\n");
4480 goto block_initialize_err;
4481 }
4482
4483 /*
4484 * Increment the instance count so that each device has a unique
4485 * instance number.
4486 */
4487 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004488 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004489 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304490 else
4491 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304492
4493 /* Add to online list even if in ftl rebuild */
4494 spin_lock_irqsave(&dev_lock, flags);
4495 list_add(&dd->online_list, &online_list);
4496 spin_unlock_irqrestore(&dev_lock, flags);
4497
Sam Bradshaw88523a62011-08-30 08:34:26 -06004498 goto done;
4499
4500block_initialize_err:
4501 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004502
4503msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004504 if (dd->isr_workq) {
4505 flush_workqueue(dd->isr_workq);
4506 destroy_workqueue(dd->isr_workq);
4507 drop_cpu(dd->work[0].cpu_binding);
4508 drop_cpu(dd->work[1].cpu_binding);
4509 drop_cpu(dd->work[2].cpu_binding);
4510 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004511setmask_err:
4512 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4513
4514iomap_err:
4515 kfree(dd);
4516 pci_set_drvdata(pdev, NULL);
4517 return rv;
4518done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004519 return rv;
4520}
4521
4522/*
4523 * Called for each probed device when the device is removed or the
4524 * driver is unloaded.
4525 *
4526 * return value
4527 * None
4528 */
4529static void mtip_pci_remove(struct pci_dev *pdev)
4530{
4531 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004532 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004533
Asai Thambi SP51c65702016-02-24 21:18:10 -08004534 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004535
Asai Thambi S P0caff002013-04-03 19:56:21 +05304536 spin_lock_irqsave(&dev_lock, flags);
4537 list_del_init(&dd->online_list);
4538 list_add(&dd->remove_list, &removing_list);
4539 spin_unlock_irqrestore(&dev_lock, flags);
4540
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004541 mtip_check_surprise_removal(pdev);
4542 synchronize_irq(dd->pdev->irq);
4543
4544 /* Spin until workers are done */
4545 to = jiffies + msecs_to_jiffies(4000);
4546 do {
4547 msleep(20);
4548 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4549 time_before(jiffies, to));
4550
Asai Thambi SP51c65702016-02-24 21:18:10 -08004551 fsync_bdev(dd->bdev);
4552
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004553 if (atomic_read(&dd->irq_workers_active) != 0) {
4554 dev_warn(&dd->pdev->dev,
4555 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004556 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004557
Asai Thambi SP51c65702016-02-24 21:18:10 -08004558 if (dd->sr)
4559 blk_mq_stop_hw_queues(dd->queue);
4560
4561 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4562
Sam Bradshaw88523a62011-08-30 08:34:26 -06004563 /* Clean up the block layer. */
4564 mtip_block_remove(dd);
4565
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004566 if (dd->isr_workq) {
4567 flush_workqueue(dd->isr_workq);
4568 destroy_workqueue(dd->isr_workq);
4569 drop_cpu(dd->work[0].cpu_binding);
4570 drop_cpu(dd->work[1].cpu_binding);
4571 drop_cpu(dd->work[2].cpu_binding);
4572 }
4573
Sam Bradshaw88523a62011-08-30 08:34:26 -06004574 pci_disable_msi(pdev);
4575
Asai Thambi S P0caff002013-04-03 19:56:21 +05304576 spin_lock_irqsave(&dev_lock, flags);
4577 list_del_init(&dd->remove_list);
4578 spin_unlock_irqrestore(&dev_lock, flags);
4579
Asai Thambi SP2132a542015-05-11 15:53:18 -07004580 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004581
Sam Bradshaw88523a62011-08-30 08:34:26 -06004582 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004583 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004584}
4585
4586/*
4587 * Called for each probed device when the device is suspended.
4588 *
4589 * return value
4590 * 0 Success
4591 * <0 Error
4592 */
4593static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4594{
4595 int rv = 0;
4596 struct driver_data *dd = pci_get_drvdata(pdev);
4597
4598 if (!dd) {
4599 dev_err(&pdev->dev,
4600 "Driver private datastructure is NULL\n");
4601 return -EFAULT;
4602 }
4603
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004604 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004605
4606 /* Disable ports & interrupts then send standby immediate */
4607 rv = mtip_block_suspend(dd);
4608 if (rv < 0) {
4609 dev_err(&pdev->dev,
4610 "Failed to suspend controller\n");
4611 return rv;
4612 }
4613
4614 /*
4615 * Save the pci config space to pdev structure &
4616 * disable the device
4617 */
4618 pci_save_state(pdev);
4619 pci_disable_device(pdev);
4620
4621 /* Move to Low power state*/
4622 pci_set_power_state(pdev, PCI_D3hot);
4623
4624 return rv;
4625}
4626
4627/*
4628 * Called for each probed device when the device is resumed.
4629 *
4630 * return value
4631 * 0 Success
4632 * <0 Error
4633 */
4634static int mtip_pci_resume(struct pci_dev *pdev)
4635{
4636 int rv = 0;
4637 struct driver_data *dd;
4638
4639 dd = pci_get_drvdata(pdev);
4640 if (!dd) {
4641 dev_err(&pdev->dev,
4642 "Driver private datastructure is NULL\n");
4643 return -EFAULT;
4644 }
4645
4646 /* Move the device to active State */
4647 pci_set_power_state(pdev, PCI_D0);
4648
4649 /* Restore PCI configuration space */
4650 pci_restore_state(pdev);
4651
4652 /* Enable the PCI device*/
4653 rv = pcim_enable_device(pdev);
4654 if (rv < 0) {
4655 dev_err(&pdev->dev,
4656 "Failed to enable card during resume\n");
4657 goto err;
4658 }
4659 pci_set_master(pdev);
4660
4661 /*
4662 * Calls hbaReset, initPort, & startPort function
4663 * then enables interrupts
4664 */
4665 rv = mtip_block_resume(dd);
4666 if (rv < 0)
4667 dev_err(&pdev->dev, "Unable to resume\n");
4668
4669err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004670 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004671
4672 return rv;
4673}
4674
4675/*
4676 * Shutdown routine
4677 *
4678 * return value
4679 * None
4680 */
4681static void mtip_pci_shutdown(struct pci_dev *pdev)
4682{
4683 struct driver_data *dd = pci_get_drvdata(pdev);
4684 if (dd)
4685 mtip_block_shutdown(dd);
4686}
4687
Sam Bradshaw88523a62011-08-30 08:34:26 -06004688/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004689static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304690 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4691 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4692 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4693 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4694 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4695 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4696 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004697 { 0 }
4698};
4699
4700/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004701static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004702 .name = MTIP_DRV_NAME,
4703 .id_table = mtip_pci_tbl,
4704 .probe = mtip_pci_probe,
4705 .remove = mtip_pci_remove,
4706 .suspend = mtip_pci_suspend,
4707 .resume = mtip_pci_resume,
4708 .shutdown = mtip_pci_shutdown,
4709};
4710
4711MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4712
4713/*
4714 * Module initialization function.
4715 *
4716 * Called once when the module is loaded. This function allocates a major
4717 * block device number to the Cyclone devices and registers the PCI layer
4718 * of the driver.
4719 *
4720 * Return value
4721 * 0 on success else error code.
4722 */
4723static int __init mtip_init(void)
4724{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004725 int error;
4726
Asai Thambi S P45422e72012-09-05 22:03:56 +05304727 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004728
Asai Thambi S P0caff002013-04-03 19:56:21 +05304729 spin_lock_init(&dev_lock);
4730
4731 INIT_LIST_HEAD(&online_list);
4732 INIT_LIST_HEAD(&removing_list);
4733
Sam Bradshaw88523a62011-08-30 08:34:26 -06004734 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004735 error = register_blkdev(0, MTIP_DRV_NAME);
4736 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304737 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004738 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004739 return -EBUSY;
4740 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004741 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004742
Asai Thambi S P0caff002013-04-03 19:56:21 +05304743 dfs_parent = debugfs_create_dir("rssd", NULL);
4744 if (IS_ERR_OR_NULL(dfs_parent)) {
4745 pr_warn("Error creating debugfs parent\n");
4746 dfs_parent = NULL;
4747 }
4748 if (dfs_parent) {
4749 dfs_device_status = debugfs_create_file("device_status",
4750 S_IRUGO, dfs_parent, NULL,
4751 &mtip_device_status_fops);
4752 if (IS_ERR_OR_NULL(dfs_device_status)) {
4753 pr_err("Error creating device_status node\n");
4754 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004755 }
4756 }
4757
Sam Bradshaw88523a62011-08-30 08:34:26 -06004758 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004759 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004760 if (error) {
4761 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004762 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004763 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004764
4765 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004766}
4767
4768/*
4769 * Module de-initialization function.
4770 *
4771 * Called once when the module is unloaded. This function deallocates
4772 * the major block device number allocated by mtip_init() and
4773 * unregisters the PCI layer of the driver.
4774 *
4775 * Return value
4776 * none
4777 */
4778static void __exit mtip_exit(void)
4779{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004780 /* Release the allocated major block device number. */
4781 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4782
4783 /* Unregister the PCI driver. */
4784 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004785
4786 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004787}
4788
4789MODULE_AUTHOR("Micron Technology, Inc");
4790MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4791MODULE_LICENSE("GPL");
4792MODULE_VERSION(MTIP_DRV_VERSION);
4793
4794module_init(mtip_init);
4795module_exit(mtip_exit);