blob: bfee50d1b31919afd33f770d5ba776a9e3eba105 [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;
1893 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1894
Sam Bradshaw88523a62011-08-30 08:34:26 -06001895 /* Build the FIS. */
1896 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1897 fis.type = 0x27;
1898 fis.opts = 1 << 7;
1899 fis.command = command[0];
1900 fis.features = command[2];
1901 fis.sect_count = command[3];
1902 if (fis.command == ATA_CMD_SMART) {
1903 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001904 fis.cyl_low = 0x4F;
1905 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001906 }
1907
1908 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001909 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001910 "feat %x, sectcnt %x\n",
1911 __func__,
1912 command[0],
1913 command[1],
1914 command[2],
1915 command[3]);
1916
1917 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1918
1919 /* Execute the command. */
1920 if (mtip_exec_internal_command(port,
1921 &fis,
1922 5,
1923 port->sector_buffer_dma,
1924 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1925 0,
1926 GFP_KERNEL,
1927 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1928 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001929 return -1;
1930 }
1931
1932 /* Collect the completion status. */
1933 command[0] = reply->command; /* Status*/
1934 command[1] = reply->features; /* Error*/
1935 command[2] = command[3];
1936
1937 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001938 " %s: Completion Status: stat %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001939 "err %x, cmd %x\n",
1940 __func__,
1941 command[0],
1942 command[1],
1943 command[2]);
1944
1945 if (user_buffer && command[3]) {
1946 if (copy_to_user(user_buffer,
1947 port->sector_buffer,
1948 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001949 return -EFAULT;
1950 }
1951 }
1952
Sam Bradshaw88523a62011-08-30 08:34:26 -06001953 return 0;
1954}
1955
1956/*
1957 * Indicates whether a command has a single sector payload.
1958 *
1959 * @command passed to the device to perform the certain event.
1960 * @features passed to the device to perform the certain event.
1961 *
1962 * return value
1963 * 1 command is one that always has a single sector payload,
1964 * regardless of the value in the Sector Count field.
1965 * 0 otherwise
1966 *
1967 */
1968static unsigned int implicit_sector(unsigned char command,
1969 unsigned char features)
1970{
1971 unsigned int rv = 0;
1972
1973 /* list of commands that have an implicit sector count of 1 */
1974 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001975 case ATA_CMD_SEC_SET_PASS:
1976 case ATA_CMD_SEC_UNLOCK:
1977 case ATA_CMD_SEC_ERASE_PREP:
1978 case ATA_CMD_SEC_ERASE_UNIT:
1979 case ATA_CMD_SEC_FREEZE_LOCK:
1980 case ATA_CMD_SEC_DISABLE_PASS:
1981 case ATA_CMD_PMP_READ:
1982 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001983 rv = 1;
1984 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001985 case ATA_CMD_SET_MAX:
1986 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001987 rv = 1;
1988 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001989 case ATA_CMD_SMART:
1990 if ((features == ATA_SMART_READ_VALUES) ||
1991 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001992 rv = 1;
1993 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001994 case ATA_CMD_CONF_OVERLAY:
1995 if ((features == ATA_DCO_IDENTIFY) ||
1996 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001997 rv = 1;
1998 break;
1999 }
2000 return rv;
2001}
2002
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002003static void mtip_set_timeout(struct host_to_dev_fis *fis, unsigned int *timeout)
2004{
2005 switch (fis->command) {
2006 case ATA_CMD_DOWNLOAD_MICRO:
2007 *timeout = 120000; /* 2 minutes */
2008 break;
2009 case ATA_CMD_SEC_ERASE_UNIT:
2010 case 0xFC:
2011 *timeout = 240000; /* 4 minutes */
2012 break;
2013 case ATA_CMD_STANDBYNOW1:
2014 *timeout = 10000; /* 10 seconds */
2015 break;
2016 case 0xF7:
2017 case 0xFA:
2018 *timeout = 60000; /* 60 seconds */
2019 break;
2020 case ATA_CMD_SMART:
2021 *timeout = 15000; /* 15 seconds */
2022 break;
2023 default:
2024 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2025 break;
2026 }
2027}
2028
Sam Bradshaw88523a62011-08-30 08:34:26 -06002029/*
2030 * Executes a taskfile
2031 * See ide_taskfile_ioctl() for derivation
2032 */
2033static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06002034 void __user *buf,
2035 ide_task_request_t *req_task,
2036 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002037{
2038 struct host_to_dev_fis fis;
2039 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002040 u8 *outbuf = NULL;
2041 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002042 dma_addr_t outbuf_dma = 0;
2043 dma_addr_t inbuf_dma = 0;
2044 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002045 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002046 unsigned int taskin = 0;
2047 unsigned int taskout = 0;
2048 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002049 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002050 unsigned int force_single_sector;
2051 unsigned int transfer_size;
2052 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002053 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002054
2055 taskout = req_task->out_size;
2056 taskin = req_task->in_size;
2057 /* 130560 = 512 * 0xFF*/
2058 if (taskin > 130560 || taskout > 130560) {
2059 err = -EINVAL;
2060 goto abort;
2061 }
2062
2063 if (taskout) {
2064 outbuf = kzalloc(taskout, GFP_KERNEL);
2065 if (outbuf == NULL) {
2066 err = -ENOMEM;
2067 goto abort;
2068 }
2069 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2070 err = -EFAULT;
2071 goto abort;
2072 }
2073 outbuf_dma = pci_map_single(dd->pdev,
2074 outbuf,
2075 taskout,
2076 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002077 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002078 err = -ENOMEM;
2079 goto abort;
2080 }
2081 dma_buffer = outbuf_dma;
2082 }
2083
2084 if (taskin) {
2085 inbuf = kzalloc(taskin, GFP_KERNEL);
2086 if (inbuf == NULL) {
2087 err = -ENOMEM;
2088 goto abort;
2089 }
2090
2091 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2092 err = -EFAULT;
2093 goto abort;
2094 }
2095 inbuf_dma = pci_map_single(dd->pdev,
2096 inbuf,
2097 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002098 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002099 err = -ENOMEM;
2100 goto abort;
2101 }
2102 dma_buffer = inbuf_dma;
2103 }
2104
2105 /* only supports PIO and non-data commands from this ioctl. */
2106 switch (req_task->data_phase) {
2107 case TASKFILE_OUT:
2108 nsect = taskout / ATA_SECT_SIZE;
2109 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2110 break;
2111 case TASKFILE_IN:
2112 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2113 break;
2114 case TASKFILE_NO_DATA:
2115 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2116 break;
2117 default:
2118 err = -EINVAL;
2119 goto abort;
2120 }
2121
Sam Bradshaw88523a62011-08-30 08:34:26 -06002122 /* Build the FIS. */
2123 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2124
2125 fis.type = 0x27;
2126 fis.opts = 1 << 7;
2127 fis.command = req_task->io_ports[7];
2128 fis.features = req_task->io_ports[1];
2129 fis.sect_count = req_task->io_ports[2];
2130 fis.lba_low = req_task->io_ports[3];
2131 fis.lba_mid = req_task->io_ports[4];
2132 fis.lba_hi = req_task->io_ports[5];
2133 /* Clear the dev bit*/
2134 fis.device = req_task->io_ports[6] & ~0x10;
2135
2136 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2137 req_task->in_flags.all =
2138 IDE_TASKFILE_STD_IN_FLAGS |
2139 (IDE_HOB_STD_IN_FLAGS << 8);
2140 fis.lba_low_ex = req_task->hob_ports[3];
2141 fis.lba_mid_ex = req_task->hob_ports[4];
2142 fis.lba_hi_ex = req_task->hob_ports[5];
2143 fis.features_ex = req_task->hob_ports[1];
2144 fis.sect_cnt_ex = req_task->hob_ports[2];
2145
2146 } else {
2147 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2148 }
2149
2150 force_single_sector = implicit_sector(fis.command, fis.features);
2151
2152 if ((taskin || taskout) && (!fis.sect_count)) {
2153 if (nsect)
2154 fis.sect_count = nsect;
2155 else {
2156 if (!force_single_sector) {
2157 dev_warn(&dd->pdev->dev,
2158 "data movement but "
2159 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002160 err = -EINVAL;
2161 goto abort;
2162 }
2163 }
2164 }
2165
2166 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002167 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002168 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2169 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002170 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002171 fis.command,
2172 fis.features,
2173 fis.sect_count,
2174 fis.lba_low,
2175 fis.lba_mid,
2176 fis.lba_hi,
2177 fis.device);
2178
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002179 mtip_set_timeout(&fis, &timeout);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002180
2181 /* Determine the correct transfer size.*/
2182 if (force_single_sector)
2183 transfer_size = ATA_SECT_SIZE;
2184 else
2185 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2186
2187 /* Execute the command.*/
2188 if (mtip_exec_internal_command(dd->port,
2189 &fis,
2190 5,
2191 dma_buffer,
2192 transfer_size,
2193 0,
2194 GFP_KERNEL,
2195 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 err = -EIO;
2197 goto abort;
2198 }
2199
2200 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2201
2202 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2203 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2204 req_task->io_ports[7] = reply->control;
2205 } else {
2206 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2207 req_task->io_ports[7] = reply->command;
2208 }
2209
2210 /* reclaim the DMA buffers.*/
2211 if (inbuf_dma)
2212 pci_unmap_single(dd->pdev, inbuf_dma,
2213 taskin, DMA_FROM_DEVICE);
2214 if (outbuf_dma)
2215 pci_unmap_single(dd->pdev, outbuf_dma,
2216 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002217 inbuf_dma = 0;
2218 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002219
2220 /* return the ATA registers to the caller.*/
2221 req_task->io_ports[1] = reply->features;
2222 req_task->io_ports[2] = reply->sect_count;
2223 req_task->io_ports[3] = reply->lba_low;
2224 req_task->io_ports[4] = reply->lba_mid;
2225 req_task->io_ports[5] = reply->lba_hi;
2226 req_task->io_ports[6] = reply->device;
2227
2228 if (req_task->out_flags.all & 1) {
2229
2230 req_task->hob_ports[3] = reply->lba_low_ex;
2231 req_task->hob_ports[4] = reply->lba_mid_ex;
2232 req_task->hob_ports[5] = reply->lba_hi_ex;
2233 req_task->hob_ports[1] = reply->features_ex;
2234 req_task->hob_ports[2] = reply->sect_cnt_ex;
2235 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002236 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002237 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002238 "err %x, sect_cnt %x, lbalo %x,"
2239 "lbamid %x, lbahi %x, dev %x\n",
2240 __func__,
2241 req_task->io_ports[7],
2242 req_task->io_ports[1],
2243 req_task->io_ports[2],
2244 req_task->io_ports[3],
2245 req_task->io_ports[4],
2246 req_task->io_ports[5],
2247 req_task->io_ports[6]);
2248
Sam Bradshaw88523a62011-08-30 08:34:26 -06002249 if (taskout) {
2250 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2251 err = -EFAULT;
2252 goto abort;
2253 }
2254 }
2255 if (taskin) {
2256 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2257 err = -EFAULT;
2258 goto abort;
2259 }
2260 }
2261abort:
2262 if (inbuf_dma)
2263 pci_unmap_single(dd->pdev, inbuf_dma,
2264 taskin, DMA_FROM_DEVICE);
2265 if (outbuf_dma)
2266 pci_unmap_single(dd->pdev, outbuf_dma,
2267 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002268 kfree(outbuf);
2269 kfree(inbuf);
2270
2271 return err;
2272}
2273
2274/*
2275 * Handle IOCTL calls from the Block Layer.
2276 *
2277 * This function is called by the Block Layer when it receives an IOCTL
2278 * command that it does not understand. If the IOCTL command is not supported
2279 * this function returns -ENOTTY.
2280 *
2281 * @dd Pointer to the driver data structure.
2282 * @cmd IOCTL command passed from the Block Layer.
2283 * @arg IOCTL argument passed from the Block Layer.
2284 *
2285 * return value
2286 * 0 The IOCTL completed successfully.
2287 * -ENOTTY The specified command is not supported.
2288 * -EFAULT An error occurred copying data to a user space buffer.
2289 * -EIO An error occurred while executing the command.
2290 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002291static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2292 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002293{
2294 switch (cmd) {
2295 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002296 {
2297 if (copy_to_user((void __user *)arg, dd->port->identify,
2298 sizeof(u16) * ATA_ID_WORDS))
2299 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002300 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002301 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002302 case HDIO_DRIVE_CMD:
2303 {
2304 u8 drive_command[4];
2305
2306 /* Copy the user command info to our buffer. */
2307 if (copy_from_user(drive_command,
2308 (void __user *) arg,
2309 sizeof(drive_command)))
2310 return -EFAULT;
2311
2312 /* Execute the drive command. */
2313 if (exec_drive_command(dd->port,
2314 drive_command,
2315 (void __user *) (arg+4)))
2316 return -EIO;
2317
2318 /* Copy the status back to the users buffer. */
2319 if (copy_to_user((void __user *) arg,
2320 drive_command,
2321 sizeof(drive_command)))
2322 return -EFAULT;
2323
2324 break;
2325 }
2326 case HDIO_DRIVE_TASK:
2327 {
2328 u8 drive_command[7];
2329
2330 /* Copy the user command info to our buffer. */
2331 if (copy_from_user(drive_command,
2332 (void __user *) arg,
2333 sizeof(drive_command)))
2334 return -EFAULT;
2335
2336 /* Execute the drive command. */
2337 if (exec_drive_task(dd->port, drive_command))
2338 return -EIO;
2339
2340 /* Copy the status back to the users buffer. */
2341 if (copy_to_user((void __user *) arg,
2342 drive_command,
2343 sizeof(drive_command)))
2344 return -EFAULT;
2345
2346 break;
2347 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002348 case HDIO_DRIVE_TASKFILE: {
2349 ide_task_request_t req_task;
2350 int ret, outtotal;
2351
2352 if (copy_from_user(&req_task, (void __user *) arg,
2353 sizeof(req_task)))
2354 return -EFAULT;
2355
2356 outtotal = sizeof(req_task);
2357
2358 ret = exec_drive_taskfile(dd, (void __user *) arg,
2359 &req_task, outtotal);
2360
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002361 if (copy_to_user((void __user *) arg, &req_task,
2362 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002363 return -EFAULT;
2364
2365 return ret;
2366 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002367
2368 default:
2369 return -EINVAL;
2370 }
2371 return 0;
2372}
2373
2374/*
2375 * Submit an IO to the hw
2376 *
2377 * This function is called by the block layer to issue an io
2378 * to the device. Upon completion, the callback function will
2379 * be called with the data parameter passed as the callback data.
2380 *
2381 * @dd Pointer to the driver data structure.
2382 * @start First sector to read.
2383 * @nsect Number of sectors to read.
2384 * @nents Number of entries in scatter list for the read command.
2385 * @tag The tag of this read command.
2386 * @callback Pointer to the function that should be called
2387 * when the read completes.
2388 * @data Callback data passed to the callback function
2389 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002390 * @dir Direction (read or write)
2391 *
2392 * return value
2393 * None
2394 */
Jens Axboe63166682011-09-27 21:27:43 -06002395static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2396 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002397 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398{
2399 struct host_to_dev_fis *fis;
2400 struct mtip_port *port = dd->port;
2401 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002402 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002403
2404 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002405 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002406
2407 command->scatter_ents = nents;
2408
2409 /*
2410 * The number of retries for this command before it is
2411 * reported as a failure to the upper layers.
2412 */
2413 command->retries = MTIP_MAX_RETRIES;
2414
2415 /* Fill out fis */
2416 fis = command->command;
2417 fis->type = 0x27;
2418 fis->opts = 1 << 7;
2419 fis->command =
2420 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002421 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2422 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002423 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002424 fis->features = nsect & 0xFF;
2425 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002426 fis->sect_count = ((tag << 3) | (tag >> 5));
2427 fis->sect_cnt_ex = 0;
2428 fis->control = 0;
2429 fis->res2 = 0;
2430 fis->res3 = 0;
2431 fill_command_sg(dd, command, nents);
2432
2433 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002434 command->command_header->opts =
2435 __force_bit2int cpu_to_le32(
2436 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002437 command->command_header->byte_count = 0;
2438
2439 /*
2440 * Set the completion function and data for the command
2441 * within this layer.
2442 */
2443 command->comp_data = dd;
2444 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002445 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002446
2447 /*
2448 * Set the completion function and data for the command passed
2449 * from the upper layer.
2450 */
2451 command->async_data = data;
2452 command->async_callback = callback;
2453
2454 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002455 * To prevent this command from being issued
2456 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002457 */
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002458 if (port->flags & MTIP_PF_PAUSE_IO) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002459 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002460 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002461 return;
2462 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002463
2464 /* Issue the command to the hardware */
2465 mtip_issue_ncq_command(port, tag);
2466
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002467 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002468}
2469
2470/*
2471 * Release a command slot.
2472 *
2473 * @dd Pointer to the driver data structure.
2474 * @tag Slot tag
2475 *
2476 * return value
2477 * None
2478 */
Jens Axboe63166682011-09-27 21:27:43 -06002479static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002480{
2481 release_slot(dd->port, tag);
2482}
2483
2484/*
2485 * Obtain a command slot and return its associated scatter list.
2486 *
2487 * @dd Pointer to the driver data structure.
2488 * @tag Pointer to an int that will receive the allocated command
2489 * slot tag.
2490 *
2491 * return value
2492 * Pointer to the scatter list for the allocated command slot
2493 * or NULL if no command slots are available.
2494 */
Jens Axboe63166682011-09-27 21:27:43 -06002495static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2496 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002497{
2498 /*
2499 * It is possible that, even with this semaphore, a thread
2500 * may think that no command slots are available. Therefore, we
2501 * need to make an attempt to get_slot().
2502 */
2503 down(&dd->port->cmd_slot);
2504 *tag = get_slot(dd->port);
2505
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002506 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002507 up(&dd->port->cmd_slot);
2508 return NULL;
2509 }
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002510 if (unlikely(*tag < 0)) {
2511 up(&dd->port->cmd_slot);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002512 return NULL;
Asai Thambi S Pa09ba132012-04-16 21:27:55 +02002513 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002514
2515 return dd->port->commands[*tag].sg;
2516}
2517
2518/*
2519 * Sysfs register/status dump.
2520 *
2521 * @dev Pointer to the device structure, passed by the kernrel.
2522 * @attr Pointer to the device_attribute structure passed by the kernel.
2523 * @buf Pointer to the char buffer that will receive the stats info.
2524 *
2525 * return value
2526 * The size, in bytes, of the data copied into buf.
2527 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002528static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002529 struct device_attribute *attr,
2530 char *buf)
2531{
2532 u32 group_allocated;
2533 struct driver_data *dd = dev_to_disk(dev)->private_data;
2534 int size = 0;
2535 int n;
2536
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002537 size += sprintf(&buf[size], "S ACTive:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002538
2539 for (n = 0; n < dd->slot_groups; n++)
2540 size += sprintf(&buf[size], "0x%08x\n",
2541 readl(dd->port->s_active[n]));
2542
2543 size += sprintf(&buf[size], "Command Issue:\n");
2544
2545 for (n = 0; n < dd->slot_groups; n++)
2546 size += sprintf(&buf[size], "0x%08x\n",
2547 readl(dd->port->cmd_issue[n]));
2548
2549 size += sprintf(&buf[size], "Allocated:\n");
2550
2551 for (n = 0; n < dd->slot_groups; n++) {
2552 if (sizeof(long) > sizeof(u32))
2553 group_allocated =
2554 dd->port->allocated[n/2] >> (32*(n&1));
2555 else
2556 group_allocated = dd->port->allocated[n];
2557 size += sprintf(&buf[size], "0x%08x\n",
2558 group_allocated);
2559 }
2560
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002561 size += sprintf(&buf[size], "Completed:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002562
2563 for (n = 0; n < dd->slot_groups; n++)
2564 size += sprintf(&buf[size], "0x%08x\n",
2565 readl(dd->port->completed[n]));
2566
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002567 size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002568 readl(dd->port->mmio + PORT_IRQ_STAT));
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002569 size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002570 readl(dd->mmio + HOST_IRQ_STAT));
2571
2572 return size;
2573}
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002574
2575static ssize_t mtip_hw_show_status(struct device *dev,
2576 struct device_attribute *attr,
2577 char *buf)
2578{
2579 struct driver_data *dd = dev_to_disk(dev)->private_data;
2580 int size = 0;
2581
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002582 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002583 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002584 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002585 size += sprintf(buf, "%s", "write_protect\n");
2586 else
2587 size += sprintf(buf, "%s", "online\n");
2588
2589 return size;
2590}
2591
Asai Thambi S P45038362012-04-09 08:35:38 +02002592static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002593static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002594
2595/*
2596 * Create the sysfs related attributes.
2597 *
2598 * @dd Pointer to the driver data structure.
2599 * @kobj Pointer to the kobj for the block device.
2600 *
2601 * return value
2602 * 0 Operation completed successfully.
2603 * -EINVAL Invalid parameter.
2604 */
Jens Axboe63166682011-09-27 21:27:43 -06002605static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002606{
2607 if (!kobj || !dd)
2608 return -EINVAL;
2609
2610 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2611 dev_warn(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002612 "Error creating 'registers' sysfs entry\n");
2613 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2614 dev_warn(&dd->pdev->dev,
2615 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002616 return 0;
2617}
2618
2619/*
2620 * Remove the sysfs related attributes.
2621 *
2622 * @dd Pointer to the driver data structure.
2623 * @kobj Pointer to the kobj for the block device.
2624 *
2625 * return value
2626 * 0 Operation completed successfully.
2627 * -EINVAL Invalid parameter.
2628 */
Jens Axboe63166682011-09-27 21:27:43 -06002629static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002630{
2631 if (!kobj || !dd)
2632 return -EINVAL;
2633
2634 sysfs_remove_file(kobj, &dev_attr_registers.attr);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002635 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002636
2637 return 0;
2638}
2639
2640/*
2641 * Perform any init/resume time hardware setup
2642 *
2643 * @dd Pointer to the driver data structure.
2644 *
2645 * return value
2646 * None
2647 */
2648static inline void hba_setup(struct driver_data *dd)
2649{
2650 u32 hwdata;
2651 hwdata = readl(dd->mmio + HOST_HSORG);
2652
2653 /* interrupt bug workaround: use only 1 IS bit.*/
2654 writel(hwdata |
2655 HSORG_DISABLE_SLOTGRP_INTR |
2656 HSORG_DISABLE_SLOTGRP_PXIS,
2657 dd->mmio + HOST_HSORG);
2658}
2659
2660/*
2661 * Detect the details of the product, and store anything needed
2662 * into the driver data structure. This includes product type and
2663 * version and number of slot groups.
2664 *
2665 * @dd Pointer to the driver data structure.
2666 *
2667 * return value
2668 * None
2669 */
2670static void mtip_detect_product(struct driver_data *dd)
2671{
2672 u32 hwdata;
2673 unsigned int rev, slotgroups;
2674
2675 /*
2676 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2677 * info register:
2678 * [15:8] hardware/software interface rev#
2679 * [ 3] asic-style interface
2680 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2681 */
2682 hwdata = readl(dd->mmio + HOST_HSORG);
2683
2684 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2685 dd->slot_groups = 1;
2686
2687 if (hwdata & 0x8) {
2688 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2689 rev = (hwdata & HSORG_HWREV) >> 8;
2690 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2691 dev_info(&dd->pdev->dev,
2692 "ASIC-FPGA design, HS rev 0x%x, "
2693 "%i slot groups [%i slots]\n",
2694 rev,
2695 slotgroups,
2696 slotgroups * 32);
2697
2698 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2699 dev_warn(&dd->pdev->dev,
2700 "Warning: driver only supports "
2701 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2702 slotgroups = MTIP_MAX_SLOT_GROUPS;
2703 }
2704 dd->slot_groups = slotgroups;
2705 return;
2706 }
2707
2708 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2709}
2710
2711/*
2712 * Blocking wait for FTL rebuild to complete
2713 *
2714 * @dd Pointer to the DRIVER_DATA structure.
2715 *
2716 * return value
2717 * 0 FTL rebuild completed successfully
2718 * -EFAULT FTL rebuild error/timeout/interruption
2719 */
2720static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2721{
2722 unsigned long timeout, cnt = 0, start;
2723
2724 dev_warn(&dd->pdev->dev,
2725 "FTL rebuild in progress. Polling for completion.\n");
2726
2727 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002728 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2729
2730 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002731 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002732 &dd->dd_flag)))
2733 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002734 if (mtip_check_surprise_removal(dd->pdev))
2735 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002736
Sam Bradshaw88523a62011-08-30 08:34:26 -06002737 if (mtip_get_identify(dd->port, NULL) < 0)
2738 return -EFAULT;
2739
2740 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2741 MTIP_FTL_REBUILD_MAGIC) {
2742 ssleep(1);
2743 /* Print message every 3 minutes */
2744 if (cnt++ >= 180) {
2745 dev_warn(&dd->pdev->dev,
2746 "FTL rebuild in progress (%d secs).\n",
2747 jiffies_to_msecs(jiffies - start) / 1000);
2748 cnt = 0;
2749 }
2750 } else {
2751 dev_warn(&dd->pdev->dev,
2752 "FTL rebuild complete (%d secs).\n",
2753 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002754 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002755 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002756 }
2757 ssleep(10);
2758 } while (time_before(jiffies, timeout));
2759
2760 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002761 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002762 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2763 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002764 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002765}
2766
2767/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002768 * service thread to issue queued commands
2769 *
2770 * @data Pointer to the driver data structure.
2771 *
2772 * return value
2773 * 0
2774 */
2775
2776static int mtip_service_thread(void *data)
2777{
2778 struct driver_data *dd = (struct driver_data *)data;
2779 unsigned long slot, slot_start, slot_wrap;
2780 unsigned int num_cmd_slots = dd->slot_groups * 32;
2781 struct mtip_port *port = dd->port;
2782
2783 while (1) {
2784 /*
2785 * the condition is to check neither an internal command is
2786 * is in progress nor error handling is active
2787 */
2788 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002789 !(port->flags & MTIP_PF_PAUSE_IO));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002790
2791 if (kthread_should_stop())
2792 break;
2793
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002794 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002795 &dd->dd_flag)))
2796 break;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002797
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002798 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2799 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002800 slot = 1;
2801 /* used to restrict the loop to one iteration */
2802 slot_start = num_cmd_slots;
2803 slot_wrap = 0;
2804 while (1) {
2805 slot = find_next_bit(port->cmds_to_issue,
2806 num_cmd_slots, slot);
2807 if (slot_wrap == 1) {
2808 if ((slot_start >= slot) ||
2809 (slot >= num_cmd_slots))
2810 break;
2811 }
2812 if (unlikely(slot_start == num_cmd_slots))
2813 slot_start = slot;
2814
2815 if (unlikely(slot == num_cmd_slots)) {
2816 slot = 1;
2817 slot_wrap = 1;
2818 continue;
2819 }
2820
2821 /* Issue the command to the hardware */
2822 mtip_issue_ncq_command(port, slot);
2823
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002824 clear_bit(slot, port->cmds_to_issue);
2825 }
2826
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002827 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2828 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02002829 if (!mtip_ftl_rebuild_poll(dd))
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002830 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002831 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002832 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002833 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002834 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002835
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002836 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002837 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002838 }
2839 return 0;
2840}
2841
2842/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002843 * Called once for each card.
2844 *
2845 * @dd Pointer to the driver data structure.
2846 *
2847 * return value
2848 * 0 on success, else an error code.
2849 */
Jens Axboe63166682011-09-27 21:27:43 -06002850static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002851{
2852 int i;
2853 int rv;
2854 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002855 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002856 unsigned char *buf;
2857 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002858
2859 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2860
2861 mtip_detect_product(dd);
2862 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2863 rv = -EIO;
2864 goto out1;
2865 }
2866 num_command_slots = dd->slot_groups * 32;
2867
2868 hba_setup(dd);
2869
Sam Bradshaw88523a62011-08-30 08:34:26 -06002870 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2871
2872 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2873 if (!dd->port) {
2874 dev_err(&dd->pdev->dev,
2875 "Memory allocation: port structure\n");
2876 return -ENOMEM;
2877 }
2878
2879 /* Counting semaphore to track command slot usage */
2880 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2881
2882 /* Spinlock to prevent concurrent issue */
2883 spin_lock_init(&dd->port->cmd_issue_lock);
2884
2885 /* Set the port mmio base address. */
2886 dd->port->mmio = dd->mmio + PORT_OFFSET;
2887 dd->port->dd = dd;
2888
2889 /* Allocate memory for the command list. */
2890 dd->port->command_list =
2891 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002892 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002893 &dd->port->command_list_dma,
2894 GFP_KERNEL);
2895 if (!dd->port->command_list) {
2896 dev_err(&dd->pdev->dev,
2897 "Memory allocation: command list\n");
2898 rv = -ENOMEM;
2899 goto out1;
2900 }
2901
2902 /* Clear the memory we have allocated. */
2903 memset(dd->port->command_list,
2904 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002905 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002906
2907 /* Setup the addresse of the RX FIS. */
2908 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2909 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2910
2911 /* Setup the address of the command tables. */
2912 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2913 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2914
2915 /* Setup the address of the identify data. */
2916 dd->port->identify = dd->port->command_table +
2917 HW_CMD_TBL_AR_SZ;
2918 dd->port->identify_dma = dd->port->command_tbl_dma +
2919 HW_CMD_TBL_AR_SZ;
2920
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002921 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002922 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2923 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2924
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002925 /* Setup the address of the log buf - for read log command */
2926 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
2927 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
2928
2929 /* Setup the address of the smart buf - for smart read data command */
2930 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
2931 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
2932
2933
Sam Bradshaw88523a62011-08-30 08:34:26 -06002934 /* Point the command headers at the command tables. */
2935 for (i = 0; i < num_command_slots; i++) {
2936 dd->port->commands[i].command_header =
2937 dd->port->command_list +
2938 (sizeof(struct mtip_cmd_hdr) * i);
2939 dd->port->commands[i].command_header_dma =
2940 dd->port->command_list_dma +
2941 (sizeof(struct mtip_cmd_hdr) * i);
2942
2943 dd->port->commands[i].command =
2944 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2945 dd->port->commands[i].command_dma =
2946 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2947
2948 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2949 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002950 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002951 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002952 dd->port->commands[i].command_header->ctba =
2953 __force_bit2int cpu_to_le32(
2954 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002955
2956 /*
2957 * If this is not done, a bug is reported by the stock
2958 * FC11 i386. Due to the fact that it has lots of kernel
2959 * debugging enabled.
2960 */
2961 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2962
2963 /* Mark all commands as currently inactive.*/
2964 atomic_set(&dd->port->commands[i].active, 0);
2965 }
2966
2967 /* Setup the pointers to the extended s_active and CI registers. */
2968 for (i = 0; i < dd->slot_groups; i++) {
2969 dd->port->s_active[i] =
2970 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2971 dd->port->cmd_issue[i] =
2972 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2973 dd->port->completed[i] =
2974 dd->port->mmio + i*0x80 + PORT_SDBV;
2975 }
2976
Asai Thambi S P45038362012-04-09 08:35:38 +02002977 timetaken = jiffies;
2978 timeout = jiffies + msecs_to_jiffies(30000);
2979 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2980 time_before(jiffies, timeout)) {
2981 mdelay(100);
2982 }
2983 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
2984 timetaken = jiffies - timetaken;
2985 dev_warn(&dd->pdev->dev,
2986 "Surprise removal detected at %u ms\n",
2987 jiffies_to_msecs(timetaken));
2988 rv = -ENODEV;
2989 goto out2 ;
2990 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002991 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002992 timetaken = jiffies - timetaken;
2993 dev_warn(&dd->pdev->dev,
2994 "Removal detected at %u ms\n",
2995 jiffies_to_msecs(timetaken));
2996 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002997 goto out2;
2998 }
2999
Asai Thambi S P45038362012-04-09 08:35:38 +02003000 /* Conditionally reset the HBA. */
3001 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3002 if (mtip_hba_reset(dd) < 0) {
3003 dev_err(&dd->pdev->dev,
3004 "Card did not reset within timeout\n");
3005 rv = -EIO;
3006 goto out2;
3007 }
3008 } else {
3009 /* Clear any pending interrupts on the HBA */
3010 writel(readl(dd->mmio + HOST_IRQ_STAT),
3011 dd->mmio + HOST_IRQ_STAT);
3012 }
3013
Sam Bradshaw88523a62011-08-30 08:34:26 -06003014 mtip_init_port(dd->port);
3015 mtip_start_port(dd->port);
3016
3017 /* Setup the ISR and enable interrupts. */
3018 rv = devm_request_irq(&dd->pdev->dev,
3019 dd->pdev->irq,
3020 mtip_irq_handler,
3021 IRQF_SHARED,
3022 dev_driver_string(&dd->pdev->dev),
3023 dd);
3024
3025 if (rv) {
3026 dev_err(&dd->pdev->dev,
3027 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3028 goto out2;
3029 }
3030
3031 /* Enable interrupts on the HBA. */
3032 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3033 dd->mmio + HOST_CTL);
3034
3035 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003036 init_waitqueue_head(&dd->port->svc_wait);
3037
Sam Bradshaw88523a62011-08-30 08:34:26 -06003038 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3039 dd->port->cmd_timer.function = mtip_timeout_function;
3040 mod_timer(&dd->port->cmd_timer,
3041 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3042
Asai Thambi S P45038362012-04-09 08:35:38 +02003043
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003044 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003045 rv = -EFAULT;
3046 goto out3;
3047 }
3048
Sam Bradshaw88523a62011-08-30 08:34:26 -06003049 if (mtip_get_identify(dd->port, NULL) < 0) {
3050 rv = -EFAULT;
3051 goto out3;
3052 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003053
3054 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3055 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003056 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003057 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003058 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003059 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003060
3061 /* check write protect, over temp and rebuild statuses */
3062 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3063 dd->port->log_buf,
3064 dd->port->log_buf_dma, 1);
3065 if (rv) {
3066 dev_warn(&dd->pdev->dev,
3067 "Error in READ LOG EXT (10h) command\n");
3068 /* non-critical error, don't fail the load */
3069 } else {
3070 buf = (unsigned char *)dd->port->log_buf;
3071 if (buf[259] & 0x1) {
3072 dev_info(&dd->pdev->dev,
3073 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003074 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003075 }
3076 if (buf[288] == 0xF7) {
3077 dev_info(&dd->pdev->dev,
3078 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003079 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003080 }
3081 if (buf[288] == 0xBF) {
3082 dev_info(&dd->pdev->dev,
3083 "Drive indicates rebuild has failed.\n");
3084 /* TODO */
3085 }
3086 }
3087
3088 /* get write protect progess */
3089 memset(&attr242, 0, sizeof(struct smart_attr));
3090 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3091 dev_warn(&dd->pdev->dev,
3092 "Unable to check write protect progress\n");
3093 else
3094 dev_info(&dd->pdev->dev,
3095 "Write protect progress: %d%% (%d blocks)\n",
3096 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003097 return rv;
3098
3099out3:
3100 del_timer_sync(&dd->port->cmd_timer);
3101
3102 /* Disable interrupts on the HBA. */
3103 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3104 dd->mmio + HOST_CTL);
3105
3106 /*Release the IRQ. */
3107 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3108
3109out2:
3110 mtip_deinit_port(dd->port);
3111
3112 /* Free the command/command header memory. */
3113 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003114 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003115 dd->port->command_list,
3116 dd->port->command_list_dma);
3117out1:
3118 /* Free the memory allocated for the for structure. */
3119 kfree(dd->port);
3120
3121 return rv;
3122}
3123
3124/*
3125 * Called to deinitialize an interface.
3126 *
3127 * @dd Pointer to the driver data structure.
3128 *
3129 * return value
3130 * 0
3131 */
Jens Axboe63166682011-09-27 21:27:43 -06003132static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003133{
3134 /*
3135 * Send standby immediate (E0h) to the drive so that it
3136 * saves its state.
3137 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003138 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003139
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003140 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003141 if (mtip_standby_immediate(dd->port))
3142 dev_warn(&dd->pdev->dev,
3143 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003144
3145 /* de-initialize the port. */
3146 mtip_deinit_port(dd->port);
3147
3148 /* Disable interrupts on the HBA. */
3149 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3150 dd->mmio + HOST_CTL);
3151 }
3152
3153 del_timer_sync(&dd->port->cmd_timer);
3154
Sam Bradshaw88523a62011-08-30 08:34:26 -06003155 /* Release the IRQ. */
3156 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3157
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003158 /* Stop the bottom half tasklet. */
3159 tasklet_kill(&dd->tasklet);
3160
Sam Bradshaw88523a62011-08-30 08:34:26 -06003161 /* Free the command/command header memory. */
3162 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003163 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003164 dd->port->command_list,
3165 dd->port->command_list_dma);
3166 /* Free the memory allocated for the for structure. */
3167 kfree(dd->port);
3168
3169 return 0;
3170}
3171
3172/*
3173 * Issue a Standby Immediate command to the device.
3174 *
3175 * This function is called by the Block Layer just before the
3176 * system powers off during a shutdown.
3177 *
3178 * @dd Pointer to the driver data structure.
3179 *
3180 * return value
3181 * 0
3182 */
Jens Axboe63166682011-09-27 21:27:43 -06003183static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003184{
3185 /*
3186 * Send standby immediate (E0h) to the drive so that it
3187 * saves its state.
3188 */
3189 mtip_standby_immediate(dd->port);
3190
3191 return 0;
3192}
3193
3194/*
3195 * Suspend function
3196 *
3197 * This function is called by the Block Layer just before the
3198 * system hibernates.
3199 *
3200 * @dd Pointer to the driver data structure.
3201 *
3202 * return value
3203 * 0 Suspend was successful
3204 * -EFAULT Suspend was not successful
3205 */
Jens Axboe63166682011-09-27 21:27:43 -06003206static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003207{
3208 /*
3209 * Send standby immediate (E0h) to the drive
3210 * so that it saves its state.
3211 */
3212 if (mtip_standby_immediate(dd->port) != 0) {
3213 dev_err(&dd->pdev->dev,
3214 "Failed standby-immediate command\n");
3215 return -EFAULT;
3216 }
3217
3218 /* Disable interrupts on the HBA.*/
3219 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3220 dd->mmio + HOST_CTL);
3221 mtip_deinit_port(dd->port);
3222
3223 return 0;
3224}
3225
3226/*
3227 * Resume function
3228 *
3229 * This function is called by the Block Layer as the
3230 * system resumes.
3231 *
3232 * @dd Pointer to the driver data structure.
3233 *
3234 * return value
3235 * 0 Resume was successful
3236 * -EFAULT Resume was not successful
3237 */
Jens Axboe63166682011-09-27 21:27:43 -06003238static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003239{
3240 /* Perform any needed hardware setup steps */
3241 hba_setup(dd);
3242
3243 /* Reset the HBA */
3244 if (mtip_hba_reset(dd) != 0) {
3245 dev_err(&dd->pdev->dev,
3246 "Unable to reset the HBA\n");
3247 return -EFAULT;
3248 }
3249
3250 /*
3251 * Enable the port, DMA engine, and FIS reception specific
3252 * h/w in controller.
3253 */
3254 mtip_init_port(dd->port);
3255 mtip_start_port(dd->port);
3256
3257 /* Enable interrupts on the HBA.*/
3258 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3259 dd->mmio + HOST_CTL);
3260
3261 return 0;
3262}
3263
3264/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003265 * Helper function for reusing disk name
3266 * upon hot insertion.
3267 */
3268static int rssd_disk_name_format(char *prefix,
3269 int index,
3270 char *buf,
3271 int buflen)
3272{
3273 const int base = 'z' - 'a' + 1;
3274 char *begin = buf + strlen(prefix);
3275 char *end = buf + buflen;
3276 char *p;
3277 int unit;
3278
3279 p = end - 1;
3280 *p = '\0';
3281 unit = base;
3282 do {
3283 if (p == begin)
3284 return -EINVAL;
3285 *--p = 'a' + (index % unit);
3286 index = (index / unit) - 1;
3287 } while (index >= 0);
3288
3289 memmove(begin, p, end - p);
3290 memcpy(buf, prefix, strlen(prefix));
3291
3292 return 0;
3293}
3294
3295/*
3296 * Block layer IOCTL handler.
3297 *
3298 * @dev Pointer to the block_device structure.
3299 * @mode ignored
3300 * @cmd IOCTL command passed from the user application.
3301 * @arg Argument passed from the user application.
3302 *
3303 * return value
3304 * 0 IOCTL completed successfully.
3305 * -ENOTTY IOCTL not supported or invalid driver data
3306 * structure pointer.
3307 */
3308static int mtip_block_ioctl(struct block_device *dev,
3309 fmode_t mode,
3310 unsigned cmd,
3311 unsigned long arg)
3312{
3313 struct driver_data *dd = dev->bd_disk->private_data;
3314
3315 if (!capable(CAP_SYS_ADMIN))
3316 return -EACCES;
3317
3318 if (!dd)
3319 return -ENOTTY;
3320
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003321 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003322 return -ENOTTY;
3323
Sam Bradshaw88523a62011-08-30 08:34:26 -06003324 switch (cmd) {
3325 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003326 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003327 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003328 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003329 }
3330}
3331
Jens Axboe16d02c02011-09-27 15:50:01 -06003332#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003333/*
3334 * Block layer compat IOCTL handler.
3335 *
3336 * @dev Pointer to the block_device structure.
3337 * @mode ignored
3338 * @cmd IOCTL command passed from the user application.
3339 * @arg Argument passed from the user application.
3340 *
3341 * return value
3342 * 0 IOCTL completed successfully.
3343 * -ENOTTY IOCTL not supported or invalid driver data
3344 * structure pointer.
3345 */
3346static int mtip_block_compat_ioctl(struct block_device *dev,
3347 fmode_t mode,
3348 unsigned cmd,
3349 unsigned long arg)
3350{
3351 struct driver_data *dd = dev->bd_disk->private_data;
3352
3353 if (!capable(CAP_SYS_ADMIN))
3354 return -EACCES;
3355
3356 if (!dd)
3357 return -ENOTTY;
3358
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003359 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003360 return -ENOTTY;
3361
Sam Bradshaw88523a62011-08-30 08:34:26 -06003362 switch (cmd) {
3363 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003364 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003365 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003366 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003367 ide_task_request_t req_task;
3368 int compat_tasksize, outtotal, ret;
3369
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003370 compat_tasksize =
3371 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003372
3373 compat_req_task =
3374 (struct mtip_compat_ide_task_request_s __user *) arg;
3375
3376 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003377 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003378 return -EFAULT;
3379
3380 if (get_user(req_task.out_size, &compat_req_task->out_size))
3381 return -EFAULT;
3382
3383 if (get_user(req_task.in_size, &compat_req_task->in_size))
3384 return -EFAULT;
3385
3386 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3387
3388 ret = exec_drive_taskfile(dd, (void __user *) arg,
3389 &req_task, outtotal);
3390
3391 if (copy_to_user((void __user *) arg, &req_task,
3392 compat_tasksize -
3393 (2 * sizeof(compat_long_t))))
3394 return -EFAULT;
3395
3396 if (put_user(req_task.out_size, &compat_req_task->out_size))
3397 return -EFAULT;
3398
3399 if (put_user(req_task.in_size, &compat_req_task->in_size))
3400 return -EFAULT;
3401
3402 return ret;
3403 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003404 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003405 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003406 }
3407}
Jens Axboe16d02c02011-09-27 15:50:01 -06003408#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003409
3410/*
3411 * Obtain the geometry of the device.
3412 *
3413 * You may think that this function is obsolete, but some applications,
3414 * fdisk for example still used CHS values. This function describes the
3415 * device as having 224 heads and 56 sectors per cylinder. These values are
3416 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3417 * partition is described in terms of a start and end cylinder this means
3418 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3419 * affects performance.
3420 *
3421 * @dev Pointer to the block_device strucutre.
3422 * @geo Pointer to a hd_geometry structure.
3423 *
3424 * return value
3425 * 0 Operation completed successfully.
3426 * -ENOTTY An error occurred while reading the drive capacity.
3427 */
3428static int mtip_block_getgeo(struct block_device *dev,
3429 struct hd_geometry *geo)
3430{
3431 struct driver_data *dd = dev->bd_disk->private_data;
3432 sector_t capacity;
3433
3434 if (!dd)
3435 return -ENOTTY;
3436
3437 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3438 dev_warn(&dd->pdev->dev,
3439 "Could not get drive capacity.\n");
3440 return -ENOTTY;
3441 }
3442
3443 geo->heads = 224;
3444 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003445 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003446 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003447 return 0;
3448}
3449
3450/*
3451 * Block device operation function.
3452 *
3453 * This structure contains pointers to the functions required by the block
3454 * layer.
3455 */
3456static const struct block_device_operations mtip_block_ops = {
3457 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003458#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003459 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003460#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003461 .getgeo = mtip_block_getgeo,
3462 .owner = THIS_MODULE
3463};
3464
3465/*
3466 * Block layer make request function.
3467 *
3468 * This function is called by the kernel to process a BIO for
3469 * the P320 device.
3470 *
3471 * @queue Pointer to the request queue. Unused other than to obtain
3472 * the driver data structure.
3473 * @bio Pointer to the BIO.
3474 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003475 */
Jens Axboea71f4832011-11-05 08:36:21 +01003476static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003477{
3478 struct driver_data *dd = queue->queuedata;
3479 struct scatterlist *sg;
3480 struct bio_vec *bvec;
3481 int nents = 0;
3482 int tag = 0;
3483
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003484 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3485 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3486 &dd->dd_flag))) {
3487 bio_endio(bio, -ENXIO);
3488 return;
3489 }
3490 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3491 bio_endio(bio, -ENODATA);
3492 return;
3493 }
3494 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3495 &dd->dd_flag) &&
3496 bio_data_dir(bio))) {
3497 bio_endio(bio, -ENODATA);
3498 return;
3499 }
Asai Thambi S P45038362012-04-09 08:35:38 +02003500 }
3501
Sam Bradshaw88523a62011-08-30 08:34:26 -06003502 if (unlikely(!bio_has_data(bio))) {
3503 blk_queue_flush(queue, 0);
3504 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003505 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003506 }
3507
Sam Bradshaw88523a62011-08-30 08:34:26 -06003508 sg = mtip_hw_get_scatterlist(dd, &tag);
3509 if (likely(sg != NULL)) {
3510 blk_queue_bounce(queue, &bio);
3511
3512 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3513 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003514 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003515 bio_io_error(bio);
3516 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003517 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003518 }
3519
3520 /* Create the scatter list for this bio. */
3521 bio_for_each_segment(bvec, bio, nents) {
3522 sg_set_page(&sg[nents],
3523 bvec->bv_page,
3524 bvec->bv_len,
3525 bvec->bv_offset);
3526 }
3527
3528 /* Issue the read/write. */
3529 mtip_hw_submit_io(dd,
3530 bio->bi_sector,
3531 bio_sectors(bio),
3532 nents,
3533 tag,
3534 bio_endio,
3535 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003536 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003537 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003538 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003539}
3540
3541/*
3542 * Block layer initialization function.
3543 *
3544 * This function is called once by the PCI layer for each P320
3545 * device that is connected to the system.
3546 *
3547 * @dd Pointer to the driver data structure.
3548 *
3549 * return value
3550 * 0 on success else an error code.
3551 */
Jens Axboe63166682011-09-27 21:27:43 -06003552static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003553{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003554 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003555 sector_t capacity;
3556 unsigned int index = 0;
3557 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003558 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003559
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003560 if (dd->disk)
3561 goto skip_create_disk; /* hw init done, before rebuild */
3562
Sam Bradshaw88523a62011-08-30 08:34:26 -06003563 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003564 wait_for_rebuild = mtip_hw_init(dd);
3565 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003566 dev_err(&dd->pdev->dev,
3567 "Protocol layer initialization failed\n");
3568 rv = -EINVAL;
3569 goto protocol_init_error;
3570 }
3571
Sam Bradshaw88523a62011-08-30 08:34:26 -06003572 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3573 if (dd->disk == NULL) {
3574 dev_err(&dd->pdev->dev,
3575 "Unable to allocate gendisk structure\n");
3576 rv = -EINVAL;
3577 goto alloc_disk_error;
3578 }
3579
3580 /* Generate the disk name, implemented same as in sd.c */
3581 do {
3582 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3583 goto ida_get_error;
3584
3585 spin_lock(&rssd_index_lock);
3586 rv = ida_get_new(&rssd_index_ida, &index);
3587 spin_unlock(&rssd_index_lock);
3588 } while (rv == -EAGAIN);
3589
3590 if (rv)
3591 goto ida_get_error;
3592
3593 rv = rssd_disk_name_format("rssd",
3594 index,
3595 dd->disk->disk_name,
3596 DISK_NAME_LEN);
3597 if (rv)
3598 goto disk_index_error;
3599
3600 dd->disk->driverfs_dev = &dd->pdev->dev;
3601 dd->disk->major = dd->major;
3602 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3603 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003604 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003605 dd->index = index;
3606
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003607 /*
3608 * if rebuild pending, start the service thread, and delay the block
3609 * queue creation and add_disk()
3610 */
3611 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3612 goto start_service_thread;
3613
3614skip_create_disk:
3615 /* Allocate the request queue. */
3616 dd->queue = blk_alloc_queue(GFP_KERNEL);
3617 if (dd->queue == NULL) {
3618 dev_err(&dd->pdev->dev,
3619 "Unable to allocate request queue\n");
3620 rv = -ENOMEM;
3621 goto block_queue_alloc_init_error;
3622 }
3623
3624 /* Attach our request function to the request queue. */
3625 blk_queue_make_request(dd->queue, mtip_make_request);
3626
3627 dd->disk->queue = dd->queue;
3628 dd->queue->queuedata = dd;
3629
3630 /* Set device limits. */
3631 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3632 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3633 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003634 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3635 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003636 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003637
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003638 /*
3639 * write back cache is not supported in the device. FUA depends on
3640 * write back cache support, hence setting flush support to zero.
3641 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003642 blk_queue_flush(dd->queue, 0);
3643
Sam Bradshaw88523a62011-08-30 08:34:26 -06003644 /* Set the capacity of the device in 512 byte sectors. */
3645 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3646 dev_warn(&dd->pdev->dev,
3647 "Could not read drive capacity\n");
3648 rv = -EIO;
3649 goto read_capacity_error;
3650 }
3651 set_capacity(dd->disk, capacity);
3652
3653 /* Enable the block device and add it to /dev */
3654 add_disk(dd->disk);
3655
3656 /*
3657 * Now that the disk is active, initialize any sysfs attributes
3658 * managed by the protocol layer.
3659 */
3660 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3661 if (kobj) {
3662 mtip_hw_sysfs_init(dd, kobj);
3663 kobject_put(kobj);
3664 }
3665
Asai Thambi S P45038362012-04-09 08:35:38 +02003666 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003667 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003668 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003669 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003670
3671start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003672 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3673
3674 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3675 dd, thd_name);
3676
3677 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003678 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003679 dd->mtip_svc_handler = NULL;
3680 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003681 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003682 }
3683
Asai Thambi S P45038362012-04-09 08:35:38 +02003684 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3685 rv = wait_for_rebuild;
3686
Sam Bradshaw88523a62011-08-30 08:34:26 -06003687 return rv;
3688
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003689kthread_run_error:
3690 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003691 del_gendisk(dd->disk);
3692
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003693read_capacity_error:
3694 blk_cleanup_queue(dd->queue);
3695
3696block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003697disk_index_error:
3698 spin_lock(&rssd_index_lock);
3699 ida_remove(&rssd_index_ida, index);
3700 spin_unlock(&rssd_index_lock);
3701
3702ida_get_error:
3703 put_disk(dd->disk);
3704
3705alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003706 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003707
3708protocol_init_error:
3709 return rv;
3710}
3711
3712/*
3713 * Block layer deinitialization function.
3714 *
3715 * Called by the PCI layer as each P320 device is removed.
3716 *
3717 * @dd Pointer to the driver data structure.
3718 *
3719 * return value
3720 * 0
3721 */
Jens Axboe63166682011-09-27 21:27:43 -06003722static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003723{
3724 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003725
3726 if (dd->mtip_svc_handler) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003727 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003728 wake_up_interruptible(&dd->port->svc_wait);
3729 kthread_stop(dd->mtip_svc_handler);
3730 }
3731
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003732 /* Clean up the sysfs attributes, if created */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003733 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003734 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3735 if (kobj) {
3736 mtip_hw_sysfs_exit(dd, kobj);
3737 kobject_put(kobj);
3738 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003739 }
3740
3741 /*
3742 * Delete our gendisk structure. This also removes the device
3743 * from /dev
3744 */
3745 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003746
3747 spin_lock(&rssd_index_lock);
3748 ida_remove(&rssd_index_ida, dd->index);
3749 spin_unlock(&rssd_index_lock);
3750
Sam Bradshaw88523a62011-08-30 08:34:26 -06003751 blk_cleanup_queue(dd->queue);
3752 dd->disk = NULL;
3753 dd->queue = NULL;
3754
3755 /* De-initialize the protocol layer. */
3756 mtip_hw_exit(dd);
3757
3758 return 0;
3759}
3760
3761/*
3762 * Function called by the PCI layer when just before the
3763 * machine shuts down.
3764 *
3765 * If a protocol layer shutdown function is present it will be called
3766 * by this function.
3767 *
3768 * @dd Pointer to the driver data structure.
3769 *
3770 * return value
3771 * 0
3772 */
Jens Axboe63166682011-09-27 21:27:43 -06003773static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003774{
3775 dev_info(&dd->pdev->dev,
3776 "Shutting down %s ...\n", dd->disk->disk_name);
3777
3778 /* Delete our gendisk structure, and cleanup the blk queue. */
3779 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003780
3781 spin_lock(&rssd_index_lock);
3782 ida_remove(&rssd_index_ida, dd->index);
3783 spin_unlock(&rssd_index_lock);
3784
Sam Bradshaw88523a62011-08-30 08:34:26 -06003785 blk_cleanup_queue(dd->queue);
3786 dd->disk = NULL;
3787 dd->queue = NULL;
3788
3789 mtip_hw_shutdown(dd);
3790 return 0;
3791}
3792
Jens Axboe63166682011-09-27 21:27:43 -06003793static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003794{
3795 dev_info(&dd->pdev->dev,
3796 "Suspending %s ...\n", dd->disk->disk_name);
3797 mtip_hw_suspend(dd);
3798 return 0;
3799}
3800
Jens Axboe63166682011-09-27 21:27:43 -06003801static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003802{
3803 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3804 dd->disk->disk_name);
3805 mtip_hw_resume(dd);
3806 return 0;
3807}
3808
3809/*
3810 * Called for each supported PCI device detected.
3811 *
3812 * This function allocates the private data structure, enables the
3813 * PCI device and then calls the block layer initialization function.
3814 *
3815 * return value
3816 * 0 on success else an error code.
3817 */
3818static int mtip_pci_probe(struct pci_dev *pdev,
3819 const struct pci_device_id *ent)
3820{
3821 int rv = 0;
3822 struct driver_data *dd = NULL;
3823
3824 /* Allocate memory for this devices private data. */
3825 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3826 if (dd == NULL) {
3827 dev_err(&pdev->dev,
3828 "Unable to allocate memory for driver data\n");
3829 return -ENOMEM;
3830 }
3831
Sam Bradshaw88523a62011-08-30 08:34:26 -06003832 /* Attach the private data to this PCI device. */
3833 pci_set_drvdata(pdev, dd);
3834
3835 rv = pcim_enable_device(pdev);
3836 if (rv < 0) {
3837 dev_err(&pdev->dev, "Unable to enable device\n");
3838 goto iomap_err;
3839 }
3840
3841 /* Map BAR5 to memory. */
3842 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3843 if (rv < 0) {
3844 dev_err(&pdev->dev, "Unable to map regions\n");
3845 goto iomap_err;
3846 }
3847
3848 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3849 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3850
3851 if (rv) {
3852 rv = pci_set_consistent_dma_mask(pdev,
3853 DMA_BIT_MASK(32));
3854 if (rv) {
3855 dev_warn(&pdev->dev,
3856 "64-bit DMA enable failed\n");
3857 goto setmask_err;
3858 }
3859 }
3860 }
3861
3862 pci_set_master(pdev);
3863
3864 if (pci_enable_msi(pdev)) {
3865 dev_warn(&pdev->dev,
3866 "Unable to enable MSI interrupt.\n");
3867 goto block_initialize_err;
3868 }
3869
3870 /* Copy the info we may need later into the private data structure. */
3871 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003872 dd->instance = instance;
3873 dd->pdev = pdev;
3874
3875 /* Initialize the block layer. */
3876 rv = mtip_block_initialize(dd);
3877 if (rv < 0) {
3878 dev_err(&pdev->dev,
3879 "Unable to initialize block layer\n");
3880 goto block_initialize_err;
3881 }
3882
3883 /*
3884 * Increment the instance count so that each device has a unique
3885 * instance number.
3886 */
3887 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003888 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003889 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003890 goto done;
3891
3892block_initialize_err:
3893 pci_disable_msi(pdev);
3894
3895setmask_err:
3896 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3897
3898iomap_err:
3899 kfree(dd);
3900 pci_set_drvdata(pdev, NULL);
3901 return rv;
3902done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003903 return rv;
3904}
3905
3906/*
3907 * Called for each probed device when the device is removed or the
3908 * driver is unloaded.
3909 *
3910 * return value
3911 * None
3912 */
3913static void mtip_pci_remove(struct pci_dev *pdev)
3914{
3915 struct driver_data *dd = pci_get_drvdata(pdev);
3916 int counter = 0;
3917
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003918 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02003919
Sam Bradshaw88523a62011-08-30 08:34:26 -06003920 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003921 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003922 counter++;
3923 msleep(20);
3924 if (counter == 10) {
3925 /* Cleanup the outstanding commands */
3926 mtip_command_cleanup(dd);
3927 break;
3928 }
3929 }
3930 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003931
3932 /* Clean up the block layer. */
3933 mtip_block_remove(dd);
3934
3935 pci_disable_msi(pdev);
3936
3937 kfree(dd);
3938 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3939}
3940
3941/*
3942 * Called for each probed device when the device is suspended.
3943 *
3944 * return value
3945 * 0 Success
3946 * <0 Error
3947 */
3948static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3949{
3950 int rv = 0;
3951 struct driver_data *dd = pci_get_drvdata(pdev);
3952
3953 if (!dd) {
3954 dev_err(&pdev->dev,
3955 "Driver private datastructure is NULL\n");
3956 return -EFAULT;
3957 }
3958
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003959 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003960
3961 /* Disable ports & interrupts then send standby immediate */
3962 rv = mtip_block_suspend(dd);
3963 if (rv < 0) {
3964 dev_err(&pdev->dev,
3965 "Failed to suspend controller\n");
3966 return rv;
3967 }
3968
3969 /*
3970 * Save the pci config space to pdev structure &
3971 * disable the device
3972 */
3973 pci_save_state(pdev);
3974 pci_disable_device(pdev);
3975
3976 /* Move to Low power state*/
3977 pci_set_power_state(pdev, PCI_D3hot);
3978
3979 return rv;
3980}
3981
3982/*
3983 * Called for each probed device when the device is resumed.
3984 *
3985 * return value
3986 * 0 Success
3987 * <0 Error
3988 */
3989static int mtip_pci_resume(struct pci_dev *pdev)
3990{
3991 int rv = 0;
3992 struct driver_data *dd;
3993
3994 dd = pci_get_drvdata(pdev);
3995 if (!dd) {
3996 dev_err(&pdev->dev,
3997 "Driver private datastructure is NULL\n");
3998 return -EFAULT;
3999 }
4000
4001 /* Move the device to active State */
4002 pci_set_power_state(pdev, PCI_D0);
4003
4004 /* Restore PCI configuration space */
4005 pci_restore_state(pdev);
4006
4007 /* Enable the PCI device*/
4008 rv = pcim_enable_device(pdev);
4009 if (rv < 0) {
4010 dev_err(&pdev->dev,
4011 "Failed to enable card during resume\n");
4012 goto err;
4013 }
4014 pci_set_master(pdev);
4015
4016 /*
4017 * Calls hbaReset, initPort, & startPort function
4018 * then enables interrupts
4019 */
4020 rv = mtip_block_resume(dd);
4021 if (rv < 0)
4022 dev_err(&pdev->dev, "Unable to resume\n");
4023
4024err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004025 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004026
4027 return rv;
4028}
4029
4030/*
4031 * Shutdown routine
4032 *
4033 * return value
4034 * None
4035 */
4036static void mtip_pci_shutdown(struct pci_dev *pdev)
4037{
4038 struct driver_data *dd = pci_get_drvdata(pdev);
4039 if (dd)
4040 mtip_block_shutdown(dd);
4041}
4042
Sam Bradshaw88523a62011-08-30 08:34:26 -06004043/* Table of device ids supported by this driver. */
4044static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4045 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
4046 { 0 }
4047};
4048
4049/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004050static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004051 .name = MTIP_DRV_NAME,
4052 .id_table = mtip_pci_tbl,
4053 .probe = mtip_pci_probe,
4054 .remove = mtip_pci_remove,
4055 .suspend = mtip_pci_suspend,
4056 .resume = mtip_pci_resume,
4057 .shutdown = mtip_pci_shutdown,
4058};
4059
4060MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4061
4062/*
4063 * Module initialization function.
4064 *
4065 * Called once when the module is loaded. This function allocates a major
4066 * block device number to the Cyclone devices and registers the PCI layer
4067 * of the driver.
4068 *
4069 * Return value
4070 * 0 on success else error code.
4071 */
4072static int __init mtip_init(void)
4073{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004074 int error;
4075
Sam Bradshaw88523a62011-08-30 08:34:26 -06004076 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4077
4078 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004079 error = register_blkdev(0, MTIP_DRV_NAME);
4080 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004081 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004082 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004083 return -EBUSY;
4084 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004085 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004086
4087 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004088 error = pci_register_driver(&mtip_pci_driver);
4089 if (error)
4090 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4091
4092 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004093}
4094
4095/*
4096 * Module de-initialization function.
4097 *
4098 * Called once when the module is unloaded. This function deallocates
4099 * the major block device number allocated by mtip_init() and
4100 * unregisters the PCI layer of the driver.
4101 *
4102 * Return value
4103 * none
4104 */
4105static void __exit mtip_exit(void)
4106{
4107 /* Release the allocated major block device number. */
4108 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4109
4110 /* Unregister the PCI driver. */
4111 pci_unregister_driver(&mtip_pci_driver);
4112}
4113
4114MODULE_AUTHOR("Micron Technology, Inc");
4115MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4116MODULE_LICENSE("GPL");
4117MODULE_VERSION(MTIP_DRV_VERSION);
4118
4119module_init(mtip_init);
4120module_exit(mtip_exit);