blob: 144e7d97b647a52a50bee1d60dd757ad09e06163 [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 SP2f17d712015-05-11 15:57:16 -0700272 /*
273 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
274 * is 1 sec but in LUN failure conditions, up to 10 secs are required
275 */
276 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530277 do {
278 mdelay(10);
279 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
280 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600281
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530282 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
283 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200284
Jens Axboe63166682011-09-27 21:27:43 -0600285 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
286 return -1;
287
288 return 0;
289}
290
291/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600292 * Issue a command to the hardware.
293 *
294 * Set the appropriate bit in the s_active and Command Issue hardware
295 * registers, causing hardware command processing to begin.
296 *
297 * @port Pointer to the port structure.
298 * @tag The tag of the command to be issued.
299 *
300 * return value
301 * None
302 */
303static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
304{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800305 int group = tag >> 5;
306
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800307 /* guard SACT and CI registers */
308 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600309 writel((1 << MTIP_TAG_BIT(tag)),
310 port->s_active[MTIP_TAG_INDEX(tag)]);
311 writel((1 << MTIP_TAG_BIT(tag)),
312 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800313 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600314}
315
316/*
Jens Axboe63166682011-09-27 21:27:43 -0600317 * Enable/disable the reception of FIS
318 *
319 * @port Pointer to the port data structure
320 * @enable 1 to enable, 0 to disable
321 *
322 * return value
323 * Previous state: 1 enabled, 0 disabled
324 */
325static int mtip_enable_fis(struct mtip_port *port, int enable)
326{
327 u32 tmp;
328
329 /* enable FIS reception */
330 tmp = readl(port->mmio + PORT_CMD);
331 if (enable)
332 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
333 else
334 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
335
336 /* Flush */
337 readl(port->mmio + PORT_CMD);
338
339 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
340}
341
342/*
343 * Enable/disable the DMA engine
344 *
345 * @port Pointer to the port data structure
346 * @enable 1 to enable, 0 to disable
347 *
348 * return value
349 * Previous state: 1 enabled, 0 disabled.
350 */
351static int mtip_enable_engine(struct mtip_port *port, int enable)
352{
353 u32 tmp;
354
355 /* enable FIS reception */
356 tmp = readl(port->mmio + PORT_CMD);
357 if (enable)
358 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
359 else
360 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
361
362 readl(port->mmio + PORT_CMD);
363 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
364}
365
366/*
367 * Enables the port DMA engine and FIS reception.
368 *
369 * return value
370 * None
371 */
372static inline void mtip_start_port(struct mtip_port *port)
373{
374 /* Enable FIS reception */
375 mtip_enable_fis(port, 1);
376
377 /* Enable the DMA engine */
378 mtip_enable_engine(port, 1);
379}
380
381/*
382 * Deinitialize a port by disabling port interrupts, the DMA engine,
383 * and FIS reception.
384 *
385 * @port Pointer to the port structure
386 *
387 * return value
388 * None
389 */
390static inline void mtip_deinit_port(struct mtip_port *port)
391{
392 /* Disable interrupts on this port */
393 writel(0, port->mmio + PORT_IRQ_MASK);
394
395 /* Disable the DMA engine */
396 mtip_enable_engine(port, 0);
397
398 /* Disable FIS reception */
399 mtip_enable_fis(port, 0);
400}
401
402/*
403 * Initialize a port.
404 *
405 * This function deinitializes the port by calling mtip_deinit_port() and
406 * then initializes it by setting the command header and RX FIS addresses,
407 * clearing the SError register and any pending port interrupts before
408 * re-enabling the default set of port interrupts.
409 *
410 * @port Pointer to the port structure.
411 *
412 * return value
413 * None
414 */
415static void mtip_init_port(struct mtip_port *port)
416{
417 int i;
418 mtip_deinit_port(port);
419
420 /* Program the command list base and FIS base addresses */
421 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
422 writel((port->command_list_dma >> 16) >> 16,
423 port->mmio + PORT_LST_ADDR_HI);
424 writel((port->rxfis_dma >> 16) >> 16,
425 port->mmio + PORT_FIS_ADDR_HI);
426 }
427
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100428 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600429 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100430 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600431
432 /* Clear SError */
433 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
434
435 /* reset the completed registers.*/
436 for (i = 0; i < port->dd->slot_groups; i++)
437 writel(0xFFFFFFFF, port->completed[i]);
438
439 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700440 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600441
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100442 /* Clear any pending interrupts on the HBA. */
443 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
444 port->dd->mmio + HOST_IRQ_STAT);
445
Jens Axboe63166682011-09-27 21:27:43 -0600446 /* Enable port interrupts */
447 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
448}
449
450/*
451 * Restart a port
452 *
453 * @port Pointer to the port data structure.
454 *
455 * return value
456 * None
457 */
458static void mtip_restart_port(struct mtip_port *port)
459{
460 unsigned long timeout;
461
462 /* Disable the DMA engine */
463 mtip_enable_engine(port, 0);
464
465 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
466 timeout = jiffies + msecs_to_jiffies(500);
467 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
468 && time_before(jiffies, timeout))
469 ;
470
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200471 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200472 return;
473
Jens Axboe63166682011-09-27 21:27:43 -0600474 /*
475 * Chip quirk: escalate to hba reset if
476 * PxCMD.CR not clear after 500 ms
477 */
478 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
479 dev_warn(&port->dd->pdev->dev,
480 "PxCMD.CR not clear, escalating reset\n");
481
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530482 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600483 dev_err(&port->dd->pdev->dev,
484 "HBA reset escalation failed.\n");
485
486 /* 30 ms delay before com reset to quiesce chip */
487 mdelay(30);
488 }
489
490 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
491
492 /* Set PxSCTL.DET */
493 writel(readl(port->mmio + PORT_SCR_CTL) |
494 1, port->mmio + PORT_SCR_CTL);
495 readl(port->mmio + PORT_SCR_CTL);
496
497 /* Wait 1 ms to quiesce chip function */
498 timeout = jiffies + msecs_to_jiffies(1);
499 while (time_before(jiffies, timeout))
500 ;
501
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200502 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200503 return;
504
Jens Axboe63166682011-09-27 21:27:43 -0600505 /* Clear PxSCTL.DET */
506 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
507 port->mmio + PORT_SCR_CTL);
508 readl(port->mmio + PORT_SCR_CTL);
509
510 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
511 timeout = jiffies + msecs_to_jiffies(500);
512 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
513 && time_before(jiffies, timeout))
514 ;
515
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200516 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200517 return;
518
Jens Axboe63166682011-09-27 21:27:43 -0600519 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
520 dev_warn(&port->dd->pdev->dev,
521 "COM reset failed\n");
522
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100523 mtip_init_port(port);
524 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600525
Jens Axboe63166682011-09-27 21:27:43 -0600526}
527
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530528static int mtip_device_reset(struct driver_data *dd)
529{
530 int rv = 0;
531
532 if (mtip_check_surprise_removal(dd->pdev))
533 return 0;
534
535 if (mtip_hba_reset(dd) < 0)
536 rv = -EFAULT;
537
538 mdelay(1);
539 mtip_init_port(dd->port);
540 mtip_start_port(dd->port);
541
542 /* Enable interrupts on the HBA. */
543 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
544 dd->mmio + HOST_CTL);
545 return rv;
546}
547
Jens Axboe63166682011-09-27 21:27:43 -0600548/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200549 * Helper function for tag logging
550 */
551static void print_tags(struct driver_data *dd,
552 char *msg,
553 unsigned long *tagbits,
554 int cnt)
555{
556 unsigned char tagmap[128];
557 int group, tagmap_len = 0;
558
559 memset(tagmap, 0, sizeof(tagmap));
560 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600561 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200562 tagbits[group-1]);
563 dev_warn(&dd->pdev->dev,
564 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
565}
566
567/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600568 * Internal command completion callback function.
569 *
570 * This function is normally called by the driver ISR when an internal
571 * command completed. This function signals the command completion by
572 * calling complete().
573 *
574 * @port Pointer to the port data structure.
575 * @tag Tag of the command that has completed.
576 * @data Pointer to a completion structure.
577 * @status Completion status.
578 *
579 * return value
580 * None
581 */
582static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600583 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600584{
Jens Axboeffc771b2014-05-09 09:42:02 -0600585 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600586 if (unlikely(status == PORT_IRQ_TF_ERR))
587 dev_warn(&port->dd->pdev->dev,
588 "Internal command %d completed with TFE\n", tag);
589
Sam Bradshaw88523a62011-08-30 08:34:26 -0600590 complete(waiting);
591}
592
Asai Thambi S P8182b492012-04-09 08:35:38 +0200593static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600594 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200595{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200596}
597
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200598static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
599 dma_addr_t buffer_dma, unsigned int sectors);
600static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
601 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600602/*
603 * Handle an error.
604 *
605 * @dd Pointer to the DRIVER_DATA structure.
606 *
607 * return value
608 * None
609 */
610static void mtip_handle_tfe(struct driver_data *dd)
611{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200612 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600613 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200614 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600615 u32 completed;
616 struct host_to_dev_fis *fis;
617 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200618 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200619 unsigned char *buf;
620 char *fail_reason = NULL;
621 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600622
623 dev_warn(&dd->pdev->dev, "Taskfile error\n");
624
625 port = dd->port;
626
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700627 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
628
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700629 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600630 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700631 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
632
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700633 if (cmd->comp_data && cmd->comp_func) {
634 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600635 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700636 }
637 goto handle_tfe_exit;
638 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600639
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200640 /* clear the tag accumulator */
641 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
642
Sam Bradshaw88523a62011-08-30 08:34:26 -0600643 /* Loop through all the groups */
644 for (group = 0; group < dd->slot_groups; group++) {
645 completed = readl(port->completed[group]);
646
Jens Axboeffc771b2014-05-09 09:42:02 -0600647 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
648
Sam Bradshaw88523a62011-08-30 08:34:26 -0600649 /* clear completed status register in the hardware.*/
650 writel(completed, port->completed[group]);
651
Sam Bradshaw88523a62011-08-30 08:34:26 -0600652 /* Process successfully completed commands */
653 for (bit = 0; bit < 32 && completed; bit++) {
654 if (!(completed & (1<<bit)))
655 continue;
656 tag = (group << 5) + bit;
657
658 /* Skip the internal command slot */
659 if (tag == MTIP_TAG_INTERNAL)
660 continue;
661
Jens Axboeffc771b2014-05-09 09:42:02 -0600662 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200663 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200665 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600666 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600667 } else {
668 dev_err(&port->dd->pdev->dev,
669 "Missing completion func for tag %d",
670 tag);
671 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600672 /* don't proceed further */
673 return;
674 }
675 }
676 }
677 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200678
679 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600680
681 /* Restart the port */
682 mdelay(20);
683 mtip_restart_port(port);
684
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200685 /* Trying to determine the cause of the error */
686 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
687 dd->port->log_buf,
688 dd->port->log_buf_dma, 1);
689 if (rv) {
690 dev_warn(&dd->pdev->dev,
691 "Error in READ LOG EXT (10h) command\n");
692 /* non-critical error, don't fail the load */
693 } else {
694 buf = (unsigned char *)dd->port->log_buf;
695 if (buf[259] & 0x1) {
696 dev_info(&dd->pdev->dev,
697 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200698 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200699 fail_all_ncq_write = 1;
700 fail_reason = "write protect";
701 }
702 if (buf[288] == 0xF7) {
703 dev_info(&dd->pdev->dev,
704 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200705 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200706 fail_all_ncq_cmds = 1;
707 fail_reason = "thermal shutdown";
708 }
709 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700710 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200711 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700712 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200713 fail_all_ncq_cmds = 1;
714 fail_reason = "rebuild failed";
715 }
716 }
717
Sam Bradshaw88523a62011-08-30 08:34:26 -0600718 /* clear the tag accumulator */
719 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
720
721 /* Loop through all the groups */
722 for (group = 0; group < dd->slot_groups; group++) {
723 for (bit = 0; bit < 32; bit++) {
724 reissue = 1;
725 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600726 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600727
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200728 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600729
730 /* Should re-issue? */
731 if (tag == MTIP_TAG_INTERNAL ||
732 fis->command == ATA_CMD_SET_FEATURES)
733 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200734 else {
735 if (fail_all_ncq_cmds ||
736 (fail_all_ncq_write &&
737 fis->command == ATA_CMD_FPDMA_WRITE)) {
738 dev_warn(&dd->pdev->dev,
739 " Fail: %s w/tag %d [%s].\n",
740 fis->command == ATA_CMD_FPDMA_WRITE ?
741 "write" : "read",
742 tag,
743 fail_reason != NULL ?
744 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200745 if (cmd->comp_func) {
746 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600747 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200748 }
749 continue;
750 }
751 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600752
753 /*
754 * First check if this command has
755 * exceeded its retries.
756 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200757 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600758
759 set_bit(tag, tagaccum);
760
Sam Bradshaw88523a62011-08-30 08:34:26 -0600761 /* Re-issue the command. */
762 mtip_issue_ncq_command(port, tag);
763
764 continue;
765 }
766
767 /* Retire a command that will not be reissued */
768 dev_warn(&port->dd->pdev->dev,
769 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600770
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200771 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600772 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600773 else
774 dev_warn(&port->dd->pdev->dev,
775 "Bad completion for tag %d\n",
776 tag);
777 }
778 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200779 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600780
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700781handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100782 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200783 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100784 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600785}
786
787/*
788 * Handle a set device bits interrupt
789 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800790static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
791 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600792{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800793 struct driver_data *dd = port->dd;
794 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600795 struct mtip_cmd *command;
796
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800797 if (!completed) {
798 WARN_ON_ONCE(!completed);
799 return;
800 }
801 /* clear completed status register in the hardware.*/
802 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600803
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800804 /* Process completed commands. */
805 for (bit = 0; (bit < 32) && completed; bit++) {
806 if (completed & 0x01) {
807 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600808
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800809 /* skip internal command slot. */
810 if (unlikely(tag == MTIP_TAG_INTERNAL))
811 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600812
Jens Axboeffc771b2014-05-09 09:42:02 -0600813 command = mtip_cmd_from_tag(dd, tag);
814 if (likely(command->comp_func))
815 command->comp_func(port, tag, command, 0);
816 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600817 dev_dbg(&dd->pdev->dev,
818 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800819 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600820
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800821 if (mtip_check_surprise_removal(
822 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800823 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600824 }
825 }
826 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800827 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600828 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800829
830 /* If last, re-enable interrupts */
831 if (atomic_dec_return(&dd->irq_workers_active) == 0)
832 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600833}
834
835/*
836 * Process legacy pio and d2h interrupts
837 */
838static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
839{
840 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600841 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600842
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200843 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100844 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600845 & (1 << MTIP_TAG_INTERNAL))) {
846 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600847 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600848 return;
849 }
850 }
851
Sam Bradshaw88523a62011-08-30 08:34:26 -0600852 return;
853}
854
855/*
856 * Demux and handle errors
857 */
858static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
859{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600860
861 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
862 dev_warn(&dd->pdev->dev,
863 "Clearing PxSERR.DIAG.x\n");
864 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
865 }
866
867 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
868 dev_warn(&dd->pdev->dev,
869 "Clearing PxSERR.DIAG.n\n");
870 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
871 }
872
873 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
874 dev_warn(&dd->pdev->dev,
875 "Port stat errors %x unhandled\n",
876 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700877 if (mtip_check_surprise_removal(dd->pdev))
878 return;
879 }
880 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
881 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
882 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600883 }
884}
885
886static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
887{
888 struct driver_data *dd = (struct driver_data *) data;
889 struct mtip_port *port = dd->port;
890 u32 hba_stat, port_stat;
891 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800892 int do_irq_enable = 1, i, workers;
893 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600894
895 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
896 if (hba_stat) {
897 rv = IRQ_HANDLED;
898
899 /* Acknowledge the interrupt status on the port.*/
900 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700901 if (unlikely(port_stat == 0xFFFFFFFF)) {
902 mtip_check_surprise_removal(dd->pdev);
903 return IRQ_HANDLED;
904 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600905 writel(port_stat, port->mmio + PORT_IRQ_STAT);
906
907 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800908 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
909 do_irq_enable = 0;
910 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
911
912 /* Start at 1: group zero is always local? */
913 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
914 i++) {
915 twork = &dd->work[i];
916 twork->completed = readl(port->completed[i]);
917 if (twork->completed)
918 workers++;
919 }
920
921 atomic_set(&dd->irq_workers_active, workers);
922 if (workers) {
923 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
924 twork = &dd->work[i];
925 if (twork->completed)
926 queue_work_on(
927 twork->cpu_binding,
928 dd->isr_workq,
929 &twork->work);
930 }
931
932 if (likely(dd->work[0].completed))
933 mtip_workq_sdbfx(port, 0,
934 dd->work[0].completed);
935
936 } else {
937 /*
938 * Chip quirk: SDB interrupt but nothing
939 * to complete
940 */
941 do_irq_enable = 1;
942 }
943 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600944
945 if (unlikely(port_stat & PORT_IRQ_ERR)) {
946 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600947 /* don't proceed further */
948 return IRQ_HANDLED;
949 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200950 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200951 &dd->dd_flag))
952 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600953
954 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
955 }
956
957 if (unlikely(port_stat & PORT_IRQ_LEGACY))
958 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
959 }
960
961 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800962 if (unlikely(do_irq_enable))
963 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600964
965 return rv;
966}
967
968/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600969 * HBA interrupt subroutine.
970 *
971 * @irq IRQ number.
972 * @instance Pointer to the driver data structure.
973 *
974 * return value
975 * IRQ_HANDLED A HBA interrupt was pending and handled.
976 * IRQ_NONE This interrupt was not for the HBA.
977 */
978static irqreturn_t mtip_irq_handler(int irq, void *instance)
979{
980 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800981
982 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600983}
984
985static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
986{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600987 writel(1 << MTIP_TAG_BIT(tag),
988 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
989}
990
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200991static bool mtip_pause_ncq(struct mtip_port *port,
992 struct host_to_dev_fis *fis)
993{
994 struct host_to_dev_fis *reply;
995 unsigned long task_file_data;
996
997 reply = port->rxfis + RX_FIS_D2H_REG;
998 task_file_data = readl(port->mmio+PORT_TFDATA);
999
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301000 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001001 return false;
1002
1003 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001004 port->ic_pause_timer = jiffies;
1005 return true;
1006 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1007 (fis->features == 0x03)) {
1008 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1009 port->ic_pause_timer = jiffies;
1010 return true;
1011 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1012 ((fis->command == 0xFC) &&
1013 (fis->features == 0x27 || fis->features == 0x72 ||
1014 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001015 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001016 /* Com reset after secure erase or lowlevel format */
1017 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001018 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001019 return false;
1020 }
1021
1022 return false;
1023}
1024
Sam Bradshaw88523a62011-08-30 08:34:26 -06001025/*
1026 * Wait for port to quiesce
1027 *
1028 * @port Pointer to port data structure
1029 * @timeout Max duration to wait (ms)
1030 *
1031 * return value
1032 * 0 Success
1033 * -EBUSY Commands still active
1034 */
1035static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1036{
1037 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001038 unsigned int n;
1039 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001040
Jens Axboe9acf03c2014-05-14 08:22:56 -06001041 blk_mq_stop_hw_queues(port->dd->queue);
1042
Sam Bradshaw88523a62011-08-30 08:34:26 -06001043 to = jiffies + msecs_to_jiffies(timeout);
1044 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001045 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1046 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001047 msleep(20);
1048 continue; /* svc thd is actively issuing commands */
1049 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001050
1051 msleep(100);
1052 if (mtip_check_surprise_removal(port->dd->pdev))
1053 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001054 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001055 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001056
Sam Bradshaw88523a62011-08-30 08:34:26 -06001057 /*
1058 * Ignore s_active bit 0 of array element 0.
1059 * This bit will always be set
1060 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001061 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001062 for (n = 1; n < port->dd->slot_groups; n++)
1063 active |= readl(port->s_active[n]);
1064
1065 if (!active)
1066 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001067 } while (time_before(jiffies, to));
1068
Jens Axboe9acf03c2014-05-14 08:22:56 -06001069 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001070 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001071err_fault:
1072 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1073 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001074}
1075
1076/*
1077 * Execute an internal command and wait for the completion.
1078 *
1079 * @port Pointer to the port data structure.
1080 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001081 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001082 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001083 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001084 * @opts Command header options, excluding the FIS length
1085 * and the number of PRD entries.
1086 * @timeout Time in ms to wait for the command to complete.
1087 *
1088 * return value
1089 * 0 Command completed successfully.
1090 * -EFAULT The buffer address is not correctly aligned.
1091 * -EBUSY Internal command or other IO in progress.
1092 * -EAGAIN Time out waiting for command to complete.
1093 */
1094static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001095 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001096 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001097 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001098 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001099 u32 opts,
1100 gfp_t atomic,
1101 unsigned long timeout)
1102{
1103 struct mtip_cmd_sg *command_sg;
1104 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001105 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301106 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001107 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001108
1109 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1110 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301111 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001112 return -EFAULT;
1113 }
1114
Jens Axboeffc771b2014-05-09 09:42:02 -06001115 int_cmd = mtip_get_int_command(dd);
1116
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001117 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001118
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001119 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1120 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1121
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301122 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001123
1124 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001125 if (fis->command != ATA_CMD_STANDBYNOW1) {
1126 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001127 if (mtip_quiesce_io(port,
1128 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301129 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001130 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001131 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001132 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001133 wake_up_interruptible(&port->svc_wait);
1134 return -EBUSY;
1135 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001136 }
1137
1138 /* Set the completion function and data for the command. */
1139 int_cmd->comp_data = &wait;
1140 int_cmd->comp_func = mtip_completion;
1141
1142 } else {
1143 /* Clear completion - we're going to poll */
1144 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001145 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001146 }
1147
1148 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001149 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001150
1151 /* Populate the SG list */
1152 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001153 __force_bit2int cpu_to_le32(opts | fis_len);
1154 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001155 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1156
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001157 command_sg->info =
1158 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1159 command_sg->dba =
1160 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1161 command_sg->dba_upper =
1162 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001163
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001164 int_cmd->command_header->opts |=
1165 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001166 }
1167
1168 /* Populate the command header */
1169 int_cmd->command_header->byte_count = 0;
1170
1171 /* Issue the command to the hardware */
1172 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1173
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174 if (atomic == GFP_KERNEL) {
1175 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001176 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001177 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001178 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301179 if (rv == -ERESTARTSYS) { /* interrupted */
1180 dev_err(&dd->pdev->dev,
1181 "Internal command [%02X] was interrupted after %lu ms\n",
1182 fis->command, timeout);
1183 rv = -EINTR;
1184 goto exec_ic_exit;
1185 } else if (rv == 0) /* timeout */
1186 dev_err(&dd->pdev->dev,
1187 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1188 fis->command, timeout);
1189 else
1190 dev_err(&dd->pdev->dev,
1191 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1192 fis->command, rv, timeout);
1193
1194 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001195 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301196 &dd->dd_flag)) {
1197 dev_err(&dd->pdev->dev,
1198 "Internal command [%02X] wait returned due to SR\n",
1199 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001200 rv = -ENXIO;
1201 goto exec_ic_exit;
1202 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301203 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001204 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301205 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001206 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001207 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301208 u32 hba_stat, port_stat;
1209
Sam Bradshaw88523a62011-08-30 08:34:26 -06001210 /* Spin for <timeout> checking if command still outstanding */
1211 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001212 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1213 & (1 << MTIP_TAG_INTERNAL))
1214 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301215 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001216 rv = -ENXIO;
1217 goto exec_ic_exit;
1218 }
1219 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001220 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301221 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001222 rv = -ENXIO;
1223 goto exec_ic_exit;
1224 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301225 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1226 if (!port_stat)
1227 continue;
1228
1229 if (port_stat & PORT_IRQ_ERR) {
1230 dev_err(&dd->pdev->dev,
1231 "Internal command [%02X] failed\n",
1232 fis->command);
1233 mtip_device_reset(dd);
1234 rv = -EIO;
1235 goto exec_ic_exit;
1236 } else {
1237 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1238 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1239 if (hba_stat)
1240 writel(hba_stat,
1241 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001242 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301243 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001244 }
1245 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001246
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001247 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1248 & (1 << MTIP_TAG_INTERNAL)) {
1249 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301250 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1251 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001252 rv = -EAGAIN;
1253 }
1254 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001255exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001256 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001257 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001258 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001259 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1260 /* NCQ paused */
1261 return rv;
1262 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001263 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001264
1265 return rv;
1266}
1267
1268/*
1269 * Byte-swap ATA ID strings.
1270 *
1271 * ATA identify data contains strings in byte-swapped 16-bit words.
1272 * They must be swapped (on all architectures) to be usable as C strings.
1273 * This function swaps bytes in-place.
1274 *
1275 * @buf The buffer location of the string
1276 * @len The number of bytes to swap
1277 *
1278 * return value
1279 * None
1280 */
1281static inline void ata_swap_string(u16 *buf, unsigned int len)
1282{
1283 int i;
1284 for (i = 0; i < (len/2); i++)
1285 be16_to_cpus(&buf[i]);
1286}
1287
Asai Thambi S P670a6412014-04-16 20:27:54 -07001288static void mtip_set_timeout(struct driver_data *dd,
1289 struct host_to_dev_fis *fis,
1290 unsigned int *timeout, u8 erasemode)
1291{
1292 switch (fis->command) {
1293 case ATA_CMD_DOWNLOAD_MICRO:
1294 *timeout = 120000; /* 2 minutes */
1295 break;
1296 case ATA_CMD_SEC_ERASE_UNIT:
1297 case 0xFC:
1298 if (erasemode)
1299 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1300 else
1301 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1302 break;
1303 case ATA_CMD_STANDBYNOW1:
1304 *timeout = 120000; /* 2 minutes */
1305 break;
1306 case 0xF7:
1307 case 0xFA:
1308 *timeout = 60000; /* 60 seconds */
1309 break;
1310 case ATA_CMD_SMART:
1311 *timeout = 15000; /* 15 seconds */
1312 break;
1313 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001314 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001315 break;
1316 }
1317}
1318
Sam Bradshaw88523a62011-08-30 08:34:26 -06001319/*
1320 * Request the device identity information.
1321 *
1322 * If a user space buffer is not specified, i.e. is NULL, the
1323 * identify information is still read from the drive and placed
1324 * into the identify data buffer (@e port->identify) in the
1325 * port data structure.
1326 * When the identify buffer contains valid identify information @e
1327 * port->identify_valid is non-zero.
1328 *
1329 * @port Pointer to the port structure.
1330 * @user_buffer A user space buffer where the identify data should be
1331 * copied.
1332 *
1333 * return value
1334 * 0 Command completed successfully.
1335 * -EFAULT An error occurred while coping data to the user buffer.
1336 * -1 Command failed.
1337 */
1338static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1339{
1340 int rv = 0;
1341 struct host_to_dev_fis fis;
1342
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001343 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001344 return -EFAULT;
1345
Sam Bradshaw88523a62011-08-30 08:34:26 -06001346 /* Build the FIS. */
1347 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1348 fis.type = 0x27;
1349 fis.opts = 1 << 7;
1350 fis.command = ATA_CMD_ID_ATA;
1351
1352 /* Set the identify information as invalid. */
1353 port->identify_valid = 0;
1354
1355 /* Clear the identify information. */
1356 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1357
1358 /* Execute the command. */
1359 if (mtip_exec_internal_command(port,
1360 &fis,
1361 5,
1362 port->identify_dma,
1363 sizeof(u16) * ATA_ID_WORDS,
1364 0,
1365 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001366 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001367 < 0) {
1368 rv = -1;
1369 goto out;
1370 }
1371
1372 /*
1373 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1374 * perform field-sensitive swapping on the string fields.
1375 * See the kernel use of ata_id_string() for proof of this.
1376 */
1377#ifdef __LITTLE_ENDIAN
1378 ata_swap_string(port->identify + 27, 40); /* model string*/
1379 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1380 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1381#else
1382 {
1383 int i;
1384 for (i = 0; i < ATA_ID_WORDS; i++)
1385 port->identify[i] = le16_to_cpu(port->identify[i]);
1386 }
1387#endif
1388
Sam Bradshaw26d58052013-10-03 10:18:05 -07001389 /* Check security locked state */
1390 if (port->identify[128] & 0x4)
1391 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1392 else
1393 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1394
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301395#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001396 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1397 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1398 port->dd->trim_supp = true;
1399 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301400#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001401 port->dd->trim_supp = false;
1402
Sam Bradshaw88523a62011-08-30 08:34:26 -06001403 /* Set the identify buffer as valid. */
1404 port->identify_valid = 1;
1405
1406 if (user_buffer) {
1407 if (copy_to_user(
1408 user_buffer,
1409 port->identify,
1410 ATA_ID_WORDS * sizeof(u16))) {
1411 rv = -EFAULT;
1412 goto out;
1413 }
1414 }
1415
1416out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001417 return rv;
1418}
1419
1420/*
1421 * Issue a standby immediate command to the device.
1422 *
1423 * @port Pointer to the port structure.
1424 *
1425 * return value
1426 * 0 Command was executed successfully.
1427 * -1 An error occurred while executing the command.
1428 */
1429static int mtip_standby_immediate(struct mtip_port *port)
1430{
1431 int rv;
1432 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001433 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001434 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001435
Sam Bradshaw88523a62011-08-30 08:34:26 -06001436 /* Build the FIS. */
1437 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1438 fis.type = 0x27;
1439 fis.opts = 1 << 7;
1440 fis.command = ATA_CMD_STANDBYNOW1;
1441
Asai Thambi S P670a6412014-04-16 20:27:54 -07001442 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1443
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001444 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001445 rv = mtip_exec_internal_command(port,
1446 &fis,
1447 5,
1448 0,
1449 0,
1450 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001451 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001452 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001453 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1454 jiffies_to_msecs(jiffies - start));
1455 if (rv)
1456 dev_warn(&port->dd->pdev->dev,
1457 "STANDBY IMMEDIATE command failed.\n");
1458
1459 return rv;
1460}
1461
1462/*
1463 * Issue a READ LOG EXT command to the device.
1464 *
1465 * @port pointer to the port structure.
1466 * @page page number to fetch
1467 * @buffer pointer to buffer
1468 * @buffer_dma dma address corresponding to @buffer
1469 * @sectors page length to fetch, in sectors
1470 *
1471 * return value
1472 * @rv return value from mtip_exec_internal_command()
1473 */
1474static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1475 dma_addr_t buffer_dma, unsigned int sectors)
1476{
1477 struct host_to_dev_fis fis;
1478
1479 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1480 fis.type = 0x27;
1481 fis.opts = 1 << 7;
1482 fis.command = ATA_CMD_READ_LOG_EXT;
1483 fis.sect_count = sectors & 0xFF;
1484 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1485 fis.lba_low = page;
1486 fis.lba_mid = 0;
1487 fis.device = ATA_DEVICE_OBS;
1488
1489 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1490
1491 return mtip_exec_internal_command(port,
1492 &fis,
1493 5,
1494 buffer_dma,
1495 sectors * ATA_SECT_SIZE,
1496 0,
1497 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001498 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001499}
1500
1501/*
1502 * Issue a SMART READ DATA command to the device.
1503 *
1504 * @port pointer to the port structure.
1505 * @buffer pointer to buffer
1506 * @buffer_dma dma address corresponding to @buffer
1507 *
1508 * return value
1509 * @rv return value from mtip_exec_internal_command()
1510 */
1511static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1512 dma_addr_t buffer_dma)
1513{
1514 struct host_to_dev_fis fis;
1515
1516 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1517 fis.type = 0x27;
1518 fis.opts = 1 << 7;
1519 fis.command = ATA_CMD_SMART;
1520 fis.features = 0xD0;
1521 fis.sect_count = 1;
1522 fis.lba_mid = 0x4F;
1523 fis.lba_hi = 0xC2;
1524 fis.device = ATA_DEVICE_OBS;
1525
1526 return mtip_exec_internal_command(port,
1527 &fis,
1528 5,
1529 buffer_dma,
1530 ATA_SECT_SIZE,
1531 0,
1532 GFP_ATOMIC,
1533 15000);
1534}
1535
1536/*
1537 * Get the value of a smart attribute
1538 *
1539 * @port pointer to the port structure
1540 * @id attribute number
1541 * @attrib pointer to return attrib information corresponding to @id
1542 *
1543 * return value
1544 * -EINVAL NULL buffer passed or unsupported attribute @id.
1545 * -EPERM Identify data not valid, SMART not supported or not enabled
1546 */
1547static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1548 struct smart_attr *attrib)
1549{
1550 int rv, i;
1551 struct smart_attr *pattr;
1552
1553 if (!attrib)
1554 return -EINVAL;
1555
1556 if (!port->identify_valid) {
1557 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1558 return -EPERM;
1559 }
1560 if (!(port->identify[82] & 0x1)) {
1561 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1562 return -EPERM;
1563 }
1564 if (!(port->identify[85] & 0x1)) {
1565 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1566 return -EPERM;
1567 }
1568
1569 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1570 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1571 if (rv) {
1572 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1573 return rv;
1574 }
1575
1576 pattr = (struct smart_attr *)(port->smart_buf + 2);
1577 for (i = 0; i < 29; i++, pattr++)
1578 if (pattr->attr_id == id) {
1579 memcpy(attrib, pattr, sizeof(struct smart_attr));
1580 break;
1581 }
1582
1583 if (i == 29) {
1584 dev_warn(&port->dd->pdev->dev,
1585 "Query for invalid SMART attribute ID\n");
1586 rv = -EINVAL;
1587 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001588
Sam Bradshaw88523a62011-08-30 08:34:26 -06001589 return rv;
1590}
1591
1592/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001593 * Trim unused sectors
1594 *
1595 * @dd pointer to driver_data structure
1596 * @lba starting lba
1597 * @len # of 512b sectors to trim
1598 *
1599 * return value
1600 * -ENOMEM Out of dma memory
1601 * -EINVAL Invalid parameters passed in, trim not supported
1602 * -EIO Error submitting trim request to hw
1603 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301604static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1605 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001606{
1607 int i, rv = 0;
1608 u64 tlba, tlen, sect_left;
1609 struct mtip_trim_entry *buf;
1610 dma_addr_t dma_addr;
1611 struct host_to_dev_fis fis;
1612
1613 if (!len || dd->trim_supp == false)
1614 return -EINVAL;
1615
1616 /* Trim request too big */
1617 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1618
1619 /* Trim request not aligned on 4k boundary */
1620 WARN_ON(len % 8 != 0);
1621
1622 /* Warn if vu_trim structure is too big */
1623 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1624
1625 /* Allocate a DMA buffer for the trim structure */
1626 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1627 GFP_KERNEL);
1628 if (!buf)
1629 return -ENOMEM;
1630 memset(buf, 0, ATA_SECT_SIZE);
1631
1632 for (i = 0, sect_left = len, tlba = lba;
1633 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1634 i++) {
1635 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1636 MTIP_MAX_TRIM_ENTRY_LEN :
1637 sect_left);
1638 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1639 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1640 tlba += tlen;
1641 sect_left -= tlen;
1642 }
1643 WARN_ON(sect_left != 0);
1644
1645 /* Build the fis */
1646 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1647 fis.type = 0x27;
1648 fis.opts = 1 << 7;
1649 fis.command = 0xfb;
1650 fis.features = 0x60;
1651 fis.sect_count = 1;
1652 fis.device = ATA_DEVICE_OBS;
1653
1654 if (mtip_exec_internal_command(dd->port,
1655 &fis,
1656 5,
1657 dma_addr,
1658 ATA_SECT_SIZE,
1659 0,
1660 GFP_KERNEL,
1661 MTIP_TRIM_TIMEOUT_MS) < 0)
1662 rv = -EIO;
1663
1664 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1665 return rv;
1666}
1667
1668/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001669 * Get the drive capacity.
1670 *
1671 * @dd Pointer to the device data structure.
1672 * @sectors Pointer to the variable that will receive the sector count.
1673 *
1674 * return value
1675 * 1 Capacity was returned successfully.
1676 * 0 The identify information is invalid.
1677 */
Jens Axboe63166682011-09-27 21:27:43 -06001678static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679{
1680 struct mtip_port *port = dd->port;
1681 u64 total, raw0, raw1, raw2, raw3;
1682 raw0 = port->identify[100];
1683 raw1 = port->identify[101];
1684 raw2 = port->identify[102];
1685 raw3 = port->identify[103];
1686 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1687 *sectors = total;
1688 return (bool) !!port->identify_valid;
1689}
1690
1691/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001692 * Display the identify command data.
1693 *
1694 * @port Pointer to the port data structure.
1695 *
1696 * return value
1697 * None
1698 */
1699static void mtip_dump_identify(struct mtip_port *port)
1700{
1701 sector_t sectors;
1702 unsigned short revid;
1703 char cbuf[42];
1704
1705 if (!port->identify_valid)
1706 return;
1707
1708 strlcpy(cbuf, (char *)(port->identify+10), 21);
1709 dev_info(&port->dd->pdev->dev,
1710 "Serial No.: %s\n", cbuf);
1711
1712 strlcpy(cbuf, (char *)(port->identify+23), 9);
1713 dev_info(&port->dd->pdev->dev,
1714 "Firmware Ver.: %s\n", cbuf);
1715
1716 strlcpy(cbuf, (char *)(port->identify+27), 41);
1717 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1718
Sam Bradshaw26d58052013-10-03 10:18:05 -07001719 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1720 port->identify[128],
1721 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1722
Sam Bradshaw88523a62011-08-30 08:34:26 -06001723 if (mtip_hw_get_capacity(port->dd, &sectors))
1724 dev_info(&port->dd->pdev->dev,
1725 "Capacity: %llu sectors (%llu MB)\n",
1726 (u64)sectors,
1727 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1728
1729 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001730 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001731 case 0x1:
1732 strlcpy(cbuf, "A0", 3);
1733 break;
1734 case 0x3:
1735 strlcpy(cbuf, "A2", 3);
1736 break;
1737 default:
1738 strlcpy(cbuf, "?", 2);
1739 break;
1740 }
1741 dev_info(&port->dd->pdev->dev,
1742 "Card Type: %s\n", cbuf);
1743}
1744
1745/*
1746 * Map the commands scatter list into the command table.
1747 *
1748 * @command Pointer to the command.
1749 * @nents Number of scatter list entries.
1750 *
1751 * return value
1752 * None
1753 */
1754static inline void fill_command_sg(struct driver_data *dd,
1755 struct mtip_cmd *command,
1756 int nents)
1757{
1758 int n;
1759 unsigned int dma_len;
1760 struct mtip_cmd_sg *command_sg;
1761 struct scatterlist *sg = command->sg;
1762
1763 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1764
1765 for (n = 0; n < nents; n++) {
1766 dma_len = sg_dma_len(sg);
1767 if (dma_len > 0x400000)
1768 dev_err(&dd->pdev->dev,
1769 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001770 command_sg->info = __force_bit2int
1771 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1772 command_sg->dba = __force_bit2int
1773 cpu_to_le32(sg_dma_address(sg));
1774 command_sg->dba_upper = __force_bit2int
1775 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001776 command_sg++;
1777 sg++;
1778 }
1779}
1780
1781/*
1782 * @brief Execute a drive command.
1783 *
1784 * return value 0 The command completed successfully.
1785 * return value -1 An error occurred while executing the command.
1786 */
Jens Axboe63166682011-09-27 21:27:43 -06001787static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001788{
1789 struct host_to_dev_fis fis;
1790 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001791 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001792
Sam Bradshaw88523a62011-08-30 08:34:26 -06001793 /* Build the FIS. */
1794 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1795 fis.type = 0x27;
1796 fis.opts = 1 << 7;
1797 fis.command = command[0];
1798 fis.features = command[1];
1799 fis.sect_count = command[2];
1800 fis.sector = command[3];
1801 fis.cyl_low = command[4];
1802 fis.cyl_hi = command[5];
1803 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1804
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001805 mtip_set_timeout(port->dd, &fis, &to, 0);
1806
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001807 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 -06001808 __func__,
1809 command[0],
1810 command[1],
1811 command[2],
1812 command[3],
1813 command[4],
1814 command[5],
1815 command[6]);
1816
1817 /* Execute the command. */
1818 if (mtip_exec_internal_command(port,
1819 &fis,
1820 5,
1821 0,
1822 0,
1823 0,
1824 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001825 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001826 return -1;
1827 }
1828
1829 command[0] = reply->command; /* Status*/
1830 command[1] = reply->features; /* Error*/
1831 command[4] = reply->cyl_low;
1832 command[5] = reply->cyl_hi;
1833
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001834 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 -06001835 __func__,
1836 command[0],
1837 command[1],
1838 command[4],
1839 command[5]);
1840
Sam Bradshaw88523a62011-08-30 08:34:26 -06001841 return 0;
1842}
1843
1844/*
1845 * @brief Execute a drive command.
1846 *
1847 * @param port Pointer to the port data structure.
1848 * @param command Pointer to the user specified command parameters.
1849 * @param user_buffer Pointer to the user space buffer where read sector
1850 * data should be copied.
1851 *
1852 * return value 0 The command completed successfully.
1853 * return value -EFAULT An error occurred while copying the completion
1854 * data to the user space buffer.
1855 * return value -1 An error occurred while executing the command.
1856 */
Jens Axboe63166682011-09-27 21:27:43 -06001857static int exec_drive_command(struct mtip_port *port, u8 *command,
1858 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001859{
1860 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001861 struct host_to_dev_fis *reply;
1862 u8 *buf = NULL;
1863 dma_addr_t dma_addr = 0;
1864 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001865 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001866
1867 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001868 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001869 return -EFAULT;
1870
1871 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1872 ATA_SECT_SIZE * xfer_sz,
1873 &dma_addr,
1874 GFP_KERNEL);
1875 if (!buf) {
1876 dev_err(&port->dd->pdev->dev,
1877 "Memory allocation failed (%d bytes)\n",
1878 ATA_SECT_SIZE * xfer_sz);
1879 return -ENOMEM;
1880 }
1881 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1882 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001883
Sam Bradshaw88523a62011-08-30 08:34:26 -06001884 /* Build the FIS. */
1885 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001886 fis.type = 0x27;
1887 fis.opts = 1 << 7;
1888 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001889 fis.features = command[2];
1890 fis.sect_count = command[3];
1891 if (fis.command == ATA_CMD_SMART) {
1892 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001893 fis.cyl_low = 0x4F;
1894 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001895 }
1896
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001897 mtip_set_timeout(port->dd, &fis, &to, 0);
1898
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001899 if (xfer_sz)
1900 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1901 else
1902 reply = (port->rxfis + RX_FIS_D2H_REG);
1903
Sam Bradshaw88523a62011-08-30 08:34:26 -06001904 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001905 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001906 "feat %x, sectcnt %x\n",
1907 __func__,
1908 command[0],
1909 command[1],
1910 command[2],
1911 command[3]);
1912
Sam Bradshaw88523a62011-08-30 08:34:26 -06001913 /* Execute the command. */
1914 if (mtip_exec_internal_command(port,
1915 &fis,
1916 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001917 (xfer_sz ? dma_addr : 0),
1918 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001919 0,
1920 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001921 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001923 rv = -EFAULT;
1924 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001925 }
1926
1927 /* Collect the completion status. */
1928 command[0] = reply->command; /* Status*/
1929 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001930 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001931
1932 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001933 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001934 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001935 __func__,
1936 command[0],
1937 command[1],
1938 command[2]);
1939
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001940 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001941 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001942 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001944 rv = -EFAULT;
1945 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001946 }
1947 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001948exit_drive_command:
1949 if (buf)
1950 dmam_free_coherent(&port->dd->pdev->dev,
1951 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1952 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001953}
1954
1955/*
1956 * Indicates whether a command has a single sector payload.
1957 *
1958 * @command passed to the device to perform the certain event.
1959 * @features passed to the device to perform the certain event.
1960 *
1961 * return value
1962 * 1 command is one that always has a single sector payload,
1963 * regardless of the value in the Sector Count field.
1964 * 0 otherwise
1965 *
1966 */
1967static unsigned int implicit_sector(unsigned char command,
1968 unsigned char features)
1969{
1970 unsigned int rv = 0;
1971
1972 /* list of commands that have an implicit sector count of 1 */
1973 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001974 case ATA_CMD_SEC_SET_PASS:
1975 case ATA_CMD_SEC_UNLOCK:
1976 case ATA_CMD_SEC_ERASE_PREP:
1977 case ATA_CMD_SEC_ERASE_UNIT:
1978 case ATA_CMD_SEC_FREEZE_LOCK:
1979 case ATA_CMD_SEC_DISABLE_PASS:
1980 case ATA_CMD_PMP_READ:
1981 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001982 rv = 1;
1983 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001984 case ATA_CMD_SET_MAX:
1985 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001986 rv = 1;
1987 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001988 case ATA_CMD_SMART:
1989 if ((features == ATA_SMART_READ_VALUES) ||
1990 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001991 rv = 1;
1992 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001993 case ATA_CMD_CONF_OVERLAY:
1994 if ((features == ATA_DCO_IDENTIFY) ||
1995 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001996 rv = 1;
1997 break;
1998 }
1999 return rv;
2000}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002001
Sam Bradshaw88523a62011-08-30 08:34:26 -06002002/*
2003 * Executes a taskfile
2004 * See ide_taskfile_ioctl() for derivation
2005 */
2006static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002007 void __user *buf,
2008 ide_task_request_t *req_task,
2009 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010{
2011 struct host_to_dev_fis fis;
2012 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002013 u8 *outbuf = NULL;
2014 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002015 dma_addr_t outbuf_dma = 0;
2016 dma_addr_t inbuf_dma = 0;
2017 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002018 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002019 unsigned int taskin = 0;
2020 unsigned int taskout = 0;
2021 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002022 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002023 unsigned int force_single_sector;
2024 unsigned int transfer_size;
2025 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002026 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002027 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002028
2029 taskout = req_task->out_size;
2030 taskin = req_task->in_size;
2031 /* 130560 = 512 * 0xFF*/
2032 if (taskin > 130560 || taskout > 130560) {
2033 err = -EINVAL;
2034 goto abort;
2035 }
2036
2037 if (taskout) {
2038 outbuf = kzalloc(taskout, GFP_KERNEL);
2039 if (outbuf == NULL) {
2040 err = -ENOMEM;
2041 goto abort;
2042 }
2043 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2044 err = -EFAULT;
2045 goto abort;
2046 }
2047 outbuf_dma = pci_map_single(dd->pdev,
2048 outbuf,
2049 taskout,
2050 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002051 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002052 err = -ENOMEM;
2053 goto abort;
2054 }
2055 dma_buffer = outbuf_dma;
2056 }
2057
2058 if (taskin) {
2059 inbuf = kzalloc(taskin, GFP_KERNEL);
2060 if (inbuf == NULL) {
2061 err = -ENOMEM;
2062 goto abort;
2063 }
2064
2065 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2066 err = -EFAULT;
2067 goto abort;
2068 }
2069 inbuf_dma = pci_map_single(dd->pdev,
2070 inbuf,
2071 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002072 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002073 err = -ENOMEM;
2074 goto abort;
2075 }
2076 dma_buffer = inbuf_dma;
2077 }
2078
2079 /* only supports PIO and non-data commands from this ioctl. */
2080 switch (req_task->data_phase) {
2081 case TASKFILE_OUT:
2082 nsect = taskout / ATA_SECT_SIZE;
2083 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2084 break;
2085 case TASKFILE_IN:
2086 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2087 break;
2088 case TASKFILE_NO_DATA:
2089 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2090 break;
2091 default:
2092 err = -EINVAL;
2093 goto abort;
2094 }
2095
Sam Bradshaw88523a62011-08-30 08:34:26 -06002096 /* Build the FIS. */
2097 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2098
2099 fis.type = 0x27;
2100 fis.opts = 1 << 7;
2101 fis.command = req_task->io_ports[7];
2102 fis.features = req_task->io_ports[1];
2103 fis.sect_count = req_task->io_ports[2];
2104 fis.lba_low = req_task->io_ports[3];
2105 fis.lba_mid = req_task->io_ports[4];
2106 fis.lba_hi = req_task->io_ports[5];
2107 /* Clear the dev bit*/
2108 fis.device = req_task->io_ports[6] & ~0x10;
2109
2110 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2111 req_task->in_flags.all =
2112 IDE_TASKFILE_STD_IN_FLAGS |
2113 (IDE_HOB_STD_IN_FLAGS << 8);
2114 fis.lba_low_ex = req_task->hob_ports[3];
2115 fis.lba_mid_ex = req_task->hob_ports[4];
2116 fis.lba_hi_ex = req_task->hob_ports[5];
2117 fis.features_ex = req_task->hob_ports[1];
2118 fis.sect_cnt_ex = req_task->hob_ports[2];
2119
2120 } else {
2121 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2122 }
2123
2124 force_single_sector = implicit_sector(fis.command, fis.features);
2125
2126 if ((taskin || taskout) && (!fis.sect_count)) {
2127 if (nsect)
2128 fis.sect_count = nsect;
2129 else {
2130 if (!force_single_sector) {
2131 dev_warn(&dd->pdev->dev,
2132 "data movement but "
2133 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002134 err = -EINVAL;
2135 goto abort;
2136 }
2137 }
2138 }
2139
2140 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002141 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2143 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002144 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002145 fis.command,
2146 fis.features,
2147 fis.sect_count,
2148 fis.lba_low,
2149 fis.lba_mid,
2150 fis.lba_hi,
2151 fis.device);
2152
Selvan Mani4453bc82012-09-27 14:36:43 +02002153 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002154 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2155 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002156 erasemode = 1;
2157 }
2158
2159 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002160
2161 /* Determine the correct transfer size.*/
2162 if (force_single_sector)
2163 transfer_size = ATA_SECT_SIZE;
2164 else
2165 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2166
2167 /* Execute the command.*/
2168 if (mtip_exec_internal_command(dd->port,
2169 &fis,
2170 5,
2171 dma_buffer,
2172 transfer_size,
2173 0,
2174 GFP_KERNEL,
2175 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002176 err = -EIO;
2177 goto abort;
2178 }
2179
2180 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2181
2182 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2183 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2184 req_task->io_ports[7] = reply->control;
2185 } else {
2186 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2187 req_task->io_ports[7] = reply->command;
2188 }
2189
2190 /* reclaim the DMA buffers.*/
2191 if (inbuf_dma)
2192 pci_unmap_single(dd->pdev, inbuf_dma,
2193 taskin, DMA_FROM_DEVICE);
2194 if (outbuf_dma)
2195 pci_unmap_single(dd->pdev, outbuf_dma,
2196 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002197 inbuf_dma = 0;
2198 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002199
2200 /* return the ATA registers to the caller.*/
2201 req_task->io_ports[1] = reply->features;
2202 req_task->io_ports[2] = reply->sect_count;
2203 req_task->io_ports[3] = reply->lba_low;
2204 req_task->io_ports[4] = reply->lba_mid;
2205 req_task->io_ports[5] = reply->lba_hi;
2206 req_task->io_ports[6] = reply->device;
2207
2208 if (req_task->out_flags.all & 1) {
2209
2210 req_task->hob_ports[3] = reply->lba_low_ex;
2211 req_task->hob_ports[4] = reply->lba_mid_ex;
2212 req_task->hob_ports[5] = reply->lba_hi_ex;
2213 req_task->hob_ports[1] = reply->features_ex;
2214 req_task->hob_ports[2] = reply->sect_cnt_ex;
2215 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002216 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002217 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002218 "err %x, sect_cnt %x, lbalo %x,"
2219 "lbamid %x, lbahi %x, dev %x\n",
2220 __func__,
2221 req_task->io_ports[7],
2222 req_task->io_ports[1],
2223 req_task->io_ports[2],
2224 req_task->io_ports[3],
2225 req_task->io_ports[4],
2226 req_task->io_ports[5],
2227 req_task->io_ports[6]);
2228
Sam Bradshaw88523a62011-08-30 08:34:26 -06002229 if (taskout) {
2230 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2231 err = -EFAULT;
2232 goto abort;
2233 }
2234 }
2235 if (taskin) {
2236 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2237 err = -EFAULT;
2238 goto abort;
2239 }
2240 }
2241abort:
2242 if (inbuf_dma)
2243 pci_unmap_single(dd->pdev, inbuf_dma,
2244 taskin, DMA_FROM_DEVICE);
2245 if (outbuf_dma)
2246 pci_unmap_single(dd->pdev, outbuf_dma,
2247 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002248 kfree(outbuf);
2249 kfree(inbuf);
2250
2251 return err;
2252}
2253
2254/*
2255 * Handle IOCTL calls from the Block Layer.
2256 *
2257 * This function is called by the Block Layer when it receives an IOCTL
2258 * command that it does not understand. If the IOCTL command is not supported
2259 * this function returns -ENOTTY.
2260 *
2261 * @dd Pointer to the driver data structure.
2262 * @cmd IOCTL command passed from the Block Layer.
2263 * @arg IOCTL argument passed from the Block Layer.
2264 *
2265 * return value
2266 * 0 The IOCTL completed successfully.
2267 * -ENOTTY The specified command is not supported.
2268 * -EFAULT An error occurred copying data to a user space buffer.
2269 * -EIO An error occurred while executing the command.
2270 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002271static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2272 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002273{
2274 switch (cmd) {
2275 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002276 {
2277 if (copy_to_user((void __user *)arg, dd->port->identify,
2278 sizeof(u16) * ATA_ID_WORDS))
2279 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002280 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002281 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002282 case HDIO_DRIVE_CMD:
2283 {
2284 u8 drive_command[4];
2285
2286 /* Copy the user command info to our buffer. */
2287 if (copy_from_user(drive_command,
2288 (void __user *) arg,
2289 sizeof(drive_command)))
2290 return -EFAULT;
2291
2292 /* Execute the drive command. */
2293 if (exec_drive_command(dd->port,
2294 drive_command,
2295 (void __user *) (arg+4)))
2296 return -EIO;
2297
2298 /* Copy the status back to the users buffer. */
2299 if (copy_to_user((void __user *) arg,
2300 drive_command,
2301 sizeof(drive_command)))
2302 return -EFAULT;
2303
2304 break;
2305 }
2306 case HDIO_DRIVE_TASK:
2307 {
2308 u8 drive_command[7];
2309
2310 /* Copy the user command info to our buffer. */
2311 if (copy_from_user(drive_command,
2312 (void __user *) arg,
2313 sizeof(drive_command)))
2314 return -EFAULT;
2315
2316 /* Execute the drive command. */
2317 if (exec_drive_task(dd->port, drive_command))
2318 return -EIO;
2319
2320 /* Copy the status back to the users buffer. */
2321 if (copy_to_user((void __user *) arg,
2322 drive_command,
2323 sizeof(drive_command)))
2324 return -EFAULT;
2325
2326 break;
2327 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002328 case HDIO_DRIVE_TASKFILE: {
2329 ide_task_request_t req_task;
2330 int ret, outtotal;
2331
2332 if (copy_from_user(&req_task, (void __user *) arg,
2333 sizeof(req_task)))
2334 return -EFAULT;
2335
2336 outtotal = sizeof(req_task);
2337
2338 ret = exec_drive_taskfile(dd, (void __user *) arg,
2339 &req_task, outtotal);
2340
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002341 if (copy_to_user((void __user *) arg, &req_task,
2342 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002343 return -EFAULT;
2344
2345 return ret;
2346 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002347
2348 default:
2349 return -EINVAL;
2350 }
2351 return 0;
2352}
2353
2354/*
2355 * Submit an IO to the hw
2356 *
2357 * This function is called by the block layer to issue an io
2358 * to the device. Upon completion, the callback function will
2359 * be called with the data parameter passed as the callback data.
2360 *
2361 * @dd Pointer to the driver data structure.
2362 * @start First sector to read.
2363 * @nsect Number of sectors to read.
2364 * @nents Number of entries in scatter list for the read command.
2365 * @tag The tag of this read command.
2366 * @callback Pointer to the function that should be called
2367 * when the read completes.
2368 * @data Callback data passed to the callback function
2369 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002370 * @dir Direction (read or write)
2371 *
2372 * return value
2373 * None
2374 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002375static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2376 struct mtip_cmd *command, int nents,
2377 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002378{
2379 struct host_to_dev_fis *fis;
2380 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002381 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2382 u64 start = blk_rq_pos(rq);
2383 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002384
2385 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002386 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002387
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002388 prefetch(&port->flags);
2389
Sam Bradshaw88523a62011-08-30 08:34:26 -06002390 command->scatter_ents = nents;
2391
2392 /*
2393 * The number of retries for this command before it is
2394 * reported as a failure to the upper layers.
2395 */
2396 command->retries = MTIP_MAX_RETRIES;
2397
2398 /* Fill out fis */
2399 fis = command->command;
2400 fis->type = 0x27;
2401 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002402 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002403 fis->command = ATA_CMD_FPDMA_READ;
2404 else
2405 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002406 fis->lba_low = start & 0xFF;
2407 fis->lba_mid = (start >> 8) & 0xFF;
2408 fis->lba_hi = (start >> 16) & 0xFF;
2409 fis->lba_low_ex = (start >> 24) & 0xFF;
2410 fis->lba_mid_ex = (start >> 32) & 0xFF;
2411 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002412 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002413 fis->features = nsect & 0xFF;
2414 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002415 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002416 fis->sect_cnt_ex = 0;
2417 fis->control = 0;
2418 fis->res2 = 0;
2419 fis->res3 = 0;
2420 fill_command_sg(dd, command, nents);
2421
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002422 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002423 fis->device |= 1 << 7;
2424
Sam Bradshaw88523a62011-08-30 08:34:26 -06002425 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002426 command->command_header->opts =
2427 __force_bit2int cpu_to_le32(
2428 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002429 command->command_header->byte_count = 0;
2430
2431 /*
2432 * Set the completion function and data for the command
2433 * within this layer.
2434 */
2435 command->comp_data = dd;
2436 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002437 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002438
2439 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002440 * To prevent this command from being issued
2441 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002442 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002443 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002444 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002445 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002446 return;
2447 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002448
2449 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002450 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002451}
2452
2453/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002454 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002455 *
2456 * @dev Pointer to the device structure, passed by the kernrel.
2457 * @attr Pointer to the device_attribute structure passed by the kernel.
2458 * @buf Pointer to the char buffer that will receive the stats info.
2459 *
2460 * return value
2461 * The size, in bytes, of the data copied into buf.
2462 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002463static ssize_t mtip_hw_show_status(struct device *dev,
2464 struct device_attribute *attr,
2465 char *buf)
2466{
2467 struct driver_data *dd = dev_to_disk(dev)->private_data;
2468 int size = 0;
2469
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002470 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002471 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002472 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002473 size += sprintf(buf, "%s", "write_protect\n");
2474 else
2475 size += sprintf(buf, "%s", "online\n");
2476
2477 return size;
2478}
2479
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002480static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002481
Asai Thambi S P0caff002013-04-03 19:56:21 +05302482/* debugsfs entries */
2483
2484static ssize_t show_device_status(struct device_driver *drv, char *buf)
2485{
2486 int size = 0;
2487 struct driver_data *dd, *tmp;
2488 unsigned long flags;
2489 char id_buf[42];
2490 u16 status = 0;
2491
2492 spin_lock_irqsave(&dev_lock, flags);
2493 size += sprintf(&buf[size], "Devices Present:\n");
2494 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002495 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302496 if (dd->port &&
2497 dd->port->identify &&
2498 dd->port->identify_valid) {
2499 strlcpy(id_buf,
2500 (char *) (dd->port->identify + 10), 21);
2501 status = *(dd->port->identify + 141);
2502 } else {
2503 memset(id_buf, 0, 42);
2504 status = 0;
2505 }
2506
2507 if (dd->port &&
2508 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2509 size += sprintf(&buf[size],
2510 " device %s %s (ftl rebuild %d %%)\n",
2511 dev_name(&dd->pdev->dev),
2512 id_buf,
2513 status);
2514 } else {
2515 size += sprintf(&buf[size],
2516 " device %s %s\n",
2517 dev_name(&dd->pdev->dev),
2518 id_buf);
2519 }
2520 }
2521 }
2522
2523 size += sprintf(&buf[size], "Devices Being Removed:\n");
2524 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002525 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302526 if (dd->port &&
2527 dd->port->identify &&
2528 dd->port->identify_valid) {
2529 strlcpy(id_buf,
2530 (char *) (dd->port->identify+10), 21);
2531 status = *(dd->port->identify + 141);
2532 } else {
2533 memset(id_buf, 0, 42);
2534 status = 0;
2535 }
2536
2537 if (dd->port &&
2538 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2539 size += sprintf(&buf[size],
2540 " device %s %s (ftl rebuild %d %%)\n",
2541 dev_name(&dd->pdev->dev),
2542 id_buf,
2543 status);
2544 } else {
2545 size += sprintf(&buf[size],
2546 " device %s %s\n",
2547 dev_name(&dd->pdev->dev),
2548 id_buf);
2549 }
2550 }
2551 }
2552 spin_unlock_irqrestore(&dev_lock, flags);
2553
2554 return size;
2555}
2556
2557static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2558 size_t len, loff_t *offset)
2559{
David Milburnc8afd0d2013-05-23 16:23:45 -05002560 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002562 char *buf;
2563 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302564
2565 if (!len || *offset)
2566 return 0;
2567
David Milburnc8afd0d2013-05-23 16:23:45 -05002568 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2569 if (!buf) {
2570 dev_err(&dd->pdev->dev,
2571 "Memory allocation: status buffer\n");
2572 return -ENOMEM;
2573 }
2574
Asai Thambi S P0caff002013-04-03 19:56:21 +05302575 size += show_device_status(NULL, buf);
2576
2577 *offset = size <= len ? size : len;
2578 size = copy_to_user(ubuf, buf, *offset);
2579 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002580 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302581
David Milburnc8afd0d2013-05-23 16:23:45 -05002582 kfree(buf);
2583 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302584}
2585
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002586static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2587 size_t len, loff_t *offset)
2588{
2589 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002590 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002591 u32 group_allocated;
2592 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002593 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002594
2595 if (!len || size)
2596 return 0;
2597
David Milburnc8afd0d2013-05-23 16:23:45 -05002598 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2599 if (!buf) {
2600 dev_err(&dd->pdev->dev,
2601 "Memory allocation: register buffer\n");
2602 return -ENOMEM;
2603 }
2604
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002605 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2606
2607 for (n = dd->slot_groups-1; n >= 0; n--)
2608 size += sprintf(&buf[size], "%08X ",
2609 readl(dd->port->s_active[n]));
2610
2611 size += sprintf(&buf[size], "]\n");
2612 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--)
2615 size += sprintf(&buf[size], "%08X ",
2616 readl(dd->port->cmd_issue[n]));
2617
2618 size += sprintf(&buf[size], "]\n");
2619 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2620
2621 for (n = dd->slot_groups-1; n >= 0; n--)
2622 size += sprintf(&buf[size], "%08X ",
2623 readl(dd->port->completed[n]));
2624
2625 size += sprintf(&buf[size], "]\n");
2626 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2627 readl(dd->port->mmio + PORT_IRQ_STAT));
2628 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2629 readl(dd->mmio + HOST_IRQ_STAT));
2630 size += sprintf(&buf[size], "\n");
2631
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002632 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2633
2634 for (n = dd->slot_groups-1; n >= 0; n--) {
2635 if (sizeof(long) > sizeof(u32))
2636 group_allocated =
2637 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2638 else
2639 group_allocated = dd->port->cmds_to_issue[n];
2640 size += sprintf(&buf[size], "%08X ", group_allocated);
2641 }
2642 size += sprintf(&buf[size], "]\n");
2643
2644 *offset = size <= len ? size : len;
2645 size = copy_to_user(ubuf, buf, *offset);
2646 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002647 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002648
David Milburnc8afd0d2013-05-23 16:23:45 -05002649 kfree(buf);
2650 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002651}
2652
2653static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2654 size_t len, loff_t *offset)
2655{
2656 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002657 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002658 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002659 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002660
2661 if (!len || size)
2662 return 0;
2663
David Milburnc8afd0d2013-05-23 16:23:45 -05002664 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2665 if (!buf) {
2666 dev_err(&dd->pdev->dev,
2667 "Memory allocation: flag buffer\n");
2668 return -ENOMEM;
2669 }
2670
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002671 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2672 dd->port->flags);
2673 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2674 dd->dd_flag);
2675
2676 *offset = size <= len ? size : len;
2677 size = copy_to_user(ubuf, buf, *offset);
2678 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002679 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002680
David Milburnc8afd0d2013-05-23 16:23:45 -05002681 kfree(buf);
2682 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002683}
2684
Asai Thambi S P0caff002013-04-03 19:56:21 +05302685static const struct file_operations mtip_device_status_fops = {
2686 .owner = THIS_MODULE,
2687 .open = simple_open,
2688 .read = mtip_hw_read_device_status,
2689 .llseek = no_llseek,
2690};
2691
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002692static const struct file_operations mtip_regs_fops = {
2693 .owner = THIS_MODULE,
2694 .open = simple_open,
2695 .read = mtip_hw_read_registers,
2696 .llseek = no_llseek,
2697};
2698
2699static const struct file_operations mtip_flags_fops = {
2700 .owner = THIS_MODULE,
2701 .open = simple_open,
2702 .read = mtip_hw_read_flags,
2703 .llseek = no_llseek,
2704};
2705
Sam Bradshaw88523a62011-08-30 08:34:26 -06002706/*
2707 * Create the sysfs related attributes.
2708 *
2709 * @dd Pointer to the driver data structure.
2710 * @kobj Pointer to the kobj for the block device.
2711 *
2712 * return value
2713 * 0 Operation completed successfully.
2714 * -EINVAL Invalid parameter.
2715 */
Jens Axboe63166682011-09-27 21:27:43 -06002716static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002717{
2718 if (!kobj || !dd)
2719 return -EINVAL;
2720
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002721 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2722 dev_warn(&dd->pdev->dev,
2723 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002724 return 0;
2725}
2726
2727/*
2728 * Remove the sysfs related attributes.
2729 *
2730 * @dd Pointer to the driver data structure.
2731 * @kobj Pointer to the kobj for the block device.
2732 *
2733 * return value
2734 * 0 Operation completed successfully.
2735 * -EINVAL Invalid parameter.
2736 */
Jens Axboe63166682011-09-27 21:27:43 -06002737static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002738{
2739 if (!kobj || !dd)
2740 return -EINVAL;
2741
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002742 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002743
2744 return 0;
2745}
2746
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002747static int mtip_hw_debugfs_init(struct driver_data *dd)
2748{
2749 if (!dfs_parent)
2750 return -1;
2751
2752 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2753 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2754 dev_warn(&dd->pdev->dev,
2755 "Error creating node %s under debugfs\n",
2756 dd->disk->disk_name);
2757 dd->dfs_node = NULL;
2758 return -1;
2759 }
2760
2761 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2762 &mtip_flags_fops);
2763 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2764 &mtip_regs_fops);
2765
2766 return 0;
2767}
2768
2769static void mtip_hw_debugfs_exit(struct driver_data *dd)
2770{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002771 if (dd->dfs_node)
2772 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002773}
2774
Sam Bradshaw88523a62011-08-30 08:34:26 -06002775/*
2776 * Perform any init/resume time hardware setup
2777 *
2778 * @dd Pointer to the driver data structure.
2779 *
2780 * return value
2781 * None
2782 */
2783static inline void hba_setup(struct driver_data *dd)
2784{
2785 u32 hwdata;
2786 hwdata = readl(dd->mmio + HOST_HSORG);
2787
2788 /* interrupt bug workaround: use only 1 IS bit.*/
2789 writel(hwdata |
2790 HSORG_DISABLE_SLOTGRP_INTR |
2791 HSORG_DISABLE_SLOTGRP_PXIS,
2792 dd->mmio + HOST_HSORG);
2793}
2794
Asai Thambi S P2077d942013-04-29 21:19:49 +02002795static int mtip_device_unaligned_constrained(struct driver_data *dd)
2796{
2797 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2798}
2799
Sam Bradshaw88523a62011-08-30 08:34:26 -06002800/*
2801 * Detect the details of the product, and store anything needed
2802 * into the driver data structure. This includes product type and
2803 * version and number of slot groups.
2804 *
2805 * @dd Pointer to the driver data structure.
2806 *
2807 * return value
2808 * None
2809 */
2810static void mtip_detect_product(struct driver_data *dd)
2811{
2812 u32 hwdata;
2813 unsigned int rev, slotgroups;
2814
2815 /*
2816 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2817 * info register:
2818 * [15:8] hardware/software interface rev#
2819 * [ 3] asic-style interface
2820 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2821 */
2822 hwdata = readl(dd->mmio + HOST_HSORG);
2823
2824 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2825 dd->slot_groups = 1;
2826
2827 if (hwdata & 0x8) {
2828 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2829 rev = (hwdata & HSORG_HWREV) >> 8;
2830 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2831 dev_info(&dd->pdev->dev,
2832 "ASIC-FPGA design, HS rev 0x%x, "
2833 "%i slot groups [%i slots]\n",
2834 rev,
2835 slotgroups,
2836 slotgroups * 32);
2837
2838 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2839 dev_warn(&dd->pdev->dev,
2840 "Warning: driver only supports "
2841 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2842 slotgroups = MTIP_MAX_SLOT_GROUPS;
2843 }
2844 dd->slot_groups = slotgroups;
2845 return;
2846 }
2847
2848 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2849}
2850
2851/*
2852 * Blocking wait for FTL rebuild to complete
2853 *
2854 * @dd Pointer to the DRIVER_DATA structure.
2855 *
2856 * return value
2857 * 0 FTL rebuild completed successfully
2858 * -EFAULT FTL rebuild error/timeout/interruption
2859 */
2860static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2861{
2862 unsigned long timeout, cnt = 0, start;
2863
2864 dev_warn(&dd->pdev->dev,
2865 "FTL rebuild in progress. Polling for completion.\n");
2866
2867 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002868 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2869
2870 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002871 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002872 &dd->dd_flag)))
2873 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002874 if (mtip_check_surprise_removal(dd->pdev))
2875 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002876
Sam Bradshaw88523a62011-08-30 08:34:26 -06002877 if (mtip_get_identify(dd->port, NULL) < 0)
2878 return -EFAULT;
2879
2880 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2881 MTIP_FTL_REBUILD_MAGIC) {
2882 ssleep(1);
2883 /* Print message every 3 minutes */
2884 if (cnt++ >= 180) {
2885 dev_warn(&dd->pdev->dev,
2886 "FTL rebuild in progress (%d secs).\n",
2887 jiffies_to_msecs(jiffies - start) / 1000);
2888 cnt = 0;
2889 }
2890 } else {
2891 dev_warn(&dd->pdev->dev,
2892 "FTL rebuild complete (%d secs).\n",
2893 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002894 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002895 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002896 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002897 } while (time_before(jiffies, timeout));
2898
2899 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002900 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002901 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2902 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002903 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002904}
2905
2906/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002907 * service thread to issue queued commands
2908 *
2909 * @data Pointer to the driver data structure.
2910 *
2911 * return value
2912 * 0
2913 */
2914
2915static int mtip_service_thread(void *data)
2916{
2917 struct driver_data *dd = (struct driver_data *)data;
2918 unsigned long slot, slot_start, slot_wrap;
2919 unsigned int num_cmd_slots = dd->slot_groups * 32;
2920 struct mtip_port *port = dd->port;
2921
2922 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002923 if (kthread_should_stop() ||
2924 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2925 goto st_out;
2926 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2927
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002928 /*
2929 * the condition is to check neither an internal command is
2930 * is in progress nor error handling is active
2931 */
2932 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002933 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002934
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002935 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2936
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002937 if (kthread_should_stop() ||
2938 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2939 goto st_out;
2940
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002941 /* If I am an orphan, start self cleanup */
2942 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002943 break;
2944
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002945 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002946 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002947 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002948
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002949restart_eh:
2950 /* Demux bits: start with error handling */
2951 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2952 mtip_handle_tfe(dd);
2953 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2954 }
2955
2956 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2957 goto restart_eh;
2958
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002959 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002960 slot = 1;
2961 /* used to restrict the loop to one iteration */
2962 slot_start = num_cmd_slots;
2963 slot_wrap = 0;
2964 while (1) {
2965 slot = find_next_bit(port->cmds_to_issue,
2966 num_cmd_slots, slot);
2967 if (slot_wrap == 1) {
2968 if ((slot_start >= slot) ||
2969 (slot >= num_cmd_slots))
2970 break;
2971 }
2972 if (unlikely(slot_start == num_cmd_slots))
2973 slot_start = slot;
2974
2975 if (unlikely(slot == num_cmd_slots)) {
2976 slot = 1;
2977 slot_wrap = 1;
2978 continue;
2979 }
2980
2981 /* Issue the command to the hardware */
2982 mtip_issue_ncq_command(port, slot);
2983
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002984 clear_bit(slot, port->cmds_to_issue);
2985 }
2986
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002987 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002988 }
2989
2990 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002991 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002992 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002993 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002994 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002995 }
2996 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002997
2998 /* wait for pci remove to exit */
2999 while (1) {
3000 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3001 break;
3002 msleep_interruptible(1000);
3003 if (kthread_should_stop())
3004 goto st_out;
3005 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003006st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003007 return 0;
3008}
3009
3010/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003011 * DMA region teardown
3012 *
3013 * @dd Pointer to driver_data structure
3014 *
3015 * return value
3016 * None
3017 */
3018static void mtip_dma_free(struct driver_data *dd)
3019{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003020 struct mtip_port *port = dd->port;
3021
3022 if (port->block1)
3023 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3024 port->block1, port->block1_dma);
3025
3026 if (port->command_list) {
3027 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3028 port->command_list, port->command_list_dma);
3029 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003030}
3031
3032/*
3033 * DMA region setup
3034 *
3035 * @dd Pointer to driver_data structure
3036 *
3037 * return value
3038 * -ENOMEM Not enough free DMA region space to initialize driver
3039 */
3040static int mtip_dma_alloc(struct driver_data *dd)
3041{
3042 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003043
3044 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3045 port->block1 =
3046 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3047 &port->block1_dma, GFP_KERNEL);
3048 if (!port->block1)
3049 return -ENOMEM;
3050 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3051
3052 /* Allocate dma memory for command list */
3053 port->command_list =
3054 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3055 &port->command_list_dma, GFP_KERNEL);
3056 if (!port->command_list) {
3057 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3058 port->block1, port->block1_dma);
3059 port->block1 = NULL;
3060 port->block1_dma = 0;
3061 return -ENOMEM;
3062 }
3063 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3064
3065 /* Setup all pointers into first DMA region */
3066 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3067 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3068 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3069 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3070 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3071 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3072 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3073 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3074
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003075 return 0;
3076}
3077
Jens Axboeffc771b2014-05-09 09:42:02 -06003078static int mtip_hw_get_identify(struct driver_data *dd)
3079{
3080 struct smart_attr attr242;
3081 unsigned char *buf;
3082 int rv;
3083
3084 if (mtip_get_identify(dd->port, NULL) < 0)
3085 return -EFAULT;
3086
3087 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3088 MTIP_FTL_REBUILD_MAGIC) {
3089 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3090 return MTIP_FTL_REBUILD_MAGIC;
3091 }
3092 mtip_dump_identify(dd->port);
3093
3094 /* check write protect, over temp and rebuild statuses */
3095 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3096 dd->port->log_buf,
3097 dd->port->log_buf_dma, 1);
3098 if (rv) {
3099 dev_warn(&dd->pdev->dev,
3100 "Error in READ LOG EXT (10h) command\n");
3101 /* non-critical error, don't fail the load */
3102 } else {
3103 buf = (unsigned char *)dd->port->log_buf;
3104 if (buf[259] & 0x1) {
3105 dev_info(&dd->pdev->dev,
3106 "Write protect bit is set.\n");
3107 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3108 }
3109 if (buf[288] == 0xF7) {
3110 dev_info(&dd->pdev->dev,
3111 "Exceeded Tmax, drive in thermal shutdown.\n");
3112 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3113 }
3114 if (buf[288] == 0xBF) {
3115 dev_info(&dd->pdev->dev,
3116 "Drive indicates rebuild has failed.\n");
3117 /* TODO */
3118 }
3119 }
3120
3121 /* get write protect progess */
3122 memset(&attr242, 0, sizeof(struct smart_attr));
3123 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3124 dev_warn(&dd->pdev->dev,
3125 "Unable to check write protect progress\n");
3126 else
3127 dev_info(&dd->pdev->dev,
3128 "Write protect progress: %u%% (%u blocks)\n",
3129 attr242.cur, le32_to_cpu(attr242.data));
3130
3131 return rv;
3132}
3133
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003134/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003135 * Called once for each card.
3136 *
3137 * @dd Pointer to the driver data structure.
3138 *
3139 * return value
3140 * 0 on success, else an error code.
3141 */
Jens Axboe63166682011-09-27 21:27:43 -06003142static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003143{
3144 int i;
3145 int rv;
3146 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003147 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003148
3149 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3150
3151 mtip_detect_product(dd);
3152 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3153 rv = -EIO;
3154 goto out1;
3155 }
3156 num_command_slots = dd->slot_groups * 32;
3157
3158 hba_setup(dd);
3159
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003160 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3161 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003162 if (!dd->port) {
3163 dev_err(&dd->pdev->dev,
3164 "Memory allocation: port structure\n");
3165 return -ENOMEM;
3166 }
3167
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003168 /* Continue workqueue setup */
3169 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3170 dd->work[i].port = dd->port;
3171
Asai Thambi S P2077d942013-04-29 21:19:49 +02003172 /* Enable unaligned IO constraints for some devices */
3173 if (mtip_device_unaligned_constrained(dd))
3174 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3175 else
3176 dd->unal_qdepth = 0;
3177
Asai Thambi S P2077d942013-04-29 21:19:49 +02003178 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003179
3180 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003181 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3182 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003183
3184 /* Set the port mmio base address. */
3185 dd->port->mmio = dd->mmio + PORT_OFFSET;
3186 dd->port->dd = dd;
3187
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003188 /* DMA allocations */
3189 rv = mtip_dma_alloc(dd);
3190 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003191 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003192
3193 /* Setup the pointers to the extended s_active and CI registers. */
3194 for (i = 0; i < dd->slot_groups; i++) {
3195 dd->port->s_active[i] =
3196 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3197 dd->port->cmd_issue[i] =
3198 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3199 dd->port->completed[i] =
3200 dd->port->mmio + i*0x80 + PORT_SDBV;
3201 }
3202
Asai Thambi S P45038362012-04-09 08:35:38 +02003203 timetaken = jiffies;
3204 timeout = jiffies + msecs_to_jiffies(30000);
3205 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3206 time_before(jiffies, timeout)) {
3207 mdelay(100);
3208 }
3209 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3210 timetaken = jiffies - timetaken;
3211 dev_warn(&dd->pdev->dev,
3212 "Surprise removal detected at %u ms\n",
3213 jiffies_to_msecs(timetaken));
3214 rv = -ENODEV;
3215 goto out2 ;
3216 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003217 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003218 timetaken = jiffies - timetaken;
3219 dev_warn(&dd->pdev->dev,
3220 "Removal detected at %u ms\n",
3221 jiffies_to_msecs(timetaken));
3222 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003223 goto out2;
3224 }
3225
Asai Thambi S P45038362012-04-09 08:35:38 +02003226 /* Conditionally reset the HBA. */
3227 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3228 if (mtip_hba_reset(dd) < 0) {
3229 dev_err(&dd->pdev->dev,
3230 "Card did not reset within timeout\n");
3231 rv = -EIO;
3232 goto out2;
3233 }
3234 } else {
3235 /* Clear any pending interrupts on the HBA */
3236 writel(readl(dd->mmio + HOST_IRQ_STAT),
3237 dd->mmio + HOST_IRQ_STAT);
3238 }
3239
Sam Bradshaw88523a62011-08-30 08:34:26 -06003240 mtip_init_port(dd->port);
3241 mtip_start_port(dd->port);
3242
3243 /* Setup the ISR and enable interrupts. */
3244 rv = devm_request_irq(&dd->pdev->dev,
3245 dd->pdev->irq,
3246 mtip_irq_handler,
3247 IRQF_SHARED,
3248 dev_driver_string(&dd->pdev->dev),
3249 dd);
3250
3251 if (rv) {
3252 dev_err(&dd->pdev->dev,
3253 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3254 goto out2;
3255 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003256 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003257
3258 /* Enable interrupts on the HBA. */
3259 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3260 dd->mmio + HOST_CTL);
3261
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003262 init_waitqueue_head(&dd->port->svc_wait);
3263
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003264 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003265 rv = -EFAULT;
3266 goto out3;
3267 }
3268
Sam Bradshaw88523a62011-08-30 08:34:26 -06003269 return rv;
3270
3271out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003272 /* Disable interrupts on the HBA. */
3273 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3274 dd->mmio + HOST_CTL);
3275
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003276 /* Release the IRQ. */
3277 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003278 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3279
3280out2:
3281 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003282 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003283
Sam Bradshaw88523a62011-08-30 08:34:26 -06003284out1:
3285 /* Free the memory allocated for the for structure. */
3286 kfree(dd->port);
3287
3288 return rv;
3289}
3290
Jens Axboeffc771b2014-05-09 09:42:02 -06003291static void mtip_standby_drive(struct driver_data *dd)
3292{
3293 if (dd->sr)
3294 return;
3295
3296 /*
3297 * Send standby immediate (E0h) to the drive so that it
3298 * saves its state.
3299 */
3300 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3301 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3302 if (mtip_standby_immediate(dd->port))
3303 dev_warn(&dd->pdev->dev,
3304 "STANDBY IMMEDIATE failed\n");
3305}
3306
Sam Bradshaw88523a62011-08-30 08:34:26 -06003307/*
3308 * Called to deinitialize an interface.
3309 *
3310 * @dd Pointer to the driver data structure.
3311 *
3312 * return value
3313 * 0
3314 */
Jens Axboe63166682011-09-27 21:27:43 -06003315static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003316{
3317 /*
3318 * Send standby immediate (E0h) to the drive so that it
3319 * saves its state.
3320 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003321 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003322 /* de-initialize the port. */
3323 mtip_deinit_port(dd->port);
3324
3325 /* Disable interrupts on the HBA. */
3326 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3327 dd->mmio + HOST_CTL);
3328 }
3329
Sam Bradshaw88523a62011-08-30 08:34:26 -06003330 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003331 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003332 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003333 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003334
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003335 /* Free dma regions */
3336 mtip_dma_free(dd);
3337
Sam Bradshaw88523a62011-08-30 08:34:26 -06003338 /* Free the memory allocated for the for structure. */
3339 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003340 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003341
3342 return 0;
3343}
3344
3345/*
3346 * Issue a Standby Immediate command to the device.
3347 *
3348 * This function is called by the Block Layer just before the
3349 * system powers off during a shutdown.
3350 *
3351 * @dd Pointer to the driver data structure.
3352 *
3353 * return value
3354 * 0
3355 */
Jens Axboe63166682011-09-27 21:27:43 -06003356static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003357{
3358 /*
3359 * Send standby immediate (E0h) to the drive so that it
3360 * saves its state.
3361 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003362 if (!dd->sr && dd->port)
3363 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003364
3365 return 0;
3366}
3367
3368/*
3369 * Suspend function
3370 *
3371 * This function is called by the Block Layer just before the
3372 * system hibernates.
3373 *
3374 * @dd Pointer to the driver data structure.
3375 *
3376 * return value
3377 * 0 Suspend was successful
3378 * -EFAULT Suspend was not successful
3379 */
Jens Axboe63166682011-09-27 21:27:43 -06003380static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003381{
3382 /*
3383 * Send standby immediate (E0h) to the drive
3384 * so that it saves its state.
3385 */
3386 if (mtip_standby_immediate(dd->port) != 0) {
3387 dev_err(&dd->pdev->dev,
3388 "Failed standby-immediate command\n");
3389 return -EFAULT;
3390 }
3391
3392 /* Disable interrupts on the HBA.*/
3393 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3394 dd->mmio + HOST_CTL);
3395 mtip_deinit_port(dd->port);
3396
3397 return 0;
3398}
3399
3400/*
3401 * Resume function
3402 *
3403 * This function is called by the Block Layer as the
3404 * system resumes.
3405 *
3406 * @dd Pointer to the driver data structure.
3407 *
3408 * return value
3409 * 0 Resume was successful
3410 * -EFAULT Resume was not successful
3411 */
Jens Axboe63166682011-09-27 21:27:43 -06003412static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003413{
3414 /* Perform any needed hardware setup steps */
3415 hba_setup(dd);
3416
3417 /* Reset the HBA */
3418 if (mtip_hba_reset(dd) != 0) {
3419 dev_err(&dd->pdev->dev,
3420 "Unable to reset the HBA\n");
3421 return -EFAULT;
3422 }
3423
3424 /*
3425 * Enable the port, DMA engine, and FIS reception specific
3426 * h/w in controller.
3427 */
3428 mtip_init_port(dd->port);
3429 mtip_start_port(dd->port);
3430
3431 /* Enable interrupts on the HBA.*/
3432 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3433 dd->mmio + HOST_CTL);
3434
3435 return 0;
3436}
3437
3438/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003439 * Helper function for reusing disk name
3440 * upon hot insertion.
3441 */
3442static int rssd_disk_name_format(char *prefix,
3443 int index,
3444 char *buf,
3445 int buflen)
3446{
3447 const int base = 'z' - 'a' + 1;
3448 char *begin = buf + strlen(prefix);
3449 char *end = buf + buflen;
3450 char *p;
3451 int unit;
3452
3453 p = end - 1;
3454 *p = '\0';
3455 unit = base;
3456 do {
3457 if (p == begin)
3458 return -EINVAL;
3459 *--p = 'a' + (index % unit);
3460 index = (index / unit) - 1;
3461 } while (index >= 0);
3462
3463 memmove(begin, p, end - p);
3464 memcpy(buf, prefix, strlen(prefix));
3465
3466 return 0;
3467}
3468
3469/*
3470 * Block layer IOCTL handler.
3471 *
3472 * @dev Pointer to the block_device structure.
3473 * @mode ignored
3474 * @cmd IOCTL command passed from the user application.
3475 * @arg Argument passed from the user application.
3476 *
3477 * return value
3478 * 0 IOCTL completed successfully.
3479 * -ENOTTY IOCTL not supported or invalid driver data
3480 * structure pointer.
3481 */
3482static int mtip_block_ioctl(struct block_device *dev,
3483 fmode_t mode,
3484 unsigned cmd,
3485 unsigned long arg)
3486{
3487 struct driver_data *dd = dev->bd_disk->private_data;
3488
3489 if (!capable(CAP_SYS_ADMIN))
3490 return -EACCES;
3491
3492 if (!dd)
3493 return -ENOTTY;
3494
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003495 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003496 return -ENOTTY;
3497
Sam Bradshaw88523a62011-08-30 08:34:26 -06003498 switch (cmd) {
3499 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003500 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003501 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003502 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003503 }
3504}
3505
Jens Axboe16d02c02011-09-27 15:50:01 -06003506#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003507/*
3508 * Block layer compat IOCTL handler.
3509 *
3510 * @dev Pointer to the block_device structure.
3511 * @mode ignored
3512 * @cmd IOCTL command passed from the user application.
3513 * @arg Argument passed from the user application.
3514 *
3515 * return value
3516 * 0 IOCTL completed successfully.
3517 * -ENOTTY IOCTL not supported or invalid driver data
3518 * structure pointer.
3519 */
3520static int mtip_block_compat_ioctl(struct block_device *dev,
3521 fmode_t mode,
3522 unsigned cmd,
3523 unsigned long arg)
3524{
3525 struct driver_data *dd = dev->bd_disk->private_data;
3526
3527 if (!capable(CAP_SYS_ADMIN))
3528 return -EACCES;
3529
3530 if (!dd)
3531 return -ENOTTY;
3532
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003533 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003534 return -ENOTTY;
3535
Sam Bradshaw88523a62011-08-30 08:34:26 -06003536 switch (cmd) {
3537 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003538 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003539 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003540 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003541 ide_task_request_t req_task;
3542 int compat_tasksize, outtotal, ret;
3543
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003544 compat_tasksize =
3545 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003546
3547 compat_req_task =
3548 (struct mtip_compat_ide_task_request_s __user *) arg;
3549
3550 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003551 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003552 return -EFAULT;
3553
3554 if (get_user(req_task.out_size, &compat_req_task->out_size))
3555 return -EFAULT;
3556
3557 if (get_user(req_task.in_size, &compat_req_task->in_size))
3558 return -EFAULT;
3559
3560 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3561
3562 ret = exec_drive_taskfile(dd, (void __user *) arg,
3563 &req_task, outtotal);
3564
3565 if (copy_to_user((void __user *) arg, &req_task,
3566 compat_tasksize -
3567 (2 * sizeof(compat_long_t))))
3568 return -EFAULT;
3569
3570 if (put_user(req_task.out_size, &compat_req_task->out_size))
3571 return -EFAULT;
3572
3573 if (put_user(req_task.in_size, &compat_req_task->in_size))
3574 return -EFAULT;
3575
3576 return ret;
3577 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003578 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003579 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003580 }
3581}
Jens Axboe16d02c02011-09-27 15:50:01 -06003582#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003583
3584/*
3585 * Obtain the geometry of the device.
3586 *
3587 * You may think that this function is obsolete, but some applications,
3588 * fdisk for example still used CHS values. This function describes the
3589 * device as having 224 heads and 56 sectors per cylinder. These values are
3590 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3591 * partition is described in terms of a start and end cylinder this means
3592 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3593 * affects performance.
3594 *
3595 * @dev Pointer to the block_device strucutre.
3596 * @geo Pointer to a hd_geometry structure.
3597 *
3598 * return value
3599 * 0 Operation completed successfully.
3600 * -ENOTTY An error occurred while reading the drive capacity.
3601 */
3602static int mtip_block_getgeo(struct block_device *dev,
3603 struct hd_geometry *geo)
3604{
3605 struct driver_data *dd = dev->bd_disk->private_data;
3606 sector_t capacity;
3607
3608 if (!dd)
3609 return -ENOTTY;
3610
3611 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3612 dev_warn(&dd->pdev->dev,
3613 "Could not get drive capacity.\n");
3614 return -ENOTTY;
3615 }
3616
3617 geo->heads = 224;
3618 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003619 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003620 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003621 return 0;
3622}
3623
3624/*
3625 * Block device operation function.
3626 *
3627 * This structure contains pointers to the functions required by the block
3628 * layer.
3629 */
3630static const struct block_device_operations mtip_block_ops = {
3631 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003632#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003633 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003634#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003635 .getgeo = mtip_block_getgeo,
3636 .owner = THIS_MODULE
3637};
3638
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003639static inline bool is_se_active(struct driver_data *dd)
3640{
3641 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3642 if (dd->port->ic_pause_timer) {
3643 unsigned long to = dd->port->ic_pause_timer +
3644 msecs_to_jiffies(1000);
3645 if (time_after(jiffies, to)) {
3646 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3647 &dd->port->flags);
3648 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3649 dd->port->ic_pause_timer = 0;
3650 wake_up_interruptible(&dd->port->svc_wait);
3651 return false;
3652 }
3653 }
3654 return true;
3655 }
3656 return false;
3657}
3658
Sam Bradshaw88523a62011-08-30 08:34:26 -06003659/*
3660 * Block layer make request function.
3661 *
3662 * This function is called by the kernel to process a BIO for
3663 * the P320 device.
3664 *
3665 * @queue Pointer to the request queue. Unused other than to obtain
3666 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003667 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003668 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003669 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003670static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003671{
Jens Axboeffc771b2014-05-09 09:42:02 -06003672 struct driver_data *dd = hctx->queue->queuedata;
3673 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3674 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003675
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003676 if (is_se_active(dd))
3677 return -ENODATA;
3678
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003679 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3680 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3681 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003682 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003683 }
3684 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003685 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003686 }
3687 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3688 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003689 rq_data_dir(rq))) {
3690 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003691 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003692 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3693 return -ENODATA;
3694 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3695 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003696 }
3697
Jens Axboeffc771b2014-05-09 09:42:02 -06003698 if (rq->cmd_flags & REQ_DISCARD) {
3699 int err;
3700
3701 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003702 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003703 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003704 }
3705
Jens Axboeffc771b2014-05-09 09:42:02 -06003706 /* Create the scatter list for this request. */
3707 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003708
Jens Axboeffc771b2014-05-09 09:42:02 -06003709 /* Issue the read/write. */
3710 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3711 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003712}
3713
Jens Axboeffc771b2014-05-09 09:42:02 -06003714static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3715 struct request *rq)
3716{
3717 struct driver_data *dd = hctx->queue->queuedata;
3718 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3719
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003720 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003721 return false;
3722
3723 /*
3724 * If unaligned depth must be limited on this controller, mark it
3725 * as unaligned if the IO isn't on a 4k boundary (start of length).
3726 */
3727 if (blk_rq_sectors(rq) <= 64) {
3728 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3729 cmd->unaligned = 1;
3730 }
3731
3732 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3733 return true;
3734
3735 return false;
3736}
3737
Jens Axboe74c45052014-10-29 11:14:52 -06003738static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3739 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003740{
Jens Axboe74c45052014-10-29 11:14:52 -06003741 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003742 int ret;
3743
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003744 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003745 return BLK_MQ_RQ_QUEUE_BUSY;
3746
Christoph Hellwige2490072014-09-13 16:40:09 -07003747 blk_mq_start_request(rq);
3748
Jens Axboeffc771b2014-05-09 09:42:02 -06003749 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003750 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003751 return BLK_MQ_RQ_QUEUE_OK;
3752
3753 rq->errors = ret;
3754 return BLK_MQ_RQ_QUEUE_ERROR;
3755}
3756
3757static void mtip_free_cmd(void *data, struct request *rq,
3758 unsigned int hctx_idx, unsigned int request_idx)
3759{
3760 struct driver_data *dd = data;
3761 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3762
3763 if (!cmd->command)
3764 return;
3765
3766 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3767 cmd->command, cmd->command_dma);
3768}
3769
3770static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3771 unsigned int request_idx, unsigned int numa_node)
3772{
3773 struct driver_data *dd = data;
3774 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3775 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3776
3777 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3778 &cmd->command_dma, GFP_KERNEL);
3779 if (!cmd->command)
3780 return -ENOMEM;
3781
3782 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3783
3784 /* Point the command headers at the command tables. */
3785 cmd->command_header = dd->port->command_list +
3786 (sizeof(struct mtip_cmd_hdr) * request_idx);
3787 cmd->command_header_dma = dd->port->command_list_dma +
3788 (sizeof(struct mtip_cmd_hdr) * request_idx);
3789
3790 if (host_cap_64)
3791 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3792
3793 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3794
3795 sg_init_table(cmd->sg, MTIP_MAX_SG);
3796 return 0;
3797}
3798
3799static struct blk_mq_ops mtip_mq_ops = {
3800 .queue_rq = mtip_queue_rq,
3801 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003802 .init_request = mtip_init_cmd,
3803 .exit_request = mtip_free_cmd,
3804};
3805
Sam Bradshaw88523a62011-08-30 08:34:26 -06003806/*
3807 * Block layer initialization function.
3808 *
3809 * This function is called once by the PCI layer for each P320
3810 * device that is connected to the system.
3811 *
3812 * @dd Pointer to the driver data structure.
3813 *
3814 * return value
3815 * 0 on success else an error code.
3816 */
Jens Axboe63166682011-09-27 21:27:43 -06003817static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003818{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003819 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003820 sector_t capacity;
3821 unsigned int index = 0;
3822 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003823 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003824
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003825 if (dd->disk)
3826 goto skip_create_disk; /* hw init done, before rebuild */
3827
Jens Axboeffc771b2014-05-09 09:42:02 -06003828 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003829 rv = -EINVAL;
3830 goto protocol_init_error;
3831 }
3832
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003833 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003834 if (dd->disk == NULL) {
3835 dev_err(&dd->pdev->dev,
3836 "Unable to allocate gendisk structure\n");
3837 rv = -EINVAL;
3838 goto alloc_disk_error;
3839 }
3840
3841 /* Generate the disk name, implemented same as in sd.c */
3842 do {
3843 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3844 goto ida_get_error;
3845
3846 spin_lock(&rssd_index_lock);
3847 rv = ida_get_new(&rssd_index_ida, &index);
3848 spin_unlock(&rssd_index_lock);
3849 } while (rv == -EAGAIN);
3850
3851 if (rv)
3852 goto ida_get_error;
3853
3854 rv = rssd_disk_name_format("rssd",
3855 index,
3856 dd->disk->disk_name,
3857 DISK_NAME_LEN);
3858 if (rv)
3859 goto disk_index_error;
3860
3861 dd->disk->driverfs_dev = &dd->pdev->dev;
3862 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003863 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3864 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003865 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003866 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003867 dd->index = index;
3868
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003869 mtip_hw_debugfs_init(dd);
3870
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003871skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003872 memset(&dd->tags, 0, sizeof(dd->tags));
3873 dd->tags.ops = &mtip_mq_ops;
3874 dd->tags.nr_hw_queues = 1;
3875 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3876 dd->tags.reserved_tags = 1;
3877 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3878 dd->tags.numa_node = dd->numa_node;
3879 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3880 dd->tags.driver_data = dd;
3881
3882 rv = blk_mq_alloc_tag_set(&dd->tags);
3883 if (rv) {
3884 dev_err(&dd->pdev->dev,
3885 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003886 goto block_queue_alloc_init_error;
3887 }
3888
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003889 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003890 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003891 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003892 dev_err(&dd->pdev->dev,
3893 "Unable to allocate request queue\n");
3894 rv = -ENOMEM;
3895 goto block_queue_alloc_init_error;
3896 }
3897
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003898 dd->disk->queue = dd->queue;
3899 dd->queue->queuedata = dd;
3900
Jens Axboeffc771b2014-05-09 09:42:02 -06003901 /* Initialize the protocol layer. */
3902 wait_for_rebuild = mtip_hw_get_identify(dd);
3903 if (wait_for_rebuild < 0) {
3904 dev_err(&dd->pdev->dev,
3905 "Protocol layer initialization failed\n");
3906 rv = -EINVAL;
3907 goto init_hw_cmds_error;
3908 }
3909
3910 /*
3911 * if rebuild pending, start the service thread, and delay the block
3912 * queue creation and add_disk()
3913 */
3914 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3915 goto start_service_thread;
3916
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003917 /* Set device limits. */
3918 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003919 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003920 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3921 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003922 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3923 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003924 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003925 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003926
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003927 /*
3928 * write back cache is not supported in the device. FUA depends on
3929 * write back cache support, hence setting flush support to zero.
3930 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003931 blk_queue_flush(dd->queue, 0);
3932
Asai Thambi S P15283462013-01-11 14:41:34 +01003933 /* Signal trim support */
3934 if (dd->trim_supp == true) {
3935 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3936 dd->queue->limits.discard_granularity = 4096;
3937 blk_queue_max_discard_sectors(dd->queue,
3938 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3939 dd->queue->limits.discard_zeroes_data = 0;
3940 }
3941
Sam Bradshaw88523a62011-08-30 08:34:26 -06003942 /* Set the capacity of the device in 512 byte sectors. */
3943 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3944 dev_warn(&dd->pdev->dev,
3945 "Could not read drive capacity\n");
3946 rv = -EIO;
3947 goto read_capacity_error;
3948 }
3949 set_capacity(dd->disk, capacity);
3950
3951 /* Enable the block device and add it to /dev */
3952 add_disk(dd->disk);
3953
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003954 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003955 /*
3956 * Now that the disk is active, initialize any sysfs attributes
3957 * managed by the protocol layer.
3958 */
3959 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3960 if (kobj) {
3961 mtip_hw_sysfs_init(dd, kobj);
3962 kobject_put(kobj);
3963 }
3964
Asai Thambi S P45038362012-04-09 08:35:38 +02003965 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003966 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003967 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003968 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003969
3970start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003971 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003972 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07003973 dd, dd->numa_node, "%s",
3974 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003975
3976 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003977 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003978 dd->mtip_svc_handler = NULL;
3979 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003980 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003981 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003982 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003983 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3984 rv = wait_for_rebuild;
3985
Sam Bradshaw88523a62011-08-30 08:34:26 -06003986 return rv;
3987
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003988kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003989 bdput(dd->bdev);
3990 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003991
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003992 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003993 del_gendisk(dd->disk);
3994
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003995read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003996init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003997 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06003998 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003999block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004000 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004001disk_index_error:
4002 spin_lock(&rssd_index_lock);
4003 ida_remove(&rssd_index_ida, index);
4004 spin_unlock(&rssd_index_lock);
4005
4006ida_get_error:
4007 put_disk(dd->disk);
4008
4009alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004010 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004011
4012protocol_init_error:
4013 return rv;
4014}
4015
4016/*
4017 * Block layer deinitialization function.
4018 *
4019 * Called by the PCI layer as each P320 device is removed.
4020 *
4021 * @dd Pointer to the driver data structure.
4022 *
4023 * return value
4024 * 0
4025 */
Jens Axboe63166682011-09-27 21:27:43 -06004026static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004027{
4028 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004029
Asai Thambi SP2132a542015-05-11 15:53:18 -07004030 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004031
Asai Thambi SP2132a542015-05-11 15:53:18 -07004032 if (dd->mtip_svc_handler) {
4033 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4034 wake_up_interruptible(&dd->port->svc_wait);
4035 kthread_stop(dd->mtip_svc_handler);
4036 }
4037
4038 /* Clean up the sysfs attributes, if created */
4039 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4040 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4041 if (kobj) {
4042 mtip_hw_sysfs_exit(dd, kobj);
4043 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004044 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004045 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004046
Asai Thambi SP2132a542015-05-11 15:53:18 -07004047 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004048 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004049 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004050 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4051 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004052
4053 /*
4054 * Delete our gendisk structure. This also removes the device
4055 * from /dev
4056 */
4057 if (dd->bdev) {
4058 bdput(dd->bdev);
4059 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004060 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004061 if (dd->disk) {
4062 del_gendisk(dd->disk);
4063 if (dd->disk->queue) {
4064 blk_cleanup_queue(dd->queue);
4065 blk_mq_free_tag_set(&dd->tags);
4066 dd->queue = NULL;
4067 }
4068 put_disk(dd->disk);
4069 }
4070 dd->disk = NULL;
4071
4072 spin_lock(&rssd_index_lock);
4073 ida_remove(&rssd_index_ida, dd->index);
4074 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004075
4076 /* De-initialize the protocol layer. */
4077 mtip_hw_exit(dd);
4078
4079 return 0;
4080}
4081
4082/*
4083 * Function called by the PCI layer when just before the
4084 * machine shuts down.
4085 *
4086 * If a protocol layer shutdown function is present it will be called
4087 * by this function.
4088 *
4089 * @dd Pointer to the driver data structure.
4090 *
4091 * return value
4092 * 0
4093 */
Jens Axboe63166682011-09-27 21:27:43 -06004094static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004095{
Jens Axboeffc771b2014-05-09 09:42:02 -06004096 mtip_hw_shutdown(dd);
4097
Sam Bradshaw88523a62011-08-30 08:34:26 -06004098 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304099 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304100 dev_info(&dd->pdev->dev,
4101 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304102
Asai Thambi SP02b48262015-05-11 15:48:00 -07004103 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304104 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304105 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004106 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004107 }
4108 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304109 dd->disk = NULL;
4110 dd->queue = NULL;
4111 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004112
4113 spin_lock(&rssd_index_lock);
4114 ida_remove(&rssd_index_ida, dd->index);
4115 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004116 return 0;
4117}
4118
Jens Axboe63166682011-09-27 21:27:43 -06004119static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004120{
4121 dev_info(&dd->pdev->dev,
4122 "Suspending %s ...\n", dd->disk->disk_name);
4123 mtip_hw_suspend(dd);
4124 return 0;
4125}
4126
Jens Axboe63166682011-09-27 21:27:43 -06004127static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004128{
4129 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4130 dd->disk->disk_name);
4131 mtip_hw_resume(dd);
4132 return 0;
4133}
4134
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004135static void drop_cpu(int cpu)
4136{
4137 cpu_use[cpu]--;
4138}
4139
4140static int get_least_used_cpu_on_node(int node)
4141{
4142 int cpu, least_used_cpu, least_cnt;
4143 const struct cpumask *node_mask;
4144
4145 node_mask = cpumask_of_node(node);
4146 least_used_cpu = cpumask_first(node_mask);
4147 least_cnt = cpu_use[least_used_cpu];
4148 cpu = least_used_cpu;
4149
4150 for_each_cpu(cpu, node_mask) {
4151 if (cpu_use[cpu] < least_cnt) {
4152 least_used_cpu = cpu;
4153 least_cnt = cpu_use[cpu];
4154 }
4155 }
4156 cpu_use[least_used_cpu]++;
4157 return least_used_cpu;
4158}
4159
4160/* Helper for selecting a node in round robin mode */
4161static inline int mtip_get_next_rr_node(void)
4162{
4163 static int next_node = -1;
4164
4165 if (next_node == -1) {
4166 next_node = first_online_node;
4167 return next_node;
4168 }
4169
4170 next_node = next_online_node(next_node);
4171 if (next_node == MAX_NUMNODES)
4172 next_node = first_online_node;
4173 return next_node;
4174}
4175
Fengguang Wu25bac122013-01-12 15:31:40 +08004176static DEFINE_HANDLER(0);
4177static DEFINE_HANDLER(1);
4178static DEFINE_HANDLER(2);
4179static DEFINE_HANDLER(3);
4180static DEFINE_HANDLER(4);
4181static DEFINE_HANDLER(5);
4182static DEFINE_HANDLER(6);
4183static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004184
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004185static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4186{
4187 int pos;
4188 unsigned short pcie_dev_ctrl;
4189
4190 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4191 if (pos) {
4192 pci_read_config_word(pdev,
4193 pos + PCI_EXP_DEVCTL,
4194 &pcie_dev_ctrl);
4195 if (pcie_dev_ctrl & (1 << 11) ||
4196 pcie_dev_ctrl & (1 << 4)) {
4197 dev_info(&dd->pdev->dev,
4198 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4199 pdev->vendor, pdev->device);
4200 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4201 PCI_EXP_DEVCTL_RELAX_EN);
4202 pci_write_config_word(pdev,
4203 pos + PCI_EXP_DEVCTL,
4204 pcie_dev_ctrl);
4205 }
4206 }
4207}
4208
4209static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4210{
4211 /*
4212 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4213 * device with device id 0x5aXX
4214 */
4215 if (pdev->bus && pdev->bus->self) {
4216 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4217 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4218 mtip_disable_link_opts(dd, pdev->bus->self);
4219 } else {
4220 /* Check further up the topology */
4221 struct pci_dev *parent_dev = pdev->bus->self;
4222 if (parent_dev->bus &&
4223 parent_dev->bus->parent &&
4224 parent_dev->bus->parent->self &&
4225 parent_dev->bus->parent->self->vendor ==
4226 PCI_VENDOR_ID_ATI &&
4227 (parent_dev->bus->parent->self->device &
4228 0xff00) == 0x5a00) {
4229 mtip_disable_link_opts(dd,
4230 parent_dev->bus->parent->self);
4231 }
4232 }
4233 }
4234}
4235
Sam Bradshaw88523a62011-08-30 08:34:26 -06004236/*
4237 * Called for each supported PCI device detected.
4238 *
4239 * This function allocates the private data structure, enables the
4240 * PCI device and then calls the block layer initialization function.
4241 *
4242 * return value
4243 * 0 on success else an error code.
4244 */
4245static int mtip_pci_probe(struct pci_dev *pdev,
4246 const struct pci_device_id *ent)
4247{
4248 int rv = 0;
4249 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004250 char cpu_list[256];
4251 const struct cpumask *node_mask;
4252 int cpu, i = 0, j = 0;
4253 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304254 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004255
4256 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004257 my_node = pcibus_to_node(pdev->bus);
4258 if (my_node != NUMA_NO_NODE) {
4259 if (!node_online(my_node))
4260 my_node = mtip_get_next_rr_node();
4261 } else {
4262 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4263 my_node = mtip_get_next_rr_node();
4264 }
4265 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4266 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004267 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004268
4269 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004270 if (dd == NULL) {
4271 dev_err(&pdev->dev,
4272 "Unable to allocate memory for driver data\n");
4273 return -ENOMEM;
4274 }
4275
Sam Bradshaw88523a62011-08-30 08:34:26 -06004276 /* Attach the private data to this PCI device. */
4277 pci_set_drvdata(pdev, dd);
4278
4279 rv = pcim_enable_device(pdev);
4280 if (rv < 0) {
4281 dev_err(&pdev->dev, "Unable to enable device\n");
4282 goto iomap_err;
4283 }
4284
4285 /* Map BAR5 to memory. */
4286 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4287 if (rv < 0) {
4288 dev_err(&pdev->dev, "Unable to map regions\n");
4289 goto iomap_err;
4290 }
4291
4292 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4293 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4294
4295 if (rv) {
4296 rv = pci_set_consistent_dma_mask(pdev,
4297 DMA_BIT_MASK(32));
4298 if (rv) {
4299 dev_warn(&pdev->dev,
4300 "64-bit DMA enable failed\n");
4301 goto setmask_err;
4302 }
4303 }
4304 }
4305
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004306 /* Copy the info we may need later into the private data structure. */
4307 dd->major = mtip_major;
4308 dd->instance = instance;
4309 dd->pdev = pdev;
4310 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004311
Asai Thambi S P0caff002013-04-03 19:56:21 +05304312 INIT_LIST_HEAD(&dd->online_list);
4313 INIT_LIST_HEAD(&dd->remove_list);
4314
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004315 memset(dd->workq_name, 0, 32);
4316 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4317
4318 dd->isr_workq = create_workqueue(dd->workq_name);
4319 if (!dd->isr_workq) {
4320 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004321 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004322 goto block_initialize_err;
4323 }
4324
4325 memset(cpu_list, 0, sizeof(cpu_list));
4326
4327 node_mask = cpumask_of_node(dd->numa_node);
4328 if (!cpumask_empty(node_mask)) {
4329 for_each_cpu(cpu, node_mask)
4330 {
4331 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4332 j = strlen(cpu_list);
4333 }
4334
4335 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4336 dd->numa_node,
4337 topology_physical_package_id(cpumask_first(node_mask)),
4338 nr_cpus_node(dd->numa_node),
4339 cpu_list);
4340 } else
4341 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4342
4343 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4344 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4345 cpu_to_node(dd->isr_binding), dd->isr_binding);
4346
4347 /* first worker context always runs in ISR */
4348 dd->work[0].cpu_binding = dd->isr_binding;
4349 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4350 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4351 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4352 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4353 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4354 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4355 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4356
4357 /* Log the bindings */
4358 for_each_present_cpu(cpu) {
4359 memset(cpu_list, 0, sizeof(cpu_list));
4360 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4361 if (dd->work[i].cpu_binding == cpu) {
4362 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4363 j = strlen(cpu_list);
4364 }
4365 }
4366 if (j)
4367 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4368 }
4369
4370 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4371 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4372 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4373 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4374 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4375 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4376 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4377 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4378
4379 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004380 rv = pci_enable_msi(pdev);
4381 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004382 dev_warn(&pdev->dev,
4383 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004384 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004385 }
4386
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004387 mtip_fix_ero_nosnoop(dd, pdev);
4388
Sam Bradshaw88523a62011-08-30 08:34:26 -06004389 /* Initialize the block layer. */
4390 rv = mtip_block_initialize(dd);
4391 if (rv < 0) {
4392 dev_err(&pdev->dev,
4393 "Unable to initialize block layer\n");
4394 goto block_initialize_err;
4395 }
4396
4397 /*
4398 * Increment the instance count so that each device has a unique
4399 * instance number.
4400 */
4401 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004402 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004403 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304404 else
4405 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304406
4407 /* Add to online list even if in ftl rebuild */
4408 spin_lock_irqsave(&dev_lock, flags);
4409 list_add(&dd->online_list, &online_list);
4410 spin_unlock_irqrestore(&dev_lock, flags);
4411
Sam Bradshaw88523a62011-08-30 08:34:26 -06004412 goto done;
4413
4414block_initialize_err:
4415 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004416
4417msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004418 if (dd->isr_workq) {
4419 flush_workqueue(dd->isr_workq);
4420 destroy_workqueue(dd->isr_workq);
4421 drop_cpu(dd->work[0].cpu_binding);
4422 drop_cpu(dd->work[1].cpu_binding);
4423 drop_cpu(dd->work[2].cpu_binding);
4424 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004425setmask_err:
4426 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4427
4428iomap_err:
4429 kfree(dd);
4430 pci_set_drvdata(pdev, NULL);
4431 return rv;
4432done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004433 return rv;
4434}
4435
4436/*
4437 * Called for each probed device when the device is removed or the
4438 * driver is unloaded.
4439 *
4440 * return value
4441 * None
4442 */
4443static void mtip_pci_remove(struct pci_dev *pdev)
4444{
4445 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004446 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004447
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004448 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004449
Asai Thambi S P0caff002013-04-03 19:56:21 +05304450 spin_lock_irqsave(&dev_lock, flags);
4451 list_del_init(&dd->online_list);
4452 list_add(&dd->remove_list, &removing_list);
4453 spin_unlock_irqrestore(&dev_lock, flags);
4454
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004455 mtip_check_surprise_removal(pdev);
4456 synchronize_irq(dd->pdev->irq);
4457
4458 /* Spin until workers are done */
4459 to = jiffies + msecs_to_jiffies(4000);
4460 do {
4461 msleep(20);
4462 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4463 time_before(jiffies, to));
4464
4465 if (atomic_read(&dd->irq_workers_active) != 0) {
4466 dev_warn(&dd->pdev->dev,
4467 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004468 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004469
Asai Thambi SP2132a542015-05-11 15:53:18 -07004470 blk_mq_stop_hw_queues(dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004471 /* Clean up the block layer. */
4472 mtip_block_remove(dd);
4473
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004474 if (dd->isr_workq) {
4475 flush_workqueue(dd->isr_workq);
4476 destroy_workqueue(dd->isr_workq);
4477 drop_cpu(dd->work[0].cpu_binding);
4478 drop_cpu(dd->work[1].cpu_binding);
4479 drop_cpu(dd->work[2].cpu_binding);
4480 }
4481
Sam Bradshaw88523a62011-08-30 08:34:26 -06004482 pci_disable_msi(pdev);
4483
Asai Thambi S P0caff002013-04-03 19:56:21 +05304484 spin_lock_irqsave(&dev_lock, flags);
4485 list_del_init(&dd->remove_list);
4486 spin_unlock_irqrestore(&dev_lock, flags);
4487
Asai Thambi SP2132a542015-05-11 15:53:18 -07004488 kfree(dd);
4489 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004490
Sam Bradshaw88523a62011-08-30 08:34:26 -06004491 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004492 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004493}
4494
4495/*
4496 * Called for each probed device when the device is suspended.
4497 *
4498 * return value
4499 * 0 Success
4500 * <0 Error
4501 */
4502static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4503{
4504 int rv = 0;
4505 struct driver_data *dd = pci_get_drvdata(pdev);
4506
4507 if (!dd) {
4508 dev_err(&pdev->dev,
4509 "Driver private datastructure is NULL\n");
4510 return -EFAULT;
4511 }
4512
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004513 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004514
4515 /* Disable ports & interrupts then send standby immediate */
4516 rv = mtip_block_suspend(dd);
4517 if (rv < 0) {
4518 dev_err(&pdev->dev,
4519 "Failed to suspend controller\n");
4520 return rv;
4521 }
4522
4523 /*
4524 * Save the pci config space to pdev structure &
4525 * disable the device
4526 */
4527 pci_save_state(pdev);
4528 pci_disable_device(pdev);
4529
4530 /* Move to Low power state*/
4531 pci_set_power_state(pdev, PCI_D3hot);
4532
4533 return rv;
4534}
4535
4536/*
4537 * Called for each probed device when the device is resumed.
4538 *
4539 * return value
4540 * 0 Success
4541 * <0 Error
4542 */
4543static int mtip_pci_resume(struct pci_dev *pdev)
4544{
4545 int rv = 0;
4546 struct driver_data *dd;
4547
4548 dd = pci_get_drvdata(pdev);
4549 if (!dd) {
4550 dev_err(&pdev->dev,
4551 "Driver private datastructure is NULL\n");
4552 return -EFAULT;
4553 }
4554
4555 /* Move the device to active State */
4556 pci_set_power_state(pdev, PCI_D0);
4557
4558 /* Restore PCI configuration space */
4559 pci_restore_state(pdev);
4560
4561 /* Enable the PCI device*/
4562 rv = pcim_enable_device(pdev);
4563 if (rv < 0) {
4564 dev_err(&pdev->dev,
4565 "Failed to enable card during resume\n");
4566 goto err;
4567 }
4568 pci_set_master(pdev);
4569
4570 /*
4571 * Calls hbaReset, initPort, & startPort function
4572 * then enables interrupts
4573 */
4574 rv = mtip_block_resume(dd);
4575 if (rv < 0)
4576 dev_err(&pdev->dev, "Unable to resume\n");
4577
4578err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004579 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004580
4581 return rv;
4582}
4583
4584/*
4585 * Shutdown routine
4586 *
4587 * return value
4588 * None
4589 */
4590static void mtip_pci_shutdown(struct pci_dev *pdev)
4591{
4592 struct driver_data *dd = pci_get_drvdata(pdev);
4593 if (dd)
4594 mtip_block_shutdown(dd);
4595}
4596
Sam Bradshaw88523a62011-08-30 08:34:26 -06004597/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004598static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304599 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4600 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4601 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4602 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4603 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4604 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4605 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004606 { 0 }
4607};
4608
4609/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004610static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004611 .name = MTIP_DRV_NAME,
4612 .id_table = mtip_pci_tbl,
4613 .probe = mtip_pci_probe,
4614 .remove = mtip_pci_remove,
4615 .suspend = mtip_pci_suspend,
4616 .resume = mtip_pci_resume,
4617 .shutdown = mtip_pci_shutdown,
4618};
4619
4620MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4621
4622/*
4623 * Module initialization function.
4624 *
4625 * Called once when the module is loaded. This function allocates a major
4626 * block device number to the Cyclone devices and registers the PCI layer
4627 * of the driver.
4628 *
4629 * Return value
4630 * 0 on success else error code.
4631 */
4632static int __init mtip_init(void)
4633{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004634 int error;
4635
Asai Thambi S P45422e72012-09-05 22:03:56 +05304636 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004637
Asai Thambi S P0caff002013-04-03 19:56:21 +05304638 spin_lock_init(&dev_lock);
4639
4640 INIT_LIST_HEAD(&online_list);
4641 INIT_LIST_HEAD(&removing_list);
4642
Sam Bradshaw88523a62011-08-30 08:34:26 -06004643 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004644 error = register_blkdev(0, MTIP_DRV_NAME);
4645 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304646 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004647 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004648 return -EBUSY;
4649 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004650 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004651
Asai Thambi S P0caff002013-04-03 19:56:21 +05304652 dfs_parent = debugfs_create_dir("rssd", NULL);
4653 if (IS_ERR_OR_NULL(dfs_parent)) {
4654 pr_warn("Error creating debugfs parent\n");
4655 dfs_parent = NULL;
4656 }
4657 if (dfs_parent) {
4658 dfs_device_status = debugfs_create_file("device_status",
4659 S_IRUGO, dfs_parent, NULL,
4660 &mtip_device_status_fops);
4661 if (IS_ERR_OR_NULL(dfs_device_status)) {
4662 pr_err("Error creating device_status node\n");
4663 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004664 }
4665 }
4666
Sam Bradshaw88523a62011-08-30 08:34:26 -06004667 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004668 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004669 if (error) {
4670 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004671 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004672 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004673
4674 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004675}
4676
4677/*
4678 * Module de-initialization function.
4679 *
4680 * Called once when the module is unloaded. This function deallocates
4681 * the major block device number allocated by mtip_init() and
4682 * unregisters the PCI layer of the driver.
4683 *
4684 * Return value
4685 * none
4686 */
4687static void __exit mtip_exit(void)
4688{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004689 /* Release the allocated major block device number. */
4690 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4691
4692 /* Unregister the PCI driver. */
4693 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004694
4695 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004696}
4697
4698MODULE_AUTHOR("Micron Technology, Inc");
4699MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4700MODULE_LICENSE("GPL");
4701MODULE_VERSION(MTIP_DRV_VERSION);
4702
4703module_init(mtip_init);
4704module_exit(mtip_exit);