blob: 5979ab3afd81a8e4f9d219f15f6e8d75e2cc1230 [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 Bradshaw88523a62011-08-30 08:34:26 -060042#include "mtip32xx.h"
43
44#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
Sam Bradshaw188b9f42014-01-15 10:14:57 -080045
46/* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
47#define AHCI_RX_FIS_SZ 0x100
48#define AHCI_RX_FIS_OFFSET 0x0
49#define AHCI_IDFY_SZ ATA_SECT_SIZE
50#define AHCI_IDFY_OFFSET 0x400
51#define AHCI_SECTBUF_SZ ATA_SECT_SIZE
52#define AHCI_SECTBUF_OFFSET 0x800
53#define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
54#define AHCI_SMARTBUF_OFFSET 0xC00
55/* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
56#define BLOCK_DMA_ALLOC_SZ 4096
57
58/* DMA region containing command table (should be 8192 bytes) */
59#define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
60#define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
61#define AHCI_CMD_TBL_OFFSET 0x0
62
63/* DMA region per command (contains header and SGL) */
64#define AHCI_CMD_TBL_HDR_SZ 0x80
65#define AHCI_CMD_TBL_HDR_OFFSET 0x0
66#define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
67#define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
68#define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
69
Sam Bradshaw88523a62011-08-30 08:34:26 -060070
Asai Thambi S P45038362012-04-09 08:35:38 +020071#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060072#define HOST_HSORG 0xFC
73#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
74#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
75#define HSORG_HWREV 0xFF00
76#define HSORG_STYLE 0x8
77#define HSORG_SLOTGROUPS 0x7
78
79#define PORT_COMMAND_ISSUE 0x38
80#define PORT_SDBV 0x7C
81
82#define PORT_OFFSET 0x100
83#define PORT_MEM_SIZE 0x80
84
85#define PORT_IRQ_ERR \
86 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
87 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
88 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
89 PORT_IRQ_OVERFLOW)
90#define PORT_IRQ_LEGACY \
91 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
92#define PORT_IRQ_HANDLED \
93 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
94 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
95 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
96#define DEF_PORT_IRQ \
97 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
98
99/* product numbers */
100#define MTIP_PRODUCT_UNKNOWN 0x00
101#define MTIP_PRODUCT_ASICFPGA 0x11
102
103/* Device instance number, incremented each time a device is probed. */
104static int instance;
105
Asai Thambi S P0caff002013-04-03 19:56:21 +0530106struct list_head online_list;
107struct list_head removing_list;
108spinlock_t dev_lock;
109
Sam Bradshaw88523a62011-08-30 08:34:26 -0600110/*
111 * Global variable used to hold the major block device number
112 * allocated in mtip_init().
113 */
Jens Axboe3ff147d2011-09-27 21:33:53 -0600114static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -0700115static struct dentry *dfs_parent;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530116static struct dentry *dfs_device_status;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600117
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800118static u32 cpu_use[NR_CPUS];
119
Sam Bradshaw88523a62011-08-30 08:34:26 -0600120static DEFINE_SPINLOCK(rssd_index_lock);
121static DEFINE_IDA(rssd_index_ida);
122
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100123static int mtip_block_initialize(struct driver_data *dd);
124
Jens Axboe16d02c02011-09-27 15:50:01 -0600125#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600126struct mtip_compat_ide_task_request_s {
127 __u8 io_ports[8];
128 __u8 hob_ports[8];
129 ide_reg_valid_t out_flags;
130 ide_reg_valid_t in_flags;
131 int data_phase;
132 int req_cmd;
133 compat_ulong_t out_size;
134 compat_ulong_t in_size;
135};
Jens Axboe16d02c02011-09-27 15:50:01 -0600136#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600137
Sam Bradshaw88523a62011-08-30 08:34:26 -0600138/*
Jens Axboe63166682011-09-27 21:27:43 -0600139 * This function check_for_surprise_removal is called
140 * while card is removed from the system and it will
141 * read the vendor id from the configration space
142 *
143 * @pdev Pointer to the pci_dev structure.
144 *
145 * return value
146 * true if device removed, else false
147 */
148static bool mtip_check_surprise_removal(struct pci_dev *pdev)
149{
150 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600151 struct driver_data *dd = pci_get_drvdata(pdev);
152
153 if (dd->sr)
154 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600155
156 /* Read the vendorID from the configuration space */
157 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600158 if (vendor_id == 0xFFFF) {
159 dd->sr = true;
160 if (dd->queue)
161 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
162 else
163 dev_warn(&dd->pdev->dev,
164 "%s: dd->queue is NULL\n", __func__);
165 if (dd->port) {
166 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
167 wake_up_interruptible(&dd->port->svc_wait);
168 } else
169 dev_warn(&dd->pdev->dev,
170 "%s: dd->port is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600171 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600172 }
173
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600174 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600175}
176
Jens Axboeffc771b2014-05-09 09:42:02 -0600177static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600178{
Jens Axboeffc771b2014-05-09 09:42:02 -0600179 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600180
Jens Axboeffc771b2014-05-09 09:42:02 -0600181 rq = blk_mq_alloc_reserved_request(dd->queue, 0, __GFP_WAIT);
182 return blk_mq_rq_to_pdu(rq);
183}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600184
Jens Axboeffc771b2014-05-09 09:42:02 -0600185static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
186{
187 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600188}
189
190/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600191 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600192 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600193static struct request *mtip_rq_from_tag(struct driver_data *dd,
194 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600195{
Jens Axboeffc771b2014-05-09 09:42:02 -0600196 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
197
198 return blk_mq_tag_to_rq(hctx->tags, tag);
199}
200
201static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
202 unsigned int tag)
203{
204 struct request *rq = mtip_rq_from_tag(dd, tag);
205
206 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600207}
208
209/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600210 * IO completion function.
211 *
212 * This completion function is called by the driver ISR when a
213 * command that was issued by the kernel completes. It first calls the
214 * asynchronous completion function which normally calls back into the block
215 * layer passing the asynchronous callback data, then unmaps the
216 * scatter list associated with the completed command, and finally
217 * clears the allocated bit associated with the completed command.
218 *
219 * @port Pointer to the port data structure.
220 * @tag Tag of the command.
221 * @data Pointer to driver_data.
222 * @status Completion status.
223 *
224 * return value
225 * None
226 */
227static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600228 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600229{
Jens Axboeffc771b2014-05-09 09:42:02 -0600230 struct driver_data *dd = port->dd;
231 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600232
233 if (unlikely(!dd) || unlikely(!port))
234 return;
235
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600236 if (unlikely(status == PORT_IRQ_TF_ERR)) {
237 dev_warn(&port->dd->pdev->dev,
238 "Command tag %d failed due to TFE\n", tag);
239 }
240
Jens Axboeffc771b2014-05-09 09:42:02 -0600241 /* Unmap the DMA scatter list entries */
242 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600243
Jens Axboeffc771b2014-05-09 09:42:02 -0600244 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600245
Jens Axboeffc771b2014-05-09 09:42:02 -0600246 if (unlikely(cmd->unaligned))
247 up(&port->cmd_slot_unal);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600248
Jens Axboeffc771b2014-05-09 09:42:02 -0600249 blk_mq_end_io(rq, status ? -EIO : 0);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600250}
251
252/*
Jens Axboe63166682011-09-27 21:27:43 -0600253 * Reset the HBA (without sleeping)
254 *
Jens Axboe63166682011-09-27 21:27:43 -0600255 * @dd Pointer to the driver data structure.
256 *
257 * return value
258 * 0 The reset was successful.
259 * -1 The HBA Reset bit did not clear.
260 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530261static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600262{
263 unsigned long timeout;
264
Jens Axboe63166682011-09-27 21:27:43 -0600265 /* Set the reset bit */
266 writel(HOST_RESET, dd->mmio + HOST_CTL);
267
268 /* Flush */
269 readl(dd->mmio + HOST_CTL);
270
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530271 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
272 timeout = jiffies + msecs_to_jiffies(2000);
273 do {
274 mdelay(10);
275 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
276 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600277
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530278 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
279 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200280
Jens Axboe63166682011-09-27 21:27:43 -0600281 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
282 return -1;
283
284 return 0;
285}
286
287/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600288 * Issue a command to the hardware.
289 *
290 * Set the appropriate bit in the s_active and Command Issue hardware
291 * registers, causing hardware command processing to begin.
292 *
293 * @port Pointer to the port structure.
294 * @tag The tag of the command to be issued.
295 *
296 * return value
297 * None
298 */
299static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
300{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800301 int group = tag >> 5;
302
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800303 /* guard SACT and CI registers */
304 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->s_active[MTIP_TAG_INDEX(tag)]);
307 writel((1 << MTIP_TAG_BIT(tag)),
308 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800309 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600310}
311
312/*
Jens Axboe63166682011-09-27 21:27:43 -0600313 * Enable/disable the reception of FIS
314 *
315 * @port Pointer to the port data structure
316 * @enable 1 to enable, 0 to disable
317 *
318 * return value
319 * Previous state: 1 enabled, 0 disabled
320 */
321static int mtip_enable_fis(struct mtip_port *port, int enable)
322{
323 u32 tmp;
324
325 /* enable FIS reception */
326 tmp = readl(port->mmio + PORT_CMD);
327 if (enable)
328 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
329 else
330 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
331
332 /* Flush */
333 readl(port->mmio + PORT_CMD);
334
335 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
336}
337
338/*
339 * Enable/disable the DMA engine
340 *
341 * @port Pointer to the port data structure
342 * @enable 1 to enable, 0 to disable
343 *
344 * return value
345 * Previous state: 1 enabled, 0 disabled.
346 */
347static int mtip_enable_engine(struct mtip_port *port, int enable)
348{
349 u32 tmp;
350
351 /* enable FIS reception */
352 tmp = readl(port->mmio + PORT_CMD);
353 if (enable)
354 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
355 else
356 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
357
358 readl(port->mmio + PORT_CMD);
359 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
360}
361
362/*
363 * Enables the port DMA engine and FIS reception.
364 *
365 * return value
366 * None
367 */
368static inline void mtip_start_port(struct mtip_port *port)
369{
370 /* Enable FIS reception */
371 mtip_enable_fis(port, 1);
372
373 /* Enable the DMA engine */
374 mtip_enable_engine(port, 1);
375}
376
377/*
378 * Deinitialize a port by disabling port interrupts, the DMA engine,
379 * and FIS reception.
380 *
381 * @port Pointer to the port structure
382 *
383 * return value
384 * None
385 */
386static inline void mtip_deinit_port(struct mtip_port *port)
387{
388 /* Disable interrupts on this port */
389 writel(0, port->mmio + PORT_IRQ_MASK);
390
391 /* Disable the DMA engine */
392 mtip_enable_engine(port, 0);
393
394 /* Disable FIS reception */
395 mtip_enable_fis(port, 0);
396}
397
398/*
399 * Initialize a port.
400 *
401 * This function deinitializes the port by calling mtip_deinit_port() and
402 * then initializes it by setting the command header and RX FIS addresses,
403 * clearing the SError register and any pending port interrupts before
404 * re-enabling the default set of port interrupts.
405 *
406 * @port Pointer to the port structure.
407 *
408 * return value
409 * None
410 */
411static void mtip_init_port(struct mtip_port *port)
412{
413 int i;
414 mtip_deinit_port(port);
415
416 /* Program the command list base and FIS base addresses */
417 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
418 writel((port->command_list_dma >> 16) >> 16,
419 port->mmio + PORT_LST_ADDR_HI);
420 writel((port->rxfis_dma >> 16) >> 16,
421 port->mmio + PORT_FIS_ADDR_HI);
422 }
423
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100424 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600425 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100426 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600427
428 /* Clear SError */
429 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
430
431 /* reset the completed registers.*/
432 for (i = 0; i < port->dd->slot_groups; i++)
433 writel(0xFFFFFFFF, port->completed[i]);
434
435 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700436 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600437
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100438 /* Clear any pending interrupts on the HBA. */
439 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
440 port->dd->mmio + HOST_IRQ_STAT);
441
Jens Axboe63166682011-09-27 21:27:43 -0600442 /* Enable port interrupts */
443 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
444}
445
446/*
447 * Restart a port
448 *
449 * @port Pointer to the port data structure.
450 *
451 * return value
452 * None
453 */
454static void mtip_restart_port(struct mtip_port *port)
455{
456 unsigned long timeout;
457
458 /* Disable the DMA engine */
459 mtip_enable_engine(port, 0);
460
461 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
462 timeout = jiffies + msecs_to_jiffies(500);
463 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
464 && time_before(jiffies, timeout))
465 ;
466
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200467 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200468 return;
469
Jens Axboe63166682011-09-27 21:27:43 -0600470 /*
471 * Chip quirk: escalate to hba reset if
472 * PxCMD.CR not clear after 500 ms
473 */
474 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
475 dev_warn(&port->dd->pdev->dev,
476 "PxCMD.CR not clear, escalating reset\n");
477
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530478 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600479 dev_err(&port->dd->pdev->dev,
480 "HBA reset escalation failed.\n");
481
482 /* 30 ms delay before com reset to quiesce chip */
483 mdelay(30);
484 }
485
486 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
487
488 /* Set PxSCTL.DET */
489 writel(readl(port->mmio + PORT_SCR_CTL) |
490 1, port->mmio + PORT_SCR_CTL);
491 readl(port->mmio + PORT_SCR_CTL);
492
493 /* Wait 1 ms to quiesce chip function */
494 timeout = jiffies + msecs_to_jiffies(1);
495 while (time_before(jiffies, timeout))
496 ;
497
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200498 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200499 return;
500
Jens Axboe63166682011-09-27 21:27:43 -0600501 /* Clear PxSCTL.DET */
502 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
503 port->mmio + PORT_SCR_CTL);
504 readl(port->mmio + PORT_SCR_CTL);
505
506 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
507 timeout = jiffies + msecs_to_jiffies(500);
508 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
509 && time_before(jiffies, timeout))
510 ;
511
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200512 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200513 return;
514
Jens Axboe63166682011-09-27 21:27:43 -0600515 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
516 dev_warn(&port->dd->pdev->dev,
517 "COM reset failed\n");
518
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100519 mtip_init_port(port);
520 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600521
Jens Axboe63166682011-09-27 21:27:43 -0600522}
523
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530524static int mtip_device_reset(struct driver_data *dd)
525{
526 int rv = 0;
527
528 if (mtip_check_surprise_removal(dd->pdev))
529 return 0;
530
531 if (mtip_hba_reset(dd) < 0)
532 rv = -EFAULT;
533
534 mdelay(1);
535 mtip_init_port(dd->port);
536 mtip_start_port(dd->port);
537
538 /* Enable interrupts on the HBA. */
539 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
540 dd->mmio + HOST_CTL);
541 return rv;
542}
543
Jens Axboe63166682011-09-27 21:27:43 -0600544/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200545 * Helper function for tag logging
546 */
547static void print_tags(struct driver_data *dd,
548 char *msg,
549 unsigned long *tagbits,
550 int cnt)
551{
552 unsigned char tagmap[128];
553 int group, tagmap_len = 0;
554
555 memset(tagmap, 0, sizeof(tagmap));
556 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600557 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200558 tagbits[group-1]);
559 dev_warn(&dd->pdev->dev,
560 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
561}
562
563/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600564 * Internal command completion callback function.
565 *
566 * This function is normally called by the driver ISR when an internal
567 * command completed. This function signals the command completion by
568 * calling complete().
569 *
570 * @port Pointer to the port data structure.
571 * @tag Tag of the command that has completed.
572 * @data Pointer to a completion structure.
573 * @status Completion status.
574 *
575 * return value
576 * None
577 */
578static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600579 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600580{
Jens Axboeffc771b2014-05-09 09:42:02 -0600581 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600582 if (unlikely(status == PORT_IRQ_TF_ERR))
583 dev_warn(&port->dd->pdev->dev,
584 "Internal command %d completed with TFE\n", tag);
585
Sam Bradshaw88523a62011-08-30 08:34:26 -0600586 complete(waiting);
587}
588
Asai Thambi S P8182b492012-04-09 08:35:38 +0200589static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600590 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200591{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200592}
593
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200594static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
595 dma_addr_t buffer_dma, unsigned int sectors);
596static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
597 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600598/*
599 * Handle an error.
600 *
601 * @dd Pointer to the DRIVER_DATA structure.
602 *
603 * return value
604 * None
605 */
606static void mtip_handle_tfe(struct driver_data *dd)
607{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200608 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600609 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200610 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600611 u32 completed;
612 struct host_to_dev_fis *fis;
613 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200614 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200615 unsigned char *buf;
616 char *fail_reason = NULL;
617 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600618
619 dev_warn(&dd->pdev->dev, "Taskfile error\n");
620
621 port = dd->port;
622
623 /* Stop the timer to prevent command timeouts. */
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700624 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
625
626 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
627 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600628 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700629 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
630
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700631 if (cmd->comp_data && cmd->comp_func) {
632 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600633 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700634 }
635 goto handle_tfe_exit;
636 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600637
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200638 /* clear the tag accumulator */
639 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
640
Sam Bradshaw88523a62011-08-30 08:34:26 -0600641 /* Loop through all the groups */
642 for (group = 0; group < dd->slot_groups; group++) {
643 completed = readl(port->completed[group]);
644
Jens Axboeffc771b2014-05-09 09:42:02 -0600645 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
646
Sam Bradshaw88523a62011-08-30 08:34:26 -0600647 /* clear completed status register in the hardware.*/
648 writel(completed, port->completed[group]);
649
Sam Bradshaw88523a62011-08-30 08:34:26 -0600650 /* Process successfully completed commands */
651 for (bit = 0; bit < 32 && completed; bit++) {
652 if (!(completed & (1<<bit)))
653 continue;
654 tag = (group << 5) + bit;
655
656 /* Skip the internal command slot */
657 if (tag == MTIP_TAG_INTERNAL)
658 continue;
659
Jens Axboeffc771b2014-05-09 09:42:02 -0600660 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200661 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600662 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200663 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600664 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600665 } else {
666 dev_err(&port->dd->pdev->dev,
667 "Missing completion func for tag %d",
668 tag);
669 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600670 /* don't proceed further */
671 return;
672 }
673 }
674 }
675 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200676
677 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600678
679 /* Restart the port */
680 mdelay(20);
681 mtip_restart_port(port);
682
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200683 /* Trying to determine the cause of the error */
684 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
685 dd->port->log_buf,
686 dd->port->log_buf_dma, 1);
687 if (rv) {
688 dev_warn(&dd->pdev->dev,
689 "Error in READ LOG EXT (10h) command\n");
690 /* non-critical error, don't fail the load */
691 } else {
692 buf = (unsigned char *)dd->port->log_buf;
693 if (buf[259] & 0x1) {
694 dev_info(&dd->pdev->dev,
695 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200696 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200697 fail_all_ncq_write = 1;
698 fail_reason = "write protect";
699 }
700 if (buf[288] == 0xF7) {
701 dev_info(&dd->pdev->dev,
702 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200703 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200704 fail_all_ncq_cmds = 1;
705 fail_reason = "thermal shutdown";
706 }
707 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700708 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200709 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700710 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200711 fail_all_ncq_cmds = 1;
712 fail_reason = "rebuild failed";
713 }
714 }
715
Sam Bradshaw88523a62011-08-30 08:34:26 -0600716 /* clear the tag accumulator */
717 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
718
719 /* Loop through all the groups */
720 for (group = 0; group < dd->slot_groups; group++) {
721 for (bit = 0; bit < 32; bit++) {
722 reissue = 1;
723 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600724 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600725
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200726 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600727
728 /* Should re-issue? */
729 if (tag == MTIP_TAG_INTERNAL ||
730 fis->command == ATA_CMD_SET_FEATURES)
731 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200732 else {
733 if (fail_all_ncq_cmds ||
734 (fail_all_ncq_write &&
735 fis->command == ATA_CMD_FPDMA_WRITE)) {
736 dev_warn(&dd->pdev->dev,
737 " Fail: %s w/tag %d [%s].\n",
738 fis->command == ATA_CMD_FPDMA_WRITE ?
739 "write" : "read",
740 tag,
741 fail_reason != NULL ?
742 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200743 if (cmd->comp_func) {
744 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600745 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200746 }
747 continue;
748 }
749 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600750
751 /*
752 * First check if this command has
753 * exceeded its retries.
754 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200755 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600756
757 set_bit(tag, tagaccum);
758
Sam Bradshaw88523a62011-08-30 08:34:26 -0600759 /* Re-issue the command. */
760 mtip_issue_ncq_command(port, tag);
761
762 continue;
763 }
764
765 /* Retire a command that will not be reissued */
766 dev_warn(&port->dd->pdev->dev,
767 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600768
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200769 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600770 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600771 else
772 dev_warn(&port->dd->pdev->dev,
773 "Bad completion for tag %d\n",
774 tag);
775 }
776 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200777 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600778
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700779handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100780 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200781 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100782 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600783}
784
785/*
786 * Handle a set device bits interrupt
787 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800788static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
789 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600790{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800791 struct driver_data *dd = port->dd;
792 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600793 struct mtip_cmd *command;
794
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800795 if (!completed) {
796 WARN_ON_ONCE(!completed);
797 return;
798 }
799 /* clear completed status register in the hardware.*/
800 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600801
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800802 /* Process completed commands. */
803 for (bit = 0; (bit < 32) && completed; bit++) {
804 if (completed & 0x01) {
805 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600806
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800807 /* skip internal command slot. */
808 if (unlikely(tag == MTIP_TAG_INTERNAL))
809 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600810
Jens Axboeffc771b2014-05-09 09:42:02 -0600811 command = mtip_cmd_from_tag(dd, tag);
812 if (likely(command->comp_func))
813 command->comp_func(port, tag, command, 0);
814 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600815 dev_dbg(&dd->pdev->dev,
816 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800817 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600818
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800819 if (mtip_check_surprise_removal(
820 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800821 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600822 }
823 }
824 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800825 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600826 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800827
828 /* If last, re-enable interrupts */
829 if (atomic_dec_return(&dd->irq_workers_active) == 0)
830 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831}
832
833/*
834 * Process legacy pio and d2h interrupts
835 */
836static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
837{
838 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600839 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200841 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100842 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600843 & (1 << MTIP_TAG_INTERNAL))) {
844 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600845 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600846 return;
847 }
848 }
849
Sam Bradshaw88523a62011-08-30 08:34:26 -0600850 return;
851}
852
853/*
854 * Demux and handle errors
855 */
856static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
857{
858 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
859 mtip_handle_tfe(dd);
860
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));
877 }
878}
879
880static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
881{
882 struct driver_data *dd = (struct driver_data *) data;
883 struct mtip_port *port = dd->port;
884 u32 hba_stat, port_stat;
885 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800886 int do_irq_enable = 1, i, workers;
887 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600888
889 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
890 if (hba_stat) {
891 rv = IRQ_HANDLED;
892
893 /* Acknowledge the interrupt status on the port.*/
894 port_stat = readl(port->mmio + PORT_IRQ_STAT);
895 writel(port_stat, port->mmio + PORT_IRQ_STAT);
896
897 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800898 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
899 do_irq_enable = 0;
900 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
901
902 /* Start at 1: group zero is always local? */
903 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
904 i++) {
905 twork = &dd->work[i];
906 twork->completed = readl(port->completed[i]);
907 if (twork->completed)
908 workers++;
909 }
910
911 atomic_set(&dd->irq_workers_active, workers);
912 if (workers) {
913 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
914 twork = &dd->work[i];
915 if (twork->completed)
916 queue_work_on(
917 twork->cpu_binding,
918 dd->isr_workq,
919 &twork->work);
920 }
921
922 if (likely(dd->work[0].completed))
923 mtip_workq_sdbfx(port, 0,
924 dd->work[0].completed);
925
926 } else {
927 /*
928 * Chip quirk: SDB interrupt but nothing
929 * to complete
930 */
931 do_irq_enable = 1;
932 }
933 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600934
935 if (unlikely(port_stat & PORT_IRQ_ERR)) {
936 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600937 /* don't proceed further */
938 return IRQ_HANDLED;
939 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200940 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200941 &dd->dd_flag))
942 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600943
944 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
945 }
946
947 if (unlikely(port_stat & PORT_IRQ_LEGACY))
948 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
949 }
950
951 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800952 if (unlikely(do_irq_enable))
953 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600954
955 return rv;
956}
957
958/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600959 * HBA interrupt subroutine.
960 *
961 * @irq IRQ number.
962 * @instance Pointer to the driver data structure.
963 *
964 * return value
965 * IRQ_HANDLED A HBA interrupt was pending and handled.
966 * IRQ_NONE This interrupt was not for the HBA.
967 */
968static irqreturn_t mtip_irq_handler(int irq, void *instance)
969{
970 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800971
972 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600973}
974
975static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
976{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600977 writel(1 << MTIP_TAG_BIT(tag),
978 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
979}
980
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200981static bool mtip_pause_ncq(struct mtip_port *port,
982 struct host_to_dev_fis *fis)
983{
984 struct host_to_dev_fis *reply;
985 unsigned long task_file_data;
986
987 reply = port->rxfis + RX_FIS_D2H_REG;
988 task_file_data = readl(port->mmio+PORT_TFDATA);
989
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530990 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
991 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
992
993 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200994 return false;
995
996 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
997 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530998 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200999 port->ic_pause_timer = jiffies;
1000 return true;
1001 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1002 (fis->features == 0x03)) {
1003 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1004 port->ic_pause_timer = jiffies;
1005 return true;
1006 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1007 ((fis->command == 0xFC) &&
1008 (fis->features == 0x27 || fis->features == 0x72 ||
1009 fis->features == 0x62 || fis->features == 0x26))) {
1010 /* Com reset after secure erase or lowlevel format */
1011 mtip_restart_port(port);
1012 return false;
1013 }
1014
1015 return false;
1016}
1017
Sam Bradshaw88523a62011-08-30 08:34:26 -06001018/*
1019 * Wait for port to quiesce
1020 *
1021 * @port Pointer to port data structure
1022 * @timeout Max duration to wait (ms)
1023 *
1024 * return value
1025 * 0 Success
1026 * -EBUSY Commands still active
1027 */
1028static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1029{
1030 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001031 unsigned int n;
1032 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033
1034 to = jiffies + msecs_to_jiffies(timeout);
1035 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001036 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1037 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001038 msleep(20);
1039 continue; /* svc thd is actively issuing commands */
1040 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001041 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001042 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001043 /*
1044 * Ignore s_active bit 0 of array element 0.
1045 * This bit will always be set
1046 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001047 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001048 for (n = 1; n < port->dd->slot_groups; n++)
1049 active |= readl(port->s_active[n]);
1050
1051 if (!active)
1052 break;
1053
1054 msleep(20);
1055 } while (time_before(jiffies, to));
1056
1057 return active ? -EBUSY : 0;
1058}
1059
1060/*
1061 * Execute an internal command and wait for the completion.
1062 *
1063 * @port Pointer to the port data structure.
1064 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001065 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001066 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001067 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001068 * @opts Command header options, excluding the FIS length
1069 * and the number of PRD entries.
1070 * @timeout Time in ms to wait for the command to complete.
1071 *
1072 * return value
1073 * 0 Command completed successfully.
1074 * -EFAULT The buffer address is not correctly aligned.
1075 * -EBUSY Internal command or other IO in progress.
1076 * -EAGAIN Time out waiting for command to complete.
1077 */
1078static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001079 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001080 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001082 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001083 u32 opts,
1084 gfp_t atomic,
1085 unsigned long timeout)
1086{
1087 struct mtip_cmd_sg *command_sg;
1088 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001089 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301090 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001091 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001092
1093 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1094 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301095 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001096 return -EFAULT;
1097 }
1098
Jens Axboeffc771b2014-05-09 09:42:02 -06001099 int_cmd = mtip_get_int_command(dd);
1100
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001101 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001102 port->ic_pause_timer = 0;
1103
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301104 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1105 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001106
1107 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001108 if (fis->command != ATA_CMD_STANDBYNOW1) {
1109 /* wait for io to complete if non atomic */
1110 if (mtip_quiesce_io(port, 5000) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301111 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001112 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001113 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001114 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001115 wake_up_interruptible(&port->svc_wait);
1116 return -EBUSY;
1117 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001118 }
1119
1120 /* Set the completion function and data for the command. */
1121 int_cmd->comp_data = &wait;
1122 int_cmd->comp_func = mtip_completion;
1123
1124 } else {
1125 /* Clear completion - we're going to poll */
1126 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001127 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001128 }
1129
1130 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001131 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001132
1133 /* Populate the SG list */
1134 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001135 __force_bit2int cpu_to_le32(opts | fis_len);
1136 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001137 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1138
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001139 command_sg->info =
1140 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1141 command_sg->dba =
1142 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1143 command_sg->dba_upper =
1144 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001145
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001146 int_cmd->command_header->opts |=
1147 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001148 }
1149
1150 /* Populate the command header */
1151 int_cmd->command_header->byte_count = 0;
1152
1153 /* Issue the command to the hardware */
1154 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1155
Sam Bradshaw88523a62011-08-30 08:34:26 -06001156 if (atomic == GFP_KERNEL) {
1157 /* Wait for the command to complete or timeout. */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301158 if (wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001159 &wait,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301160 msecs_to_jiffies(timeout)) <= 0) {
1161 if (rv == -ERESTARTSYS) { /* interrupted */
1162 dev_err(&dd->pdev->dev,
1163 "Internal command [%02X] was interrupted after %lu ms\n",
1164 fis->command, timeout);
1165 rv = -EINTR;
1166 goto exec_ic_exit;
1167 } else if (rv == 0) /* timeout */
1168 dev_err(&dd->pdev->dev,
1169 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1170 fis->command, timeout);
1171 else
1172 dev_err(&dd->pdev->dev,
1173 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1174 fis->command, rv, timeout);
1175
1176 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001177 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301178 &dd->dd_flag)) {
1179 dev_err(&dd->pdev->dev,
1180 "Internal command [%02X] wait returned due to SR\n",
1181 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001182 rv = -ENXIO;
1183 goto exec_ic_exit;
1184 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301185 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001186 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301187 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001188 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001189 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301190 u32 hba_stat, port_stat;
1191
Sam Bradshaw88523a62011-08-30 08:34:26 -06001192 /* Spin for <timeout> checking if command still outstanding */
1193 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001194 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1195 & (1 << MTIP_TAG_INTERNAL))
1196 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301197 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001198 rv = -ENXIO;
1199 goto exec_ic_exit;
1200 }
1201 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001202 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301203 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001204 rv = -ENXIO;
1205 goto exec_ic_exit;
1206 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301207 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1208 if (!port_stat)
1209 continue;
1210
1211 if (port_stat & PORT_IRQ_ERR) {
1212 dev_err(&dd->pdev->dev,
1213 "Internal command [%02X] failed\n",
1214 fis->command);
1215 mtip_device_reset(dd);
1216 rv = -EIO;
1217 goto exec_ic_exit;
1218 } else {
1219 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1220 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1221 if (hba_stat)
1222 writel(hba_stat,
1223 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001224 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301225 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001226 }
1227 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001228
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001229 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1230 & (1 << MTIP_TAG_INTERNAL)) {
1231 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301232 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1233 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001234 rv = -EAGAIN;
1235 }
1236 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001237exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001238 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001239 mtip_put_int_command(dd, int_cmd);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001240 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1241 /* NCQ paused */
1242 return rv;
1243 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001244 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001245 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001246
1247 return rv;
1248}
1249
1250/*
1251 * Byte-swap ATA ID strings.
1252 *
1253 * ATA identify data contains strings in byte-swapped 16-bit words.
1254 * They must be swapped (on all architectures) to be usable as C strings.
1255 * This function swaps bytes in-place.
1256 *
1257 * @buf The buffer location of the string
1258 * @len The number of bytes to swap
1259 *
1260 * return value
1261 * None
1262 */
1263static inline void ata_swap_string(u16 *buf, unsigned int len)
1264{
1265 int i;
1266 for (i = 0; i < (len/2); i++)
1267 be16_to_cpus(&buf[i]);
1268}
1269
Asai Thambi S P670a6412014-04-16 20:27:54 -07001270static void mtip_set_timeout(struct driver_data *dd,
1271 struct host_to_dev_fis *fis,
1272 unsigned int *timeout, u8 erasemode)
1273{
1274 switch (fis->command) {
1275 case ATA_CMD_DOWNLOAD_MICRO:
1276 *timeout = 120000; /* 2 minutes */
1277 break;
1278 case ATA_CMD_SEC_ERASE_UNIT:
1279 case 0xFC:
1280 if (erasemode)
1281 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1282 else
1283 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1284 break;
1285 case ATA_CMD_STANDBYNOW1:
1286 *timeout = 120000; /* 2 minutes */
1287 break;
1288 case 0xF7:
1289 case 0xFA:
1290 *timeout = 60000; /* 60 seconds */
1291 break;
1292 case ATA_CMD_SMART:
1293 *timeout = 15000; /* 15 seconds */
1294 break;
1295 default:
1296 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1297 break;
1298 }
1299}
1300
Sam Bradshaw88523a62011-08-30 08:34:26 -06001301/*
1302 * Request the device identity information.
1303 *
1304 * If a user space buffer is not specified, i.e. is NULL, the
1305 * identify information is still read from the drive and placed
1306 * into the identify data buffer (@e port->identify) in the
1307 * port data structure.
1308 * When the identify buffer contains valid identify information @e
1309 * port->identify_valid is non-zero.
1310 *
1311 * @port Pointer to the port structure.
1312 * @user_buffer A user space buffer where the identify data should be
1313 * copied.
1314 *
1315 * return value
1316 * 0 Command completed successfully.
1317 * -EFAULT An error occurred while coping data to the user buffer.
1318 * -1 Command failed.
1319 */
1320static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1321{
1322 int rv = 0;
1323 struct host_to_dev_fis fis;
1324
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001325 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001326 return -EFAULT;
1327
Sam Bradshaw88523a62011-08-30 08:34:26 -06001328 /* Build the FIS. */
1329 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1330 fis.type = 0x27;
1331 fis.opts = 1 << 7;
1332 fis.command = ATA_CMD_ID_ATA;
1333
1334 /* Set the identify information as invalid. */
1335 port->identify_valid = 0;
1336
1337 /* Clear the identify information. */
1338 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1339
1340 /* Execute the command. */
1341 if (mtip_exec_internal_command(port,
1342 &fis,
1343 5,
1344 port->identify_dma,
1345 sizeof(u16) * ATA_ID_WORDS,
1346 0,
1347 GFP_KERNEL,
1348 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1349 < 0) {
1350 rv = -1;
1351 goto out;
1352 }
1353
1354 /*
1355 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1356 * perform field-sensitive swapping on the string fields.
1357 * See the kernel use of ata_id_string() for proof of this.
1358 */
1359#ifdef __LITTLE_ENDIAN
1360 ata_swap_string(port->identify + 27, 40); /* model string*/
1361 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1362 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1363#else
1364 {
1365 int i;
1366 for (i = 0; i < ATA_ID_WORDS; i++)
1367 port->identify[i] = le16_to_cpu(port->identify[i]);
1368 }
1369#endif
1370
Sam Bradshaw26d58052013-10-03 10:18:05 -07001371 /* Check security locked state */
1372 if (port->identify[128] & 0x4)
1373 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1374 else
1375 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1376
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301377#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001378 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1379 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1380 port->dd->trim_supp = true;
1381 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301382#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001383 port->dd->trim_supp = false;
1384
Sam Bradshaw88523a62011-08-30 08:34:26 -06001385 /* Set the identify buffer as valid. */
1386 port->identify_valid = 1;
1387
1388 if (user_buffer) {
1389 if (copy_to_user(
1390 user_buffer,
1391 port->identify,
1392 ATA_ID_WORDS * sizeof(u16))) {
1393 rv = -EFAULT;
1394 goto out;
1395 }
1396 }
1397
1398out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001399 return rv;
1400}
1401
1402/*
1403 * Issue a standby immediate command to the device.
1404 *
1405 * @port Pointer to the port structure.
1406 *
1407 * return value
1408 * 0 Command was executed successfully.
1409 * -1 An error occurred while executing the command.
1410 */
1411static int mtip_standby_immediate(struct mtip_port *port)
1412{
1413 int rv;
1414 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001415 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001416 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001417
Sam Bradshaw88523a62011-08-30 08:34:26 -06001418 /* Build the FIS. */
1419 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1420 fis.type = 0x27;
1421 fis.opts = 1 << 7;
1422 fis.command = ATA_CMD_STANDBYNOW1;
1423
Asai Thambi S P670a6412014-04-16 20:27:54 -07001424 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1425
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001426 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001427 rv = mtip_exec_internal_command(port,
1428 &fis,
1429 5,
1430 0,
1431 0,
1432 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001433 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001434 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001435 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1436 jiffies_to_msecs(jiffies - start));
1437 if (rv)
1438 dev_warn(&port->dd->pdev->dev,
1439 "STANDBY IMMEDIATE command failed.\n");
1440
1441 return rv;
1442}
1443
1444/*
1445 * Issue a READ LOG EXT command to the device.
1446 *
1447 * @port pointer to the port structure.
1448 * @page page number to fetch
1449 * @buffer pointer to buffer
1450 * @buffer_dma dma address corresponding to @buffer
1451 * @sectors page length to fetch, in sectors
1452 *
1453 * return value
1454 * @rv return value from mtip_exec_internal_command()
1455 */
1456static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1457 dma_addr_t buffer_dma, unsigned int sectors)
1458{
1459 struct host_to_dev_fis fis;
1460
1461 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1462 fis.type = 0x27;
1463 fis.opts = 1 << 7;
1464 fis.command = ATA_CMD_READ_LOG_EXT;
1465 fis.sect_count = sectors & 0xFF;
1466 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1467 fis.lba_low = page;
1468 fis.lba_mid = 0;
1469 fis.device = ATA_DEVICE_OBS;
1470
1471 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1472
1473 return mtip_exec_internal_command(port,
1474 &fis,
1475 5,
1476 buffer_dma,
1477 sectors * ATA_SECT_SIZE,
1478 0,
1479 GFP_ATOMIC,
1480 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1481}
1482
1483/*
1484 * Issue a SMART READ DATA command to the device.
1485 *
1486 * @port pointer to the port structure.
1487 * @buffer pointer to buffer
1488 * @buffer_dma dma address corresponding to @buffer
1489 *
1490 * return value
1491 * @rv return value from mtip_exec_internal_command()
1492 */
1493static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1494 dma_addr_t buffer_dma)
1495{
1496 struct host_to_dev_fis fis;
1497
1498 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1499 fis.type = 0x27;
1500 fis.opts = 1 << 7;
1501 fis.command = ATA_CMD_SMART;
1502 fis.features = 0xD0;
1503 fis.sect_count = 1;
1504 fis.lba_mid = 0x4F;
1505 fis.lba_hi = 0xC2;
1506 fis.device = ATA_DEVICE_OBS;
1507
1508 return mtip_exec_internal_command(port,
1509 &fis,
1510 5,
1511 buffer_dma,
1512 ATA_SECT_SIZE,
1513 0,
1514 GFP_ATOMIC,
1515 15000);
1516}
1517
1518/*
1519 * Get the value of a smart attribute
1520 *
1521 * @port pointer to the port structure
1522 * @id attribute number
1523 * @attrib pointer to return attrib information corresponding to @id
1524 *
1525 * return value
1526 * -EINVAL NULL buffer passed or unsupported attribute @id.
1527 * -EPERM Identify data not valid, SMART not supported or not enabled
1528 */
1529static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1530 struct smart_attr *attrib)
1531{
1532 int rv, i;
1533 struct smart_attr *pattr;
1534
1535 if (!attrib)
1536 return -EINVAL;
1537
1538 if (!port->identify_valid) {
1539 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1540 return -EPERM;
1541 }
1542 if (!(port->identify[82] & 0x1)) {
1543 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1544 return -EPERM;
1545 }
1546 if (!(port->identify[85] & 0x1)) {
1547 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1548 return -EPERM;
1549 }
1550
1551 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1552 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1553 if (rv) {
1554 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1555 return rv;
1556 }
1557
1558 pattr = (struct smart_attr *)(port->smart_buf + 2);
1559 for (i = 0; i < 29; i++, pattr++)
1560 if (pattr->attr_id == id) {
1561 memcpy(attrib, pattr, sizeof(struct smart_attr));
1562 break;
1563 }
1564
1565 if (i == 29) {
1566 dev_warn(&port->dd->pdev->dev,
1567 "Query for invalid SMART attribute ID\n");
1568 rv = -EINVAL;
1569 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001570
Sam Bradshaw88523a62011-08-30 08:34:26 -06001571 return rv;
1572}
1573
1574/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001575 * Trim unused sectors
1576 *
1577 * @dd pointer to driver_data structure
1578 * @lba starting lba
1579 * @len # of 512b sectors to trim
1580 *
1581 * return value
1582 * -ENOMEM Out of dma memory
1583 * -EINVAL Invalid parameters passed in, trim not supported
1584 * -EIO Error submitting trim request to hw
1585 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301586static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1587 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001588{
1589 int i, rv = 0;
1590 u64 tlba, tlen, sect_left;
1591 struct mtip_trim_entry *buf;
1592 dma_addr_t dma_addr;
1593 struct host_to_dev_fis fis;
1594
1595 if (!len || dd->trim_supp == false)
1596 return -EINVAL;
1597
1598 /* Trim request too big */
1599 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1600
1601 /* Trim request not aligned on 4k boundary */
1602 WARN_ON(len % 8 != 0);
1603
1604 /* Warn if vu_trim structure is too big */
1605 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1606
1607 /* Allocate a DMA buffer for the trim structure */
1608 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1609 GFP_KERNEL);
1610 if (!buf)
1611 return -ENOMEM;
1612 memset(buf, 0, ATA_SECT_SIZE);
1613
1614 for (i = 0, sect_left = len, tlba = lba;
1615 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1616 i++) {
1617 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1618 MTIP_MAX_TRIM_ENTRY_LEN :
1619 sect_left);
1620 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1621 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1622 tlba += tlen;
1623 sect_left -= tlen;
1624 }
1625 WARN_ON(sect_left != 0);
1626
1627 /* Build the fis */
1628 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1629 fis.type = 0x27;
1630 fis.opts = 1 << 7;
1631 fis.command = 0xfb;
1632 fis.features = 0x60;
1633 fis.sect_count = 1;
1634 fis.device = ATA_DEVICE_OBS;
1635
1636 if (mtip_exec_internal_command(dd->port,
1637 &fis,
1638 5,
1639 dma_addr,
1640 ATA_SECT_SIZE,
1641 0,
1642 GFP_KERNEL,
1643 MTIP_TRIM_TIMEOUT_MS) < 0)
1644 rv = -EIO;
1645
1646 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1647 return rv;
1648}
1649
1650/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001651 * Get the drive capacity.
1652 *
1653 * @dd Pointer to the device data structure.
1654 * @sectors Pointer to the variable that will receive the sector count.
1655 *
1656 * return value
1657 * 1 Capacity was returned successfully.
1658 * 0 The identify information is invalid.
1659 */
Jens Axboe63166682011-09-27 21:27:43 -06001660static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001661{
1662 struct mtip_port *port = dd->port;
1663 u64 total, raw0, raw1, raw2, raw3;
1664 raw0 = port->identify[100];
1665 raw1 = port->identify[101];
1666 raw2 = port->identify[102];
1667 raw3 = port->identify[103];
1668 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1669 *sectors = total;
1670 return (bool) !!port->identify_valid;
1671}
1672
1673/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001674 * Display the identify command data.
1675 *
1676 * @port Pointer to the port data structure.
1677 *
1678 * return value
1679 * None
1680 */
1681static void mtip_dump_identify(struct mtip_port *port)
1682{
1683 sector_t sectors;
1684 unsigned short revid;
1685 char cbuf[42];
1686
1687 if (!port->identify_valid)
1688 return;
1689
1690 strlcpy(cbuf, (char *)(port->identify+10), 21);
1691 dev_info(&port->dd->pdev->dev,
1692 "Serial No.: %s\n", cbuf);
1693
1694 strlcpy(cbuf, (char *)(port->identify+23), 9);
1695 dev_info(&port->dd->pdev->dev,
1696 "Firmware Ver.: %s\n", cbuf);
1697
1698 strlcpy(cbuf, (char *)(port->identify+27), 41);
1699 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1700
Sam Bradshaw26d58052013-10-03 10:18:05 -07001701 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1702 port->identify[128],
1703 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1704
Sam Bradshaw88523a62011-08-30 08:34:26 -06001705 if (mtip_hw_get_capacity(port->dd, &sectors))
1706 dev_info(&port->dd->pdev->dev,
1707 "Capacity: %llu sectors (%llu MB)\n",
1708 (u64)sectors,
1709 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1710
1711 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001712 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001713 case 0x1:
1714 strlcpy(cbuf, "A0", 3);
1715 break;
1716 case 0x3:
1717 strlcpy(cbuf, "A2", 3);
1718 break;
1719 default:
1720 strlcpy(cbuf, "?", 2);
1721 break;
1722 }
1723 dev_info(&port->dd->pdev->dev,
1724 "Card Type: %s\n", cbuf);
1725}
1726
1727/*
1728 * Map the commands scatter list into the command table.
1729 *
1730 * @command Pointer to the command.
1731 * @nents Number of scatter list entries.
1732 *
1733 * return value
1734 * None
1735 */
1736static inline void fill_command_sg(struct driver_data *dd,
1737 struct mtip_cmd *command,
1738 int nents)
1739{
1740 int n;
1741 unsigned int dma_len;
1742 struct mtip_cmd_sg *command_sg;
1743 struct scatterlist *sg = command->sg;
1744
1745 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1746
1747 for (n = 0; n < nents; n++) {
1748 dma_len = sg_dma_len(sg);
1749 if (dma_len > 0x400000)
1750 dev_err(&dd->pdev->dev,
1751 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001752 command_sg->info = __force_bit2int
1753 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1754 command_sg->dba = __force_bit2int
1755 cpu_to_le32(sg_dma_address(sg));
1756 command_sg->dba_upper = __force_bit2int
1757 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001758 command_sg++;
1759 sg++;
1760 }
1761}
1762
1763/*
1764 * @brief Execute a drive command.
1765 *
1766 * return value 0 The command completed successfully.
1767 * return value -1 An error occurred while executing the command.
1768 */
Jens Axboe63166682011-09-27 21:27:43 -06001769static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001770{
1771 struct host_to_dev_fis fis;
1772 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1773
Sam Bradshaw88523a62011-08-30 08:34:26 -06001774 /* Build the FIS. */
1775 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1776 fis.type = 0x27;
1777 fis.opts = 1 << 7;
1778 fis.command = command[0];
1779 fis.features = command[1];
1780 fis.sect_count = command[2];
1781 fis.sector = command[3];
1782 fis.cyl_low = command[4];
1783 fis.cyl_hi = command[5];
1784 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1785
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001786 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 -06001787 __func__,
1788 command[0],
1789 command[1],
1790 command[2],
1791 command[3],
1792 command[4],
1793 command[5],
1794 command[6]);
1795
1796 /* Execute the command. */
1797 if (mtip_exec_internal_command(port,
1798 &fis,
1799 5,
1800 0,
1801 0,
1802 0,
1803 GFP_KERNEL,
1804 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001805 return -1;
1806 }
1807
1808 command[0] = reply->command; /* Status*/
1809 command[1] = reply->features; /* Error*/
1810 command[4] = reply->cyl_low;
1811 command[5] = reply->cyl_hi;
1812
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001813 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 -06001814 __func__,
1815 command[0],
1816 command[1],
1817 command[4],
1818 command[5]);
1819
Sam Bradshaw88523a62011-08-30 08:34:26 -06001820 return 0;
1821}
1822
1823/*
1824 * @brief Execute a drive command.
1825 *
1826 * @param port Pointer to the port data structure.
1827 * @param command Pointer to the user specified command parameters.
1828 * @param user_buffer Pointer to the user space buffer where read sector
1829 * data should be copied.
1830 *
1831 * return value 0 The command completed successfully.
1832 * return value -EFAULT An error occurred while copying the completion
1833 * data to the user space buffer.
1834 * return value -1 An error occurred while executing the command.
1835 */
Jens Axboe63166682011-09-27 21:27:43 -06001836static int exec_drive_command(struct mtip_port *port, u8 *command,
1837 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001838{
1839 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001840 struct host_to_dev_fis *reply;
1841 u8 *buf = NULL;
1842 dma_addr_t dma_addr = 0;
1843 int rv = 0, xfer_sz = command[3];
1844
1845 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001846 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001847 return -EFAULT;
1848
1849 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1850 ATA_SECT_SIZE * xfer_sz,
1851 &dma_addr,
1852 GFP_KERNEL);
1853 if (!buf) {
1854 dev_err(&port->dd->pdev->dev,
1855 "Memory allocation failed (%d bytes)\n",
1856 ATA_SECT_SIZE * xfer_sz);
1857 return -ENOMEM;
1858 }
1859 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1860 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001861
Sam Bradshaw88523a62011-08-30 08:34:26 -06001862 /* Build the FIS. */
1863 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001864 fis.type = 0x27;
1865 fis.opts = 1 << 7;
1866 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001867 fis.features = command[2];
1868 fis.sect_count = command[3];
1869 if (fis.command == ATA_CMD_SMART) {
1870 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001871 fis.cyl_low = 0x4F;
1872 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001873 }
1874
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001875 if (xfer_sz)
1876 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1877 else
1878 reply = (port->rxfis + RX_FIS_D2H_REG);
1879
Sam Bradshaw88523a62011-08-30 08:34:26 -06001880 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001881 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 "feat %x, sectcnt %x\n",
1883 __func__,
1884 command[0],
1885 command[1],
1886 command[2],
1887 command[3]);
1888
Sam Bradshaw88523a62011-08-30 08:34:26 -06001889 /* Execute the command. */
1890 if (mtip_exec_internal_command(port,
1891 &fis,
1892 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001893 (xfer_sz ? dma_addr : 0),
1894 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001895 0,
1896 GFP_KERNEL,
1897 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1898 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001899 rv = -EFAULT;
1900 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001901 }
1902
1903 /* Collect the completion status. */
1904 command[0] = reply->command; /* Status*/
1905 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001906 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001907
1908 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001909 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001910 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001911 __func__,
1912 command[0],
1913 command[1],
1914 command[2]);
1915
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001916 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001917 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001918 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001919 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001920 rv = -EFAULT;
1921 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922 }
1923 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001924exit_drive_command:
1925 if (buf)
1926 dmam_free_coherent(&port->dd->pdev->dev,
1927 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1928 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001929}
1930
1931/*
1932 * Indicates whether a command has a single sector payload.
1933 *
1934 * @command passed to the device to perform the certain event.
1935 * @features passed to the device to perform the certain event.
1936 *
1937 * return value
1938 * 1 command is one that always has a single sector payload,
1939 * regardless of the value in the Sector Count field.
1940 * 0 otherwise
1941 *
1942 */
1943static unsigned int implicit_sector(unsigned char command,
1944 unsigned char features)
1945{
1946 unsigned int rv = 0;
1947
1948 /* list of commands that have an implicit sector count of 1 */
1949 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001950 case ATA_CMD_SEC_SET_PASS:
1951 case ATA_CMD_SEC_UNLOCK:
1952 case ATA_CMD_SEC_ERASE_PREP:
1953 case ATA_CMD_SEC_ERASE_UNIT:
1954 case ATA_CMD_SEC_FREEZE_LOCK:
1955 case ATA_CMD_SEC_DISABLE_PASS:
1956 case ATA_CMD_PMP_READ:
1957 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001958 rv = 1;
1959 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001960 case ATA_CMD_SET_MAX:
1961 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001962 rv = 1;
1963 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001964 case ATA_CMD_SMART:
1965 if ((features == ATA_SMART_READ_VALUES) ||
1966 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001967 rv = 1;
1968 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001969 case ATA_CMD_CONF_OVERLAY:
1970 if ((features == ATA_DCO_IDENTIFY) ||
1971 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001972 rv = 1;
1973 break;
1974 }
1975 return rv;
1976}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001977
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978/*
1979 * Executes a taskfile
1980 * See ide_taskfile_ioctl() for derivation
1981 */
1982static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001983 void __user *buf,
1984 ide_task_request_t *req_task,
1985 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001986{
1987 struct host_to_dev_fis fis;
1988 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989 u8 *outbuf = NULL;
1990 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001991 dma_addr_t outbuf_dma = 0;
1992 dma_addr_t inbuf_dma = 0;
1993 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001994 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001995 unsigned int taskin = 0;
1996 unsigned int taskout = 0;
1997 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001998 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001999 unsigned int force_single_sector;
2000 unsigned int transfer_size;
2001 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002002 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002003 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002004
2005 taskout = req_task->out_size;
2006 taskin = req_task->in_size;
2007 /* 130560 = 512 * 0xFF*/
2008 if (taskin > 130560 || taskout > 130560) {
2009 err = -EINVAL;
2010 goto abort;
2011 }
2012
2013 if (taskout) {
2014 outbuf = kzalloc(taskout, GFP_KERNEL);
2015 if (outbuf == NULL) {
2016 err = -ENOMEM;
2017 goto abort;
2018 }
2019 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2020 err = -EFAULT;
2021 goto abort;
2022 }
2023 outbuf_dma = pci_map_single(dd->pdev,
2024 outbuf,
2025 taskout,
2026 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002027 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002028 err = -ENOMEM;
2029 goto abort;
2030 }
2031 dma_buffer = outbuf_dma;
2032 }
2033
2034 if (taskin) {
2035 inbuf = kzalloc(taskin, GFP_KERNEL);
2036 if (inbuf == NULL) {
2037 err = -ENOMEM;
2038 goto abort;
2039 }
2040
2041 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2042 err = -EFAULT;
2043 goto abort;
2044 }
2045 inbuf_dma = pci_map_single(dd->pdev,
2046 inbuf,
2047 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002048 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002049 err = -ENOMEM;
2050 goto abort;
2051 }
2052 dma_buffer = inbuf_dma;
2053 }
2054
2055 /* only supports PIO and non-data commands from this ioctl. */
2056 switch (req_task->data_phase) {
2057 case TASKFILE_OUT:
2058 nsect = taskout / ATA_SECT_SIZE;
2059 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2060 break;
2061 case TASKFILE_IN:
2062 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2063 break;
2064 case TASKFILE_NO_DATA:
2065 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2066 break;
2067 default:
2068 err = -EINVAL;
2069 goto abort;
2070 }
2071
Sam Bradshaw88523a62011-08-30 08:34:26 -06002072 /* Build the FIS. */
2073 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2074
2075 fis.type = 0x27;
2076 fis.opts = 1 << 7;
2077 fis.command = req_task->io_ports[7];
2078 fis.features = req_task->io_ports[1];
2079 fis.sect_count = req_task->io_ports[2];
2080 fis.lba_low = req_task->io_ports[3];
2081 fis.lba_mid = req_task->io_ports[4];
2082 fis.lba_hi = req_task->io_ports[5];
2083 /* Clear the dev bit*/
2084 fis.device = req_task->io_ports[6] & ~0x10;
2085
2086 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2087 req_task->in_flags.all =
2088 IDE_TASKFILE_STD_IN_FLAGS |
2089 (IDE_HOB_STD_IN_FLAGS << 8);
2090 fis.lba_low_ex = req_task->hob_ports[3];
2091 fis.lba_mid_ex = req_task->hob_ports[4];
2092 fis.lba_hi_ex = req_task->hob_ports[5];
2093 fis.features_ex = req_task->hob_ports[1];
2094 fis.sect_cnt_ex = req_task->hob_ports[2];
2095
2096 } else {
2097 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2098 }
2099
2100 force_single_sector = implicit_sector(fis.command, fis.features);
2101
2102 if ((taskin || taskout) && (!fis.sect_count)) {
2103 if (nsect)
2104 fis.sect_count = nsect;
2105 else {
2106 if (!force_single_sector) {
2107 dev_warn(&dd->pdev->dev,
2108 "data movement but "
2109 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002110 err = -EINVAL;
2111 goto abort;
2112 }
2113 }
2114 }
2115
2116 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002117 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002118 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2119 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002120 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002121 fis.command,
2122 fis.features,
2123 fis.sect_count,
2124 fis.lba_low,
2125 fis.lba_mid,
2126 fis.lba_hi,
2127 fis.device);
2128
Selvan Mani4453bc82012-09-27 14:36:43 +02002129 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002130 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2131 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002132 erasemode = 1;
2133 }
2134
2135 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002136
2137 /* Determine the correct transfer size.*/
2138 if (force_single_sector)
2139 transfer_size = ATA_SECT_SIZE;
2140 else
2141 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2142
2143 /* Execute the command.*/
2144 if (mtip_exec_internal_command(dd->port,
2145 &fis,
2146 5,
2147 dma_buffer,
2148 transfer_size,
2149 0,
2150 GFP_KERNEL,
2151 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002152 err = -EIO;
2153 goto abort;
2154 }
2155
2156 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2157
2158 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2159 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2160 req_task->io_ports[7] = reply->control;
2161 } else {
2162 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2163 req_task->io_ports[7] = reply->command;
2164 }
2165
2166 /* reclaim the DMA buffers.*/
2167 if (inbuf_dma)
2168 pci_unmap_single(dd->pdev, inbuf_dma,
2169 taskin, DMA_FROM_DEVICE);
2170 if (outbuf_dma)
2171 pci_unmap_single(dd->pdev, outbuf_dma,
2172 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002173 inbuf_dma = 0;
2174 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002175
2176 /* return the ATA registers to the caller.*/
2177 req_task->io_ports[1] = reply->features;
2178 req_task->io_ports[2] = reply->sect_count;
2179 req_task->io_ports[3] = reply->lba_low;
2180 req_task->io_ports[4] = reply->lba_mid;
2181 req_task->io_ports[5] = reply->lba_hi;
2182 req_task->io_ports[6] = reply->device;
2183
2184 if (req_task->out_flags.all & 1) {
2185
2186 req_task->hob_ports[3] = reply->lba_low_ex;
2187 req_task->hob_ports[4] = reply->lba_mid_ex;
2188 req_task->hob_ports[5] = reply->lba_hi_ex;
2189 req_task->hob_ports[1] = reply->features_ex;
2190 req_task->hob_ports[2] = reply->sect_cnt_ex;
2191 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002192 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002193 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002194 "err %x, sect_cnt %x, lbalo %x,"
2195 "lbamid %x, lbahi %x, dev %x\n",
2196 __func__,
2197 req_task->io_ports[7],
2198 req_task->io_ports[1],
2199 req_task->io_ports[2],
2200 req_task->io_ports[3],
2201 req_task->io_ports[4],
2202 req_task->io_ports[5],
2203 req_task->io_ports[6]);
2204
Sam Bradshaw88523a62011-08-30 08:34:26 -06002205 if (taskout) {
2206 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2207 err = -EFAULT;
2208 goto abort;
2209 }
2210 }
2211 if (taskin) {
2212 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2213 err = -EFAULT;
2214 goto abort;
2215 }
2216 }
2217abort:
2218 if (inbuf_dma)
2219 pci_unmap_single(dd->pdev, inbuf_dma,
2220 taskin, DMA_FROM_DEVICE);
2221 if (outbuf_dma)
2222 pci_unmap_single(dd->pdev, outbuf_dma,
2223 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002224 kfree(outbuf);
2225 kfree(inbuf);
2226
2227 return err;
2228}
2229
2230/*
2231 * Handle IOCTL calls from the Block Layer.
2232 *
2233 * This function is called by the Block Layer when it receives an IOCTL
2234 * command that it does not understand. If the IOCTL command is not supported
2235 * this function returns -ENOTTY.
2236 *
2237 * @dd Pointer to the driver data structure.
2238 * @cmd IOCTL command passed from the Block Layer.
2239 * @arg IOCTL argument passed from the Block Layer.
2240 *
2241 * return value
2242 * 0 The IOCTL completed successfully.
2243 * -ENOTTY The specified command is not supported.
2244 * -EFAULT An error occurred copying data to a user space buffer.
2245 * -EIO An error occurred while executing the command.
2246 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002247static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2248 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002249{
2250 switch (cmd) {
2251 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002252 {
2253 if (copy_to_user((void __user *)arg, dd->port->identify,
2254 sizeof(u16) * ATA_ID_WORDS))
2255 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002256 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002257 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002258 case HDIO_DRIVE_CMD:
2259 {
2260 u8 drive_command[4];
2261
2262 /* Copy the user command info to our buffer. */
2263 if (copy_from_user(drive_command,
2264 (void __user *) arg,
2265 sizeof(drive_command)))
2266 return -EFAULT;
2267
2268 /* Execute the drive command. */
2269 if (exec_drive_command(dd->port,
2270 drive_command,
2271 (void __user *) (arg+4)))
2272 return -EIO;
2273
2274 /* Copy the status back to the users buffer. */
2275 if (copy_to_user((void __user *) arg,
2276 drive_command,
2277 sizeof(drive_command)))
2278 return -EFAULT;
2279
2280 break;
2281 }
2282 case HDIO_DRIVE_TASK:
2283 {
2284 u8 drive_command[7];
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_task(dd->port, drive_command))
2294 return -EIO;
2295
2296 /* Copy the status back to the users buffer. */
2297 if (copy_to_user((void __user *) arg,
2298 drive_command,
2299 sizeof(drive_command)))
2300 return -EFAULT;
2301
2302 break;
2303 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002304 case HDIO_DRIVE_TASKFILE: {
2305 ide_task_request_t req_task;
2306 int ret, outtotal;
2307
2308 if (copy_from_user(&req_task, (void __user *) arg,
2309 sizeof(req_task)))
2310 return -EFAULT;
2311
2312 outtotal = sizeof(req_task);
2313
2314 ret = exec_drive_taskfile(dd, (void __user *) arg,
2315 &req_task, outtotal);
2316
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002317 if (copy_to_user((void __user *) arg, &req_task,
2318 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002319 return -EFAULT;
2320
2321 return ret;
2322 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002323
2324 default:
2325 return -EINVAL;
2326 }
2327 return 0;
2328}
2329
2330/*
2331 * Submit an IO to the hw
2332 *
2333 * This function is called by the block layer to issue an io
2334 * to the device. Upon completion, the callback function will
2335 * be called with the data parameter passed as the callback data.
2336 *
2337 * @dd Pointer to the driver data structure.
2338 * @start First sector to read.
2339 * @nsect Number of sectors to read.
2340 * @nents Number of entries in scatter list for the read command.
2341 * @tag The tag of this read command.
2342 * @callback Pointer to the function that should be called
2343 * when the read completes.
2344 * @data Callback data passed to the callback function
2345 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002346 * @dir Direction (read or write)
2347 *
2348 * return value
2349 * None
2350 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002351static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2352 struct mtip_cmd *command, int nents,
2353 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002354{
2355 struct host_to_dev_fis *fis;
2356 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002357 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2358 u64 start = blk_rq_pos(rq);
2359 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002360
2361 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002362 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002363
2364 command->scatter_ents = nents;
2365
2366 /*
2367 * The number of retries for this command before it is
2368 * reported as a failure to the upper layers.
2369 */
2370 command->retries = MTIP_MAX_RETRIES;
2371
2372 /* Fill out fis */
2373 fis = command->command;
2374 fis->type = 0x27;
2375 fis->opts = 1 << 7;
Jens Axboeffc771b2014-05-09 09:42:02 -06002376 if (rq_data_dir(rq) == READ)
2377 fis->command = ATA_CMD_FPDMA_READ;
2378 else
2379 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002380 fis->lba_low = start & 0xFF;
2381 fis->lba_mid = (start >> 8) & 0xFF;
2382 fis->lba_hi = (start >> 16) & 0xFF;
2383 fis->lba_low_ex = (start >> 24) & 0xFF;
2384 fis->lba_mid_ex = (start >> 32) & 0xFF;
2385 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002386 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002387 fis->features = nsect & 0xFF;
2388 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002389 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002390 fis->sect_cnt_ex = 0;
2391 fis->control = 0;
2392 fis->res2 = 0;
2393 fis->res3 = 0;
2394 fill_command_sg(dd, command, nents);
2395
Jens Axboeffc771b2014-05-09 09:42:02 -06002396 if (command->unaligned)
Asai Thambi S P2077d942013-04-29 21:19:49 +02002397 fis->device |= 1 << 7;
2398
Sam Bradshaw88523a62011-08-30 08:34:26 -06002399 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002400 command->command_header->opts =
2401 __force_bit2int cpu_to_le32(
2402 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002403 command->command_header->byte_count = 0;
2404
2405 /*
2406 * Set the completion function and data for the command
2407 * within this layer.
2408 */
2409 command->comp_data = dd;
2410 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002411 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002412
2413 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002414 * To prevent this command from being issued
2415 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002416 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002417 if (port->flags & MTIP_PF_PAUSE_IO) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002418 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002419 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002420 return;
2421 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422
2423 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002424 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002425}
2426
2427/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002428 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002429 *
2430 * @dev Pointer to the device structure, passed by the kernrel.
2431 * @attr Pointer to the device_attribute structure passed by the kernel.
2432 * @buf Pointer to the char buffer that will receive the stats info.
2433 *
2434 * return value
2435 * The size, in bytes, of the data copied into buf.
2436 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002437static ssize_t mtip_hw_show_status(struct device *dev,
2438 struct device_attribute *attr,
2439 char *buf)
2440{
2441 struct driver_data *dd = dev_to_disk(dev)->private_data;
2442 int size = 0;
2443
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002444 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002445 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002446 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002447 size += sprintf(buf, "%s", "write_protect\n");
2448 else
2449 size += sprintf(buf, "%s", "online\n");
2450
2451 return size;
2452}
2453
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002454static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002455
Asai Thambi S P0caff002013-04-03 19:56:21 +05302456/* debugsfs entries */
2457
2458static ssize_t show_device_status(struct device_driver *drv, char *buf)
2459{
2460 int size = 0;
2461 struct driver_data *dd, *tmp;
2462 unsigned long flags;
2463 char id_buf[42];
2464 u16 status = 0;
2465
2466 spin_lock_irqsave(&dev_lock, flags);
2467 size += sprintf(&buf[size], "Devices Present:\n");
2468 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002469 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302470 if (dd->port &&
2471 dd->port->identify &&
2472 dd->port->identify_valid) {
2473 strlcpy(id_buf,
2474 (char *) (dd->port->identify + 10), 21);
2475 status = *(dd->port->identify + 141);
2476 } else {
2477 memset(id_buf, 0, 42);
2478 status = 0;
2479 }
2480
2481 if (dd->port &&
2482 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2483 size += sprintf(&buf[size],
2484 " device %s %s (ftl rebuild %d %%)\n",
2485 dev_name(&dd->pdev->dev),
2486 id_buf,
2487 status);
2488 } else {
2489 size += sprintf(&buf[size],
2490 " device %s %s\n",
2491 dev_name(&dd->pdev->dev),
2492 id_buf);
2493 }
2494 }
2495 }
2496
2497 size += sprintf(&buf[size], "Devices Being Removed:\n");
2498 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002499 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302500 if (dd->port &&
2501 dd->port->identify &&
2502 dd->port->identify_valid) {
2503 strlcpy(id_buf,
2504 (char *) (dd->port->identify+10), 21);
2505 status = *(dd->port->identify + 141);
2506 } else {
2507 memset(id_buf, 0, 42);
2508 status = 0;
2509 }
2510
2511 if (dd->port &&
2512 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2513 size += sprintf(&buf[size],
2514 " device %s %s (ftl rebuild %d %%)\n",
2515 dev_name(&dd->pdev->dev),
2516 id_buf,
2517 status);
2518 } else {
2519 size += sprintf(&buf[size],
2520 " device %s %s\n",
2521 dev_name(&dd->pdev->dev),
2522 id_buf);
2523 }
2524 }
2525 }
2526 spin_unlock_irqrestore(&dev_lock, flags);
2527
2528 return size;
2529}
2530
2531static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2532 size_t len, loff_t *offset)
2533{
David Milburnc8afd0d2013-05-23 16:23:45 -05002534 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302535 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002536 char *buf;
2537 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302538
2539 if (!len || *offset)
2540 return 0;
2541
David Milburnc8afd0d2013-05-23 16:23:45 -05002542 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2543 if (!buf) {
2544 dev_err(&dd->pdev->dev,
2545 "Memory allocation: status buffer\n");
2546 return -ENOMEM;
2547 }
2548
Asai Thambi S P0caff002013-04-03 19:56:21 +05302549 size += show_device_status(NULL, buf);
2550
2551 *offset = size <= len ? size : len;
2552 size = copy_to_user(ubuf, buf, *offset);
2553 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002554 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302555
David Milburnc8afd0d2013-05-23 16:23:45 -05002556 kfree(buf);
2557 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302558}
2559
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002560static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2561 size_t len, loff_t *offset)
2562{
2563 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002564 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002565 u32 group_allocated;
2566 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002567 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002568
2569 if (!len || size)
2570 return 0;
2571
David Milburnc8afd0d2013-05-23 16:23:45 -05002572 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2573 if (!buf) {
2574 dev_err(&dd->pdev->dev,
2575 "Memory allocation: register buffer\n");
2576 return -ENOMEM;
2577 }
2578
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002579 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2580
2581 for (n = dd->slot_groups-1; n >= 0; n--)
2582 size += sprintf(&buf[size], "%08X ",
2583 readl(dd->port->s_active[n]));
2584
2585 size += sprintf(&buf[size], "]\n");
2586 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2587
2588 for (n = dd->slot_groups-1; n >= 0; n--)
2589 size += sprintf(&buf[size], "%08X ",
2590 readl(dd->port->cmd_issue[n]));
2591
2592 size += sprintf(&buf[size], "]\n");
2593 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2594
2595 for (n = dd->slot_groups-1; n >= 0; n--)
2596 size += sprintf(&buf[size], "%08X ",
2597 readl(dd->port->completed[n]));
2598
2599 size += sprintf(&buf[size], "]\n");
2600 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2601 readl(dd->port->mmio + PORT_IRQ_STAT));
2602 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2603 readl(dd->mmio + HOST_IRQ_STAT));
2604 size += sprintf(&buf[size], "\n");
2605
2606 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2607
2608 for (n = dd->slot_groups-1; n >= 0; n--) {
2609 if (sizeof(long) > sizeof(u32))
2610 group_allocated =
2611 dd->port->allocated[n/2] >> (32*(n&1));
2612 else
2613 group_allocated = dd->port->allocated[n];
2614 size += sprintf(&buf[size], "%08X ", group_allocated);
2615 }
2616 size += sprintf(&buf[size], "]\n");
2617
2618 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2619
2620 for (n = dd->slot_groups-1; n >= 0; n--) {
2621 if (sizeof(long) > sizeof(u32))
2622 group_allocated =
2623 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2624 else
2625 group_allocated = dd->port->cmds_to_issue[n];
2626 size += sprintf(&buf[size], "%08X ", group_allocated);
2627 }
2628 size += sprintf(&buf[size], "]\n");
2629
2630 *offset = size <= len ? size : len;
2631 size = copy_to_user(ubuf, buf, *offset);
2632 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002633 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002634
David Milburnc8afd0d2013-05-23 16:23:45 -05002635 kfree(buf);
2636 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002637}
2638
2639static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2640 size_t len, loff_t *offset)
2641{
2642 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002643 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002644 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002645 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002646
2647 if (!len || size)
2648 return 0;
2649
David Milburnc8afd0d2013-05-23 16:23:45 -05002650 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2651 if (!buf) {
2652 dev_err(&dd->pdev->dev,
2653 "Memory allocation: flag buffer\n");
2654 return -ENOMEM;
2655 }
2656
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002657 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2658 dd->port->flags);
2659 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2660 dd->dd_flag);
2661
2662 *offset = size <= len ? size : len;
2663 size = copy_to_user(ubuf, buf, *offset);
2664 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002665 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002666
David Milburnc8afd0d2013-05-23 16:23:45 -05002667 kfree(buf);
2668 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002669}
2670
Asai Thambi S P0caff002013-04-03 19:56:21 +05302671static const struct file_operations mtip_device_status_fops = {
2672 .owner = THIS_MODULE,
2673 .open = simple_open,
2674 .read = mtip_hw_read_device_status,
2675 .llseek = no_llseek,
2676};
2677
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002678static const struct file_operations mtip_regs_fops = {
2679 .owner = THIS_MODULE,
2680 .open = simple_open,
2681 .read = mtip_hw_read_registers,
2682 .llseek = no_llseek,
2683};
2684
2685static const struct file_operations mtip_flags_fops = {
2686 .owner = THIS_MODULE,
2687 .open = simple_open,
2688 .read = mtip_hw_read_flags,
2689 .llseek = no_llseek,
2690};
2691
Sam Bradshaw88523a62011-08-30 08:34:26 -06002692/*
2693 * Create the sysfs related attributes.
2694 *
2695 * @dd Pointer to the driver data structure.
2696 * @kobj Pointer to the kobj for the block device.
2697 *
2698 * return value
2699 * 0 Operation completed successfully.
2700 * -EINVAL Invalid parameter.
2701 */
Jens Axboe63166682011-09-27 21:27:43 -06002702static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002703{
2704 if (!kobj || !dd)
2705 return -EINVAL;
2706
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002707 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2708 dev_warn(&dd->pdev->dev,
2709 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002710 return 0;
2711}
2712
2713/*
2714 * Remove the sysfs related attributes.
2715 *
2716 * @dd Pointer to the driver data structure.
2717 * @kobj Pointer to the kobj for the block device.
2718 *
2719 * return value
2720 * 0 Operation completed successfully.
2721 * -EINVAL Invalid parameter.
2722 */
Jens Axboe63166682011-09-27 21:27:43 -06002723static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002724{
2725 if (!kobj || !dd)
2726 return -EINVAL;
2727
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002728 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002729
2730 return 0;
2731}
2732
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002733static int mtip_hw_debugfs_init(struct driver_data *dd)
2734{
2735 if (!dfs_parent)
2736 return -1;
2737
2738 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2739 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2740 dev_warn(&dd->pdev->dev,
2741 "Error creating node %s under debugfs\n",
2742 dd->disk->disk_name);
2743 dd->dfs_node = NULL;
2744 return -1;
2745 }
2746
2747 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2748 &mtip_flags_fops);
2749 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2750 &mtip_regs_fops);
2751
2752 return 0;
2753}
2754
2755static void mtip_hw_debugfs_exit(struct driver_data *dd)
2756{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002757 if (dd->dfs_node)
2758 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002759}
2760
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002761static int mtip_free_orphan(struct driver_data *dd)
2762{
2763 struct kobject *kobj;
2764
2765 if (dd->bdev) {
2766 if (dd->bdev->bd_holders >= 1)
2767 return -2;
2768
2769 bdput(dd->bdev);
2770 dd->bdev = NULL;
2771 }
2772
2773 mtip_hw_debugfs_exit(dd);
2774
2775 spin_lock(&rssd_index_lock);
2776 ida_remove(&rssd_index_ida, dd->index);
2777 spin_unlock(&rssd_index_lock);
2778
2779 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
2780 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
2781 put_disk(dd->disk);
2782 } else {
2783 if (dd->disk) {
2784 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
2785 if (kobj) {
2786 mtip_hw_sysfs_exit(dd, kobj);
2787 kobject_put(kobj);
2788 }
2789 del_gendisk(dd->disk);
2790 dd->disk = NULL;
2791 }
2792 if (dd->queue) {
2793 dd->queue->queuedata = NULL;
2794 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06002795 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002796 dd->queue = NULL;
2797 }
2798 }
2799 kfree(dd);
2800 return 0;
2801}
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002802
Sam Bradshaw88523a62011-08-30 08:34:26 -06002803/*
2804 * Perform any init/resume time hardware setup
2805 *
2806 * @dd Pointer to the driver data structure.
2807 *
2808 * return value
2809 * None
2810 */
2811static inline void hba_setup(struct driver_data *dd)
2812{
2813 u32 hwdata;
2814 hwdata = readl(dd->mmio + HOST_HSORG);
2815
2816 /* interrupt bug workaround: use only 1 IS bit.*/
2817 writel(hwdata |
2818 HSORG_DISABLE_SLOTGRP_INTR |
2819 HSORG_DISABLE_SLOTGRP_PXIS,
2820 dd->mmio + HOST_HSORG);
2821}
2822
Asai Thambi S P2077d942013-04-29 21:19:49 +02002823static int mtip_device_unaligned_constrained(struct driver_data *dd)
2824{
2825 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2826}
2827
Sam Bradshaw88523a62011-08-30 08:34:26 -06002828/*
2829 * Detect the details of the product, and store anything needed
2830 * into the driver data structure. This includes product type and
2831 * version and number of slot groups.
2832 *
2833 * @dd Pointer to the driver data structure.
2834 *
2835 * return value
2836 * None
2837 */
2838static void mtip_detect_product(struct driver_data *dd)
2839{
2840 u32 hwdata;
2841 unsigned int rev, slotgroups;
2842
2843 /*
2844 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2845 * info register:
2846 * [15:8] hardware/software interface rev#
2847 * [ 3] asic-style interface
2848 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2849 */
2850 hwdata = readl(dd->mmio + HOST_HSORG);
2851
2852 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2853 dd->slot_groups = 1;
2854
2855 if (hwdata & 0x8) {
2856 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2857 rev = (hwdata & HSORG_HWREV) >> 8;
2858 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2859 dev_info(&dd->pdev->dev,
2860 "ASIC-FPGA design, HS rev 0x%x, "
2861 "%i slot groups [%i slots]\n",
2862 rev,
2863 slotgroups,
2864 slotgroups * 32);
2865
2866 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2867 dev_warn(&dd->pdev->dev,
2868 "Warning: driver only supports "
2869 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2870 slotgroups = MTIP_MAX_SLOT_GROUPS;
2871 }
2872 dd->slot_groups = slotgroups;
2873 return;
2874 }
2875
2876 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2877}
2878
2879/*
2880 * Blocking wait for FTL rebuild to complete
2881 *
2882 * @dd Pointer to the DRIVER_DATA structure.
2883 *
2884 * return value
2885 * 0 FTL rebuild completed successfully
2886 * -EFAULT FTL rebuild error/timeout/interruption
2887 */
2888static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2889{
2890 unsigned long timeout, cnt = 0, start;
2891
2892 dev_warn(&dd->pdev->dev,
2893 "FTL rebuild in progress. Polling for completion.\n");
2894
2895 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002896 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2897
2898 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002899 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002900 &dd->dd_flag)))
2901 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002902 if (mtip_check_surprise_removal(dd->pdev))
2903 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002904
Sam Bradshaw88523a62011-08-30 08:34:26 -06002905 if (mtip_get_identify(dd->port, NULL) < 0)
2906 return -EFAULT;
2907
2908 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2909 MTIP_FTL_REBUILD_MAGIC) {
2910 ssleep(1);
2911 /* Print message every 3 minutes */
2912 if (cnt++ >= 180) {
2913 dev_warn(&dd->pdev->dev,
2914 "FTL rebuild in progress (%d secs).\n",
2915 jiffies_to_msecs(jiffies - start) / 1000);
2916 cnt = 0;
2917 }
2918 } else {
2919 dev_warn(&dd->pdev->dev,
2920 "FTL rebuild complete (%d secs).\n",
2921 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002922 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002923 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002924 }
2925 ssleep(10);
2926 } while (time_before(jiffies, timeout));
2927
2928 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002929 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002930 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2931 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002932 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002933}
2934
2935/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002936 * service thread to issue queued commands
2937 *
2938 * @data Pointer to the driver data structure.
2939 *
2940 * return value
2941 * 0
2942 */
2943
2944static int mtip_service_thread(void *data)
2945{
2946 struct driver_data *dd = (struct driver_data *)data;
2947 unsigned long slot, slot_start, slot_wrap;
2948 unsigned int num_cmd_slots = dd->slot_groups * 32;
2949 struct mtip_port *port = dd->port;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002950 int ret;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002951
2952 while (1) {
2953 /*
2954 * the condition is to check neither an internal command is
2955 * is in progress nor error handling is active
2956 */
2957 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002958 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002959
2960 if (kthread_should_stop())
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002961 goto st_out;
2962
2963 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2964
2965 /* If I am an orphan, start self cleanup */
2966 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002967 break;
2968
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002969 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002970 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002971 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002972
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002973 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002974 slot = 1;
2975 /* used to restrict the loop to one iteration */
2976 slot_start = num_cmd_slots;
2977 slot_wrap = 0;
2978 while (1) {
2979 slot = find_next_bit(port->cmds_to_issue,
2980 num_cmd_slots, slot);
2981 if (slot_wrap == 1) {
2982 if ((slot_start >= slot) ||
2983 (slot >= num_cmd_slots))
2984 break;
2985 }
2986 if (unlikely(slot_start == num_cmd_slots))
2987 slot_start = slot;
2988
2989 if (unlikely(slot == num_cmd_slots)) {
2990 slot = 1;
2991 slot_wrap = 1;
2992 continue;
2993 }
2994
2995 /* Issue the command to the hardware */
2996 mtip_issue_ncq_command(port, slot);
2997
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002998 clear_bit(slot, port->cmds_to_issue);
2999 }
3000
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003001 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3002 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003003 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003004 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003005 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003006 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003007 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003008 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003009
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003010 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003011 goto st_out;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003012 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003013
3014 /* wait for pci remove to exit */
3015 while (1) {
3016 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3017 break;
3018 msleep_interruptible(1000);
3019 if (kthread_should_stop())
3020 goto st_out;
3021 }
3022
3023 while (1) {
3024 ret = mtip_free_orphan(dd);
3025 if (!ret) {
3026 /* NOTE: All data structures are invalid, do not
3027 * access any here */
3028 return 0;
3029 }
3030 msleep_interruptible(1000);
3031 if (kthread_should_stop())
3032 goto st_out;
3033 }
3034st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003035 return 0;
3036}
3037
3038/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003039 * DMA region teardown
3040 *
3041 * @dd Pointer to driver_data structure
3042 *
3043 * return value
3044 * None
3045 */
3046static void mtip_dma_free(struct driver_data *dd)
3047{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003048 struct mtip_port *port = dd->port;
3049
3050 if (port->block1)
3051 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3052 port->block1, port->block1_dma);
3053
3054 if (port->command_list) {
3055 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3056 port->command_list, port->command_list_dma);
3057 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003058}
3059
3060/*
3061 * DMA region setup
3062 *
3063 * @dd Pointer to driver_data structure
3064 *
3065 * return value
3066 * -ENOMEM Not enough free DMA region space to initialize driver
3067 */
3068static int mtip_dma_alloc(struct driver_data *dd)
3069{
3070 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003071
3072 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3073 port->block1 =
3074 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3075 &port->block1_dma, GFP_KERNEL);
3076 if (!port->block1)
3077 return -ENOMEM;
3078 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3079
3080 /* Allocate dma memory for command list */
3081 port->command_list =
3082 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3083 &port->command_list_dma, GFP_KERNEL);
3084 if (!port->command_list) {
3085 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3086 port->block1, port->block1_dma);
3087 port->block1 = NULL;
3088 port->block1_dma = 0;
3089 return -ENOMEM;
3090 }
3091 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3092
3093 /* Setup all pointers into first DMA region */
3094 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3095 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3096 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3097 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3098 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3099 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3100 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3101 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3102
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003103 return 0;
3104}
3105
Jens Axboeffc771b2014-05-09 09:42:02 -06003106static int mtip_hw_get_identify(struct driver_data *dd)
3107{
3108 struct smart_attr attr242;
3109 unsigned char *buf;
3110 int rv;
3111
3112 if (mtip_get_identify(dd->port, NULL) < 0)
3113 return -EFAULT;
3114
3115 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3116 MTIP_FTL_REBUILD_MAGIC) {
3117 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3118 return MTIP_FTL_REBUILD_MAGIC;
3119 }
3120 mtip_dump_identify(dd->port);
3121
3122 /* check write protect, over temp and rebuild statuses */
3123 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3124 dd->port->log_buf,
3125 dd->port->log_buf_dma, 1);
3126 if (rv) {
3127 dev_warn(&dd->pdev->dev,
3128 "Error in READ LOG EXT (10h) command\n");
3129 /* non-critical error, don't fail the load */
3130 } else {
3131 buf = (unsigned char *)dd->port->log_buf;
3132 if (buf[259] & 0x1) {
3133 dev_info(&dd->pdev->dev,
3134 "Write protect bit is set.\n");
3135 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3136 }
3137 if (buf[288] == 0xF7) {
3138 dev_info(&dd->pdev->dev,
3139 "Exceeded Tmax, drive in thermal shutdown.\n");
3140 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3141 }
3142 if (buf[288] == 0xBF) {
3143 dev_info(&dd->pdev->dev,
3144 "Drive indicates rebuild has failed.\n");
3145 /* TODO */
3146 }
3147 }
3148
3149 /* get write protect progess */
3150 memset(&attr242, 0, sizeof(struct smart_attr));
3151 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3152 dev_warn(&dd->pdev->dev,
3153 "Unable to check write protect progress\n");
3154 else
3155 dev_info(&dd->pdev->dev,
3156 "Write protect progress: %u%% (%u blocks)\n",
3157 attr242.cur, le32_to_cpu(attr242.data));
3158
3159 return rv;
3160}
3161
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003162/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003163 * Called once for each card.
3164 *
3165 * @dd Pointer to the driver data structure.
3166 *
3167 * return value
3168 * 0 on success, else an error code.
3169 */
Jens Axboe63166682011-09-27 21:27:43 -06003170static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003171{
3172 int i;
3173 int rv;
3174 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003175 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003176
3177 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3178
3179 mtip_detect_product(dd);
3180 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3181 rv = -EIO;
3182 goto out1;
3183 }
3184 num_command_slots = dd->slot_groups * 32;
3185
3186 hba_setup(dd);
3187
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003188 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3189 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003190 if (!dd->port) {
3191 dev_err(&dd->pdev->dev,
3192 "Memory allocation: port structure\n");
3193 return -ENOMEM;
3194 }
3195
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003196 /* Continue workqueue setup */
3197 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3198 dd->work[i].port = dd->port;
3199
Asai Thambi S P2077d942013-04-29 21:19:49 +02003200 /* Enable unaligned IO constraints for some devices */
3201 if (mtip_device_unaligned_constrained(dd))
3202 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3203 else
3204 dd->unal_qdepth = 0;
3205
Asai Thambi S P2077d942013-04-29 21:19:49 +02003206 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003207
3208 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003209 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3210 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003211
3212 /* Set the port mmio base address. */
3213 dd->port->mmio = dd->mmio + PORT_OFFSET;
3214 dd->port->dd = dd;
3215
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003216 /* DMA allocations */
3217 rv = mtip_dma_alloc(dd);
3218 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003219 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003220
3221 /* Setup the pointers to the extended s_active and CI registers. */
3222 for (i = 0; i < dd->slot_groups; i++) {
3223 dd->port->s_active[i] =
3224 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3225 dd->port->cmd_issue[i] =
3226 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3227 dd->port->completed[i] =
3228 dd->port->mmio + i*0x80 + PORT_SDBV;
3229 }
3230
Asai Thambi S P45038362012-04-09 08:35:38 +02003231 timetaken = jiffies;
3232 timeout = jiffies + msecs_to_jiffies(30000);
3233 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3234 time_before(jiffies, timeout)) {
3235 mdelay(100);
3236 }
3237 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3238 timetaken = jiffies - timetaken;
3239 dev_warn(&dd->pdev->dev,
3240 "Surprise removal detected at %u ms\n",
3241 jiffies_to_msecs(timetaken));
3242 rv = -ENODEV;
3243 goto out2 ;
3244 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003245 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003246 timetaken = jiffies - timetaken;
3247 dev_warn(&dd->pdev->dev,
3248 "Removal detected at %u ms\n",
3249 jiffies_to_msecs(timetaken));
3250 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003251 goto out2;
3252 }
3253
Asai Thambi S P45038362012-04-09 08:35:38 +02003254 /* Conditionally reset the HBA. */
3255 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3256 if (mtip_hba_reset(dd) < 0) {
3257 dev_err(&dd->pdev->dev,
3258 "Card did not reset within timeout\n");
3259 rv = -EIO;
3260 goto out2;
3261 }
3262 } else {
3263 /* Clear any pending interrupts on the HBA */
3264 writel(readl(dd->mmio + HOST_IRQ_STAT),
3265 dd->mmio + HOST_IRQ_STAT);
3266 }
3267
Sam Bradshaw88523a62011-08-30 08:34:26 -06003268 mtip_init_port(dd->port);
3269 mtip_start_port(dd->port);
3270
3271 /* Setup the ISR and enable interrupts. */
3272 rv = devm_request_irq(&dd->pdev->dev,
3273 dd->pdev->irq,
3274 mtip_irq_handler,
3275 IRQF_SHARED,
3276 dev_driver_string(&dd->pdev->dev),
3277 dd);
3278
3279 if (rv) {
3280 dev_err(&dd->pdev->dev,
3281 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3282 goto out2;
3283 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003284 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003285
3286 /* Enable interrupts on the HBA. */
3287 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3288 dd->mmio + HOST_CTL);
3289
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003290 init_waitqueue_head(&dd->port->svc_wait);
3291
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003292 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003293 rv = -EFAULT;
3294 goto out3;
3295 }
3296
Sam Bradshaw88523a62011-08-30 08:34:26 -06003297 return rv;
3298
3299out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003300 /* Disable interrupts on the HBA. */
3301 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3302 dd->mmio + HOST_CTL);
3303
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003304 /* Release the IRQ. */
3305 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003306 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3307
3308out2:
3309 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003310 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003311
Sam Bradshaw88523a62011-08-30 08:34:26 -06003312out1:
3313 /* Free the memory allocated for the for structure. */
3314 kfree(dd->port);
3315
3316 return rv;
3317}
3318
Jens Axboeffc771b2014-05-09 09:42:02 -06003319static void mtip_standby_drive(struct driver_data *dd)
3320{
3321 if (dd->sr)
3322 return;
3323
3324 /*
3325 * Send standby immediate (E0h) to the drive so that it
3326 * saves its state.
3327 */
3328 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3329 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3330 if (mtip_standby_immediate(dd->port))
3331 dev_warn(&dd->pdev->dev,
3332 "STANDBY IMMEDIATE failed\n");
3333}
3334
Sam Bradshaw88523a62011-08-30 08:34:26 -06003335/*
3336 * Called to deinitialize an interface.
3337 *
3338 * @dd Pointer to the driver data structure.
3339 *
3340 * return value
3341 * 0
3342 */
Jens Axboe63166682011-09-27 21:27:43 -06003343static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003344{
3345 /*
3346 * Send standby immediate (E0h) to the drive so that it
3347 * saves its state.
3348 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003349 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003350 /* de-initialize the port. */
3351 mtip_deinit_port(dd->port);
3352
3353 /* Disable interrupts on the HBA. */
3354 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3355 dd->mmio + HOST_CTL);
3356 }
3357
Sam Bradshaw88523a62011-08-30 08:34:26 -06003358 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003359 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003360 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3361
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003362 /* Free dma regions */
3363 mtip_dma_free(dd);
3364
Sam Bradshaw88523a62011-08-30 08:34:26 -06003365 /* Free the memory allocated for the for structure. */
3366 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003367 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003368
3369 return 0;
3370}
3371
3372/*
3373 * Issue a Standby Immediate command to the device.
3374 *
3375 * This function is called by the Block Layer just before the
3376 * system powers off during a shutdown.
3377 *
3378 * @dd Pointer to the driver data structure.
3379 *
3380 * return value
3381 * 0
3382 */
Jens Axboe63166682011-09-27 21:27:43 -06003383static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003384{
3385 /*
3386 * Send standby immediate (E0h) to the drive so that it
3387 * saves its state.
3388 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003389 if (!dd->sr && dd->port)
3390 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003391
3392 return 0;
3393}
3394
3395/*
3396 * Suspend function
3397 *
3398 * This function is called by the Block Layer just before the
3399 * system hibernates.
3400 *
3401 * @dd Pointer to the driver data structure.
3402 *
3403 * return value
3404 * 0 Suspend was successful
3405 * -EFAULT Suspend was not successful
3406 */
Jens Axboe63166682011-09-27 21:27:43 -06003407static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003408{
3409 /*
3410 * Send standby immediate (E0h) to the drive
3411 * so that it saves its state.
3412 */
3413 if (mtip_standby_immediate(dd->port) != 0) {
3414 dev_err(&dd->pdev->dev,
3415 "Failed standby-immediate command\n");
3416 return -EFAULT;
3417 }
3418
3419 /* Disable interrupts on the HBA.*/
3420 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3421 dd->mmio + HOST_CTL);
3422 mtip_deinit_port(dd->port);
3423
3424 return 0;
3425}
3426
3427/*
3428 * Resume function
3429 *
3430 * This function is called by the Block Layer as the
3431 * system resumes.
3432 *
3433 * @dd Pointer to the driver data structure.
3434 *
3435 * return value
3436 * 0 Resume was successful
3437 * -EFAULT Resume was not successful
3438 */
Jens Axboe63166682011-09-27 21:27:43 -06003439static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003440{
3441 /* Perform any needed hardware setup steps */
3442 hba_setup(dd);
3443
3444 /* Reset the HBA */
3445 if (mtip_hba_reset(dd) != 0) {
3446 dev_err(&dd->pdev->dev,
3447 "Unable to reset the HBA\n");
3448 return -EFAULT;
3449 }
3450
3451 /*
3452 * Enable the port, DMA engine, and FIS reception specific
3453 * h/w in controller.
3454 */
3455 mtip_init_port(dd->port);
3456 mtip_start_port(dd->port);
3457
3458 /* Enable interrupts on the HBA.*/
3459 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3460 dd->mmio + HOST_CTL);
3461
3462 return 0;
3463}
3464
3465/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003466 * Helper function for reusing disk name
3467 * upon hot insertion.
3468 */
3469static int rssd_disk_name_format(char *prefix,
3470 int index,
3471 char *buf,
3472 int buflen)
3473{
3474 const int base = 'z' - 'a' + 1;
3475 char *begin = buf + strlen(prefix);
3476 char *end = buf + buflen;
3477 char *p;
3478 int unit;
3479
3480 p = end - 1;
3481 *p = '\0';
3482 unit = base;
3483 do {
3484 if (p == begin)
3485 return -EINVAL;
3486 *--p = 'a' + (index % unit);
3487 index = (index / unit) - 1;
3488 } while (index >= 0);
3489
3490 memmove(begin, p, end - p);
3491 memcpy(buf, prefix, strlen(prefix));
3492
3493 return 0;
3494}
3495
3496/*
3497 * Block layer IOCTL handler.
3498 *
3499 * @dev Pointer to the block_device structure.
3500 * @mode ignored
3501 * @cmd IOCTL command passed from the user application.
3502 * @arg Argument passed from the user application.
3503 *
3504 * return value
3505 * 0 IOCTL completed successfully.
3506 * -ENOTTY IOCTL not supported or invalid driver data
3507 * structure pointer.
3508 */
3509static int mtip_block_ioctl(struct block_device *dev,
3510 fmode_t mode,
3511 unsigned cmd,
3512 unsigned long arg)
3513{
3514 struct driver_data *dd = dev->bd_disk->private_data;
3515
3516 if (!capable(CAP_SYS_ADMIN))
3517 return -EACCES;
3518
3519 if (!dd)
3520 return -ENOTTY;
3521
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003522 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003523 return -ENOTTY;
3524
Sam Bradshaw88523a62011-08-30 08:34:26 -06003525 switch (cmd) {
3526 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003527 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003528 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003529 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003530 }
3531}
3532
Jens Axboe16d02c02011-09-27 15:50:01 -06003533#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534/*
3535 * Block layer compat IOCTL handler.
3536 *
3537 * @dev Pointer to the block_device structure.
3538 * @mode ignored
3539 * @cmd IOCTL command passed from the user application.
3540 * @arg Argument passed from the user application.
3541 *
3542 * return value
3543 * 0 IOCTL completed successfully.
3544 * -ENOTTY IOCTL not supported or invalid driver data
3545 * structure pointer.
3546 */
3547static int mtip_block_compat_ioctl(struct block_device *dev,
3548 fmode_t mode,
3549 unsigned cmd,
3550 unsigned long arg)
3551{
3552 struct driver_data *dd = dev->bd_disk->private_data;
3553
3554 if (!capable(CAP_SYS_ADMIN))
3555 return -EACCES;
3556
3557 if (!dd)
3558 return -ENOTTY;
3559
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003560 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003561 return -ENOTTY;
3562
Sam Bradshaw88523a62011-08-30 08:34:26 -06003563 switch (cmd) {
3564 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003565 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003566 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003567 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003568 ide_task_request_t req_task;
3569 int compat_tasksize, outtotal, ret;
3570
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003571 compat_tasksize =
3572 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003573
3574 compat_req_task =
3575 (struct mtip_compat_ide_task_request_s __user *) arg;
3576
3577 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003578 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003579 return -EFAULT;
3580
3581 if (get_user(req_task.out_size, &compat_req_task->out_size))
3582 return -EFAULT;
3583
3584 if (get_user(req_task.in_size, &compat_req_task->in_size))
3585 return -EFAULT;
3586
3587 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3588
3589 ret = exec_drive_taskfile(dd, (void __user *) arg,
3590 &req_task, outtotal);
3591
3592 if (copy_to_user((void __user *) arg, &req_task,
3593 compat_tasksize -
3594 (2 * sizeof(compat_long_t))))
3595 return -EFAULT;
3596
3597 if (put_user(req_task.out_size, &compat_req_task->out_size))
3598 return -EFAULT;
3599
3600 if (put_user(req_task.in_size, &compat_req_task->in_size))
3601 return -EFAULT;
3602
3603 return ret;
3604 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003605 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003606 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003607 }
3608}
Jens Axboe16d02c02011-09-27 15:50:01 -06003609#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003610
3611/*
3612 * Obtain the geometry of the device.
3613 *
3614 * You may think that this function is obsolete, but some applications,
3615 * fdisk for example still used CHS values. This function describes the
3616 * device as having 224 heads and 56 sectors per cylinder. These values are
3617 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3618 * partition is described in terms of a start and end cylinder this means
3619 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3620 * affects performance.
3621 *
3622 * @dev Pointer to the block_device strucutre.
3623 * @geo Pointer to a hd_geometry structure.
3624 *
3625 * return value
3626 * 0 Operation completed successfully.
3627 * -ENOTTY An error occurred while reading the drive capacity.
3628 */
3629static int mtip_block_getgeo(struct block_device *dev,
3630 struct hd_geometry *geo)
3631{
3632 struct driver_data *dd = dev->bd_disk->private_data;
3633 sector_t capacity;
3634
3635 if (!dd)
3636 return -ENOTTY;
3637
3638 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3639 dev_warn(&dd->pdev->dev,
3640 "Could not get drive capacity.\n");
3641 return -ENOTTY;
3642 }
3643
3644 geo->heads = 224;
3645 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003646 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003647 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003648 return 0;
3649}
3650
3651/*
3652 * Block device operation function.
3653 *
3654 * This structure contains pointers to the functions required by the block
3655 * layer.
3656 */
3657static const struct block_device_operations mtip_block_ops = {
3658 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003659#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003660 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003661#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003662 .getgeo = mtip_block_getgeo,
3663 .owner = THIS_MODULE
3664};
3665
3666/*
3667 * Block layer make request function.
3668 *
3669 * This function is called by the kernel to process a BIO for
3670 * the P320 device.
3671 *
3672 * @queue Pointer to the request queue. Unused other than to obtain
3673 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003674 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003675 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003676 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003677static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003678{
Jens Axboeffc771b2014-05-09 09:42:02 -06003679 struct driver_data *dd = hctx->queue->queuedata;
3680 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3681 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003682
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003683 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3684 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3685 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003686 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003687 }
3688 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003689 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003690 }
3691 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3692 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003693 rq_data_dir(rq))) {
3694 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003695 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003696 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3697 return -ENODATA;
3698 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3699 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003700 }
3701
Jens Axboeffc771b2014-05-09 09:42:02 -06003702 if (rq->cmd_flags & REQ_DISCARD) {
3703 int err;
3704
3705 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
3706 blk_mq_end_io(rq, err);
3707 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003708 }
3709
Jens Axboeffc771b2014-05-09 09:42:02 -06003710 /* Create the scatter list for this request. */
3711 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003712
Jens Axboeffc771b2014-05-09 09:42:02 -06003713 /* Issue the read/write. */
3714 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3715 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003716}
3717
Jens Axboeffc771b2014-05-09 09:42:02 -06003718static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3719 struct request *rq)
3720{
3721 struct driver_data *dd = hctx->queue->queuedata;
3722 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3723
3724 if (!dd->unal_qdepth || rq_data_dir(rq) == READ)
3725 return false;
3726
3727 /*
3728 * If unaligned depth must be limited on this controller, mark it
3729 * as unaligned if the IO isn't on a 4k boundary (start of length).
3730 */
3731 if (blk_rq_sectors(rq) <= 64) {
3732 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3733 cmd->unaligned = 1;
3734 }
3735
3736 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3737 return true;
3738
3739 return false;
3740}
3741
3742static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
3743{
3744 int ret;
3745
3746 if (mtip_check_unal_depth(hctx, rq))
3747 return BLK_MQ_RQ_QUEUE_BUSY;
3748
3749 ret = mtip_submit_request(hctx, rq);
3750 if (!ret)
3751 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,
3802 .alloc_hctx = blk_mq_alloc_single_hw_queue,
3803 .free_hctx = blk_mq_free_single_hw_queue,
3804 .init_request = mtip_init_cmd,
3805 .exit_request = mtip_free_cmd,
3806};
3807
Sam Bradshaw88523a62011-08-30 08:34:26 -06003808/*
3809 * Block layer initialization function.
3810 *
3811 * This function is called once by the PCI layer for each P320
3812 * device that is connected to the system.
3813 *
3814 * @dd Pointer to the driver data structure.
3815 *
3816 * return value
3817 * 0 on success else an error code.
3818 */
Jens Axboe63166682011-09-27 21:27:43 -06003819static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003820{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003821 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003822 sector_t capacity;
3823 unsigned int index = 0;
3824 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003825 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003826
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003827 if (dd->disk)
3828 goto skip_create_disk; /* hw init done, before rebuild */
3829
Jens Axboeffc771b2014-05-09 09:42:02 -06003830 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003831 rv = -EINVAL;
3832 goto protocol_init_error;
3833 }
3834
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003835 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003836 if (dd->disk == NULL) {
3837 dev_err(&dd->pdev->dev,
3838 "Unable to allocate gendisk structure\n");
3839 rv = -EINVAL;
3840 goto alloc_disk_error;
3841 }
3842
3843 /* Generate the disk name, implemented same as in sd.c */
3844 do {
3845 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3846 goto ida_get_error;
3847
3848 spin_lock(&rssd_index_lock);
3849 rv = ida_get_new(&rssd_index_ida, &index);
3850 spin_unlock(&rssd_index_lock);
3851 } while (rv == -EAGAIN);
3852
3853 if (rv)
3854 goto ida_get_error;
3855
3856 rv = rssd_disk_name_format("rssd",
3857 index,
3858 dd->disk->disk_name,
3859 DISK_NAME_LEN);
3860 if (rv)
3861 goto disk_index_error;
3862
3863 dd->disk->driverfs_dev = &dd->pdev->dev;
3864 dd->disk->major = dd->major;
3865 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3866 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003867 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003868 dd->index = index;
3869
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003870 mtip_hw_debugfs_init(dd);
3871
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003872skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003873 memset(&dd->tags, 0, sizeof(dd->tags));
3874 dd->tags.ops = &mtip_mq_ops;
3875 dd->tags.nr_hw_queues = 1;
3876 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3877 dd->tags.reserved_tags = 1;
3878 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3879 dd->tags.numa_node = dd->numa_node;
3880 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3881 dd->tags.driver_data = dd;
3882
3883 rv = blk_mq_alloc_tag_set(&dd->tags);
3884 if (rv) {
3885 dev_err(&dd->pdev->dev,
3886 "Unable to allocate request queue\n");
3887 rv = -ENOMEM;
3888 goto block_queue_alloc_init_error;
3889 }
3890
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003891 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003892 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003893 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003894 dev_err(&dd->pdev->dev,
3895 "Unable to allocate request queue\n");
3896 rv = -ENOMEM;
3897 goto block_queue_alloc_init_error;
3898 }
3899
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003900 dd->disk->queue = dd->queue;
3901 dd->queue->queuedata = dd;
3902
Jens Axboeffc771b2014-05-09 09:42:02 -06003903 /* Initialize the protocol layer. */
3904 wait_for_rebuild = mtip_hw_get_identify(dd);
3905 if (wait_for_rebuild < 0) {
3906 dev_err(&dd->pdev->dev,
3907 "Protocol layer initialization failed\n");
3908 rv = -EINVAL;
3909 goto init_hw_cmds_error;
3910 }
3911
3912 /*
3913 * if rebuild pending, start the service thread, and delay the block
3914 * queue creation and add_disk()
3915 */
3916 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3917 goto start_service_thread;
3918
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003919 /* Set device limits. */
3920 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3921 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3922 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003923 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3924 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003925 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003926 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003927
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003928 /*
3929 * write back cache is not supported in the device. FUA depends on
3930 * write back cache support, hence setting flush support to zero.
3931 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003932 blk_queue_flush(dd->queue, 0);
3933
Asai Thambi S P15283462013-01-11 14:41:34 +01003934 /* Signal trim support */
3935 if (dd->trim_supp == true) {
3936 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3937 dd->queue->limits.discard_granularity = 4096;
3938 blk_queue_max_discard_sectors(dd->queue,
3939 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3940 dd->queue->limits.discard_zeroes_data = 0;
3941 }
3942
Sam Bradshaw88523a62011-08-30 08:34:26 -06003943 /* Set the capacity of the device in 512 byte sectors. */
3944 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3945 dev_warn(&dd->pdev->dev,
3946 "Could not read drive capacity\n");
3947 rv = -EIO;
3948 goto read_capacity_error;
3949 }
3950 set_capacity(dd->disk, capacity);
3951
3952 /* Enable the block device and add it to /dev */
3953 add_disk(dd->disk);
3954
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003955 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003956 /*
3957 * Now that the disk is active, initialize any sysfs attributes
3958 * managed by the protocol layer.
3959 */
3960 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3961 if (kobj) {
3962 mtip_hw_sysfs_init(dd, kobj);
3963 kobject_put(kobj);
3964 }
3965
Asai Thambi S P45038362012-04-09 08:35:38 +02003966 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003967 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003968 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003969 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003970
3971start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003972 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003973 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07003974 dd, dd->numa_node, "%s",
3975 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003976
3977 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003978 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003979 dd->mtip_svc_handler = NULL;
3980 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003981 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003982 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003983 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003984 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3985 rv = wait_for_rebuild;
3986
Sam Bradshaw88523a62011-08-30 08:34:26 -06003987 return rv;
3988
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003989kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003990 bdput(dd->bdev);
3991 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003992
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003993 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003994 del_gendisk(dd->disk);
3995
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003996read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003997init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003998 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06003999 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004000block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004001 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004002disk_index_error:
4003 spin_lock(&rssd_index_lock);
4004 ida_remove(&rssd_index_ida, index);
4005 spin_unlock(&rssd_index_lock);
4006
4007ida_get_error:
4008 put_disk(dd->disk);
4009
4010alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004011 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004012
4013protocol_init_error:
4014 return rv;
4015}
4016
4017/*
4018 * Block layer deinitialization function.
4019 *
4020 * Called by the PCI layer as each P320 device is removed.
4021 *
4022 * @dd Pointer to the driver data structure.
4023 *
4024 * return value
4025 * 0
4026 */
Jens Axboe63166682011-09-27 21:27:43 -06004027static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004028{
4029 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004030
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004031 if (!dd->sr) {
4032 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004033
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004034 if (dd->mtip_svc_handler) {
4035 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4036 wake_up_interruptible(&dd->port->svc_wait);
4037 kthread_stop(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004038 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004039
4040 /* Clean up the sysfs attributes, if created */
4041 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4042 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4043 if (kobj) {
4044 mtip_hw_sysfs_exit(dd, kobj);
4045 kobject_put(kobj);
4046 }
4047 }
Jens Axboeffc771b2014-05-09 09:42:02 -06004048
4049 mtip_standby_drive(dd);
4050
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004051 /*
4052 * Delete our gendisk structure. This also removes the device
4053 * from /dev
4054 */
4055 if (dd->bdev) {
4056 bdput(dd->bdev);
4057 dd->bdev = NULL;
4058 }
4059 if (dd->disk) {
4060 if (dd->disk->queue) {
4061 del_gendisk(dd->disk);
4062 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004063 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004064 dd->queue = NULL;
4065 } else
4066 put_disk(dd->disk);
4067 }
4068 dd->disk = NULL;
4069
4070 spin_lock(&rssd_index_lock);
4071 ida_remove(&rssd_index_ida, dd->index);
4072 spin_unlock(&rssd_index_lock);
4073 } else {
4074 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4075 dd->disk->disk_name);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004076 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004077
4078 /* De-initialize the protocol layer. */
4079 mtip_hw_exit(dd);
4080
4081 return 0;
4082}
4083
4084/*
4085 * Function called by the PCI layer when just before the
4086 * machine shuts down.
4087 *
4088 * If a protocol layer shutdown function is present it will be called
4089 * by this function.
4090 *
4091 * @dd Pointer to the driver data structure.
4092 *
4093 * return value
4094 * 0
4095 */
Jens Axboe63166682011-09-27 21:27:43 -06004096static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004097{
Jens Axboeffc771b2014-05-09 09:42:02 -06004098 mtip_hw_shutdown(dd);
4099
Sam Bradshaw88523a62011-08-30 08:34:26 -06004100 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304101 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304102 dev_info(&dd->pdev->dev,
4103 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304104
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304105 if (dd->disk->queue) {
4106 del_gendisk(dd->disk);
4107 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004108 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304109 } else
4110 put_disk(dd->disk);
4111 dd->disk = NULL;
4112 dd->queue = NULL;
4113 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004114
4115 spin_lock(&rssd_index_lock);
4116 ida_remove(&rssd_index_ida, dd->index);
4117 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004118 return 0;
4119}
4120
Jens Axboe63166682011-09-27 21:27:43 -06004121static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004122{
4123 dev_info(&dd->pdev->dev,
4124 "Suspending %s ...\n", dd->disk->disk_name);
4125 mtip_hw_suspend(dd);
4126 return 0;
4127}
4128
Jens Axboe63166682011-09-27 21:27:43 -06004129static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004130{
4131 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4132 dd->disk->disk_name);
4133 mtip_hw_resume(dd);
4134 return 0;
4135}
4136
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004137static void drop_cpu(int cpu)
4138{
4139 cpu_use[cpu]--;
4140}
4141
4142static int get_least_used_cpu_on_node(int node)
4143{
4144 int cpu, least_used_cpu, least_cnt;
4145 const struct cpumask *node_mask;
4146
4147 node_mask = cpumask_of_node(node);
4148 least_used_cpu = cpumask_first(node_mask);
4149 least_cnt = cpu_use[least_used_cpu];
4150 cpu = least_used_cpu;
4151
4152 for_each_cpu(cpu, node_mask) {
4153 if (cpu_use[cpu] < least_cnt) {
4154 least_used_cpu = cpu;
4155 least_cnt = cpu_use[cpu];
4156 }
4157 }
4158 cpu_use[least_used_cpu]++;
4159 return least_used_cpu;
4160}
4161
4162/* Helper for selecting a node in round robin mode */
4163static inline int mtip_get_next_rr_node(void)
4164{
4165 static int next_node = -1;
4166
4167 if (next_node == -1) {
4168 next_node = first_online_node;
4169 return next_node;
4170 }
4171
4172 next_node = next_online_node(next_node);
4173 if (next_node == MAX_NUMNODES)
4174 next_node = first_online_node;
4175 return next_node;
4176}
4177
Fengguang Wu25bac122013-01-12 15:31:40 +08004178static DEFINE_HANDLER(0);
4179static DEFINE_HANDLER(1);
4180static DEFINE_HANDLER(2);
4181static DEFINE_HANDLER(3);
4182static DEFINE_HANDLER(4);
4183static DEFINE_HANDLER(5);
4184static DEFINE_HANDLER(6);
4185static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004186
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004187static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4188{
4189 int pos;
4190 unsigned short pcie_dev_ctrl;
4191
4192 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4193 if (pos) {
4194 pci_read_config_word(pdev,
4195 pos + PCI_EXP_DEVCTL,
4196 &pcie_dev_ctrl);
4197 if (pcie_dev_ctrl & (1 << 11) ||
4198 pcie_dev_ctrl & (1 << 4)) {
4199 dev_info(&dd->pdev->dev,
4200 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4201 pdev->vendor, pdev->device);
4202 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4203 PCI_EXP_DEVCTL_RELAX_EN);
4204 pci_write_config_word(pdev,
4205 pos + PCI_EXP_DEVCTL,
4206 pcie_dev_ctrl);
4207 }
4208 }
4209}
4210
4211static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4212{
4213 /*
4214 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4215 * device with device id 0x5aXX
4216 */
4217 if (pdev->bus && pdev->bus->self) {
4218 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4219 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4220 mtip_disable_link_opts(dd, pdev->bus->self);
4221 } else {
4222 /* Check further up the topology */
4223 struct pci_dev *parent_dev = pdev->bus->self;
4224 if (parent_dev->bus &&
4225 parent_dev->bus->parent &&
4226 parent_dev->bus->parent->self &&
4227 parent_dev->bus->parent->self->vendor ==
4228 PCI_VENDOR_ID_ATI &&
4229 (parent_dev->bus->parent->self->device &
4230 0xff00) == 0x5a00) {
4231 mtip_disable_link_opts(dd,
4232 parent_dev->bus->parent->self);
4233 }
4234 }
4235 }
4236}
4237
Sam Bradshaw88523a62011-08-30 08:34:26 -06004238/*
4239 * Called for each supported PCI device detected.
4240 *
4241 * This function allocates the private data structure, enables the
4242 * PCI device and then calls the block layer initialization function.
4243 *
4244 * return value
4245 * 0 on success else an error code.
4246 */
4247static int mtip_pci_probe(struct pci_dev *pdev,
4248 const struct pci_device_id *ent)
4249{
4250 int rv = 0;
4251 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004252 char cpu_list[256];
4253 const struct cpumask *node_mask;
4254 int cpu, i = 0, j = 0;
4255 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304256 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004257
4258 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004259 my_node = pcibus_to_node(pdev->bus);
4260 if (my_node != NUMA_NO_NODE) {
4261 if (!node_online(my_node))
4262 my_node = mtip_get_next_rr_node();
4263 } else {
4264 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4265 my_node = mtip_get_next_rr_node();
4266 }
4267 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4268 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004269 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004270
4271 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004272 if (dd == NULL) {
4273 dev_err(&pdev->dev,
4274 "Unable to allocate memory for driver data\n");
4275 return -ENOMEM;
4276 }
4277
Sam Bradshaw88523a62011-08-30 08:34:26 -06004278 /* Attach the private data to this PCI device. */
4279 pci_set_drvdata(pdev, dd);
4280
4281 rv = pcim_enable_device(pdev);
4282 if (rv < 0) {
4283 dev_err(&pdev->dev, "Unable to enable device\n");
4284 goto iomap_err;
4285 }
4286
4287 /* Map BAR5 to memory. */
4288 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4289 if (rv < 0) {
4290 dev_err(&pdev->dev, "Unable to map regions\n");
4291 goto iomap_err;
4292 }
4293
4294 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4295 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4296
4297 if (rv) {
4298 rv = pci_set_consistent_dma_mask(pdev,
4299 DMA_BIT_MASK(32));
4300 if (rv) {
4301 dev_warn(&pdev->dev,
4302 "64-bit DMA enable failed\n");
4303 goto setmask_err;
4304 }
4305 }
4306 }
4307
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004308 /* Copy the info we may need later into the private data structure. */
4309 dd->major = mtip_major;
4310 dd->instance = instance;
4311 dd->pdev = pdev;
4312 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004313
Asai Thambi S P0caff002013-04-03 19:56:21 +05304314 INIT_LIST_HEAD(&dd->online_list);
4315 INIT_LIST_HEAD(&dd->remove_list);
4316
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004317 memset(dd->workq_name, 0, 32);
4318 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4319
4320 dd->isr_workq = create_workqueue(dd->workq_name);
4321 if (!dd->isr_workq) {
4322 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004323 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004324 goto block_initialize_err;
4325 }
4326
4327 memset(cpu_list, 0, sizeof(cpu_list));
4328
4329 node_mask = cpumask_of_node(dd->numa_node);
4330 if (!cpumask_empty(node_mask)) {
4331 for_each_cpu(cpu, node_mask)
4332 {
4333 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4334 j = strlen(cpu_list);
4335 }
4336
4337 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4338 dd->numa_node,
4339 topology_physical_package_id(cpumask_first(node_mask)),
4340 nr_cpus_node(dd->numa_node),
4341 cpu_list);
4342 } else
4343 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4344
4345 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4346 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4347 cpu_to_node(dd->isr_binding), dd->isr_binding);
4348
4349 /* first worker context always runs in ISR */
4350 dd->work[0].cpu_binding = dd->isr_binding;
4351 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4352 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4353 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4354 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4355 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4356 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4357 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4358
4359 /* Log the bindings */
4360 for_each_present_cpu(cpu) {
4361 memset(cpu_list, 0, sizeof(cpu_list));
4362 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4363 if (dd->work[i].cpu_binding == cpu) {
4364 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4365 j = strlen(cpu_list);
4366 }
4367 }
4368 if (j)
4369 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4370 }
4371
4372 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4373 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4374 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4375 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4376 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4377 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4378 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4379 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4380
4381 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004382 rv = pci_enable_msi(pdev);
4383 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004384 dev_warn(&pdev->dev,
4385 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004386 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004387 }
4388
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004389 mtip_fix_ero_nosnoop(dd, pdev);
4390
Sam Bradshaw88523a62011-08-30 08:34:26 -06004391 /* Initialize the block layer. */
4392 rv = mtip_block_initialize(dd);
4393 if (rv < 0) {
4394 dev_err(&pdev->dev,
4395 "Unable to initialize block layer\n");
4396 goto block_initialize_err;
4397 }
4398
4399 /*
4400 * Increment the instance count so that each device has a unique
4401 * instance number.
4402 */
4403 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004404 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004405 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304406 else
4407 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304408
4409 /* Add to online list even if in ftl rebuild */
4410 spin_lock_irqsave(&dev_lock, flags);
4411 list_add(&dd->online_list, &online_list);
4412 spin_unlock_irqrestore(&dev_lock, flags);
4413
Sam Bradshaw88523a62011-08-30 08:34:26 -06004414 goto done;
4415
4416block_initialize_err:
4417 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004418
4419msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004420 if (dd->isr_workq) {
4421 flush_workqueue(dd->isr_workq);
4422 destroy_workqueue(dd->isr_workq);
4423 drop_cpu(dd->work[0].cpu_binding);
4424 drop_cpu(dd->work[1].cpu_binding);
4425 drop_cpu(dd->work[2].cpu_binding);
4426 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004427setmask_err:
4428 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4429
4430iomap_err:
4431 kfree(dd);
4432 pci_set_drvdata(pdev, NULL);
4433 return rv;
4434done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004435 return rv;
4436}
4437
4438/*
4439 * Called for each probed device when the device is removed or the
4440 * driver is unloaded.
4441 *
4442 * return value
4443 * None
4444 */
4445static void mtip_pci_remove(struct pci_dev *pdev)
4446{
4447 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004448 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004449
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004450 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004451
Asai Thambi S P0caff002013-04-03 19:56:21 +05304452 spin_lock_irqsave(&dev_lock, flags);
4453 list_del_init(&dd->online_list);
4454 list_add(&dd->remove_list, &removing_list);
4455 spin_unlock_irqrestore(&dev_lock, flags);
4456
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004457 mtip_check_surprise_removal(pdev);
4458 synchronize_irq(dd->pdev->irq);
4459
4460 /* Spin until workers are done */
4461 to = jiffies + msecs_to_jiffies(4000);
4462 do {
4463 msleep(20);
4464 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4465 time_before(jiffies, to));
4466
4467 if (atomic_read(&dd->irq_workers_active) != 0) {
4468 dev_warn(&dd->pdev->dev,
4469 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004470 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004471
4472 /* Clean up the block layer. */
4473 mtip_block_remove(dd);
4474
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004475 if (dd->isr_workq) {
4476 flush_workqueue(dd->isr_workq);
4477 destroy_workqueue(dd->isr_workq);
4478 drop_cpu(dd->work[0].cpu_binding);
4479 drop_cpu(dd->work[1].cpu_binding);
4480 drop_cpu(dd->work[2].cpu_binding);
4481 }
4482
Sam Bradshaw88523a62011-08-30 08:34:26 -06004483 pci_disable_msi(pdev);
4484
Asai Thambi S P0caff002013-04-03 19:56:21 +05304485 spin_lock_irqsave(&dev_lock, flags);
4486 list_del_init(&dd->remove_list);
4487 spin_unlock_irqrestore(&dev_lock, flags);
4488
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004489 if (!dd->sr)
4490 kfree(dd);
4491 else
4492 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4493
Sam Bradshaw88523a62011-08-30 08:34:26 -06004494 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004495 pci_set_drvdata(pdev, NULL);
4496 pci_dev_put(pdev);
4497
Sam Bradshaw88523a62011-08-30 08:34:26 -06004498}
4499
4500/*
4501 * Called for each probed device when the device is suspended.
4502 *
4503 * return value
4504 * 0 Success
4505 * <0 Error
4506 */
4507static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4508{
4509 int rv = 0;
4510 struct driver_data *dd = pci_get_drvdata(pdev);
4511
4512 if (!dd) {
4513 dev_err(&pdev->dev,
4514 "Driver private datastructure is NULL\n");
4515 return -EFAULT;
4516 }
4517
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004518 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004519
4520 /* Disable ports & interrupts then send standby immediate */
4521 rv = mtip_block_suspend(dd);
4522 if (rv < 0) {
4523 dev_err(&pdev->dev,
4524 "Failed to suspend controller\n");
4525 return rv;
4526 }
4527
4528 /*
4529 * Save the pci config space to pdev structure &
4530 * disable the device
4531 */
4532 pci_save_state(pdev);
4533 pci_disable_device(pdev);
4534
4535 /* Move to Low power state*/
4536 pci_set_power_state(pdev, PCI_D3hot);
4537
4538 return rv;
4539}
4540
4541/*
4542 * Called for each probed device when the device is resumed.
4543 *
4544 * return value
4545 * 0 Success
4546 * <0 Error
4547 */
4548static int mtip_pci_resume(struct pci_dev *pdev)
4549{
4550 int rv = 0;
4551 struct driver_data *dd;
4552
4553 dd = pci_get_drvdata(pdev);
4554 if (!dd) {
4555 dev_err(&pdev->dev,
4556 "Driver private datastructure is NULL\n");
4557 return -EFAULT;
4558 }
4559
4560 /* Move the device to active State */
4561 pci_set_power_state(pdev, PCI_D0);
4562
4563 /* Restore PCI configuration space */
4564 pci_restore_state(pdev);
4565
4566 /* Enable the PCI device*/
4567 rv = pcim_enable_device(pdev);
4568 if (rv < 0) {
4569 dev_err(&pdev->dev,
4570 "Failed to enable card during resume\n");
4571 goto err;
4572 }
4573 pci_set_master(pdev);
4574
4575 /*
4576 * Calls hbaReset, initPort, & startPort function
4577 * then enables interrupts
4578 */
4579 rv = mtip_block_resume(dd);
4580 if (rv < 0)
4581 dev_err(&pdev->dev, "Unable to resume\n");
4582
4583err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004584 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004585
4586 return rv;
4587}
4588
4589/*
4590 * Shutdown routine
4591 *
4592 * return value
4593 * None
4594 */
4595static void mtip_pci_shutdown(struct pci_dev *pdev)
4596{
4597 struct driver_data *dd = pci_get_drvdata(pdev);
4598 if (dd)
4599 mtip_block_shutdown(dd);
4600}
4601
Sam Bradshaw88523a62011-08-30 08:34:26 -06004602/* Table of device ids supported by this driver. */
4603static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304604 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4605 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4606 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4607 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4608 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4609 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4610 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004611 { 0 }
4612};
4613
4614/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004615static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004616 .name = MTIP_DRV_NAME,
4617 .id_table = mtip_pci_tbl,
4618 .probe = mtip_pci_probe,
4619 .remove = mtip_pci_remove,
4620 .suspend = mtip_pci_suspend,
4621 .resume = mtip_pci_resume,
4622 .shutdown = mtip_pci_shutdown,
4623};
4624
4625MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4626
4627/*
4628 * Module initialization function.
4629 *
4630 * Called once when the module is loaded. This function allocates a major
4631 * block device number to the Cyclone devices and registers the PCI layer
4632 * of the driver.
4633 *
4634 * Return value
4635 * 0 on success else error code.
4636 */
4637static int __init mtip_init(void)
4638{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004639 int error;
4640
Asai Thambi S P45422e72012-09-05 22:03:56 +05304641 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004642
Asai Thambi S P0caff002013-04-03 19:56:21 +05304643 spin_lock_init(&dev_lock);
4644
4645 INIT_LIST_HEAD(&online_list);
4646 INIT_LIST_HEAD(&removing_list);
4647
Sam Bradshaw88523a62011-08-30 08:34:26 -06004648 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004649 error = register_blkdev(0, MTIP_DRV_NAME);
4650 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304651 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004652 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004653 return -EBUSY;
4654 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004655 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004656
Asai Thambi S P0caff002013-04-03 19:56:21 +05304657 dfs_parent = debugfs_create_dir("rssd", NULL);
4658 if (IS_ERR_OR_NULL(dfs_parent)) {
4659 pr_warn("Error creating debugfs parent\n");
4660 dfs_parent = NULL;
4661 }
4662 if (dfs_parent) {
4663 dfs_device_status = debugfs_create_file("device_status",
4664 S_IRUGO, dfs_parent, NULL,
4665 &mtip_device_status_fops);
4666 if (IS_ERR_OR_NULL(dfs_device_status)) {
4667 pr_err("Error creating device_status node\n");
4668 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004669 }
4670 }
4671
Sam Bradshaw88523a62011-08-30 08:34:26 -06004672 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004673 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004674 if (error) {
4675 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004676 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004677 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004678
4679 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004680}
4681
4682/*
4683 * Module de-initialization function.
4684 *
4685 * Called once when the module is unloaded. This function deallocates
4686 * the major block device number allocated by mtip_init() and
4687 * unregisters the PCI layer of the driver.
4688 *
4689 * Return value
4690 * none
4691 */
4692static void __exit mtip_exit(void)
4693{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004694 /* Release the allocated major block device number. */
4695 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4696
4697 /* Unregister the PCI driver. */
4698 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004699
4700 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004701}
4702
4703MODULE_AUTHOR("Micron Technology, Inc");
4704MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4705MODULE_LICENSE("GPL");
4706MODULE_VERSION(MTIP_DRV_VERSION);
4707
4708module_init(mtip_init);
4709module_exit(mtip_exit);