blob: cd271d5e1b73a5a100a513fe4953cbb44f11bf2d [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>
Sam Bradshaw88523a62011-08-30 08:34:26 -060040#include "mtip32xx.h"
41
42#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
43#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
44#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
45#define HW_PORT_PRIV_DMA_SZ \
46 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
47
Asai Thambi S P45038362012-04-09 08:35:38 +020048#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060049#define HOST_HSORG 0xFC
50#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
51#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
52#define HSORG_HWREV 0xFF00
53#define HSORG_STYLE 0x8
54#define HSORG_SLOTGROUPS 0x7
55
56#define PORT_COMMAND_ISSUE 0x38
57#define PORT_SDBV 0x7C
58
59#define PORT_OFFSET 0x100
60#define PORT_MEM_SIZE 0x80
61
62#define PORT_IRQ_ERR \
63 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
64 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
65 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
66 PORT_IRQ_OVERFLOW)
67#define PORT_IRQ_LEGACY \
68 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
69#define PORT_IRQ_HANDLED \
70 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
71 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
72 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
73#define DEF_PORT_IRQ \
74 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
75
76/* product numbers */
77#define MTIP_PRODUCT_UNKNOWN 0x00
78#define MTIP_PRODUCT_ASICFPGA 0x11
79
80/* Device instance number, incremented each time a device is probed. */
81static int instance;
82
83/*
84 * Global variable used to hold the major block device number
85 * allocated in mtip_init().
86 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060087static int mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -060088
89static DEFINE_SPINLOCK(rssd_index_lock);
90static DEFINE_IDA(rssd_index_ida);
91
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010092static int mtip_block_initialize(struct driver_data *dd);
93
Jens Axboe16d02c02011-09-27 15:50:01 -060094#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060095struct mtip_compat_ide_task_request_s {
96 __u8 io_ports[8];
97 __u8 hob_ports[8];
98 ide_reg_valid_t out_flags;
99 ide_reg_valid_t in_flags;
100 int data_phase;
101 int req_cmd;
102 compat_ulong_t out_size;
103 compat_ulong_t in_size;
104};
Jens Axboe16d02c02011-09-27 15:50:01 -0600105#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600106
Sam Bradshaw88523a62011-08-30 08:34:26 -0600107/*
Jens Axboe63166682011-09-27 21:27:43 -0600108 * This function check_for_surprise_removal is called
109 * while card is removed from the system and it will
110 * read the vendor id from the configration space
111 *
112 * @pdev Pointer to the pci_dev structure.
113 *
114 * return value
115 * true if device removed, else false
116 */
117static bool mtip_check_surprise_removal(struct pci_dev *pdev)
118{
119 u16 vendor_id = 0;
120
121 /* Read the vendorID from the configuration space */
122 pci_read_config_word(pdev, 0x00, &vendor_id);
123 if (vendor_id == 0xFFFF)
124 return true; /* device removed */
125
126 return false; /* device present */
127}
128
129/*
130 * This function is called for clean the pending command in the
131 * command slot during the surprise removal of device and return
132 * error to the upper layer.
133 *
134 * @dd Pointer to the DRIVER_DATA structure.
135 *
136 * return value
137 * None
138 */
139static void mtip_command_cleanup(struct driver_data *dd)
140{
141 int group = 0, commandslot = 0, commandindex = 0;
142 struct mtip_cmd *command;
143 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200144 static int in_progress;
145
146 if (in_progress)
147 return;
148
149 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600150
151 for (group = 0; group < 4; group++) {
152 for (commandslot = 0; commandslot < 32; commandslot++) {
153 if (!(port->allocated[group] & (1 << commandslot)))
154 continue;
155
156 commandindex = group << 5 | commandslot;
157 command = &port->commands[commandindex];
158
159 if (atomic_read(&command->active)
160 && (command->async_callback)) {
161 command->async_callback(command->async_data,
162 -ENODEV);
163 command->async_callback = NULL;
164 command->async_data = NULL;
165 }
166
167 dma_unmap_sg(&port->dd->pdev->dev,
168 command->sg,
169 command->scatter_ents,
170 command->direction);
171 }
172 }
173
174 up(&port->cmd_slot);
175
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200176 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +0200177 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600178}
179
180/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600181 * Obtain an empty command slot.
182 *
183 * This function needs to be reentrant since it could be called
184 * at the same time on multiple CPUs. The allocation of the
185 * command slot must be atomic.
186 *
187 * @port Pointer to the port data structure.
188 *
189 * return value
190 * >= 0 Index of command slot obtained.
191 * -1 No command slots available.
192 */
193static int get_slot(struct mtip_port *port)
194{
195 int slot, i;
196 unsigned int num_command_slots = port->dd->slot_groups * 32;
197
198 /*
199 * Try 10 times, because there is a small race here.
200 * that's ok, because it's still cheaper than a lock.
201 *
202 * Race: Since this section is not protected by lock, same bit
203 * could be chosen by different process contexts running in
204 * different processor. So instead of costly lock, we are going
205 * with loop.
206 */
207 for (i = 0; i < 10; i++) {
208 slot = find_next_zero_bit(port->allocated,
209 num_command_slots, 1);
210 if ((slot < num_command_slots) &&
211 (!test_and_set_bit(slot, port->allocated)))
212 return slot;
213 }
214 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
215
216 if (mtip_check_surprise_removal(port->dd->pdev)) {
217 /* Device not present, clean outstanding commands */
218 mtip_command_cleanup(port->dd);
219 }
220 return -1;
221}
222
223/*
224 * Release a command slot.
225 *
226 * @port Pointer to the port data structure.
227 * @tag Tag of command to release
228 *
229 * return value
230 * None
231 */
232static inline void release_slot(struct mtip_port *port, int tag)
233{
234 smp_mb__before_clear_bit();
235 clear_bit(tag, port->allocated);
236 smp_mb__after_clear_bit();
237}
238
239/*
Jens Axboe63166682011-09-27 21:27:43 -0600240 * Reset the HBA (without sleeping)
241 *
242 * Just like hba_reset, except does not call sleep, so can be
243 * run from interrupt/tasklet context.
244 *
245 * @dd Pointer to the driver data structure.
246 *
247 * return value
248 * 0 The reset was successful.
249 * -1 The HBA Reset bit did not clear.
250 */
251static int hba_reset_nosleep(struct driver_data *dd)
252{
253 unsigned long timeout;
254
255 /* Chip quirk: quiesce any chip function */
256 mdelay(10);
257
258 /* Set the reset bit */
259 writel(HOST_RESET, dd->mmio + HOST_CTL);
260
261 /* Flush */
262 readl(dd->mmio + HOST_CTL);
263
264 /*
265 * Wait 10ms then spin for up to 1 second
266 * waiting for reset acknowledgement
267 */
268 timeout = jiffies + msecs_to_jiffies(1000);
269 mdelay(10);
270 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
271 && time_before(jiffies, timeout))
272 mdelay(1);
273
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200274 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200275 return -1;
276
Jens Axboe63166682011-09-27 21:27:43 -0600277 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
278 return -1;
279
280 return 0;
281}
282
283/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600284 * Issue a command to the hardware.
285 *
286 * Set the appropriate bit in the s_active and Command Issue hardware
287 * registers, causing hardware command processing to begin.
288 *
289 * @port Pointer to the port structure.
290 * @tag The tag of the command to be issued.
291 *
292 * return value
293 * None
294 */
295static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
296{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600297 atomic_set(&port->commands[tag].active, 1);
298
Asai Thambi S P0a07ab22012-05-29 18:43:16 -0700299 spin_lock(&port->cmd_issue_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600300
301 writel((1 << MTIP_TAG_BIT(tag)),
302 port->s_active[MTIP_TAG_INDEX(tag)]);
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
305
Asai Thambi S P0a07ab22012-05-29 18:43:16 -0700306 spin_unlock(&port->cmd_issue_lock);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200307
308 /* Set the command's timeout value.*/
309 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
310 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600311}
312
313/*
Jens Axboe63166682011-09-27 21:27:43 -0600314 * Enable/disable the reception of FIS
315 *
316 * @port Pointer to the port data structure
317 * @enable 1 to enable, 0 to disable
318 *
319 * return value
320 * Previous state: 1 enabled, 0 disabled
321 */
322static int mtip_enable_fis(struct mtip_port *port, int enable)
323{
324 u32 tmp;
325
326 /* enable FIS reception */
327 tmp = readl(port->mmio + PORT_CMD);
328 if (enable)
329 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
330 else
331 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332
333 /* Flush */
334 readl(port->mmio + PORT_CMD);
335
336 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
337}
338
339/*
340 * Enable/disable the DMA engine
341 *
342 * @port Pointer to the port data structure
343 * @enable 1 to enable, 0 to disable
344 *
345 * return value
346 * Previous state: 1 enabled, 0 disabled.
347 */
348static int mtip_enable_engine(struct mtip_port *port, int enable)
349{
350 u32 tmp;
351
352 /* enable FIS reception */
353 tmp = readl(port->mmio + PORT_CMD);
354 if (enable)
355 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
356 else
357 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
358
359 readl(port->mmio + PORT_CMD);
360 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
361}
362
363/*
364 * Enables the port DMA engine and FIS reception.
365 *
366 * return value
367 * None
368 */
369static inline void mtip_start_port(struct mtip_port *port)
370{
371 /* Enable FIS reception */
372 mtip_enable_fis(port, 1);
373
374 /* Enable the DMA engine */
375 mtip_enable_engine(port, 1);
376}
377
378/*
379 * Deinitialize a port by disabling port interrupts, the DMA engine,
380 * and FIS reception.
381 *
382 * @port Pointer to the port structure
383 *
384 * return value
385 * None
386 */
387static inline void mtip_deinit_port(struct mtip_port *port)
388{
389 /* Disable interrupts on this port */
390 writel(0, port->mmio + PORT_IRQ_MASK);
391
392 /* Disable the DMA engine */
393 mtip_enable_engine(port, 0);
394
395 /* Disable FIS reception */
396 mtip_enable_fis(port, 0);
397}
398
399/*
400 * Initialize a port.
401 *
402 * This function deinitializes the port by calling mtip_deinit_port() and
403 * then initializes it by setting the command header and RX FIS addresses,
404 * clearing the SError register and any pending port interrupts before
405 * re-enabling the default set of port interrupts.
406 *
407 * @port Pointer to the port structure.
408 *
409 * return value
410 * None
411 */
412static void mtip_init_port(struct mtip_port *port)
413{
414 int i;
415 mtip_deinit_port(port);
416
417 /* Program the command list base and FIS base addresses */
418 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
419 writel((port->command_list_dma >> 16) >> 16,
420 port->mmio + PORT_LST_ADDR_HI);
421 writel((port->rxfis_dma >> 16) >> 16,
422 port->mmio + PORT_FIS_ADDR_HI);
423 }
424
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100425 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600426 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600428
429 /* Clear SError */
430 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
431
432 /* reset the completed registers.*/
433 for (i = 0; i < port->dd->slot_groups; i++)
434 writel(0xFFFFFFFF, port->completed[i]);
435
436 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700437 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600438
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100439 /* Clear any pending interrupts on the HBA. */
440 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
441 port->dd->mmio + HOST_IRQ_STAT);
442
Jens Axboe63166682011-09-27 21:27:43 -0600443 /* Enable port interrupts */
444 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
445}
446
447/*
448 * Restart a port
449 *
450 * @port Pointer to the port data structure.
451 *
452 * return value
453 * None
454 */
455static void mtip_restart_port(struct mtip_port *port)
456{
457 unsigned long timeout;
458
459 /* Disable the DMA engine */
460 mtip_enable_engine(port, 0);
461
462 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
463 timeout = jiffies + msecs_to_jiffies(500);
464 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
465 && time_before(jiffies, timeout))
466 ;
467
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200468 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200469 return;
470
Jens Axboe63166682011-09-27 21:27:43 -0600471 /*
472 * Chip quirk: escalate to hba reset if
473 * PxCMD.CR not clear after 500 ms
474 */
475 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
476 dev_warn(&port->dd->pdev->dev,
477 "PxCMD.CR not clear, escalating reset\n");
478
479 if (hba_reset_nosleep(port->dd))
480 dev_err(&port->dd->pdev->dev,
481 "HBA reset escalation failed.\n");
482
483 /* 30 ms delay before com reset to quiesce chip */
484 mdelay(30);
485 }
486
487 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
488
489 /* Set PxSCTL.DET */
490 writel(readl(port->mmio + PORT_SCR_CTL) |
491 1, port->mmio + PORT_SCR_CTL);
492 readl(port->mmio + PORT_SCR_CTL);
493
494 /* Wait 1 ms to quiesce chip function */
495 timeout = jiffies + msecs_to_jiffies(1);
496 while (time_before(jiffies, timeout))
497 ;
498
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200499 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200500 return;
501
Jens Axboe63166682011-09-27 21:27:43 -0600502 /* Clear PxSCTL.DET */
503 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
504 port->mmio + PORT_SCR_CTL);
505 readl(port->mmio + PORT_SCR_CTL);
506
507 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
508 timeout = jiffies + msecs_to_jiffies(500);
509 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
510 && time_before(jiffies, timeout))
511 ;
512
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200513 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200514 return;
515
Jens Axboe63166682011-09-27 21:27:43 -0600516 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
517 dev_warn(&port->dd->pdev->dev,
518 "COM reset failed\n");
519
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100520 mtip_init_port(port);
521 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600522
Jens Axboe63166682011-09-27 21:27:43 -0600523}
524
525/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200526 * Helper function for tag logging
527 */
528static void print_tags(struct driver_data *dd,
529 char *msg,
530 unsigned long *tagbits,
531 int cnt)
532{
533 unsigned char tagmap[128];
534 int group, tagmap_len = 0;
535
536 memset(tagmap, 0, sizeof(tagmap));
537 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
538 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
539 tagbits[group-1]);
540 dev_warn(&dd->pdev->dev,
541 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
542}
543
544/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600545 * Called periodically to see if any read/write commands are
546 * taking too long to complete.
547 *
548 * @data Pointer to the PORT data structure.
549 *
550 * return value
551 * None
552 */
Jens Axboe63166682011-09-27 21:27:43 -0600553static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600554{
555 struct mtip_port *port = (struct mtip_port *) data;
556 struct host_to_dev_fis *fis;
557 struct mtip_cmd *command;
558 int tag, cmdto_cnt = 0;
559 unsigned int bit, group;
560 unsigned int num_command_slots = port->dd->slot_groups * 32;
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200561 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600562
563 if (unlikely(!port))
564 return;
565
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200566 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600567 mod_timer(&port->cmd_timer,
568 jiffies + msecs_to_jiffies(30000));
569 return;
570 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200571 /* clear the tag accumulator */
572 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600573
574 for (tag = 0; tag < num_command_slots; tag++) {
575 /*
576 * Skip internal command slot as it has
577 * its own timeout mechanism
578 */
579 if (tag == MTIP_TAG_INTERNAL)
580 continue;
581
582 if (atomic_read(&port->commands[tag].active) &&
583 (time_after(jiffies, port->commands[tag].comp_time))) {
584 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100585 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600586
587 command = &port->commands[tag];
588 fis = (struct host_to_dev_fis *) command->command;
589
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200590 set_bit(tag, tagaccum);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600591 cmdto_cnt++;
592 if (cmdto_cnt == 1)
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200593 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600594
595 /*
596 * Clear the completed bit. This should prevent
597 * any interrupt handlers from trying to retire
598 * the command.
599 */
600 writel(1 << bit, port->completed[group]);
601
602 /* Call the async completion callback. */
603 if (likely(command->async_callback))
604 command->async_callback(command->async_data,
605 -EIO);
606 command->async_callback = NULL;
607 command->comp_func = NULL;
608
609 /* Unmap the DMA scatter list entries */
610 dma_unmap_sg(&port->dd->pdev->dev,
611 command->sg,
612 command->scatter_ents,
613 command->direction);
614
615 /*
616 * Clear the allocated bit and active tag for the
617 * command.
618 */
619 atomic_set(&port->commands[tag].active, 0);
620 release_slot(port, tag);
621
622 up(&port->cmd_slot);
623 }
624 }
625
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200626 if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200627 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
628
Sam Bradshaw88523a62011-08-30 08:34:26 -0600629 mtip_restart_port(port);
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200630 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100631 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600632 }
633
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200634 if (port->ic_pause_timer) {
635 to = port->ic_pause_timer + msecs_to_jiffies(1000);
636 if (time_after(jiffies, to)) {
637 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
638 port->ic_pause_timer = 0;
639 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
640 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
641 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
642 wake_up_interruptible(&port->svc_wait);
643 }
644
645
646 }
647 }
648
Sam Bradshaw88523a62011-08-30 08:34:26 -0600649 /* Restart the timer */
650 mod_timer(&port->cmd_timer,
651 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
652}
653
654/*
655 * IO completion function.
656 *
657 * This completion function is called by the driver ISR when a
658 * command that was issued by the kernel completes. It first calls the
659 * asynchronous completion function which normally calls back into the block
660 * layer passing the asynchronous callback data, then unmaps the
661 * scatter list associated with the completed command, and finally
662 * clears the allocated bit associated with the completed command.
663 *
664 * @port Pointer to the port data structure.
665 * @tag Tag of the command.
666 * @data Pointer to driver_data.
667 * @status Completion status.
668 *
669 * return value
670 * None
671 */
672static void mtip_async_complete(struct mtip_port *port,
673 int tag,
674 void *data,
675 int status)
676{
677 struct mtip_cmd *command;
678 struct driver_data *dd = data;
679 int cb_status = status ? -EIO : 0;
680
681 if (unlikely(!dd) || unlikely(!port))
682 return;
683
684 command = &port->commands[tag];
685
686 if (unlikely(status == PORT_IRQ_TF_ERR)) {
687 dev_warn(&port->dd->pdev->dev,
688 "Command tag %d failed due to TFE\n", tag);
689 }
690
691 /* Upper layer callback */
692 if (likely(command->async_callback))
693 command->async_callback(command->async_data, cb_status);
694
695 command->async_callback = NULL;
696 command->comp_func = NULL;
697
698 /* Unmap the DMA scatter list entries */
699 dma_unmap_sg(&dd->pdev->dev,
700 command->sg,
701 command->scatter_ents,
702 command->direction);
703
704 /* Clear the allocated and active bits for the command */
705 atomic_set(&port->commands[tag].active, 0);
706 release_slot(port, tag);
707
708 up(&port->cmd_slot);
709}
710
711/*
712 * Internal command completion callback function.
713 *
714 * This function is normally called by the driver ISR when an internal
715 * command completed. This function signals the command completion by
716 * calling complete().
717 *
718 * @port Pointer to the port data structure.
719 * @tag Tag of the command that has completed.
720 * @data Pointer to a completion structure.
721 * @status Completion status.
722 *
723 * return value
724 * None
725 */
726static void mtip_completion(struct mtip_port *port,
727 int tag,
728 void *data,
729 int status)
730{
731 struct mtip_cmd *command = &port->commands[tag];
732 struct completion *waiting = data;
733 if (unlikely(status == PORT_IRQ_TF_ERR))
734 dev_warn(&port->dd->pdev->dev,
735 "Internal command %d completed with TFE\n", tag);
736
737 command->async_callback = NULL;
738 command->comp_func = NULL;
739
740 complete(waiting);
741}
742
Asai Thambi S P8182b492012-04-09 08:35:38 +0200743static void mtip_null_completion(struct mtip_port *port,
744 int tag,
745 void *data,
746 int status)
747{
748 return;
749}
750
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200751static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
752 dma_addr_t buffer_dma, unsigned int sectors);
753static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
754 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600755/*
756 * Handle an error.
757 *
758 * @dd Pointer to the DRIVER_DATA structure.
759 *
760 * return value
761 * None
762 */
763static void mtip_handle_tfe(struct driver_data *dd)
764{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200765 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600766 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200767 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600768 u32 completed;
769 struct host_to_dev_fis *fis;
770 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200771 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200772 unsigned char *buf;
773 char *fail_reason = NULL;
774 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600775
776 dev_warn(&dd->pdev->dev, "Taskfile error\n");
777
778 port = dd->port;
779
780 /* Stop the timer to prevent command timeouts. */
781 del_timer(&port->cmd_timer);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700782 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
783
784 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
785 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
786 cmd = &port->commands[MTIP_TAG_INTERNAL];
787 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
788
789 atomic_inc(&cmd->active); /* active > 1 indicates error */
790 if (cmd->comp_data && cmd->comp_func) {
791 cmd->comp_func(port, MTIP_TAG_INTERNAL,
792 cmd->comp_data, PORT_IRQ_TF_ERR);
793 }
794 goto handle_tfe_exit;
795 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600796
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200797 /* clear the tag accumulator */
798 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
799
Sam Bradshaw88523a62011-08-30 08:34:26 -0600800 /* Loop through all the groups */
801 for (group = 0; group < dd->slot_groups; group++) {
802 completed = readl(port->completed[group]);
803
804 /* clear completed status register in the hardware.*/
805 writel(completed, port->completed[group]);
806
Sam Bradshaw88523a62011-08-30 08:34:26 -0600807 /* Process successfully completed commands */
808 for (bit = 0; bit < 32 && completed; bit++) {
809 if (!(completed & (1<<bit)))
810 continue;
811 tag = (group << 5) + bit;
812
813 /* Skip the internal command slot */
814 if (tag == MTIP_TAG_INTERNAL)
815 continue;
816
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200817 cmd = &port->commands[tag];
818 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600819 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200820 cmd_cnt++;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200821 atomic_set(&cmd->active, 0);
822 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600823 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200824 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600825 0);
826 } else {
827 dev_err(&port->dd->pdev->dev,
828 "Missing completion func for tag %d",
829 tag);
830 if (mtip_check_surprise_removal(dd->pdev)) {
831 mtip_command_cleanup(dd);
832 /* don't proceed further */
833 return;
834 }
835 }
836 }
837 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200838
839 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840
841 /* Restart the port */
842 mdelay(20);
843 mtip_restart_port(port);
844
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200845 /* Trying to determine the cause of the error */
846 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
847 dd->port->log_buf,
848 dd->port->log_buf_dma, 1);
849 if (rv) {
850 dev_warn(&dd->pdev->dev,
851 "Error in READ LOG EXT (10h) command\n");
852 /* non-critical error, don't fail the load */
853 } else {
854 buf = (unsigned char *)dd->port->log_buf;
855 if (buf[259] & 0x1) {
856 dev_info(&dd->pdev->dev,
857 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200858 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200859 fail_all_ncq_write = 1;
860 fail_reason = "write protect";
861 }
862 if (buf[288] == 0xF7) {
863 dev_info(&dd->pdev->dev,
864 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200865 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200866 fail_all_ncq_cmds = 1;
867 fail_reason = "thermal shutdown";
868 }
869 if (buf[288] == 0xBF) {
870 dev_info(&dd->pdev->dev,
871 "Drive indicates rebuild has failed.\n");
872 fail_all_ncq_cmds = 1;
873 fail_reason = "rebuild failed";
874 }
875 }
876
Sam Bradshaw88523a62011-08-30 08:34:26 -0600877 /* clear the tag accumulator */
878 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
879
880 /* Loop through all the groups */
881 for (group = 0; group < dd->slot_groups; group++) {
882 for (bit = 0; bit < 32; bit++) {
883 reissue = 1;
884 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200885 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600886
887 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200888 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600889 continue;
890
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200891 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600892
893 /* Should re-issue? */
894 if (tag == MTIP_TAG_INTERNAL ||
895 fis->command == ATA_CMD_SET_FEATURES)
896 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200897 else {
898 if (fail_all_ncq_cmds ||
899 (fail_all_ncq_write &&
900 fis->command == ATA_CMD_FPDMA_WRITE)) {
901 dev_warn(&dd->pdev->dev,
902 " Fail: %s w/tag %d [%s].\n",
903 fis->command == ATA_CMD_FPDMA_WRITE ?
904 "write" : "read",
905 tag,
906 fail_reason != NULL ?
907 fail_reason : "unknown");
908 atomic_set(&cmd->active, 0);
909 if (cmd->comp_func) {
910 cmd->comp_func(port, tag,
911 cmd->comp_data,
912 -ENODATA);
913 }
914 continue;
915 }
916 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600917
918 /*
919 * First check if this command has
920 * exceeded its retries.
921 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200922 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600923
924 set_bit(tag, tagaccum);
925
Sam Bradshaw88523a62011-08-30 08:34:26 -0600926 /* Re-issue the command. */
927 mtip_issue_ncq_command(port, tag);
928
929 continue;
930 }
931
932 /* Retire a command that will not be reissued */
933 dev_warn(&port->dd->pdev->dev,
934 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200935 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600936
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200937 if (cmd->comp_func)
938 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -0600939 port,
940 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200941 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600942 PORT_IRQ_TF_ERR);
943 else
944 dev_warn(&port->dd->pdev->dev,
945 "Bad completion for tag %d\n",
946 tag);
947 }
948 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200949 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600950
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700951handle_tfe_exit:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100952 /* clear eh_active */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200953 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100954 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600955
956 mod_timer(&port->cmd_timer,
957 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
958}
959
960/*
961 * Handle a set device bits interrupt
962 */
963static inline void mtip_process_sdbf(struct driver_data *dd)
964{
965 struct mtip_port *port = dd->port;
966 int group, tag, bit;
967 u32 completed;
968 struct mtip_cmd *command;
969
970 /* walk all bits in all slot groups */
971 for (group = 0; group < dd->slot_groups; group++) {
972 completed = readl(port->completed[group]);
973
974 /* clear completed status register in the hardware.*/
975 writel(completed, port->completed[group]);
976
977 /* Process completed commands. */
978 for (bit = 0;
979 (bit < 32) && completed;
980 bit++, completed >>= 1) {
981 if (completed & 0x01) {
982 tag = (group << 5) | bit;
983
984 /* skip internal command slot. */
985 if (unlikely(tag == MTIP_TAG_INTERNAL))
986 continue;
987
988 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600989 /* make internal callback */
990 if (likely(command->comp_func)) {
991 command->comp_func(
992 port,
993 tag,
994 command->comp_data,
995 0);
996 } else {
997 dev_warn(&dd->pdev->dev,
998 "Null completion "
999 "for tag %d",
1000 tag);
1001
1002 if (mtip_check_surprise_removal(
1003 dd->pdev)) {
1004 mtip_command_cleanup(dd);
1005 return;
1006 }
1007 }
1008 }
1009 }
1010 }
1011}
1012
1013/*
1014 * Process legacy pio and d2h interrupts
1015 */
1016static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1017{
1018 struct mtip_port *port = dd->port;
1019 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1020
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001021 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001022 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -06001023 & (1 << MTIP_TAG_INTERNAL))) {
1024 if (cmd->comp_func) {
1025 cmd->comp_func(port,
1026 MTIP_TAG_INTERNAL,
1027 cmd->comp_data,
1028 0);
1029 return;
1030 }
1031 }
1032
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033 return;
1034}
1035
1036/*
1037 * Demux and handle errors
1038 */
1039static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1040{
1041 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1042 mtip_handle_tfe(dd);
1043
1044 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1045 dev_warn(&dd->pdev->dev,
1046 "Clearing PxSERR.DIAG.x\n");
1047 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1048 }
1049
1050 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1051 dev_warn(&dd->pdev->dev,
1052 "Clearing PxSERR.DIAG.n\n");
1053 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1054 }
1055
1056 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1057 dev_warn(&dd->pdev->dev,
1058 "Port stat errors %x unhandled\n",
1059 (port_stat & ~PORT_IRQ_HANDLED));
1060 }
1061}
1062
1063static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1064{
1065 struct driver_data *dd = (struct driver_data *) data;
1066 struct mtip_port *port = dd->port;
1067 u32 hba_stat, port_stat;
1068 int rv = IRQ_NONE;
1069
1070 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1071 if (hba_stat) {
1072 rv = IRQ_HANDLED;
1073
1074 /* Acknowledge the interrupt status on the port.*/
1075 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1076 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1077
1078 /* Demux port status */
1079 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1080 mtip_process_sdbf(dd);
1081
1082 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1083 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1084 mtip_command_cleanup(dd);
1085 /* don't proceed further */
1086 return IRQ_HANDLED;
1087 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001088 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001089 &dd->dd_flag))
1090 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001091
1092 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1093 }
1094
1095 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1096 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1097 }
1098
1099 /* acknowledge interrupt */
1100 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1101
1102 return rv;
1103}
1104
1105/*
1106 * Wrapper for mtip_handle_irq
1107 * (ignores return code)
1108 */
1109static void mtip_tasklet(unsigned long data)
1110{
1111 mtip_handle_irq((struct driver_data *) data);
1112}
1113
1114/*
1115 * HBA interrupt subroutine.
1116 *
1117 * @irq IRQ number.
1118 * @instance Pointer to the driver data structure.
1119 *
1120 * return value
1121 * IRQ_HANDLED A HBA interrupt was pending and handled.
1122 * IRQ_NONE This interrupt was not for the HBA.
1123 */
1124static irqreturn_t mtip_irq_handler(int irq, void *instance)
1125{
1126 struct driver_data *dd = instance;
1127 tasklet_schedule(&dd->tasklet);
1128 return IRQ_HANDLED;
1129}
1130
1131static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1132{
1133 atomic_set(&port->commands[tag].active, 1);
1134 writel(1 << MTIP_TAG_BIT(tag),
1135 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1136}
1137
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001138static bool mtip_pause_ncq(struct mtip_port *port,
1139 struct host_to_dev_fis *fis)
1140{
1141 struct host_to_dev_fis *reply;
1142 unsigned long task_file_data;
1143
1144 reply = port->rxfis + RX_FIS_D2H_REG;
1145 task_file_data = readl(port->mmio+PORT_TFDATA);
1146
1147 if ((task_file_data & 1) || (fis->command == ATA_CMD_SEC_ERASE_UNIT))
1148 return false;
1149
1150 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1151 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1152 port->ic_pause_timer = jiffies;
1153 return true;
1154 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1155 (fis->features == 0x03)) {
1156 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1157 port->ic_pause_timer = jiffies;
1158 return true;
1159 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1160 ((fis->command == 0xFC) &&
1161 (fis->features == 0x27 || fis->features == 0x72 ||
1162 fis->features == 0x62 || fis->features == 0x26))) {
1163 /* Com reset after secure erase or lowlevel format */
1164 mtip_restart_port(port);
1165 return false;
1166 }
1167
1168 return false;
1169}
1170
Sam Bradshaw88523a62011-08-30 08:34:26 -06001171/*
1172 * Wait for port to quiesce
1173 *
1174 * @port Pointer to port data structure
1175 * @timeout Max duration to wait (ms)
1176 *
1177 * return value
1178 * 0 Success
1179 * -EBUSY Commands still active
1180 */
1181static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1182{
1183 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001184 unsigned int n;
1185 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001186
1187 to = jiffies + msecs_to_jiffies(timeout);
1188 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001189 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1190 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001191 msleep(20);
1192 continue; /* svc thd is actively issuing commands */
1193 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001194 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001195 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001196 /*
1197 * Ignore s_active bit 0 of array element 0.
1198 * This bit will always be set
1199 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001200 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001201 for (n = 1; n < port->dd->slot_groups; n++)
1202 active |= readl(port->s_active[n]);
1203
1204 if (!active)
1205 break;
1206
1207 msleep(20);
1208 } while (time_before(jiffies, to));
1209
1210 return active ? -EBUSY : 0;
1211}
1212
1213/*
1214 * Execute an internal command and wait for the completion.
1215 *
1216 * @port Pointer to the port data structure.
1217 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001218 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001219 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001220 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001221 * @opts Command header options, excluding the FIS length
1222 * and the number of PRD entries.
1223 * @timeout Time in ms to wait for the command to complete.
1224 *
1225 * return value
1226 * 0 Command completed successfully.
1227 * -EFAULT The buffer address is not correctly aligned.
1228 * -EBUSY Internal command or other IO in progress.
1229 * -EAGAIN Time out waiting for command to complete.
1230 */
1231static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001232 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001233 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001234 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001235 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001236 u32 opts,
1237 gfp_t atomic,
1238 unsigned long timeout)
1239{
1240 struct mtip_cmd_sg *command_sg;
1241 DECLARE_COMPLETION_ONSTACK(wait);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001242 int rv = 0, ready2go = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001243 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001244 unsigned long to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001245
1246 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1247 if (buffer & 0x00000007) {
1248 dev_err(&port->dd->pdev->dev,
1249 "SG buffer is not 8 byte aligned\n");
1250 return -EFAULT;
1251 }
1252
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001253 to = jiffies + msecs_to_jiffies(timeout);
1254 do {
1255 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1256 port->allocated);
1257 if (ready2go)
1258 break;
1259 mdelay(100);
1260 } while (time_before(jiffies, to));
1261 if (!ready2go) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001262 dev_warn(&port->dd->pdev->dev,
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001263 "Internal cmd active. new cmd [%02X]\n", fis->command);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001264 return -EBUSY;
1265 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001266 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001267 port->ic_pause_timer = 0;
1268
1269 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1270 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1271 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1272 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001273
1274 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001275 if (fis->command != ATA_CMD_STANDBYNOW1) {
1276 /* wait for io to complete if non atomic */
1277 if (mtip_quiesce_io(port, 5000) < 0) {
1278 dev_warn(&port->dd->pdev->dev,
1279 "Failed to quiesce IO\n");
1280 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001281 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001282 wake_up_interruptible(&port->svc_wait);
1283 return -EBUSY;
1284 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001285 }
1286
1287 /* Set the completion function and data for the command. */
1288 int_cmd->comp_data = &wait;
1289 int_cmd->comp_func = mtip_completion;
1290
1291 } else {
1292 /* Clear completion - we're going to poll */
1293 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001294 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001295 }
1296
1297 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001298 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001299
1300 /* Populate the SG list */
1301 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001302 __force_bit2int cpu_to_le32(opts | fis_len);
1303 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001304 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1305
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001306 command_sg->info =
1307 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1308 command_sg->dba =
1309 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1310 command_sg->dba_upper =
1311 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001312
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001313 int_cmd->command_header->opts |=
1314 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001315 }
1316
1317 /* Populate the command header */
1318 int_cmd->command_header->byte_count = 0;
1319
1320 /* Issue the command to the hardware */
1321 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1322
1323 /* Poll if atomic, wait_for_completion otherwise */
1324 if (atomic == GFP_KERNEL) {
1325 /* Wait for the command to complete or timeout. */
1326 if (wait_for_completion_timeout(
1327 &wait,
1328 msecs_to_jiffies(timeout)) == 0) {
1329 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001330 "Internal command did not complete [%d] "
1331 "within timeout of %lu ms\n",
1332 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001333 if (mtip_check_surprise_removal(port->dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001334 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001335 &port->dd->dd_flag)) {
1336 rv = -ENXIO;
1337 goto exec_ic_exit;
1338 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001339 rv = -EAGAIN;
1340 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001341 } else {
1342 /* Spin for <timeout> checking if command still outstanding */
1343 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001344 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1345 & (1 << MTIP_TAG_INTERNAL))
1346 && time_before(jiffies, timeout)) {
1347 if (mtip_check_surprise_removal(port->dd->pdev)) {
1348 rv = -ENXIO;
1349 goto exec_ic_exit;
1350 }
1351 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001352 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001353 &port->dd->dd_flag)) {
1354 rv = -ENXIO;
1355 goto exec_ic_exit;
1356 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001357 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1358 atomic_inc(&int_cmd->active); /* error */
1359 break;
Asai Thambi S P45038362012-04-09 08:35:38 +02001360 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001361 }
1362 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001363
1364 if (atomic_read(&int_cmd->active) > 1) {
1365 dev_err(&port->dd->pdev->dev,
1366 "Internal command [%02X] failed\n", fis->command);
1367 rv = -EIO;
1368 }
1369 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1370 & (1 << MTIP_TAG_INTERNAL)) {
1371 rv = -ENXIO;
1372 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1373 &port->dd->dd_flag)) {
1374 mtip_restart_port(port);
1375 rv = -EAGAIN;
1376 }
1377 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001378exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001379 /* Clear the allocated and active bits for the internal command. */
1380 atomic_set(&int_cmd->active, 0);
1381 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001382 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1383 /* NCQ paused */
1384 return rv;
1385 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001386 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001387 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001388
1389 return rv;
1390}
1391
1392/*
1393 * Byte-swap ATA ID strings.
1394 *
1395 * ATA identify data contains strings in byte-swapped 16-bit words.
1396 * They must be swapped (on all architectures) to be usable as C strings.
1397 * This function swaps bytes in-place.
1398 *
1399 * @buf The buffer location of the string
1400 * @len The number of bytes to swap
1401 *
1402 * return value
1403 * None
1404 */
1405static inline void ata_swap_string(u16 *buf, unsigned int len)
1406{
1407 int i;
1408 for (i = 0; i < (len/2); i++)
1409 be16_to_cpus(&buf[i]);
1410}
1411
1412/*
1413 * Request the device identity information.
1414 *
1415 * If a user space buffer is not specified, i.e. is NULL, the
1416 * identify information is still read from the drive and placed
1417 * into the identify data buffer (@e port->identify) in the
1418 * port data structure.
1419 * When the identify buffer contains valid identify information @e
1420 * port->identify_valid is non-zero.
1421 *
1422 * @port Pointer to the port structure.
1423 * @user_buffer A user space buffer where the identify data should be
1424 * copied.
1425 *
1426 * return value
1427 * 0 Command completed successfully.
1428 * -EFAULT An error occurred while coping data to the user buffer.
1429 * -1 Command failed.
1430 */
1431static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1432{
1433 int rv = 0;
1434 struct host_to_dev_fis fis;
1435
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001436 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001437 return -EFAULT;
1438
Sam Bradshaw88523a62011-08-30 08:34:26 -06001439 /* Build the FIS. */
1440 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1441 fis.type = 0x27;
1442 fis.opts = 1 << 7;
1443 fis.command = ATA_CMD_ID_ATA;
1444
1445 /* Set the identify information as invalid. */
1446 port->identify_valid = 0;
1447
1448 /* Clear the identify information. */
1449 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1450
1451 /* Execute the command. */
1452 if (mtip_exec_internal_command(port,
1453 &fis,
1454 5,
1455 port->identify_dma,
1456 sizeof(u16) * ATA_ID_WORDS,
1457 0,
1458 GFP_KERNEL,
1459 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1460 < 0) {
1461 rv = -1;
1462 goto out;
1463 }
1464
1465 /*
1466 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1467 * perform field-sensitive swapping on the string fields.
1468 * See the kernel use of ata_id_string() for proof of this.
1469 */
1470#ifdef __LITTLE_ENDIAN
1471 ata_swap_string(port->identify + 27, 40); /* model string*/
1472 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1473 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1474#else
1475 {
1476 int i;
1477 for (i = 0; i < ATA_ID_WORDS; i++)
1478 port->identify[i] = le16_to_cpu(port->identify[i]);
1479 }
1480#endif
1481
1482 /* Set the identify buffer as valid. */
1483 port->identify_valid = 1;
1484
1485 if (user_buffer) {
1486 if (copy_to_user(
1487 user_buffer,
1488 port->identify,
1489 ATA_ID_WORDS * sizeof(u16))) {
1490 rv = -EFAULT;
1491 goto out;
1492 }
1493 }
1494
1495out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001496 return rv;
1497}
1498
1499/*
1500 * Issue a standby immediate command to the device.
1501 *
1502 * @port Pointer to the port structure.
1503 *
1504 * return value
1505 * 0 Command was executed successfully.
1506 * -1 An error occurred while executing the command.
1507 */
1508static int mtip_standby_immediate(struct mtip_port *port)
1509{
1510 int rv;
1511 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001512 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001513
Sam Bradshaw88523a62011-08-30 08:34:26 -06001514 /* Build the FIS. */
1515 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1516 fis.type = 0x27;
1517 fis.opts = 1 << 7;
1518 fis.command = ATA_CMD_STANDBYNOW1;
1519
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001520 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001521 rv = mtip_exec_internal_command(port,
1522 &fis,
1523 5,
1524 0,
1525 0,
1526 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001527 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001528 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001529 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1530 jiffies_to_msecs(jiffies - start));
1531 if (rv)
1532 dev_warn(&port->dd->pdev->dev,
1533 "STANDBY IMMEDIATE command failed.\n");
1534
1535 return rv;
1536}
1537
1538/*
1539 * Issue a READ LOG EXT command to the device.
1540 *
1541 * @port pointer to the port structure.
1542 * @page page number to fetch
1543 * @buffer pointer to buffer
1544 * @buffer_dma dma address corresponding to @buffer
1545 * @sectors page length to fetch, in sectors
1546 *
1547 * return value
1548 * @rv return value from mtip_exec_internal_command()
1549 */
1550static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1551 dma_addr_t buffer_dma, unsigned int sectors)
1552{
1553 struct host_to_dev_fis fis;
1554
1555 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1556 fis.type = 0x27;
1557 fis.opts = 1 << 7;
1558 fis.command = ATA_CMD_READ_LOG_EXT;
1559 fis.sect_count = sectors & 0xFF;
1560 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1561 fis.lba_low = page;
1562 fis.lba_mid = 0;
1563 fis.device = ATA_DEVICE_OBS;
1564
1565 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1566
1567 return mtip_exec_internal_command(port,
1568 &fis,
1569 5,
1570 buffer_dma,
1571 sectors * ATA_SECT_SIZE,
1572 0,
1573 GFP_ATOMIC,
1574 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1575}
1576
1577/*
1578 * Issue a SMART READ DATA command to the device.
1579 *
1580 * @port pointer to the port structure.
1581 * @buffer pointer to buffer
1582 * @buffer_dma dma address corresponding to @buffer
1583 *
1584 * return value
1585 * @rv return value from mtip_exec_internal_command()
1586 */
1587static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1588 dma_addr_t buffer_dma)
1589{
1590 struct host_to_dev_fis fis;
1591
1592 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1593 fis.type = 0x27;
1594 fis.opts = 1 << 7;
1595 fis.command = ATA_CMD_SMART;
1596 fis.features = 0xD0;
1597 fis.sect_count = 1;
1598 fis.lba_mid = 0x4F;
1599 fis.lba_hi = 0xC2;
1600 fis.device = ATA_DEVICE_OBS;
1601
1602 return mtip_exec_internal_command(port,
1603 &fis,
1604 5,
1605 buffer_dma,
1606 ATA_SECT_SIZE,
1607 0,
1608 GFP_ATOMIC,
1609 15000);
1610}
1611
1612/*
1613 * Get the value of a smart attribute
1614 *
1615 * @port pointer to the port structure
1616 * @id attribute number
1617 * @attrib pointer to return attrib information corresponding to @id
1618 *
1619 * return value
1620 * -EINVAL NULL buffer passed or unsupported attribute @id.
1621 * -EPERM Identify data not valid, SMART not supported or not enabled
1622 */
1623static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1624 struct smart_attr *attrib)
1625{
1626 int rv, i;
1627 struct smart_attr *pattr;
1628
1629 if (!attrib)
1630 return -EINVAL;
1631
1632 if (!port->identify_valid) {
1633 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1634 return -EPERM;
1635 }
1636 if (!(port->identify[82] & 0x1)) {
1637 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1638 return -EPERM;
1639 }
1640 if (!(port->identify[85] & 0x1)) {
1641 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1642 return -EPERM;
1643 }
1644
1645 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1646 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1647 if (rv) {
1648 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1649 return rv;
1650 }
1651
1652 pattr = (struct smart_attr *)(port->smart_buf + 2);
1653 for (i = 0; i < 29; i++, pattr++)
1654 if (pattr->attr_id == id) {
1655 memcpy(attrib, pattr, sizeof(struct smart_attr));
1656 break;
1657 }
1658
1659 if (i == 29) {
1660 dev_warn(&port->dd->pdev->dev,
1661 "Query for invalid SMART attribute ID\n");
1662 rv = -EINVAL;
1663 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001664
Sam Bradshaw88523a62011-08-30 08:34:26 -06001665 return rv;
1666}
1667
1668/*
1669 * Get the drive capacity.
1670 *
1671 * @dd Pointer to the device data structure.
1672 * @sectors Pointer to the variable that will receive the sector count.
1673 *
1674 * return value
1675 * 1 Capacity was returned successfully.
1676 * 0 The identify information is invalid.
1677 */
Jens Axboe63166682011-09-27 21:27:43 -06001678static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679{
1680 struct mtip_port *port = dd->port;
1681 u64 total, raw0, raw1, raw2, raw3;
1682 raw0 = port->identify[100];
1683 raw1 = port->identify[101];
1684 raw2 = port->identify[102];
1685 raw3 = port->identify[103];
1686 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1687 *sectors = total;
1688 return (bool) !!port->identify_valid;
1689}
1690
1691/*
1692 * Reset the HBA.
1693 *
1694 * Resets the HBA by setting the HBA Reset bit in the Global
1695 * HBA Control register. After setting the HBA Reset bit the
1696 * function waits for 1 second before reading the HBA Reset
1697 * bit to make sure it has cleared. If HBA Reset is not clear
1698 * an error is returned. Cannot be used in non-blockable
1699 * context.
1700 *
1701 * @dd Pointer to the driver data structure.
1702 *
1703 * return value
1704 * 0 The reset was successful.
1705 * -1 The HBA Reset bit did not clear.
1706 */
1707static int mtip_hba_reset(struct driver_data *dd)
1708{
1709 mtip_deinit_port(dd->port);
1710
1711 /* Set the reset bit */
1712 writel(HOST_RESET, dd->mmio + HOST_CTL);
1713
1714 /* Flush */
1715 readl(dd->mmio + HOST_CTL);
1716
1717 /* Wait for reset to clear */
1718 ssleep(1);
1719
1720 /* Check the bit has cleared */
1721 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1722 dev_err(&dd->pdev->dev,
1723 "Reset bit did not clear.\n");
1724 return -1;
1725 }
1726
1727 return 0;
1728}
1729
1730/*
1731 * Display the identify command data.
1732 *
1733 * @port Pointer to the port data structure.
1734 *
1735 * return value
1736 * None
1737 */
1738static void mtip_dump_identify(struct mtip_port *port)
1739{
1740 sector_t sectors;
1741 unsigned short revid;
1742 char cbuf[42];
1743
1744 if (!port->identify_valid)
1745 return;
1746
1747 strlcpy(cbuf, (char *)(port->identify+10), 21);
1748 dev_info(&port->dd->pdev->dev,
1749 "Serial No.: %s\n", cbuf);
1750
1751 strlcpy(cbuf, (char *)(port->identify+23), 9);
1752 dev_info(&port->dd->pdev->dev,
1753 "Firmware Ver.: %s\n", cbuf);
1754
1755 strlcpy(cbuf, (char *)(port->identify+27), 41);
1756 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1757
1758 if (mtip_hw_get_capacity(port->dd, &sectors))
1759 dev_info(&port->dd->pdev->dev,
1760 "Capacity: %llu sectors (%llu MB)\n",
1761 (u64)sectors,
1762 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1763
1764 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001765 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001766 case 0x1:
1767 strlcpy(cbuf, "A0", 3);
1768 break;
1769 case 0x3:
1770 strlcpy(cbuf, "A2", 3);
1771 break;
1772 default:
1773 strlcpy(cbuf, "?", 2);
1774 break;
1775 }
1776 dev_info(&port->dd->pdev->dev,
1777 "Card Type: %s\n", cbuf);
1778}
1779
1780/*
1781 * Map the commands scatter list into the command table.
1782 *
1783 * @command Pointer to the command.
1784 * @nents Number of scatter list entries.
1785 *
1786 * return value
1787 * None
1788 */
1789static inline void fill_command_sg(struct driver_data *dd,
1790 struct mtip_cmd *command,
1791 int nents)
1792{
1793 int n;
1794 unsigned int dma_len;
1795 struct mtip_cmd_sg *command_sg;
1796 struct scatterlist *sg = command->sg;
1797
1798 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1799
1800 for (n = 0; n < nents; n++) {
1801 dma_len = sg_dma_len(sg);
1802 if (dma_len > 0x400000)
1803 dev_err(&dd->pdev->dev,
1804 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001805 command_sg->info = __force_bit2int
1806 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1807 command_sg->dba = __force_bit2int
1808 cpu_to_le32(sg_dma_address(sg));
1809 command_sg->dba_upper = __force_bit2int
1810 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001811 command_sg++;
1812 sg++;
1813 }
1814}
1815
1816/*
1817 * @brief Execute a drive command.
1818 *
1819 * return value 0 The command completed successfully.
1820 * return value -1 An error occurred while executing the command.
1821 */
Jens Axboe63166682011-09-27 21:27:43 -06001822static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001823{
1824 struct host_to_dev_fis fis;
1825 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1826
Sam Bradshaw88523a62011-08-30 08:34:26 -06001827 /* Build the FIS. */
1828 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1829 fis.type = 0x27;
1830 fis.opts = 1 << 7;
1831 fis.command = command[0];
1832 fis.features = command[1];
1833 fis.sect_count = command[2];
1834 fis.sector = command[3];
1835 fis.cyl_low = command[4];
1836 fis.cyl_hi = command[5];
1837 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1838
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001839 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 -06001840 __func__,
1841 command[0],
1842 command[1],
1843 command[2],
1844 command[3],
1845 command[4],
1846 command[5],
1847 command[6]);
1848
1849 /* Execute the command. */
1850 if (mtip_exec_internal_command(port,
1851 &fis,
1852 5,
1853 0,
1854 0,
1855 0,
1856 GFP_KERNEL,
1857 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001858 return -1;
1859 }
1860
1861 command[0] = reply->command; /* Status*/
1862 command[1] = reply->features; /* Error*/
1863 command[4] = reply->cyl_low;
1864 command[5] = reply->cyl_hi;
1865
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001866 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 -06001867 __func__,
1868 command[0],
1869 command[1],
1870 command[4],
1871 command[5]);
1872
Sam Bradshaw88523a62011-08-30 08:34:26 -06001873 return 0;
1874}
1875
1876/*
1877 * @brief Execute a drive command.
1878 *
1879 * @param port Pointer to the port data structure.
1880 * @param command Pointer to the user specified command parameters.
1881 * @param user_buffer Pointer to the user space buffer where read sector
1882 * data should be copied.
1883 *
1884 * return value 0 The command completed successfully.
1885 * return value -EFAULT An error occurred while copying the completion
1886 * data to the user space buffer.
1887 * return value -1 An error occurred while executing the command.
1888 */
Jens Axboe63166682011-09-27 21:27:43 -06001889static int exec_drive_command(struct mtip_port *port, u8 *command,
1890 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001891{
1892 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001893 struct host_to_dev_fis *reply;
1894 u8 *buf = NULL;
1895 dma_addr_t dma_addr = 0;
1896 int rv = 0, xfer_sz = command[3];
1897
1898 if (xfer_sz) {
1899 if (user_buffer)
1900 return -EFAULT;
1901
1902 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1903 ATA_SECT_SIZE * xfer_sz,
1904 &dma_addr,
1905 GFP_KERNEL);
1906 if (!buf) {
1907 dev_err(&port->dd->pdev->dev,
1908 "Memory allocation failed (%d bytes)\n",
1909 ATA_SECT_SIZE * xfer_sz);
1910 return -ENOMEM;
1911 }
1912 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1913 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001914
Sam Bradshaw88523a62011-08-30 08:34:26 -06001915 /* Build the FIS. */
1916 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001917 fis.type = 0x27;
1918 fis.opts = 1 << 7;
1919 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001920 fis.features = command[2];
1921 fis.sect_count = command[3];
1922 if (fis.command == ATA_CMD_SMART) {
1923 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001924 fis.cyl_low = 0x4F;
1925 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001926 }
1927
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001928 if (xfer_sz)
1929 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1930 else
1931 reply = (port->rxfis + RX_FIS_D2H_REG);
1932
Sam Bradshaw88523a62011-08-30 08:34:26 -06001933 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001934 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001935 "feat %x, sectcnt %x\n",
1936 __func__,
1937 command[0],
1938 command[1],
1939 command[2],
1940 command[3]);
1941
Sam Bradshaw88523a62011-08-30 08:34:26 -06001942 /* Execute the command. */
1943 if (mtip_exec_internal_command(port,
1944 &fis,
1945 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001946 (xfer_sz ? dma_addr : 0),
1947 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001948 0,
1949 GFP_KERNEL,
1950 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1951 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001952 rv = -EFAULT;
1953 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001954 }
1955
1956 /* Collect the completion status. */
1957 command[0] = reply->command; /* Status*/
1958 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001959 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001960
1961 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001962 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001963 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001964 __func__,
1965 command[0],
1966 command[1],
1967 command[2]);
1968
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001969 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001970 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001971 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001972 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001973 rv = -EFAULT;
1974 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001975 }
1976 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001977exit_drive_command:
1978 if (buf)
1979 dmam_free_coherent(&port->dd->pdev->dev,
1980 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1981 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001982}
1983
1984/*
1985 * Indicates whether a command has a single sector payload.
1986 *
1987 * @command passed to the device to perform the certain event.
1988 * @features passed to the device to perform the certain event.
1989 *
1990 * return value
1991 * 1 command is one that always has a single sector payload,
1992 * regardless of the value in the Sector Count field.
1993 * 0 otherwise
1994 *
1995 */
1996static unsigned int implicit_sector(unsigned char command,
1997 unsigned char features)
1998{
1999 unsigned int rv = 0;
2000
2001 /* list of commands that have an implicit sector count of 1 */
2002 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002003 case ATA_CMD_SEC_SET_PASS:
2004 case ATA_CMD_SEC_UNLOCK:
2005 case ATA_CMD_SEC_ERASE_PREP:
2006 case ATA_CMD_SEC_ERASE_UNIT:
2007 case ATA_CMD_SEC_FREEZE_LOCK:
2008 case ATA_CMD_SEC_DISABLE_PASS:
2009 case ATA_CMD_PMP_READ:
2010 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06002011 rv = 1;
2012 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002013 case ATA_CMD_SET_MAX:
2014 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002015 rv = 1;
2016 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002017 case ATA_CMD_SMART:
2018 if ((features == ATA_SMART_READ_VALUES) ||
2019 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002020 rv = 1;
2021 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002022 case ATA_CMD_CONF_OVERLAY:
2023 if ((features == ATA_DCO_IDENTIFY) ||
2024 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06002025 rv = 1;
2026 break;
2027 }
2028 return rv;
2029}
2030
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002031static void mtip_set_timeout(struct host_to_dev_fis *fis, unsigned int *timeout)
2032{
2033 switch (fis->command) {
2034 case ATA_CMD_DOWNLOAD_MICRO:
2035 *timeout = 120000; /* 2 minutes */
2036 break;
2037 case ATA_CMD_SEC_ERASE_UNIT:
2038 case 0xFC:
2039 *timeout = 240000; /* 4 minutes */
2040 break;
2041 case ATA_CMD_STANDBYNOW1:
2042 *timeout = 10000; /* 10 seconds */
2043 break;
2044 case 0xF7:
2045 case 0xFA:
2046 *timeout = 60000; /* 60 seconds */
2047 break;
2048 case ATA_CMD_SMART:
2049 *timeout = 15000; /* 15 seconds */
2050 break;
2051 default:
2052 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2053 break;
2054 }
2055}
2056
Sam Bradshaw88523a62011-08-30 08:34:26 -06002057/*
2058 * Executes a taskfile
2059 * See ide_taskfile_ioctl() for derivation
2060 */
2061static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002062 void __user *buf,
2063 ide_task_request_t *req_task,
2064 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002065{
2066 struct host_to_dev_fis fis;
2067 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002068 u8 *outbuf = NULL;
2069 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002070 dma_addr_t outbuf_dma = 0;
2071 dma_addr_t inbuf_dma = 0;
2072 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002073 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002074 unsigned int taskin = 0;
2075 unsigned int taskout = 0;
2076 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002077 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002078 unsigned int force_single_sector;
2079 unsigned int transfer_size;
2080 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002081 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002082
2083 taskout = req_task->out_size;
2084 taskin = req_task->in_size;
2085 /* 130560 = 512 * 0xFF*/
2086 if (taskin > 130560 || taskout > 130560) {
2087 err = -EINVAL;
2088 goto abort;
2089 }
2090
2091 if (taskout) {
2092 outbuf = kzalloc(taskout, GFP_KERNEL);
2093 if (outbuf == NULL) {
2094 err = -ENOMEM;
2095 goto abort;
2096 }
2097 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2098 err = -EFAULT;
2099 goto abort;
2100 }
2101 outbuf_dma = pci_map_single(dd->pdev,
2102 outbuf,
2103 taskout,
2104 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002105 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002106 err = -ENOMEM;
2107 goto abort;
2108 }
2109 dma_buffer = outbuf_dma;
2110 }
2111
2112 if (taskin) {
2113 inbuf = kzalloc(taskin, GFP_KERNEL);
2114 if (inbuf == NULL) {
2115 err = -ENOMEM;
2116 goto abort;
2117 }
2118
2119 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2120 err = -EFAULT;
2121 goto abort;
2122 }
2123 inbuf_dma = pci_map_single(dd->pdev,
2124 inbuf,
2125 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002126 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002127 err = -ENOMEM;
2128 goto abort;
2129 }
2130 dma_buffer = inbuf_dma;
2131 }
2132
2133 /* only supports PIO and non-data commands from this ioctl. */
2134 switch (req_task->data_phase) {
2135 case TASKFILE_OUT:
2136 nsect = taskout / ATA_SECT_SIZE;
2137 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2138 break;
2139 case TASKFILE_IN:
2140 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2141 break;
2142 case TASKFILE_NO_DATA:
2143 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2144 break;
2145 default:
2146 err = -EINVAL;
2147 goto abort;
2148 }
2149
Sam Bradshaw88523a62011-08-30 08:34:26 -06002150 /* Build the FIS. */
2151 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2152
2153 fis.type = 0x27;
2154 fis.opts = 1 << 7;
2155 fis.command = req_task->io_ports[7];
2156 fis.features = req_task->io_ports[1];
2157 fis.sect_count = req_task->io_ports[2];
2158 fis.lba_low = req_task->io_ports[3];
2159 fis.lba_mid = req_task->io_ports[4];
2160 fis.lba_hi = req_task->io_ports[5];
2161 /* Clear the dev bit*/
2162 fis.device = req_task->io_ports[6] & ~0x10;
2163
2164 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2165 req_task->in_flags.all =
2166 IDE_TASKFILE_STD_IN_FLAGS |
2167 (IDE_HOB_STD_IN_FLAGS << 8);
2168 fis.lba_low_ex = req_task->hob_ports[3];
2169 fis.lba_mid_ex = req_task->hob_ports[4];
2170 fis.lba_hi_ex = req_task->hob_ports[5];
2171 fis.features_ex = req_task->hob_ports[1];
2172 fis.sect_cnt_ex = req_task->hob_ports[2];
2173
2174 } else {
2175 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2176 }
2177
2178 force_single_sector = implicit_sector(fis.command, fis.features);
2179
2180 if ((taskin || taskout) && (!fis.sect_count)) {
2181 if (nsect)
2182 fis.sect_count = nsect;
2183 else {
2184 if (!force_single_sector) {
2185 dev_warn(&dd->pdev->dev,
2186 "data movement but "
2187 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002188 err = -EINVAL;
2189 goto abort;
2190 }
2191 }
2192 }
2193
2194 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002195 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2197 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002198 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002199 fis.command,
2200 fis.features,
2201 fis.sect_count,
2202 fis.lba_low,
2203 fis.lba_mid,
2204 fis.lba_hi,
2205 fis.device);
2206
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002207 mtip_set_timeout(&fis, &timeout);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002208
2209 /* Determine the correct transfer size.*/
2210 if (force_single_sector)
2211 transfer_size = ATA_SECT_SIZE;
2212 else
2213 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2214
2215 /* Execute the command.*/
2216 if (mtip_exec_internal_command(dd->port,
2217 &fis,
2218 5,
2219 dma_buffer,
2220 transfer_size,
2221 0,
2222 GFP_KERNEL,
2223 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002224 err = -EIO;
2225 goto abort;
2226 }
2227
2228 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2229
2230 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2231 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2232 req_task->io_ports[7] = reply->control;
2233 } else {
2234 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2235 req_task->io_ports[7] = reply->command;
2236 }
2237
2238 /* reclaim the DMA buffers.*/
2239 if (inbuf_dma)
2240 pci_unmap_single(dd->pdev, inbuf_dma,
2241 taskin, DMA_FROM_DEVICE);
2242 if (outbuf_dma)
2243 pci_unmap_single(dd->pdev, outbuf_dma,
2244 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002245 inbuf_dma = 0;
2246 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002247
2248 /* return the ATA registers to the caller.*/
2249 req_task->io_ports[1] = reply->features;
2250 req_task->io_ports[2] = reply->sect_count;
2251 req_task->io_ports[3] = reply->lba_low;
2252 req_task->io_ports[4] = reply->lba_mid;
2253 req_task->io_ports[5] = reply->lba_hi;
2254 req_task->io_ports[6] = reply->device;
2255
2256 if (req_task->out_flags.all & 1) {
2257
2258 req_task->hob_ports[3] = reply->lba_low_ex;
2259 req_task->hob_ports[4] = reply->lba_mid_ex;
2260 req_task->hob_ports[5] = reply->lba_hi_ex;
2261 req_task->hob_ports[1] = reply->features_ex;
2262 req_task->hob_ports[2] = reply->sect_cnt_ex;
2263 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002264 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002265 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002266 "err %x, sect_cnt %x, lbalo %x,"
2267 "lbamid %x, lbahi %x, dev %x\n",
2268 __func__,
2269 req_task->io_ports[7],
2270 req_task->io_ports[1],
2271 req_task->io_ports[2],
2272 req_task->io_ports[3],
2273 req_task->io_ports[4],
2274 req_task->io_ports[5],
2275 req_task->io_ports[6]);
2276
Sam Bradshaw88523a62011-08-30 08:34:26 -06002277 if (taskout) {
2278 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2279 err = -EFAULT;
2280 goto abort;
2281 }
2282 }
2283 if (taskin) {
2284 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2285 err = -EFAULT;
2286 goto abort;
2287 }
2288 }
2289abort:
2290 if (inbuf_dma)
2291 pci_unmap_single(dd->pdev, inbuf_dma,
2292 taskin, DMA_FROM_DEVICE);
2293 if (outbuf_dma)
2294 pci_unmap_single(dd->pdev, outbuf_dma,
2295 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002296 kfree(outbuf);
2297 kfree(inbuf);
2298
2299 return err;
2300}
2301
2302/*
2303 * Handle IOCTL calls from the Block Layer.
2304 *
2305 * This function is called by the Block Layer when it receives an IOCTL
2306 * command that it does not understand. If the IOCTL command is not supported
2307 * this function returns -ENOTTY.
2308 *
2309 * @dd Pointer to the driver data structure.
2310 * @cmd IOCTL command passed from the Block Layer.
2311 * @arg IOCTL argument passed from the Block Layer.
2312 *
2313 * return value
2314 * 0 The IOCTL completed successfully.
2315 * -ENOTTY The specified command is not supported.
2316 * -EFAULT An error occurred copying data to a user space buffer.
2317 * -EIO An error occurred while executing the command.
2318 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002319static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2320 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002321{
2322 switch (cmd) {
2323 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002324 {
2325 if (copy_to_user((void __user *)arg, dd->port->identify,
2326 sizeof(u16) * ATA_ID_WORDS))
2327 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002328 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002329 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002330 case HDIO_DRIVE_CMD:
2331 {
2332 u8 drive_command[4];
2333
2334 /* Copy the user command info to our buffer. */
2335 if (copy_from_user(drive_command,
2336 (void __user *) arg,
2337 sizeof(drive_command)))
2338 return -EFAULT;
2339
2340 /* Execute the drive command. */
2341 if (exec_drive_command(dd->port,
2342 drive_command,
2343 (void __user *) (arg+4)))
2344 return -EIO;
2345
2346 /* Copy the status back to the users buffer. */
2347 if (copy_to_user((void __user *) arg,
2348 drive_command,
2349 sizeof(drive_command)))
2350 return -EFAULT;
2351
2352 break;
2353 }
2354 case HDIO_DRIVE_TASK:
2355 {
2356 u8 drive_command[7];
2357
2358 /* Copy the user command info to our buffer. */
2359 if (copy_from_user(drive_command,
2360 (void __user *) arg,
2361 sizeof(drive_command)))
2362 return -EFAULT;
2363
2364 /* Execute the drive command. */
2365 if (exec_drive_task(dd->port, drive_command))
2366 return -EIO;
2367
2368 /* Copy the status back to the users buffer. */
2369 if (copy_to_user((void __user *) arg,
2370 drive_command,
2371 sizeof(drive_command)))
2372 return -EFAULT;
2373
2374 break;
2375 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002376 case HDIO_DRIVE_TASKFILE: {
2377 ide_task_request_t req_task;
2378 int ret, outtotal;
2379
2380 if (copy_from_user(&req_task, (void __user *) arg,
2381 sizeof(req_task)))
2382 return -EFAULT;
2383
2384 outtotal = sizeof(req_task);
2385
2386 ret = exec_drive_taskfile(dd, (void __user *) arg,
2387 &req_task, outtotal);
2388
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002389 if (copy_to_user((void __user *) arg, &req_task,
2390 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002391 return -EFAULT;
2392
2393 return ret;
2394 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002395
2396 default:
2397 return -EINVAL;
2398 }
2399 return 0;
2400}
2401
2402/*
2403 * Submit an IO to the hw
2404 *
2405 * This function is called by the block layer to issue an io
2406 * to the device. Upon completion, the callback function will
2407 * be called with the data parameter passed as the callback data.
2408 *
2409 * @dd Pointer to the driver data structure.
2410 * @start First sector to read.
2411 * @nsect Number of sectors to read.
2412 * @nents Number of entries in scatter list for the read command.
2413 * @tag The tag of this read command.
2414 * @callback Pointer to the function that should be called
2415 * when the read completes.
2416 * @data Callback data passed to the callback function
2417 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418 * @dir Direction (read or write)
2419 *
2420 * return value
2421 * None
2422 */
Jens Axboe63166682011-09-27 21:27:43 -06002423static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2424 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002425 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002426{
2427 struct host_to_dev_fis *fis;
2428 struct mtip_port *port = dd->port;
2429 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002430 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431
2432 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002433 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002434
2435 command->scatter_ents = nents;
2436
2437 /*
2438 * The number of retries for this command before it is
2439 * reported as a failure to the upper layers.
2440 */
2441 command->retries = MTIP_MAX_RETRIES;
2442
2443 /* Fill out fis */
2444 fis = command->command;
2445 fis->type = 0x27;
2446 fis->opts = 1 << 7;
2447 fis->command =
2448 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002449 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2450 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002451 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002452 fis->features = nsect & 0xFF;
2453 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002454 fis->sect_count = ((tag << 3) | (tag >> 5));
2455 fis->sect_cnt_ex = 0;
2456 fis->control = 0;
2457 fis->res2 = 0;
2458 fis->res3 = 0;
2459 fill_command_sg(dd, command, nents);
2460
2461 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002462 command->command_header->opts =
2463 __force_bit2int cpu_to_le32(
2464 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002465 command->command_header->byte_count = 0;
2466
2467 /*
2468 * Set the completion function and data for the command
2469 * within this layer.
2470 */
2471 command->comp_data = dd;
2472 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002473 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002474
2475 /*
2476 * Set the completion function and data for the command passed
2477 * from the upper layer.
2478 */
2479 command->async_data = data;
2480 command->async_callback = callback;
2481
2482 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002483 * To prevent this command from being issued
2484 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002485 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002486 if (port->flags & MTIP_PF_PAUSE_IO) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002487 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002488 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002489 return;
2490 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002491
2492 /* Issue the command to the hardware */
2493 mtip_issue_ncq_command(port, tag);
2494
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002495 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002496}
2497
2498/*
2499 * Release a command slot.
2500 *
2501 * @dd Pointer to the driver data structure.
2502 * @tag Slot tag
2503 *
2504 * return value
2505 * None
2506 */
Jens Axboe63166682011-09-27 21:27:43 -06002507static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002508{
2509 release_slot(dd->port, tag);
2510}
2511
2512/*
2513 * Obtain a command slot and return its associated scatter list.
2514 *
2515 * @dd Pointer to the driver data structure.
2516 * @tag Pointer to an int that will receive the allocated command
2517 * slot tag.
2518 *
2519 * return value
2520 * Pointer to the scatter list for the allocated command slot
2521 * or NULL if no command slots are available.
2522 */
Jens Axboe63166682011-09-27 21:27:43 -06002523static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2524 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002525{
2526 /*
2527 * It is possible that, even with this semaphore, a thread
2528 * may think that no command slots are available. Therefore, we
2529 * need to make an attempt to get_slot().
2530 */
2531 down(&dd->port->cmd_slot);
2532 *tag = get_slot(dd->port);
2533
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002534 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002535 up(&dd->port->cmd_slot);
2536 return NULL;
2537 }
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002538 if (unlikely(*tag < 0)) {
2539 up(&dd->port->cmd_slot);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002540 return NULL;
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002541 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002542
2543 return dd->port->commands[*tag].sg;
2544}
2545
2546/*
2547 * Sysfs register/status dump.
2548 *
2549 * @dev Pointer to the device structure, passed by the kernrel.
2550 * @attr Pointer to the device_attribute structure passed by the kernel.
2551 * @buf Pointer to the char buffer that will receive the stats info.
2552 *
2553 * return value
2554 * The size, in bytes, of the data copied into buf.
2555 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002556static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002557 struct device_attribute *attr,
2558 char *buf)
2559{
2560 u32 group_allocated;
2561 struct driver_data *dd = dev_to_disk(dev)->private_data;
2562 int size = 0;
2563 int n;
2564
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002565 size += sprintf(&buf[size], "S ACTive:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002566
2567 for (n = 0; n < dd->slot_groups; n++)
2568 size += sprintf(&buf[size], "0x%08x\n",
2569 readl(dd->port->s_active[n]));
2570
2571 size += sprintf(&buf[size], "Command Issue:\n");
2572
2573 for (n = 0; n < dd->slot_groups; n++)
2574 size += sprintf(&buf[size], "0x%08x\n",
2575 readl(dd->port->cmd_issue[n]));
2576
2577 size += sprintf(&buf[size], "Allocated:\n");
2578
2579 for (n = 0; n < dd->slot_groups; n++) {
2580 if (sizeof(long) > sizeof(u32))
2581 group_allocated =
2582 dd->port->allocated[n/2] >> (32*(n&1));
2583 else
2584 group_allocated = dd->port->allocated[n];
2585 size += sprintf(&buf[size], "0x%08x\n",
2586 group_allocated);
2587 }
2588
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002589 size += sprintf(&buf[size], "Completed:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002590
2591 for (n = 0; n < dd->slot_groups; n++)
2592 size += sprintf(&buf[size], "0x%08x\n",
2593 readl(dd->port->completed[n]));
2594
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002595 size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002596 readl(dd->port->mmio + PORT_IRQ_STAT));
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002597 size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002598 readl(dd->mmio + HOST_IRQ_STAT));
2599
2600 return size;
2601}
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002602
2603static ssize_t mtip_hw_show_status(struct device *dev,
2604 struct device_attribute *attr,
2605 char *buf)
2606{
2607 struct driver_data *dd = dev_to_disk(dev)->private_data;
2608 int size = 0;
2609
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002610 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002611 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002612 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002613 size += sprintf(buf, "%s", "write_protect\n");
2614 else
2615 size += sprintf(buf, "%s", "online\n");
2616
2617 return size;
2618}
2619
Asai Thambi S P45038362012-04-09 08:35:38 +02002620static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002621static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002622
2623/*
2624 * Create the sysfs related attributes.
2625 *
2626 * @dd Pointer to the driver data structure.
2627 * @kobj Pointer to the kobj for the block device.
2628 *
2629 * return value
2630 * 0 Operation completed successfully.
2631 * -EINVAL Invalid parameter.
2632 */
Jens Axboe63166682011-09-27 21:27:43 -06002633static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002634{
2635 if (!kobj || !dd)
2636 return -EINVAL;
2637
2638 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2639 dev_warn(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002640 "Error creating 'registers' sysfs entry\n");
2641 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2642 dev_warn(&dd->pdev->dev,
2643 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002644 return 0;
2645}
2646
2647/*
2648 * Remove the sysfs related attributes.
2649 *
2650 * @dd Pointer to the driver data structure.
2651 * @kobj Pointer to the kobj for the block device.
2652 *
2653 * return value
2654 * 0 Operation completed successfully.
2655 * -EINVAL Invalid parameter.
2656 */
Jens Axboe63166682011-09-27 21:27:43 -06002657static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002658{
2659 if (!kobj || !dd)
2660 return -EINVAL;
2661
2662 sysfs_remove_file(kobj, &dev_attr_registers.attr);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002663 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002664
2665 return 0;
2666}
2667
2668/*
2669 * Perform any init/resume time hardware setup
2670 *
2671 * @dd Pointer to the driver data structure.
2672 *
2673 * return value
2674 * None
2675 */
2676static inline void hba_setup(struct driver_data *dd)
2677{
2678 u32 hwdata;
2679 hwdata = readl(dd->mmio + HOST_HSORG);
2680
2681 /* interrupt bug workaround: use only 1 IS bit.*/
2682 writel(hwdata |
2683 HSORG_DISABLE_SLOTGRP_INTR |
2684 HSORG_DISABLE_SLOTGRP_PXIS,
2685 dd->mmio + HOST_HSORG);
2686}
2687
2688/*
2689 * Detect the details of the product, and store anything needed
2690 * into the driver data structure. This includes product type and
2691 * version and number of slot groups.
2692 *
2693 * @dd Pointer to the driver data structure.
2694 *
2695 * return value
2696 * None
2697 */
2698static void mtip_detect_product(struct driver_data *dd)
2699{
2700 u32 hwdata;
2701 unsigned int rev, slotgroups;
2702
2703 /*
2704 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2705 * info register:
2706 * [15:8] hardware/software interface rev#
2707 * [ 3] asic-style interface
2708 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2709 */
2710 hwdata = readl(dd->mmio + HOST_HSORG);
2711
2712 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2713 dd->slot_groups = 1;
2714
2715 if (hwdata & 0x8) {
2716 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2717 rev = (hwdata & HSORG_HWREV) >> 8;
2718 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2719 dev_info(&dd->pdev->dev,
2720 "ASIC-FPGA design, HS rev 0x%x, "
2721 "%i slot groups [%i slots]\n",
2722 rev,
2723 slotgroups,
2724 slotgroups * 32);
2725
2726 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2727 dev_warn(&dd->pdev->dev,
2728 "Warning: driver only supports "
2729 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2730 slotgroups = MTIP_MAX_SLOT_GROUPS;
2731 }
2732 dd->slot_groups = slotgroups;
2733 return;
2734 }
2735
2736 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2737}
2738
2739/*
2740 * Blocking wait for FTL rebuild to complete
2741 *
2742 * @dd Pointer to the DRIVER_DATA structure.
2743 *
2744 * return value
2745 * 0 FTL rebuild completed successfully
2746 * -EFAULT FTL rebuild error/timeout/interruption
2747 */
2748static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2749{
2750 unsigned long timeout, cnt = 0, start;
2751
2752 dev_warn(&dd->pdev->dev,
2753 "FTL rebuild in progress. Polling for completion.\n");
2754
2755 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002756 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2757
2758 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002759 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002760 &dd->dd_flag)))
2761 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002762 if (mtip_check_surprise_removal(dd->pdev))
2763 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002764
Sam Bradshaw88523a62011-08-30 08:34:26 -06002765 if (mtip_get_identify(dd->port, NULL) < 0)
2766 return -EFAULT;
2767
2768 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2769 MTIP_FTL_REBUILD_MAGIC) {
2770 ssleep(1);
2771 /* Print message every 3 minutes */
2772 if (cnt++ >= 180) {
2773 dev_warn(&dd->pdev->dev,
2774 "FTL rebuild in progress (%d secs).\n",
2775 jiffies_to_msecs(jiffies - start) / 1000);
2776 cnt = 0;
2777 }
2778 } else {
2779 dev_warn(&dd->pdev->dev,
2780 "FTL rebuild complete (%d secs).\n",
2781 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002782 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002783 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002784 }
2785 ssleep(10);
2786 } while (time_before(jiffies, timeout));
2787
2788 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002789 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002790 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2791 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002792 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002793}
2794
2795/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002796 * service thread to issue queued commands
2797 *
2798 * @data Pointer to the driver data structure.
2799 *
2800 * return value
2801 * 0
2802 */
2803
2804static int mtip_service_thread(void *data)
2805{
2806 struct driver_data *dd = (struct driver_data *)data;
2807 unsigned long slot, slot_start, slot_wrap;
2808 unsigned int num_cmd_slots = dd->slot_groups * 32;
2809 struct mtip_port *port = dd->port;
2810
2811 while (1) {
2812 /*
2813 * the condition is to check neither an internal command is
2814 * is in progress nor error handling is active
2815 */
2816 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002817 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002818
2819 if (kthread_should_stop())
2820 break;
2821
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002822 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002823 &dd->dd_flag)))
2824 break;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002825
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002826 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2827 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002828 slot = 1;
2829 /* used to restrict the loop to one iteration */
2830 slot_start = num_cmd_slots;
2831 slot_wrap = 0;
2832 while (1) {
2833 slot = find_next_bit(port->cmds_to_issue,
2834 num_cmd_slots, slot);
2835 if (slot_wrap == 1) {
2836 if ((slot_start >= slot) ||
2837 (slot >= num_cmd_slots))
2838 break;
2839 }
2840 if (unlikely(slot_start == num_cmd_slots))
2841 slot_start = slot;
2842
2843 if (unlikely(slot == num_cmd_slots)) {
2844 slot = 1;
2845 slot_wrap = 1;
2846 continue;
2847 }
2848
2849 /* Issue the command to the hardware */
2850 mtip_issue_ncq_command(port, slot);
2851
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002852 clear_bit(slot, port->cmds_to_issue);
2853 }
2854
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002855 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2856 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02002857 if (!mtip_ftl_rebuild_poll(dd))
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002858 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002859 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002860 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002861 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002862 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002863
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002864 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002865 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002866 }
2867 return 0;
2868}
2869
2870/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002871 * Called once for each card.
2872 *
2873 * @dd Pointer to the driver data structure.
2874 *
2875 * return value
2876 * 0 on success, else an error code.
2877 */
Jens Axboe63166682011-09-27 21:27:43 -06002878static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002879{
2880 int i;
2881 int rv;
2882 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002883 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002884 unsigned char *buf;
2885 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002886
2887 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2888
2889 mtip_detect_product(dd);
2890 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2891 rv = -EIO;
2892 goto out1;
2893 }
2894 num_command_slots = dd->slot_groups * 32;
2895
2896 hba_setup(dd);
2897
Sam Bradshaw88523a62011-08-30 08:34:26 -06002898 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2899
2900 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2901 if (!dd->port) {
2902 dev_err(&dd->pdev->dev,
2903 "Memory allocation: port structure\n");
2904 return -ENOMEM;
2905 }
2906
2907 /* Counting semaphore to track command slot usage */
2908 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2909
2910 /* Spinlock to prevent concurrent issue */
2911 spin_lock_init(&dd->port->cmd_issue_lock);
2912
2913 /* Set the port mmio base address. */
2914 dd->port->mmio = dd->mmio + PORT_OFFSET;
2915 dd->port->dd = dd;
2916
2917 /* Allocate memory for the command list. */
2918 dd->port->command_list =
2919 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002920 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002921 &dd->port->command_list_dma,
2922 GFP_KERNEL);
2923 if (!dd->port->command_list) {
2924 dev_err(&dd->pdev->dev,
2925 "Memory allocation: command list\n");
2926 rv = -ENOMEM;
2927 goto out1;
2928 }
2929
2930 /* Clear the memory we have allocated. */
2931 memset(dd->port->command_list,
2932 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002933 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002934
2935 /* Setup the addresse of the RX FIS. */
2936 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2937 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2938
2939 /* Setup the address of the command tables. */
2940 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2941 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2942
2943 /* Setup the address of the identify data. */
2944 dd->port->identify = dd->port->command_table +
2945 HW_CMD_TBL_AR_SZ;
2946 dd->port->identify_dma = dd->port->command_tbl_dma +
2947 HW_CMD_TBL_AR_SZ;
2948
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002949 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002950 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2951 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2952
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002953 /* Setup the address of the log buf - for read log command */
2954 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
2955 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
2956
2957 /* Setup the address of the smart buf - for smart read data command */
2958 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
2959 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
2960
2961
Sam Bradshaw88523a62011-08-30 08:34:26 -06002962 /* Point the command headers at the command tables. */
2963 for (i = 0; i < num_command_slots; i++) {
2964 dd->port->commands[i].command_header =
2965 dd->port->command_list +
2966 (sizeof(struct mtip_cmd_hdr) * i);
2967 dd->port->commands[i].command_header_dma =
2968 dd->port->command_list_dma +
2969 (sizeof(struct mtip_cmd_hdr) * i);
2970
2971 dd->port->commands[i].command =
2972 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2973 dd->port->commands[i].command_dma =
2974 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2975
2976 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2977 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002978 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002979 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002980 dd->port->commands[i].command_header->ctba =
2981 __force_bit2int cpu_to_le32(
2982 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002983
2984 /*
2985 * If this is not done, a bug is reported by the stock
2986 * FC11 i386. Due to the fact that it has lots of kernel
2987 * debugging enabled.
2988 */
2989 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2990
2991 /* Mark all commands as currently inactive.*/
2992 atomic_set(&dd->port->commands[i].active, 0);
2993 }
2994
2995 /* Setup the pointers to the extended s_active and CI registers. */
2996 for (i = 0; i < dd->slot_groups; i++) {
2997 dd->port->s_active[i] =
2998 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2999 dd->port->cmd_issue[i] =
3000 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3001 dd->port->completed[i] =
3002 dd->port->mmio + i*0x80 + PORT_SDBV;
3003 }
3004
Asai Thambi S P45038362012-04-09 08:35:38 +02003005 timetaken = jiffies;
3006 timeout = jiffies + msecs_to_jiffies(30000);
3007 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3008 time_before(jiffies, timeout)) {
3009 mdelay(100);
3010 }
3011 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3012 timetaken = jiffies - timetaken;
3013 dev_warn(&dd->pdev->dev,
3014 "Surprise removal detected at %u ms\n",
3015 jiffies_to_msecs(timetaken));
3016 rv = -ENODEV;
3017 goto out2 ;
3018 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003019 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003020 timetaken = jiffies - timetaken;
3021 dev_warn(&dd->pdev->dev,
3022 "Removal detected at %u ms\n",
3023 jiffies_to_msecs(timetaken));
3024 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003025 goto out2;
3026 }
3027
Asai Thambi S P45038362012-04-09 08:35:38 +02003028 /* Conditionally reset the HBA. */
3029 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3030 if (mtip_hba_reset(dd) < 0) {
3031 dev_err(&dd->pdev->dev,
3032 "Card did not reset within timeout\n");
3033 rv = -EIO;
3034 goto out2;
3035 }
3036 } else {
3037 /* Clear any pending interrupts on the HBA */
3038 writel(readl(dd->mmio + HOST_IRQ_STAT),
3039 dd->mmio + HOST_IRQ_STAT);
3040 }
3041
Sam Bradshaw88523a62011-08-30 08:34:26 -06003042 mtip_init_port(dd->port);
3043 mtip_start_port(dd->port);
3044
3045 /* Setup the ISR and enable interrupts. */
3046 rv = devm_request_irq(&dd->pdev->dev,
3047 dd->pdev->irq,
3048 mtip_irq_handler,
3049 IRQF_SHARED,
3050 dev_driver_string(&dd->pdev->dev),
3051 dd);
3052
3053 if (rv) {
3054 dev_err(&dd->pdev->dev,
3055 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3056 goto out2;
3057 }
3058
3059 /* Enable interrupts on the HBA. */
3060 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3061 dd->mmio + HOST_CTL);
3062
3063 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003064 init_waitqueue_head(&dd->port->svc_wait);
3065
Sam Bradshaw88523a62011-08-30 08:34:26 -06003066 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3067 dd->port->cmd_timer.function = mtip_timeout_function;
3068 mod_timer(&dd->port->cmd_timer,
3069 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3070
Asai Thambi S P45038362012-04-09 08:35:38 +02003071
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003072 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003073 rv = -EFAULT;
3074 goto out3;
3075 }
3076
Sam Bradshaw88523a62011-08-30 08:34:26 -06003077 if (mtip_get_identify(dd->port, NULL) < 0) {
3078 rv = -EFAULT;
3079 goto out3;
3080 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003081
3082 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3083 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003084 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003085 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003086 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003087 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003088
3089 /* check write protect, over temp and rebuild statuses */
3090 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3091 dd->port->log_buf,
3092 dd->port->log_buf_dma, 1);
3093 if (rv) {
3094 dev_warn(&dd->pdev->dev,
3095 "Error in READ LOG EXT (10h) command\n");
3096 /* non-critical error, don't fail the load */
3097 } else {
3098 buf = (unsigned char *)dd->port->log_buf;
3099 if (buf[259] & 0x1) {
3100 dev_info(&dd->pdev->dev,
3101 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003102 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003103 }
3104 if (buf[288] == 0xF7) {
3105 dev_info(&dd->pdev->dev,
3106 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003107 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003108 }
3109 if (buf[288] == 0xBF) {
3110 dev_info(&dd->pdev->dev,
3111 "Drive indicates rebuild has failed.\n");
3112 /* TODO */
3113 }
3114 }
3115
3116 /* get write protect progess */
3117 memset(&attr242, 0, sizeof(struct smart_attr));
3118 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3119 dev_warn(&dd->pdev->dev,
3120 "Unable to check write protect progress\n");
3121 else
3122 dev_info(&dd->pdev->dev,
3123 "Write protect progress: %d%% (%d blocks)\n",
3124 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003125 return rv;
3126
3127out3:
3128 del_timer_sync(&dd->port->cmd_timer);
3129
3130 /* Disable interrupts on the HBA. */
3131 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3132 dd->mmio + HOST_CTL);
3133
3134 /*Release the IRQ. */
3135 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3136
3137out2:
3138 mtip_deinit_port(dd->port);
3139
3140 /* Free the command/command header memory. */
3141 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003142 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003143 dd->port->command_list,
3144 dd->port->command_list_dma);
3145out1:
3146 /* Free the memory allocated for the for structure. */
3147 kfree(dd->port);
3148
3149 return rv;
3150}
3151
3152/*
3153 * Called to deinitialize an interface.
3154 *
3155 * @dd Pointer to the driver data structure.
3156 *
3157 * return value
3158 * 0
3159 */
Jens Axboe63166682011-09-27 21:27:43 -06003160static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003161{
3162 /*
3163 * Send standby immediate (E0h) to the drive so that it
3164 * saves its state.
3165 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003166 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003167
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003168 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003169 if (mtip_standby_immediate(dd->port))
3170 dev_warn(&dd->pdev->dev,
3171 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003172
3173 /* de-initialize the port. */
3174 mtip_deinit_port(dd->port);
3175
3176 /* Disable interrupts on the HBA. */
3177 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3178 dd->mmio + HOST_CTL);
3179 }
3180
3181 del_timer_sync(&dd->port->cmd_timer);
3182
Sam Bradshaw88523a62011-08-30 08:34:26 -06003183 /* Release the IRQ. */
3184 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3185
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003186 /* Stop the bottom half tasklet. */
3187 tasklet_kill(&dd->tasklet);
3188
Sam Bradshaw88523a62011-08-30 08:34:26 -06003189 /* Free the command/command header memory. */
3190 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003191 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003192 dd->port->command_list,
3193 dd->port->command_list_dma);
3194 /* Free the memory allocated for the for structure. */
3195 kfree(dd->port);
3196
3197 return 0;
3198}
3199
3200/*
3201 * Issue a Standby Immediate command to the device.
3202 *
3203 * This function is called by the Block Layer just before the
3204 * system powers off during a shutdown.
3205 *
3206 * @dd Pointer to the driver data structure.
3207 *
3208 * return value
3209 * 0
3210 */
Jens Axboe63166682011-09-27 21:27:43 -06003211static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003212{
3213 /*
3214 * Send standby immediate (E0h) to the drive so that it
3215 * saves its state.
3216 */
3217 mtip_standby_immediate(dd->port);
3218
3219 return 0;
3220}
3221
3222/*
3223 * Suspend function
3224 *
3225 * This function is called by the Block Layer just before the
3226 * system hibernates.
3227 *
3228 * @dd Pointer to the driver data structure.
3229 *
3230 * return value
3231 * 0 Suspend was successful
3232 * -EFAULT Suspend was not successful
3233 */
Jens Axboe63166682011-09-27 21:27:43 -06003234static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003235{
3236 /*
3237 * Send standby immediate (E0h) to the drive
3238 * so that it saves its state.
3239 */
3240 if (mtip_standby_immediate(dd->port) != 0) {
3241 dev_err(&dd->pdev->dev,
3242 "Failed standby-immediate command\n");
3243 return -EFAULT;
3244 }
3245
3246 /* Disable interrupts on the HBA.*/
3247 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3248 dd->mmio + HOST_CTL);
3249 mtip_deinit_port(dd->port);
3250
3251 return 0;
3252}
3253
3254/*
3255 * Resume function
3256 *
3257 * This function is called by the Block Layer as the
3258 * system resumes.
3259 *
3260 * @dd Pointer to the driver data structure.
3261 *
3262 * return value
3263 * 0 Resume was successful
3264 * -EFAULT Resume was not successful
3265 */
Jens Axboe63166682011-09-27 21:27:43 -06003266static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003267{
3268 /* Perform any needed hardware setup steps */
3269 hba_setup(dd);
3270
3271 /* Reset the HBA */
3272 if (mtip_hba_reset(dd) != 0) {
3273 dev_err(&dd->pdev->dev,
3274 "Unable to reset the HBA\n");
3275 return -EFAULT;
3276 }
3277
3278 /*
3279 * Enable the port, DMA engine, and FIS reception specific
3280 * h/w in controller.
3281 */
3282 mtip_init_port(dd->port);
3283 mtip_start_port(dd->port);
3284
3285 /* Enable interrupts on the HBA.*/
3286 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3287 dd->mmio + HOST_CTL);
3288
3289 return 0;
3290}
3291
3292/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003293 * Helper function for reusing disk name
3294 * upon hot insertion.
3295 */
3296static int rssd_disk_name_format(char *prefix,
3297 int index,
3298 char *buf,
3299 int buflen)
3300{
3301 const int base = 'z' - 'a' + 1;
3302 char *begin = buf + strlen(prefix);
3303 char *end = buf + buflen;
3304 char *p;
3305 int unit;
3306
3307 p = end - 1;
3308 *p = '\0';
3309 unit = base;
3310 do {
3311 if (p == begin)
3312 return -EINVAL;
3313 *--p = 'a' + (index % unit);
3314 index = (index / unit) - 1;
3315 } while (index >= 0);
3316
3317 memmove(begin, p, end - p);
3318 memcpy(buf, prefix, strlen(prefix));
3319
3320 return 0;
3321}
3322
3323/*
3324 * Block layer IOCTL handler.
3325 *
3326 * @dev Pointer to the block_device structure.
3327 * @mode ignored
3328 * @cmd IOCTL command passed from the user application.
3329 * @arg Argument passed from the user application.
3330 *
3331 * return value
3332 * 0 IOCTL completed successfully.
3333 * -ENOTTY IOCTL not supported or invalid driver data
3334 * structure pointer.
3335 */
3336static int mtip_block_ioctl(struct block_device *dev,
3337 fmode_t mode,
3338 unsigned cmd,
3339 unsigned long arg)
3340{
3341 struct driver_data *dd = dev->bd_disk->private_data;
3342
3343 if (!capable(CAP_SYS_ADMIN))
3344 return -EACCES;
3345
3346 if (!dd)
3347 return -ENOTTY;
3348
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003349 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003350 return -ENOTTY;
3351
Sam Bradshaw88523a62011-08-30 08:34:26 -06003352 switch (cmd) {
3353 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003354 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003355 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003356 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003357 }
3358}
3359
Jens Axboe16d02c02011-09-27 15:50:01 -06003360#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003361/*
3362 * Block layer compat IOCTL handler.
3363 *
3364 * @dev Pointer to the block_device structure.
3365 * @mode ignored
3366 * @cmd IOCTL command passed from the user application.
3367 * @arg Argument passed from the user application.
3368 *
3369 * return value
3370 * 0 IOCTL completed successfully.
3371 * -ENOTTY IOCTL not supported or invalid driver data
3372 * structure pointer.
3373 */
3374static int mtip_block_compat_ioctl(struct block_device *dev,
3375 fmode_t mode,
3376 unsigned cmd,
3377 unsigned long arg)
3378{
3379 struct driver_data *dd = dev->bd_disk->private_data;
3380
3381 if (!capable(CAP_SYS_ADMIN))
3382 return -EACCES;
3383
3384 if (!dd)
3385 return -ENOTTY;
3386
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003387 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003388 return -ENOTTY;
3389
Sam Bradshaw88523a62011-08-30 08:34:26 -06003390 switch (cmd) {
3391 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003392 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003393 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003394 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003395 ide_task_request_t req_task;
3396 int compat_tasksize, outtotal, ret;
3397
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003398 compat_tasksize =
3399 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003400
3401 compat_req_task =
3402 (struct mtip_compat_ide_task_request_s __user *) arg;
3403
3404 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003405 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003406 return -EFAULT;
3407
3408 if (get_user(req_task.out_size, &compat_req_task->out_size))
3409 return -EFAULT;
3410
3411 if (get_user(req_task.in_size, &compat_req_task->in_size))
3412 return -EFAULT;
3413
3414 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3415
3416 ret = exec_drive_taskfile(dd, (void __user *) arg,
3417 &req_task, outtotal);
3418
3419 if (copy_to_user((void __user *) arg, &req_task,
3420 compat_tasksize -
3421 (2 * sizeof(compat_long_t))))
3422 return -EFAULT;
3423
3424 if (put_user(req_task.out_size, &compat_req_task->out_size))
3425 return -EFAULT;
3426
3427 if (put_user(req_task.in_size, &compat_req_task->in_size))
3428 return -EFAULT;
3429
3430 return ret;
3431 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003432 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003433 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003434 }
3435}
Jens Axboe16d02c02011-09-27 15:50:01 -06003436#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003437
3438/*
3439 * Obtain the geometry of the device.
3440 *
3441 * You may think that this function is obsolete, but some applications,
3442 * fdisk for example still used CHS values. This function describes the
3443 * device as having 224 heads and 56 sectors per cylinder. These values are
3444 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3445 * partition is described in terms of a start and end cylinder this means
3446 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3447 * affects performance.
3448 *
3449 * @dev Pointer to the block_device strucutre.
3450 * @geo Pointer to a hd_geometry structure.
3451 *
3452 * return value
3453 * 0 Operation completed successfully.
3454 * -ENOTTY An error occurred while reading the drive capacity.
3455 */
3456static int mtip_block_getgeo(struct block_device *dev,
3457 struct hd_geometry *geo)
3458{
3459 struct driver_data *dd = dev->bd_disk->private_data;
3460 sector_t capacity;
3461
3462 if (!dd)
3463 return -ENOTTY;
3464
3465 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3466 dev_warn(&dd->pdev->dev,
3467 "Could not get drive capacity.\n");
3468 return -ENOTTY;
3469 }
3470
3471 geo->heads = 224;
3472 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003473 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003474 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003475 return 0;
3476}
3477
3478/*
3479 * Block device operation function.
3480 *
3481 * This structure contains pointers to the functions required by the block
3482 * layer.
3483 */
3484static const struct block_device_operations mtip_block_ops = {
3485 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003486#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003487 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003488#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003489 .getgeo = mtip_block_getgeo,
3490 .owner = THIS_MODULE
3491};
3492
3493/*
3494 * Block layer make request function.
3495 *
3496 * This function is called by the kernel to process a BIO for
3497 * the P320 device.
3498 *
3499 * @queue Pointer to the request queue. Unused other than to obtain
3500 * the driver data structure.
3501 * @bio Pointer to the BIO.
3502 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003503 */
Jens Axboea71f4832011-11-05 08:36:21 +01003504static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003505{
3506 struct driver_data *dd = queue->queuedata;
3507 struct scatterlist *sg;
3508 struct bio_vec *bvec;
3509 int nents = 0;
3510 int tag = 0;
3511
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003512 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3513 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3514 &dd->dd_flag))) {
3515 bio_endio(bio, -ENXIO);
3516 return;
3517 }
3518 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3519 bio_endio(bio, -ENODATA);
3520 return;
3521 }
3522 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3523 &dd->dd_flag) &&
3524 bio_data_dir(bio))) {
3525 bio_endio(bio, -ENODATA);
3526 return;
3527 }
Asai Thambi S P45038362012-04-09 08:35:38 +02003528 }
3529
Sam Bradshaw88523a62011-08-30 08:34:26 -06003530 if (unlikely(!bio_has_data(bio))) {
3531 blk_queue_flush(queue, 0);
3532 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003533 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534 }
3535
Sam Bradshaw88523a62011-08-30 08:34:26 -06003536 sg = mtip_hw_get_scatterlist(dd, &tag);
3537 if (likely(sg != NULL)) {
3538 blk_queue_bounce(queue, &bio);
3539
3540 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3541 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003542 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003543 bio_io_error(bio);
3544 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003545 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003546 }
3547
3548 /* Create the scatter list for this bio. */
3549 bio_for_each_segment(bvec, bio, nents) {
3550 sg_set_page(&sg[nents],
3551 bvec->bv_page,
3552 bvec->bv_len,
3553 bvec->bv_offset);
3554 }
3555
3556 /* Issue the read/write. */
3557 mtip_hw_submit_io(dd,
3558 bio->bi_sector,
3559 bio_sectors(bio),
3560 nents,
3561 tag,
3562 bio_endio,
3563 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003564 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003565 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003566 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003567}
3568
3569/*
3570 * Block layer initialization function.
3571 *
3572 * This function is called once by the PCI layer for each P320
3573 * device that is connected to the system.
3574 *
3575 * @dd Pointer to the driver data structure.
3576 *
3577 * return value
3578 * 0 on success else an error code.
3579 */
Jens Axboe63166682011-09-27 21:27:43 -06003580static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003581{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003582 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003583 sector_t capacity;
3584 unsigned int index = 0;
3585 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003586 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003587
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003588 if (dd->disk)
3589 goto skip_create_disk; /* hw init done, before rebuild */
3590
Sam Bradshaw88523a62011-08-30 08:34:26 -06003591 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003592 wait_for_rebuild = mtip_hw_init(dd);
3593 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003594 dev_err(&dd->pdev->dev,
3595 "Protocol layer initialization failed\n");
3596 rv = -EINVAL;
3597 goto protocol_init_error;
3598 }
3599
Sam Bradshaw88523a62011-08-30 08:34:26 -06003600 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3601 if (dd->disk == NULL) {
3602 dev_err(&dd->pdev->dev,
3603 "Unable to allocate gendisk structure\n");
3604 rv = -EINVAL;
3605 goto alloc_disk_error;
3606 }
3607
3608 /* Generate the disk name, implemented same as in sd.c */
3609 do {
3610 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3611 goto ida_get_error;
3612
3613 spin_lock(&rssd_index_lock);
3614 rv = ida_get_new(&rssd_index_ida, &index);
3615 spin_unlock(&rssd_index_lock);
3616 } while (rv == -EAGAIN);
3617
3618 if (rv)
3619 goto ida_get_error;
3620
3621 rv = rssd_disk_name_format("rssd",
3622 index,
3623 dd->disk->disk_name,
3624 DISK_NAME_LEN);
3625 if (rv)
3626 goto disk_index_error;
3627
3628 dd->disk->driverfs_dev = &dd->pdev->dev;
3629 dd->disk->major = dd->major;
3630 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3631 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003632 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003633 dd->index = index;
3634
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003635 /*
3636 * if rebuild pending, start the service thread, and delay the block
3637 * queue creation and add_disk()
3638 */
3639 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3640 goto start_service_thread;
3641
3642skip_create_disk:
3643 /* Allocate the request queue. */
3644 dd->queue = blk_alloc_queue(GFP_KERNEL);
3645 if (dd->queue == NULL) {
3646 dev_err(&dd->pdev->dev,
3647 "Unable to allocate request queue\n");
3648 rv = -ENOMEM;
3649 goto block_queue_alloc_init_error;
3650 }
3651
3652 /* Attach our request function to the request queue. */
3653 blk_queue_make_request(dd->queue, mtip_make_request);
3654
3655 dd->disk->queue = dd->queue;
3656 dd->queue->queuedata = dd;
3657
3658 /* Set device limits. */
3659 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3660 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3661 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003662 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3663 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003664 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003665
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003666 /*
3667 * write back cache is not supported in the device. FUA depends on
3668 * write back cache support, hence setting flush support to zero.
3669 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003670 blk_queue_flush(dd->queue, 0);
3671
Sam Bradshaw88523a62011-08-30 08:34:26 -06003672 /* Set the capacity of the device in 512 byte sectors. */
3673 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3674 dev_warn(&dd->pdev->dev,
3675 "Could not read drive capacity\n");
3676 rv = -EIO;
3677 goto read_capacity_error;
3678 }
3679 set_capacity(dd->disk, capacity);
3680
3681 /* Enable the block device and add it to /dev */
3682 add_disk(dd->disk);
3683
3684 /*
3685 * Now that the disk is active, initialize any sysfs attributes
3686 * managed by the protocol layer.
3687 */
3688 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3689 if (kobj) {
3690 mtip_hw_sysfs_init(dd, kobj);
3691 kobject_put(kobj);
3692 }
3693
Asai Thambi S P45038362012-04-09 08:35:38 +02003694 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003695 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003696 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003697 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003698
3699start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003700 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3701
3702 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3703 dd, thd_name);
3704
3705 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003706 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003707 dd->mtip_svc_handler = NULL;
3708 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003709 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003710 }
3711
Asai Thambi S P45038362012-04-09 08:35:38 +02003712 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3713 rv = wait_for_rebuild;
3714
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715 return rv;
3716
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003717kthread_run_error:
3718 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003719 del_gendisk(dd->disk);
3720
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003721read_capacity_error:
3722 blk_cleanup_queue(dd->queue);
3723
3724block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003725disk_index_error:
3726 spin_lock(&rssd_index_lock);
3727 ida_remove(&rssd_index_ida, index);
3728 spin_unlock(&rssd_index_lock);
3729
3730ida_get_error:
3731 put_disk(dd->disk);
3732
3733alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003734 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003735
3736protocol_init_error:
3737 return rv;
3738}
3739
3740/*
3741 * Block layer deinitialization function.
3742 *
3743 * Called by the PCI layer as each P320 device is removed.
3744 *
3745 * @dd Pointer to the driver data structure.
3746 *
3747 * return value
3748 * 0
3749 */
Jens Axboe63166682011-09-27 21:27:43 -06003750static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003751{
3752 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003753
3754 if (dd->mtip_svc_handler) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003755 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003756 wake_up_interruptible(&dd->port->svc_wait);
3757 kthread_stop(dd->mtip_svc_handler);
3758 }
3759
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003760 /* Clean up the sysfs attributes, if created */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003761 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003762 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3763 if (kobj) {
3764 mtip_hw_sysfs_exit(dd, kobj);
3765 kobject_put(kobj);
3766 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003767 }
3768
3769 /*
3770 * Delete our gendisk structure. This also removes the device
3771 * from /dev
3772 */
3773 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003774
3775 spin_lock(&rssd_index_lock);
3776 ida_remove(&rssd_index_ida, dd->index);
3777 spin_unlock(&rssd_index_lock);
3778
Sam Bradshaw88523a62011-08-30 08:34:26 -06003779 blk_cleanup_queue(dd->queue);
3780 dd->disk = NULL;
3781 dd->queue = NULL;
3782
3783 /* De-initialize the protocol layer. */
3784 mtip_hw_exit(dd);
3785
3786 return 0;
3787}
3788
3789/*
3790 * Function called by the PCI layer when just before the
3791 * machine shuts down.
3792 *
3793 * If a protocol layer shutdown function is present it will be called
3794 * by this function.
3795 *
3796 * @dd Pointer to the driver data structure.
3797 *
3798 * return value
3799 * 0
3800 */
Jens Axboe63166682011-09-27 21:27:43 -06003801static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003802{
3803 dev_info(&dd->pdev->dev,
3804 "Shutting down %s ...\n", dd->disk->disk_name);
3805
3806 /* Delete our gendisk structure, and cleanup the blk queue. */
3807 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003808
3809 spin_lock(&rssd_index_lock);
3810 ida_remove(&rssd_index_ida, dd->index);
3811 spin_unlock(&rssd_index_lock);
3812
Sam Bradshaw88523a62011-08-30 08:34:26 -06003813 blk_cleanup_queue(dd->queue);
3814 dd->disk = NULL;
3815 dd->queue = NULL;
3816
3817 mtip_hw_shutdown(dd);
3818 return 0;
3819}
3820
Jens Axboe63166682011-09-27 21:27:43 -06003821static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003822{
3823 dev_info(&dd->pdev->dev,
3824 "Suspending %s ...\n", dd->disk->disk_name);
3825 mtip_hw_suspend(dd);
3826 return 0;
3827}
3828
Jens Axboe63166682011-09-27 21:27:43 -06003829static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003830{
3831 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3832 dd->disk->disk_name);
3833 mtip_hw_resume(dd);
3834 return 0;
3835}
3836
3837/*
3838 * Called for each supported PCI device detected.
3839 *
3840 * This function allocates the private data structure, enables the
3841 * PCI device and then calls the block layer initialization function.
3842 *
3843 * return value
3844 * 0 on success else an error code.
3845 */
3846static int mtip_pci_probe(struct pci_dev *pdev,
3847 const struct pci_device_id *ent)
3848{
3849 int rv = 0;
3850 struct driver_data *dd = NULL;
3851
3852 /* Allocate memory for this devices private data. */
3853 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3854 if (dd == NULL) {
3855 dev_err(&pdev->dev,
3856 "Unable to allocate memory for driver data\n");
3857 return -ENOMEM;
3858 }
3859
Sam Bradshaw88523a62011-08-30 08:34:26 -06003860 /* Attach the private data to this PCI device. */
3861 pci_set_drvdata(pdev, dd);
3862
3863 rv = pcim_enable_device(pdev);
3864 if (rv < 0) {
3865 dev_err(&pdev->dev, "Unable to enable device\n");
3866 goto iomap_err;
3867 }
3868
3869 /* Map BAR5 to memory. */
3870 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3871 if (rv < 0) {
3872 dev_err(&pdev->dev, "Unable to map regions\n");
3873 goto iomap_err;
3874 }
3875
3876 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3877 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3878
3879 if (rv) {
3880 rv = pci_set_consistent_dma_mask(pdev,
3881 DMA_BIT_MASK(32));
3882 if (rv) {
3883 dev_warn(&pdev->dev,
3884 "64-bit DMA enable failed\n");
3885 goto setmask_err;
3886 }
3887 }
3888 }
3889
3890 pci_set_master(pdev);
3891
3892 if (pci_enable_msi(pdev)) {
3893 dev_warn(&pdev->dev,
3894 "Unable to enable MSI interrupt.\n");
3895 goto block_initialize_err;
3896 }
3897
3898 /* Copy the info we may need later into the private data structure. */
3899 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003900 dd->instance = instance;
3901 dd->pdev = pdev;
3902
3903 /* Initialize the block layer. */
3904 rv = mtip_block_initialize(dd);
3905 if (rv < 0) {
3906 dev_err(&pdev->dev,
3907 "Unable to initialize block layer\n");
3908 goto block_initialize_err;
3909 }
3910
3911 /*
3912 * Increment the instance count so that each device has a unique
3913 * instance number.
3914 */
3915 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003916 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003917 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003918 goto done;
3919
3920block_initialize_err:
3921 pci_disable_msi(pdev);
3922
3923setmask_err:
3924 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3925
3926iomap_err:
3927 kfree(dd);
3928 pci_set_drvdata(pdev, NULL);
3929 return rv;
3930done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003931 return rv;
3932}
3933
3934/*
3935 * Called for each probed device when the device is removed or the
3936 * driver is unloaded.
3937 *
3938 * return value
3939 * None
3940 */
3941static void mtip_pci_remove(struct pci_dev *pdev)
3942{
3943 struct driver_data *dd = pci_get_drvdata(pdev);
3944 int counter = 0;
3945
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003946 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02003947
Sam Bradshaw88523a62011-08-30 08:34:26 -06003948 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003949 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003950 counter++;
3951 msleep(20);
3952 if (counter == 10) {
3953 /* Cleanup the outstanding commands */
3954 mtip_command_cleanup(dd);
3955 break;
3956 }
3957 }
3958 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003959
3960 /* Clean up the block layer. */
3961 mtip_block_remove(dd);
3962
3963 pci_disable_msi(pdev);
3964
3965 kfree(dd);
3966 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3967}
3968
3969/*
3970 * Called for each probed device when the device is suspended.
3971 *
3972 * return value
3973 * 0 Success
3974 * <0 Error
3975 */
3976static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3977{
3978 int rv = 0;
3979 struct driver_data *dd = pci_get_drvdata(pdev);
3980
3981 if (!dd) {
3982 dev_err(&pdev->dev,
3983 "Driver private datastructure is NULL\n");
3984 return -EFAULT;
3985 }
3986
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003987 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003988
3989 /* Disable ports & interrupts then send standby immediate */
3990 rv = mtip_block_suspend(dd);
3991 if (rv < 0) {
3992 dev_err(&pdev->dev,
3993 "Failed to suspend controller\n");
3994 return rv;
3995 }
3996
3997 /*
3998 * Save the pci config space to pdev structure &
3999 * disable the device
4000 */
4001 pci_save_state(pdev);
4002 pci_disable_device(pdev);
4003
4004 /* Move to Low power state*/
4005 pci_set_power_state(pdev, PCI_D3hot);
4006
4007 return rv;
4008}
4009
4010/*
4011 * Called for each probed device when the device is resumed.
4012 *
4013 * return value
4014 * 0 Success
4015 * <0 Error
4016 */
4017static int mtip_pci_resume(struct pci_dev *pdev)
4018{
4019 int rv = 0;
4020 struct driver_data *dd;
4021
4022 dd = pci_get_drvdata(pdev);
4023 if (!dd) {
4024 dev_err(&pdev->dev,
4025 "Driver private datastructure is NULL\n");
4026 return -EFAULT;
4027 }
4028
4029 /* Move the device to active State */
4030 pci_set_power_state(pdev, PCI_D0);
4031
4032 /* Restore PCI configuration space */
4033 pci_restore_state(pdev);
4034
4035 /* Enable the PCI device*/
4036 rv = pcim_enable_device(pdev);
4037 if (rv < 0) {
4038 dev_err(&pdev->dev,
4039 "Failed to enable card during resume\n");
4040 goto err;
4041 }
4042 pci_set_master(pdev);
4043
4044 /*
4045 * Calls hbaReset, initPort, & startPort function
4046 * then enables interrupts
4047 */
4048 rv = mtip_block_resume(dd);
4049 if (rv < 0)
4050 dev_err(&pdev->dev, "Unable to resume\n");
4051
4052err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004053 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004054
4055 return rv;
4056}
4057
4058/*
4059 * Shutdown routine
4060 *
4061 * return value
4062 * None
4063 */
4064static void mtip_pci_shutdown(struct pci_dev *pdev)
4065{
4066 struct driver_data *dd = pci_get_drvdata(pdev);
4067 if (dd)
4068 mtip_block_shutdown(dd);
4069}
4070
Sam Bradshaw88523a62011-08-30 08:34:26 -06004071/* Table of device ids supported by this driver. */
4072static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4073 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
4074 { 0 }
4075};
4076
4077/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004078static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004079 .name = MTIP_DRV_NAME,
4080 .id_table = mtip_pci_tbl,
4081 .probe = mtip_pci_probe,
4082 .remove = mtip_pci_remove,
4083 .suspend = mtip_pci_suspend,
4084 .resume = mtip_pci_resume,
4085 .shutdown = mtip_pci_shutdown,
4086};
4087
4088MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4089
4090/*
4091 * Module initialization function.
4092 *
4093 * Called once when the module is loaded. This function allocates a major
4094 * block device number to the Cyclone devices and registers the PCI layer
4095 * of the driver.
4096 *
4097 * Return value
4098 * 0 on success else error code.
4099 */
4100static int __init mtip_init(void)
4101{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004102 int error;
4103
Sam Bradshaw88523a62011-08-30 08:34:26 -06004104 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4105
4106 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004107 error = register_blkdev(0, MTIP_DRV_NAME);
4108 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004109 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004110 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004111 return -EBUSY;
4112 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004113 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004114
4115 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004116 error = pci_register_driver(&mtip_pci_driver);
4117 if (error)
4118 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4119
4120 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004121}
4122
4123/*
4124 * Module de-initialization function.
4125 *
4126 * Called once when the module is unloaded. This function deallocates
4127 * the major block device number allocated by mtip_init() and
4128 * unregisters the PCI layer of the driver.
4129 *
4130 * Return value
4131 * none
4132 */
4133static void __exit mtip_exit(void)
4134{
4135 /* Release the allocated major block device number. */
4136 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4137
4138 /* Unregister the PCI driver. */
4139 pci_unregister_driver(&mtip_pci_driver);
4140}
4141
4142MODULE_AUTHOR("Micron Technology, Inc");
4143MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4144MODULE_LICENSE("GPL");
4145MODULE_VERSION(MTIP_DRV_VERSION);
4146
4147module_init(mtip_init);
4148module_exit(mtip_exit);