blob: ae331ab4a451588d62967264491a15125a8777e7 [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
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700623 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
624
625 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
626 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600627 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700628 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
629
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700630 if (cmd->comp_data && cmd->comp_func) {
631 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600632 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700633 }
634 goto handle_tfe_exit;
635 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600636
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200637 /* clear the tag accumulator */
638 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
639
Sam Bradshaw88523a62011-08-30 08:34:26 -0600640 /* Loop through all the groups */
641 for (group = 0; group < dd->slot_groups; group++) {
642 completed = readl(port->completed[group]);
643
Jens Axboeffc771b2014-05-09 09:42:02 -0600644 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
645
Sam Bradshaw88523a62011-08-30 08:34:26 -0600646 /* clear completed status register in the hardware.*/
647 writel(completed, port->completed[group]);
648
Sam Bradshaw88523a62011-08-30 08:34:26 -0600649 /* Process successfully completed commands */
650 for (bit = 0; bit < 32 && completed; bit++) {
651 if (!(completed & (1<<bit)))
652 continue;
653 tag = (group << 5) + bit;
654
655 /* Skip the internal command slot */
656 if (tag == MTIP_TAG_INTERNAL)
657 continue;
658
Jens Axboeffc771b2014-05-09 09:42:02 -0600659 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200660 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600661 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200662 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600663 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 } else {
665 dev_err(&port->dd->pdev->dev,
666 "Missing completion func for tag %d",
667 tag);
668 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600669 /* don't proceed further */
670 return;
671 }
672 }
673 }
674 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200675
676 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600677
678 /* Restart the port */
679 mdelay(20);
680 mtip_restart_port(port);
681
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200682 /* Trying to determine the cause of the error */
683 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
684 dd->port->log_buf,
685 dd->port->log_buf_dma, 1);
686 if (rv) {
687 dev_warn(&dd->pdev->dev,
688 "Error in READ LOG EXT (10h) command\n");
689 /* non-critical error, don't fail the load */
690 } else {
691 buf = (unsigned char *)dd->port->log_buf;
692 if (buf[259] & 0x1) {
693 dev_info(&dd->pdev->dev,
694 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200695 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200696 fail_all_ncq_write = 1;
697 fail_reason = "write protect";
698 }
699 if (buf[288] == 0xF7) {
700 dev_info(&dd->pdev->dev,
701 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200702 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200703 fail_all_ncq_cmds = 1;
704 fail_reason = "thermal shutdown";
705 }
706 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700707 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200708 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700709 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200710 fail_all_ncq_cmds = 1;
711 fail_reason = "rebuild failed";
712 }
713 }
714
Sam Bradshaw88523a62011-08-30 08:34:26 -0600715 /* clear the tag accumulator */
716 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
717
718 /* Loop through all the groups */
719 for (group = 0; group < dd->slot_groups; group++) {
720 for (bit = 0; bit < 32; bit++) {
721 reissue = 1;
722 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600723 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600724
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200725 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600726
727 /* Should re-issue? */
728 if (tag == MTIP_TAG_INTERNAL ||
729 fis->command == ATA_CMD_SET_FEATURES)
730 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200731 else {
732 if (fail_all_ncq_cmds ||
733 (fail_all_ncq_write &&
734 fis->command == ATA_CMD_FPDMA_WRITE)) {
735 dev_warn(&dd->pdev->dev,
736 " Fail: %s w/tag %d [%s].\n",
737 fis->command == ATA_CMD_FPDMA_WRITE ?
738 "write" : "read",
739 tag,
740 fail_reason != NULL ?
741 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200742 if (cmd->comp_func) {
743 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600744 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200745 }
746 continue;
747 }
748 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600749
750 /*
751 * First check if this command has
752 * exceeded its retries.
753 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200754 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600755
756 set_bit(tag, tagaccum);
757
Sam Bradshaw88523a62011-08-30 08:34:26 -0600758 /* Re-issue the command. */
759 mtip_issue_ncq_command(port, tag);
760
761 continue;
762 }
763
764 /* Retire a command that will not be reissued */
765 dev_warn(&port->dd->pdev->dev,
766 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600767
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200768 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600769 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600770 else
771 dev_warn(&port->dd->pdev->dev,
772 "Bad completion for tag %d\n",
773 tag);
774 }
775 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200776 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600777
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700778handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100779 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200780 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100781 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600782}
783
784/*
785 * Handle a set device bits interrupt
786 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800787static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
788 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600789{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800790 struct driver_data *dd = port->dd;
791 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600792 struct mtip_cmd *command;
793
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800794 if (!completed) {
795 WARN_ON_ONCE(!completed);
796 return;
797 }
798 /* clear completed status register in the hardware.*/
799 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600800
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800801 /* Process completed commands. */
802 for (bit = 0; (bit < 32) && completed; bit++) {
803 if (completed & 0x01) {
804 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600805
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800806 /* skip internal command slot. */
807 if (unlikely(tag == MTIP_TAG_INTERNAL))
808 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600809
Jens Axboeffc771b2014-05-09 09:42:02 -0600810 command = mtip_cmd_from_tag(dd, tag);
811 if (likely(command->comp_func))
812 command->comp_func(port, tag, command, 0);
813 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600814 dev_dbg(&dd->pdev->dev,
815 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800816 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600817
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800818 if (mtip_check_surprise_removal(
819 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800820 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600821 }
822 }
823 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800824 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600825 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800826
827 /* If last, re-enable interrupts */
828 if (atomic_dec_return(&dd->irq_workers_active) == 0)
829 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600830}
831
832/*
833 * Process legacy pio and d2h interrupts
834 */
835static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
836{
837 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600838 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600839
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200840 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100841 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600842 & (1 << MTIP_TAG_INTERNAL))) {
843 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600844 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600845 return;
846 }
847 }
848
Sam Bradshaw88523a62011-08-30 08:34:26 -0600849 return;
850}
851
852/*
853 * Demux and handle errors
854 */
855static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
856{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600857
858 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
859 dev_warn(&dd->pdev->dev,
860 "Clearing PxSERR.DIAG.x\n");
861 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
862 }
863
864 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
865 dev_warn(&dd->pdev->dev,
866 "Clearing PxSERR.DIAG.n\n");
867 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
868 }
869
870 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
871 dev_warn(&dd->pdev->dev,
872 "Port stat errors %x unhandled\n",
873 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700874 if (mtip_check_surprise_removal(dd->pdev))
875 return;
876 }
877 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
878 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
879 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600880 }
881}
882
883static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
884{
885 struct driver_data *dd = (struct driver_data *) data;
886 struct mtip_port *port = dd->port;
887 u32 hba_stat, port_stat;
888 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800889 int do_irq_enable = 1, i, workers;
890 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600891
892 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
893 if (hba_stat) {
894 rv = IRQ_HANDLED;
895
896 /* Acknowledge the interrupt status on the port.*/
897 port_stat = readl(port->mmio + PORT_IRQ_STAT);
898 writel(port_stat, port->mmio + PORT_IRQ_STAT);
899
900 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800901 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
902 do_irq_enable = 0;
903 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
904
905 /* Start at 1: group zero is always local? */
906 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
907 i++) {
908 twork = &dd->work[i];
909 twork->completed = readl(port->completed[i]);
910 if (twork->completed)
911 workers++;
912 }
913
914 atomic_set(&dd->irq_workers_active, workers);
915 if (workers) {
916 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
917 twork = &dd->work[i];
918 if (twork->completed)
919 queue_work_on(
920 twork->cpu_binding,
921 dd->isr_workq,
922 &twork->work);
923 }
924
925 if (likely(dd->work[0].completed))
926 mtip_workq_sdbfx(port, 0,
927 dd->work[0].completed);
928
929 } else {
930 /*
931 * Chip quirk: SDB interrupt but nothing
932 * to complete
933 */
934 do_irq_enable = 1;
935 }
936 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600937
938 if (unlikely(port_stat & PORT_IRQ_ERR)) {
939 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600940 /* don't proceed further */
941 return IRQ_HANDLED;
942 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200943 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200944 &dd->dd_flag))
945 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600946
947 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
948 }
949
950 if (unlikely(port_stat & PORT_IRQ_LEGACY))
951 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
952 }
953
954 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800955 if (unlikely(do_irq_enable))
956 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600957
958 return rv;
959}
960
961/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600962 * HBA interrupt subroutine.
963 *
964 * @irq IRQ number.
965 * @instance Pointer to the driver data structure.
966 *
967 * return value
968 * IRQ_HANDLED A HBA interrupt was pending and handled.
969 * IRQ_NONE This interrupt was not for the HBA.
970 */
971static irqreturn_t mtip_irq_handler(int irq, void *instance)
972{
973 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800974
975 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600976}
977
978static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
979{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600980 writel(1 << MTIP_TAG_BIT(tag),
981 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
982}
983
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200984static bool mtip_pause_ncq(struct mtip_port *port,
985 struct host_to_dev_fis *fis)
986{
987 struct host_to_dev_fis *reply;
988 unsigned long task_file_data;
989
990 reply = port->rxfis + RX_FIS_D2H_REG;
991 task_file_data = readl(port->mmio+PORT_TFDATA);
992
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530993 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
994 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
995
996 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200997 return false;
998
999 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1000 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301001 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001002 port->ic_pause_timer = jiffies;
1003 return true;
1004 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1005 (fis->features == 0x03)) {
1006 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1007 port->ic_pause_timer = jiffies;
1008 return true;
1009 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1010 ((fis->command == 0xFC) &&
1011 (fis->features == 0x27 || fis->features == 0x72 ||
1012 fis->features == 0x62 || fis->features == 0x26))) {
1013 /* Com reset after secure erase or lowlevel format */
1014 mtip_restart_port(port);
1015 return false;
1016 }
1017
1018 return false;
1019}
1020
Sam Bradshaw88523a62011-08-30 08:34:26 -06001021/*
1022 * Wait for port to quiesce
1023 *
1024 * @port Pointer to port data structure
1025 * @timeout Max duration to wait (ms)
1026 *
1027 * return value
1028 * 0 Success
1029 * -EBUSY Commands still active
1030 */
1031static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1032{
1033 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001034 unsigned int n;
1035 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001036
Jens Axboe9acf03c2014-05-14 08:22:56 -06001037 blk_mq_stop_hw_queues(port->dd->queue);
1038
Sam Bradshaw88523a62011-08-30 08:34:26 -06001039 to = jiffies + msecs_to_jiffies(timeout);
1040 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001041 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1042 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001043 msleep(20);
1044 continue; /* svc thd is actively issuing commands */
1045 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001046
1047 msleep(100);
1048 if (mtip_check_surprise_removal(port->dd->pdev))
1049 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001050 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001051 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001052
Sam Bradshaw88523a62011-08-30 08:34:26 -06001053 /*
1054 * Ignore s_active bit 0 of array element 0.
1055 * This bit will always be set
1056 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001057 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001058 for (n = 1; n < port->dd->slot_groups; n++)
1059 active |= readl(port->s_active[n]);
1060
1061 if (!active)
1062 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001063 } while (time_before(jiffies, to));
1064
Jens Axboe9acf03c2014-05-14 08:22:56 -06001065 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001066 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001067err_fault:
1068 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1069 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001070}
1071
1072/*
1073 * Execute an internal command and wait for the completion.
1074 *
1075 * @port Pointer to the port data structure.
1076 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001077 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001078 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001079 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001080 * @opts Command header options, excluding the FIS length
1081 * and the number of PRD entries.
1082 * @timeout Time in ms to wait for the command to complete.
1083 *
1084 * return value
1085 * 0 Command completed successfully.
1086 * -EFAULT The buffer address is not correctly aligned.
1087 * -EBUSY Internal command or other IO in progress.
1088 * -EAGAIN Time out waiting for command to complete.
1089 */
1090static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001091 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001092 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001093 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001094 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001095 u32 opts,
1096 gfp_t atomic,
1097 unsigned long timeout)
1098{
1099 struct mtip_cmd_sg *command_sg;
1100 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001101 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301102 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001103 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001104
1105 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1106 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301107 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001108 return -EFAULT;
1109 }
1110
Jens Axboeffc771b2014-05-09 09:42:02 -06001111 int_cmd = mtip_get_int_command(dd);
1112
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001113 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001114 port->ic_pause_timer = 0;
1115
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301116 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1117 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001118
1119 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001120 if (fis->command != ATA_CMD_STANDBYNOW1) {
1121 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001122 if (mtip_quiesce_io(port,
1123 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301124 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001125 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001126 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001127 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001128 wake_up_interruptible(&port->svc_wait);
1129 return -EBUSY;
1130 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001131 }
1132
1133 /* Set the completion function and data for the command. */
1134 int_cmd->comp_data = &wait;
1135 int_cmd->comp_func = mtip_completion;
1136
1137 } else {
1138 /* Clear completion - we're going to poll */
1139 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001140 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001141 }
1142
1143 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001144 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001145
1146 /* Populate the SG list */
1147 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001148 __force_bit2int cpu_to_le32(opts | fis_len);
1149 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001150 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1151
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001152 command_sg->info =
1153 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1154 command_sg->dba =
1155 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1156 command_sg->dba_upper =
1157 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001158
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001159 int_cmd->command_header->opts |=
1160 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001161 }
1162
1163 /* Populate the command header */
1164 int_cmd->command_header->byte_count = 0;
1165
1166 /* Issue the command to the hardware */
1167 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1168
Sam Bradshaw88523a62011-08-30 08:34:26 -06001169 if (atomic == GFP_KERNEL) {
1170 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001171 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001172 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001173 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301174 if (rv == -ERESTARTSYS) { /* interrupted */
1175 dev_err(&dd->pdev->dev,
1176 "Internal command [%02X] was interrupted after %lu ms\n",
1177 fis->command, timeout);
1178 rv = -EINTR;
1179 goto exec_ic_exit;
1180 } else if (rv == 0) /* timeout */
1181 dev_err(&dd->pdev->dev,
1182 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1183 fis->command, timeout);
1184 else
1185 dev_err(&dd->pdev->dev,
1186 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1187 fis->command, rv, timeout);
1188
1189 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001190 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301191 &dd->dd_flag)) {
1192 dev_err(&dd->pdev->dev,
1193 "Internal command [%02X] wait returned due to SR\n",
1194 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001195 rv = -ENXIO;
1196 goto exec_ic_exit;
1197 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301198 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001199 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301200 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001201 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001202 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301203 u32 hba_stat, port_stat;
1204
Sam Bradshaw88523a62011-08-30 08:34:26 -06001205 /* Spin for <timeout> checking if command still outstanding */
1206 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001207 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1208 & (1 << MTIP_TAG_INTERNAL))
1209 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301210 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001211 rv = -ENXIO;
1212 goto exec_ic_exit;
1213 }
1214 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001215 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301216 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001217 rv = -ENXIO;
1218 goto exec_ic_exit;
1219 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301220 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1221 if (!port_stat)
1222 continue;
1223
1224 if (port_stat & PORT_IRQ_ERR) {
1225 dev_err(&dd->pdev->dev,
1226 "Internal command [%02X] failed\n",
1227 fis->command);
1228 mtip_device_reset(dd);
1229 rv = -EIO;
1230 goto exec_ic_exit;
1231 } else {
1232 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1233 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1234 if (hba_stat)
1235 writel(hba_stat,
1236 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001237 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301238 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001239 }
1240 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001241
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001242 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1243 & (1 << MTIP_TAG_INTERNAL)) {
1244 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301245 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1246 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001247 rv = -EAGAIN;
1248 }
1249 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001250exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001251 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001252 mtip_put_int_command(dd, int_cmd);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001253 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1254 /* NCQ paused */
1255 return rv;
1256 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001257 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001258 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001259
1260 return rv;
1261}
1262
1263/*
1264 * Byte-swap ATA ID strings.
1265 *
1266 * ATA identify data contains strings in byte-swapped 16-bit words.
1267 * They must be swapped (on all architectures) to be usable as C strings.
1268 * This function swaps bytes in-place.
1269 *
1270 * @buf The buffer location of the string
1271 * @len The number of bytes to swap
1272 *
1273 * return value
1274 * None
1275 */
1276static inline void ata_swap_string(u16 *buf, unsigned int len)
1277{
1278 int i;
1279 for (i = 0; i < (len/2); i++)
1280 be16_to_cpus(&buf[i]);
1281}
1282
Asai Thambi S P670a6412014-04-16 20:27:54 -07001283static void mtip_set_timeout(struct driver_data *dd,
1284 struct host_to_dev_fis *fis,
1285 unsigned int *timeout, u8 erasemode)
1286{
1287 switch (fis->command) {
1288 case ATA_CMD_DOWNLOAD_MICRO:
1289 *timeout = 120000; /* 2 minutes */
1290 break;
1291 case ATA_CMD_SEC_ERASE_UNIT:
1292 case 0xFC:
1293 if (erasemode)
1294 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1295 else
1296 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1297 break;
1298 case ATA_CMD_STANDBYNOW1:
1299 *timeout = 120000; /* 2 minutes */
1300 break;
1301 case 0xF7:
1302 case 0xFA:
1303 *timeout = 60000; /* 60 seconds */
1304 break;
1305 case ATA_CMD_SMART:
1306 *timeout = 15000; /* 15 seconds */
1307 break;
1308 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001309 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001310 break;
1311 }
1312}
1313
Sam Bradshaw88523a62011-08-30 08:34:26 -06001314/*
1315 * Request the device identity information.
1316 *
1317 * If a user space buffer is not specified, i.e. is NULL, the
1318 * identify information is still read from the drive and placed
1319 * into the identify data buffer (@e port->identify) in the
1320 * port data structure.
1321 * When the identify buffer contains valid identify information @e
1322 * port->identify_valid is non-zero.
1323 *
1324 * @port Pointer to the port structure.
1325 * @user_buffer A user space buffer where the identify data should be
1326 * copied.
1327 *
1328 * return value
1329 * 0 Command completed successfully.
1330 * -EFAULT An error occurred while coping data to the user buffer.
1331 * -1 Command failed.
1332 */
1333static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1334{
1335 int rv = 0;
1336 struct host_to_dev_fis fis;
1337
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001338 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001339 return -EFAULT;
1340
Sam Bradshaw88523a62011-08-30 08:34:26 -06001341 /* Build the FIS. */
1342 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1343 fis.type = 0x27;
1344 fis.opts = 1 << 7;
1345 fis.command = ATA_CMD_ID_ATA;
1346
1347 /* Set the identify information as invalid. */
1348 port->identify_valid = 0;
1349
1350 /* Clear the identify information. */
1351 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1352
1353 /* Execute the command. */
1354 if (mtip_exec_internal_command(port,
1355 &fis,
1356 5,
1357 port->identify_dma,
1358 sizeof(u16) * ATA_ID_WORDS,
1359 0,
1360 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001361 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001362 < 0) {
1363 rv = -1;
1364 goto out;
1365 }
1366
1367 /*
1368 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1369 * perform field-sensitive swapping on the string fields.
1370 * See the kernel use of ata_id_string() for proof of this.
1371 */
1372#ifdef __LITTLE_ENDIAN
1373 ata_swap_string(port->identify + 27, 40); /* model string*/
1374 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1375 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1376#else
1377 {
1378 int i;
1379 for (i = 0; i < ATA_ID_WORDS; i++)
1380 port->identify[i] = le16_to_cpu(port->identify[i]);
1381 }
1382#endif
1383
Sam Bradshaw26d58052013-10-03 10:18:05 -07001384 /* Check security locked state */
1385 if (port->identify[128] & 0x4)
1386 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1387 else
1388 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1389
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301390#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001391 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1392 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1393 port->dd->trim_supp = true;
1394 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301395#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001396 port->dd->trim_supp = false;
1397
Sam Bradshaw88523a62011-08-30 08:34:26 -06001398 /* Set the identify buffer as valid. */
1399 port->identify_valid = 1;
1400
1401 if (user_buffer) {
1402 if (copy_to_user(
1403 user_buffer,
1404 port->identify,
1405 ATA_ID_WORDS * sizeof(u16))) {
1406 rv = -EFAULT;
1407 goto out;
1408 }
1409 }
1410
1411out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001412 return rv;
1413}
1414
1415/*
1416 * Issue a standby immediate command to the device.
1417 *
1418 * @port Pointer to the port structure.
1419 *
1420 * return value
1421 * 0 Command was executed successfully.
1422 * -1 An error occurred while executing the command.
1423 */
1424static int mtip_standby_immediate(struct mtip_port *port)
1425{
1426 int rv;
1427 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001428 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001429 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001430
Sam Bradshaw88523a62011-08-30 08:34:26 -06001431 /* Build the FIS. */
1432 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1433 fis.type = 0x27;
1434 fis.opts = 1 << 7;
1435 fis.command = ATA_CMD_STANDBYNOW1;
1436
Asai Thambi S P670a6412014-04-16 20:27:54 -07001437 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1438
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001439 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001440 rv = mtip_exec_internal_command(port,
1441 &fis,
1442 5,
1443 0,
1444 0,
1445 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001446 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001447 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001448 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1449 jiffies_to_msecs(jiffies - start));
1450 if (rv)
1451 dev_warn(&port->dd->pdev->dev,
1452 "STANDBY IMMEDIATE command failed.\n");
1453
1454 return rv;
1455}
1456
1457/*
1458 * Issue a READ LOG EXT command to the device.
1459 *
1460 * @port pointer to the port structure.
1461 * @page page number to fetch
1462 * @buffer pointer to buffer
1463 * @buffer_dma dma address corresponding to @buffer
1464 * @sectors page length to fetch, in sectors
1465 *
1466 * return value
1467 * @rv return value from mtip_exec_internal_command()
1468 */
1469static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1470 dma_addr_t buffer_dma, unsigned int sectors)
1471{
1472 struct host_to_dev_fis fis;
1473
1474 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1475 fis.type = 0x27;
1476 fis.opts = 1 << 7;
1477 fis.command = ATA_CMD_READ_LOG_EXT;
1478 fis.sect_count = sectors & 0xFF;
1479 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1480 fis.lba_low = page;
1481 fis.lba_mid = 0;
1482 fis.device = ATA_DEVICE_OBS;
1483
1484 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1485
1486 return mtip_exec_internal_command(port,
1487 &fis,
1488 5,
1489 buffer_dma,
1490 sectors * ATA_SECT_SIZE,
1491 0,
1492 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001493 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001494}
1495
1496/*
1497 * Issue a SMART READ DATA command to the device.
1498 *
1499 * @port pointer to the port structure.
1500 * @buffer pointer to buffer
1501 * @buffer_dma dma address corresponding to @buffer
1502 *
1503 * return value
1504 * @rv return value from mtip_exec_internal_command()
1505 */
1506static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1507 dma_addr_t buffer_dma)
1508{
1509 struct host_to_dev_fis fis;
1510
1511 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1512 fis.type = 0x27;
1513 fis.opts = 1 << 7;
1514 fis.command = ATA_CMD_SMART;
1515 fis.features = 0xD0;
1516 fis.sect_count = 1;
1517 fis.lba_mid = 0x4F;
1518 fis.lba_hi = 0xC2;
1519 fis.device = ATA_DEVICE_OBS;
1520
1521 return mtip_exec_internal_command(port,
1522 &fis,
1523 5,
1524 buffer_dma,
1525 ATA_SECT_SIZE,
1526 0,
1527 GFP_ATOMIC,
1528 15000);
1529}
1530
1531/*
1532 * Get the value of a smart attribute
1533 *
1534 * @port pointer to the port structure
1535 * @id attribute number
1536 * @attrib pointer to return attrib information corresponding to @id
1537 *
1538 * return value
1539 * -EINVAL NULL buffer passed or unsupported attribute @id.
1540 * -EPERM Identify data not valid, SMART not supported or not enabled
1541 */
1542static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1543 struct smart_attr *attrib)
1544{
1545 int rv, i;
1546 struct smart_attr *pattr;
1547
1548 if (!attrib)
1549 return -EINVAL;
1550
1551 if (!port->identify_valid) {
1552 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1553 return -EPERM;
1554 }
1555 if (!(port->identify[82] & 0x1)) {
1556 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1557 return -EPERM;
1558 }
1559 if (!(port->identify[85] & 0x1)) {
1560 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1561 return -EPERM;
1562 }
1563
1564 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1565 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1566 if (rv) {
1567 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1568 return rv;
1569 }
1570
1571 pattr = (struct smart_attr *)(port->smart_buf + 2);
1572 for (i = 0; i < 29; i++, pattr++)
1573 if (pattr->attr_id == id) {
1574 memcpy(attrib, pattr, sizeof(struct smart_attr));
1575 break;
1576 }
1577
1578 if (i == 29) {
1579 dev_warn(&port->dd->pdev->dev,
1580 "Query for invalid SMART attribute ID\n");
1581 rv = -EINVAL;
1582 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001583
Sam Bradshaw88523a62011-08-30 08:34:26 -06001584 return rv;
1585}
1586
1587/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001588 * Trim unused sectors
1589 *
1590 * @dd pointer to driver_data structure
1591 * @lba starting lba
1592 * @len # of 512b sectors to trim
1593 *
1594 * return value
1595 * -ENOMEM Out of dma memory
1596 * -EINVAL Invalid parameters passed in, trim not supported
1597 * -EIO Error submitting trim request to hw
1598 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301599static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1600 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001601{
1602 int i, rv = 0;
1603 u64 tlba, tlen, sect_left;
1604 struct mtip_trim_entry *buf;
1605 dma_addr_t dma_addr;
1606 struct host_to_dev_fis fis;
1607
1608 if (!len || dd->trim_supp == false)
1609 return -EINVAL;
1610
1611 /* Trim request too big */
1612 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1613
1614 /* Trim request not aligned on 4k boundary */
1615 WARN_ON(len % 8 != 0);
1616
1617 /* Warn if vu_trim structure is too big */
1618 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1619
1620 /* Allocate a DMA buffer for the trim structure */
1621 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1622 GFP_KERNEL);
1623 if (!buf)
1624 return -ENOMEM;
1625 memset(buf, 0, ATA_SECT_SIZE);
1626
1627 for (i = 0, sect_left = len, tlba = lba;
1628 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1629 i++) {
1630 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1631 MTIP_MAX_TRIM_ENTRY_LEN :
1632 sect_left);
1633 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1634 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1635 tlba += tlen;
1636 sect_left -= tlen;
1637 }
1638 WARN_ON(sect_left != 0);
1639
1640 /* Build the fis */
1641 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1642 fis.type = 0x27;
1643 fis.opts = 1 << 7;
1644 fis.command = 0xfb;
1645 fis.features = 0x60;
1646 fis.sect_count = 1;
1647 fis.device = ATA_DEVICE_OBS;
1648
1649 if (mtip_exec_internal_command(dd->port,
1650 &fis,
1651 5,
1652 dma_addr,
1653 ATA_SECT_SIZE,
1654 0,
1655 GFP_KERNEL,
1656 MTIP_TRIM_TIMEOUT_MS) < 0)
1657 rv = -EIO;
1658
1659 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1660 return rv;
1661}
1662
1663/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001664 * Get the drive capacity.
1665 *
1666 * @dd Pointer to the device data structure.
1667 * @sectors Pointer to the variable that will receive the sector count.
1668 *
1669 * return value
1670 * 1 Capacity was returned successfully.
1671 * 0 The identify information is invalid.
1672 */
Jens Axboe63166682011-09-27 21:27:43 -06001673static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001674{
1675 struct mtip_port *port = dd->port;
1676 u64 total, raw0, raw1, raw2, raw3;
1677 raw0 = port->identify[100];
1678 raw1 = port->identify[101];
1679 raw2 = port->identify[102];
1680 raw3 = port->identify[103];
1681 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1682 *sectors = total;
1683 return (bool) !!port->identify_valid;
1684}
1685
1686/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001687 * Display the identify command data.
1688 *
1689 * @port Pointer to the port data structure.
1690 *
1691 * return value
1692 * None
1693 */
1694static void mtip_dump_identify(struct mtip_port *port)
1695{
1696 sector_t sectors;
1697 unsigned short revid;
1698 char cbuf[42];
1699
1700 if (!port->identify_valid)
1701 return;
1702
1703 strlcpy(cbuf, (char *)(port->identify+10), 21);
1704 dev_info(&port->dd->pdev->dev,
1705 "Serial No.: %s\n", cbuf);
1706
1707 strlcpy(cbuf, (char *)(port->identify+23), 9);
1708 dev_info(&port->dd->pdev->dev,
1709 "Firmware Ver.: %s\n", cbuf);
1710
1711 strlcpy(cbuf, (char *)(port->identify+27), 41);
1712 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1713
Sam Bradshaw26d58052013-10-03 10:18:05 -07001714 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1715 port->identify[128],
1716 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1717
Sam Bradshaw88523a62011-08-30 08:34:26 -06001718 if (mtip_hw_get_capacity(port->dd, &sectors))
1719 dev_info(&port->dd->pdev->dev,
1720 "Capacity: %llu sectors (%llu MB)\n",
1721 (u64)sectors,
1722 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1723
1724 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001725 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001726 case 0x1:
1727 strlcpy(cbuf, "A0", 3);
1728 break;
1729 case 0x3:
1730 strlcpy(cbuf, "A2", 3);
1731 break;
1732 default:
1733 strlcpy(cbuf, "?", 2);
1734 break;
1735 }
1736 dev_info(&port->dd->pdev->dev,
1737 "Card Type: %s\n", cbuf);
1738}
1739
1740/*
1741 * Map the commands scatter list into the command table.
1742 *
1743 * @command Pointer to the command.
1744 * @nents Number of scatter list entries.
1745 *
1746 * return value
1747 * None
1748 */
1749static inline void fill_command_sg(struct driver_data *dd,
1750 struct mtip_cmd *command,
1751 int nents)
1752{
1753 int n;
1754 unsigned int dma_len;
1755 struct mtip_cmd_sg *command_sg;
1756 struct scatterlist *sg = command->sg;
1757
1758 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1759
1760 for (n = 0; n < nents; n++) {
1761 dma_len = sg_dma_len(sg);
1762 if (dma_len > 0x400000)
1763 dev_err(&dd->pdev->dev,
1764 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001765 command_sg->info = __force_bit2int
1766 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1767 command_sg->dba = __force_bit2int
1768 cpu_to_le32(sg_dma_address(sg));
1769 command_sg->dba_upper = __force_bit2int
1770 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001771 command_sg++;
1772 sg++;
1773 }
1774}
1775
1776/*
1777 * @brief Execute a drive command.
1778 *
1779 * return value 0 The command completed successfully.
1780 * return value -1 An error occurred while executing the command.
1781 */
Jens Axboe63166682011-09-27 21:27:43 -06001782static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001783{
1784 struct host_to_dev_fis fis;
1785 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001786 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001787
Sam Bradshaw88523a62011-08-30 08:34:26 -06001788 /* Build the FIS. */
1789 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1790 fis.type = 0x27;
1791 fis.opts = 1 << 7;
1792 fis.command = command[0];
1793 fis.features = command[1];
1794 fis.sect_count = command[2];
1795 fis.sector = command[3];
1796 fis.cyl_low = command[4];
1797 fis.cyl_hi = command[5];
1798 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1799
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001800 mtip_set_timeout(port->dd, &fis, &to, 0);
1801
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001802 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 -06001803 __func__,
1804 command[0],
1805 command[1],
1806 command[2],
1807 command[3],
1808 command[4],
1809 command[5],
1810 command[6]);
1811
1812 /* Execute the command. */
1813 if (mtip_exec_internal_command(port,
1814 &fis,
1815 5,
1816 0,
1817 0,
1818 0,
1819 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001820 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001821 return -1;
1822 }
1823
1824 command[0] = reply->command; /* Status*/
1825 command[1] = reply->features; /* Error*/
1826 command[4] = reply->cyl_low;
1827 command[5] = reply->cyl_hi;
1828
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001829 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 -06001830 __func__,
1831 command[0],
1832 command[1],
1833 command[4],
1834 command[5]);
1835
Sam Bradshaw88523a62011-08-30 08:34:26 -06001836 return 0;
1837}
1838
1839/*
1840 * @brief Execute a drive command.
1841 *
1842 * @param port Pointer to the port data structure.
1843 * @param command Pointer to the user specified command parameters.
1844 * @param user_buffer Pointer to the user space buffer where read sector
1845 * data should be copied.
1846 *
1847 * return value 0 The command completed successfully.
1848 * return value -EFAULT An error occurred while copying the completion
1849 * data to the user space buffer.
1850 * return value -1 An error occurred while executing the command.
1851 */
Jens Axboe63166682011-09-27 21:27:43 -06001852static int exec_drive_command(struct mtip_port *port, u8 *command,
1853 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001854{
1855 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001856 struct host_to_dev_fis *reply;
1857 u8 *buf = NULL;
1858 dma_addr_t dma_addr = 0;
1859 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001860 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001861
1862 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001863 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001864 return -EFAULT;
1865
1866 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1867 ATA_SECT_SIZE * xfer_sz,
1868 &dma_addr,
1869 GFP_KERNEL);
1870 if (!buf) {
1871 dev_err(&port->dd->pdev->dev,
1872 "Memory allocation failed (%d bytes)\n",
1873 ATA_SECT_SIZE * xfer_sz);
1874 return -ENOMEM;
1875 }
1876 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1877 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001878
Sam Bradshaw88523a62011-08-30 08:34:26 -06001879 /* Build the FIS. */
1880 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001881 fis.type = 0x27;
1882 fis.opts = 1 << 7;
1883 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001884 fis.features = command[2];
1885 fis.sect_count = command[3];
1886 if (fis.command == ATA_CMD_SMART) {
1887 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001888 fis.cyl_low = 0x4F;
1889 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001890 }
1891
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001892 mtip_set_timeout(port->dd, &fis, &to, 0);
1893
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001894 if (xfer_sz)
1895 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1896 else
1897 reply = (port->rxfis + RX_FIS_D2H_REG);
1898
Sam Bradshaw88523a62011-08-30 08:34:26 -06001899 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001900 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001901 "feat %x, sectcnt %x\n",
1902 __func__,
1903 command[0],
1904 command[1],
1905 command[2],
1906 command[3]);
1907
Sam Bradshaw88523a62011-08-30 08:34:26 -06001908 /* Execute the command. */
1909 if (mtip_exec_internal_command(port,
1910 &fis,
1911 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001912 (xfer_sz ? dma_addr : 0),
1913 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001914 0,
1915 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001916 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001917 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001918 rv = -EFAULT;
1919 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001920 }
1921
1922 /* Collect the completion status. */
1923 command[0] = reply->command; /* Status*/
1924 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001925 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001926
1927 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001928 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001929 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001930 __func__,
1931 command[0],
1932 command[1],
1933 command[2]);
1934
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001935 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001936 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001937 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001938 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001939 rv = -EFAULT;
1940 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001941 }
1942 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001943exit_drive_command:
1944 if (buf)
1945 dmam_free_coherent(&port->dd->pdev->dev,
1946 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1947 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001948}
1949
1950/*
1951 * Indicates whether a command has a single sector payload.
1952 *
1953 * @command passed to the device to perform the certain event.
1954 * @features passed to the device to perform the certain event.
1955 *
1956 * return value
1957 * 1 command is one that always has a single sector payload,
1958 * regardless of the value in the Sector Count field.
1959 * 0 otherwise
1960 *
1961 */
1962static unsigned int implicit_sector(unsigned char command,
1963 unsigned char features)
1964{
1965 unsigned int rv = 0;
1966
1967 /* list of commands that have an implicit sector count of 1 */
1968 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001969 case ATA_CMD_SEC_SET_PASS:
1970 case ATA_CMD_SEC_UNLOCK:
1971 case ATA_CMD_SEC_ERASE_PREP:
1972 case ATA_CMD_SEC_ERASE_UNIT:
1973 case ATA_CMD_SEC_FREEZE_LOCK:
1974 case ATA_CMD_SEC_DISABLE_PASS:
1975 case ATA_CMD_PMP_READ:
1976 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001977 rv = 1;
1978 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001979 case ATA_CMD_SET_MAX:
1980 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001981 rv = 1;
1982 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001983 case ATA_CMD_SMART:
1984 if ((features == ATA_SMART_READ_VALUES) ||
1985 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001986 rv = 1;
1987 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001988 case ATA_CMD_CONF_OVERLAY:
1989 if ((features == ATA_DCO_IDENTIFY) ||
1990 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001991 rv = 1;
1992 break;
1993 }
1994 return rv;
1995}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001996
Sam Bradshaw88523a62011-08-30 08:34:26 -06001997/*
1998 * Executes a taskfile
1999 * See ide_taskfile_ioctl() for derivation
2000 */
2001static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002002 void __user *buf,
2003 ide_task_request_t *req_task,
2004 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005{
2006 struct host_to_dev_fis fis;
2007 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002008 u8 *outbuf = NULL;
2009 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002010 dma_addr_t outbuf_dma = 0;
2011 dma_addr_t inbuf_dma = 0;
2012 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002013 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002014 unsigned int taskin = 0;
2015 unsigned int taskout = 0;
2016 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002017 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002018 unsigned int force_single_sector;
2019 unsigned int transfer_size;
2020 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002021 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002022 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002023
2024 taskout = req_task->out_size;
2025 taskin = req_task->in_size;
2026 /* 130560 = 512 * 0xFF*/
2027 if (taskin > 130560 || taskout > 130560) {
2028 err = -EINVAL;
2029 goto abort;
2030 }
2031
2032 if (taskout) {
2033 outbuf = kzalloc(taskout, GFP_KERNEL);
2034 if (outbuf == NULL) {
2035 err = -ENOMEM;
2036 goto abort;
2037 }
2038 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2039 err = -EFAULT;
2040 goto abort;
2041 }
2042 outbuf_dma = pci_map_single(dd->pdev,
2043 outbuf,
2044 taskout,
2045 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002046 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002047 err = -ENOMEM;
2048 goto abort;
2049 }
2050 dma_buffer = outbuf_dma;
2051 }
2052
2053 if (taskin) {
2054 inbuf = kzalloc(taskin, GFP_KERNEL);
2055 if (inbuf == NULL) {
2056 err = -ENOMEM;
2057 goto abort;
2058 }
2059
2060 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2061 err = -EFAULT;
2062 goto abort;
2063 }
2064 inbuf_dma = pci_map_single(dd->pdev,
2065 inbuf,
2066 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002067 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002068 err = -ENOMEM;
2069 goto abort;
2070 }
2071 dma_buffer = inbuf_dma;
2072 }
2073
2074 /* only supports PIO and non-data commands from this ioctl. */
2075 switch (req_task->data_phase) {
2076 case TASKFILE_OUT:
2077 nsect = taskout / ATA_SECT_SIZE;
2078 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2079 break;
2080 case TASKFILE_IN:
2081 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2082 break;
2083 case TASKFILE_NO_DATA:
2084 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2085 break;
2086 default:
2087 err = -EINVAL;
2088 goto abort;
2089 }
2090
Sam Bradshaw88523a62011-08-30 08:34:26 -06002091 /* Build the FIS. */
2092 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2093
2094 fis.type = 0x27;
2095 fis.opts = 1 << 7;
2096 fis.command = req_task->io_ports[7];
2097 fis.features = req_task->io_ports[1];
2098 fis.sect_count = req_task->io_ports[2];
2099 fis.lba_low = req_task->io_ports[3];
2100 fis.lba_mid = req_task->io_ports[4];
2101 fis.lba_hi = req_task->io_ports[5];
2102 /* Clear the dev bit*/
2103 fis.device = req_task->io_ports[6] & ~0x10;
2104
2105 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2106 req_task->in_flags.all =
2107 IDE_TASKFILE_STD_IN_FLAGS |
2108 (IDE_HOB_STD_IN_FLAGS << 8);
2109 fis.lba_low_ex = req_task->hob_ports[3];
2110 fis.lba_mid_ex = req_task->hob_ports[4];
2111 fis.lba_hi_ex = req_task->hob_ports[5];
2112 fis.features_ex = req_task->hob_ports[1];
2113 fis.sect_cnt_ex = req_task->hob_ports[2];
2114
2115 } else {
2116 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2117 }
2118
2119 force_single_sector = implicit_sector(fis.command, fis.features);
2120
2121 if ((taskin || taskout) && (!fis.sect_count)) {
2122 if (nsect)
2123 fis.sect_count = nsect;
2124 else {
2125 if (!force_single_sector) {
2126 dev_warn(&dd->pdev->dev,
2127 "data movement but "
2128 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002129 err = -EINVAL;
2130 goto abort;
2131 }
2132 }
2133 }
2134
2135 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002136 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002137 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2138 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002139 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002140 fis.command,
2141 fis.features,
2142 fis.sect_count,
2143 fis.lba_low,
2144 fis.lba_mid,
2145 fis.lba_hi,
2146 fis.device);
2147
Selvan Mani4453bc82012-09-27 14:36:43 +02002148 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002149 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2150 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002151 erasemode = 1;
2152 }
2153
2154 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002155
2156 /* Determine the correct transfer size.*/
2157 if (force_single_sector)
2158 transfer_size = ATA_SECT_SIZE;
2159 else
2160 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2161
2162 /* Execute the command.*/
2163 if (mtip_exec_internal_command(dd->port,
2164 &fis,
2165 5,
2166 dma_buffer,
2167 transfer_size,
2168 0,
2169 GFP_KERNEL,
2170 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002171 err = -EIO;
2172 goto abort;
2173 }
2174
2175 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2176
2177 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2178 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2179 req_task->io_ports[7] = reply->control;
2180 } else {
2181 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2182 req_task->io_ports[7] = reply->command;
2183 }
2184
2185 /* reclaim the DMA buffers.*/
2186 if (inbuf_dma)
2187 pci_unmap_single(dd->pdev, inbuf_dma,
2188 taskin, DMA_FROM_DEVICE);
2189 if (outbuf_dma)
2190 pci_unmap_single(dd->pdev, outbuf_dma,
2191 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002192 inbuf_dma = 0;
2193 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002194
2195 /* return the ATA registers to the caller.*/
2196 req_task->io_ports[1] = reply->features;
2197 req_task->io_ports[2] = reply->sect_count;
2198 req_task->io_ports[3] = reply->lba_low;
2199 req_task->io_ports[4] = reply->lba_mid;
2200 req_task->io_ports[5] = reply->lba_hi;
2201 req_task->io_ports[6] = reply->device;
2202
2203 if (req_task->out_flags.all & 1) {
2204
2205 req_task->hob_ports[3] = reply->lba_low_ex;
2206 req_task->hob_ports[4] = reply->lba_mid_ex;
2207 req_task->hob_ports[5] = reply->lba_hi_ex;
2208 req_task->hob_ports[1] = reply->features_ex;
2209 req_task->hob_ports[2] = reply->sect_cnt_ex;
2210 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002211 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002212 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002213 "err %x, sect_cnt %x, lbalo %x,"
2214 "lbamid %x, lbahi %x, dev %x\n",
2215 __func__,
2216 req_task->io_ports[7],
2217 req_task->io_ports[1],
2218 req_task->io_ports[2],
2219 req_task->io_ports[3],
2220 req_task->io_ports[4],
2221 req_task->io_ports[5],
2222 req_task->io_ports[6]);
2223
Sam Bradshaw88523a62011-08-30 08:34:26 -06002224 if (taskout) {
2225 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2226 err = -EFAULT;
2227 goto abort;
2228 }
2229 }
2230 if (taskin) {
2231 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2232 err = -EFAULT;
2233 goto abort;
2234 }
2235 }
2236abort:
2237 if (inbuf_dma)
2238 pci_unmap_single(dd->pdev, inbuf_dma,
2239 taskin, DMA_FROM_DEVICE);
2240 if (outbuf_dma)
2241 pci_unmap_single(dd->pdev, outbuf_dma,
2242 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002243 kfree(outbuf);
2244 kfree(inbuf);
2245
2246 return err;
2247}
2248
2249/*
2250 * Handle IOCTL calls from the Block Layer.
2251 *
2252 * This function is called by the Block Layer when it receives an IOCTL
2253 * command that it does not understand. If the IOCTL command is not supported
2254 * this function returns -ENOTTY.
2255 *
2256 * @dd Pointer to the driver data structure.
2257 * @cmd IOCTL command passed from the Block Layer.
2258 * @arg IOCTL argument passed from the Block Layer.
2259 *
2260 * return value
2261 * 0 The IOCTL completed successfully.
2262 * -ENOTTY The specified command is not supported.
2263 * -EFAULT An error occurred copying data to a user space buffer.
2264 * -EIO An error occurred while executing the command.
2265 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002266static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2267 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002268{
2269 switch (cmd) {
2270 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002271 {
2272 if (copy_to_user((void __user *)arg, dd->port->identify,
2273 sizeof(u16) * ATA_ID_WORDS))
2274 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002275 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002276 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002277 case HDIO_DRIVE_CMD:
2278 {
2279 u8 drive_command[4];
2280
2281 /* Copy the user command info to our buffer. */
2282 if (copy_from_user(drive_command,
2283 (void __user *) arg,
2284 sizeof(drive_command)))
2285 return -EFAULT;
2286
2287 /* Execute the drive command. */
2288 if (exec_drive_command(dd->port,
2289 drive_command,
2290 (void __user *) (arg+4)))
2291 return -EIO;
2292
2293 /* Copy the status back to the users buffer. */
2294 if (copy_to_user((void __user *) arg,
2295 drive_command,
2296 sizeof(drive_command)))
2297 return -EFAULT;
2298
2299 break;
2300 }
2301 case HDIO_DRIVE_TASK:
2302 {
2303 u8 drive_command[7];
2304
2305 /* Copy the user command info to our buffer. */
2306 if (copy_from_user(drive_command,
2307 (void __user *) arg,
2308 sizeof(drive_command)))
2309 return -EFAULT;
2310
2311 /* Execute the drive command. */
2312 if (exec_drive_task(dd->port, drive_command))
2313 return -EIO;
2314
2315 /* Copy the status back to the users buffer. */
2316 if (copy_to_user((void __user *) arg,
2317 drive_command,
2318 sizeof(drive_command)))
2319 return -EFAULT;
2320
2321 break;
2322 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002323 case HDIO_DRIVE_TASKFILE: {
2324 ide_task_request_t req_task;
2325 int ret, outtotal;
2326
2327 if (copy_from_user(&req_task, (void __user *) arg,
2328 sizeof(req_task)))
2329 return -EFAULT;
2330
2331 outtotal = sizeof(req_task);
2332
2333 ret = exec_drive_taskfile(dd, (void __user *) arg,
2334 &req_task, outtotal);
2335
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002336 if (copy_to_user((void __user *) arg, &req_task,
2337 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002338 return -EFAULT;
2339
2340 return ret;
2341 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002342
2343 default:
2344 return -EINVAL;
2345 }
2346 return 0;
2347}
2348
2349/*
2350 * Submit an IO to the hw
2351 *
2352 * This function is called by the block layer to issue an io
2353 * to the device. Upon completion, the callback function will
2354 * be called with the data parameter passed as the callback data.
2355 *
2356 * @dd Pointer to the driver data structure.
2357 * @start First sector to read.
2358 * @nsect Number of sectors to read.
2359 * @nents Number of entries in scatter list for the read command.
2360 * @tag The tag of this read command.
2361 * @callback Pointer to the function that should be called
2362 * when the read completes.
2363 * @data Callback data passed to the callback function
2364 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002365 * @dir Direction (read or write)
2366 *
2367 * return value
2368 * None
2369 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002370static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2371 struct mtip_cmd *command, int nents,
2372 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002373{
2374 struct host_to_dev_fis *fis;
2375 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002376 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2377 u64 start = blk_rq_pos(rq);
2378 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002379
2380 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002381 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002382
2383 command->scatter_ents = nents;
2384
2385 /*
2386 * The number of retries for this command before it is
2387 * reported as a failure to the upper layers.
2388 */
2389 command->retries = MTIP_MAX_RETRIES;
2390
2391 /* Fill out fis */
2392 fis = command->command;
2393 fis->type = 0x27;
2394 fis->opts = 1 << 7;
Jens Axboeffc771b2014-05-09 09:42:02 -06002395 if (rq_data_dir(rq) == READ)
2396 fis->command = ATA_CMD_FPDMA_READ;
2397 else
2398 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002399 fis->lba_low = start & 0xFF;
2400 fis->lba_mid = (start >> 8) & 0xFF;
2401 fis->lba_hi = (start >> 16) & 0xFF;
2402 fis->lba_low_ex = (start >> 24) & 0xFF;
2403 fis->lba_mid_ex = (start >> 32) & 0xFF;
2404 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002406 fis->features = nsect & 0xFF;
2407 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002408 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 fis->sect_cnt_ex = 0;
2410 fis->control = 0;
2411 fis->res2 = 0;
2412 fis->res3 = 0;
2413 fill_command_sg(dd, command, nents);
2414
Jens Axboeffc771b2014-05-09 09:42:02 -06002415 if (command->unaligned)
Asai Thambi S P2077d942013-04-29 21:19:49 +02002416 fis->device |= 1 << 7;
2417
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002419 command->command_header->opts =
2420 __force_bit2int cpu_to_le32(
2421 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 command->command_header->byte_count = 0;
2423
2424 /*
2425 * Set the completion function and data for the command
2426 * within this layer.
2427 */
2428 command->comp_data = dd;
2429 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002430 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431
2432 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002433 * To prevent this command from being issued
2434 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002436 if (port->flags & MTIP_PF_PAUSE_IO) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002437 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002438 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002439 return;
2440 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002441
2442 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002443 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002444}
2445
2446/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002447 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002448 *
2449 * @dev Pointer to the device structure, passed by the kernrel.
2450 * @attr Pointer to the device_attribute structure passed by the kernel.
2451 * @buf Pointer to the char buffer that will receive the stats info.
2452 *
2453 * return value
2454 * The size, in bytes, of the data copied into buf.
2455 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002456static ssize_t mtip_hw_show_status(struct device *dev,
2457 struct device_attribute *attr,
2458 char *buf)
2459{
2460 struct driver_data *dd = dev_to_disk(dev)->private_data;
2461 int size = 0;
2462
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002463 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002464 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002465 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002466 size += sprintf(buf, "%s", "write_protect\n");
2467 else
2468 size += sprintf(buf, "%s", "online\n");
2469
2470 return size;
2471}
2472
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002473static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002474
Asai Thambi S P0caff002013-04-03 19:56:21 +05302475/* debugsfs entries */
2476
2477static ssize_t show_device_status(struct device_driver *drv, char *buf)
2478{
2479 int size = 0;
2480 struct driver_data *dd, *tmp;
2481 unsigned long flags;
2482 char id_buf[42];
2483 u16 status = 0;
2484
2485 spin_lock_irqsave(&dev_lock, flags);
2486 size += sprintf(&buf[size], "Devices Present:\n");
2487 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002488 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302489 if (dd->port &&
2490 dd->port->identify &&
2491 dd->port->identify_valid) {
2492 strlcpy(id_buf,
2493 (char *) (dd->port->identify + 10), 21);
2494 status = *(dd->port->identify + 141);
2495 } else {
2496 memset(id_buf, 0, 42);
2497 status = 0;
2498 }
2499
2500 if (dd->port &&
2501 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2502 size += sprintf(&buf[size],
2503 " device %s %s (ftl rebuild %d %%)\n",
2504 dev_name(&dd->pdev->dev),
2505 id_buf,
2506 status);
2507 } else {
2508 size += sprintf(&buf[size],
2509 " device %s %s\n",
2510 dev_name(&dd->pdev->dev),
2511 id_buf);
2512 }
2513 }
2514 }
2515
2516 size += sprintf(&buf[size], "Devices Being Removed:\n");
2517 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002518 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302519 if (dd->port &&
2520 dd->port->identify &&
2521 dd->port->identify_valid) {
2522 strlcpy(id_buf,
2523 (char *) (dd->port->identify+10), 21);
2524 status = *(dd->port->identify + 141);
2525 } else {
2526 memset(id_buf, 0, 42);
2527 status = 0;
2528 }
2529
2530 if (dd->port &&
2531 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2532 size += sprintf(&buf[size],
2533 " device %s %s (ftl rebuild %d %%)\n",
2534 dev_name(&dd->pdev->dev),
2535 id_buf,
2536 status);
2537 } else {
2538 size += sprintf(&buf[size],
2539 " device %s %s\n",
2540 dev_name(&dd->pdev->dev),
2541 id_buf);
2542 }
2543 }
2544 }
2545 spin_unlock_irqrestore(&dev_lock, flags);
2546
2547 return size;
2548}
2549
2550static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2551 size_t len, loff_t *offset)
2552{
David Milburnc8afd0d2013-05-23 16:23:45 -05002553 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302554 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002555 char *buf;
2556 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302557
2558 if (!len || *offset)
2559 return 0;
2560
David Milburnc8afd0d2013-05-23 16:23:45 -05002561 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2562 if (!buf) {
2563 dev_err(&dd->pdev->dev,
2564 "Memory allocation: status buffer\n");
2565 return -ENOMEM;
2566 }
2567
Asai Thambi S P0caff002013-04-03 19:56:21 +05302568 size += show_device_status(NULL, buf);
2569
2570 *offset = size <= len ? size : len;
2571 size = copy_to_user(ubuf, buf, *offset);
2572 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002573 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302574
David Milburnc8afd0d2013-05-23 16:23:45 -05002575 kfree(buf);
2576 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302577}
2578
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002579static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2580 size_t len, loff_t *offset)
2581{
2582 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002583 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002584 u32 group_allocated;
2585 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002586 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002587
2588 if (!len || size)
2589 return 0;
2590
David Milburnc8afd0d2013-05-23 16:23:45 -05002591 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2592 if (!buf) {
2593 dev_err(&dd->pdev->dev,
2594 "Memory allocation: register buffer\n");
2595 return -ENOMEM;
2596 }
2597
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002598 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2599
2600 for (n = dd->slot_groups-1; n >= 0; n--)
2601 size += sprintf(&buf[size], "%08X ",
2602 readl(dd->port->s_active[n]));
2603
2604 size += sprintf(&buf[size], "]\n");
2605 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2606
2607 for (n = dd->slot_groups-1; n >= 0; n--)
2608 size += sprintf(&buf[size], "%08X ",
2609 readl(dd->port->cmd_issue[n]));
2610
2611 size += sprintf(&buf[size], "]\n");
2612 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--)
2615 size += sprintf(&buf[size], "%08X ",
2616 readl(dd->port->completed[n]));
2617
2618 size += sprintf(&buf[size], "]\n");
2619 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2620 readl(dd->port->mmio + PORT_IRQ_STAT));
2621 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2622 readl(dd->mmio + HOST_IRQ_STAT));
2623 size += sprintf(&buf[size], "\n");
2624
2625 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2626
2627 for (n = dd->slot_groups-1; n >= 0; n--) {
2628 if (sizeof(long) > sizeof(u32))
2629 group_allocated =
2630 dd->port->allocated[n/2] >> (32*(n&1));
2631 else
2632 group_allocated = dd->port->allocated[n];
2633 size += sprintf(&buf[size], "%08X ", group_allocated);
2634 }
2635 size += sprintf(&buf[size], "]\n");
2636
2637 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2638
2639 for (n = dd->slot_groups-1; n >= 0; n--) {
2640 if (sizeof(long) > sizeof(u32))
2641 group_allocated =
2642 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2643 else
2644 group_allocated = dd->port->cmds_to_issue[n];
2645 size += sprintf(&buf[size], "%08X ", group_allocated);
2646 }
2647 size += sprintf(&buf[size], "]\n");
2648
2649 *offset = size <= len ? size : len;
2650 size = copy_to_user(ubuf, buf, *offset);
2651 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002652 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002653
David Milburnc8afd0d2013-05-23 16:23:45 -05002654 kfree(buf);
2655 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002656}
2657
2658static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2659 size_t len, loff_t *offset)
2660{
2661 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002662 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002663 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002664 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002665
2666 if (!len || size)
2667 return 0;
2668
David Milburnc8afd0d2013-05-23 16:23:45 -05002669 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2670 if (!buf) {
2671 dev_err(&dd->pdev->dev,
2672 "Memory allocation: flag buffer\n");
2673 return -ENOMEM;
2674 }
2675
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002676 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2677 dd->port->flags);
2678 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2679 dd->dd_flag);
2680
2681 *offset = size <= len ? size : len;
2682 size = copy_to_user(ubuf, buf, *offset);
2683 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002684 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002685
David Milburnc8afd0d2013-05-23 16:23:45 -05002686 kfree(buf);
2687 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002688}
2689
Asai Thambi S P0caff002013-04-03 19:56:21 +05302690static const struct file_operations mtip_device_status_fops = {
2691 .owner = THIS_MODULE,
2692 .open = simple_open,
2693 .read = mtip_hw_read_device_status,
2694 .llseek = no_llseek,
2695};
2696
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002697static const struct file_operations mtip_regs_fops = {
2698 .owner = THIS_MODULE,
2699 .open = simple_open,
2700 .read = mtip_hw_read_registers,
2701 .llseek = no_llseek,
2702};
2703
2704static const struct file_operations mtip_flags_fops = {
2705 .owner = THIS_MODULE,
2706 .open = simple_open,
2707 .read = mtip_hw_read_flags,
2708 .llseek = no_llseek,
2709};
2710
Sam Bradshaw88523a62011-08-30 08:34:26 -06002711/*
2712 * Create the sysfs related attributes.
2713 *
2714 * @dd Pointer to the driver data structure.
2715 * @kobj Pointer to the kobj for the block device.
2716 *
2717 * return value
2718 * 0 Operation completed successfully.
2719 * -EINVAL Invalid parameter.
2720 */
Jens Axboe63166682011-09-27 21:27:43 -06002721static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002722{
2723 if (!kobj || !dd)
2724 return -EINVAL;
2725
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002726 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2727 dev_warn(&dd->pdev->dev,
2728 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002729 return 0;
2730}
2731
2732/*
2733 * Remove the sysfs related attributes.
2734 *
2735 * @dd Pointer to the driver data structure.
2736 * @kobj Pointer to the kobj for the block device.
2737 *
2738 * return value
2739 * 0 Operation completed successfully.
2740 * -EINVAL Invalid parameter.
2741 */
Jens Axboe63166682011-09-27 21:27:43 -06002742static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002743{
2744 if (!kobj || !dd)
2745 return -EINVAL;
2746
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002747 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002748
2749 return 0;
2750}
2751
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002752static int mtip_hw_debugfs_init(struct driver_data *dd)
2753{
2754 if (!dfs_parent)
2755 return -1;
2756
2757 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2758 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2759 dev_warn(&dd->pdev->dev,
2760 "Error creating node %s under debugfs\n",
2761 dd->disk->disk_name);
2762 dd->dfs_node = NULL;
2763 return -1;
2764 }
2765
2766 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2767 &mtip_flags_fops);
2768 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2769 &mtip_regs_fops);
2770
2771 return 0;
2772}
2773
2774static void mtip_hw_debugfs_exit(struct driver_data *dd)
2775{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002776 if (dd->dfs_node)
2777 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002778}
2779
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002780static int mtip_free_orphan(struct driver_data *dd)
2781{
2782 struct kobject *kobj;
2783
2784 if (dd->bdev) {
2785 if (dd->bdev->bd_holders >= 1)
2786 return -2;
2787
2788 bdput(dd->bdev);
2789 dd->bdev = NULL;
2790 }
2791
2792 mtip_hw_debugfs_exit(dd);
2793
2794 spin_lock(&rssd_index_lock);
2795 ida_remove(&rssd_index_ida, dd->index);
2796 spin_unlock(&rssd_index_lock);
2797
2798 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
2799 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
2800 put_disk(dd->disk);
2801 } else {
2802 if (dd->disk) {
2803 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
2804 if (kobj) {
2805 mtip_hw_sysfs_exit(dd, kobj);
2806 kobject_put(kobj);
2807 }
2808 del_gendisk(dd->disk);
2809 dd->disk = NULL;
2810 }
2811 if (dd->queue) {
2812 dd->queue->queuedata = NULL;
2813 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06002814 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002815 dd->queue = NULL;
2816 }
2817 }
2818 kfree(dd);
2819 return 0;
2820}
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002821
Sam Bradshaw88523a62011-08-30 08:34:26 -06002822/*
2823 * Perform any init/resume time hardware setup
2824 *
2825 * @dd Pointer to the driver data structure.
2826 *
2827 * return value
2828 * None
2829 */
2830static inline void hba_setup(struct driver_data *dd)
2831{
2832 u32 hwdata;
2833 hwdata = readl(dd->mmio + HOST_HSORG);
2834
2835 /* interrupt bug workaround: use only 1 IS bit.*/
2836 writel(hwdata |
2837 HSORG_DISABLE_SLOTGRP_INTR |
2838 HSORG_DISABLE_SLOTGRP_PXIS,
2839 dd->mmio + HOST_HSORG);
2840}
2841
Asai Thambi S P2077d942013-04-29 21:19:49 +02002842static int mtip_device_unaligned_constrained(struct driver_data *dd)
2843{
2844 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2845}
2846
Sam Bradshaw88523a62011-08-30 08:34:26 -06002847/*
2848 * Detect the details of the product, and store anything needed
2849 * into the driver data structure. This includes product type and
2850 * version and number of slot groups.
2851 *
2852 * @dd Pointer to the driver data structure.
2853 *
2854 * return value
2855 * None
2856 */
2857static void mtip_detect_product(struct driver_data *dd)
2858{
2859 u32 hwdata;
2860 unsigned int rev, slotgroups;
2861
2862 /*
2863 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2864 * info register:
2865 * [15:8] hardware/software interface rev#
2866 * [ 3] asic-style interface
2867 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2868 */
2869 hwdata = readl(dd->mmio + HOST_HSORG);
2870
2871 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2872 dd->slot_groups = 1;
2873
2874 if (hwdata & 0x8) {
2875 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2876 rev = (hwdata & HSORG_HWREV) >> 8;
2877 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2878 dev_info(&dd->pdev->dev,
2879 "ASIC-FPGA design, HS rev 0x%x, "
2880 "%i slot groups [%i slots]\n",
2881 rev,
2882 slotgroups,
2883 slotgroups * 32);
2884
2885 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2886 dev_warn(&dd->pdev->dev,
2887 "Warning: driver only supports "
2888 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2889 slotgroups = MTIP_MAX_SLOT_GROUPS;
2890 }
2891 dd->slot_groups = slotgroups;
2892 return;
2893 }
2894
2895 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2896}
2897
2898/*
2899 * Blocking wait for FTL rebuild to complete
2900 *
2901 * @dd Pointer to the DRIVER_DATA structure.
2902 *
2903 * return value
2904 * 0 FTL rebuild completed successfully
2905 * -EFAULT FTL rebuild error/timeout/interruption
2906 */
2907static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2908{
2909 unsigned long timeout, cnt = 0, start;
2910
2911 dev_warn(&dd->pdev->dev,
2912 "FTL rebuild in progress. Polling for completion.\n");
2913
2914 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002915 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2916
2917 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002918 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002919 &dd->dd_flag)))
2920 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002921 if (mtip_check_surprise_removal(dd->pdev))
2922 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002923
Sam Bradshaw88523a62011-08-30 08:34:26 -06002924 if (mtip_get_identify(dd->port, NULL) < 0)
2925 return -EFAULT;
2926
2927 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2928 MTIP_FTL_REBUILD_MAGIC) {
2929 ssleep(1);
2930 /* Print message every 3 minutes */
2931 if (cnt++ >= 180) {
2932 dev_warn(&dd->pdev->dev,
2933 "FTL rebuild in progress (%d secs).\n",
2934 jiffies_to_msecs(jiffies - start) / 1000);
2935 cnt = 0;
2936 }
2937 } else {
2938 dev_warn(&dd->pdev->dev,
2939 "FTL rebuild complete (%d secs).\n",
2940 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002941 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002942 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002943 }
2944 ssleep(10);
2945 } while (time_before(jiffies, timeout));
2946
2947 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002948 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002949 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2950 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002951 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002952}
2953
2954/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002955 * service thread to issue queued commands
2956 *
2957 * @data Pointer to the driver data structure.
2958 *
2959 * return value
2960 * 0
2961 */
2962
2963static int mtip_service_thread(void *data)
2964{
2965 struct driver_data *dd = (struct driver_data *)data;
2966 unsigned long slot, slot_start, slot_wrap;
2967 unsigned int num_cmd_slots = dd->slot_groups * 32;
2968 struct mtip_port *port = dd->port;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002969 int ret;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002970
2971 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002972 if (kthread_should_stop() ||
2973 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2974 goto st_out;
2975 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2976
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002977 /*
2978 * the condition is to check neither an internal command is
2979 * is in progress nor error handling is active
2980 */
2981 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002982 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002983
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002984 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2985
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002986 if (kthread_should_stop() ||
2987 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2988 goto st_out;
2989
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002990 /* If I am an orphan, start self cleanup */
2991 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002992 break;
2993
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002994 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002995 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002996 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002997
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002998restart_eh:
2999 /* Demux bits: start with error handling */
3000 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
3001 mtip_handle_tfe(dd);
3002 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
3003 }
3004
3005 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
3006 goto restart_eh;
3007
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003008 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003009 slot = 1;
3010 /* used to restrict the loop to one iteration */
3011 slot_start = num_cmd_slots;
3012 slot_wrap = 0;
3013 while (1) {
3014 slot = find_next_bit(port->cmds_to_issue,
3015 num_cmd_slots, slot);
3016 if (slot_wrap == 1) {
3017 if ((slot_start >= slot) ||
3018 (slot >= num_cmd_slots))
3019 break;
3020 }
3021 if (unlikely(slot_start == num_cmd_slots))
3022 slot_start = slot;
3023
3024 if (unlikely(slot == num_cmd_slots)) {
3025 slot = 1;
3026 slot_wrap = 1;
3027 continue;
3028 }
3029
3030 /* Issue the command to the hardware */
3031 mtip_issue_ncq_command(port, slot);
3032
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003033 clear_bit(slot, port->cmds_to_issue);
3034 }
3035
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003036 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003037 }
3038
3039 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003040 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003041 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003042 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003043 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003044 }
3045 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003046
3047 /* wait for pci remove to exit */
3048 while (1) {
3049 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3050 break;
3051 msleep_interruptible(1000);
3052 if (kthread_should_stop())
3053 goto st_out;
3054 }
3055
3056 while (1) {
3057 ret = mtip_free_orphan(dd);
3058 if (!ret) {
3059 /* NOTE: All data structures are invalid, do not
3060 * access any here */
3061 return 0;
3062 }
3063 msleep_interruptible(1000);
3064 if (kthread_should_stop())
3065 goto st_out;
3066 }
3067st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003068 return 0;
3069}
3070
3071/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003072 * DMA region teardown
3073 *
3074 * @dd Pointer to driver_data structure
3075 *
3076 * return value
3077 * None
3078 */
3079static void mtip_dma_free(struct driver_data *dd)
3080{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003081 struct mtip_port *port = dd->port;
3082
3083 if (port->block1)
3084 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3085 port->block1, port->block1_dma);
3086
3087 if (port->command_list) {
3088 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3089 port->command_list, port->command_list_dma);
3090 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003091}
3092
3093/*
3094 * DMA region setup
3095 *
3096 * @dd Pointer to driver_data structure
3097 *
3098 * return value
3099 * -ENOMEM Not enough free DMA region space to initialize driver
3100 */
3101static int mtip_dma_alloc(struct driver_data *dd)
3102{
3103 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003104
3105 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3106 port->block1 =
3107 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3108 &port->block1_dma, GFP_KERNEL);
3109 if (!port->block1)
3110 return -ENOMEM;
3111 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3112
3113 /* Allocate dma memory for command list */
3114 port->command_list =
3115 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3116 &port->command_list_dma, GFP_KERNEL);
3117 if (!port->command_list) {
3118 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3119 port->block1, port->block1_dma);
3120 port->block1 = NULL;
3121 port->block1_dma = 0;
3122 return -ENOMEM;
3123 }
3124 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3125
3126 /* Setup all pointers into first DMA region */
3127 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3128 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3129 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3130 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3131 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3132 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3133 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3134 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3135
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003136 return 0;
3137}
3138
Jens Axboeffc771b2014-05-09 09:42:02 -06003139static int mtip_hw_get_identify(struct driver_data *dd)
3140{
3141 struct smart_attr attr242;
3142 unsigned char *buf;
3143 int rv;
3144
3145 if (mtip_get_identify(dd->port, NULL) < 0)
3146 return -EFAULT;
3147
3148 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3149 MTIP_FTL_REBUILD_MAGIC) {
3150 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3151 return MTIP_FTL_REBUILD_MAGIC;
3152 }
3153 mtip_dump_identify(dd->port);
3154
3155 /* check write protect, over temp and rebuild statuses */
3156 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3157 dd->port->log_buf,
3158 dd->port->log_buf_dma, 1);
3159 if (rv) {
3160 dev_warn(&dd->pdev->dev,
3161 "Error in READ LOG EXT (10h) command\n");
3162 /* non-critical error, don't fail the load */
3163 } else {
3164 buf = (unsigned char *)dd->port->log_buf;
3165 if (buf[259] & 0x1) {
3166 dev_info(&dd->pdev->dev,
3167 "Write protect bit is set.\n");
3168 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3169 }
3170 if (buf[288] == 0xF7) {
3171 dev_info(&dd->pdev->dev,
3172 "Exceeded Tmax, drive in thermal shutdown.\n");
3173 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3174 }
3175 if (buf[288] == 0xBF) {
3176 dev_info(&dd->pdev->dev,
3177 "Drive indicates rebuild has failed.\n");
3178 /* TODO */
3179 }
3180 }
3181
3182 /* get write protect progess */
3183 memset(&attr242, 0, sizeof(struct smart_attr));
3184 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3185 dev_warn(&dd->pdev->dev,
3186 "Unable to check write protect progress\n");
3187 else
3188 dev_info(&dd->pdev->dev,
3189 "Write protect progress: %u%% (%u blocks)\n",
3190 attr242.cur, le32_to_cpu(attr242.data));
3191
3192 return rv;
3193}
3194
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003195/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003196 * Called once for each card.
3197 *
3198 * @dd Pointer to the driver data structure.
3199 *
3200 * return value
3201 * 0 on success, else an error code.
3202 */
Jens Axboe63166682011-09-27 21:27:43 -06003203static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003204{
3205 int i;
3206 int rv;
3207 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003208 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003209
3210 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3211
3212 mtip_detect_product(dd);
3213 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3214 rv = -EIO;
3215 goto out1;
3216 }
3217 num_command_slots = dd->slot_groups * 32;
3218
3219 hba_setup(dd);
3220
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003221 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3222 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003223 if (!dd->port) {
3224 dev_err(&dd->pdev->dev,
3225 "Memory allocation: port structure\n");
3226 return -ENOMEM;
3227 }
3228
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003229 /* Continue workqueue setup */
3230 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3231 dd->work[i].port = dd->port;
3232
Asai Thambi S P2077d942013-04-29 21:19:49 +02003233 /* Enable unaligned IO constraints for some devices */
3234 if (mtip_device_unaligned_constrained(dd))
3235 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3236 else
3237 dd->unal_qdepth = 0;
3238
Asai Thambi S P2077d942013-04-29 21:19:49 +02003239 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003240
3241 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003242 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3243 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003244
3245 /* Set the port mmio base address. */
3246 dd->port->mmio = dd->mmio + PORT_OFFSET;
3247 dd->port->dd = dd;
3248
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003249 /* DMA allocations */
3250 rv = mtip_dma_alloc(dd);
3251 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003252 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003253
3254 /* Setup the pointers to the extended s_active and CI registers. */
3255 for (i = 0; i < dd->slot_groups; i++) {
3256 dd->port->s_active[i] =
3257 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3258 dd->port->cmd_issue[i] =
3259 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3260 dd->port->completed[i] =
3261 dd->port->mmio + i*0x80 + PORT_SDBV;
3262 }
3263
Asai Thambi S P45038362012-04-09 08:35:38 +02003264 timetaken = jiffies;
3265 timeout = jiffies + msecs_to_jiffies(30000);
3266 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3267 time_before(jiffies, timeout)) {
3268 mdelay(100);
3269 }
3270 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3271 timetaken = jiffies - timetaken;
3272 dev_warn(&dd->pdev->dev,
3273 "Surprise removal detected at %u ms\n",
3274 jiffies_to_msecs(timetaken));
3275 rv = -ENODEV;
3276 goto out2 ;
3277 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003278 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003279 timetaken = jiffies - timetaken;
3280 dev_warn(&dd->pdev->dev,
3281 "Removal detected at %u ms\n",
3282 jiffies_to_msecs(timetaken));
3283 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003284 goto out2;
3285 }
3286
Asai Thambi S P45038362012-04-09 08:35:38 +02003287 /* Conditionally reset the HBA. */
3288 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3289 if (mtip_hba_reset(dd) < 0) {
3290 dev_err(&dd->pdev->dev,
3291 "Card did not reset within timeout\n");
3292 rv = -EIO;
3293 goto out2;
3294 }
3295 } else {
3296 /* Clear any pending interrupts on the HBA */
3297 writel(readl(dd->mmio + HOST_IRQ_STAT),
3298 dd->mmio + HOST_IRQ_STAT);
3299 }
3300
Sam Bradshaw88523a62011-08-30 08:34:26 -06003301 mtip_init_port(dd->port);
3302 mtip_start_port(dd->port);
3303
3304 /* Setup the ISR and enable interrupts. */
3305 rv = devm_request_irq(&dd->pdev->dev,
3306 dd->pdev->irq,
3307 mtip_irq_handler,
3308 IRQF_SHARED,
3309 dev_driver_string(&dd->pdev->dev),
3310 dd);
3311
3312 if (rv) {
3313 dev_err(&dd->pdev->dev,
3314 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3315 goto out2;
3316 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003317 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003318
3319 /* Enable interrupts on the HBA. */
3320 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3321 dd->mmio + HOST_CTL);
3322
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003323 init_waitqueue_head(&dd->port->svc_wait);
3324
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003325 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003326 rv = -EFAULT;
3327 goto out3;
3328 }
3329
Sam Bradshaw88523a62011-08-30 08:34:26 -06003330 return rv;
3331
3332out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003333 /* Disable interrupts on the HBA. */
3334 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3335 dd->mmio + HOST_CTL);
3336
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003337 /* Release the IRQ. */
3338 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003339 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3340
3341out2:
3342 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003343 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003344
Sam Bradshaw88523a62011-08-30 08:34:26 -06003345out1:
3346 /* Free the memory allocated for the for structure. */
3347 kfree(dd->port);
3348
3349 return rv;
3350}
3351
Jens Axboeffc771b2014-05-09 09:42:02 -06003352static void mtip_standby_drive(struct driver_data *dd)
3353{
3354 if (dd->sr)
3355 return;
3356
3357 /*
3358 * Send standby immediate (E0h) to the drive so that it
3359 * saves its state.
3360 */
3361 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3362 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3363 if (mtip_standby_immediate(dd->port))
3364 dev_warn(&dd->pdev->dev,
3365 "STANDBY IMMEDIATE failed\n");
3366}
3367
Sam Bradshaw88523a62011-08-30 08:34:26 -06003368/*
3369 * Called to deinitialize an interface.
3370 *
3371 * @dd Pointer to the driver data structure.
3372 *
3373 * return value
3374 * 0
3375 */
Jens Axboe63166682011-09-27 21:27:43 -06003376static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003377{
3378 /*
3379 * Send standby immediate (E0h) to the drive so that it
3380 * saves its state.
3381 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003382 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003383 /* de-initialize the port. */
3384 mtip_deinit_port(dd->port);
3385
3386 /* Disable interrupts on the HBA. */
3387 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3388 dd->mmio + HOST_CTL);
3389 }
3390
Sam Bradshaw88523a62011-08-30 08:34:26 -06003391 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003392 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003393 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3394
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003395 /* Free dma regions */
3396 mtip_dma_free(dd);
3397
Sam Bradshaw88523a62011-08-30 08:34:26 -06003398 /* Free the memory allocated for the for structure. */
3399 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003400 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003401
3402 return 0;
3403}
3404
3405/*
3406 * Issue a Standby Immediate command to the device.
3407 *
3408 * This function is called by the Block Layer just before the
3409 * system powers off during a shutdown.
3410 *
3411 * @dd Pointer to the driver data structure.
3412 *
3413 * return value
3414 * 0
3415 */
Jens Axboe63166682011-09-27 21:27:43 -06003416static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003417{
3418 /*
3419 * Send standby immediate (E0h) to the drive so that it
3420 * saves its state.
3421 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003422 if (!dd->sr && dd->port)
3423 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003424
3425 return 0;
3426}
3427
3428/*
3429 * Suspend function
3430 *
3431 * This function is called by the Block Layer just before the
3432 * system hibernates.
3433 *
3434 * @dd Pointer to the driver data structure.
3435 *
3436 * return value
3437 * 0 Suspend was successful
3438 * -EFAULT Suspend was not successful
3439 */
Jens Axboe63166682011-09-27 21:27:43 -06003440static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003441{
3442 /*
3443 * Send standby immediate (E0h) to the drive
3444 * so that it saves its state.
3445 */
3446 if (mtip_standby_immediate(dd->port) != 0) {
3447 dev_err(&dd->pdev->dev,
3448 "Failed standby-immediate command\n");
3449 return -EFAULT;
3450 }
3451
3452 /* Disable interrupts on the HBA.*/
3453 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3454 dd->mmio + HOST_CTL);
3455 mtip_deinit_port(dd->port);
3456
3457 return 0;
3458}
3459
3460/*
3461 * Resume function
3462 *
3463 * This function is called by the Block Layer as the
3464 * system resumes.
3465 *
3466 * @dd Pointer to the driver data structure.
3467 *
3468 * return value
3469 * 0 Resume was successful
3470 * -EFAULT Resume was not successful
3471 */
Jens Axboe63166682011-09-27 21:27:43 -06003472static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003473{
3474 /* Perform any needed hardware setup steps */
3475 hba_setup(dd);
3476
3477 /* Reset the HBA */
3478 if (mtip_hba_reset(dd) != 0) {
3479 dev_err(&dd->pdev->dev,
3480 "Unable to reset the HBA\n");
3481 return -EFAULT;
3482 }
3483
3484 /*
3485 * Enable the port, DMA engine, and FIS reception specific
3486 * h/w in controller.
3487 */
3488 mtip_init_port(dd->port);
3489 mtip_start_port(dd->port);
3490
3491 /* Enable interrupts on the HBA.*/
3492 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3493 dd->mmio + HOST_CTL);
3494
3495 return 0;
3496}
3497
3498/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003499 * Helper function for reusing disk name
3500 * upon hot insertion.
3501 */
3502static int rssd_disk_name_format(char *prefix,
3503 int index,
3504 char *buf,
3505 int buflen)
3506{
3507 const int base = 'z' - 'a' + 1;
3508 char *begin = buf + strlen(prefix);
3509 char *end = buf + buflen;
3510 char *p;
3511 int unit;
3512
3513 p = end - 1;
3514 *p = '\0';
3515 unit = base;
3516 do {
3517 if (p == begin)
3518 return -EINVAL;
3519 *--p = 'a' + (index % unit);
3520 index = (index / unit) - 1;
3521 } while (index >= 0);
3522
3523 memmove(begin, p, end - p);
3524 memcpy(buf, prefix, strlen(prefix));
3525
3526 return 0;
3527}
3528
3529/*
3530 * Block layer IOCTL handler.
3531 *
3532 * @dev Pointer to the block_device structure.
3533 * @mode ignored
3534 * @cmd IOCTL command passed from the user application.
3535 * @arg Argument passed from the user application.
3536 *
3537 * return value
3538 * 0 IOCTL completed successfully.
3539 * -ENOTTY IOCTL not supported or invalid driver data
3540 * structure pointer.
3541 */
3542static int mtip_block_ioctl(struct block_device *dev,
3543 fmode_t mode,
3544 unsigned cmd,
3545 unsigned long arg)
3546{
3547 struct driver_data *dd = dev->bd_disk->private_data;
3548
3549 if (!capable(CAP_SYS_ADMIN))
3550 return -EACCES;
3551
3552 if (!dd)
3553 return -ENOTTY;
3554
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003555 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003556 return -ENOTTY;
3557
Sam Bradshaw88523a62011-08-30 08:34:26 -06003558 switch (cmd) {
3559 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003560 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003561 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003562 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003563 }
3564}
3565
Jens Axboe16d02c02011-09-27 15:50:01 -06003566#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003567/*
3568 * Block layer compat IOCTL handler.
3569 *
3570 * @dev Pointer to the block_device structure.
3571 * @mode ignored
3572 * @cmd IOCTL command passed from the user application.
3573 * @arg Argument passed from the user application.
3574 *
3575 * return value
3576 * 0 IOCTL completed successfully.
3577 * -ENOTTY IOCTL not supported or invalid driver data
3578 * structure pointer.
3579 */
3580static int mtip_block_compat_ioctl(struct block_device *dev,
3581 fmode_t mode,
3582 unsigned cmd,
3583 unsigned long arg)
3584{
3585 struct driver_data *dd = dev->bd_disk->private_data;
3586
3587 if (!capable(CAP_SYS_ADMIN))
3588 return -EACCES;
3589
3590 if (!dd)
3591 return -ENOTTY;
3592
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003593 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003594 return -ENOTTY;
3595
Sam Bradshaw88523a62011-08-30 08:34:26 -06003596 switch (cmd) {
3597 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003598 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003599 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003600 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003601 ide_task_request_t req_task;
3602 int compat_tasksize, outtotal, ret;
3603
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003604 compat_tasksize =
3605 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003606
3607 compat_req_task =
3608 (struct mtip_compat_ide_task_request_s __user *) arg;
3609
3610 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003611 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003612 return -EFAULT;
3613
3614 if (get_user(req_task.out_size, &compat_req_task->out_size))
3615 return -EFAULT;
3616
3617 if (get_user(req_task.in_size, &compat_req_task->in_size))
3618 return -EFAULT;
3619
3620 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3621
3622 ret = exec_drive_taskfile(dd, (void __user *) arg,
3623 &req_task, outtotal);
3624
3625 if (copy_to_user((void __user *) arg, &req_task,
3626 compat_tasksize -
3627 (2 * sizeof(compat_long_t))))
3628 return -EFAULT;
3629
3630 if (put_user(req_task.out_size, &compat_req_task->out_size))
3631 return -EFAULT;
3632
3633 if (put_user(req_task.in_size, &compat_req_task->in_size))
3634 return -EFAULT;
3635
3636 return ret;
3637 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003638 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003639 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003640 }
3641}
Jens Axboe16d02c02011-09-27 15:50:01 -06003642#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003643
3644/*
3645 * Obtain the geometry of the device.
3646 *
3647 * You may think that this function is obsolete, but some applications,
3648 * fdisk for example still used CHS values. This function describes the
3649 * device as having 224 heads and 56 sectors per cylinder. These values are
3650 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3651 * partition is described in terms of a start and end cylinder this means
3652 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3653 * affects performance.
3654 *
3655 * @dev Pointer to the block_device strucutre.
3656 * @geo Pointer to a hd_geometry structure.
3657 *
3658 * return value
3659 * 0 Operation completed successfully.
3660 * -ENOTTY An error occurred while reading the drive capacity.
3661 */
3662static int mtip_block_getgeo(struct block_device *dev,
3663 struct hd_geometry *geo)
3664{
3665 struct driver_data *dd = dev->bd_disk->private_data;
3666 sector_t capacity;
3667
3668 if (!dd)
3669 return -ENOTTY;
3670
3671 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3672 dev_warn(&dd->pdev->dev,
3673 "Could not get drive capacity.\n");
3674 return -ENOTTY;
3675 }
3676
3677 geo->heads = 224;
3678 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003679 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003680 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003681 return 0;
3682}
3683
3684/*
3685 * Block device operation function.
3686 *
3687 * This structure contains pointers to the functions required by the block
3688 * layer.
3689 */
3690static const struct block_device_operations mtip_block_ops = {
3691 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003692#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003693 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003694#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003695 .getgeo = mtip_block_getgeo,
3696 .owner = THIS_MODULE
3697};
3698
3699/*
3700 * Block layer make request function.
3701 *
3702 * This function is called by the kernel to process a BIO for
3703 * the P320 device.
3704 *
3705 * @queue Pointer to the request queue. Unused other than to obtain
3706 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003707 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003708 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003709 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003710static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003711{
Jens Axboeffc771b2014-05-09 09:42:02 -06003712 struct driver_data *dd = hctx->queue->queuedata;
3713 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3714 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003716 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3717 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3718 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003719 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003720 }
3721 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003722 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003723 }
3724 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3725 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003726 rq_data_dir(rq))) {
3727 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003728 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003729 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3730 return -ENODATA;
3731 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3732 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003733 }
3734
Jens Axboeffc771b2014-05-09 09:42:02 -06003735 if (rq->cmd_flags & REQ_DISCARD) {
3736 int err;
3737
3738 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
3739 blk_mq_end_io(rq, err);
3740 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003741 }
3742
Jens Axboeffc771b2014-05-09 09:42:02 -06003743 /* Create the scatter list for this request. */
3744 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003745
Jens Axboeffc771b2014-05-09 09:42:02 -06003746 /* Issue the read/write. */
3747 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3748 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003749}
3750
Jens Axboeffc771b2014-05-09 09:42:02 -06003751static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3752 struct request *rq)
3753{
3754 struct driver_data *dd = hctx->queue->queuedata;
3755 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3756
3757 if (!dd->unal_qdepth || rq_data_dir(rq) == READ)
3758 return false;
3759
3760 /*
3761 * If unaligned depth must be limited on this controller, mark it
3762 * as unaligned if the IO isn't on a 4k boundary (start of length).
3763 */
3764 if (blk_rq_sectors(rq) <= 64) {
3765 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3766 cmd->unaligned = 1;
3767 }
3768
3769 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3770 return true;
3771
3772 return false;
3773}
3774
3775static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
3776{
3777 int ret;
3778
3779 if (mtip_check_unal_depth(hctx, rq))
3780 return BLK_MQ_RQ_QUEUE_BUSY;
3781
3782 ret = mtip_submit_request(hctx, rq);
3783 if (!ret)
3784 return BLK_MQ_RQ_QUEUE_OK;
3785
3786 rq->errors = ret;
3787 return BLK_MQ_RQ_QUEUE_ERROR;
3788}
3789
3790static void mtip_free_cmd(void *data, struct request *rq,
3791 unsigned int hctx_idx, unsigned int request_idx)
3792{
3793 struct driver_data *dd = data;
3794 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3795
3796 if (!cmd->command)
3797 return;
3798
3799 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3800 cmd->command, cmd->command_dma);
3801}
3802
3803static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3804 unsigned int request_idx, unsigned int numa_node)
3805{
3806 struct driver_data *dd = data;
3807 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3808 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3809
3810 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3811 &cmd->command_dma, GFP_KERNEL);
3812 if (!cmd->command)
3813 return -ENOMEM;
3814
3815 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3816
3817 /* Point the command headers at the command tables. */
3818 cmd->command_header = dd->port->command_list +
3819 (sizeof(struct mtip_cmd_hdr) * request_idx);
3820 cmd->command_header_dma = dd->port->command_list_dma +
3821 (sizeof(struct mtip_cmd_hdr) * request_idx);
3822
3823 if (host_cap_64)
3824 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3825
3826 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3827
3828 sg_init_table(cmd->sg, MTIP_MAX_SG);
3829 return 0;
3830}
3831
3832static struct blk_mq_ops mtip_mq_ops = {
3833 .queue_rq = mtip_queue_rq,
3834 .map_queue = blk_mq_map_queue,
3835 .alloc_hctx = blk_mq_alloc_single_hw_queue,
3836 .free_hctx = blk_mq_free_single_hw_queue,
3837 .init_request = mtip_init_cmd,
3838 .exit_request = mtip_free_cmd,
3839};
3840
Sam Bradshaw88523a62011-08-30 08:34:26 -06003841/*
3842 * Block layer initialization function.
3843 *
3844 * This function is called once by the PCI layer for each P320
3845 * device that is connected to the system.
3846 *
3847 * @dd Pointer to the driver data structure.
3848 *
3849 * return value
3850 * 0 on success else an error code.
3851 */
Jens Axboe63166682011-09-27 21:27:43 -06003852static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003853{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003854 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003855 sector_t capacity;
3856 unsigned int index = 0;
3857 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003858 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003859
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003860 if (dd->disk)
3861 goto skip_create_disk; /* hw init done, before rebuild */
3862
Jens Axboeffc771b2014-05-09 09:42:02 -06003863 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003864 rv = -EINVAL;
3865 goto protocol_init_error;
3866 }
3867
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003868 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003869 if (dd->disk == NULL) {
3870 dev_err(&dd->pdev->dev,
3871 "Unable to allocate gendisk structure\n");
3872 rv = -EINVAL;
3873 goto alloc_disk_error;
3874 }
3875
3876 /* Generate the disk name, implemented same as in sd.c */
3877 do {
3878 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3879 goto ida_get_error;
3880
3881 spin_lock(&rssd_index_lock);
3882 rv = ida_get_new(&rssd_index_ida, &index);
3883 spin_unlock(&rssd_index_lock);
3884 } while (rv == -EAGAIN);
3885
3886 if (rv)
3887 goto ida_get_error;
3888
3889 rv = rssd_disk_name_format("rssd",
3890 index,
3891 dd->disk->disk_name,
3892 DISK_NAME_LEN);
3893 if (rv)
3894 goto disk_index_error;
3895
3896 dd->disk->driverfs_dev = &dd->pdev->dev;
3897 dd->disk->major = dd->major;
3898 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3899 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003900 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003901 dd->index = index;
3902
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003903 mtip_hw_debugfs_init(dd);
3904
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003905skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003906 memset(&dd->tags, 0, sizeof(dd->tags));
3907 dd->tags.ops = &mtip_mq_ops;
3908 dd->tags.nr_hw_queues = 1;
3909 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3910 dd->tags.reserved_tags = 1;
3911 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3912 dd->tags.numa_node = dd->numa_node;
3913 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3914 dd->tags.driver_data = dd;
3915
3916 rv = blk_mq_alloc_tag_set(&dd->tags);
3917 if (rv) {
3918 dev_err(&dd->pdev->dev,
3919 "Unable to allocate request queue\n");
3920 rv = -ENOMEM;
3921 goto block_queue_alloc_init_error;
3922 }
3923
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003924 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003925 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003926 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003927 dev_err(&dd->pdev->dev,
3928 "Unable to allocate request queue\n");
3929 rv = -ENOMEM;
3930 goto block_queue_alloc_init_error;
3931 }
3932
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003933 dd->disk->queue = dd->queue;
3934 dd->queue->queuedata = dd;
3935
Jens Axboeffc771b2014-05-09 09:42:02 -06003936 /* Initialize the protocol layer. */
3937 wait_for_rebuild = mtip_hw_get_identify(dd);
3938 if (wait_for_rebuild < 0) {
3939 dev_err(&dd->pdev->dev,
3940 "Protocol layer initialization failed\n");
3941 rv = -EINVAL;
3942 goto init_hw_cmds_error;
3943 }
3944
3945 /*
3946 * if rebuild pending, start the service thread, and delay the block
3947 * queue creation and add_disk()
3948 */
3949 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3950 goto start_service_thread;
3951
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003952 /* Set device limits. */
3953 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3954 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3955 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003956 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3957 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003958 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003959 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003960
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003961 /*
3962 * write back cache is not supported in the device. FUA depends on
3963 * write back cache support, hence setting flush support to zero.
3964 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003965 blk_queue_flush(dd->queue, 0);
3966
Asai Thambi S P15283462013-01-11 14:41:34 +01003967 /* Signal trim support */
3968 if (dd->trim_supp == true) {
3969 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3970 dd->queue->limits.discard_granularity = 4096;
3971 blk_queue_max_discard_sectors(dd->queue,
3972 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3973 dd->queue->limits.discard_zeroes_data = 0;
3974 }
3975
Sam Bradshaw88523a62011-08-30 08:34:26 -06003976 /* Set the capacity of the device in 512 byte sectors. */
3977 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3978 dev_warn(&dd->pdev->dev,
3979 "Could not read drive capacity\n");
3980 rv = -EIO;
3981 goto read_capacity_error;
3982 }
3983 set_capacity(dd->disk, capacity);
3984
3985 /* Enable the block device and add it to /dev */
3986 add_disk(dd->disk);
3987
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003988 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003989 /*
3990 * Now that the disk is active, initialize any sysfs attributes
3991 * managed by the protocol layer.
3992 */
3993 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3994 if (kobj) {
3995 mtip_hw_sysfs_init(dd, kobj);
3996 kobject_put(kobj);
3997 }
3998
Asai Thambi S P45038362012-04-09 08:35:38 +02003999 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004000 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004001 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004002 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004003
4004start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004005 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004006 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07004007 dd, dd->numa_node, "%s",
4008 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004009
4010 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004011 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004012 dd->mtip_svc_handler = NULL;
4013 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004014 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004015 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004016 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004017 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4018 rv = wait_for_rebuild;
4019
Sam Bradshaw88523a62011-08-30 08:34:26 -06004020 return rv;
4021
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004022kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004023 bdput(dd->bdev);
4024 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004025
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004026 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004027 del_gendisk(dd->disk);
4028
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004029read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004030init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004031 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004032 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004033block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004034 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004035disk_index_error:
4036 spin_lock(&rssd_index_lock);
4037 ida_remove(&rssd_index_ida, index);
4038 spin_unlock(&rssd_index_lock);
4039
4040ida_get_error:
4041 put_disk(dd->disk);
4042
4043alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004044 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004045
4046protocol_init_error:
4047 return rv;
4048}
4049
4050/*
4051 * Block layer deinitialization function.
4052 *
4053 * Called by the PCI layer as each P320 device is removed.
4054 *
4055 * @dd Pointer to the driver data structure.
4056 *
4057 * return value
4058 * 0
4059 */
Jens Axboe63166682011-09-27 21:27:43 -06004060static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004061{
4062 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004063
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004064 if (!dd->sr) {
4065 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004066
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004067 if (dd->mtip_svc_handler) {
4068 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4069 wake_up_interruptible(&dd->port->svc_wait);
4070 kthread_stop(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004071 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004072
4073 /* Clean up the sysfs attributes, if created */
4074 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4075 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4076 if (kobj) {
4077 mtip_hw_sysfs_exit(dd, kobj);
4078 kobject_put(kobj);
4079 }
4080 }
Jens Axboeffc771b2014-05-09 09:42:02 -06004081
4082 mtip_standby_drive(dd);
4083
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004084 /*
4085 * Delete our gendisk structure. This also removes the device
4086 * from /dev
4087 */
4088 if (dd->bdev) {
4089 bdput(dd->bdev);
4090 dd->bdev = NULL;
4091 }
4092 if (dd->disk) {
4093 if (dd->disk->queue) {
4094 del_gendisk(dd->disk);
4095 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004096 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004097 dd->queue = NULL;
4098 } else
4099 put_disk(dd->disk);
4100 }
4101 dd->disk = NULL;
4102
4103 spin_lock(&rssd_index_lock);
4104 ida_remove(&rssd_index_ida, dd->index);
4105 spin_unlock(&rssd_index_lock);
4106 } else {
4107 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4108 dd->disk->disk_name);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004109 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004110
4111 /* De-initialize the protocol layer. */
4112 mtip_hw_exit(dd);
4113
4114 return 0;
4115}
4116
4117/*
4118 * Function called by the PCI layer when just before the
4119 * machine shuts down.
4120 *
4121 * If a protocol layer shutdown function is present it will be called
4122 * by this function.
4123 *
4124 * @dd Pointer to the driver data structure.
4125 *
4126 * return value
4127 * 0
4128 */
Jens Axboe63166682011-09-27 21:27:43 -06004129static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004130{
Jens Axboeffc771b2014-05-09 09:42:02 -06004131 mtip_hw_shutdown(dd);
4132
Sam Bradshaw88523a62011-08-30 08:34:26 -06004133 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304134 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304135 dev_info(&dd->pdev->dev,
4136 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304137
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304138 if (dd->disk->queue) {
4139 del_gendisk(dd->disk);
4140 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004141 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304142 } else
4143 put_disk(dd->disk);
4144 dd->disk = NULL;
4145 dd->queue = NULL;
4146 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004147
4148 spin_lock(&rssd_index_lock);
4149 ida_remove(&rssd_index_ida, dd->index);
4150 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004151 return 0;
4152}
4153
Jens Axboe63166682011-09-27 21:27:43 -06004154static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004155{
4156 dev_info(&dd->pdev->dev,
4157 "Suspending %s ...\n", dd->disk->disk_name);
4158 mtip_hw_suspend(dd);
4159 return 0;
4160}
4161
Jens Axboe63166682011-09-27 21:27:43 -06004162static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004163{
4164 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4165 dd->disk->disk_name);
4166 mtip_hw_resume(dd);
4167 return 0;
4168}
4169
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004170static void drop_cpu(int cpu)
4171{
4172 cpu_use[cpu]--;
4173}
4174
4175static int get_least_used_cpu_on_node(int node)
4176{
4177 int cpu, least_used_cpu, least_cnt;
4178 const struct cpumask *node_mask;
4179
4180 node_mask = cpumask_of_node(node);
4181 least_used_cpu = cpumask_first(node_mask);
4182 least_cnt = cpu_use[least_used_cpu];
4183 cpu = least_used_cpu;
4184
4185 for_each_cpu(cpu, node_mask) {
4186 if (cpu_use[cpu] < least_cnt) {
4187 least_used_cpu = cpu;
4188 least_cnt = cpu_use[cpu];
4189 }
4190 }
4191 cpu_use[least_used_cpu]++;
4192 return least_used_cpu;
4193}
4194
4195/* Helper for selecting a node in round robin mode */
4196static inline int mtip_get_next_rr_node(void)
4197{
4198 static int next_node = -1;
4199
4200 if (next_node == -1) {
4201 next_node = first_online_node;
4202 return next_node;
4203 }
4204
4205 next_node = next_online_node(next_node);
4206 if (next_node == MAX_NUMNODES)
4207 next_node = first_online_node;
4208 return next_node;
4209}
4210
Fengguang Wu25bac122013-01-12 15:31:40 +08004211static DEFINE_HANDLER(0);
4212static DEFINE_HANDLER(1);
4213static DEFINE_HANDLER(2);
4214static DEFINE_HANDLER(3);
4215static DEFINE_HANDLER(4);
4216static DEFINE_HANDLER(5);
4217static DEFINE_HANDLER(6);
4218static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004219
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004220static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4221{
4222 int pos;
4223 unsigned short pcie_dev_ctrl;
4224
4225 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4226 if (pos) {
4227 pci_read_config_word(pdev,
4228 pos + PCI_EXP_DEVCTL,
4229 &pcie_dev_ctrl);
4230 if (pcie_dev_ctrl & (1 << 11) ||
4231 pcie_dev_ctrl & (1 << 4)) {
4232 dev_info(&dd->pdev->dev,
4233 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4234 pdev->vendor, pdev->device);
4235 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4236 PCI_EXP_DEVCTL_RELAX_EN);
4237 pci_write_config_word(pdev,
4238 pos + PCI_EXP_DEVCTL,
4239 pcie_dev_ctrl);
4240 }
4241 }
4242}
4243
4244static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4245{
4246 /*
4247 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4248 * device with device id 0x5aXX
4249 */
4250 if (pdev->bus && pdev->bus->self) {
4251 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4252 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4253 mtip_disable_link_opts(dd, pdev->bus->self);
4254 } else {
4255 /* Check further up the topology */
4256 struct pci_dev *parent_dev = pdev->bus->self;
4257 if (parent_dev->bus &&
4258 parent_dev->bus->parent &&
4259 parent_dev->bus->parent->self &&
4260 parent_dev->bus->parent->self->vendor ==
4261 PCI_VENDOR_ID_ATI &&
4262 (parent_dev->bus->parent->self->device &
4263 0xff00) == 0x5a00) {
4264 mtip_disable_link_opts(dd,
4265 parent_dev->bus->parent->self);
4266 }
4267 }
4268 }
4269}
4270
Sam Bradshaw88523a62011-08-30 08:34:26 -06004271/*
4272 * Called for each supported PCI device detected.
4273 *
4274 * This function allocates the private data structure, enables the
4275 * PCI device and then calls the block layer initialization function.
4276 *
4277 * return value
4278 * 0 on success else an error code.
4279 */
4280static int mtip_pci_probe(struct pci_dev *pdev,
4281 const struct pci_device_id *ent)
4282{
4283 int rv = 0;
4284 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004285 char cpu_list[256];
4286 const struct cpumask *node_mask;
4287 int cpu, i = 0, j = 0;
4288 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304289 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004290
4291 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004292 my_node = pcibus_to_node(pdev->bus);
4293 if (my_node != NUMA_NO_NODE) {
4294 if (!node_online(my_node))
4295 my_node = mtip_get_next_rr_node();
4296 } else {
4297 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4298 my_node = mtip_get_next_rr_node();
4299 }
4300 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4301 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004302 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004303
4304 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004305 if (dd == NULL) {
4306 dev_err(&pdev->dev,
4307 "Unable to allocate memory for driver data\n");
4308 return -ENOMEM;
4309 }
4310
Sam Bradshaw88523a62011-08-30 08:34:26 -06004311 /* Attach the private data to this PCI device. */
4312 pci_set_drvdata(pdev, dd);
4313
4314 rv = pcim_enable_device(pdev);
4315 if (rv < 0) {
4316 dev_err(&pdev->dev, "Unable to enable device\n");
4317 goto iomap_err;
4318 }
4319
4320 /* Map BAR5 to memory. */
4321 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4322 if (rv < 0) {
4323 dev_err(&pdev->dev, "Unable to map regions\n");
4324 goto iomap_err;
4325 }
4326
4327 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4328 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4329
4330 if (rv) {
4331 rv = pci_set_consistent_dma_mask(pdev,
4332 DMA_BIT_MASK(32));
4333 if (rv) {
4334 dev_warn(&pdev->dev,
4335 "64-bit DMA enable failed\n");
4336 goto setmask_err;
4337 }
4338 }
4339 }
4340
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004341 /* Copy the info we may need later into the private data structure. */
4342 dd->major = mtip_major;
4343 dd->instance = instance;
4344 dd->pdev = pdev;
4345 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004346
Asai Thambi S P0caff002013-04-03 19:56:21 +05304347 INIT_LIST_HEAD(&dd->online_list);
4348 INIT_LIST_HEAD(&dd->remove_list);
4349
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004350 memset(dd->workq_name, 0, 32);
4351 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4352
4353 dd->isr_workq = create_workqueue(dd->workq_name);
4354 if (!dd->isr_workq) {
4355 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004356 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004357 goto block_initialize_err;
4358 }
4359
4360 memset(cpu_list, 0, sizeof(cpu_list));
4361
4362 node_mask = cpumask_of_node(dd->numa_node);
4363 if (!cpumask_empty(node_mask)) {
4364 for_each_cpu(cpu, node_mask)
4365 {
4366 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4367 j = strlen(cpu_list);
4368 }
4369
4370 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4371 dd->numa_node,
4372 topology_physical_package_id(cpumask_first(node_mask)),
4373 nr_cpus_node(dd->numa_node),
4374 cpu_list);
4375 } else
4376 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4377
4378 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4379 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4380 cpu_to_node(dd->isr_binding), dd->isr_binding);
4381
4382 /* first worker context always runs in ISR */
4383 dd->work[0].cpu_binding = dd->isr_binding;
4384 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4385 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4386 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4387 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4388 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4389 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4390 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4391
4392 /* Log the bindings */
4393 for_each_present_cpu(cpu) {
4394 memset(cpu_list, 0, sizeof(cpu_list));
4395 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4396 if (dd->work[i].cpu_binding == cpu) {
4397 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4398 j = strlen(cpu_list);
4399 }
4400 }
4401 if (j)
4402 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4403 }
4404
4405 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4406 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4407 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4408 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4409 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4410 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4411 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4412 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4413
4414 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004415 rv = pci_enable_msi(pdev);
4416 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004417 dev_warn(&pdev->dev,
4418 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004419 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004420 }
4421
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004422 mtip_fix_ero_nosnoop(dd, pdev);
4423
Sam Bradshaw88523a62011-08-30 08:34:26 -06004424 /* Initialize the block layer. */
4425 rv = mtip_block_initialize(dd);
4426 if (rv < 0) {
4427 dev_err(&pdev->dev,
4428 "Unable to initialize block layer\n");
4429 goto block_initialize_err;
4430 }
4431
4432 /*
4433 * Increment the instance count so that each device has a unique
4434 * instance number.
4435 */
4436 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004437 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004438 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304439 else
4440 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304441
4442 /* Add to online list even if in ftl rebuild */
4443 spin_lock_irqsave(&dev_lock, flags);
4444 list_add(&dd->online_list, &online_list);
4445 spin_unlock_irqrestore(&dev_lock, flags);
4446
Sam Bradshaw88523a62011-08-30 08:34:26 -06004447 goto done;
4448
4449block_initialize_err:
4450 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004451
4452msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004453 if (dd->isr_workq) {
4454 flush_workqueue(dd->isr_workq);
4455 destroy_workqueue(dd->isr_workq);
4456 drop_cpu(dd->work[0].cpu_binding);
4457 drop_cpu(dd->work[1].cpu_binding);
4458 drop_cpu(dd->work[2].cpu_binding);
4459 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004460setmask_err:
4461 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4462
4463iomap_err:
4464 kfree(dd);
4465 pci_set_drvdata(pdev, NULL);
4466 return rv;
4467done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004468 return rv;
4469}
4470
4471/*
4472 * Called for each probed device when the device is removed or the
4473 * driver is unloaded.
4474 *
4475 * return value
4476 * None
4477 */
4478static void mtip_pci_remove(struct pci_dev *pdev)
4479{
4480 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004481 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004482
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004483 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004484
Asai Thambi S P0caff002013-04-03 19:56:21 +05304485 spin_lock_irqsave(&dev_lock, flags);
4486 list_del_init(&dd->online_list);
4487 list_add(&dd->remove_list, &removing_list);
4488 spin_unlock_irqrestore(&dev_lock, flags);
4489
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004490 mtip_check_surprise_removal(pdev);
4491 synchronize_irq(dd->pdev->irq);
4492
4493 /* Spin until workers are done */
4494 to = jiffies + msecs_to_jiffies(4000);
4495 do {
4496 msleep(20);
4497 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4498 time_before(jiffies, to));
4499
4500 if (atomic_read(&dd->irq_workers_active) != 0) {
4501 dev_warn(&dd->pdev->dev,
4502 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004503 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004504
4505 /* Clean up the block layer. */
4506 mtip_block_remove(dd);
4507
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004508 if (dd->isr_workq) {
4509 flush_workqueue(dd->isr_workq);
4510 destroy_workqueue(dd->isr_workq);
4511 drop_cpu(dd->work[0].cpu_binding);
4512 drop_cpu(dd->work[1].cpu_binding);
4513 drop_cpu(dd->work[2].cpu_binding);
4514 }
4515
Sam Bradshaw88523a62011-08-30 08:34:26 -06004516 pci_disable_msi(pdev);
4517
Asai Thambi S P0caff002013-04-03 19:56:21 +05304518 spin_lock_irqsave(&dev_lock, flags);
4519 list_del_init(&dd->remove_list);
4520 spin_unlock_irqrestore(&dev_lock, flags);
4521
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004522 if (!dd->sr)
4523 kfree(dd);
4524 else
4525 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4526
Sam Bradshaw88523a62011-08-30 08:34:26 -06004527 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004528 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004529}
4530
4531/*
4532 * Called for each probed device when the device is suspended.
4533 *
4534 * return value
4535 * 0 Success
4536 * <0 Error
4537 */
4538static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4539{
4540 int rv = 0;
4541 struct driver_data *dd = pci_get_drvdata(pdev);
4542
4543 if (!dd) {
4544 dev_err(&pdev->dev,
4545 "Driver private datastructure is NULL\n");
4546 return -EFAULT;
4547 }
4548
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004549 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004550
4551 /* Disable ports & interrupts then send standby immediate */
4552 rv = mtip_block_suspend(dd);
4553 if (rv < 0) {
4554 dev_err(&pdev->dev,
4555 "Failed to suspend controller\n");
4556 return rv;
4557 }
4558
4559 /*
4560 * Save the pci config space to pdev structure &
4561 * disable the device
4562 */
4563 pci_save_state(pdev);
4564 pci_disable_device(pdev);
4565
4566 /* Move to Low power state*/
4567 pci_set_power_state(pdev, PCI_D3hot);
4568
4569 return rv;
4570}
4571
4572/*
4573 * Called for each probed device when the device is resumed.
4574 *
4575 * return value
4576 * 0 Success
4577 * <0 Error
4578 */
4579static int mtip_pci_resume(struct pci_dev *pdev)
4580{
4581 int rv = 0;
4582 struct driver_data *dd;
4583
4584 dd = pci_get_drvdata(pdev);
4585 if (!dd) {
4586 dev_err(&pdev->dev,
4587 "Driver private datastructure is NULL\n");
4588 return -EFAULT;
4589 }
4590
4591 /* Move the device to active State */
4592 pci_set_power_state(pdev, PCI_D0);
4593
4594 /* Restore PCI configuration space */
4595 pci_restore_state(pdev);
4596
4597 /* Enable the PCI device*/
4598 rv = pcim_enable_device(pdev);
4599 if (rv < 0) {
4600 dev_err(&pdev->dev,
4601 "Failed to enable card during resume\n");
4602 goto err;
4603 }
4604 pci_set_master(pdev);
4605
4606 /*
4607 * Calls hbaReset, initPort, & startPort function
4608 * then enables interrupts
4609 */
4610 rv = mtip_block_resume(dd);
4611 if (rv < 0)
4612 dev_err(&pdev->dev, "Unable to resume\n");
4613
4614err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004615 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004616
4617 return rv;
4618}
4619
4620/*
4621 * Shutdown routine
4622 *
4623 * return value
4624 * None
4625 */
4626static void mtip_pci_shutdown(struct pci_dev *pdev)
4627{
4628 struct driver_data *dd = pci_get_drvdata(pdev);
4629 if (dd)
4630 mtip_block_shutdown(dd);
4631}
4632
Sam Bradshaw88523a62011-08-30 08:34:26 -06004633/* Table of device ids supported by this driver. */
4634static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304635 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4636 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4637 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4638 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4639 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4640 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4641 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004642 { 0 }
4643};
4644
4645/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004646static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004647 .name = MTIP_DRV_NAME,
4648 .id_table = mtip_pci_tbl,
4649 .probe = mtip_pci_probe,
4650 .remove = mtip_pci_remove,
4651 .suspend = mtip_pci_suspend,
4652 .resume = mtip_pci_resume,
4653 .shutdown = mtip_pci_shutdown,
4654};
4655
4656MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4657
4658/*
4659 * Module initialization function.
4660 *
4661 * Called once when the module is loaded. This function allocates a major
4662 * block device number to the Cyclone devices and registers the PCI layer
4663 * of the driver.
4664 *
4665 * Return value
4666 * 0 on success else error code.
4667 */
4668static int __init mtip_init(void)
4669{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004670 int error;
4671
Asai Thambi S P45422e72012-09-05 22:03:56 +05304672 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004673
Asai Thambi S P0caff002013-04-03 19:56:21 +05304674 spin_lock_init(&dev_lock);
4675
4676 INIT_LIST_HEAD(&online_list);
4677 INIT_LIST_HEAD(&removing_list);
4678
Sam Bradshaw88523a62011-08-30 08:34:26 -06004679 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004680 error = register_blkdev(0, MTIP_DRV_NAME);
4681 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304682 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004683 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004684 return -EBUSY;
4685 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004686 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004687
Asai Thambi S P0caff002013-04-03 19:56:21 +05304688 dfs_parent = debugfs_create_dir("rssd", NULL);
4689 if (IS_ERR_OR_NULL(dfs_parent)) {
4690 pr_warn("Error creating debugfs parent\n");
4691 dfs_parent = NULL;
4692 }
4693 if (dfs_parent) {
4694 dfs_device_status = debugfs_create_file("device_status",
4695 S_IRUGO, dfs_parent, NULL,
4696 &mtip_device_status_fops);
4697 if (IS_ERR_OR_NULL(dfs_device_status)) {
4698 pr_err("Error creating device_status node\n");
4699 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004700 }
4701 }
4702
Sam Bradshaw88523a62011-08-30 08:34:26 -06004703 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004704 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004705 if (error) {
4706 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004707 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004708 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004709
4710 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004711}
4712
4713/*
4714 * Module de-initialization function.
4715 *
4716 * Called once when the module is unloaded. This function deallocates
4717 * the major block device number allocated by mtip_init() and
4718 * unregisters the PCI layer of the driver.
4719 *
4720 * Return value
4721 * none
4722 */
4723static void __exit mtip_exit(void)
4724{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004725 /* Release the allocated major block device number. */
4726 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4727
4728 /* Unregister the PCI driver. */
4729 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004730
4731 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004732}
4733
4734MODULE_AUTHOR("Micron Technology, Inc");
4735MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4736MODULE_LICENSE("GPL");
4737MODULE_VERSION(MTIP_DRV_VERSION);
4738
4739module_init(mtip_init);
4740module_exit(mtip_exit);