blob: 92cb60194ed2a2d2819c658e76d588c9c298468b [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
Asai Thambi S P0caff002013-04-03 19:56:21 +0530107struct list_head online_list;
108struct list_head removing_list;
109spinlock_t dev_lock;
110
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__);
166 if (dd->port) {
167 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
168 wake_up_interruptible(&dd->port->svc_wait);
169 } else
170 dev_warn(&dd->pdev->dev,
171 "%s: dd->port is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600172 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600173 }
174
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600175 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600176}
177
Jens Axboeffc771b2014-05-09 09:42:02 -0600178static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600179{
Jens Axboeffc771b2014-05-09 09:42:02 -0600180 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600181
Jens Axboe61789762014-05-28 09:50:26 -0600182 rq = blk_mq_alloc_request(dd->queue, 0, __GFP_WAIT, true);
Jens Axboeffc771b2014-05-09 09:42:02 -0600183 return blk_mq_rq_to_pdu(rq);
184}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600185
Jens Axboeffc771b2014-05-09 09:42:02 -0600186static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
187{
188 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600189}
190
191/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600192 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600193 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600194static struct request *mtip_rq_from_tag(struct driver_data *dd,
195 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600196{
Jens Axboe0e62f512014-06-04 10:23:49 -0600197 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
198
199 return blk_mq_tag_to_rq(hctx->tags, tag);
Jens Axboeffc771b2014-05-09 09:42:02 -0600200}
201
202static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
203 unsigned int tag)
204{
205 struct request *rq = mtip_rq_from_tag(dd, tag);
206
207 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600208}
209
210/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600211 * IO completion function.
212 *
213 * This completion function is called by the driver ISR when a
214 * command that was issued by the kernel completes. It first calls the
215 * asynchronous completion function which normally calls back into the block
216 * layer passing the asynchronous callback data, then unmaps the
217 * scatter list associated with the completed command, and finally
218 * clears the allocated bit associated with the completed command.
219 *
220 * @port Pointer to the port data structure.
221 * @tag Tag of the command.
222 * @data Pointer to driver_data.
223 * @status Completion status.
224 *
225 * return value
226 * None
227 */
228static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600229 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600230{
Jens Axboeffc771b2014-05-09 09:42:02 -0600231 struct driver_data *dd = port->dd;
232 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600233
234 if (unlikely(!dd) || unlikely(!port))
235 return;
236
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600237 if (unlikely(status == PORT_IRQ_TF_ERR)) {
238 dev_warn(&port->dd->pdev->dev,
239 "Command tag %d failed due to TFE\n", tag);
240 }
241
Jens Axboeffc771b2014-05-09 09:42:02 -0600242 /* Unmap the DMA scatter list entries */
243 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600244
Jens Axboeffc771b2014-05-09 09:42:02 -0600245 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600246
Jens Axboeffc771b2014-05-09 09:42:02 -0600247 if (unlikely(cmd->unaligned))
248 up(&port->cmd_slot_unal);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600249
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700250 blk_mq_end_request(rq, status ? -EIO : 0);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600251}
252
253/*
Jens Axboe63166682011-09-27 21:27:43 -0600254 * Reset the HBA (without sleeping)
255 *
Jens Axboe63166682011-09-27 21:27:43 -0600256 * @dd Pointer to the driver data structure.
257 *
258 * return value
259 * 0 The reset was successful.
260 * -1 The HBA Reset bit did not clear.
261 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530262static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600263{
264 unsigned long timeout;
265
Jens Axboe63166682011-09-27 21:27:43 -0600266 /* Set the reset bit */
267 writel(HOST_RESET, dd->mmio + HOST_CTL);
268
269 /* Flush */
270 readl(dd->mmio + HOST_CTL);
271
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530272 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
273 timeout = jiffies + msecs_to_jiffies(2000);
274 do {
275 mdelay(10);
276 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
277 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600278
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530279 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
280 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200281
Jens Axboe63166682011-09-27 21:27:43 -0600282 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
283 return -1;
284
285 return 0;
286}
287
288/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600289 * Issue a command to the hardware.
290 *
291 * Set the appropriate bit in the s_active and Command Issue hardware
292 * registers, causing hardware command processing to begin.
293 *
294 * @port Pointer to the port structure.
295 * @tag The tag of the command to be issued.
296 *
297 * return value
298 * None
299 */
300static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
301{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800302 int group = tag >> 5;
303
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800304 /* guard SACT and CI registers */
305 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600306 writel((1 << MTIP_TAG_BIT(tag)),
307 port->s_active[MTIP_TAG_INDEX(tag)]);
308 writel((1 << MTIP_TAG_BIT(tag)),
309 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800310 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600311}
312
313/*
Jens Axboe63166682011-09-27 21:27:43 -0600314 * Enable/disable the reception of FIS
315 *
316 * @port Pointer to the port data structure
317 * @enable 1 to enable, 0 to disable
318 *
319 * return value
320 * Previous state: 1 enabled, 0 disabled
321 */
322static int mtip_enable_fis(struct mtip_port *port, int enable)
323{
324 u32 tmp;
325
326 /* enable FIS reception */
327 tmp = readl(port->mmio + PORT_CMD);
328 if (enable)
329 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
330 else
331 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332
333 /* Flush */
334 readl(port->mmio + PORT_CMD);
335
336 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
337}
338
339/*
340 * Enable/disable the DMA engine
341 *
342 * @port Pointer to the port data structure
343 * @enable 1 to enable, 0 to disable
344 *
345 * return value
346 * Previous state: 1 enabled, 0 disabled.
347 */
348static int mtip_enable_engine(struct mtip_port *port, int enable)
349{
350 u32 tmp;
351
352 /* enable FIS reception */
353 tmp = readl(port->mmio + PORT_CMD);
354 if (enable)
355 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
356 else
357 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
358
359 readl(port->mmio + PORT_CMD);
360 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
361}
362
363/*
364 * Enables the port DMA engine and FIS reception.
365 *
366 * return value
367 * None
368 */
369static inline void mtip_start_port(struct mtip_port *port)
370{
371 /* Enable FIS reception */
372 mtip_enable_fis(port, 1);
373
374 /* Enable the DMA engine */
375 mtip_enable_engine(port, 1);
376}
377
378/*
379 * Deinitialize a port by disabling port interrupts, the DMA engine,
380 * and FIS reception.
381 *
382 * @port Pointer to the port structure
383 *
384 * return value
385 * None
386 */
387static inline void mtip_deinit_port(struct mtip_port *port)
388{
389 /* Disable interrupts on this port */
390 writel(0, port->mmio + PORT_IRQ_MASK);
391
392 /* Disable the DMA engine */
393 mtip_enable_engine(port, 0);
394
395 /* Disable FIS reception */
396 mtip_enable_fis(port, 0);
397}
398
399/*
400 * Initialize a port.
401 *
402 * This function deinitializes the port by calling mtip_deinit_port() and
403 * then initializes it by setting the command header and RX FIS addresses,
404 * clearing the SError register and any pending port interrupts before
405 * re-enabling the default set of port interrupts.
406 *
407 * @port Pointer to the port structure.
408 *
409 * return value
410 * None
411 */
412static void mtip_init_port(struct mtip_port *port)
413{
414 int i;
415 mtip_deinit_port(port);
416
417 /* Program the command list base and FIS base addresses */
418 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
419 writel((port->command_list_dma >> 16) >> 16,
420 port->mmio + PORT_LST_ADDR_HI);
421 writel((port->rxfis_dma >> 16) >> 16,
422 port->mmio + PORT_FIS_ADDR_HI);
423 }
424
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100425 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600426 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600428
429 /* Clear SError */
430 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
431
432 /* reset the completed registers.*/
433 for (i = 0; i < port->dd->slot_groups; i++)
434 writel(0xFFFFFFFF, port->completed[i]);
435
436 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700437 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600438
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100439 /* Clear any pending interrupts on the HBA. */
440 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
441 port->dd->mmio + HOST_IRQ_STAT);
442
Jens Axboe63166682011-09-27 21:27:43 -0600443 /* Enable port interrupts */
444 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
445}
446
447/*
448 * Restart a port
449 *
450 * @port Pointer to the port data structure.
451 *
452 * return value
453 * None
454 */
455static void mtip_restart_port(struct mtip_port *port)
456{
457 unsigned long timeout;
458
459 /* Disable the DMA engine */
460 mtip_enable_engine(port, 0);
461
462 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
463 timeout = jiffies + msecs_to_jiffies(500);
464 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
465 && time_before(jiffies, timeout))
466 ;
467
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200468 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200469 return;
470
Jens Axboe63166682011-09-27 21:27:43 -0600471 /*
472 * Chip quirk: escalate to hba reset if
473 * PxCMD.CR not clear after 500 ms
474 */
475 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
476 dev_warn(&port->dd->pdev->dev,
477 "PxCMD.CR not clear, escalating reset\n");
478
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530479 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600480 dev_err(&port->dd->pdev->dev,
481 "HBA reset escalation failed.\n");
482
483 /* 30 ms delay before com reset to quiesce chip */
484 mdelay(30);
485 }
486
487 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
488
489 /* Set PxSCTL.DET */
490 writel(readl(port->mmio + PORT_SCR_CTL) |
491 1, port->mmio + PORT_SCR_CTL);
492 readl(port->mmio + PORT_SCR_CTL);
493
494 /* Wait 1 ms to quiesce chip function */
495 timeout = jiffies + msecs_to_jiffies(1);
496 while (time_before(jiffies, timeout))
497 ;
498
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200499 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200500 return;
501
Jens Axboe63166682011-09-27 21:27:43 -0600502 /* Clear PxSCTL.DET */
503 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
504 port->mmio + PORT_SCR_CTL);
505 readl(port->mmio + PORT_SCR_CTL);
506
507 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
508 timeout = jiffies + msecs_to_jiffies(500);
509 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
510 && time_before(jiffies, timeout))
511 ;
512
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200513 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200514 return;
515
Jens Axboe63166682011-09-27 21:27:43 -0600516 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
517 dev_warn(&port->dd->pdev->dev,
518 "COM reset failed\n");
519
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100520 mtip_init_port(port);
521 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600522
Jens Axboe63166682011-09-27 21:27:43 -0600523}
524
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530525static int mtip_device_reset(struct driver_data *dd)
526{
527 int rv = 0;
528
529 if (mtip_check_surprise_removal(dd->pdev))
530 return 0;
531
532 if (mtip_hba_reset(dd) < 0)
533 rv = -EFAULT;
534
535 mdelay(1);
536 mtip_init_port(dd->port);
537 mtip_start_port(dd->port);
538
539 /* Enable interrupts on the HBA. */
540 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
541 dd->mmio + HOST_CTL);
542 return rv;
543}
544
Jens Axboe63166682011-09-27 21:27:43 -0600545/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200546 * Helper function for tag logging
547 */
548static void print_tags(struct driver_data *dd,
549 char *msg,
550 unsigned long *tagbits,
551 int cnt)
552{
553 unsigned char tagmap[128];
554 int group, tagmap_len = 0;
555
556 memset(tagmap, 0, sizeof(tagmap));
557 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600558 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200559 tagbits[group-1]);
560 dev_warn(&dd->pdev->dev,
561 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
562}
563
564/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600565 * Internal command completion callback function.
566 *
567 * This function is normally called by the driver ISR when an internal
568 * command completed. This function signals the command completion by
569 * calling complete().
570 *
571 * @port Pointer to the port data structure.
572 * @tag Tag of the command that has completed.
573 * @data Pointer to a completion structure.
574 * @status Completion status.
575 *
576 * return value
577 * None
578 */
579static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600580 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600581{
Jens Axboeffc771b2014-05-09 09:42:02 -0600582 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600583 if (unlikely(status == PORT_IRQ_TF_ERR))
584 dev_warn(&port->dd->pdev->dev,
585 "Internal command %d completed with TFE\n", tag);
586
Sam Bradshaw88523a62011-08-30 08:34:26 -0600587 complete(waiting);
588}
589
Asai Thambi S P8182b492012-04-09 08:35:38 +0200590static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600591 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200592{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200593}
594
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200595static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
596 dma_addr_t buffer_dma, unsigned int sectors);
597static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
598 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600599/*
600 * Handle an error.
601 *
602 * @dd Pointer to the DRIVER_DATA structure.
603 *
604 * return value
605 * None
606 */
607static void mtip_handle_tfe(struct driver_data *dd)
608{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200609 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600610 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200611 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600612 u32 completed;
613 struct host_to_dev_fis *fis;
614 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200615 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200616 unsigned char *buf;
617 char *fail_reason = NULL;
618 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600619
620 dev_warn(&dd->pdev->dev, "Taskfile error\n");
621
622 port = dd->port;
623
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700624 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
625
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700626 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600627 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700628 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
629
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700630 if (cmd->comp_data && cmd->comp_func) {
631 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600632 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700633 }
634 goto handle_tfe_exit;
635 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600636
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200637 /* clear the tag accumulator */
638 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
639
Sam Bradshaw88523a62011-08-30 08:34:26 -0600640 /* Loop through all the groups */
641 for (group = 0; group < dd->slot_groups; group++) {
642 completed = readl(port->completed[group]);
643
Jens Axboeffc771b2014-05-09 09:42:02 -0600644 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
645
Sam Bradshaw88523a62011-08-30 08:34:26 -0600646 /* clear completed status register in the hardware.*/
647 writel(completed, port->completed[group]);
648
Sam Bradshaw88523a62011-08-30 08:34:26 -0600649 /* Process successfully completed commands */
650 for (bit = 0; bit < 32 && completed; bit++) {
651 if (!(completed & (1<<bit)))
652 continue;
653 tag = (group << 5) + bit;
654
655 /* Skip the internal command slot */
656 if (tag == MTIP_TAG_INTERNAL)
657 continue;
658
Jens Axboeffc771b2014-05-09 09:42:02 -0600659 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200660 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600661 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200662 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600663 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 } else {
665 dev_err(&port->dd->pdev->dev,
666 "Missing completion func for tag %d",
667 tag);
668 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600669 /* don't proceed further */
670 return;
671 }
672 }
673 }
674 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200675
676 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600677
678 /* Restart the port */
679 mdelay(20);
680 mtip_restart_port(port);
681
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200682 /* Trying to determine the cause of the error */
683 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
684 dd->port->log_buf,
685 dd->port->log_buf_dma, 1);
686 if (rv) {
687 dev_warn(&dd->pdev->dev,
688 "Error in READ LOG EXT (10h) command\n");
689 /* non-critical error, don't fail the load */
690 } else {
691 buf = (unsigned char *)dd->port->log_buf;
692 if (buf[259] & 0x1) {
693 dev_info(&dd->pdev->dev,
694 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200695 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200696 fail_all_ncq_write = 1;
697 fail_reason = "write protect";
698 }
699 if (buf[288] == 0xF7) {
700 dev_info(&dd->pdev->dev,
701 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200702 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200703 fail_all_ncq_cmds = 1;
704 fail_reason = "thermal shutdown";
705 }
706 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700707 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200708 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700709 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200710 fail_all_ncq_cmds = 1;
711 fail_reason = "rebuild failed";
712 }
713 }
714
Sam Bradshaw88523a62011-08-30 08:34:26 -0600715 /* clear the tag accumulator */
716 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
717
718 /* Loop through all the groups */
719 for (group = 0; group < dd->slot_groups; group++) {
720 for (bit = 0; bit < 32; bit++) {
721 reissue = 1;
722 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600723 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600724
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200725 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600726
727 /* Should re-issue? */
728 if (tag == MTIP_TAG_INTERNAL ||
729 fis->command == ATA_CMD_SET_FEATURES)
730 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200731 else {
732 if (fail_all_ncq_cmds ||
733 (fail_all_ncq_write &&
734 fis->command == ATA_CMD_FPDMA_WRITE)) {
735 dev_warn(&dd->pdev->dev,
736 " Fail: %s w/tag %d [%s].\n",
737 fis->command == ATA_CMD_FPDMA_WRITE ?
738 "write" : "read",
739 tag,
740 fail_reason != NULL ?
741 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200742 if (cmd->comp_func) {
743 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600744 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200745 }
746 continue;
747 }
748 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600749
750 /*
751 * First check if this command has
752 * exceeded its retries.
753 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200754 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600755
756 set_bit(tag, tagaccum);
757
Sam Bradshaw88523a62011-08-30 08:34:26 -0600758 /* Re-issue the command. */
759 mtip_issue_ncq_command(port, tag);
760
761 continue;
762 }
763
764 /* Retire a command that will not be reissued */
765 dev_warn(&port->dd->pdev->dev,
766 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600767
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200768 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600769 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600770 else
771 dev_warn(&port->dd->pdev->dev,
772 "Bad completion for tag %d\n",
773 tag);
774 }
775 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200776 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600777
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700778handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100779 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200780 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100781 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600782}
783
784/*
785 * Handle a set device bits interrupt
786 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800787static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
788 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600789{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800790 struct driver_data *dd = port->dd;
791 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600792 struct mtip_cmd *command;
793
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800794 if (!completed) {
795 WARN_ON_ONCE(!completed);
796 return;
797 }
798 /* clear completed status register in the hardware.*/
799 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600800
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800801 /* Process completed commands. */
802 for (bit = 0; (bit < 32) && completed; bit++) {
803 if (completed & 0x01) {
804 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600805
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800806 /* skip internal command slot. */
807 if (unlikely(tag == MTIP_TAG_INTERNAL))
808 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600809
Jens Axboeffc771b2014-05-09 09:42:02 -0600810 command = mtip_cmd_from_tag(dd, tag);
811 if (likely(command->comp_func))
812 command->comp_func(port, tag, command, 0);
813 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600814 dev_dbg(&dd->pdev->dev,
815 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800816 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600817
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800818 if (mtip_check_surprise_removal(
819 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800820 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600821 }
822 }
823 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800824 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600825 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800826
827 /* If last, re-enable interrupts */
828 if (atomic_dec_return(&dd->irq_workers_active) == 0)
829 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600830}
831
832/*
833 * Process legacy pio and d2h interrupts
834 */
835static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
836{
837 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600838 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600839
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200840 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100841 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600842 & (1 << MTIP_TAG_INTERNAL))) {
843 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600844 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600845 return;
846 }
847 }
848
Sam Bradshaw88523a62011-08-30 08:34:26 -0600849 return;
850}
851
852/*
853 * Demux and handle errors
854 */
855static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
856{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600857
858 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
859 dev_warn(&dd->pdev->dev,
860 "Clearing PxSERR.DIAG.x\n");
861 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
862 }
863
864 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
865 dev_warn(&dd->pdev->dev,
866 "Clearing PxSERR.DIAG.n\n");
867 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
868 }
869
870 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
871 dev_warn(&dd->pdev->dev,
872 "Port stat errors %x unhandled\n",
873 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700874 if (mtip_check_surprise_removal(dd->pdev))
875 return;
876 }
877 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
878 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
879 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600880 }
881}
882
883static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
884{
885 struct driver_data *dd = (struct driver_data *) data;
886 struct mtip_port *port = dd->port;
887 u32 hba_stat, port_stat;
888 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800889 int do_irq_enable = 1, i, workers;
890 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600891
892 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
893 if (hba_stat) {
894 rv = IRQ_HANDLED;
895
896 /* Acknowledge the interrupt status on the port.*/
897 port_stat = readl(port->mmio + PORT_IRQ_STAT);
898 writel(port_stat, port->mmio + PORT_IRQ_STAT);
899
900 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800901 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
902 do_irq_enable = 0;
903 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
904
905 /* Start at 1: group zero is always local? */
906 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
907 i++) {
908 twork = &dd->work[i];
909 twork->completed = readl(port->completed[i]);
910 if (twork->completed)
911 workers++;
912 }
913
914 atomic_set(&dd->irq_workers_active, workers);
915 if (workers) {
916 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
917 twork = &dd->work[i];
918 if (twork->completed)
919 queue_work_on(
920 twork->cpu_binding,
921 dd->isr_workq,
922 &twork->work);
923 }
924
925 if (likely(dd->work[0].completed))
926 mtip_workq_sdbfx(port, 0,
927 dd->work[0].completed);
928
929 } else {
930 /*
931 * Chip quirk: SDB interrupt but nothing
932 * to complete
933 */
934 do_irq_enable = 1;
935 }
936 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600937
938 if (unlikely(port_stat & PORT_IRQ_ERR)) {
939 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600940 /* don't proceed further */
941 return IRQ_HANDLED;
942 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200943 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200944 &dd->dd_flag))
945 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600946
947 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
948 }
949
950 if (unlikely(port_stat & PORT_IRQ_LEGACY))
951 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
952 }
953
954 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800955 if (unlikely(do_irq_enable))
956 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600957
958 return rv;
959}
960
961/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600962 * HBA interrupt subroutine.
963 *
964 * @irq IRQ number.
965 * @instance Pointer to the driver data structure.
966 *
967 * return value
968 * IRQ_HANDLED A HBA interrupt was pending and handled.
969 * IRQ_NONE This interrupt was not for the HBA.
970 */
971static irqreturn_t mtip_irq_handler(int irq, void *instance)
972{
973 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800974
975 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600976}
977
978static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
979{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600980 writel(1 << MTIP_TAG_BIT(tag),
981 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
982}
983
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200984static bool mtip_pause_ncq(struct mtip_port *port,
985 struct host_to_dev_fis *fis)
986{
987 struct host_to_dev_fis *reply;
988 unsigned long task_file_data;
989
990 reply = port->rxfis + RX_FIS_D2H_REG;
991 task_file_data = readl(port->mmio+PORT_TFDATA);
992
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530993 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200994 return false;
995
996 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200997 port->ic_pause_timer = jiffies;
998 return true;
999 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1000 (fis->features == 0x03)) {
1001 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1002 port->ic_pause_timer = jiffies;
1003 return true;
1004 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1005 ((fis->command == 0xFC) &&
1006 (fis->features == 0x27 || fis->features == 0x72 ||
1007 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001008 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001009 /* Com reset after secure erase or lowlevel format */
1010 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001011 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001012 return false;
1013 }
1014
1015 return false;
1016}
1017
Sam Bradshaw88523a62011-08-30 08:34:26 -06001018/*
1019 * Wait for port to quiesce
1020 *
1021 * @port Pointer to port data structure
1022 * @timeout Max duration to wait (ms)
1023 *
1024 * return value
1025 * 0 Success
1026 * -EBUSY Commands still active
1027 */
1028static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1029{
1030 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001031 unsigned int n;
1032 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033
Jens Axboe9acf03c2014-05-14 08:22:56 -06001034 blk_mq_stop_hw_queues(port->dd->queue);
1035
Sam Bradshaw88523a62011-08-30 08:34:26 -06001036 to = jiffies + msecs_to_jiffies(timeout);
1037 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001038 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1039 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001040 msleep(20);
1041 continue; /* svc thd is actively issuing commands */
1042 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001043
1044 msleep(100);
1045 if (mtip_check_surprise_removal(port->dd->pdev))
1046 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001047 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001048 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001049
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050 /*
1051 * Ignore s_active bit 0 of array element 0.
1052 * This bit will always be set
1053 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001054 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001055 for (n = 1; n < port->dd->slot_groups; n++)
1056 active |= readl(port->s_active[n]);
1057
1058 if (!active)
1059 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001060 } while (time_before(jiffies, to));
1061
Jens Axboe9acf03c2014-05-14 08:22:56 -06001062 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001063 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001064err_fault:
1065 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1066 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001067}
1068
1069/*
1070 * Execute an internal command and wait for the completion.
1071 *
1072 * @port Pointer to the port data structure.
1073 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001074 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001075 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001076 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001077 * @opts Command header options, excluding the FIS length
1078 * and the number of PRD entries.
1079 * @timeout Time in ms to wait for the command to complete.
1080 *
1081 * return value
1082 * 0 Command completed successfully.
1083 * -EFAULT The buffer address is not correctly aligned.
1084 * -EBUSY Internal command or other IO in progress.
1085 * -EAGAIN Time out waiting for command to complete.
1086 */
1087static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001088 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001089 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001090 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001091 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001092 u32 opts,
1093 gfp_t atomic,
1094 unsigned long timeout)
1095{
1096 struct mtip_cmd_sg *command_sg;
1097 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001098 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301099 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001100 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001101
1102 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1103 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301104 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001105 return -EFAULT;
1106 }
1107
Jens Axboeffc771b2014-05-09 09:42:02 -06001108 int_cmd = mtip_get_int_command(dd);
1109
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001110 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001111
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001112 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1113 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1114
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301115 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001116
1117 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001118 if (fis->command != ATA_CMD_STANDBYNOW1) {
1119 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001120 if (mtip_quiesce_io(port,
1121 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301122 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001123 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001124 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001125 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001126 wake_up_interruptible(&port->svc_wait);
1127 return -EBUSY;
1128 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001129 }
1130
1131 /* Set the completion function and data for the command. */
1132 int_cmd->comp_data = &wait;
1133 int_cmd->comp_func = mtip_completion;
1134
1135 } else {
1136 /* Clear completion - we're going to poll */
1137 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001138 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001139 }
1140
1141 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001142 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001143
1144 /* Populate the SG list */
1145 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001146 __force_bit2int cpu_to_le32(opts | fis_len);
1147 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001148 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1149
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001150 command_sg->info =
1151 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1152 command_sg->dba =
1153 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1154 command_sg->dba_upper =
1155 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001156
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001157 int_cmd->command_header->opts |=
1158 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001159 }
1160
1161 /* Populate the command header */
1162 int_cmd->command_header->byte_count = 0;
1163
1164 /* Issue the command to the hardware */
1165 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1166
Sam Bradshaw88523a62011-08-30 08:34:26 -06001167 if (atomic == GFP_KERNEL) {
1168 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001169 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001170 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001171 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301172 if (rv == -ERESTARTSYS) { /* interrupted */
1173 dev_err(&dd->pdev->dev,
1174 "Internal command [%02X] was interrupted after %lu ms\n",
1175 fis->command, timeout);
1176 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) {
2031 outbuf = kzalloc(taskout, GFP_KERNEL);
2032 if (outbuf == NULL) {
2033 err = -ENOMEM;
2034 goto abort;
2035 }
2036 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2037 err = -EFAULT;
2038 goto abort;
2039 }
2040 outbuf_dma = pci_map_single(dd->pdev,
2041 outbuf,
2042 taskout,
2043 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002044 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002045 err = -ENOMEM;
2046 goto abort;
2047 }
2048 dma_buffer = outbuf_dma;
2049 }
2050
2051 if (taskin) {
2052 inbuf = kzalloc(taskin, GFP_KERNEL);
2053 if (inbuf == NULL) {
2054 err = -ENOMEM;
2055 goto abort;
2056 }
2057
2058 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2059 err = -EFAULT;
2060 goto abort;
2061 }
2062 inbuf_dma = pci_map_single(dd->pdev,
2063 inbuf,
2064 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002065 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002066 err = -ENOMEM;
2067 goto abort;
2068 }
2069 dma_buffer = inbuf_dma;
2070 }
2071
2072 /* only supports PIO and non-data commands from this ioctl. */
2073 switch (req_task->data_phase) {
2074 case TASKFILE_OUT:
2075 nsect = taskout / ATA_SECT_SIZE;
2076 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2077 break;
2078 case TASKFILE_IN:
2079 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2080 break;
2081 case TASKFILE_NO_DATA:
2082 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2083 break;
2084 default:
2085 err = -EINVAL;
2086 goto abort;
2087 }
2088
Sam Bradshaw88523a62011-08-30 08:34:26 -06002089 /* Build the FIS. */
2090 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2091
2092 fis.type = 0x27;
2093 fis.opts = 1 << 7;
2094 fis.command = req_task->io_ports[7];
2095 fis.features = req_task->io_ports[1];
2096 fis.sect_count = req_task->io_ports[2];
2097 fis.lba_low = req_task->io_ports[3];
2098 fis.lba_mid = req_task->io_ports[4];
2099 fis.lba_hi = req_task->io_ports[5];
2100 /* Clear the dev bit*/
2101 fis.device = req_task->io_ports[6] & ~0x10;
2102
2103 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2104 req_task->in_flags.all =
2105 IDE_TASKFILE_STD_IN_FLAGS |
2106 (IDE_HOB_STD_IN_FLAGS << 8);
2107 fis.lba_low_ex = req_task->hob_ports[3];
2108 fis.lba_mid_ex = req_task->hob_ports[4];
2109 fis.lba_hi_ex = req_task->hob_ports[5];
2110 fis.features_ex = req_task->hob_ports[1];
2111 fis.sect_cnt_ex = req_task->hob_ports[2];
2112
2113 } else {
2114 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2115 }
2116
2117 force_single_sector = implicit_sector(fis.command, fis.features);
2118
2119 if ((taskin || taskout) && (!fis.sect_count)) {
2120 if (nsect)
2121 fis.sect_count = nsect;
2122 else {
2123 if (!force_single_sector) {
2124 dev_warn(&dd->pdev->dev,
2125 "data movement but "
2126 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002127 err = -EINVAL;
2128 goto abort;
2129 }
2130 }
2131 }
2132
2133 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002134 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002135 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2136 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002137 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002138 fis.command,
2139 fis.features,
2140 fis.sect_count,
2141 fis.lba_low,
2142 fis.lba_mid,
2143 fis.lba_hi,
2144 fis.device);
2145
Selvan Mani4453bc82012-09-27 14:36:43 +02002146 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002147 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2148 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002149 erasemode = 1;
2150 }
2151
2152 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002153
2154 /* Determine the correct transfer size.*/
2155 if (force_single_sector)
2156 transfer_size = ATA_SECT_SIZE;
2157 else
2158 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2159
2160 /* Execute the command.*/
2161 if (mtip_exec_internal_command(dd->port,
2162 &fis,
2163 5,
2164 dma_buffer,
2165 transfer_size,
2166 0,
2167 GFP_KERNEL,
2168 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002169 err = -EIO;
2170 goto abort;
2171 }
2172
2173 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2174
2175 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2176 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2177 req_task->io_ports[7] = reply->control;
2178 } else {
2179 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2180 req_task->io_ports[7] = reply->command;
2181 }
2182
2183 /* reclaim the DMA buffers.*/
2184 if (inbuf_dma)
2185 pci_unmap_single(dd->pdev, inbuf_dma,
2186 taskin, DMA_FROM_DEVICE);
2187 if (outbuf_dma)
2188 pci_unmap_single(dd->pdev, outbuf_dma,
2189 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002190 inbuf_dma = 0;
2191 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002192
2193 /* return the ATA registers to the caller.*/
2194 req_task->io_ports[1] = reply->features;
2195 req_task->io_ports[2] = reply->sect_count;
2196 req_task->io_ports[3] = reply->lba_low;
2197 req_task->io_ports[4] = reply->lba_mid;
2198 req_task->io_ports[5] = reply->lba_hi;
2199 req_task->io_ports[6] = reply->device;
2200
2201 if (req_task->out_flags.all & 1) {
2202
2203 req_task->hob_ports[3] = reply->lba_low_ex;
2204 req_task->hob_ports[4] = reply->lba_mid_ex;
2205 req_task->hob_ports[5] = reply->lba_hi_ex;
2206 req_task->hob_ports[1] = reply->features_ex;
2207 req_task->hob_ports[2] = reply->sect_cnt_ex;
2208 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002209 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002210 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002211 "err %x, sect_cnt %x, lbalo %x,"
2212 "lbamid %x, lbahi %x, dev %x\n",
2213 __func__,
2214 req_task->io_ports[7],
2215 req_task->io_ports[1],
2216 req_task->io_ports[2],
2217 req_task->io_ports[3],
2218 req_task->io_ports[4],
2219 req_task->io_ports[5],
2220 req_task->io_ports[6]);
2221
Sam Bradshaw88523a62011-08-30 08:34:26 -06002222 if (taskout) {
2223 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2224 err = -EFAULT;
2225 goto abort;
2226 }
2227 }
2228 if (taskin) {
2229 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2230 err = -EFAULT;
2231 goto abort;
2232 }
2233 }
2234abort:
2235 if (inbuf_dma)
2236 pci_unmap_single(dd->pdev, inbuf_dma,
2237 taskin, DMA_FROM_DEVICE);
2238 if (outbuf_dma)
2239 pci_unmap_single(dd->pdev, outbuf_dma,
2240 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002241 kfree(outbuf);
2242 kfree(inbuf);
2243
2244 return err;
2245}
2246
2247/*
2248 * Handle IOCTL calls from the Block Layer.
2249 *
2250 * This function is called by the Block Layer when it receives an IOCTL
2251 * command that it does not understand. If the IOCTL command is not supported
2252 * this function returns -ENOTTY.
2253 *
2254 * @dd Pointer to the driver data structure.
2255 * @cmd IOCTL command passed from the Block Layer.
2256 * @arg IOCTL argument passed from the Block Layer.
2257 *
2258 * return value
2259 * 0 The IOCTL completed successfully.
2260 * -ENOTTY The specified command is not supported.
2261 * -EFAULT An error occurred copying data to a user space buffer.
2262 * -EIO An error occurred while executing the command.
2263 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002264static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2265 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002266{
2267 switch (cmd) {
2268 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002269 {
2270 if (copy_to_user((void __user *)arg, dd->port->identify,
2271 sizeof(u16) * ATA_ID_WORDS))
2272 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002273 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002274 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002275 case HDIO_DRIVE_CMD:
2276 {
2277 u8 drive_command[4];
2278
2279 /* Copy the user command info to our buffer. */
2280 if (copy_from_user(drive_command,
2281 (void __user *) arg,
2282 sizeof(drive_command)))
2283 return -EFAULT;
2284
2285 /* Execute the drive command. */
2286 if (exec_drive_command(dd->port,
2287 drive_command,
2288 (void __user *) (arg+4)))
2289 return -EIO;
2290
2291 /* Copy the status back to the users buffer. */
2292 if (copy_to_user((void __user *) arg,
2293 drive_command,
2294 sizeof(drive_command)))
2295 return -EFAULT;
2296
2297 break;
2298 }
2299 case HDIO_DRIVE_TASK:
2300 {
2301 u8 drive_command[7];
2302
2303 /* Copy the user command info to our buffer. */
2304 if (copy_from_user(drive_command,
2305 (void __user *) arg,
2306 sizeof(drive_command)))
2307 return -EFAULT;
2308
2309 /* Execute the drive command. */
2310 if (exec_drive_task(dd->port, drive_command))
2311 return -EIO;
2312
2313 /* Copy the status back to the users buffer. */
2314 if (copy_to_user((void __user *) arg,
2315 drive_command,
2316 sizeof(drive_command)))
2317 return -EFAULT;
2318
2319 break;
2320 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002321 case HDIO_DRIVE_TASKFILE: {
2322 ide_task_request_t req_task;
2323 int ret, outtotal;
2324
2325 if (copy_from_user(&req_task, (void __user *) arg,
2326 sizeof(req_task)))
2327 return -EFAULT;
2328
2329 outtotal = sizeof(req_task);
2330
2331 ret = exec_drive_taskfile(dd, (void __user *) arg,
2332 &req_task, outtotal);
2333
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002334 if (copy_to_user((void __user *) arg, &req_task,
2335 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002336 return -EFAULT;
2337
2338 return ret;
2339 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002340
2341 default:
2342 return -EINVAL;
2343 }
2344 return 0;
2345}
2346
2347/*
2348 * Submit an IO to the hw
2349 *
2350 * This function is called by the block layer to issue an io
2351 * to the device. Upon completion, the callback function will
2352 * be called with the data parameter passed as the callback data.
2353 *
2354 * @dd Pointer to the driver data structure.
2355 * @start First sector to read.
2356 * @nsect Number of sectors to read.
2357 * @nents Number of entries in scatter list for the read command.
2358 * @tag The tag of this read command.
2359 * @callback Pointer to the function that should be called
2360 * when the read completes.
2361 * @data Callback data passed to the callback function
2362 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002363 * @dir Direction (read or write)
2364 *
2365 * return value
2366 * None
2367 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002368static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2369 struct mtip_cmd *command, int nents,
2370 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002371{
2372 struct host_to_dev_fis *fis;
2373 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002374 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2375 u64 start = blk_rq_pos(rq);
2376 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002377
2378 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002379 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002380
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002381 prefetch(&port->flags);
2382
Sam Bradshaw88523a62011-08-30 08:34:26 -06002383 command->scatter_ents = nents;
2384
2385 /*
2386 * The number of retries for this command before it is
2387 * reported as a failure to the upper layers.
2388 */
2389 command->retries = MTIP_MAX_RETRIES;
2390
2391 /* Fill out fis */
2392 fis = command->command;
2393 fis->type = 0x27;
2394 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002395 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002396 fis->command = ATA_CMD_FPDMA_READ;
2397 else
2398 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002399 fis->lba_low = start & 0xFF;
2400 fis->lba_mid = (start >> 8) & 0xFF;
2401 fis->lba_hi = (start >> 16) & 0xFF;
2402 fis->lba_low_ex = (start >> 24) & 0xFF;
2403 fis->lba_mid_ex = (start >> 32) & 0xFF;
2404 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002406 fis->features = nsect & 0xFF;
2407 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002408 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 fis->sect_cnt_ex = 0;
2410 fis->control = 0;
2411 fis->res2 = 0;
2412 fis->res3 = 0;
2413 fill_command_sg(dd, command, nents);
2414
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002415 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002416 fis->device |= 1 << 7;
2417
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002419 command->command_header->opts =
2420 __force_bit2int cpu_to_le32(
2421 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 command->command_header->byte_count = 0;
2423
2424 /*
2425 * Set the completion function and data for the command
2426 * within this layer.
2427 */
2428 command->comp_data = dd;
2429 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002430 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431
2432 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002433 * To prevent this command from being issued
2434 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002436 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002437 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002438 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002439 return;
2440 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002441
2442 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002443 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002444}
2445
2446/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002447 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002448 *
2449 * @dev Pointer to the device structure, passed by the kernrel.
2450 * @attr Pointer to the device_attribute structure passed by the kernel.
2451 * @buf Pointer to the char buffer that will receive the stats info.
2452 *
2453 * return value
2454 * The size, in bytes, of the data copied into buf.
2455 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002456static ssize_t mtip_hw_show_status(struct device *dev,
2457 struct device_attribute *attr,
2458 char *buf)
2459{
2460 struct driver_data *dd = dev_to_disk(dev)->private_data;
2461 int size = 0;
2462
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002463 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002464 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002465 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002466 size += sprintf(buf, "%s", "write_protect\n");
2467 else
2468 size += sprintf(buf, "%s", "online\n");
2469
2470 return size;
2471}
2472
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002473static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002474
Asai Thambi S P0caff002013-04-03 19:56:21 +05302475/* debugsfs entries */
2476
2477static ssize_t show_device_status(struct device_driver *drv, char *buf)
2478{
2479 int size = 0;
2480 struct driver_data *dd, *tmp;
2481 unsigned long flags;
2482 char id_buf[42];
2483 u16 status = 0;
2484
2485 spin_lock_irqsave(&dev_lock, flags);
2486 size += sprintf(&buf[size], "Devices Present:\n");
2487 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002488 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302489 if (dd->port &&
2490 dd->port->identify &&
2491 dd->port->identify_valid) {
2492 strlcpy(id_buf,
2493 (char *) (dd->port->identify + 10), 21);
2494 status = *(dd->port->identify + 141);
2495 } else {
2496 memset(id_buf, 0, 42);
2497 status = 0;
2498 }
2499
2500 if (dd->port &&
2501 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2502 size += sprintf(&buf[size],
2503 " device %s %s (ftl rebuild %d %%)\n",
2504 dev_name(&dd->pdev->dev),
2505 id_buf,
2506 status);
2507 } else {
2508 size += sprintf(&buf[size],
2509 " device %s %s\n",
2510 dev_name(&dd->pdev->dev),
2511 id_buf);
2512 }
2513 }
2514 }
2515
2516 size += sprintf(&buf[size], "Devices Being Removed:\n");
2517 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002518 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302519 if (dd->port &&
2520 dd->port->identify &&
2521 dd->port->identify_valid) {
2522 strlcpy(id_buf,
2523 (char *) (dd->port->identify+10), 21);
2524 status = *(dd->port->identify + 141);
2525 } else {
2526 memset(id_buf, 0, 42);
2527 status = 0;
2528 }
2529
2530 if (dd->port &&
2531 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2532 size += sprintf(&buf[size],
2533 " device %s %s (ftl rebuild %d %%)\n",
2534 dev_name(&dd->pdev->dev),
2535 id_buf,
2536 status);
2537 } else {
2538 size += sprintf(&buf[size],
2539 " device %s %s\n",
2540 dev_name(&dd->pdev->dev),
2541 id_buf);
2542 }
2543 }
2544 }
2545 spin_unlock_irqrestore(&dev_lock, flags);
2546
2547 return size;
2548}
2549
2550static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2551 size_t len, loff_t *offset)
2552{
David Milburnc8afd0d2013-05-23 16:23:45 -05002553 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302554 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002555 char *buf;
2556 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302557
2558 if (!len || *offset)
2559 return 0;
2560
David Milburnc8afd0d2013-05-23 16:23:45 -05002561 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2562 if (!buf) {
2563 dev_err(&dd->pdev->dev,
2564 "Memory allocation: status buffer\n");
2565 return -ENOMEM;
2566 }
2567
Asai Thambi S P0caff002013-04-03 19:56:21 +05302568 size += show_device_status(NULL, buf);
2569
2570 *offset = size <= len ? size : len;
2571 size = copy_to_user(ubuf, buf, *offset);
2572 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002573 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302574
David Milburnc8afd0d2013-05-23 16:23:45 -05002575 kfree(buf);
2576 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302577}
2578
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002579static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2580 size_t len, loff_t *offset)
2581{
2582 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002583 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002584 u32 group_allocated;
2585 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002586 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002587
2588 if (!len || size)
2589 return 0;
2590
David Milburnc8afd0d2013-05-23 16:23:45 -05002591 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2592 if (!buf) {
2593 dev_err(&dd->pdev->dev,
2594 "Memory allocation: register buffer\n");
2595 return -ENOMEM;
2596 }
2597
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002598 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2599
2600 for (n = dd->slot_groups-1; n >= 0; n--)
2601 size += sprintf(&buf[size], "%08X ",
2602 readl(dd->port->s_active[n]));
2603
2604 size += sprintf(&buf[size], "]\n");
2605 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2606
2607 for (n = dd->slot_groups-1; n >= 0; n--)
2608 size += sprintf(&buf[size], "%08X ",
2609 readl(dd->port->cmd_issue[n]));
2610
2611 size += sprintf(&buf[size], "]\n");
2612 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--)
2615 size += sprintf(&buf[size], "%08X ",
2616 readl(dd->port->completed[n]));
2617
2618 size += sprintf(&buf[size], "]\n");
2619 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2620 readl(dd->port->mmio + PORT_IRQ_STAT));
2621 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2622 readl(dd->mmio + HOST_IRQ_STAT));
2623 size += sprintf(&buf[size], "\n");
2624
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002625 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2626
2627 for (n = dd->slot_groups-1; n >= 0; n--) {
2628 if (sizeof(long) > sizeof(u32))
2629 group_allocated =
2630 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2631 else
2632 group_allocated = dd->port->cmds_to_issue[n];
2633 size += sprintf(&buf[size], "%08X ", group_allocated);
2634 }
2635 size += sprintf(&buf[size], "]\n");
2636
2637 *offset = size <= len ? size : len;
2638 size = copy_to_user(ubuf, buf, *offset);
2639 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002640 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002641
David Milburnc8afd0d2013-05-23 16:23:45 -05002642 kfree(buf);
2643 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002644}
2645
2646static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2647 size_t len, loff_t *offset)
2648{
2649 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002650 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002651 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002652 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002653
2654 if (!len || size)
2655 return 0;
2656
David Milburnc8afd0d2013-05-23 16:23:45 -05002657 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2658 if (!buf) {
2659 dev_err(&dd->pdev->dev,
2660 "Memory allocation: flag buffer\n");
2661 return -ENOMEM;
2662 }
2663
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002664 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2665 dd->port->flags);
2666 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2667 dd->dd_flag);
2668
2669 *offset = size <= len ? size : len;
2670 size = copy_to_user(ubuf, buf, *offset);
2671 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002672 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002673
David Milburnc8afd0d2013-05-23 16:23:45 -05002674 kfree(buf);
2675 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002676}
2677
Asai Thambi S P0caff002013-04-03 19:56:21 +05302678static const struct file_operations mtip_device_status_fops = {
2679 .owner = THIS_MODULE,
2680 .open = simple_open,
2681 .read = mtip_hw_read_device_status,
2682 .llseek = no_llseek,
2683};
2684
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002685static const struct file_operations mtip_regs_fops = {
2686 .owner = THIS_MODULE,
2687 .open = simple_open,
2688 .read = mtip_hw_read_registers,
2689 .llseek = no_llseek,
2690};
2691
2692static const struct file_operations mtip_flags_fops = {
2693 .owner = THIS_MODULE,
2694 .open = simple_open,
2695 .read = mtip_hw_read_flags,
2696 .llseek = no_llseek,
2697};
2698
Sam Bradshaw88523a62011-08-30 08:34:26 -06002699/*
2700 * Create the sysfs related attributes.
2701 *
2702 * @dd Pointer to the driver data structure.
2703 * @kobj Pointer to the kobj for the block device.
2704 *
2705 * return value
2706 * 0 Operation completed successfully.
2707 * -EINVAL Invalid parameter.
2708 */
Jens Axboe63166682011-09-27 21:27:43 -06002709static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002710{
2711 if (!kobj || !dd)
2712 return -EINVAL;
2713
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002714 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2715 dev_warn(&dd->pdev->dev,
2716 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002717 return 0;
2718}
2719
2720/*
2721 * Remove the sysfs related attributes.
2722 *
2723 * @dd Pointer to the driver data structure.
2724 * @kobj Pointer to the kobj for the block device.
2725 *
2726 * return value
2727 * 0 Operation completed successfully.
2728 * -EINVAL Invalid parameter.
2729 */
Jens Axboe63166682011-09-27 21:27:43 -06002730static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002731{
2732 if (!kobj || !dd)
2733 return -EINVAL;
2734
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002735 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002736
2737 return 0;
2738}
2739
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002740static int mtip_hw_debugfs_init(struct driver_data *dd)
2741{
2742 if (!dfs_parent)
2743 return -1;
2744
2745 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2746 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2747 dev_warn(&dd->pdev->dev,
2748 "Error creating node %s under debugfs\n",
2749 dd->disk->disk_name);
2750 dd->dfs_node = NULL;
2751 return -1;
2752 }
2753
2754 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2755 &mtip_flags_fops);
2756 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2757 &mtip_regs_fops);
2758
2759 return 0;
2760}
2761
2762static void mtip_hw_debugfs_exit(struct driver_data *dd)
2763{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002764 if (dd->dfs_node)
2765 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002766}
2767
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002768static int mtip_free_orphan(struct driver_data *dd)
2769{
2770 struct kobject *kobj;
2771
2772 if (dd->bdev) {
2773 if (dd->bdev->bd_holders >= 1)
2774 return -2;
2775
2776 bdput(dd->bdev);
2777 dd->bdev = NULL;
2778 }
2779
2780 mtip_hw_debugfs_exit(dd);
2781
2782 spin_lock(&rssd_index_lock);
2783 ida_remove(&rssd_index_ida, dd->index);
2784 spin_unlock(&rssd_index_lock);
2785
2786 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
2787 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
2788 put_disk(dd->disk);
2789 } else {
2790 if (dd->disk) {
2791 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
2792 if (kobj) {
2793 mtip_hw_sysfs_exit(dd, kobj);
2794 kobject_put(kobj);
2795 }
2796 del_gendisk(dd->disk);
Asai Thambi SP02b48262015-05-11 15:48:00 -07002797 put_disk(dd->disk);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002798 dd->disk = NULL;
2799 }
2800 if (dd->queue) {
2801 dd->queue->queuedata = NULL;
2802 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06002803 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002804 dd->queue = NULL;
2805 }
2806 }
2807 kfree(dd);
2808 return 0;
2809}
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002810
Sam Bradshaw88523a62011-08-30 08:34:26 -06002811/*
2812 * Perform any init/resume time hardware setup
2813 *
2814 * @dd Pointer to the driver data structure.
2815 *
2816 * return value
2817 * None
2818 */
2819static inline void hba_setup(struct driver_data *dd)
2820{
2821 u32 hwdata;
2822 hwdata = readl(dd->mmio + HOST_HSORG);
2823
2824 /* interrupt bug workaround: use only 1 IS bit.*/
2825 writel(hwdata |
2826 HSORG_DISABLE_SLOTGRP_INTR |
2827 HSORG_DISABLE_SLOTGRP_PXIS,
2828 dd->mmio + HOST_HSORG);
2829}
2830
Asai Thambi S P2077d942013-04-29 21:19:49 +02002831static int mtip_device_unaligned_constrained(struct driver_data *dd)
2832{
2833 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2834}
2835
Sam Bradshaw88523a62011-08-30 08:34:26 -06002836/*
2837 * Detect the details of the product, and store anything needed
2838 * into the driver data structure. This includes product type and
2839 * version and number of slot groups.
2840 *
2841 * @dd Pointer to the driver data structure.
2842 *
2843 * return value
2844 * None
2845 */
2846static void mtip_detect_product(struct driver_data *dd)
2847{
2848 u32 hwdata;
2849 unsigned int rev, slotgroups;
2850
2851 /*
2852 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2853 * info register:
2854 * [15:8] hardware/software interface rev#
2855 * [ 3] asic-style interface
2856 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2857 */
2858 hwdata = readl(dd->mmio + HOST_HSORG);
2859
2860 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2861 dd->slot_groups = 1;
2862
2863 if (hwdata & 0x8) {
2864 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2865 rev = (hwdata & HSORG_HWREV) >> 8;
2866 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2867 dev_info(&dd->pdev->dev,
2868 "ASIC-FPGA design, HS rev 0x%x, "
2869 "%i slot groups [%i slots]\n",
2870 rev,
2871 slotgroups,
2872 slotgroups * 32);
2873
2874 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2875 dev_warn(&dd->pdev->dev,
2876 "Warning: driver only supports "
2877 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2878 slotgroups = MTIP_MAX_SLOT_GROUPS;
2879 }
2880 dd->slot_groups = slotgroups;
2881 return;
2882 }
2883
2884 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2885}
2886
2887/*
2888 * Blocking wait for FTL rebuild to complete
2889 *
2890 * @dd Pointer to the DRIVER_DATA structure.
2891 *
2892 * return value
2893 * 0 FTL rebuild completed successfully
2894 * -EFAULT FTL rebuild error/timeout/interruption
2895 */
2896static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2897{
2898 unsigned long timeout, cnt = 0, start;
2899
2900 dev_warn(&dd->pdev->dev,
2901 "FTL rebuild in progress. Polling for completion.\n");
2902
2903 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002904 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2905
2906 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002907 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002908 &dd->dd_flag)))
2909 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002910 if (mtip_check_surprise_removal(dd->pdev))
2911 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002912
Sam Bradshaw88523a62011-08-30 08:34:26 -06002913 if (mtip_get_identify(dd->port, NULL) < 0)
2914 return -EFAULT;
2915
2916 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2917 MTIP_FTL_REBUILD_MAGIC) {
2918 ssleep(1);
2919 /* Print message every 3 minutes */
2920 if (cnt++ >= 180) {
2921 dev_warn(&dd->pdev->dev,
2922 "FTL rebuild in progress (%d secs).\n",
2923 jiffies_to_msecs(jiffies - start) / 1000);
2924 cnt = 0;
2925 }
2926 } else {
2927 dev_warn(&dd->pdev->dev,
2928 "FTL rebuild complete (%d secs).\n",
2929 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002930 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002931 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002932 }
2933 ssleep(10);
2934 } while (time_before(jiffies, timeout));
2935
2936 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002937 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002938 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2939 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002940 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002941}
2942
2943/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002944 * service thread to issue queued commands
2945 *
2946 * @data Pointer to the driver data structure.
2947 *
2948 * return value
2949 * 0
2950 */
2951
2952static int mtip_service_thread(void *data)
2953{
2954 struct driver_data *dd = (struct driver_data *)data;
2955 unsigned long slot, slot_start, slot_wrap;
2956 unsigned int num_cmd_slots = dd->slot_groups * 32;
2957 struct mtip_port *port = dd->port;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002958 int ret;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002959
2960 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002961 if (kthread_should_stop() ||
2962 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2963 goto st_out;
2964 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2965
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002966 /*
2967 * the condition is to check neither an internal command is
2968 * is in progress nor error handling is active
2969 */
2970 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002971 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002972
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002973 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2974
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002975 if (kthread_should_stop() ||
2976 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2977 goto st_out;
2978
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002979 /* If I am an orphan, start self cleanup */
2980 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002981 break;
2982
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002983 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002984 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002985 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002986
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002987restart_eh:
2988 /* Demux bits: start with error handling */
2989 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2990 mtip_handle_tfe(dd);
2991 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2992 }
2993
2994 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2995 goto restart_eh;
2996
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002997 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002998 slot = 1;
2999 /* used to restrict the loop to one iteration */
3000 slot_start = num_cmd_slots;
3001 slot_wrap = 0;
3002 while (1) {
3003 slot = find_next_bit(port->cmds_to_issue,
3004 num_cmd_slots, slot);
3005 if (slot_wrap == 1) {
3006 if ((slot_start >= slot) ||
3007 (slot >= num_cmd_slots))
3008 break;
3009 }
3010 if (unlikely(slot_start == num_cmd_slots))
3011 slot_start = slot;
3012
3013 if (unlikely(slot == num_cmd_slots)) {
3014 slot = 1;
3015 slot_wrap = 1;
3016 continue;
3017 }
3018
3019 /* Issue the command to the hardware */
3020 mtip_issue_ncq_command(port, slot);
3021
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003022 clear_bit(slot, port->cmds_to_issue);
3023 }
3024
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003025 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003026 }
3027
3028 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003029 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003030 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003031 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003032 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003033 }
3034 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003035
3036 /* wait for pci remove to exit */
3037 while (1) {
3038 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3039 break;
3040 msleep_interruptible(1000);
3041 if (kthread_should_stop())
3042 goto st_out;
3043 }
3044
3045 while (1) {
3046 ret = mtip_free_orphan(dd);
3047 if (!ret) {
3048 /* NOTE: All data structures are invalid, do not
3049 * access any here */
3050 return 0;
3051 }
3052 msleep_interruptible(1000);
3053 if (kthread_should_stop())
3054 goto st_out;
3055 }
3056st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003057 return 0;
3058}
3059
3060/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003061 * DMA region teardown
3062 *
3063 * @dd Pointer to driver_data structure
3064 *
3065 * return value
3066 * None
3067 */
3068static void mtip_dma_free(struct driver_data *dd)
3069{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003070 struct mtip_port *port = dd->port;
3071
3072 if (port->block1)
3073 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3074 port->block1, port->block1_dma);
3075
3076 if (port->command_list) {
3077 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3078 port->command_list, port->command_list_dma);
3079 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003080}
3081
3082/*
3083 * DMA region setup
3084 *
3085 * @dd Pointer to driver_data structure
3086 *
3087 * return value
3088 * -ENOMEM Not enough free DMA region space to initialize driver
3089 */
3090static int mtip_dma_alloc(struct driver_data *dd)
3091{
3092 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003093
3094 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3095 port->block1 =
3096 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3097 &port->block1_dma, GFP_KERNEL);
3098 if (!port->block1)
3099 return -ENOMEM;
3100 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3101
3102 /* Allocate dma memory for command list */
3103 port->command_list =
3104 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3105 &port->command_list_dma, GFP_KERNEL);
3106 if (!port->command_list) {
3107 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3108 port->block1, port->block1_dma);
3109 port->block1 = NULL;
3110 port->block1_dma = 0;
3111 return -ENOMEM;
3112 }
3113 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3114
3115 /* Setup all pointers into first DMA region */
3116 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3117 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3118 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3119 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3120 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3121 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3122 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3123 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3124
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003125 return 0;
3126}
3127
Jens Axboeffc771b2014-05-09 09:42:02 -06003128static int mtip_hw_get_identify(struct driver_data *dd)
3129{
3130 struct smart_attr attr242;
3131 unsigned char *buf;
3132 int rv;
3133
3134 if (mtip_get_identify(dd->port, NULL) < 0)
3135 return -EFAULT;
3136
3137 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3138 MTIP_FTL_REBUILD_MAGIC) {
3139 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3140 return MTIP_FTL_REBUILD_MAGIC;
3141 }
3142 mtip_dump_identify(dd->port);
3143
3144 /* check write protect, over temp and rebuild statuses */
3145 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3146 dd->port->log_buf,
3147 dd->port->log_buf_dma, 1);
3148 if (rv) {
3149 dev_warn(&dd->pdev->dev,
3150 "Error in READ LOG EXT (10h) command\n");
3151 /* non-critical error, don't fail the load */
3152 } else {
3153 buf = (unsigned char *)dd->port->log_buf;
3154 if (buf[259] & 0x1) {
3155 dev_info(&dd->pdev->dev,
3156 "Write protect bit is set.\n");
3157 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3158 }
3159 if (buf[288] == 0xF7) {
3160 dev_info(&dd->pdev->dev,
3161 "Exceeded Tmax, drive in thermal shutdown.\n");
3162 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3163 }
3164 if (buf[288] == 0xBF) {
3165 dev_info(&dd->pdev->dev,
3166 "Drive indicates rebuild has failed.\n");
3167 /* TODO */
3168 }
3169 }
3170
3171 /* get write protect progess */
3172 memset(&attr242, 0, sizeof(struct smart_attr));
3173 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3174 dev_warn(&dd->pdev->dev,
3175 "Unable to check write protect progress\n");
3176 else
3177 dev_info(&dd->pdev->dev,
3178 "Write protect progress: %u%% (%u blocks)\n",
3179 attr242.cur, le32_to_cpu(attr242.data));
3180
3181 return rv;
3182}
3183
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003184/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003185 * Called once for each card.
3186 *
3187 * @dd Pointer to the driver data structure.
3188 *
3189 * return value
3190 * 0 on success, else an error code.
3191 */
Jens Axboe63166682011-09-27 21:27:43 -06003192static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003193{
3194 int i;
3195 int rv;
3196 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003197 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003198
3199 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3200
3201 mtip_detect_product(dd);
3202 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3203 rv = -EIO;
3204 goto out1;
3205 }
3206 num_command_slots = dd->slot_groups * 32;
3207
3208 hba_setup(dd);
3209
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003210 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3211 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003212 if (!dd->port) {
3213 dev_err(&dd->pdev->dev,
3214 "Memory allocation: port structure\n");
3215 return -ENOMEM;
3216 }
3217
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003218 /* Continue workqueue setup */
3219 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3220 dd->work[i].port = dd->port;
3221
Asai Thambi S P2077d942013-04-29 21:19:49 +02003222 /* Enable unaligned IO constraints for some devices */
3223 if (mtip_device_unaligned_constrained(dd))
3224 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3225 else
3226 dd->unal_qdepth = 0;
3227
Asai Thambi S P2077d942013-04-29 21:19:49 +02003228 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003229
3230 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003231 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3232 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003233
3234 /* Set the port mmio base address. */
3235 dd->port->mmio = dd->mmio + PORT_OFFSET;
3236 dd->port->dd = dd;
3237
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003238 /* DMA allocations */
3239 rv = mtip_dma_alloc(dd);
3240 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003241 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003242
3243 /* Setup the pointers to the extended s_active and CI registers. */
3244 for (i = 0; i < dd->slot_groups; i++) {
3245 dd->port->s_active[i] =
3246 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3247 dd->port->cmd_issue[i] =
3248 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3249 dd->port->completed[i] =
3250 dd->port->mmio + i*0x80 + PORT_SDBV;
3251 }
3252
Asai Thambi S P45038362012-04-09 08:35:38 +02003253 timetaken = jiffies;
3254 timeout = jiffies + msecs_to_jiffies(30000);
3255 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3256 time_before(jiffies, timeout)) {
3257 mdelay(100);
3258 }
3259 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3260 timetaken = jiffies - timetaken;
3261 dev_warn(&dd->pdev->dev,
3262 "Surprise removal detected at %u ms\n",
3263 jiffies_to_msecs(timetaken));
3264 rv = -ENODEV;
3265 goto out2 ;
3266 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003267 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003268 timetaken = jiffies - timetaken;
3269 dev_warn(&dd->pdev->dev,
3270 "Removal detected at %u ms\n",
3271 jiffies_to_msecs(timetaken));
3272 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003273 goto out2;
3274 }
3275
Asai Thambi S P45038362012-04-09 08:35:38 +02003276 /* Conditionally reset the HBA. */
3277 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3278 if (mtip_hba_reset(dd) < 0) {
3279 dev_err(&dd->pdev->dev,
3280 "Card did not reset within timeout\n");
3281 rv = -EIO;
3282 goto out2;
3283 }
3284 } else {
3285 /* Clear any pending interrupts on the HBA */
3286 writel(readl(dd->mmio + HOST_IRQ_STAT),
3287 dd->mmio + HOST_IRQ_STAT);
3288 }
3289
Sam Bradshaw88523a62011-08-30 08:34:26 -06003290 mtip_init_port(dd->port);
3291 mtip_start_port(dd->port);
3292
3293 /* Setup the ISR and enable interrupts. */
3294 rv = devm_request_irq(&dd->pdev->dev,
3295 dd->pdev->irq,
3296 mtip_irq_handler,
3297 IRQF_SHARED,
3298 dev_driver_string(&dd->pdev->dev),
3299 dd);
3300
3301 if (rv) {
3302 dev_err(&dd->pdev->dev,
3303 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3304 goto out2;
3305 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003306 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003307
3308 /* Enable interrupts on the HBA. */
3309 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3310 dd->mmio + HOST_CTL);
3311
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003312 init_waitqueue_head(&dd->port->svc_wait);
3313
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003314 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003315 rv = -EFAULT;
3316 goto out3;
3317 }
3318
Sam Bradshaw88523a62011-08-30 08:34:26 -06003319 return rv;
3320
3321out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003322 /* Disable interrupts on the HBA. */
3323 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3324 dd->mmio + HOST_CTL);
3325
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003326 /* Release the IRQ. */
3327 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003328 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3329
3330out2:
3331 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003332 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003333
Sam Bradshaw88523a62011-08-30 08:34:26 -06003334out1:
3335 /* Free the memory allocated for the for structure. */
3336 kfree(dd->port);
3337
3338 return rv;
3339}
3340
Jens Axboeffc771b2014-05-09 09:42:02 -06003341static void mtip_standby_drive(struct driver_data *dd)
3342{
3343 if (dd->sr)
3344 return;
3345
3346 /*
3347 * Send standby immediate (E0h) to the drive so that it
3348 * saves its state.
3349 */
3350 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3351 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3352 if (mtip_standby_immediate(dd->port))
3353 dev_warn(&dd->pdev->dev,
3354 "STANDBY IMMEDIATE failed\n");
3355}
3356
Sam Bradshaw88523a62011-08-30 08:34:26 -06003357/*
3358 * Called to deinitialize an interface.
3359 *
3360 * @dd Pointer to the driver data structure.
3361 *
3362 * return value
3363 * 0
3364 */
Jens Axboe63166682011-09-27 21:27:43 -06003365static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003366{
3367 /*
3368 * Send standby immediate (E0h) to the drive so that it
3369 * saves its state.
3370 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003371 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003372 /* de-initialize the port. */
3373 mtip_deinit_port(dd->port);
3374
3375 /* Disable interrupts on the HBA. */
3376 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3377 dd->mmio + HOST_CTL);
3378 }
3379
Sam Bradshaw88523a62011-08-30 08:34:26 -06003380 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003381 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003382 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3383
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003384 /* Free dma regions */
3385 mtip_dma_free(dd);
3386
Sam Bradshaw88523a62011-08-30 08:34:26 -06003387 /* Free the memory allocated for the for structure. */
3388 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003389 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003390
3391 return 0;
3392}
3393
3394/*
3395 * Issue a Standby Immediate command to the device.
3396 *
3397 * This function is called by the Block Layer just before the
3398 * system powers off during a shutdown.
3399 *
3400 * @dd Pointer to the driver data structure.
3401 *
3402 * return value
3403 * 0
3404 */
Jens Axboe63166682011-09-27 21:27:43 -06003405static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003406{
3407 /*
3408 * Send standby immediate (E0h) to the drive so that it
3409 * saves its state.
3410 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003411 if (!dd->sr && dd->port)
3412 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003413
3414 return 0;
3415}
3416
3417/*
3418 * Suspend function
3419 *
3420 * This function is called by the Block Layer just before the
3421 * system hibernates.
3422 *
3423 * @dd Pointer to the driver data structure.
3424 *
3425 * return value
3426 * 0 Suspend was successful
3427 * -EFAULT Suspend was not successful
3428 */
Jens Axboe63166682011-09-27 21:27:43 -06003429static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003430{
3431 /*
3432 * Send standby immediate (E0h) to the drive
3433 * so that it saves its state.
3434 */
3435 if (mtip_standby_immediate(dd->port) != 0) {
3436 dev_err(&dd->pdev->dev,
3437 "Failed standby-immediate command\n");
3438 return -EFAULT;
3439 }
3440
3441 /* Disable interrupts on the HBA.*/
3442 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3443 dd->mmio + HOST_CTL);
3444 mtip_deinit_port(dd->port);
3445
3446 return 0;
3447}
3448
3449/*
3450 * Resume function
3451 *
3452 * This function is called by the Block Layer as the
3453 * system resumes.
3454 *
3455 * @dd Pointer to the driver data structure.
3456 *
3457 * return value
3458 * 0 Resume was successful
3459 * -EFAULT Resume was not successful
3460 */
Jens Axboe63166682011-09-27 21:27:43 -06003461static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003462{
3463 /* Perform any needed hardware setup steps */
3464 hba_setup(dd);
3465
3466 /* Reset the HBA */
3467 if (mtip_hba_reset(dd) != 0) {
3468 dev_err(&dd->pdev->dev,
3469 "Unable to reset the HBA\n");
3470 return -EFAULT;
3471 }
3472
3473 /*
3474 * Enable the port, DMA engine, and FIS reception specific
3475 * h/w in controller.
3476 */
3477 mtip_init_port(dd->port);
3478 mtip_start_port(dd->port);
3479
3480 /* Enable interrupts on the HBA.*/
3481 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3482 dd->mmio + HOST_CTL);
3483
3484 return 0;
3485}
3486
3487/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003488 * Helper function for reusing disk name
3489 * upon hot insertion.
3490 */
3491static int rssd_disk_name_format(char *prefix,
3492 int index,
3493 char *buf,
3494 int buflen)
3495{
3496 const int base = 'z' - 'a' + 1;
3497 char *begin = buf + strlen(prefix);
3498 char *end = buf + buflen;
3499 char *p;
3500 int unit;
3501
3502 p = end - 1;
3503 *p = '\0';
3504 unit = base;
3505 do {
3506 if (p == begin)
3507 return -EINVAL;
3508 *--p = 'a' + (index % unit);
3509 index = (index / unit) - 1;
3510 } while (index >= 0);
3511
3512 memmove(begin, p, end - p);
3513 memcpy(buf, prefix, strlen(prefix));
3514
3515 return 0;
3516}
3517
3518/*
3519 * Block layer IOCTL handler.
3520 *
3521 * @dev Pointer to the block_device structure.
3522 * @mode ignored
3523 * @cmd IOCTL command passed from the user application.
3524 * @arg Argument passed from the user application.
3525 *
3526 * return value
3527 * 0 IOCTL completed successfully.
3528 * -ENOTTY IOCTL not supported or invalid driver data
3529 * structure pointer.
3530 */
3531static int mtip_block_ioctl(struct block_device *dev,
3532 fmode_t mode,
3533 unsigned cmd,
3534 unsigned long arg)
3535{
3536 struct driver_data *dd = dev->bd_disk->private_data;
3537
3538 if (!capable(CAP_SYS_ADMIN))
3539 return -EACCES;
3540
3541 if (!dd)
3542 return -ENOTTY;
3543
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003544 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003545 return -ENOTTY;
3546
Sam Bradshaw88523a62011-08-30 08:34:26 -06003547 switch (cmd) {
3548 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003549 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003550 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003551 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003552 }
3553}
3554
Jens Axboe16d02c02011-09-27 15:50:01 -06003555#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003556/*
3557 * Block layer compat IOCTL handler.
3558 *
3559 * @dev Pointer to the block_device structure.
3560 * @mode ignored
3561 * @cmd IOCTL command passed from the user application.
3562 * @arg Argument passed from the user application.
3563 *
3564 * return value
3565 * 0 IOCTL completed successfully.
3566 * -ENOTTY IOCTL not supported or invalid driver data
3567 * structure pointer.
3568 */
3569static int mtip_block_compat_ioctl(struct block_device *dev,
3570 fmode_t mode,
3571 unsigned cmd,
3572 unsigned long arg)
3573{
3574 struct driver_data *dd = dev->bd_disk->private_data;
3575
3576 if (!capable(CAP_SYS_ADMIN))
3577 return -EACCES;
3578
3579 if (!dd)
3580 return -ENOTTY;
3581
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003582 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003583 return -ENOTTY;
3584
Sam Bradshaw88523a62011-08-30 08:34:26 -06003585 switch (cmd) {
3586 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003587 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003588 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003589 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003590 ide_task_request_t req_task;
3591 int compat_tasksize, outtotal, ret;
3592
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003593 compat_tasksize =
3594 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003595
3596 compat_req_task =
3597 (struct mtip_compat_ide_task_request_s __user *) arg;
3598
3599 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003600 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003601 return -EFAULT;
3602
3603 if (get_user(req_task.out_size, &compat_req_task->out_size))
3604 return -EFAULT;
3605
3606 if (get_user(req_task.in_size, &compat_req_task->in_size))
3607 return -EFAULT;
3608
3609 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3610
3611 ret = exec_drive_taskfile(dd, (void __user *) arg,
3612 &req_task, outtotal);
3613
3614 if (copy_to_user((void __user *) arg, &req_task,
3615 compat_tasksize -
3616 (2 * sizeof(compat_long_t))))
3617 return -EFAULT;
3618
3619 if (put_user(req_task.out_size, &compat_req_task->out_size))
3620 return -EFAULT;
3621
3622 if (put_user(req_task.in_size, &compat_req_task->in_size))
3623 return -EFAULT;
3624
3625 return ret;
3626 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003627 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003628 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003629 }
3630}
Jens Axboe16d02c02011-09-27 15:50:01 -06003631#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003632
3633/*
3634 * Obtain the geometry of the device.
3635 *
3636 * You may think that this function is obsolete, but some applications,
3637 * fdisk for example still used CHS values. This function describes the
3638 * device as having 224 heads and 56 sectors per cylinder. These values are
3639 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3640 * partition is described in terms of a start and end cylinder this means
3641 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3642 * affects performance.
3643 *
3644 * @dev Pointer to the block_device strucutre.
3645 * @geo Pointer to a hd_geometry structure.
3646 *
3647 * return value
3648 * 0 Operation completed successfully.
3649 * -ENOTTY An error occurred while reading the drive capacity.
3650 */
3651static int mtip_block_getgeo(struct block_device *dev,
3652 struct hd_geometry *geo)
3653{
3654 struct driver_data *dd = dev->bd_disk->private_data;
3655 sector_t capacity;
3656
3657 if (!dd)
3658 return -ENOTTY;
3659
3660 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3661 dev_warn(&dd->pdev->dev,
3662 "Could not get drive capacity.\n");
3663 return -ENOTTY;
3664 }
3665
3666 geo->heads = 224;
3667 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003668 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003669 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003670 return 0;
3671}
3672
3673/*
3674 * Block device operation function.
3675 *
3676 * This structure contains pointers to the functions required by the block
3677 * layer.
3678 */
3679static const struct block_device_operations mtip_block_ops = {
3680 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003681#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003682 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003683#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003684 .getgeo = mtip_block_getgeo,
3685 .owner = THIS_MODULE
3686};
3687
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003688static inline bool is_se_active(struct driver_data *dd)
3689{
3690 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3691 if (dd->port->ic_pause_timer) {
3692 unsigned long to = dd->port->ic_pause_timer +
3693 msecs_to_jiffies(1000);
3694 if (time_after(jiffies, to)) {
3695 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3696 &dd->port->flags);
3697 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3698 dd->port->ic_pause_timer = 0;
3699 wake_up_interruptible(&dd->port->svc_wait);
3700 return false;
3701 }
3702 }
3703 return true;
3704 }
3705 return false;
3706}
3707
Sam Bradshaw88523a62011-08-30 08:34:26 -06003708/*
3709 * Block layer make request function.
3710 *
3711 * This function is called by the kernel to process a BIO for
3712 * the P320 device.
3713 *
3714 * @queue Pointer to the request queue. Unused other than to obtain
3715 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003716 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003717 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003718 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003719static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003720{
Jens Axboeffc771b2014-05-09 09:42:02 -06003721 struct driver_data *dd = hctx->queue->queuedata;
3722 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3723 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003724
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003725 if (is_se_active(dd))
3726 return -ENODATA;
3727
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003728 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3729 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3730 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003731 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003732 }
3733 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003734 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003735 }
3736 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3737 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003738 rq_data_dir(rq))) {
3739 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003740 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003741 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3742 return -ENODATA;
3743 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3744 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003745 }
3746
Jens Axboeffc771b2014-05-09 09:42:02 -06003747 if (rq->cmd_flags & REQ_DISCARD) {
3748 int err;
3749
3750 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003751 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003752 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003753 }
3754
Jens Axboeffc771b2014-05-09 09:42:02 -06003755 /* Create the scatter list for this request. */
3756 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003757
Jens Axboeffc771b2014-05-09 09:42:02 -06003758 /* Issue the read/write. */
3759 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3760 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003761}
3762
Jens Axboeffc771b2014-05-09 09:42:02 -06003763static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3764 struct request *rq)
3765{
3766 struct driver_data *dd = hctx->queue->queuedata;
3767 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3768
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003769 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003770 return false;
3771
3772 /*
3773 * If unaligned depth must be limited on this controller, mark it
3774 * as unaligned if the IO isn't on a 4k boundary (start of length).
3775 */
3776 if (blk_rq_sectors(rq) <= 64) {
3777 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3778 cmd->unaligned = 1;
3779 }
3780
3781 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3782 return true;
3783
3784 return false;
3785}
3786
Jens Axboe74c45052014-10-29 11:14:52 -06003787static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3788 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003789{
Jens Axboe74c45052014-10-29 11:14:52 -06003790 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003791 int ret;
3792
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003793 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003794 return BLK_MQ_RQ_QUEUE_BUSY;
3795
Christoph Hellwige2490072014-09-13 16:40:09 -07003796 blk_mq_start_request(rq);
3797
Jens Axboeffc771b2014-05-09 09:42:02 -06003798 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003799 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003800 return BLK_MQ_RQ_QUEUE_OK;
3801
3802 rq->errors = ret;
3803 return BLK_MQ_RQ_QUEUE_ERROR;
3804}
3805
3806static void mtip_free_cmd(void *data, struct request *rq,
3807 unsigned int hctx_idx, unsigned int request_idx)
3808{
3809 struct driver_data *dd = data;
3810 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3811
3812 if (!cmd->command)
3813 return;
3814
3815 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3816 cmd->command, cmd->command_dma);
3817}
3818
3819static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3820 unsigned int request_idx, unsigned int numa_node)
3821{
3822 struct driver_data *dd = data;
3823 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3824 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3825
3826 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3827 &cmd->command_dma, GFP_KERNEL);
3828 if (!cmd->command)
3829 return -ENOMEM;
3830
3831 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3832
3833 /* Point the command headers at the command tables. */
3834 cmd->command_header = dd->port->command_list +
3835 (sizeof(struct mtip_cmd_hdr) * request_idx);
3836 cmd->command_header_dma = dd->port->command_list_dma +
3837 (sizeof(struct mtip_cmd_hdr) * request_idx);
3838
3839 if (host_cap_64)
3840 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3841
3842 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3843
3844 sg_init_table(cmd->sg, MTIP_MAX_SG);
3845 return 0;
3846}
3847
3848static struct blk_mq_ops mtip_mq_ops = {
3849 .queue_rq = mtip_queue_rq,
3850 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003851 .init_request = mtip_init_cmd,
3852 .exit_request = mtip_free_cmd,
3853};
3854
Sam Bradshaw88523a62011-08-30 08:34:26 -06003855/*
3856 * Block layer initialization function.
3857 *
3858 * This function is called once by the PCI layer for each P320
3859 * device that is connected to the system.
3860 *
3861 * @dd Pointer to the driver data structure.
3862 *
3863 * return value
3864 * 0 on success else an error code.
3865 */
Jens Axboe63166682011-09-27 21:27:43 -06003866static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003867{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003868 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003869 sector_t capacity;
3870 unsigned int index = 0;
3871 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003872 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003873
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003874 if (dd->disk)
3875 goto skip_create_disk; /* hw init done, before rebuild */
3876
Jens Axboeffc771b2014-05-09 09:42:02 -06003877 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003878 rv = -EINVAL;
3879 goto protocol_init_error;
3880 }
3881
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003882 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003883 if (dd->disk == NULL) {
3884 dev_err(&dd->pdev->dev,
3885 "Unable to allocate gendisk structure\n");
3886 rv = -EINVAL;
3887 goto alloc_disk_error;
3888 }
3889
3890 /* Generate the disk name, implemented same as in sd.c */
3891 do {
3892 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3893 goto ida_get_error;
3894
3895 spin_lock(&rssd_index_lock);
3896 rv = ida_get_new(&rssd_index_ida, &index);
3897 spin_unlock(&rssd_index_lock);
3898 } while (rv == -EAGAIN);
3899
3900 if (rv)
3901 goto ida_get_error;
3902
3903 rv = rssd_disk_name_format("rssd",
3904 index,
3905 dd->disk->disk_name,
3906 DISK_NAME_LEN);
3907 if (rv)
3908 goto disk_index_error;
3909
3910 dd->disk->driverfs_dev = &dd->pdev->dev;
3911 dd->disk->major = dd->major;
3912 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3913 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003914 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003915 dd->index = index;
3916
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003917 mtip_hw_debugfs_init(dd);
3918
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003919skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003920 memset(&dd->tags, 0, sizeof(dd->tags));
3921 dd->tags.ops = &mtip_mq_ops;
3922 dd->tags.nr_hw_queues = 1;
3923 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3924 dd->tags.reserved_tags = 1;
3925 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3926 dd->tags.numa_node = dd->numa_node;
3927 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3928 dd->tags.driver_data = dd;
3929
3930 rv = blk_mq_alloc_tag_set(&dd->tags);
3931 if (rv) {
3932 dev_err(&dd->pdev->dev,
3933 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003934 goto block_queue_alloc_init_error;
3935 }
3936
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003937 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003938 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003939 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003940 dev_err(&dd->pdev->dev,
3941 "Unable to allocate request queue\n");
3942 rv = -ENOMEM;
3943 goto block_queue_alloc_init_error;
3944 }
3945
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003946 dd->disk->queue = dd->queue;
3947 dd->queue->queuedata = dd;
3948
Jens Axboeffc771b2014-05-09 09:42:02 -06003949 /* Initialize the protocol layer. */
3950 wait_for_rebuild = mtip_hw_get_identify(dd);
3951 if (wait_for_rebuild < 0) {
3952 dev_err(&dd->pdev->dev,
3953 "Protocol layer initialization failed\n");
3954 rv = -EINVAL;
3955 goto init_hw_cmds_error;
3956 }
3957
3958 /*
3959 * if rebuild pending, start the service thread, and delay the block
3960 * queue creation and add_disk()
3961 */
3962 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3963 goto start_service_thread;
3964
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003965 /* Set device limits. */
3966 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003967 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003968 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3969 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003970 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3971 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003972 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003973 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003974
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003975 /*
3976 * write back cache is not supported in the device. FUA depends on
3977 * write back cache support, hence setting flush support to zero.
3978 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003979 blk_queue_flush(dd->queue, 0);
3980
Asai Thambi S P15283462013-01-11 14:41:34 +01003981 /* Signal trim support */
3982 if (dd->trim_supp == true) {
3983 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3984 dd->queue->limits.discard_granularity = 4096;
3985 blk_queue_max_discard_sectors(dd->queue,
3986 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3987 dd->queue->limits.discard_zeroes_data = 0;
3988 }
3989
Sam Bradshaw88523a62011-08-30 08:34:26 -06003990 /* Set the capacity of the device in 512 byte sectors. */
3991 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3992 dev_warn(&dd->pdev->dev,
3993 "Could not read drive capacity\n");
3994 rv = -EIO;
3995 goto read_capacity_error;
3996 }
3997 set_capacity(dd->disk, capacity);
3998
3999 /* Enable the block device and add it to /dev */
4000 add_disk(dd->disk);
4001
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004002 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004003 /*
4004 * Now that the disk is active, initialize any sysfs attributes
4005 * managed by the protocol layer.
4006 */
4007 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4008 if (kobj) {
4009 mtip_hw_sysfs_init(dd, kobj);
4010 kobject_put(kobj);
4011 }
4012
Asai Thambi S P45038362012-04-09 08:35:38 +02004013 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004014 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004015 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004016 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004017
4018start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004019 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004020 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07004021 dd, dd->numa_node, "%s",
4022 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004023
4024 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004025 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004026 dd->mtip_svc_handler = NULL;
4027 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004028 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004029 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004030 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004031 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4032 rv = wait_for_rebuild;
4033
Sam Bradshaw88523a62011-08-30 08:34:26 -06004034 return rv;
4035
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004036kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004037 bdput(dd->bdev);
4038 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004039
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004040 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004041 del_gendisk(dd->disk);
4042
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004043read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004044init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004045 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004046 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004047block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004048 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004049disk_index_error:
4050 spin_lock(&rssd_index_lock);
4051 ida_remove(&rssd_index_ida, index);
4052 spin_unlock(&rssd_index_lock);
4053
4054ida_get_error:
4055 put_disk(dd->disk);
4056
4057alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004058 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004059
4060protocol_init_error:
4061 return rv;
4062}
4063
4064/*
4065 * Block layer deinitialization function.
4066 *
4067 * Called by the PCI layer as each P320 device is removed.
4068 *
4069 * @dd Pointer to the driver data structure.
4070 *
4071 * return value
4072 * 0
4073 */
Jens Axboe63166682011-09-27 21:27:43 -06004074static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004075{
4076 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004077
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004078 if (!dd->sr) {
4079 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004080
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004081 if (dd->mtip_svc_handler) {
4082 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4083 wake_up_interruptible(&dd->port->svc_wait);
4084 kthread_stop(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004085 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004086
4087 /* Clean up the sysfs attributes, if created */
4088 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4089 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4090 if (kobj) {
4091 mtip_hw_sysfs_exit(dd, kobj);
4092 kobject_put(kobj);
4093 }
4094 }
Jens Axboeffc771b2014-05-09 09:42:02 -06004095
4096 mtip_standby_drive(dd);
4097
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004098 /*
4099 * Delete our gendisk structure. This also removes the device
4100 * from /dev
4101 */
4102 if (dd->bdev) {
4103 bdput(dd->bdev);
4104 dd->bdev = NULL;
4105 }
4106 if (dd->disk) {
Asai Thambi SP02b48262015-05-11 15:48:00 -07004107 del_gendisk(dd->disk);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004108 if (dd->disk->queue) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004109 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004110 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004111 dd->queue = NULL;
Asai Thambi SP02b48262015-05-11 15:48:00 -07004112 }
4113 put_disk(dd->disk);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004114 }
4115 dd->disk = NULL;
4116
4117 spin_lock(&rssd_index_lock);
4118 ida_remove(&rssd_index_ida, dd->index);
4119 spin_unlock(&rssd_index_lock);
4120 } else {
4121 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4122 dd->disk->disk_name);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004123 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004124
4125 /* De-initialize the protocol layer. */
4126 mtip_hw_exit(dd);
4127
4128 return 0;
4129}
4130
4131/*
4132 * Function called by the PCI layer when just before the
4133 * machine shuts down.
4134 *
4135 * If a protocol layer shutdown function is present it will be called
4136 * by this function.
4137 *
4138 * @dd Pointer to the driver data structure.
4139 *
4140 * return value
4141 * 0
4142 */
Jens Axboe63166682011-09-27 21:27:43 -06004143static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004144{
Jens Axboeffc771b2014-05-09 09:42:02 -06004145 mtip_hw_shutdown(dd);
4146
Sam Bradshaw88523a62011-08-30 08:34:26 -06004147 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304148 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304149 dev_info(&dd->pdev->dev,
4150 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304151
Asai Thambi SP02b48262015-05-11 15:48:00 -07004152 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304153 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304154 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004155 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004156 }
4157 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304158 dd->disk = NULL;
4159 dd->queue = NULL;
4160 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004161
4162 spin_lock(&rssd_index_lock);
4163 ida_remove(&rssd_index_ida, dd->index);
4164 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004165 return 0;
4166}
4167
Jens Axboe63166682011-09-27 21:27:43 -06004168static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004169{
4170 dev_info(&dd->pdev->dev,
4171 "Suspending %s ...\n", dd->disk->disk_name);
4172 mtip_hw_suspend(dd);
4173 return 0;
4174}
4175
Jens Axboe63166682011-09-27 21:27:43 -06004176static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004177{
4178 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4179 dd->disk->disk_name);
4180 mtip_hw_resume(dd);
4181 return 0;
4182}
4183
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004184static void drop_cpu(int cpu)
4185{
4186 cpu_use[cpu]--;
4187}
4188
4189static int get_least_used_cpu_on_node(int node)
4190{
4191 int cpu, least_used_cpu, least_cnt;
4192 const struct cpumask *node_mask;
4193
4194 node_mask = cpumask_of_node(node);
4195 least_used_cpu = cpumask_first(node_mask);
4196 least_cnt = cpu_use[least_used_cpu];
4197 cpu = least_used_cpu;
4198
4199 for_each_cpu(cpu, node_mask) {
4200 if (cpu_use[cpu] < least_cnt) {
4201 least_used_cpu = cpu;
4202 least_cnt = cpu_use[cpu];
4203 }
4204 }
4205 cpu_use[least_used_cpu]++;
4206 return least_used_cpu;
4207}
4208
4209/* Helper for selecting a node in round robin mode */
4210static inline int mtip_get_next_rr_node(void)
4211{
4212 static int next_node = -1;
4213
4214 if (next_node == -1) {
4215 next_node = first_online_node;
4216 return next_node;
4217 }
4218
4219 next_node = next_online_node(next_node);
4220 if (next_node == MAX_NUMNODES)
4221 next_node = first_online_node;
4222 return next_node;
4223}
4224
Fengguang Wu25bac122013-01-12 15:31:40 +08004225static DEFINE_HANDLER(0);
4226static DEFINE_HANDLER(1);
4227static DEFINE_HANDLER(2);
4228static DEFINE_HANDLER(3);
4229static DEFINE_HANDLER(4);
4230static DEFINE_HANDLER(5);
4231static DEFINE_HANDLER(6);
4232static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004233
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004234static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4235{
4236 int pos;
4237 unsigned short pcie_dev_ctrl;
4238
4239 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4240 if (pos) {
4241 pci_read_config_word(pdev,
4242 pos + PCI_EXP_DEVCTL,
4243 &pcie_dev_ctrl);
4244 if (pcie_dev_ctrl & (1 << 11) ||
4245 pcie_dev_ctrl & (1 << 4)) {
4246 dev_info(&dd->pdev->dev,
4247 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4248 pdev->vendor, pdev->device);
4249 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4250 PCI_EXP_DEVCTL_RELAX_EN);
4251 pci_write_config_word(pdev,
4252 pos + PCI_EXP_DEVCTL,
4253 pcie_dev_ctrl);
4254 }
4255 }
4256}
4257
4258static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4259{
4260 /*
4261 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4262 * device with device id 0x5aXX
4263 */
4264 if (pdev->bus && pdev->bus->self) {
4265 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4266 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4267 mtip_disable_link_opts(dd, pdev->bus->self);
4268 } else {
4269 /* Check further up the topology */
4270 struct pci_dev *parent_dev = pdev->bus->self;
4271 if (parent_dev->bus &&
4272 parent_dev->bus->parent &&
4273 parent_dev->bus->parent->self &&
4274 parent_dev->bus->parent->self->vendor ==
4275 PCI_VENDOR_ID_ATI &&
4276 (parent_dev->bus->parent->self->device &
4277 0xff00) == 0x5a00) {
4278 mtip_disable_link_opts(dd,
4279 parent_dev->bus->parent->self);
4280 }
4281 }
4282 }
4283}
4284
Sam Bradshaw88523a62011-08-30 08:34:26 -06004285/*
4286 * Called for each supported PCI device detected.
4287 *
4288 * This function allocates the private data structure, enables the
4289 * PCI device and then calls the block layer initialization function.
4290 *
4291 * return value
4292 * 0 on success else an error code.
4293 */
4294static int mtip_pci_probe(struct pci_dev *pdev,
4295 const struct pci_device_id *ent)
4296{
4297 int rv = 0;
4298 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004299 char cpu_list[256];
4300 const struct cpumask *node_mask;
4301 int cpu, i = 0, j = 0;
4302 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304303 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004304
4305 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004306 my_node = pcibus_to_node(pdev->bus);
4307 if (my_node != NUMA_NO_NODE) {
4308 if (!node_online(my_node))
4309 my_node = mtip_get_next_rr_node();
4310 } else {
4311 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4312 my_node = mtip_get_next_rr_node();
4313 }
4314 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4315 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004316 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004317
4318 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004319 if (dd == NULL) {
4320 dev_err(&pdev->dev,
4321 "Unable to allocate memory for driver data\n");
4322 return -ENOMEM;
4323 }
4324
Sam Bradshaw88523a62011-08-30 08:34:26 -06004325 /* Attach the private data to this PCI device. */
4326 pci_set_drvdata(pdev, dd);
4327
4328 rv = pcim_enable_device(pdev);
4329 if (rv < 0) {
4330 dev_err(&pdev->dev, "Unable to enable device\n");
4331 goto iomap_err;
4332 }
4333
4334 /* Map BAR5 to memory. */
4335 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4336 if (rv < 0) {
4337 dev_err(&pdev->dev, "Unable to map regions\n");
4338 goto iomap_err;
4339 }
4340
4341 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4342 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4343
4344 if (rv) {
4345 rv = pci_set_consistent_dma_mask(pdev,
4346 DMA_BIT_MASK(32));
4347 if (rv) {
4348 dev_warn(&pdev->dev,
4349 "64-bit DMA enable failed\n");
4350 goto setmask_err;
4351 }
4352 }
4353 }
4354
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004355 /* Copy the info we may need later into the private data structure. */
4356 dd->major = mtip_major;
4357 dd->instance = instance;
4358 dd->pdev = pdev;
4359 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004360
Asai Thambi S P0caff002013-04-03 19:56:21 +05304361 INIT_LIST_HEAD(&dd->online_list);
4362 INIT_LIST_HEAD(&dd->remove_list);
4363
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004364 memset(dd->workq_name, 0, 32);
4365 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4366
4367 dd->isr_workq = create_workqueue(dd->workq_name);
4368 if (!dd->isr_workq) {
4369 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004370 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004371 goto block_initialize_err;
4372 }
4373
4374 memset(cpu_list, 0, sizeof(cpu_list));
4375
4376 node_mask = cpumask_of_node(dd->numa_node);
4377 if (!cpumask_empty(node_mask)) {
4378 for_each_cpu(cpu, node_mask)
4379 {
4380 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4381 j = strlen(cpu_list);
4382 }
4383
4384 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4385 dd->numa_node,
4386 topology_physical_package_id(cpumask_first(node_mask)),
4387 nr_cpus_node(dd->numa_node),
4388 cpu_list);
4389 } else
4390 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4391
4392 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4393 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4394 cpu_to_node(dd->isr_binding), dd->isr_binding);
4395
4396 /* first worker context always runs in ISR */
4397 dd->work[0].cpu_binding = dd->isr_binding;
4398 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4399 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4400 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4401 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4402 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4403 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4404 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4405
4406 /* Log the bindings */
4407 for_each_present_cpu(cpu) {
4408 memset(cpu_list, 0, sizeof(cpu_list));
4409 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4410 if (dd->work[i].cpu_binding == cpu) {
4411 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4412 j = strlen(cpu_list);
4413 }
4414 }
4415 if (j)
4416 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4417 }
4418
4419 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4420 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4421 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4422 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4423 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4424 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4425 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4426 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4427
4428 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004429 rv = pci_enable_msi(pdev);
4430 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004431 dev_warn(&pdev->dev,
4432 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004433 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004434 }
4435
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004436 mtip_fix_ero_nosnoop(dd, pdev);
4437
Sam Bradshaw88523a62011-08-30 08:34:26 -06004438 /* Initialize the block layer. */
4439 rv = mtip_block_initialize(dd);
4440 if (rv < 0) {
4441 dev_err(&pdev->dev,
4442 "Unable to initialize block layer\n");
4443 goto block_initialize_err;
4444 }
4445
4446 /*
4447 * Increment the instance count so that each device has a unique
4448 * instance number.
4449 */
4450 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004451 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004452 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304453 else
4454 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304455
4456 /* Add to online list even if in ftl rebuild */
4457 spin_lock_irqsave(&dev_lock, flags);
4458 list_add(&dd->online_list, &online_list);
4459 spin_unlock_irqrestore(&dev_lock, flags);
4460
Sam Bradshaw88523a62011-08-30 08:34:26 -06004461 goto done;
4462
4463block_initialize_err:
4464 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004465
4466msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004467 if (dd->isr_workq) {
4468 flush_workqueue(dd->isr_workq);
4469 destroy_workqueue(dd->isr_workq);
4470 drop_cpu(dd->work[0].cpu_binding);
4471 drop_cpu(dd->work[1].cpu_binding);
4472 drop_cpu(dd->work[2].cpu_binding);
4473 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004474setmask_err:
4475 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4476
4477iomap_err:
4478 kfree(dd);
4479 pci_set_drvdata(pdev, NULL);
4480 return rv;
4481done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004482 return rv;
4483}
4484
4485/*
4486 * Called for each probed device when the device is removed or the
4487 * driver is unloaded.
4488 *
4489 * return value
4490 * None
4491 */
4492static void mtip_pci_remove(struct pci_dev *pdev)
4493{
4494 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004495 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004496
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004497 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004498
Asai Thambi S P0caff002013-04-03 19:56:21 +05304499 spin_lock_irqsave(&dev_lock, flags);
4500 list_del_init(&dd->online_list);
4501 list_add(&dd->remove_list, &removing_list);
4502 spin_unlock_irqrestore(&dev_lock, flags);
4503
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004504 mtip_check_surprise_removal(pdev);
4505 synchronize_irq(dd->pdev->irq);
4506
4507 /* Spin until workers are done */
4508 to = jiffies + msecs_to_jiffies(4000);
4509 do {
4510 msleep(20);
4511 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4512 time_before(jiffies, to));
4513
4514 if (atomic_read(&dd->irq_workers_active) != 0) {
4515 dev_warn(&dd->pdev->dev,
4516 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004517 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004518
4519 /* Clean up the block layer. */
4520 mtip_block_remove(dd);
4521
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004522 if (dd->isr_workq) {
4523 flush_workqueue(dd->isr_workq);
4524 destroy_workqueue(dd->isr_workq);
4525 drop_cpu(dd->work[0].cpu_binding);
4526 drop_cpu(dd->work[1].cpu_binding);
4527 drop_cpu(dd->work[2].cpu_binding);
4528 }
4529
Sam Bradshaw88523a62011-08-30 08:34:26 -06004530 pci_disable_msi(pdev);
4531
Asai Thambi S P0caff002013-04-03 19:56:21 +05304532 spin_lock_irqsave(&dev_lock, flags);
4533 list_del_init(&dd->remove_list);
4534 spin_unlock_irqrestore(&dev_lock, flags);
4535
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004536 if (!dd->sr)
4537 kfree(dd);
4538 else
4539 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4540
Sam Bradshaw88523a62011-08-30 08:34:26 -06004541 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004542 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004543}
4544
4545/*
4546 * Called for each probed device when the device is suspended.
4547 *
4548 * return value
4549 * 0 Success
4550 * <0 Error
4551 */
4552static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4553{
4554 int rv = 0;
4555 struct driver_data *dd = pci_get_drvdata(pdev);
4556
4557 if (!dd) {
4558 dev_err(&pdev->dev,
4559 "Driver private datastructure is NULL\n");
4560 return -EFAULT;
4561 }
4562
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004563 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004564
4565 /* Disable ports & interrupts then send standby immediate */
4566 rv = mtip_block_suspend(dd);
4567 if (rv < 0) {
4568 dev_err(&pdev->dev,
4569 "Failed to suspend controller\n");
4570 return rv;
4571 }
4572
4573 /*
4574 * Save the pci config space to pdev structure &
4575 * disable the device
4576 */
4577 pci_save_state(pdev);
4578 pci_disable_device(pdev);
4579
4580 /* Move to Low power state*/
4581 pci_set_power_state(pdev, PCI_D3hot);
4582
4583 return rv;
4584}
4585
4586/*
4587 * Called for each probed device when the device is resumed.
4588 *
4589 * return value
4590 * 0 Success
4591 * <0 Error
4592 */
4593static int mtip_pci_resume(struct pci_dev *pdev)
4594{
4595 int rv = 0;
4596 struct driver_data *dd;
4597
4598 dd = pci_get_drvdata(pdev);
4599 if (!dd) {
4600 dev_err(&pdev->dev,
4601 "Driver private datastructure is NULL\n");
4602 return -EFAULT;
4603 }
4604
4605 /* Move the device to active State */
4606 pci_set_power_state(pdev, PCI_D0);
4607
4608 /* Restore PCI configuration space */
4609 pci_restore_state(pdev);
4610
4611 /* Enable the PCI device*/
4612 rv = pcim_enable_device(pdev);
4613 if (rv < 0) {
4614 dev_err(&pdev->dev,
4615 "Failed to enable card during resume\n");
4616 goto err;
4617 }
4618 pci_set_master(pdev);
4619
4620 /*
4621 * Calls hbaReset, initPort, & startPort function
4622 * then enables interrupts
4623 */
4624 rv = mtip_block_resume(dd);
4625 if (rv < 0)
4626 dev_err(&pdev->dev, "Unable to resume\n");
4627
4628err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004629 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004630
4631 return rv;
4632}
4633
4634/*
4635 * Shutdown routine
4636 *
4637 * return value
4638 * None
4639 */
4640static void mtip_pci_shutdown(struct pci_dev *pdev)
4641{
4642 struct driver_data *dd = pci_get_drvdata(pdev);
4643 if (dd)
4644 mtip_block_shutdown(dd);
4645}
4646
Sam Bradshaw88523a62011-08-30 08:34:26 -06004647/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004648static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304649 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4650 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4651 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4652 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4653 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4654 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4655 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004656 { 0 }
4657};
4658
4659/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004660static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004661 .name = MTIP_DRV_NAME,
4662 .id_table = mtip_pci_tbl,
4663 .probe = mtip_pci_probe,
4664 .remove = mtip_pci_remove,
4665 .suspend = mtip_pci_suspend,
4666 .resume = mtip_pci_resume,
4667 .shutdown = mtip_pci_shutdown,
4668};
4669
4670MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4671
4672/*
4673 * Module initialization function.
4674 *
4675 * Called once when the module is loaded. This function allocates a major
4676 * block device number to the Cyclone devices and registers the PCI layer
4677 * of the driver.
4678 *
4679 * Return value
4680 * 0 on success else error code.
4681 */
4682static int __init mtip_init(void)
4683{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004684 int error;
4685
Asai Thambi S P45422e72012-09-05 22:03:56 +05304686 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004687
Asai Thambi S P0caff002013-04-03 19:56:21 +05304688 spin_lock_init(&dev_lock);
4689
4690 INIT_LIST_HEAD(&online_list);
4691 INIT_LIST_HEAD(&removing_list);
4692
Sam Bradshaw88523a62011-08-30 08:34:26 -06004693 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004694 error = register_blkdev(0, MTIP_DRV_NAME);
4695 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304696 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004697 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004698 return -EBUSY;
4699 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004700 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004701
Asai Thambi S P0caff002013-04-03 19:56:21 +05304702 dfs_parent = debugfs_create_dir("rssd", NULL);
4703 if (IS_ERR_OR_NULL(dfs_parent)) {
4704 pr_warn("Error creating debugfs parent\n");
4705 dfs_parent = NULL;
4706 }
4707 if (dfs_parent) {
4708 dfs_device_status = debugfs_create_file("device_status",
4709 S_IRUGO, dfs_parent, NULL,
4710 &mtip_device_status_fops);
4711 if (IS_ERR_OR_NULL(dfs_device_status)) {
4712 pr_err("Error creating device_status node\n");
4713 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004714 }
4715 }
4716
Sam Bradshaw88523a62011-08-30 08:34:26 -06004717 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004718 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004719 if (error) {
4720 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004721 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004722 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004723
4724 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004725}
4726
4727/*
4728 * Module de-initialization function.
4729 *
4730 * Called once when the module is unloaded. This function deallocates
4731 * the major block device number allocated by mtip_init() and
4732 * unregisters the PCI layer of the driver.
4733 *
4734 * Return value
4735 * none
4736 */
4737static void __exit mtip_exit(void)
4738{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004739 /* Release the allocated major block device number. */
4740 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4741
4742 /* Unregister the PCI driver. */
4743 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004744
4745 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004746}
4747
4748MODULE_AUTHOR("Micron Technology, Inc");
4749MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4750MODULE_LICENSE("GPL");
4751MODULE_VERSION(MTIP_DRV_VERSION);
4752
4753module_init(mtip_init);
4754module_exit(mtip_exit);