blob: cf557a457cc03faf319f451b2f7a031e06929cc9 [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
91static DEFINE_SPINLOCK(rssd_index_lock);
92static DEFINE_IDA(rssd_index_ida);
93
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010094static int mtip_block_initialize(struct driver_data *dd);
95
Jens Axboe16d02c02011-09-27 15:50:01 -060096#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060097struct mtip_compat_ide_task_request_s {
98 __u8 io_ports[8];
99 __u8 hob_ports[8];
100 ide_reg_valid_t out_flags;
101 ide_reg_valid_t in_flags;
102 int data_phase;
103 int req_cmd;
104 compat_ulong_t out_size;
105 compat_ulong_t in_size;
106};
Jens Axboe16d02c02011-09-27 15:50:01 -0600107#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600108
Sam Bradshaw88523a62011-08-30 08:34:26 -0600109/*
Jens Axboe63166682011-09-27 21:27:43 -0600110 * This function check_for_surprise_removal is called
111 * while card is removed from the system and it will
112 * read the vendor id from the configration space
113 *
114 * @pdev Pointer to the pci_dev structure.
115 *
116 * return value
117 * true if device removed, else false
118 */
119static bool mtip_check_surprise_removal(struct pci_dev *pdev)
120{
121 u16 vendor_id = 0;
122
123 /* Read the vendorID from the configuration space */
124 pci_read_config_word(pdev, 0x00, &vendor_id);
125 if (vendor_id == 0xFFFF)
126 return true; /* device removed */
127
128 return false; /* device present */
129}
130
131/*
132 * This function is called for clean the pending command in the
133 * command slot during the surprise removal of device and return
134 * error to the upper layer.
135 *
136 * @dd Pointer to the DRIVER_DATA structure.
137 *
138 * return value
139 * None
140 */
141static void mtip_command_cleanup(struct driver_data *dd)
142{
143 int group = 0, commandslot = 0, commandindex = 0;
144 struct mtip_cmd *command;
145 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200146 static int in_progress;
147
148 if (in_progress)
149 return;
150
151 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600152
153 for (group = 0; group < 4; group++) {
154 for (commandslot = 0; commandslot < 32; commandslot++) {
155 if (!(port->allocated[group] & (1 << commandslot)))
156 continue;
157
158 commandindex = group << 5 | commandslot;
159 command = &port->commands[commandindex];
160
161 if (atomic_read(&command->active)
162 && (command->async_callback)) {
163 command->async_callback(command->async_data,
164 -ENODEV);
165 command->async_callback = NULL;
166 command->async_data = NULL;
167 }
168
169 dma_unmap_sg(&port->dd->pdev->dev,
170 command->sg,
171 command->scatter_ents,
172 command->direction);
173 }
174 }
175
176 up(&port->cmd_slot);
177
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200178 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +0200179 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600180}
181
182/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600183 * Obtain an empty command slot.
184 *
185 * This function needs to be reentrant since it could be called
186 * at the same time on multiple CPUs. The allocation of the
187 * command slot must be atomic.
188 *
189 * @port Pointer to the port data structure.
190 *
191 * return value
192 * >= 0 Index of command slot obtained.
193 * -1 No command slots available.
194 */
195static int get_slot(struct mtip_port *port)
196{
197 int slot, i;
198 unsigned int num_command_slots = port->dd->slot_groups * 32;
199
200 /*
201 * Try 10 times, because there is a small race here.
202 * that's ok, because it's still cheaper than a lock.
203 *
204 * Race: Since this section is not protected by lock, same bit
205 * could be chosen by different process contexts running in
206 * different processor. So instead of costly lock, we are going
207 * with loop.
208 */
209 for (i = 0; i < 10; i++) {
210 slot = find_next_zero_bit(port->allocated,
211 num_command_slots, 1);
212 if ((slot < num_command_slots) &&
213 (!test_and_set_bit(slot, port->allocated)))
214 return slot;
215 }
216 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
217
218 if (mtip_check_surprise_removal(port->dd->pdev)) {
219 /* Device not present, clean outstanding commands */
220 mtip_command_cleanup(port->dd);
221 }
222 return -1;
223}
224
225/*
226 * Release a command slot.
227 *
228 * @port Pointer to the port data structure.
229 * @tag Tag of command to release
230 *
231 * return value
232 * None
233 */
234static inline void release_slot(struct mtip_port *port, int tag)
235{
236 smp_mb__before_clear_bit();
237 clear_bit(tag, port->allocated);
238 smp_mb__after_clear_bit();
239}
240
241/*
Jens Axboe63166682011-09-27 21:27:43 -0600242 * Reset the HBA (without sleeping)
243 *
244 * Just like hba_reset, except does not call sleep, so can be
245 * run from interrupt/tasklet context.
246 *
247 * @dd Pointer to the driver data structure.
248 *
249 * return value
250 * 0 The reset was successful.
251 * -1 The HBA Reset bit did not clear.
252 */
253static int hba_reset_nosleep(struct driver_data *dd)
254{
255 unsigned long timeout;
256
257 /* Chip quirk: quiesce any chip function */
258 mdelay(10);
259
260 /* Set the reset bit */
261 writel(HOST_RESET, dd->mmio + HOST_CTL);
262
263 /* Flush */
264 readl(dd->mmio + HOST_CTL);
265
266 /*
267 * Wait 10ms then spin for up to 1 second
268 * waiting for reset acknowledgement
269 */
270 timeout = jiffies + msecs_to_jiffies(1000);
271 mdelay(10);
272 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
273 && time_before(jiffies, timeout))
274 mdelay(1);
275
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200276 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200277 return -1;
278
Jens Axboe63166682011-09-27 21:27:43 -0600279 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
280 return -1;
281
282 return 0;
283}
284
285/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600286 * Issue a command to the hardware.
287 *
288 * Set the appropriate bit in the s_active and Command Issue hardware
289 * registers, causing hardware command processing to begin.
290 *
291 * @port Pointer to the port structure.
292 * @tag The tag of the command to be issued.
293 *
294 * return value
295 * None
296 */
297static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
298{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600299 atomic_set(&port->commands[tag].active, 1);
300
Asai Thambi S P0a07ab22012-05-29 18:43:16 -0700301 spin_lock(&port->cmd_issue_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600302
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
307
Asai Thambi S P0a07ab22012-05-29 18:43:16 -0700308 spin_unlock(&port->cmd_issue_lock);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200309
310 /* Set the command's timeout value.*/
311 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
312 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600313}
314
315/*
Jens Axboe63166682011-09-27 21:27:43 -0600316 * Enable/disable the reception of FIS
317 *
318 * @port Pointer to the port data structure
319 * @enable 1 to enable, 0 to disable
320 *
321 * return value
322 * Previous state: 1 enabled, 0 disabled
323 */
324static int mtip_enable_fis(struct mtip_port *port, int enable)
325{
326 u32 tmp;
327
328 /* enable FIS reception */
329 tmp = readl(port->mmio + PORT_CMD);
330 if (enable)
331 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332 else
333 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
334
335 /* Flush */
336 readl(port->mmio + PORT_CMD);
337
338 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
339}
340
341/*
342 * Enable/disable the DMA engine
343 *
344 * @port Pointer to the port data structure
345 * @enable 1 to enable, 0 to disable
346 *
347 * return value
348 * Previous state: 1 enabled, 0 disabled.
349 */
350static int mtip_enable_engine(struct mtip_port *port, int enable)
351{
352 u32 tmp;
353
354 /* enable FIS reception */
355 tmp = readl(port->mmio + PORT_CMD);
356 if (enable)
357 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
358 else
359 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
360
361 readl(port->mmio + PORT_CMD);
362 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
363}
364
365/*
366 * Enables the port DMA engine and FIS reception.
367 *
368 * return value
369 * None
370 */
371static inline void mtip_start_port(struct mtip_port *port)
372{
373 /* Enable FIS reception */
374 mtip_enable_fis(port, 1);
375
376 /* Enable the DMA engine */
377 mtip_enable_engine(port, 1);
378}
379
380/*
381 * Deinitialize a port by disabling port interrupts, the DMA engine,
382 * and FIS reception.
383 *
384 * @port Pointer to the port structure
385 *
386 * return value
387 * None
388 */
389static inline void mtip_deinit_port(struct mtip_port *port)
390{
391 /* Disable interrupts on this port */
392 writel(0, port->mmio + PORT_IRQ_MASK);
393
394 /* Disable the DMA engine */
395 mtip_enable_engine(port, 0);
396
397 /* Disable FIS reception */
398 mtip_enable_fis(port, 0);
399}
400
401/*
402 * Initialize a port.
403 *
404 * This function deinitializes the port by calling mtip_deinit_port() and
405 * then initializes it by setting the command header and RX FIS addresses,
406 * clearing the SError register and any pending port interrupts before
407 * re-enabling the default set of port interrupts.
408 *
409 * @port Pointer to the port structure.
410 *
411 * return value
412 * None
413 */
414static void mtip_init_port(struct mtip_port *port)
415{
416 int i;
417 mtip_deinit_port(port);
418
419 /* Program the command list base and FIS base addresses */
420 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
421 writel((port->command_list_dma >> 16) >> 16,
422 port->mmio + PORT_LST_ADDR_HI);
423 writel((port->rxfis_dma >> 16) >> 16,
424 port->mmio + PORT_FIS_ADDR_HI);
425 }
426
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600428 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100429 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600430
431 /* Clear SError */
432 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
433
434 /* reset the completed registers.*/
435 for (i = 0; i < port->dd->slot_groups; i++)
436 writel(0xFFFFFFFF, port->completed[i]);
437
438 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700439 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600440
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100441 /* Clear any pending interrupts on the HBA. */
442 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
443 port->dd->mmio + HOST_IRQ_STAT);
444
Jens Axboe63166682011-09-27 21:27:43 -0600445 /* Enable port interrupts */
446 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
447}
448
449/*
450 * Restart a port
451 *
452 * @port Pointer to the port data structure.
453 *
454 * return value
455 * None
456 */
457static void mtip_restart_port(struct mtip_port *port)
458{
459 unsigned long timeout;
460
461 /* Disable the DMA engine */
462 mtip_enable_engine(port, 0);
463
464 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
465 timeout = jiffies + msecs_to_jiffies(500);
466 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
467 && time_before(jiffies, timeout))
468 ;
469
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200470 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200471 return;
472
Jens Axboe63166682011-09-27 21:27:43 -0600473 /*
474 * Chip quirk: escalate to hba reset if
475 * PxCMD.CR not clear after 500 ms
476 */
477 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
478 dev_warn(&port->dd->pdev->dev,
479 "PxCMD.CR not clear, escalating reset\n");
480
481 if (hba_reset_nosleep(port->dd))
482 dev_err(&port->dd->pdev->dev,
483 "HBA reset escalation failed.\n");
484
485 /* 30 ms delay before com reset to quiesce chip */
486 mdelay(30);
487 }
488
489 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
490
491 /* Set PxSCTL.DET */
492 writel(readl(port->mmio + PORT_SCR_CTL) |
493 1, port->mmio + PORT_SCR_CTL);
494 readl(port->mmio + PORT_SCR_CTL);
495
496 /* Wait 1 ms to quiesce chip function */
497 timeout = jiffies + msecs_to_jiffies(1);
498 while (time_before(jiffies, timeout))
499 ;
500
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200501 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200502 return;
503
Jens Axboe63166682011-09-27 21:27:43 -0600504 /* Clear PxSCTL.DET */
505 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
506 port->mmio + PORT_SCR_CTL);
507 readl(port->mmio + PORT_SCR_CTL);
508
509 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
510 timeout = jiffies + msecs_to_jiffies(500);
511 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
512 && time_before(jiffies, timeout))
513 ;
514
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200515 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200516 return;
517
Jens Axboe63166682011-09-27 21:27:43 -0600518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
521
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100522 mtip_init_port(port);
523 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600524
Jens Axboe63166682011-09-27 21:27:43 -0600525}
526
527/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200528 * Helper function for tag logging
529 */
530static void print_tags(struct driver_data *dd,
531 char *msg,
532 unsigned long *tagbits,
533 int cnt)
534{
535 unsigned char tagmap[128];
536 int group, tagmap_len = 0;
537
538 memset(tagmap, 0, sizeof(tagmap));
539 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
540 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
541 tagbits[group-1]);
542 dev_warn(&dd->pdev->dev,
543 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
544}
545
546/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600547 * Called periodically to see if any read/write commands are
548 * taking too long to complete.
549 *
550 * @data Pointer to the PORT data structure.
551 *
552 * return value
553 * None
554 */
Jens Axboe63166682011-09-27 21:27:43 -0600555static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600556{
557 struct mtip_port *port = (struct mtip_port *) data;
558 struct host_to_dev_fis *fis;
559 struct mtip_cmd *command;
560 int tag, cmdto_cnt = 0;
561 unsigned int bit, group;
562 unsigned int num_command_slots = port->dd->slot_groups * 32;
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200563 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600564
565 if (unlikely(!port))
566 return;
567
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200568 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600569 mod_timer(&port->cmd_timer,
570 jiffies + msecs_to_jiffies(30000));
571 return;
572 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200573 /* clear the tag accumulator */
574 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600575
576 for (tag = 0; tag < num_command_slots; tag++) {
577 /*
578 * Skip internal command slot as it has
579 * its own timeout mechanism
580 */
581 if (tag == MTIP_TAG_INTERNAL)
582 continue;
583
584 if (atomic_read(&port->commands[tag].active) &&
585 (time_after(jiffies, port->commands[tag].comp_time))) {
586 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100587 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600588
589 command = &port->commands[tag];
590 fis = (struct host_to_dev_fis *) command->command;
591
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200592 set_bit(tag, tagaccum);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600593 cmdto_cnt++;
594 if (cmdto_cnt == 1)
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200595 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600596
597 /*
598 * Clear the completed bit. This should prevent
599 * any interrupt handlers from trying to retire
600 * the command.
601 */
602 writel(1 << bit, port->completed[group]);
603
604 /* Call the async completion callback. */
605 if (likely(command->async_callback))
606 command->async_callback(command->async_data,
607 -EIO);
608 command->async_callback = NULL;
609 command->comp_func = NULL;
610
611 /* Unmap the DMA scatter list entries */
612 dma_unmap_sg(&port->dd->pdev->dev,
613 command->sg,
614 command->scatter_ents,
615 command->direction);
616
617 /*
618 * Clear the allocated bit and active tag for the
619 * command.
620 */
621 atomic_set(&port->commands[tag].active, 0);
622 release_slot(port, tag);
623
624 up(&port->cmd_slot);
625 }
626 }
627
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200628 if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200629 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
630
Sam Bradshaw88523a62011-08-30 08:34:26 -0600631 mtip_restart_port(port);
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200632 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100633 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600634 }
635
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200636 if (port->ic_pause_timer) {
637 to = port->ic_pause_timer + msecs_to_jiffies(1000);
638 if (time_after(jiffies, to)) {
639 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
640 port->ic_pause_timer = 0;
641 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
642 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
643 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
644 wake_up_interruptible(&port->svc_wait);
645 }
646
647
648 }
649 }
650
Sam Bradshaw88523a62011-08-30 08:34:26 -0600651 /* Restart the timer */
652 mod_timer(&port->cmd_timer,
653 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
654}
655
656/*
657 * IO completion function.
658 *
659 * This completion function is called by the driver ISR when a
660 * command that was issued by the kernel completes. It first calls the
661 * asynchronous completion function which normally calls back into the block
662 * layer passing the asynchronous callback data, then unmaps the
663 * scatter list associated with the completed command, and finally
664 * clears the allocated bit associated with the completed command.
665 *
666 * @port Pointer to the port data structure.
667 * @tag Tag of the command.
668 * @data Pointer to driver_data.
669 * @status Completion status.
670 *
671 * return value
672 * None
673 */
674static void mtip_async_complete(struct mtip_port *port,
675 int tag,
676 void *data,
677 int status)
678{
679 struct mtip_cmd *command;
680 struct driver_data *dd = data;
681 int cb_status = status ? -EIO : 0;
682
683 if (unlikely(!dd) || unlikely(!port))
684 return;
685
686 command = &port->commands[tag];
687
688 if (unlikely(status == PORT_IRQ_TF_ERR)) {
689 dev_warn(&port->dd->pdev->dev,
690 "Command tag %d failed due to TFE\n", tag);
691 }
692
693 /* Upper layer callback */
694 if (likely(command->async_callback))
695 command->async_callback(command->async_data, cb_status);
696
697 command->async_callback = NULL;
698 command->comp_func = NULL;
699
700 /* Unmap the DMA scatter list entries */
701 dma_unmap_sg(&dd->pdev->dev,
702 command->sg,
703 command->scatter_ents,
704 command->direction);
705
706 /* Clear the allocated and active bits for the command */
707 atomic_set(&port->commands[tag].active, 0);
708 release_slot(port, tag);
709
710 up(&port->cmd_slot);
711}
712
713/*
714 * Internal command completion callback function.
715 *
716 * This function is normally called by the driver ISR when an internal
717 * command completed. This function signals the command completion by
718 * calling complete().
719 *
720 * @port Pointer to the port data structure.
721 * @tag Tag of the command that has completed.
722 * @data Pointer to a completion structure.
723 * @status Completion status.
724 *
725 * return value
726 * None
727 */
728static void mtip_completion(struct mtip_port *port,
729 int tag,
730 void *data,
731 int status)
732{
733 struct mtip_cmd *command = &port->commands[tag];
734 struct completion *waiting = data;
735 if (unlikely(status == PORT_IRQ_TF_ERR))
736 dev_warn(&port->dd->pdev->dev,
737 "Internal command %d completed with TFE\n", tag);
738
739 command->async_callback = NULL;
740 command->comp_func = NULL;
741
742 complete(waiting);
743}
744
Asai Thambi S P8182b492012-04-09 08:35:38 +0200745static void mtip_null_completion(struct mtip_port *port,
746 int tag,
747 void *data,
748 int status)
749{
750 return;
751}
752
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200753static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
754 dma_addr_t buffer_dma, unsigned int sectors);
755static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
756 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600757/*
758 * Handle an error.
759 *
760 * @dd Pointer to the DRIVER_DATA structure.
761 *
762 * return value
763 * None
764 */
765static void mtip_handle_tfe(struct driver_data *dd)
766{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200767 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600768 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200769 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600770 u32 completed;
771 struct host_to_dev_fis *fis;
772 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200773 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200774 unsigned char *buf;
775 char *fail_reason = NULL;
776 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600777
778 dev_warn(&dd->pdev->dev, "Taskfile error\n");
779
780 port = dd->port;
781
782 /* Stop the timer to prevent command timeouts. */
783 del_timer(&port->cmd_timer);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700784 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
785
786 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
787 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
788 cmd = &port->commands[MTIP_TAG_INTERNAL];
789 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
790
791 atomic_inc(&cmd->active); /* active > 1 indicates error */
792 if (cmd->comp_data && cmd->comp_func) {
793 cmd->comp_func(port, MTIP_TAG_INTERNAL,
794 cmd->comp_data, PORT_IRQ_TF_ERR);
795 }
796 goto handle_tfe_exit;
797 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600798
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200799 /* clear the tag accumulator */
800 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
801
Sam Bradshaw88523a62011-08-30 08:34:26 -0600802 /* Loop through all the groups */
803 for (group = 0; group < dd->slot_groups; group++) {
804 completed = readl(port->completed[group]);
805
806 /* clear completed status register in the hardware.*/
807 writel(completed, port->completed[group]);
808
Sam Bradshaw88523a62011-08-30 08:34:26 -0600809 /* Process successfully completed commands */
810 for (bit = 0; bit < 32 && completed; bit++) {
811 if (!(completed & (1<<bit)))
812 continue;
813 tag = (group << 5) + bit;
814
815 /* Skip the internal command slot */
816 if (tag == MTIP_TAG_INTERNAL)
817 continue;
818
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200819 cmd = &port->commands[tag];
820 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600821 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200822 cmd_cnt++;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200823 atomic_set(&cmd->active, 0);
824 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600825 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200826 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600827 0);
828 } else {
829 dev_err(&port->dd->pdev->dev,
830 "Missing completion func for tag %d",
831 tag);
832 if (mtip_check_surprise_removal(dd->pdev)) {
833 mtip_command_cleanup(dd);
834 /* don't proceed further */
835 return;
836 }
837 }
838 }
839 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200840
841 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600842
843 /* Restart the port */
844 mdelay(20);
845 mtip_restart_port(port);
846
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200847 /* Trying to determine the cause of the error */
848 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
849 dd->port->log_buf,
850 dd->port->log_buf_dma, 1);
851 if (rv) {
852 dev_warn(&dd->pdev->dev,
853 "Error in READ LOG EXT (10h) command\n");
854 /* non-critical error, don't fail the load */
855 } else {
856 buf = (unsigned char *)dd->port->log_buf;
857 if (buf[259] & 0x1) {
858 dev_info(&dd->pdev->dev,
859 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200860 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200861 fail_all_ncq_write = 1;
862 fail_reason = "write protect";
863 }
864 if (buf[288] == 0xF7) {
865 dev_info(&dd->pdev->dev,
866 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200867 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200868 fail_all_ncq_cmds = 1;
869 fail_reason = "thermal shutdown";
870 }
871 if (buf[288] == 0xBF) {
872 dev_info(&dd->pdev->dev,
873 "Drive indicates rebuild has failed.\n");
874 fail_all_ncq_cmds = 1;
875 fail_reason = "rebuild failed";
876 }
877 }
878
Sam Bradshaw88523a62011-08-30 08:34:26 -0600879 /* clear the tag accumulator */
880 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
881
882 /* Loop through all the groups */
883 for (group = 0; group < dd->slot_groups; group++) {
884 for (bit = 0; bit < 32; bit++) {
885 reissue = 1;
886 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200887 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600888
889 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200890 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600891 continue;
892
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200893 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600894
895 /* Should re-issue? */
896 if (tag == MTIP_TAG_INTERNAL ||
897 fis->command == ATA_CMD_SET_FEATURES)
898 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200899 else {
900 if (fail_all_ncq_cmds ||
901 (fail_all_ncq_write &&
902 fis->command == ATA_CMD_FPDMA_WRITE)) {
903 dev_warn(&dd->pdev->dev,
904 " Fail: %s w/tag %d [%s].\n",
905 fis->command == ATA_CMD_FPDMA_WRITE ?
906 "write" : "read",
907 tag,
908 fail_reason != NULL ?
909 fail_reason : "unknown");
910 atomic_set(&cmd->active, 0);
911 if (cmd->comp_func) {
912 cmd->comp_func(port, tag,
913 cmd->comp_data,
914 -ENODATA);
915 }
916 continue;
917 }
918 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600919
920 /*
921 * First check if this command has
922 * exceeded its retries.
923 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200924 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600925
926 set_bit(tag, tagaccum);
927
Sam Bradshaw88523a62011-08-30 08:34:26 -0600928 /* Re-issue the command. */
929 mtip_issue_ncq_command(port, tag);
930
931 continue;
932 }
933
934 /* Retire a command that will not be reissued */
935 dev_warn(&port->dd->pdev->dev,
936 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200937 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600938
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200939 if (cmd->comp_func)
940 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -0600941 port,
942 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200943 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600944 PORT_IRQ_TF_ERR);
945 else
946 dev_warn(&port->dd->pdev->dev,
947 "Bad completion for tag %d\n",
948 tag);
949 }
950 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200951 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600952
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700953handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100954 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200955 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100956 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600957
958 mod_timer(&port->cmd_timer,
959 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
960}
961
962/*
963 * Handle a set device bits interrupt
964 */
965static inline void mtip_process_sdbf(struct driver_data *dd)
966{
967 struct mtip_port *port = dd->port;
968 int group, tag, bit;
969 u32 completed;
970 struct mtip_cmd *command;
971
972 /* walk all bits in all slot groups */
973 for (group = 0; group < dd->slot_groups; group++) {
974 completed = readl(port->completed[group]);
Asai Thambi S P377b8fc62012-05-29 18:44:01 -0700975 if (!completed)
976 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600977
978 /* clear completed status register in the hardware.*/
979 writel(completed, port->completed[group]);
980
981 /* Process completed commands. */
982 for (bit = 0;
983 (bit < 32) && completed;
984 bit++, completed >>= 1) {
985 if (completed & 0x01) {
986 tag = (group << 5) | bit;
987
988 /* skip internal command slot. */
989 if (unlikely(tag == MTIP_TAG_INTERNAL))
990 continue;
991
992 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600993 /* 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);
1005
1006 if (mtip_check_surprise_removal(
1007 dd->pdev)) {
1008 mtip_command_cleanup(dd);
1009 return;
1010 }
1011 }
1012 }
1013 }
1014 }
1015}
1016
1017/*
1018 * Process legacy pio and d2h interrupts
1019 */
1020static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1021{
1022 struct mtip_port *port = dd->port;
1023 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1024
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001025 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001026 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -06001027 & (1 << MTIP_TAG_INTERNAL))) {
1028 if (cmd->comp_func) {
1029 cmd->comp_func(port,
1030 MTIP_TAG_INTERNAL,
1031 cmd->comp_data,
1032 0);
1033 return;
1034 }
1035 }
1036
Sam Bradshaw88523a62011-08-30 08:34:26 -06001037 return;
1038}
1039
1040/*
1041 * Demux and handle errors
1042 */
1043static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1044{
1045 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1046 mtip_handle_tfe(dd);
1047
1048 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1049 dev_warn(&dd->pdev->dev,
1050 "Clearing PxSERR.DIAG.x\n");
1051 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1052 }
1053
1054 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1055 dev_warn(&dd->pdev->dev,
1056 "Clearing PxSERR.DIAG.n\n");
1057 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1058 }
1059
1060 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1061 dev_warn(&dd->pdev->dev,
1062 "Port stat errors %x unhandled\n",
1063 (port_stat & ~PORT_IRQ_HANDLED));
1064 }
1065}
1066
1067static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1068{
1069 struct driver_data *dd = (struct driver_data *) data;
1070 struct mtip_port *port = dd->port;
1071 u32 hba_stat, port_stat;
1072 int rv = IRQ_NONE;
1073
1074 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1075 if (hba_stat) {
1076 rv = IRQ_HANDLED;
1077
1078 /* Acknowledge the interrupt status on the port.*/
1079 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1080 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1081
1082 /* Demux port status */
1083 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1084 mtip_process_sdbf(dd);
1085
1086 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1087 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1088 mtip_command_cleanup(dd);
1089 /* don't proceed further */
1090 return IRQ_HANDLED;
1091 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001092 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001093 &dd->dd_flag))
1094 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001095
1096 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1097 }
1098
1099 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1100 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1101 }
1102
1103 /* acknowledge interrupt */
1104 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1105
1106 return rv;
1107}
1108
1109/*
1110 * Wrapper for mtip_handle_irq
1111 * (ignores return code)
1112 */
1113static void mtip_tasklet(unsigned long data)
1114{
1115 mtip_handle_irq((struct driver_data *) data);
1116}
1117
1118/*
1119 * HBA interrupt subroutine.
1120 *
1121 * @irq IRQ number.
1122 * @instance Pointer to the driver data structure.
1123 *
1124 * return value
1125 * IRQ_HANDLED A HBA interrupt was pending and handled.
1126 * IRQ_NONE This interrupt was not for the HBA.
1127 */
1128static irqreturn_t mtip_irq_handler(int irq, void *instance)
1129{
1130 struct driver_data *dd = instance;
1131 tasklet_schedule(&dd->tasklet);
1132 return IRQ_HANDLED;
1133}
1134
1135static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1136{
1137 atomic_set(&port->commands[tag].active, 1);
1138 writel(1 << MTIP_TAG_BIT(tag),
1139 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1140}
1141
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001142static bool mtip_pause_ncq(struct mtip_port *port,
1143 struct host_to_dev_fis *fis)
1144{
1145 struct host_to_dev_fis *reply;
1146 unsigned long task_file_data;
1147
1148 reply = port->rxfis + RX_FIS_D2H_REG;
1149 task_file_data = readl(port->mmio+PORT_TFDATA);
1150
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301151 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1152 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1153
1154 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001155 return false;
1156
1157 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1158 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301159 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001160 port->ic_pause_timer = jiffies;
1161 return true;
1162 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1163 (fis->features == 0x03)) {
1164 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1165 port->ic_pause_timer = jiffies;
1166 return true;
1167 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1168 ((fis->command == 0xFC) &&
1169 (fis->features == 0x27 || fis->features == 0x72 ||
1170 fis->features == 0x62 || fis->features == 0x26))) {
1171 /* Com reset after secure erase or lowlevel format */
1172 mtip_restart_port(port);
1173 return false;
1174 }
1175
1176 return false;
1177}
1178
Sam Bradshaw88523a62011-08-30 08:34:26 -06001179/*
1180 * Wait for port to quiesce
1181 *
1182 * @port Pointer to port data structure
1183 * @timeout Max duration to wait (ms)
1184 *
1185 * return value
1186 * 0 Success
1187 * -EBUSY Commands still active
1188 */
1189static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1190{
1191 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001192 unsigned int n;
1193 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001194
1195 to = jiffies + msecs_to_jiffies(timeout);
1196 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001197 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1198 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001199 msleep(20);
1200 continue; /* svc thd is actively issuing commands */
1201 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001202 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001203 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001204 /*
1205 * Ignore s_active bit 0 of array element 0.
1206 * This bit will always be set
1207 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001208 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001209 for (n = 1; n < port->dd->slot_groups; n++)
1210 active |= readl(port->s_active[n]);
1211
1212 if (!active)
1213 break;
1214
1215 msleep(20);
1216 } while (time_before(jiffies, to));
1217
1218 return active ? -EBUSY : 0;
1219}
1220
1221/*
1222 * Execute an internal command and wait for the completion.
1223 *
1224 * @port Pointer to the port data structure.
1225 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001226 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001227 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001228 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001229 * @opts Command header options, excluding the FIS length
1230 * and the number of PRD entries.
1231 * @timeout Time in ms to wait for the command to complete.
1232 *
1233 * return value
1234 * 0 Command completed successfully.
1235 * -EFAULT The buffer address is not correctly aligned.
1236 * -EBUSY Internal command or other IO in progress.
1237 * -EAGAIN Time out waiting for command to complete.
1238 */
1239static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001240 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001241 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001242 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001243 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001244 u32 opts,
1245 gfp_t atomic,
1246 unsigned long timeout)
1247{
1248 struct mtip_cmd_sg *command_sg;
1249 DECLARE_COMPLETION_ONSTACK(wait);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001250 int rv = 0, ready2go = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001251 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001252 unsigned long to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001253
1254 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1255 if (buffer & 0x00000007) {
1256 dev_err(&port->dd->pdev->dev,
1257 "SG buffer is not 8 byte aligned\n");
1258 return -EFAULT;
1259 }
1260
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001261 to = jiffies + msecs_to_jiffies(timeout);
1262 do {
1263 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1264 port->allocated);
1265 if (ready2go)
1266 break;
1267 mdelay(100);
1268 } while (time_before(jiffies, to));
1269 if (!ready2go) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001270 dev_warn(&port->dd->pdev->dev,
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001271 "Internal cmd active. new cmd [%02X]\n", fis->command);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001272 return -EBUSY;
1273 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001274 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001275 port->ic_pause_timer = 0;
1276
1277 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1278 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1279 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1280 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001281
1282 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001283 if (fis->command != ATA_CMD_STANDBYNOW1) {
1284 /* wait for io to complete if non atomic */
1285 if (mtip_quiesce_io(port, 5000) < 0) {
1286 dev_warn(&port->dd->pdev->dev,
1287 "Failed to quiesce IO\n");
1288 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001289 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001290 wake_up_interruptible(&port->svc_wait);
1291 return -EBUSY;
1292 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001293 }
1294
1295 /* Set the completion function and data for the command. */
1296 int_cmd->comp_data = &wait;
1297 int_cmd->comp_func = mtip_completion;
1298
1299 } else {
1300 /* Clear completion - we're going to poll */
1301 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001302 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001303 }
1304
1305 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001306 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001307
1308 /* Populate the SG list */
1309 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001310 __force_bit2int cpu_to_le32(opts | fis_len);
1311 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001312 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1313
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001314 command_sg->info =
1315 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1316 command_sg->dba =
1317 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1318 command_sg->dba_upper =
1319 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001320
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001321 int_cmd->command_header->opts |=
1322 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001323 }
1324
1325 /* Populate the command header */
1326 int_cmd->command_header->byte_count = 0;
1327
1328 /* Issue the command to the hardware */
1329 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1330
1331 /* Poll if atomic, wait_for_completion otherwise */
1332 if (atomic == GFP_KERNEL) {
1333 /* Wait for the command to complete or timeout. */
1334 if (wait_for_completion_timeout(
1335 &wait,
1336 msecs_to_jiffies(timeout)) == 0) {
1337 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001338 "Internal command did not complete [%d] "
1339 "within timeout of %lu ms\n",
1340 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001341 if (mtip_check_surprise_removal(port->dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001342 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001343 &port->dd->dd_flag)) {
1344 rv = -ENXIO;
1345 goto exec_ic_exit;
1346 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001347 rv = -EAGAIN;
1348 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001349 } else {
1350 /* Spin for <timeout> checking if command still outstanding */
1351 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001352 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1353 & (1 << MTIP_TAG_INTERNAL))
1354 && time_before(jiffies, timeout)) {
1355 if (mtip_check_surprise_removal(port->dd->pdev)) {
1356 rv = -ENXIO;
1357 goto exec_ic_exit;
1358 }
1359 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001360 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001361 &port->dd->dd_flag)) {
1362 rv = -ENXIO;
1363 goto exec_ic_exit;
1364 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001365 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1366 atomic_inc(&int_cmd->active); /* error */
1367 break;
Asai Thambi S P45038362012-04-09 08:35:38 +02001368 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001369 }
1370 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001371
1372 if (atomic_read(&int_cmd->active) > 1) {
1373 dev_err(&port->dd->pdev->dev,
1374 "Internal command [%02X] failed\n", fis->command);
1375 rv = -EIO;
1376 }
1377 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1378 & (1 << MTIP_TAG_INTERNAL)) {
1379 rv = -ENXIO;
1380 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1381 &port->dd->dd_flag)) {
1382 mtip_restart_port(port);
1383 rv = -EAGAIN;
1384 }
1385 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001386exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001387 /* Clear the allocated and active bits for the internal command. */
1388 atomic_set(&int_cmd->active, 0);
1389 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001390 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1391 /* NCQ paused */
1392 return rv;
1393 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001394 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001395 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001396
1397 return rv;
1398}
1399
1400/*
1401 * Byte-swap ATA ID strings.
1402 *
1403 * ATA identify data contains strings in byte-swapped 16-bit words.
1404 * They must be swapped (on all architectures) to be usable as C strings.
1405 * This function swaps bytes in-place.
1406 *
1407 * @buf The buffer location of the string
1408 * @len The number of bytes to swap
1409 *
1410 * return value
1411 * None
1412 */
1413static inline void ata_swap_string(u16 *buf, unsigned int len)
1414{
1415 int i;
1416 for (i = 0; i < (len/2); i++)
1417 be16_to_cpus(&buf[i]);
1418}
1419
1420/*
1421 * Request the device identity information.
1422 *
1423 * If a user space buffer is not specified, i.e. is NULL, the
1424 * identify information is still read from the drive and placed
1425 * into the identify data buffer (@e port->identify) in the
1426 * port data structure.
1427 * When the identify buffer contains valid identify information @e
1428 * port->identify_valid is non-zero.
1429 *
1430 * @port Pointer to the port structure.
1431 * @user_buffer A user space buffer where the identify data should be
1432 * copied.
1433 *
1434 * return value
1435 * 0 Command completed successfully.
1436 * -EFAULT An error occurred while coping data to the user buffer.
1437 * -1 Command failed.
1438 */
1439static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1440{
1441 int rv = 0;
1442 struct host_to_dev_fis fis;
1443
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001444 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001445 return -EFAULT;
1446
Sam Bradshaw88523a62011-08-30 08:34:26 -06001447 /* Build the FIS. */
1448 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1449 fis.type = 0x27;
1450 fis.opts = 1 << 7;
1451 fis.command = ATA_CMD_ID_ATA;
1452
1453 /* Set the identify information as invalid. */
1454 port->identify_valid = 0;
1455
1456 /* Clear the identify information. */
1457 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1458
1459 /* Execute the command. */
1460 if (mtip_exec_internal_command(port,
1461 &fis,
1462 5,
1463 port->identify_dma,
1464 sizeof(u16) * ATA_ID_WORDS,
1465 0,
1466 GFP_KERNEL,
1467 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1468 < 0) {
1469 rv = -1;
1470 goto out;
1471 }
1472
1473 /*
1474 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1475 * perform field-sensitive swapping on the string fields.
1476 * See the kernel use of ata_id_string() for proof of this.
1477 */
1478#ifdef __LITTLE_ENDIAN
1479 ata_swap_string(port->identify + 27, 40); /* model string*/
1480 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1481 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1482#else
1483 {
1484 int i;
1485 for (i = 0; i < ATA_ID_WORDS; i++)
1486 port->identify[i] = le16_to_cpu(port->identify[i]);
1487 }
1488#endif
1489
1490 /* Set the identify buffer as valid. */
1491 port->identify_valid = 1;
1492
1493 if (user_buffer) {
1494 if (copy_to_user(
1495 user_buffer,
1496 port->identify,
1497 ATA_ID_WORDS * sizeof(u16))) {
1498 rv = -EFAULT;
1499 goto out;
1500 }
1501 }
1502
1503out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001504 return rv;
1505}
1506
1507/*
1508 * Issue a standby immediate command to the device.
1509 *
1510 * @port Pointer to the port structure.
1511 *
1512 * return value
1513 * 0 Command was executed successfully.
1514 * -1 An error occurred while executing the command.
1515 */
1516static int mtip_standby_immediate(struct mtip_port *port)
1517{
1518 int rv;
1519 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001520 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001521
Sam Bradshaw88523a62011-08-30 08:34:26 -06001522 /* Build the FIS. */
1523 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1524 fis.type = 0x27;
1525 fis.opts = 1 << 7;
1526 fis.command = ATA_CMD_STANDBYNOW1;
1527
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001528 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001529 rv = mtip_exec_internal_command(port,
1530 &fis,
1531 5,
1532 0,
1533 0,
1534 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001535 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001536 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001537 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1538 jiffies_to_msecs(jiffies - start));
1539 if (rv)
1540 dev_warn(&port->dd->pdev->dev,
1541 "STANDBY IMMEDIATE command failed.\n");
1542
1543 return rv;
1544}
1545
1546/*
1547 * Issue a READ LOG EXT command to the device.
1548 *
1549 * @port pointer to the port structure.
1550 * @page page number to fetch
1551 * @buffer pointer to buffer
1552 * @buffer_dma dma address corresponding to @buffer
1553 * @sectors page length to fetch, in sectors
1554 *
1555 * return value
1556 * @rv return value from mtip_exec_internal_command()
1557 */
1558static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1559 dma_addr_t buffer_dma, unsigned int sectors)
1560{
1561 struct host_to_dev_fis fis;
1562
1563 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1564 fis.type = 0x27;
1565 fis.opts = 1 << 7;
1566 fis.command = ATA_CMD_READ_LOG_EXT;
1567 fis.sect_count = sectors & 0xFF;
1568 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1569 fis.lba_low = page;
1570 fis.lba_mid = 0;
1571 fis.device = ATA_DEVICE_OBS;
1572
1573 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1574
1575 return mtip_exec_internal_command(port,
1576 &fis,
1577 5,
1578 buffer_dma,
1579 sectors * ATA_SECT_SIZE,
1580 0,
1581 GFP_ATOMIC,
1582 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1583}
1584
1585/*
1586 * Issue a SMART READ DATA command to the device.
1587 *
1588 * @port pointer to the port structure.
1589 * @buffer pointer to buffer
1590 * @buffer_dma dma address corresponding to @buffer
1591 *
1592 * return value
1593 * @rv return value from mtip_exec_internal_command()
1594 */
1595static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1596 dma_addr_t buffer_dma)
1597{
1598 struct host_to_dev_fis fis;
1599
1600 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1601 fis.type = 0x27;
1602 fis.opts = 1 << 7;
1603 fis.command = ATA_CMD_SMART;
1604 fis.features = 0xD0;
1605 fis.sect_count = 1;
1606 fis.lba_mid = 0x4F;
1607 fis.lba_hi = 0xC2;
1608 fis.device = ATA_DEVICE_OBS;
1609
1610 return mtip_exec_internal_command(port,
1611 &fis,
1612 5,
1613 buffer_dma,
1614 ATA_SECT_SIZE,
1615 0,
1616 GFP_ATOMIC,
1617 15000);
1618}
1619
1620/*
1621 * Get the value of a smart attribute
1622 *
1623 * @port pointer to the port structure
1624 * @id attribute number
1625 * @attrib pointer to return attrib information corresponding to @id
1626 *
1627 * return value
1628 * -EINVAL NULL buffer passed or unsupported attribute @id.
1629 * -EPERM Identify data not valid, SMART not supported or not enabled
1630 */
1631static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1632 struct smart_attr *attrib)
1633{
1634 int rv, i;
1635 struct smart_attr *pattr;
1636
1637 if (!attrib)
1638 return -EINVAL;
1639
1640 if (!port->identify_valid) {
1641 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1642 return -EPERM;
1643 }
1644 if (!(port->identify[82] & 0x1)) {
1645 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1646 return -EPERM;
1647 }
1648 if (!(port->identify[85] & 0x1)) {
1649 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1650 return -EPERM;
1651 }
1652
1653 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1654 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1655 if (rv) {
1656 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1657 return rv;
1658 }
1659
1660 pattr = (struct smart_attr *)(port->smart_buf + 2);
1661 for (i = 0; i < 29; i++, pattr++)
1662 if (pattr->attr_id == id) {
1663 memcpy(attrib, pattr, sizeof(struct smart_attr));
1664 break;
1665 }
1666
1667 if (i == 29) {
1668 dev_warn(&port->dd->pdev->dev,
1669 "Query for invalid SMART attribute ID\n");
1670 rv = -EINVAL;
1671 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001672
Sam Bradshaw88523a62011-08-30 08:34:26 -06001673 return rv;
1674}
1675
1676/*
1677 * Get the drive capacity.
1678 *
1679 * @dd Pointer to the device data structure.
1680 * @sectors Pointer to the variable that will receive the sector count.
1681 *
1682 * return value
1683 * 1 Capacity was returned successfully.
1684 * 0 The identify information is invalid.
1685 */
Jens Axboe63166682011-09-27 21:27:43 -06001686static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001687{
1688 struct mtip_port *port = dd->port;
1689 u64 total, raw0, raw1, raw2, raw3;
1690 raw0 = port->identify[100];
1691 raw1 = port->identify[101];
1692 raw2 = port->identify[102];
1693 raw3 = port->identify[103];
1694 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1695 *sectors = total;
1696 return (bool) !!port->identify_valid;
1697}
1698
1699/*
1700 * Reset the HBA.
1701 *
1702 * Resets the HBA by setting the HBA Reset bit in the Global
1703 * HBA Control register. After setting the HBA Reset bit the
1704 * function waits for 1 second before reading the HBA Reset
1705 * bit to make sure it has cleared. If HBA Reset is not clear
1706 * an error is returned. Cannot be used in non-blockable
1707 * context.
1708 *
1709 * @dd Pointer to the driver data structure.
1710 *
1711 * return value
1712 * 0 The reset was successful.
1713 * -1 The HBA Reset bit did not clear.
1714 */
1715static int mtip_hba_reset(struct driver_data *dd)
1716{
1717 mtip_deinit_port(dd->port);
1718
1719 /* Set the reset bit */
1720 writel(HOST_RESET, dd->mmio + HOST_CTL);
1721
1722 /* Flush */
1723 readl(dd->mmio + HOST_CTL);
1724
1725 /* Wait for reset to clear */
1726 ssleep(1);
1727
1728 /* Check the bit has cleared */
1729 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1730 dev_err(&dd->pdev->dev,
1731 "Reset bit did not clear.\n");
1732 return -1;
1733 }
1734
1735 return 0;
1736}
1737
1738/*
1739 * Display the identify command data.
1740 *
1741 * @port Pointer to the port data structure.
1742 *
1743 * return value
1744 * None
1745 */
1746static void mtip_dump_identify(struct mtip_port *port)
1747{
1748 sector_t sectors;
1749 unsigned short revid;
1750 char cbuf[42];
1751
1752 if (!port->identify_valid)
1753 return;
1754
1755 strlcpy(cbuf, (char *)(port->identify+10), 21);
1756 dev_info(&port->dd->pdev->dev,
1757 "Serial No.: %s\n", cbuf);
1758
1759 strlcpy(cbuf, (char *)(port->identify+23), 9);
1760 dev_info(&port->dd->pdev->dev,
1761 "Firmware Ver.: %s\n", cbuf);
1762
1763 strlcpy(cbuf, (char *)(port->identify+27), 41);
1764 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1765
1766 if (mtip_hw_get_capacity(port->dd, &sectors))
1767 dev_info(&port->dd->pdev->dev,
1768 "Capacity: %llu sectors (%llu MB)\n",
1769 (u64)sectors,
1770 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1771
1772 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001773 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001774 case 0x1:
1775 strlcpy(cbuf, "A0", 3);
1776 break;
1777 case 0x3:
1778 strlcpy(cbuf, "A2", 3);
1779 break;
1780 default:
1781 strlcpy(cbuf, "?", 2);
1782 break;
1783 }
1784 dev_info(&port->dd->pdev->dev,
1785 "Card Type: %s\n", cbuf);
1786}
1787
1788/*
1789 * Map the commands scatter list into the command table.
1790 *
1791 * @command Pointer to the command.
1792 * @nents Number of scatter list entries.
1793 *
1794 * return value
1795 * None
1796 */
1797static inline void fill_command_sg(struct driver_data *dd,
1798 struct mtip_cmd *command,
1799 int nents)
1800{
1801 int n;
1802 unsigned int dma_len;
1803 struct mtip_cmd_sg *command_sg;
1804 struct scatterlist *sg = command->sg;
1805
1806 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1807
1808 for (n = 0; n < nents; n++) {
1809 dma_len = sg_dma_len(sg);
1810 if (dma_len > 0x400000)
1811 dev_err(&dd->pdev->dev,
1812 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001813 command_sg->info = __force_bit2int
1814 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1815 command_sg->dba = __force_bit2int
1816 cpu_to_le32(sg_dma_address(sg));
1817 command_sg->dba_upper = __force_bit2int
1818 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001819 command_sg++;
1820 sg++;
1821 }
1822}
1823
1824/*
1825 * @brief Execute a drive command.
1826 *
1827 * return value 0 The command completed successfully.
1828 * return value -1 An error occurred while executing the command.
1829 */
Jens Axboe63166682011-09-27 21:27:43 -06001830static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001831{
1832 struct host_to_dev_fis fis;
1833 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1834
Sam Bradshaw88523a62011-08-30 08:34:26 -06001835 /* Build the FIS. */
1836 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1837 fis.type = 0x27;
1838 fis.opts = 1 << 7;
1839 fis.command = command[0];
1840 fis.features = command[1];
1841 fis.sect_count = command[2];
1842 fis.sector = command[3];
1843 fis.cyl_low = command[4];
1844 fis.cyl_hi = command[5];
1845 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1846
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001847 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 -06001848 __func__,
1849 command[0],
1850 command[1],
1851 command[2],
1852 command[3],
1853 command[4],
1854 command[5],
1855 command[6]);
1856
1857 /* Execute the command. */
1858 if (mtip_exec_internal_command(port,
1859 &fis,
1860 5,
1861 0,
1862 0,
1863 0,
1864 GFP_KERNEL,
1865 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001866 return -1;
1867 }
1868
1869 command[0] = reply->command; /* Status*/
1870 command[1] = reply->features; /* Error*/
1871 command[4] = reply->cyl_low;
1872 command[5] = reply->cyl_hi;
1873
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001874 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 -06001875 __func__,
1876 command[0],
1877 command[1],
1878 command[4],
1879 command[5]);
1880
Sam Bradshaw88523a62011-08-30 08:34:26 -06001881 return 0;
1882}
1883
1884/*
1885 * @brief Execute a drive command.
1886 *
1887 * @param port Pointer to the port data structure.
1888 * @param command Pointer to the user specified command parameters.
1889 * @param user_buffer Pointer to the user space buffer where read sector
1890 * data should be copied.
1891 *
1892 * return value 0 The command completed successfully.
1893 * return value -EFAULT An error occurred while copying the completion
1894 * data to the user space buffer.
1895 * return value -1 An error occurred while executing the command.
1896 */
Jens Axboe63166682011-09-27 21:27:43 -06001897static int exec_drive_command(struct mtip_port *port, u8 *command,
1898 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001899{
1900 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001901 struct host_to_dev_fis *reply;
1902 u8 *buf = NULL;
1903 dma_addr_t dma_addr = 0;
1904 int rv = 0, xfer_sz = command[3];
1905
1906 if (xfer_sz) {
1907 if (user_buffer)
1908 return -EFAULT;
1909
1910 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1911 ATA_SECT_SIZE * xfer_sz,
1912 &dma_addr,
1913 GFP_KERNEL);
1914 if (!buf) {
1915 dev_err(&port->dd->pdev->dev,
1916 "Memory allocation failed (%d bytes)\n",
1917 ATA_SECT_SIZE * xfer_sz);
1918 return -ENOMEM;
1919 }
1920 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1921 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922
Sam Bradshaw88523a62011-08-30 08:34:26 -06001923 /* Build the FIS. */
1924 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001925 fis.type = 0x27;
1926 fis.opts = 1 << 7;
1927 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928 fis.features = command[2];
1929 fis.sect_count = command[3];
1930 if (fis.command == ATA_CMD_SMART) {
1931 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001932 fis.cyl_low = 0x4F;
1933 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001934 }
1935
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001936 if (xfer_sz)
1937 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1938 else
1939 reply = (port->rxfis + RX_FIS_D2H_REG);
1940
Sam Bradshaw88523a62011-08-30 08:34:26 -06001941 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001942 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 "feat %x, sectcnt %x\n",
1944 __func__,
1945 command[0],
1946 command[1],
1947 command[2],
1948 command[3]);
1949
Sam Bradshaw88523a62011-08-30 08:34:26 -06001950 /* Execute the command. */
1951 if (mtip_exec_internal_command(port,
1952 &fis,
1953 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001954 (xfer_sz ? dma_addr : 0),
1955 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001956 0,
1957 GFP_KERNEL,
1958 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1959 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001960 rv = -EFAULT;
1961 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001962 }
1963
1964 /* Collect the completion status. */
1965 command[0] = reply->command; /* Status*/
1966 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001967 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001968
1969 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001970 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001971 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001972 __func__,
1973 command[0],
1974 command[1],
1975 command[2]);
1976
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001977 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001979 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001980 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001981 rv = -EFAULT;
1982 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001983 }
1984 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001985exit_drive_command:
1986 if (buf)
1987 dmam_free_coherent(&port->dd->pdev->dev,
1988 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1989 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001990}
1991
1992/*
1993 * Indicates whether a command has a single sector payload.
1994 *
1995 * @command passed to the device to perform the certain event.
1996 * @features passed to the device to perform the certain event.
1997 *
1998 * return value
1999 * 1 command is one that always has a single sector payload,
2000 * regardless of the value in the Sector Count field.
2001 * 0 otherwise
2002 *
2003 */
2004static unsigned int implicit_sector(unsigned char command,
2005 unsigned char features)
2006{
2007 unsigned int rv = 0;
2008
2009 /* list of commands that have an implicit sector count of 1 */
2010 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002011 case ATA_CMD_SEC_SET_PASS:
2012 case ATA_CMD_SEC_UNLOCK:
2013 case ATA_CMD_SEC_ERASE_PREP:
2014 case ATA_CMD_SEC_ERASE_UNIT:
2015 case ATA_CMD_SEC_FREEZE_LOCK:
2016 case ATA_CMD_SEC_DISABLE_PASS:
2017 case ATA_CMD_PMP_READ:
2018 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06002019 rv = 1;
2020 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002021 case ATA_CMD_SET_MAX:
2022 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002023 rv = 1;
2024 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002025 case ATA_CMD_SMART:
2026 if ((features == ATA_SMART_READ_VALUES) ||
2027 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002028 rv = 1;
2029 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002030 case ATA_CMD_CONF_OVERLAY:
2031 if ((features == ATA_DCO_IDENTIFY) ||
2032 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002033 rv = 1;
2034 break;
2035 }
2036 return rv;
2037}
2038
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002039static void mtip_set_timeout(struct host_to_dev_fis *fis, unsigned int *timeout)
2040{
2041 switch (fis->command) {
2042 case ATA_CMD_DOWNLOAD_MICRO:
2043 *timeout = 120000; /* 2 minutes */
2044 break;
2045 case ATA_CMD_SEC_ERASE_UNIT:
2046 case 0xFC:
2047 *timeout = 240000; /* 4 minutes */
2048 break;
2049 case ATA_CMD_STANDBYNOW1:
Asai Thambi S Pd7c8b942012-09-05 22:02:16 +05302050 *timeout = 120000; /* 2 minutes */
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002051 break;
2052 case 0xF7:
2053 case 0xFA:
2054 *timeout = 60000; /* 60 seconds */
2055 break;
2056 case ATA_CMD_SMART:
2057 *timeout = 15000; /* 15 seconds */
2058 break;
2059 default:
2060 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2061 break;
2062 }
2063}
2064
Sam Bradshaw88523a62011-08-30 08:34:26 -06002065/*
2066 * Executes a taskfile
2067 * See ide_taskfile_ioctl() for derivation
2068 */
2069static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002070 void __user *buf,
2071 ide_task_request_t *req_task,
2072 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002073{
2074 struct host_to_dev_fis fis;
2075 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002076 u8 *outbuf = NULL;
2077 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002078 dma_addr_t outbuf_dma = 0;
2079 dma_addr_t inbuf_dma = 0;
2080 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002081 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002082 unsigned int taskin = 0;
2083 unsigned int taskout = 0;
2084 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002085 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002086 unsigned int force_single_sector;
2087 unsigned int transfer_size;
2088 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002089 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002090
2091 taskout = req_task->out_size;
2092 taskin = req_task->in_size;
2093 /* 130560 = 512 * 0xFF*/
2094 if (taskin > 130560 || taskout > 130560) {
2095 err = -EINVAL;
2096 goto abort;
2097 }
2098
2099 if (taskout) {
2100 outbuf = kzalloc(taskout, GFP_KERNEL);
2101 if (outbuf == NULL) {
2102 err = -ENOMEM;
2103 goto abort;
2104 }
2105 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2106 err = -EFAULT;
2107 goto abort;
2108 }
2109 outbuf_dma = pci_map_single(dd->pdev,
2110 outbuf,
2111 taskout,
2112 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002113 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002114 err = -ENOMEM;
2115 goto abort;
2116 }
2117 dma_buffer = outbuf_dma;
2118 }
2119
2120 if (taskin) {
2121 inbuf = kzalloc(taskin, GFP_KERNEL);
2122 if (inbuf == NULL) {
2123 err = -ENOMEM;
2124 goto abort;
2125 }
2126
2127 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2128 err = -EFAULT;
2129 goto abort;
2130 }
2131 inbuf_dma = pci_map_single(dd->pdev,
2132 inbuf,
2133 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002134 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002135 err = -ENOMEM;
2136 goto abort;
2137 }
2138 dma_buffer = inbuf_dma;
2139 }
2140
2141 /* only supports PIO and non-data commands from this ioctl. */
2142 switch (req_task->data_phase) {
2143 case TASKFILE_OUT:
2144 nsect = taskout / ATA_SECT_SIZE;
2145 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2146 break;
2147 case TASKFILE_IN:
2148 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2149 break;
2150 case TASKFILE_NO_DATA:
2151 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2152 break;
2153 default:
2154 err = -EINVAL;
2155 goto abort;
2156 }
2157
Sam Bradshaw88523a62011-08-30 08:34:26 -06002158 /* Build the FIS. */
2159 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2160
2161 fis.type = 0x27;
2162 fis.opts = 1 << 7;
2163 fis.command = req_task->io_ports[7];
2164 fis.features = req_task->io_ports[1];
2165 fis.sect_count = req_task->io_ports[2];
2166 fis.lba_low = req_task->io_ports[3];
2167 fis.lba_mid = req_task->io_ports[4];
2168 fis.lba_hi = req_task->io_ports[5];
2169 /* Clear the dev bit*/
2170 fis.device = req_task->io_ports[6] & ~0x10;
2171
2172 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2173 req_task->in_flags.all =
2174 IDE_TASKFILE_STD_IN_FLAGS |
2175 (IDE_HOB_STD_IN_FLAGS << 8);
2176 fis.lba_low_ex = req_task->hob_ports[3];
2177 fis.lba_mid_ex = req_task->hob_ports[4];
2178 fis.lba_hi_ex = req_task->hob_ports[5];
2179 fis.features_ex = req_task->hob_ports[1];
2180 fis.sect_cnt_ex = req_task->hob_ports[2];
2181
2182 } else {
2183 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2184 }
2185
2186 force_single_sector = implicit_sector(fis.command, fis.features);
2187
2188 if ((taskin || taskout) && (!fis.sect_count)) {
2189 if (nsect)
2190 fis.sect_count = nsect;
2191 else {
2192 if (!force_single_sector) {
2193 dev_warn(&dd->pdev->dev,
2194 "data movement but "
2195 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 err = -EINVAL;
2197 goto abort;
2198 }
2199 }
2200 }
2201
2202 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002203 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002204 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2205 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002206 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002207 fis.command,
2208 fis.features,
2209 fis.sect_count,
2210 fis.lba_low,
2211 fis.lba_mid,
2212 fis.lba_hi,
2213 fis.device);
2214
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002215 mtip_set_timeout(&fis, &timeout);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002216
2217 /* Determine the correct transfer size.*/
2218 if (force_single_sector)
2219 transfer_size = ATA_SECT_SIZE;
2220 else
2221 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2222
2223 /* Execute the command.*/
2224 if (mtip_exec_internal_command(dd->port,
2225 &fis,
2226 5,
2227 dma_buffer,
2228 transfer_size,
2229 0,
2230 GFP_KERNEL,
2231 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002232 err = -EIO;
2233 goto abort;
2234 }
2235
2236 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2237
2238 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2239 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2240 req_task->io_ports[7] = reply->control;
2241 } else {
2242 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2243 req_task->io_ports[7] = reply->command;
2244 }
2245
2246 /* reclaim the DMA buffers.*/
2247 if (inbuf_dma)
2248 pci_unmap_single(dd->pdev, inbuf_dma,
2249 taskin, DMA_FROM_DEVICE);
2250 if (outbuf_dma)
2251 pci_unmap_single(dd->pdev, outbuf_dma,
2252 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002253 inbuf_dma = 0;
2254 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002255
2256 /* return the ATA registers to the caller.*/
2257 req_task->io_ports[1] = reply->features;
2258 req_task->io_ports[2] = reply->sect_count;
2259 req_task->io_ports[3] = reply->lba_low;
2260 req_task->io_ports[4] = reply->lba_mid;
2261 req_task->io_ports[5] = reply->lba_hi;
2262 req_task->io_ports[6] = reply->device;
2263
2264 if (req_task->out_flags.all & 1) {
2265
2266 req_task->hob_ports[3] = reply->lba_low_ex;
2267 req_task->hob_ports[4] = reply->lba_mid_ex;
2268 req_task->hob_ports[5] = reply->lba_hi_ex;
2269 req_task->hob_ports[1] = reply->features_ex;
2270 req_task->hob_ports[2] = reply->sect_cnt_ex;
2271 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002272 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002273 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002274 "err %x, sect_cnt %x, lbalo %x,"
2275 "lbamid %x, lbahi %x, dev %x\n",
2276 __func__,
2277 req_task->io_ports[7],
2278 req_task->io_ports[1],
2279 req_task->io_ports[2],
2280 req_task->io_ports[3],
2281 req_task->io_ports[4],
2282 req_task->io_ports[5],
2283 req_task->io_ports[6]);
2284
Sam Bradshaw88523a62011-08-30 08:34:26 -06002285 if (taskout) {
2286 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2287 err = -EFAULT;
2288 goto abort;
2289 }
2290 }
2291 if (taskin) {
2292 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2293 err = -EFAULT;
2294 goto abort;
2295 }
2296 }
2297abort:
2298 if (inbuf_dma)
2299 pci_unmap_single(dd->pdev, inbuf_dma,
2300 taskin, DMA_FROM_DEVICE);
2301 if (outbuf_dma)
2302 pci_unmap_single(dd->pdev, outbuf_dma,
2303 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002304 kfree(outbuf);
2305 kfree(inbuf);
2306
2307 return err;
2308}
2309
2310/*
2311 * Handle IOCTL calls from the Block Layer.
2312 *
2313 * This function is called by the Block Layer when it receives an IOCTL
2314 * command that it does not understand. If the IOCTL command is not supported
2315 * this function returns -ENOTTY.
2316 *
2317 * @dd Pointer to the driver data structure.
2318 * @cmd IOCTL command passed from the Block Layer.
2319 * @arg IOCTL argument passed from the Block Layer.
2320 *
2321 * return value
2322 * 0 The IOCTL completed successfully.
2323 * -ENOTTY The specified command is not supported.
2324 * -EFAULT An error occurred copying data to a user space buffer.
2325 * -EIO An error occurred while executing the command.
2326 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002327static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2328 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002329{
2330 switch (cmd) {
2331 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002332 {
2333 if (copy_to_user((void __user *)arg, dd->port->identify,
2334 sizeof(u16) * ATA_ID_WORDS))
2335 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002336 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002337 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002338 case HDIO_DRIVE_CMD:
2339 {
2340 u8 drive_command[4];
2341
2342 /* Copy the user command info to our buffer. */
2343 if (copy_from_user(drive_command,
2344 (void __user *) arg,
2345 sizeof(drive_command)))
2346 return -EFAULT;
2347
2348 /* Execute the drive command. */
2349 if (exec_drive_command(dd->port,
2350 drive_command,
2351 (void __user *) (arg+4)))
2352 return -EIO;
2353
2354 /* Copy the status back to the users buffer. */
2355 if (copy_to_user((void __user *) arg,
2356 drive_command,
2357 sizeof(drive_command)))
2358 return -EFAULT;
2359
2360 break;
2361 }
2362 case HDIO_DRIVE_TASK:
2363 {
2364 u8 drive_command[7];
2365
2366 /* Copy the user command info to our buffer. */
2367 if (copy_from_user(drive_command,
2368 (void __user *) arg,
2369 sizeof(drive_command)))
2370 return -EFAULT;
2371
2372 /* Execute the drive command. */
2373 if (exec_drive_task(dd->port, drive_command))
2374 return -EIO;
2375
2376 /* Copy the status back to the users buffer. */
2377 if (copy_to_user((void __user *) arg,
2378 drive_command,
2379 sizeof(drive_command)))
2380 return -EFAULT;
2381
2382 break;
2383 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002384 case HDIO_DRIVE_TASKFILE: {
2385 ide_task_request_t req_task;
2386 int ret, outtotal;
2387
2388 if (copy_from_user(&req_task, (void __user *) arg,
2389 sizeof(req_task)))
2390 return -EFAULT;
2391
2392 outtotal = sizeof(req_task);
2393
2394 ret = exec_drive_taskfile(dd, (void __user *) arg,
2395 &req_task, outtotal);
2396
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002397 if (copy_to_user((void __user *) arg, &req_task,
2398 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002399 return -EFAULT;
2400
2401 return ret;
2402 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002403
2404 default:
2405 return -EINVAL;
2406 }
2407 return 0;
2408}
2409
2410/*
2411 * Submit an IO to the hw
2412 *
2413 * This function is called by the block layer to issue an io
2414 * to the device. Upon completion, the callback function will
2415 * be called with the data parameter passed as the callback data.
2416 *
2417 * @dd Pointer to the driver data structure.
2418 * @start First sector to read.
2419 * @nsect Number of sectors to read.
2420 * @nents Number of entries in scatter list for the read command.
2421 * @tag The tag of this read command.
2422 * @callback Pointer to the function that should be called
2423 * when the read completes.
2424 * @data Callback data passed to the callback function
2425 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002426 * @dir Direction (read or write)
2427 *
2428 * return value
2429 * None
2430 */
Jens Axboe63166682011-09-27 21:27:43 -06002431static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2432 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002433 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002434{
2435 struct host_to_dev_fis *fis;
2436 struct mtip_port *port = dd->port;
2437 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002438 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002439
2440 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002441 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002442
2443 command->scatter_ents = nents;
2444
2445 /*
2446 * The number of retries for this command before it is
2447 * reported as a failure to the upper layers.
2448 */
2449 command->retries = MTIP_MAX_RETRIES;
2450
2451 /* Fill out fis */
2452 fis = command->command;
2453 fis->type = 0x27;
2454 fis->opts = 1 << 7;
2455 fis->command =
2456 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002457 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2458 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002459 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002460 fis->features = nsect & 0xFF;
2461 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002462 fis->sect_count = ((tag << 3) | (tag >> 5));
2463 fis->sect_cnt_ex = 0;
2464 fis->control = 0;
2465 fis->res2 = 0;
2466 fis->res3 = 0;
2467 fill_command_sg(dd, command, nents);
2468
2469 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002470 command->command_header->opts =
2471 __force_bit2int cpu_to_le32(
2472 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002473 command->command_header->byte_count = 0;
2474
2475 /*
2476 * Set the completion function and data for the command
2477 * within this layer.
2478 */
2479 command->comp_data = dd;
2480 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002481 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002482
2483 /*
2484 * Set the completion function and data for the command passed
2485 * from the upper layer.
2486 */
2487 command->async_data = data;
2488 command->async_callback = callback;
2489
2490 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002491 * To prevent this command from being issued
2492 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002493 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002494 if (port->flags & MTIP_PF_PAUSE_IO) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002495 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002496 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002497 return;
2498 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002499
2500 /* Issue the command to the hardware */
2501 mtip_issue_ncq_command(port, tag);
2502
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002503 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002504}
2505
2506/*
2507 * Release a command slot.
2508 *
2509 * @dd Pointer to the driver data structure.
2510 * @tag Slot tag
2511 *
2512 * return value
2513 * None
2514 */
Jens Axboe63166682011-09-27 21:27:43 -06002515static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002516{
2517 release_slot(dd->port, tag);
2518}
2519
2520/*
2521 * Obtain a command slot and return its associated scatter list.
2522 *
2523 * @dd Pointer to the driver data structure.
2524 * @tag Pointer to an int that will receive the allocated command
2525 * slot tag.
2526 *
2527 * return value
2528 * Pointer to the scatter list for the allocated command slot
2529 * or NULL if no command slots are available.
2530 */
Jens Axboe63166682011-09-27 21:27:43 -06002531static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2532 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002533{
2534 /*
2535 * It is possible that, even with this semaphore, a thread
2536 * may think that no command slots are available. Therefore, we
2537 * need to make an attempt to get_slot().
2538 */
2539 down(&dd->port->cmd_slot);
2540 *tag = get_slot(dd->port);
2541
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002542 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002543 up(&dd->port->cmd_slot);
2544 return NULL;
2545 }
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002546 if (unlikely(*tag < 0)) {
2547 up(&dd->port->cmd_slot);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002548 return NULL;
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002549 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002550
2551 return dd->port->commands[*tag].sg;
2552}
2553
2554/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002555 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002556 *
2557 * @dev Pointer to the device structure, passed by the kernrel.
2558 * @attr Pointer to the device_attribute structure passed by the kernel.
2559 * @buf Pointer to the char buffer that will receive the stats info.
2560 *
2561 * return value
2562 * The size, in bytes, of the data copied into buf.
2563 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002564static ssize_t mtip_hw_show_status(struct device *dev,
2565 struct device_attribute *attr,
2566 char *buf)
2567{
2568 struct driver_data *dd = dev_to_disk(dev)->private_data;
2569 int size = 0;
2570
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002571 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002572 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002573 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002574 size += sprintf(buf, "%s", "write_protect\n");
2575 else
2576 size += sprintf(buf, "%s", "online\n");
2577
2578 return size;
2579}
2580
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002581static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002582
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002583static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2584 size_t len, loff_t *offset)
2585{
2586 struct driver_data *dd = (struct driver_data *)f->private_data;
2587 char buf[MTIP_DFS_MAX_BUF_SIZE];
2588 u32 group_allocated;
2589 int size = *offset;
2590 int n;
2591
2592 if (!len || size)
2593 return 0;
2594
2595 if (size < 0)
2596 return -EINVAL;
2597
2598 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2599
2600 for (n = dd->slot_groups-1; n >= 0; n--)
2601 size += sprintf(&buf[size], "%08X ",
2602 readl(dd->port->s_active[n]));
2603
2604 size += sprintf(&buf[size], "]\n");
2605 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2606
2607 for (n = dd->slot_groups-1; n >= 0; n--)
2608 size += sprintf(&buf[size], "%08X ",
2609 readl(dd->port->cmd_issue[n]));
2610
2611 size += sprintf(&buf[size], "]\n");
2612 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--)
2615 size += sprintf(&buf[size], "%08X ",
2616 readl(dd->port->completed[n]));
2617
2618 size += sprintf(&buf[size], "]\n");
2619 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2620 readl(dd->port->mmio + PORT_IRQ_STAT));
2621 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2622 readl(dd->mmio + HOST_IRQ_STAT));
2623 size += sprintf(&buf[size], "\n");
2624
2625 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2626
2627 for (n = dd->slot_groups-1; n >= 0; n--) {
2628 if (sizeof(long) > sizeof(u32))
2629 group_allocated =
2630 dd->port->allocated[n/2] >> (32*(n&1));
2631 else
2632 group_allocated = dd->port->allocated[n];
2633 size += sprintf(&buf[size], "%08X ", group_allocated);
2634 }
2635 size += sprintf(&buf[size], "]\n");
2636
2637 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2638
2639 for (n = dd->slot_groups-1; n >= 0; n--) {
2640 if (sizeof(long) > sizeof(u32))
2641 group_allocated =
2642 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2643 else
2644 group_allocated = dd->port->cmds_to_issue[n];
2645 size += sprintf(&buf[size], "%08X ", group_allocated);
2646 }
2647 size += sprintf(&buf[size], "]\n");
2648
2649 *offset = size <= len ? size : len;
2650 size = copy_to_user(ubuf, buf, *offset);
2651 if (size)
2652 return -EFAULT;
2653
2654 return *offset;
2655}
2656
2657static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2658 size_t len, loff_t *offset)
2659{
2660 struct driver_data *dd = (struct driver_data *)f->private_data;
2661 char buf[MTIP_DFS_MAX_BUF_SIZE];
2662 int size = *offset;
2663
2664 if (!len || size)
2665 return 0;
2666
2667 if (size < 0)
2668 return -EINVAL;
2669
2670 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2671 dd->port->flags);
2672 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2673 dd->dd_flag);
2674
2675 *offset = size <= len ? size : len;
2676 size = copy_to_user(ubuf, buf, *offset);
2677 if (size)
2678 return -EFAULT;
2679
2680 return *offset;
2681}
2682
2683static const struct file_operations mtip_regs_fops = {
2684 .owner = THIS_MODULE,
2685 .open = simple_open,
2686 .read = mtip_hw_read_registers,
2687 .llseek = no_llseek,
2688};
2689
2690static const struct file_operations mtip_flags_fops = {
2691 .owner = THIS_MODULE,
2692 .open = simple_open,
2693 .read = mtip_hw_read_flags,
2694 .llseek = no_llseek,
2695};
2696
Sam Bradshaw88523a62011-08-30 08:34:26 -06002697/*
2698 * Create the sysfs related attributes.
2699 *
2700 * @dd Pointer to the driver data structure.
2701 * @kobj Pointer to the kobj for the block device.
2702 *
2703 * return value
2704 * 0 Operation completed successfully.
2705 * -EINVAL Invalid parameter.
2706 */
Jens Axboe63166682011-09-27 21:27:43 -06002707static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002708{
2709 if (!kobj || !dd)
2710 return -EINVAL;
2711
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002712 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2713 dev_warn(&dd->pdev->dev,
2714 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002715 return 0;
2716}
2717
2718/*
2719 * Remove the sysfs related attributes.
2720 *
2721 * @dd Pointer to the driver data structure.
2722 * @kobj Pointer to the kobj for the block device.
2723 *
2724 * return value
2725 * 0 Operation completed successfully.
2726 * -EINVAL Invalid parameter.
2727 */
Jens Axboe63166682011-09-27 21:27:43 -06002728static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002729{
2730 if (!kobj || !dd)
2731 return -EINVAL;
2732
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002733 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002734
2735 return 0;
2736}
2737
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002738static int mtip_hw_debugfs_init(struct driver_data *dd)
2739{
2740 if (!dfs_parent)
2741 return -1;
2742
2743 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2744 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2745 dev_warn(&dd->pdev->dev,
2746 "Error creating node %s under debugfs\n",
2747 dd->disk->disk_name);
2748 dd->dfs_node = NULL;
2749 return -1;
2750 }
2751
2752 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2753 &mtip_flags_fops);
2754 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2755 &mtip_regs_fops);
2756
2757 return 0;
2758}
2759
2760static void mtip_hw_debugfs_exit(struct driver_data *dd)
2761{
2762 debugfs_remove_recursive(dd->dfs_node);
2763}
2764
2765
Sam Bradshaw88523a62011-08-30 08:34:26 -06002766/*
2767 * Perform any init/resume time hardware setup
2768 *
2769 * @dd Pointer to the driver data structure.
2770 *
2771 * return value
2772 * None
2773 */
2774static inline void hba_setup(struct driver_data *dd)
2775{
2776 u32 hwdata;
2777 hwdata = readl(dd->mmio + HOST_HSORG);
2778
2779 /* interrupt bug workaround: use only 1 IS bit.*/
2780 writel(hwdata |
2781 HSORG_DISABLE_SLOTGRP_INTR |
2782 HSORG_DISABLE_SLOTGRP_PXIS,
2783 dd->mmio + HOST_HSORG);
2784}
2785
2786/*
2787 * Detect the details of the product, and store anything needed
2788 * into the driver data structure. This includes product type and
2789 * version and number of slot groups.
2790 *
2791 * @dd Pointer to the driver data structure.
2792 *
2793 * return value
2794 * None
2795 */
2796static void mtip_detect_product(struct driver_data *dd)
2797{
2798 u32 hwdata;
2799 unsigned int rev, slotgroups;
2800
2801 /*
2802 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2803 * info register:
2804 * [15:8] hardware/software interface rev#
2805 * [ 3] asic-style interface
2806 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2807 */
2808 hwdata = readl(dd->mmio + HOST_HSORG);
2809
2810 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2811 dd->slot_groups = 1;
2812
2813 if (hwdata & 0x8) {
2814 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2815 rev = (hwdata & HSORG_HWREV) >> 8;
2816 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2817 dev_info(&dd->pdev->dev,
2818 "ASIC-FPGA design, HS rev 0x%x, "
2819 "%i slot groups [%i slots]\n",
2820 rev,
2821 slotgroups,
2822 slotgroups * 32);
2823
2824 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2825 dev_warn(&dd->pdev->dev,
2826 "Warning: driver only supports "
2827 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2828 slotgroups = MTIP_MAX_SLOT_GROUPS;
2829 }
2830 dd->slot_groups = slotgroups;
2831 return;
2832 }
2833
2834 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2835}
2836
2837/*
2838 * Blocking wait for FTL rebuild to complete
2839 *
2840 * @dd Pointer to the DRIVER_DATA structure.
2841 *
2842 * return value
2843 * 0 FTL rebuild completed successfully
2844 * -EFAULT FTL rebuild error/timeout/interruption
2845 */
2846static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2847{
2848 unsigned long timeout, cnt = 0, start;
2849
2850 dev_warn(&dd->pdev->dev,
2851 "FTL rebuild in progress. Polling for completion.\n");
2852
2853 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002854 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2855
2856 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002857 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002858 &dd->dd_flag)))
2859 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002860 if (mtip_check_surprise_removal(dd->pdev))
2861 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002862
Sam Bradshaw88523a62011-08-30 08:34:26 -06002863 if (mtip_get_identify(dd->port, NULL) < 0)
2864 return -EFAULT;
2865
2866 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2867 MTIP_FTL_REBUILD_MAGIC) {
2868 ssleep(1);
2869 /* Print message every 3 minutes */
2870 if (cnt++ >= 180) {
2871 dev_warn(&dd->pdev->dev,
2872 "FTL rebuild in progress (%d secs).\n",
2873 jiffies_to_msecs(jiffies - start) / 1000);
2874 cnt = 0;
2875 }
2876 } else {
2877 dev_warn(&dd->pdev->dev,
2878 "FTL rebuild complete (%d secs).\n",
2879 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002880 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002881 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002882 }
2883 ssleep(10);
2884 } while (time_before(jiffies, timeout));
2885
2886 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002887 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002888 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2889 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002890 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002891}
2892
2893/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002894 * service thread to issue queued commands
2895 *
2896 * @data Pointer to the driver data structure.
2897 *
2898 * return value
2899 * 0
2900 */
2901
2902static int mtip_service_thread(void *data)
2903{
2904 struct driver_data *dd = (struct driver_data *)data;
2905 unsigned long slot, slot_start, slot_wrap;
2906 unsigned int num_cmd_slots = dd->slot_groups * 32;
2907 struct mtip_port *port = dd->port;
2908
2909 while (1) {
2910 /*
2911 * the condition is to check neither an internal command is
2912 * is in progress nor error handling is active
2913 */
2914 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002915 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002916
2917 if (kthread_should_stop())
2918 break;
2919
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002920 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002921 &dd->dd_flag)))
2922 break;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002923
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002924 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2925 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002926 slot = 1;
2927 /* used to restrict the loop to one iteration */
2928 slot_start = num_cmd_slots;
2929 slot_wrap = 0;
2930 while (1) {
2931 slot = find_next_bit(port->cmds_to_issue,
2932 num_cmd_slots, slot);
2933 if (slot_wrap == 1) {
2934 if ((slot_start >= slot) ||
2935 (slot >= num_cmd_slots))
2936 break;
2937 }
2938 if (unlikely(slot_start == num_cmd_slots))
2939 slot_start = slot;
2940
2941 if (unlikely(slot == num_cmd_slots)) {
2942 slot = 1;
2943 slot_wrap = 1;
2944 continue;
2945 }
2946
2947 /* Issue the command to the hardware */
2948 mtip_issue_ncq_command(port, slot);
2949
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002950 clear_bit(slot, port->cmds_to_issue);
2951 }
2952
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002953 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2954 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02002955 if (!mtip_ftl_rebuild_poll(dd))
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002956 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002957 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002958 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002959 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002960 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002961
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002962 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002963 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002964 }
2965 return 0;
2966}
2967
2968/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002969 * Called once for each card.
2970 *
2971 * @dd Pointer to the driver data structure.
2972 *
2973 * return value
2974 * 0 on success, else an error code.
2975 */
Jens Axboe63166682011-09-27 21:27:43 -06002976static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002977{
2978 int i;
2979 int rv;
2980 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002981 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002982 unsigned char *buf;
2983 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002984
2985 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2986
2987 mtip_detect_product(dd);
2988 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2989 rv = -EIO;
2990 goto out1;
2991 }
2992 num_command_slots = dd->slot_groups * 32;
2993
2994 hba_setup(dd);
2995
Sam Bradshaw88523a62011-08-30 08:34:26 -06002996 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2997
2998 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2999 if (!dd->port) {
3000 dev_err(&dd->pdev->dev,
3001 "Memory allocation: port structure\n");
3002 return -ENOMEM;
3003 }
3004
3005 /* Counting semaphore to track command slot usage */
3006 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
3007
3008 /* Spinlock to prevent concurrent issue */
3009 spin_lock_init(&dd->port->cmd_issue_lock);
3010
3011 /* Set the port mmio base address. */
3012 dd->port->mmio = dd->mmio + PORT_OFFSET;
3013 dd->port->dd = dd;
3014
3015 /* Allocate memory for the command list. */
3016 dd->port->command_list =
3017 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003018 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003019 &dd->port->command_list_dma,
3020 GFP_KERNEL);
3021 if (!dd->port->command_list) {
3022 dev_err(&dd->pdev->dev,
3023 "Memory allocation: command list\n");
3024 rv = -ENOMEM;
3025 goto out1;
3026 }
3027
3028 /* Clear the memory we have allocated. */
3029 memset(dd->port->command_list,
3030 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003031 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003032
3033 /* Setup the addresse of the RX FIS. */
3034 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3035 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3036
3037 /* Setup the address of the command tables. */
3038 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3039 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3040
3041 /* Setup the address of the identify data. */
3042 dd->port->identify = dd->port->command_table +
3043 HW_CMD_TBL_AR_SZ;
3044 dd->port->identify_dma = dd->port->command_tbl_dma +
3045 HW_CMD_TBL_AR_SZ;
3046
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003047 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003048 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3049 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3050
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003051 /* Setup the address of the log buf - for read log command */
3052 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3053 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3054
3055 /* Setup the address of the smart buf - for smart read data command */
3056 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3057 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3058
3059
Sam Bradshaw88523a62011-08-30 08:34:26 -06003060 /* Point the command headers at the command tables. */
3061 for (i = 0; i < num_command_slots; i++) {
3062 dd->port->commands[i].command_header =
3063 dd->port->command_list +
3064 (sizeof(struct mtip_cmd_hdr) * i);
3065 dd->port->commands[i].command_header_dma =
3066 dd->port->command_list_dma +
3067 (sizeof(struct mtip_cmd_hdr) * i);
3068
3069 dd->port->commands[i].command =
3070 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3071 dd->port->commands[i].command_dma =
3072 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3073
3074 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3075 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003076 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06003077 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003078 dd->port->commands[i].command_header->ctba =
3079 __force_bit2int cpu_to_le32(
3080 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003081
3082 /*
3083 * If this is not done, a bug is reported by the stock
3084 * FC11 i386. Due to the fact that it has lots of kernel
3085 * debugging enabled.
3086 */
3087 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3088
3089 /* Mark all commands as currently inactive.*/
3090 atomic_set(&dd->port->commands[i].active, 0);
3091 }
3092
3093 /* Setup the pointers to the extended s_active and CI registers. */
3094 for (i = 0; i < dd->slot_groups; i++) {
3095 dd->port->s_active[i] =
3096 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3097 dd->port->cmd_issue[i] =
3098 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3099 dd->port->completed[i] =
3100 dd->port->mmio + i*0x80 + PORT_SDBV;
3101 }
3102
Asai Thambi S P45038362012-04-09 08:35:38 +02003103 timetaken = jiffies;
3104 timeout = jiffies + msecs_to_jiffies(30000);
3105 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3106 time_before(jiffies, timeout)) {
3107 mdelay(100);
3108 }
3109 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3110 timetaken = jiffies - timetaken;
3111 dev_warn(&dd->pdev->dev,
3112 "Surprise removal detected at %u ms\n",
3113 jiffies_to_msecs(timetaken));
3114 rv = -ENODEV;
3115 goto out2 ;
3116 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003117 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003118 timetaken = jiffies - timetaken;
3119 dev_warn(&dd->pdev->dev,
3120 "Removal detected at %u ms\n",
3121 jiffies_to_msecs(timetaken));
3122 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003123 goto out2;
3124 }
3125
Asai Thambi S P45038362012-04-09 08:35:38 +02003126 /* Conditionally reset the HBA. */
3127 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3128 if (mtip_hba_reset(dd) < 0) {
3129 dev_err(&dd->pdev->dev,
3130 "Card did not reset within timeout\n");
3131 rv = -EIO;
3132 goto out2;
3133 }
3134 } else {
3135 /* Clear any pending interrupts on the HBA */
3136 writel(readl(dd->mmio + HOST_IRQ_STAT),
3137 dd->mmio + HOST_IRQ_STAT);
3138 }
3139
Sam Bradshaw88523a62011-08-30 08:34:26 -06003140 mtip_init_port(dd->port);
3141 mtip_start_port(dd->port);
3142
3143 /* Setup the ISR and enable interrupts. */
3144 rv = devm_request_irq(&dd->pdev->dev,
3145 dd->pdev->irq,
3146 mtip_irq_handler,
3147 IRQF_SHARED,
3148 dev_driver_string(&dd->pdev->dev),
3149 dd);
3150
3151 if (rv) {
3152 dev_err(&dd->pdev->dev,
3153 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3154 goto out2;
3155 }
3156
3157 /* Enable interrupts on the HBA. */
3158 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3159 dd->mmio + HOST_CTL);
3160
3161 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003162 init_waitqueue_head(&dd->port->svc_wait);
3163
Sam Bradshaw88523a62011-08-30 08:34:26 -06003164 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3165 dd->port->cmd_timer.function = mtip_timeout_function;
3166 mod_timer(&dd->port->cmd_timer,
3167 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3168
Asai Thambi S P45038362012-04-09 08:35:38 +02003169
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003170 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003171 rv = -EFAULT;
3172 goto out3;
3173 }
3174
Sam Bradshaw88523a62011-08-30 08:34:26 -06003175 if (mtip_get_identify(dd->port, NULL) < 0) {
3176 rv = -EFAULT;
3177 goto out3;
3178 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003179
3180 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3181 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003182 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003183 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003184 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003185 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003186
3187 /* check write protect, over temp and rebuild statuses */
3188 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3189 dd->port->log_buf,
3190 dd->port->log_buf_dma, 1);
3191 if (rv) {
3192 dev_warn(&dd->pdev->dev,
3193 "Error in READ LOG EXT (10h) command\n");
3194 /* non-critical error, don't fail the load */
3195 } else {
3196 buf = (unsigned char *)dd->port->log_buf;
3197 if (buf[259] & 0x1) {
3198 dev_info(&dd->pdev->dev,
3199 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003200 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003201 }
3202 if (buf[288] == 0xF7) {
3203 dev_info(&dd->pdev->dev,
3204 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003205 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003206 }
3207 if (buf[288] == 0xBF) {
3208 dev_info(&dd->pdev->dev,
3209 "Drive indicates rebuild has failed.\n");
3210 /* TODO */
3211 }
3212 }
3213
3214 /* get write protect progess */
3215 memset(&attr242, 0, sizeof(struct smart_attr));
3216 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3217 dev_warn(&dd->pdev->dev,
3218 "Unable to check write protect progress\n");
3219 else
3220 dev_info(&dd->pdev->dev,
3221 "Write protect progress: %d%% (%d blocks)\n",
3222 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003223 return rv;
3224
3225out3:
3226 del_timer_sync(&dd->port->cmd_timer);
3227
3228 /* Disable interrupts on the HBA. */
3229 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3230 dd->mmio + HOST_CTL);
3231
3232 /*Release the IRQ. */
3233 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3234
3235out2:
3236 mtip_deinit_port(dd->port);
3237
3238 /* Free the command/command header memory. */
3239 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003240 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003241 dd->port->command_list,
3242 dd->port->command_list_dma);
3243out1:
3244 /* Free the memory allocated for the for structure. */
3245 kfree(dd->port);
3246
3247 return rv;
3248}
3249
3250/*
3251 * Called to deinitialize an interface.
3252 *
3253 * @dd Pointer to the driver data structure.
3254 *
3255 * return value
3256 * 0
3257 */
Jens Axboe63166682011-09-27 21:27:43 -06003258static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003259{
3260 /*
3261 * Send standby immediate (E0h) to the drive so that it
3262 * saves its state.
3263 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003264 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003265
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003266 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003267 if (mtip_standby_immediate(dd->port))
3268 dev_warn(&dd->pdev->dev,
3269 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003270
3271 /* de-initialize the port. */
3272 mtip_deinit_port(dd->port);
3273
3274 /* Disable interrupts on the HBA. */
3275 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3276 dd->mmio + HOST_CTL);
3277 }
3278
3279 del_timer_sync(&dd->port->cmd_timer);
3280
Sam Bradshaw88523a62011-08-30 08:34:26 -06003281 /* Release the IRQ. */
3282 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3283
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003284 /* Stop the bottom half tasklet. */
3285 tasklet_kill(&dd->tasklet);
3286
Sam Bradshaw88523a62011-08-30 08:34:26 -06003287 /* Free the command/command header memory. */
3288 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003289 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003290 dd->port->command_list,
3291 dd->port->command_list_dma);
3292 /* Free the memory allocated for the for structure. */
3293 kfree(dd->port);
3294
3295 return 0;
3296}
3297
3298/*
3299 * Issue a Standby Immediate command to the device.
3300 *
3301 * This function is called by the Block Layer just before the
3302 * system powers off during a shutdown.
3303 *
3304 * @dd Pointer to the driver data structure.
3305 *
3306 * return value
3307 * 0
3308 */
Jens Axboe63166682011-09-27 21:27:43 -06003309static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003310{
3311 /*
3312 * Send standby immediate (E0h) to the drive so that it
3313 * saves its state.
3314 */
3315 mtip_standby_immediate(dd->port);
3316
3317 return 0;
3318}
3319
3320/*
3321 * Suspend function
3322 *
3323 * This function is called by the Block Layer just before the
3324 * system hibernates.
3325 *
3326 * @dd Pointer to the driver data structure.
3327 *
3328 * return value
3329 * 0 Suspend was successful
3330 * -EFAULT Suspend was not successful
3331 */
Jens Axboe63166682011-09-27 21:27:43 -06003332static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003333{
3334 /*
3335 * Send standby immediate (E0h) to the drive
3336 * so that it saves its state.
3337 */
3338 if (mtip_standby_immediate(dd->port) != 0) {
3339 dev_err(&dd->pdev->dev,
3340 "Failed standby-immediate command\n");
3341 return -EFAULT;
3342 }
3343
3344 /* Disable interrupts on the HBA.*/
3345 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3346 dd->mmio + HOST_CTL);
3347 mtip_deinit_port(dd->port);
3348
3349 return 0;
3350}
3351
3352/*
3353 * Resume function
3354 *
3355 * This function is called by the Block Layer as the
3356 * system resumes.
3357 *
3358 * @dd Pointer to the driver data structure.
3359 *
3360 * return value
3361 * 0 Resume was successful
3362 * -EFAULT Resume was not successful
3363 */
Jens Axboe63166682011-09-27 21:27:43 -06003364static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003365{
3366 /* Perform any needed hardware setup steps */
3367 hba_setup(dd);
3368
3369 /* Reset the HBA */
3370 if (mtip_hba_reset(dd) != 0) {
3371 dev_err(&dd->pdev->dev,
3372 "Unable to reset the HBA\n");
3373 return -EFAULT;
3374 }
3375
3376 /*
3377 * Enable the port, DMA engine, and FIS reception specific
3378 * h/w in controller.
3379 */
3380 mtip_init_port(dd->port);
3381 mtip_start_port(dd->port);
3382
3383 /* Enable interrupts on the HBA.*/
3384 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3385 dd->mmio + HOST_CTL);
3386
3387 return 0;
3388}
3389
3390/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003391 * Helper function for reusing disk name
3392 * upon hot insertion.
3393 */
3394static int rssd_disk_name_format(char *prefix,
3395 int index,
3396 char *buf,
3397 int buflen)
3398{
3399 const int base = 'z' - 'a' + 1;
3400 char *begin = buf + strlen(prefix);
3401 char *end = buf + buflen;
3402 char *p;
3403 int unit;
3404
3405 p = end - 1;
3406 *p = '\0';
3407 unit = base;
3408 do {
3409 if (p == begin)
3410 return -EINVAL;
3411 *--p = 'a' + (index % unit);
3412 index = (index / unit) - 1;
3413 } while (index >= 0);
3414
3415 memmove(begin, p, end - p);
3416 memcpy(buf, prefix, strlen(prefix));
3417
3418 return 0;
3419}
3420
3421/*
3422 * Block layer IOCTL handler.
3423 *
3424 * @dev Pointer to the block_device structure.
3425 * @mode ignored
3426 * @cmd IOCTL command passed from the user application.
3427 * @arg Argument passed from the user application.
3428 *
3429 * return value
3430 * 0 IOCTL completed successfully.
3431 * -ENOTTY IOCTL not supported or invalid driver data
3432 * structure pointer.
3433 */
3434static int mtip_block_ioctl(struct block_device *dev,
3435 fmode_t mode,
3436 unsigned cmd,
3437 unsigned long arg)
3438{
3439 struct driver_data *dd = dev->bd_disk->private_data;
3440
3441 if (!capable(CAP_SYS_ADMIN))
3442 return -EACCES;
3443
3444 if (!dd)
3445 return -ENOTTY;
3446
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003447 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003448 return -ENOTTY;
3449
Sam Bradshaw88523a62011-08-30 08:34:26 -06003450 switch (cmd) {
3451 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003452 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003453 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003454 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003455 }
3456}
3457
Jens Axboe16d02c02011-09-27 15:50:01 -06003458#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003459/*
3460 * Block layer compat IOCTL handler.
3461 *
3462 * @dev Pointer to the block_device structure.
3463 * @mode ignored
3464 * @cmd IOCTL command passed from the user application.
3465 * @arg Argument passed from the user application.
3466 *
3467 * return value
3468 * 0 IOCTL completed successfully.
3469 * -ENOTTY IOCTL not supported or invalid driver data
3470 * structure pointer.
3471 */
3472static int mtip_block_compat_ioctl(struct block_device *dev,
3473 fmode_t mode,
3474 unsigned cmd,
3475 unsigned long arg)
3476{
3477 struct driver_data *dd = dev->bd_disk->private_data;
3478
3479 if (!capable(CAP_SYS_ADMIN))
3480 return -EACCES;
3481
3482 if (!dd)
3483 return -ENOTTY;
3484
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003485 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003486 return -ENOTTY;
3487
Sam Bradshaw88523a62011-08-30 08:34:26 -06003488 switch (cmd) {
3489 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003490 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003491 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003492 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003493 ide_task_request_t req_task;
3494 int compat_tasksize, outtotal, ret;
3495
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003496 compat_tasksize =
3497 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003498
3499 compat_req_task =
3500 (struct mtip_compat_ide_task_request_s __user *) arg;
3501
3502 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003503 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003504 return -EFAULT;
3505
3506 if (get_user(req_task.out_size, &compat_req_task->out_size))
3507 return -EFAULT;
3508
3509 if (get_user(req_task.in_size, &compat_req_task->in_size))
3510 return -EFAULT;
3511
3512 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3513
3514 ret = exec_drive_taskfile(dd, (void __user *) arg,
3515 &req_task, outtotal);
3516
3517 if (copy_to_user((void __user *) arg, &req_task,
3518 compat_tasksize -
3519 (2 * sizeof(compat_long_t))))
3520 return -EFAULT;
3521
3522 if (put_user(req_task.out_size, &compat_req_task->out_size))
3523 return -EFAULT;
3524
3525 if (put_user(req_task.in_size, &compat_req_task->in_size))
3526 return -EFAULT;
3527
3528 return ret;
3529 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003530 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003531 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003532 }
3533}
Jens Axboe16d02c02011-09-27 15:50:01 -06003534#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003535
3536/*
3537 * Obtain the geometry of the device.
3538 *
3539 * You may think that this function is obsolete, but some applications,
3540 * fdisk for example still used CHS values. This function describes the
3541 * device as having 224 heads and 56 sectors per cylinder. These values are
3542 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3543 * partition is described in terms of a start and end cylinder this means
3544 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3545 * affects performance.
3546 *
3547 * @dev Pointer to the block_device strucutre.
3548 * @geo Pointer to a hd_geometry structure.
3549 *
3550 * return value
3551 * 0 Operation completed successfully.
3552 * -ENOTTY An error occurred while reading the drive capacity.
3553 */
3554static int mtip_block_getgeo(struct block_device *dev,
3555 struct hd_geometry *geo)
3556{
3557 struct driver_data *dd = dev->bd_disk->private_data;
3558 sector_t capacity;
3559
3560 if (!dd)
3561 return -ENOTTY;
3562
3563 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3564 dev_warn(&dd->pdev->dev,
3565 "Could not get drive capacity.\n");
3566 return -ENOTTY;
3567 }
3568
3569 geo->heads = 224;
3570 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003571 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003572 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003573 return 0;
3574}
3575
3576/*
3577 * Block device operation function.
3578 *
3579 * This structure contains pointers to the functions required by the block
3580 * layer.
3581 */
3582static const struct block_device_operations mtip_block_ops = {
3583 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003584#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003585 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003586#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003587 .getgeo = mtip_block_getgeo,
3588 .owner = THIS_MODULE
3589};
3590
3591/*
3592 * Block layer make request function.
3593 *
3594 * This function is called by the kernel to process a BIO for
3595 * the P320 device.
3596 *
3597 * @queue Pointer to the request queue. Unused other than to obtain
3598 * the driver data structure.
3599 * @bio Pointer to the BIO.
3600 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003601 */
Jens Axboea71f4832011-11-05 08:36:21 +01003602static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003603{
3604 struct driver_data *dd = queue->queuedata;
3605 struct scatterlist *sg;
3606 struct bio_vec *bvec;
3607 int nents = 0;
3608 int tag = 0;
3609
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003610 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3611 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3612 &dd->dd_flag))) {
3613 bio_endio(bio, -ENXIO);
3614 return;
3615 }
3616 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3617 bio_endio(bio, -ENODATA);
3618 return;
3619 }
3620 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3621 &dd->dd_flag) &&
3622 bio_data_dir(bio))) {
3623 bio_endio(bio, -ENODATA);
3624 return;
3625 }
Asai Thambi S P12a166c2012-09-05 22:01:36 +05303626 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3627 bio_endio(bio, -ENODATA);
3628 return;
3629 }
Asai Thambi S P45038362012-04-09 08:35:38 +02003630 }
3631
Sam Bradshaw88523a62011-08-30 08:34:26 -06003632 if (unlikely(!bio_has_data(bio))) {
3633 blk_queue_flush(queue, 0);
3634 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003635 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003636 }
3637
Sam Bradshaw88523a62011-08-30 08:34:26 -06003638 sg = mtip_hw_get_scatterlist(dd, &tag);
3639 if (likely(sg != NULL)) {
3640 blk_queue_bounce(queue, &bio);
3641
3642 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3643 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003644 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003645 bio_io_error(bio);
3646 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003647 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003648 }
3649
3650 /* Create the scatter list for this bio. */
3651 bio_for_each_segment(bvec, bio, nents) {
3652 sg_set_page(&sg[nents],
3653 bvec->bv_page,
3654 bvec->bv_len,
3655 bvec->bv_offset);
3656 }
3657
3658 /* Issue the read/write. */
3659 mtip_hw_submit_io(dd,
3660 bio->bi_sector,
3661 bio_sectors(bio),
3662 nents,
3663 tag,
3664 bio_endio,
3665 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003666 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003667 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003668 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003669}
3670
3671/*
3672 * Block layer initialization function.
3673 *
3674 * This function is called once by the PCI layer for each P320
3675 * device that is connected to the system.
3676 *
3677 * @dd Pointer to the driver data structure.
3678 *
3679 * return value
3680 * 0 on success else an error code.
3681 */
Jens Axboe63166682011-09-27 21:27:43 -06003682static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003683{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003684 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003685 sector_t capacity;
3686 unsigned int index = 0;
3687 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003688 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003689
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003690 if (dd->disk)
3691 goto skip_create_disk; /* hw init done, before rebuild */
3692
Sam Bradshaw88523a62011-08-30 08:34:26 -06003693 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003694 wait_for_rebuild = mtip_hw_init(dd);
3695 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003696 dev_err(&dd->pdev->dev,
3697 "Protocol layer initialization failed\n");
3698 rv = -EINVAL;
3699 goto protocol_init_error;
3700 }
3701
Sam Bradshaw88523a62011-08-30 08:34:26 -06003702 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3703 if (dd->disk == NULL) {
3704 dev_err(&dd->pdev->dev,
3705 "Unable to allocate gendisk structure\n");
3706 rv = -EINVAL;
3707 goto alloc_disk_error;
3708 }
3709
3710 /* Generate the disk name, implemented same as in sd.c */
3711 do {
3712 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3713 goto ida_get_error;
3714
3715 spin_lock(&rssd_index_lock);
3716 rv = ida_get_new(&rssd_index_ida, &index);
3717 spin_unlock(&rssd_index_lock);
3718 } while (rv == -EAGAIN);
3719
3720 if (rv)
3721 goto ida_get_error;
3722
3723 rv = rssd_disk_name_format("rssd",
3724 index,
3725 dd->disk->disk_name,
3726 DISK_NAME_LEN);
3727 if (rv)
3728 goto disk_index_error;
3729
3730 dd->disk->driverfs_dev = &dd->pdev->dev;
3731 dd->disk->major = dd->major;
3732 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3733 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003734 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003735 dd->index = index;
3736
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003737 /*
3738 * if rebuild pending, start the service thread, and delay the block
3739 * queue creation and add_disk()
3740 */
3741 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3742 goto start_service_thread;
3743
3744skip_create_disk:
3745 /* Allocate the request queue. */
3746 dd->queue = blk_alloc_queue(GFP_KERNEL);
3747 if (dd->queue == NULL) {
3748 dev_err(&dd->pdev->dev,
3749 "Unable to allocate request queue\n");
3750 rv = -ENOMEM;
3751 goto block_queue_alloc_init_error;
3752 }
3753
3754 /* Attach our request function to the request queue. */
3755 blk_queue_make_request(dd->queue, mtip_make_request);
3756
3757 dd->disk->queue = dd->queue;
3758 dd->queue->queuedata = dd;
3759
3760 /* Set device limits. */
3761 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3762 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3763 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003764 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3765 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003766 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003767
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003768 /*
3769 * write back cache is not supported in the device. FUA depends on
3770 * write back cache support, hence setting flush support to zero.
3771 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003772 blk_queue_flush(dd->queue, 0);
3773
Sam Bradshaw88523a62011-08-30 08:34:26 -06003774 /* Set the capacity of the device in 512 byte sectors. */
3775 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3776 dev_warn(&dd->pdev->dev,
3777 "Could not read drive capacity\n");
3778 rv = -EIO;
3779 goto read_capacity_error;
3780 }
3781 set_capacity(dd->disk, capacity);
3782
3783 /* Enable the block device and add it to /dev */
3784 add_disk(dd->disk);
3785
3786 /*
3787 * Now that the disk is active, initialize any sysfs attributes
3788 * managed by the protocol layer.
3789 */
3790 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3791 if (kobj) {
3792 mtip_hw_sysfs_init(dd, kobj);
3793 kobject_put(kobj);
3794 }
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003795 mtip_hw_debugfs_init(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003796
Asai Thambi S P45038362012-04-09 08:35:38 +02003797 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003798 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003799 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003800 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003801
3802start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003803 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3804
3805 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3806 dd, thd_name);
3807
3808 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003809 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003810 dd->mtip_svc_handler = NULL;
3811 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003812 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003813 }
3814
Asai Thambi S P45038362012-04-09 08:35:38 +02003815 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3816 rv = wait_for_rebuild;
3817
Sam Bradshaw88523a62011-08-30 08:34:26 -06003818 return rv;
3819
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003820kthread_run_error:
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003821 mtip_hw_debugfs_exit(dd);
3822
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003823 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003824 del_gendisk(dd->disk);
3825
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003826read_capacity_error:
3827 blk_cleanup_queue(dd->queue);
3828
3829block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003830disk_index_error:
3831 spin_lock(&rssd_index_lock);
3832 ida_remove(&rssd_index_ida, index);
3833 spin_unlock(&rssd_index_lock);
3834
3835ida_get_error:
3836 put_disk(dd->disk);
3837
3838alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003839 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003840
3841protocol_init_error:
3842 return rv;
3843}
3844
3845/*
3846 * Block layer deinitialization function.
3847 *
3848 * Called by the PCI layer as each P320 device is removed.
3849 *
3850 * @dd Pointer to the driver data structure.
3851 *
3852 * return value
3853 * 0
3854 */
Jens Axboe63166682011-09-27 21:27:43 -06003855static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003856{
3857 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003858
3859 if (dd->mtip_svc_handler) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003860 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003861 wake_up_interruptible(&dd->port->svc_wait);
3862 kthread_stop(dd->mtip_svc_handler);
3863 }
3864
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003865 /* Clean up the sysfs attributes, if created */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003866 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003867 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3868 if (kobj) {
3869 mtip_hw_sysfs_exit(dd, kobj);
3870 kobject_put(kobj);
3871 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003872 }
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003873 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003874
3875 /*
3876 * Delete our gendisk structure. This also removes the device
3877 * from /dev
3878 */
3879 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003880
3881 spin_lock(&rssd_index_lock);
3882 ida_remove(&rssd_index_ida, dd->index);
3883 spin_unlock(&rssd_index_lock);
3884
Sam Bradshaw88523a62011-08-30 08:34:26 -06003885 blk_cleanup_queue(dd->queue);
3886 dd->disk = NULL;
3887 dd->queue = NULL;
3888
3889 /* De-initialize the protocol layer. */
3890 mtip_hw_exit(dd);
3891
3892 return 0;
3893}
3894
3895/*
3896 * Function called by the PCI layer when just before the
3897 * machine shuts down.
3898 *
3899 * If a protocol layer shutdown function is present it will be called
3900 * by this function.
3901 *
3902 * @dd Pointer to the driver data structure.
3903 *
3904 * return value
3905 * 0
3906 */
Jens Axboe63166682011-09-27 21:27:43 -06003907static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003908{
3909 dev_info(&dd->pdev->dev,
3910 "Shutting down %s ...\n", dd->disk->disk_name);
3911
3912 /* Delete our gendisk structure, and cleanup the blk queue. */
3913 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003914
3915 spin_lock(&rssd_index_lock);
3916 ida_remove(&rssd_index_ida, dd->index);
3917 spin_unlock(&rssd_index_lock);
3918
Sam Bradshaw88523a62011-08-30 08:34:26 -06003919 blk_cleanup_queue(dd->queue);
3920 dd->disk = NULL;
3921 dd->queue = NULL;
3922
3923 mtip_hw_shutdown(dd);
3924 return 0;
3925}
3926
Jens Axboe63166682011-09-27 21:27:43 -06003927static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003928{
3929 dev_info(&dd->pdev->dev,
3930 "Suspending %s ...\n", dd->disk->disk_name);
3931 mtip_hw_suspend(dd);
3932 return 0;
3933}
3934
Jens Axboe63166682011-09-27 21:27:43 -06003935static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003936{
3937 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3938 dd->disk->disk_name);
3939 mtip_hw_resume(dd);
3940 return 0;
3941}
3942
3943/*
3944 * Called for each supported PCI device detected.
3945 *
3946 * This function allocates the private data structure, enables the
3947 * PCI device and then calls the block layer initialization function.
3948 *
3949 * return value
3950 * 0 on success else an error code.
3951 */
3952static int mtip_pci_probe(struct pci_dev *pdev,
3953 const struct pci_device_id *ent)
3954{
3955 int rv = 0;
3956 struct driver_data *dd = NULL;
3957
3958 /* Allocate memory for this devices private data. */
3959 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3960 if (dd == NULL) {
3961 dev_err(&pdev->dev,
3962 "Unable to allocate memory for driver data\n");
3963 return -ENOMEM;
3964 }
3965
Sam Bradshaw88523a62011-08-30 08:34:26 -06003966 /* Attach the private data to this PCI device. */
3967 pci_set_drvdata(pdev, dd);
3968
3969 rv = pcim_enable_device(pdev);
3970 if (rv < 0) {
3971 dev_err(&pdev->dev, "Unable to enable device\n");
3972 goto iomap_err;
3973 }
3974
3975 /* Map BAR5 to memory. */
3976 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3977 if (rv < 0) {
3978 dev_err(&pdev->dev, "Unable to map regions\n");
3979 goto iomap_err;
3980 }
3981
3982 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3983 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3984
3985 if (rv) {
3986 rv = pci_set_consistent_dma_mask(pdev,
3987 DMA_BIT_MASK(32));
3988 if (rv) {
3989 dev_warn(&pdev->dev,
3990 "64-bit DMA enable failed\n");
3991 goto setmask_err;
3992 }
3993 }
3994 }
3995
3996 pci_set_master(pdev);
3997
3998 if (pci_enable_msi(pdev)) {
3999 dev_warn(&pdev->dev,
4000 "Unable to enable MSI interrupt.\n");
4001 goto block_initialize_err;
4002 }
4003
4004 /* Copy the info we may need later into the private data structure. */
4005 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004006 dd->instance = instance;
4007 dd->pdev = pdev;
4008
4009 /* Initialize the block layer. */
4010 rv = mtip_block_initialize(dd);
4011 if (rv < 0) {
4012 dev_err(&pdev->dev,
4013 "Unable to initialize block layer\n");
4014 goto block_initialize_err;
4015 }
4016
4017 /*
4018 * Increment the instance count so that each device has a unique
4019 * instance number.
4020 */
4021 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004022 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004023 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004024 goto done;
4025
4026block_initialize_err:
4027 pci_disable_msi(pdev);
4028
4029setmask_err:
4030 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4031
4032iomap_err:
4033 kfree(dd);
4034 pci_set_drvdata(pdev, NULL);
4035 return rv;
4036done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004037 return rv;
4038}
4039
4040/*
4041 * Called for each probed device when the device is removed or the
4042 * driver is unloaded.
4043 *
4044 * return value
4045 * None
4046 */
4047static void mtip_pci_remove(struct pci_dev *pdev)
4048{
4049 struct driver_data *dd = pci_get_drvdata(pdev);
4050 int counter = 0;
4051
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004052 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004053
Sam Bradshaw88523a62011-08-30 08:34:26 -06004054 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004055 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004056 counter++;
4057 msleep(20);
4058 if (counter == 10) {
4059 /* Cleanup the outstanding commands */
4060 mtip_command_cleanup(dd);
4061 break;
4062 }
4063 }
4064 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004065
4066 /* Clean up the block layer. */
4067 mtip_block_remove(dd);
4068
4069 pci_disable_msi(pdev);
4070
4071 kfree(dd);
4072 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4073}
4074
4075/*
4076 * Called for each probed device when the device is suspended.
4077 *
4078 * return value
4079 * 0 Success
4080 * <0 Error
4081 */
4082static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4083{
4084 int rv = 0;
4085 struct driver_data *dd = pci_get_drvdata(pdev);
4086
4087 if (!dd) {
4088 dev_err(&pdev->dev,
4089 "Driver private datastructure is NULL\n");
4090 return -EFAULT;
4091 }
4092
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004093 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004094
4095 /* Disable ports & interrupts then send standby immediate */
4096 rv = mtip_block_suspend(dd);
4097 if (rv < 0) {
4098 dev_err(&pdev->dev,
4099 "Failed to suspend controller\n");
4100 return rv;
4101 }
4102
4103 /*
4104 * Save the pci config space to pdev structure &
4105 * disable the device
4106 */
4107 pci_save_state(pdev);
4108 pci_disable_device(pdev);
4109
4110 /* Move to Low power state*/
4111 pci_set_power_state(pdev, PCI_D3hot);
4112
4113 return rv;
4114}
4115
4116/*
4117 * Called for each probed device when the device is resumed.
4118 *
4119 * return value
4120 * 0 Success
4121 * <0 Error
4122 */
4123static int mtip_pci_resume(struct pci_dev *pdev)
4124{
4125 int rv = 0;
4126 struct driver_data *dd;
4127
4128 dd = pci_get_drvdata(pdev);
4129 if (!dd) {
4130 dev_err(&pdev->dev,
4131 "Driver private datastructure is NULL\n");
4132 return -EFAULT;
4133 }
4134
4135 /* Move the device to active State */
4136 pci_set_power_state(pdev, PCI_D0);
4137
4138 /* Restore PCI configuration space */
4139 pci_restore_state(pdev);
4140
4141 /* Enable the PCI device*/
4142 rv = pcim_enable_device(pdev);
4143 if (rv < 0) {
4144 dev_err(&pdev->dev,
4145 "Failed to enable card during resume\n");
4146 goto err;
4147 }
4148 pci_set_master(pdev);
4149
4150 /*
4151 * Calls hbaReset, initPort, & startPort function
4152 * then enables interrupts
4153 */
4154 rv = mtip_block_resume(dd);
4155 if (rv < 0)
4156 dev_err(&pdev->dev, "Unable to resume\n");
4157
4158err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004159 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004160
4161 return rv;
4162}
4163
4164/*
4165 * Shutdown routine
4166 *
4167 * return value
4168 * None
4169 */
4170static void mtip_pci_shutdown(struct pci_dev *pdev)
4171{
4172 struct driver_data *dd = pci_get_drvdata(pdev);
4173 if (dd)
4174 mtip_block_shutdown(dd);
4175}
4176
Sam Bradshaw88523a62011-08-30 08:34:26 -06004177/* Table of device ids supported by this driver. */
4178static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304179 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4180 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4181 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4182 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4183 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4184 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4185 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004186 { 0 }
4187};
4188
4189/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004190static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004191 .name = MTIP_DRV_NAME,
4192 .id_table = mtip_pci_tbl,
4193 .probe = mtip_pci_probe,
4194 .remove = mtip_pci_remove,
4195 .suspend = mtip_pci_suspend,
4196 .resume = mtip_pci_resume,
4197 .shutdown = mtip_pci_shutdown,
4198};
4199
4200MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4201
4202/*
4203 * Module initialization function.
4204 *
4205 * Called once when the module is loaded. This function allocates a major
4206 * block device number to the Cyclone devices and registers the PCI layer
4207 * of the driver.
4208 *
4209 * Return value
4210 * 0 on success else error code.
4211 */
4212static int __init mtip_init(void)
4213{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004214 int error;
4215
Sam Bradshaw88523a62011-08-30 08:34:26 -06004216 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4217
4218 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004219 error = register_blkdev(0, MTIP_DRV_NAME);
4220 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004221 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004222 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004223 return -EBUSY;
4224 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004225 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004226
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004227 if (!dfs_parent) {
4228 dfs_parent = debugfs_create_dir("rssd", NULL);
4229 if (IS_ERR_OR_NULL(dfs_parent)) {
4230 printk(KERN_WARNING "Error creating debugfs parent\n");
4231 dfs_parent = NULL;
4232 }
4233 }
4234
Sam Bradshaw88523a62011-08-30 08:34:26 -06004235 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004236 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004237 if (error) {
4238 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004239 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004240 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004241
4242 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004243}
4244
4245/*
4246 * Module de-initialization function.
4247 *
4248 * Called once when the module is unloaded. This function deallocates
4249 * the major block device number allocated by mtip_init() and
4250 * unregisters the PCI layer of the driver.
4251 *
4252 * Return value
4253 * none
4254 */
4255static void __exit mtip_exit(void)
4256{
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004257 debugfs_remove_recursive(dfs_parent);
4258
Sam Bradshaw88523a62011-08-30 08:34:26 -06004259 /* Release the allocated major block device number. */
4260 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4261
4262 /* Unregister the PCI driver. */
4263 pci_unregister_driver(&mtip_pci_driver);
4264}
4265
4266MODULE_AUTHOR("Micron Technology, Inc");
4267MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4268MODULE_LICENSE("GPL");
4269MODULE_VERSION(MTIP_DRV_VERSION);
4270
4271module_init(mtip_init);
4272module_exit(mtip_exit);