blob: a64da08ccd3e9183d8056c5823631e43282721b5 [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 /* Unmap the DMA scatter list entries */
237 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600238
Jens Axboeffc771b2014-05-09 09:42:02 -0600239 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600240
Jens Axboeffc771b2014-05-09 09:42:02 -0600241 if (unlikely(cmd->unaligned))
242 up(&port->cmd_slot_unal);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600243
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700244 blk_mq_end_request(rq, status ? -EIO : 0);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600245}
246
247/*
Jens Axboe63166682011-09-27 21:27:43 -0600248 * Reset the HBA (without sleeping)
249 *
Jens Axboe63166682011-09-27 21:27:43 -0600250 * @dd Pointer to the driver data structure.
251 *
252 * return value
253 * 0 The reset was successful.
254 * -1 The HBA Reset bit did not clear.
255 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530256static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600257{
258 unsigned long timeout;
259
Jens Axboe63166682011-09-27 21:27:43 -0600260 /* Set the reset bit */
261 writel(HOST_RESET, dd->mmio + HOST_CTL);
262
263 /* Flush */
264 readl(dd->mmio + HOST_CTL);
265
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700266 /*
267 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
268 * is 1 sec but in LUN failure conditions, up to 10 secs are required
269 */
270 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530271 do {
272 mdelay(10);
273 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
274 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600275
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530276 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
277 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200278
Jens Axboe63166682011-09-27 21:27:43 -0600279 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
280 return -1;
281
282 return 0;
283}
284
285/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600286 * Issue a command to the hardware.
287 *
288 * Set the appropriate bit in the s_active and Command Issue hardware
289 * registers, causing hardware command processing to begin.
290 *
291 * @port Pointer to the port structure.
292 * @tag The tag of the command to be issued.
293 *
294 * return value
295 * None
296 */
297static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
298{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800299 int group = tag >> 5;
300
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800301 /* guard SACT and CI registers */
302 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800307 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600308}
309
310/*
Jens Axboe63166682011-09-27 21:27:43 -0600311 * Enable/disable the reception of FIS
312 *
313 * @port Pointer to the port data structure
314 * @enable 1 to enable, 0 to disable
315 *
316 * return value
317 * Previous state: 1 enabled, 0 disabled
318 */
319static int mtip_enable_fis(struct mtip_port *port, int enable)
320{
321 u32 tmp;
322
323 /* enable FIS reception */
324 tmp = readl(port->mmio + PORT_CMD);
325 if (enable)
326 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
327 else
328 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
329
330 /* Flush */
331 readl(port->mmio + PORT_CMD);
332
333 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
334}
335
336/*
337 * Enable/disable the DMA engine
338 *
339 * @port Pointer to the port data structure
340 * @enable 1 to enable, 0 to disable
341 *
342 * return value
343 * Previous state: 1 enabled, 0 disabled.
344 */
345static int mtip_enable_engine(struct mtip_port *port, int enable)
346{
347 u32 tmp;
348
349 /* enable FIS reception */
350 tmp = readl(port->mmio + PORT_CMD);
351 if (enable)
352 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
353 else
354 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
355
356 readl(port->mmio + PORT_CMD);
357 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
358}
359
360/*
361 * Enables the port DMA engine and FIS reception.
362 *
363 * return value
364 * None
365 */
366static inline void mtip_start_port(struct mtip_port *port)
367{
368 /* Enable FIS reception */
369 mtip_enable_fis(port, 1);
370
371 /* Enable the DMA engine */
372 mtip_enable_engine(port, 1);
373}
374
375/*
376 * Deinitialize a port by disabling port interrupts, the DMA engine,
377 * and FIS reception.
378 *
379 * @port Pointer to the port structure
380 *
381 * return value
382 * None
383 */
384static inline void mtip_deinit_port(struct mtip_port *port)
385{
386 /* Disable interrupts on this port */
387 writel(0, port->mmio + PORT_IRQ_MASK);
388
389 /* Disable the DMA engine */
390 mtip_enable_engine(port, 0);
391
392 /* Disable FIS reception */
393 mtip_enable_fis(port, 0);
394}
395
396/*
397 * Initialize a port.
398 *
399 * This function deinitializes the port by calling mtip_deinit_port() and
400 * then initializes it by setting the command header and RX FIS addresses,
401 * clearing the SError register and any pending port interrupts before
402 * re-enabling the default set of port interrupts.
403 *
404 * @port Pointer to the port structure.
405 *
406 * return value
407 * None
408 */
409static void mtip_init_port(struct mtip_port *port)
410{
411 int i;
412 mtip_deinit_port(port);
413
414 /* Program the command list base and FIS base addresses */
415 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
416 writel((port->command_list_dma >> 16) >> 16,
417 port->mmio + PORT_LST_ADDR_HI);
418 writel((port->rxfis_dma >> 16) >> 16,
419 port->mmio + PORT_FIS_ADDR_HI);
420 }
421
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100422 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600423 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100424 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600425
426 /* Clear SError */
427 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
428
429 /* reset the completed registers.*/
430 for (i = 0; i < port->dd->slot_groups; i++)
431 writel(0xFFFFFFFF, port->completed[i]);
432
433 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700434 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600435
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100436 /* Clear any pending interrupts on the HBA. */
437 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
438 port->dd->mmio + HOST_IRQ_STAT);
439
Jens Axboe63166682011-09-27 21:27:43 -0600440 /* Enable port interrupts */
441 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
442}
443
444/*
445 * Restart a port
446 *
447 * @port Pointer to the port data structure.
448 *
449 * return value
450 * None
451 */
452static void mtip_restart_port(struct mtip_port *port)
453{
454 unsigned long timeout;
455
456 /* Disable the DMA engine */
457 mtip_enable_engine(port, 0);
458
459 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
460 timeout = jiffies + msecs_to_jiffies(500);
461 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
462 && time_before(jiffies, timeout))
463 ;
464
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200465 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200466 return;
467
Jens Axboe63166682011-09-27 21:27:43 -0600468 /*
469 * Chip quirk: escalate to hba reset if
470 * PxCMD.CR not clear after 500 ms
471 */
472 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
473 dev_warn(&port->dd->pdev->dev,
474 "PxCMD.CR not clear, escalating reset\n");
475
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530476 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600477 dev_err(&port->dd->pdev->dev,
478 "HBA reset escalation failed.\n");
479
480 /* 30 ms delay before com reset to quiesce chip */
481 mdelay(30);
482 }
483
484 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
485
486 /* Set PxSCTL.DET */
487 writel(readl(port->mmio + PORT_SCR_CTL) |
488 1, port->mmio + PORT_SCR_CTL);
489 readl(port->mmio + PORT_SCR_CTL);
490
491 /* Wait 1 ms to quiesce chip function */
492 timeout = jiffies + msecs_to_jiffies(1);
493 while (time_before(jiffies, timeout))
494 ;
495
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200496 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200497 return;
498
Jens Axboe63166682011-09-27 21:27:43 -0600499 /* Clear PxSCTL.DET */
500 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
501 port->mmio + PORT_SCR_CTL);
502 readl(port->mmio + PORT_SCR_CTL);
503
504 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
505 timeout = jiffies + msecs_to_jiffies(500);
506 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
507 && time_before(jiffies, timeout))
508 ;
509
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200510 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200511 return;
512
Jens Axboe63166682011-09-27 21:27:43 -0600513 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
514 dev_warn(&port->dd->pdev->dev,
515 "COM reset failed\n");
516
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100517 mtip_init_port(port);
518 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600519
Jens Axboe63166682011-09-27 21:27:43 -0600520}
521
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530522static int mtip_device_reset(struct driver_data *dd)
523{
524 int rv = 0;
525
526 if (mtip_check_surprise_removal(dd->pdev))
527 return 0;
528
529 if (mtip_hba_reset(dd) < 0)
530 rv = -EFAULT;
531
532 mdelay(1);
533 mtip_init_port(dd->port);
534 mtip_start_port(dd->port);
535
536 /* Enable interrupts on the HBA. */
537 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
538 dd->mmio + HOST_CTL);
539 return rv;
540}
541
Jens Axboe63166682011-09-27 21:27:43 -0600542/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200543 * Helper function for tag logging
544 */
545static void print_tags(struct driver_data *dd,
546 char *msg,
547 unsigned long *tagbits,
548 int cnt)
549{
550 unsigned char tagmap[128];
551 int group, tagmap_len = 0;
552
553 memset(tagmap, 0, sizeof(tagmap));
554 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600555 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200556 tagbits[group-1]);
557 dev_warn(&dd->pdev->dev,
558 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
559}
560
561/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600562 * Internal command completion callback function.
563 *
564 * This function is normally called by the driver ISR when an internal
565 * command completed. This function signals the command completion by
566 * calling complete().
567 *
568 * @port Pointer to the port data structure.
569 * @tag Tag of the command that has completed.
570 * @data Pointer to a completion structure.
571 * @status Completion status.
572 *
573 * return value
574 * None
575 */
576static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600577 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600578{
Jens Axboeffc771b2014-05-09 09:42:02 -0600579 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600580 if (unlikely(status == PORT_IRQ_TF_ERR))
581 dev_warn(&port->dd->pdev->dev,
582 "Internal command %d completed with TFE\n", tag);
583
Sam Bradshaw88523a62011-08-30 08:34:26 -0600584 complete(waiting);
585}
586
Asai Thambi S P8182b492012-04-09 08:35:38 +0200587static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600588 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200589{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200590}
591
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200592static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
593 dma_addr_t buffer_dma, unsigned int sectors);
594static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
595 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600596/*
597 * Handle an error.
598 *
599 * @dd Pointer to the DRIVER_DATA structure.
600 *
601 * return value
602 * None
603 */
604static void mtip_handle_tfe(struct driver_data *dd)
605{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200606 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600607 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200608 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600609 u32 completed;
610 struct host_to_dev_fis *fis;
611 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200612 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200613 unsigned char *buf;
614 char *fail_reason = NULL;
615 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600616
617 dev_warn(&dd->pdev->dev, "Taskfile error\n");
618
619 port = dd->port;
620
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700621 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600622 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700623 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
624
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700625 if (cmd->comp_data && cmd->comp_func) {
626 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600627 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700628 }
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800629 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700630 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600631
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200632 /* clear the tag accumulator */
633 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
634
Sam Bradshaw88523a62011-08-30 08:34:26 -0600635 /* Loop through all the groups */
636 for (group = 0; group < dd->slot_groups; group++) {
637 completed = readl(port->completed[group]);
638
Jens Axboeffc771b2014-05-09 09:42:02 -0600639 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
640
Sam Bradshaw88523a62011-08-30 08:34:26 -0600641 /* clear completed status register in the hardware.*/
642 writel(completed, port->completed[group]);
643
Sam Bradshaw88523a62011-08-30 08:34:26 -0600644 /* Process successfully completed commands */
645 for (bit = 0; bit < 32 && completed; bit++) {
646 if (!(completed & (1<<bit)))
647 continue;
648 tag = (group << 5) + bit;
649
650 /* Skip the internal command slot */
651 if (tag == MTIP_TAG_INTERNAL)
652 continue;
653
Jens Axboeffc771b2014-05-09 09:42:02 -0600654 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200655 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600656 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200657 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600658 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600659 } else {
660 dev_err(&port->dd->pdev->dev,
661 "Missing completion func for tag %d",
662 tag);
663 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 /* don't proceed further */
665 return;
666 }
667 }
668 }
669 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200670
671 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600672
673 /* Restart the port */
674 mdelay(20);
675 mtip_restart_port(port);
676
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200677 /* Trying to determine the cause of the error */
678 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
679 dd->port->log_buf,
680 dd->port->log_buf_dma, 1);
681 if (rv) {
682 dev_warn(&dd->pdev->dev,
683 "Error in READ LOG EXT (10h) command\n");
684 /* non-critical error, don't fail the load */
685 } else {
686 buf = (unsigned char *)dd->port->log_buf;
687 if (buf[259] & 0x1) {
688 dev_info(&dd->pdev->dev,
689 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200690 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200691 fail_all_ncq_write = 1;
692 fail_reason = "write protect";
693 }
694 if (buf[288] == 0xF7) {
695 dev_info(&dd->pdev->dev,
696 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200697 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200698 fail_all_ncq_cmds = 1;
699 fail_reason = "thermal shutdown";
700 }
701 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800702 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200703 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700704 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200705 fail_all_ncq_cmds = 1;
706 fail_reason = "rebuild failed";
707 }
708 }
709
Sam Bradshaw88523a62011-08-30 08:34:26 -0600710 /* clear the tag accumulator */
711 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
712
713 /* Loop through all the groups */
714 for (group = 0; group < dd->slot_groups; group++) {
715 for (bit = 0; bit < 32; bit++) {
716 reissue = 1;
717 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600718 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600719
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200720 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600721
722 /* Should re-issue? */
723 if (tag == MTIP_TAG_INTERNAL ||
724 fis->command == ATA_CMD_SET_FEATURES)
725 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200726 else {
727 if (fail_all_ncq_cmds ||
728 (fail_all_ncq_write &&
729 fis->command == ATA_CMD_FPDMA_WRITE)) {
730 dev_warn(&dd->pdev->dev,
731 " Fail: %s w/tag %d [%s].\n",
732 fis->command == ATA_CMD_FPDMA_WRITE ?
733 "write" : "read",
734 tag,
735 fail_reason != NULL ?
736 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200737 if (cmd->comp_func) {
738 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600739 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200740 }
741 continue;
742 }
743 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600744
745 /*
746 * First check if this command has
747 * exceeded its retries.
748 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200749 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600750
751 set_bit(tag, tagaccum);
752
Sam Bradshaw88523a62011-08-30 08:34:26 -0600753 /* Re-issue the command. */
754 mtip_issue_ncq_command(port, tag);
755
756 continue;
757 }
758
759 /* Retire a command that will not be reissued */
760 dev_warn(&port->dd->pdev->dev,
761 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600762
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200763 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600764 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600765 else
766 dev_warn(&port->dd->pdev->dev,
767 "Bad completion for tag %d\n",
768 tag);
769 }
770 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200771 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600772}
773
774/*
775 * Handle a set device bits interrupt
776 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800777static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
778 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600779{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800780 struct driver_data *dd = port->dd;
781 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600782 struct mtip_cmd *command;
783
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800784 if (!completed) {
785 WARN_ON_ONCE(!completed);
786 return;
787 }
788 /* clear completed status register in the hardware.*/
789 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600790
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800791 /* Process completed commands. */
792 for (bit = 0; (bit < 32) && completed; bit++) {
793 if (completed & 0x01) {
794 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600795
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800796 /* skip internal command slot. */
797 if (unlikely(tag == MTIP_TAG_INTERNAL))
798 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600799
Jens Axboeffc771b2014-05-09 09:42:02 -0600800 command = mtip_cmd_from_tag(dd, tag);
801 if (likely(command->comp_func))
802 command->comp_func(port, tag, command, 0);
803 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600804 dev_dbg(&dd->pdev->dev,
805 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800806 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600807
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800808 if (mtip_check_surprise_removal(
809 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800810 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600811 }
812 }
813 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800814 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600815 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800816
817 /* If last, re-enable interrupts */
818 if (atomic_dec_return(&dd->irq_workers_active) == 0)
819 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600820}
821
822/*
823 * Process legacy pio and d2h interrupts
824 */
825static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
826{
827 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600828 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600829
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200830 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100831 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600832 & (1 << MTIP_TAG_INTERNAL))) {
833 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600834 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600835 return;
836 }
837 }
838
Sam Bradshaw88523a62011-08-30 08:34:26 -0600839 return;
840}
841
842/*
843 * Demux and handle errors
844 */
845static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
846{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600847
848 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
849 dev_warn(&dd->pdev->dev,
850 "Clearing PxSERR.DIAG.x\n");
851 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
852 }
853
854 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
855 dev_warn(&dd->pdev->dev,
856 "Clearing PxSERR.DIAG.n\n");
857 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
858 }
859
860 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
861 dev_warn(&dd->pdev->dev,
862 "Port stat errors %x unhandled\n",
863 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700864 if (mtip_check_surprise_removal(dd->pdev))
865 return;
866 }
867 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
868 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
869 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600870 }
871}
872
873static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
874{
875 struct driver_data *dd = (struct driver_data *) data;
876 struct mtip_port *port = dd->port;
877 u32 hba_stat, port_stat;
878 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800879 int do_irq_enable = 1, i, workers;
880 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600881
882 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
883 if (hba_stat) {
884 rv = IRQ_HANDLED;
885
886 /* Acknowledge the interrupt status on the port.*/
887 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700888 if (unlikely(port_stat == 0xFFFFFFFF)) {
889 mtip_check_surprise_removal(dd->pdev);
890 return IRQ_HANDLED;
891 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600892 writel(port_stat, port->mmio + PORT_IRQ_STAT);
893
894 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800895 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
896 do_irq_enable = 0;
897 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
898
899 /* Start at 1: group zero is always local? */
900 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
901 i++) {
902 twork = &dd->work[i];
903 twork->completed = readl(port->completed[i]);
904 if (twork->completed)
905 workers++;
906 }
907
908 atomic_set(&dd->irq_workers_active, workers);
909 if (workers) {
910 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
911 twork = &dd->work[i];
912 if (twork->completed)
913 queue_work_on(
914 twork->cpu_binding,
915 dd->isr_workq,
916 &twork->work);
917 }
918
919 if (likely(dd->work[0].completed))
920 mtip_workq_sdbfx(port, 0,
921 dd->work[0].completed);
922
923 } else {
924 /*
925 * Chip quirk: SDB interrupt but nothing
926 * to complete
927 */
928 do_irq_enable = 1;
929 }
930 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600931
932 if (unlikely(port_stat & PORT_IRQ_ERR)) {
933 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600934 /* don't proceed further */
935 return IRQ_HANDLED;
936 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200937 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200938 &dd->dd_flag))
939 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600940
941 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
942 }
943
944 if (unlikely(port_stat & PORT_IRQ_LEGACY))
945 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
946 }
947
948 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800949 if (unlikely(do_irq_enable))
950 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951
952 return rv;
953}
954
955/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600956 * HBA interrupt subroutine.
957 *
958 * @irq IRQ number.
959 * @instance Pointer to the driver data structure.
960 *
961 * return value
962 * IRQ_HANDLED A HBA interrupt was pending and handled.
963 * IRQ_NONE This interrupt was not for the HBA.
964 */
965static irqreturn_t mtip_irq_handler(int irq, void *instance)
966{
967 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800968
969 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600970}
971
972static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
973{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600974 writel(1 << MTIP_TAG_BIT(tag),
975 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
976}
977
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200978static bool mtip_pause_ncq(struct mtip_port *port,
979 struct host_to_dev_fis *fis)
980{
981 struct host_to_dev_fis *reply;
982 unsigned long task_file_data;
983
984 reply = port->rxfis + RX_FIS_D2H_REG;
985 task_file_data = readl(port->mmio+PORT_TFDATA);
986
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530987 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200988 return false;
989
990 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200991 port->ic_pause_timer = jiffies;
992 return true;
993 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
994 (fis->features == 0x03)) {
995 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
996 port->ic_pause_timer = jiffies;
997 return true;
998 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
999 ((fis->command == 0xFC) &&
1000 (fis->features == 0x27 || fis->features == 0x72 ||
1001 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001002 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001003 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001004 /* Com reset after secure erase or lowlevel format */
1005 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001006 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001007 return false;
1008 }
1009
1010 return false;
1011}
1012
Sam Bradshaw88523a62011-08-30 08:34:26 -06001013/*
1014 * Wait for port to quiesce
1015 *
1016 * @port Pointer to port data structure
1017 * @timeout Max duration to wait (ms)
1018 *
1019 * return value
1020 * 0 Success
1021 * -EBUSY Commands still active
1022 */
1023static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1024{
1025 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001026 unsigned int n;
1027 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001028
Jens Axboe9acf03c2014-05-14 08:22:56 -06001029 blk_mq_stop_hw_queues(port->dd->queue);
1030
Sam Bradshaw88523a62011-08-30 08:34:26 -06001031 to = jiffies + msecs_to_jiffies(timeout);
1032 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001033 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1034 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001035 msleep(20);
1036 continue; /* svc thd is actively issuing commands */
1037 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001038
1039 msleep(100);
1040 if (mtip_check_surprise_removal(port->dd->pdev))
1041 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001042 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001043 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001044
Sam Bradshaw88523a62011-08-30 08:34:26 -06001045 /*
1046 * Ignore s_active bit 0 of array element 0.
1047 * This bit will always be set
1048 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001049 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050 for (n = 1; n < port->dd->slot_groups; n++)
1051 active |= readl(port->s_active[n]);
1052
1053 if (!active)
1054 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001055 } while (time_before(jiffies, to));
1056
Jens Axboe9acf03c2014-05-14 08:22:56 -06001057 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001058 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001059err_fault:
1060 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1061 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001062}
1063
1064/*
1065 * Execute an internal command and wait for the completion.
1066 *
1067 * @port Pointer to the port data structure.
1068 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001069 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001070 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001071 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001072 * @opts Command header options, excluding the FIS length
1073 * and the number of PRD entries.
1074 * @timeout Time in ms to wait for the command to complete.
1075 *
1076 * return value
1077 * 0 Command completed successfully.
1078 * -EFAULT The buffer address is not correctly aligned.
1079 * -EBUSY Internal command or other IO in progress.
1080 * -EAGAIN Time out waiting for command to complete.
1081 */
1082static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001083 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001084 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001085 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001086 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001087 u32 opts,
1088 gfp_t atomic,
1089 unsigned long timeout)
1090{
1091 struct mtip_cmd_sg *command_sg;
1092 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001093 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301094 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001095 int rv = 0;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001096 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001097
1098 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1099 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301100 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001101 return -EFAULT;
1102 }
1103
Jens Axboeffc771b2014-05-09 09:42:02 -06001104 int_cmd = mtip_get_int_command(dd);
1105
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001106 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001107
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001108 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1109 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1110
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301111 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001112
1113 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001114 if (fis->command != ATA_CMD_STANDBYNOW1) {
1115 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001116 if (mtip_quiesce_io(port,
1117 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301118 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001119 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001120 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001121 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001122 wake_up_interruptible(&port->svc_wait);
1123 return -EBUSY;
1124 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001125 }
1126
1127 /* Set the completion function and data for the command. */
1128 int_cmd->comp_data = &wait;
1129 int_cmd->comp_func = mtip_completion;
1130
1131 } else {
1132 /* Clear completion - we're going to poll */
1133 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001134 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001135 }
1136
1137 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001138 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001139
1140 /* Populate the SG list */
1141 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001142 __force_bit2int cpu_to_le32(opts | fis_len);
1143 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001144 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1145
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001146 command_sg->info =
1147 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1148 command_sg->dba =
1149 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1150 command_sg->dba_upper =
1151 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001152
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001153 int_cmd->command_header->opts |=
1154 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001155 }
1156
1157 /* Populate the command header */
1158 int_cmd->command_header->byte_count = 0;
1159
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001160 start = jiffies;
1161
Sam Bradshaw88523a62011-08-30 08:34:26 -06001162 /* Issue the command to the hardware */
1163 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1164
Sam Bradshaw88523a62011-08-30 08:34:26 -06001165 if (atomic == GFP_KERNEL) {
1166 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001167 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001168 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001169 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001170
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301171 if (rv == -ERESTARTSYS) { /* interrupted */
1172 dev_err(&dd->pdev->dev,
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001173 "Internal command [%02X] was interrupted after %u ms\n",
1174 fis->command,
1175 jiffies_to_msecs(jiffies - start));
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301176 rv = -EINTR;
1177 goto exec_ic_exit;
1178 } else if (rv == 0) /* timeout */
1179 dev_err(&dd->pdev->dev,
1180 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1181 fis->command, timeout);
1182 else
1183 dev_err(&dd->pdev->dev,
1184 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1185 fis->command, rv, timeout);
1186
1187 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001188 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301189 &dd->dd_flag)) {
1190 dev_err(&dd->pdev->dev,
1191 "Internal command [%02X] wait returned due to SR\n",
1192 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001193 rv = -ENXIO;
1194 goto exec_ic_exit;
1195 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301196 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301198 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001199 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001200 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301201 u32 hba_stat, port_stat;
1202
Sam Bradshaw88523a62011-08-30 08:34:26 -06001203 /* Spin for <timeout> checking if command still outstanding */
1204 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001205 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1206 & (1 << MTIP_TAG_INTERNAL))
1207 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301208 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001209 rv = -ENXIO;
1210 goto exec_ic_exit;
1211 }
1212 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001213 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301214 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001215 rv = -ENXIO;
1216 goto exec_ic_exit;
1217 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301218 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1219 if (!port_stat)
1220 continue;
1221
1222 if (port_stat & PORT_IRQ_ERR) {
1223 dev_err(&dd->pdev->dev,
1224 "Internal command [%02X] failed\n",
1225 fis->command);
1226 mtip_device_reset(dd);
1227 rv = -EIO;
1228 goto exec_ic_exit;
1229 } else {
1230 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1231 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1232 if (hba_stat)
1233 writel(hba_stat,
1234 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001235 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301236 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001237 }
1238 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001239
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001240 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1241 & (1 << MTIP_TAG_INTERNAL)) {
1242 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301243 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1244 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001245 rv = -EAGAIN;
1246 }
1247 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001248exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001249 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001250 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001251 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001252 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1253 /* NCQ paused */
1254 return rv;
1255 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001256 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001257
1258 return rv;
1259}
1260
1261/*
1262 * Byte-swap ATA ID strings.
1263 *
1264 * ATA identify data contains strings in byte-swapped 16-bit words.
1265 * They must be swapped (on all architectures) to be usable as C strings.
1266 * This function swaps bytes in-place.
1267 *
1268 * @buf The buffer location of the string
1269 * @len The number of bytes to swap
1270 *
1271 * return value
1272 * None
1273 */
1274static inline void ata_swap_string(u16 *buf, unsigned int len)
1275{
1276 int i;
1277 for (i = 0; i < (len/2); i++)
1278 be16_to_cpus(&buf[i]);
1279}
1280
Asai Thambi S P670a6412014-04-16 20:27:54 -07001281static void mtip_set_timeout(struct driver_data *dd,
1282 struct host_to_dev_fis *fis,
1283 unsigned int *timeout, u8 erasemode)
1284{
1285 switch (fis->command) {
1286 case ATA_CMD_DOWNLOAD_MICRO:
1287 *timeout = 120000; /* 2 minutes */
1288 break;
1289 case ATA_CMD_SEC_ERASE_UNIT:
1290 case 0xFC:
1291 if (erasemode)
1292 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1293 else
1294 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1295 break;
1296 case ATA_CMD_STANDBYNOW1:
1297 *timeout = 120000; /* 2 minutes */
1298 break;
1299 case 0xF7:
1300 case 0xFA:
1301 *timeout = 60000; /* 60 seconds */
1302 break;
1303 case ATA_CMD_SMART:
1304 *timeout = 15000; /* 15 seconds */
1305 break;
1306 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001307 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001308 break;
1309 }
1310}
1311
Sam Bradshaw88523a62011-08-30 08:34:26 -06001312/*
1313 * Request the device identity information.
1314 *
1315 * If a user space buffer is not specified, i.e. is NULL, the
1316 * identify information is still read from the drive and placed
1317 * into the identify data buffer (@e port->identify) in the
1318 * port data structure.
1319 * When the identify buffer contains valid identify information @e
1320 * port->identify_valid is non-zero.
1321 *
1322 * @port Pointer to the port structure.
1323 * @user_buffer A user space buffer where the identify data should be
1324 * copied.
1325 *
1326 * return value
1327 * 0 Command completed successfully.
1328 * -EFAULT An error occurred while coping data to the user buffer.
1329 * -1 Command failed.
1330 */
1331static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1332{
1333 int rv = 0;
1334 struct host_to_dev_fis fis;
1335
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001336 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001337 return -EFAULT;
1338
Sam Bradshaw88523a62011-08-30 08:34:26 -06001339 /* Build the FIS. */
1340 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1341 fis.type = 0x27;
1342 fis.opts = 1 << 7;
1343 fis.command = ATA_CMD_ID_ATA;
1344
1345 /* Set the identify information as invalid. */
1346 port->identify_valid = 0;
1347
1348 /* Clear the identify information. */
1349 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1350
1351 /* Execute the command. */
1352 if (mtip_exec_internal_command(port,
1353 &fis,
1354 5,
1355 port->identify_dma,
1356 sizeof(u16) * ATA_ID_WORDS,
1357 0,
1358 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001359 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001360 < 0) {
1361 rv = -1;
1362 goto out;
1363 }
1364
1365 /*
1366 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1367 * perform field-sensitive swapping on the string fields.
1368 * See the kernel use of ata_id_string() for proof of this.
1369 */
1370#ifdef __LITTLE_ENDIAN
1371 ata_swap_string(port->identify + 27, 40); /* model string*/
1372 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1373 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1374#else
1375 {
1376 int i;
1377 for (i = 0; i < ATA_ID_WORDS; i++)
1378 port->identify[i] = le16_to_cpu(port->identify[i]);
1379 }
1380#endif
1381
Sam Bradshaw26d58052013-10-03 10:18:05 -07001382 /* Check security locked state */
1383 if (port->identify[128] & 0x4)
1384 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1385 else
1386 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1387
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301388#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001389 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1390 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1391 port->dd->trim_supp = true;
1392 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301393#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001394 port->dd->trim_supp = false;
1395
Sam Bradshaw88523a62011-08-30 08:34:26 -06001396 /* Set the identify buffer as valid. */
1397 port->identify_valid = 1;
1398
1399 if (user_buffer) {
1400 if (copy_to_user(
1401 user_buffer,
1402 port->identify,
1403 ATA_ID_WORDS * sizeof(u16))) {
1404 rv = -EFAULT;
1405 goto out;
1406 }
1407 }
1408
1409out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001410 return rv;
1411}
1412
1413/*
1414 * Issue a standby immediate command to the device.
1415 *
1416 * @port Pointer to the port structure.
1417 *
1418 * return value
1419 * 0 Command was executed successfully.
1420 * -1 An error occurred while executing the command.
1421 */
1422static int mtip_standby_immediate(struct mtip_port *port)
1423{
1424 int rv;
1425 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001426 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001427 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001428
Sam Bradshaw88523a62011-08-30 08:34:26 -06001429 /* Build the FIS. */
1430 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1431 fis.type = 0x27;
1432 fis.opts = 1 << 7;
1433 fis.command = ATA_CMD_STANDBYNOW1;
1434
Asai Thambi S P670a6412014-04-16 20:27:54 -07001435 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1436
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001437 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001438 rv = mtip_exec_internal_command(port,
1439 &fis,
1440 5,
1441 0,
1442 0,
1443 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001444 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001445 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001446 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1447 jiffies_to_msecs(jiffies - start));
1448 if (rv)
1449 dev_warn(&port->dd->pdev->dev,
1450 "STANDBY IMMEDIATE command failed.\n");
1451
1452 return rv;
1453}
1454
1455/*
1456 * Issue a READ LOG EXT command to the device.
1457 *
1458 * @port pointer to the port structure.
1459 * @page page number to fetch
1460 * @buffer pointer to buffer
1461 * @buffer_dma dma address corresponding to @buffer
1462 * @sectors page length to fetch, in sectors
1463 *
1464 * return value
1465 * @rv return value from mtip_exec_internal_command()
1466 */
1467static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1468 dma_addr_t buffer_dma, unsigned int sectors)
1469{
1470 struct host_to_dev_fis fis;
1471
1472 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1473 fis.type = 0x27;
1474 fis.opts = 1 << 7;
1475 fis.command = ATA_CMD_READ_LOG_EXT;
1476 fis.sect_count = sectors & 0xFF;
1477 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1478 fis.lba_low = page;
1479 fis.lba_mid = 0;
1480 fis.device = ATA_DEVICE_OBS;
1481
1482 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1483
1484 return mtip_exec_internal_command(port,
1485 &fis,
1486 5,
1487 buffer_dma,
1488 sectors * ATA_SECT_SIZE,
1489 0,
1490 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001491 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001492}
1493
1494/*
1495 * Issue a SMART READ DATA command to the device.
1496 *
1497 * @port pointer to the port structure.
1498 * @buffer pointer to buffer
1499 * @buffer_dma dma address corresponding to @buffer
1500 *
1501 * return value
1502 * @rv return value from mtip_exec_internal_command()
1503 */
1504static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1505 dma_addr_t buffer_dma)
1506{
1507 struct host_to_dev_fis fis;
1508
1509 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1510 fis.type = 0x27;
1511 fis.opts = 1 << 7;
1512 fis.command = ATA_CMD_SMART;
1513 fis.features = 0xD0;
1514 fis.sect_count = 1;
1515 fis.lba_mid = 0x4F;
1516 fis.lba_hi = 0xC2;
1517 fis.device = ATA_DEVICE_OBS;
1518
1519 return mtip_exec_internal_command(port,
1520 &fis,
1521 5,
1522 buffer_dma,
1523 ATA_SECT_SIZE,
1524 0,
1525 GFP_ATOMIC,
1526 15000);
1527}
1528
1529/*
1530 * Get the value of a smart attribute
1531 *
1532 * @port pointer to the port structure
1533 * @id attribute number
1534 * @attrib pointer to return attrib information corresponding to @id
1535 *
1536 * return value
1537 * -EINVAL NULL buffer passed or unsupported attribute @id.
1538 * -EPERM Identify data not valid, SMART not supported or not enabled
1539 */
1540static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1541 struct smart_attr *attrib)
1542{
1543 int rv, i;
1544 struct smart_attr *pattr;
1545
1546 if (!attrib)
1547 return -EINVAL;
1548
1549 if (!port->identify_valid) {
1550 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1551 return -EPERM;
1552 }
1553 if (!(port->identify[82] & 0x1)) {
1554 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1555 return -EPERM;
1556 }
1557 if (!(port->identify[85] & 0x1)) {
1558 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1559 return -EPERM;
1560 }
1561
1562 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1563 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1564 if (rv) {
1565 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1566 return rv;
1567 }
1568
1569 pattr = (struct smart_attr *)(port->smart_buf + 2);
1570 for (i = 0; i < 29; i++, pattr++)
1571 if (pattr->attr_id == id) {
1572 memcpy(attrib, pattr, sizeof(struct smart_attr));
1573 break;
1574 }
1575
1576 if (i == 29) {
1577 dev_warn(&port->dd->pdev->dev,
1578 "Query for invalid SMART attribute ID\n");
1579 rv = -EINVAL;
1580 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001581
Sam Bradshaw88523a62011-08-30 08:34:26 -06001582 return rv;
1583}
1584
1585/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001586 * Trim unused sectors
1587 *
1588 * @dd pointer to driver_data structure
1589 * @lba starting lba
1590 * @len # of 512b sectors to trim
1591 *
1592 * return value
1593 * -ENOMEM Out of dma memory
1594 * -EINVAL Invalid parameters passed in, trim not supported
1595 * -EIO Error submitting trim request to hw
1596 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301597static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1598 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001599{
1600 int i, rv = 0;
1601 u64 tlba, tlen, sect_left;
1602 struct mtip_trim_entry *buf;
1603 dma_addr_t dma_addr;
1604 struct host_to_dev_fis fis;
1605
1606 if (!len || dd->trim_supp == false)
1607 return -EINVAL;
1608
1609 /* Trim request too big */
1610 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1611
1612 /* Trim request not aligned on 4k boundary */
1613 WARN_ON(len % 8 != 0);
1614
1615 /* Warn if vu_trim structure is too big */
1616 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1617
1618 /* Allocate a DMA buffer for the trim structure */
1619 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1620 GFP_KERNEL);
1621 if (!buf)
1622 return -ENOMEM;
1623 memset(buf, 0, ATA_SECT_SIZE);
1624
1625 for (i = 0, sect_left = len, tlba = lba;
1626 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1627 i++) {
1628 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1629 MTIP_MAX_TRIM_ENTRY_LEN :
1630 sect_left);
1631 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1632 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1633 tlba += tlen;
1634 sect_left -= tlen;
1635 }
1636 WARN_ON(sect_left != 0);
1637
1638 /* Build the fis */
1639 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1640 fis.type = 0x27;
1641 fis.opts = 1 << 7;
1642 fis.command = 0xfb;
1643 fis.features = 0x60;
1644 fis.sect_count = 1;
1645 fis.device = ATA_DEVICE_OBS;
1646
1647 if (mtip_exec_internal_command(dd->port,
1648 &fis,
1649 5,
1650 dma_addr,
1651 ATA_SECT_SIZE,
1652 0,
1653 GFP_KERNEL,
1654 MTIP_TRIM_TIMEOUT_MS) < 0)
1655 rv = -EIO;
1656
1657 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1658 return rv;
1659}
1660
1661/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001662 * Get the drive capacity.
1663 *
1664 * @dd Pointer to the device data structure.
1665 * @sectors Pointer to the variable that will receive the sector count.
1666 *
1667 * return value
1668 * 1 Capacity was returned successfully.
1669 * 0 The identify information is invalid.
1670 */
Jens Axboe63166682011-09-27 21:27:43 -06001671static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001672{
1673 struct mtip_port *port = dd->port;
1674 u64 total, raw0, raw1, raw2, raw3;
1675 raw0 = port->identify[100];
1676 raw1 = port->identify[101];
1677 raw2 = port->identify[102];
1678 raw3 = port->identify[103];
1679 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1680 *sectors = total;
1681 return (bool) !!port->identify_valid;
1682}
1683
1684/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001685 * Display the identify command data.
1686 *
1687 * @port Pointer to the port data structure.
1688 *
1689 * return value
1690 * None
1691 */
1692static void mtip_dump_identify(struct mtip_port *port)
1693{
1694 sector_t sectors;
1695 unsigned short revid;
1696 char cbuf[42];
1697
1698 if (!port->identify_valid)
1699 return;
1700
1701 strlcpy(cbuf, (char *)(port->identify+10), 21);
1702 dev_info(&port->dd->pdev->dev,
1703 "Serial No.: %s\n", cbuf);
1704
1705 strlcpy(cbuf, (char *)(port->identify+23), 9);
1706 dev_info(&port->dd->pdev->dev,
1707 "Firmware Ver.: %s\n", cbuf);
1708
1709 strlcpy(cbuf, (char *)(port->identify+27), 41);
1710 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1711
Sam Bradshaw26d58052013-10-03 10:18:05 -07001712 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1713 port->identify[128],
1714 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1715
Sam Bradshaw88523a62011-08-30 08:34:26 -06001716 if (mtip_hw_get_capacity(port->dd, &sectors))
1717 dev_info(&port->dd->pdev->dev,
1718 "Capacity: %llu sectors (%llu MB)\n",
1719 (u64)sectors,
1720 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1721
1722 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001723 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001724 case 0x1:
1725 strlcpy(cbuf, "A0", 3);
1726 break;
1727 case 0x3:
1728 strlcpy(cbuf, "A2", 3);
1729 break;
1730 default:
1731 strlcpy(cbuf, "?", 2);
1732 break;
1733 }
1734 dev_info(&port->dd->pdev->dev,
1735 "Card Type: %s\n", cbuf);
1736}
1737
1738/*
1739 * Map the commands scatter list into the command table.
1740 *
1741 * @command Pointer to the command.
1742 * @nents Number of scatter list entries.
1743 *
1744 * return value
1745 * None
1746 */
1747static inline void fill_command_sg(struct driver_data *dd,
1748 struct mtip_cmd *command,
1749 int nents)
1750{
1751 int n;
1752 unsigned int dma_len;
1753 struct mtip_cmd_sg *command_sg;
1754 struct scatterlist *sg = command->sg;
1755
1756 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1757
1758 for (n = 0; n < nents; n++) {
1759 dma_len = sg_dma_len(sg);
1760 if (dma_len > 0x400000)
1761 dev_err(&dd->pdev->dev,
1762 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001763 command_sg->info = __force_bit2int
1764 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1765 command_sg->dba = __force_bit2int
1766 cpu_to_le32(sg_dma_address(sg));
1767 command_sg->dba_upper = __force_bit2int
1768 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001769 command_sg++;
1770 sg++;
1771 }
1772}
1773
1774/*
1775 * @brief Execute a drive command.
1776 *
1777 * return value 0 The command completed successfully.
1778 * return value -1 An error occurred while executing the command.
1779 */
Jens Axboe63166682011-09-27 21:27:43 -06001780static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001781{
1782 struct host_to_dev_fis fis;
1783 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001784 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001785
Sam Bradshaw88523a62011-08-30 08:34:26 -06001786 /* Build the FIS. */
1787 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1788 fis.type = 0x27;
1789 fis.opts = 1 << 7;
1790 fis.command = command[0];
1791 fis.features = command[1];
1792 fis.sect_count = command[2];
1793 fis.sector = command[3];
1794 fis.cyl_low = command[4];
1795 fis.cyl_hi = command[5];
1796 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1797
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001798 mtip_set_timeout(port->dd, &fis, &to, 0);
1799
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001800 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 -06001801 __func__,
1802 command[0],
1803 command[1],
1804 command[2],
1805 command[3],
1806 command[4],
1807 command[5],
1808 command[6]);
1809
1810 /* Execute the command. */
1811 if (mtip_exec_internal_command(port,
1812 &fis,
1813 5,
1814 0,
1815 0,
1816 0,
1817 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001818 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001819 return -1;
1820 }
1821
1822 command[0] = reply->command; /* Status*/
1823 command[1] = reply->features; /* Error*/
1824 command[4] = reply->cyl_low;
1825 command[5] = reply->cyl_hi;
1826
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001827 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 -06001828 __func__,
1829 command[0],
1830 command[1],
1831 command[4],
1832 command[5]);
1833
Sam Bradshaw88523a62011-08-30 08:34:26 -06001834 return 0;
1835}
1836
1837/*
1838 * @brief Execute a drive command.
1839 *
1840 * @param port Pointer to the port data structure.
1841 * @param command Pointer to the user specified command parameters.
1842 * @param user_buffer Pointer to the user space buffer where read sector
1843 * data should be copied.
1844 *
1845 * return value 0 The command completed successfully.
1846 * return value -EFAULT An error occurred while copying the completion
1847 * data to the user space buffer.
1848 * return value -1 An error occurred while executing the command.
1849 */
Jens Axboe63166682011-09-27 21:27:43 -06001850static int exec_drive_command(struct mtip_port *port, u8 *command,
1851 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001852{
1853 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001854 struct host_to_dev_fis *reply;
1855 u8 *buf = NULL;
1856 dma_addr_t dma_addr = 0;
1857 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001858 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001859
1860 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001861 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001862 return -EFAULT;
1863
1864 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1865 ATA_SECT_SIZE * xfer_sz,
1866 &dma_addr,
1867 GFP_KERNEL);
1868 if (!buf) {
1869 dev_err(&port->dd->pdev->dev,
1870 "Memory allocation failed (%d bytes)\n",
1871 ATA_SECT_SIZE * xfer_sz);
1872 return -ENOMEM;
1873 }
1874 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1875 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001876
Sam Bradshaw88523a62011-08-30 08:34:26 -06001877 /* Build the FIS. */
1878 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001879 fis.type = 0x27;
1880 fis.opts = 1 << 7;
1881 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 fis.features = command[2];
1883 fis.sect_count = command[3];
1884 if (fis.command == ATA_CMD_SMART) {
1885 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001886 fis.cyl_low = 0x4F;
1887 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001888 }
1889
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001890 mtip_set_timeout(port->dd, &fis, &to, 0);
1891
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001892 if (xfer_sz)
1893 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1894 else
1895 reply = (port->rxfis + RX_FIS_D2H_REG);
1896
Sam Bradshaw88523a62011-08-30 08:34:26 -06001897 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001898 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001899 "feat %x, sectcnt %x\n",
1900 __func__,
1901 command[0],
1902 command[1],
1903 command[2],
1904 command[3]);
1905
Sam Bradshaw88523a62011-08-30 08:34:26 -06001906 /* Execute the command. */
1907 if (mtip_exec_internal_command(port,
1908 &fis,
1909 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001910 (xfer_sz ? dma_addr : 0),
1911 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001912 0,
1913 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001914 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001915 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001916 rv = -EFAULT;
1917 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001918 }
1919
1920 /* Collect the completion status. */
1921 command[0] = reply->command; /* Status*/
1922 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001923 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001924
1925 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001926 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001927 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928 __func__,
1929 command[0],
1930 command[1],
1931 command[2]);
1932
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001933 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001934 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001935 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001936 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001937 rv = -EFAULT;
1938 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001939 }
1940 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001941exit_drive_command:
1942 if (buf)
1943 dmam_free_coherent(&port->dd->pdev->dev,
1944 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1945 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001946}
1947
1948/*
1949 * Indicates whether a command has a single sector payload.
1950 *
1951 * @command passed to the device to perform the certain event.
1952 * @features passed to the device to perform the certain event.
1953 *
1954 * return value
1955 * 1 command is one that always has a single sector payload,
1956 * regardless of the value in the Sector Count field.
1957 * 0 otherwise
1958 *
1959 */
1960static unsigned int implicit_sector(unsigned char command,
1961 unsigned char features)
1962{
1963 unsigned int rv = 0;
1964
1965 /* list of commands that have an implicit sector count of 1 */
1966 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001967 case ATA_CMD_SEC_SET_PASS:
1968 case ATA_CMD_SEC_UNLOCK:
1969 case ATA_CMD_SEC_ERASE_PREP:
1970 case ATA_CMD_SEC_ERASE_UNIT:
1971 case ATA_CMD_SEC_FREEZE_LOCK:
1972 case ATA_CMD_SEC_DISABLE_PASS:
1973 case ATA_CMD_PMP_READ:
1974 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001975 rv = 1;
1976 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001977 case ATA_CMD_SET_MAX:
1978 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001979 rv = 1;
1980 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001981 case ATA_CMD_SMART:
1982 if ((features == ATA_SMART_READ_VALUES) ||
1983 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001984 rv = 1;
1985 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001986 case ATA_CMD_CONF_OVERLAY:
1987 if ((features == ATA_DCO_IDENTIFY) ||
1988 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989 rv = 1;
1990 break;
1991 }
1992 return rv;
1993}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001994
Sam Bradshaw88523a62011-08-30 08:34:26 -06001995/*
1996 * Executes a taskfile
1997 * See ide_taskfile_ioctl() for derivation
1998 */
1999static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002000 void __user *buf,
2001 ide_task_request_t *req_task,
2002 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002003{
2004 struct host_to_dev_fis fis;
2005 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002006 u8 *outbuf = NULL;
2007 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002008 dma_addr_t outbuf_dma = 0;
2009 dma_addr_t inbuf_dma = 0;
2010 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002011 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002012 unsigned int taskin = 0;
2013 unsigned int taskout = 0;
2014 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002015 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002016 unsigned int force_single_sector;
2017 unsigned int transfer_size;
2018 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002019 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002020 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002021
2022 taskout = req_task->out_size;
2023 taskin = req_task->in_size;
2024 /* 130560 = 512 * 0xFF*/
2025 if (taskin > 130560 || taskout > 130560) {
2026 err = -EINVAL;
2027 goto abort;
2028 }
2029
2030 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002031 outbuf = memdup_user(buf + outtotal, taskout);
2032 if (IS_ERR(outbuf)) {
2033 err = PTR_ERR(outbuf);
2034 outbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002035 goto abort;
2036 }
2037 outbuf_dma = pci_map_single(dd->pdev,
2038 outbuf,
2039 taskout,
2040 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002041 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002042 err = -ENOMEM;
2043 goto abort;
2044 }
2045 dma_buffer = outbuf_dma;
2046 }
2047
2048 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002049 inbuf = memdup_user(buf + intotal, taskin);
2050 if (IS_ERR(inbuf)) {
2051 err = PTR_ERR(inbuf);
2052 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002053 goto abort;
2054 }
2055 inbuf_dma = pci_map_single(dd->pdev,
2056 inbuf,
2057 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002058 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002059 err = -ENOMEM;
2060 goto abort;
2061 }
2062 dma_buffer = inbuf_dma;
2063 }
2064
2065 /* only supports PIO and non-data commands from this ioctl. */
2066 switch (req_task->data_phase) {
2067 case TASKFILE_OUT:
2068 nsect = taskout / ATA_SECT_SIZE;
2069 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2070 break;
2071 case TASKFILE_IN:
2072 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2073 break;
2074 case TASKFILE_NO_DATA:
2075 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2076 break;
2077 default:
2078 err = -EINVAL;
2079 goto abort;
2080 }
2081
Sam Bradshaw88523a62011-08-30 08:34:26 -06002082 /* Build the FIS. */
2083 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2084
2085 fis.type = 0x27;
2086 fis.opts = 1 << 7;
2087 fis.command = req_task->io_ports[7];
2088 fis.features = req_task->io_ports[1];
2089 fis.sect_count = req_task->io_ports[2];
2090 fis.lba_low = req_task->io_ports[3];
2091 fis.lba_mid = req_task->io_ports[4];
2092 fis.lba_hi = req_task->io_ports[5];
2093 /* Clear the dev bit*/
2094 fis.device = req_task->io_ports[6] & ~0x10;
2095
2096 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2097 req_task->in_flags.all =
2098 IDE_TASKFILE_STD_IN_FLAGS |
2099 (IDE_HOB_STD_IN_FLAGS << 8);
2100 fis.lba_low_ex = req_task->hob_ports[3];
2101 fis.lba_mid_ex = req_task->hob_ports[4];
2102 fis.lba_hi_ex = req_task->hob_ports[5];
2103 fis.features_ex = req_task->hob_ports[1];
2104 fis.sect_cnt_ex = req_task->hob_ports[2];
2105
2106 } else {
2107 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2108 }
2109
2110 force_single_sector = implicit_sector(fis.command, fis.features);
2111
2112 if ((taskin || taskout) && (!fis.sect_count)) {
2113 if (nsect)
2114 fis.sect_count = nsect;
2115 else {
2116 if (!force_single_sector) {
2117 dev_warn(&dd->pdev->dev,
2118 "data movement but "
2119 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002120 err = -EINVAL;
2121 goto abort;
2122 }
2123 }
2124 }
2125
2126 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002127 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002128 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2129 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002130 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002131 fis.command,
2132 fis.features,
2133 fis.sect_count,
2134 fis.lba_low,
2135 fis.lba_mid,
2136 fis.lba_hi,
2137 fis.device);
2138
Selvan Mani4453bc82012-09-27 14:36:43 +02002139 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002140 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2141 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002142 erasemode = 1;
2143 }
2144
2145 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002146
2147 /* Determine the correct transfer size.*/
2148 if (force_single_sector)
2149 transfer_size = ATA_SECT_SIZE;
2150 else
2151 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2152
2153 /* Execute the command.*/
2154 if (mtip_exec_internal_command(dd->port,
2155 &fis,
2156 5,
2157 dma_buffer,
2158 transfer_size,
2159 0,
2160 GFP_KERNEL,
2161 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002162 err = -EIO;
2163 goto abort;
2164 }
2165
2166 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2167
2168 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2169 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2170 req_task->io_ports[7] = reply->control;
2171 } else {
2172 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2173 req_task->io_ports[7] = reply->command;
2174 }
2175
2176 /* reclaim the DMA buffers.*/
2177 if (inbuf_dma)
2178 pci_unmap_single(dd->pdev, inbuf_dma,
2179 taskin, DMA_FROM_DEVICE);
2180 if (outbuf_dma)
2181 pci_unmap_single(dd->pdev, outbuf_dma,
2182 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002183 inbuf_dma = 0;
2184 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002185
2186 /* return the ATA registers to the caller.*/
2187 req_task->io_ports[1] = reply->features;
2188 req_task->io_ports[2] = reply->sect_count;
2189 req_task->io_ports[3] = reply->lba_low;
2190 req_task->io_ports[4] = reply->lba_mid;
2191 req_task->io_ports[5] = reply->lba_hi;
2192 req_task->io_ports[6] = reply->device;
2193
2194 if (req_task->out_flags.all & 1) {
2195
2196 req_task->hob_ports[3] = reply->lba_low_ex;
2197 req_task->hob_ports[4] = reply->lba_mid_ex;
2198 req_task->hob_ports[5] = reply->lba_hi_ex;
2199 req_task->hob_ports[1] = reply->features_ex;
2200 req_task->hob_ports[2] = reply->sect_cnt_ex;
2201 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002202 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002203 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002204 "err %x, sect_cnt %x, lbalo %x,"
2205 "lbamid %x, lbahi %x, dev %x\n",
2206 __func__,
2207 req_task->io_ports[7],
2208 req_task->io_ports[1],
2209 req_task->io_ports[2],
2210 req_task->io_ports[3],
2211 req_task->io_ports[4],
2212 req_task->io_ports[5],
2213 req_task->io_ports[6]);
2214
Sam Bradshaw88523a62011-08-30 08:34:26 -06002215 if (taskout) {
2216 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2217 err = -EFAULT;
2218 goto abort;
2219 }
2220 }
2221 if (taskin) {
2222 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2223 err = -EFAULT;
2224 goto abort;
2225 }
2226 }
2227abort:
2228 if (inbuf_dma)
2229 pci_unmap_single(dd->pdev, inbuf_dma,
2230 taskin, DMA_FROM_DEVICE);
2231 if (outbuf_dma)
2232 pci_unmap_single(dd->pdev, outbuf_dma,
2233 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002234 kfree(outbuf);
2235 kfree(inbuf);
2236
2237 return err;
2238}
2239
2240/*
2241 * Handle IOCTL calls from the Block Layer.
2242 *
2243 * This function is called by the Block Layer when it receives an IOCTL
2244 * command that it does not understand. If the IOCTL command is not supported
2245 * this function returns -ENOTTY.
2246 *
2247 * @dd Pointer to the driver data structure.
2248 * @cmd IOCTL command passed from the Block Layer.
2249 * @arg IOCTL argument passed from the Block Layer.
2250 *
2251 * return value
2252 * 0 The IOCTL completed successfully.
2253 * -ENOTTY The specified command is not supported.
2254 * -EFAULT An error occurred copying data to a user space buffer.
2255 * -EIO An error occurred while executing the command.
2256 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002257static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2258 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002259{
2260 switch (cmd) {
2261 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002262 {
2263 if (copy_to_user((void __user *)arg, dd->port->identify,
2264 sizeof(u16) * ATA_ID_WORDS))
2265 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002266 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002267 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002268 case HDIO_DRIVE_CMD:
2269 {
2270 u8 drive_command[4];
2271
2272 /* Copy the user command info to our buffer. */
2273 if (copy_from_user(drive_command,
2274 (void __user *) arg,
2275 sizeof(drive_command)))
2276 return -EFAULT;
2277
2278 /* Execute the drive command. */
2279 if (exec_drive_command(dd->port,
2280 drive_command,
2281 (void __user *) (arg+4)))
2282 return -EIO;
2283
2284 /* Copy the status back to the users buffer. */
2285 if (copy_to_user((void __user *) arg,
2286 drive_command,
2287 sizeof(drive_command)))
2288 return -EFAULT;
2289
2290 break;
2291 }
2292 case HDIO_DRIVE_TASK:
2293 {
2294 u8 drive_command[7];
2295
2296 /* Copy the user command info to our buffer. */
2297 if (copy_from_user(drive_command,
2298 (void __user *) arg,
2299 sizeof(drive_command)))
2300 return -EFAULT;
2301
2302 /* Execute the drive command. */
2303 if (exec_drive_task(dd->port, drive_command))
2304 return -EIO;
2305
2306 /* Copy the status back to the users buffer. */
2307 if (copy_to_user((void __user *) arg,
2308 drive_command,
2309 sizeof(drive_command)))
2310 return -EFAULT;
2311
2312 break;
2313 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002314 case HDIO_DRIVE_TASKFILE: {
2315 ide_task_request_t req_task;
2316 int ret, outtotal;
2317
2318 if (copy_from_user(&req_task, (void __user *) arg,
2319 sizeof(req_task)))
2320 return -EFAULT;
2321
2322 outtotal = sizeof(req_task);
2323
2324 ret = exec_drive_taskfile(dd, (void __user *) arg,
2325 &req_task, outtotal);
2326
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002327 if (copy_to_user((void __user *) arg, &req_task,
2328 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002329 return -EFAULT;
2330
2331 return ret;
2332 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002333
2334 default:
2335 return -EINVAL;
2336 }
2337 return 0;
2338}
2339
2340/*
2341 * Submit an IO to the hw
2342 *
2343 * This function is called by the block layer to issue an io
2344 * to the device. Upon completion, the callback function will
2345 * be called with the data parameter passed as the callback data.
2346 *
2347 * @dd Pointer to the driver data structure.
2348 * @start First sector to read.
2349 * @nsect Number of sectors to read.
2350 * @nents Number of entries in scatter list for the read command.
2351 * @tag The tag of this read command.
2352 * @callback Pointer to the function that should be called
2353 * when the read completes.
2354 * @data Callback data passed to the callback function
2355 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002356 * @dir Direction (read or write)
2357 *
2358 * return value
2359 * None
2360 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002361static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2362 struct mtip_cmd *command, int nents,
2363 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002364{
2365 struct host_to_dev_fis *fis;
2366 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002367 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2368 u64 start = blk_rq_pos(rq);
2369 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002370
2371 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002372 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002373
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002374 prefetch(&port->flags);
2375
Sam Bradshaw88523a62011-08-30 08:34:26 -06002376 command->scatter_ents = nents;
2377
2378 /*
2379 * The number of retries for this command before it is
2380 * reported as a failure to the upper layers.
2381 */
2382 command->retries = MTIP_MAX_RETRIES;
2383
2384 /* Fill out fis */
2385 fis = command->command;
2386 fis->type = 0x27;
2387 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002388 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002389 fis->command = ATA_CMD_FPDMA_READ;
2390 else
2391 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002392 fis->lba_low = start & 0xFF;
2393 fis->lba_mid = (start >> 8) & 0xFF;
2394 fis->lba_hi = (start >> 16) & 0xFF;
2395 fis->lba_low_ex = (start >> 24) & 0xFF;
2396 fis->lba_mid_ex = (start >> 32) & 0xFF;
2397 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002399 fis->features = nsect & 0xFF;
2400 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002401 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002402 fis->sect_cnt_ex = 0;
2403 fis->control = 0;
2404 fis->res2 = 0;
2405 fis->res3 = 0;
2406 fill_command_sg(dd, command, nents);
2407
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002408 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002409 fis->device |= 1 << 7;
2410
Sam Bradshaw88523a62011-08-30 08:34:26 -06002411 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002412 command->command_header->opts =
2413 __force_bit2int cpu_to_le32(
2414 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002415 command->command_header->byte_count = 0;
2416
2417 /*
2418 * Set the completion function and data for the command
2419 * within this layer.
2420 */
2421 command->comp_data = dd;
2422 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002423 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002424
2425 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002426 * To prevent this command from being issued
2427 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002428 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002429 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002430 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002431 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002432 return;
2433 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002434
2435 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002436 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002437}
2438
2439/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002440 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002441 *
2442 * @dev Pointer to the device structure, passed by the kernrel.
2443 * @attr Pointer to the device_attribute structure passed by the kernel.
2444 * @buf Pointer to the char buffer that will receive the stats info.
2445 *
2446 * return value
2447 * The size, in bytes, of the data copied into buf.
2448 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002449static ssize_t mtip_hw_show_status(struct device *dev,
2450 struct device_attribute *attr,
2451 char *buf)
2452{
2453 struct driver_data *dd = dev_to_disk(dev)->private_data;
2454 int size = 0;
2455
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002456 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002457 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002458 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002459 size += sprintf(buf, "%s", "write_protect\n");
2460 else
2461 size += sprintf(buf, "%s", "online\n");
2462
2463 return size;
2464}
2465
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002466static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002467
Asai Thambi S P0caff002013-04-03 19:56:21 +05302468/* debugsfs entries */
2469
2470static ssize_t show_device_status(struct device_driver *drv, char *buf)
2471{
2472 int size = 0;
2473 struct driver_data *dd, *tmp;
2474 unsigned long flags;
2475 char id_buf[42];
2476 u16 status = 0;
2477
2478 spin_lock_irqsave(&dev_lock, flags);
2479 size += sprintf(&buf[size], "Devices Present:\n");
2480 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002481 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302482 if (dd->port &&
2483 dd->port->identify &&
2484 dd->port->identify_valid) {
2485 strlcpy(id_buf,
2486 (char *) (dd->port->identify + 10), 21);
2487 status = *(dd->port->identify + 141);
2488 } else {
2489 memset(id_buf, 0, 42);
2490 status = 0;
2491 }
2492
2493 if (dd->port &&
2494 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2495 size += sprintf(&buf[size],
2496 " device %s %s (ftl rebuild %d %%)\n",
2497 dev_name(&dd->pdev->dev),
2498 id_buf,
2499 status);
2500 } else {
2501 size += sprintf(&buf[size],
2502 " device %s %s\n",
2503 dev_name(&dd->pdev->dev),
2504 id_buf);
2505 }
2506 }
2507 }
2508
2509 size += sprintf(&buf[size], "Devices Being Removed:\n");
2510 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002511 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302512 if (dd->port &&
2513 dd->port->identify &&
2514 dd->port->identify_valid) {
2515 strlcpy(id_buf,
2516 (char *) (dd->port->identify+10), 21);
2517 status = *(dd->port->identify + 141);
2518 } else {
2519 memset(id_buf, 0, 42);
2520 status = 0;
2521 }
2522
2523 if (dd->port &&
2524 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2525 size += sprintf(&buf[size],
2526 " device %s %s (ftl rebuild %d %%)\n",
2527 dev_name(&dd->pdev->dev),
2528 id_buf,
2529 status);
2530 } else {
2531 size += sprintf(&buf[size],
2532 " device %s %s\n",
2533 dev_name(&dd->pdev->dev),
2534 id_buf);
2535 }
2536 }
2537 }
2538 spin_unlock_irqrestore(&dev_lock, flags);
2539
2540 return size;
2541}
2542
2543static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2544 size_t len, loff_t *offset)
2545{
David Milburnc8afd0d2013-05-23 16:23:45 -05002546 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302547 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002548 char *buf;
2549 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302550
2551 if (!len || *offset)
2552 return 0;
2553
David Milburnc8afd0d2013-05-23 16:23:45 -05002554 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2555 if (!buf) {
2556 dev_err(&dd->pdev->dev,
2557 "Memory allocation: status buffer\n");
2558 return -ENOMEM;
2559 }
2560
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561 size += show_device_status(NULL, buf);
2562
2563 *offset = size <= len ? size : len;
2564 size = copy_to_user(ubuf, buf, *offset);
2565 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002566 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302567
David Milburnc8afd0d2013-05-23 16:23:45 -05002568 kfree(buf);
2569 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302570}
2571
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002572static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2573 size_t len, loff_t *offset)
2574{
2575 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002576 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002577 u32 group_allocated;
2578 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002579 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002580
2581 if (!len || size)
2582 return 0;
2583
David Milburnc8afd0d2013-05-23 16:23:45 -05002584 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2585 if (!buf) {
2586 dev_err(&dd->pdev->dev,
2587 "Memory allocation: register buffer\n");
2588 return -ENOMEM;
2589 }
2590
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002591 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2592
2593 for (n = dd->slot_groups-1; n >= 0; n--)
2594 size += sprintf(&buf[size], "%08X ",
2595 readl(dd->port->s_active[n]));
2596
2597 size += sprintf(&buf[size], "]\n");
2598 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2599
2600 for (n = dd->slot_groups-1; n >= 0; n--)
2601 size += sprintf(&buf[size], "%08X ",
2602 readl(dd->port->cmd_issue[n]));
2603
2604 size += sprintf(&buf[size], "]\n");
2605 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2606
2607 for (n = dd->slot_groups-1; n >= 0; n--)
2608 size += sprintf(&buf[size], "%08X ",
2609 readl(dd->port->completed[n]));
2610
2611 size += sprintf(&buf[size], "]\n");
2612 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2613 readl(dd->port->mmio + PORT_IRQ_STAT));
2614 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2615 readl(dd->mmio + HOST_IRQ_STAT));
2616 size += sprintf(&buf[size], "\n");
2617
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002618 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2619
2620 for (n = dd->slot_groups-1; n >= 0; n--) {
2621 if (sizeof(long) > sizeof(u32))
2622 group_allocated =
2623 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2624 else
2625 group_allocated = dd->port->cmds_to_issue[n];
2626 size += sprintf(&buf[size], "%08X ", group_allocated);
2627 }
2628 size += sprintf(&buf[size], "]\n");
2629
2630 *offset = size <= len ? size : len;
2631 size = copy_to_user(ubuf, buf, *offset);
2632 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002633 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002634
David Milburnc8afd0d2013-05-23 16:23:45 -05002635 kfree(buf);
2636 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002637}
2638
2639static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2640 size_t len, loff_t *offset)
2641{
2642 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002643 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002644 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002645 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002646
2647 if (!len || size)
2648 return 0;
2649
David Milburnc8afd0d2013-05-23 16:23:45 -05002650 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2651 if (!buf) {
2652 dev_err(&dd->pdev->dev,
2653 "Memory allocation: flag buffer\n");
2654 return -ENOMEM;
2655 }
2656
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002657 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2658 dd->port->flags);
2659 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2660 dd->dd_flag);
2661
2662 *offset = size <= len ? size : len;
2663 size = copy_to_user(ubuf, buf, *offset);
2664 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002665 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002666
David Milburnc8afd0d2013-05-23 16:23:45 -05002667 kfree(buf);
2668 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002669}
2670
Asai Thambi S P0caff002013-04-03 19:56:21 +05302671static const struct file_operations mtip_device_status_fops = {
2672 .owner = THIS_MODULE,
2673 .open = simple_open,
2674 .read = mtip_hw_read_device_status,
2675 .llseek = no_llseek,
2676};
2677
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002678static const struct file_operations mtip_regs_fops = {
2679 .owner = THIS_MODULE,
2680 .open = simple_open,
2681 .read = mtip_hw_read_registers,
2682 .llseek = no_llseek,
2683};
2684
2685static const struct file_operations mtip_flags_fops = {
2686 .owner = THIS_MODULE,
2687 .open = simple_open,
2688 .read = mtip_hw_read_flags,
2689 .llseek = no_llseek,
2690};
2691
Sam Bradshaw88523a62011-08-30 08:34:26 -06002692/*
2693 * Create the sysfs related attributes.
2694 *
2695 * @dd Pointer to the driver data structure.
2696 * @kobj Pointer to the kobj for the block device.
2697 *
2698 * return value
2699 * 0 Operation completed successfully.
2700 * -EINVAL Invalid parameter.
2701 */
Jens Axboe63166682011-09-27 21:27:43 -06002702static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002703{
2704 if (!kobj || !dd)
2705 return -EINVAL;
2706
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002707 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2708 dev_warn(&dd->pdev->dev,
2709 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002710 return 0;
2711}
2712
2713/*
2714 * Remove the sysfs related attributes.
2715 *
2716 * @dd Pointer to the driver data structure.
2717 * @kobj Pointer to the kobj for the block device.
2718 *
2719 * return value
2720 * 0 Operation completed successfully.
2721 * -EINVAL Invalid parameter.
2722 */
Jens Axboe63166682011-09-27 21:27:43 -06002723static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002724{
2725 if (!kobj || !dd)
2726 return -EINVAL;
2727
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002728 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002729
2730 return 0;
2731}
2732
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002733static int mtip_hw_debugfs_init(struct driver_data *dd)
2734{
2735 if (!dfs_parent)
2736 return -1;
2737
2738 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2739 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2740 dev_warn(&dd->pdev->dev,
2741 "Error creating node %s under debugfs\n",
2742 dd->disk->disk_name);
2743 dd->dfs_node = NULL;
2744 return -1;
2745 }
2746
2747 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2748 &mtip_flags_fops);
2749 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2750 &mtip_regs_fops);
2751
2752 return 0;
2753}
2754
2755static void mtip_hw_debugfs_exit(struct driver_data *dd)
2756{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002757 if (dd->dfs_node)
2758 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002759}
2760
Sam Bradshaw88523a62011-08-30 08:34:26 -06002761/*
2762 * Perform any init/resume time hardware setup
2763 *
2764 * @dd Pointer to the driver data structure.
2765 *
2766 * return value
2767 * None
2768 */
2769static inline void hba_setup(struct driver_data *dd)
2770{
2771 u32 hwdata;
2772 hwdata = readl(dd->mmio + HOST_HSORG);
2773
2774 /* interrupt bug workaround: use only 1 IS bit.*/
2775 writel(hwdata |
2776 HSORG_DISABLE_SLOTGRP_INTR |
2777 HSORG_DISABLE_SLOTGRP_PXIS,
2778 dd->mmio + HOST_HSORG);
2779}
2780
Asai Thambi S P2077d942013-04-29 21:19:49 +02002781static int mtip_device_unaligned_constrained(struct driver_data *dd)
2782{
2783 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2784}
2785
Sam Bradshaw88523a62011-08-30 08:34:26 -06002786/*
2787 * Detect the details of the product, and store anything needed
2788 * into the driver data structure. This includes product type and
2789 * version and number of slot groups.
2790 *
2791 * @dd Pointer to the driver data structure.
2792 *
2793 * return value
2794 * None
2795 */
2796static void mtip_detect_product(struct driver_data *dd)
2797{
2798 u32 hwdata;
2799 unsigned int rev, slotgroups;
2800
2801 /*
2802 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2803 * info register:
2804 * [15:8] hardware/software interface rev#
2805 * [ 3] asic-style interface
2806 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2807 */
2808 hwdata = readl(dd->mmio + HOST_HSORG);
2809
2810 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2811 dd->slot_groups = 1;
2812
2813 if (hwdata & 0x8) {
2814 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2815 rev = (hwdata & HSORG_HWREV) >> 8;
2816 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2817 dev_info(&dd->pdev->dev,
2818 "ASIC-FPGA design, HS rev 0x%x, "
2819 "%i slot groups [%i slots]\n",
2820 rev,
2821 slotgroups,
2822 slotgroups * 32);
2823
2824 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2825 dev_warn(&dd->pdev->dev,
2826 "Warning: driver only supports "
2827 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2828 slotgroups = MTIP_MAX_SLOT_GROUPS;
2829 }
2830 dd->slot_groups = slotgroups;
2831 return;
2832 }
2833
2834 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2835}
2836
2837/*
2838 * Blocking wait for FTL rebuild to complete
2839 *
2840 * @dd Pointer to the DRIVER_DATA structure.
2841 *
2842 * return value
2843 * 0 FTL rebuild completed successfully
2844 * -EFAULT FTL rebuild error/timeout/interruption
2845 */
2846static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2847{
2848 unsigned long timeout, cnt = 0, start;
2849
2850 dev_warn(&dd->pdev->dev,
2851 "FTL rebuild in progress. Polling for completion.\n");
2852
2853 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002854 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2855
2856 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002857 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002858 &dd->dd_flag)))
2859 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002860 if (mtip_check_surprise_removal(dd->pdev))
2861 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002862
Sam Bradshaw88523a62011-08-30 08:34:26 -06002863 if (mtip_get_identify(dd->port, NULL) < 0)
2864 return -EFAULT;
2865
2866 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2867 MTIP_FTL_REBUILD_MAGIC) {
2868 ssleep(1);
2869 /* Print message every 3 minutes */
2870 if (cnt++ >= 180) {
2871 dev_warn(&dd->pdev->dev,
2872 "FTL rebuild in progress (%d secs).\n",
2873 jiffies_to_msecs(jiffies - start) / 1000);
2874 cnt = 0;
2875 }
2876 } else {
2877 dev_warn(&dd->pdev->dev,
2878 "FTL rebuild complete (%d secs).\n",
2879 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002880 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002881 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002882 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002883 } while (time_before(jiffies, timeout));
2884
2885 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002886 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002887 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2888 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002889 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002890}
2891
2892/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002893 * service thread to issue queued commands
2894 *
2895 * @data Pointer to the driver data structure.
2896 *
2897 * return value
2898 * 0
2899 */
2900
2901static int mtip_service_thread(void *data)
2902{
2903 struct driver_data *dd = (struct driver_data *)data;
2904 unsigned long slot, slot_start, slot_wrap;
2905 unsigned int num_cmd_slots = dd->slot_groups * 32;
2906 struct mtip_port *port = dd->port;
2907
2908 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002909 if (kthread_should_stop() ||
2910 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2911 goto st_out;
2912 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2913
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002914 /*
2915 * the condition is to check neither an internal command is
2916 * is in progress nor error handling is active
2917 */
2918 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002919 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002920
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002921 if (kthread_should_stop() ||
2922 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2923 goto st_out;
2924
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002925 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002926 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002927 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002928
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002929 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2930
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002931restart_eh:
2932 /* Demux bits: start with error handling */
2933 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2934 mtip_handle_tfe(dd);
2935 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2936 }
2937
2938 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2939 goto restart_eh;
2940
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002941 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002942 slot = 1;
2943 /* used to restrict the loop to one iteration */
2944 slot_start = num_cmd_slots;
2945 slot_wrap = 0;
2946 while (1) {
2947 slot = find_next_bit(port->cmds_to_issue,
2948 num_cmd_slots, slot);
2949 if (slot_wrap == 1) {
2950 if ((slot_start >= slot) ||
2951 (slot >= num_cmd_slots))
2952 break;
2953 }
2954 if (unlikely(slot_start == num_cmd_slots))
2955 slot_start = slot;
2956
2957 if (unlikely(slot == num_cmd_slots)) {
2958 slot = 1;
2959 slot_wrap = 1;
2960 continue;
2961 }
2962
2963 /* Issue the command to the hardware */
2964 mtip_issue_ncq_command(port, slot);
2965
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002966 clear_bit(slot, port->cmds_to_issue);
2967 }
2968
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002969 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002970 }
2971
2972 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08002973 if (mtip_ftl_rebuild_poll(dd) == 0)
2974 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002975 }
2976 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002977
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002978st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002979 return 0;
2980}
2981
2982/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002983 * DMA region teardown
2984 *
2985 * @dd Pointer to driver_data structure
2986 *
2987 * return value
2988 * None
2989 */
2990static void mtip_dma_free(struct driver_data *dd)
2991{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002992 struct mtip_port *port = dd->port;
2993
2994 if (port->block1)
2995 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2996 port->block1, port->block1_dma);
2997
2998 if (port->command_list) {
2999 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3000 port->command_list, port->command_list_dma);
3001 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003002}
3003
3004/*
3005 * DMA region setup
3006 *
3007 * @dd Pointer to driver_data structure
3008 *
3009 * return value
3010 * -ENOMEM Not enough free DMA region space to initialize driver
3011 */
3012static int mtip_dma_alloc(struct driver_data *dd)
3013{
3014 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003015
3016 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3017 port->block1 =
3018 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3019 &port->block1_dma, GFP_KERNEL);
3020 if (!port->block1)
3021 return -ENOMEM;
3022 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3023
3024 /* Allocate dma memory for command list */
3025 port->command_list =
3026 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3027 &port->command_list_dma, GFP_KERNEL);
3028 if (!port->command_list) {
3029 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3030 port->block1, port->block1_dma);
3031 port->block1 = NULL;
3032 port->block1_dma = 0;
3033 return -ENOMEM;
3034 }
3035 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3036
3037 /* Setup all pointers into first DMA region */
3038 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3039 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3040 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3041 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3042 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3043 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3044 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3045 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3046
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003047 return 0;
3048}
3049
Jens Axboeffc771b2014-05-09 09:42:02 -06003050static int mtip_hw_get_identify(struct driver_data *dd)
3051{
3052 struct smart_attr attr242;
3053 unsigned char *buf;
3054 int rv;
3055
3056 if (mtip_get_identify(dd->port, NULL) < 0)
3057 return -EFAULT;
3058
3059 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3060 MTIP_FTL_REBUILD_MAGIC) {
3061 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3062 return MTIP_FTL_REBUILD_MAGIC;
3063 }
3064 mtip_dump_identify(dd->port);
3065
3066 /* check write protect, over temp and rebuild statuses */
3067 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3068 dd->port->log_buf,
3069 dd->port->log_buf_dma, 1);
3070 if (rv) {
3071 dev_warn(&dd->pdev->dev,
3072 "Error in READ LOG EXT (10h) command\n");
3073 /* non-critical error, don't fail the load */
3074 } else {
3075 buf = (unsigned char *)dd->port->log_buf;
3076 if (buf[259] & 0x1) {
3077 dev_info(&dd->pdev->dev,
3078 "Write protect bit is set.\n");
3079 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3080 }
3081 if (buf[288] == 0xF7) {
3082 dev_info(&dd->pdev->dev,
3083 "Exceeded Tmax, drive in thermal shutdown.\n");
3084 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3085 }
3086 if (buf[288] == 0xBF) {
3087 dev_info(&dd->pdev->dev,
3088 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003089 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06003090 }
3091 }
3092
3093 /* get write protect progess */
3094 memset(&attr242, 0, sizeof(struct smart_attr));
3095 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3096 dev_warn(&dd->pdev->dev,
3097 "Unable to check write protect progress\n");
3098 else
3099 dev_info(&dd->pdev->dev,
3100 "Write protect progress: %u%% (%u blocks)\n",
3101 attr242.cur, le32_to_cpu(attr242.data));
3102
3103 return rv;
3104}
3105
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003106/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003107 * Called once for each card.
3108 *
3109 * @dd Pointer to the driver data structure.
3110 *
3111 * return value
3112 * 0 on success, else an error code.
3113 */
Jens Axboe63166682011-09-27 21:27:43 -06003114static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003115{
3116 int i;
3117 int rv;
3118 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003119 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003120
3121 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3122
3123 mtip_detect_product(dd);
3124 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3125 rv = -EIO;
3126 goto out1;
3127 }
3128 num_command_slots = dd->slot_groups * 32;
3129
3130 hba_setup(dd);
3131
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003132 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3133 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003134 if (!dd->port) {
3135 dev_err(&dd->pdev->dev,
3136 "Memory allocation: port structure\n");
3137 return -ENOMEM;
3138 }
3139
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003140 /* Continue workqueue setup */
3141 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3142 dd->work[i].port = dd->port;
3143
Asai Thambi S P2077d942013-04-29 21:19:49 +02003144 /* Enable unaligned IO constraints for some devices */
3145 if (mtip_device_unaligned_constrained(dd))
3146 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3147 else
3148 dd->unal_qdepth = 0;
3149
Asai Thambi S P2077d942013-04-29 21:19:49 +02003150 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003151
3152 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003153 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3154 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003155
3156 /* Set the port mmio base address. */
3157 dd->port->mmio = dd->mmio + PORT_OFFSET;
3158 dd->port->dd = dd;
3159
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003160 /* DMA allocations */
3161 rv = mtip_dma_alloc(dd);
3162 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003163 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003164
3165 /* Setup the pointers to the extended s_active and CI registers. */
3166 for (i = 0; i < dd->slot_groups; i++) {
3167 dd->port->s_active[i] =
3168 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3169 dd->port->cmd_issue[i] =
3170 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3171 dd->port->completed[i] =
3172 dd->port->mmio + i*0x80 + PORT_SDBV;
3173 }
3174
Asai Thambi S P45038362012-04-09 08:35:38 +02003175 timetaken = jiffies;
3176 timeout = jiffies + msecs_to_jiffies(30000);
3177 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3178 time_before(jiffies, timeout)) {
3179 mdelay(100);
3180 }
3181 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3182 timetaken = jiffies - timetaken;
3183 dev_warn(&dd->pdev->dev,
3184 "Surprise removal detected at %u ms\n",
3185 jiffies_to_msecs(timetaken));
3186 rv = -ENODEV;
3187 goto out2 ;
3188 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003189 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003190 timetaken = jiffies - timetaken;
3191 dev_warn(&dd->pdev->dev,
3192 "Removal detected at %u ms\n",
3193 jiffies_to_msecs(timetaken));
3194 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003195 goto out2;
3196 }
3197
Asai Thambi S P45038362012-04-09 08:35:38 +02003198 /* Conditionally reset the HBA. */
3199 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3200 if (mtip_hba_reset(dd) < 0) {
3201 dev_err(&dd->pdev->dev,
3202 "Card did not reset within timeout\n");
3203 rv = -EIO;
3204 goto out2;
3205 }
3206 } else {
3207 /* Clear any pending interrupts on the HBA */
3208 writel(readl(dd->mmio + HOST_IRQ_STAT),
3209 dd->mmio + HOST_IRQ_STAT);
3210 }
3211
Sam Bradshaw88523a62011-08-30 08:34:26 -06003212 mtip_init_port(dd->port);
3213 mtip_start_port(dd->port);
3214
3215 /* Setup the ISR and enable interrupts. */
3216 rv = devm_request_irq(&dd->pdev->dev,
3217 dd->pdev->irq,
3218 mtip_irq_handler,
3219 IRQF_SHARED,
3220 dev_driver_string(&dd->pdev->dev),
3221 dd);
3222
3223 if (rv) {
3224 dev_err(&dd->pdev->dev,
3225 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3226 goto out2;
3227 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003228 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003229
3230 /* Enable interrupts on the HBA. */
3231 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3232 dd->mmio + HOST_CTL);
3233
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003234 init_waitqueue_head(&dd->port->svc_wait);
3235
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003236 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003237 rv = -EFAULT;
3238 goto out3;
3239 }
3240
Sam Bradshaw88523a62011-08-30 08:34:26 -06003241 return rv;
3242
3243out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003244 /* Disable interrupts on the HBA. */
3245 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3246 dd->mmio + HOST_CTL);
3247
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003248 /* Release the IRQ. */
3249 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003250 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3251
3252out2:
3253 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003254 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003255
Sam Bradshaw88523a62011-08-30 08:34:26 -06003256out1:
3257 /* Free the memory allocated for the for structure. */
3258 kfree(dd->port);
3259
3260 return rv;
3261}
3262
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003263static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003264{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003265 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003266
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003267 if (dd->sr || !dd->port)
3268 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003269 /*
3270 * Send standby immediate (E0h) to the drive so that it
3271 * saves its state.
3272 */
3273 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003274 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3275 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3276 rv = mtip_standby_immediate(dd->port);
3277 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003278 dev_warn(&dd->pdev->dev,
3279 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003280 }
3281 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003282}
3283
Sam Bradshaw88523a62011-08-30 08:34:26 -06003284/*
3285 * Called to deinitialize an interface.
3286 *
3287 * @dd Pointer to the driver data structure.
3288 *
3289 * return value
3290 * 0
3291 */
Jens Axboe63166682011-09-27 21:27:43 -06003292static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003293{
3294 /*
3295 * Send standby immediate (E0h) to the drive so that it
3296 * saves its state.
3297 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003298 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003299 /* de-initialize the port. */
3300 mtip_deinit_port(dd->port);
3301
3302 /* Disable interrupts on the HBA. */
3303 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3304 dd->mmio + HOST_CTL);
3305 }
3306
Sam Bradshaw88523a62011-08-30 08:34:26 -06003307 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003308 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003309 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003310 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003311
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003312 /* Free dma regions */
3313 mtip_dma_free(dd);
3314
Sam Bradshaw88523a62011-08-30 08:34:26 -06003315 /* Free the memory allocated for the for structure. */
3316 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003317 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003318
3319 return 0;
3320}
3321
3322/*
3323 * Issue a Standby Immediate command to the device.
3324 *
3325 * This function is called by the Block Layer just before the
3326 * system powers off during a shutdown.
3327 *
3328 * @dd Pointer to the driver data structure.
3329 *
3330 * return value
3331 * 0
3332 */
Jens Axboe63166682011-09-27 21:27:43 -06003333static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003334{
3335 /*
3336 * Send standby immediate (E0h) to the drive so that it
3337 * saves its state.
3338 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003339 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003340
3341 return 0;
3342}
3343
3344/*
3345 * Suspend function
3346 *
3347 * This function is called by the Block Layer just before the
3348 * system hibernates.
3349 *
3350 * @dd Pointer to the driver data structure.
3351 *
3352 * return value
3353 * 0 Suspend was successful
3354 * -EFAULT Suspend was not successful
3355 */
Jens Axboe63166682011-09-27 21:27:43 -06003356static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003357{
3358 /*
3359 * Send standby immediate (E0h) to the drive
3360 * so that it saves its state.
3361 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003362 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003363 dev_err(&dd->pdev->dev,
3364 "Failed standby-immediate command\n");
3365 return -EFAULT;
3366 }
3367
3368 /* Disable interrupts on the HBA.*/
3369 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3370 dd->mmio + HOST_CTL);
3371 mtip_deinit_port(dd->port);
3372
3373 return 0;
3374}
3375
3376/*
3377 * Resume function
3378 *
3379 * This function is called by the Block Layer as the
3380 * system resumes.
3381 *
3382 * @dd Pointer to the driver data structure.
3383 *
3384 * return value
3385 * 0 Resume was successful
3386 * -EFAULT Resume was not successful
3387 */
Jens Axboe63166682011-09-27 21:27:43 -06003388static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003389{
3390 /* Perform any needed hardware setup steps */
3391 hba_setup(dd);
3392
3393 /* Reset the HBA */
3394 if (mtip_hba_reset(dd) != 0) {
3395 dev_err(&dd->pdev->dev,
3396 "Unable to reset the HBA\n");
3397 return -EFAULT;
3398 }
3399
3400 /*
3401 * Enable the port, DMA engine, and FIS reception specific
3402 * h/w in controller.
3403 */
3404 mtip_init_port(dd->port);
3405 mtip_start_port(dd->port);
3406
3407 /* Enable interrupts on the HBA.*/
3408 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3409 dd->mmio + HOST_CTL);
3410
3411 return 0;
3412}
3413
3414/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003415 * Helper function for reusing disk name
3416 * upon hot insertion.
3417 */
3418static int rssd_disk_name_format(char *prefix,
3419 int index,
3420 char *buf,
3421 int buflen)
3422{
3423 const int base = 'z' - 'a' + 1;
3424 char *begin = buf + strlen(prefix);
3425 char *end = buf + buflen;
3426 char *p;
3427 int unit;
3428
3429 p = end - 1;
3430 *p = '\0';
3431 unit = base;
3432 do {
3433 if (p == begin)
3434 return -EINVAL;
3435 *--p = 'a' + (index % unit);
3436 index = (index / unit) - 1;
3437 } while (index >= 0);
3438
3439 memmove(begin, p, end - p);
3440 memcpy(buf, prefix, strlen(prefix));
3441
3442 return 0;
3443}
3444
3445/*
3446 * Block layer IOCTL handler.
3447 *
3448 * @dev Pointer to the block_device structure.
3449 * @mode ignored
3450 * @cmd IOCTL command passed from the user application.
3451 * @arg Argument passed from the user application.
3452 *
3453 * return value
3454 * 0 IOCTL completed successfully.
3455 * -ENOTTY IOCTL not supported or invalid driver data
3456 * structure pointer.
3457 */
3458static int mtip_block_ioctl(struct block_device *dev,
3459 fmode_t mode,
3460 unsigned cmd,
3461 unsigned long arg)
3462{
3463 struct driver_data *dd = dev->bd_disk->private_data;
3464
3465 if (!capable(CAP_SYS_ADMIN))
3466 return -EACCES;
3467
3468 if (!dd)
3469 return -ENOTTY;
3470
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003471 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003472 return -ENOTTY;
3473
Sam Bradshaw88523a62011-08-30 08:34:26 -06003474 switch (cmd) {
3475 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003476 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003477 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003478 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003479 }
3480}
3481
Jens Axboe16d02c02011-09-27 15:50:01 -06003482#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003483/*
3484 * Block layer compat IOCTL handler.
3485 *
3486 * @dev Pointer to the block_device structure.
3487 * @mode ignored
3488 * @cmd IOCTL command passed from the user application.
3489 * @arg Argument passed from the user application.
3490 *
3491 * return value
3492 * 0 IOCTL completed successfully.
3493 * -ENOTTY IOCTL not supported or invalid driver data
3494 * structure pointer.
3495 */
3496static int mtip_block_compat_ioctl(struct block_device *dev,
3497 fmode_t mode,
3498 unsigned cmd,
3499 unsigned long arg)
3500{
3501 struct driver_data *dd = dev->bd_disk->private_data;
3502
3503 if (!capable(CAP_SYS_ADMIN))
3504 return -EACCES;
3505
3506 if (!dd)
3507 return -ENOTTY;
3508
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003509 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003510 return -ENOTTY;
3511
Sam Bradshaw88523a62011-08-30 08:34:26 -06003512 switch (cmd) {
3513 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003514 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003515 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003516 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003517 ide_task_request_t req_task;
3518 int compat_tasksize, outtotal, ret;
3519
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003520 compat_tasksize =
3521 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003522
3523 compat_req_task =
3524 (struct mtip_compat_ide_task_request_s __user *) arg;
3525
3526 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003527 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003528 return -EFAULT;
3529
3530 if (get_user(req_task.out_size, &compat_req_task->out_size))
3531 return -EFAULT;
3532
3533 if (get_user(req_task.in_size, &compat_req_task->in_size))
3534 return -EFAULT;
3535
3536 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3537
3538 ret = exec_drive_taskfile(dd, (void __user *) arg,
3539 &req_task, outtotal);
3540
3541 if (copy_to_user((void __user *) arg, &req_task,
3542 compat_tasksize -
3543 (2 * sizeof(compat_long_t))))
3544 return -EFAULT;
3545
3546 if (put_user(req_task.out_size, &compat_req_task->out_size))
3547 return -EFAULT;
3548
3549 if (put_user(req_task.in_size, &compat_req_task->in_size))
3550 return -EFAULT;
3551
3552 return ret;
3553 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003554 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003555 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003556 }
3557}
Jens Axboe16d02c02011-09-27 15:50:01 -06003558#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003559
3560/*
3561 * Obtain the geometry of the device.
3562 *
3563 * You may think that this function is obsolete, but some applications,
3564 * fdisk for example still used CHS values. This function describes the
3565 * device as having 224 heads and 56 sectors per cylinder. These values are
3566 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3567 * partition is described in terms of a start and end cylinder this means
3568 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3569 * affects performance.
3570 *
3571 * @dev Pointer to the block_device strucutre.
3572 * @geo Pointer to a hd_geometry structure.
3573 *
3574 * return value
3575 * 0 Operation completed successfully.
3576 * -ENOTTY An error occurred while reading the drive capacity.
3577 */
3578static int mtip_block_getgeo(struct block_device *dev,
3579 struct hd_geometry *geo)
3580{
3581 struct driver_data *dd = dev->bd_disk->private_data;
3582 sector_t capacity;
3583
3584 if (!dd)
3585 return -ENOTTY;
3586
3587 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3588 dev_warn(&dd->pdev->dev,
3589 "Could not get drive capacity.\n");
3590 return -ENOTTY;
3591 }
3592
3593 geo->heads = 224;
3594 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003595 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003596 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003597 return 0;
3598}
3599
Asai Thambi SP51c65702016-02-24 21:18:10 -08003600static int mtip_block_open(struct block_device *dev, fmode_t mode)
3601{
3602 struct driver_data *dd;
3603
3604 if (dev && dev->bd_disk) {
3605 dd = (struct driver_data *) dev->bd_disk->private_data;
3606
3607 if (dd) {
3608 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3609 &dd->dd_flag)) {
3610 return -ENODEV;
3611 }
3612 return 0;
3613 }
3614 }
3615 return -ENODEV;
3616}
3617
3618void mtip_block_release(struct gendisk *disk, fmode_t mode)
3619{
3620}
3621
Sam Bradshaw88523a62011-08-30 08:34:26 -06003622/*
3623 * Block device operation function.
3624 *
3625 * This structure contains pointers to the functions required by the block
3626 * layer.
3627 */
3628static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003629 .open = mtip_block_open,
3630 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003631 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003632#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003633 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003634#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003635 .getgeo = mtip_block_getgeo,
3636 .owner = THIS_MODULE
3637};
3638
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003639static inline bool is_se_active(struct driver_data *dd)
3640{
3641 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3642 if (dd->port->ic_pause_timer) {
3643 unsigned long to = dd->port->ic_pause_timer +
3644 msecs_to_jiffies(1000);
3645 if (time_after(jiffies, to)) {
3646 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3647 &dd->port->flags);
3648 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3649 dd->port->ic_pause_timer = 0;
3650 wake_up_interruptible(&dd->port->svc_wait);
3651 return false;
3652 }
3653 }
3654 return true;
3655 }
3656 return false;
3657}
3658
Sam Bradshaw88523a62011-08-30 08:34:26 -06003659/*
3660 * Block layer make request function.
3661 *
3662 * This function is called by the kernel to process a BIO for
3663 * the P320 device.
3664 *
3665 * @queue Pointer to the request queue. Unused other than to obtain
3666 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003667 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003668 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003669 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003670static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003671{
Jens Axboeffc771b2014-05-09 09:42:02 -06003672 struct driver_data *dd = hctx->queue->queuedata;
3673 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3674 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003675
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003676 if (is_se_active(dd))
3677 return -ENODATA;
3678
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003679 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3680 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3681 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003682 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003683 }
3684 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003685 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003686 }
3687 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3688 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003689 rq_data_dir(rq))) {
3690 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003691 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003692 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3693 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003694 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003695 }
3696
Jens Axboeffc771b2014-05-09 09:42:02 -06003697 if (rq->cmd_flags & REQ_DISCARD) {
3698 int err;
3699
3700 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003701 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003702 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003703 }
3704
Jens Axboeffc771b2014-05-09 09:42:02 -06003705 /* Create the scatter list for this request. */
3706 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003707
Jens Axboeffc771b2014-05-09 09:42:02 -06003708 /* Issue the read/write. */
3709 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3710 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003711}
3712
Jens Axboeffc771b2014-05-09 09:42:02 -06003713static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3714 struct request *rq)
3715{
3716 struct driver_data *dd = hctx->queue->queuedata;
3717 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3718
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003719 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003720 return false;
3721
3722 /*
3723 * If unaligned depth must be limited on this controller, mark it
3724 * as unaligned if the IO isn't on a 4k boundary (start of length).
3725 */
3726 if (blk_rq_sectors(rq) <= 64) {
3727 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3728 cmd->unaligned = 1;
3729 }
3730
3731 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3732 return true;
3733
3734 return false;
3735}
3736
Jens Axboe74c45052014-10-29 11:14:52 -06003737static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3738 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003739{
Jens Axboe74c45052014-10-29 11:14:52 -06003740 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003741 int ret;
3742
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003743 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003744 return BLK_MQ_RQ_QUEUE_BUSY;
3745
Christoph Hellwige2490072014-09-13 16:40:09 -07003746 blk_mq_start_request(rq);
3747
Jens Axboeffc771b2014-05-09 09:42:02 -06003748 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003749 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003750 return BLK_MQ_RQ_QUEUE_OK;
3751
3752 rq->errors = ret;
3753 return BLK_MQ_RQ_QUEUE_ERROR;
3754}
3755
3756static void mtip_free_cmd(void *data, struct request *rq,
3757 unsigned int hctx_idx, unsigned int request_idx)
3758{
3759 struct driver_data *dd = data;
3760 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3761
3762 if (!cmd->command)
3763 return;
3764
3765 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3766 cmd->command, cmd->command_dma);
3767}
3768
3769static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3770 unsigned int request_idx, unsigned int numa_node)
3771{
3772 struct driver_data *dd = data;
3773 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3774 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3775
Jeff Moyer74c9c912015-07-29 10:22:50 -04003776 /*
3777 * For flush requests, request_idx starts at the end of the
3778 * tag space. Since we don't support FLUSH/FUA, simply return
3779 * 0 as there's nothing to be done.
3780 */
3781 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3782 return 0;
3783
Jens Axboeffc771b2014-05-09 09:42:02 -06003784 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3785 &cmd->command_dma, GFP_KERNEL);
3786 if (!cmd->command)
3787 return -ENOMEM;
3788
3789 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3790
3791 /* Point the command headers at the command tables. */
3792 cmd->command_header = dd->port->command_list +
3793 (sizeof(struct mtip_cmd_hdr) * request_idx);
3794 cmd->command_header_dma = dd->port->command_list_dma +
3795 (sizeof(struct mtip_cmd_hdr) * request_idx);
3796
3797 if (host_cap_64)
3798 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3799
3800 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3801
3802 sg_init_table(cmd->sg, MTIP_MAX_SG);
3803 return 0;
3804}
3805
3806static struct blk_mq_ops mtip_mq_ops = {
3807 .queue_rq = mtip_queue_rq,
3808 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003809 .init_request = mtip_init_cmd,
3810 .exit_request = mtip_free_cmd,
3811};
3812
Sam Bradshaw88523a62011-08-30 08:34:26 -06003813/*
3814 * Block layer initialization function.
3815 *
3816 * This function is called once by the PCI layer for each P320
3817 * device that is connected to the system.
3818 *
3819 * @dd Pointer to the driver data structure.
3820 *
3821 * return value
3822 * 0 on success else an error code.
3823 */
Jens Axboe63166682011-09-27 21:27:43 -06003824static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003825{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003826 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003827 sector_t capacity;
3828 unsigned int index = 0;
3829 struct kobject *kobj;
3830
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003831 if (dd->disk)
3832 goto skip_create_disk; /* hw init done, before rebuild */
3833
Jens Axboeffc771b2014-05-09 09:42:02 -06003834 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003835 rv = -EINVAL;
3836 goto protocol_init_error;
3837 }
3838
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003839 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003840 if (dd->disk == NULL) {
3841 dev_err(&dd->pdev->dev,
3842 "Unable to allocate gendisk structure\n");
3843 rv = -EINVAL;
3844 goto alloc_disk_error;
3845 }
3846
3847 /* Generate the disk name, implemented same as in sd.c */
3848 do {
3849 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3850 goto ida_get_error;
3851
3852 spin_lock(&rssd_index_lock);
3853 rv = ida_get_new(&rssd_index_ida, &index);
3854 spin_unlock(&rssd_index_lock);
3855 } while (rv == -EAGAIN);
3856
3857 if (rv)
3858 goto ida_get_error;
3859
3860 rv = rssd_disk_name_format("rssd",
3861 index,
3862 dd->disk->disk_name,
3863 DISK_NAME_LEN);
3864 if (rv)
3865 goto disk_index_error;
3866
3867 dd->disk->driverfs_dev = &dd->pdev->dev;
3868 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003869 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3870 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003871 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003872 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003873 dd->index = index;
3874
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003875 mtip_hw_debugfs_init(dd);
3876
Jens Axboeffc771b2014-05-09 09:42:02 -06003877 memset(&dd->tags, 0, sizeof(dd->tags));
3878 dd->tags.ops = &mtip_mq_ops;
3879 dd->tags.nr_hw_queues = 1;
3880 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3881 dd->tags.reserved_tags = 1;
3882 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3883 dd->tags.numa_node = dd->numa_node;
3884 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3885 dd->tags.driver_data = dd;
3886
3887 rv = blk_mq_alloc_tag_set(&dd->tags);
3888 if (rv) {
3889 dev_err(&dd->pdev->dev,
3890 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003891 goto block_queue_alloc_init_error;
3892 }
3893
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003894 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003895 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003896 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003897 dev_err(&dd->pdev->dev,
3898 "Unable to allocate request queue\n");
3899 rv = -ENOMEM;
3900 goto block_queue_alloc_init_error;
3901 }
3902
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003903 dd->disk->queue = dd->queue;
3904 dd->queue->queuedata = dd;
3905
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003906skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003907 /* Initialize the protocol layer. */
3908 wait_for_rebuild = mtip_hw_get_identify(dd);
3909 if (wait_for_rebuild < 0) {
3910 dev_err(&dd->pdev->dev,
3911 "Protocol layer initialization failed\n");
3912 rv = -EINVAL;
3913 goto init_hw_cmds_error;
3914 }
3915
3916 /*
3917 * if rebuild pending, start the service thread, and delay the block
3918 * queue creation and add_disk()
3919 */
3920 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3921 goto start_service_thread;
3922
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003923 /* Set device limits. */
3924 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003925 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003926 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3927 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003928 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3929 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003930 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003931 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003932
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003933 /*
3934 * write back cache is not supported in the device. FUA depends on
3935 * write back cache support, hence setting flush support to zero.
3936 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003937 blk_queue_flush(dd->queue, 0);
3938
Asai Thambi S P15283462013-01-11 14:41:34 +01003939 /* Signal trim support */
3940 if (dd->trim_supp == true) {
3941 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3942 dd->queue->limits.discard_granularity = 4096;
3943 blk_queue_max_discard_sectors(dd->queue,
3944 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3945 dd->queue->limits.discard_zeroes_data = 0;
3946 }
3947
Sam Bradshaw88523a62011-08-30 08:34:26 -06003948 /* Set the capacity of the device in 512 byte sectors. */
3949 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3950 dev_warn(&dd->pdev->dev,
3951 "Could not read drive capacity\n");
3952 rv = -EIO;
3953 goto read_capacity_error;
3954 }
3955 set_capacity(dd->disk, capacity);
3956
3957 /* Enable the block device and add it to /dev */
3958 add_disk(dd->disk);
3959
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003960 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003961 /*
3962 * Now that the disk is active, initialize any sysfs attributes
3963 * managed by the protocol layer.
3964 */
3965 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3966 if (kobj) {
3967 mtip_hw_sysfs_init(dd, kobj);
3968 kobject_put(kobj);
3969 }
3970
Asai Thambi S P45038362012-04-09 08:35:38 +02003971 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003972 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003973 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003974 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003975
3976start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003977 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01003978 dd, dd->numa_node,
3979 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003980
3981 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003982 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003983 dd->mtip_svc_handler = NULL;
3984 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003985 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003986 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003987 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003988 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3989 rv = wait_for_rebuild;
3990
Sam Bradshaw88523a62011-08-30 08:34:26 -06003991 return rv;
3992
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003993kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003994 bdput(dd->bdev);
3995 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003996
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003997 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003998 del_gendisk(dd->disk);
3999
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004000read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004001init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004002 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004003 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004004block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004005 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004006disk_index_error:
4007 spin_lock(&rssd_index_lock);
4008 ida_remove(&rssd_index_ida, index);
4009 spin_unlock(&rssd_index_lock);
4010
4011ida_get_error:
4012 put_disk(dd->disk);
4013
4014alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004015 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004016
4017protocol_init_error:
4018 return rv;
4019}
4020
4021/*
4022 * Block layer deinitialization function.
4023 *
4024 * Called by the PCI layer as each P320 device is removed.
4025 *
4026 * @dd Pointer to the driver data structure.
4027 *
4028 * return value
4029 * 0
4030 */
Jens Axboe63166682011-09-27 21:27:43 -06004031static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004032{
4033 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004034
Asai Thambi SP2132a542015-05-11 15:53:18 -07004035 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004036
Asai Thambi SP2132a542015-05-11 15:53:18 -07004037 if (dd->mtip_svc_handler) {
4038 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4039 wake_up_interruptible(&dd->port->svc_wait);
4040 kthread_stop(dd->mtip_svc_handler);
4041 }
4042
4043 /* Clean up the sysfs attributes, if created */
4044 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4045 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4046 if (kobj) {
4047 mtip_hw_sysfs_exit(dd, kobj);
4048 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004049 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004050 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004051
Asai Thambi SP2132a542015-05-11 15:53:18 -07004052 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004053 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004054 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004055 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4056 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004057
4058 /*
4059 * Delete our gendisk structure. This also removes the device
4060 * from /dev
4061 */
4062 if (dd->bdev) {
4063 bdput(dd->bdev);
4064 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004065 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004066 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004067 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4068 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004069 if (dd->disk->queue) {
4070 blk_cleanup_queue(dd->queue);
4071 blk_mq_free_tag_set(&dd->tags);
4072 dd->queue = NULL;
4073 }
4074 put_disk(dd->disk);
4075 }
4076 dd->disk = NULL;
4077
4078 spin_lock(&rssd_index_lock);
4079 ida_remove(&rssd_index_ida, dd->index);
4080 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004081
4082 /* De-initialize the protocol layer. */
4083 mtip_hw_exit(dd);
4084
4085 return 0;
4086}
4087
4088/*
4089 * Function called by the PCI layer when just before the
4090 * machine shuts down.
4091 *
4092 * If a protocol layer shutdown function is present it will be called
4093 * by this function.
4094 *
4095 * @dd Pointer to the driver data structure.
4096 *
4097 * return value
4098 * 0
4099 */
Jens Axboe63166682011-09-27 21:27:43 -06004100static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004101{
Jens Axboeffc771b2014-05-09 09:42:02 -06004102 mtip_hw_shutdown(dd);
4103
Sam Bradshaw88523a62011-08-30 08:34:26 -06004104 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304105 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304106 dev_info(&dd->pdev->dev,
4107 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304108
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004109 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4110 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304111 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304112 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004113 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004114 }
4115 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304116 dd->disk = NULL;
4117 dd->queue = NULL;
4118 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004119
4120 spin_lock(&rssd_index_lock);
4121 ida_remove(&rssd_index_ida, dd->index);
4122 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004123 return 0;
4124}
4125
Jens Axboe63166682011-09-27 21:27:43 -06004126static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004127{
4128 dev_info(&dd->pdev->dev,
4129 "Suspending %s ...\n", dd->disk->disk_name);
4130 mtip_hw_suspend(dd);
4131 return 0;
4132}
4133
Jens Axboe63166682011-09-27 21:27:43 -06004134static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004135{
4136 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4137 dd->disk->disk_name);
4138 mtip_hw_resume(dd);
4139 return 0;
4140}
4141
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004142static void drop_cpu(int cpu)
4143{
4144 cpu_use[cpu]--;
4145}
4146
4147static int get_least_used_cpu_on_node(int node)
4148{
4149 int cpu, least_used_cpu, least_cnt;
4150 const struct cpumask *node_mask;
4151
4152 node_mask = cpumask_of_node(node);
4153 least_used_cpu = cpumask_first(node_mask);
4154 least_cnt = cpu_use[least_used_cpu];
4155 cpu = least_used_cpu;
4156
4157 for_each_cpu(cpu, node_mask) {
4158 if (cpu_use[cpu] < least_cnt) {
4159 least_used_cpu = cpu;
4160 least_cnt = cpu_use[cpu];
4161 }
4162 }
4163 cpu_use[least_used_cpu]++;
4164 return least_used_cpu;
4165}
4166
4167/* Helper for selecting a node in round robin mode */
4168static inline int mtip_get_next_rr_node(void)
4169{
4170 static int next_node = -1;
4171
4172 if (next_node == -1) {
4173 next_node = first_online_node;
4174 return next_node;
4175 }
4176
4177 next_node = next_online_node(next_node);
4178 if (next_node == MAX_NUMNODES)
4179 next_node = first_online_node;
4180 return next_node;
4181}
4182
Fengguang Wu25bac122013-01-12 15:31:40 +08004183static DEFINE_HANDLER(0);
4184static DEFINE_HANDLER(1);
4185static DEFINE_HANDLER(2);
4186static DEFINE_HANDLER(3);
4187static DEFINE_HANDLER(4);
4188static DEFINE_HANDLER(5);
4189static DEFINE_HANDLER(6);
4190static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004191
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004192static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4193{
4194 int pos;
4195 unsigned short pcie_dev_ctrl;
4196
4197 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4198 if (pos) {
4199 pci_read_config_word(pdev,
4200 pos + PCI_EXP_DEVCTL,
4201 &pcie_dev_ctrl);
4202 if (pcie_dev_ctrl & (1 << 11) ||
4203 pcie_dev_ctrl & (1 << 4)) {
4204 dev_info(&dd->pdev->dev,
4205 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4206 pdev->vendor, pdev->device);
4207 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4208 PCI_EXP_DEVCTL_RELAX_EN);
4209 pci_write_config_word(pdev,
4210 pos + PCI_EXP_DEVCTL,
4211 pcie_dev_ctrl);
4212 }
4213 }
4214}
4215
4216static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4217{
4218 /*
4219 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4220 * device with device id 0x5aXX
4221 */
4222 if (pdev->bus && pdev->bus->self) {
4223 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4224 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4225 mtip_disable_link_opts(dd, pdev->bus->self);
4226 } else {
4227 /* Check further up the topology */
4228 struct pci_dev *parent_dev = pdev->bus->self;
4229 if (parent_dev->bus &&
4230 parent_dev->bus->parent &&
4231 parent_dev->bus->parent->self &&
4232 parent_dev->bus->parent->self->vendor ==
4233 PCI_VENDOR_ID_ATI &&
4234 (parent_dev->bus->parent->self->device &
4235 0xff00) == 0x5a00) {
4236 mtip_disable_link_opts(dd,
4237 parent_dev->bus->parent->self);
4238 }
4239 }
4240 }
4241}
4242
Sam Bradshaw88523a62011-08-30 08:34:26 -06004243/*
4244 * Called for each supported PCI device detected.
4245 *
4246 * This function allocates the private data structure, enables the
4247 * PCI device and then calls the block layer initialization function.
4248 *
4249 * return value
4250 * 0 on success else an error code.
4251 */
4252static int mtip_pci_probe(struct pci_dev *pdev,
4253 const struct pci_device_id *ent)
4254{
4255 int rv = 0;
4256 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004257 char cpu_list[256];
4258 const struct cpumask *node_mask;
4259 int cpu, i = 0, j = 0;
4260 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304261 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004262
4263 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004264 my_node = pcibus_to_node(pdev->bus);
4265 if (my_node != NUMA_NO_NODE) {
4266 if (!node_online(my_node))
4267 my_node = mtip_get_next_rr_node();
4268 } else {
4269 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4270 my_node = mtip_get_next_rr_node();
4271 }
4272 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4273 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004274 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004275
4276 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004277 if (dd == NULL) {
4278 dev_err(&pdev->dev,
4279 "Unable to allocate memory for driver data\n");
4280 return -ENOMEM;
4281 }
4282
Sam Bradshaw88523a62011-08-30 08:34:26 -06004283 /* Attach the private data to this PCI device. */
4284 pci_set_drvdata(pdev, dd);
4285
4286 rv = pcim_enable_device(pdev);
4287 if (rv < 0) {
4288 dev_err(&pdev->dev, "Unable to enable device\n");
4289 goto iomap_err;
4290 }
4291
4292 /* Map BAR5 to memory. */
4293 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4294 if (rv < 0) {
4295 dev_err(&pdev->dev, "Unable to map regions\n");
4296 goto iomap_err;
4297 }
4298
4299 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4300 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4301
4302 if (rv) {
4303 rv = pci_set_consistent_dma_mask(pdev,
4304 DMA_BIT_MASK(32));
4305 if (rv) {
4306 dev_warn(&pdev->dev,
4307 "64-bit DMA enable failed\n");
4308 goto setmask_err;
4309 }
4310 }
4311 }
4312
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004313 /* Copy the info we may need later into the private data structure. */
4314 dd->major = mtip_major;
4315 dd->instance = instance;
4316 dd->pdev = pdev;
4317 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004318
Asai Thambi S P0caff002013-04-03 19:56:21 +05304319 INIT_LIST_HEAD(&dd->online_list);
4320 INIT_LIST_HEAD(&dd->remove_list);
4321
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004322 memset(dd->workq_name, 0, 32);
4323 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4324
4325 dd->isr_workq = create_workqueue(dd->workq_name);
4326 if (!dd->isr_workq) {
4327 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004328 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004329 goto block_initialize_err;
4330 }
4331
4332 memset(cpu_list, 0, sizeof(cpu_list));
4333
4334 node_mask = cpumask_of_node(dd->numa_node);
4335 if (!cpumask_empty(node_mask)) {
4336 for_each_cpu(cpu, node_mask)
4337 {
4338 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4339 j = strlen(cpu_list);
4340 }
4341
4342 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4343 dd->numa_node,
4344 topology_physical_package_id(cpumask_first(node_mask)),
4345 nr_cpus_node(dd->numa_node),
4346 cpu_list);
4347 } else
4348 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4349
4350 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4351 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4352 cpu_to_node(dd->isr_binding), dd->isr_binding);
4353
4354 /* first worker context always runs in ISR */
4355 dd->work[0].cpu_binding = dd->isr_binding;
4356 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4357 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4358 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4359 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4360 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4361 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4362 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4363
4364 /* Log the bindings */
4365 for_each_present_cpu(cpu) {
4366 memset(cpu_list, 0, sizeof(cpu_list));
4367 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4368 if (dd->work[i].cpu_binding == cpu) {
4369 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4370 j = strlen(cpu_list);
4371 }
4372 }
4373 if (j)
4374 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4375 }
4376
4377 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4378 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4379 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4380 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4381 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4382 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4383 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4384 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4385
4386 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004387 rv = pci_enable_msi(pdev);
4388 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004389 dev_warn(&pdev->dev,
4390 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004391 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004392 }
4393
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004394 mtip_fix_ero_nosnoop(dd, pdev);
4395
Sam Bradshaw88523a62011-08-30 08:34:26 -06004396 /* Initialize the block layer. */
4397 rv = mtip_block_initialize(dd);
4398 if (rv < 0) {
4399 dev_err(&pdev->dev,
4400 "Unable to initialize block layer\n");
4401 goto block_initialize_err;
4402 }
4403
4404 /*
4405 * Increment the instance count so that each device has a unique
4406 * instance number.
4407 */
4408 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004409 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004410 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304411 else
4412 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304413
4414 /* Add to online list even if in ftl rebuild */
4415 spin_lock_irqsave(&dev_lock, flags);
4416 list_add(&dd->online_list, &online_list);
4417 spin_unlock_irqrestore(&dev_lock, flags);
4418
Sam Bradshaw88523a62011-08-30 08:34:26 -06004419 goto done;
4420
4421block_initialize_err:
4422 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004423
4424msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004425 if (dd->isr_workq) {
4426 flush_workqueue(dd->isr_workq);
4427 destroy_workqueue(dd->isr_workq);
4428 drop_cpu(dd->work[0].cpu_binding);
4429 drop_cpu(dd->work[1].cpu_binding);
4430 drop_cpu(dd->work[2].cpu_binding);
4431 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004432setmask_err:
4433 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4434
4435iomap_err:
4436 kfree(dd);
4437 pci_set_drvdata(pdev, NULL);
4438 return rv;
4439done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004440 return rv;
4441}
4442
4443/*
4444 * Called for each probed device when the device is removed or the
4445 * driver is unloaded.
4446 *
4447 * return value
4448 * None
4449 */
4450static void mtip_pci_remove(struct pci_dev *pdev)
4451{
4452 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004453 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004454
Asai Thambi SP51c65702016-02-24 21:18:10 -08004455 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004456
Asai Thambi S P0caff002013-04-03 19:56:21 +05304457 spin_lock_irqsave(&dev_lock, flags);
4458 list_del_init(&dd->online_list);
4459 list_add(&dd->remove_list, &removing_list);
4460 spin_unlock_irqrestore(&dev_lock, flags);
4461
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004462 mtip_check_surprise_removal(pdev);
4463 synchronize_irq(dd->pdev->irq);
4464
4465 /* Spin until workers are done */
4466 to = jiffies + msecs_to_jiffies(4000);
4467 do {
4468 msleep(20);
4469 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4470 time_before(jiffies, to));
4471
Asai Thambi SP51c65702016-02-24 21:18:10 -08004472 fsync_bdev(dd->bdev);
4473
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004474 if (atomic_read(&dd->irq_workers_active) != 0) {
4475 dev_warn(&dd->pdev->dev,
4476 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004477 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004478
Asai Thambi SP51c65702016-02-24 21:18:10 -08004479 if (dd->sr)
4480 blk_mq_stop_hw_queues(dd->queue);
4481
4482 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4483
Sam Bradshaw88523a62011-08-30 08:34:26 -06004484 /* Clean up the block layer. */
4485 mtip_block_remove(dd);
4486
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004487 if (dd->isr_workq) {
4488 flush_workqueue(dd->isr_workq);
4489 destroy_workqueue(dd->isr_workq);
4490 drop_cpu(dd->work[0].cpu_binding);
4491 drop_cpu(dd->work[1].cpu_binding);
4492 drop_cpu(dd->work[2].cpu_binding);
4493 }
4494
Sam Bradshaw88523a62011-08-30 08:34:26 -06004495 pci_disable_msi(pdev);
4496
Asai Thambi S P0caff002013-04-03 19:56:21 +05304497 spin_lock_irqsave(&dev_lock, flags);
4498 list_del_init(&dd->remove_list);
4499 spin_unlock_irqrestore(&dev_lock, flags);
4500
Asai Thambi SP2132a542015-05-11 15:53:18 -07004501 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004502
Sam Bradshaw88523a62011-08-30 08:34:26 -06004503 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004504 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004505}
4506
4507/*
4508 * Called for each probed device when the device is suspended.
4509 *
4510 * return value
4511 * 0 Success
4512 * <0 Error
4513 */
4514static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4515{
4516 int rv = 0;
4517 struct driver_data *dd = pci_get_drvdata(pdev);
4518
4519 if (!dd) {
4520 dev_err(&pdev->dev,
4521 "Driver private datastructure is NULL\n");
4522 return -EFAULT;
4523 }
4524
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004525 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004526
4527 /* Disable ports & interrupts then send standby immediate */
4528 rv = mtip_block_suspend(dd);
4529 if (rv < 0) {
4530 dev_err(&pdev->dev,
4531 "Failed to suspend controller\n");
4532 return rv;
4533 }
4534
4535 /*
4536 * Save the pci config space to pdev structure &
4537 * disable the device
4538 */
4539 pci_save_state(pdev);
4540 pci_disable_device(pdev);
4541
4542 /* Move to Low power state*/
4543 pci_set_power_state(pdev, PCI_D3hot);
4544
4545 return rv;
4546}
4547
4548/*
4549 * Called for each probed device when the device is resumed.
4550 *
4551 * return value
4552 * 0 Success
4553 * <0 Error
4554 */
4555static int mtip_pci_resume(struct pci_dev *pdev)
4556{
4557 int rv = 0;
4558 struct driver_data *dd;
4559
4560 dd = pci_get_drvdata(pdev);
4561 if (!dd) {
4562 dev_err(&pdev->dev,
4563 "Driver private datastructure is NULL\n");
4564 return -EFAULT;
4565 }
4566
4567 /* Move the device to active State */
4568 pci_set_power_state(pdev, PCI_D0);
4569
4570 /* Restore PCI configuration space */
4571 pci_restore_state(pdev);
4572
4573 /* Enable the PCI device*/
4574 rv = pcim_enable_device(pdev);
4575 if (rv < 0) {
4576 dev_err(&pdev->dev,
4577 "Failed to enable card during resume\n");
4578 goto err;
4579 }
4580 pci_set_master(pdev);
4581
4582 /*
4583 * Calls hbaReset, initPort, & startPort function
4584 * then enables interrupts
4585 */
4586 rv = mtip_block_resume(dd);
4587 if (rv < 0)
4588 dev_err(&pdev->dev, "Unable to resume\n");
4589
4590err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004591 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004592
4593 return rv;
4594}
4595
4596/*
4597 * Shutdown routine
4598 *
4599 * return value
4600 * None
4601 */
4602static void mtip_pci_shutdown(struct pci_dev *pdev)
4603{
4604 struct driver_data *dd = pci_get_drvdata(pdev);
4605 if (dd)
4606 mtip_block_shutdown(dd);
4607}
4608
Sam Bradshaw88523a62011-08-30 08:34:26 -06004609/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004610static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304611 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4612 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4613 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4614 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4615 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4616 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4617 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004618 { 0 }
4619};
4620
4621/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004622static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004623 .name = MTIP_DRV_NAME,
4624 .id_table = mtip_pci_tbl,
4625 .probe = mtip_pci_probe,
4626 .remove = mtip_pci_remove,
4627 .suspend = mtip_pci_suspend,
4628 .resume = mtip_pci_resume,
4629 .shutdown = mtip_pci_shutdown,
4630};
4631
4632MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4633
4634/*
4635 * Module initialization function.
4636 *
4637 * Called once when the module is loaded. This function allocates a major
4638 * block device number to the Cyclone devices and registers the PCI layer
4639 * of the driver.
4640 *
4641 * Return value
4642 * 0 on success else error code.
4643 */
4644static int __init mtip_init(void)
4645{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004646 int error;
4647
Asai Thambi S P45422e72012-09-05 22:03:56 +05304648 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004649
Asai Thambi S P0caff002013-04-03 19:56:21 +05304650 spin_lock_init(&dev_lock);
4651
4652 INIT_LIST_HEAD(&online_list);
4653 INIT_LIST_HEAD(&removing_list);
4654
Sam Bradshaw88523a62011-08-30 08:34:26 -06004655 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004656 error = register_blkdev(0, MTIP_DRV_NAME);
4657 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304658 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004659 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004660 return -EBUSY;
4661 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004662 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004663
Asai Thambi S P0caff002013-04-03 19:56:21 +05304664 dfs_parent = debugfs_create_dir("rssd", NULL);
4665 if (IS_ERR_OR_NULL(dfs_parent)) {
4666 pr_warn("Error creating debugfs parent\n");
4667 dfs_parent = NULL;
4668 }
4669 if (dfs_parent) {
4670 dfs_device_status = debugfs_create_file("device_status",
4671 S_IRUGO, dfs_parent, NULL,
4672 &mtip_device_status_fops);
4673 if (IS_ERR_OR_NULL(dfs_device_status)) {
4674 pr_err("Error creating device_status node\n");
4675 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004676 }
4677 }
4678
Sam Bradshaw88523a62011-08-30 08:34:26 -06004679 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004680 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004681 if (error) {
4682 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004683 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004684 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004685
4686 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004687}
4688
4689/*
4690 * Module de-initialization function.
4691 *
4692 * Called once when the module is unloaded. This function deallocates
4693 * the major block device number allocated by mtip_init() and
4694 * unregisters the PCI layer of the driver.
4695 *
4696 * Return value
4697 * none
4698 */
4699static void __exit mtip_exit(void)
4700{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004701 /* Release the allocated major block device number. */
4702 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4703
4704 /* Unregister the PCI driver. */
4705 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004706
4707 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004708}
4709
4710MODULE_AUTHOR("Micron Technology, Inc");
4711MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4712MODULE_LICENSE("GPL");
4713MODULE_VERSION(MTIP_DRV_VERSION);
4714
4715module_init(mtip_init);
4716module_exit(mtip_exit);