blob: 4efc676aa5f456c91fdba5345c9777b6a7451d68 [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
Jens Axboe9acf03c2014-05-14 08:22:56 -06001034 blk_mq_stop_hw_queues(port->dd->queue);
1035
Sam Bradshaw88523a62011-08-30 08:34:26 -06001036 to = jiffies + msecs_to_jiffies(timeout);
1037 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001038 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1039 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001040 msleep(20);
1041 continue; /* svc thd is actively issuing commands */
1042 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001043 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001044 goto err_fault;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001045 /*
1046 * Ignore s_active bit 0 of array element 0.
1047 * This bit will always be set
1048 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001049 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050 for (n = 1; n < port->dd->slot_groups; n++)
1051 active |= readl(port->s_active[n]);
1052
1053 if (!active)
1054 break;
1055
1056 msleep(20);
1057 } while (time_before(jiffies, to));
1058
Jens Axboe9acf03c2014-05-14 08:22:56 -06001059 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001060 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001061err_fault:
1062 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1063 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001064}
1065
1066/*
1067 * Execute an internal command and wait for the completion.
1068 *
1069 * @port Pointer to the port data structure.
1070 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001071 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001072 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001073 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001074 * @opts Command header options, excluding the FIS length
1075 * and the number of PRD entries.
1076 * @timeout Time in ms to wait for the command to complete.
1077 *
1078 * return value
1079 * 0 Command completed successfully.
1080 * -EFAULT The buffer address is not correctly aligned.
1081 * -EBUSY Internal command or other IO in progress.
1082 * -EAGAIN Time out waiting for command to complete.
1083 */
1084static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001085 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001086 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001087 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001088 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001089 u32 opts,
1090 gfp_t atomic,
1091 unsigned long timeout)
1092{
1093 struct mtip_cmd_sg *command_sg;
1094 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001095 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301096 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001097 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001098
1099 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1100 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301101 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001102 return -EFAULT;
1103 }
1104
Jens Axboeffc771b2014-05-09 09:42:02 -06001105 int_cmd = mtip_get_int_command(dd);
1106
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001107 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001108 port->ic_pause_timer = 0;
1109
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301110 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1111 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001112
1113 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001114 if (fis->command != ATA_CMD_STANDBYNOW1) {
1115 /* wait for io to complete if non atomic */
1116 if (mtip_quiesce_io(port, 5000) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301117 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001118 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001119 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001120 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001121 wake_up_interruptible(&port->svc_wait);
1122 return -EBUSY;
1123 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001124 }
1125
1126 /* Set the completion function and data for the command. */
1127 int_cmd->comp_data = &wait;
1128 int_cmd->comp_func = mtip_completion;
1129
1130 } else {
1131 /* Clear completion - we're going to poll */
1132 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001133 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001134 }
1135
1136 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001137 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001138
1139 /* Populate the SG list */
1140 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001141 __force_bit2int cpu_to_le32(opts | fis_len);
1142 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001143 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1144
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001145 command_sg->info =
1146 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1147 command_sg->dba =
1148 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1149 command_sg->dba_upper =
1150 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001151
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001152 int_cmd->command_header->opts |=
1153 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001154 }
1155
1156 /* Populate the command header */
1157 int_cmd->command_header->byte_count = 0;
1158
1159 /* Issue the command to the hardware */
1160 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1161
Sam Bradshaw88523a62011-08-30 08:34:26 -06001162 if (atomic == GFP_KERNEL) {
1163 /* Wait for the command to complete or timeout. */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301164 if (wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001165 &wait,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301166 msecs_to_jiffies(timeout)) <= 0) {
1167 if (rv == -ERESTARTSYS) { /* interrupted */
1168 dev_err(&dd->pdev->dev,
1169 "Internal command [%02X] was interrupted after %lu ms\n",
1170 fis->command, timeout);
1171 rv = -EINTR;
1172 goto exec_ic_exit;
1173 } else if (rv == 0) /* timeout */
1174 dev_err(&dd->pdev->dev,
1175 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1176 fis->command, timeout);
1177 else
1178 dev_err(&dd->pdev->dev,
1179 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1180 fis->command, rv, timeout);
1181
1182 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001183 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301184 &dd->dd_flag)) {
1185 dev_err(&dd->pdev->dev,
1186 "Internal command [%02X] wait returned due to SR\n",
1187 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001188 rv = -ENXIO;
1189 goto exec_ic_exit;
1190 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301191 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001192 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301193 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001194 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001195 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301196 u32 hba_stat, port_stat;
1197
Sam Bradshaw88523a62011-08-30 08:34:26 -06001198 /* Spin for <timeout> checking if command still outstanding */
1199 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001200 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1201 & (1 << MTIP_TAG_INTERNAL))
1202 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301203 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001204 rv = -ENXIO;
1205 goto exec_ic_exit;
1206 }
1207 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001208 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301209 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001210 rv = -ENXIO;
1211 goto exec_ic_exit;
1212 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301213 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1214 if (!port_stat)
1215 continue;
1216
1217 if (port_stat & PORT_IRQ_ERR) {
1218 dev_err(&dd->pdev->dev,
1219 "Internal command [%02X] failed\n",
1220 fis->command);
1221 mtip_device_reset(dd);
1222 rv = -EIO;
1223 goto exec_ic_exit;
1224 } else {
1225 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1226 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1227 if (hba_stat)
1228 writel(hba_stat,
1229 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001230 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301231 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001232 }
1233 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001234
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001235 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1236 & (1 << MTIP_TAG_INTERNAL)) {
1237 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301238 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1239 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001240 rv = -EAGAIN;
1241 }
1242 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001243exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001244 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001245 mtip_put_int_command(dd, int_cmd);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001246 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1247 /* NCQ paused */
1248 return rv;
1249 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001250 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001251 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001252
1253 return rv;
1254}
1255
1256/*
1257 * Byte-swap ATA ID strings.
1258 *
1259 * ATA identify data contains strings in byte-swapped 16-bit words.
1260 * They must be swapped (on all architectures) to be usable as C strings.
1261 * This function swaps bytes in-place.
1262 *
1263 * @buf The buffer location of the string
1264 * @len The number of bytes to swap
1265 *
1266 * return value
1267 * None
1268 */
1269static inline void ata_swap_string(u16 *buf, unsigned int len)
1270{
1271 int i;
1272 for (i = 0; i < (len/2); i++)
1273 be16_to_cpus(&buf[i]);
1274}
1275
Asai Thambi S P670a6412014-04-16 20:27:54 -07001276static void mtip_set_timeout(struct driver_data *dd,
1277 struct host_to_dev_fis *fis,
1278 unsigned int *timeout, u8 erasemode)
1279{
1280 switch (fis->command) {
1281 case ATA_CMD_DOWNLOAD_MICRO:
1282 *timeout = 120000; /* 2 minutes */
1283 break;
1284 case ATA_CMD_SEC_ERASE_UNIT:
1285 case 0xFC:
1286 if (erasemode)
1287 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1288 else
1289 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1290 break;
1291 case ATA_CMD_STANDBYNOW1:
1292 *timeout = 120000; /* 2 minutes */
1293 break;
1294 case 0xF7:
1295 case 0xFA:
1296 *timeout = 60000; /* 60 seconds */
1297 break;
1298 case ATA_CMD_SMART:
1299 *timeout = 15000; /* 15 seconds */
1300 break;
1301 default:
1302 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1303 break;
1304 }
1305}
1306
Sam Bradshaw88523a62011-08-30 08:34:26 -06001307/*
1308 * Request the device identity information.
1309 *
1310 * If a user space buffer is not specified, i.e. is NULL, the
1311 * identify information is still read from the drive and placed
1312 * into the identify data buffer (@e port->identify) in the
1313 * port data structure.
1314 * When the identify buffer contains valid identify information @e
1315 * port->identify_valid is non-zero.
1316 *
1317 * @port Pointer to the port structure.
1318 * @user_buffer A user space buffer where the identify data should be
1319 * copied.
1320 *
1321 * return value
1322 * 0 Command completed successfully.
1323 * -EFAULT An error occurred while coping data to the user buffer.
1324 * -1 Command failed.
1325 */
1326static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1327{
1328 int rv = 0;
1329 struct host_to_dev_fis fis;
1330
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001331 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001332 return -EFAULT;
1333
Sam Bradshaw88523a62011-08-30 08:34:26 -06001334 /* Build the FIS. */
1335 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1336 fis.type = 0x27;
1337 fis.opts = 1 << 7;
1338 fis.command = ATA_CMD_ID_ATA;
1339
1340 /* Set the identify information as invalid. */
1341 port->identify_valid = 0;
1342
1343 /* Clear the identify information. */
1344 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1345
1346 /* Execute the command. */
1347 if (mtip_exec_internal_command(port,
1348 &fis,
1349 5,
1350 port->identify_dma,
1351 sizeof(u16) * ATA_ID_WORDS,
1352 0,
1353 GFP_KERNEL,
1354 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1355 < 0) {
1356 rv = -1;
1357 goto out;
1358 }
1359
1360 /*
1361 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1362 * perform field-sensitive swapping on the string fields.
1363 * See the kernel use of ata_id_string() for proof of this.
1364 */
1365#ifdef __LITTLE_ENDIAN
1366 ata_swap_string(port->identify + 27, 40); /* model string*/
1367 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1368 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1369#else
1370 {
1371 int i;
1372 for (i = 0; i < ATA_ID_WORDS; i++)
1373 port->identify[i] = le16_to_cpu(port->identify[i]);
1374 }
1375#endif
1376
Sam Bradshaw26d58052013-10-03 10:18:05 -07001377 /* Check security locked state */
1378 if (port->identify[128] & 0x4)
1379 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1380 else
1381 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1382
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301383#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001384 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1385 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1386 port->dd->trim_supp = true;
1387 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301388#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001389 port->dd->trim_supp = false;
1390
Sam Bradshaw88523a62011-08-30 08:34:26 -06001391 /* Set the identify buffer as valid. */
1392 port->identify_valid = 1;
1393
1394 if (user_buffer) {
1395 if (copy_to_user(
1396 user_buffer,
1397 port->identify,
1398 ATA_ID_WORDS * sizeof(u16))) {
1399 rv = -EFAULT;
1400 goto out;
1401 }
1402 }
1403
1404out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001405 return rv;
1406}
1407
1408/*
1409 * Issue a standby immediate command to the device.
1410 *
1411 * @port Pointer to the port structure.
1412 *
1413 * return value
1414 * 0 Command was executed successfully.
1415 * -1 An error occurred while executing the command.
1416 */
1417static int mtip_standby_immediate(struct mtip_port *port)
1418{
1419 int rv;
1420 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001421 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001422 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001423
Sam Bradshaw88523a62011-08-30 08:34:26 -06001424 /* Build the FIS. */
1425 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1426 fis.type = 0x27;
1427 fis.opts = 1 << 7;
1428 fis.command = ATA_CMD_STANDBYNOW1;
1429
Asai Thambi S P670a6412014-04-16 20:27:54 -07001430 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1431
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001432 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001433 rv = mtip_exec_internal_command(port,
1434 &fis,
1435 5,
1436 0,
1437 0,
1438 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001439 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001440 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001441 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1442 jiffies_to_msecs(jiffies - start));
1443 if (rv)
1444 dev_warn(&port->dd->pdev->dev,
1445 "STANDBY IMMEDIATE command failed.\n");
1446
1447 return rv;
1448}
1449
1450/*
1451 * Issue a READ LOG EXT command to the device.
1452 *
1453 * @port pointer to the port structure.
1454 * @page page number to fetch
1455 * @buffer pointer to buffer
1456 * @buffer_dma dma address corresponding to @buffer
1457 * @sectors page length to fetch, in sectors
1458 *
1459 * return value
1460 * @rv return value from mtip_exec_internal_command()
1461 */
1462static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1463 dma_addr_t buffer_dma, unsigned int sectors)
1464{
1465 struct host_to_dev_fis fis;
1466
1467 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1468 fis.type = 0x27;
1469 fis.opts = 1 << 7;
1470 fis.command = ATA_CMD_READ_LOG_EXT;
1471 fis.sect_count = sectors & 0xFF;
1472 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1473 fis.lba_low = page;
1474 fis.lba_mid = 0;
1475 fis.device = ATA_DEVICE_OBS;
1476
1477 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1478
1479 return mtip_exec_internal_command(port,
1480 &fis,
1481 5,
1482 buffer_dma,
1483 sectors * ATA_SECT_SIZE,
1484 0,
1485 GFP_ATOMIC,
1486 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1487}
1488
1489/*
1490 * Issue a SMART READ DATA command to the device.
1491 *
1492 * @port pointer to the port structure.
1493 * @buffer pointer to buffer
1494 * @buffer_dma dma address corresponding to @buffer
1495 *
1496 * return value
1497 * @rv return value from mtip_exec_internal_command()
1498 */
1499static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1500 dma_addr_t buffer_dma)
1501{
1502 struct host_to_dev_fis fis;
1503
1504 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1505 fis.type = 0x27;
1506 fis.opts = 1 << 7;
1507 fis.command = ATA_CMD_SMART;
1508 fis.features = 0xD0;
1509 fis.sect_count = 1;
1510 fis.lba_mid = 0x4F;
1511 fis.lba_hi = 0xC2;
1512 fis.device = ATA_DEVICE_OBS;
1513
1514 return mtip_exec_internal_command(port,
1515 &fis,
1516 5,
1517 buffer_dma,
1518 ATA_SECT_SIZE,
1519 0,
1520 GFP_ATOMIC,
1521 15000);
1522}
1523
1524/*
1525 * Get the value of a smart attribute
1526 *
1527 * @port pointer to the port structure
1528 * @id attribute number
1529 * @attrib pointer to return attrib information corresponding to @id
1530 *
1531 * return value
1532 * -EINVAL NULL buffer passed or unsupported attribute @id.
1533 * -EPERM Identify data not valid, SMART not supported or not enabled
1534 */
1535static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1536 struct smart_attr *attrib)
1537{
1538 int rv, i;
1539 struct smart_attr *pattr;
1540
1541 if (!attrib)
1542 return -EINVAL;
1543
1544 if (!port->identify_valid) {
1545 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1546 return -EPERM;
1547 }
1548 if (!(port->identify[82] & 0x1)) {
1549 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1550 return -EPERM;
1551 }
1552 if (!(port->identify[85] & 0x1)) {
1553 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1554 return -EPERM;
1555 }
1556
1557 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1558 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1559 if (rv) {
1560 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1561 return rv;
1562 }
1563
1564 pattr = (struct smart_attr *)(port->smart_buf + 2);
1565 for (i = 0; i < 29; i++, pattr++)
1566 if (pattr->attr_id == id) {
1567 memcpy(attrib, pattr, sizeof(struct smart_attr));
1568 break;
1569 }
1570
1571 if (i == 29) {
1572 dev_warn(&port->dd->pdev->dev,
1573 "Query for invalid SMART attribute ID\n");
1574 rv = -EINVAL;
1575 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001576
Sam Bradshaw88523a62011-08-30 08:34:26 -06001577 return rv;
1578}
1579
1580/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001581 * Trim unused sectors
1582 *
1583 * @dd pointer to driver_data structure
1584 * @lba starting lba
1585 * @len # of 512b sectors to trim
1586 *
1587 * return value
1588 * -ENOMEM Out of dma memory
1589 * -EINVAL Invalid parameters passed in, trim not supported
1590 * -EIO Error submitting trim request to hw
1591 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301592static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1593 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001594{
1595 int i, rv = 0;
1596 u64 tlba, tlen, sect_left;
1597 struct mtip_trim_entry *buf;
1598 dma_addr_t dma_addr;
1599 struct host_to_dev_fis fis;
1600
1601 if (!len || dd->trim_supp == false)
1602 return -EINVAL;
1603
1604 /* Trim request too big */
1605 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1606
1607 /* Trim request not aligned on 4k boundary */
1608 WARN_ON(len % 8 != 0);
1609
1610 /* Warn if vu_trim structure is too big */
1611 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1612
1613 /* Allocate a DMA buffer for the trim structure */
1614 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1615 GFP_KERNEL);
1616 if (!buf)
1617 return -ENOMEM;
1618 memset(buf, 0, ATA_SECT_SIZE);
1619
1620 for (i = 0, sect_left = len, tlba = lba;
1621 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1622 i++) {
1623 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1624 MTIP_MAX_TRIM_ENTRY_LEN :
1625 sect_left);
1626 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1627 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1628 tlba += tlen;
1629 sect_left -= tlen;
1630 }
1631 WARN_ON(sect_left != 0);
1632
1633 /* Build the fis */
1634 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1635 fis.type = 0x27;
1636 fis.opts = 1 << 7;
1637 fis.command = 0xfb;
1638 fis.features = 0x60;
1639 fis.sect_count = 1;
1640 fis.device = ATA_DEVICE_OBS;
1641
1642 if (mtip_exec_internal_command(dd->port,
1643 &fis,
1644 5,
1645 dma_addr,
1646 ATA_SECT_SIZE,
1647 0,
1648 GFP_KERNEL,
1649 MTIP_TRIM_TIMEOUT_MS) < 0)
1650 rv = -EIO;
1651
1652 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1653 return rv;
1654}
1655
1656/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001657 * Get the drive capacity.
1658 *
1659 * @dd Pointer to the device data structure.
1660 * @sectors Pointer to the variable that will receive the sector count.
1661 *
1662 * return value
1663 * 1 Capacity was returned successfully.
1664 * 0 The identify information is invalid.
1665 */
Jens Axboe63166682011-09-27 21:27:43 -06001666static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001667{
1668 struct mtip_port *port = dd->port;
1669 u64 total, raw0, raw1, raw2, raw3;
1670 raw0 = port->identify[100];
1671 raw1 = port->identify[101];
1672 raw2 = port->identify[102];
1673 raw3 = port->identify[103];
1674 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1675 *sectors = total;
1676 return (bool) !!port->identify_valid;
1677}
1678
1679/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001680 * Display the identify command data.
1681 *
1682 * @port Pointer to the port data structure.
1683 *
1684 * return value
1685 * None
1686 */
1687static void mtip_dump_identify(struct mtip_port *port)
1688{
1689 sector_t sectors;
1690 unsigned short revid;
1691 char cbuf[42];
1692
1693 if (!port->identify_valid)
1694 return;
1695
1696 strlcpy(cbuf, (char *)(port->identify+10), 21);
1697 dev_info(&port->dd->pdev->dev,
1698 "Serial No.: %s\n", cbuf);
1699
1700 strlcpy(cbuf, (char *)(port->identify+23), 9);
1701 dev_info(&port->dd->pdev->dev,
1702 "Firmware Ver.: %s\n", cbuf);
1703
1704 strlcpy(cbuf, (char *)(port->identify+27), 41);
1705 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1706
Sam Bradshaw26d58052013-10-03 10:18:05 -07001707 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1708 port->identify[128],
1709 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1710
Sam Bradshaw88523a62011-08-30 08:34:26 -06001711 if (mtip_hw_get_capacity(port->dd, &sectors))
1712 dev_info(&port->dd->pdev->dev,
1713 "Capacity: %llu sectors (%llu MB)\n",
1714 (u64)sectors,
1715 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1716
1717 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001718 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001719 case 0x1:
1720 strlcpy(cbuf, "A0", 3);
1721 break;
1722 case 0x3:
1723 strlcpy(cbuf, "A2", 3);
1724 break;
1725 default:
1726 strlcpy(cbuf, "?", 2);
1727 break;
1728 }
1729 dev_info(&port->dd->pdev->dev,
1730 "Card Type: %s\n", cbuf);
1731}
1732
1733/*
1734 * Map the commands scatter list into the command table.
1735 *
1736 * @command Pointer to the command.
1737 * @nents Number of scatter list entries.
1738 *
1739 * return value
1740 * None
1741 */
1742static inline void fill_command_sg(struct driver_data *dd,
1743 struct mtip_cmd *command,
1744 int nents)
1745{
1746 int n;
1747 unsigned int dma_len;
1748 struct mtip_cmd_sg *command_sg;
1749 struct scatterlist *sg = command->sg;
1750
1751 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1752
1753 for (n = 0; n < nents; n++) {
1754 dma_len = sg_dma_len(sg);
1755 if (dma_len > 0x400000)
1756 dev_err(&dd->pdev->dev,
1757 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001758 command_sg->info = __force_bit2int
1759 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1760 command_sg->dba = __force_bit2int
1761 cpu_to_le32(sg_dma_address(sg));
1762 command_sg->dba_upper = __force_bit2int
1763 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001764 command_sg++;
1765 sg++;
1766 }
1767}
1768
1769/*
1770 * @brief Execute a drive command.
1771 *
1772 * return value 0 The command completed successfully.
1773 * return value -1 An error occurred while executing the command.
1774 */
Jens Axboe63166682011-09-27 21:27:43 -06001775static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001776{
1777 struct host_to_dev_fis fis;
1778 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1779
Sam Bradshaw88523a62011-08-30 08:34:26 -06001780 /* Build the FIS. */
1781 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1782 fis.type = 0x27;
1783 fis.opts = 1 << 7;
1784 fis.command = command[0];
1785 fis.features = command[1];
1786 fis.sect_count = command[2];
1787 fis.sector = command[3];
1788 fis.cyl_low = command[4];
1789 fis.cyl_hi = command[5];
1790 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1791
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001792 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 -06001793 __func__,
1794 command[0],
1795 command[1],
1796 command[2],
1797 command[3],
1798 command[4],
1799 command[5],
1800 command[6]);
1801
1802 /* Execute the command. */
1803 if (mtip_exec_internal_command(port,
1804 &fis,
1805 5,
1806 0,
1807 0,
1808 0,
1809 GFP_KERNEL,
1810 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001811 return -1;
1812 }
1813
1814 command[0] = reply->command; /* Status*/
1815 command[1] = reply->features; /* Error*/
1816 command[4] = reply->cyl_low;
1817 command[5] = reply->cyl_hi;
1818
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001819 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 -06001820 __func__,
1821 command[0],
1822 command[1],
1823 command[4],
1824 command[5]);
1825
Sam Bradshaw88523a62011-08-30 08:34:26 -06001826 return 0;
1827}
1828
1829/*
1830 * @brief Execute a drive command.
1831 *
1832 * @param port Pointer to the port data structure.
1833 * @param command Pointer to the user specified command parameters.
1834 * @param user_buffer Pointer to the user space buffer where read sector
1835 * data should be copied.
1836 *
1837 * return value 0 The command completed successfully.
1838 * return value -EFAULT An error occurred while copying the completion
1839 * data to the user space buffer.
1840 * return value -1 An error occurred while executing the command.
1841 */
Jens Axboe63166682011-09-27 21:27:43 -06001842static int exec_drive_command(struct mtip_port *port, u8 *command,
1843 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001844{
1845 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001846 struct host_to_dev_fis *reply;
1847 u8 *buf = NULL;
1848 dma_addr_t dma_addr = 0;
1849 int rv = 0, xfer_sz = command[3];
1850
1851 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001852 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001853 return -EFAULT;
1854
1855 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1856 ATA_SECT_SIZE * xfer_sz,
1857 &dma_addr,
1858 GFP_KERNEL);
1859 if (!buf) {
1860 dev_err(&port->dd->pdev->dev,
1861 "Memory allocation failed (%d bytes)\n",
1862 ATA_SECT_SIZE * xfer_sz);
1863 return -ENOMEM;
1864 }
1865 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1866 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001867
Sam Bradshaw88523a62011-08-30 08:34:26 -06001868 /* Build the FIS. */
1869 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001870 fis.type = 0x27;
1871 fis.opts = 1 << 7;
1872 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001873 fis.features = command[2];
1874 fis.sect_count = command[3];
1875 if (fis.command == ATA_CMD_SMART) {
1876 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001877 fis.cyl_low = 0x4F;
1878 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001879 }
1880
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001881 if (xfer_sz)
1882 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1883 else
1884 reply = (port->rxfis + RX_FIS_D2H_REG);
1885
Sam Bradshaw88523a62011-08-30 08:34:26 -06001886 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001887 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001888 "feat %x, sectcnt %x\n",
1889 __func__,
1890 command[0],
1891 command[1],
1892 command[2],
1893 command[3]);
1894
Sam Bradshaw88523a62011-08-30 08:34:26 -06001895 /* Execute the command. */
1896 if (mtip_exec_internal_command(port,
1897 &fis,
1898 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001899 (xfer_sz ? dma_addr : 0),
1900 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001901 0,
1902 GFP_KERNEL,
1903 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1904 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001905 rv = -EFAULT;
1906 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001907 }
1908
1909 /* Collect the completion status. */
1910 command[0] = reply->command; /* Status*/
1911 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001912 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001913
1914 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001915 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001916 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001917 __func__,
1918 command[0],
1919 command[1],
1920 command[2]);
1921
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001922 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001923 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001924 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001925 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001926 rv = -EFAULT;
1927 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928 }
1929 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001930exit_drive_command:
1931 if (buf)
1932 dmam_free_coherent(&port->dd->pdev->dev,
1933 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1934 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001935}
1936
1937/*
1938 * Indicates whether a command has a single sector payload.
1939 *
1940 * @command passed to the device to perform the certain event.
1941 * @features passed to the device to perform the certain event.
1942 *
1943 * return value
1944 * 1 command is one that always has a single sector payload,
1945 * regardless of the value in the Sector Count field.
1946 * 0 otherwise
1947 *
1948 */
1949static unsigned int implicit_sector(unsigned char command,
1950 unsigned char features)
1951{
1952 unsigned int rv = 0;
1953
1954 /* list of commands that have an implicit sector count of 1 */
1955 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001956 case ATA_CMD_SEC_SET_PASS:
1957 case ATA_CMD_SEC_UNLOCK:
1958 case ATA_CMD_SEC_ERASE_PREP:
1959 case ATA_CMD_SEC_ERASE_UNIT:
1960 case ATA_CMD_SEC_FREEZE_LOCK:
1961 case ATA_CMD_SEC_DISABLE_PASS:
1962 case ATA_CMD_PMP_READ:
1963 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001964 rv = 1;
1965 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001966 case ATA_CMD_SET_MAX:
1967 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001968 rv = 1;
1969 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001970 case ATA_CMD_SMART:
1971 if ((features == ATA_SMART_READ_VALUES) ||
1972 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001973 rv = 1;
1974 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001975 case ATA_CMD_CONF_OVERLAY:
1976 if ((features == ATA_DCO_IDENTIFY) ||
1977 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978 rv = 1;
1979 break;
1980 }
1981 return rv;
1982}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001983
Sam Bradshaw88523a62011-08-30 08:34:26 -06001984/*
1985 * Executes a taskfile
1986 * See ide_taskfile_ioctl() for derivation
1987 */
1988static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001989 void __user *buf,
1990 ide_task_request_t *req_task,
1991 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001992{
1993 struct host_to_dev_fis fis;
1994 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001995 u8 *outbuf = NULL;
1996 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001997 dma_addr_t outbuf_dma = 0;
1998 dma_addr_t inbuf_dma = 0;
1999 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002000 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002001 unsigned int taskin = 0;
2002 unsigned int taskout = 0;
2003 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002004 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005 unsigned int force_single_sector;
2006 unsigned int transfer_size;
2007 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002008 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002009 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010
2011 taskout = req_task->out_size;
2012 taskin = req_task->in_size;
2013 /* 130560 = 512 * 0xFF*/
2014 if (taskin > 130560 || taskout > 130560) {
2015 err = -EINVAL;
2016 goto abort;
2017 }
2018
2019 if (taskout) {
2020 outbuf = kzalloc(taskout, GFP_KERNEL);
2021 if (outbuf == NULL) {
2022 err = -ENOMEM;
2023 goto abort;
2024 }
2025 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2026 err = -EFAULT;
2027 goto abort;
2028 }
2029 outbuf_dma = pci_map_single(dd->pdev,
2030 outbuf,
2031 taskout,
2032 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002033 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002034 err = -ENOMEM;
2035 goto abort;
2036 }
2037 dma_buffer = outbuf_dma;
2038 }
2039
2040 if (taskin) {
2041 inbuf = kzalloc(taskin, GFP_KERNEL);
2042 if (inbuf == NULL) {
2043 err = -ENOMEM;
2044 goto abort;
2045 }
2046
2047 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2048 err = -EFAULT;
2049 goto abort;
2050 }
2051 inbuf_dma = pci_map_single(dd->pdev,
2052 inbuf,
2053 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002054 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002055 err = -ENOMEM;
2056 goto abort;
2057 }
2058 dma_buffer = inbuf_dma;
2059 }
2060
2061 /* only supports PIO and non-data commands from this ioctl. */
2062 switch (req_task->data_phase) {
2063 case TASKFILE_OUT:
2064 nsect = taskout / ATA_SECT_SIZE;
2065 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2066 break;
2067 case TASKFILE_IN:
2068 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2069 break;
2070 case TASKFILE_NO_DATA:
2071 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2072 break;
2073 default:
2074 err = -EINVAL;
2075 goto abort;
2076 }
2077
Sam Bradshaw88523a62011-08-30 08:34:26 -06002078 /* Build the FIS. */
2079 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2080
2081 fis.type = 0x27;
2082 fis.opts = 1 << 7;
2083 fis.command = req_task->io_ports[7];
2084 fis.features = req_task->io_ports[1];
2085 fis.sect_count = req_task->io_ports[2];
2086 fis.lba_low = req_task->io_ports[3];
2087 fis.lba_mid = req_task->io_ports[4];
2088 fis.lba_hi = req_task->io_ports[5];
2089 /* Clear the dev bit*/
2090 fis.device = req_task->io_ports[6] & ~0x10;
2091
2092 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2093 req_task->in_flags.all =
2094 IDE_TASKFILE_STD_IN_FLAGS |
2095 (IDE_HOB_STD_IN_FLAGS << 8);
2096 fis.lba_low_ex = req_task->hob_ports[3];
2097 fis.lba_mid_ex = req_task->hob_ports[4];
2098 fis.lba_hi_ex = req_task->hob_ports[5];
2099 fis.features_ex = req_task->hob_ports[1];
2100 fis.sect_cnt_ex = req_task->hob_ports[2];
2101
2102 } else {
2103 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2104 }
2105
2106 force_single_sector = implicit_sector(fis.command, fis.features);
2107
2108 if ((taskin || taskout) && (!fis.sect_count)) {
2109 if (nsect)
2110 fis.sect_count = nsect;
2111 else {
2112 if (!force_single_sector) {
2113 dev_warn(&dd->pdev->dev,
2114 "data movement but "
2115 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002116 err = -EINVAL;
2117 goto abort;
2118 }
2119 }
2120 }
2121
2122 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002123 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002124 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2125 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002126 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002127 fis.command,
2128 fis.features,
2129 fis.sect_count,
2130 fis.lba_low,
2131 fis.lba_mid,
2132 fis.lba_hi,
2133 fis.device);
2134
Selvan Mani4453bc82012-09-27 14:36:43 +02002135 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002136 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2137 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002138 erasemode = 1;
2139 }
2140
2141 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142
2143 /* Determine the correct transfer size.*/
2144 if (force_single_sector)
2145 transfer_size = ATA_SECT_SIZE;
2146 else
2147 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2148
2149 /* Execute the command.*/
2150 if (mtip_exec_internal_command(dd->port,
2151 &fis,
2152 5,
2153 dma_buffer,
2154 transfer_size,
2155 0,
2156 GFP_KERNEL,
2157 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002158 err = -EIO;
2159 goto abort;
2160 }
2161
2162 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2163
2164 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2165 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2166 req_task->io_ports[7] = reply->control;
2167 } else {
2168 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2169 req_task->io_ports[7] = reply->command;
2170 }
2171
2172 /* reclaim the DMA buffers.*/
2173 if (inbuf_dma)
2174 pci_unmap_single(dd->pdev, inbuf_dma,
2175 taskin, DMA_FROM_DEVICE);
2176 if (outbuf_dma)
2177 pci_unmap_single(dd->pdev, outbuf_dma,
2178 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002179 inbuf_dma = 0;
2180 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002181
2182 /* return the ATA registers to the caller.*/
2183 req_task->io_ports[1] = reply->features;
2184 req_task->io_ports[2] = reply->sect_count;
2185 req_task->io_ports[3] = reply->lba_low;
2186 req_task->io_ports[4] = reply->lba_mid;
2187 req_task->io_ports[5] = reply->lba_hi;
2188 req_task->io_ports[6] = reply->device;
2189
2190 if (req_task->out_flags.all & 1) {
2191
2192 req_task->hob_ports[3] = reply->lba_low_ex;
2193 req_task->hob_ports[4] = reply->lba_mid_ex;
2194 req_task->hob_ports[5] = reply->lba_hi_ex;
2195 req_task->hob_ports[1] = reply->features_ex;
2196 req_task->hob_ports[2] = reply->sect_cnt_ex;
2197 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002198 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002199 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002200 "err %x, sect_cnt %x, lbalo %x,"
2201 "lbamid %x, lbahi %x, dev %x\n",
2202 __func__,
2203 req_task->io_ports[7],
2204 req_task->io_ports[1],
2205 req_task->io_ports[2],
2206 req_task->io_ports[3],
2207 req_task->io_ports[4],
2208 req_task->io_ports[5],
2209 req_task->io_ports[6]);
2210
Sam Bradshaw88523a62011-08-30 08:34:26 -06002211 if (taskout) {
2212 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2213 err = -EFAULT;
2214 goto abort;
2215 }
2216 }
2217 if (taskin) {
2218 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2219 err = -EFAULT;
2220 goto abort;
2221 }
2222 }
2223abort:
2224 if (inbuf_dma)
2225 pci_unmap_single(dd->pdev, inbuf_dma,
2226 taskin, DMA_FROM_DEVICE);
2227 if (outbuf_dma)
2228 pci_unmap_single(dd->pdev, outbuf_dma,
2229 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002230 kfree(outbuf);
2231 kfree(inbuf);
2232
2233 return err;
2234}
2235
2236/*
2237 * Handle IOCTL calls from the Block Layer.
2238 *
2239 * This function is called by the Block Layer when it receives an IOCTL
2240 * command that it does not understand. If the IOCTL command is not supported
2241 * this function returns -ENOTTY.
2242 *
2243 * @dd Pointer to the driver data structure.
2244 * @cmd IOCTL command passed from the Block Layer.
2245 * @arg IOCTL argument passed from the Block Layer.
2246 *
2247 * return value
2248 * 0 The IOCTL completed successfully.
2249 * -ENOTTY The specified command is not supported.
2250 * -EFAULT An error occurred copying data to a user space buffer.
2251 * -EIO An error occurred while executing the command.
2252 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002253static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2254 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002255{
2256 switch (cmd) {
2257 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002258 {
2259 if (copy_to_user((void __user *)arg, dd->port->identify,
2260 sizeof(u16) * ATA_ID_WORDS))
2261 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002262 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002263 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002264 case HDIO_DRIVE_CMD:
2265 {
2266 u8 drive_command[4];
2267
2268 /* Copy the user command info to our buffer. */
2269 if (copy_from_user(drive_command,
2270 (void __user *) arg,
2271 sizeof(drive_command)))
2272 return -EFAULT;
2273
2274 /* Execute the drive command. */
2275 if (exec_drive_command(dd->port,
2276 drive_command,
2277 (void __user *) (arg+4)))
2278 return -EIO;
2279
2280 /* Copy the status back to the users buffer. */
2281 if (copy_to_user((void __user *) arg,
2282 drive_command,
2283 sizeof(drive_command)))
2284 return -EFAULT;
2285
2286 break;
2287 }
2288 case HDIO_DRIVE_TASK:
2289 {
2290 u8 drive_command[7];
2291
2292 /* Copy the user command info to our buffer. */
2293 if (copy_from_user(drive_command,
2294 (void __user *) arg,
2295 sizeof(drive_command)))
2296 return -EFAULT;
2297
2298 /* Execute the drive command. */
2299 if (exec_drive_task(dd->port, drive_command))
2300 return -EIO;
2301
2302 /* Copy the status back to the users buffer. */
2303 if (copy_to_user((void __user *) arg,
2304 drive_command,
2305 sizeof(drive_command)))
2306 return -EFAULT;
2307
2308 break;
2309 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002310 case HDIO_DRIVE_TASKFILE: {
2311 ide_task_request_t req_task;
2312 int ret, outtotal;
2313
2314 if (copy_from_user(&req_task, (void __user *) arg,
2315 sizeof(req_task)))
2316 return -EFAULT;
2317
2318 outtotal = sizeof(req_task);
2319
2320 ret = exec_drive_taskfile(dd, (void __user *) arg,
2321 &req_task, outtotal);
2322
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002323 if (copy_to_user((void __user *) arg, &req_task,
2324 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002325 return -EFAULT;
2326
2327 return ret;
2328 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002329
2330 default:
2331 return -EINVAL;
2332 }
2333 return 0;
2334}
2335
2336/*
2337 * Submit an IO to the hw
2338 *
2339 * This function is called by the block layer to issue an io
2340 * to the device. Upon completion, the callback function will
2341 * be called with the data parameter passed as the callback data.
2342 *
2343 * @dd Pointer to the driver data structure.
2344 * @start First sector to read.
2345 * @nsect Number of sectors to read.
2346 * @nents Number of entries in scatter list for the read command.
2347 * @tag The tag of this read command.
2348 * @callback Pointer to the function that should be called
2349 * when the read completes.
2350 * @data Callback data passed to the callback function
2351 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002352 * @dir Direction (read or write)
2353 *
2354 * return value
2355 * None
2356 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002357static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2358 struct mtip_cmd *command, int nents,
2359 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002360{
2361 struct host_to_dev_fis *fis;
2362 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002363 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2364 u64 start = blk_rq_pos(rq);
2365 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002366
2367 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002368 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002369
2370 command->scatter_ents = nents;
2371
2372 /*
2373 * The number of retries for this command before it is
2374 * reported as a failure to the upper layers.
2375 */
2376 command->retries = MTIP_MAX_RETRIES;
2377
2378 /* Fill out fis */
2379 fis = command->command;
2380 fis->type = 0x27;
2381 fis->opts = 1 << 7;
Jens Axboeffc771b2014-05-09 09:42:02 -06002382 if (rq_data_dir(rq) == READ)
2383 fis->command = ATA_CMD_FPDMA_READ;
2384 else
2385 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002386 fis->lba_low = start & 0xFF;
2387 fis->lba_mid = (start >> 8) & 0xFF;
2388 fis->lba_hi = (start >> 16) & 0xFF;
2389 fis->lba_low_ex = (start >> 24) & 0xFF;
2390 fis->lba_mid_ex = (start >> 32) & 0xFF;
2391 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002392 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002393 fis->features = nsect & 0xFF;
2394 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002395 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002396 fis->sect_cnt_ex = 0;
2397 fis->control = 0;
2398 fis->res2 = 0;
2399 fis->res3 = 0;
2400 fill_command_sg(dd, command, nents);
2401
Jens Axboeffc771b2014-05-09 09:42:02 -06002402 if (command->unaligned)
Asai Thambi S P2077d942013-04-29 21:19:49 +02002403 fis->device |= 1 << 7;
2404
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002406 command->command_header->opts =
2407 __force_bit2int cpu_to_le32(
2408 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 command->command_header->byte_count = 0;
2410
2411 /*
2412 * Set the completion function and data for the command
2413 * within this layer.
2414 */
2415 command->comp_data = dd;
2416 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002417 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418
2419 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002420 * To prevent this command from being issued
2421 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002423 if (port->flags & MTIP_PF_PAUSE_IO) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002424 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002425 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002426 return;
2427 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002428
2429 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002430 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431}
2432
2433/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002434 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435 *
2436 * @dev Pointer to the device structure, passed by the kernrel.
2437 * @attr Pointer to the device_attribute structure passed by the kernel.
2438 * @buf Pointer to the char buffer that will receive the stats info.
2439 *
2440 * return value
2441 * The size, in bytes, of the data copied into buf.
2442 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002443static ssize_t mtip_hw_show_status(struct device *dev,
2444 struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct driver_data *dd = dev_to_disk(dev)->private_data;
2448 int size = 0;
2449
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002450 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002451 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002452 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002453 size += sprintf(buf, "%s", "write_protect\n");
2454 else
2455 size += sprintf(buf, "%s", "online\n");
2456
2457 return size;
2458}
2459
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002460static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002461
Asai Thambi S P0caff002013-04-03 19:56:21 +05302462/* debugsfs entries */
2463
2464static ssize_t show_device_status(struct device_driver *drv, char *buf)
2465{
2466 int size = 0;
2467 struct driver_data *dd, *tmp;
2468 unsigned long flags;
2469 char id_buf[42];
2470 u16 status = 0;
2471
2472 spin_lock_irqsave(&dev_lock, flags);
2473 size += sprintf(&buf[size], "Devices Present:\n");
2474 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002475 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302476 if (dd->port &&
2477 dd->port->identify &&
2478 dd->port->identify_valid) {
2479 strlcpy(id_buf,
2480 (char *) (dd->port->identify + 10), 21);
2481 status = *(dd->port->identify + 141);
2482 } else {
2483 memset(id_buf, 0, 42);
2484 status = 0;
2485 }
2486
2487 if (dd->port &&
2488 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2489 size += sprintf(&buf[size],
2490 " device %s %s (ftl rebuild %d %%)\n",
2491 dev_name(&dd->pdev->dev),
2492 id_buf,
2493 status);
2494 } else {
2495 size += sprintf(&buf[size],
2496 " device %s %s\n",
2497 dev_name(&dd->pdev->dev),
2498 id_buf);
2499 }
2500 }
2501 }
2502
2503 size += sprintf(&buf[size], "Devices Being Removed:\n");
2504 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002505 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302506 if (dd->port &&
2507 dd->port->identify &&
2508 dd->port->identify_valid) {
2509 strlcpy(id_buf,
2510 (char *) (dd->port->identify+10), 21);
2511 status = *(dd->port->identify + 141);
2512 } else {
2513 memset(id_buf, 0, 42);
2514 status = 0;
2515 }
2516
2517 if (dd->port &&
2518 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2519 size += sprintf(&buf[size],
2520 " device %s %s (ftl rebuild %d %%)\n",
2521 dev_name(&dd->pdev->dev),
2522 id_buf,
2523 status);
2524 } else {
2525 size += sprintf(&buf[size],
2526 " device %s %s\n",
2527 dev_name(&dd->pdev->dev),
2528 id_buf);
2529 }
2530 }
2531 }
2532 spin_unlock_irqrestore(&dev_lock, flags);
2533
2534 return size;
2535}
2536
2537static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2538 size_t len, loff_t *offset)
2539{
David Milburnc8afd0d2013-05-23 16:23:45 -05002540 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302541 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002542 char *buf;
2543 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302544
2545 if (!len || *offset)
2546 return 0;
2547
David Milburnc8afd0d2013-05-23 16:23:45 -05002548 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2549 if (!buf) {
2550 dev_err(&dd->pdev->dev,
2551 "Memory allocation: status buffer\n");
2552 return -ENOMEM;
2553 }
2554
Asai Thambi S P0caff002013-04-03 19:56:21 +05302555 size += show_device_status(NULL, buf);
2556
2557 *offset = size <= len ? size : len;
2558 size = copy_to_user(ubuf, buf, *offset);
2559 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002560 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561
David Milburnc8afd0d2013-05-23 16:23:45 -05002562 kfree(buf);
2563 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302564}
2565
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002566static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2567 size_t len, loff_t *offset)
2568{
2569 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002570 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002571 u32 group_allocated;
2572 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002573 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002574
2575 if (!len || size)
2576 return 0;
2577
David Milburnc8afd0d2013-05-23 16:23:45 -05002578 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2579 if (!buf) {
2580 dev_err(&dd->pdev->dev,
2581 "Memory allocation: register buffer\n");
2582 return -ENOMEM;
2583 }
2584
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002585 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2586
2587 for (n = dd->slot_groups-1; n >= 0; n--)
2588 size += sprintf(&buf[size], "%08X ",
2589 readl(dd->port->s_active[n]));
2590
2591 size += sprintf(&buf[size], "]\n");
2592 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2593
2594 for (n = dd->slot_groups-1; n >= 0; n--)
2595 size += sprintf(&buf[size], "%08X ",
2596 readl(dd->port->cmd_issue[n]));
2597
2598 size += sprintf(&buf[size], "]\n");
2599 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2600
2601 for (n = dd->slot_groups-1; n >= 0; n--)
2602 size += sprintf(&buf[size], "%08X ",
2603 readl(dd->port->completed[n]));
2604
2605 size += sprintf(&buf[size], "]\n");
2606 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2607 readl(dd->port->mmio + PORT_IRQ_STAT));
2608 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2609 readl(dd->mmio + HOST_IRQ_STAT));
2610 size += sprintf(&buf[size], "\n");
2611
2612 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--) {
2615 if (sizeof(long) > sizeof(u32))
2616 group_allocated =
2617 dd->port->allocated[n/2] >> (32*(n&1));
2618 else
2619 group_allocated = dd->port->allocated[n];
2620 size += sprintf(&buf[size], "%08X ", group_allocated);
2621 }
2622 size += sprintf(&buf[size], "]\n");
2623
2624 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2625
2626 for (n = dd->slot_groups-1; n >= 0; n--) {
2627 if (sizeof(long) > sizeof(u32))
2628 group_allocated =
2629 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2630 else
2631 group_allocated = dd->port->cmds_to_issue[n];
2632 size += sprintf(&buf[size], "%08X ", group_allocated);
2633 }
2634 size += sprintf(&buf[size], "]\n");
2635
2636 *offset = size <= len ? size : len;
2637 size = copy_to_user(ubuf, buf, *offset);
2638 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002639 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002640
David Milburnc8afd0d2013-05-23 16:23:45 -05002641 kfree(buf);
2642 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002643}
2644
2645static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2646 size_t len, loff_t *offset)
2647{
2648 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002649 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002650 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002651 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002652
2653 if (!len || size)
2654 return 0;
2655
David Milburnc8afd0d2013-05-23 16:23:45 -05002656 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2657 if (!buf) {
2658 dev_err(&dd->pdev->dev,
2659 "Memory allocation: flag buffer\n");
2660 return -ENOMEM;
2661 }
2662
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002663 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2664 dd->port->flags);
2665 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2666 dd->dd_flag);
2667
2668 *offset = size <= len ? size : len;
2669 size = copy_to_user(ubuf, buf, *offset);
2670 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002671 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002672
David Milburnc8afd0d2013-05-23 16:23:45 -05002673 kfree(buf);
2674 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002675}
2676
Asai Thambi S P0caff002013-04-03 19:56:21 +05302677static const struct file_operations mtip_device_status_fops = {
2678 .owner = THIS_MODULE,
2679 .open = simple_open,
2680 .read = mtip_hw_read_device_status,
2681 .llseek = no_llseek,
2682};
2683
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002684static const struct file_operations mtip_regs_fops = {
2685 .owner = THIS_MODULE,
2686 .open = simple_open,
2687 .read = mtip_hw_read_registers,
2688 .llseek = no_llseek,
2689};
2690
2691static const struct file_operations mtip_flags_fops = {
2692 .owner = THIS_MODULE,
2693 .open = simple_open,
2694 .read = mtip_hw_read_flags,
2695 .llseek = no_llseek,
2696};
2697
Sam Bradshaw88523a62011-08-30 08:34:26 -06002698/*
2699 * Create the sysfs related attributes.
2700 *
2701 * @dd Pointer to the driver data structure.
2702 * @kobj Pointer to the kobj for the block device.
2703 *
2704 * return value
2705 * 0 Operation completed successfully.
2706 * -EINVAL Invalid parameter.
2707 */
Jens Axboe63166682011-09-27 21:27:43 -06002708static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002709{
2710 if (!kobj || !dd)
2711 return -EINVAL;
2712
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002713 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2714 dev_warn(&dd->pdev->dev,
2715 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002716 return 0;
2717}
2718
2719/*
2720 * Remove the sysfs related attributes.
2721 *
2722 * @dd Pointer to the driver data structure.
2723 * @kobj Pointer to the kobj for the block device.
2724 *
2725 * return value
2726 * 0 Operation completed successfully.
2727 * -EINVAL Invalid parameter.
2728 */
Jens Axboe63166682011-09-27 21:27:43 -06002729static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002730{
2731 if (!kobj || !dd)
2732 return -EINVAL;
2733
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002734 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002735
2736 return 0;
2737}
2738
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002739static int mtip_hw_debugfs_init(struct driver_data *dd)
2740{
2741 if (!dfs_parent)
2742 return -1;
2743
2744 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2745 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2746 dev_warn(&dd->pdev->dev,
2747 "Error creating node %s under debugfs\n",
2748 dd->disk->disk_name);
2749 dd->dfs_node = NULL;
2750 return -1;
2751 }
2752
2753 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2754 &mtip_flags_fops);
2755 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2756 &mtip_regs_fops);
2757
2758 return 0;
2759}
2760
2761static void mtip_hw_debugfs_exit(struct driver_data *dd)
2762{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002763 if (dd->dfs_node)
2764 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002765}
2766
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002767static int mtip_free_orphan(struct driver_data *dd)
2768{
2769 struct kobject *kobj;
2770
2771 if (dd->bdev) {
2772 if (dd->bdev->bd_holders >= 1)
2773 return -2;
2774
2775 bdput(dd->bdev);
2776 dd->bdev = NULL;
2777 }
2778
2779 mtip_hw_debugfs_exit(dd);
2780
2781 spin_lock(&rssd_index_lock);
2782 ida_remove(&rssd_index_ida, dd->index);
2783 spin_unlock(&rssd_index_lock);
2784
2785 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
2786 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
2787 put_disk(dd->disk);
2788 } else {
2789 if (dd->disk) {
2790 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
2791 if (kobj) {
2792 mtip_hw_sysfs_exit(dd, kobj);
2793 kobject_put(kobj);
2794 }
2795 del_gendisk(dd->disk);
2796 dd->disk = NULL;
2797 }
2798 if (dd->queue) {
2799 dd->queue->queuedata = NULL;
2800 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06002801 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002802 dd->queue = NULL;
2803 }
2804 }
2805 kfree(dd);
2806 return 0;
2807}
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002808
Sam Bradshaw88523a62011-08-30 08:34:26 -06002809/*
2810 * Perform any init/resume time hardware setup
2811 *
2812 * @dd Pointer to the driver data structure.
2813 *
2814 * return value
2815 * None
2816 */
2817static inline void hba_setup(struct driver_data *dd)
2818{
2819 u32 hwdata;
2820 hwdata = readl(dd->mmio + HOST_HSORG);
2821
2822 /* interrupt bug workaround: use only 1 IS bit.*/
2823 writel(hwdata |
2824 HSORG_DISABLE_SLOTGRP_INTR |
2825 HSORG_DISABLE_SLOTGRP_PXIS,
2826 dd->mmio + HOST_HSORG);
2827}
2828
Asai Thambi S P2077d942013-04-29 21:19:49 +02002829static int mtip_device_unaligned_constrained(struct driver_data *dd)
2830{
2831 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2832}
2833
Sam Bradshaw88523a62011-08-30 08:34:26 -06002834/*
2835 * Detect the details of the product, and store anything needed
2836 * into the driver data structure. This includes product type and
2837 * version and number of slot groups.
2838 *
2839 * @dd Pointer to the driver data structure.
2840 *
2841 * return value
2842 * None
2843 */
2844static void mtip_detect_product(struct driver_data *dd)
2845{
2846 u32 hwdata;
2847 unsigned int rev, slotgroups;
2848
2849 /*
2850 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2851 * info register:
2852 * [15:8] hardware/software interface rev#
2853 * [ 3] asic-style interface
2854 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2855 */
2856 hwdata = readl(dd->mmio + HOST_HSORG);
2857
2858 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2859 dd->slot_groups = 1;
2860
2861 if (hwdata & 0x8) {
2862 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2863 rev = (hwdata & HSORG_HWREV) >> 8;
2864 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2865 dev_info(&dd->pdev->dev,
2866 "ASIC-FPGA design, HS rev 0x%x, "
2867 "%i slot groups [%i slots]\n",
2868 rev,
2869 slotgroups,
2870 slotgroups * 32);
2871
2872 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2873 dev_warn(&dd->pdev->dev,
2874 "Warning: driver only supports "
2875 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2876 slotgroups = MTIP_MAX_SLOT_GROUPS;
2877 }
2878 dd->slot_groups = slotgroups;
2879 return;
2880 }
2881
2882 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2883}
2884
2885/*
2886 * Blocking wait for FTL rebuild to complete
2887 *
2888 * @dd Pointer to the DRIVER_DATA structure.
2889 *
2890 * return value
2891 * 0 FTL rebuild completed successfully
2892 * -EFAULT FTL rebuild error/timeout/interruption
2893 */
2894static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2895{
2896 unsigned long timeout, cnt = 0, start;
2897
2898 dev_warn(&dd->pdev->dev,
2899 "FTL rebuild in progress. Polling for completion.\n");
2900
2901 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002902 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2903
2904 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002905 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002906 &dd->dd_flag)))
2907 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002908 if (mtip_check_surprise_removal(dd->pdev))
2909 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002910
Sam Bradshaw88523a62011-08-30 08:34:26 -06002911 if (mtip_get_identify(dd->port, NULL) < 0)
2912 return -EFAULT;
2913
2914 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2915 MTIP_FTL_REBUILD_MAGIC) {
2916 ssleep(1);
2917 /* Print message every 3 minutes */
2918 if (cnt++ >= 180) {
2919 dev_warn(&dd->pdev->dev,
2920 "FTL rebuild in progress (%d secs).\n",
2921 jiffies_to_msecs(jiffies - start) / 1000);
2922 cnt = 0;
2923 }
2924 } else {
2925 dev_warn(&dd->pdev->dev,
2926 "FTL rebuild complete (%d secs).\n",
2927 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002928 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002929 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002930 }
2931 ssleep(10);
2932 } while (time_before(jiffies, timeout));
2933
2934 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002935 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002936 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2937 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002938 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002939}
2940
2941/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002942 * service thread to issue queued commands
2943 *
2944 * @data Pointer to the driver data structure.
2945 *
2946 * return value
2947 * 0
2948 */
2949
2950static int mtip_service_thread(void *data)
2951{
2952 struct driver_data *dd = (struct driver_data *)data;
2953 unsigned long slot, slot_start, slot_wrap;
2954 unsigned int num_cmd_slots = dd->slot_groups * 32;
2955 struct mtip_port *port = dd->port;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002956 int ret;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002957
2958 while (1) {
2959 /*
2960 * the condition is to check neither an internal command is
2961 * is in progress nor error handling is active
2962 */
2963 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002964 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002965
2966 if (kthread_should_stop())
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002967 goto st_out;
2968
2969 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2970
2971 /* If I am an orphan, start self cleanup */
2972 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002973 break;
2974
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002975 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002976 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002977 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002978
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002979 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002980 slot = 1;
2981 /* used to restrict the loop to one iteration */
2982 slot_start = num_cmd_slots;
2983 slot_wrap = 0;
2984 while (1) {
2985 slot = find_next_bit(port->cmds_to_issue,
2986 num_cmd_slots, slot);
2987 if (slot_wrap == 1) {
2988 if ((slot_start >= slot) ||
2989 (slot >= num_cmd_slots))
2990 break;
2991 }
2992 if (unlikely(slot_start == num_cmd_slots))
2993 slot_start = slot;
2994
2995 if (unlikely(slot == num_cmd_slots)) {
2996 slot = 1;
2997 slot_wrap = 1;
2998 continue;
2999 }
3000
3001 /* Issue the command to the hardware */
3002 mtip_issue_ncq_command(port, slot);
3003
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003004 clear_bit(slot, port->cmds_to_issue);
3005 }
3006
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003007 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3008 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003009 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003010 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003011 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003012 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003013 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003014 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003015
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003016 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003017 goto st_out;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003018 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003019
3020 /* wait for pci remove to exit */
3021 while (1) {
3022 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3023 break;
3024 msleep_interruptible(1000);
3025 if (kthread_should_stop())
3026 goto st_out;
3027 }
3028
3029 while (1) {
3030 ret = mtip_free_orphan(dd);
3031 if (!ret) {
3032 /* NOTE: All data structures are invalid, do not
3033 * access any here */
3034 return 0;
3035 }
3036 msleep_interruptible(1000);
3037 if (kthread_should_stop())
3038 goto st_out;
3039 }
3040st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003041 return 0;
3042}
3043
3044/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003045 * DMA region teardown
3046 *
3047 * @dd Pointer to driver_data structure
3048 *
3049 * return value
3050 * None
3051 */
3052static void mtip_dma_free(struct driver_data *dd)
3053{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003054 struct mtip_port *port = dd->port;
3055
3056 if (port->block1)
3057 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3058 port->block1, port->block1_dma);
3059
3060 if (port->command_list) {
3061 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3062 port->command_list, port->command_list_dma);
3063 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003064}
3065
3066/*
3067 * DMA region setup
3068 *
3069 * @dd Pointer to driver_data structure
3070 *
3071 * return value
3072 * -ENOMEM Not enough free DMA region space to initialize driver
3073 */
3074static int mtip_dma_alloc(struct driver_data *dd)
3075{
3076 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003077
3078 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3079 port->block1 =
3080 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3081 &port->block1_dma, GFP_KERNEL);
3082 if (!port->block1)
3083 return -ENOMEM;
3084 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3085
3086 /* Allocate dma memory for command list */
3087 port->command_list =
3088 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3089 &port->command_list_dma, GFP_KERNEL);
3090 if (!port->command_list) {
3091 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3092 port->block1, port->block1_dma);
3093 port->block1 = NULL;
3094 port->block1_dma = 0;
3095 return -ENOMEM;
3096 }
3097 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3098
3099 /* Setup all pointers into first DMA region */
3100 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3101 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3102 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3103 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3104 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3105 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3106 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3107 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3108
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003109 return 0;
3110}
3111
Jens Axboeffc771b2014-05-09 09:42:02 -06003112static int mtip_hw_get_identify(struct driver_data *dd)
3113{
3114 struct smart_attr attr242;
3115 unsigned char *buf;
3116 int rv;
3117
3118 if (mtip_get_identify(dd->port, NULL) < 0)
3119 return -EFAULT;
3120
3121 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3122 MTIP_FTL_REBUILD_MAGIC) {
3123 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3124 return MTIP_FTL_REBUILD_MAGIC;
3125 }
3126 mtip_dump_identify(dd->port);
3127
3128 /* check write protect, over temp and rebuild statuses */
3129 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3130 dd->port->log_buf,
3131 dd->port->log_buf_dma, 1);
3132 if (rv) {
3133 dev_warn(&dd->pdev->dev,
3134 "Error in READ LOG EXT (10h) command\n");
3135 /* non-critical error, don't fail the load */
3136 } else {
3137 buf = (unsigned char *)dd->port->log_buf;
3138 if (buf[259] & 0x1) {
3139 dev_info(&dd->pdev->dev,
3140 "Write protect bit is set.\n");
3141 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3142 }
3143 if (buf[288] == 0xF7) {
3144 dev_info(&dd->pdev->dev,
3145 "Exceeded Tmax, drive in thermal shutdown.\n");
3146 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3147 }
3148 if (buf[288] == 0xBF) {
3149 dev_info(&dd->pdev->dev,
3150 "Drive indicates rebuild has failed.\n");
3151 /* TODO */
3152 }
3153 }
3154
3155 /* get write protect progess */
3156 memset(&attr242, 0, sizeof(struct smart_attr));
3157 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3158 dev_warn(&dd->pdev->dev,
3159 "Unable to check write protect progress\n");
3160 else
3161 dev_info(&dd->pdev->dev,
3162 "Write protect progress: %u%% (%u blocks)\n",
3163 attr242.cur, le32_to_cpu(attr242.data));
3164
3165 return rv;
3166}
3167
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003168/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003169 * Called once for each card.
3170 *
3171 * @dd Pointer to the driver data structure.
3172 *
3173 * return value
3174 * 0 on success, else an error code.
3175 */
Jens Axboe63166682011-09-27 21:27:43 -06003176static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003177{
3178 int i;
3179 int rv;
3180 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003181 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003182
3183 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3184
3185 mtip_detect_product(dd);
3186 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3187 rv = -EIO;
3188 goto out1;
3189 }
3190 num_command_slots = dd->slot_groups * 32;
3191
3192 hba_setup(dd);
3193
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003194 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3195 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003196 if (!dd->port) {
3197 dev_err(&dd->pdev->dev,
3198 "Memory allocation: port structure\n");
3199 return -ENOMEM;
3200 }
3201
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003202 /* Continue workqueue setup */
3203 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3204 dd->work[i].port = dd->port;
3205
Asai Thambi S P2077d942013-04-29 21:19:49 +02003206 /* Enable unaligned IO constraints for some devices */
3207 if (mtip_device_unaligned_constrained(dd))
3208 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3209 else
3210 dd->unal_qdepth = 0;
3211
Asai Thambi S P2077d942013-04-29 21:19:49 +02003212 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003213
3214 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003215 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3216 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003217
3218 /* Set the port mmio base address. */
3219 dd->port->mmio = dd->mmio + PORT_OFFSET;
3220 dd->port->dd = dd;
3221
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003222 /* DMA allocations */
3223 rv = mtip_dma_alloc(dd);
3224 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003225 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003226
3227 /* Setup the pointers to the extended s_active and CI registers. */
3228 for (i = 0; i < dd->slot_groups; i++) {
3229 dd->port->s_active[i] =
3230 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3231 dd->port->cmd_issue[i] =
3232 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3233 dd->port->completed[i] =
3234 dd->port->mmio + i*0x80 + PORT_SDBV;
3235 }
3236
Asai Thambi S P45038362012-04-09 08:35:38 +02003237 timetaken = jiffies;
3238 timeout = jiffies + msecs_to_jiffies(30000);
3239 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3240 time_before(jiffies, timeout)) {
3241 mdelay(100);
3242 }
3243 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3244 timetaken = jiffies - timetaken;
3245 dev_warn(&dd->pdev->dev,
3246 "Surprise removal detected at %u ms\n",
3247 jiffies_to_msecs(timetaken));
3248 rv = -ENODEV;
3249 goto out2 ;
3250 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003251 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003252 timetaken = jiffies - timetaken;
3253 dev_warn(&dd->pdev->dev,
3254 "Removal detected at %u ms\n",
3255 jiffies_to_msecs(timetaken));
3256 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003257 goto out2;
3258 }
3259
Asai Thambi S P45038362012-04-09 08:35:38 +02003260 /* Conditionally reset the HBA. */
3261 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3262 if (mtip_hba_reset(dd) < 0) {
3263 dev_err(&dd->pdev->dev,
3264 "Card did not reset within timeout\n");
3265 rv = -EIO;
3266 goto out2;
3267 }
3268 } else {
3269 /* Clear any pending interrupts on the HBA */
3270 writel(readl(dd->mmio + HOST_IRQ_STAT),
3271 dd->mmio + HOST_IRQ_STAT);
3272 }
3273
Sam Bradshaw88523a62011-08-30 08:34:26 -06003274 mtip_init_port(dd->port);
3275 mtip_start_port(dd->port);
3276
3277 /* Setup the ISR and enable interrupts. */
3278 rv = devm_request_irq(&dd->pdev->dev,
3279 dd->pdev->irq,
3280 mtip_irq_handler,
3281 IRQF_SHARED,
3282 dev_driver_string(&dd->pdev->dev),
3283 dd);
3284
3285 if (rv) {
3286 dev_err(&dd->pdev->dev,
3287 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3288 goto out2;
3289 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003290 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003291
3292 /* Enable interrupts on the HBA. */
3293 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3294 dd->mmio + HOST_CTL);
3295
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003296 init_waitqueue_head(&dd->port->svc_wait);
3297
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003298 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003299 rv = -EFAULT;
3300 goto out3;
3301 }
3302
Sam Bradshaw88523a62011-08-30 08:34:26 -06003303 return rv;
3304
3305out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003306 /* Disable interrupts on the HBA. */
3307 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3308 dd->mmio + HOST_CTL);
3309
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003310 /* Release the IRQ. */
3311 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003312 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3313
3314out2:
3315 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003316 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003317
Sam Bradshaw88523a62011-08-30 08:34:26 -06003318out1:
3319 /* Free the memory allocated for the for structure. */
3320 kfree(dd->port);
3321
3322 return rv;
3323}
3324
Jens Axboeffc771b2014-05-09 09:42:02 -06003325static void mtip_standby_drive(struct driver_data *dd)
3326{
3327 if (dd->sr)
3328 return;
3329
3330 /*
3331 * Send standby immediate (E0h) to the drive so that it
3332 * saves its state.
3333 */
3334 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3335 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3336 if (mtip_standby_immediate(dd->port))
3337 dev_warn(&dd->pdev->dev,
3338 "STANDBY IMMEDIATE failed\n");
3339}
3340
Sam Bradshaw88523a62011-08-30 08:34:26 -06003341/*
3342 * Called to deinitialize an interface.
3343 *
3344 * @dd Pointer to the driver data structure.
3345 *
3346 * return value
3347 * 0
3348 */
Jens Axboe63166682011-09-27 21:27:43 -06003349static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003350{
3351 /*
3352 * Send standby immediate (E0h) to the drive so that it
3353 * saves its state.
3354 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003355 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003356 /* de-initialize the port. */
3357 mtip_deinit_port(dd->port);
3358
3359 /* Disable interrupts on the HBA. */
3360 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3361 dd->mmio + HOST_CTL);
3362 }
3363
Sam Bradshaw88523a62011-08-30 08:34:26 -06003364 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003365 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003366 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3367
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003368 /* Free dma regions */
3369 mtip_dma_free(dd);
3370
Sam Bradshaw88523a62011-08-30 08:34:26 -06003371 /* Free the memory allocated for the for structure. */
3372 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003373 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003374
3375 return 0;
3376}
3377
3378/*
3379 * Issue a Standby Immediate command to the device.
3380 *
3381 * This function is called by the Block Layer just before the
3382 * system powers off during a shutdown.
3383 *
3384 * @dd Pointer to the driver data structure.
3385 *
3386 * return value
3387 * 0
3388 */
Jens Axboe63166682011-09-27 21:27:43 -06003389static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003390{
3391 /*
3392 * Send standby immediate (E0h) to the drive so that it
3393 * saves its state.
3394 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003395 if (!dd->sr && dd->port)
3396 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003397
3398 return 0;
3399}
3400
3401/*
3402 * Suspend function
3403 *
3404 * This function is called by the Block Layer just before the
3405 * system hibernates.
3406 *
3407 * @dd Pointer to the driver data structure.
3408 *
3409 * return value
3410 * 0 Suspend was successful
3411 * -EFAULT Suspend was not successful
3412 */
Jens Axboe63166682011-09-27 21:27:43 -06003413static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003414{
3415 /*
3416 * Send standby immediate (E0h) to the drive
3417 * so that it saves its state.
3418 */
3419 if (mtip_standby_immediate(dd->port) != 0) {
3420 dev_err(&dd->pdev->dev,
3421 "Failed standby-immediate command\n");
3422 return -EFAULT;
3423 }
3424
3425 /* Disable interrupts on the HBA.*/
3426 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3427 dd->mmio + HOST_CTL);
3428 mtip_deinit_port(dd->port);
3429
3430 return 0;
3431}
3432
3433/*
3434 * Resume function
3435 *
3436 * This function is called by the Block Layer as the
3437 * system resumes.
3438 *
3439 * @dd Pointer to the driver data structure.
3440 *
3441 * return value
3442 * 0 Resume was successful
3443 * -EFAULT Resume was not successful
3444 */
Jens Axboe63166682011-09-27 21:27:43 -06003445static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003446{
3447 /* Perform any needed hardware setup steps */
3448 hba_setup(dd);
3449
3450 /* Reset the HBA */
3451 if (mtip_hba_reset(dd) != 0) {
3452 dev_err(&dd->pdev->dev,
3453 "Unable to reset the HBA\n");
3454 return -EFAULT;
3455 }
3456
3457 /*
3458 * Enable the port, DMA engine, and FIS reception specific
3459 * h/w in controller.
3460 */
3461 mtip_init_port(dd->port);
3462 mtip_start_port(dd->port);
3463
3464 /* Enable interrupts on the HBA.*/
3465 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3466 dd->mmio + HOST_CTL);
3467
3468 return 0;
3469}
3470
3471/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003472 * Helper function for reusing disk name
3473 * upon hot insertion.
3474 */
3475static int rssd_disk_name_format(char *prefix,
3476 int index,
3477 char *buf,
3478 int buflen)
3479{
3480 const int base = 'z' - 'a' + 1;
3481 char *begin = buf + strlen(prefix);
3482 char *end = buf + buflen;
3483 char *p;
3484 int unit;
3485
3486 p = end - 1;
3487 *p = '\0';
3488 unit = base;
3489 do {
3490 if (p == begin)
3491 return -EINVAL;
3492 *--p = 'a' + (index % unit);
3493 index = (index / unit) - 1;
3494 } while (index >= 0);
3495
3496 memmove(begin, p, end - p);
3497 memcpy(buf, prefix, strlen(prefix));
3498
3499 return 0;
3500}
3501
3502/*
3503 * Block layer IOCTL handler.
3504 *
3505 * @dev Pointer to the block_device structure.
3506 * @mode ignored
3507 * @cmd IOCTL command passed from the user application.
3508 * @arg Argument passed from the user application.
3509 *
3510 * return value
3511 * 0 IOCTL completed successfully.
3512 * -ENOTTY IOCTL not supported or invalid driver data
3513 * structure pointer.
3514 */
3515static int mtip_block_ioctl(struct block_device *dev,
3516 fmode_t mode,
3517 unsigned cmd,
3518 unsigned long arg)
3519{
3520 struct driver_data *dd = dev->bd_disk->private_data;
3521
3522 if (!capable(CAP_SYS_ADMIN))
3523 return -EACCES;
3524
3525 if (!dd)
3526 return -ENOTTY;
3527
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003528 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003529 return -ENOTTY;
3530
Sam Bradshaw88523a62011-08-30 08:34:26 -06003531 switch (cmd) {
3532 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003533 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003535 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003536 }
3537}
3538
Jens Axboe16d02c02011-09-27 15:50:01 -06003539#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003540/*
3541 * Block layer compat IOCTL handler.
3542 *
3543 * @dev Pointer to the block_device structure.
3544 * @mode ignored
3545 * @cmd IOCTL command passed from the user application.
3546 * @arg Argument passed from the user application.
3547 *
3548 * return value
3549 * 0 IOCTL completed successfully.
3550 * -ENOTTY IOCTL not supported or invalid driver data
3551 * structure pointer.
3552 */
3553static int mtip_block_compat_ioctl(struct block_device *dev,
3554 fmode_t mode,
3555 unsigned cmd,
3556 unsigned long arg)
3557{
3558 struct driver_data *dd = dev->bd_disk->private_data;
3559
3560 if (!capable(CAP_SYS_ADMIN))
3561 return -EACCES;
3562
3563 if (!dd)
3564 return -ENOTTY;
3565
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003566 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003567 return -ENOTTY;
3568
Sam Bradshaw88523a62011-08-30 08:34:26 -06003569 switch (cmd) {
3570 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003571 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003572 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003573 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003574 ide_task_request_t req_task;
3575 int compat_tasksize, outtotal, ret;
3576
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003577 compat_tasksize =
3578 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003579
3580 compat_req_task =
3581 (struct mtip_compat_ide_task_request_s __user *) arg;
3582
3583 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003584 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003585 return -EFAULT;
3586
3587 if (get_user(req_task.out_size, &compat_req_task->out_size))
3588 return -EFAULT;
3589
3590 if (get_user(req_task.in_size, &compat_req_task->in_size))
3591 return -EFAULT;
3592
3593 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3594
3595 ret = exec_drive_taskfile(dd, (void __user *) arg,
3596 &req_task, outtotal);
3597
3598 if (copy_to_user((void __user *) arg, &req_task,
3599 compat_tasksize -
3600 (2 * sizeof(compat_long_t))))
3601 return -EFAULT;
3602
3603 if (put_user(req_task.out_size, &compat_req_task->out_size))
3604 return -EFAULT;
3605
3606 if (put_user(req_task.in_size, &compat_req_task->in_size))
3607 return -EFAULT;
3608
3609 return ret;
3610 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003611 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003612 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003613 }
3614}
Jens Axboe16d02c02011-09-27 15:50:01 -06003615#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003616
3617/*
3618 * Obtain the geometry of the device.
3619 *
3620 * You may think that this function is obsolete, but some applications,
3621 * fdisk for example still used CHS values. This function describes the
3622 * device as having 224 heads and 56 sectors per cylinder. These values are
3623 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3624 * partition is described in terms of a start and end cylinder this means
3625 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3626 * affects performance.
3627 *
3628 * @dev Pointer to the block_device strucutre.
3629 * @geo Pointer to a hd_geometry structure.
3630 *
3631 * return value
3632 * 0 Operation completed successfully.
3633 * -ENOTTY An error occurred while reading the drive capacity.
3634 */
3635static int mtip_block_getgeo(struct block_device *dev,
3636 struct hd_geometry *geo)
3637{
3638 struct driver_data *dd = dev->bd_disk->private_data;
3639 sector_t capacity;
3640
3641 if (!dd)
3642 return -ENOTTY;
3643
3644 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3645 dev_warn(&dd->pdev->dev,
3646 "Could not get drive capacity.\n");
3647 return -ENOTTY;
3648 }
3649
3650 geo->heads = 224;
3651 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003652 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003653 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003654 return 0;
3655}
3656
3657/*
3658 * Block device operation function.
3659 *
3660 * This structure contains pointers to the functions required by the block
3661 * layer.
3662 */
3663static const struct block_device_operations mtip_block_ops = {
3664 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003665#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003666 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003667#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003668 .getgeo = mtip_block_getgeo,
3669 .owner = THIS_MODULE
3670};
3671
3672/*
3673 * Block layer make request function.
3674 *
3675 * This function is called by the kernel to process a BIO for
3676 * the P320 device.
3677 *
3678 * @queue Pointer to the request queue. Unused other than to obtain
3679 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003680 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003681 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003682 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003683static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003684{
Jens Axboeffc771b2014-05-09 09:42:02 -06003685 struct driver_data *dd = hctx->queue->queuedata;
3686 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3687 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003688
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003689 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3690 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3691 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003692 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003693 }
3694 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003695 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003696 }
3697 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3698 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003699 rq_data_dir(rq))) {
3700 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003701 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003702 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3703 return -ENODATA;
3704 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3705 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003706 }
3707
Jens Axboeffc771b2014-05-09 09:42:02 -06003708 if (rq->cmd_flags & REQ_DISCARD) {
3709 int err;
3710
3711 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
3712 blk_mq_end_io(rq, err);
3713 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003714 }
3715
Jens Axboeffc771b2014-05-09 09:42:02 -06003716 /* Create the scatter list for this request. */
3717 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003718
Jens Axboeffc771b2014-05-09 09:42:02 -06003719 /* Issue the read/write. */
3720 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3721 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003722}
3723
Jens Axboeffc771b2014-05-09 09:42:02 -06003724static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3725 struct request *rq)
3726{
3727 struct driver_data *dd = hctx->queue->queuedata;
3728 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3729
3730 if (!dd->unal_qdepth || rq_data_dir(rq) == READ)
3731 return false;
3732
3733 /*
3734 * If unaligned depth must be limited on this controller, mark it
3735 * as unaligned if the IO isn't on a 4k boundary (start of length).
3736 */
3737 if (blk_rq_sectors(rq) <= 64) {
3738 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3739 cmd->unaligned = 1;
3740 }
3741
3742 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3743 return true;
3744
3745 return false;
3746}
3747
3748static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
3749{
3750 int ret;
3751
3752 if (mtip_check_unal_depth(hctx, rq))
3753 return BLK_MQ_RQ_QUEUE_BUSY;
3754
3755 ret = mtip_submit_request(hctx, rq);
3756 if (!ret)
3757 return BLK_MQ_RQ_QUEUE_OK;
3758
3759 rq->errors = ret;
3760 return BLK_MQ_RQ_QUEUE_ERROR;
3761}
3762
3763static void mtip_free_cmd(void *data, struct request *rq,
3764 unsigned int hctx_idx, unsigned int request_idx)
3765{
3766 struct driver_data *dd = data;
3767 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3768
3769 if (!cmd->command)
3770 return;
3771
3772 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3773 cmd->command, cmd->command_dma);
3774}
3775
3776static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3777 unsigned int request_idx, unsigned int numa_node)
3778{
3779 struct driver_data *dd = data;
3780 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3781 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3782
3783 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3784 &cmd->command_dma, GFP_KERNEL);
3785 if (!cmd->command)
3786 return -ENOMEM;
3787
3788 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3789
3790 /* Point the command headers at the command tables. */
3791 cmd->command_header = dd->port->command_list +
3792 (sizeof(struct mtip_cmd_hdr) * request_idx);
3793 cmd->command_header_dma = dd->port->command_list_dma +
3794 (sizeof(struct mtip_cmd_hdr) * request_idx);
3795
3796 if (host_cap_64)
3797 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3798
3799 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3800
3801 sg_init_table(cmd->sg, MTIP_MAX_SG);
3802 return 0;
3803}
3804
3805static struct blk_mq_ops mtip_mq_ops = {
3806 .queue_rq = mtip_queue_rq,
3807 .map_queue = blk_mq_map_queue,
3808 .alloc_hctx = blk_mq_alloc_single_hw_queue,
3809 .free_hctx = blk_mq_free_single_hw_queue,
3810 .init_request = mtip_init_cmd,
3811 .exit_request = mtip_free_cmd,
3812};
3813
Sam Bradshaw88523a62011-08-30 08:34:26 -06003814/*
3815 * Block layer initialization function.
3816 *
3817 * This function is called once by the PCI layer for each P320
3818 * device that is connected to the system.
3819 *
3820 * @dd Pointer to the driver data structure.
3821 *
3822 * return value
3823 * 0 on success else an error code.
3824 */
Jens Axboe63166682011-09-27 21:27:43 -06003825static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003826{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003827 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003828 sector_t capacity;
3829 unsigned int index = 0;
3830 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003831 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003832
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003833 if (dd->disk)
3834 goto skip_create_disk; /* hw init done, before rebuild */
3835
Jens Axboeffc771b2014-05-09 09:42:02 -06003836 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003837 rv = -EINVAL;
3838 goto protocol_init_error;
3839 }
3840
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003841 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003842 if (dd->disk == NULL) {
3843 dev_err(&dd->pdev->dev,
3844 "Unable to allocate gendisk structure\n");
3845 rv = -EINVAL;
3846 goto alloc_disk_error;
3847 }
3848
3849 /* Generate the disk name, implemented same as in sd.c */
3850 do {
3851 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3852 goto ida_get_error;
3853
3854 spin_lock(&rssd_index_lock);
3855 rv = ida_get_new(&rssd_index_ida, &index);
3856 spin_unlock(&rssd_index_lock);
3857 } while (rv == -EAGAIN);
3858
3859 if (rv)
3860 goto ida_get_error;
3861
3862 rv = rssd_disk_name_format("rssd",
3863 index,
3864 dd->disk->disk_name,
3865 DISK_NAME_LEN);
3866 if (rv)
3867 goto disk_index_error;
3868
3869 dd->disk->driverfs_dev = &dd->pdev->dev;
3870 dd->disk->major = dd->major;
3871 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3872 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003873 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003874 dd->index = index;
3875
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003876 mtip_hw_debugfs_init(dd);
3877
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003878skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003879 memset(&dd->tags, 0, sizeof(dd->tags));
3880 dd->tags.ops = &mtip_mq_ops;
3881 dd->tags.nr_hw_queues = 1;
3882 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3883 dd->tags.reserved_tags = 1;
3884 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3885 dd->tags.numa_node = dd->numa_node;
3886 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3887 dd->tags.driver_data = dd;
3888
3889 rv = blk_mq_alloc_tag_set(&dd->tags);
3890 if (rv) {
3891 dev_err(&dd->pdev->dev,
3892 "Unable to allocate request queue\n");
3893 rv = -ENOMEM;
3894 goto block_queue_alloc_init_error;
3895 }
3896
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003897 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003898 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003899 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003900 dev_err(&dd->pdev->dev,
3901 "Unable to allocate request queue\n");
3902 rv = -ENOMEM;
3903 goto block_queue_alloc_init_error;
3904 }
3905
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003906 dd->disk->queue = dd->queue;
3907 dd->queue->queuedata = dd;
3908
Jens Axboeffc771b2014-05-09 09:42:02 -06003909 /* Initialize the protocol layer. */
3910 wait_for_rebuild = mtip_hw_get_identify(dd);
3911 if (wait_for_rebuild < 0) {
3912 dev_err(&dd->pdev->dev,
3913 "Protocol layer initialization failed\n");
3914 rv = -EINVAL;
3915 goto init_hw_cmds_error;
3916 }
3917
3918 /*
3919 * if rebuild pending, start the service thread, and delay the block
3920 * queue creation and add_disk()
3921 */
3922 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3923 goto start_service_thread;
3924
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003925 /* Set device limits. */
3926 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3927 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3928 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003929 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3930 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003931 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003932 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003933
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003934 /*
3935 * write back cache is not supported in the device. FUA depends on
3936 * write back cache support, hence setting flush support to zero.
3937 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003938 blk_queue_flush(dd->queue, 0);
3939
Asai Thambi S P15283462013-01-11 14:41:34 +01003940 /* Signal trim support */
3941 if (dd->trim_supp == true) {
3942 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3943 dd->queue->limits.discard_granularity = 4096;
3944 blk_queue_max_discard_sectors(dd->queue,
3945 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3946 dd->queue->limits.discard_zeroes_data = 0;
3947 }
3948
Sam Bradshaw88523a62011-08-30 08:34:26 -06003949 /* Set the capacity of the device in 512 byte sectors. */
3950 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3951 dev_warn(&dd->pdev->dev,
3952 "Could not read drive capacity\n");
3953 rv = -EIO;
3954 goto read_capacity_error;
3955 }
3956 set_capacity(dd->disk, capacity);
3957
3958 /* Enable the block device and add it to /dev */
3959 add_disk(dd->disk);
3960
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003961 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003962 /*
3963 * Now that the disk is active, initialize any sysfs attributes
3964 * managed by the protocol layer.
3965 */
3966 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3967 if (kobj) {
3968 mtip_hw_sysfs_init(dd, kobj);
3969 kobject_put(kobj);
3970 }
3971
Asai Thambi S P45038362012-04-09 08:35:38 +02003972 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003973 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003974 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003975 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003976
3977start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003978 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003979 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07003980 dd, dd->numa_node, "%s",
3981 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003982
3983 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003984 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003985 dd->mtip_svc_handler = NULL;
3986 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003987 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003988 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003989 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003990 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3991 rv = wait_for_rebuild;
3992
Sam Bradshaw88523a62011-08-30 08:34:26 -06003993 return rv;
3994
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003995kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003996 bdput(dd->bdev);
3997 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003998
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003999 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004000 del_gendisk(dd->disk);
4001
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004002read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004003init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004004 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004005 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004006block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004007 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004008disk_index_error:
4009 spin_lock(&rssd_index_lock);
4010 ida_remove(&rssd_index_ida, index);
4011 spin_unlock(&rssd_index_lock);
4012
4013ida_get_error:
4014 put_disk(dd->disk);
4015
4016alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004017 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004018
4019protocol_init_error:
4020 return rv;
4021}
4022
4023/*
4024 * Block layer deinitialization function.
4025 *
4026 * Called by the PCI layer as each P320 device is removed.
4027 *
4028 * @dd Pointer to the driver data structure.
4029 *
4030 * return value
4031 * 0
4032 */
Jens Axboe63166682011-09-27 21:27:43 -06004033static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004034{
4035 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004036
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004037 if (!dd->sr) {
4038 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004039
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004040 if (dd->mtip_svc_handler) {
4041 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4042 wake_up_interruptible(&dd->port->svc_wait);
4043 kthread_stop(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004044 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004045
4046 /* Clean up the sysfs attributes, if created */
4047 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4048 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4049 if (kobj) {
4050 mtip_hw_sysfs_exit(dd, kobj);
4051 kobject_put(kobj);
4052 }
4053 }
Jens Axboeffc771b2014-05-09 09:42:02 -06004054
4055 mtip_standby_drive(dd);
4056
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004057 /*
4058 * Delete our gendisk structure. This also removes the device
4059 * from /dev
4060 */
4061 if (dd->bdev) {
4062 bdput(dd->bdev);
4063 dd->bdev = NULL;
4064 }
4065 if (dd->disk) {
4066 if (dd->disk->queue) {
4067 del_gendisk(dd->disk);
4068 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004069 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004070 dd->queue = NULL;
4071 } else
4072 put_disk(dd->disk);
4073 }
4074 dd->disk = NULL;
4075
4076 spin_lock(&rssd_index_lock);
4077 ida_remove(&rssd_index_ida, dd->index);
4078 spin_unlock(&rssd_index_lock);
4079 } else {
4080 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4081 dd->disk->disk_name);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004082 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004083
4084 /* De-initialize the protocol layer. */
4085 mtip_hw_exit(dd);
4086
4087 return 0;
4088}
4089
4090/*
4091 * Function called by the PCI layer when just before the
4092 * machine shuts down.
4093 *
4094 * If a protocol layer shutdown function is present it will be called
4095 * by this function.
4096 *
4097 * @dd Pointer to the driver data structure.
4098 *
4099 * return value
4100 * 0
4101 */
Jens Axboe63166682011-09-27 21:27:43 -06004102static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004103{
Jens Axboeffc771b2014-05-09 09:42:02 -06004104 mtip_hw_shutdown(dd);
4105
Sam Bradshaw88523a62011-08-30 08:34:26 -06004106 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304107 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304108 dev_info(&dd->pdev->dev,
4109 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304110
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304111 if (dd->disk->queue) {
4112 del_gendisk(dd->disk);
4113 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004114 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304115 } else
4116 put_disk(dd->disk);
4117 dd->disk = NULL;
4118 dd->queue = NULL;
4119 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004120
4121 spin_lock(&rssd_index_lock);
4122 ida_remove(&rssd_index_ida, dd->index);
4123 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004124 return 0;
4125}
4126
Jens Axboe63166682011-09-27 21:27:43 -06004127static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004128{
4129 dev_info(&dd->pdev->dev,
4130 "Suspending %s ...\n", dd->disk->disk_name);
4131 mtip_hw_suspend(dd);
4132 return 0;
4133}
4134
Jens Axboe63166682011-09-27 21:27:43 -06004135static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004136{
4137 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4138 dd->disk->disk_name);
4139 mtip_hw_resume(dd);
4140 return 0;
4141}
4142
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004143static void drop_cpu(int cpu)
4144{
4145 cpu_use[cpu]--;
4146}
4147
4148static int get_least_used_cpu_on_node(int node)
4149{
4150 int cpu, least_used_cpu, least_cnt;
4151 const struct cpumask *node_mask;
4152
4153 node_mask = cpumask_of_node(node);
4154 least_used_cpu = cpumask_first(node_mask);
4155 least_cnt = cpu_use[least_used_cpu];
4156 cpu = least_used_cpu;
4157
4158 for_each_cpu(cpu, node_mask) {
4159 if (cpu_use[cpu] < least_cnt) {
4160 least_used_cpu = cpu;
4161 least_cnt = cpu_use[cpu];
4162 }
4163 }
4164 cpu_use[least_used_cpu]++;
4165 return least_used_cpu;
4166}
4167
4168/* Helper for selecting a node in round robin mode */
4169static inline int mtip_get_next_rr_node(void)
4170{
4171 static int next_node = -1;
4172
4173 if (next_node == -1) {
4174 next_node = first_online_node;
4175 return next_node;
4176 }
4177
4178 next_node = next_online_node(next_node);
4179 if (next_node == MAX_NUMNODES)
4180 next_node = first_online_node;
4181 return next_node;
4182}
4183
Fengguang Wu25bac122013-01-12 15:31:40 +08004184static DEFINE_HANDLER(0);
4185static DEFINE_HANDLER(1);
4186static DEFINE_HANDLER(2);
4187static DEFINE_HANDLER(3);
4188static DEFINE_HANDLER(4);
4189static DEFINE_HANDLER(5);
4190static DEFINE_HANDLER(6);
4191static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004192
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004193static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4194{
4195 int pos;
4196 unsigned short pcie_dev_ctrl;
4197
4198 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4199 if (pos) {
4200 pci_read_config_word(pdev,
4201 pos + PCI_EXP_DEVCTL,
4202 &pcie_dev_ctrl);
4203 if (pcie_dev_ctrl & (1 << 11) ||
4204 pcie_dev_ctrl & (1 << 4)) {
4205 dev_info(&dd->pdev->dev,
4206 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4207 pdev->vendor, pdev->device);
4208 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4209 PCI_EXP_DEVCTL_RELAX_EN);
4210 pci_write_config_word(pdev,
4211 pos + PCI_EXP_DEVCTL,
4212 pcie_dev_ctrl);
4213 }
4214 }
4215}
4216
4217static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4218{
4219 /*
4220 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4221 * device with device id 0x5aXX
4222 */
4223 if (pdev->bus && pdev->bus->self) {
4224 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4225 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4226 mtip_disable_link_opts(dd, pdev->bus->self);
4227 } else {
4228 /* Check further up the topology */
4229 struct pci_dev *parent_dev = pdev->bus->self;
4230 if (parent_dev->bus &&
4231 parent_dev->bus->parent &&
4232 parent_dev->bus->parent->self &&
4233 parent_dev->bus->parent->self->vendor ==
4234 PCI_VENDOR_ID_ATI &&
4235 (parent_dev->bus->parent->self->device &
4236 0xff00) == 0x5a00) {
4237 mtip_disable_link_opts(dd,
4238 parent_dev->bus->parent->self);
4239 }
4240 }
4241 }
4242}
4243
Sam Bradshaw88523a62011-08-30 08:34:26 -06004244/*
4245 * Called for each supported PCI device detected.
4246 *
4247 * This function allocates the private data structure, enables the
4248 * PCI device and then calls the block layer initialization function.
4249 *
4250 * return value
4251 * 0 on success else an error code.
4252 */
4253static int mtip_pci_probe(struct pci_dev *pdev,
4254 const struct pci_device_id *ent)
4255{
4256 int rv = 0;
4257 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004258 char cpu_list[256];
4259 const struct cpumask *node_mask;
4260 int cpu, i = 0, j = 0;
4261 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304262 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004263
4264 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004265 my_node = pcibus_to_node(pdev->bus);
4266 if (my_node != NUMA_NO_NODE) {
4267 if (!node_online(my_node))
4268 my_node = mtip_get_next_rr_node();
4269 } else {
4270 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4271 my_node = mtip_get_next_rr_node();
4272 }
4273 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4274 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004275 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004276
4277 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004278 if (dd == NULL) {
4279 dev_err(&pdev->dev,
4280 "Unable to allocate memory for driver data\n");
4281 return -ENOMEM;
4282 }
4283
Sam Bradshaw88523a62011-08-30 08:34:26 -06004284 /* Attach the private data to this PCI device. */
4285 pci_set_drvdata(pdev, dd);
4286
4287 rv = pcim_enable_device(pdev);
4288 if (rv < 0) {
4289 dev_err(&pdev->dev, "Unable to enable device\n");
4290 goto iomap_err;
4291 }
4292
4293 /* Map BAR5 to memory. */
4294 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4295 if (rv < 0) {
4296 dev_err(&pdev->dev, "Unable to map regions\n");
4297 goto iomap_err;
4298 }
4299
4300 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4301 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4302
4303 if (rv) {
4304 rv = pci_set_consistent_dma_mask(pdev,
4305 DMA_BIT_MASK(32));
4306 if (rv) {
4307 dev_warn(&pdev->dev,
4308 "64-bit DMA enable failed\n");
4309 goto setmask_err;
4310 }
4311 }
4312 }
4313
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004314 /* Copy the info we may need later into the private data structure. */
4315 dd->major = mtip_major;
4316 dd->instance = instance;
4317 dd->pdev = pdev;
4318 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004319
Asai Thambi S P0caff002013-04-03 19:56:21 +05304320 INIT_LIST_HEAD(&dd->online_list);
4321 INIT_LIST_HEAD(&dd->remove_list);
4322
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004323 memset(dd->workq_name, 0, 32);
4324 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4325
4326 dd->isr_workq = create_workqueue(dd->workq_name);
4327 if (!dd->isr_workq) {
4328 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004329 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004330 goto block_initialize_err;
4331 }
4332
4333 memset(cpu_list, 0, sizeof(cpu_list));
4334
4335 node_mask = cpumask_of_node(dd->numa_node);
4336 if (!cpumask_empty(node_mask)) {
4337 for_each_cpu(cpu, node_mask)
4338 {
4339 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4340 j = strlen(cpu_list);
4341 }
4342
4343 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4344 dd->numa_node,
4345 topology_physical_package_id(cpumask_first(node_mask)),
4346 nr_cpus_node(dd->numa_node),
4347 cpu_list);
4348 } else
4349 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4350
4351 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4352 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4353 cpu_to_node(dd->isr_binding), dd->isr_binding);
4354
4355 /* first worker context always runs in ISR */
4356 dd->work[0].cpu_binding = dd->isr_binding;
4357 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4358 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4359 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4360 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4361 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4362 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4363 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4364
4365 /* Log the bindings */
4366 for_each_present_cpu(cpu) {
4367 memset(cpu_list, 0, sizeof(cpu_list));
4368 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4369 if (dd->work[i].cpu_binding == cpu) {
4370 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4371 j = strlen(cpu_list);
4372 }
4373 }
4374 if (j)
4375 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4376 }
4377
4378 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4379 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4380 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4381 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4382 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4383 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4384 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4385 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4386
4387 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004388 rv = pci_enable_msi(pdev);
4389 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004390 dev_warn(&pdev->dev,
4391 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004392 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004393 }
4394
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004395 mtip_fix_ero_nosnoop(dd, pdev);
4396
Sam Bradshaw88523a62011-08-30 08:34:26 -06004397 /* Initialize the block layer. */
4398 rv = mtip_block_initialize(dd);
4399 if (rv < 0) {
4400 dev_err(&pdev->dev,
4401 "Unable to initialize block layer\n");
4402 goto block_initialize_err;
4403 }
4404
4405 /*
4406 * Increment the instance count so that each device has a unique
4407 * instance number.
4408 */
4409 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004410 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004411 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304412 else
4413 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304414
4415 /* Add to online list even if in ftl rebuild */
4416 spin_lock_irqsave(&dev_lock, flags);
4417 list_add(&dd->online_list, &online_list);
4418 spin_unlock_irqrestore(&dev_lock, flags);
4419
Sam Bradshaw88523a62011-08-30 08:34:26 -06004420 goto done;
4421
4422block_initialize_err:
4423 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004424
4425msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004426 if (dd->isr_workq) {
4427 flush_workqueue(dd->isr_workq);
4428 destroy_workqueue(dd->isr_workq);
4429 drop_cpu(dd->work[0].cpu_binding);
4430 drop_cpu(dd->work[1].cpu_binding);
4431 drop_cpu(dd->work[2].cpu_binding);
4432 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004433setmask_err:
4434 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4435
4436iomap_err:
4437 kfree(dd);
4438 pci_set_drvdata(pdev, NULL);
4439 return rv;
4440done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004441 return rv;
4442}
4443
4444/*
4445 * Called for each probed device when the device is removed or the
4446 * driver is unloaded.
4447 *
4448 * return value
4449 * None
4450 */
4451static void mtip_pci_remove(struct pci_dev *pdev)
4452{
4453 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004454 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004455
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004456 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004457
Asai Thambi S P0caff002013-04-03 19:56:21 +05304458 spin_lock_irqsave(&dev_lock, flags);
4459 list_del_init(&dd->online_list);
4460 list_add(&dd->remove_list, &removing_list);
4461 spin_unlock_irqrestore(&dev_lock, flags);
4462
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004463 mtip_check_surprise_removal(pdev);
4464 synchronize_irq(dd->pdev->irq);
4465
4466 /* Spin until workers are done */
4467 to = jiffies + msecs_to_jiffies(4000);
4468 do {
4469 msleep(20);
4470 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4471 time_before(jiffies, to));
4472
4473 if (atomic_read(&dd->irq_workers_active) != 0) {
4474 dev_warn(&dd->pdev->dev,
4475 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004476 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004477
4478 /* Clean up the block layer. */
4479 mtip_block_remove(dd);
4480
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004481 if (dd->isr_workq) {
4482 flush_workqueue(dd->isr_workq);
4483 destroy_workqueue(dd->isr_workq);
4484 drop_cpu(dd->work[0].cpu_binding);
4485 drop_cpu(dd->work[1].cpu_binding);
4486 drop_cpu(dd->work[2].cpu_binding);
4487 }
4488
Sam Bradshaw88523a62011-08-30 08:34:26 -06004489 pci_disable_msi(pdev);
4490
Asai Thambi S P0caff002013-04-03 19:56:21 +05304491 spin_lock_irqsave(&dev_lock, flags);
4492 list_del_init(&dd->remove_list);
4493 spin_unlock_irqrestore(&dev_lock, flags);
4494
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004495 if (!dd->sr)
4496 kfree(dd);
4497 else
4498 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4499
Sam Bradshaw88523a62011-08-30 08:34:26 -06004500 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004501 pci_set_drvdata(pdev, NULL);
4502 pci_dev_put(pdev);
4503
Sam Bradshaw88523a62011-08-30 08:34:26 -06004504}
4505
4506/*
4507 * Called for each probed device when the device is suspended.
4508 *
4509 * return value
4510 * 0 Success
4511 * <0 Error
4512 */
4513static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4514{
4515 int rv = 0;
4516 struct driver_data *dd = pci_get_drvdata(pdev);
4517
4518 if (!dd) {
4519 dev_err(&pdev->dev,
4520 "Driver private datastructure is NULL\n");
4521 return -EFAULT;
4522 }
4523
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004524 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004525
4526 /* Disable ports & interrupts then send standby immediate */
4527 rv = mtip_block_suspend(dd);
4528 if (rv < 0) {
4529 dev_err(&pdev->dev,
4530 "Failed to suspend controller\n");
4531 return rv;
4532 }
4533
4534 /*
4535 * Save the pci config space to pdev structure &
4536 * disable the device
4537 */
4538 pci_save_state(pdev);
4539 pci_disable_device(pdev);
4540
4541 /* Move to Low power state*/
4542 pci_set_power_state(pdev, PCI_D3hot);
4543
4544 return rv;
4545}
4546
4547/*
4548 * Called for each probed device when the device is resumed.
4549 *
4550 * return value
4551 * 0 Success
4552 * <0 Error
4553 */
4554static int mtip_pci_resume(struct pci_dev *pdev)
4555{
4556 int rv = 0;
4557 struct driver_data *dd;
4558
4559 dd = pci_get_drvdata(pdev);
4560 if (!dd) {
4561 dev_err(&pdev->dev,
4562 "Driver private datastructure is NULL\n");
4563 return -EFAULT;
4564 }
4565
4566 /* Move the device to active State */
4567 pci_set_power_state(pdev, PCI_D0);
4568
4569 /* Restore PCI configuration space */
4570 pci_restore_state(pdev);
4571
4572 /* Enable the PCI device*/
4573 rv = pcim_enable_device(pdev);
4574 if (rv < 0) {
4575 dev_err(&pdev->dev,
4576 "Failed to enable card during resume\n");
4577 goto err;
4578 }
4579 pci_set_master(pdev);
4580
4581 /*
4582 * Calls hbaReset, initPort, & startPort function
4583 * then enables interrupts
4584 */
4585 rv = mtip_block_resume(dd);
4586 if (rv < 0)
4587 dev_err(&pdev->dev, "Unable to resume\n");
4588
4589err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004590 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004591
4592 return rv;
4593}
4594
4595/*
4596 * Shutdown routine
4597 *
4598 * return value
4599 * None
4600 */
4601static void mtip_pci_shutdown(struct pci_dev *pdev)
4602{
4603 struct driver_data *dd = pci_get_drvdata(pdev);
4604 if (dd)
4605 mtip_block_shutdown(dd);
4606}
4607
Sam Bradshaw88523a62011-08-30 08:34:26 -06004608/* Table of device ids supported by this driver. */
4609static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304610 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4611 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4612 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4613 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4614 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4615 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4616 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004617 { 0 }
4618};
4619
4620/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004621static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004622 .name = MTIP_DRV_NAME,
4623 .id_table = mtip_pci_tbl,
4624 .probe = mtip_pci_probe,
4625 .remove = mtip_pci_remove,
4626 .suspend = mtip_pci_suspend,
4627 .resume = mtip_pci_resume,
4628 .shutdown = mtip_pci_shutdown,
4629};
4630
4631MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4632
4633/*
4634 * Module initialization function.
4635 *
4636 * Called once when the module is loaded. This function allocates a major
4637 * block device number to the Cyclone devices and registers the PCI layer
4638 * of the driver.
4639 *
4640 * Return value
4641 * 0 on success else error code.
4642 */
4643static int __init mtip_init(void)
4644{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004645 int error;
4646
Asai Thambi S P45422e72012-09-05 22:03:56 +05304647 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004648
Asai Thambi S P0caff002013-04-03 19:56:21 +05304649 spin_lock_init(&dev_lock);
4650
4651 INIT_LIST_HEAD(&online_list);
4652 INIT_LIST_HEAD(&removing_list);
4653
Sam Bradshaw88523a62011-08-30 08:34:26 -06004654 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004655 error = register_blkdev(0, MTIP_DRV_NAME);
4656 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304657 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004658 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004659 return -EBUSY;
4660 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004661 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004662
Asai Thambi S P0caff002013-04-03 19:56:21 +05304663 dfs_parent = debugfs_create_dir("rssd", NULL);
4664 if (IS_ERR_OR_NULL(dfs_parent)) {
4665 pr_warn("Error creating debugfs parent\n");
4666 dfs_parent = NULL;
4667 }
4668 if (dfs_parent) {
4669 dfs_device_status = debugfs_create_file("device_status",
4670 S_IRUGO, dfs_parent, NULL,
4671 &mtip_device_status_fops);
4672 if (IS_ERR_OR_NULL(dfs_device_status)) {
4673 pr_err("Error creating device_status node\n");
4674 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004675 }
4676 }
4677
Sam Bradshaw88523a62011-08-30 08:34:26 -06004678 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004679 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004680 if (error) {
4681 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004682 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004683 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004684
4685 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004686}
4687
4688/*
4689 * Module de-initialization function.
4690 *
4691 * Called once when the module is unloaded. This function deallocates
4692 * the major block device number allocated by mtip_init() and
4693 * unregisters the PCI layer of the driver.
4694 *
4695 * Return value
4696 * none
4697 */
4698static void __exit mtip_exit(void)
4699{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004700 /* Release the allocated major block device number. */
4701 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4702
4703 /* Unregister the PCI driver. */
4704 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004705
4706 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004707}
4708
4709MODULE_AUTHOR("Micron Technology, Inc");
4710MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4711MODULE_LICENSE("GPL");
4712MODULE_VERSION(MTIP_DRV_VERSION);
4713
4714module_init(mtip_init);
4715module_exit(mtip_exit);