blob: 27641bc83962e782476bca0988484f04d9c85538 [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>
34#include <linux/bio.h>
35#include <linux/dma-mapping.h>
36#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010037#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060038#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020039#include <linux/export.h>
Asai Thambi S P7b421d22012-06-04 12:44:02 -070040#include <linux/debugfs.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060041#include "mtip32xx.h"
42
43#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
Sam Bradshaw188b9f42014-01-15 10:14:57 -080044
45/* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
46#define AHCI_RX_FIS_SZ 0x100
47#define AHCI_RX_FIS_OFFSET 0x0
48#define AHCI_IDFY_SZ ATA_SECT_SIZE
49#define AHCI_IDFY_OFFSET 0x400
50#define AHCI_SECTBUF_SZ ATA_SECT_SIZE
51#define AHCI_SECTBUF_OFFSET 0x800
52#define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
53#define AHCI_SMARTBUF_OFFSET 0xC00
54/* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
55#define BLOCK_DMA_ALLOC_SZ 4096
56
57/* DMA region containing command table (should be 8192 bytes) */
58#define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
59#define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
60#define AHCI_CMD_TBL_OFFSET 0x0
61
62/* DMA region per command (contains header and SGL) */
63#define AHCI_CMD_TBL_HDR_SZ 0x80
64#define AHCI_CMD_TBL_HDR_OFFSET 0x0
65#define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
66#define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
67#define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
68
Sam Bradshaw88523a62011-08-30 08:34:26 -060069
Asai Thambi S P45038362012-04-09 08:35:38 +020070#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060071#define HOST_HSORG 0xFC
72#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
73#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
74#define HSORG_HWREV 0xFF00
75#define HSORG_STYLE 0x8
76#define HSORG_SLOTGROUPS 0x7
77
78#define PORT_COMMAND_ISSUE 0x38
79#define PORT_SDBV 0x7C
80
81#define PORT_OFFSET 0x100
82#define PORT_MEM_SIZE 0x80
83
84#define PORT_IRQ_ERR \
85 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
86 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
87 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
88 PORT_IRQ_OVERFLOW)
89#define PORT_IRQ_LEGACY \
90 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
91#define PORT_IRQ_HANDLED \
92 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
93 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
94 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
95#define DEF_PORT_IRQ \
96 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
97
98/* product numbers */
99#define MTIP_PRODUCT_UNKNOWN 0x00
100#define MTIP_PRODUCT_ASICFPGA 0x11
101
102/* Device instance number, incremented each time a device is probed. */
103static int instance;
104
Asai Thambi S P0caff002013-04-03 19:56:21 +0530105struct list_head online_list;
106struct list_head removing_list;
107spinlock_t dev_lock;
108
Sam Bradshaw88523a62011-08-30 08:34:26 -0600109/*
110 * Global variable used to hold the major block device number
111 * allocated in mtip_init().
112 */
Jens Axboe3ff147d2011-09-27 21:33:53 -0600113static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -0700114static struct dentry *dfs_parent;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530115static struct dentry *dfs_device_status;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600116
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800117static u32 cpu_use[NR_CPUS];
118
Sam Bradshaw88523a62011-08-30 08:34:26 -0600119static DEFINE_SPINLOCK(rssd_index_lock);
120static DEFINE_IDA(rssd_index_ida);
121
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100122static int mtip_block_initialize(struct driver_data *dd);
123
Jens Axboe16d02c02011-09-27 15:50:01 -0600124#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600125struct mtip_compat_ide_task_request_s {
126 __u8 io_ports[8];
127 __u8 hob_ports[8];
128 ide_reg_valid_t out_flags;
129 ide_reg_valid_t in_flags;
130 int data_phase;
131 int req_cmd;
132 compat_ulong_t out_size;
133 compat_ulong_t in_size;
134};
Jens Axboe16d02c02011-09-27 15:50:01 -0600135#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600136
Sam Bradshaw88523a62011-08-30 08:34:26 -0600137/*
Jens Axboe63166682011-09-27 21:27:43 -0600138 * This function check_for_surprise_removal is called
139 * while card is removed from the system and it will
140 * read the vendor id from the configration space
141 *
142 * @pdev Pointer to the pci_dev structure.
143 *
144 * return value
145 * true if device removed, else false
146 */
147static bool mtip_check_surprise_removal(struct pci_dev *pdev)
148{
149 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600150 struct driver_data *dd = pci_get_drvdata(pdev);
151
152 if (dd->sr)
153 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600154
155 /* Read the vendorID from the configuration space */
156 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600157 if (vendor_id == 0xFFFF) {
158 dd->sr = true;
159 if (dd->queue)
160 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
161 else
162 dev_warn(&dd->pdev->dev,
163 "%s: dd->queue is NULL\n", __func__);
164 if (dd->port) {
165 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
166 wake_up_interruptible(&dd->port->svc_wait);
167 } else
168 dev_warn(&dd->pdev->dev,
169 "%s: dd->port is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600170 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600171 }
172
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600173 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600174}
175
176/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600177 * Obtain an empty command slot.
178 *
179 * This function needs to be reentrant since it could be called
180 * at the same time on multiple CPUs. The allocation of the
181 * command slot must be atomic.
182 *
183 * @port Pointer to the port data structure.
184 *
185 * return value
186 * >= 0 Index of command slot obtained.
187 * -1 No command slots available.
188 */
189static int get_slot(struct mtip_port *port)
190{
191 int slot, i;
192 unsigned int num_command_slots = port->dd->slot_groups * 32;
193
194 /*
195 * Try 10 times, because there is a small race here.
196 * that's ok, because it's still cheaper than a lock.
197 *
198 * Race: Since this section is not protected by lock, same bit
199 * could be chosen by different process contexts running in
200 * different processor. So instead of costly lock, we are going
201 * with loop.
202 */
203 for (i = 0; i < 10; i++) {
204 slot = find_next_zero_bit(port->allocated,
205 num_command_slots, 1);
206 if ((slot < num_command_slots) &&
207 (!test_and_set_bit(slot, port->allocated)))
208 return slot;
209 }
210 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
211
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600212 mtip_check_surprise_removal(port->dd->pdev);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600213 return -1;
214}
215
216/*
217 * Release a command slot.
218 *
219 * @port Pointer to the port data structure.
220 * @tag Tag of command to release
221 *
222 * return value
223 * None
224 */
225static inline void release_slot(struct mtip_port *port, int tag)
226{
227 smp_mb__before_clear_bit();
228 clear_bit(tag, port->allocated);
229 smp_mb__after_clear_bit();
230}
231
232/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600233 * IO completion function.
234 *
235 * This completion function is called by the driver ISR when a
236 * command that was issued by the kernel completes. It first calls the
237 * asynchronous completion function which normally calls back into the block
238 * layer passing the asynchronous callback data, then unmaps the
239 * scatter list associated with the completed command, and finally
240 * clears the allocated bit associated with the completed command.
241 *
242 * @port Pointer to the port data structure.
243 * @tag Tag of the command.
244 * @data Pointer to driver_data.
245 * @status Completion status.
246 *
247 * return value
248 * None
249 */
250static void mtip_async_complete(struct mtip_port *port,
251 int tag,
252 void *data,
253 int status)
254{
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700255 struct mtip_cmd *cmd;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600256 struct driver_data *dd = data;
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700257 int unaligned, cb_status = status ? -EIO : 0;
258 void (*func)(void *, int);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600259
260 if (unlikely(!dd) || unlikely(!port))
261 return;
262
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700263 cmd = &port->commands[tag];
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600264
265 if (unlikely(status == PORT_IRQ_TF_ERR)) {
266 dev_warn(&port->dd->pdev->dev,
267 "Command tag %d failed due to TFE\n", tag);
268 }
269
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700270 /* Clear the active flag */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600271 atomic_set(&port->commands[tag].active, 0);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600272
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600273 /* Upper layer callback */
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700274 func = cmd->async_callback;
275 if (likely(func && cmpxchg(&cmd->async_callback, func, 0) == func)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600276
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700277 /* Unmap the DMA scatter list entries */
278 dma_unmap_sg(&dd->pdev->dev,
279 cmd->sg,
280 cmd->scatter_ents,
281 cmd->direction);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600282
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700283 func(cmd->async_data, cb_status);
284 unaligned = cmd->unaligned;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600285
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700286 /* Clear the allocated bit for the command */
287 release_slot(port, tag);
288
289 if (unlikely(unaligned))
290 up(&port->cmd_slot_unal);
291 else
292 up(&port->cmd_slot);
293 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600294}
295
296/*
297 * This function is called for clean the pending command in the
298 * command slot during the surprise removal of device and return
299 * error to the upper layer.
300 *
301 * @dd Pointer to the DRIVER_DATA structure.
302 *
303 * return value
304 * None
305 */
306static void mtip_command_cleanup(struct driver_data *dd)
307{
308 int tag = 0;
309 struct mtip_cmd *cmd;
310 struct mtip_port *port = dd->port;
311 unsigned int num_cmd_slots = dd->slot_groups * 32;
312
313 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
314 return;
315
316 if (!port)
317 return;
318
319 cmd = &port->commands[MTIP_TAG_INTERNAL];
320 if (atomic_read(&cmd->active))
321 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) &
322 (1 << MTIP_TAG_INTERNAL))
323 if (cmd->comp_func)
324 cmd->comp_func(port, MTIP_TAG_INTERNAL,
325 cmd->comp_data, -ENODEV);
326
327 while (1) {
328 tag = find_next_bit(port->allocated, num_cmd_slots, tag);
329 if (tag >= num_cmd_slots)
330 break;
331
332 cmd = &port->commands[tag];
333 if (atomic_read(&cmd->active))
334 mtip_async_complete(port, tag, dd, -ENODEV);
335 }
336
337 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
338}
339
340/*
Jens Axboe63166682011-09-27 21:27:43 -0600341 * Reset the HBA (without sleeping)
342 *
Jens Axboe63166682011-09-27 21:27:43 -0600343 * @dd Pointer to the driver data structure.
344 *
345 * return value
346 * 0 The reset was successful.
347 * -1 The HBA Reset bit did not clear.
348 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530349static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600350{
351 unsigned long timeout;
352
Jens Axboe63166682011-09-27 21:27:43 -0600353 /* Set the reset bit */
354 writel(HOST_RESET, dd->mmio + HOST_CTL);
355
356 /* Flush */
357 readl(dd->mmio + HOST_CTL);
358
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530359 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
360 timeout = jiffies + msecs_to_jiffies(2000);
361 do {
362 mdelay(10);
363 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
364 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600365
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530366 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
367 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200368
Jens Axboe63166682011-09-27 21:27:43 -0600369 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
370 return -1;
371
372 return 0;
373}
374
375/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600376 * Issue a command to the hardware.
377 *
378 * Set the appropriate bit in the s_active and Command Issue hardware
379 * registers, causing hardware command processing to begin.
380 *
381 * @port Pointer to the port structure.
382 * @tag The tag of the command to be issued.
383 *
384 * return value
385 * None
386 */
387static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
388{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800389 int group = tag >> 5;
390
Sam Bradshaw88523a62011-08-30 08:34:26 -0600391 atomic_set(&port->commands[tag].active, 1);
392
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800393 /* guard SACT and CI registers */
394 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600395 writel((1 << MTIP_TAG_BIT(tag)),
396 port->s_active[MTIP_TAG_INDEX(tag)]);
397 writel((1 << MTIP_TAG_BIT(tag)),
398 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800399 spin_unlock(&port->cmd_issue_lock[group]);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200400
401 /* Set the command's timeout value.*/
402 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
403 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600404}
405
406/*
Jens Axboe63166682011-09-27 21:27:43 -0600407 * Enable/disable the reception of FIS
408 *
409 * @port Pointer to the port data structure
410 * @enable 1 to enable, 0 to disable
411 *
412 * return value
413 * Previous state: 1 enabled, 0 disabled
414 */
415static int mtip_enable_fis(struct mtip_port *port, int enable)
416{
417 u32 tmp;
418
419 /* enable FIS reception */
420 tmp = readl(port->mmio + PORT_CMD);
421 if (enable)
422 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
423 else
424 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
425
426 /* Flush */
427 readl(port->mmio + PORT_CMD);
428
429 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
430}
431
432/*
433 * Enable/disable the DMA engine
434 *
435 * @port Pointer to the port data structure
436 * @enable 1 to enable, 0 to disable
437 *
438 * return value
439 * Previous state: 1 enabled, 0 disabled.
440 */
441static int mtip_enable_engine(struct mtip_port *port, int enable)
442{
443 u32 tmp;
444
445 /* enable FIS reception */
446 tmp = readl(port->mmio + PORT_CMD);
447 if (enable)
448 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
449 else
450 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
451
452 readl(port->mmio + PORT_CMD);
453 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
454}
455
456/*
457 * Enables the port DMA engine and FIS reception.
458 *
459 * return value
460 * None
461 */
462static inline void mtip_start_port(struct mtip_port *port)
463{
464 /* Enable FIS reception */
465 mtip_enable_fis(port, 1);
466
467 /* Enable the DMA engine */
468 mtip_enable_engine(port, 1);
469}
470
471/*
472 * Deinitialize a port by disabling port interrupts, the DMA engine,
473 * and FIS reception.
474 *
475 * @port Pointer to the port structure
476 *
477 * return value
478 * None
479 */
480static inline void mtip_deinit_port(struct mtip_port *port)
481{
482 /* Disable interrupts on this port */
483 writel(0, port->mmio + PORT_IRQ_MASK);
484
485 /* Disable the DMA engine */
486 mtip_enable_engine(port, 0);
487
488 /* Disable FIS reception */
489 mtip_enable_fis(port, 0);
490}
491
492/*
493 * Initialize a port.
494 *
495 * This function deinitializes the port by calling mtip_deinit_port() and
496 * then initializes it by setting the command header and RX FIS addresses,
497 * clearing the SError register and any pending port interrupts before
498 * re-enabling the default set of port interrupts.
499 *
500 * @port Pointer to the port structure.
501 *
502 * return value
503 * None
504 */
505static void mtip_init_port(struct mtip_port *port)
506{
507 int i;
508 mtip_deinit_port(port);
509
510 /* Program the command list base and FIS base addresses */
511 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
512 writel((port->command_list_dma >> 16) >> 16,
513 port->mmio + PORT_LST_ADDR_HI);
514 writel((port->rxfis_dma >> 16) >> 16,
515 port->mmio + PORT_FIS_ADDR_HI);
516 }
517
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100518 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600519 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100520 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600521
522 /* Clear SError */
523 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
524
525 /* reset the completed registers.*/
526 for (i = 0; i < port->dd->slot_groups; i++)
527 writel(0xFFFFFFFF, port->completed[i]);
528
529 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700530 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600531
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100532 /* Clear any pending interrupts on the HBA. */
533 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
534 port->dd->mmio + HOST_IRQ_STAT);
535
Jens Axboe63166682011-09-27 21:27:43 -0600536 /* Enable port interrupts */
537 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
538}
539
540/*
541 * Restart a port
542 *
543 * @port Pointer to the port data structure.
544 *
545 * return value
546 * None
547 */
548static void mtip_restart_port(struct mtip_port *port)
549{
550 unsigned long timeout;
551
552 /* Disable the DMA engine */
553 mtip_enable_engine(port, 0);
554
555 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
556 timeout = jiffies + msecs_to_jiffies(500);
557 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
558 && time_before(jiffies, timeout))
559 ;
560
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200561 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200562 return;
563
Jens Axboe63166682011-09-27 21:27:43 -0600564 /*
565 * Chip quirk: escalate to hba reset if
566 * PxCMD.CR not clear after 500 ms
567 */
568 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
569 dev_warn(&port->dd->pdev->dev,
570 "PxCMD.CR not clear, escalating reset\n");
571
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530572 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600573 dev_err(&port->dd->pdev->dev,
574 "HBA reset escalation failed.\n");
575
576 /* 30 ms delay before com reset to quiesce chip */
577 mdelay(30);
578 }
579
580 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
581
582 /* Set PxSCTL.DET */
583 writel(readl(port->mmio + PORT_SCR_CTL) |
584 1, port->mmio + PORT_SCR_CTL);
585 readl(port->mmio + PORT_SCR_CTL);
586
587 /* Wait 1 ms to quiesce chip function */
588 timeout = jiffies + msecs_to_jiffies(1);
589 while (time_before(jiffies, timeout))
590 ;
591
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200592 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200593 return;
594
Jens Axboe63166682011-09-27 21:27:43 -0600595 /* Clear PxSCTL.DET */
596 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
597 port->mmio + PORT_SCR_CTL);
598 readl(port->mmio + PORT_SCR_CTL);
599
600 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
601 timeout = jiffies + msecs_to_jiffies(500);
602 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
603 && time_before(jiffies, timeout))
604 ;
605
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200606 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200607 return;
608
Jens Axboe63166682011-09-27 21:27:43 -0600609 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
610 dev_warn(&port->dd->pdev->dev,
611 "COM reset failed\n");
612
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100613 mtip_init_port(port);
614 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600615
Jens Axboe63166682011-09-27 21:27:43 -0600616}
617
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530618static int mtip_device_reset(struct driver_data *dd)
619{
620 int rv = 0;
621
622 if (mtip_check_surprise_removal(dd->pdev))
623 return 0;
624
625 if (mtip_hba_reset(dd) < 0)
626 rv = -EFAULT;
627
628 mdelay(1);
629 mtip_init_port(dd->port);
630 mtip_start_port(dd->port);
631
632 /* Enable interrupts on the HBA. */
633 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
634 dd->mmio + HOST_CTL);
635 return rv;
636}
637
Jens Axboe63166682011-09-27 21:27:43 -0600638/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200639 * Helper function for tag logging
640 */
641static void print_tags(struct driver_data *dd,
642 char *msg,
643 unsigned long *tagbits,
644 int cnt)
645{
646 unsigned char tagmap[128];
647 int group, tagmap_len = 0;
648
649 memset(tagmap, 0, sizeof(tagmap));
650 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
651 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
652 tagbits[group-1]);
653 dev_warn(&dd->pdev->dev,
654 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
655}
656
657/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600658 * Called periodically to see if any read/write commands are
659 * taking too long to complete.
660 *
661 * @data Pointer to the PORT data structure.
662 *
663 * return value
664 * None
665 */
Jens Axboe63166682011-09-27 21:27:43 -0600666static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600667{
668 struct mtip_port *port = (struct mtip_port *) data;
669 struct host_to_dev_fis *fis;
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700670 struct mtip_cmd *cmd;
671 int unaligned, tag, cmdto_cnt = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600672 unsigned int bit, group;
Wei Yongjun298d8012012-11-08 17:35:38 +0800673 unsigned int num_command_slots;
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200674 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700675 void (*func)(void *, int);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600676
677 if (unlikely(!port))
678 return;
679
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600680 if (unlikely(port->dd->sr))
681 return;
682
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200683 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600684 mod_timer(&port->cmd_timer,
685 jiffies + msecs_to_jiffies(30000));
686 return;
687 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200688 /* clear the tag accumulator */
689 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
Wei Yongjun298d8012012-11-08 17:35:38 +0800690 num_command_slots = port->dd->slot_groups * 32;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600691
692 for (tag = 0; tag < num_command_slots; tag++) {
693 /*
694 * Skip internal command slot as it has
695 * its own timeout mechanism
696 */
697 if (tag == MTIP_TAG_INTERNAL)
698 continue;
699
700 if (atomic_read(&port->commands[tag].active) &&
701 (time_after(jiffies, port->commands[tag].comp_time))) {
702 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100703 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600704
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700705 cmd = &port->commands[tag];
706 fis = (struct host_to_dev_fis *) cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600707
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200708 set_bit(tag, tagaccum);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600709 cmdto_cnt++;
710 if (cmdto_cnt == 1)
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200711 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600712
713 /*
714 * Clear the completed bit. This should prevent
715 * any interrupt handlers from trying to retire
716 * the command.
717 */
718 writel(1 << bit, port->completed[group]);
719
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700720 /* Clear the active flag for the command */
Sam Bradshaw88523a62011-08-30 08:34:26 -0600721 atomic_set(&port->commands[tag].active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600722
Sam Bradshaw5eb92912014-03-13 14:33:30 -0700723 func = cmd->async_callback;
724 if (func &&
725 cmpxchg(&cmd->async_callback, func, 0) == func) {
726
727 /* Unmap the DMA scatter list entries */
728 dma_unmap_sg(&port->dd->pdev->dev,
729 cmd->sg,
730 cmd->scatter_ents,
731 cmd->direction);
732
733 func(cmd->async_data, -EIO);
734 unaligned = cmd->unaligned;
735
736 /* Clear the allocated bit for the command. */
737 release_slot(port, tag);
738
739 if (unaligned)
740 up(&port->cmd_slot_unal);
741 else
742 up(&port->cmd_slot);
743 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600744 }
745 }
746
Asai Thambi S P47cd4b32013-01-11 18:46:04 +0530747 if (cmdto_cnt) {
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200748 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
Asai Thambi S P47cd4b32013-01-11 18:46:04 +0530749 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530750 mtip_device_reset(port->dd);
Asai Thambi S P47cd4b32013-01-11 18:46:04 +0530751 wake_up_interruptible(&port->svc_wait);
752 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200753 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600754 }
755
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200756 if (port->ic_pause_timer) {
757 to = port->ic_pause_timer + msecs_to_jiffies(1000);
758 if (time_after(jiffies, to)) {
759 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
760 port->ic_pause_timer = 0;
761 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
762 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
763 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
764 wake_up_interruptible(&port->svc_wait);
765 }
766
767
768 }
769 }
770
Sam Bradshaw88523a62011-08-30 08:34:26 -0600771 /* Restart the timer */
772 mod_timer(&port->cmd_timer,
773 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
774}
775
776/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600777 * Internal command completion callback function.
778 *
779 * This function is normally called by the driver ISR when an internal
780 * command completed. This function signals the command completion by
781 * calling complete().
782 *
783 * @port Pointer to the port data structure.
784 * @tag Tag of the command that has completed.
785 * @data Pointer to a completion structure.
786 * @status Completion status.
787 *
788 * return value
789 * None
790 */
791static void mtip_completion(struct mtip_port *port,
792 int tag,
793 void *data,
794 int status)
795{
796 struct mtip_cmd *command = &port->commands[tag];
797 struct completion *waiting = data;
798 if (unlikely(status == PORT_IRQ_TF_ERR))
799 dev_warn(&port->dd->pdev->dev,
800 "Internal command %d completed with TFE\n", tag);
801
802 command->async_callback = NULL;
803 command->comp_func = NULL;
804
805 complete(waiting);
806}
807
Asai Thambi S P8182b492012-04-09 08:35:38 +0200808static void mtip_null_completion(struct mtip_port *port,
809 int tag,
810 void *data,
811 int status)
812{
813 return;
814}
815
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200816static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
817 dma_addr_t buffer_dma, unsigned int sectors);
818static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
819 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600820/*
821 * Handle an error.
822 *
823 * @dd Pointer to the DRIVER_DATA structure.
824 *
825 * return value
826 * None
827 */
828static void mtip_handle_tfe(struct driver_data *dd)
829{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200830 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200832 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600833 u32 completed;
834 struct host_to_dev_fis *fis;
835 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200836 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200837 unsigned char *buf;
838 char *fail_reason = NULL;
839 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840
841 dev_warn(&dd->pdev->dev, "Taskfile error\n");
842
843 port = dd->port;
844
845 /* Stop the timer to prevent command timeouts. */
846 del_timer(&port->cmd_timer);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700847 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
848
849 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
850 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
851 cmd = &port->commands[MTIP_TAG_INTERNAL];
852 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
853
854 atomic_inc(&cmd->active); /* active > 1 indicates error */
855 if (cmd->comp_data && cmd->comp_func) {
856 cmd->comp_func(port, MTIP_TAG_INTERNAL,
857 cmd->comp_data, PORT_IRQ_TF_ERR);
858 }
859 goto handle_tfe_exit;
860 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600861
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200862 /* clear the tag accumulator */
863 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
864
Sam Bradshaw88523a62011-08-30 08:34:26 -0600865 /* Loop through all the groups */
866 for (group = 0; group < dd->slot_groups; group++) {
867 completed = readl(port->completed[group]);
868
869 /* clear completed status register in the hardware.*/
870 writel(completed, port->completed[group]);
871
Sam Bradshaw88523a62011-08-30 08:34:26 -0600872 /* Process successfully completed commands */
873 for (bit = 0; bit < 32 && completed; bit++) {
874 if (!(completed & (1<<bit)))
875 continue;
876 tag = (group << 5) + bit;
877
878 /* Skip the internal command slot */
879 if (tag == MTIP_TAG_INTERNAL)
880 continue;
881
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200882 cmd = &port->commands[tag];
883 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600884 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200885 cmd_cnt++;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200886 atomic_set(&cmd->active, 0);
887 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600888 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200889 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600890 0);
891 } else {
892 dev_err(&port->dd->pdev->dev,
893 "Missing completion func for tag %d",
894 tag);
895 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600896 /* don't proceed further */
897 return;
898 }
899 }
900 }
901 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200902
903 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600904
905 /* Restart the port */
906 mdelay(20);
907 mtip_restart_port(port);
908
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200909 /* Trying to determine the cause of the error */
910 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
911 dd->port->log_buf,
912 dd->port->log_buf_dma, 1);
913 if (rv) {
914 dev_warn(&dd->pdev->dev,
915 "Error in READ LOG EXT (10h) command\n");
916 /* non-critical error, don't fail the load */
917 } else {
918 buf = (unsigned char *)dd->port->log_buf;
919 if (buf[259] & 0x1) {
920 dev_info(&dd->pdev->dev,
921 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200922 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200923 fail_all_ncq_write = 1;
924 fail_reason = "write protect";
925 }
926 if (buf[288] == 0xF7) {
927 dev_info(&dd->pdev->dev,
928 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200929 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200930 fail_all_ncq_cmds = 1;
931 fail_reason = "thermal shutdown";
932 }
933 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700934 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200935 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700936 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200937 fail_all_ncq_cmds = 1;
938 fail_reason = "rebuild failed";
939 }
940 }
941
Sam Bradshaw88523a62011-08-30 08:34:26 -0600942 /* clear the tag accumulator */
943 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
944
945 /* Loop through all the groups */
946 for (group = 0; group < dd->slot_groups; group++) {
947 for (bit = 0; bit < 32; bit++) {
948 reissue = 1;
949 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200950 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951
952 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200953 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600954 continue;
955
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200956 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600957
958 /* Should re-issue? */
959 if (tag == MTIP_TAG_INTERNAL ||
960 fis->command == ATA_CMD_SET_FEATURES)
961 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200962 else {
963 if (fail_all_ncq_cmds ||
964 (fail_all_ncq_write &&
965 fis->command == ATA_CMD_FPDMA_WRITE)) {
966 dev_warn(&dd->pdev->dev,
967 " Fail: %s w/tag %d [%s].\n",
968 fis->command == ATA_CMD_FPDMA_WRITE ?
969 "write" : "read",
970 tag,
971 fail_reason != NULL ?
972 fail_reason : "unknown");
973 atomic_set(&cmd->active, 0);
974 if (cmd->comp_func) {
975 cmd->comp_func(port, tag,
976 cmd->comp_data,
977 -ENODATA);
978 }
979 continue;
980 }
981 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600982
983 /*
984 * First check if this command has
985 * exceeded its retries.
986 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200987 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600988
989 set_bit(tag, tagaccum);
990
Sam Bradshaw88523a62011-08-30 08:34:26 -0600991 /* Re-issue the command. */
992 mtip_issue_ncq_command(port, tag);
993
994 continue;
995 }
996
997 /* Retire a command that will not be reissued */
998 dev_warn(&port->dd->pdev->dev,
999 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001000 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001001
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001002 if (cmd->comp_func)
1003 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001004 port,
1005 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001006 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001007 PORT_IRQ_TF_ERR);
1008 else
1009 dev_warn(&port->dd->pdev->dev,
1010 "Bad completion for tag %d\n",
1011 tag);
1012 }
1013 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +02001014 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001015
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001016handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001017 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001018 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001019 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001020
1021 mod_timer(&port->cmd_timer,
1022 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
1023}
1024
1025/*
1026 * Handle a set device bits interrupt
1027 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001028static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
1029 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001030{
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001031 struct driver_data *dd = port->dd;
1032 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033 struct mtip_cmd *command;
1034
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001035 if (!completed) {
1036 WARN_ON_ONCE(!completed);
1037 return;
1038 }
1039 /* clear completed status register in the hardware.*/
1040 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001041
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001042 /* Process completed commands. */
1043 for (bit = 0; (bit < 32) && completed; bit++) {
1044 if (completed & 0x01) {
1045 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001046
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001047 /* skip internal command slot. */
1048 if (unlikely(tag == MTIP_TAG_INTERNAL))
1049 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001051 command = &port->commands[tag];
1052 /* make internal callback */
1053 if (likely(command->comp_func)) {
1054 command->comp_func(
1055 port,
1056 tag,
1057 command->comp_data,
1058 0);
1059 } else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06001060 dev_dbg(&dd->pdev->dev,
1061 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001062 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001063
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001064 if (mtip_check_surprise_removal(
1065 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001066 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001067 }
1068 }
1069 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001070 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001071 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001072
1073 /* If last, re-enable interrupts */
1074 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1075 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001076}
1077
1078/*
1079 * Process legacy pio and d2h interrupts
1080 */
1081static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1082{
1083 struct mtip_port *port = dd->port;
1084 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1085
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001086 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001087 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -06001088 & (1 << MTIP_TAG_INTERNAL))) {
1089 if (cmd->comp_func) {
1090 cmd->comp_func(port,
1091 MTIP_TAG_INTERNAL,
1092 cmd->comp_data,
1093 0);
1094 return;
1095 }
1096 }
1097
Sam Bradshaw88523a62011-08-30 08:34:26 -06001098 return;
1099}
1100
1101/*
1102 * Demux and handle errors
1103 */
1104static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1105{
1106 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1107 mtip_handle_tfe(dd);
1108
1109 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1110 dev_warn(&dd->pdev->dev,
1111 "Clearing PxSERR.DIAG.x\n");
1112 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1113 }
1114
1115 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1116 dev_warn(&dd->pdev->dev,
1117 "Clearing PxSERR.DIAG.n\n");
1118 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1119 }
1120
1121 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1122 dev_warn(&dd->pdev->dev,
1123 "Port stat errors %x unhandled\n",
1124 (port_stat & ~PORT_IRQ_HANDLED));
1125 }
1126}
1127
1128static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1129{
1130 struct driver_data *dd = (struct driver_data *) data;
1131 struct mtip_port *port = dd->port;
1132 u32 hba_stat, port_stat;
1133 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001134 int do_irq_enable = 1, i, workers;
1135 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001136
1137 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1138 if (hba_stat) {
1139 rv = IRQ_HANDLED;
1140
1141 /* Acknowledge the interrupt status on the port.*/
1142 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1143 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1144
1145 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001146 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1147 do_irq_enable = 0;
1148 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1149
1150 /* Start at 1: group zero is always local? */
1151 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1152 i++) {
1153 twork = &dd->work[i];
1154 twork->completed = readl(port->completed[i]);
1155 if (twork->completed)
1156 workers++;
1157 }
1158
1159 atomic_set(&dd->irq_workers_active, workers);
1160 if (workers) {
1161 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1162 twork = &dd->work[i];
1163 if (twork->completed)
1164 queue_work_on(
1165 twork->cpu_binding,
1166 dd->isr_workq,
1167 &twork->work);
1168 }
1169
1170 if (likely(dd->work[0].completed))
1171 mtip_workq_sdbfx(port, 0,
1172 dd->work[0].completed);
1173
1174 } else {
1175 /*
1176 * Chip quirk: SDB interrupt but nothing
1177 * to complete
1178 */
1179 do_irq_enable = 1;
1180 }
1181 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001182
1183 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1184 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001185 /* don't proceed further */
1186 return IRQ_HANDLED;
1187 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001188 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001189 &dd->dd_flag))
1190 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001191
1192 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1193 }
1194
1195 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1196 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1197 }
1198
1199 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001200 if (unlikely(do_irq_enable))
1201 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001202
1203 return rv;
1204}
1205
1206/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001207 * HBA interrupt subroutine.
1208 *
1209 * @irq IRQ number.
1210 * @instance Pointer to the driver data structure.
1211 *
1212 * return value
1213 * IRQ_HANDLED A HBA interrupt was pending and handled.
1214 * IRQ_NONE This interrupt was not for the HBA.
1215 */
1216static irqreturn_t mtip_irq_handler(int irq, void *instance)
1217{
1218 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001219
1220 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001221}
1222
1223static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1224{
1225 atomic_set(&port->commands[tag].active, 1);
1226 writel(1 << MTIP_TAG_BIT(tag),
1227 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1228}
1229
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001230static bool mtip_pause_ncq(struct mtip_port *port,
1231 struct host_to_dev_fis *fis)
1232{
1233 struct host_to_dev_fis *reply;
1234 unsigned long task_file_data;
1235
1236 reply = port->rxfis + RX_FIS_D2H_REG;
1237 task_file_data = readl(port->mmio+PORT_TFDATA);
1238
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301239 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1240 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1241
1242 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001243 return false;
1244
1245 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1246 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301247 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001248 port->ic_pause_timer = jiffies;
1249 return true;
1250 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1251 (fis->features == 0x03)) {
1252 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1253 port->ic_pause_timer = jiffies;
1254 return true;
1255 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1256 ((fis->command == 0xFC) &&
1257 (fis->features == 0x27 || fis->features == 0x72 ||
1258 fis->features == 0x62 || fis->features == 0x26))) {
1259 /* Com reset after secure erase or lowlevel format */
1260 mtip_restart_port(port);
1261 return false;
1262 }
1263
1264 return false;
1265}
1266
Sam Bradshaw88523a62011-08-30 08:34:26 -06001267/*
1268 * Wait for port to quiesce
1269 *
1270 * @port Pointer to port data structure
1271 * @timeout Max duration to wait (ms)
1272 *
1273 * return value
1274 * 0 Success
1275 * -EBUSY Commands still active
1276 */
1277static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1278{
1279 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001280 unsigned int n;
1281 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001282
1283 to = jiffies + msecs_to_jiffies(timeout);
1284 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001285 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1286 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001287 msleep(20);
1288 continue; /* svc thd is actively issuing commands */
1289 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001290 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001291 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001292 /*
1293 * Ignore s_active bit 0 of array element 0.
1294 * This bit will always be set
1295 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001296 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001297 for (n = 1; n < port->dd->slot_groups; n++)
1298 active |= readl(port->s_active[n]);
1299
1300 if (!active)
1301 break;
1302
1303 msleep(20);
1304 } while (time_before(jiffies, to));
1305
1306 return active ? -EBUSY : 0;
1307}
1308
1309/*
1310 * Execute an internal command and wait for the completion.
1311 *
1312 * @port Pointer to the port data structure.
1313 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001314 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001315 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001316 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001317 * @opts Command header options, excluding the FIS length
1318 * and the number of PRD entries.
1319 * @timeout Time in ms to wait for the command to complete.
1320 *
1321 * return value
1322 * 0 Command completed successfully.
1323 * -EFAULT The buffer address is not correctly aligned.
1324 * -EBUSY Internal command or other IO in progress.
1325 * -EAGAIN Time out waiting for command to complete.
1326 */
1327static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001328 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001329 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001330 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001331 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001332 u32 opts,
1333 gfp_t atomic,
1334 unsigned long timeout)
1335{
1336 struct mtip_cmd_sg *command_sg;
1337 DECLARE_COMPLETION_ONSTACK(wait);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001338 int rv = 0, ready2go = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001339 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001340 unsigned long to;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301341 struct driver_data *dd = port->dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001342
1343 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1344 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301345 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001346 return -EFAULT;
1347 }
1348
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001349 to = jiffies + msecs_to_jiffies(timeout);
1350 do {
1351 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1352 port->allocated);
1353 if (ready2go)
1354 break;
1355 mdelay(100);
1356 } while (time_before(jiffies, to));
1357 if (!ready2go) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301358 dev_warn(&dd->pdev->dev,
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001359 "Internal cmd active. new cmd [%02X]\n", fis->command);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001360 return -EBUSY;
1361 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001362 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001363 port->ic_pause_timer = 0;
1364
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301365 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1366 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001367
1368 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001369 if (fis->command != ATA_CMD_STANDBYNOW1) {
1370 /* wait for io to complete if non atomic */
1371 if (mtip_quiesce_io(port, 5000) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301372 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001373 "Failed to quiesce IO\n");
1374 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001375 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001376 wake_up_interruptible(&port->svc_wait);
1377 return -EBUSY;
1378 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001379 }
1380
1381 /* Set the completion function and data for the command. */
1382 int_cmd->comp_data = &wait;
1383 int_cmd->comp_func = mtip_completion;
1384
1385 } else {
1386 /* Clear completion - we're going to poll */
1387 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001388 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001389 }
1390
1391 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001392 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001393
1394 /* Populate the SG list */
1395 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001396 __force_bit2int cpu_to_le32(opts | fis_len);
1397 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001398 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1399
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001400 command_sg->info =
1401 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1402 command_sg->dba =
1403 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1404 command_sg->dba_upper =
1405 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001406
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001407 int_cmd->command_header->opts |=
1408 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001409 }
1410
1411 /* Populate the command header */
1412 int_cmd->command_header->byte_count = 0;
1413
1414 /* Issue the command to the hardware */
1415 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1416
Sam Bradshaw88523a62011-08-30 08:34:26 -06001417 if (atomic == GFP_KERNEL) {
1418 /* Wait for the command to complete or timeout. */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301419 if (wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001420 &wait,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301421 msecs_to_jiffies(timeout)) <= 0) {
1422 if (rv == -ERESTARTSYS) { /* interrupted */
1423 dev_err(&dd->pdev->dev,
1424 "Internal command [%02X] was interrupted after %lu ms\n",
1425 fis->command, timeout);
1426 rv = -EINTR;
1427 goto exec_ic_exit;
1428 } else if (rv == 0) /* timeout */
1429 dev_err(&dd->pdev->dev,
1430 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1431 fis->command, timeout);
1432 else
1433 dev_err(&dd->pdev->dev,
1434 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1435 fis->command, rv, timeout);
1436
1437 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001438 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301439 &dd->dd_flag)) {
1440 dev_err(&dd->pdev->dev,
1441 "Internal command [%02X] wait returned due to SR\n",
1442 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001443 rv = -ENXIO;
1444 goto exec_ic_exit;
1445 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301446 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001447 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301448 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001449 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001450 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301451 u32 hba_stat, port_stat;
1452
Sam Bradshaw88523a62011-08-30 08:34:26 -06001453 /* Spin for <timeout> checking if command still outstanding */
1454 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001455 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1456 & (1 << MTIP_TAG_INTERNAL))
1457 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301458 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001459 rv = -ENXIO;
1460 goto exec_ic_exit;
1461 }
1462 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001463 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301464 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001465 rv = -ENXIO;
1466 goto exec_ic_exit;
1467 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301468 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1469 if (!port_stat)
1470 continue;
1471
1472 if (port_stat & PORT_IRQ_ERR) {
1473 dev_err(&dd->pdev->dev,
1474 "Internal command [%02X] failed\n",
1475 fis->command);
1476 mtip_device_reset(dd);
1477 rv = -EIO;
1478 goto exec_ic_exit;
1479 } else {
1480 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1481 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1482 if (hba_stat)
1483 writel(hba_stat,
1484 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001485 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301486 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001487 }
1488 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001489
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001490 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1491 & (1 << MTIP_TAG_INTERNAL)) {
1492 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301493 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1494 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001495 rv = -EAGAIN;
1496 }
1497 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001498exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001499 /* Clear the allocated and active bits for the internal command. */
1500 atomic_set(&int_cmd->active, 0);
1501 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001502 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1503 /* NCQ paused */
1504 return rv;
1505 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001506 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001507 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001508
1509 return rv;
1510}
1511
1512/*
1513 * Byte-swap ATA ID strings.
1514 *
1515 * ATA identify data contains strings in byte-swapped 16-bit words.
1516 * They must be swapped (on all architectures) to be usable as C strings.
1517 * This function swaps bytes in-place.
1518 *
1519 * @buf The buffer location of the string
1520 * @len The number of bytes to swap
1521 *
1522 * return value
1523 * None
1524 */
1525static inline void ata_swap_string(u16 *buf, unsigned int len)
1526{
1527 int i;
1528 for (i = 0; i < (len/2); i++)
1529 be16_to_cpus(&buf[i]);
1530}
1531
Asai Thambi S P670a6412014-04-16 20:27:54 -07001532static void mtip_set_timeout(struct driver_data *dd,
1533 struct host_to_dev_fis *fis,
1534 unsigned int *timeout, u8 erasemode)
1535{
1536 switch (fis->command) {
1537 case ATA_CMD_DOWNLOAD_MICRO:
1538 *timeout = 120000; /* 2 minutes */
1539 break;
1540 case ATA_CMD_SEC_ERASE_UNIT:
1541 case 0xFC:
1542 if (erasemode)
1543 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1544 else
1545 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1546 break;
1547 case ATA_CMD_STANDBYNOW1:
1548 *timeout = 120000; /* 2 minutes */
1549 break;
1550 case 0xF7:
1551 case 0xFA:
1552 *timeout = 60000; /* 60 seconds */
1553 break;
1554 case ATA_CMD_SMART:
1555 *timeout = 15000; /* 15 seconds */
1556 break;
1557 default:
1558 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1559 break;
1560 }
1561}
1562
Sam Bradshaw88523a62011-08-30 08:34:26 -06001563/*
1564 * Request the device identity information.
1565 *
1566 * If a user space buffer is not specified, i.e. is NULL, the
1567 * identify information is still read from the drive and placed
1568 * into the identify data buffer (@e port->identify) in the
1569 * port data structure.
1570 * When the identify buffer contains valid identify information @e
1571 * port->identify_valid is non-zero.
1572 *
1573 * @port Pointer to the port structure.
1574 * @user_buffer A user space buffer where the identify data should be
1575 * copied.
1576 *
1577 * return value
1578 * 0 Command completed successfully.
1579 * -EFAULT An error occurred while coping data to the user buffer.
1580 * -1 Command failed.
1581 */
1582static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1583{
1584 int rv = 0;
1585 struct host_to_dev_fis fis;
1586
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001587 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001588 return -EFAULT;
1589
Sam Bradshaw88523a62011-08-30 08:34:26 -06001590 /* Build the FIS. */
1591 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1592 fis.type = 0x27;
1593 fis.opts = 1 << 7;
1594 fis.command = ATA_CMD_ID_ATA;
1595
1596 /* Set the identify information as invalid. */
1597 port->identify_valid = 0;
1598
1599 /* Clear the identify information. */
1600 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1601
1602 /* Execute the command. */
1603 if (mtip_exec_internal_command(port,
1604 &fis,
1605 5,
1606 port->identify_dma,
1607 sizeof(u16) * ATA_ID_WORDS,
1608 0,
1609 GFP_KERNEL,
1610 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1611 < 0) {
1612 rv = -1;
1613 goto out;
1614 }
1615
1616 /*
1617 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1618 * perform field-sensitive swapping on the string fields.
1619 * See the kernel use of ata_id_string() for proof of this.
1620 */
1621#ifdef __LITTLE_ENDIAN
1622 ata_swap_string(port->identify + 27, 40); /* model string*/
1623 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1624 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1625#else
1626 {
1627 int i;
1628 for (i = 0; i < ATA_ID_WORDS; i++)
1629 port->identify[i] = le16_to_cpu(port->identify[i]);
1630 }
1631#endif
1632
Sam Bradshaw26d58052013-10-03 10:18:05 -07001633 /* Check security locked state */
1634 if (port->identify[128] & 0x4)
1635 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1636 else
1637 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1638
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301639#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001640 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1641 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1642 port->dd->trim_supp = true;
1643 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301644#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001645 port->dd->trim_supp = false;
1646
Sam Bradshaw88523a62011-08-30 08:34:26 -06001647 /* Set the identify buffer as valid. */
1648 port->identify_valid = 1;
1649
1650 if (user_buffer) {
1651 if (copy_to_user(
1652 user_buffer,
1653 port->identify,
1654 ATA_ID_WORDS * sizeof(u16))) {
1655 rv = -EFAULT;
1656 goto out;
1657 }
1658 }
1659
1660out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001661 return rv;
1662}
1663
1664/*
1665 * Issue a standby immediate command to the device.
1666 *
1667 * @port Pointer to the port structure.
1668 *
1669 * return value
1670 * 0 Command was executed successfully.
1671 * -1 An error occurred while executing the command.
1672 */
1673static int mtip_standby_immediate(struct mtip_port *port)
1674{
1675 int rv;
1676 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001677 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001678 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679
Sam Bradshaw88523a62011-08-30 08:34:26 -06001680 /* Build the FIS. */
1681 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1682 fis.type = 0x27;
1683 fis.opts = 1 << 7;
1684 fis.command = ATA_CMD_STANDBYNOW1;
1685
Asai Thambi S P670a6412014-04-16 20:27:54 -07001686 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1687
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001688 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001689 rv = mtip_exec_internal_command(port,
1690 &fis,
1691 5,
1692 0,
1693 0,
1694 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001695 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001696 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001697 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1698 jiffies_to_msecs(jiffies - start));
1699 if (rv)
1700 dev_warn(&port->dd->pdev->dev,
1701 "STANDBY IMMEDIATE command failed.\n");
1702
1703 return rv;
1704}
1705
1706/*
1707 * Issue a READ LOG EXT command to the device.
1708 *
1709 * @port pointer to the port structure.
1710 * @page page number to fetch
1711 * @buffer pointer to buffer
1712 * @buffer_dma dma address corresponding to @buffer
1713 * @sectors page length to fetch, in sectors
1714 *
1715 * return value
1716 * @rv return value from mtip_exec_internal_command()
1717 */
1718static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1719 dma_addr_t buffer_dma, unsigned int sectors)
1720{
1721 struct host_to_dev_fis fis;
1722
1723 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1724 fis.type = 0x27;
1725 fis.opts = 1 << 7;
1726 fis.command = ATA_CMD_READ_LOG_EXT;
1727 fis.sect_count = sectors & 0xFF;
1728 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1729 fis.lba_low = page;
1730 fis.lba_mid = 0;
1731 fis.device = ATA_DEVICE_OBS;
1732
1733 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1734
1735 return mtip_exec_internal_command(port,
1736 &fis,
1737 5,
1738 buffer_dma,
1739 sectors * ATA_SECT_SIZE,
1740 0,
1741 GFP_ATOMIC,
1742 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1743}
1744
1745/*
1746 * Issue a SMART READ DATA command to the device.
1747 *
1748 * @port pointer to the port structure.
1749 * @buffer pointer to buffer
1750 * @buffer_dma dma address corresponding to @buffer
1751 *
1752 * return value
1753 * @rv return value from mtip_exec_internal_command()
1754 */
1755static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1756 dma_addr_t buffer_dma)
1757{
1758 struct host_to_dev_fis fis;
1759
1760 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1761 fis.type = 0x27;
1762 fis.opts = 1 << 7;
1763 fis.command = ATA_CMD_SMART;
1764 fis.features = 0xD0;
1765 fis.sect_count = 1;
1766 fis.lba_mid = 0x4F;
1767 fis.lba_hi = 0xC2;
1768 fis.device = ATA_DEVICE_OBS;
1769
1770 return mtip_exec_internal_command(port,
1771 &fis,
1772 5,
1773 buffer_dma,
1774 ATA_SECT_SIZE,
1775 0,
1776 GFP_ATOMIC,
1777 15000);
1778}
1779
1780/*
1781 * Get the value of a smart attribute
1782 *
1783 * @port pointer to the port structure
1784 * @id attribute number
1785 * @attrib pointer to return attrib information corresponding to @id
1786 *
1787 * return value
1788 * -EINVAL NULL buffer passed or unsupported attribute @id.
1789 * -EPERM Identify data not valid, SMART not supported or not enabled
1790 */
1791static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1792 struct smart_attr *attrib)
1793{
1794 int rv, i;
1795 struct smart_attr *pattr;
1796
1797 if (!attrib)
1798 return -EINVAL;
1799
1800 if (!port->identify_valid) {
1801 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1802 return -EPERM;
1803 }
1804 if (!(port->identify[82] & 0x1)) {
1805 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1806 return -EPERM;
1807 }
1808 if (!(port->identify[85] & 0x1)) {
1809 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1810 return -EPERM;
1811 }
1812
1813 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1814 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1815 if (rv) {
1816 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1817 return rv;
1818 }
1819
1820 pattr = (struct smart_attr *)(port->smart_buf + 2);
1821 for (i = 0; i < 29; i++, pattr++)
1822 if (pattr->attr_id == id) {
1823 memcpy(attrib, pattr, sizeof(struct smart_attr));
1824 break;
1825 }
1826
1827 if (i == 29) {
1828 dev_warn(&port->dd->pdev->dev,
1829 "Query for invalid SMART attribute ID\n");
1830 rv = -EINVAL;
1831 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001832
Sam Bradshaw88523a62011-08-30 08:34:26 -06001833 return rv;
1834}
1835
1836/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001837 * Trim unused sectors
1838 *
1839 * @dd pointer to driver_data structure
1840 * @lba starting lba
1841 * @len # of 512b sectors to trim
1842 *
1843 * return value
1844 * -ENOMEM Out of dma memory
1845 * -EINVAL Invalid parameters passed in, trim not supported
1846 * -EIO Error submitting trim request to hw
1847 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301848static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1849 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001850{
1851 int i, rv = 0;
1852 u64 tlba, tlen, sect_left;
1853 struct mtip_trim_entry *buf;
1854 dma_addr_t dma_addr;
1855 struct host_to_dev_fis fis;
1856
1857 if (!len || dd->trim_supp == false)
1858 return -EINVAL;
1859
1860 /* Trim request too big */
1861 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1862
1863 /* Trim request not aligned on 4k boundary */
1864 WARN_ON(len % 8 != 0);
1865
1866 /* Warn if vu_trim structure is too big */
1867 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1868
1869 /* Allocate a DMA buffer for the trim structure */
1870 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1871 GFP_KERNEL);
1872 if (!buf)
1873 return -ENOMEM;
1874 memset(buf, 0, ATA_SECT_SIZE);
1875
1876 for (i = 0, sect_left = len, tlba = lba;
1877 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1878 i++) {
1879 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1880 MTIP_MAX_TRIM_ENTRY_LEN :
1881 sect_left);
1882 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1883 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1884 tlba += tlen;
1885 sect_left -= tlen;
1886 }
1887 WARN_ON(sect_left != 0);
1888
1889 /* Build the fis */
1890 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1891 fis.type = 0x27;
1892 fis.opts = 1 << 7;
1893 fis.command = 0xfb;
1894 fis.features = 0x60;
1895 fis.sect_count = 1;
1896 fis.device = ATA_DEVICE_OBS;
1897
1898 if (mtip_exec_internal_command(dd->port,
1899 &fis,
1900 5,
1901 dma_addr,
1902 ATA_SECT_SIZE,
1903 0,
1904 GFP_KERNEL,
1905 MTIP_TRIM_TIMEOUT_MS) < 0)
1906 rv = -EIO;
1907
1908 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1909 return rv;
1910}
1911
1912/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001913 * Get the drive capacity.
1914 *
1915 * @dd Pointer to the device data structure.
1916 * @sectors Pointer to the variable that will receive the sector count.
1917 *
1918 * return value
1919 * 1 Capacity was returned successfully.
1920 * 0 The identify information is invalid.
1921 */
Jens Axboe63166682011-09-27 21:27:43 -06001922static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001923{
1924 struct mtip_port *port = dd->port;
1925 u64 total, raw0, raw1, raw2, raw3;
1926 raw0 = port->identify[100];
1927 raw1 = port->identify[101];
1928 raw2 = port->identify[102];
1929 raw3 = port->identify[103];
1930 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1931 *sectors = total;
1932 return (bool) !!port->identify_valid;
1933}
1934
1935/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001936 * Display the identify command data.
1937 *
1938 * @port Pointer to the port data structure.
1939 *
1940 * return value
1941 * None
1942 */
1943static void mtip_dump_identify(struct mtip_port *port)
1944{
1945 sector_t sectors;
1946 unsigned short revid;
1947 char cbuf[42];
1948
1949 if (!port->identify_valid)
1950 return;
1951
1952 strlcpy(cbuf, (char *)(port->identify+10), 21);
1953 dev_info(&port->dd->pdev->dev,
1954 "Serial No.: %s\n", cbuf);
1955
1956 strlcpy(cbuf, (char *)(port->identify+23), 9);
1957 dev_info(&port->dd->pdev->dev,
1958 "Firmware Ver.: %s\n", cbuf);
1959
1960 strlcpy(cbuf, (char *)(port->identify+27), 41);
1961 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1962
Sam Bradshaw26d58052013-10-03 10:18:05 -07001963 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1964 port->identify[128],
1965 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1966
Sam Bradshaw88523a62011-08-30 08:34:26 -06001967 if (mtip_hw_get_capacity(port->dd, &sectors))
1968 dev_info(&port->dd->pdev->dev,
1969 "Capacity: %llu sectors (%llu MB)\n",
1970 (u64)sectors,
1971 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1972
1973 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001974 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001975 case 0x1:
1976 strlcpy(cbuf, "A0", 3);
1977 break;
1978 case 0x3:
1979 strlcpy(cbuf, "A2", 3);
1980 break;
1981 default:
1982 strlcpy(cbuf, "?", 2);
1983 break;
1984 }
1985 dev_info(&port->dd->pdev->dev,
1986 "Card Type: %s\n", cbuf);
1987}
1988
1989/*
1990 * Map the commands scatter list into the command table.
1991 *
1992 * @command Pointer to the command.
1993 * @nents Number of scatter list entries.
1994 *
1995 * return value
1996 * None
1997 */
1998static inline void fill_command_sg(struct driver_data *dd,
1999 struct mtip_cmd *command,
2000 int nents)
2001{
2002 int n;
2003 unsigned int dma_len;
2004 struct mtip_cmd_sg *command_sg;
2005 struct scatterlist *sg = command->sg;
2006
2007 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
2008
2009 for (n = 0; n < nents; n++) {
2010 dma_len = sg_dma_len(sg);
2011 if (dma_len > 0x400000)
2012 dev_err(&dd->pdev->dev,
2013 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002014 command_sg->info = __force_bit2int
2015 cpu_to_le32((dma_len-1) & 0x3FFFFF);
2016 command_sg->dba = __force_bit2int
2017 cpu_to_le32(sg_dma_address(sg));
2018 command_sg->dba_upper = __force_bit2int
2019 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002020 command_sg++;
2021 sg++;
2022 }
2023}
2024
2025/*
2026 * @brief Execute a drive command.
2027 *
2028 * return value 0 The command completed successfully.
2029 * return value -1 An error occurred while executing the command.
2030 */
Jens Axboe63166682011-09-27 21:27:43 -06002031static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002032{
2033 struct host_to_dev_fis fis;
2034 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
2035
Sam Bradshaw88523a62011-08-30 08:34:26 -06002036 /* Build the FIS. */
2037 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2038 fis.type = 0x27;
2039 fis.opts = 1 << 7;
2040 fis.command = command[0];
2041 fis.features = command[1];
2042 fis.sect_count = command[2];
2043 fis.sector = command[3];
2044 fis.cyl_low = command[4];
2045 fis.cyl_hi = command[5];
2046 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
2047
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002048 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 -06002049 __func__,
2050 command[0],
2051 command[1],
2052 command[2],
2053 command[3],
2054 command[4],
2055 command[5],
2056 command[6]);
2057
2058 /* Execute the command. */
2059 if (mtip_exec_internal_command(port,
2060 &fis,
2061 5,
2062 0,
2063 0,
2064 0,
2065 GFP_KERNEL,
2066 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002067 return -1;
2068 }
2069
2070 command[0] = reply->command; /* Status*/
2071 command[1] = reply->features; /* Error*/
2072 command[4] = reply->cyl_low;
2073 command[5] = reply->cyl_hi;
2074
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002075 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 -06002076 __func__,
2077 command[0],
2078 command[1],
2079 command[4],
2080 command[5]);
2081
Sam Bradshaw88523a62011-08-30 08:34:26 -06002082 return 0;
2083}
2084
2085/*
2086 * @brief Execute a drive command.
2087 *
2088 * @param port Pointer to the port data structure.
2089 * @param command Pointer to the user specified command parameters.
2090 * @param user_buffer Pointer to the user space buffer where read sector
2091 * data should be copied.
2092 *
2093 * return value 0 The command completed successfully.
2094 * return value -EFAULT An error occurred while copying the completion
2095 * data to the user space buffer.
2096 * return value -1 An error occurred while executing the command.
2097 */
Jens Axboe63166682011-09-27 21:27:43 -06002098static int exec_drive_command(struct mtip_port *port, u8 *command,
2099 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002100{
2101 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002102 struct host_to_dev_fis *reply;
2103 u8 *buf = NULL;
2104 dma_addr_t dma_addr = 0;
2105 int rv = 0, xfer_sz = command[3];
2106
2107 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05002108 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002109 return -EFAULT;
2110
2111 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2112 ATA_SECT_SIZE * xfer_sz,
2113 &dma_addr,
2114 GFP_KERNEL);
2115 if (!buf) {
2116 dev_err(&port->dd->pdev->dev,
2117 "Memory allocation failed (%d bytes)\n",
2118 ATA_SECT_SIZE * xfer_sz);
2119 return -ENOMEM;
2120 }
2121 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2122 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002123
Sam Bradshaw88523a62011-08-30 08:34:26 -06002124 /* Build the FIS. */
2125 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002126 fis.type = 0x27;
2127 fis.opts = 1 << 7;
2128 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06002129 fis.features = command[2];
2130 fis.sect_count = command[3];
2131 if (fis.command == ATA_CMD_SMART) {
2132 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002133 fis.cyl_low = 0x4F;
2134 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002135 }
2136
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002137 if (xfer_sz)
2138 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2139 else
2140 reply = (port->rxfis + RX_FIS_D2H_REG);
2141
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002143 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06002144 "feat %x, sectcnt %x\n",
2145 __func__,
2146 command[0],
2147 command[1],
2148 command[2],
2149 command[3]);
2150
Sam Bradshaw88523a62011-08-30 08:34:26 -06002151 /* Execute the command. */
2152 if (mtip_exec_internal_command(port,
2153 &fis,
2154 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002155 (xfer_sz ? dma_addr : 0),
2156 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002157 0,
2158 GFP_KERNEL,
2159 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2160 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002161 rv = -EFAULT;
2162 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002163 }
2164
2165 /* Collect the completion status. */
2166 command[0] = reply->command; /* Status*/
2167 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002168 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002169
2170 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002171 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002172 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002173 __func__,
2174 command[0],
2175 command[1],
2176 command[2]);
2177
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002178 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002179 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002180 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002181 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002182 rv = -EFAULT;
2183 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002184 }
2185 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002186exit_drive_command:
2187 if (buf)
2188 dmam_free_coherent(&port->dd->pdev->dev,
2189 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2190 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002191}
2192
2193/*
2194 * Indicates whether a command has a single sector payload.
2195 *
2196 * @command passed to the device to perform the certain event.
2197 * @features passed to the device to perform the certain event.
2198 *
2199 * return value
2200 * 1 command is one that always has a single sector payload,
2201 * regardless of the value in the Sector Count field.
2202 * 0 otherwise
2203 *
2204 */
2205static unsigned int implicit_sector(unsigned char command,
2206 unsigned char features)
2207{
2208 unsigned int rv = 0;
2209
2210 /* list of commands that have an implicit sector count of 1 */
2211 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002212 case ATA_CMD_SEC_SET_PASS:
2213 case ATA_CMD_SEC_UNLOCK:
2214 case ATA_CMD_SEC_ERASE_PREP:
2215 case ATA_CMD_SEC_ERASE_UNIT:
2216 case ATA_CMD_SEC_FREEZE_LOCK:
2217 case ATA_CMD_SEC_DISABLE_PASS:
2218 case ATA_CMD_PMP_READ:
2219 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06002220 rv = 1;
2221 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002222 case ATA_CMD_SET_MAX:
2223 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002224 rv = 1;
2225 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002226 case ATA_CMD_SMART:
2227 if ((features == ATA_SMART_READ_VALUES) ||
2228 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002229 rv = 1;
2230 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002231 case ATA_CMD_CONF_OVERLAY:
2232 if ((features == ATA_DCO_IDENTIFY) ||
2233 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002234 rv = 1;
2235 break;
2236 }
2237 return rv;
2238}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002239
Sam Bradshaw88523a62011-08-30 08:34:26 -06002240/*
2241 * Executes a taskfile
2242 * See ide_taskfile_ioctl() for derivation
2243 */
2244static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002245 void __user *buf,
2246 ide_task_request_t *req_task,
2247 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002248{
2249 struct host_to_dev_fis fis;
2250 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002251 u8 *outbuf = NULL;
2252 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002253 dma_addr_t outbuf_dma = 0;
2254 dma_addr_t inbuf_dma = 0;
2255 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002256 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002257 unsigned int taskin = 0;
2258 unsigned int taskout = 0;
2259 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002260 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002261 unsigned int force_single_sector;
2262 unsigned int transfer_size;
2263 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002264 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002265 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002266
2267 taskout = req_task->out_size;
2268 taskin = req_task->in_size;
2269 /* 130560 = 512 * 0xFF*/
2270 if (taskin > 130560 || taskout > 130560) {
2271 err = -EINVAL;
2272 goto abort;
2273 }
2274
2275 if (taskout) {
2276 outbuf = kzalloc(taskout, GFP_KERNEL);
2277 if (outbuf == NULL) {
2278 err = -ENOMEM;
2279 goto abort;
2280 }
2281 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2282 err = -EFAULT;
2283 goto abort;
2284 }
2285 outbuf_dma = pci_map_single(dd->pdev,
2286 outbuf,
2287 taskout,
2288 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002289 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002290 err = -ENOMEM;
2291 goto abort;
2292 }
2293 dma_buffer = outbuf_dma;
2294 }
2295
2296 if (taskin) {
2297 inbuf = kzalloc(taskin, GFP_KERNEL);
2298 if (inbuf == NULL) {
2299 err = -ENOMEM;
2300 goto abort;
2301 }
2302
2303 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2304 err = -EFAULT;
2305 goto abort;
2306 }
2307 inbuf_dma = pci_map_single(dd->pdev,
2308 inbuf,
2309 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002310 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002311 err = -ENOMEM;
2312 goto abort;
2313 }
2314 dma_buffer = inbuf_dma;
2315 }
2316
2317 /* only supports PIO and non-data commands from this ioctl. */
2318 switch (req_task->data_phase) {
2319 case TASKFILE_OUT:
2320 nsect = taskout / ATA_SECT_SIZE;
2321 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2322 break;
2323 case TASKFILE_IN:
2324 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2325 break;
2326 case TASKFILE_NO_DATA:
2327 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2328 break;
2329 default:
2330 err = -EINVAL;
2331 goto abort;
2332 }
2333
Sam Bradshaw88523a62011-08-30 08:34:26 -06002334 /* Build the FIS. */
2335 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2336
2337 fis.type = 0x27;
2338 fis.opts = 1 << 7;
2339 fis.command = req_task->io_ports[7];
2340 fis.features = req_task->io_ports[1];
2341 fis.sect_count = req_task->io_ports[2];
2342 fis.lba_low = req_task->io_ports[3];
2343 fis.lba_mid = req_task->io_ports[4];
2344 fis.lba_hi = req_task->io_ports[5];
2345 /* Clear the dev bit*/
2346 fis.device = req_task->io_ports[6] & ~0x10;
2347
2348 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2349 req_task->in_flags.all =
2350 IDE_TASKFILE_STD_IN_FLAGS |
2351 (IDE_HOB_STD_IN_FLAGS << 8);
2352 fis.lba_low_ex = req_task->hob_ports[3];
2353 fis.lba_mid_ex = req_task->hob_ports[4];
2354 fis.lba_hi_ex = req_task->hob_ports[5];
2355 fis.features_ex = req_task->hob_ports[1];
2356 fis.sect_cnt_ex = req_task->hob_ports[2];
2357
2358 } else {
2359 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2360 }
2361
2362 force_single_sector = implicit_sector(fis.command, fis.features);
2363
2364 if ((taskin || taskout) && (!fis.sect_count)) {
2365 if (nsect)
2366 fis.sect_count = nsect;
2367 else {
2368 if (!force_single_sector) {
2369 dev_warn(&dd->pdev->dev,
2370 "data movement but "
2371 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002372 err = -EINVAL;
2373 goto abort;
2374 }
2375 }
2376 }
2377
2378 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002379 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002380 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2381 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002382 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002383 fis.command,
2384 fis.features,
2385 fis.sect_count,
2386 fis.lba_low,
2387 fis.lba_mid,
2388 fis.lba_hi,
2389 fis.device);
2390
Selvan Mani4453bc82012-09-27 14:36:43 +02002391 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002392 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2393 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002394 erasemode = 1;
2395 }
2396
2397 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398
2399 /* Determine the correct transfer size.*/
2400 if (force_single_sector)
2401 transfer_size = ATA_SECT_SIZE;
2402 else
2403 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2404
2405 /* Execute the command.*/
2406 if (mtip_exec_internal_command(dd->port,
2407 &fis,
2408 5,
2409 dma_buffer,
2410 transfer_size,
2411 0,
2412 GFP_KERNEL,
2413 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002414 err = -EIO;
2415 goto abort;
2416 }
2417
2418 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2419
2420 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2421 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2422 req_task->io_ports[7] = reply->control;
2423 } else {
2424 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2425 req_task->io_ports[7] = reply->command;
2426 }
2427
2428 /* reclaim the DMA buffers.*/
2429 if (inbuf_dma)
2430 pci_unmap_single(dd->pdev, inbuf_dma,
2431 taskin, DMA_FROM_DEVICE);
2432 if (outbuf_dma)
2433 pci_unmap_single(dd->pdev, outbuf_dma,
2434 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002435 inbuf_dma = 0;
2436 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002437
2438 /* return the ATA registers to the caller.*/
2439 req_task->io_ports[1] = reply->features;
2440 req_task->io_ports[2] = reply->sect_count;
2441 req_task->io_ports[3] = reply->lba_low;
2442 req_task->io_ports[4] = reply->lba_mid;
2443 req_task->io_ports[5] = reply->lba_hi;
2444 req_task->io_ports[6] = reply->device;
2445
2446 if (req_task->out_flags.all & 1) {
2447
2448 req_task->hob_ports[3] = reply->lba_low_ex;
2449 req_task->hob_ports[4] = reply->lba_mid_ex;
2450 req_task->hob_ports[5] = reply->lba_hi_ex;
2451 req_task->hob_ports[1] = reply->features_ex;
2452 req_task->hob_ports[2] = reply->sect_cnt_ex;
2453 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002454 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002455 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002456 "err %x, sect_cnt %x, lbalo %x,"
2457 "lbamid %x, lbahi %x, dev %x\n",
2458 __func__,
2459 req_task->io_ports[7],
2460 req_task->io_ports[1],
2461 req_task->io_ports[2],
2462 req_task->io_ports[3],
2463 req_task->io_ports[4],
2464 req_task->io_ports[5],
2465 req_task->io_ports[6]);
2466
Sam Bradshaw88523a62011-08-30 08:34:26 -06002467 if (taskout) {
2468 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2469 err = -EFAULT;
2470 goto abort;
2471 }
2472 }
2473 if (taskin) {
2474 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2475 err = -EFAULT;
2476 goto abort;
2477 }
2478 }
2479abort:
2480 if (inbuf_dma)
2481 pci_unmap_single(dd->pdev, inbuf_dma,
2482 taskin, DMA_FROM_DEVICE);
2483 if (outbuf_dma)
2484 pci_unmap_single(dd->pdev, outbuf_dma,
2485 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002486 kfree(outbuf);
2487 kfree(inbuf);
2488
2489 return err;
2490}
2491
2492/*
2493 * Handle IOCTL calls from the Block Layer.
2494 *
2495 * This function is called by the Block Layer when it receives an IOCTL
2496 * command that it does not understand. If the IOCTL command is not supported
2497 * this function returns -ENOTTY.
2498 *
2499 * @dd Pointer to the driver data structure.
2500 * @cmd IOCTL command passed from the Block Layer.
2501 * @arg IOCTL argument passed from the Block Layer.
2502 *
2503 * return value
2504 * 0 The IOCTL completed successfully.
2505 * -ENOTTY The specified command is not supported.
2506 * -EFAULT An error occurred copying data to a user space buffer.
2507 * -EIO An error occurred while executing the command.
2508 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002509static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2510 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002511{
2512 switch (cmd) {
2513 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002514 {
2515 if (copy_to_user((void __user *)arg, dd->port->identify,
2516 sizeof(u16) * ATA_ID_WORDS))
2517 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002518 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002519 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002520 case HDIO_DRIVE_CMD:
2521 {
2522 u8 drive_command[4];
2523
2524 /* Copy the user command info to our buffer. */
2525 if (copy_from_user(drive_command,
2526 (void __user *) arg,
2527 sizeof(drive_command)))
2528 return -EFAULT;
2529
2530 /* Execute the drive command. */
2531 if (exec_drive_command(dd->port,
2532 drive_command,
2533 (void __user *) (arg+4)))
2534 return -EIO;
2535
2536 /* Copy the status back to the users buffer. */
2537 if (copy_to_user((void __user *) arg,
2538 drive_command,
2539 sizeof(drive_command)))
2540 return -EFAULT;
2541
2542 break;
2543 }
2544 case HDIO_DRIVE_TASK:
2545 {
2546 u8 drive_command[7];
2547
2548 /* Copy the user command info to our buffer. */
2549 if (copy_from_user(drive_command,
2550 (void __user *) arg,
2551 sizeof(drive_command)))
2552 return -EFAULT;
2553
2554 /* Execute the drive command. */
2555 if (exec_drive_task(dd->port, drive_command))
2556 return -EIO;
2557
2558 /* Copy the status back to the users buffer. */
2559 if (copy_to_user((void __user *) arg,
2560 drive_command,
2561 sizeof(drive_command)))
2562 return -EFAULT;
2563
2564 break;
2565 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002566 case HDIO_DRIVE_TASKFILE: {
2567 ide_task_request_t req_task;
2568 int ret, outtotal;
2569
2570 if (copy_from_user(&req_task, (void __user *) arg,
2571 sizeof(req_task)))
2572 return -EFAULT;
2573
2574 outtotal = sizeof(req_task);
2575
2576 ret = exec_drive_taskfile(dd, (void __user *) arg,
2577 &req_task, outtotal);
2578
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002579 if (copy_to_user((void __user *) arg, &req_task,
2580 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002581 return -EFAULT;
2582
2583 return ret;
2584 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002585
2586 default:
2587 return -EINVAL;
2588 }
2589 return 0;
2590}
2591
2592/*
2593 * Submit an IO to the hw
2594 *
2595 * This function is called by the block layer to issue an io
2596 * to the device. Upon completion, the callback function will
2597 * be called with the data parameter passed as the callback data.
2598 *
2599 * @dd Pointer to the driver data structure.
2600 * @start First sector to read.
2601 * @nsect Number of sectors to read.
2602 * @nents Number of entries in scatter list for the read command.
2603 * @tag The tag of this read command.
2604 * @callback Pointer to the function that should be called
2605 * when the read completes.
2606 * @data Callback data passed to the callback function
2607 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002608 * @dir Direction (read or write)
2609 *
2610 * return value
2611 * None
2612 */
Jens Axboe7c5d6232012-11-08 07:58:53 +01002613static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
Jens Axboe63166682011-09-27 21:27:43 -06002614 int nsect, int nents, int tag, void *callback,
Asai Thambi S P2077d942013-04-29 21:19:49 +02002615 void *data, int dir, int unaligned)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002616{
2617 struct host_to_dev_fis *fis;
2618 struct mtip_port *port = dd->port;
2619 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002620 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Jens Axboe7c5d6232012-11-08 07:58:53 +01002621 u64 start = sector;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002622
2623 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002624 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002625
2626 command->scatter_ents = nents;
2627
Asai Thambi S P2077d942013-04-29 21:19:49 +02002628 command->unaligned = unaligned;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002629 /*
2630 * The number of retries for this command before it is
2631 * reported as a failure to the upper layers.
2632 */
2633 command->retries = MTIP_MAX_RETRIES;
2634
2635 /* Fill out fis */
2636 fis = command->command;
2637 fis->type = 0x27;
2638 fis->opts = 1 << 7;
2639 fis->command =
2640 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Selvan Manieda45312012-11-07 06:03:53 -07002641 fis->lba_low = start & 0xFF;
2642 fis->lba_mid = (start >> 8) & 0xFF;
2643 fis->lba_hi = (start >> 16) & 0xFF;
2644 fis->lba_low_ex = (start >> 24) & 0xFF;
2645 fis->lba_mid_ex = (start >> 32) & 0xFF;
2646 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002647 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002648 fis->features = nsect & 0xFF;
2649 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002650 fis->sect_count = ((tag << 3) | (tag >> 5));
2651 fis->sect_cnt_ex = 0;
2652 fis->control = 0;
2653 fis->res2 = 0;
2654 fis->res3 = 0;
2655 fill_command_sg(dd, command, nents);
2656
Asai Thambi S P2077d942013-04-29 21:19:49 +02002657 if (unaligned)
2658 fis->device |= 1 << 7;
2659
Sam Bradshaw88523a62011-08-30 08:34:26 -06002660 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002661 command->command_header->opts =
2662 __force_bit2int cpu_to_le32(
2663 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002664 command->command_header->byte_count = 0;
2665
2666 /*
2667 * Set the completion function and data for the command
2668 * within this layer.
2669 */
2670 command->comp_data = dd;
2671 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002672 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002673
2674 /*
2675 * Set the completion function and data for the command passed
2676 * from the upper layer.
2677 */
2678 command->async_data = data;
2679 command->async_callback = callback;
2680
2681 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002682 * To prevent this command from being issued
2683 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002684 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002685 if (port->flags & MTIP_PF_PAUSE_IO) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002686 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002687 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002688 return;
2689 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002690
2691 /* Issue the command to the hardware */
2692 mtip_issue_ncq_command(port, tag);
2693
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002694 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002695}
2696
2697/*
2698 * Release a command slot.
2699 *
2700 * @dd Pointer to the driver data structure.
2701 * @tag Slot tag
2702 *
2703 * return value
2704 * None
2705 */
Asai Thambi S P2077d942013-04-29 21:19:49 +02002706static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag,
2707 int unaligned)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002708{
Asai Thambi S P2077d942013-04-29 21:19:49 +02002709 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2710 &dd->port->cmd_slot;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002711 release_slot(dd->port, tag);
Asai Thambi S P2077d942013-04-29 21:19:49 +02002712 up(sem);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002713}
2714
2715/*
2716 * Obtain a command slot and return its associated scatter list.
2717 *
2718 * @dd Pointer to the driver data structure.
2719 * @tag Pointer to an int that will receive the allocated command
2720 * slot tag.
2721 *
2722 * return value
2723 * Pointer to the scatter list for the allocated command slot
2724 * or NULL if no command slots are available.
2725 */
Jens Axboe63166682011-09-27 21:27:43 -06002726static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
Asai Thambi S P2077d942013-04-29 21:19:49 +02002727 int *tag, int unaligned)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002728{
Asai Thambi S P2077d942013-04-29 21:19:49 +02002729 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2730 &dd->port->cmd_slot;
2731
Sam Bradshaw88523a62011-08-30 08:34:26 -06002732 /*
2733 * It is possible that, even with this semaphore, a thread
2734 * may think that no command slots are available. Therefore, we
2735 * need to make an attempt to get_slot().
2736 */
Asai Thambi S P2077d942013-04-29 21:19:49 +02002737 down(sem);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002738 *tag = get_slot(dd->port);
2739
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002740 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P2077d942013-04-29 21:19:49 +02002741 up(sem);
Asai Thambi S P45038362012-04-09 08:35:38 +02002742 return NULL;
2743 }
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002744 if (unlikely(*tag < 0)) {
Asai Thambi S P2077d942013-04-29 21:19:49 +02002745 up(sem);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002746 return NULL;
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002747 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002748
2749 return dd->port->commands[*tag].sg;
2750}
2751
2752/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002753 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002754 *
2755 * @dev Pointer to the device structure, passed by the kernrel.
2756 * @attr Pointer to the device_attribute structure passed by the kernel.
2757 * @buf Pointer to the char buffer that will receive the stats info.
2758 *
2759 * return value
2760 * The size, in bytes, of the data copied into buf.
2761 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002762static ssize_t mtip_hw_show_status(struct device *dev,
2763 struct device_attribute *attr,
2764 char *buf)
2765{
2766 struct driver_data *dd = dev_to_disk(dev)->private_data;
2767 int size = 0;
2768
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002769 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002770 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002771 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002772 size += sprintf(buf, "%s", "write_protect\n");
2773 else
2774 size += sprintf(buf, "%s", "online\n");
2775
2776 return size;
2777}
2778
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002779static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002780
Asai Thambi S P0caff002013-04-03 19:56:21 +05302781/* debugsfs entries */
2782
2783static ssize_t show_device_status(struct device_driver *drv, char *buf)
2784{
2785 int size = 0;
2786 struct driver_data *dd, *tmp;
2787 unsigned long flags;
2788 char id_buf[42];
2789 u16 status = 0;
2790
2791 spin_lock_irqsave(&dev_lock, flags);
2792 size += sprintf(&buf[size], "Devices Present:\n");
2793 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002794 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302795 if (dd->port &&
2796 dd->port->identify &&
2797 dd->port->identify_valid) {
2798 strlcpy(id_buf,
2799 (char *) (dd->port->identify + 10), 21);
2800 status = *(dd->port->identify + 141);
2801 } else {
2802 memset(id_buf, 0, 42);
2803 status = 0;
2804 }
2805
2806 if (dd->port &&
2807 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2808 size += sprintf(&buf[size],
2809 " device %s %s (ftl rebuild %d %%)\n",
2810 dev_name(&dd->pdev->dev),
2811 id_buf,
2812 status);
2813 } else {
2814 size += sprintf(&buf[size],
2815 " device %s %s\n",
2816 dev_name(&dd->pdev->dev),
2817 id_buf);
2818 }
2819 }
2820 }
2821
2822 size += sprintf(&buf[size], "Devices Being Removed:\n");
2823 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002824 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302825 if (dd->port &&
2826 dd->port->identify &&
2827 dd->port->identify_valid) {
2828 strlcpy(id_buf,
2829 (char *) (dd->port->identify+10), 21);
2830 status = *(dd->port->identify + 141);
2831 } else {
2832 memset(id_buf, 0, 42);
2833 status = 0;
2834 }
2835
2836 if (dd->port &&
2837 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2838 size += sprintf(&buf[size],
2839 " device %s %s (ftl rebuild %d %%)\n",
2840 dev_name(&dd->pdev->dev),
2841 id_buf,
2842 status);
2843 } else {
2844 size += sprintf(&buf[size],
2845 " device %s %s\n",
2846 dev_name(&dd->pdev->dev),
2847 id_buf);
2848 }
2849 }
2850 }
2851 spin_unlock_irqrestore(&dev_lock, flags);
2852
2853 return size;
2854}
2855
2856static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2857 size_t len, loff_t *offset)
2858{
David Milburnc8afd0d2013-05-23 16:23:45 -05002859 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302860 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002861 char *buf;
2862 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302863
2864 if (!len || *offset)
2865 return 0;
2866
David Milburnc8afd0d2013-05-23 16:23:45 -05002867 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2868 if (!buf) {
2869 dev_err(&dd->pdev->dev,
2870 "Memory allocation: status buffer\n");
2871 return -ENOMEM;
2872 }
2873
Asai Thambi S P0caff002013-04-03 19:56:21 +05302874 size += show_device_status(NULL, buf);
2875
2876 *offset = size <= len ? size : len;
2877 size = copy_to_user(ubuf, buf, *offset);
2878 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002879 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302880
David Milburnc8afd0d2013-05-23 16:23:45 -05002881 kfree(buf);
2882 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302883}
2884
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002885static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2886 size_t len, loff_t *offset)
2887{
2888 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002889 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002890 u32 group_allocated;
2891 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002892 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002893
2894 if (!len || size)
2895 return 0;
2896
David Milburnc8afd0d2013-05-23 16:23:45 -05002897 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2898 if (!buf) {
2899 dev_err(&dd->pdev->dev,
2900 "Memory allocation: register buffer\n");
2901 return -ENOMEM;
2902 }
2903
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002904 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2905
2906 for (n = dd->slot_groups-1; n >= 0; n--)
2907 size += sprintf(&buf[size], "%08X ",
2908 readl(dd->port->s_active[n]));
2909
2910 size += sprintf(&buf[size], "]\n");
2911 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2912
2913 for (n = dd->slot_groups-1; n >= 0; n--)
2914 size += sprintf(&buf[size], "%08X ",
2915 readl(dd->port->cmd_issue[n]));
2916
2917 size += sprintf(&buf[size], "]\n");
2918 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2919
2920 for (n = dd->slot_groups-1; n >= 0; n--)
2921 size += sprintf(&buf[size], "%08X ",
2922 readl(dd->port->completed[n]));
2923
2924 size += sprintf(&buf[size], "]\n");
2925 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2926 readl(dd->port->mmio + PORT_IRQ_STAT));
2927 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2928 readl(dd->mmio + HOST_IRQ_STAT));
2929 size += sprintf(&buf[size], "\n");
2930
2931 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2932
2933 for (n = dd->slot_groups-1; n >= 0; n--) {
2934 if (sizeof(long) > sizeof(u32))
2935 group_allocated =
2936 dd->port->allocated[n/2] >> (32*(n&1));
2937 else
2938 group_allocated = dd->port->allocated[n];
2939 size += sprintf(&buf[size], "%08X ", group_allocated);
2940 }
2941 size += sprintf(&buf[size], "]\n");
2942
2943 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2944
2945 for (n = dd->slot_groups-1; n >= 0; n--) {
2946 if (sizeof(long) > sizeof(u32))
2947 group_allocated =
2948 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2949 else
2950 group_allocated = dd->port->cmds_to_issue[n];
2951 size += sprintf(&buf[size], "%08X ", group_allocated);
2952 }
2953 size += sprintf(&buf[size], "]\n");
2954
2955 *offset = size <= len ? size : len;
2956 size = copy_to_user(ubuf, buf, *offset);
2957 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002958 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002959
David Milburnc8afd0d2013-05-23 16:23:45 -05002960 kfree(buf);
2961 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002962}
2963
2964static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2965 size_t len, loff_t *offset)
2966{
2967 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002968 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002969 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002970 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002971
2972 if (!len || size)
2973 return 0;
2974
David Milburnc8afd0d2013-05-23 16:23:45 -05002975 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2976 if (!buf) {
2977 dev_err(&dd->pdev->dev,
2978 "Memory allocation: flag buffer\n");
2979 return -ENOMEM;
2980 }
2981
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002982 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2983 dd->port->flags);
2984 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2985 dd->dd_flag);
2986
2987 *offset = size <= len ? size : len;
2988 size = copy_to_user(ubuf, buf, *offset);
2989 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002990 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002991
David Milburnc8afd0d2013-05-23 16:23:45 -05002992 kfree(buf);
2993 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002994}
2995
Asai Thambi S P0caff002013-04-03 19:56:21 +05302996static const struct file_operations mtip_device_status_fops = {
2997 .owner = THIS_MODULE,
2998 .open = simple_open,
2999 .read = mtip_hw_read_device_status,
3000 .llseek = no_llseek,
3001};
3002
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003003static const struct file_operations mtip_regs_fops = {
3004 .owner = THIS_MODULE,
3005 .open = simple_open,
3006 .read = mtip_hw_read_registers,
3007 .llseek = no_llseek,
3008};
3009
3010static const struct file_operations mtip_flags_fops = {
3011 .owner = THIS_MODULE,
3012 .open = simple_open,
3013 .read = mtip_hw_read_flags,
3014 .llseek = no_llseek,
3015};
3016
Sam Bradshaw88523a62011-08-30 08:34:26 -06003017/*
3018 * Create the sysfs related attributes.
3019 *
3020 * @dd Pointer to the driver data structure.
3021 * @kobj Pointer to the kobj for the block device.
3022 *
3023 * return value
3024 * 0 Operation completed successfully.
3025 * -EINVAL Invalid parameter.
3026 */
Jens Axboe63166682011-09-27 21:27:43 -06003027static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003028{
3029 if (!kobj || !dd)
3030 return -EINVAL;
3031
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003032 if (sysfs_create_file(kobj, &dev_attr_status.attr))
3033 dev_warn(&dd->pdev->dev,
3034 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003035 return 0;
3036}
3037
3038/*
3039 * Remove the sysfs related attributes.
3040 *
3041 * @dd Pointer to the driver data structure.
3042 * @kobj Pointer to the kobj for the block device.
3043 *
3044 * return value
3045 * 0 Operation completed successfully.
3046 * -EINVAL Invalid parameter.
3047 */
Jens Axboe63166682011-09-27 21:27:43 -06003048static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003049{
3050 if (!kobj || !dd)
3051 return -EINVAL;
3052
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003053 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003054
3055 return 0;
3056}
3057
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003058static int mtip_hw_debugfs_init(struct driver_data *dd)
3059{
3060 if (!dfs_parent)
3061 return -1;
3062
3063 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
3064 if (IS_ERR_OR_NULL(dd->dfs_node)) {
3065 dev_warn(&dd->pdev->dev,
3066 "Error creating node %s under debugfs\n",
3067 dd->disk->disk_name);
3068 dd->dfs_node = NULL;
3069 return -1;
3070 }
3071
3072 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
3073 &mtip_flags_fops);
3074 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
3075 &mtip_regs_fops);
3076
3077 return 0;
3078}
3079
3080static void mtip_hw_debugfs_exit(struct driver_data *dd)
3081{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02003082 if (dd->dfs_node)
3083 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003084}
3085
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003086static int mtip_free_orphan(struct driver_data *dd)
3087{
3088 struct kobject *kobj;
3089
3090 if (dd->bdev) {
3091 if (dd->bdev->bd_holders >= 1)
3092 return -2;
3093
3094 bdput(dd->bdev);
3095 dd->bdev = NULL;
3096 }
3097
3098 mtip_hw_debugfs_exit(dd);
3099
3100 spin_lock(&rssd_index_lock);
3101 ida_remove(&rssd_index_ida, dd->index);
3102 spin_unlock(&rssd_index_lock);
3103
3104 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
3105 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
3106 put_disk(dd->disk);
3107 } else {
3108 if (dd->disk) {
3109 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3110 if (kobj) {
3111 mtip_hw_sysfs_exit(dd, kobj);
3112 kobject_put(kobj);
3113 }
3114 del_gendisk(dd->disk);
3115 dd->disk = NULL;
3116 }
3117 if (dd->queue) {
3118 dd->queue->queuedata = NULL;
3119 blk_cleanup_queue(dd->queue);
3120 dd->queue = NULL;
3121 }
3122 }
3123 kfree(dd);
3124 return 0;
3125}
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003126
Sam Bradshaw88523a62011-08-30 08:34:26 -06003127/*
3128 * Perform any init/resume time hardware setup
3129 *
3130 * @dd Pointer to the driver data structure.
3131 *
3132 * return value
3133 * None
3134 */
3135static inline void hba_setup(struct driver_data *dd)
3136{
3137 u32 hwdata;
3138 hwdata = readl(dd->mmio + HOST_HSORG);
3139
3140 /* interrupt bug workaround: use only 1 IS bit.*/
3141 writel(hwdata |
3142 HSORG_DISABLE_SLOTGRP_INTR |
3143 HSORG_DISABLE_SLOTGRP_PXIS,
3144 dd->mmio + HOST_HSORG);
3145}
3146
Asai Thambi S P2077d942013-04-29 21:19:49 +02003147static int mtip_device_unaligned_constrained(struct driver_data *dd)
3148{
3149 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
3150}
3151
Sam Bradshaw88523a62011-08-30 08:34:26 -06003152/*
3153 * Detect the details of the product, and store anything needed
3154 * into the driver data structure. This includes product type and
3155 * version and number of slot groups.
3156 *
3157 * @dd Pointer to the driver data structure.
3158 *
3159 * return value
3160 * None
3161 */
3162static void mtip_detect_product(struct driver_data *dd)
3163{
3164 u32 hwdata;
3165 unsigned int rev, slotgroups;
3166
3167 /*
3168 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
3169 * info register:
3170 * [15:8] hardware/software interface rev#
3171 * [ 3] asic-style interface
3172 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
3173 */
3174 hwdata = readl(dd->mmio + HOST_HSORG);
3175
3176 dd->product_type = MTIP_PRODUCT_UNKNOWN;
3177 dd->slot_groups = 1;
3178
3179 if (hwdata & 0x8) {
3180 dd->product_type = MTIP_PRODUCT_ASICFPGA;
3181 rev = (hwdata & HSORG_HWREV) >> 8;
3182 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
3183 dev_info(&dd->pdev->dev,
3184 "ASIC-FPGA design, HS rev 0x%x, "
3185 "%i slot groups [%i slots]\n",
3186 rev,
3187 slotgroups,
3188 slotgroups * 32);
3189
3190 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
3191 dev_warn(&dd->pdev->dev,
3192 "Warning: driver only supports "
3193 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
3194 slotgroups = MTIP_MAX_SLOT_GROUPS;
3195 }
3196 dd->slot_groups = slotgroups;
3197 return;
3198 }
3199
3200 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
3201}
3202
3203/*
3204 * Blocking wait for FTL rebuild to complete
3205 *
3206 * @dd Pointer to the DRIVER_DATA structure.
3207 *
3208 * return value
3209 * 0 FTL rebuild completed successfully
3210 * -EFAULT FTL rebuild error/timeout/interruption
3211 */
3212static int mtip_ftl_rebuild_poll(struct driver_data *dd)
3213{
3214 unsigned long timeout, cnt = 0, start;
3215
3216 dev_warn(&dd->pdev->dev,
3217 "FTL rebuild in progress. Polling for completion.\n");
3218
3219 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003220 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
3221
3222 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003223 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02003224 &dd->dd_flag)))
3225 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003226 if (mtip_check_surprise_removal(dd->pdev))
3227 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003228
Sam Bradshaw88523a62011-08-30 08:34:26 -06003229 if (mtip_get_identify(dd->port, NULL) < 0)
3230 return -EFAULT;
3231
3232 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3233 MTIP_FTL_REBUILD_MAGIC) {
3234 ssleep(1);
3235 /* Print message every 3 minutes */
3236 if (cnt++ >= 180) {
3237 dev_warn(&dd->pdev->dev,
3238 "FTL rebuild in progress (%d secs).\n",
3239 jiffies_to_msecs(jiffies - start) / 1000);
3240 cnt = 0;
3241 }
3242 } else {
3243 dev_warn(&dd->pdev->dev,
3244 "FTL rebuild complete (%d secs).\n",
3245 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003246 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02003247 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003248 }
3249 ssleep(10);
3250 } while (time_before(jiffies, timeout));
3251
3252 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02003253 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003254 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3255 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02003256 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003257}
3258
3259/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003260 * service thread to issue queued commands
3261 *
3262 * @data Pointer to the driver data structure.
3263 *
3264 * return value
3265 * 0
3266 */
3267
3268static int mtip_service_thread(void *data)
3269{
3270 struct driver_data *dd = (struct driver_data *)data;
3271 unsigned long slot, slot_start, slot_wrap;
3272 unsigned int num_cmd_slots = dd->slot_groups * 32;
3273 struct mtip_port *port = dd->port;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003274 int ret;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003275
3276 while (1) {
3277 /*
3278 * the condition is to check neither an internal command is
3279 * is in progress nor error handling is active
3280 */
3281 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003282 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003283
3284 if (kthread_should_stop())
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003285 goto st_out;
3286
3287 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3288
3289 /* If I am an orphan, start self cleanup */
3290 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003291 break;
3292
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003293 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02003294 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003295 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003296
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003297 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003298 slot = 1;
3299 /* used to restrict the loop to one iteration */
3300 slot_start = num_cmd_slots;
3301 slot_wrap = 0;
3302 while (1) {
3303 slot = find_next_bit(port->cmds_to_issue,
3304 num_cmd_slots, slot);
3305 if (slot_wrap == 1) {
3306 if ((slot_start >= slot) ||
3307 (slot >= num_cmd_slots))
3308 break;
3309 }
3310 if (unlikely(slot_start == num_cmd_slots))
3311 slot_start = slot;
3312
3313 if (unlikely(slot == num_cmd_slots)) {
3314 slot = 1;
3315 slot_wrap = 1;
3316 continue;
3317 }
3318
3319 /* Issue the command to the hardware */
3320 mtip_issue_ncq_command(port, slot);
3321
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003322 clear_bit(slot, port->cmds_to_issue);
3323 }
3324
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003325 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3326 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003327 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003328 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003329 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003330 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003331 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003332 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003333
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003334 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003335 goto st_out;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003336 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003337
3338 /* wait for pci remove to exit */
3339 while (1) {
3340 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3341 break;
3342 msleep_interruptible(1000);
3343 if (kthread_should_stop())
3344 goto st_out;
3345 }
3346
3347 while (1) {
3348 ret = mtip_free_orphan(dd);
3349 if (!ret) {
3350 /* NOTE: All data structures are invalid, do not
3351 * access any here */
3352 return 0;
3353 }
3354 msleep_interruptible(1000);
3355 if (kthread_should_stop())
3356 goto st_out;
3357 }
3358st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003359 return 0;
3360}
3361
3362/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003363 * DMA region teardown
3364 *
3365 * @dd Pointer to driver_data structure
3366 *
3367 * return value
3368 * None
3369 */
3370static void mtip_dma_free(struct driver_data *dd)
3371{
3372 int i;
3373 struct mtip_port *port = dd->port;
3374
3375 if (port->block1)
3376 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3377 port->block1, port->block1_dma);
3378
3379 if (port->command_list) {
3380 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3381 port->command_list, port->command_list_dma);
3382 }
3383
3384 for (i = 0; i < MTIP_MAX_COMMAND_SLOTS; i++) {
3385 if (port->commands[i].command)
3386 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3387 port->commands[i].command,
3388 port->commands[i].command_dma);
3389 }
3390}
3391
3392/*
3393 * DMA region setup
3394 *
3395 * @dd Pointer to driver_data structure
3396 *
3397 * return value
3398 * -ENOMEM Not enough free DMA region space to initialize driver
3399 */
3400static int mtip_dma_alloc(struct driver_data *dd)
3401{
3402 struct mtip_port *port = dd->port;
3403 int i, rv = 0;
3404 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3405
3406 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3407 port->block1 =
3408 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3409 &port->block1_dma, GFP_KERNEL);
3410 if (!port->block1)
3411 return -ENOMEM;
3412 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3413
3414 /* Allocate dma memory for command list */
3415 port->command_list =
3416 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3417 &port->command_list_dma, GFP_KERNEL);
3418 if (!port->command_list) {
3419 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3420 port->block1, port->block1_dma);
3421 port->block1 = NULL;
3422 port->block1_dma = 0;
3423 return -ENOMEM;
3424 }
3425 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3426
3427 /* Setup all pointers into first DMA region */
3428 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3429 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3430 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3431 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3432 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3433 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3434 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3435 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3436
3437 /* Setup per command SGL DMA region */
3438
3439 /* Point the command headers at the command tables */
3440 for (i = 0; i < MTIP_MAX_COMMAND_SLOTS; i++) {
3441 port->commands[i].command =
3442 dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3443 &port->commands[i].command_dma, GFP_KERNEL);
3444 if (!port->commands[i].command) {
3445 rv = -ENOMEM;
3446 mtip_dma_free(dd);
3447 return rv;
3448 }
3449 memset(port->commands[i].command, 0, CMD_DMA_ALLOC_SZ);
3450
3451 port->commands[i].command_header = port->command_list +
3452 (sizeof(struct mtip_cmd_hdr) * i);
3453 port->commands[i].command_header_dma =
3454 dd->port->command_list_dma +
3455 (sizeof(struct mtip_cmd_hdr) * i);
3456
3457 if (host_cap_64)
3458 port->commands[i].command_header->ctbau =
3459 __force_bit2int cpu_to_le32(
3460 (port->commands[i].command_dma >> 16) >> 16);
3461
3462 port->commands[i].command_header->ctba =
3463 __force_bit2int cpu_to_le32(
3464 port->commands[i].command_dma & 0xFFFFFFFF);
3465
3466 sg_init_table(port->commands[i].sg, MTIP_MAX_SG);
3467
3468 /* Mark command as currently inactive */
3469 atomic_set(&dd->port->commands[i].active, 0);
3470 }
3471 return 0;
3472}
3473
3474/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003475 * Called once for each card.
3476 *
3477 * @dd Pointer to the driver data structure.
3478 *
3479 * return value
3480 * 0 on success, else an error code.
3481 */
Jens Axboe63166682011-09-27 21:27:43 -06003482static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003483{
3484 int i;
3485 int rv;
3486 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003487 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003488 unsigned char *buf;
3489 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003490
3491 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3492
3493 mtip_detect_product(dd);
3494 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3495 rv = -EIO;
3496 goto out1;
3497 }
3498 num_command_slots = dd->slot_groups * 32;
3499
3500 hba_setup(dd);
3501
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003502 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3503 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003504 if (!dd->port) {
3505 dev_err(&dd->pdev->dev,
3506 "Memory allocation: port structure\n");
3507 return -ENOMEM;
3508 }
3509
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003510 /* Continue workqueue setup */
3511 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3512 dd->work[i].port = dd->port;
3513
Asai Thambi S P2077d942013-04-29 21:19:49 +02003514 /* Enable unaligned IO constraints for some devices */
3515 if (mtip_device_unaligned_constrained(dd))
3516 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3517 else
3518 dd->unal_qdepth = 0;
3519
Sam Bradshaw88523a62011-08-30 08:34:26 -06003520 /* Counting semaphore to track command slot usage */
Asai Thambi S P2077d942013-04-29 21:19:49 +02003521 sema_init(&dd->port->cmd_slot, num_command_slots - 1 - dd->unal_qdepth);
3522 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003523
3524 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003525 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3526 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003527
3528 /* Set the port mmio base address. */
3529 dd->port->mmio = dd->mmio + PORT_OFFSET;
3530 dd->port->dd = dd;
3531
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003532 /* DMA allocations */
3533 rv = mtip_dma_alloc(dd);
3534 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003535 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003536
3537 /* Setup the pointers to the extended s_active and CI registers. */
3538 for (i = 0; i < dd->slot_groups; i++) {
3539 dd->port->s_active[i] =
3540 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3541 dd->port->cmd_issue[i] =
3542 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3543 dd->port->completed[i] =
3544 dd->port->mmio + i*0x80 + PORT_SDBV;
3545 }
3546
Asai Thambi S P45038362012-04-09 08:35:38 +02003547 timetaken = jiffies;
3548 timeout = jiffies + msecs_to_jiffies(30000);
3549 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3550 time_before(jiffies, timeout)) {
3551 mdelay(100);
3552 }
3553 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3554 timetaken = jiffies - timetaken;
3555 dev_warn(&dd->pdev->dev,
3556 "Surprise removal detected at %u ms\n",
3557 jiffies_to_msecs(timetaken));
3558 rv = -ENODEV;
3559 goto out2 ;
3560 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003561 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003562 timetaken = jiffies - timetaken;
3563 dev_warn(&dd->pdev->dev,
3564 "Removal detected at %u ms\n",
3565 jiffies_to_msecs(timetaken));
3566 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003567 goto out2;
3568 }
3569
Asai Thambi S P45038362012-04-09 08:35:38 +02003570 /* Conditionally reset the HBA. */
3571 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3572 if (mtip_hba_reset(dd) < 0) {
3573 dev_err(&dd->pdev->dev,
3574 "Card did not reset within timeout\n");
3575 rv = -EIO;
3576 goto out2;
3577 }
3578 } else {
3579 /* Clear any pending interrupts on the HBA */
3580 writel(readl(dd->mmio + HOST_IRQ_STAT),
3581 dd->mmio + HOST_IRQ_STAT);
3582 }
3583
Sam Bradshaw88523a62011-08-30 08:34:26 -06003584 mtip_init_port(dd->port);
3585 mtip_start_port(dd->port);
3586
3587 /* Setup the ISR and enable interrupts. */
3588 rv = devm_request_irq(&dd->pdev->dev,
3589 dd->pdev->irq,
3590 mtip_irq_handler,
3591 IRQF_SHARED,
3592 dev_driver_string(&dd->pdev->dev),
3593 dd);
3594
3595 if (rv) {
3596 dev_err(&dd->pdev->dev,
3597 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3598 goto out2;
3599 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003600 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003601
3602 /* Enable interrupts on the HBA. */
3603 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3604 dd->mmio + HOST_CTL);
3605
3606 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003607 init_waitqueue_head(&dd->port->svc_wait);
3608
Sam Bradshaw88523a62011-08-30 08:34:26 -06003609 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3610 dd->port->cmd_timer.function = mtip_timeout_function;
3611 mod_timer(&dd->port->cmd_timer,
3612 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3613
Asai Thambi S P45038362012-04-09 08:35:38 +02003614
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003615 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003616 rv = -EFAULT;
3617 goto out3;
3618 }
3619
Sam Bradshaw88523a62011-08-30 08:34:26 -06003620 if (mtip_get_identify(dd->port, NULL) < 0) {
3621 rv = -EFAULT;
3622 goto out3;
3623 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003624 mtip_dump_identify(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003625
3626 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3627 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003628 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003629 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003630 }
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003631
3632 /* check write protect, over temp and rebuild statuses */
3633 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3634 dd->port->log_buf,
3635 dd->port->log_buf_dma, 1);
3636 if (rv) {
3637 dev_warn(&dd->pdev->dev,
3638 "Error in READ LOG EXT (10h) command\n");
3639 /* non-critical error, don't fail the load */
3640 } else {
3641 buf = (unsigned char *)dd->port->log_buf;
3642 if (buf[259] & 0x1) {
3643 dev_info(&dd->pdev->dev,
3644 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003645 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003646 }
3647 if (buf[288] == 0xF7) {
3648 dev_info(&dd->pdev->dev,
3649 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003650 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003651 }
3652 if (buf[288] == 0xBF) {
3653 dev_info(&dd->pdev->dev,
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003654 "Drive is in security locked state.\n");
3655 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003656 }
3657 }
3658
3659 /* get write protect progess */
3660 memset(&attr242, 0, sizeof(struct smart_attr));
3661 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3662 dev_warn(&dd->pdev->dev,
3663 "Unable to check write protect progress\n");
3664 else
3665 dev_info(&dd->pdev->dev,
Asai Thambi S Pb62868e2012-09-05 22:03:32 +05303666 "Write protect progress: %u%% (%u blocks)\n",
3667 attr242.cur, le32_to_cpu(attr242.data));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003668 return rv;
3669
3670out3:
3671 del_timer_sync(&dd->port->cmd_timer);
3672
3673 /* Disable interrupts on the HBA. */
3674 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3675 dd->mmio + HOST_CTL);
3676
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003677 /* Release the IRQ. */
3678 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003679 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3680
3681out2:
3682 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003683 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003684
Sam Bradshaw88523a62011-08-30 08:34:26 -06003685out1:
3686 /* Free the memory allocated for the for structure. */
3687 kfree(dd->port);
3688
3689 return rv;
3690}
3691
3692/*
3693 * Called to deinitialize an interface.
3694 *
3695 * @dd Pointer to the driver data structure.
3696 *
3697 * return value
3698 * 0
3699 */
Jens Axboe63166682011-09-27 21:27:43 -06003700static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003701{
3702 /*
3703 * Send standby immediate (E0h) to the drive so that it
3704 * saves its state.
3705 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003706 if (!dd->sr) {
Sam Bradshaw26d58052013-10-03 10:18:05 -07003707 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3708 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02003709 if (mtip_standby_immediate(dd->port))
3710 dev_warn(&dd->pdev->dev,
3711 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003712
3713 /* de-initialize the port. */
3714 mtip_deinit_port(dd->port);
3715
3716 /* Disable interrupts on the HBA. */
3717 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3718 dd->mmio + HOST_CTL);
3719 }
3720
3721 del_timer_sync(&dd->port->cmd_timer);
3722
Sam Bradshaw88523a62011-08-30 08:34:26 -06003723 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003724 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003725 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3726
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003727 /* Free dma regions */
3728 mtip_dma_free(dd);
3729
Sam Bradshaw88523a62011-08-30 08:34:26 -06003730 /* Free the memory allocated for the for structure. */
3731 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003732 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003733
3734 return 0;
3735}
3736
3737/*
3738 * Issue a Standby Immediate command to the device.
3739 *
3740 * This function is called by the Block Layer just before the
3741 * system powers off during a shutdown.
3742 *
3743 * @dd Pointer to the driver data structure.
3744 *
3745 * return value
3746 * 0
3747 */
Jens Axboe63166682011-09-27 21:27:43 -06003748static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003749{
3750 /*
3751 * Send standby immediate (E0h) to the drive so that it
3752 * saves its state.
3753 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003754 if (!dd->sr && dd->port)
3755 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003756
3757 return 0;
3758}
3759
3760/*
3761 * Suspend function
3762 *
3763 * This function is called by the Block Layer just before the
3764 * system hibernates.
3765 *
3766 * @dd Pointer to the driver data structure.
3767 *
3768 * return value
3769 * 0 Suspend was successful
3770 * -EFAULT Suspend was not successful
3771 */
Jens Axboe63166682011-09-27 21:27:43 -06003772static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003773{
3774 /*
3775 * Send standby immediate (E0h) to the drive
3776 * so that it saves its state.
3777 */
3778 if (mtip_standby_immediate(dd->port) != 0) {
3779 dev_err(&dd->pdev->dev,
3780 "Failed standby-immediate command\n");
3781 return -EFAULT;
3782 }
3783
3784 /* Disable interrupts on the HBA.*/
3785 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3786 dd->mmio + HOST_CTL);
3787 mtip_deinit_port(dd->port);
3788
3789 return 0;
3790}
3791
3792/*
3793 * Resume function
3794 *
3795 * This function is called by the Block Layer as the
3796 * system resumes.
3797 *
3798 * @dd Pointer to the driver data structure.
3799 *
3800 * return value
3801 * 0 Resume was successful
3802 * -EFAULT Resume was not successful
3803 */
Jens Axboe63166682011-09-27 21:27:43 -06003804static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003805{
3806 /* Perform any needed hardware setup steps */
3807 hba_setup(dd);
3808
3809 /* Reset the HBA */
3810 if (mtip_hba_reset(dd) != 0) {
3811 dev_err(&dd->pdev->dev,
3812 "Unable to reset the HBA\n");
3813 return -EFAULT;
3814 }
3815
3816 /*
3817 * Enable the port, DMA engine, and FIS reception specific
3818 * h/w in controller.
3819 */
3820 mtip_init_port(dd->port);
3821 mtip_start_port(dd->port);
3822
3823 /* Enable interrupts on the HBA.*/
3824 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3825 dd->mmio + HOST_CTL);
3826
3827 return 0;
3828}
3829
3830/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003831 * Helper function for reusing disk name
3832 * upon hot insertion.
3833 */
3834static int rssd_disk_name_format(char *prefix,
3835 int index,
3836 char *buf,
3837 int buflen)
3838{
3839 const int base = 'z' - 'a' + 1;
3840 char *begin = buf + strlen(prefix);
3841 char *end = buf + buflen;
3842 char *p;
3843 int unit;
3844
3845 p = end - 1;
3846 *p = '\0';
3847 unit = base;
3848 do {
3849 if (p == begin)
3850 return -EINVAL;
3851 *--p = 'a' + (index % unit);
3852 index = (index / unit) - 1;
3853 } while (index >= 0);
3854
3855 memmove(begin, p, end - p);
3856 memcpy(buf, prefix, strlen(prefix));
3857
3858 return 0;
3859}
3860
3861/*
3862 * Block layer IOCTL handler.
3863 *
3864 * @dev Pointer to the block_device structure.
3865 * @mode ignored
3866 * @cmd IOCTL command passed from the user application.
3867 * @arg Argument passed from the user application.
3868 *
3869 * return value
3870 * 0 IOCTL completed successfully.
3871 * -ENOTTY IOCTL not supported or invalid driver data
3872 * structure pointer.
3873 */
3874static int mtip_block_ioctl(struct block_device *dev,
3875 fmode_t mode,
3876 unsigned cmd,
3877 unsigned long arg)
3878{
3879 struct driver_data *dd = dev->bd_disk->private_data;
3880
3881 if (!capable(CAP_SYS_ADMIN))
3882 return -EACCES;
3883
3884 if (!dd)
3885 return -ENOTTY;
3886
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003887 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003888 return -ENOTTY;
3889
Sam Bradshaw88523a62011-08-30 08:34:26 -06003890 switch (cmd) {
3891 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003892 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003893 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003894 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003895 }
3896}
3897
Jens Axboe16d02c02011-09-27 15:50:01 -06003898#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003899/*
3900 * Block layer compat IOCTL handler.
3901 *
3902 * @dev Pointer to the block_device structure.
3903 * @mode ignored
3904 * @cmd IOCTL command passed from the user application.
3905 * @arg Argument passed from the user application.
3906 *
3907 * return value
3908 * 0 IOCTL completed successfully.
3909 * -ENOTTY IOCTL not supported or invalid driver data
3910 * structure pointer.
3911 */
3912static int mtip_block_compat_ioctl(struct block_device *dev,
3913 fmode_t mode,
3914 unsigned cmd,
3915 unsigned long arg)
3916{
3917 struct driver_data *dd = dev->bd_disk->private_data;
3918
3919 if (!capable(CAP_SYS_ADMIN))
3920 return -EACCES;
3921
3922 if (!dd)
3923 return -ENOTTY;
3924
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003925 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003926 return -ENOTTY;
3927
Sam Bradshaw88523a62011-08-30 08:34:26 -06003928 switch (cmd) {
3929 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003930 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003931 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003932 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003933 ide_task_request_t req_task;
3934 int compat_tasksize, outtotal, ret;
3935
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003936 compat_tasksize =
3937 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003938
3939 compat_req_task =
3940 (struct mtip_compat_ide_task_request_s __user *) arg;
3941
3942 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003943 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003944 return -EFAULT;
3945
3946 if (get_user(req_task.out_size, &compat_req_task->out_size))
3947 return -EFAULT;
3948
3949 if (get_user(req_task.in_size, &compat_req_task->in_size))
3950 return -EFAULT;
3951
3952 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3953
3954 ret = exec_drive_taskfile(dd, (void __user *) arg,
3955 &req_task, outtotal);
3956
3957 if (copy_to_user((void __user *) arg, &req_task,
3958 compat_tasksize -
3959 (2 * sizeof(compat_long_t))))
3960 return -EFAULT;
3961
3962 if (put_user(req_task.out_size, &compat_req_task->out_size))
3963 return -EFAULT;
3964
3965 if (put_user(req_task.in_size, &compat_req_task->in_size))
3966 return -EFAULT;
3967
3968 return ret;
3969 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003970 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003971 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003972 }
3973}
Jens Axboe16d02c02011-09-27 15:50:01 -06003974#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003975
3976/*
3977 * Obtain the geometry of the device.
3978 *
3979 * You may think that this function is obsolete, but some applications,
3980 * fdisk for example still used CHS values. This function describes the
3981 * device as having 224 heads and 56 sectors per cylinder. These values are
3982 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3983 * partition is described in terms of a start and end cylinder this means
3984 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3985 * affects performance.
3986 *
3987 * @dev Pointer to the block_device strucutre.
3988 * @geo Pointer to a hd_geometry structure.
3989 *
3990 * return value
3991 * 0 Operation completed successfully.
3992 * -ENOTTY An error occurred while reading the drive capacity.
3993 */
3994static int mtip_block_getgeo(struct block_device *dev,
3995 struct hd_geometry *geo)
3996{
3997 struct driver_data *dd = dev->bd_disk->private_data;
3998 sector_t capacity;
3999
4000 if (!dd)
4001 return -ENOTTY;
4002
4003 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4004 dev_warn(&dd->pdev->dev,
4005 "Could not get drive capacity.\n");
4006 return -ENOTTY;
4007 }
4008
4009 geo->heads = 224;
4010 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004011 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06004012 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004013 return 0;
4014}
4015
4016/*
4017 * Block device operation function.
4018 *
4019 * This structure contains pointers to the functions required by the block
4020 * layer.
4021 */
4022static const struct block_device_operations mtip_block_ops = {
4023 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06004024#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06004025 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06004026#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06004027 .getgeo = mtip_block_getgeo,
4028 .owner = THIS_MODULE
4029};
4030
4031/*
4032 * Block layer make request function.
4033 *
4034 * This function is called by the kernel to process a BIO for
4035 * the P320 device.
4036 *
4037 * @queue Pointer to the request queue. Unused other than to obtain
4038 * the driver data structure.
4039 * @bio Pointer to the BIO.
4040 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06004041 */
Jens Axboea71f4832011-11-05 08:36:21 +01004042static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004043{
4044 struct driver_data *dd = queue->queuedata;
4045 struct scatterlist *sg;
Kent Overstreet79886132013-11-23 17:19:00 -08004046 struct bio_vec bvec;
4047 struct bvec_iter iter;
4048 int nents = 0;
Asai Thambi S P2077d942013-04-29 21:19:49 +02004049 int tag = 0, unaligned = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004050
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004051 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
4052 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
4053 &dd->dd_flag))) {
4054 bio_endio(bio, -ENXIO);
4055 return;
4056 }
4057 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
4058 bio_endio(bio, -ENODATA);
4059 return;
4060 }
4061 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
4062 &dd->dd_flag) &&
4063 bio_data_dir(bio))) {
4064 bio_endio(bio, -ENODATA);
4065 return;
4066 }
Asai Thambi S P12a166c2012-09-05 22:01:36 +05304067 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
4068 bio_endio(bio, -ENODATA);
4069 return;
4070 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004071 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
4072 bio_endio(bio, -ENXIO);
4073 return;
4074 }
Asai Thambi S P45038362012-04-09 08:35:38 +02004075 }
4076
Asai Thambi S P15283462013-01-11 14:41:34 +01004077 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07004078 bio_endio(bio, mtip_send_trim(dd, bio->bi_iter.bi_sector,
Asai Thambi S P15283462013-01-11 14:41:34 +01004079 bio_sectors(bio)));
4080 return;
4081 }
4082
Sam Bradshaw88523a62011-08-30 08:34:26 -06004083 if (unlikely(!bio_has_data(bio))) {
4084 blk_queue_flush(queue, 0);
4085 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01004086 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004087 }
4088
Asai Thambi S P2077d942013-04-29 21:19:49 +02004089 if (bio_data_dir(bio) == WRITE && bio_sectors(bio) <= 64 &&
4090 dd->unal_qdepth) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07004091 if (bio->bi_iter.bi_sector % 8 != 0)
4092 /* Unaligned on 4k boundaries */
Asai Thambi S P2077d942013-04-29 21:19:49 +02004093 unaligned = 1;
4094 else if (bio_sectors(bio) % 8 != 0) /* Aligned but not 4k/8k */
4095 unaligned = 1;
4096 }
4097
4098 sg = mtip_hw_get_scatterlist(dd, &tag, unaligned);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004099 if (likely(sg != NULL)) {
4100 blk_queue_bounce(queue, &bio);
4101
4102 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
4103 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02004104 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004105 bio_io_error(bio);
Asai Thambi S P2077d942013-04-29 21:19:49 +02004106 mtip_hw_release_scatterlist(dd, tag, unaligned);
Jens Axboea71f4832011-11-05 08:36:21 +01004107 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004108 }
4109
4110 /* Create the scatter list for this bio. */
Kent Overstreet79886132013-11-23 17:19:00 -08004111 bio_for_each_segment(bvec, bio, iter) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004112 sg_set_page(&sg[nents],
Kent Overstreet79886132013-11-23 17:19:00 -08004113 bvec.bv_page,
4114 bvec.bv_len,
4115 bvec.bv_offset);
Sam Bradshaw093c9592013-05-15 10:09:05 +02004116 nents++;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004117 }
4118
4119 /* Issue the read/write. */
4120 mtip_hw_submit_io(dd,
Kent Overstreet4f024f32013-10-11 15:44:27 -07004121 bio->bi_iter.bi_sector,
Sam Bradshaw88523a62011-08-30 08:34:26 -06004122 bio_sectors(bio),
4123 nents,
4124 tag,
4125 bio_endio,
4126 bio,
Asai Thambi S P2077d942013-04-29 21:19:49 +02004127 bio_data_dir(bio),
4128 unaligned);
Jens Axboea71f4832011-11-05 08:36:21 +01004129 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06004130 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004131}
4132
4133/*
4134 * Block layer initialization function.
4135 *
4136 * This function is called once by the PCI layer for each P320
4137 * device that is connected to the system.
4138 *
4139 * @dd Pointer to the driver data structure.
4140 *
4141 * return value
4142 * 0 on success else an error code.
4143 */
Jens Axboe63166682011-09-27 21:27:43 -06004144static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004145{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004146 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004147 sector_t capacity;
4148 unsigned int index = 0;
4149 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004150 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06004151
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004152 if (dd->disk)
4153 goto skip_create_disk; /* hw init done, before rebuild */
4154
Sam Bradshaw88523a62011-08-30 08:34:26 -06004155 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004156 wait_for_rebuild = mtip_hw_init(dd);
4157 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004158 dev_err(&dd->pdev->dev,
4159 "Protocol layer initialization failed\n");
4160 rv = -EINVAL;
4161 goto protocol_init_error;
4162 }
4163
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004164 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004165 if (dd->disk == NULL) {
4166 dev_err(&dd->pdev->dev,
4167 "Unable to allocate gendisk structure\n");
4168 rv = -EINVAL;
4169 goto alloc_disk_error;
4170 }
4171
4172 /* Generate the disk name, implemented same as in sd.c */
4173 do {
4174 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
4175 goto ida_get_error;
4176
4177 spin_lock(&rssd_index_lock);
4178 rv = ida_get_new(&rssd_index_ida, &index);
4179 spin_unlock(&rssd_index_lock);
4180 } while (rv == -EAGAIN);
4181
4182 if (rv)
4183 goto ida_get_error;
4184
4185 rv = rssd_disk_name_format("rssd",
4186 index,
4187 dd->disk->disk_name,
4188 DISK_NAME_LEN);
4189 if (rv)
4190 goto disk_index_error;
4191
4192 dd->disk->driverfs_dev = &dd->pdev->dev;
4193 dd->disk->major = dd->major;
4194 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
4195 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004196 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004197 dd->index = index;
4198
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004199 mtip_hw_debugfs_init(dd);
4200
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004201 /*
4202 * if rebuild pending, start the service thread, and delay the block
4203 * queue creation and add_disk()
4204 */
4205 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4206 goto start_service_thread;
4207
4208skip_create_disk:
4209 /* Allocate the request queue. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004210 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004211 if (dd->queue == NULL) {
4212 dev_err(&dd->pdev->dev,
4213 "Unable to allocate request queue\n");
4214 rv = -ENOMEM;
4215 goto block_queue_alloc_init_error;
4216 }
4217
4218 /* Attach our request function to the request queue. */
4219 blk_queue_make_request(dd->queue, mtip_make_request);
4220
4221 dd->disk->queue = dd->queue;
4222 dd->queue->queuedata = dd;
4223
4224 /* Set device limits. */
4225 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
4226 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4227 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004228 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4229 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004230 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00004231 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004232
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01004233 /*
4234 * write back cache is not supported in the device. FUA depends on
4235 * write back cache support, hence setting flush support to zero.
4236 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004237 blk_queue_flush(dd->queue, 0);
4238
Asai Thambi S P15283462013-01-11 14:41:34 +01004239 /* Signal trim support */
4240 if (dd->trim_supp == true) {
4241 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4242 dd->queue->limits.discard_granularity = 4096;
4243 blk_queue_max_discard_sectors(dd->queue,
4244 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4245 dd->queue->limits.discard_zeroes_data = 0;
4246 }
4247
Sam Bradshaw88523a62011-08-30 08:34:26 -06004248 /* Set the capacity of the device in 512 byte sectors. */
4249 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4250 dev_warn(&dd->pdev->dev,
4251 "Could not read drive capacity\n");
4252 rv = -EIO;
4253 goto read_capacity_error;
4254 }
4255 set_capacity(dd->disk, capacity);
4256
4257 /* Enable the block device and add it to /dev */
4258 add_disk(dd->disk);
4259
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004260 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004261 /*
4262 * Now that the disk is active, initialize any sysfs attributes
4263 * managed by the protocol layer.
4264 */
4265 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4266 if (kobj) {
4267 mtip_hw_sysfs_init(dd, kobj);
4268 kobject_put(kobj);
4269 }
4270
Asai Thambi S P45038362012-04-09 08:35:38 +02004271 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004272 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004273 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004274 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004275
4276start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004277 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004278 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Kees Cookf1701682013-07-03 15:04:58 -07004279 dd, dd->numa_node, "%s",
4280 thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004281
4282 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004283 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004284 dd->mtip_svc_handler = NULL;
4285 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004286 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004287 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004288 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004289 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4290 rv = wait_for_rebuild;
4291
Sam Bradshaw88523a62011-08-30 08:34:26 -06004292 return rv;
4293
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004294kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004295 bdput(dd->bdev);
4296 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004297
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004298 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004299 del_gendisk(dd->disk);
4300
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004301read_capacity_error:
4302 blk_cleanup_queue(dd->queue);
4303
4304block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004305 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004306disk_index_error:
4307 spin_lock(&rssd_index_lock);
4308 ida_remove(&rssd_index_ida, index);
4309 spin_unlock(&rssd_index_lock);
4310
4311ida_get_error:
4312 put_disk(dd->disk);
4313
4314alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004315 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004316
4317protocol_init_error:
4318 return rv;
4319}
4320
4321/*
4322 * Block layer deinitialization function.
4323 *
4324 * Called by the PCI layer as each P320 device is removed.
4325 *
4326 * @dd Pointer to the driver data structure.
4327 *
4328 * return value
4329 * 0
4330 */
Jens Axboe63166682011-09-27 21:27:43 -06004331static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004332{
4333 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004334
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004335 if (!dd->sr) {
4336 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004337
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004338 if (dd->mtip_svc_handler) {
4339 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4340 wake_up_interruptible(&dd->port->svc_wait);
4341 kthread_stop(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004342 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004343
4344 /* Clean up the sysfs attributes, if created */
4345 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4346 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4347 if (kobj) {
4348 mtip_hw_sysfs_exit(dd, kobj);
4349 kobject_put(kobj);
4350 }
4351 }
4352 /*
4353 * Delete our gendisk structure. This also removes the device
4354 * from /dev
4355 */
4356 if (dd->bdev) {
4357 bdput(dd->bdev);
4358 dd->bdev = NULL;
4359 }
4360 if (dd->disk) {
4361 if (dd->disk->queue) {
4362 del_gendisk(dd->disk);
4363 blk_cleanup_queue(dd->queue);
4364 dd->queue = NULL;
4365 } else
4366 put_disk(dd->disk);
4367 }
4368 dd->disk = NULL;
4369
4370 spin_lock(&rssd_index_lock);
4371 ida_remove(&rssd_index_ida, dd->index);
4372 spin_unlock(&rssd_index_lock);
4373 } else {
4374 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4375 dd->disk->disk_name);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004376 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004377
4378 /* De-initialize the protocol layer. */
4379 mtip_hw_exit(dd);
4380
4381 return 0;
4382}
4383
4384/*
4385 * Function called by the PCI layer when just before the
4386 * machine shuts down.
4387 *
4388 * If a protocol layer shutdown function is present it will be called
4389 * by this function.
4390 *
4391 * @dd Pointer to the driver data structure.
4392 *
4393 * return value
4394 * 0
4395 */
Jens Axboe63166682011-09-27 21:27:43 -06004396static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004397{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004398 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304399 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304400 dev_info(&dd->pdev->dev,
4401 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304402
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304403 if (dd->disk->queue) {
4404 del_gendisk(dd->disk);
4405 blk_cleanup_queue(dd->queue);
4406 } else
4407 put_disk(dd->disk);
4408 dd->disk = NULL;
4409 dd->queue = NULL;
4410 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004411
4412 spin_lock(&rssd_index_lock);
4413 ida_remove(&rssd_index_ida, dd->index);
4414 spin_unlock(&rssd_index_lock);
4415
Sam Bradshaw88523a62011-08-30 08:34:26 -06004416 mtip_hw_shutdown(dd);
4417 return 0;
4418}
4419
Jens Axboe63166682011-09-27 21:27:43 -06004420static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004421{
4422 dev_info(&dd->pdev->dev,
4423 "Suspending %s ...\n", dd->disk->disk_name);
4424 mtip_hw_suspend(dd);
4425 return 0;
4426}
4427
Jens Axboe63166682011-09-27 21:27:43 -06004428static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004429{
4430 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4431 dd->disk->disk_name);
4432 mtip_hw_resume(dd);
4433 return 0;
4434}
4435
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004436static void drop_cpu(int cpu)
4437{
4438 cpu_use[cpu]--;
4439}
4440
4441static int get_least_used_cpu_on_node(int node)
4442{
4443 int cpu, least_used_cpu, least_cnt;
4444 const struct cpumask *node_mask;
4445
4446 node_mask = cpumask_of_node(node);
4447 least_used_cpu = cpumask_first(node_mask);
4448 least_cnt = cpu_use[least_used_cpu];
4449 cpu = least_used_cpu;
4450
4451 for_each_cpu(cpu, node_mask) {
4452 if (cpu_use[cpu] < least_cnt) {
4453 least_used_cpu = cpu;
4454 least_cnt = cpu_use[cpu];
4455 }
4456 }
4457 cpu_use[least_used_cpu]++;
4458 return least_used_cpu;
4459}
4460
4461/* Helper for selecting a node in round robin mode */
4462static inline int mtip_get_next_rr_node(void)
4463{
4464 static int next_node = -1;
4465
4466 if (next_node == -1) {
4467 next_node = first_online_node;
4468 return next_node;
4469 }
4470
4471 next_node = next_online_node(next_node);
4472 if (next_node == MAX_NUMNODES)
4473 next_node = first_online_node;
4474 return next_node;
4475}
4476
Fengguang Wu25bac122013-01-12 15:31:40 +08004477static DEFINE_HANDLER(0);
4478static DEFINE_HANDLER(1);
4479static DEFINE_HANDLER(2);
4480static DEFINE_HANDLER(3);
4481static DEFINE_HANDLER(4);
4482static DEFINE_HANDLER(5);
4483static DEFINE_HANDLER(6);
4484static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004485
Sam Bradshaw88523a62011-08-30 08:34:26 -06004486/*
4487 * Called for each supported PCI device detected.
4488 *
4489 * This function allocates the private data structure, enables the
4490 * PCI device and then calls the block layer initialization function.
4491 *
4492 * return value
4493 * 0 on success else an error code.
4494 */
4495static int mtip_pci_probe(struct pci_dev *pdev,
4496 const struct pci_device_id *ent)
4497{
4498 int rv = 0;
4499 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004500 char cpu_list[256];
4501 const struct cpumask *node_mask;
4502 int cpu, i = 0, j = 0;
4503 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304504 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004505
4506 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004507 my_node = pcibus_to_node(pdev->bus);
4508 if (my_node != NUMA_NO_NODE) {
4509 if (!node_online(my_node))
4510 my_node = mtip_get_next_rr_node();
4511 } else {
4512 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4513 my_node = mtip_get_next_rr_node();
4514 }
4515 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4516 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004517 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004518
4519 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004520 if (dd == NULL) {
4521 dev_err(&pdev->dev,
4522 "Unable to allocate memory for driver data\n");
4523 return -ENOMEM;
4524 }
4525
Sam Bradshaw88523a62011-08-30 08:34:26 -06004526 /* Attach the private data to this PCI device. */
4527 pci_set_drvdata(pdev, dd);
4528
4529 rv = pcim_enable_device(pdev);
4530 if (rv < 0) {
4531 dev_err(&pdev->dev, "Unable to enable device\n");
4532 goto iomap_err;
4533 }
4534
4535 /* Map BAR5 to memory. */
4536 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4537 if (rv < 0) {
4538 dev_err(&pdev->dev, "Unable to map regions\n");
4539 goto iomap_err;
4540 }
4541
4542 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4543 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4544
4545 if (rv) {
4546 rv = pci_set_consistent_dma_mask(pdev,
4547 DMA_BIT_MASK(32));
4548 if (rv) {
4549 dev_warn(&pdev->dev,
4550 "64-bit DMA enable failed\n");
4551 goto setmask_err;
4552 }
4553 }
4554 }
4555
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004556 /* Copy the info we may need later into the private data structure. */
4557 dd->major = mtip_major;
4558 dd->instance = instance;
4559 dd->pdev = pdev;
4560 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004561
Asai Thambi S P0caff002013-04-03 19:56:21 +05304562 INIT_LIST_HEAD(&dd->online_list);
4563 INIT_LIST_HEAD(&dd->remove_list);
4564
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004565 memset(dd->workq_name, 0, 32);
4566 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4567
4568 dd->isr_workq = create_workqueue(dd->workq_name);
4569 if (!dd->isr_workq) {
4570 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004571 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004572 goto block_initialize_err;
4573 }
4574
4575 memset(cpu_list, 0, sizeof(cpu_list));
4576
4577 node_mask = cpumask_of_node(dd->numa_node);
4578 if (!cpumask_empty(node_mask)) {
4579 for_each_cpu(cpu, node_mask)
4580 {
4581 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4582 j = strlen(cpu_list);
4583 }
4584
4585 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4586 dd->numa_node,
4587 topology_physical_package_id(cpumask_first(node_mask)),
4588 nr_cpus_node(dd->numa_node),
4589 cpu_list);
4590 } else
4591 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4592
4593 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4594 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4595 cpu_to_node(dd->isr_binding), dd->isr_binding);
4596
4597 /* first worker context always runs in ISR */
4598 dd->work[0].cpu_binding = dd->isr_binding;
4599 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4600 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4601 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4602 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4603 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4604 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4605 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4606
4607 /* Log the bindings */
4608 for_each_present_cpu(cpu) {
4609 memset(cpu_list, 0, sizeof(cpu_list));
4610 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4611 if (dd->work[i].cpu_binding == cpu) {
4612 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4613 j = strlen(cpu_list);
4614 }
4615 }
4616 if (j)
4617 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4618 }
4619
4620 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4621 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4622 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4623 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4624 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4625 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4626 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4627 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4628
4629 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004630 rv = pci_enable_msi(pdev);
4631 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004632 dev_warn(&pdev->dev,
4633 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004634 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004635 }
4636
Sam Bradshaw88523a62011-08-30 08:34:26 -06004637 /* Initialize the block layer. */
4638 rv = mtip_block_initialize(dd);
4639 if (rv < 0) {
4640 dev_err(&pdev->dev,
4641 "Unable to initialize block layer\n");
4642 goto block_initialize_err;
4643 }
4644
4645 /*
4646 * Increment the instance count so that each device has a unique
4647 * instance number.
4648 */
4649 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004650 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004651 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304652 else
4653 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304654
4655 /* Add to online list even if in ftl rebuild */
4656 spin_lock_irqsave(&dev_lock, flags);
4657 list_add(&dd->online_list, &online_list);
4658 spin_unlock_irqrestore(&dev_lock, flags);
4659
Sam Bradshaw88523a62011-08-30 08:34:26 -06004660 goto done;
4661
4662block_initialize_err:
4663 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004664
4665msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004666 if (dd->isr_workq) {
4667 flush_workqueue(dd->isr_workq);
4668 destroy_workqueue(dd->isr_workq);
4669 drop_cpu(dd->work[0].cpu_binding);
4670 drop_cpu(dd->work[1].cpu_binding);
4671 drop_cpu(dd->work[2].cpu_binding);
4672 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004673setmask_err:
4674 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4675
4676iomap_err:
4677 kfree(dd);
4678 pci_set_drvdata(pdev, NULL);
4679 return rv;
4680done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004681 return rv;
4682}
4683
4684/*
4685 * Called for each probed device when the device is removed or the
4686 * driver is unloaded.
4687 *
4688 * return value
4689 * None
4690 */
4691static void mtip_pci_remove(struct pci_dev *pdev)
4692{
4693 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004694 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004695
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004696 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004697
Asai Thambi S P0caff002013-04-03 19:56:21 +05304698 spin_lock_irqsave(&dev_lock, flags);
4699 list_del_init(&dd->online_list);
4700 list_add(&dd->remove_list, &removing_list);
4701 spin_unlock_irqrestore(&dev_lock, flags);
4702
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004703 mtip_check_surprise_removal(pdev);
4704 synchronize_irq(dd->pdev->irq);
4705
4706 /* Spin until workers are done */
4707 to = jiffies + msecs_to_jiffies(4000);
4708 do {
4709 msleep(20);
4710 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4711 time_before(jiffies, to));
4712
4713 if (atomic_read(&dd->irq_workers_active) != 0) {
4714 dev_warn(&dd->pdev->dev,
4715 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004716 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004717 /* Cleanup the outstanding commands */
4718 mtip_command_cleanup(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004719
4720 /* Clean up the block layer. */
4721 mtip_block_remove(dd);
4722
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004723 if (dd->isr_workq) {
4724 flush_workqueue(dd->isr_workq);
4725 destroy_workqueue(dd->isr_workq);
4726 drop_cpu(dd->work[0].cpu_binding);
4727 drop_cpu(dd->work[1].cpu_binding);
4728 drop_cpu(dd->work[2].cpu_binding);
4729 }
4730
Sam Bradshaw88523a62011-08-30 08:34:26 -06004731 pci_disable_msi(pdev);
4732
Asai Thambi S P0caff002013-04-03 19:56:21 +05304733 spin_lock_irqsave(&dev_lock, flags);
4734 list_del_init(&dd->remove_list);
4735 spin_unlock_irqrestore(&dev_lock, flags);
4736
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004737 if (!dd->sr)
4738 kfree(dd);
4739 else
4740 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4741
Sam Bradshaw88523a62011-08-30 08:34:26 -06004742 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004743 pci_set_drvdata(pdev, NULL);
4744 pci_dev_put(pdev);
4745
Sam Bradshaw88523a62011-08-30 08:34:26 -06004746}
4747
4748/*
4749 * Called for each probed device when the device is suspended.
4750 *
4751 * return value
4752 * 0 Success
4753 * <0 Error
4754 */
4755static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4756{
4757 int rv = 0;
4758 struct driver_data *dd = pci_get_drvdata(pdev);
4759
4760 if (!dd) {
4761 dev_err(&pdev->dev,
4762 "Driver private datastructure is NULL\n");
4763 return -EFAULT;
4764 }
4765
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004766 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004767
4768 /* Disable ports & interrupts then send standby immediate */
4769 rv = mtip_block_suspend(dd);
4770 if (rv < 0) {
4771 dev_err(&pdev->dev,
4772 "Failed to suspend controller\n");
4773 return rv;
4774 }
4775
4776 /*
4777 * Save the pci config space to pdev structure &
4778 * disable the device
4779 */
4780 pci_save_state(pdev);
4781 pci_disable_device(pdev);
4782
4783 /* Move to Low power state*/
4784 pci_set_power_state(pdev, PCI_D3hot);
4785
4786 return rv;
4787}
4788
4789/*
4790 * Called for each probed device when the device is resumed.
4791 *
4792 * return value
4793 * 0 Success
4794 * <0 Error
4795 */
4796static int mtip_pci_resume(struct pci_dev *pdev)
4797{
4798 int rv = 0;
4799 struct driver_data *dd;
4800
4801 dd = pci_get_drvdata(pdev);
4802 if (!dd) {
4803 dev_err(&pdev->dev,
4804 "Driver private datastructure is NULL\n");
4805 return -EFAULT;
4806 }
4807
4808 /* Move the device to active State */
4809 pci_set_power_state(pdev, PCI_D0);
4810
4811 /* Restore PCI configuration space */
4812 pci_restore_state(pdev);
4813
4814 /* Enable the PCI device*/
4815 rv = pcim_enable_device(pdev);
4816 if (rv < 0) {
4817 dev_err(&pdev->dev,
4818 "Failed to enable card during resume\n");
4819 goto err;
4820 }
4821 pci_set_master(pdev);
4822
4823 /*
4824 * Calls hbaReset, initPort, & startPort function
4825 * then enables interrupts
4826 */
4827 rv = mtip_block_resume(dd);
4828 if (rv < 0)
4829 dev_err(&pdev->dev, "Unable to resume\n");
4830
4831err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004832 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004833
4834 return rv;
4835}
4836
4837/*
4838 * Shutdown routine
4839 *
4840 * return value
4841 * None
4842 */
4843static void mtip_pci_shutdown(struct pci_dev *pdev)
4844{
4845 struct driver_data *dd = pci_get_drvdata(pdev);
4846 if (dd)
4847 mtip_block_shutdown(dd);
4848}
4849
Sam Bradshaw88523a62011-08-30 08:34:26 -06004850/* Table of device ids supported by this driver. */
4851static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304852 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4853 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4854 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4855 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4856 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4857 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4858 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004859 { 0 }
4860};
4861
4862/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004863static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004864 .name = MTIP_DRV_NAME,
4865 .id_table = mtip_pci_tbl,
4866 .probe = mtip_pci_probe,
4867 .remove = mtip_pci_remove,
4868 .suspend = mtip_pci_suspend,
4869 .resume = mtip_pci_resume,
4870 .shutdown = mtip_pci_shutdown,
4871};
4872
4873MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4874
4875/*
4876 * Module initialization function.
4877 *
4878 * Called once when the module is loaded. This function allocates a major
4879 * block device number to the Cyclone devices and registers the PCI layer
4880 * of the driver.
4881 *
4882 * Return value
4883 * 0 on success else error code.
4884 */
4885static int __init mtip_init(void)
4886{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004887 int error;
4888
Asai Thambi S P45422e72012-09-05 22:03:56 +05304889 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004890
Asai Thambi S P0caff002013-04-03 19:56:21 +05304891 spin_lock_init(&dev_lock);
4892
4893 INIT_LIST_HEAD(&online_list);
4894 INIT_LIST_HEAD(&removing_list);
4895
Sam Bradshaw88523a62011-08-30 08:34:26 -06004896 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004897 error = register_blkdev(0, MTIP_DRV_NAME);
4898 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304899 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004900 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004901 return -EBUSY;
4902 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004903 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004904
Asai Thambi S P0caff002013-04-03 19:56:21 +05304905 dfs_parent = debugfs_create_dir("rssd", NULL);
4906 if (IS_ERR_OR_NULL(dfs_parent)) {
4907 pr_warn("Error creating debugfs parent\n");
4908 dfs_parent = NULL;
4909 }
4910 if (dfs_parent) {
4911 dfs_device_status = debugfs_create_file("device_status",
4912 S_IRUGO, dfs_parent, NULL,
4913 &mtip_device_status_fops);
4914 if (IS_ERR_OR_NULL(dfs_device_status)) {
4915 pr_err("Error creating device_status node\n");
4916 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004917 }
4918 }
4919
Sam Bradshaw88523a62011-08-30 08:34:26 -06004920 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004921 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004922 if (error) {
4923 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004924 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004925 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004926
4927 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004928}
4929
4930/*
4931 * Module de-initialization function.
4932 *
4933 * Called once when the module is unloaded. This function deallocates
4934 * the major block device number allocated by mtip_init() and
4935 * unregisters the PCI layer of the driver.
4936 *
4937 * Return value
4938 * none
4939 */
4940static void __exit mtip_exit(void)
4941{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004942 /* Release the allocated major block device number. */
4943 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4944
4945 /* Unregister the PCI driver. */
4946 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004947
4948 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004949}
4950
4951MODULE_AUTHOR("Micron Technology, Inc");
4952MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4953MODULE_LICENSE("GPL");
4954MODULE_VERSION(MTIP_DRV_VERSION);
4955
4956module_init(mtip_init);
4957module_exit(mtip_exit);