blob: 0c429b51e535b082e24e66b0b4d0f1837710ca03 [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);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700898 if (unlikely(port_stat == 0xFFFFFFFF)) {
899 mtip_check_surprise_removal(dd->pdev);
900 return IRQ_HANDLED;
901 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600902 writel(port_stat, port->mmio + PORT_IRQ_STAT);
903
904 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800905 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
906 do_irq_enable = 0;
907 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
908
909 /* Start at 1: group zero is always local? */
910 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
911 i++) {
912 twork = &dd->work[i];
913 twork->completed = readl(port->completed[i]);
914 if (twork->completed)
915 workers++;
916 }
917
918 atomic_set(&dd->irq_workers_active, workers);
919 if (workers) {
920 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
921 twork = &dd->work[i];
922 if (twork->completed)
923 queue_work_on(
924 twork->cpu_binding,
925 dd->isr_workq,
926 &twork->work);
927 }
928
929 if (likely(dd->work[0].completed))
930 mtip_workq_sdbfx(port, 0,
931 dd->work[0].completed);
932
933 } else {
934 /*
935 * Chip quirk: SDB interrupt but nothing
936 * to complete
937 */
938 do_irq_enable = 1;
939 }
940 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600941
942 if (unlikely(port_stat & PORT_IRQ_ERR)) {
943 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600944 /* don't proceed further */
945 return IRQ_HANDLED;
946 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200947 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200948 &dd->dd_flag))
949 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600950
951 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
952 }
953
954 if (unlikely(port_stat & PORT_IRQ_LEGACY))
955 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
956 }
957
958 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800959 if (unlikely(do_irq_enable))
960 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600961
962 return rv;
963}
964
965/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600966 * HBA interrupt subroutine.
967 *
968 * @irq IRQ number.
969 * @instance Pointer to the driver data structure.
970 *
971 * return value
972 * IRQ_HANDLED A HBA interrupt was pending and handled.
973 * IRQ_NONE This interrupt was not for the HBA.
974 */
975static irqreturn_t mtip_irq_handler(int irq, void *instance)
976{
977 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800978
979 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600980}
981
982static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
983{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600984 writel(1 << MTIP_TAG_BIT(tag),
985 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
986}
987
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200988static bool mtip_pause_ncq(struct mtip_port *port,
989 struct host_to_dev_fis *fis)
990{
991 struct host_to_dev_fis *reply;
992 unsigned long task_file_data;
993
994 reply = port->rxfis + RX_FIS_D2H_REG;
995 task_file_data = readl(port->mmio+PORT_TFDATA);
996
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530997 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200998 return false;
999
1000 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001001 port->ic_pause_timer = jiffies;
1002 return true;
1003 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1004 (fis->features == 0x03)) {
1005 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1006 port->ic_pause_timer = jiffies;
1007 return true;
1008 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1009 ((fis->command == 0xFC) &&
1010 (fis->features == 0x27 || fis->features == 0x72 ||
1011 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001012 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001013 /* Com reset after secure erase or lowlevel format */
1014 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001015 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001016 return false;
1017 }
1018
1019 return false;
1020}
1021
Sam Bradshaw88523a62011-08-30 08:34:26 -06001022/*
1023 * Wait for port to quiesce
1024 *
1025 * @port Pointer to port data structure
1026 * @timeout Max duration to wait (ms)
1027 *
1028 * return value
1029 * 0 Success
1030 * -EBUSY Commands still active
1031 */
1032static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1033{
1034 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001035 unsigned int n;
1036 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001037
Jens Axboe9acf03c2014-05-14 08:22:56 -06001038 blk_mq_stop_hw_queues(port->dd->queue);
1039
Sam Bradshaw88523a62011-08-30 08:34:26 -06001040 to = jiffies + msecs_to_jiffies(timeout);
1041 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001042 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1043 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001044 msleep(20);
1045 continue; /* svc thd is actively issuing commands */
1046 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001047
1048 msleep(100);
1049 if (mtip_check_surprise_removal(port->dd->pdev))
1050 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001051 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001052 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001053
Sam Bradshaw88523a62011-08-30 08:34:26 -06001054 /*
1055 * Ignore s_active bit 0 of array element 0.
1056 * This bit will always be set
1057 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001058 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001059 for (n = 1; n < port->dd->slot_groups; n++)
1060 active |= readl(port->s_active[n]);
1061
1062 if (!active)
1063 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001064 } while (time_before(jiffies, to));
1065
Jens Axboe9acf03c2014-05-14 08:22:56 -06001066 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001067 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001068err_fault:
1069 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1070 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001071}
1072
1073/*
1074 * Execute an internal command and wait for the completion.
1075 *
1076 * @port Pointer to the port data structure.
1077 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001078 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001079 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001080 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 * @opts Command header options, excluding the FIS length
1082 * and the number of PRD entries.
1083 * @timeout Time in ms to wait for the command to complete.
1084 *
1085 * return value
1086 * 0 Command completed successfully.
1087 * -EFAULT The buffer address is not correctly aligned.
1088 * -EBUSY Internal command or other IO in progress.
1089 * -EAGAIN Time out waiting for command to complete.
1090 */
1091static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001092 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001093 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001094 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001095 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001096 u32 opts,
1097 gfp_t atomic,
1098 unsigned long timeout)
1099{
1100 struct mtip_cmd_sg *command_sg;
1101 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001102 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301103 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001104 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001105
1106 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1107 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301108 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001109 return -EFAULT;
1110 }
1111
Jens Axboeffc771b2014-05-09 09:42:02 -06001112 int_cmd = mtip_get_int_command(dd);
1113
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001114 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001115
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001116 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1117 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1118
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301119 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001120
1121 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001122 if (fis->command != ATA_CMD_STANDBYNOW1) {
1123 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001124 if (mtip_quiesce_io(port,
1125 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301126 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001127 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001128 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001129 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001130 wake_up_interruptible(&port->svc_wait);
1131 return -EBUSY;
1132 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133 }
1134
1135 /* Set the completion function and data for the command. */
1136 int_cmd->comp_data = &wait;
1137 int_cmd->comp_func = mtip_completion;
1138
1139 } else {
1140 /* Clear completion - we're going to poll */
1141 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001142 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001143 }
1144
1145 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001146 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001147
1148 /* Populate the SG list */
1149 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001150 __force_bit2int cpu_to_le32(opts | fis_len);
1151 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001152 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1153
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001154 command_sg->info =
1155 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1156 command_sg->dba =
1157 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1158 command_sg->dba_upper =
1159 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001160
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001161 int_cmd->command_header->opts |=
1162 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001163 }
1164
1165 /* Populate the command header */
1166 int_cmd->command_header->byte_count = 0;
1167
1168 /* Issue the command to the hardware */
1169 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1170
Sam Bradshaw88523a62011-08-30 08:34:26 -06001171 if (atomic == GFP_KERNEL) {
1172 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001173 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001175 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301176 if (rv == -ERESTARTSYS) { /* interrupted */
1177 dev_err(&dd->pdev->dev,
1178 "Internal command [%02X] was interrupted after %lu ms\n",
1179 fis->command, timeout);
1180 rv = -EINTR;
1181 goto exec_ic_exit;
1182 } else if (rv == 0) /* timeout */
1183 dev_err(&dd->pdev->dev,
1184 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1185 fis->command, timeout);
1186 else
1187 dev_err(&dd->pdev->dev,
1188 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1189 fis->command, rv, timeout);
1190
1191 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001192 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301193 &dd->dd_flag)) {
1194 dev_err(&dd->pdev->dev,
1195 "Internal command [%02X] wait returned due to SR\n",
1196 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001197 rv = -ENXIO;
1198 goto exec_ic_exit;
1199 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301200 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001201 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301202 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001203 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001204 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301205 u32 hba_stat, port_stat;
1206
Sam Bradshaw88523a62011-08-30 08:34:26 -06001207 /* Spin for <timeout> checking if command still outstanding */
1208 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001209 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1210 & (1 << MTIP_TAG_INTERNAL))
1211 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301212 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001213 rv = -ENXIO;
1214 goto exec_ic_exit;
1215 }
1216 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001217 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301218 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001219 rv = -ENXIO;
1220 goto exec_ic_exit;
1221 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301222 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1223 if (!port_stat)
1224 continue;
1225
1226 if (port_stat & PORT_IRQ_ERR) {
1227 dev_err(&dd->pdev->dev,
1228 "Internal command [%02X] failed\n",
1229 fis->command);
1230 mtip_device_reset(dd);
1231 rv = -EIO;
1232 goto exec_ic_exit;
1233 } else {
1234 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1235 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1236 if (hba_stat)
1237 writel(hba_stat,
1238 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001239 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301240 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001241 }
1242 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001243
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001244 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1245 & (1 << MTIP_TAG_INTERNAL)) {
1246 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301247 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1248 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001249 rv = -EAGAIN;
1250 }
1251 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001252exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001253 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001254 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001255 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001256 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1257 /* NCQ paused */
1258 return rv;
1259 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001260 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001261
1262 return rv;
1263}
1264
1265/*
1266 * Byte-swap ATA ID strings.
1267 *
1268 * ATA identify data contains strings in byte-swapped 16-bit words.
1269 * They must be swapped (on all architectures) to be usable as C strings.
1270 * This function swaps bytes in-place.
1271 *
1272 * @buf The buffer location of the string
1273 * @len The number of bytes to swap
1274 *
1275 * return value
1276 * None
1277 */
1278static inline void ata_swap_string(u16 *buf, unsigned int len)
1279{
1280 int i;
1281 for (i = 0; i < (len/2); i++)
1282 be16_to_cpus(&buf[i]);
1283}
1284
Asai Thambi S P670a6412014-04-16 20:27:54 -07001285static void mtip_set_timeout(struct driver_data *dd,
1286 struct host_to_dev_fis *fis,
1287 unsigned int *timeout, u8 erasemode)
1288{
1289 switch (fis->command) {
1290 case ATA_CMD_DOWNLOAD_MICRO:
1291 *timeout = 120000; /* 2 minutes */
1292 break;
1293 case ATA_CMD_SEC_ERASE_UNIT:
1294 case 0xFC:
1295 if (erasemode)
1296 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1297 else
1298 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1299 break;
1300 case ATA_CMD_STANDBYNOW1:
1301 *timeout = 120000; /* 2 minutes */
1302 break;
1303 case 0xF7:
1304 case 0xFA:
1305 *timeout = 60000; /* 60 seconds */
1306 break;
1307 case ATA_CMD_SMART:
1308 *timeout = 15000; /* 15 seconds */
1309 break;
1310 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001311 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001312 break;
1313 }
1314}
1315
Sam Bradshaw88523a62011-08-30 08:34:26 -06001316/*
1317 * Request the device identity information.
1318 *
1319 * If a user space buffer is not specified, i.e. is NULL, the
1320 * identify information is still read from the drive and placed
1321 * into the identify data buffer (@e port->identify) in the
1322 * port data structure.
1323 * When the identify buffer contains valid identify information @e
1324 * port->identify_valid is non-zero.
1325 *
1326 * @port Pointer to the port structure.
1327 * @user_buffer A user space buffer where the identify data should be
1328 * copied.
1329 *
1330 * return value
1331 * 0 Command completed successfully.
1332 * -EFAULT An error occurred while coping data to the user buffer.
1333 * -1 Command failed.
1334 */
1335static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1336{
1337 int rv = 0;
1338 struct host_to_dev_fis fis;
1339
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001340 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001341 return -EFAULT;
1342
Sam Bradshaw88523a62011-08-30 08:34:26 -06001343 /* Build the FIS. */
1344 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1345 fis.type = 0x27;
1346 fis.opts = 1 << 7;
1347 fis.command = ATA_CMD_ID_ATA;
1348
1349 /* Set the identify information as invalid. */
1350 port->identify_valid = 0;
1351
1352 /* Clear the identify information. */
1353 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1354
1355 /* Execute the command. */
1356 if (mtip_exec_internal_command(port,
1357 &fis,
1358 5,
1359 port->identify_dma,
1360 sizeof(u16) * ATA_ID_WORDS,
1361 0,
1362 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001363 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001364 < 0) {
1365 rv = -1;
1366 goto out;
1367 }
1368
1369 /*
1370 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1371 * perform field-sensitive swapping on the string fields.
1372 * See the kernel use of ata_id_string() for proof of this.
1373 */
1374#ifdef __LITTLE_ENDIAN
1375 ata_swap_string(port->identify + 27, 40); /* model string*/
1376 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1377 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1378#else
1379 {
1380 int i;
1381 for (i = 0; i < ATA_ID_WORDS; i++)
1382 port->identify[i] = le16_to_cpu(port->identify[i]);
1383 }
1384#endif
1385
Sam Bradshaw26d58052013-10-03 10:18:05 -07001386 /* Check security locked state */
1387 if (port->identify[128] & 0x4)
1388 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1389 else
1390 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1391
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301392#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001393 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1394 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1395 port->dd->trim_supp = true;
1396 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301397#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001398 port->dd->trim_supp = false;
1399
Sam Bradshaw88523a62011-08-30 08:34:26 -06001400 /* Set the identify buffer as valid. */
1401 port->identify_valid = 1;
1402
1403 if (user_buffer) {
1404 if (copy_to_user(
1405 user_buffer,
1406 port->identify,
1407 ATA_ID_WORDS * sizeof(u16))) {
1408 rv = -EFAULT;
1409 goto out;
1410 }
1411 }
1412
1413out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001414 return rv;
1415}
1416
1417/*
1418 * Issue a standby immediate command to the device.
1419 *
1420 * @port Pointer to the port structure.
1421 *
1422 * return value
1423 * 0 Command was executed successfully.
1424 * -1 An error occurred while executing the command.
1425 */
1426static int mtip_standby_immediate(struct mtip_port *port)
1427{
1428 int rv;
1429 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001430 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001431 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001432
Sam Bradshaw88523a62011-08-30 08:34:26 -06001433 /* Build the FIS. */
1434 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1435 fis.type = 0x27;
1436 fis.opts = 1 << 7;
1437 fis.command = ATA_CMD_STANDBYNOW1;
1438
Asai Thambi S P670a6412014-04-16 20:27:54 -07001439 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1440
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001441 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001442 rv = mtip_exec_internal_command(port,
1443 &fis,
1444 5,
1445 0,
1446 0,
1447 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001448 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001449 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001450 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1451 jiffies_to_msecs(jiffies - start));
1452 if (rv)
1453 dev_warn(&port->dd->pdev->dev,
1454 "STANDBY IMMEDIATE command failed.\n");
1455
1456 return rv;
1457}
1458
1459/*
1460 * Issue a READ LOG EXT command to the device.
1461 *
1462 * @port pointer to the port structure.
1463 * @page page number to fetch
1464 * @buffer pointer to buffer
1465 * @buffer_dma dma address corresponding to @buffer
1466 * @sectors page length to fetch, in sectors
1467 *
1468 * return value
1469 * @rv return value from mtip_exec_internal_command()
1470 */
1471static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1472 dma_addr_t buffer_dma, unsigned int sectors)
1473{
1474 struct host_to_dev_fis fis;
1475
1476 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1477 fis.type = 0x27;
1478 fis.opts = 1 << 7;
1479 fis.command = ATA_CMD_READ_LOG_EXT;
1480 fis.sect_count = sectors & 0xFF;
1481 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1482 fis.lba_low = page;
1483 fis.lba_mid = 0;
1484 fis.device = ATA_DEVICE_OBS;
1485
1486 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1487
1488 return mtip_exec_internal_command(port,
1489 &fis,
1490 5,
1491 buffer_dma,
1492 sectors * ATA_SECT_SIZE,
1493 0,
1494 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001495 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001496}
1497
1498/*
1499 * Issue a SMART READ DATA command to the device.
1500 *
1501 * @port pointer to the port structure.
1502 * @buffer pointer to buffer
1503 * @buffer_dma dma address corresponding to @buffer
1504 *
1505 * return value
1506 * @rv return value from mtip_exec_internal_command()
1507 */
1508static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1509 dma_addr_t buffer_dma)
1510{
1511 struct host_to_dev_fis fis;
1512
1513 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1514 fis.type = 0x27;
1515 fis.opts = 1 << 7;
1516 fis.command = ATA_CMD_SMART;
1517 fis.features = 0xD0;
1518 fis.sect_count = 1;
1519 fis.lba_mid = 0x4F;
1520 fis.lba_hi = 0xC2;
1521 fis.device = ATA_DEVICE_OBS;
1522
1523 return mtip_exec_internal_command(port,
1524 &fis,
1525 5,
1526 buffer_dma,
1527 ATA_SECT_SIZE,
1528 0,
1529 GFP_ATOMIC,
1530 15000);
1531}
1532
1533/*
1534 * Get the value of a smart attribute
1535 *
1536 * @port pointer to the port structure
1537 * @id attribute number
1538 * @attrib pointer to return attrib information corresponding to @id
1539 *
1540 * return value
1541 * -EINVAL NULL buffer passed or unsupported attribute @id.
1542 * -EPERM Identify data not valid, SMART not supported or not enabled
1543 */
1544static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1545 struct smart_attr *attrib)
1546{
1547 int rv, i;
1548 struct smart_attr *pattr;
1549
1550 if (!attrib)
1551 return -EINVAL;
1552
1553 if (!port->identify_valid) {
1554 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1555 return -EPERM;
1556 }
1557 if (!(port->identify[82] & 0x1)) {
1558 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1559 return -EPERM;
1560 }
1561 if (!(port->identify[85] & 0x1)) {
1562 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1563 return -EPERM;
1564 }
1565
1566 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1567 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1568 if (rv) {
1569 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1570 return rv;
1571 }
1572
1573 pattr = (struct smart_attr *)(port->smart_buf + 2);
1574 for (i = 0; i < 29; i++, pattr++)
1575 if (pattr->attr_id == id) {
1576 memcpy(attrib, pattr, sizeof(struct smart_attr));
1577 break;
1578 }
1579
1580 if (i == 29) {
1581 dev_warn(&port->dd->pdev->dev,
1582 "Query for invalid SMART attribute ID\n");
1583 rv = -EINVAL;
1584 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001585
Sam Bradshaw88523a62011-08-30 08:34:26 -06001586 return rv;
1587}
1588
1589/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001590 * Trim unused sectors
1591 *
1592 * @dd pointer to driver_data structure
1593 * @lba starting lba
1594 * @len # of 512b sectors to trim
1595 *
1596 * return value
1597 * -ENOMEM Out of dma memory
1598 * -EINVAL Invalid parameters passed in, trim not supported
1599 * -EIO Error submitting trim request to hw
1600 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301601static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1602 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001603{
1604 int i, rv = 0;
1605 u64 tlba, tlen, sect_left;
1606 struct mtip_trim_entry *buf;
1607 dma_addr_t dma_addr;
1608 struct host_to_dev_fis fis;
1609
1610 if (!len || dd->trim_supp == false)
1611 return -EINVAL;
1612
1613 /* Trim request too big */
1614 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1615
1616 /* Trim request not aligned on 4k boundary */
1617 WARN_ON(len % 8 != 0);
1618
1619 /* Warn if vu_trim structure is too big */
1620 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1621
1622 /* Allocate a DMA buffer for the trim structure */
1623 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1624 GFP_KERNEL);
1625 if (!buf)
1626 return -ENOMEM;
1627 memset(buf, 0, ATA_SECT_SIZE);
1628
1629 for (i = 0, sect_left = len, tlba = lba;
1630 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1631 i++) {
1632 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1633 MTIP_MAX_TRIM_ENTRY_LEN :
1634 sect_left);
1635 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1636 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1637 tlba += tlen;
1638 sect_left -= tlen;
1639 }
1640 WARN_ON(sect_left != 0);
1641
1642 /* Build the fis */
1643 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1644 fis.type = 0x27;
1645 fis.opts = 1 << 7;
1646 fis.command = 0xfb;
1647 fis.features = 0x60;
1648 fis.sect_count = 1;
1649 fis.device = ATA_DEVICE_OBS;
1650
1651 if (mtip_exec_internal_command(dd->port,
1652 &fis,
1653 5,
1654 dma_addr,
1655 ATA_SECT_SIZE,
1656 0,
1657 GFP_KERNEL,
1658 MTIP_TRIM_TIMEOUT_MS) < 0)
1659 rv = -EIO;
1660
1661 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1662 return rv;
1663}
1664
1665/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001666 * Get the drive capacity.
1667 *
1668 * @dd Pointer to the device data structure.
1669 * @sectors Pointer to the variable that will receive the sector count.
1670 *
1671 * return value
1672 * 1 Capacity was returned successfully.
1673 * 0 The identify information is invalid.
1674 */
Jens Axboe63166682011-09-27 21:27:43 -06001675static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001676{
1677 struct mtip_port *port = dd->port;
1678 u64 total, raw0, raw1, raw2, raw3;
1679 raw0 = port->identify[100];
1680 raw1 = port->identify[101];
1681 raw2 = port->identify[102];
1682 raw3 = port->identify[103];
1683 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1684 *sectors = total;
1685 return (bool) !!port->identify_valid;
1686}
1687
1688/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001689 * Display the identify command data.
1690 *
1691 * @port Pointer to the port data structure.
1692 *
1693 * return value
1694 * None
1695 */
1696static void mtip_dump_identify(struct mtip_port *port)
1697{
1698 sector_t sectors;
1699 unsigned short revid;
1700 char cbuf[42];
1701
1702 if (!port->identify_valid)
1703 return;
1704
1705 strlcpy(cbuf, (char *)(port->identify+10), 21);
1706 dev_info(&port->dd->pdev->dev,
1707 "Serial No.: %s\n", cbuf);
1708
1709 strlcpy(cbuf, (char *)(port->identify+23), 9);
1710 dev_info(&port->dd->pdev->dev,
1711 "Firmware Ver.: %s\n", cbuf);
1712
1713 strlcpy(cbuf, (char *)(port->identify+27), 41);
1714 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1715
Sam Bradshaw26d58052013-10-03 10:18:05 -07001716 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1717 port->identify[128],
1718 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1719
Sam Bradshaw88523a62011-08-30 08:34:26 -06001720 if (mtip_hw_get_capacity(port->dd, &sectors))
1721 dev_info(&port->dd->pdev->dev,
1722 "Capacity: %llu sectors (%llu MB)\n",
1723 (u64)sectors,
1724 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1725
1726 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001727 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001728 case 0x1:
1729 strlcpy(cbuf, "A0", 3);
1730 break;
1731 case 0x3:
1732 strlcpy(cbuf, "A2", 3);
1733 break;
1734 default:
1735 strlcpy(cbuf, "?", 2);
1736 break;
1737 }
1738 dev_info(&port->dd->pdev->dev,
1739 "Card Type: %s\n", cbuf);
1740}
1741
1742/*
1743 * Map the commands scatter list into the command table.
1744 *
1745 * @command Pointer to the command.
1746 * @nents Number of scatter list entries.
1747 *
1748 * return value
1749 * None
1750 */
1751static inline void fill_command_sg(struct driver_data *dd,
1752 struct mtip_cmd *command,
1753 int nents)
1754{
1755 int n;
1756 unsigned int dma_len;
1757 struct mtip_cmd_sg *command_sg;
1758 struct scatterlist *sg = command->sg;
1759
1760 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1761
1762 for (n = 0; n < nents; n++) {
1763 dma_len = sg_dma_len(sg);
1764 if (dma_len > 0x400000)
1765 dev_err(&dd->pdev->dev,
1766 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001767 command_sg->info = __force_bit2int
1768 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1769 command_sg->dba = __force_bit2int
1770 cpu_to_le32(sg_dma_address(sg));
1771 command_sg->dba_upper = __force_bit2int
1772 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001773 command_sg++;
1774 sg++;
1775 }
1776}
1777
1778/*
1779 * @brief Execute a drive command.
1780 *
1781 * return value 0 The command completed successfully.
1782 * return value -1 An error occurred while executing the command.
1783 */
Jens Axboe63166682011-09-27 21:27:43 -06001784static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001785{
1786 struct host_to_dev_fis fis;
1787 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001788 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001789
Sam Bradshaw88523a62011-08-30 08:34:26 -06001790 /* Build the FIS. */
1791 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1792 fis.type = 0x27;
1793 fis.opts = 1 << 7;
1794 fis.command = command[0];
1795 fis.features = command[1];
1796 fis.sect_count = command[2];
1797 fis.sector = command[3];
1798 fis.cyl_low = command[4];
1799 fis.cyl_hi = command[5];
1800 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1801
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001802 mtip_set_timeout(port->dd, &fis, &to, 0);
1803
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001804 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 -06001805 __func__,
1806 command[0],
1807 command[1],
1808 command[2],
1809 command[3],
1810 command[4],
1811 command[5],
1812 command[6]);
1813
1814 /* Execute the command. */
1815 if (mtip_exec_internal_command(port,
1816 &fis,
1817 5,
1818 0,
1819 0,
1820 0,
1821 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001822 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001823 return -1;
1824 }
1825
1826 command[0] = reply->command; /* Status*/
1827 command[1] = reply->features; /* Error*/
1828 command[4] = reply->cyl_low;
1829 command[5] = reply->cyl_hi;
1830
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001831 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 -06001832 __func__,
1833 command[0],
1834 command[1],
1835 command[4],
1836 command[5]);
1837
Sam Bradshaw88523a62011-08-30 08:34:26 -06001838 return 0;
1839}
1840
1841/*
1842 * @brief Execute a drive command.
1843 *
1844 * @param port Pointer to the port data structure.
1845 * @param command Pointer to the user specified command parameters.
1846 * @param user_buffer Pointer to the user space buffer where read sector
1847 * data should be copied.
1848 *
1849 * return value 0 The command completed successfully.
1850 * return value -EFAULT An error occurred while copying the completion
1851 * data to the user space buffer.
1852 * return value -1 An error occurred while executing the command.
1853 */
Jens Axboe63166682011-09-27 21:27:43 -06001854static int exec_drive_command(struct mtip_port *port, u8 *command,
1855 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001856{
1857 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001858 struct host_to_dev_fis *reply;
1859 u8 *buf = NULL;
1860 dma_addr_t dma_addr = 0;
1861 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001862 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001863
1864 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001865 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001866 return -EFAULT;
1867
1868 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1869 ATA_SECT_SIZE * xfer_sz,
1870 &dma_addr,
1871 GFP_KERNEL);
1872 if (!buf) {
1873 dev_err(&port->dd->pdev->dev,
1874 "Memory allocation failed (%d bytes)\n",
1875 ATA_SECT_SIZE * xfer_sz);
1876 return -ENOMEM;
1877 }
1878 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1879 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001880
Sam Bradshaw88523a62011-08-30 08:34:26 -06001881 /* Build the FIS. */
1882 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001883 fis.type = 0x27;
1884 fis.opts = 1 << 7;
1885 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001886 fis.features = command[2];
1887 fis.sect_count = command[3];
1888 if (fis.command == ATA_CMD_SMART) {
1889 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001890 fis.cyl_low = 0x4F;
1891 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001892 }
1893
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001894 mtip_set_timeout(port->dd, &fis, &to, 0);
1895
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001896 if (xfer_sz)
1897 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1898 else
1899 reply = (port->rxfis + RX_FIS_D2H_REG);
1900
Sam Bradshaw88523a62011-08-30 08:34:26 -06001901 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001902 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001903 "feat %x, sectcnt %x\n",
1904 __func__,
1905 command[0],
1906 command[1],
1907 command[2],
1908 command[3]);
1909
Sam Bradshaw88523a62011-08-30 08:34:26 -06001910 /* Execute the command. */
1911 if (mtip_exec_internal_command(port,
1912 &fis,
1913 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001914 (xfer_sz ? dma_addr : 0),
1915 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001916 0,
1917 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001918 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001919 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001920 rv = -EFAULT;
1921 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922 }
1923
1924 /* Collect the completion status. */
1925 command[0] = reply->command; /* Status*/
1926 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001927 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928
1929 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001930 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001931 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001932 __func__,
1933 command[0],
1934 command[1],
1935 command[2]);
1936
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001937 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001938 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001939 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001940 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001941 rv = -EFAULT;
1942 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 }
1944 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001945exit_drive_command:
1946 if (buf)
1947 dmam_free_coherent(&port->dd->pdev->dev,
1948 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1949 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001950}
1951
1952/*
1953 * Indicates whether a command has a single sector payload.
1954 *
1955 * @command passed to the device to perform the certain event.
1956 * @features passed to the device to perform the certain event.
1957 *
1958 * return value
1959 * 1 command is one that always has a single sector payload,
1960 * regardless of the value in the Sector Count field.
1961 * 0 otherwise
1962 *
1963 */
1964static unsigned int implicit_sector(unsigned char command,
1965 unsigned char features)
1966{
1967 unsigned int rv = 0;
1968
1969 /* list of commands that have an implicit sector count of 1 */
1970 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001971 case ATA_CMD_SEC_SET_PASS:
1972 case ATA_CMD_SEC_UNLOCK:
1973 case ATA_CMD_SEC_ERASE_PREP:
1974 case ATA_CMD_SEC_ERASE_UNIT:
1975 case ATA_CMD_SEC_FREEZE_LOCK:
1976 case ATA_CMD_SEC_DISABLE_PASS:
1977 case ATA_CMD_PMP_READ:
1978 case ATA_CMD_PMP_WRITE:
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_SET_MAX:
1982 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001983 rv = 1;
1984 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001985 case ATA_CMD_SMART:
1986 if ((features == ATA_SMART_READ_VALUES) ||
1987 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001988 rv = 1;
1989 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001990 case ATA_CMD_CONF_OVERLAY:
1991 if ((features == ATA_DCO_IDENTIFY) ||
1992 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001993 rv = 1;
1994 break;
1995 }
1996 return rv;
1997}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001998
Sam Bradshaw88523a62011-08-30 08:34:26 -06001999/*
2000 * Executes a taskfile
2001 * See ide_taskfile_ioctl() for derivation
2002 */
2003static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002004 void __user *buf,
2005 ide_task_request_t *req_task,
2006 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002007{
2008 struct host_to_dev_fis fis;
2009 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010 u8 *outbuf = NULL;
2011 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002012 dma_addr_t outbuf_dma = 0;
2013 dma_addr_t inbuf_dma = 0;
2014 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002015 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002016 unsigned int taskin = 0;
2017 unsigned int taskout = 0;
2018 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002019 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002020 unsigned int force_single_sector;
2021 unsigned int transfer_size;
2022 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002023 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002024 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002025
2026 taskout = req_task->out_size;
2027 taskin = req_task->in_size;
2028 /* 130560 = 512 * 0xFF*/
2029 if (taskin > 130560 || taskout > 130560) {
2030 err = -EINVAL;
2031 goto abort;
2032 }
2033
2034 if (taskout) {
2035 outbuf = kzalloc(taskout, GFP_KERNEL);
2036 if (outbuf == NULL) {
2037 err = -ENOMEM;
2038 goto abort;
2039 }
2040 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2041 err = -EFAULT;
2042 goto abort;
2043 }
2044 outbuf_dma = pci_map_single(dd->pdev,
2045 outbuf,
2046 taskout,
2047 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002048 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002049 err = -ENOMEM;
2050 goto abort;
2051 }
2052 dma_buffer = outbuf_dma;
2053 }
2054
2055 if (taskin) {
2056 inbuf = kzalloc(taskin, GFP_KERNEL);
2057 if (inbuf == NULL) {
2058 err = -ENOMEM;
2059 goto abort;
2060 }
2061
2062 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2063 err = -EFAULT;
2064 goto abort;
2065 }
2066 inbuf_dma = pci_map_single(dd->pdev,
2067 inbuf,
2068 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002069 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002070 err = -ENOMEM;
2071 goto abort;
2072 }
2073 dma_buffer = inbuf_dma;
2074 }
2075
2076 /* only supports PIO and non-data commands from this ioctl. */
2077 switch (req_task->data_phase) {
2078 case TASKFILE_OUT:
2079 nsect = taskout / ATA_SECT_SIZE;
2080 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2081 break;
2082 case TASKFILE_IN:
2083 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2084 break;
2085 case TASKFILE_NO_DATA:
2086 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2087 break;
2088 default:
2089 err = -EINVAL;
2090 goto abort;
2091 }
2092
Sam Bradshaw88523a62011-08-30 08:34:26 -06002093 /* Build the FIS. */
2094 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2095
2096 fis.type = 0x27;
2097 fis.opts = 1 << 7;
2098 fis.command = req_task->io_ports[7];
2099 fis.features = req_task->io_ports[1];
2100 fis.sect_count = req_task->io_ports[2];
2101 fis.lba_low = req_task->io_ports[3];
2102 fis.lba_mid = req_task->io_ports[4];
2103 fis.lba_hi = req_task->io_ports[5];
2104 /* Clear the dev bit*/
2105 fis.device = req_task->io_ports[6] & ~0x10;
2106
2107 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2108 req_task->in_flags.all =
2109 IDE_TASKFILE_STD_IN_FLAGS |
2110 (IDE_HOB_STD_IN_FLAGS << 8);
2111 fis.lba_low_ex = req_task->hob_ports[3];
2112 fis.lba_mid_ex = req_task->hob_ports[4];
2113 fis.lba_hi_ex = req_task->hob_ports[5];
2114 fis.features_ex = req_task->hob_ports[1];
2115 fis.sect_cnt_ex = req_task->hob_ports[2];
2116
2117 } else {
2118 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2119 }
2120
2121 force_single_sector = implicit_sector(fis.command, fis.features);
2122
2123 if ((taskin || taskout) && (!fis.sect_count)) {
2124 if (nsect)
2125 fis.sect_count = nsect;
2126 else {
2127 if (!force_single_sector) {
2128 dev_warn(&dd->pdev->dev,
2129 "data movement but "
2130 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002131 err = -EINVAL;
2132 goto abort;
2133 }
2134 }
2135 }
2136
2137 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002138 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002139 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2140 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002141 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142 fis.command,
2143 fis.features,
2144 fis.sect_count,
2145 fis.lba_low,
2146 fis.lba_mid,
2147 fis.lba_hi,
2148 fis.device);
2149
Selvan Mani4453bc82012-09-27 14:36:43 +02002150 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002151 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2152 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002153 erasemode = 1;
2154 }
2155
2156 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002157
2158 /* Determine the correct transfer size.*/
2159 if (force_single_sector)
2160 transfer_size = ATA_SECT_SIZE;
2161 else
2162 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2163
2164 /* Execute the command.*/
2165 if (mtip_exec_internal_command(dd->port,
2166 &fis,
2167 5,
2168 dma_buffer,
2169 transfer_size,
2170 0,
2171 GFP_KERNEL,
2172 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002173 err = -EIO;
2174 goto abort;
2175 }
2176
2177 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2178
2179 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2180 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2181 req_task->io_ports[7] = reply->control;
2182 } else {
2183 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2184 req_task->io_ports[7] = reply->command;
2185 }
2186
2187 /* reclaim the DMA buffers.*/
2188 if (inbuf_dma)
2189 pci_unmap_single(dd->pdev, inbuf_dma,
2190 taskin, DMA_FROM_DEVICE);
2191 if (outbuf_dma)
2192 pci_unmap_single(dd->pdev, outbuf_dma,
2193 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002194 inbuf_dma = 0;
2195 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196
2197 /* return the ATA registers to the caller.*/
2198 req_task->io_ports[1] = reply->features;
2199 req_task->io_ports[2] = reply->sect_count;
2200 req_task->io_ports[3] = reply->lba_low;
2201 req_task->io_ports[4] = reply->lba_mid;
2202 req_task->io_ports[5] = reply->lba_hi;
2203 req_task->io_ports[6] = reply->device;
2204
2205 if (req_task->out_flags.all & 1) {
2206
2207 req_task->hob_ports[3] = reply->lba_low_ex;
2208 req_task->hob_ports[4] = reply->lba_mid_ex;
2209 req_task->hob_ports[5] = reply->lba_hi_ex;
2210 req_task->hob_ports[1] = reply->features_ex;
2211 req_task->hob_ports[2] = reply->sect_cnt_ex;
2212 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002213 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002214 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002215 "err %x, sect_cnt %x, lbalo %x,"
2216 "lbamid %x, lbahi %x, dev %x\n",
2217 __func__,
2218 req_task->io_ports[7],
2219 req_task->io_ports[1],
2220 req_task->io_ports[2],
2221 req_task->io_ports[3],
2222 req_task->io_ports[4],
2223 req_task->io_ports[5],
2224 req_task->io_ports[6]);
2225
Sam Bradshaw88523a62011-08-30 08:34:26 -06002226 if (taskout) {
2227 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2228 err = -EFAULT;
2229 goto abort;
2230 }
2231 }
2232 if (taskin) {
2233 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2234 err = -EFAULT;
2235 goto abort;
2236 }
2237 }
2238abort:
2239 if (inbuf_dma)
2240 pci_unmap_single(dd->pdev, inbuf_dma,
2241 taskin, DMA_FROM_DEVICE);
2242 if (outbuf_dma)
2243 pci_unmap_single(dd->pdev, outbuf_dma,
2244 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002245 kfree(outbuf);
2246 kfree(inbuf);
2247
2248 return err;
2249}
2250
2251/*
2252 * Handle IOCTL calls from the Block Layer.
2253 *
2254 * This function is called by the Block Layer when it receives an IOCTL
2255 * command that it does not understand. If the IOCTL command is not supported
2256 * this function returns -ENOTTY.
2257 *
2258 * @dd Pointer to the driver data structure.
2259 * @cmd IOCTL command passed from the Block Layer.
2260 * @arg IOCTL argument passed from the Block Layer.
2261 *
2262 * return value
2263 * 0 The IOCTL completed successfully.
2264 * -ENOTTY The specified command is not supported.
2265 * -EFAULT An error occurred copying data to a user space buffer.
2266 * -EIO An error occurred while executing the command.
2267 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002268static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2269 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002270{
2271 switch (cmd) {
2272 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002273 {
2274 if (copy_to_user((void __user *)arg, dd->port->identify,
2275 sizeof(u16) * ATA_ID_WORDS))
2276 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002277 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002278 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002279 case HDIO_DRIVE_CMD:
2280 {
2281 u8 drive_command[4];
2282
2283 /* Copy the user command info to our buffer. */
2284 if (copy_from_user(drive_command,
2285 (void __user *) arg,
2286 sizeof(drive_command)))
2287 return -EFAULT;
2288
2289 /* Execute the drive command. */
2290 if (exec_drive_command(dd->port,
2291 drive_command,
2292 (void __user *) (arg+4)))
2293 return -EIO;
2294
2295 /* Copy the status back to the users buffer. */
2296 if (copy_to_user((void __user *) arg,
2297 drive_command,
2298 sizeof(drive_command)))
2299 return -EFAULT;
2300
2301 break;
2302 }
2303 case HDIO_DRIVE_TASK:
2304 {
2305 u8 drive_command[7];
2306
2307 /* Copy the user command info to our buffer. */
2308 if (copy_from_user(drive_command,
2309 (void __user *) arg,
2310 sizeof(drive_command)))
2311 return -EFAULT;
2312
2313 /* Execute the drive command. */
2314 if (exec_drive_task(dd->port, drive_command))
2315 return -EIO;
2316
2317 /* Copy the status back to the users buffer. */
2318 if (copy_to_user((void __user *) arg,
2319 drive_command,
2320 sizeof(drive_command)))
2321 return -EFAULT;
2322
2323 break;
2324 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002325 case HDIO_DRIVE_TASKFILE: {
2326 ide_task_request_t req_task;
2327 int ret, outtotal;
2328
2329 if (copy_from_user(&req_task, (void __user *) arg,
2330 sizeof(req_task)))
2331 return -EFAULT;
2332
2333 outtotal = sizeof(req_task);
2334
2335 ret = exec_drive_taskfile(dd, (void __user *) arg,
2336 &req_task, outtotal);
2337
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002338 if (copy_to_user((void __user *) arg, &req_task,
2339 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002340 return -EFAULT;
2341
2342 return ret;
2343 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002344
2345 default:
2346 return -EINVAL;
2347 }
2348 return 0;
2349}
2350
2351/*
2352 * Submit an IO to the hw
2353 *
2354 * This function is called by the block layer to issue an io
2355 * to the device. Upon completion, the callback function will
2356 * be called with the data parameter passed as the callback data.
2357 *
2358 * @dd Pointer to the driver data structure.
2359 * @start First sector to read.
2360 * @nsect Number of sectors to read.
2361 * @nents Number of entries in scatter list for the read command.
2362 * @tag The tag of this read command.
2363 * @callback Pointer to the function that should be called
2364 * when the read completes.
2365 * @data Callback data passed to the callback function
2366 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002367 * @dir Direction (read or write)
2368 *
2369 * return value
2370 * None
2371 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002372static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2373 struct mtip_cmd *command, int nents,
2374 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002375{
2376 struct host_to_dev_fis *fis;
2377 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002378 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2379 u64 start = blk_rq_pos(rq);
2380 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002381
2382 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002383 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002384
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002385 prefetch(&port->flags);
2386
Sam Bradshaw88523a62011-08-30 08:34:26 -06002387 command->scatter_ents = nents;
2388
2389 /*
2390 * The number of retries for this command before it is
2391 * reported as a failure to the upper layers.
2392 */
2393 command->retries = MTIP_MAX_RETRIES;
2394
2395 /* Fill out fis */
2396 fis = command->command;
2397 fis->type = 0x27;
2398 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002399 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002400 fis->command = ATA_CMD_FPDMA_READ;
2401 else
2402 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002403 fis->lba_low = start & 0xFF;
2404 fis->lba_mid = (start >> 8) & 0xFF;
2405 fis->lba_hi = (start >> 16) & 0xFF;
2406 fis->lba_low_ex = (start >> 24) & 0xFF;
2407 fis->lba_mid_ex = (start >> 32) & 0xFF;
2408 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002410 fis->features = nsect & 0xFF;
2411 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002412 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002413 fis->sect_cnt_ex = 0;
2414 fis->control = 0;
2415 fis->res2 = 0;
2416 fis->res3 = 0;
2417 fill_command_sg(dd, command, nents);
2418
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002419 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002420 fis->device |= 1 << 7;
2421
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002423 command->command_header->opts =
2424 __force_bit2int cpu_to_le32(
2425 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002426 command->command_header->byte_count = 0;
2427
2428 /*
2429 * Set the completion function and data for the command
2430 * within this layer.
2431 */
2432 command->comp_data = dd;
2433 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002434 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435
2436 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002437 * To prevent this command from being issued
2438 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002439 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002440 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002441 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002442 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002443 return;
2444 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002445
2446 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002447 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002448}
2449
2450/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002451 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002452 *
2453 * @dev Pointer to the device structure, passed by the kernrel.
2454 * @attr Pointer to the device_attribute structure passed by the kernel.
2455 * @buf Pointer to the char buffer that will receive the stats info.
2456 *
2457 * return value
2458 * The size, in bytes, of the data copied into buf.
2459 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002460static ssize_t mtip_hw_show_status(struct device *dev,
2461 struct device_attribute *attr,
2462 char *buf)
2463{
2464 struct driver_data *dd = dev_to_disk(dev)->private_data;
2465 int size = 0;
2466
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002467 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002468 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002469 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002470 size += sprintf(buf, "%s", "write_protect\n");
2471 else
2472 size += sprintf(buf, "%s", "online\n");
2473
2474 return size;
2475}
2476
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002477static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002478
Asai Thambi S P0caff002013-04-03 19:56:21 +05302479/* debugsfs entries */
2480
2481static ssize_t show_device_status(struct device_driver *drv, char *buf)
2482{
2483 int size = 0;
2484 struct driver_data *dd, *tmp;
2485 unsigned long flags;
2486 char id_buf[42];
2487 u16 status = 0;
2488
2489 spin_lock_irqsave(&dev_lock, flags);
2490 size += sprintf(&buf[size], "Devices Present:\n");
2491 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002492 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302493 if (dd->port &&
2494 dd->port->identify &&
2495 dd->port->identify_valid) {
2496 strlcpy(id_buf,
2497 (char *) (dd->port->identify + 10), 21);
2498 status = *(dd->port->identify + 141);
2499 } else {
2500 memset(id_buf, 0, 42);
2501 status = 0;
2502 }
2503
2504 if (dd->port &&
2505 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2506 size += sprintf(&buf[size],
2507 " device %s %s (ftl rebuild %d %%)\n",
2508 dev_name(&dd->pdev->dev),
2509 id_buf,
2510 status);
2511 } else {
2512 size += sprintf(&buf[size],
2513 " device %s %s\n",
2514 dev_name(&dd->pdev->dev),
2515 id_buf);
2516 }
2517 }
2518 }
2519
2520 size += sprintf(&buf[size], "Devices Being Removed:\n");
2521 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002522 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302523 if (dd->port &&
2524 dd->port->identify &&
2525 dd->port->identify_valid) {
2526 strlcpy(id_buf,
2527 (char *) (dd->port->identify+10), 21);
2528 status = *(dd->port->identify + 141);
2529 } else {
2530 memset(id_buf, 0, 42);
2531 status = 0;
2532 }
2533
2534 if (dd->port &&
2535 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2536 size += sprintf(&buf[size],
2537 " device %s %s (ftl rebuild %d %%)\n",
2538 dev_name(&dd->pdev->dev),
2539 id_buf,
2540 status);
2541 } else {
2542 size += sprintf(&buf[size],
2543 " device %s %s\n",
2544 dev_name(&dd->pdev->dev),
2545 id_buf);
2546 }
2547 }
2548 }
2549 spin_unlock_irqrestore(&dev_lock, flags);
2550
2551 return size;
2552}
2553
2554static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2555 size_t len, loff_t *offset)
2556{
David Milburnc8afd0d2013-05-23 16:23:45 -05002557 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302558 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002559 char *buf;
2560 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561
2562 if (!len || *offset)
2563 return 0;
2564
David Milburnc8afd0d2013-05-23 16:23:45 -05002565 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2566 if (!buf) {
2567 dev_err(&dd->pdev->dev,
2568 "Memory allocation: status buffer\n");
2569 return -ENOMEM;
2570 }
2571
Asai Thambi S P0caff002013-04-03 19:56:21 +05302572 size += show_device_status(NULL, buf);
2573
2574 *offset = size <= len ? size : len;
2575 size = copy_to_user(ubuf, buf, *offset);
2576 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002577 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302578
David Milburnc8afd0d2013-05-23 16:23:45 -05002579 kfree(buf);
2580 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302581}
2582
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002583static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2584 size_t len, loff_t *offset)
2585{
2586 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002587 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002588 u32 group_allocated;
2589 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002590 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002591
2592 if (!len || size)
2593 return 0;
2594
David Milburnc8afd0d2013-05-23 16:23:45 -05002595 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2596 if (!buf) {
2597 dev_err(&dd->pdev->dev,
2598 "Memory allocation: register buffer\n");
2599 return -ENOMEM;
2600 }
2601
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002602 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2603
2604 for (n = dd->slot_groups-1; n >= 0; n--)
2605 size += sprintf(&buf[size], "%08X ",
2606 readl(dd->port->s_active[n]));
2607
2608 size += sprintf(&buf[size], "]\n");
2609 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2610
2611 for (n = dd->slot_groups-1; n >= 0; n--)
2612 size += sprintf(&buf[size], "%08X ",
2613 readl(dd->port->cmd_issue[n]));
2614
2615 size += sprintf(&buf[size], "]\n");
2616 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2617
2618 for (n = dd->slot_groups-1; n >= 0; n--)
2619 size += sprintf(&buf[size], "%08X ",
2620 readl(dd->port->completed[n]));
2621
2622 size += sprintf(&buf[size], "]\n");
2623 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2624 readl(dd->port->mmio + PORT_IRQ_STAT));
2625 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2626 readl(dd->mmio + HOST_IRQ_STAT));
2627 size += sprintf(&buf[size], "\n");
2628
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002629 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2630
2631 for (n = dd->slot_groups-1; n >= 0; n--) {
2632 if (sizeof(long) > sizeof(u32))
2633 group_allocated =
2634 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2635 else
2636 group_allocated = dd->port->cmds_to_issue[n];
2637 size += sprintf(&buf[size], "%08X ", group_allocated);
2638 }
2639 size += sprintf(&buf[size], "]\n");
2640
2641 *offset = size <= len ? size : len;
2642 size = copy_to_user(ubuf, buf, *offset);
2643 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002644 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002645
David Milburnc8afd0d2013-05-23 16:23:45 -05002646 kfree(buf);
2647 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002648}
2649
2650static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2651 size_t len, loff_t *offset)
2652{
2653 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002654 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002655 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002656 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002657
2658 if (!len || size)
2659 return 0;
2660
David Milburnc8afd0d2013-05-23 16:23:45 -05002661 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2662 if (!buf) {
2663 dev_err(&dd->pdev->dev,
2664 "Memory allocation: flag buffer\n");
2665 return -ENOMEM;
2666 }
2667
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002668 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2669 dd->port->flags);
2670 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2671 dd->dd_flag);
2672
2673 *offset = size <= len ? size : len;
2674 size = copy_to_user(ubuf, buf, *offset);
2675 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002676 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002677
David Milburnc8afd0d2013-05-23 16:23:45 -05002678 kfree(buf);
2679 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002680}
2681
Asai Thambi S P0caff002013-04-03 19:56:21 +05302682static const struct file_operations mtip_device_status_fops = {
2683 .owner = THIS_MODULE,
2684 .open = simple_open,
2685 .read = mtip_hw_read_device_status,
2686 .llseek = no_llseek,
2687};
2688
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002689static const struct file_operations mtip_regs_fops = {
2690 .owner = THIS_MODULE,
2691 .open = simple_open,
2692 .read = mtip_hw_read_registers,
2693 .llseek = no_llseek,
2694};
2695
2696static const struct file_operations mtip_flags_fops = {
2697 .owner = THIS_MODULE,
2698 .open = simple_open,
2699 .read = mtip_hw_read_flags,
2700 .llseek = no_llseek,
2701};
2702
Sam Bradshaw88523a62011-08-30 08:34:26 -06002703/*
2704 * Create the sysfs related attributes.
2705 *
2706 * @dd Pointer to the driver data structure.
2707 * @kobj Pointer to the kobj for the block device.
2708 *
2709 * return value
2710 * 0 Operation completed successfully.
2711 * -EINVAL Invalid parameter.
2712 */
Jens Axboe63166682011-09-27 21:27:43 -06002713static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002714{
2715 if (!kobj || !dd)
2716 return -EINVAL;
2717
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002718 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2719 dev_warn(&dd->pdev->dev,
2720 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002721 return 0;
2722}
2723
2724/*
2725 * Remove the sysfs related attributes.
2726 *
2727 * @dd Pointer to the driver data structure.
2728 * @kobj Pointer to the kobj for the block device.
2729 *
2730 * return value
2731 * 0 Operation completed successfully.
2732 * -EINVAL Invalid parameter.
2733 */
Jens Axboe63166682011-09-27 21:27:43 -06002734static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002735{
2736 if (!kobj || !dd)
2737 return -EINVAL;
2738
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002739 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002740
2741 return 0;
2742}
2743
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002744static int mtip_hw_debugfs_init(struct driver_data *dd)
2745{
2746 if (!dfs_parent)
2747 return -1;
2748
2749 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2750 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2751 dev_warn(&dd->pdev->dev,
2752 "Error creating node %s under debugfs\n",
2753 dd->disk->disk_name);
2754 dd->dfs_node = NULL;
2755 return -1;
2756 }
2757
2758 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2759 &mtip_flags_fops);
2760 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2761 &mtip_regs_fops);
2762
2763 return 0;
2764}
2765
2766static void mtip_hw_debugfs_exit(struct driver_data *dd)
2767{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002768 if (dd->dfs_node)
2769 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002770}
2771
Sam Bradshaw88523a62011-08-30 08:34:26 -06002772/*
2773 * Perform any init/resume time hardware setup
2774 *
2775 * @dd Pointer to the driver data structure.
2776 *
2777 * return value
2778 * None
2779 */
2780static inline void hba_setup(struct driver_data *dd)
2781{
2782 u32 hwdata;
2783 hwdata = readl(dd->mmio + HOST_HSORG);
2784
2785 /* interrupt bug workaround: use only 1 IS bit.*/
2786 writel(hwdata |
2787 HSORG_DISABLE_SLOTGRP_INTR |
2788 HSORG_DISABLE_SLOTGRP_PXIS,
2789 dd->mmio + HOST_HSORG);
2790}
2791
Asai Thambi S P2077d942013-04-29 21:19:49 +02002792static int mtip_device_unaligned_constrained(struct driver_data *dd)
2793{
2794 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2795}
2796
Sam Bradshaw88523a62011-08-30 08:34:26 -06002797/*
2798 * Detect the details of the product, and store anything needed
2799 * into the driver data structure. This includes product type and
2800 * version and number of slot groups.
2801 *
2802 * @dd Pointer to the driver data structure.
2803 *
2804 * return value
2805 * None
2806 */
2807static void mtip_detect_product(struct driver_data *dd)
2808{
2809 u32 hwdata;
2810 unsigned int rev, slotgroups;
2811
2812 /*
2813 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2814 * info register:
2815 * [15:8] hardware/software interface rev#
2816 * [ 3] asic-style interface
2817 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2818 */
2819 hwdata = readl(dd->mmio + HOST_HSORG);
2820
2821 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2822 dd->slot_groups = 1;
2823
2824 if (hwdata & 0x8) {
2825 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2826 rev = (hwdata & HSORG_HWREV) >> 8;
2827 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2828 dev_info(&dd->pdev->dev,
2829 "ASIC-FPGA design, HS rev 0x%x, "
2830 "%i slot groups [%i slots]\n",
2831 rev,
2832 slotgroups,
2833 slotgroups * 32);
2834
2835 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2836 dev_warn(&dd->pdev->dev,
2837 "Warning: driver only supports "
2838 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2839 slotgroups = MTIP_MAX_SLOT_GROUPS;
2840 }
2841 dd->slot_groups = slotgroups;
2842 return;
2843 }
2844
2845 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2846}
2847
2848/*
2849 * Blocking wait for FTL rebuild to complete
2850 *
2851 * @dd Pointer to the DRIVER_DATA structure.
2852 *
2853 * return value
2854 * 0 FTL rebuild completed successfully
2855 * -EFAULT FTL rebuild error/timeout/interruption
2856 */
2857static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2858{
2859 unsigned long timeout, cnt = 0, start;
2860
2861 dev_warn(&dd->pdev->dev,
2862 "FTL rebuild in progress. Polling for completion.\n");
2863
2864 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002865 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2866
2867 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002868 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002869 &dd->dd_flag)))
2870 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002871 if (mtip_check_surprise_removal(dd->pdev))
2872 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002873
Sam Bradshaw88523a62011-08-30 08:34:26 -06002874 if (mtip_get_identify(dd->port, NULL) < 0)
2875 return -EFAULT;
2876
2877 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2878 MTIP_FTL_REBUILD_MAGIC) {
2879 ssleep(1);
2880 /* Print message every 3 minutes */
2881 if (cnt++ >= 180) {
2882 dev_warn(&dd->pdev->dev,
2883 "FTL rebuild in progress (%d secs).\n",
2884 jiffies_to_msecs(jiffies - start) / 1000);
2885 cnt = 0;
2886 }
2887 } else {
2888 dev_warn(&dd->pdev->dev,
2889 "FTL rebuild complete (%d secs).\n",
2890 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002891 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002892 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002893 }
2894 ssleep(10);
2895 } while (time_before(jiffies, timeout));
2896
2897 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002898 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002899 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2900 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002901 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002902}
2903
2904/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002905 * service thread to issue queued commands
2906 *
2907 * @data Pointer to the driver data structure.
2908 *
2909 * return value
2910 * 0
2911 */
2912
2913static int mtip_service_thread(void *data)
2914{
2915 struct driver_data *dd = (struct driver_data *)data;
2916 unsigned long slot, slot_start, slot_wrap;
2917 unsigned int num_cmd_slots = dd->slot_groups * 32;
2918 struct mtip_port *port = dd->port;
2919
2920 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002921 if (kthread_should_stop() ||
2922 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2923 goto st_out;
2924 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2925
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002926 /*
2927 * the condition is to check neither an internal command is
2928 * is in progress nor error handling is active
2929 */
2930 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002931 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002932
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002933 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2934
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002935 if (kthread_should_stop() ||
2936 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2937 goto st_out;
2938
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002939 /* If I am an orphan, start self cleanup */
2940 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002941 break;
2942
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002943 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002944 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002945 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002946
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002947restart_eh:
2948 /* Demux bits: start with error handling */
2949 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2950 mtip_handle_tfe(dd);
2951 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2952 }
2953
2954 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2955 goto restart_eh;
2956
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002957 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002958 slot = 1;
2959 /* used to restrict the loop to one iteration */
2960 slot_start = num_cmd_slots;
2961 slot_wrap = 0;
2962 while (1) {
2963 slot = find_next_bit(port->cmds_to_issue,
2964 num_cmd_slots, slot);
2965 if (slot_wrap == 1) {
2966 if ((slot_start >= slot) ||
2967 (slot >= num_cmd_slots))
2968 break;
2969 }
2970 if (unlikely(slot_start == num_cmd_slots))
2971 slot_start = slot;
2972
2973 if (unlikely(slot == num_cmd_slots)) {
2974 slot = 1;
2975 slot_wrap = 1;
2976 continue;
2977 }
2978
2979 /* Issue the command to the hardware */
2980 mtip_issue_ncq_command(port, slot);
2981
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002982 clear_bit(slot, port->cmds_to_issue);
2983 }
2984
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002985 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002986 }
2987
2988 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002989 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002990 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002991 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002992 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002993 }
2994 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002995
2996 /* wait for pci remove to exit */
2997 while (1) {
2998 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
2999 break;
3000 msleep_interruptible(1000);
3001 if (kthread_should_stop())
3002 goto st_out;
3003 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003004st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003005 return 0;
3006}
3007
3008/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003009 * DMA region teardown
3010 *
3011 * @dd Pointer to driver_data structure
3012 *
3013 * return value
3014 * None
3015 */
3016static void mtip_dma_free(struct driver_data *dd)
3017{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003018 struct mtip_port *port = dd->port;
3019
3020 if (port->block1)
3021 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3022 port->block1, port->block1_dma);
3023
3024 if (port->command_list) {
3025 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3026 port->command_list, port->command_list_dma);
3027 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003028}
3029
3030/*
3031 * DMA region setup
3032 *
3033 * @dd Pointer to driver_data structure
3034 *
3035 * return value
3036 * -ENOMEM Not enough free DMA region space to initialize driver
3037 */
3038static int mtip_dma_alloc(struct driver_data *dd)
3039{
3040 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003041
3042 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3043 port->block1 =
3044 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3045 &port->block1_dma, GFP_KERNEL);
3046 if (!port->block1)
3047 return -ENOMEM;
3048 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3049
3050 /* Allocate dma memory for command list */
3051 port->command_list =
3052 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3053 &port->command_list_dma, GFP_KERNEL);
3054 if (!port->command_list) {
3055 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3056 port->block1, port->block1_dma);
3057 port->block1 = NULL;
3058 port->block1_dma = 0;
3059 return -ENOMEM;
3060 }
3061 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3062
3063 /* Setup all pointers into first DMA region */
3064 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3065 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3066 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3067 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3068 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3069 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3070 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3071 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3072
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003073 return 0;
3074}
3075
Jens Axboeffc771b2014-05-09 09:42:02 -06003076static int mtip_hw_get_identify(struct driver_data *dd)
3077{
3078 struct smart_attr attr242;
3079 unsigned char *buf;
3080 int rv;
3081
3082 if (mtip_get_identify(dd->port, NULL) < 0)
3083 return -EFAULT;
3084
3085 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3086 MTIP_FTL_REBUILD_MAGIC) {
3087 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3088 return MTIP_FTL_REBUILD_MAGIC;
3089 }
3090 mtip_dump_identify(dd->port);
3091
3092 /* check write protect, over temp and rebuild statuses */
3093 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3094 dd->port->log_buf,
3095 dd->port->log_buf_dma, 1);
3096 if (rv) {
3097 dev_warn(&dd->pdev->dev,
3098 "Error in READ LOG EXT (10h) command\n");
3099 /* non-critical error, don't fail the load */
3100 } else {
3101 buf = (unsigned char *)dd->port->log_buf;
3102 if (buf[259] & 0x1) {
3103 dev_info(&dd->pdev->dev,
3104 "Write protect bit is set.\n");
3105 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3106 }
3107 if (buf[288] == 0xF7) {
3108 dev_info(&dd->pdev->dev,
3109 "Exceeded Tmax, drive in thermal shutdown.\n");
3110 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3111 }
3112 if (buf[288] == 0xBF) {
3113 dev_info(&dd->pdev->dev,
3114 "Drive indicates rebuild has failed.\n");
3115 /* TODO */
3116 }
3117 }
3118
3119 /* get write protect progess */
3120 memset(&attr242, 0, sizeof(struct smart_attr));
3121 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3122 dev_warn(&dd->pdev->dev,
3123 "Unable to check write protect progress\n");
3124 else
3125 dev_info(&dd->pdev->dev,
3126 "Write protect progress: %u%% (%u blocks)\n",
3127 attr242.cur, le32_to_cpu(attr242.data));
3128
3129 return rv;
3130}
3131
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003132/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003133 * Called once for each card.
3134 *
3135 * @dd Pointer to the driver data structure.
3136 *
3137 * return value
3138 * 0 on success, else an error code.
3139 */
Jens Axboe63166682011-09-27 21:27:43 -06003140static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003141{
3142 int i;
3143 int rv;
3144 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003145 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003146
3147 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3148
3149 mtip_detect_product(dd);
3150 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3151 rv = -EIO;
3152 goto out1;
3153 }
3154 num_command_slots = dd->slot_groups * 32;
3155
3156 hba_setup(dd);
3157
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003158 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3159 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003160 if (!dd->port) {
3161 dev_err(&dd->pdev->dev,
3162 "Memory allocation: port structure\n");
3163 return -ENOMEM;
3164 }
3165
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003166 /* Continue workqueue setup */
3167 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3168 dd->work[i].port = dd->port;
3169
Asai Thambi S P2077d942013-04-29 21:19:49 +02003170 /* Enable unaligned IO constraints for some devices */
3171 if (mtip_device_unaligned_constrained(dd))
3172 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3173 else
3174 dd->unal_qdepth = 0;
3175
Asai Thambi S P2077d942013-04-29 21:19:49 +02003176 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003177
3178 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003179 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3180 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003181
3182 /* Set the port mmio base address. */
3183 dd->port->mmio = dd->mmio + PORT_OFFSET;
3184 dd->port->dd = dd;
3185
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003186 /* DMA allocations */
3187 rv = mtip_dma_alloc(dd);
3188 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003189 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003190
3191 /* Setup the pointers to the extended s_active and CI registers. */
3192 for (i = 0; i < dd->slot_groups; i++) {
3193 dd->port->s_active[i] =
3194 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3195 dd->port->cmd_issue[i] =
3196 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3197 dd->port->completed[i] =
3198 dd->port->mmio + i*0x80 + PORT_SDBV;
3199 }
3200
Asai Thambi S P45038362012-04-09 08:35:38 +02003201 timetaken = jiffies;
3202 timeout = jiffies + msecs_to_jiffies(30000);
3203 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3204 time_before(jiffies, timeout)) {
3205 mdelay(100);
3206 }
3207 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3208 timetaken = jiffies - timetaken;
3209 dev_warn(&dd->pdev->dev,
3210 "Surprise removal detected at %u ms\n",
3211 jiffies_to_msecs(timetaken));
3212 rv = -ENODEV;
3213 goto out2 ;
3214 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003215 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003216 timetaken = jiffies - timetaken;
3217 dev_warn(&dd->pdev->dev,
3218 "Removal detected at %u ms\n",
3219 jiffies_to_msecs(timetaken));
3220 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003221 goto out2;
3222 }
3223
Asai Thambi S P45038362012-04-09 08:35:38 +02003224 /* Conditionally reset the HBA. */
3225 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3226 if (mtip_hba_reset(dd) < 0) {
3227 dev_err(&dd->pdev->dev,
3228 "Card did not reset within timeout\n");
3229 rv = -EIO;
3230 goto out2;
3231 }
3232 } else {
3233 /* Clear any pending interrupts on the HBA */
3234 writel(readl(dd->mmio + HOST_IRQ_STAT),
3235 dd->mmio + HOST_IRQ_STAT);
3236 }
3237
Sam Bradshaw88523a62011-08-30 08:34:26 -06003238 mtip_init_port(dd->port);
3239 mtip_start_port(dd->port);
3240
3241 /* Setup the ISR and enable interrupts. */
3242 rv = devm_request_irq(&dd->pdev->dev,
3243 dd->pdev->irq,
3244 mtip_irq_handler,
3245 IRQF_SHARED,
3246 dev_driver_string(&dd->pdev->dev),
3247 dd);
3248
3249 if (rv) {
3250 dev_err(&dd->pdev->dev,
3251 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3252 goto out2;
3253 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003254 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003255
3256 /* Enable interrupts on the HBA. */
3257 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3258 dd->mmio + HOST_CTL);
3259
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003260 init_waitqueue_head(&dd->port->svc_wait);
3261
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003262 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003263 rv = -EFAULT;
3264 goto out3;
3265 }
3266
Sam Bradshaw88523a62011-08-30 08:34:26 -06003267 return rv;
3268
3269out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003270 /* Disable interrupts on the HBA. */
3271 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3272 dd->mmio + HOST_CTL);
3273
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003274 /* Release the IRQ. */
3275 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003276 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3277
3278out2:
3279 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003280 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003281
Sam Bradshaw88523a62011-08-30 08:34:26 -06003282out1:
3283 /* Free the memory allocated for the for structure. */
3284 kfree(dd->port);
3285
3286 return rv;
3287}
3288
Jens Axboeffc771b2014-05-09 09:42:02 -06003289static void mtip_standby_drive(struct driver_data *dd)
3290{
3291 if (dd->sr)
3292 return;
3293
3294 /*
3295 * Send standby immediate (E0h) to the drive so that it
3296 * saves its state.
3297 */
3298 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3299 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3300 if (mtip_standby_immediate(dd->port))
3301 dev_warn(&dd->pdev->dev,
3302 "STANDBY IMMEDIATE failed\n");
3303}
3304
Sam Bradshaw88523a62011-08-30 08:34:26 -06003305/*
3306 * Called to deinitialize an interface.
3307 *
3308 * @dd Pointer to the driver data structure.
3309 *
3310 * return value
3311 * 0
3312 */
Jens Axboe63166682011-09-27 21:27:43 -06003313static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003314{
3315 /*
3316 * Send standby immediate (E0h) to the drive so that it
3317 * saves its state.
3318 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003319 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003320 /* de-initialize the port. */
3321 mtip_deinit_port(dd->port);
3322
3323 /* Disable interrupts on the HBA. */
3324 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3325 dd->mmio + HOST_CTL);
3326 }
3327
Sam Bradshaw88523a62011-08-30 08:34:26 -06003328 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003329 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003330 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003331 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003332
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003333 /* Free dma regions */
3334 mtip_dma_free(dd);
3335
Sam Bradshaw88523a62011-08-30 08:34:26 -06003336 /* Free the memory allocated for the for structure. */
3337 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003338 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003339
3340 return 0;
3341}
3342
3343/*
3344 * Issue a Standby Immediate command to the device.
3345 *
3346 * This function is called by the Block Layer just before the
3347 * system powers off during a shutdown.
3348 *
3349 * @dd Pointer to the driver data structure.
3350 *
3351 * return value
3352 * 0
3353 */
Jens Axboe63166682011-09-27 21:27:43 -06003354static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003355{
3356 /*
3357 * Send standby immediate (E0h) to the drive so that it
3358 * saves its state.
3359 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003360 if (!dd->sr && dd->port)
3361 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003362
3363 return 0;
3364}
3365
3366/*
3367 * Suspend function
3368 *
3369 * This function is called by the Block Layer just before the
3370 * system hibernates.
3371 *
3372 * @dd Pointer to the driver data structure.
3373 *
3374 * return value
3375 * 0 Suspend was successful
3376 * -EFAULT Suspend was not successful
3377 */
Jens Axboe63166682011-09-27 21:27:43 -06003378static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003379{
3380 /*
3381 * Send standby immediate (E0h) to the drive
3382 * so that it saves its state.
3383 */
3384 if (mtip_standby_immediate(dd->port) != 0) {
3385 dev_err(&dd->pdev->dev,
3386 "Failed standby-immediate command\n");
3387 return -EFAULT;
3388 }
3389
3390 /* Disable interrupts on the HBA.*/
3391 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3392 dd->mmio + HOST_CTL);
3393 mtip_deinit_port(dd->port);
3394
3395 return 0;
3396}
3397
3398/*
3399 * Resume function
3400 *
3401 * This function is called by the Block Layer as the
3402 * system resumes.
3403 *
3404 * @dd Pointer to the driver data structure.
3405 *
3406 * return value
3407 * 0 Resume was successful
3408 * -EFAULT Resume was not successful
3409 */
Jens Axboe63166682011-09-27 21:27:43 -06003410static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003411{
3412 /* Perform any needed hardware setup steps */
3413 hba_setup(dd);
3414
3415 /* Reset the HBA */
3416 if (mtip_hba_reset(dd) != 0) {
3417 dev_err(&dd->pdev->dev,
3418 "Unable to reset the HBA\n");
3419 return -EFAULT;
3420 }
3421
3422 /*
3423 * Enable the port, DMA engine, and FIS reception specific
3424 * h/w in controller.
3425 */
3426 mtip_init_port(dd->port);
3427 mtip_start_port(dd->port);
3428
3429 /* Enable interrupts on the HBA.*/
3430 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3431 dd->mmio + HOST_CTL);
3432
3433 return 0;
3434}
3435
3436/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003437 * Helper function for reusing disk name
3438 * upon hot insertion.
3439 */
3440static int rssd_disk_name_format(char *prefix,
3441 int index,
3442 char *buf,
3443 int buflen)
3444{
3445 const int base = 'z' - 'a' + 1;
3446 char *begin = buf + strlen(prefix);
3447 char *end = buf + buflen;
3448 char *p;
3449 int unit;
3450
3451 p = end - 1;
3452 *p = '\0';
3453 unit = base;
3454 do {
3455 if (p == begin)
3456 return -EINVAL;
3457 *--p = 'a' + (index % unit);
3458 index = (index / unit) - 1;
3459 } while (index >= 0);
3460
3461 memmove(begin, p, end - p);
3462 memcpy(buf, prefix, strlen(prefix));
3463
3464 return 0;
3465}
3466
3467/*
3468 * Block layer IOCTL handler.
3469 *
3470 * @dev Pointer to the block_device structure.
3471 * @mode ignored
3472 * @cmd IOCTL command passed from the user application.
3473 * @arg Argument passed from the user application.
3474 *
3475 * return value
3476 * 0 IOCTL completed successfully.
3477 * -ENOTTY IOCTL not supported or invalid driver data
3478 * structure pointer.
3479 */
3480static int mtip_block_ioctl(struct block_device *dev,
3481 fmode_t mode,
3482 unsigned cmd,
3483 unsigned long arg)
3484{
3485 struct driver_data *dd = dev->bd_disk->private_data;
3486
3487 if (!capable(CAP_SYS_ADMIN))
3488 return -EACCES;
3489
3490 if (!dd)
3491 return -ENOTTY;
3492
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003493 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003494 return -ENOTTY;
3495
Sam Bradshaw88523a62011-08-30 08:34:26 -06003496 switch (cmd) {
3497 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003498 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003499 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003500 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003501 }
3502}
3503
Jens Axboe16d02c02011-09-27 15:50:01 -06003504#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003505/*
3506 * Block layer compat IOCTL handler.
3507 *
3508 * @dev Pointer to the block_device structure.
3509 * @mode ignored
3510 * @cmd IOCTL command passed from the user application.
3511 * @arg Argument passed from the user application.
3512 *
3513 * return value
3514 * 0 IOCTL completed successfully.
3515 * -ENOTTY IOCTL not supported or invalid driver data
3516 * structure pointer.
3517 */
3518static int mtip_block_compat_ioctl(struct block_device *dev,
3519 fmode_t mode,
3520 unsigned cmd,
3521 unsigned long arg)
3522{
3523 struct driver_data *dd = dev->bd_disk->private_data;
3524
3525 if (!capable(CAP_SYS_ADMIN))
3526 return -EACCES;
3527
3528 if (!dd)
3529 return -ENOTTY;
3530
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003531 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003532 return -ENOTTY;
3533
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534 switch (cmd) {
3535 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003536 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003537 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003538 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003539 ide_task_request_t req_task;
3540 int compat_tasksize, outtotal, ret;
3541
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003542 compat_tasksize =
3543 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003544
3545 compat_req_task =
3546 (struct mtip_compat_ide_task_request_s __user *) arg;
3547
3548 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003549 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003550 return -EFAULT;
3551
3552 if (get_user(req_task.out_size, &compat_req_task->out_size))
3553 return -EFAULT;
3554
3555 if (get_user(req_task.in_size, &compat_req_task->in_size))
3556 return -EFAULT;
3557
3558 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3559
3560 ret = exec_drive_taskfile(dd, (void __user *) arg,
3561 &req_task, outtotal);
3562
3563 if (copy_to_user((void __user *) arg, &req_task,
3564 compat_tasksize -
3565 (2 * sizeof(compat_long_t))))
3566 return -EFAULT;
3567
3568 if (put_user(req_task.out_size, &compat_req_task->out_size))
3569 return -EFAULT;
3570
3571 if (put_user(req_task.in_size, &compat_req_task->in_size))
3572 return -EFAULT;
3573
3574 return ret;
3575 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003576 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003577 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003578 }
3579}
Jens Axboe16d02c02011-09-27 15:50:01 -06003580#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003581
3582/*
3583 * Obtain the geometry of the device.
3584 *
3585 * You may think that this function is obsolete, but some applications,
3586 * fdisk for example still used CHS values. This function describes the
3587 * device as having 224 heads and 56 sectors per cylinder. These values are
3588 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3589 * partition is described in terms of a start and end cylinder this means
3590 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3591 * affects performance.
3592 *
3593 * @dev Pointer to the block_device strucutre.
3594 * @geo Pointer to a hd_geometry structure.
3595 *
3596 * return value
3597 * 0 Operation completed successfully.
3598 * -ENOTTY An error occurred while reading the drive capacity.
3599 */
3600static int mtip_block_getgeo(struct block_device *dev,
3601 struct hd_geometry *geo)
3602{
3603 struct driver_data *dd = dev->bd_disk->private_data;
3604 sector_t capacity;
3605
3606 if (!dd)
3607 return -ENOTTY;
3608
3609 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3610 dev_warn(&dd->pdev->dev,
3611 "Could not get drive capacity.\n");
3612 return -ENOTTY;
3613 }
3614
3615 geo->heads = 224;
3616 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003617 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003618 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003619 return 0;
3620}
3621
3622/*
3623 * Block device operation function.
3624 *
3625 * This structure contains pointers to the functions required by the block
3626 * layer.
3627 */
3628static const struct block_device_operations mtip_block_ops = {
3629 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003630#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003631 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003632#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003633 .getgeo = mtip_block_getgeo,
3634 .owner = THIS_MODULE
3635};
3636
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003637static inline bool is_se_active(struct driver_data *dd)
3638{
3639 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3640 if (dd->port->ic_pause_timer) {
3641 unsigned long to = dd->port->ic_pause_timer +
3642 msecs_to_jiffies(1000);
3643 if (time_after(jiffies, to)) {
3644 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3645 &dd->port->flags);
3646 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3647 dd->port->ic_pause_timer = 0;
3648 wake_up_interruptible(&dd->port->svc_wait);
3649 return false;
3650 }
3651 }
3652 return true;
3653 }
3654 return false;
3655}
3656
Sam Bradshaw88523a62011-08-30 08:34:26 -06003657/*
3658 * Block layer make request function.
3659 *
3660 * This function is called by the kernel to process a BIO for
3661 * the P320 device.
3662 *
3663 * @queue Pointer to the request queue. Unused other than to obtain
3664 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003665 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003666 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003667 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003668static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003669{
Jens Axboeffc771b2014-05-09 09:42:02 -06003670 struct driver_data *dd = hctx->queue->queuedata;
3671 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3672 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003673
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003674 if (is_se_active(dd))
3675 return -ENODATA;
3676
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003677 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3678 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3679 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003680 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003681 }
3682 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003683 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003684 }
3685 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3686 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003687 rq_data_dir(rq))) {
3688 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003689 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003690 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3691 return -ENODATA;
3692 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3693 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003694 }
3695
Jens Axboeffc771b2014-05-09 09:42:02 -06003696 if (rq->cmd_flags & REQ_DISCARD) {
3697 int err;
3698
3699 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003700 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003701 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003702 }
3703
Jens Axboeffc771b2014-05-09 09:42:02 -06003704 /* Create the scatter list for this request. */
3705 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003706
Jens Axboeffc771b2014-05-09 09:42:02 -06003707 /* Issue the read/write. */
3708 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3709 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003710}
3711
Jens Axboeffc771b2014-05-09 09:42:02 -06003712static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3713 struct request *rq)
3714{
3715 struct driver_data *dd = hctx->queue->queuedata;
3716 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3717
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003718 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003719 return false;
3720
3721 /*
3722 * If unaligned depth must be limited on this controller, mark it
3723 * as unaligned if the IO isn't on a 4k boundary (start of length).
3724 */
3725 if (blk_rq_sectors(rq) <= 64) {
3726 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3727 cmd->unaligned = 1;
3728 }
3729
3730 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3731 return true;
3732
3733 return false;
3734}
3735
Jens Axboe74c45052014-10-29 11:14:52 -06003736static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3737 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003738{
Jens Axboe74c45052014-10-29 11:14:52 -06003739 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003740 int ret;
3741
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003742 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003743 return BLK_MQ_RQ_QUEUE_BUSY;
3744
Christoph Hellwige2490072014-09-13 16:40:09 -07003745 blk_mq_start_request(rq);
3746
Jens Axboeffc771b2014-05-09 09:42:02 -06003747 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003748 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003749 return BLK_MQ_RQ_QUEUE_OK;
3750
3751 rq->errors = ret;
3752 return BLK_MQ_RQ_QUEUE_ERROR;
3753}
3754
3755static void mtip_free_cmd(void *data, struct request *rq,
3756 unsigned int hctx_idx, unsigned int request_idx)
3757{
3758 struct driver_data *dd = data;
3759 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3760
3761 if (!cmd->command)
3762 return;
3763
3764 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3765 cmd->command, cmd->command_dma);
3766}
3767
3768static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3769 unsigned int request_idx, unsigned int numa_node)
3770{
3771 struct driver_data *dd = data;
3772 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3773 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3774
3775 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3776 &cmd->command_dma, GFP_KERNEL);
3777 if (!cmd->command)
3778 return -ENOMEM;
3779
3780 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3781
3782 /* Point the command headers at the command tables. */
3783 cmd->command_header = dd->port->command_list +
3784 (sizeof(struct mtip_cmd_hdr) * request_idx);
3785 cmd->command_header_dma = dd->port->command_list_dma +
3786 (sizeof(struct mtip_cmd_hdr) * request_idx);
3787
3788 if (host_cap_64)
3789 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3790
3791 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3792
3793 sg_init_table(cmd->sg, MTIP_MAX_SG);
3794 return 0;
3795}
3796
3797static struct blk_mq_ops mtip_mq_ops = {
3798 .queue_rq = mtip_queue_rq,
3799 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003800 .init_request = mtip_init_cmd,
3801 .exit_request = mtip_free_cmd,
3802};
3803
Sam Bradshaw88523a62011-08-30 08:34:26 -06003804/*
3805 * Block layer initialization function.
3806 *
3807 * This function is called once by the PCI layer for each P320
3808 * device that is connected to the system.
3809 *
3810 * @dd Pointer to the driver data structure.
3811 *
3812 * return value
3813 * 0 on success else an error code.
3814 */
Jens Axboe63166682011-09-27 21:27:43 -06003815static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003816{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003817 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003818 sector_t capacity;
3819 unsigned int index = 0;
3820 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003821 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003822
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003823 if (dd->disk)
3824 goto skip_create_disk; /* hw init done, before rebuild */
3825
Jens Axboeffc771b2014-05-09 09:42:02 -06003826 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003827 rv = -EINVAL;
3828 goto protocol_init_error;
3829 }
3830
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003831 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003832 if (dd->disk == NULL) {
3833 dev_err(&dd->pdev->dev,
3834 "Unable to allocate gendisk structure\n");
3835 rv = -EINVAL;
3836 goto alloc_disk_error;
3837 }
3838
3839 /* Generate the disk name, implemented same as in sd.c */
3840 do {
3841 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3842 goto ida_get_error;
3843
3844 spin_lock(&rssd_index_lock);
3845 rv = ida_get_new(&rssd_index_ida, &index);
3846 spin_unlock(&rssd_index_lock);
3847 } while (rv == -EAGAIN);
3848
3849 if (rv)
3850 goto ida_get_error;
3851
3852 rv = rssd_disk_name_format("rssd",
3853 index,
3854 dd->disk->disk_name,
3855 DISK_NAME_LEN);
3856 if (rv)
3857 goto disk_index_error;
3858
3859 dd->disk->driverfs_dev = &dd->pdev->dev;
3860 dd->disk->major = dd->major;
3861 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3862 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003863 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003864 dd->index = index;
3865
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003866 mtip_hw_debugfs_init(dd);
3867
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003868skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003869 memset(&dd->tags, 0, sizeof(dd->tags));
3870 dd->tags.ops = &mtip_mq_ops;
3871 dd->tags.nr_hw_queues = 1;
3872 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3873 dd->tags.reserved_tags = 1;
3874 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3875 dd->tags.numa_node = dd->numa_node;
3876 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3877 dd->tags.driver_data = dd;
3878
3879 rv = blk_mq_alloc_tag_set(&dd->tags);
3880 if (rv) {
3881 dev_err(&dd->pdev->dev,
3882 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003883 goto block_queue_alloc_init_error;
3884 }
3885
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003886 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003887 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003888 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003889 dev_err(&dd->pdev->dev,
3890 "Unable to allocate request queue\n");
3891 rv = -ENOMEM;
3892 goto block_queue_alloc_init_error;
3893 }
3894
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003895 dd->disk->queue = dd->queue;
3896 dd->queue->queuedata = dd;
3897
Jens Axboeffc771b2014-05-09 09:42:02 -06003898 /* Initialize the protocol layer. */
3899 wait_for_rebuild = mtip_hw_get_identify(dd);
3900 if (wait_for_rebuild < 0) {
3901 dev_err(&dd->pdev->dev,
3902 "Protocol layer initialization failed\n");
3903 rv = -EINVAL;
3904 goto init_hw_cmds_error;
3905 }
3906
3907 /*
3908 * if rebuild pending, start the service thread, and delay the block
3909 * queue creation and add_disk()
3910 */
3911 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3912 goto start_service_thread;
3913
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003914 /* Set device limits. */
3915 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003916 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003917 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3918 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003919 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3920 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003921 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003922 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003923
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003924 /*
3925 * write back cache is not supported in the device. FUA depends on
3926 * write back cache support, hence setting flush support to zero.
3927 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003928 blk_queue_flush(dd->queue, 0);
3929
Asai Thambi S P15283462013-01-11 14:41:34 +01003930 /* Signal trim support */
3931 if (dd->trim_supp == true) {
3932 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3933 dd->queue->limits.discard_granularity = 4096;
3934 blk_queue_max_discard_sectors(dd->queue,
3935 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3936 dd->queue->limits.discard_zeroes_data = 0;
3937 }
3938
Sam Bradshaw88523a62011-08-30 08:34:26 -06003939 /* Set the capacity of the device in 512 byte sectors. */
3940 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3941 dev_warn(&dd->pdev->dev,
3942 "Could not read drive capacity\n");
3943 rv = -EIO;
3944 goto read_capacity_error;
3945 }
3946 set_capacity(dd->disk, capacity);
3947
3948 /* Enable the block device and add it to /dev */
3949 add_disk(dd->disk);
3950
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003951 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003952 /*
3953 * Now that the disk is active, initialize any sysfs attributes
3954 * managed by the protocol layer.
3955 */
3956 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3957 if (kobj) {
3958 mtip_hw_sysfs_init(dd, kobj);
3959 kobject_put(kobj);
3960 }
3961
Asai Thambi S P45038362012-04-09 08:35:38 +02003962 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003963 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003964 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003965 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003966
3967start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003968 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003969 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07003970 dd, dd->numa_node, "%s",
3971 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003972
3973 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003974 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003975 dd->mtip_svc_handler = NULL;
3976 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003977 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003978 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003979 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003980 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3981 rv = wait_for_rebuild;
3982
Sam Bradshaw88523a62011-08-30 08:34:26 -06003983 return rv;
3984
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003985kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003986 bdput(dd->bdev);
3987 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003988
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003989 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003990 del_gendisk(dd->disk);
3991
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003992read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003993init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003994 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06003995 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003996block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003997 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003998disk_index_error:
3999 spin_lock(&rssd_index_lock);
4000 ida_remove(&rssd_index_ida, index);
4001 spin_unlock(&rssd_index_lock);
4002
4003ida_get_error:
4004 put_disk(dd->disk);
4005
4006alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004007 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004008
4009protocol_init_error:
4010 return rv;
4011}
4012
4013/*
4014 * Block layer deinitialization function.
4015 *
4016 * Called by the PCI layer as each P320 device is removed.
4017 *
4018 * @dd Pointer to the driver data structure.
4019 *
4020 * return value
4021 * 0
4022 */
Jens Axboe63166682011-09-27 21:27:43 -06004023static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004024{
4025 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004026
Asai Thambi SP2132a542015-05-11 15:53:18 -07004027 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004028
Asai Thambi SP2132a542015-05-11 15:53:18 -07004029 if (dd->mtip_svc_handler) {
4030 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4031 wake_up_interruptible(&dd->port->svc_wait);
4032 kthread_stop(dd->mtip_svc_handler);
4033 }
4034
4035 /* Clean up the sysfs attributes, if created */
4036 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4037 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4038 if (kobj) {
4039 mtip_hw_sysfs_exit(dd, kobj);
4040 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004041 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004042 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004043
Asai Thambi SP2132a542015-05-11 15:53:18 -07004044 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004045 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004046 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004047 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4048 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004049
4050 /*
4051 * Delete our gendisk structure. This also removes the device
4052 * from /dev
4053 */
4054 if (dd->bdev) {
4055 bdput(dd->bdev);
4056 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004057 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004058 if (dd->disk) {
4059 del_gendisk(dd->disk);
4060 if (dd->disk->queue) {
4061 blk_cleanup_queue(dd->queue);
4062 blk_mq_free_tag_set(&dd->tags);
4063 dd->queue = NULL;
4064 }
4065 put_disk(dd->disk);
4066 }
4067 dd->disk = NULL;
4068
4069 spin_lock(&rssd_index_lock);
4070 ida_remove(&rssd_index_ida, dd->index);
4071 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004072
4073 /* De-initialize the protocol layer. */
4074 mtip_hw_exit(dd);
4075
4076 return 0;
4077}
4078
4079/*
4080 * Function called by the PCI layer when just before the
4081 * machine shuts down.
4082 *
4083 * If a protocol layer shutdown function is present it will be called
4084 * by this function.
4085 *
4086 * @dd Pointer to the driver data structure.
4087 *
4088 * return value
4089 * 0
4090 */
Jens Axboe63166682011-09-27 21:27:43 -06004091static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004092{
Jens Axboeffc771b2014-05-09 09:42:02 -06004093 mtip_hw_shutdown(dd);
4094
Sam Bradshaw88523a62011-08-30 08:34:26 -06004095 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304096 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304097 dev_info(&dd->pdev->dev,
4098 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304099
Asai Thambi SP02b48262015-05-11 15:48:00 -07004100 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304101 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304102 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004103 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004104 }
4105 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304106 dd->disk = NULL;
4107 dd->queue = NULL;
4108 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004109
4110 spin_lock(&rssd_index_lock);
4111 ida_remove(&rssd_index_ida, dd->index);
4112 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004113 return 0;
4114}
4115
Jens Axboe63166682011-09-27 21:27:43 -06004116static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004117{
4118 dev_info(&dd->pdev->dev,
4119 "Suspending %s ...\n", dd->disk->disk_name);
4120 mtip_hw_suspend(dd);
4121 return 0;
4122}
4123
Jens Axboe63166682011-09-27 21:27:43 -06004124static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004125{
4126 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4127 dd->disk->disk_name);
4128 mtip_hw_resume(dd);
4129 return 0;
4130}
4131
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004132static void drop_cpu(int cpu)
4133{
4134 cpu_use[cpu]--;
4135}
4136
4137static int get_least_used_cpu_on_node(int node)
4138{
4139 int cpu, least_used_cpu, least_cnt;
4140 const struct cpumask *node_mask;
4141
4142 node_mask = cpumask_of_node(node);
4143 least_used_cpu = cpumask_first(node_mask);
4144 least_cnt = cpu_use[least_used_cpu];
4145 cpu = least_used_cpu;
4146
4147 for_each_cpu(cpu, node_mask) {
4148 if (cpu_use[cpu] < least_cnt) {
4149 least_used_cpu = cpu;
4150 least_cnt = cpu_use[cpu];
4151 }
4152 }
4153 cpu_use[least_used_cpu]++;
4154 return least_used_cpu;
4155}
4156
4157/* Helper for selecting a node in round robin mode */
4158static inline int mtip_get_next_rr_node(void)
4159{
4160 static int next_node = -1;
4161
4162 if (next_node == -1) {
4163 next_node = first_online_node;
4164 return next_node;
4165 }
4166
4167 next_node = next_online_node(next_node);
4168 if (next_node == MAX_NUMNODES)
4169 next_node = first_online_node;
4170 return next_node;
4171}
4172
Fengguang Wu25bac122013-01-12 15:31:40 +08004173static DEFINE_HANDLER(0);
4174static DEFINE_HANDLER(1);
4175static DEFINE_HANDLER(2);
4176static DEFINE_HANDLER(3);
4177static DEFINE_HANDLER(4);
4178static DEFINE_HANDLER(5);
4179static DEFINE_HANDLER(6);
4180static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004181
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004182static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4183{
4184 int pos;
4185 unsigned short pcie_dev_ctrl;
4186
4187 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4188 if (pos) {
4189 pci_read_config_word(pdev,
4190 pos + PCI_EXP_DEVCTL,
4191 &pcie_dev_ctrl);
4192 if (pcie_dev_ctrl & (1 << 11) ||
4193 pcie_dev_ctrl & (1 << 4)) {
4194 dev_info(&dd->pdev->dev,
4195 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4196 pdev->vendor, pdev->device);
4197 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4198 PCI_EXP_DEVCTL_RELAX_EN);
4199 pci_write_config_word(pdev,
4200 pos + PCI_EXP_DEVCTL,
4201 pcie_dev_ctrl);
4202 }
4203 }
4204}
4205
4206static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4207{
4208 /*
4209 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4210 * device with device id 0x5aXX
4211 */
4212 if (pdev->bus && pdev->bus->self) {
4213 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4214 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4215 mtip_disable_link_opts(dd, pdev->bus->self);
4216 } else {
4217 /* Check further up the topology */
4218 struct pci_dev *parent_dev = pdev->bus->self;
4219 if (parent_dev->bus &&
4220 parent_dev->bus->parent &&
4221 parent_dev->bus->parent->self &&
4222 parent_dev->bus->parent->self->vendor ==
4223 PCI_VENDOR_ID_ATI &&
4224 (parent_dev->bus->parent->self->device &
4225 0xff00) == 0x5a00) {
4226 mtip_disable_link_opts(dd,
4227 parent_dev->bus->parent->self);
4228 }
4229 }
4230 }
4231}
4232
Sam Bradshaw88523a62011-08-30 08:34:26 -06004233/*
4234 * Called for each supported PCI device detected.
4235 *
4236 * This function allocates the private data structure, enables the
4237 * PCI device and then calls the block layer initialization function.
4238 *
4239 * return value
4240 * 0 on success else an error code.
4241 */
4242static int mtip_pci_probe(struct pci_dev *pdev,
4243 const struct pci_device_id *ent)
4244{
4245 int rv = 0;
4246 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004247 char cpu_list[256];
4248 const struct cpumask *node_mask;
4249 int cpu, i = 0, j = 0;
4250 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304251 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004252
4253 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004254 my_node = pcibus_to_node(pdev->bus);
4255 if (my_node != NUMA_NO_NODE) {
4256 if (!node_online(my_node))
4257 my_node = mtip_get_next_rr_node();
4258 } else {
4259 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4260 my_node = mtip_get_next_rr_node();
4261 }
4262 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4263 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004264 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004265
4266 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004267 if (dd == NULL) {
4268 dev_err(&pdev->dev,
4269 "Unable to allocate memory for driver data\n");
4270 return -ENOMEM;
4271 }
4272
Sam Bradshaw88523a62011-08-30 08:34:26 -06004273 /* Attach the private data to this PCI device. */
4274 pci_set_drvdata(pdev, dd);
4275
4276 rv = pcim_enable_device(pdev);
4277 if (rv < 0) {
4278 dev_err(&pdev->dev, "Unable to enable device\n");
4279 goto iomap_err;
4280 }
4281
4282 /* Map BAR5 to memory. */
4283 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4284 if (rv < 0) {
4285 dev_err(&pdev->dev, "Unable to map regions\n");
4286 goto iomap_err;
4287 }
4288
4289 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4290 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4291
4292 if (rv) {
4293 rv = pci_set_consistent_dma_mask(pdev,
4294 DMA_BIT_MASK(32));
4295 if (rv) {
4296 dev_warn(&pdev->dev,
4297 "64-bit DMA enable failed\n");
4298 goto setmask_err;
4299 }
4300 }
4301 }
4302
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004303 /* Copy the info we may need later into the private data structure. */
4304 dd->major = mtip_major;
4305 dd->instance = instance;
4306 dd->pdev = pdev;
4307 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004308
Asai Thambi S P0caff002013-04-03 19:56:21 +05304309 INIT_LIST_HEAD(&dd->online_list);
4310 INIT_LIST_HEAD(&dd->remove_list);
4311
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004312 memset(dd->workq_name, 0, 32);
4313 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4314
4315 dd->isr_workq = create_workqueue(dd->workq_name);
4316 if (!dd->isr_workq) {
4317 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004318 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004319 goto block_initialize_err;
4320 }
4321
4322 memset(cpu_list, 0, sizeof(cpu_list));
4323
4324 node_mask = cpumask_of_node(dd->numa_node);
4325 if (!cpumask_empty(node_mask)) {
4326 for_each_cpu(cpu, node_mask)
4327 {
4328 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4329 j = strlen(cpu_list);
4330 }
4331
4332 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4333 dd->numa_node,
4334 topology_physical_package_id(cpumask_first(node_mask)),
4335 nr_cpus_node(dd->numa_node),
4336 cpu_list);
4337 } else
4338 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4339
4340 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4341 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4342 cpu_to_node(dd->isr_binding), dd->isr_binding);
4343
4344 /* first worker context always runs in ISR */
4345 dd->work[0].cpu_binding = dd->isr_binding;
4346 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4347 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4348 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4349 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4350 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4351 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4352 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4353
4354 /* Log the bindings */
4355 for_each_present_cpu(cpu) {
4356 memset(cpu_list, 0, sizeof(cpu_list));
4357 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4358 if (dd->work[i].cpu_binding == cpu) {
4359 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4360 j = strlen(cpu_list);
4361 }
4362 }
4363 if (j)
4364 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4365 }
4366
4367 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4368 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4369 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4370 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4371 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4372 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4373 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4374 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4375
4376 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004377 rv = pci_enable_msi(pdev);
4378 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004379 dev_warn(&pdev->dev,
4380 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004381 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004382 }
4383
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004384 mtip_fix_ero_nosnoop(dd, pdev);
4385
Sam Bradshaw88523a62011-08-30 08:34:26 -06004386 /* Initialize the block layer. */
4387 rv = mtip_block_initialize(dd);
4388 if (rv < 0) {
4389 dev_err(&pdev->dev,
4390 "Unable to initialize block layer\n");
4391 goto block_initialize_err;
4392 }
4393
4394 /*
4395 * Increment the instance count so that each device has a unique
4396 * instance number.
4397 */
4398 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004399 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004400 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304401 else
4402 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304403
4404 /* Add to online list even if in ftl rebuild */
4405 spin_lock_irqsave(&dev_lock, flags);
4406 list_add(&dd->online_list, &online_list);
4407 spin_unlock_irqrestore(&dev_lock, flags);
4408
Sam Bradshaw88523a62011-08-30 08:34:26 -06004409 goto done;
4410
4411block_initialize_err:
4412 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004413
4414msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004415 if (dd->isr_workq) {
4416 flush_workqueue(dd->isr_workq);
4417 destroy_workqueue(dd->isr_workq);
4418 drop_cpu(dd->work[0].cpu_binding);
4419 drop_cpu(dd->work[1].cpu_binding);
4420 drop_cpu(dd->work[2].cpu_binding);
4421 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004422setmask_err:
4423 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4424
4425iomap_err:
4426 kfree(dd);
4427 pci_set_drvdata(pdev, NULL);
4428 return rv;
4429done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004430 return rv;
4431}
4432
4433/*
4434 * Called for each probed device when the device is removed or the
4435 * driver is unloaded.
4436 *
4437 * return value
4438 * None
4439 */
4440static void mtip_pci_remove(struct pci_dev *pdev)
4441{
4442 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004443 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004444
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004445 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004446
Asai Thambi S P0caff002013-04-03 19:56:21 +05304447 spin_lock_irqsave(&dev_lock, flags);
4448 list_del_init(&dd->online_list);
4449 list_add(&dd->remove_list, &removing_list);
4450 spin_unlock_irqrestore(&dev_lock, flags);
4451
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004452 mtip_check_surprise_removal(pdev);
4453 synchronize_irq(dd->pdev->irq);
4454
4455 /* Spin until workers are done */
4456 to = jiffies + msecs_to_jiffies(4000);
4457 do {
4458 msleep(20);
4459 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4460 time_before(jiffies, to));
4461
4462 if (atomic_read(&dd->irq_workers_active) != 0) {
4463 dev_warn(&dd->pdev->dev,
4464 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004465 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004466
Asai Thambi SP2132a542015-05-11 15:53:18 -07004467 blk_mq_stop_hw_queues(dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004468 /* Clean up the block layer. */
4469 mtip_block_remove(dd);
4470
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004471 if (dd->isr_workq) {
4472 flush_workqueue(dd->isr_workq);
4473 destroy_workqueue(dd->isr_workq);
4474 drop_cpu(dd->work[0].cpu_binding);
4475 drop_cpu(dd->work[1].cpu_binding);
4476 drop_cpu(dd->work[2].cpu_binding);
4477 }
4478
Sam Bradshaw88523a62011-08-30 08:34:26 -06004479 pci_disable_msi(pdev);
4480
Asai Thambi S P0caff002013-04-03 19:56:21 +05304481 spin_lock_irqsave(&dev_lock, flags);
4482 list_del_init(&dd->remove_list);
4483 spin_unlock_irqrestore(&dev_lock, flags);
4484
Asai Thambi SP2132a542015-05-11 15:53:18 -07004485 kfree(dd);
4486 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004487
Sam Bradshaw88523a62011-08-30 08:34:26 -06004488 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004489 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004490}
4491
4492/*
4493 * Called for each probed device when the device is suspended.
4494 *
4495 * return value
4496 * 0 Success
4497 * <0 Error
4498 */
4499static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4500{
4501 int rv = 0;
4502 struct driver_data *dd = pci_get_drvdata(pdev);
4503
4504 if (!dd) {
4505 dev_err(&pdev->dev,
4506 "Driver private datastructure is NULL\n");
4507 return -EFAULT;
4508 }
4509
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004510 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004511
4512 /* Disable ports & interrupts then send standby immediate */
4513 rv = mtip_block_suspend(dd);
4514 if (rv < 0) {
4515 dev_err(&pdev->dev,
4516 "Failed to suspend controller\n");
4517 return rv;
4518 }
4519
4520 /*
4521 * Save the pci config space to pdev structure &
4522 * disable the device
4523 */
4524 pci_save_state(pdev);
4525 pci_disable_device(pdev);
4526
4527 /* Move to Low power state*/
4528 pci_set_power_state(pdev, PCI_D3hot);
4529
4530 return rv;
4531}
4532
4533/*
4534 * Called for each probed device when the device is resumed.
4535 *
4536 * return value
4537 * 0 Success
4538 * <0 Error
4539 */
4540static int mtip_pci_resume(struct pci_dev *pdev)
4541{
4542 int rv = 0;
4543 struct driver_data *dd;
4544
4545 dd = pci_get_drvdata(pdev);
4546 if (!dd) {
4547 dev_err(&pdev->dev,
4548 "Driver private datastructure is NULL\n");
4549 return -EFAULT;
4550 }
4551
4552 /* Move the device to active State */
4553 pci_set_power_state(pdev, PCI_D0);
4554
4555 /* Restore PCI configuration space */
4556 pci_restore_state(pdev);
4557
4558 /* Enable the PCI device*/
4559 rv = pcim_enable_device(pdev);
4560 if (rv < 0) {
4561 dev_err(&pdev->dev,
4562 "Failed to enable card during resume\n");
4563 goto err;
4564 }
4565 pci_set_master(pdev);
4566
4567 /*
4568 * Calls hbaReset, initPort, & startPort function
4569 * then enables interrupts
4570 */
4571 rv = mtip_block_resume(dd);
4572 if (rv < 0)
4573 dev_err(&pdev->dev, "Unable to resume\n");
4574
4575err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004576 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004577
4578 return rv;
4579}
4580
4581/*
4582 * Shutdown routine
4583 *
4584 * return value
4585 * None
4586 */
4587static void mtip_pci_shutdown(struct pci_dev *pdev)
4588{
4589 struct driver_data *dd = pci_get_drvdata(pdev);
4590 if (dd)
4591 mtip_block_shutdown(dd);
4592}
4593
Sam Bradshaw88523a62011-08-30 08:34:26 -06004594/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004595static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304596 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4597 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4598 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4599 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4600 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4601 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4602 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004603 { 0 }
4604};
4605
4606/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004607static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004608 .name = MTIP_DRV_NAME,
4609 .id_table = mtip_pci_tbl,
4610 .probe = mtip_pci_probe,
4611 .remove = mtip_pci_remove,
4612 .suspend = mtip_pci_suspend,
4613 .resume = mtip_pci_resume,
4614 .shutdown = mtip_pci_shutdown,
4615};
4616
4617MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4618
4619/*
4620 * Module initialization function.
4621 *
4622 * Called once when the module is loaded. This function allocates a major
4623 * block device number to the Cyclone devices and registers the PCI layer
4624 * of the driver.
4625 *
4626 * Return value
4627 * 0 on success else error code.
4628 */
4629static int __init mtip_init(void)
4630{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004631 int error;
4632
Asai Thambi S P45422e72012-09-05 22:03:56 +05304633 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004634
Asai Thambi S P0caff002013-04-03 19:56:21 +05304635 spin_lock_init(&dev_lock);
4636
4637 INIT_LIST_HEAD(&online_list);
4638 INIT_LIST_HEAD(&removing_list);
4639
Sam Bradshaw88523a62011-08-30 08:34:26 -06004640 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004641 error = register_blkdev(0, MTIP_DRV_NAME);
4642 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304643 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004644 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004645 return -EBUSY;
4646 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004647 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004648
Asai Thambi S P0caff002013-04-03 19:56:21 +05304649 dfs_parent = debugfs_create_dir("rssd", NULL);
4650 if (IS_ERR_OR_NULL(dfs_parent)) {
4651 pr_warn("Error creating debugfs parent\n");
4652 dfs_parent = NULL;
4653 }
4654 if (dfs_parent) {
4655 dfs_device_status = debugfs_create_file("device_status",
4656 S_IRUGO, dfs_parent, NULL,
4657 &mtip_device_status_fops);
4658 if (IS_ERR_OR_NULL(dfs_device_status)) {
4659 pr_err("Error creating device_status node\n");
4660 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004661 }
4662 }
4663
Sam Bradshaw88523a62011-08-30 08:34:26 -06004664 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004665 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004666 if (error) {
4667 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004668 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004669 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004670
4671 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004672}
4673
4674/*
4675 * Module de-initialization function.
4676 *
4677 * Called once when the module is unloaded. This function deallocates
4678 * the major block device number allocated by mtip_init() and
4679 * unregisters the PCI layer of the driver.
4680 *
4681 * Return value
4682 * none
4683 */
4684static void __exit mtip_exit(void)
4685{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004686 /* Release the allocated major block device number. */
4687 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4688
4689 /* Unregister the PCI driver. */
4690 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004691
4692 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004693}
4694
4695MODULE_AUTHOR("Micron Technology, Inc");
4696MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4697MODULE_LICENSE("GPL");
4698MODULE_VERSION(MTIP_DRV_VERSION);
4699
4700module_init(mtip_init);
4701module_exit(mtip_exit);