blob: f07dbcba7ba60e73d7b2641a746521078bec9bf5 [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)
44#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
45#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
46#define HW_PORT_PRIV_DMA_SZ \
47 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
48
Asai Thambi S P45038362012-04-09 08:35:38 +020049#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060050#define HOST_HSORG 0xFC
51#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
52#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
53#define HSORG_HWREV 0xFF00
54#define HSORG_STYLE 0x8
55#define HSORG_SLOTGROUPS 0x7
56
57#define PORT_COMMAND_ISSUE 0x38
58#define PORT_SDBV 0x7C
59
60#define PORT_OFFSET 0x100
61#define PORT_MEM_SIZE 0x80
62
63#define PORT_IRQ_ERR \
64 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
65 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
66 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
67 PORT_IRQ_OVERFLOW)
68#define PORT_IRQ_LEGACY \
69 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
70#define PORT_IRQ_HANDLED \
71 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
72 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
73 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
74#define DEF_PORT_IRQ \
75 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
76
77/* product numbers */
78#define MTIP_PRODUCT_UNKNOWN 0x00
79#define MTIP_PRODUCT_ASICFPGA 0x11
80
81/* Device instance number, incremented each time a device is probed. */
82static int instance;
83
84/*
85 * Global variable used to hold the major block device number
86 * allocated in mtip_init().
87 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060088static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -070089static struct dentry *dfs_parent;
Sam Bradshaw88523a62011-08-30 08:34:26 -060090
Asai Thambi S P16c906e52012-12-20 07:46:25 -080091static u32 cpu_use[NR_CPUS];
92
Sam Bradshaw88523a62011-08-30 08:34:26 -060093static DEFINE_SPINLOCK(rssd_index_lock);
94static DEFINE_IDA(rssd_index_ida);
95
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010096static int mtip_block_initialize(struct driver_data *dd);
97
Jens Axboe16d02c02011-09-27 15:50:01 -060098#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060099struct mtip_compat_ide_task_request_s {
100 __u8 io_ports[8];
101 __u8 hob_ports[8];
102 ide_reg_valid_t out_flags;
103 ide_reg_valid_t in_flags;
104 int data_phase;
105 int req_cmd;
106 compat_ulong_t out_size;
107 compat_ulong_t in_size;
108};
Jens Axboe16d02c02011-09-27 15:50:01 -0600109#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600110
Sam Bradshaw88523a62011-08-30 08:34:26 -0600111/*
Jens Axboe63166682011-09-27 21:27:43 -0600112 * This function check_for_surprise_removal is called
113 * while card is removed from the system and it will
114 * read the vendor id from the configration space
115 *
116 * @pdev Pointer to the pci_dev structure.
117 *
118 * return value
119 * true if device removed, else false
120 */
121static bool mtip_check_surprise_removal(struct pci_dev *pdev)
122{
123 u16 vendor_id = 0;
124
125 /* Read the vendorID from the configuration space */
126 pci_read_config_word(pdev, 0x00, &vendor_id);
127 if (vendor_id == 0xFFFF)
128 return true; /* device removed */
129
130 return false; /* device present */
131}
132
133/*
134 * This function is called for clean the pending command in the
135 * command slot during the surprise removal of device and return
136 * error to the upper layer.
137 *
138 * @dd Pointer to the DRIVER_DATA structure.
139 *
140 * return value
141 * None
142 */
143static void mtip_command_cleanup(struct driver_data *dd)
144{
145 int group = 0, commandslot = 0, commandindex = 0;
146 struct mtip_cmd *command;
147 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200148 static int in_progress;
149
150 if (in_progress)
151 return;
152
153 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600154
155 for (group = 0; group < 4; group++) {
156 for (commandslot = 0; commandslot < 32; commandslot++) {
157 if (!(port->allocated[group] & (1 << commandslot)))
158 continue;
159
160 commandindex = group << 5 | commandslot;
161 command = &port->commands[commandindex];
162
163 if (atomic_read(&command->active)
164 && (command->async_callback)) {
165 command->async_callback(command->async_data,
166 -ENODEV);
167 command->async_callback = NULL;
168 command->async_data = NULL;
169 }
170
171 dma_unmap_sg(&port->dd->pdev->dev,
172 command->sg,
173 command->scatter_ents,
174 command->direction);
175 }
176 }
177
178 up(&port->cmd_slot);
179
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200180 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +0200181 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600182}
183
184/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600185 * Obtain an empty command slot.
186 *
187 * This function needs to be reentrant since it could be called
188 * at the same time on multiple CPUs. The allocation of the
189 * command slot must be atomic.
190 *
191 * @port Pointer to the port data structure.
192 *
193 * return value
194 * >= 0 Index of command slot obtained.
195 * -1 No command slots available.
196 */
197static int get_slot(struct mtip_port *port)
198{
199 int slot, i;
200 unsigned int num_command_slots = port->dd->slot_groups * 32;
201
202 /*
203 * Try 10 times, because there is a small race here.
204 * that's ok, because it's still cheaper than a lock.
205 *
206 * Race: Since this section is not protected by lock, same bit
207 * could be chosen by different process contexts running in
208 * different processor. So instead of costly lock, we are going
209 * with loop.
210 */
211 for (i = 0; i < 10; i++) {
212 slot = find_next_zero_bit(port->allocated,
213 num_command_slots, 1);
214 if ((slot < num_command_slots) &&
215 (!test_and_set_bit(slot, port->allocated)))
216 return slot;
217 }
218 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
219
220 if (mtip_check_surprise_removal(port->dd->pdev)) {
221 /* Device not present, clean outstanding commands */
222 mtip_command_cleanup(port->dd);
223 }
224 return -1;
225}
226
227/*
228 * Release a command slot.
229 *
230 * @port Pointer to the port data structure.
231 * @tag Tag of command to release
232 *
233 * return value
234 * None
235 */
236static inline void release_slot(struct mtip_port *port, int tag)
237{
238 smp_mb__before_clear_bit();
239 clear_bit(tag, port->allocated);
240 smp_mb__after_clear_bit();
241}
242
243/*
Jens Axboe63166682011-09-27 21:27:43 -0600244 * Reset the HBA (without sleeping)
245 *
246 * Just like hba_reset, except does not call sleep, so can be
247 * run from interrupt/tasklet context.
248 *
249 * @dd Pointer to the driver data structure.
250 *
251 * return value
252 * 0 The reset was successful.
253 * -1 The HBA Reset bit did not clear.
254 */
255static int hba_reset_nosleep(struct driver_data *dd)
256{
257 unsigned long timeout;
258
259 /* Chip quirk: quiesce any chip function */
260 mdelay(10);
261
262 /* Set the reset bit */
263 writel(HOST_RESET, dd->mmio + HOST_CTL);
264
265 /* Flush */
266 readl(dd->mmio + HOST_CTL);
267
268 /*
269 * Wait 10ms then spin for up to 1 second
270 * waiting for reset acknowledgement
271 */
272 timeout = jiffies + msecs_to_jiffies(1000);
273 mdelay(10);
274 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
275 && time_before(jiffies, timeout))
276 mdelay(1);
277
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200278 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200279 return -1;
280
Jens Axboe63166682011-09-27 21:27:43 -0600281 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
282 return -1;
283
284 return 0;
285}
286
287/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600288 * Issue a command to the hardware.
289 *
290 * Set the appropriate bit in the s_active and Command Issue hardware
291 * registers, causing hardware command processing to begin.
292 *
293 * @port Pointer to the port structure.
294 * @tag The tag of the command to be issued.
295 *
296 * return value
297 * None
298 */
299static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
300{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800301 int group = tag >> 5;
302
Sam Bradshaw88523a62011-08-30 08:34:26 -0600303 atomic_set(&port->commands[tag].active, 1);
304
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800305 /* guard SACT and CI registers */
306 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600307 writel((1 << MTIP_TAG_BIT(tag)),
308 port->s_active[MTIP_TAG_INDEX(tag)]);
309 writel((1 << MTIP_TAG_BIT(tag)),
310 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800311 spin_unlock(&port->cmd_issue_lock[group]);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200312
313 /* Set the command's timeout value.*/
314 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
315 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600316}
317
318/*
Jens Axboe63166682011-09-27 21:27:43 -0600319 * Enable/disable the reception of FIS
320 *
321 * @port Pointer to the port data structure
322 * @enable 1 to enable, 0 to disable
323 *
324 * return value
325 * Previous state: 1 enabled, 0 disabled
326 */
327static int mtip_enable_fis(struct mtip_port *port, int enable)
328{
329 u32 tmp;
330
331 /* enable FIS reception */
332 tmp = readl(port->mmio + PORT_CMD);
333 if (enable)
334 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
335 else
336 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
337
338 /* Flush */
339 readl(port->mmio + PORT_CMD);
340
341 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
342}
343
344/*
345 * Enable/disable the DMA engine
346 *
347 * @port Pointer to the port data structure
348 * @enable 1 to enable, 0 to disable
349 *
350 * return value
351 * Previous state: 1 enabled, 0 disabled.
352 */
353static int mtip_enable_engine(struct mtip_port *port, int enable)
354{
355 u32 tmp;
356
357 /* enable FIS reception */
358 tmp = readl(port->mmio + PORT_CMD);
359 if (enable)
360 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
361 else
362 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
363
364 readl(port->mmio + PORT_CMD);
365 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
366}
367
368/*
369 * Enables the port DMA engine and FIS reception.
370 *
371 * return value
372 * None
373 */
374static inline void mtip_start_port(struct mtip_port *port)
375{
376 /* Enable FIS reception */
377 mtip_enable_fis(port, 1);
378
379 /* Enable the DMA engine */
380 mtip_enable_engine(port, 1);
381}
382
383/*
384 * Deinitialize a port by disabling port interrupts, the DMA engine,
385 * and FIS reception.
386 *
387 * @port Pointer to the port structure
388 *
389 * return value
390 * None
391 */
392static inline void mtip_deinit_port(struct mtip_port *port)
393{
394 /* Disable interrupts on this port */
395 writel(0, port->mmio + PORT_IRQ_MASK);
396
397 /* Disable the DMA engine */
398 mtip_enable_engine(port, 0);
399
400 /* Disable FIS reception */
401 mtip_enable_fis(port, 0);
402}
403
404/*
405 * Initialize a port.
406 *
407 * This function deinitializes the port by calling mtip_deinit_port() and
408 * then initializes it by setting the command header and RX FIS addresses,
409 * clearing the SError register and any pending port interrupts before
410 * re-enabling the default set of port interrupts.
411 *
412 * @port Pointer to the port structure.
413 *
414 * return value
415 * None
416 */
417static void mtip_init_port(struct mtip_port *port)
418{
419 int i;
420 mtip_deinit_port(port);
421
422 /* Program the command list base and FIS base addresses */
423 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
424 writel((port->command_list_dma >> 16) >> 16,
425 port->mmio + PORT_LST_ADDR_HI);
426 writel((port->rxfis_dma >> 16) >> 16,
427 port->mmio + PORT_FIS_ADDR_HI);
428 }
429
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100430 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600431 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100432 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600433
434 /* Clear SError */
435 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
436
437 /* reset the completed registers.*/
438 for (i = 0; i < port->dd->slot_groups; i++)
439 writel(0xFFFFFFFF, port->completed[i]);
440
441 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700442 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600443
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100444 /* Clear any pending interrupts on the HBA. */
445 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
446 port->dd->mmio + HOST_IRQ_STAT);
447
Jens Axboe63166682011-09-27 21:27:43 -0600448 /* Enable port interrupts */
449 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
450}
451
452/*
453 * Restart a port
454 *
455 * @port Pointer to the port data structure.
456 *
457 * return value
458 * None
459 */
460static void mtip_restart_port(struct mtip_port *port)
461{
462 unsigned long timeout;
463
464 /* Disable the DMA engine */
465 mtip_enable_engine(port, 0);
466
467 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
468 timeout = jiffies + msecs_to_jiffies(500);
469 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
470 && time_before(jiffies, timeout))
471 ;
472
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200473 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200474 return;
475
Jens Axboe63166682011-09-27 21:27:43 -0600476 /*
477 * Chip quirk: escalate to hba reset if
478 * PxCMD.CR not clear after 500 ms
479 */
480 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
481 dev_warn(&port->dd->pdev->dev,
482 "PxCMD.CR not clear, escalating reset\n");
483
484 if (hba_reset_nosleep(port->dd))
485 dev_err(&port->dd->pdev->dev,
486 "HBA reset escalation failed.\n");
487
488 /* 30 ms delay before com reset to quiesce chip */
489 mdelay(30);
490 }
491
492 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
493
494 /* Set PxSCTL.DET */
495 writel(readl(port->mmio + PORT_SCR_CTL) |
496 1, port->mmio + PORT_SCR_CTL);
497 readl(port->mmio + PORT_SCR_CTL);
498
499 /* Wait 1 ms to quiesce chip function */
500 timeout = jiffies + msecs_to_jiffies(1);
501 while (time_before(jiffies, timeout))
502 ;
503
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200504 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200505 return;
506
Jens Axboe63166682011-09-27 21:27:43 -0600507 /* Clear PxSCTL.DET */
508 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
509 port->mmio + PORT_SCR_CTL);
510 readl(port->mmio + PORT_SCR_CTL);
511
512 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
513 timeout = jiffies + msecs_to_jiffies(500);
514 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
515 && time_before(jiffies, timeout))
516 ;
517
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200518 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200519 return;
520
Jens Axboe63166682011-09-27 21:27:43 -0600521 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
522 dev_warn(&port->dd->pdev->dev,
523 "COM reset failed\n");
524
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100525 mtip_init_port(port);
526 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600527
Jens Axboe63166682011-09-27 21:27:43 -0600528}
529
530/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200531 * Helper function for tag logging
532 */
533static void print_tags(struct driver_data *dd,
534 char *msg,
535 unsigned long *tagbits,
536 int cnt)
537{
538 unsigned char tagmap[128];
539 int group, tagmap_len = 0;
540
541 memset(tagmap, 0, sizeof(tagmap));
542 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
543 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
544 tagbits[group-1]);
545 dev_warn(&dd->pdev->dev,
546 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
547}
548
549/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600550 * Called periodically to see if any read/write commands are
551 * taking too long to complete.
552 *
553 * @data Pointer to the PORT data structure.
554 *
555 * return value
556 * None
557 */
Jens Axboe63166682011-09-27 21:27:43 -0600558static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600559{
560 struct mtip_port *port = (struct mtip_port *) data;
561 struct host_to_dev_fis *fis;
562 struct mtip_cmd *command;
563 int tag, cmdto_cnt = 0;
564 unsigned int bit, group;
Wei Yongjun298d8012012-11-08 17:35:38 +0800565 unsigned int num_command_slots;
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200566 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600567
568 if (unlikely(!port))
569 return;
570
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200571 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600572 mod_timer(&port->cmd_timer,
573 jiffies + msecs_to_jiffies(30000));
574 return;
575 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200576 /* clear the tag accumulator */
577 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
Wei Yongjun298d8012012-11-08 17:35:38 +0800578 num_command_slots = port->dd->slot_groups * 32;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600579
580 for (tag = 0; tag < num_command_slots; tag++) {
581 /*
582 * Skip internal command slot as it has
583 * its own timeout mechanism
584 */
585 if (tag == MTIP_TAG_INTERNAL)
586 continue;
587
588 if (atomic_read(&port->commands[tag].active) &&
589 (time_after(jiffies, port->commands[tag].comp_time))) {
590 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100591 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600592
593 command = &port->commands[tag];
594 fis = (struct host_to_dev_fis *) command->command;
595
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200596 set_bit(tag, tagaccum);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600597 cmdto_cnt++;
598 if (cmdto_cnt == 1)
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200599 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600600
601 /*
602 * Clear the completed bit. This should prevent
603 * any interrupt handlers from trying to retire
604 * the command.
605 */
606 writel(1 << bit, port->completed[group]);
607
608 /* Call the async completion callback. */
609 if (likely(command->async_callback))
610 command->async_callback(command->async_data,
611 -EIO);
612 command->async_callback = NULL;
613 command->comp_func = NULL;
614
615 /* Unmap the DMA scatter list entries */
616 dma_unmap_sg(&port->dd->pdev->dev,
617 command->sg,
618 command->scatter_ents,
619 command->direction);
620
621 /*
622 * Clear the allocated bit and active tag for the
623 * command.
624 */
625 atomic_set(&port->commands[tag].active, 0);
626 release_slot(port, tag);
627
628 up(&port->cmd_slot);
629 }
630 }
631
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200632 if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200633 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
634
Sam Bradshaw88523a62011-08-30 08:34:26 -0600635 mtip_restart_port(port);
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200636 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100637 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600638 }
639
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200640 if (port->ic_pause_timer) {
641 to = port->ic_pause_timer + msecs_to_jiffies(1000);
642 if (time_after(jiffies, to)) {
643 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
644 port->ic_pause_timer = 0;
645 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
646 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
647 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
648 wake_up_interruptible(&port->svc_wait);
649 }
650
651
652 }
653 }
654
Sam Bradshaw88523a62011-08-30 08:34:26 -0600655 /* Restart the timer */
656 mod_timer(&port->cmd_timer,
657 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
658}
659
660/*
661 * IO completion function.
662 *
663 * This completion function is called by the driver ISR when a
664 * command that was issued by the kernel completes. It first calls the
665 * asynchronous completion function which normally calls back into the block
666 * layer passing the asynchronous callback data, then unmaps the
667 * scatter list associated with the completed command, and finally
668 * clears the allocated bit associated with the completed command.
669 *
670 * @port Pointer to the port data structure.
671 * @tag Tag of the command.
672 * @data Pointer to driver_data.
673 * @status Completion status.
674 *
675 * return value
676 * None
677 */
678static void mtip_async_complete(struct mtip_port *port,
679 int tag,
680 void *data,
681 int status)
682{
683 struct mtip_cmd *command;
684 struct driver_data *dd = data;
685 int cb_status = status ? -EIO : 0;
686
687 if (unlikely(!dd) || unlikely(!port))
688 return;
689
690 command = &port->commands[tag];
691
692 if (unlikely(status == PORT_IRQ_TF_ERR)) {
693 dev_warn(&port->dd->pdev->dev,
694 "Command tag %d failed due to TFE\n", tag);
695 }
696
697 /* Upper layer callback */
698 if (likely(command->async_callback))
699 command->async_callback(command->async_data, cb_status);
700
701 command->async_callback = NULL;
702 command->comp_func = NULL;
703
704 /* Unmap the DMA scatter list entries */
705 dma_unmap_sg(&dd->pdev->dev,
706 command->sg,
707 command->scatter_ents,
708 command->direction);
709
710 /* Clear the allocated and active bits for the command */
711 atomic_set(&port->commands[tag].active, 0);
712 release_slot(port, tag);
713
714 up(&port->cmd_slot);
715}
716
717/*
718 * Internal command completion callback function.
719 *
720 * This function is normally called by the driver ISR when an internal
721 * command completed. This function signals the command completion by
722 * calling complete().
723 *
724 * @port Pointer to the port data structure.
725 * @tag Tag of the command that has completed.
726 * @data Pointer to a completion structure.
727 * @status Completion status.
728 *
729 * return value
730 * None
731 */
732static void mtip_completion(struct mtip_port *port,
733 int tag,
734 void *data,
735 int status)
736{
737 struct mtip_cmd *command = &port->commands[tag];
738 struct completion *waiting = data;
739 if (unlikely(status == PORT_IRQ_TF_ERR))
740 dev_warn(&port->dd->pdev->dev,
741 "Internal command %d completed with TFE\n", tag);
742
743 command->async_callback = NULL;
744 command->comp_func = NULL;
745
746 complete(waiting);
747}
748
Asai Thambi S P8182b492012-04-09 08:35:38 +0200749static void mtip_null_completion(struct mtip_port *port,
750 int tag,
751 void *data,
752 int status)
753{
754 return;
755}
756
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200757static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
758 dma_addr_t buffer_dma, unsigned int sectors);
759static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
760 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600761/*
762 * Handle an error.
763 *
764 * @dd Pointer to the DRIVER_DATA structure.
765 *
766 * return value
767 * None
768 */
769static void mtip_handle_tfe(struct driver_data *dd)
770{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200771 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600772 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200773 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600774 u32 completed;
775 struct host_to_dev_fis *fis;
776 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200777 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200778 unsigned char *buf;
779 char *fail_reason = NULL;
780 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600781
782 dev_warn(&dd->pdev->dev, "Taskfile error\n");
783
784 port = dd->port;
785
786 /* Stop the timer to prevent command timeouts. */
787 del_timer(&port->cmd_timer);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700788 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
789
790 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
791 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
792 cmd = &port->commands[MTIP_TAG_INTERNAL];
793 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
794
795 atomic_inc(&cmd->active); /* active > 1 indicates error */
796 if (cmd->comp_data && cmd->comp_func) {
797 cmd->comp_func(port, MTIP_TAG_INTERNAL,
798 cmd->comp_data, PORT_IRQ_TF_ERR);
799 }
800 goto handle_tfe_exit;
801 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600802
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200803 /* clear the tag accumulator */
804 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
805
Sam Bradshaw88523a62011-08-30 08:34:26 -0600806 /* Loop through all the groups */
807 for (group = 0; group < dd->slot_groups; group++) {
808 completed = readl(port->completed[group]);
809
810 /* clear completed status register in the hardware.*/
811 writel(completed, port->completed[group]);
812
Sam Bradshaw88523a62011-08-30 08:34:26 -0600813 /* Process successfully completed commands */
814 for (bit = 0; bit < 32 && completed; bit++) {
815 if (!(completed & (1<<bit)))
816 continue;
817 tag = (group << 5) + bit;
818
819 /* Skip the internal command slot */
820 if (tag == MTIP_TAG_INTERNAL)
821 continue;
822
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200823 cmd = &port->commands[tag];
824 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600825 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200826 cmd_cnt++;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200827 atomic_set(&cmd->active, 0);
828 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600829 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200830 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831 0);
832 } else {
833 dev_err(&port->dd->pdev->dev,
834 "Missing completion func for tag %d",
835 tag);
836 if (mtip_check_surprise_removal(dd->pdev)) {
837 mtip_command_cleanup(dd);
838 /* don't proceed further */
839 return;
840 }
841 }
842 }
843 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200844
845 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600846
847 /* Restart the port */
848 mdelay(20);
849 mtip_restart_port(port);
850
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200851 /* Trying to determine the cause of the error */
852 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
853 dd->port->log_buf,
854 dd->port->log_buf_dma, 1);
855 if (rv) {
856 dev_warn(&dd->pdev->dev,
857 "Error in READ LOG EXT (10h) command\n");
858 /* non-critical error, don't fail the load */
859 } else {
860 buf = (unsigned char *)dd->port->log_buf;
861 if (buf[259] & 0x1) {
862 dev_info(&dd->pdev->dev,
863 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200864 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200865 fail_all_ncq_write = 1;
866 fail_reason = "write protect";
867 }
868 if (buf[288] == 0xF7) {
869 dev_info(&dd->pdev->dev,
870 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200871 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200872 fail_all_ncq_cmds = 1;
873 fail_reason = "thermal shutdown";
874 }
875 if (buf[288] == 0xBF) {
876 dev_info(&dd->pdev->dev,
877 "Drive indicates rebuild has failed.\n");
878 fail_all_ncq_cmds = 1;
879 fail_reason = "rebuild failed";
880 }
881 }
882
Sam Bradshaw88523a62011-08-30 08:34:26 -0600883 /* clear the tag accumulator */
884 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
885
886 /* Loop through all the groups */
887 for (group = 0; group < dd->slot_groups; group++) {
888 for (bit = 0; bit < 32; bit++) {
889 reissue = 1;
890 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200891 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600892
893 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200894 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600895 continue;
896
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200897 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600898
899 /* Should re-issue? */
900 if (tag == MTIP_TAG_INTERNAL ||
901 fis->command == ATA_CMD_SET_FEATURES)
902 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200903 else {
904 if (fail_all_ncq_cmds ||
905 (fail_all_ncq_write &&
906 fis->command == ATA_CMD_FPDMA_WRITE)) {
907 dev_warn(&dd->pdev->dev,
908 " Fail: %s w/tag %d [%s].\n",
909 fis->command == ATA_CMD_FPDMA_WRITE ?
910 "write" : "read",
911 tag,
912 fail_reason != NULL ?
913 fail_reason : "unknown");
914 atomic_set(&cmd->active, 0);
915 if (cmd->comp_func) {
916 cmd->comp_func(port, tag,
917 cmd->comp_data,
918 -ENODATA);
919 }
920 continue;
921 }
922 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600923
924 /*
925 * First check if this command has
926 * exceeded its retries.
927 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200928 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600929
930 set_bit(tag, tagaccum);
931
Sam Bradshaw88523a62011-08-30 08:34:26 -0600932 /* Re-issue the command. */
933 mtip_issue_ncq_command(port, tag);
934
935 continue;
936 }
937
938 /* Retire a command that will not be reissued */
939 dev_warn(&port->dd->pdev->dev,
940 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200941 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600942
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200943 if (cmd->comp_func)
944 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -0600945 port,
946 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200947 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600948 PORT_IRQ_TF_ERR);
949 else
950 dev_warn(&port->dd->pdev->dev,
951 "Bad completion for tag %d\n",
952 tag);
953 }
954 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200955 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600956
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700957handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100958 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200959 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100960 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600961
962 mod_timer(&port->cmd_timer,
963 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
964}
965
966/*
967 * Handle a set device bits interrupt
968 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800969static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
970 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600971{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800972 struct driver_data *dd = port->dd;
973 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600974 struct mtip_cmd *command;
975
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800976 if (!completed) {
977 WARN_ON_ONCE(!completed);
978 return;
979 }
980 /* clear completed status register in the hardware.*/
981 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600982
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800983 /* Process completed commands. */
984 for (bit = 0; (bit < 32) && completed; bit++) {
985 if (completed & 0x01) {
986 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600987
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800988 /* skip internal command slot. */
989 if (unlikely(tag == MTIP_TAG_INTERNAL))
990 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600991
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800992 command = &port->commands[tag];
993 /* make internal callback */
994 if (likely(command->comp_func)) {
995 command->comp_func(
996 port,
997 tag,
998 command->comp_data,
999 0);
1000 } else {
1001 dev_warn(&dd->pdev->dev,
1002 "Null completion "
1003 "for tag %d",
1004 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001005
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001006 if (mtip_check_surprise_removal(
1007 dd->pdev)) {
1008 mtip_command_cleanup(dd);
1009 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001010 }
1011 }
1012 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001013 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001014 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001015
1016 /* If last, re-enable interrupts */
1017 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1018 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001019}
1020
1021/*
1022 * Process legacy pio and d2h interrupts
1023 */
1024static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1025{
1026 struct mtip_port *port = dd->port;
1027 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1028
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001029 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001030 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -06001031 & (1 << MTIP_TAG_INTERNAL))) {
1032 if (cmd->comp_func) {
1033 cmd->comp_func(port,
1034 MTIP_TAG_INTERNAL,
1035 cmd->comp_data,
1036 0);
1037 return;
1038 }
1039 }
1040
Sam Bradshaw88523a62011-08-30 08:34:26 -06001041 return;
1042}
1043
1044/*
1045 * Demux and handle errors
1046 */
1047static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1048{
1049 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1050 mtip_handle_tfe(dd);
1051
1052 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1053 dev_warn(&dd->pdev->dev,
1054 "Clearing PxSERR.DIAG.x\n");
1055 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1056 }
1057
1058 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1059 dev_warn(&dd->pdev->dev,
1060 "Clearing PxSERR.DIAG.n\n");
1061 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1062 }
1063
1064 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1065 dev_warn(&dd->pdev->dev,
1066 "Port stat errors %x unhandled\n",
1067 (port_stat & ~PORT_IRQ_HANDLED));
1068 }
1069}
1070
1071static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1072{
1073 struct driver_data *dd = (struct driver_data *) data;
1074 struct mtip_port *port = dd->port;
1075 u32 hba_stat, port_stat;
1076 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001077 int do_irq_enable = 1, i, workers;
1078 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001079
1080 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1081 if (hba_stat) {
1082 rv = IRQ_HANDLED;
1083
1084 /* Acknowledge the interrupt status on the port.*/
1085 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1086 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1087
1088 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001089 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1090 do_irq_enable = 0;
1091 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1092
1093 /* Start at 1: group zero is always local? */
1094 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1095 i++) {
1096 twork = &dd->work[i];
1097 twork->completed = readl(port->completed[i]);
1098 if (twork->completed)
1099 workers++;
1100 }
1101
1102 atomic_set(&dd->irq_workers_active, workers);
1103 if (workers) {
1104 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1105 twork = &dd->work[i];
1106 if (twork->completed)
1107 queue_work_on(
1108 twork->cpu_binding,
1109 dd->isr_workq,
1110 &twork->work);
1111 }
1112
1113 if (likely(dd->work[0].completed))
1114 mtip_workq_sdbfx(port, 0,
1115 dd->work[0].completed);
1116
1117 } else {
1118 /*
1119 * Chip quirk: SDB interrupt but nothing
1120 * to complete
1121 */
1122 do_irq_enable = 1;
1123 }
1124 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001125
1126 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1127 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1128 mtip_command_cleanup(dd);
1129 /* don't proceed further */
1130 return IRQ_HANDLED;
1131 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001132 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001133 &dd->dd_flag))
1134 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001135
1136 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1137 }
1138
1139 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1140 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1141 }
1142
1143 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001144 if (unlikely(do_irq_enable))
1145 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001146
1147 return rv;
1148}
1149
1150/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001151 * HBA interrupt subroutine.
1152 *
1153 * @irq IRQ number.
1154 * @instance Pointer to the driver data structure.
1155 *
1156 * return value
1157 * IRQ_HANDLED A HBA interrupt was pending and handled.
1158 * IRQ_NONE This interrupt was not for the HBA.
1159 */
1160static irqreturn_t mtip_irq_handler(int irq, void *instance)
1161{
1162 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08001163
1164 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001165}
1166
1167static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1168{
1169 atomic_set(&port->commands[tag].active, 1);
1170 writel(1 << MTIP_TAG_BIT(tag),
1171 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1172}
1173
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001174static bool mtip_pause_ncq(struct mtip_port *port,
1175 struct host_to_dev_fis *fis)
1176{
1177 struct host_to_dev_fis *reply;
1178 unsigned long task_file_data;
1179
1180 reply = port->rxfis + RX_FIS_D2H_REG;
1181 task_file_data = readl(port->mmio+PORT_TFDATA);
1182
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301183 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1184 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1185
1186 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001187 return false;
1188
1189 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1190 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301191 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001192 port->ic_pause_timer = jiffies;
1193 return true;
1194 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1195 (fis->features == 0x03)) {
1196 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1197 port->ic_pause_timer = jiffies;
1198 return true;
1199 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1200 ((fis->command == 0xFC) &&
1201 (fis->features == 0x27 || fis->features == 0x72 ||
1202 fis->features == 0x62 || fis->features == 0x26))) {
1203 /* Com reset after secure erase or lowlevel format */
1204 mtip_restart_port(port);
1205 return false;
1206 }
1207
1208 return false;
1209}
1210
Sam Bradshaw88523a62011-08-30 08:34:26 -06001211/*
1212 * Wait for port to quiesce
1213 *
1214 * @port Pointer to port data structure
1215 * @timeout Max duration to wait (ms)
1216 *
1217 * return value
1218 * 0 Success
1219 * -EBUSY Commands still active
1220 */
1221static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1222{
1223 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001224 unsigned int n;
1225 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001226
1227 to = jiffies + msecs_to_jiffies(timeout);
1228 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001229 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1230 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001231 msleep(20);
1232 continue; /* svc thd is actively issuing commands */
1233 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001234 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001235 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001236 /*
1237 * Ignore s_active bit 0 of array element 0.
1238 * This bit will always be set
1239 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001240 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001241 for (n = 1; n < port->dd->slot_groups; n++)
1242 active |= readl(port->s_active[n]);
1243
1244 if (!active)
1245 break;
1246
1247 msleep(20);
1248 } while (time_before(jiffies, to));
1249
1250 return active ? -EBUSY : 0;
1251}
1252
1253/*
1254 * Execute an internal command and wait for the completion.
1255 *
1256 * @port Pointer to the port data structure.
1257 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001258 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001259 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001260 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001261 * @opts Command header options, excluding the FIS length
1262 * and the number of PRD entries.
1263 * @timeout Time in ms to wait for the command to complete.
1264 *
1265 * return value
1266 * 0 Command completed successfully.
1267 * -EFAULT The buffer address is not correctly aligned.
1268 * -EBUSY Internal command or other IO in progress.
1269 * -EAGAIN Time out waiting for command to complete.
1270 */
1271static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001272 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001273 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001274 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001275 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001276 u32 opts,
1277 gfp_t atomic,
1278 unsigned long timeout)
1279{
1280 struct mtip_cmd_sg *command_sg;
1281 DECLARE_COMPLETION_ONSTACK(wait);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001282 int rv = 0, ready2go = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001283 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001284 unsigned long to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001285
1286 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1287 if (buffer & 0x00000007) {
1288 dev_err(&port->dd->pdev->dev,
1289 "SG buffer is not 8 byte aligned\n");
1290 return -EFAULT;
1291 }
1292
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001293 to = jiffies + msecs_to_jiffies(timeout);
1294 do {
1295 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1296 port->allocated);
1297 if (ready2go)
1298 break;
1299 mdelay(100);
1300 } while (time_before(jiffies, to));
1301 if (!ready2go) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001302 dev_warn(&port->dd->pdev->dev,
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001303 "Internal cmd active. new cmd [%02X]\n", fis->command);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001304 return -EBUSY;
1305 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001306 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001307 port->ic_pause_timer = 0;
1308
1309 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1310 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1311 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1312 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001313
1314 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001315 if (fis->command != ATA_CMD_STANDBYNOW1) {
1316 /* wait for io to complete if non atomic */
1317 if (mtip_quiesce_io(port, 5000) < 0) {
1318 dev_warn(&port->dd->pdev->dev,
1319 "Failed to quiesce IO\n");
1320 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001321 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001322 wake_up_interruptible(&port->svc_wait);
1323 return -EBUSY;
1324 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001325 }
1326
1327 /* Set the completion function and data for the command. */
1328 int_cmd->comp_data = &wait;
1329 int_cmd->comp_func = mtip_completion;
1330
1331 } else {
1332 /* Clear completion - we're going to poll */
1333 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001334 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001335 }
1336
1337 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001338 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001339
1340 /* Populate the SG list */
1341 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001342 __force_bit2int cpu_to_le32(opts | fis_len);
1343 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001344 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1345
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001346 command_sg->info =
1347 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1348 command_sg->dba =
1349 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1350 command_sg->dba_upper =
1351 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001352
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001353 int_cmd->command_header->opts |=
1354 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001355 }
1356
1357 /* Populate the command header */
1358 int_cmd->command_header->byte_count = 0;
1359
1360 /* Issue the command to the hardware */
1361 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1362
1363 /* Poll if atomic, wait_for_completion otherwise */
1364 if (atomic == GFP_KERNEL) {
1365 /* Wait for the command to complete or timeout. */
1366 if (wait_for_completion_timeout(
1367 &wait,
1368 msecs_to_jiffies(timeout)) == 0) {
1369 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001370 "Internal command did not complete [%d] "
1371 "within timeout of %lu ms\n",
1372 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001373 if (mtip_check_surprise_removal(port->dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001374 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001375 &port->dd->dd_flag)) {
1376 rv = -ENXIO;
1377 goto exec_ic_exit;
1378 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001379 rv = -EAGAIN;
1380 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001381 } else {
1382 /* Spin for <timeout> checking if command still outstanding */
1383 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001384 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1385 & (1 << MTIP_TAG_INTERNAL))
1386 && time_before(jiffies, timeout)) {
1387 if (mtip_check_surprise_removal(port->dd->pdev)) {
1388 rv = -ENXIO;
1389 goto exec_ic_exit;
1390 }
1391 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001392 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001393 &port->dd->dd_flag)) {
1394 rv = -ENXIO;
1395 goto exec_ic_exit;
1396 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001397 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1398 atomic_inc(&int_cmd->active); /* error */
1399 break;
Asai Thambi S P45038362012-04-09 08:35:38 +02001400 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001401 }
1402 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001403
1404 if (atomic_read(&int_cmd->active) > 1) {
1405 dev_err(&port->dd->pdev->dev,
1406 "Internal command [%02X] failed\n", fis->command);
1407 rv = -EIO;
1408 }
1409 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1410 & (1 << MTIP_TAG_INTERNAL)) {
1411 rv = -ENXIO;
1412 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1413 &port->dd->dd_flag)) {
1414 mtip_restart_port(port);
1415 rv = -EAGAIN;
1416 }
1417 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001418exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001419 /* Clear the allocated and active bits for the internal command. */
1420 atomic_set(&int_cmd->active, 0);
1421 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001422 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1423 /* NCQ paused */
1424 return rv;
1425 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001426 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001427 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001428
1429 return rv;
1430}
1431
1432/*
1433 * Byte-swap ATA ID strings.
1434 *
1435 * ATA identify data contains strings in byte-swapped 16-bit words.
1436 * They must be swapped (on all architectures) to be usable as C strings.
1437 * This function swaps bytes in-place.
1438 *
1439 * @buf The buffer location of the string
1440 * @len The number of bytes to swap
1441 *
1442 * return value
1443 * None
1444 */
1445static inline void ata_swap_string(u16 *buf, unsigned int len)
1446{
1447 int i;
1448 for (i = 0; i < (len/2); i++)
1449 be16_to_cpus(&buf[i]);
1450}
1451
1452/*
1453 * Request the device identity information.
1454 *
1455 * If a user space buffer is not specified, i.e. is NULL, the
1456 * identify information is still read from the drive and placed
1457 * into the identify data buffer (@e port->identify) in the
1458 * port data structure.
1459 * When the identify buffer contains valid identify information @e
1460 * port->identify_valid is non-zero.
1461 *
1462 * @port Pointer to the port structure.
1463 * @user_buffer A user space buffer where the identify data should be
1464 * copied.
1465 *
1466 * return value
1467 * 0 Command completed successfully.
1468 * -EFAULT An error occurred while coping data to the user buffer.
1469 * -1 Command failed.
1470 */
1471static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1472{
1473 int rv = 0;
1474 struct host_to_dev_fis fis;
1475
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001476 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001477 return -EFAULT;
1478
Sam Bradshaw88523a62011-08-30 08:34:26 -06001479 /* Build the FIS. */
1480 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1481 fis.type = 0x27;
1482 fis.opts = 1 << 7;
1483 fis.command = ATA_CMD_ID_ATA;
1484
1485 /* Set the identify information as invalid. */
1486 port->identify_valid = 0;
1487
1488 /* Clear the identify information. */
1489 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1490
1491 /* Execute the command. */
1492 if (mtip_exec_internal_command(port,
1493 &fis,
1494 5,
1495 port->identify_dma,
1496 sizeof(u16) * ATA_ID_WORDS,
1497 0,
1498 GFP_KERNEL,
1499 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1500 < 0) {
1501 rv = -1;
1502 goto out;
1503 }
1504
1505 /*
1506 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1507 * perform field-sensitive swapping on the string fields.
1508 * See the kernel use of ata_id_string() for proof of this.
1509 */
1510#ifdef __LITTLE_ENDIAN
1511 ata_swap_string(port->identify + 27, 40); /* model string*/
1512 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1513 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1514#else
1515 {
1516 int i;
1517 for (i = 0; i < ATA_ID_WORDS; i++)
1518 port->identify[i] = le16_to_cpu(port->identify[i]);
1519 }
1520#endif
1521
Asai Thambi S P15283462013-01-11 14:41:34 +01001522 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1523 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1524 port->dd->trim_supp = true;
1525 else
1526 port->dd->trim_supp = false;
1527
Sam Bradshaw88523a62011-08-30 08:34:26 -06001528 /* Set the identify buffer as valid. */
1529 port->identify_valid = 1;
1530
1531 if (user_buffer) {
1532 if (copy_to_user(
1533 user_buffer,
1534 port->identify,
1535 ATA_ID_WORDS * sizeof(u16))) {
1536 rv = -EFAULT;
1537 goto out;
1538 }
1539 }
1540
1541out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001542 return rv;
1543}
1544
1545/*
1546 * Issue a standby immediate command to the device.
1547 *
1548 * @port Pointer to the port structure.
1549 *
1550 * return value
1551 * 0 Command was executed successfully.
1552 * -1 An error occurred while executing the command.
1553 */
1554static int mtip_standby_immediate(struct mtip_port *port)
1555{
1556 int rv;
1557 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001558 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001559
Sam Bradshaw88523a62011-08-30 08:34:26 -06001560 /* Build the FIS. */
1561 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1562 fis.type = 0x27;
1563 fis.opts = 1 << 7;
1564 fis.command = ATA_CMD_STANDBYNOW1;
1565
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001566 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001567 rv = mtip_exec_internal_command(port,
1568 &fis,
1569 5,
1570 0,
1571 0,
1572 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001573 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001574 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001575 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1576 jiffies_to_msecs(jiffies - start));
1577 if (rv)
1578 dev_warn(&port->dd->pdev->dev,
1579 "STANDBY IMMEDIATE command failed.\n");
1580
1581 return rv;
1582}
1583
1584/*
1585 * Issue a READ LOG EXT command to the device.
1586 *
1587 * @port pointer to the port structure.
1588 * @page page number to fetch
1589 * @buffer pointer to buffer
1590 * @buffer_dma dma address corresponding to @buffer
1591 * @sectors page length to fetch, in sectors
1592 *
1593 * return value
1594 * @rv return value from mtip_exec_internal_command()
1595 */
1596static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1597 dma_addr_t buffer_dma, unsigned int sectors)
1598{
1599 struct host_to_dev_fis fis;
1600
1601 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1602 fis.type = 0x27;
1603 fis.opts = 1 << 7;
1604 fis.command = ATA_CMD_READ_LOG_EXT;
1605 fis.sect_count = sectors & 0xFF;
1606 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1607 fis.lba_low = page;
1608 fis.lba_mid = 0;
1609 fis.device = ATA_DEVICE_OBS;
1610
1611 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1612
1613 return mtip_exec_internal_command(port,
1614 &fis,
1615 5,
1616 buffer_dma,
1617 sectors * ATA_SECT_SIZE,
1618 0,
1619 GFP_ATOMIC,
1620 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1621}
1622
1623/*
1624 * Issue a SMART READ DATA command to the device.
1625 *
1626 * @port pointer to the port structure.
1627 * @buffer pointer to buffer
1628 * @buffer_dma dma address corresponding to @buffer
1629 *
1630 * return value
1631 * @rv return value from mtip_exec_internal_command()
1632 */
1633static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1634 dma_addr_t buffer_dma)
1635{
1636 struct host_to_dev_fis fis;
1637
1638 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1639 fis.type = 0x27;
1640 fis.opts = 1 << 7;
1641 fis.command = ATA_CMD_SMART;
1642 fis.features = 0xD0;
1643 fis.sect_count = 1;
1644 fis.lba_mid = 0x4F;
1645 fis.lba_hi = 0xC2;
1646 fis.device = ATA_DEVICE_OBS;
1647
1648 return mtip_exec_internal_command(port,
1649 &fis,
1650 5,
1651 buffer_dma,
1652 ATA_SECT_SIZE,
1653 0,
1654 GFP_ATOMIC,
1655 15000);
1656}
1657
1658/*
1659 * Get the value of a smart attribute
1660 *
1661 * @port pointer to the port structure
1662 * @id attribute number
1663 * @attrib pointer to return attrib information corresponding to @id
1664 *
1665 * return value
1666 * -EINVAL NULL buffer passed or unsupported attribute @id.
1667 * -EPERM Identify data not valid, SMART not supported or not enabled
1668 */
1669static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1670 struct smart_attr *attrib)
1671{
1672 int rv, i;
1673 struct smart_attr *pattr;
1674
1675 if (!attrib)
1676 return -EINVAL;
1677
1678 if (!port->identify_valid) {
1679 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1680 return -EPERM;
1681 }
1682 if (!(port->identify[82] & 0x1)) {
1683 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1684 return -EPERM;
1685 }
1686 if (!(port->identify[85] & 0x1)) {
1687 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1688 return -EPERM;
1689 }
1690
1691 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1692 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1693 if (rv) {
1694 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1695 return rv;
1696 }
1697
1698 pattr = (struct smart_attr *)(port->smart_buf + 2);
1699 for (i = 0; i < 29; i++, pattr++)
1700 if (pattr->attr_id == id) {
1701 memcpy(attrib, pattr, sizeof(struct smart_attr));
1702 break;
1703 }
1704
1705 if (i == 29) {
1706 dev_warn(&port->dd->pdev->dev,
1707 "Query for invalid SMART attribute ID\n");
1708 rv = -EINVAL;
1709 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001710
Sam Bradshaw88523a62011-08-30 08:34:26 -06001711 return rv;
1712}
1713
1714/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001715 * Trim unused sectors
1716 *
1717 * @dd pointer to driver_data structure
1718 * @lba starting lba
1719 * @len # of 512b sectors to trim
1720 *
1721 * return value
1722 * -ENOMEM Out of dma memory
1723 * -EINVAL Invalid parameters passed in, trim not supported
1724 * -EIO Error submitting trim request to hw
1725 */
1726int mtip_send_trim(struct driver_data *dd, unsigned int lba, unsigned int len)
1727{
1728 int i, rv = 0;
1729 u64 tlba, tlen, sect_left;
1730 struct mtip_trim_entry *buf;
1731 dma_addr_t dma_addr;
1732 struct host_to_dev_fis fis;
1733
1734 if (!len || dd->trim_supp == false)
1735 return -EINVAL;
1736
1737 /* Trim request too big */
1738 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1739
1740 /* Trim request not aligned on 4k boundary */
1741 WARN_ON(len % 8 != 0);
1742
1743 /* Warn if vu_trim structure is too big */
1744 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1745
1746 /* Allocate a DMA buffer for the trim structure */
1747 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1748 GFP_KERNEL);
1749 if (!buf)
1750 return -ENOMEM;
1751 memset(buf, 0, ATA_SECT_SIZE);
1752
1753 for (i = 0, sect_left = len, tlba = lba;
1754 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1755 i++) {
1756 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1757 MTIP_MAX_TRIM_ENTRY_LEN :
1758 sect_left);
1759 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1760 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1761 tlba += tlen;
1762 sect_left -= tlen;
1763 }
1764 WARN_ON(sect_left != 0);
1765
1766 /* Build the fis */
1767 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1768 fis.type = 0x27;
1769 fis.opts = 1 << 7;
1770 fis.command = 0xfb;
1771 fis.features = 0x60;
1772 fis.sect_count = 1;
1773 fis.device = ATA_DEVICE_OBS;
1774
1775 if (mtip_exec_internal_command(dd->port,
1776 &fis,
1777 5,
1778 dma_addr,
1779 ATA_SECT_SIZE,
1780 0,
1781 GFP_KERNEL,
1782 MTIP_TRIM_TIMEOUT_MS) < 0)
1783 rv = -EIO;
1784
1785 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1786 return rv;
1787}
1788
1789/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001790 * Get the drive capacity.
1791 *
1792 * @dd Pointer to the device data structure.
1793 * @sectors Pointer to the variable that will receive the sector count.
1794 *
1795 * return value
1796 * 1 Capacity was returned successfully.
1797 * 0 The identify information is invalid.
1798 */
Jens Axboe63166682011-09-27 21:27:43 -06001799static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001800{
1801 struct mtip_port *port = dd->port;
1802 u64 total, raw0, raw1, raw2, raw3;
1803 raw0 = port->identify[100];
1804 raw1 = port->identify[101];
1805 raw2 = port->identify[102];
1806 raw3 = port->identify[103];
1807 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1808 *sectors = total;
1809 return (bool) !!port->identify_valid;
1810}
1811
1812/*
1813 * Reset the HBA.
1814 *
1815 * Resets the HBA by setting the HBA Reset bit in the Global
1816 * HBA Control register. After setting the HBA Reset bit the
1817 * function waits for 1 second before reading the HBA Reset
1818 * bit to make sure it has cleared. If HBA Reset is not clear
1819 * an error is returned. Cannot be used in non-blockable
1820 * context.
1821 *
1822 * @dd Pointer to the driver data structure.
1823 *
1824 * return value
1825 * 0 The reset was successful.
1826 * -1 The HBA Reset bit did not clear.
1827 */
1828static int mtip_hba_reset(struct driver_data *dd)
1829{
1830 mtip_deinit_port(dd->port);
1831
1832 /* Set the reset bit */
1833 writel(HOST_RESET, dd->mmio + HOST_CTL);
1834
1835 /* Flush */
1836 readl(dd->mmio + HOST_CTL);
1837
1838 /* Wait for reset to clear */
1839 ssleep(1);
1840
1841 /* Check the bit has cleared */
1842 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1843 dev_err(&dd->pdev->dev,
1844 "Reset bit did not clear.\n");
1845 return -1;
1846 }
1847
1848 return 0;
1849}
1850
1851/*
1852 * Display the identify command data.
1853 *
1854 * @port Pointer to the port data structure.
1855 *
1856 * return value
1857 * None
1858 */
1859static void mtip_dump_identify(struct mtip_port *port)
1860{
1861 sector_t sectors;
1862 unsigned short revid;
1863 char cbuf[42];
1864
1865 if (!port->identify_valid)
1866 return;
1867
1868 strlcpy(cbuf, (char *)(port->identify+10), 21);
1869 dev_info(&port->dd->pdev->dev,
1870 "Serial No.: %s\n", cbuf);
1871
1872 strlcpy(cbuf, (char *)(port->identify+23), 9);
1873 dev_info(&port->dd->pdev->dev,
1874 "Firmware Ver.: %s\n", cbuf);
1875
1876 strlcpy(cbuf, (char *)(port->identify+27), 41);
1877 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1878
1879 if (mtip_hw_get_capacity(port->dd, &sectors))
1880 dev_info(&port->dd->pdev->dev,
1881 "Capacity: %llu sectors (%llu MB)\n",
1882 (u64)sectors,
1883 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1884
1885 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001886 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001887 case 0x1:
1888 strlcpy(cbuf, "A0", 3);
1889 break;
1890 case 0x3:
1891 strlcpy(cbuf, "A2", 3);
1892 break;
1893 default:
1894 strlcpy(cbuf, "?", 2);
1895 break;
1896 }
1897 dev_info(&port->dd->pdev->dev,
1898 "Card Type: %s\n", cbuf);
1899}
1900
1901/*
1902 * Map the commands scatter list into the command table.
1903 *
1904 * @command Pointer to the command.
1905 * @nents Number of scatter list entries.
1906 *
1907 * return value
1908 * None
1909 */
1910static inline void fill_command_sg(struct driver_data *dd,
1911 struct mtip_cmd *command,
1912 int nents)
1913{
1914 int n;
1915 unsigned int dma_len;
1916 struct mtip_cmd_sg *command_sg;
1917 struct scatterlist *sg = command->sg;
1918
1919 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1920
1921 for (n = 0; n < nents; n++) {
1922 dma_len = sg_dma_len(sg);
1923 if (dma_len > 0x400000)
1924 dev_err(&dd->pdev->dev,
1925 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001926 command_sg->info = __force_bit2int
1927 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1928 command_sg->dba = __force_bit2int
1929 cpu_to_le32(sg_dma_address(sg));
1930 command_sg->dba_upper = __force_bit2int
1931 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001932 command_sg++;
1933 sg++;
1934 }
1935}
1936
1937/*
1938 * @brief Execute a drive command.
1939 *
1940 * return value 0 The command completed successfully.
1941 * return value -1 An error occurred while executing the command.
1942 */
Jens Axboe63166682011-09-27 21:27:43 -06001943static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001944{
1945 struct host_to_dev_fis fis;
1946 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1947
Sam Bradshaw88523a62011-08-30 08:34:26 -06001948 /* Build the FIS. */
1949 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1950 fis.type = 0x27;
1951 fis.opts = 1 << 7;
1952 fis.command = command[0];
1953 fis.features = command[1];
1954 fis.sect_count = command[2];
1955 fis.sector = command[3];
1956 fis.cyl_low = command[4];
1957 fis.cyl_hi = command[5];
1958 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1959
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001960 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 -06001961 __func__,
1962 command[0],
1963 command[1],
1964 command[2],
1965 command[3],
1966 command[4],
1967 command[5],
1968 command[6]);
1969
1970 /* Execute the command. */
1971 if (mtip_exec_internal_command(port,
1972 &fis,
1973 5,
1974 0,
1975 0,
1976 0,
1977 GFP_KERNEL,
1978 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001979 return -1;
1980 }
1981
1982 command[0] = reply->command; /* Status*/
1983 command[1] = reply->features; /* Error*/
1984 command[4] = reply->cyl_low;
1985 command[5] = reply->cyl_hi;
1986
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001987 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 -06001988 __func__,
1989 command[0],
1990 command[1],
1991 command[4],
1992 command[5]);
1993
Sam Bradshaw88523a62011-08-30 08:34:26 -06001994 return 0;
1995}
1996
1997/*
1998 * @brief Execute a drive command.
1999 *
2000 * @param port Pointer to the port data structure.
2001 * @param command Pointer to the user specified command parameters.
2002 * @param user_buffer Pointer to the user space buffer where read sector
2003 * data should be copied.
2004 *
2005 * return value 0 The command completed successfully.
2006 * return value -EFAULT An error occurred while copying the completion
2007 * data to the user space buffer.
2008 * return value -1 An error occurred while executing the command.
2009 */
Jens Axboe63166682011-09-27 21:27:43 -06002010static int exec_drive_command(struct mtip_port *port, u8 *command,
2011 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002012{
2013 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002014 struct host_to_dev_fis *reply;
2015 u8 *buf = NULL;
2016 dma_addr_t dma_addr = 0;
2017 int rv = 0, xfer_sz = command[3];
2018
2019 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05002020 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002021 return -EFAULT;
2022
2023 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2024 ATA_SECT_SIZE * xfer_sz,
2025 &dma_addr,
2026 GFP_KERNEL);
2027 if (!buf) {
2028 dev_err(&port->dd->pdev->dev,
2029 "Memory allocation failed (%d bytes)\n",
2030 ATA_SECT_SIZE * xfer_sz);
2031 return -ENOMEM;
2032 }
2033 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2034 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002035
Sam Bradshaw88523a62011-08-30 08:34:26 -06002036 /* Build the FIS. */
2037 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002038 fis.type = 0x27;
2039 fis.opts = 1 << 7;
2040 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06002041 fis.features = command[2];
2042 fis.sect_count = command[3];
2043 if (fis.command == ATA_CMD_SMART) {
2044 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002045 fis.cyl_low = 0x4F;
2046 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002047 }
2048
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002049 if (xfer_sz)
2050 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2051 else
2052 reply = (port->rxfis + RX_FIS_D2H_REG);
2053
Sam Bradshaw88523a62011-08-30 08:34:26 -06002054 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002055 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06002056 "feat %x, sectcnt %x\n",
2057 __func__,
2058 command[0],
2059 command[1],
2060 command[2],
2061 command[3]);
2062
Sam Bradshaw88523a62011-08-30 08:34:26 -06002063 /* Execute the command. */
2064 if (mtip_exec_internal_command(port,
2065 &fis,
2066 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002067 (xfer_sz ? dma_addr : 0),
2068 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002069 0,
2070 GFP_KERNEL,
2071 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2072 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002073 rv = -EFAULT;
2074 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002075 }
2076
2077 /* Collect the completion status. */
2078 command[0] = reply->command; /* Status*/
2079 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002080 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002081
2082 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002083 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002084 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002085 __func__,
2086 command[0],
2087 command[1],
2088 command[2]);
2089
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002090 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002091 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002092 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002093 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002094 rv = -EFAULT;
2095 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002096 }
2097 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07002098exit_drive_command:
2099 if (buf)
2100 dmam_free_coherent(&port->dd->pdev->dev,
2101 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2102 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002103}
2104
2105/*
2106 * Indicates whether a command has a single sector payload.
2107 *
2108 * @command passed to the device to perform the certain event.
2109 * @features passed to the device to perform the certain event.
2110 *
2111 * return value
2112 * 1 command is one that always has a single sector payload,
2113 * regardless of the value in the Sector Count field.
2114 * 0 otherwise
2115 *
2116 */
2117static unsigned int implicit_sector(unsigned char command,
2118 unsigned char features)
2119{
2120 unsigned int rv = 0;
2121
2122 /* list of commands that have an implicit sector count of 1 */
2123 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002124 case ATA_CMD_SEC_SET_PASS:
2125 case ATA_CMD_SEC_UNLOCK:
2126 case ATA_CMD_SEC_ERASE_PREP:
2127 case ATA_CMD_SEC_ERASE_UNIT:
2128 case ATA_CMD_SEC_FREEZE_LOCK:
2129 case ATA_CMD_SEC_DISABLE_PASS:
2130 case ATA_CMD_PMP_READ:
2131 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06002132 rv = 1;
2133 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002134 case ATA_CMD_SET_MAX:
2135 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002136 rv = 1;
2137 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002138 case ATA_CMD_SMART:
2139 if ((features == ATA_SMART_READ_VALUES) ||
2140 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002141 rv = 1;
2142 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002143 case ATA_CMD_CONF_OVERLAY:
2144 if ((features == ATA_DCO_IDENTIFY) ||
2145 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002146 rv = 1;
2147 break;
2148 }
2149 return rv;
2150}
Selvan Mani4453bc82012-09-27 14:36:43 +02002151static void mtip_set_timeout(struct driver_data *dd,
2152 struct host_to_dev_fis *fis,
2153 unsigned int *timeout, u8 erasemode)
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002154{
2155 switch (fis->command) {
2156 case ATA_CMD_DOWNLOAD_MICRO:
2157 *timeout = 120000; /* 2 minutes */
2158 break;
2159 case ATA_CMD_SEC_ERASE_UNIT:
2160 case 0xFC:
Selvan Mani4453bc82012-09-27 14:36:43 +02002161 if (erasemode)
2162 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2163 else
2164 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002165 break;
2166 case ATA_CMD_STANDBYNOW1:
Asai Thambi S Pd7c8b942012-09-05 22:02:16 +05302167 *timeout = 120000; /* 2 minutes */
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002168 break;
2169 case 0xF7:
2170 case 0xFA:
2171 *timeout = 60000; /* 60 seconds */
2172 break;
2173 case ATA_CMD_SMART:
2174 *timeout = 15000; /* 15 seconds */
2175 break;
2176 default:
2177 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2178 break;
2179 }
2180}
2181
Sam Bradshaw88523a62011-08-30 08:34:26 -06002182/*
2183 * Executes a taskfile
2184 * See ide_taskfile_ioctl() for derivation
2185 */
2186static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002187 void __user *buf,
2188 ide_task_request_t *req_task,
2189 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002190{
2191 struct host_to_dev_fis fis;
2192 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002193 u8 *outbuf = NULL;
2194 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002195 dma_addr_t outbuf_dma = 0;
2196 dma_addr_t inbuf_dma = 0;
2197 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002198 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002199 unsigned int taskin = 0;
2200 unsigned int taskout = 0;
2201 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002202 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002203 unsigned int force_single_sector;
2204 unsigned int transfer_size;
2205 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002206 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002207 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002208
2209 taskout = req_task->out_size;
2210 taskin = req_task->in_size;
2211 /* 130560 = 512 * 0xFF*/
2212 if (taskin > 130560 || taskout > 130560) {
2213 err = -EINVAL;
2214 goto abort;
2215 }
2216
2217 if (taskout) {
2218 outbuf = kzalloc(taskout, GFP_KERNEL);
2219 if (outbuf == NULL) {
2220 err = -ENOMEM;
2221 goto abort;
2222 }
2223 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2224 err = -EFAULT;
2225 goto abort;
2226 }
2227 outbuf_dma = pci_map_single(dd->pdev,
2228 outbuf,
2229 taskout,
2230 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002231 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002232 err = -ENOMEM;
2233 goto abort;
2234 }
2235 dma_buffer = outbuf_dma;
2236 }
2237
2238 if (taskin) {
2239 inbuf = kzalloc(taskin, GFP_KERNEL);
2240 if (inbuf == NULL) {
2241 err = -ENOMEM;
2242 goto abort;
2243 }
2244
2245 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2246 err = -EFAULT;
2247 goto abort;
2248 }
2249 inbuf_dma = pci_map_single(dd->pdev,
2250 inbuf,
2251 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002252 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002253 err = -ENOMEM;
2254 goto abort;
2255 }
2256 dma_buffer = inbuf_dma;
2257 }
2258
2259 /* only supports PIO and non-data commands from this ioctl. */
2260 switch (req_task->data_phase) {
2261 case TASKFILE_OUT:
2262 nsect = taskout / ATA_SECT_SIZE;
2263 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2264 break;
2265 case TASKFILE_IN:
2266 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2267 break;
2268 case TASKFILE_NO_DATA:
2269 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2270 break;
2271 default:
2272 err = -EINVAL;
2273 goto abort;
2274 }
2275
Sam Bradshaw88523a62011-08-30 08:34:26 -06002276 /* Build the FIS. */
2277 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2278
2279 fis.type = 0x27;
2280 fis.opts = 1 << 7;
2281 fis.command = req_task->io_ports[7];
2282 fis.features = req_task->io_ports[1];
2283 fis.sect_count = req_task->io_ports[2];
2284 fis.lba_low = req_task->io_ports[3];
2285 fis.lba_mid = req_task->io_ports[4];
2286 fis.lba_hi = req_task->io_ports[5];
2287 /* Clear the dev bit*/
2288 fis.device = req_task->io_ports[6] & ~0x10;
2289
2290 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2291 req_task->in_flags.all =
2292 IDE_TASKFILE_STD_IN_FLAGS |
2293 (IDE_HOB_STD_IN_FLAGS << 8);
2294 fis.lba_low_ex = req_task->hob_ports[3];
2295 fis.lba_mid_ex = req_task->hob_ports[4];
2296 fis.lba_hi_ex = req_task->hob_ports[5];
2297 fis.features_ex = req_task->hob_ports[1];
2298 fis.sect_cnt_ex = req_task->hob_ports[2];
2299
2300 } else {
2301 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2302 }
2303
2304 force_single_sector = implicit_sector(fis.command, fis.features);
2305
2306 if ((taskin || taskout) && (!fis.sect_count)) {
2307 if (nsect)
2308 fis.sect_count = nsect;
2309 else {
2310 if (!force_single_sector) {
2311 dev_warn(&dd->pdev->dev,
2312 "data movement but "
2313 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002314 err = -EINVAL;
2315 goto abort;
2316 }
2317 }
2318 }
2319
2320 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002321 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002322 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2323 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002324 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002325 fis.command,
2326 fis.features,
2327 fis.sect_count,
2328 fis.lba_low,
2329 fis.lba_mid,
2330 fis.lba_hi,
2331 fis.device);
2332
Selvan Mani4453bc82012-09-27 14:36:43 +02002333 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002334 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2335 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002336 erasemode = 1;
2337 }
2338
2339 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002340
2341 /* Determine the correct transfer size.*/
2342 if (force_single_sector)
2343 transfer_size = ATA_SECT_SIZE;
2344 else
2345 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2346
2347 /* Execute the command.*/
2348 if (mtip_exec_internal_command(dd->port,
2349 &fis,
2350 5,
2351 dma_buffer,
2352 transfer_size,
2353 0,
2354 GFP_KERNEL,
2355 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002356 err = -EIO;
2357 goto abort;
2358 }
2359
2360 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2361
2362 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2363 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2364 req_task->io_ports[7] = reply->control;
2365 } else {
2366 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2367 req_task->io_ports[7] = reply->command;
2368 }
2369
2370 /* reclaim the DMA buffers.*/
2371 if (inbuf_dma)
2372 pci_unmap_single(dd->pdev, inbuf_dma,
2373 taskin, DMA_FROM_DEVICE);
2374 if (outbuf_dma)
2375 pci_unmap_single(dd->pdev, outbuf_dma,
2376 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002377 inbuf_dma = 0;
2378 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002379
2380 /* return the ATA registers to the caller.*/
2381 req_task->io_ports[1] = reply->features;
2382 req_task->io_ports[2] = reply->sect_count;
2383 req_task->io_ports[3] = reply->lba_low;
2384 req_task->io_ports[4] = reply->lba_mid;
2385 req_task->io_ports[5] = reply->lba_hi;
2386 req_task->io_ports[6] = reply->device;
2387
2388 if (req_task->out_flags.all & 1) {
2389
2390 req_task->hob_ports[3] = reply->lba_low_ex;
2391 req_task->hob_ports[4] = reply->lba_mid_ex;
2392 req_task->hob_ports[5] = reply->lba_hi_ex;
2393 req_task->hob_ports[1] = reply->features_ex;
2394 req_task->hob_ports[2] = reply->sect_cnt_ex;
2395 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002396 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002397 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398 "err %x, sect_cnt %x, lbalo %x,"
2399 "lbamid %x, lbahi %x, dev %x\n",
2400 __func__,
2401 req_task->io_ports[7],
2402 req_task->io_ports[1],
2403 req_task->io_ports[2],
2404 req_task->io_ports[3],
2405 req_task->io_ports[4],
2406 req_task->io_ports[5],
2407 req_task->io_ports[6]);
2408
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 if (taskout) {
2410 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2411 err = -EFAULT;
2412 goto abort;
2413 }
2414 }
2415 if (taskin) {
2416 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2417 err = -EFAULT;
2418 goto abort;
2419 }
2420 }
2421abort:
2422 if (inbuf_dma)
2423 pci_unmap_single(dd->pdev, inbuf_dma,
2424 taskin, DMA_FROM_DEVICE);
2425 if (outbuf_dma)
2426 pci_unmap_single(dd->pdev, outbuf_dma,
2427 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002428 kfree(outbuf);
2429 kfree(inbuf);
2430
2431 return err;
2432}
2433
2434/*
2435 * Handle IOCTL calls from the Block Layer.
2436 *
2437 * This function is called by the Block Layer when it receives an IOCTL
2438 * command that it does not understand. If the IOCTL command is not supported
2439 * this function returns -ENOTTY.
2440 *
2441 * @dd Pointer to the driver data structure.
2442 * @cmd IOCTL command passed from the Block Layer.
2443 * @arg IOCTL argument passed from the Block Layer.
2444 *
2445 * return value
2446 * 0 The IOCTL completed successfully.
2447 * -ENOTTY The specified command is not supported.
2448 * -EFAULT An error occurred copying data to a user space buffer.
2449 * -EIO An error occurred while executing the command.
2450 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002451static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2452 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002453{
2454 switch (cmd) {
2455 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002456 {
2457 if (copy_to_user((void __user *)arg, dd->port->identify,
2458 sizeof(u16) * ATA_ID_WORDS))
2459 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002460 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002461 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002462 case HDIO_DRIVE_CMD:
2463 {
2464 u8 drive_command[4];
2465
2466 /* Copy the user command info to our buffer. */
2467 if (copy_from_user(drive_command,
2468 (void __user *) arg,
2469 sizeof(drive_command)))
2470 return -EFAULT;
2471
2472 /* Execute the drive command. */
2473 if (exec_drive_command(dd->port,
2474 drive_command,
2475 (void __user *) (arg+4)))
2476 return -EIO;
2477
2478 /* Copy the status back to the users buffer. */
2479 if (copy_to_user((void __user *) arg,
2480 drive_command,
2481 sizeof(drive_command)))
2482 return -EFAULT;
2483
2484 break;
2485 }
2486 case HDIO_DRIVE_TASK:
2487 {
2488 u8 drive_command[7];
2489
2490 /* Copy the user command info to our buffer. */
2491 if (copy_from_user(drive_command,
2492 (void __user *) arg,
2493 sizeof(drive_command)))
2494 return -EFAULT;
2495
2496 /* Execute the drive command. */
2497 if (exec_drive_task(dd->port, drive_command))
2498 return -EIO;
2499
2500 /* Copy the status back to the users buffer. */
2501 if (copy_to_user((void __user *) arg,
2502 drive_command,
2503 sizeof(drive_command)))
2504 return -EFAULT;
2505
2506 break;
2507 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002508 case HDIO_DRIVE_TASKFILE: {
2509 ide_task_request_t req_task;
2510 int ret, outtotal;
2511
2512 if (copy_from_user(&req_task, (void __user *) arg,
2513 sizeof(req_task)))
2514 return -EFAULT;
2515
2516 outtotal = sizeof(req_task);
2517
2518 ret = exec_drive_taskfile(dd, (void __user *) arg,
2519 &req_task, outtotal);
2520
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002521 if (copy_to_user((void __user *) arg, &req_task,
2522 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002523 return -EFAULT;
2524
2525 return ret;
2526 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002527
2528 default:
2529 return -EINVAL;
2530 }
2531 return 0;
2532}
2533
2534/*
2535 * Submit an IO to the hw
2536 *
2537 * This function is called by the block layer to issue an io
2538 * to the device. Upon completion, the callback function will
2539 * be called with the data parameter passed as the callback data.
2540 *
2541 * @dd Pointer to the driver data structure.
2542 * @start First sector to read.
2543 * @nsect Number of sectors to read.
2544 * @nents Number of entries in scatter list for the read command.
2545 * @tag The tag of this read command.
2546 * @callback Pointer to the function that should be called
2547 * when the read completes.
2548 * @data Callback data passed to the callback function
2549 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002550 * @dir Direction (read or write)
2551 *
2552 * return value
2553 * None
2554 */
Jens Axboe7c5d6232012-11-08 07:58:53 +01002555static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
Jens Axboe63166682011-09-27 21:27:43 -06002556 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002557 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002558{
2559 struct host_to_dev_fis *fis;
2560 struct mtip_port *port = dd->port;
2561 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002562 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Jens Axboe7c5d6232012-11-08 07:58:53 +01002563 u64 start = sector;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002564
2565 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002566 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002567
2568 command->scatter_ents = nents;
2569
2570 /*
2571 * The number of retries for this command before it is
2572 * reported as a failure to the upper layers.
2573 */
2574 command->retries = MTIP_MAX_RETRIES;
2575
2576 /* Fill out fis */
2577 fis = command->command;
2578 fis->type = 0x27;
2579 fis->opts = 1 << 7;
2580 fis->command =
2581 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Selvan Manieda45312012-11-07 06:03:53 -07002582 fis->lba_low = start & 0xFF;
2583 fis->lba_mid = (start >> 8) & 0xFF;
2584 fis->lba_hi = (start >> 16) & 0xFF;
2585 fis->lba_low_ex = (start >> 24) & 0xFF;
2586 fis->lba_mid_ex = (start >> 32) & 0xFF;
2587 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002588 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002589 fis->features = nsect & 0xFF;
2590 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002591 fis->sect_count = ((tag << 3) | (tag >> 5));
2592 fis->sect_cnt_ex = 0;
2593 fis->control = 0;
2594 fis->res2 = 0;
2595 fis->res3 = 0;
2596 fill_command_sg(dd, command, nents);
2597
2598 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002599 command->command_header->opts =
2600 __force_bit2int cpu_to_le32(
2601 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002602 command->command_header->byte_count = 0;
2603
2604 /*
2605 * Set the completion function and data for the command
2606 * within this layer.
2607 */
2608 command->comp_data = dd;
2609 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002610 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002611
2612 /*
2613 * Set the completion function and data for the command passed
2614 * from the upper layer.
2615 */
2616 command->async_data = data;
2617 command->async_callback = callback;
2618
2619 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002620 * To prevent this command from being issued
2621 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002622 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002623 if (port->flags & MTIP_PF_PAUSE_IO) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002624 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002625 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002626 return;
2627 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002628
2629 /* Issue the command to the hardware */
2630 mtip_issue_ncq_command(port, tag);
2631
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002632 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002633}
2634
2635/*
2636 * Release a command slot.
2637 *
2638 * @dd Pointer to the driver data structure.
2639 * @tag Slot tag
2640 *
2641 * return value
2642 * None
2643 */
Jens Axboe63166682011-09-27 21:27:43 -06002644static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002645{
2646 release_slot(dd->port, tag);
2647}
2648
2649/*
2650 * Obtain a command slot and return its associated scatter list.
2651 *
2652 * @dd Pointer to the driver data structure.
2653 * @tag Pointer to an int that will receive the allocated command
2654 * slot tag.
2655 *
2656 * return value
2657 * Pointer to the scatter list for the allocated command slot
2658 * or NULL if no command slots are available.
2659 */
Jens Axboe63166682011-09-27 21:27:43 -06002660static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2661 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002662{
2663 /*
2664 * It is possible that, even with this semaphore, a thread
2665 * may think that no command slots are available. Therefore, we
2666 * need to make an attempt to get_slot().
2667 */
2668 down(&dd->port->cmd_slot);
2669 *tag = get_slot(dd->port);
2670
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002671 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002672 up(&dd->port->cmd_slot);
2673 return NULL;
2674 }
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002675 if (unlikely(*tag < 0)) {
2676 up(&dd->port->cmd_slot);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002677 return NULL;
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002678 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002679
2680 return dd->port->commands[*tag].sg;
2681}
2682
2683/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002684 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002685 *
2686 * @dev Pointer to the device structure, passed by the kernrel.
2687 * @attr Pointer to the device_attribute structure passed by the kernel.
2688 * @buf Pointer to the char buffer that will receive the stats info.
2689 *
2690 * return value
2691 * The size, in bytes, of the data copied into buf.
2692 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002693static ssize_t mtip_hw_show_status(struct device *dev,
2694 struct device_attribute *attr,
2695 char *buf)
2696{
2697 struct driver_data *dd = dev_to_disk(dev)->private_data;
2698 int size = 0;
2699
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002700 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002701 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002702 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002703 size += sprintf(buf, "%s", "write_protect\n");
2704 else
2705 size += sprintf(buf, "%s", "online\n");
2706
2707 return size;
2708}
2709
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002710static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002711
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002712static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2713 size_t len, loff_t *offset)
2714{
2715 struct driver_data *dd = (struct driver_data *)f->private_data;
2716 char buf[MTIP_DFS_MAX_BUF_SIZE];
2717 u32 group_allocated;
2718 int size = *offset;
2719 int n;
2720
2721 if (!len || size)
2722 return 0;
2723
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002724 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2725
2726 for (n = dd->slot_groups-1; n >= 0; n--)
2727 size += sprintf(&buf[size], "%08X ",
2728 readl(dd->port->s_active[n]));
2729
2730 size += sprintf(&buf[size], "]\n");
2731 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2732
2733 for (n = dd->slot_groups-1; n >= 0; n--)
2734 size += sprintf(&buf[size], "%08X ",
2735 readl(dd->port->cmd_issue[n]));
2736
2737 size += sprintf(&buf[size], "]\n");
2738 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2739
2740 for (n = dd->slot_groups-1; n >= 0; n--)
2741 size += sprintf(&buf[size], "%08X ",
2742 readl(dd->port->completed[n]));
2743
2744 size += sprintf(&buf[size], "]\n");
2745 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2746 readl(dd->port->mmio + PORT_IRQ_STAT));
2747 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2748 readl(dd->mmio + HOST_IRQ_STAT));
2749 size += sprintf(&buf[size], "\n");
2750
2751 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2752
2753 for (n = dd->slot_groups-1; n >= 0; n--) {
2754 if (sizeof(long) > sizeof(u32))
2755 group_allocated =
2756 dd->port->allocated[n/2] >> (32*(n&1));
2757 else
2758 group_allocated = dd->port->allocated[n];
2759 size += sprintf(&buf[size], "%08X ", group_allocated);
2760 }
2761 size += sprintf(&buf[size], "]\n");
2762
2763 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2764
2765 for (n = dd->slot_groups-1; n >= 0; n--) {
2766 if (sizeof(long) > sizeof(u32))
2767 group_allocated =
2768 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2769 else
2770 group_allocated = dd->port->cmds_to_issue[n];
2771 size += sprintf(&buf[size], "%08X ", group_allocated);
2772 }
2773 size += sprintf(&buf[size], "]\n");
2774
2775 *offset = size <= len ? size : len;
2776 size = copy_to_user(ubuf, buf, *offset);
2777 if (size)
2778 return -EFAULT;
2779
2780 return *offset;
2781}
2782
2783static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2784 size_t len, loff_t *offset)
2785{
2786 struct driver_data *dd = (struct driver_data *)f->private_data;
2787 char buf[MTIP_DFS_MAX_BUF_SIZE];
2788 int size = *offset;
2789
2790 if (!len || size)
2791 return 0;
2792
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002793 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2794 dd->port->flags);
2795 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2796 dd->dd_flag);
2797
2798 *offset = size <= len ? size : len;
2799 size = copy_to_user(ubuf, buf, *offset);
2800 if (size)
2801 return -EFAULT;
2802
2803 return *offset;
2804}
2805
2806static const struct file_operations mtip_regs_fops = {
2807 .owner = THIS_MODULE,
2808 .open = simple_open,
2809 .read = mtip_hw_read_registers,
2810 .llseek = no_llseek,
2811};
2812
2813static const struct file_operations mtip_flags_fops = {
2814 .owner = THIS_MODULE,
2815 .open = simple_open,
2816 .read = mtip_hw_read_flags,
2817 .llseek = no_llseek,
2818};
2819
Sam Bradshaw88523a62011-08-30 08:34:26 -06002820/*
2821 * Create the sysfs related attributes.
2822 *
2823 * @dd Pointer to the driver data structure.
2824 * @kobj Pointer to the kobj for the block device.
2825 *
2826 * return value
2827 * 0 Operation completed successfully.
2828 * -EINVAL Invalid parameter.
2829 */
Jens Axboe63166682011-09-27 21:27:43 -06002830static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002831{
2832 if (!kobj || !dd)
2833 return -EINVAL;
2834
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002835 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2836 dev_warn(&dd->pdev->dev,
2837 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002838 return 0;
2839}
2840
2841/*
2842 * Remove the sysfs related attributes.
2843 *
2844 * @dd Pointer to the driver data structure.
2845 * @kobj Pointer to the kobj for the block device.
2846 *
2847 * return value
2848 * 0 Operation completed successfully.
2849 * -EINVAL Invalid parameter.
2850 */
Jens Axboe63166682011-09-27 21:27:43 -06002851static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002852{
2853 if (!kobj || !dd)
2854 return -EINVAL;
2855
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002856 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002857
2858 return 0;
2859}
2860
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002861static int mtip_hw_debugfs_init(struct driver_data *dd)
2862{
2863 if (!dfs_parent)
2864 return -1;
2865
2866 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2867 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2868 dev_warn(&dd->pdev->dev,
2869 "Error creating node %s under debugfs\n",
2870 dd->disk->disk_name);
2871 dd->dfs_node = NULL;
2872 return -1;
2873 }
2874
2875 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2876 &mtip_flags_fops);
2877 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2878 &mtip_regs_fops);
2879
2880 return 0;
2881}
2882
2883static void mtip_hw_debugfs_exit(struct driver_data *dd)
2884{
2885 debugfs_remove_recursive(dd->dfs_node);
2886}
2887
2888
Sam Bradshaw88523a62011-08-30 08:34:26 -06002889/*
2890 * Perform any init/resume time hardware setup
2891 *
2892 * @dd Pointer to the driver data structure.
2893 *
2894 * return value
2895 * None
2896 */
2897static inline void hba_setup(struct driver_data *dd)
2898{
2899 u32 hwdata;
2900 hwdata = readl(dd->mmio + HOST_HSORG);
2901
2902 /* interrupt bug workaround: use only 1 IS bit.*/
2903 writel(hwdata |
2904 HSORG_DISABLE_SLOTGRP_INTR |
2905 HSORG_DISABLE_SLOTGRP_PXIS,
2906 dd->mmio + HOST_HSORG);
2907}
2908
2909/*
2910 * Detect the details of the product, and store anything needed
2911 * into the driver data structure. This includes product type and
2912 * version and number of slot groups.
2913 *
2914 * @dd Pointer to the driver data structure.
2915 *
2916 * return value
2917 * None
2918 */
2919static void mtip_detect_product(struct driver_data *dd)
2920{
2921 u32 hwdata;
2922 unsigned int rev, slotgroups;
2923
2924 /*
2925 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2926 * info register:
2927 * [15:8] hardware/software interface rev#
2928 * [ 3] asic-style interface
2929 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2930 */
2931 hwdata = readl(dd->mmio + HOST_HSORG);
2932
2933 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2934 dd->slot_groups = 1;
2935
2936 if (hwdata & 0x8) {
2937 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2938 rev = (hwdata & HSORG_HWREV) >> 8;
2939 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2940 dev_info(&dd->pdev->dev,
2941 "ASIC-FPGA design, HS rev 0x%x, "
2942 "%i slot groups [%i slots]\n",
2943 rev,
2944 slotgroups,
2945 slotgroups * 32);
2946
2947 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2948 dev_warn(&dd->pdev->dev,
2949 "Warning: driver only supports "
2950 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2951 slotgroups = MTIP_MAX_SLOT_GROUPS;
2952 }
2953 dd->slot_groups = slotgroups;
2954 return;
2955 }
2956
2957 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2958}
2959
2960/*
2961 * Blocking wait for FTL rebuild to complete
2962 *
2963 * @dd Pointer to the DRIVER_DATA structure.
2964 *
2965 * return value
2966 * 0 FTL rebuild completed successfully
2967 * -EFAULT FTL rebuild error/timeout/interruption
2968 */
2969static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2970{
2971 unsigned long timeout, cnt = 0, start;
2972
2973 dev_warn(&dd->pdev->dev,
2974 "FTL rebuild in progress. Polling for completion.\n");
2975
2976 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002977 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2978
2979 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002980 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002981 &dd->dd_flag)))
2982 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002983 if (mtip_check_surprise_removal(dd->pdev))
2984 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002985
Sam Bradshaw88523a62011-08-30 08:34:26 -06002986 if (mtip_get_identify(dd->port, NULL) < 0)
2987 return -EFAULT;
2988
2989 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2990 MTIP_FTL_REBUILD_MAGIC) {
2991 ssleep(1);
2992 /* Print message every 3 minutes */
2993 if (cnt++ >= 180) {
2994 dev_warn(&dd->pdev->dev,
2995 "FTL rebuild in progress (%d secs).\n",
2996 jiffies_to_msecs(jiffies - start) / 1000);
2997 cnt = 0;
2998 }
2999 } else {
3000 dev_warn(&dd->pdev->dev,
3001 "FTL rebuild complete (%d secs).\n",
3002 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003003 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02003004 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003005 }
3006 ssleep(10);
3007 } while (time_before(jiffies, timeout));
3008
3009 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02003010 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003011 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3012 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02003013 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003014}
3015
3016/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003017 * service thread to issue queued commands
3018 *
3019 * @data Pointer to the driver data structure.
3020 *
3021 * return value
3022 * 0
3023 */
3024
3025static int mtip_service_thread(void *data)
3026{
3027 struct driver_data *dd = (struct driver_data *)data;
3028 unsigned long slot, slot_start, slot_wrap;
3029 unsigned int num_cmd_slots = dd->slot_groups * 32;
3030 struct mtip_port *port = dd->port;
3031
3032 while (1) {
3033 /*
3034 * the condition is to check neither an internal command is
3035 * is in progress nor error handling is active
3036 */
3037 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003038 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003039
3040 if (kthread_should_stop())
3041 break;
3042
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003043 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02003044 &dd->dd_flag)))
3045 break;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003046
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003047 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3048 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003049 slot = 1;
3050 /* used to restrict the loop to one iteration */
3051 slot_start = num_cmd_slots;
3052 slot_wrap = 0;
3053 while (1) {
3054 slot = find_next_bit(port->cmds_to_issue,
3055 num_cmd_slots, slot);
3056 if (slot_wrap == 1) {
3057 if ((slot_start >= slot) ||
3058 (slot >= num_cmd_slots))
3059 break;
3060 }
3061 if (unlikely(slot_start == num_cmd_slots))
3062 slot_start = slot;
3063
3064 if (unlikely(slot == num_cmd_slots)) {
3065 slot = 1;
3066 slot_wrap = 1;
3067 continue;
3068 }
3069
3070 /* Issue the command to the hardware */
3071 mtip_issue_ncq_command(port, slot);
3072
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003073 clear_bit(slot, port->cmds_to_issue);
3074 }
3075
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003076 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3077 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02003078 if (!mtip_ftl_rebuild_poll(dd))
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003079 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02003080 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003081 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003082 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003083 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003084
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003085 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003086 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003087 }
3088 return 0;
3089}
3090
3091/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003092 * Called once for each card.
3093 *
3094 * @dd Pointer to the driver data structure.
3095 *
3096 * return value
3097 * 0 on success, else an error code.
3098 */
Jens Axboe63166682011-09-27 21:27:43 -06003099static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003100{
3101 int i;
3102 int rv;
3103 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003104 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003105 unsigned char *buf;
3106 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003107
3108 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3109
3110 mtip_detect_product(dd);
3111 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3112 rv = -EIO;
3113 goto out1;
3114 }
3115 num_command_slots = dd->slot_groups * 32;
3116
3117 hba_setup(dd);
3118
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003119 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3120 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003121 if (!dd->port) {
3122 dev_err(&dd->pdev->dev,
3123 "Memory allocation: port structure\n");
3124 return -ENOMEM;
3125 }
3126
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003127 /* Continue workqueue setup */
3128 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3129 dd->work[i].port = dd->port;
3130
Sam Bradshaw88523a62011-08-30 08:34:26 -06003131 /* Counting semaphore to track command slot usage */
3132 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
3133
3134 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003135 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3136 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003137
3138 /* Set the port mmio base address. */
3139 dd->port->mmio = dd->mmio + PORT_OFFSET;
3140 dd->port->dd = dd;
3141
3142 /* Allocate memory for the command list. */
3143 dd->port->command_list =
3144 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003145 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003146 &dd->port->command_list_dma,
3147 GFP_KERNEL);
3148 if (!dd->port->command_list) {
3149 dev_err(&dd->pdev->dev,
3150 "Memory allocation: command list\n");
3151 rv = -ENOMEM;
3152 goto out1;
3153 }
3154
3155 /* Clear the memory we have allocated. */
3156 memset(dd->port->command_list,
3157 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003158 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003159
3160 /* Setup the addresse of the RX FIS. */
3161 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3162 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3163
3164 /* Setup the address of the command tables. */
3165 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3166 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3167
3168 /* Setup the address of the identify data. */
3169 dd->port->identify = dd->port->command_table +
3170 HW_CMD_TBL_AR_SZ;
3171 dd->port->identify_dma = dd->port->command_tbl_dma +
3172 HW_CMD_TBL_AR_SZ;
3173
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003174 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003175 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3176 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3177
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003178 /* Setup the address of the log buf - for read log command */
3179 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3180 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3181
3182 /* Setup the address of the smart buf - for smart read data command */
3183 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3184 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3185
3186
Sam Bradshaw88523a62011-08-30 08:34:26 -06003187 /* Point the command headers at the command tables. */
3188 for (i = 0; i < num_command_slots; i++) {
3189 dd->port->commands[i].command_header =
3190 dd->port->command_list +
3191 (sizeof(struct mtip_cmd_hdr) * i);
3192 dd->port->commands[i].command_header_dma =
3193 dd->port->command_list_dma +
3194 (sizeof(struct mtip_cmd_hdr) * i);
3195
3196 dd->port->commands[i].command =
3197 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3198 dd->port->commands[i].command_dma =
3199 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3200
3201 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3202 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003203 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06003204 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003205 dd->port->commands[i].command_header->ctba =
3206 __force_bit2int cpu_to_le32(
3207 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003208
3209 /*
3210 * If this is not done, a bug is reported by the stock
3211 * FC11 i386. Due to the fact that it has lots of kernel
3212 * debugging enabled.
3213 */
3214 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3215
3216 /* Mark all commands as currently inactive.*/
3217 atomic_set(&dd->port->commands[i].active, 0);
3218 }
3219
3220 /* Setup the pointers to the extended s_active and CI registers. */
3221 for (i = 0; i < dd->slot_groups; i++) {
3222 dd->port->s_active[i] =
3223 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3224 dd->port->cmd_issue[i] =
3225 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3226 dd->port->completed[i] =
3227 dd->port->mmio + i*0x80 + PORT_SDBV;
3228 }
3229
Asai Thambi S P45038362012-04-09 08:35:38 +02003230 timetaken = jiffies;
3231 timeout = jiffies + msecs_to_jiffies(30000);
3232 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3233 time_before(jiffies, timeout)) {
3234 mdelay(100);
3235 }
3236 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3237 timetaken = jiffies - timetaken;
3238 dev_warn(&dd->pdev->dev,
3239 "Surprise removal detected at %u ms\n",
3240 jiffies_to_msecs(timetaken));
3241 rv = -ENODEV;
3242 goto out2 ;
3243 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003244 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003245 timetaken = jiffies - timetaken;
3246 dev_warn(&dd->pdev->dev,
3247 "Removal detected at %u ms\n",
3248 jiffies_to_msecs(timetaken));
3249 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003250 goto out2;
3251 }
3252
Asai Thambi S P45038362012-04-09 08:35:38 +02003253 /* Conditionally reset the HBA. */
3254 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3255 if (mtip_hba_reset(dd) < 0) {
3256 dev_err(&dd->pdev->dev,
3257 "Card did not reset within timeout\n");
3258 rv = -EIO;
3259 goto out2;
3260 }
3261 } else {
3262 /* Clear any pending interrupts on the HBA */
3263 writel(readl(dd->mmio + HOST_IRQ_STAT),
3264 dd->mmio + HOST_IRQ_STAT);
3265 }
3266
Sam Bradshaw88523a62011-08-30 08:34:26 -06003267 mtip_init_port(dd->port);
3268 mtip_start_port(dd->port);
3269
3270 /* Setup the ISR and enable interrupts. */
3271 rv = devm_request_irq(&dd->pdev->dev,
3272 dd->pdev->irq,
3273 mtip_irq_handler,
3274 IRQF_SHARED,
3275 dev_driver_string(&dd->pdev->dev),
3276 dd);
3277
3278 if (rv) {
3279 dev_err(&dd->pdev->dev,
3280 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3281 goto out2;
3282 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003283 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003284
3285 /* Enable interrupts on the HBA. */
3286 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3287 dd->mmio + HOST_CTL);
3288
3289 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003290 init_waitqueue_head(&dd->port->svc_wait);
3291
Sam Bradshaw88523a62011-08-30 08:34:26 -06003292 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3293 dd->port->cmd_timer.function = mtip_timeout_function;
3294 mod_timer(&dd->port->cmd_timer,
3295 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3296
Asai Thambi S P45038362012-04-09 08:35:38 +02003297
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003298 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003299 rv = -EFAULT;
3300 goto out3;
3301 }
3302
Sam Bradshaw88523a62011-08-30 08:34:26 -06003303 if (mtip_get_identify(dd->port, NULL) < 0) {
3304 rv = -EFAULT;
3305 goto out3;
3306 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003307
3308 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3309 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003310 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003311 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003312 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003313 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003314
3315 /* check write protect, over temp and rebuild statuses */
3316 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3317 dd->port->log_buf,
3318 dd->port->log_buf_dma, 1);
3319 if (rv) {
3320 dev_warn(&dd->pdev->dev,
3321 "Error in READ LOG EXT (10h) command\n");
3322 /* non-critical error, don't fail the load */
3323 } else {
3324 buf = (unsigned char *)dd->port->log_buf;
3325 if (buf[259] & 0x1) {
3326 dev_info(&dd->pdev->dev,
3327 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003328 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003329 }
3330 if (buf[288] == 0xF7) {
3331 dev_info(&dd->pdev->dev,
3332 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003333 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003334 }
3335 if (buf[288] == 0xBF) {
3336 dev_info(&dd->pdev->dev,
3337 "Drive indicates rebuild has failed.\n");
3338 /* TODO */
3339 }
3340 }
3341
3342 /* get write protect progess */
3343 memset(&attr242, 0, sizeof(struct smart_attr));
3344 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3345 dev_warn(&dd->pdev->dev,
3346 "Unable to check write protect progress\n");
3347 else
3348 dev_info(&dd->pdev->dev,
Asai Thambi S Pb62868e2012-09-05 22:03:32 +05303349 "Write protect progress: %u%% (%u blocks)\n",
3350 attr242.cur, le32_to_cpu(attr242.data));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003351 return rv;
3352
3353out3:
3354 del_timer_sync(&dd->port->cmd_timer);
3355
3356 /* Disable interrupts on the HBA. */
3357 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3358 dd->mmio + HOST_CTL);
3359
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003360 /* Release the IRQ. */
3361 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003362 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3363
3364out2:
3365 mtip_deinit_port(dd->port);
3366
3367 /* Free the command/command header memory. */
3368 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003369 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003370 dd->port->command_list,
3371 dd->port->command_list_dma);
3372out1:
3373 /* Free the memory allocated for the for structure. */
3374 kfree(dd->port);
3375
3376 return rv;
3377}
3378
3379/*
3380 * Called to deinitialize an interface.
3381 *
3382 * @dd Pointer to the driver data structure.
3383 *
3384 * return value
3385 * 0
3386 */
Jens Axboe63166682011-09-27 21:27:43 -06003387static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003388{
3389 /*
3390 * Send standby immediate (E0h) to the drive so that it
3391 * saves its state.
3392 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003393 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003394
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003395 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003396 if (mtip_standby_immediate(dd->port))
3397 dev_warn(&dd->pdev->dev,
3398 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003399
3400 /* de-initialize the port. */
3401 mtip_deinit_port(dd->port);
3402
3403 /* Disable interrupts on the HBA. */
3404 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3405 dd->mmio + HOST_CTL);
3406 }
3407
3408 del_timer_sync(&dd->port->cmd_timer);
3409
Sam Bradshaw88523a62011-08-30 08:34:26 -06003410 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003411 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003412 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3413
3414 /* Free the command/command header memory. */
3415 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003416 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003417 dd->port->command_list,
3418 dd->port->command_list_dma);
3419 /* Free the memory allocated for the for structure. */
3420 kfree(dd->port);
3421
3422 return 0;
3423}
3424
3425/*
3426 * Issue a Standby Immediate command to the device.
3427 *
3428 * This function is called by the Block Layer just before the
3429 * system powers off during a shutdown.
3430 *
3431 * @dd Pointer to the driver data structure.
3432 *
3433 * return value
3434 * 0
3435 */
Jens Axboe63166682011-09-27 21:27:43 -06003436static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003437{
3438 /*
3439 * Send standby immediate (E0h) to the drive so that it
3440 * saves its state.
3441 */
3442 mtip_standby_immediate(dd->port);
3443
3444 return 0;
3445}
3446
3447/*
3448 * Suspend function
3449 *
3450 * This function is called by the Block Layer just before the
3451 * system hibernates.
3452 *
3453 * @dd Pointer to the driver data structure.
3454 *
3455 * return value
3456 * 0 Suspend was successful
3457 * -EFAULT Suspend was not successful
3458 */
Jens Axboe63166682011-09-27 21:27:43 -06003459static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003460{
3461 /*
3462 * Send standby immediate (E0h) to the drive
3463 * so that it saves its state.
3464 */
3465 if (mtip_standby_immediate(dd->port) != 0) {
3466 dev_err(&dd->pdev->dev,
3467 "Failed standby-immediate command\n");
3468 return -EFAULT;
3469 }
3470
3471 /* Disable interrupts on the HBA.*/
3472 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3473 dd->mmio + HOST_CTL);
3474 mtip_deinit_port(dd->port);
3475
3476 return 0;
3477}
3478
3479/*
3480 * Resume function
3481 *
3482 * This function is called by the Block Layer as the
3483 * system resumes.
3484 *
3485 * @dd Pointer to the driver data structure.
3486 *
3487 * return value
3488 * 0 Resume was successful
3489 * -EFAULT Resume was not successful
3490 */
Jens Axboe63166682011-09-27 21:27:43 -06003491static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003492{
3493 /* Perform any needed hardware setup steps */
3494 hba_setup(dd);
3495
3496 /* Reset the HBA */
3497 if (mtip_hba_reset(dd) != 0) {
3498 dev_err(&dd->pdev->dev,
3499 "Unable to reset the HBA\n");
3500 return -EFAULT;
3501 }
3502
3503 /*
3504 * Enable the port, DMA engine, and FIS reception specific
3505 * h/w in controller.
3506 */
3507 mtip_init_port(dd->port);
3508 mtip_start_port(dd->port);
3509
3510 /* Enable interrupts on the HBA.*/
3511 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3512 dd->mmio + HOST_CTL);
3513
3514 return 0;
3515}
3516
3517/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003518 * Helper function for reusing disk name
3519 * upon hot insertion.
3520 */
3521static int rssd_disk_name_format(char *prefix,
3522 int index,
3523 char *buf,
3524 int buflen)
3525{
3526 const int base = 'z' - 'a' + 1;
3527 char *begin = buf + strlen(prefix);
3528 char *end = buf + buflen;
3529 char *p;
3530 int unit;
3531
3532 p = end - 1;
3533 *p = '\0';
3534 unit = base;
3535 do {
3536 if (p == begin)
3537 return -EINVAL;
3538 *--p = 'a' + (index % unit);
3539 index = (index / unit) - 1;
3540 } while (index >= 0);
3541
3542 memmove(begin, p, end - p);
3543 memcpy(buf, prefix, strlen(prefix));
3544
3545 return 0;
3546}
3547
3548/*
3549 * Block layer IOCTL handler.
3550 *
3551 * @dev Pointer to the block_device structure.
3552 * @mode ignored
3553 * @cmd IOCTL command passed from the user application.
3554 * @arg Argument passed from the user application.
3555 *
3556 * return value
3557 * 0 IOCTL completed successfully.
3558 * -ENOTTY IOCTL not supported or invalid driver data
3559 * structure pointer.
3560 */
3561static int mtip_block_ioctl(struct block_device *dev,
3562 fmode_t mode,
3563 unsigned cmd,
3564 unsigned long arg)
3565{
3566 struct driver_data *dd = dev->bd_disk->private_data;
3567
3568 if (!capable(CAP_SYS_ADMIN))
3569 return -EACCES;
3570
3571 if (!dd)
3572 return -ENOTTY;
3573
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003574 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003575 return -ENOTTY;
3576
Sam Bradshaw88523a62011-08-30 08:34:26 -06003577 switch (cmd) {
3578 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003579 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003580 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003581 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003582 }
3583}
3584
Jens Axboe16d02c02011-09-27 15:50:01 -06003585#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003586/*
3587 * Block layer compat IOCTL handler.
3588 *
3589 * @dev Pointer to the block_device structure.
3590 * @mode ignored
3591 * @cmd IOCTL command passed from the user application.
3592 * @arg Argument passed from the user application.
3593 *
3594 * return value
3595 * 0 IOCTL completed successfully.
3596 * -ENOTTY IOCTL not supported or invalid driver data
3597 * structure pointer.
3598 */
3599static int mtip_block_compat_ioctl(struct block_device *dev,
3600 fmode_t mode,
3601 unsigned cmd,
3602 unsigned long arg)
3603{
3604 struct driver_data *dd = dev->bd_disk->private_data;
3605
3606 if (!capable(CAP_SYS_ADMIN))
3607 return -EACCES;
3608
3609 if (!dd)
3610 return -ENOTTY;
3611
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003612 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003613 return -ENOTTY;
3614
Sam Bradshaw88523a62011-08-30 08:34:26 -06003615 switch (cmd) {
3616 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003617 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003618 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003619 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003620 ide_task_request_t req_task;
3621 int compat_tasksize, outtotal, ret;
3622
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003623 compat_tasksize =
3624 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003625
3626 compat_req_task =
3627 (struct mtip_compat_ide_task_request_s __user *) arg;
3628
3629 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003630 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003631 return -EFAULT;
3632
3633 if (get_user(req_task.out_size, &compat_req_task->out_size))
3634 return -EFAULT;
3635
3636 if (get_user(req_task.in_size, &compat_req_task->in_size))
3637 return -EFAULT;
3638
3639 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3640
3641 ret = exec_drive_taskfile(dd, (void __user *) arg,
3642 &req_task, outtotal);
3643
3644 if (copy_to_user((void __user *) arg, &req_task,
3645 compat_tasksize -
3646 (2 * sizeof(compat_long_t))))
3647 return -EFAULT;
3648
3649 if (put_user(req_task.out_size, &compat_req_task->out_size))
3650 return -EFAULT;
3651
3652 if (put_user(req_task.in_size, &compat_req_task->in_size))
3653 return -EFAULT;
3654
3655 return ret;
3656 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003657 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003658 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003659 }
3660}
Jens Axboe16d02c02011-09-27 15:50:01 -06003661#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003662
3663/*
3664 * Obtain the geometry of the device.
3665 *
3666 * You may think that this function is obsolete, but some applications,
3667 * fdisk for example still used CHS values. This function describes the
3668 * device as having 224 heads and 56 sectors per cylinder. These values are
3669 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3670 * partition is described in terms of a start and end cylinder this means
3671 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3672 * affects performance.
3673 *
3674 * @dev Pointer to the block_device strucutre.
3675 * @geo Pointer to a hd_geometry structure.
3676 *
3677 * return value
3678 * 0 Operation completed successfully.
3679 * -ENOTTY An error occurred while reading the drive capacity.
3680 */
3681static int mtip_block_getgeo(struct block_device *dev,
3682 struct hd_geometry *geo)
3683{
3684 struct driver_data *dd = dev->bd_disk->private_data;
3685 sector_t capacity;
3686
3687 if (!dd)
3688 return -ENOTTY;
3689
3690 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3691 dev_warn(&dd->pdev->dev,
3692 "Could not get drive capacity.\n");
3693 return -ENOTTY;
3694 }
3695
3696 geo->heads = 224;
3697 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003698 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003699 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003700 return 0;
3701}
3702
3703/*
3704 * Block device operation function.
3705 *
3706 * This structure contains pointers to the functions required by the block
3707 * layer.
3708 */
3709static const struct block_device_operations mtip_block_ops = {
3710 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003711#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003712 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003713#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003714 .getgeo = mtip_block_getgeo,
3715 .owner = THIS_MODULE
3716};
3717
3718/*
3719 * Block layer make request function.
3720 *
3721 * This function is called by the kernel to process a BIO for
3722 * the P320 device.
3723 *
3724 * @queue Pointer to the request queue. Unused other than to obtain
3725 * the driver data structure.
3726 * @bio Pointer to the BIO.
3727 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003728 */
Jens Axboea71f4832011-11-05 08:36:21 +01003729static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003730{
3731 struct driver_data *dd = queue->queuedata;
3732 struct scatterlist *sg;
3733 struct bio_vec *bvec;
3734 int nents = 0;
3735 int tag = 0;
3736
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003737 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3738 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3739 &dd->dd_flag))) {
3740 bio_endio(bio, -ENXIO);
3741 return;
3742 }
3743 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3744 bio_endio(bio, -ENODATA);
3745 return;
3746 }
3747 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3748 &dd->dd_flag) &&
3749 bio_data_dir(bio))) {
3750 bio_endio(bio, -ENODATA);
3751 return;
3752 }
Asai Thambi S P12a166c2012-09-05 22:01:36 +05303753 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3754 bio_endio(bio, -ENODATA);
3755 return;
3756 }
Asai Thambi S P45038362012-04-09 08:35:38 +02003757 }
3758
Asai Thambi S P15283462013-01-11 14:41:34 +01003759 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
3760 bio_endio(bio, mtip_send_trim(dd, bio->bi_sector,
3761 bio_sectors(bio)));
3762 return;
3763 }
3764
Sam Bradshaw88523a62011-08-30 08:34:26 -06003765 if (unlikely(!bio_has_data(bio))) {
3766 blk_queue_flush(queue, 0);
3767 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003768 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003769 }
3770
Sam Bradshaw88523a62011-08-30 08:34:26 -06003771 sg = mtip_hw_get_scatterlist(dd, &tag);
3772 if (likely(sg != NULL)) {
3773 blk_queue_bounce(queue, &bio);
3774
3775 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3776 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003777 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003778 bio_io_error(bio);
3779 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003780 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003781 }
3782
3783 /* Create the scatter list for this bio. */
3784 bio_for_each_segment(bvec, bio, nents) {
3785 sg_set_page(&sg[nents],
3786 bvec->bv_page,
3787 bvec->bv_len,
3788 bvec->bv_offset);
3789 }
3790
3791 /* Issue the read/write. */
3792 mtip_hw_submit_io(dd,
3793 bio->bi_sector,
3794 bio_sectors(bio),
3795 nents,
3796 tag,
3797 bio_endio,
3798 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003799 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003800 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003801 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003802}
3803
3804/*
3805 * Block layer initialization function.
3806 *
3807 * This function is called once by the PCI layer for each P320
3808 * device that is connected to the system.
3809 *
3810 * @dd Pointer to the driver data structure.
3811 *
3812 * return value
3813 * 0 on success else an error code.
3814 */
Jens Axboe63166682011-09-27 21:27:43 -06003815static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003816{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003817 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003818 sector_t capacity;
3819 unsigned int index = 0;
3820 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003821 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003822
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003823 if (dd->disk)
3824 goto skip_create_disk; /* hw init done, before rebuild */
3825
Sam Bradshaw88523a62011-08-30 08:34:26 -06003826 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003827 wait_for_rebuild = mtip_hw_init(dd);
3828 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003829 dev_err(&dd->pdev->dev,
3830 "Protocol layer initialization failed\n");
3831 rv = -EINVAL;
3832 goto protocol_init_error;
3833 }
3834
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003835 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003836 if (dd->disk == NULL) {
3837 dev_err(&dd->pdev->dev,
3838 "Unable to allocate gendisk structure\n");
3839 rv = -EINVAL;
3840 goto alloc_disk_error;
3841 }
3842
3843 /* Generate the disk name, implemented same as in sd.c */
3844 do {
3845 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3846 goto ida_get_error;
3847
3848 spin_lock(&rssd_index_lock);
3849 rv = ida_get_new(&rssd_index_ida, &index);
3850 spin_unlock(&rssd_index_lock);
3851 } while (rv == -EAGAIN);
3852
3853 if (rv)
3854 goto ida_get_error;
3855
3856 rv = rssd_disk_name_format("rssd",
3857 index,
3858 dd->disk->disk_name,
3859 DISK_NAME_LEN);
3860 if (rv)
3861 goto disk_index_error;
3862
3863 dd->disk->driverfs_dev = &dd->pdev->dev;
3864 dd->disk->major = dd->major;
3865 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3866 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003867 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003868 dd->index = index;
3869
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003870 /*
3871 * if rebuild pending, start the service thread, and delay the block
3872 * queue creation and add_disk()
3873 */
3874 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3875 goto start_service_thread;
3876
3877skip_create_disk:
3878 /* Allocate the request queue. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003879 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003880 if (dd->queue == NULL) {
3881 dev_err(&dd->pdev->dev,
3882 "Unable to allocate request queue\n");
3883 rv = -ENOMEM;
3884 goto block_queue_alloc_init_error;
3885 }
3886
3887 /* Attach our request function to the request queue. */
3888 blk_queue_make_request(dd->queue, mtip_make_request);
3889
3890 dd->disk->queue = dd->queue;
3891 dd->queue->queuedata = dd;
3892
3893 /* Set device limits. */
3894 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3895 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3896 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003897 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3898 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003899 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003900
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003901 /*
3902 * write back cache is not supported in the device. FUA depends on
3903 * write back cache support, hence setting flush support to zero.
3904 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003905 blk_queue_flush(dd->queue, 0);
3906
Asai Thambi S P15283462013-01-11 14:41:34 +01003907 /* Signal trim support */
3908 if (dd->trim_supp == true) {
3909 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3910 dd->queue->limits.discard_granularity = 4096;
3911 blk_queue_max_discard_sectors(dd->queue,
3912 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3913 dd->queue->limits.discard_zeroes_data = 0;
3914 }
3915
Sam Bradshaw88523a62011-08-30 08:34:26 -06003916 /* Set the capacity of the device in 512 byte sectors. */
3917 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3918 dev_warn(&dd->pdev->dev,
3919 "Could not read drive capacity\n");
3920 rv = -EIO;
3921 goto read_capacity_error;
3922 }
3923 set_capacity(dd->disk, capacity);
3924
3925 /* Enable the block device and add it to /dev */
3926 add_disk(dd->disk);
3927
3928 /*
3929 * Now that the disk is active, initialize any sysfs attributes
3930 * managed by the protocol layer.
3931 */
3932 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3933 if (kobj) {
3934 mtip_hw_sysfs_init(dd, kobj);
3935 kobject_put(kobj);
3936 }
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003937 mtip_hw_debugfs_init(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003938
Asai Thambi S P45038362012-04-09 08:35:38 +02003939 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003940 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003941 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003942 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003943
3944start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003945 sprintf(thd_name, "mtip_svc_thd_%02d", index);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003946 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
3947 dd, dd->numa_node, thd_name);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003948
3949 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003950 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003951 dd->mtip_svc_handler = NULL;
3952 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003953 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003954 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003955 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003956 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3957 rv = wait_for_rebuild;
3958
Sam Bradshaw88523a62011-08-30 08:34:26 -06003959 return rv;
3960
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003961kthread_run_error:
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003962 mtip_hw_debugfs_exit(dd);
3963
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003964 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003965 del_gendisk(dd->disk);
3966
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003967read_capacity_error:
3968 blk_cleanup_queue(dd->queue);
3969
3970block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003971disk_index_error:
3972 spin_lock(&rssd_index_lock);
3973 ida_remove(&rssd_index_ida, index);
3974 spin_unlock(&rssd_index_lock);
3975
3976ida_get_error:
3977 put_disk(dd->disk);
3978
3979alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003980 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003981
3982protocol_init_error:
3983 return rv;
3984}
3985
3986/*
3987 * Block layer deinitialization function.
3988 *
3989 * Called by the PCI layer as each P320 device is removed.
3990 *
3991 * @dd Pointer to the driver data structure.
3992 *
3993 * return value
3994 * 0
3995 */
Jens Axboe63166682011-09-27 21:27:43 -06003996static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003997{
3998 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003999
4000 if (dd->mtip_svc_handler) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004001 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004002 wake_up_interruptible(&dd->port->svc_wait);
4003 kthread_stop(dd->mtip_svc_handler);
4004 }
4005
Asai Thambi S Pf6587212012-04-09 08:35:38 +02004006 /* Clean up the sysfs attributes, if created */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004007 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02004008 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4009 if (kobj) {
4010 mtip_hw_sysfs_exit(dd, kobj);
4011 kobject_put(kobj);
4012 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004013 }
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004014 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004015
4016 /*
4017 * Delete our gendisk structure. This also removes the device
4018 * from /dev
4019 */
4020 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02004021
4022 spin_lock(&rssd_index_lock);
4023 ida_remove(&rssd_index_ida, dd->index);
4024 spin_unlock(&rssd_index_lock);
4025
Sam Bradshaw88523a62011-08-30 08:34:26 -06004026 blk_cleanup_queue(dd->queue);
4027 dd->disk = NULL;
4028 dd->queue = NULL;
4029
4030 /* De-initialize the protocol layer. */
4031 mtip_hw_exit(dd);
4032
4033 return 0;
4034}
4035
4036/*
4037 * Function called by the PCI layer when just before the
4038 * machine shuts down.
4039 *
4040 * If a protocol layer shutdown function is present it will be called
4041 * by this function.
4042 *
4043 * @dd Pointer to the driver data structure.
4044 *
4045 * return value
4046 * 0
4047 */
Jens Axboe63166682011-09-27 21:27:43 -06004048static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004049{
4050 dev_info(&dd->pdev->dev,
4051 "Shutting down %s ...\n", dd->disk->disk_name);
4052
4053 /* Delete our gendisk structure, and cleanup the blk queue. */
4054 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02004055
4056 spin_lock(&rssd_index_lock);
4057 ida_remove(&rssd_index_ida, dd->index);
4058 spin_unlock(&rssd_index_lock);
4059
Sam Bradshaw88523a62011-08-30 08:34:26 -06004060 blk_cleanup_queue(dd->queue);
4061 dd->disk = NULL;
4062 dd->queue = NULL;
4063
4064 mtip_hw_shutdown(dd);
4065 return 0;
4066}
4067
Jens Axboe63166682011-09-27 21:27:43 -06004068static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004069{
4070 dev_info(&dd->pdev->dev,
4071 "Suspending %s ...\n", dd->disk->disk_name);
4072 mtip_hw_suspend(dd);
4073 return 0;
4074}
4075
Jens Axboe63166682011-09-27 21:27:43 -06004076static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004077{
4078 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4079 dd->disk->disk_name);
4080 mtip_hw_resume(dd);
4081 return 0;
4082}
4083
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004084static void drop_cpu(int cpu)
4085{
4086 cpu_use[cpu]--;
4087}
4088
4089static int get_least_used_cpu_on_node(int node)
4090{
4091 int cpu, least_used_cpu, least_cnt;
4092 const struct cpumask *node_mask;
4093
4094 node_mask = cpumask_of_node(node);
4095 least_used_cpu = cpumask_first(node_mask);
4096 least_cnt = cpu_use[least_used_cpu];
4097 cpu = least_used_cpu;
4098
4099 for_each_cpu(cpu, node_mask) {
4100 if (cpu_use[cpu] < least_cnt) {
4101 least_used_cpu = cpu;
4102 least_cnt = cpu_use[cpu];
4103 }
4104 }
4105 cpu_use[least_used_cpu]++;
4106 return least_used_cpu;
4107}
4108
4109/* Helper for selecting a node in round robin mode */
4110static inline int mtip_get_next_rr_node(void)
4111{
4112 static int next_node = -1;
4113
4114 if (next_node == -1) {
4115 next_node = first_online_node;
4116 return next_node;
4117 }
4118
4119 next_node = next_online_node(next_node);
4120 if (next_node == MAX_NUMNODES)
4121 next_node = first_online_node;
4122 return next_node;
4123}
4124
Fengguang Wu25bac122013-01-12 15:31:40 +08004125static DEFINE_HANDLER(0);
4126static DEFINE_HANDLER(1);
4127static DEFINE_HANDLER(2);
4128static DEFINE_HANDLER(3);
4129static DEFINE_HANDLER(4);
4130static DEFINE_HANDLER(5);
4131static DEFINE_HANDLER(6);
4132static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004133
Sam Bradshaw88523a62011-08-30 08:34:26 -06004134/*
4135 * Called for each supported PCI device detected.
4136 *
4137 * This function allocates the private data structure, enables the
4138 * PCI device and then calls the block layer initialization function.
4139 *
4140 * return value
4141 * 0 on success else an error code.
4142 */
4143static int mtip_pci_probe(struct pci_dev *pdev,
4144 const struct pci_device_id *ent)
4145{
4146 int rv = 0;
4147 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004148 char cpu_list[256];
4149 const struct cpumask *node_mask;
4150 int cpu, i = 0, j = 0;
4151 int my_node = NUMA_NO_NODE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004152
4153 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004154 my_node = pcibus_to_node(pdev->bus);
4155 if (my_node != NUMA_NO_NODE) {
4156 if (!node_online(my_node))
4157 my_node = mtip_get_next_rr_node();
4158 } else {
4159 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4160 my_node = mtip_get_next_rr_node();
4161 }
4162 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4163 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4164 cpu_to_node(smp_processor_id()), smp_processor_id());
4165
4166 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004167 if (dd == NULL) {
4168 dev_err(&pdev->dev,
4169 "Unable to allocate memory for driver data\n");
4170 return -ENOMEM;
4171 }
4172
Sam Bradshaw88523a62011-08-30 08:34:26 -06004173 /* Attach the private data to this PCI device. */
4174 pci_set_drvdata(pdev, dd);
4175
4176 rv = pcim_enable_device(pdev);
4177 if (rv < 0) {
4178 dev_err(&pdev->dev, "Unable to enable device\n");
4179 goto iomap_err;
4180 }
4181
4182 /* Map BAR5 to memory. */
4183 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4184 if (rv < 0) {
4185 dev_err(&pdev->dev, "Unable to map regions\n");
4186 goto iomap_err;
4187 }
4188
4189 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4190 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4191
4192 if (rv) {
4193 rv = pci_set_consistent_dma_mask(pdev,
4194 DMA_BIT_MASK(32));
4195 if (rv) {
4196 dev_warn(&pdev->dev,
4197 "64-bit DMA enable failed\n");
4198 goto setmask_err;
4199 }
4200 }
4201 }
4202
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004203 /* Copy the info we may need later into the private data structure. */
4204 dd->major = mtip_major;
4205 dd->instance = instance;
4206 dd->pdev = pdev;
4207 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004208
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004209 memset(dd->workq_name, 0, 32);
4210 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4211
4212 dd->isr_workq = create_workqueue(dd->workq_name);
4213 if (!dd->isr_workq) {
4214 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4215 goto block_initialize_err;
4216 }
4217
4218 memset(cpu_list, 0, sizeof(cpu_list));
4219
4220 node_mask = cpumask_of_node(dd->numa_node);
4221 if (!cpumask_empty(node_mask)) {
4222 for_each_cpu(cpu, node_mask)
4223 {
4224 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4225 j = strlen(cpu_list);
4226 }
4227
4228 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4229 dd->numa_node,
4230 topology_physical_package_id(cpumask_first(node_mask)),
4231 nr_cpus_node(dd->numa_node),
4232 cpu_list);
4233 } else
4234 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4235
4236 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4237 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4238 cpu_to_node(dd->isr_binding), dd->isr_binding);
4239
4240 /* first worker context always runs in ISR */
4241 dd->work[0].cpu_binding = dd->isr_binding;
4242 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4243 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4244 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4245 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4246 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4247 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4248 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4249
4250 /* Log the bindings */
4251 for_each_present_cpu(cpu) {
4252 memset(cpu_list, 0, sizeof(cpu_list));
4253 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4254 if (dd->work[i].cpu_binding == cpu) {
4255 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4256 j = strlen(cpu_list);
4257 }
4258 }
4259 if (j)
4260 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4261 }
4262
4263 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4264 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4265 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4266 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4267 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4268 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4269 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4270 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4271
4272 pci_set_master(pdev);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004273 if (pci_enable_msi(pdev)) {
4274 dev_warn(&pdev->dev,
4275 "Unable to enable MSI interrupt.\n");
4276 goto block_initialize_err;
4277 }
4278
Sam Bradshaw88523a62011-08-30 08:34:26 -06004279 /* Initialize the block layer. */
4280 rv = mtip_block_initialize(dd);
4281 if (rv < 0) {
4282 dev_err(&pdev->dev,
4283 "Unable to initialize block layer\n");
4284 goto block_initialize_err;
4285 }
4286
4287 /*
4288 * Increment the instance count so that each device has a unique
4289 * instance number.
4290 */
4291 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004292 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004293 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004294 goto done;
4295
4296block_initialize_err:
4297 pci_disable_msi(pdev);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004298 if (dd->isr_workq) {
4299 flush_workqueue(dd->isr_workq);
4300 destroy_workqueue(dd->isr_workq);
4301 drop_cpu(dd->work[0].cpu_binding);
4302 drop_cpu(dd->work[1].cpu_binding);
4303 drop_cpu(dd->work[2].cpu_binding);
4304 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004305setmask_err:
4306 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4307
4308iomap_err:
4309 kfree(dd);
4310 pci_set_drvdata(pdev, NULL);
4311 return rv;
4312done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004313 return rv;
4314}
4315
4316/*
4317 * Called for each probed device when the device is removed or the
4318 * driver is unloaded.
4319 *
4320 * return value
4321 * None
4322 */
4323static void mtip_pci_remove(struct pci_dev *pdev)
4324{
4325 struct driver_data *dd = pci_get_drvdata(pdev);
4326 int counter = 0;
4327
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004328 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004329
Sam Bradshaw88523a62011-08-30 08:34:26 -06004330 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004331 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004332 counter++;
4333 msleep(20);
4334 if (counter == 10) {
4335 /* Cleanup the outstanding commands */
4336 mtip_command_cleanup(dd);
4337 break;
4338 }
4339 }
4340 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004341
4342 /* Clean up the block layer. */
4343 mtip_block_remove(dd);
4344
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004345 if (dd->isr_workq) {
4346 flush_workqueue(dd->isr_workq);
4347 destroy_workqueue(dd->isr_workq);
4348 drop_cpu(dd->work[0].cpu_binding);
4349 drop_cpu(dd->work[1].cpu_binding);
4350 drop_cpu(dd->work[2].cpu_binding);
4351 }
4352
Sam Bradshaw88523a62011-08-30 08:34:26 -06004353 pci_disable_msi(pdev);
4354
4355 kfree(dd);
4356 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4357}
4358
4359/*
4360 * Called for each probed device when the device is suspended.
4361 *
4362 * return value
4363 * 0 Success
4364 * <0 Error
4365 */
4366static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4367{
4368 int rv = 0;
4369 struct driver_data *dd = pci_get_drvdata(pdev);
4370
4371 if (!dd) {
4372 dev_err(&pdev->dev,
4373 "Driver private datastructure is NULL\n");
4374 return -EFAULT;
4375 }
4376
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004377 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004378
4379 /* Disable ports & interrupts then send standby immediate */
4380 rv = mtip_block_suspend(dd);
4381 if (rv < 0) {
4382 dev_err(&pdev->dev,
4383 "Failed to suspend controller\n");
4384 return rv;
4385 }
4386
4387 /*
4388 * Save the pci config space to pdev structure &
4389 * disable the device
4390 */
4391 pci_save_state(pdev);
4392 pci_disable_device(pdev);
4393
4394 /* Move to Low power state*/
4395 pci_set_power_state(pdev, PCI_D3hot);
4396
4397 return rv;
4398}
4399
4400/*
4401 * Called for each probed device when the device is resumed.
4402 *
4403 * return value
4404 * 0 Success
4405 * <0 Error
4406 */
4407static int mtip_pci_resume(struct pci_dev *pdev)
4408{
4409 int rv = 0;
4410 struct driver_data *dd;
4411
4412 dd = pci_get_drvdata(pdev);
4413 if (!dd) {
4414 dev_err(&pdev->dev,
4415 "Driver private datastructure is NULL\n");
4416 return -EFAULT;
4417 }
4418
4419 /* Move the device to active State */
4420 pci_set_power_state(pdev, PCI_D0);
4421
4422 /* Restore PCI configuration space */
4423 pci_restore_state(pdev);
4424
4425 /* Enable the PCI device*/
4426 rv = pcim_enable_device(pdev);
4427 if (rv < 0) {
4428 dev_err(&pdev->dev,
4429 "Failed to enable card during resume\n");
4430 goto err;
4431 }
4432 pci_set_master(pdev);
4433
4434 /*
4435 * Calls hbaReset, initPort, & startPort function
4436 * then enables interrupts
4437 */
4438 rv = mtip_block_resume(dd);
4439 if (rv < 0)
4440 dev_err(&pdev->dev, "Unable to resume\n");
4441
4442err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004443 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004444
4445 return rv;
4446}
4447
4448/*
4449 * Shutdown routine
4450 *
4451 * return value
4452 * None
4453 */
4454static void mtip_pci_shutdown(struct pci_dev *pdev)
4455{
4456 struct driver_data *dd = pci_get_drvdata(pdev);
4457 if (dd)
4458 mtip_block_shutdown(dd);
4459}
4460
Sam Bradshaw88523a62011-08-30 08:34:26 -06004461/* Table of device ids supported by this driver. */
4462static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304463 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4464 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4465 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4466 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4467 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4468 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4469 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004470 { 0 }
4471};
4472
4473/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004474static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004475 .name = MTIP_DRV_NAME,
4476 .id_table = mtip_pci_tbl,
4477 .probe = mtip_pci_probe,
4478 .remove = mtip_pci_remove,
4479 .suspend = mtip_pci_suspend,
4480 .resume = mtip_pci_resume,
4481 .shutdown = mtip_pci_shutdown,
4482};
4483
4484MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4485
4486/*
4487 * Module initialization function.
4488 *
4489 * Called once when the module is loaded. This function allocates a major
4490 * block device number to the Cyclone devices and registers the PCI layer
4491 * of the driver.
4492 *
4493 * Return value
4494 * 0 on success else error code.
4495 */
4496static int __init mtip_init(void)
4497{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004498 int error;
4499
Asai Thambi S P45422e72012-09-05 22:03:56 +05304500 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004501
4502 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004503 error = register_blkdev(0, MTIP_DRV_NAME);
4504 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304505 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004506 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004507 return -EBUSY;
4508 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004509 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004510
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004511 if (!dfs_parent) {
4512 dfs_parent = debugfs_create_dir("rssd", NULL);
4513 if (IS_ERR_OR_NULL(dfs_parent)) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304514 pr_warn("Error creating debugfs parent\n");
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004515 dfs_parent = NULL;
4516 }
4517 }
4518
Sam Bradshaw88523a62011-08-30 08:34:26 -06004519 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004520 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004521 if (error) {
4522 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004523 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004524 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004525
4526 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004527}
4528
4529/*
4530 * Module de-initialization function.
4531 *
4532 * Called once when the module is unloaded. This function deallocates
4533 * the major block device number allocated by mtip_init() and
4534 * unregisters the PCI layer of the driver.
4535 *
4536 * Return value
4537 * none
4538 */
4539static void __exit mtip_exit(void)
4540{
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004541 debugfs_remove_recursive(dfs_parent);
4542
Sam Bradshaw88523a62011-08-30 08:34:26 -06004543 /* Release the allocated major block device number. */
4544 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4545
4546 /* Unregister the PCI driver. */
4547 pci_unregister_driver(&mtip_pci_driver);
4548}
4549
4550MODULE_AUTHOR("Micron Technology, Inc");
4551MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4552MODULE_LICENSE("GPL");
4553MODULE_VERSION(MTIP_DRV_VERSION);
4554
4555module_init(mtip_init);
4556module_exit(mtip_exit);