blob: be96626accec85517b2c6ae84d8307606cbde422 [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 P45038362012-04-09 08:35:38 +0200176 set_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag);
177 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 P45038362012-04-09 08:35:38 +0200274 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))
275 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{
297 unsigned long flags = 0;
298
299 atomic_set(&port->commands[tag].active, 1);
300
301 spin_lock_irqsave(&port->cmd_issue_lock, flags);
302
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
307
308 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200309
310 /* Set the command's timeout value.*/
311 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
312 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600313}
314
315/*
Jens Axboe63166682011-09-27 21:27:43 -0600316 * Enable/disable the reception of FIS
317 *
318 * @port Pointer to the port data structure
319 * @enable 1 to enable, 0 to disable
320 *
321 * return value
322 * Previous state: 1 enabled, 0 disabled
323 */
324static int mtip_enable_fis(struct mtip_port *port, int enable)
325{
326 u32 tmp;
327
328 /* enable FIS reception */
329 tmp = readl(port->mmio + PORT_CMD);
330 if (enable)
331 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332 else
333 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
334
335 /* Flush */
336 readl(port->mmio + PORT_CMD);
337
338 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
339}
340
341/*
342 * Enable/disable the DMA engine
343 *
344 * @port Pointer to the port data structure
345 * @enable 1 to enable, 0 to disable
346 *
347 * return value
348 * Previous state: 1 enabled, 0 disabled.
349 */
350static int mtip_enable_engine(struct mtip_port *port, int enable)
351{
352 u32 tmp;
353
354 /* enable FIS reception */
355 tmp = readl(port->mmio + PORT_CMD);
356 if (enable)
357 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
358 else
359 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
360
361 readl(port->mmio + PORT_CMD);
362 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
363}
364
365/*
366 * Enables the port DMA engine and FIS reception.
367 *
368 * return value
369 * None
370 */
371static inline void mtip_start_port(struct mtip_port *port)
372{
373 /* Enable FIS reception */
374 mtip_enable_fis(port, 1);
375
376 /* Enable the DMA engine */
377 mtip_enable_engine(port, 1);
378}
379
380/*
381 * Deinitialize a port by disabling port interrupts, the DMA engine,
382 * and FIS reception.
383 *
384 * @port Pointer to the port structure
385 *
386 * return value
387 * None
388 */
389static inline void mtip_deinit_port(struct mtip_port *port)
390{
391 /* Disable interrupts on this port */
392 writel(0, port->mmio + PORT_IRQ_MASK);
393
394 /* Disable the DMA engine */
395 mtip_enable_engine(port, 0);
396
397 /* Disable FIS reception */
398 mtip_enable_fis(port, 0);
399}
400
401/*
402 * Initialize a port.
403 *
404 * This function deinitializes the port by calling mtip_deinit_port() and
405 * then initializes it by setting the command header and RX FIS addresses,
406 * clearing the SError register and any pending port interrupts before
407 * re-enabling the default set of port interrupts.
408 *
409 * @port Pointer to the port structure.
410 *
411 * return value
412 * None
413 */
414static void mtip_init_port(struct mtip_port *port)
415{
416 int i;
417 mtip_deinit_port(port);
418
419 /* Program the command list base and FIS base addresses */
420 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
421 writel((port->command_list_dma >> 16) >> 16,
422 port->mmio + PORT_LST_ADDR_HI);
423 writel((port->rxfis_dma >> 16) >> 16,
424 port->mmio + PORT_FIS_ADDR_HI);
425 }
426
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600428 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100429 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600430
431 /* Clear SError */
432 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
433
434 /* reset the completed registers.*/
435 for (i = 0; i < port->dd->slot_groups; i++)
436 writel(0xFFFFFFFF, port->completed[i]);
437
438 /* Clear any pending interrupts for this port */
439 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
440
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100441 /* Clear any pending interrupts on the HBA. */
442 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
443 port->dd->mmio + HOST_IRQ_STAT);
444
Jens Axboe63166682011-09-27 21:27:43 -0600445 /* Enable port interrupts */
446 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
447}
448
449/*
450 * Restart a port
451 *
452 * @port Pointer to the port data structure.
453 *
454 * return value
455 * None
456 */
457static void mtip_restart_port(struct mtip_port *port)
458{
459 unsigned long timeout;
460
461 /* Disable the DMA engine */
462 mtip_enable_engine(port, 0);
463
464 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
465 timeout = jiffies + msecs_to_jiffies(500);
466 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
467 && time_before(jiffies, timeout))
468 ;
469
Asai Thambi S P45038362012-04-09 08:35:38 +0200470 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
471 return;
472
Jens Axboe63166682011-09-27 21:27:43 -0600473 /*
474 * Chip quirk: escalate to hba reset if
475 * PxCMD.CR not clear after 500 ms
476 */
477 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
478 dev_warn(&port->dd->pdev->dev,
479 "PxCMD.CR not clear, escalating reset\n");
480
481 if (hba_reset_nosleep(port->dd))
482 dev_err(&port->dd->pdev->dev,
483 "HBA reset escalation failed.\n");
484
485 /* 30 ms delay before com reset to quiesce chip */
486 mdelay(30);
487 }
488
489 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
490
491 /* Set PxSCTL.DET */
492 writel(readl(port->mmio + PORT_SCR_CTL) |
493 1, port->mmio + PORT_SCR_CTL);
494 readl(port->mmio + PORT_SCR_CTL);
495
496 /* Wait 1 ms to quiesce chip function */
497 timeout = jiffies + msecs_to_jiffies(1);
498 while (time_before(jiffies, timeout))
499 ;
500
Asai Thambi S P45038362012-04-09 08:35:38 +0200501 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
502 return;
503
Jens Axboe63166682011-09-27 21:27:43 -0600504 /* Clear PxSCTL.DET */
505 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
506 port->mmio + PORT_SCR_CTL);
507 readl(port->mmio + PORT_SCR_CTL);
508
509 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
510 timeout = jiffies + msecs_to_jiffies(500);
511 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
512 && time_before(jiffies, timeout))
513 ;
514
Asai Thambi S P45038362012-04-09 08:35:38 +0200515 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
516 return;
517
Jens Axboe63166682011-09-27 21:27:43 -0600518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
521
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100522 mtip_init_port(port);
523 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600524
Jens Axboe63166682011-09-27 21:27:43 -0600525}
526
527/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600528 * Called periodically to see if any read/write commands are
529 * taking too long to complete.
530 *
531 * @data Pointer to the PORT data structure.
532 *
533 * return value
534 * None
535 */
Jens Axboe63166682011-09-27 21:27:43 -0600536static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600537{
538 struct mtip_port *port = (struct mtip_port *) data;
539 struct host_to_dev_fis *fis;
540 struct mtip_cmd *command;
541 int tag, cmdto_cnt = 0;
542 unsigned int bit, group;
543 unsigned int num_command_slots = port->dd->slot_groups * 32;
544
545 if (unlikely(!port))
546 return;
547
Asai Thambi S P45038362012-04-09 08:35:38 +0200548 if (test_bit(MTIP_DD_FLAG_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600549 mod_timer(&port->cmd_timer,
550 jiffies + msecs_to_jiffies(30000));
551 return;
552 }
553
554 for (tag = 0; tag < num_command_slots; tag++) {
555 /*
556 * Skip internal command slot as it has
557 * its own timeout mechanism
558 */
559 if (tag == MTIP_TAG_INTERNAL)
560 continue;
561
562 if (atomic_read(&port->commands[tag].active) &&
563 (time_after(jiffies, port->commands[tag].comp_time))) {
564 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100565 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600566
567 command = &port->commands[tag];
568 fis = (struct host_to_dev_fis *) command->command;
569
570 dev_warn(&port->dd->pdev->dev,
571 "Timeout for command tag %d\n", tag);
572
573 cmdto_cnt++;
574 if (cmdto_cnt == 1)
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100575 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600576
577 /*
578 * Clear the completed bit. This should prevent
579 * any interrupt handlers from trying to retire
580 * the command.
581 */
582 writel(1 << bit, port->completed[group]);
583
584 /* Call the async completion callback. */
585 if (likely(command->async_callback))
586 command->async_callback(command->async_data,
587 -EIO);
588 command->async_callback = NULL;
589 command->comp_func = NULL;
590
591 /* Unmap the DMA scatter list entries */
592 dma_unmap_sg(&port->dd->pdev->dev,
593 command->sg,
594 command->scatter_ents,
595 command->direction);
596
597 /*
598 * Clear the allocated bit and active tag for the
599 * command.
600 */
601 atomic_set(&port->commands[tag].active, 0);
602 release_slot(port, tag);
603
604 up(&port->cmd_slot);
605 }
606 }
607
608 if (cmdto_cnt) {
609 dev_warn(&port->dd->pdev->dev,
610 "%d commands timed out: restarting port",
611 cmdto_cnt);
612 mtip_restart_port(port);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100613 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
614 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600615 }
616
617 /* Restart the timer */
618 mod_timer(&port->cmd_timer,
619 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
620}
621
622/*
623 * IO completion function.
624 *
625 * This completion function is called by the driver ISR when a
626 * command that was issued by the kernel completes. It first calls the
627 * asynchronous completion function which normally calls back into the block
628 * layer passing the asynchronous callback data, then unmaps the
629 * scatter list associated with the completed command, and finally
630 * clears the allocated bit associated with the completed command.
631 *
632 * @port Pointer to the port data structure.
633 * @tag Tag of the command.
634 * @data Pointer to driver_data.
635 * @status Completion status.
636 *
637 * return value
638 * None
639 */
640static void mtip_async_complete(struct mtip_port *port,
641 int tag,
642 void *data,
643 int status)
644{
645 struct mtip_cmd *command;
646 struct driver_data *dd = data;
647 int cb_status = status ? -EIO : 0;
648
649 if (unlikely(!dd) || unlikely(!port))
650 return;
651
652 command = &port->commands[tag];
653
654 if (unlikely(status == PORT_IRQ_TF_ERR)) {
655 dev_warn(&port->dd->pdev->dev,
656 "Command tag %d failed due to TFE\n", tag);
657 }
658
659 /* Upper layer callback */
660 if (likely(command->async_callback))
661 command->async_callback(command->async_data, cb_status);
662
663 command->async_callback = NULL;
664 command->comp_func = NULL;
665
666 /* Unmap the DMA scatter list entries */
667 dma_unmap_sg(&dd->pdev->dev,
668 command->sg,
669 command->scatter_ents,
670 command->direction);
671
672 /* Clear the allocated and active bits for the command */
673 atomic_set(&port->commands[tag].active, 0);
674 release_slot(port, tag);
675
676 up(&port->cmd_slot);
677}
678
679/*
680 * Internal command completion callback function.
681 *
682 * This function is normally called by the driver ISR when an internal
683 * command completed. This function signals the command completion by
684 * calling complete().
685 *
686 * @port Pointer to the port data structure.
687 * @tag Tag of the command that has completed.
688 * @data Pointer to a completion structure.
689 * @status Completion status.
690 *
691 * return value
692 * None
693 */
694static void mtip_completion(struct mtip_port *port,
695 int tag,
696 void *data,
697 int status)
698{
699 struct mtip_cmd *command = &port->commands[tag];
700 struct completion *waiting = data;
701 if (unlikely(status == PORT_IRQ_TF_ERR))
702 dev_warn(&port->dd->pdev->dev,
703 "Internal command %d completed with TFE\n", tag);
704
705 command->async_callback = NULL;
706 command->comp_func = NULL;
707
708 complete(waiting);
709}
710
Asai Thambi S P8182b492012-04-09 08:35:38 +0200711static void mtip_null_completion(struct mtip_port *port,
712 int tag,
713 void *data,
714 int status)
715{
716 return;
717}
718
Sam Bradshaw88523a62011-08-30 08:34:26 -0600719/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600720 * Helper function for tag logging
721 */
722static void print_tags(struct driver_data *dd,
723 char *msg,
724 unsigned long *tagbits)
725{
726 unsigned int tag, count = 0;
727
728 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
729 if (test_bit(tag, tagbits))
730 count++;
731 }
732 if (count)
733 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
734}
735
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200736static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
737 dma_addr_t buffer_dma, unsigned int sectors);
738static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
739 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600740/*
741 * Handle an error.
742 *
743 * @dd Pointer to the DRIVER_DATA structure.
744 *
745 * return value
746 * None
747 */
748static void mtip_handle_tfe(struct driver_data *dd)
749{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200750 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600751 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200752 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600753 u32 completed;
754 struct host_to_dev_fis *fis;
755 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200756 unsigned char *buf;
757 char *fail_reason = NULL;
758 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600759
760 dev_warn(&dd->pdev->dev, "Taskfile error\n");
761
762 port = dd->port;
763
764 /* Stop the timer to prevent command timeouts. */
765 del_timer(&port->cmd_timer);
766
767 /* Set eh_active */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100768 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600769
770 /* Loop through all the groups */
771 for (group = 0; group < dd->slot_groups; group++) {
772 completed = readl(port->completed[group]);
773
774 /* clear completed status register in the hardware.*/
775 writel(completed, port->completed[group]);
776
777 /* clear the tag accumulator */
778 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
779
780 /* Process successfully completed commands */
781 for (bit = 0; bit < 32 && completed; bit++) {
782 if (!(completed & (1<<bit)))
783 continue;
784 tag = (group << 5) + bit;
785
786 /* Skip the internal command slot */
787 if (tag == MTIP_TAG_INTERNAL)
788 continue;
789
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200790 cmd = &port->commands[tag];
791 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600792 set_bit(tag, tagaccum);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200793 atomic_set(&cmd->active, 0);
794 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600795 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200796 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600797 0);
798 } else {
799 dev_err(&port->dd->pdev->dev,
800 "Missing completion func for tag %d",
801 tag);
802 if (mtip_check_surprise_removal(dd->pdev)) {
803 mtip_command_cleanup(dd);
804 /* don't proceed further */
805 return;
806 }
807 }
808 }
809 }
810 print_tags(dd, "TFE tags completed:", tagaccum);
811
812 /* Restart the port */
813 mdelay(20);
814 mtip_restart_port(port);
815
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200816 /* Trying to determine the cause of the error */
817 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
818 dd->port->log_buf,
819 dd->port->log_buf_dma, 1);
820 if (rv) {
821 dev_warn(&dd->pdev->dev,
822 "Error in READ LOG EXT (10h) command\n");
823 /* non-critical error, don't fail the load */
824 } else {
825 buf = (unsigned char *)dd->port->log_buf;
826 if (buf[259] & 0x1) {
827 dev_info(&dd->pdev->dev,
828 "Write protect bit is set.\n");
829 set_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag);
830 fail_all_ncq_write = 1;
831 fail_reason = "write protect";
832 }
833 if (buf[288] == 0xF7) {
834 dev_info(&dd->pdev->dev,
835 "Exceeded Tmax, drive in thermal shutdown.\n");
836 set_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag);
837 fail_all_ncq_cmds = 1;
838 fail_reason = "thermal shutdown";
839 }
840 if (buf[288] == 0xBF) {
841 dev_info(&dd->pdev->dev,
842 "Drive indicates rebuild has failed.\n");
843 fail_all_ncq_cmds = 1;
844 fail_reason = "rebuild failed";
845 }
846 }
847
Sam Bradshaw88523a62011-08-30 08:34:26 -0600848 /* clear the tag accumulator */
849 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
850
851 /* Loop through all the groups */
852 for (group = 0; group < dd->slot_groups; group++) {
853 for (bit = 0; bit < 32; bit++) {
854 reissue = 1;
855 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200856 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600857
858 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200859 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600860 continue;
861
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200862 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600863
864 /* Should re-issue? */
865 if (tag == MTIP_TAG_INTERNAL ||
866 fis->command == ATA_CMD_SET_FEATURES)
867 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200868 else {
869 if (fail_all_ncq_cmds ||
870 (fail_all_ncq_write &&
871 fis->command == ATA_CMD_FPDMA_WRITE)) {
872 dev_warn(&dd->pdev->dev,
873 " Fail: %s w/tag %d [%s].\n",
874 fis->command == ATA_CMD_FPDMA_WRITE ?
875 "write" : "read",
876 tag,
877 fail_reason != NULL ?
878 fail_reason : "unknown");
879 atomic_set(&cmd->active, 0);
880 if (cmd->comp_func) {
881 cmd->comp_func(port, tag,
882 cmd->comp_data,
883 -ENODATA);
884 }
885 continue;
886 }
887 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600888
889 /*
890 * First check if this command has
891 * exceeded its retries.
892 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200893 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600894
895 set_bit(tag, tagaccum);
896
Sam Bradshaw88523a62011-08-30 08:34:26 -0600897 /* Re-issue the command. */
898 mtip_issue_ncq_command(port, tag);
899
900 continue;
901 }
902
903 /* Retire a command that will not be reissued */
904 dev_warn(&port->dd->pdev->dev,
905 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200906 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600907
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200908 if (cmd->comp_func)
909 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -0600910 port,
911 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200912 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600913 PORT_IRQ_TF_ERR);
914 else
915 dev_warn(&port->dd->pdev->dev,
916 "Bad completion for tag %d\n",
917 tag);
918 }
919 }
920 print_tags(dd, "TFE tags reissued:", tagaccum);
921
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100922 /* clear eh_active */
923 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
924 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600925
926 mod_timer(&port->cmd_timer,
927 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
928}
929
930/*
931 * Handle a set device bits interrupt
932 */
933static inline void mtip_process_sdbf(struct driver_data *dd)
934{
935 struct mtip_port *port = dd->port;
936 int group, tag, bit;
937 u32 completed;
938 struct mtip_cmd *command;
939
940 /* walk all bits in all slot groups */
941 for (group = 0; group < dd->slot_groups; group++) {
942 completed = readl(port->completed[group]);
943
944 /* clear completed status register in the hardware.*/
945 writel(completed, port->completed[group]);
946
947 /* Process completed commands. */
948 for (bit = 0;
949 (bit < 32) && completed;
950 bit++, completed >>= 1) {
951 if (completed & 0x01) {
952 tag = (group << 5) | bit;
953
954 /* skip internal command slot. */
955 if (unlikely(tag == MTIP_TAG_INTERNAL))
956 continue;
957
958 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600959 /* make internal callback */
960 if (likely(command->comp_func)) {
961 command->comp_func(
962 port,
963 tag,
964 command->comp_data,
965 0);
966 } else {
967 dev_warn(&dd->pdev->dev,
968 "Null completion "
969 "for tag %d",
970 tag);
971
972 if (mtip_check_surprise_removal(
973 dd->pdev)) {
974 mtip_command_cleanup(dd);
975 return;
976 }
977 }
978 }
979 }
980 }
981}
982
983/*
984 * Process legacy pio and d2h interrupts
985 */
986static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
987{
988 struct mtip_port *port = dd->port;
989 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
990
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100991 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
992 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600993 & (1 << MTIP_TAG_INTERNAL))) {
994 if (cmd->comp_func) {
995 cmd->comp_func(port,
996 MTIP_TAG_INTERNAL,
997 cmd->comp_data,
998 0);
999 return;
1000 }
1001 }
1002
Sam Bradshaw88523a62011-08-30 08:34:26 -06001003 return;
1004}
1005
1006/*
1007 * Demux and handle errors
1008 */
1009static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1010{
1011 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1012 mtip_handle_tfe(dd);
1013
1014 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1015 dev_warn(&dd->pdev->dev,
1016 "Clearing PxSERR.DIAG.x\n");
1017 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1018 }
1019
1020 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1021 dev_warn(&dd->pdev->dev,
1022 "Clearing PxSERR.DIAG.n\n");
1023 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1024 }
1025
1026 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1027 dev_warn(&dd->pdev->dev,
1028 "Port stat errors %x unhandled\n",
1029 (port_stat & ~PORT_IRQ_HANDLED));
1030 }
1031}
1032
1033static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1034{
1035 struct driver_data *dd = (struct driver_data *) data;
1036 struct mtip_port *port = dd->port;
1037 u32 hba_stat, port_stat;
1038 int rv = IRQ_NONE;
1039
1040 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1041 if (hba_stat) {
1042 rv = IRQ_HANDLED;
1043
1044 /* Acknowledge the interrupt status on the port.*/
1045 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1046 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1047
1048 /* Demux port status */
1049 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1050 mtip_process_sdbf(dd);
1051
1052 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1053 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1054 mtip_command_cleanup(dd);
1055 /* don't proceed further */
1056 return IRQ_HANDLED;
1057 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001058 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1059 &dd->dd_flag))
1060 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001061
1062 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1063 }
1064
1065 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1066 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1067 }
1068
1069 /* acknowledge interrupt */
1070 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1071
1072 return rv;
1073}
1074
1075/*
1076 * Wrapper for mtip_handle_irq
1077 * (ignores return code)
1078 */
1079static void mtip_tasklet(unsigned long data)
1080{
1081 mtip_handle_irq((struct driver_data *) data);
1082}
1083
1084/*
1085 * HBA interrupt subroutine.
1086 *
1087 * @irq IRQ number.
1088 * @instance Pointer to the driver data structure.
1089 *
1090 * return value
1091 * IRQ_HANDLED A HBA interrupt was pending and handled.
1092 * IRQ_NONE This interrupt was not for the HBA.
1093 */
1094static irqreturn_t mtip_irq_handler(int irq, void *instance)
1095{
1096 struct driver_data *dd = instance;
1097 tasklet_schedule(&dd->tasklet);
1098 return IRQ_HANDLED;
1099}
1100
1101static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1102{
1103 atomic_set(&port->commands[tag].active, 1);
1104 writel(1 << MTIP_TAG_BIT(tag),
1105 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1106}
1107
1108/*
1109 * Wait for port to quiesce
1110 *
1111 * @port Pointer to port data structure
1112 * @timeout Max duration to wait (ms)
1113 *
1114 * return value
1115 * 0 Success
1116 * -EBUSY Commands still active
1117 */
1118static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1119{
1120 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001121 unsigned int n;
1122 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001123
1124 to = jiffies + msecs_to_jiffies(timeout);
1125 do {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01001126 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1127 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001128 msleep(20);
1129 continue; /* svc thd is actively issuing commands */
1130 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001131 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1132 &port->dd->dd_flag))
1133 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001134 /*
1135 * Ignore s_active bit 0 of array element 0.
1136 * This bit will always be set
1137 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001138 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001139 for (n = 1; n < port->dd->slot_groups; n++)
1140 active |= readl(port->s_active[n]);
1141
1142 if (!active)
1143 break;
1144
1145 msleep(20);
1146 } while (time_before(jiffies, to));
1147
1148 return active ? -EBUSY : 0;
1149}
1150
1151/*
1152 * Execute an internal command and wait for the completion.
1153 *
1154 * @port Pointer to the port data structure.
1155 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001156 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001157 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001158 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001159 * @opts Command header options, excluding the FIS length
1160 * and the number of PRD entries.
1161 * @timeout Time in ms to wait for the command to complete.
1162 *
1163 * return value
1164 * 0 Command completed successfully.
1165 * -EFAULT The buffer address is not correctly aligned.
1166 * -EBUSY Internal command or other IO in progress.
1167 * -EAGAIN Time out waiting for command to complete.
1168 */
1169static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001170 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001171 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001172 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001173 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174 u32 opts,
1175 gfp_t atomic,
1176 unsigned long timeout)
1177{
1178 struct mtip_cmd_sg *command_sg;
1179 DECLARE_COMPLETION_ONSTACK(wait);
1180 int rv = 0;
1181 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1182
1183 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1184 if (buffer & 0x00000007) {
1185 dev_err(&port->dd->pdev->dev,
1186 "SG buffer is not 8 byte aligned\n");
1187 return -EFAULT;
1188 }
1189
1190 /* Only one internal command should be running at a time */
1191 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1192 dev_warn(&port->dd->pdev->dev,
1193 "Internal command already active\n");
1194 return -EBUSY;
1195 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001196 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197
1198 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001199 if (fis->command != ATA_CMD_STANDBYNOW1) {
1200 /* wait for io to complete if non atomic */
1201 if (mtip_quiesce_io(port, 5000) < 0) {
1202 dev_warn(&port->dd->pdev->dev,
1203 "Failed to quiesce IO\n");
1204 release_slot(port, MTIP_TAG_INTERNAL);
1205 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT,
1206 &port->flags);
1207 wake_up_interruptible(&port->svc_wait);
1208 return -EBUSY;
1209 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001210 }
1211
1212 /* Set the completion function and data for the command. */
1213 int_cmd->comp_data = &wait;
1214 int_cmd->comp_func = mtip_completion;
1215
1216 } else {
1217 /* Clear completion - we're going to poll */
1218 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001219 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001220 }
1221
1222 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001223 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001224
1225 /* Populate the SG list */
1226 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001227 __force_bit2int cpu_to_le32(opts | fis_len);
1228 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001229 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1230
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001231 command_sg->info =
1232 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1233 command_sg->dba =
1234 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1235 command_sg->dba_upper =
1236 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001237
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001238 int_cmd->command_header->opts |=
1239 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001240 }
1241
1242 /* Populate the command header */
1243 int_cmd->command_header->byte_count = 0;
1244
1245 /* Issue the command to the hardware */
1246 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1247
1248 /* Poll if atomic, wait_for_completion otherwise */
1249 if (atomic == GFP_KERNEL) {
1250 /* Wait for the command to complete or timeout. */
1251 if (wait_for_completion_timeout(
1252 &wait,
1253 msecs_to_jiffies(timeout)) == 0) {
1254 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001255 "Internal command did not complete [%d] "
1256 "within timeout of %lu ms\n",
1257 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001258 if (mtip_check_surprise_removal(port->dd->pdev) ||
1259 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1260 &port->dd->dd_flag)) {
1261 rv = -ENXIO;
1262 goto exec_ic_exit;
1263 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001264 rv = -EAGAIN;
1265 }
1266
1267 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1268 & (1 << MTIP_TAG_INTERNAL)) {
1269 dev_warn(&port->dd->pdev->dev,
1270 "Retiring internal command but CI is 1.\n");
Asai Thambi S P45038362012-04-09 08:35:38 +02001271 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1272 &port->dd->dd_flag)) {
1273 hba_reset_nosleep(port->dd);
1274 rv = -ENXIO;
1275 } else {
1276 mtip_restart_port(port);
1277 rv = -EAGAIN;
1278 }
1279 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001280 }
1281
1282 } else {
1283 /* Spin for <timeout> checking if command still outstanding */
1284 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001285 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1286 & (1 << MTIP_TAG_INTERNAL))
1287 && time_before(jiffies, timeout)) {
1288 if (mtip_check_surprise_removal(port->dd->pdev)) {
1289 rv = -ENXIO;
1290 goto exec_ic_exit;
1291 }
1292 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P45038362012-04-09 08:35:38 +02001293 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1294 &port->dd->dd_flag)) {
1295 rv = -ENXIO;
1296 goto exec_ic_exit;
1297 }
1298 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001299
1300 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1301 & (1 << MTIP_TAG_INTERNAL)) {
1302 dev_err(&port->dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001303 "Internal command did not complete [atomic]\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001304 rv = -EAGAIN;
Asai Thambi S P45038362012-04-09 08:35:38 +02001305 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1306 &port->dd->dd_flag)) {
1307 hba_reset_nosleep(port->dd);
1308 rv = -ENXIO;
1309 } else {
1310 mtip_restart_port(port);
1311 rv = -EAGAIN;
1312 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001313 }
1314 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001315exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001316 /* Clear the allocated and active bits for the internal command. */
1317 atomic_set(&int_cmd->active, 0);
1318 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001319 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1320 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001321
1322 return rv;
1323}
1324
1325/*
1326 * Byte-swap ATA ID strings.
1327 *
1328 * ATA identify data contains strings in byte-swapped 16-bit words.
1329 * They must be swapped (on all architectures) to be usable as C strings.
1330 * This function swaps bytes in-place.
1331 *
1332 * @buf The buffer location of the string
1333 * @len The number of bytes to swap
1334 *
1335 * return value
1336 * None
1337 */
1338static inline void ata_swap_string(u16 *buf, unsigned int len)
1339{
1340 int i;
1341 for (i = 0; i < (len/2); i++)
1342 be16_to_cpus(&buf[i]);
1343}
1344
1345/*
1346 * Request the device identity information.
1347 *
1348 * If a user space buffer is not specified, i.e. is NULL, the
1349 * identify information is still read from the drive and placed
1350 * into the identify data buffer (@e port->identify) in the
1351 * port data structure.
1352 * When the identify buffer contains valid identify information @e
1353 * port->identify_valid is non-zero.
1354 *
1355 * @port Pointer to the port structure.
1356 * @user_buffer A user space buffer where the identify data should be
1357 * copied.
1358 *
1359 * return value
1360 * 0 Command completed successfully.
1361 * -EFAULT An error occurred while coping data to the user buffer.
1362 * -1 Command failed.
1363 */
1364static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1365{
1366 int rv = 0;
1367 struct host_to_dev_fis fis;
1368
Asai Thambi S P45038362012-04-09 08:35:38 +02001369 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1370 return -EFAULT;
1371
Sam Bradshaw88523a62011-08-30 08:34:26 -06001372 /* Build the FIS. */
1373 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1374 fis.type = 0x27;
1375 fis.opts = 1 << 7;
1376 fis.command = ATA_CMD_ID_ATA;
1377
1378 /* Set the identify information as invalid. */
1379 port->identify_valid = 0;
1380
1381 /* Clear the identify information. */
1382 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1383
1384 /* Execute the command. */
1385 if (mtip_exec_internal_command(port,
1386 &fis,
1387 5,
1388 port->identify_dma,
1389 sizeof(u16) * ATA_ID_WORDS,
1390 0,
1391 GFP_KERNEL,
1392 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1393 < 0) {
1394 rv = -1;
1395 goto out;
1396 }
1397
1398 /*
1399 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1400 * perform field-sensitive swapping on the string fields.
1401 * See the kernel use of ata_id_string() for proof of this.
1402 */
1403#ifdef __LITTLE_ENDIAN
1404 ata_swap_string(port->identify + 27, 40); /* model string*/
1405 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1406 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1407#else
1408 {
1409 int i;
1410 for (i = 0; i < ATA_ID_WORDS; i++)
1411 port->identify[i] = le16_to_cpu(port->identify[i]);
1412 }
1413#endif
1414
1415 /* Set the identify buffer as valid. */
1416 port->identify_valid = 1;
1417
1418 if (user_buffer) {
1419 if (copy_to_user(
1420 user_buffer,
1421 port->identify,
1422 ATA_ID_WORDS * sizeof(u16))) {
1423 rv = -EFAULT;
1424 goto out;
1425 }
1426 }
1427
1428out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001429 return rv;
1430}
1431
1432/*
1433 * Issue a standby immediate command to the device.
1434 *
1435 * @port Pointer to the port structure.
1436 *
1437 * return value
1438 * 0 Command was executed successfully.
1439 * -1 An error occurred while executing the command.
1440 */
1441static int mtip_standby_immediate(struct mtip_port *port)
1442{
1443 int rv;
1444 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001445 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001446
Sam Bradshaw88523a62011-08-30 08:34:26 -06001447 /* Build the FIS. */
1448 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1449 fis.type = 0x27;
1450 fis.opts = 1 << 7;
1451 fis.command = ATA_CMD_STANDBYNOW1;
1452
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001453 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001454 rv = mtip_exec_internal_command(port,
1455 &fis,
1456 5,
1457 0,
1458 0,
1459 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001460 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001461 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001462 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1463 jiffies_to_msecs(jiffies - start));
1464 if (rv)
1465 dev_warn(&port->dd->pdev->dev,
1466 "STANDBY IMMEDIATE command failed.\n");
1467
1468 return rv;
1469}
1470
1471/*
1472 * Issue a READ LOG EXT command to the device.
1473 *
1474 * @port pointer to the port structure.
1475 * @page page number to fetch
1476 * @buffer pointer to buffer
1477 * @buffer_dma dma address corresponding to @buffer
1478 * @sectors page length to fetch, in sectors
1479 *
1480 * return value
1481 * @rv return value from mtip_exec_internal_command()
1482 */
1483static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1484 dma_addr_t buffer_dma, unsigned int sectors)
1485{
1486 struct host_to_dev_fis fis;
1487
1488 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1489 fis.type = 0x27;
1490 fis.opts = 1 << 7;
1491 fis.command = ATA_CMD_READ_LOG_EXT;
1492 fis.sect_count = sectors & 0xFF;
1493 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1494 fis.lba_low = page;
1495 fis.lba_mid = 0;
1496 fis.device = ATA_DEVICE_OBS;
1497
1498 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1499
1500 return mtip_exec_internal_command(port,
1501 &fis,
1502 5,
1503 buffer_dma,
1504 sectors * ATA_SECT_SIZE,
1505 0,
1506 GFP_ATOMIC,
1507 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1508}
1509
1510/*
1511 * Issue a SMART READ DATA command to the device.
1512 *
1513 * @port pointer to the port structure.
1514 * @buffer pointer to buffer
1515 * @buffer_dma dma address corresponding to @buffer
1516 *
1517 * return value
1518 * @rv return value from mtip_exec_internal_command()
1519 */
1520static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1521 dma_addr_t buffer_dma)
1522{
1523 struct host_to_dev_fis fis;
1524
1525 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1526 fis.type = 0x27;
1527 fis.opts = 1 << 7;
1528 fis.command = ATA_CMD_SMART;
1529 fis.features = 0xD0;
1530 fis.sect_count = 1;
1531 fis.lba_mid = 0x4F;
1532 fis.lba_hi = 0xC2;
1533 fis.device = ATA_DEVICE_OBS;
1534
1535 return mtip_exec_internal_command(port,
1536 &fis,
1537 5,
1538 buffer_dma,
1539 ATA_SECT_SIZE,
1540 0,
1541 GFP_ATOMIC,
1542 15000);
1543}
1544
1545/*
1546 * Get the value of a smart attribute
1547 *
1548 * @port pointer to the port structure
1549 * @id attribute number
1550 * @attrib pointer to return attrib information corresponding to @id
1551 *
1552 * return value
1553 * -EINVAL NULL buffer passed or unsupported attribute @id.
1554 * -EPERM Identify data not valid, SMART not supported or not enabled
1555 */
1556static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1557 struct smart_attr *attrib)
1558{
1559 int rv, i;
1560 struct smart_attr *pattr;
1561
1562 if (!attrib)
1563 return -EINVAL;
1564
1565 if (!port->identify_valid) {
1566 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1567 return -EPERM;
1568 }
1569 if (!(port->identify[82] & 0x1)) {
1570 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1571 return -EPERM;
1572 }
1573 if (!(port->identify[85] & 0x1)) {
1574 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1575 return -EPERM;
1576 }
1577
1578 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1579 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1580 if (rv) {
1581 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1582 return rv;
1583 }
1584
1585 pattr = (struct smart_attr *)(port->smart_buf + 2);
1586 for (i = 0; i < 29; i++, pattr++)
1587 if (pattr->attr_id == id) {
1588 memcpy(attrib, pattr, sizeof(struct smart_attr));
1589 break;
1590 }
1591
1592 if (i == 29) {
1593 dev_warn(&port->dd->pdev->dev,
1594 "Query for invalid SMART attribute ID\n");
1595 rv = -EINVAL;
1596 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001597
Sam Bradshaw88523a62011-08-30 08:34:26 -06001598 return rv;
1599}
1600
1601/*
1602 * Get the drive capacity.
1603 *
1604 * @dd Pointer to the device data structure.
1605 * @sectors Pointer to the variable that will receive the sector count.
1606 *
1607 * return value
1608 * 1 Capacity was returned successfully.
1609 * 0 The identify information is invalid.
1610 */
Jens Axboe63166682011-09-27 21:27:43 -06001611static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001612{
1613 struct mtip_port *port = dd->port;
1614 u64 total, raw0, raw1, raw2, raw3;
1615 raw0 = port->identify[100];
1616 raw1 = port->identify[101];
1617 raw2 = port->identify[102];
1618 raw3 = port->identify[103];
1619 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1620 *sectors = total;
1621 return (bool) !!port->identify_valid;
1622}
1623
1624/*
1625 * Reset the HBA.
1626 *
1627 * Resets the HBA by setting the HBA Reset bit in the Global
1628 * HBA Control register. After setting the HBA Reset bit the
1629 * function waits for 1 second before reading the HBA Reset
1630 * bit to make sure it has cleared. If HBA Reset is not clear
1631 * an error is returned. Cannot be used in non-blockable
1632 * context.
1633 *
1634 * @dd Pointer to the driver data structure.
1635 *
1636 * return value
1637 * 0 The reset was successful.
1638 * -1 The HBA Reset bit did not clear.
1639 */
1640static int mtip_hba_reset(struct driver_data *dd)
1641{
1642 mtip_deinit_port(dd->port);
1643
1644 /* Set the reset bit */
1645 writel(HOST_RESET, dd->mmio + HOST_CTL);
1646
1647 /* Flush */
1648 readl(dd->mmio + HOST_CTL);
1649
1650 /* Wait for reset to clear */
1651 ssleep(1);
1652
1653 /* Check the bit has cleared */
1654 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1655 dev_err(&dd->pdev->dev,
1656 "Reset bit did not clear.\n");
1657 return -1;
1658 }
1659
1660 return 0;
1661}
1662
1663/*
1664 * Display the identify command data.
1665 *
1666 * @port Pointer to the port data structure.
1667 *
1668 * return value
1669 * None
1670 */
1671static void mtip_dump_identify(struct mtip_port *port)
1672{
1673 sector_t sectors;
1674 unsigned short revid;
1675 char cbuf[42];
1676
1677 if (!port->identify_valid)
1678 return;
1679
1680 strlcpy(cbuf, (char *)(port->identify+10), 21);
1681 dev_info(&port->dd->pdev->dev,
1682 "Serial No.: %s\n", cbuf);
1683
1684 strlcpy(cbuf, (char *)(port->identify+23), 9);
1685 dev_info(&port->dd->pdev->dev,
1686 "Firmware Ver.: %s\n", cbuf);
1687
1688 strlcpy(cbuf, (char *)(port->identify+27), 41);
1689 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1690
1691 if (mtip_hw_get_capacity(port->dd, &sectors))
1692 dev_info(&port->dd->pdev->dev,
1693 "Capacity: %llu sectors (%llu MB)\n",
1694 (u64)sectors,
1695 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1696
1697 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001698 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001699 case 0x1:
1700 strlcpy(cbuf, "A0", 3);
1701 break;
1702 case 0x3:
1703 strlcpy(cbuf, "A2", 3);
1704 break;
1705 default:
1706 strlcpy(cbuf, "?", 2);
1707 break;
1708 }
1709 dev_info(&port->dd->pdev->dev,
1710 "Card Type: %s\n", cbuf);
1711}
1712
1713/*
1714 * Map the commands scatter list into the command table.
1715 *
1716 * @command Pointer to the command.
1717 * @nents Number of scatter list entries.
1718 *
1719 * return value
1720 * None
1721 */
1722static inline void fill_command_sg(struct driver_data *dd,
1723 struct mtip_cmd *command,
1724 int nents)
1725{
1726 int n;
1727 unsigned int dma_len;
1728 struct mtip_cmd_sg *command_sg;
1729 struct scatterlist *sg = command->sg;
1730
1731 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1732
1733 for (n = 0; n < nents; n++) {
1734 dma_len = sg_dma_len(sg);
1735 if (dma_len > 0x400000)
1736 dev_err(&dd->pdev->dev,
1737 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001738 command_sg->info = __force_bit2int
1739 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1740 command_sg->dba = __force_bit2int
1741 cpu_to_le32(sg_dma_address(sg));
1742 command_sg->dba_upper = __force_bit2int
1743 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001744 command_sg++;
1745 sg++;
1746 }
1747}
1748
1749/*
1750 * @brief Execute a drive command.
1751 *
1752 * return value 0 The command completed successfully.
1753 * return value -1 An error occurred while executing the command.
1754 */
Jens Axboe63166682011-09-27 21:27:43 -06001755static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001756{
1757 struct host_to_dev_fis fis;
1758 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1759
Sam Bradshaw88523a62011-08-30 08:34:26 -06001760 /* Build the FIS. */
1761 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1762 fis.type = 0x27;
1763 fis.opts = 1 << 7;
1764 fis.command = command[0];
1765 fis.features = command[1];
1766 fis.sect_count = command[2];
1767 fis.sector = command[3];
1768 fis.cyl_low = command[4];
1769 fis.cyl_hi = command[5];
1770 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1771
1772
Asai Thambi S P45038362012-04-09 08:35:38 +02001773 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 -06001774 __func__,
1775 command[0],
1776 command[1],
1777 command[2],
1778 command[3],
1779 command[4],
1780 command[5],
1781 command[6]);
1782
1783 /* Execute the command. */
1784 if (mtip_exec_internal_command(port,
1785 &fis,
1786 5,
1787 0,
1788 0,
1789 0,
1790 GFP_KERNEL,
1791 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001792 return -1;
1793 }
1794
1795 command[0] = reply->command; /* Status*/
1796 command[1] = reply->features; /* Error*/
1797 command[4] = reply->cyl_low;
1798 command[5] = reply->cyl_hi;
1799
Asai Thambi S P45038362012-04-09 08:35:38 +02001800 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 -06001801 __func__,
1802 command[0],
1803 command[1],
1804 command[4],
1805 command[5]);
1806
Sam Bradshaw88523a62011-08-30 08:34:26 -06001807 return 0;
1808}
1809
1810/*
1811 * @brief Execute a drive command.
1812 *
1813 * @param port Pointer to the port data structure.
1814 * @param command Pointer to the user specified command parameters.
1815 * @param user_buffer Pointer to the user space buffer where read sector
1816 * data should be copied.
1817 *
1818 * return value 0 The command completed successfully.
1819 * return value -EFAULT An error occurred while copying the completion
1820 * data to the user space buffer.
1821 * return value -1 An error occurred while executing the command.
1822 */
Jens Axboe63166682011-09-27 21:27:43 -06001823static int exec_drive_command(struct mtip_port *port, u8 *command,
1824 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001825{
1826 struct host_to_dev_fis fis;
1827 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1828
Sam Bradshaw88523a62011-08-30 08:34:26 -06001829 /* Build the FIS. */
1830 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1831 fis.type = 0x27;
1832 fis.opts = 1 << 7;
1833 fis.command = command[0];
1834 fis.features = command[2];
1835 fis.sect_count = command[3];
1836 if (fis.command == ATA_CMD_SMART) {
1837 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001838 fis.cyl_low = 0x4F;
1839 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001840 }
1841
1842 dbg_printk(MTIP_DRV_NAME
1843 "%s: User Command: cmd %x, sect %x, "
1844 "feat %x, sectcnt %x\n",
1845 __func__,
1846 command[0],
1847 command[1],
1848 command[2],
1849 command[3]);
1850
1851 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1852
1853 /* Execute the command. */
1854 if (mtip_exec_internal_command(port,
1855 &fis,
1856 5,
1857 port->sector_buffer_dma,
1858 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1859 0,
1860 GFP_KERNEL,
1861 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1862 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001863 return -1;
1864 }
1865
1866 /* Collect the completion status. */
1867 command[0] = reply->command; /* Status*/
1868 command[1] = reply->features; /* Error*/
1869 command[2] = command[3];
1870
1871 dbg_printk(MTIP_DRV_NAME
1872 "%s: Completion Status: stat %x, "
1873 "err %x, cmd %x\n",
1874 __func__,
1875 command[0],
1876 command[1],
1877 command[2]);
1878
1879 if (user_buffer && command[3]) {
1880 if (copy_to_user(user_buffer,
1881 port->sector_buffer,
1882 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001883 return -EFAULT;
1884 }
1885 }
1886
Sam Bradshaw88523a62011-08-30 08:34:26 -06001887 return 0;
1888}
1889
1890/*
1891 * Indicates whether a command has a single sector payload.
1892 *
1893 * @command passed to the device to perform the certain event.
1894 * @features passed to the device to perform the certain event.
1895 *
1896 * return value
1897 * 1 command is one that always has a single sector payload,
1898 * regardless of the value in the Sector Count field.
1899 * 0 otherwise
1900 *
1901 */
1902static unsigned int implicit_sector(unsigned char command,
1903 unsigned char features)
1904{
1905 unsigned int rv = 0;
1906
1907 /* list of commands that have an implicit sector count of 1 */
1908 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001909 case ATA_CMD_SEC_SET_PASS:
1910 case ATA_CMD_SEC_UNLOCK:
1911 case ATA_CMD_SEC_ERASE_PREP:
1912 case ATA_CMD_SEC_ERASE_UNIT:
1913 case ATA_CMD_SEC_FREEZE_LOCK:
1914 case ATA_CMD_SEC_DISABLE_PASS:
1915 case ATA_CMD_PMP_READ:
1916 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001917 rv = 1;
1918 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001919 case ATA_CMD_SET_MAX:
1920 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001921 rv = 1;
1922 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001923 case ATA_CMD_SMART:
1924 if ((features == ATA_SMART_READ_VALUES) ||
1925 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001926 rv = 1;
1927 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001928 case ATA_CMD_CONF_OVERLAY:
1929 if ((features == ATA_DCO_IDENTIFY) ||
1930 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001931 rv = 1;
1932 break;
1933 }
1934 return rv;
1935}
1936
1937/*
1938 * Executes a taskfile
1939 * See ide_taskfile_ioctl() for derivation
1940 */
1941static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001942 void __user *buf,
1943 ide_task_request_t *req_task,
1944 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001945{
1946 struct host_to_dev_fis fis;
1947 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001948 u8 *outbuf = NULL;
1949 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001950 dma_addr_t outbuf_dma = 0;
1951 dma_addr_t inbuf_dma = 0;
1952 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001953 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001954 unsigned int taskin = 0;
1955 unsigned int taskout = 0;
1956 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001957 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1958 unsigned int force_single_sector;
1959 unsigned int transfer_size;
1960 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001961 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001962
1963 taskout = req_task->out_size;
1964 taskin = req_task->in_size;
1965 /* 130560 = 512 * 0xFF*/
1966 if (taskin > 130560 || taskout > 130560) {
1967 err = -EINVAL;
1968 goto abort;
1969 }
1970
1971 if (taskout) {
1972 outbuf = kzalloc(taskout, GFP_KERNEL);
1973 if (outbuf == NULL) {
1974 err = -ENOMEM;
1975 goto abort;
1976 }
1977 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1978 err = -EFAULT;
1979 goto abort;
1980 }
1981 outbuf_dma = pci_map_single(dd->pdev,
1982 outbuf,
1983 taskout,
1984 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001985 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001986 err = -ENOMEM;
1987 goto abort;
1988 }
1989 dma_buffer = outbuf_dma;
1990 }
1991
1992 if (taskin) {
1993 inbuf = kzalloc(taskin, GFP_KERNEL);
1994 if (inbuf == NULL) {
1995 err = -ENOMEM;
1996 goto abort;
1997 }
1998
1999 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2000 err = -EFAULT;
2001 goto abort;
2002 }
2003 inbuf_dma = pci_map_single(dd->pdev,
2004 inbuf,
2005 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002006 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002007 err = -ENOMEM;
2008 goto abort;
2009 }
2010 dma_buffer = inbuf_dma;
2011 }
2012
2013 /* only supports PIO and non-data commands from this ioctl. */
2014 switch (req_task->data_phase) {
2015 case TASKFILE_OUT:
2016 nsect = taskout / ATA_SECT_SIZE;
2017 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2018 break;
2019 case TASKFILE_IN:
2020 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2021 break;
2022 case TASKFILE_NO_DATA:
2023 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2024 break;
2025 default:
2026 err = -EINVAL;
2027 goto abort;
2028 }
2029
Sam Bradshaw88523a62011-08-30 08:34:26 -06002030 /* Build the FIS. */
2031 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2032
2033 fis.type = 0x27;
2034 fis.opts = 1 << 7;
2035 fis.command = req_task->io_ports[7];
2036 fis.features = req_task->io_ports[1];
2037 fis.sect_count = req_task->io_ports[2];
2038 fis.lba_low = req_task->io_ports[3];
2039 fis.lba_mid = req_task->io_ports[4];
2040 fis.lba_hi = req_task->io_ports[5];
2041 /* Clear the dev bit*/
2042 fis.device = req_task->io_ports[6] & ~0x10;
2043
2044 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2045 req_task->in_flags.all =
2046 IDE_TASKFILE_STD_IN_FLAGS |
2047 (IDE_HOB_STD_IN_FLAGS << 8);
2048 fis.lba_low_ex = req_task->hob_ports[3];
2049 fis.lba_mid_ex = req_task->hob_ports[4];
2050 fis.lba_hi_ex = req_task->hob_ports[5];
2051 fis.features_ex = req_task->hob_ports[1];
2052 fis.sect_cnt_ex = req_task->hob_ports[2];
2053
2054 } else {
2055 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2056 }
2057
2058 force_single_sector = implicit_sector(fis.command, fis.features);
2059
2060 if ((taskin || taskout) && (!fis.sect_count)) {
2061 if (nsect)
2062 fis.sect_count = nsect;
2063 else {
2064 if (!force_single_sector) {
2065 dev_warn(&dd->pdev->dev,
2066 "data movement but "
2067 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002068 err = -EINVAL;
2069 goto abort;
2070 }
2071 }
2072 }
2073
2074 dbg_printk(MTIP_DRV_NAME
2075 "taskfile: cmd %x, feat %x, nsect %x,"
2076 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2077 " head/dev %x\n",
2078 fis.command,
2079 fis.features,
2080 fis.sect_count,
2081 fis.lba_low,
2082 fis.lba_mid,
2083 fis.lba_hi,
2084 fis.device);
2085
2086 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002087 case ATA_CMD_DOWNLOAD_MICRO:
2088 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002089 timeout = 60000;
2090 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002091 case ATA_CMD_SEC_ERASE_UNIT:
2092 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002093 timeout = 240000;
2094 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002095 case ATA_CMD_STANDBYNOW1:
2096 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002097 timeout = 10000;
2098 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002099 case 0xF7:
2100 case 0xFA:
2101 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002102 timeout = 10000;
2103 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002104 case ATA_CMD_SMART:
2105 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002106 timeout = 10000;
2107 break;
2108 default:
2109 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2110 break;
2111 }
2112
2113 /* Determine the correct transfer size.*/
2114 if (force_single_sector)
2115 transfer_size = ATA_SECT_SIZE;
2116 else
2117 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2118
2119 /* Execute the command.*/
2120 if (mtip_exec_internal_command(dd->port,
2121 &fis,
2122 5,
2123 dma_buffer,
2124 transfer_size,
2125 0,
2126 GFP_KERNEL,
2127 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002128 err = -EIO;
2129 goto abort;
2130 }
2131
2132 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2133
2134 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2135 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2136 req_task->io_ports[7] = reply->control;
2137 } else {
2138 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2139 req_task->io_ports[7] = reply->command;
2140 }
2141
2142 /* reclaim the DMA buffers.*/
2143 if (inbuf_dma)
2144 pci_unmap_single(dd->pdev, inbuf_dma,
2145 taskin, DMA_FROM_DEVICE);
2146 if (outbuf_dma)
2147 pci_unmap_single(dd->pdev, outbuf_dma,
2148 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002149 inbuf_dma = 0;
2150 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002151
2152 /* return the ATA registers to the caller.*/
2153 req_task->io_ports[1] = reply->features;
2154 req_task->io_ports[2] = reply->sect_count;
2155 req_task->io_ports[3] = reply->lba_low;
2156 req_task->io_ports[4] = reply->lba_mid;
2157 req_task->io_ports[5] = reply->lba_hi;
2158 req_task->io_ports[6] = reply->device;
2159
2160 if (req_task->out_flags.all & 1) {
2161
2162 req_task->hob_ports[3] = reply->lba_low_ex;
2163 req_task->hob_ports[4] = reply->lba_mid_ex;
2164 req_task->hob_ports[5] = reply->lba_hi_ex;
2165 req_task->hob_ports[1] = reply->features_ex;
2166 req_task->hob_ports[2] = reply->sect_cnt_ex;
2167 }
2168
2169 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002170 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06002171 ((fis.command == 0xFC) &&
2172 (fis.features == 0x27 || fis.features == 0x72 ||
2173 fis.features == 0x62 || fis.features == 0x26))) &&
2174 !(reply->command & 1)) {
2175 mtip_restart_port(dd->port);
2176 }
2177
2178 dbg_printk(MTIP_DRV_NAME
2179 "%s: Completion: stat %x,"
2180 "err %x, sect_cnt %x, lbalo %x,"
2181 "lbamid %x, lbahi %x, dev %x\n",
2182 __func__,
2183 req_task->io_ports[7],
2184 req_task->io_ports[1],
2185 req_task->io_ports[2],
2186 req_task->io_ports[3],
2187 req_task->io_ports[4],
2188 req_task->io_ports[5],
2189 req_task->io_ports[6]);
2190
Sam Bradshaw88523a62011-08-30 08:34:26 -06002191 if (taskout) {
2192 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2193 err = -EFAULT;
2194 goto abort;
2195 }
2196 }
2197 if (taskin) {
2198 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2199 err = -EFAULT;
2200 goto abort;
2201 }
2202 }
2203abort:
2204 if (inbuf_dma)
2205 pci_unmap_single(dd->pdev, inbuf_dma,
2206 taskin, DMA_FROM_DEVICE);
2207 if (outbuf_dma)
2208 pci_unmap_single(dd->pdev, outbuf_dma,
2209 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002210 kfree(outbuf);
2211 kfree(inbuf);
2212
2213 return err;
2214}
2215
2216/*
2217 * Handle IOCTL calls from the Block Layer.
2218 *
2219 * This function is called by the Block Layer when it receives an IOCTL
2220 * command that it does not understand. If the IOCTL command is not supported
2221 * this function returns -ENOTTY.
2222 *
2223 * @dd Pointer to the driver data structure.
2224 * @cmd IOCTL command passed from the Block Layer.
2225 * @arg IOCTL argument passed from the Block Layer.
2226 *
2227 * return value
2228 * 0 The IOCTL completed successfully.
2229 * -ENOTTY The specified command is not supported.
2230 * -EFAULT An error occurred copying data to a user space buffer.
2231 * -EIO An error occurred while executing the command.
2232 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002233static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2234 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002235{
2236 switch (cmd) {
2237 case HDIO_GET_IDENTITY:
2238 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
2239 dev_warn(&dd->pdev->dev,
2240 "Unable to read identity\n");
2241 return -EIO;
2242 }
2243
2244 break;
2245 case HDIO_DRIVE_CMD:
2246 {
2247 u8 drive_command[4];
2248
2249 /* Copy the user command info to our buffer. */
2250 if (copy_from_user(drive_command,
2251 (void __user *) arg,
2252 sizeof(drive_command)))
2253 return -EFAULT;
2254
2255 /* Execute the drive command. */
2256 if (exec_drive_command(dd->port,
2257 drive_command,
2258 (void __user *) (arg+4)))
2259 return -EIO;
2260
2261 /* Copy the status back to the users buffer. */
2262 if (copy_to_user((void __user *) arg,
2263 drive_command,
2264 sizeof(drive_command)))
2265 return -EFAULT;
2266
2267 break;
2268 }
2269 case HDIO_DRIVE_TASK:
2270 {
2271 u8 drive_command[7];
2272
2273 /* Copy the user command info to our buffer. */
2274 if (copy_from_user(drive_command,
2275 (void __user *) arg,
2276 sizeof(drive_command)))
2277 return -EFAULT;
2278
2279 /* Execute the drive command. */
2280 if (exec_drive_task(dd->port, drive_command))
2281 return -EIO;
2282
2283 /* Copy the status back to the users buffer. */
2284 if (copy_to_user((void __user *) arg,
2285 drive_command,
2286 sizeof(drive_command)))
2287 return -EFAULT;
2288
2289 break;
2290 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002291 case HDIO_DRIVE_TASKFILE: {
2292 ide_task_request_t req_task;
2293 int ret, outtotal;
2294
2295 if (copy_from_user(&req_task, (void __user *) arg,
2296 sizeof(req_task)))
2297 return -EFAULT;
2298
2299 outtotal = sizeof(req_task);
2300
2301 ret = exec_drive_taskfile(dd, (void __user *) arg,
2302 &req_task, outtotal);
2303
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002304 if (copy_to_user((void __user *) arg, &req_task,
2305 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002306 return -EFAULT;
2307
2308 return ret;
2309 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002310
2311 default:
2312 return -EINVAL;
2313 }
2314 return 0;
2315}
2316
2317/*
2318 * Submit an IO to the hw
2319 *
2320 * This function is called by the block layer to issue an io
2321 * to the device. Upon completion, the callback function will
2322 * be called with the data parameter passed as the callback data.
2323 *
2324 * @dd Pointer to the driver data structure.
2325 * @start First sector to read.
2326 * @nsect Number of sectors to read.
2327 * @nents Number of entries in scatter list for the read command.
2328 * @tag The tag of this read command.
2329 * @callback Pointer to the function that should be called
2330 * when the read completes.
2331 * @data Callback data passed to the callback function
2332 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002333 * @dir Direction (read or write)
2334 *
2335 * return value
2336 * None
2337 */
Jens Axboe63166682011-09-27 21:27:43 -06002338static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2339 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002340 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002341{
2342 struct host_to_dev_fis *fis;
2343 struct mtip_port *port = dd->port;
2344 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002345 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002346
2347 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002348 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002349
2350 command->scatter_ents = nents;
2351
2352 /*
2353 * The number of retries for this command before it is
2354 * reported as a failure to the upper layers.
2355 */
2356 command->retries = MTIP_MAX_RETRIES;
2357
2358 /* Fill out fis */
2359 fis = command->command;
2360 fis->type = 0x27;
2361 fis->opts = 1 << 7;
2362 fis->command =
2363 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002364 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2365 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002366 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002367 fis->features = nsect & 0xFF;
2368 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002369 fis->sect_count = ((tag << 3) | (tag >> 5));
2370 fis->sect_cnt_ex = 0;
2371 fis->control = 0;
2372 fis->res2 = 0;
2373 fis->res3 = 0;
2374 fill_command_sg(dd, command, nents);
2375
2376 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002377 command->command_header->opts =
2378 __force_bit2int cpu_to_le32(
2379 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002380 command->command_header->byte_count = 0;
2381
2382 /*
2383 * Set the completion function and data for the command
2384 * within this layer.
2385 */
2386 command->comp_data = dd;
2387 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002388 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002389
2390 /*
2391 * Set the completion function and data for the command passed
2392 * from the upper layer.
2393 */
2394 command->async_data = data;
2395 command->async_callback = callback;
2396
2397 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002398 * To prevent this command from being issued
2399 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002400 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002401 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2402 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2403 set_bit(tag, port->cmds_to_issue);
2404 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2405 return;
2406 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002407
2408 /* Issue the command to the hardware */
2409 mtip_issue_ncq_command(port, tag);
2410
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002411 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002412}
2413
2414/*
2415 * Release a command slot.
2416 *
2417 * @dd Pointer to the driver data structure.
2418 * @tag Slot tag
2419 *
2420 * return value
2421 * None
2422 */
Jens Axboe63166682011-09-27 21:27:43 -06002423static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002424{
2425 release_slot(dd->port, tag);
2426}
2427
2428/*
2429 * Obtain a command slot and return its associated scatter list.
2430 *
2431 * @dd Pointer to the driver data structure.
2432 * @tag Pointer to an int that will receive the allocated command
2433 * slot tag.
2434 *
2435 * return value
2436 * Pointer to the scatter list for the allocated command slot
2437 * or NULL if no command slots are available.
2438 */
Jens Axboe63166682011-09-27 21:27:43 -06002439static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2440 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002441{
2442 /*
2443 * It is possible that, even with this semaphore, a thread
2444 * may think that no command slots are available. Therefore, we
2445 * need to make an attempt to get_slot().
2446 */
2447 down(&dd->port->cmd_slot);
2448 *tag = get_slot(dd->port);
2449
Asai Thambi S P45038362012-04-09 08:35:38 +02002450 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2451 up(&dd->port->cmd_slot);
2452 return NULL;
2453 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002454 if (unlikely(*tag < 0))
2455 return NULL;
2456
2457 return dd->port->commands[*tag].sg;
2458}
2459
2460/*
2461 * Sysfs register/status dump.
2462 *
2463 * @dev Pointer to the device structure, passed by the kernrel.
2464 * @attr Pointer to the device_attribute structure passed by the kernel.
2465 * @buf Pointer to the char buffer that will receive the stats info.
2466 *
2467 * return value
2468 * The size, in bytes, of the data copied into buf.
2469 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002470static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002471 struct device_attribute *attr,
2472 char *buf)
2473{
2474 u32 group_allocated;
2475 struct driver_data *dd = dev_to_disk(dev)->private_data;
2476 int size = 0;
2477 int n;
2478
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002479 size += sprintf(&buf[size], "S ACTive:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002480
2481 for (n = 0; n < dd->slot_groups; n++)
2482 size += sprintf(&buf[size], "0x%08x\n",
2483 readl(dd->port->s_active[n]));
2484
2485 size += sprintf(&buf[size], "Command Issue:\n");
2486
2487 for (n = 0; n < dd->slot_groups; n++)
2488 size += sprintf(&buf[size], "0x%08x\n",
2489 readl(dd->port->cmd_issue[n]));
2490
2491 size += sprintf(&buf[size], "Allocated:\n");
2492
2493 for (n = 0; n < dd->slot_groups; n++) {
2494 if (sizeof(long) > sizeof(u32))
2495 group_allocated =
2496 dd->port->allocated[n/2] >> (32*(n&1));
2497 else
2498 group_allocated = dd->port->allocated[n];
2499 size += sprintf(&buf[size], "0x%08x\n",
2500 group_allocated);
2501 }
2502
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002503 size += sprintf(&buf[size], "Completed:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002504
2505 for (n = 0; n < dd->slot_groups; n++)
2506 size += sprintf(&buf[size], "0x%08x\n",
2507 readl(dd->port->completed[n]));
2508
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002509 size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002510 readl(dd->port->mmio + PORT_IRQ_STAT));
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002511 size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002512 readl(dd->mmio + HOST_IRQ_STAT));
2513
2514 return size;
2515}
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002516
2517static ssize_t mtip_hw_show_status(struct device *dev,
2518 struct device_attribute *attr,
2519 char *buf)
2520{
2521 struct driver_data *dd = dev_to_disk(dev)->private_data;
2522 int size = 0;
2523
2524 if (test_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag))
2525 size += sprintf(buf, "%s", "thermal_shutdown\n");
2526 else if (test_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag))
2527 size += sprintf(buf, "%s", "write_protect\n");
2528 else
2529 size += sprintf(buf, "%s", "online\n");
2530
2531 return size;
2532}
2533
Asai Thambi S P45038362012-04-09 08:35:38 +02002534static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002535static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002536
2537/*
2538 * Create the sysfs related attributes.
2539 *
2540 * @dd Pointer to the driver data structure.
2541 * @kobj Pointer to the kobj for the block device.
2542 *
2543 * return value
2544 * 0 Operation completed successfully.
2545 * -EINVAL Invalid parameter.
2546 */
Jens Axboe63166682011-09-27 21:27:43 -06002547static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002548{
2549 if (!kobj || !dd)
2550 return -EINVAL;
2551
2552 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2553 dev_warn(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002554 "Error creating 'registers' sysfs entry\n");
2555 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2556 dev_warn(&dd->pdev->dev,
2557 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002558 return 0;
2559}
2560
2561/*
2562 * Remove the sysfs related attributes.
2563 *
2564 * @dd Pointer to the driver data structure.
2565 * @kobj Pointer to the kobj for the block device.
2566 *
2567 * return value
2568 * 0 Operation completed successfully.
2569 * -EINVAL Invalid parameter.
2570 */
Jens Axboe63166682011-09-27 21:27:43 -06002571static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002572{
2573 if (!kobj || !dd)
2574 return -EINVAL;
2575
2576 sysfs_remove_file(kobj, &dev_attr_registers.attr);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002577 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002578
2579 return 0;
2580}
2581
2582/*
2583 * Perform any init/resume time hardware setup
2584 *
2585 * @dd Pointer to the driver data structure.
2586 *
2587 * return value
2588 * None
2589 */
2590static inline void hba_setup(struct driver_data *dd)
2591{
2592 u32 hwdata;
2593 hwdata = readl(dd->mmio + HOST_HSORG);
2594
2595 /* interrupt bug workaround: use only 1 IS bit.*/
2596 writel(hwdata |
2597 HSORG_DISABLE_SLOTGRP_INTR |
2598 HSORG_DISABLE_SLOTGRP_PXIS,
2599 dd->mmio + HOST_HSORG);
2600}
2601
2602/*
2603 * Detect the details of the product, and store anything needed
2604 * into the driver data structure. This includes product type and
2605 * version and number of slot groups.
2606 *
2607 * @dd Pointer to the driver data structure.
2608 *
2609 * return value
2610 * None
2611 */
2612static void mtip_detect_product(struct driver_data *dd)
2613{
2614 u32 hwdata;
2615 unsigned int rev, slotgroups;
2616
2617 /*
2618 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2619 * info register:
2620 * [15:8] hardware/software interface rev#
2621 * [ 3] asic-style interface
2622 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2623 */
2624 hwdata = readl(dd->mmio + HOST_HSORG);
2625
2626 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2627 dd->slot_groups = 1;
2628
2629 if (hwdata & 0x8) {
2630 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2631 rev = (hwdata & HSORG_HWREV) >> 8;
2632 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2633 dev_info(&dd->pdev->dev,
2634 "ASIC-FPGA design, HS rev 0x%x, "
2635 "%i slot groups [%i slots]\n",
2636 rev,
2637 slotgroups,
2638 slotgroups * 32);
2639
2640 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2641 dev_warn(&dd->pdev->dev,
2642 "Warning: driver only supports "
2643 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2644 slotgroups = MTIP_MAX_SLOT_GROUPS;
2645 }
2646 dd->slot_groups = slotgroups;
2647 return;
2648 }
2649
2650 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2651}
2652
2653/*
2654 * Blocking wait for FTL rebuild to complete
2655 *
2656 * @dd Pointer to the DRIVER_DATA structure.
2657 *
2658 * return value
2659 * 0 FTL rebuild completed successfully
2660 * -EFAULT FTL rebuild error/timeout/interruption
2661 */
2662static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2663{
2664 unsigned long timeout, cnt = 0, start;
2665
2666 dev_warn(&dd->pdev->dev,
2667 "FTL rebuild in progress. Polling for completion.\n");
2668
2669 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002670 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2671
2672 do {
Asai Thambi S P45038362012-04-09 08:35:38 +02002673 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2674 &dd->dd_flag)))
2675 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002676 if (mtip_check_surprise_removal(dd->pdev))
2677 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002678
Sam Bradshaw88523a62011-08-30 08:34:26 -06002679 if (mtip_get_identify(dd->port, NULL) < 0)
2680 return -EFAULT;
2681
2682 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2683 MTIP_FTL_REBUILD_MAGIC) {
2684 ssleep(1);
2685 /* Print message every 3 minutes */
2686 if (cnt++ >= 180) {
2687 dev_warn(&dd->pdev->dev,
2688 "FTL rebuild in progress (%d secs).\n",
2689 jiffies_to_msecs(jiffies - start) / 1000);
2690 cnt = 0;
2691 }
2692 } else {
2693 dev_warn(&dd->pdev->dev,
2694 "FTL rebuild complete (%d secs).\n",
2695 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002696 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002697 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002698 }
2699 ssleep(10);
2700 } while (time_before(jiffies, timeout));
2701
2702 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002703 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002704 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2705 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002706 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002707}
2708
2709/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002710 * service thread to issue queued commands
2711 *
2712 * @data Pointer to the driver data structure.
2713 *
2714 * return value
2715 * 0
2716 */
2717
2718static int mtip_service_thread(void *data)
2719{
2720 struct driver_data *dd = (struct driver_data *)data;
2721 unsigned long slot, slot_start, slot_wrap;
2722 unsigned int num_cmd_slots = dd->slot_groups * 32;
2723 struct mtip_port *port = dd->port;
2724
2725 while (1) {
2726 /*
2727 * the condition is to check neither an internal command is
2728 * is in progress nor error handling is active
2729 */
2730 wait_event_interruptible(port->svc_wait, (port->flags) &&
2731 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2732 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2733
2734 if (kthread_should_stop())
2735 break;
2736
Asai Thambi S P45038362012-04-09 08:35:38 +02002737 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2738 &dd->dd_flag)))
2739 break;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002740 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002741 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002742 slot = 1;
2743 /* used to restrict the loop to one iteration */
2744 slot_start = num_cmd_slots;
2745 slot_wrap = 0;
2746 while (1) {
2747 slot = find_next_bit(port->cmds_to_issue,
2748 num_cmd_slots, slot);
2749 if (slot_wrap == 1) {
2750 if ((slot_start >= slot) ||
2751 (slot >= num_cmd_slots))
2752 break;
2753 }
2754 if (unlikely(slot_start == num_cmd_slots))
2755 slot_start = slot;
2756
2757 if (unlikely(slot == num_cmd_slots)) {
2758 slot = 1;
2759 slot_wrap = 1;
2760 continue;
2761 }
2762
2763 /* Issue the command to the hardware */
2764 mtip_issue_ncq_command(port, slot);
2765
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002766 clear_bit(slot, port->cmds_to_issue);
2767 }
2768
2769 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002770 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02002771 if (!mtip_ftl_rebuild_poll(dd))
2772 set_bit(MTIP_DD_FLAG_REBUILD_FAILED_BIT,
2773 &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002774 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002775 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002776 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2777
2778 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2779 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002780 }
2781 return 0;
2782}
2783
2784/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002785 * Called once for each card.
2786 *
2787 * @dd Pointer to the driver data structure.
2788 *
2789 * return value
2790 * 0 on success, else an error code.
2791 */
Jens Axboe63166682011-09-27 21:27:43 -06002792static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002793{
2794 int i;
2795 int rv;
2796 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002797 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002798 unsigned char *buf;
2799 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002800
2801 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2802
2803 mtip_detect_product(dd);
2804 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2805 rv = -EIO;
2806 goto out1;
2807 }
2808 num_command_slots = dd->slot_groups * 32;
2809
2810 hba_setup(dd);
2811
Sam Bradshaw88523a62011-08-30 08:34:26 -06002812 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2813
2814 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2815 if (!dd->port) {
2816 dev_err(&dd->pdev->dev,
2817 "Memory allocation: port structure\n");
2818 return -ENOMEM;
2819 }
2820
2821 /* Counting semaphore to track command slot usage */
2822 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2823
2824 /* Spinlock to prevent concurrent issue */
2825 spin_lock_init(&dd->port->cmd_issue_lock);
2826
2827 /* Set the port mmio base address. */
2828 dd->port->mmio = dd->mmio + PORT_OFFSET;
2829 dd->port->dd = dd;
2830
2831 /* Allocate memory for the command list. */
2832 dd->port->command_list =
2833 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002834 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002835 &dd->port->command_list_dma,
2836 GFP_KERNEL);
2837 if (!dd->port->command_list) {
2838 dev_err(&dd->pdev->dev,
2839 "Memory allocation: command list\n");
2840 rv = -ENOMEM;
2841 goto out1;
2842 }
2843
2844 /* Clear the memory we have allocated. */
2845 memset(dd->port->command_list,
2846 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002847 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002848
2849 /* Setup the addresse of the RX FIS. */
2850 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2851 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2852
2853 /* Setup the address of the command tables. */
2854 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2855 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2856
2857 /* Setup the address of the identify data. */
2858 dd->port->identify = dd->port->command_table +
2859 HW_CMD_TBL_AR_SZ;
2860 dd->port->identify_dma = dd->port->command_tbl_dma +
2861 HW_CMD_TBL_AR_SZ;
2862
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002863 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002864 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2865 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2866
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002867 /* Setup the address of the log buf - for read log command */
2868 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
2869 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
2870
2871 /* Setup the address of the smart buf - for smart read data command */
2872 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
2873 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
2874
2875
Sam Bradshaw88523a62011-08-30 08:34:26 -06002876 /* Point the command headers at the command tables. */
2877 for (i = 0; i < num_command_slots; i++) {
2878 dd->port->commands[i].command_header =
2879 dd->port->command_list +
2880 (sizeof(struct mtip_cmd_hdr) * i);
2881 dd->port->commands[i].command_header_dma =
2882 dd->port->command_list_dma +
2883 (sizeof(struct mtip_cmd_hdr) * i);
2884
2885 dd->port->commands[i].command =
2886 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2887 dd->port->commands[i].command_dma =
2888 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2889
2890 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2891 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002892 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002893 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002894 dd->port->commands[i].command_header->ctba =
2895 __force_bit2int cpu_to_le32(
2896 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002897
2898 /*
2899 * If this is not done, a bug is reported by the stock
2900 * FC11 i386. Due to the fact that it has lots of kernel
2901 * debugging enabled.
2902 */
2903 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2904
2905 /* Mark all commands as currently inactive.*/
2906 atomic_set(&dd->port->commands[i].active, 0);
2907 }
2908
2909 /* Setup the pointers to the extended s_active and CI registers. */
2910 for (i = 0; i < dd->slot_groups; i++) {
2911 dd->port->s_active[i] =
2912 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2913 dd->port->cmd_issue[i] =
2914 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2915 dd->port->completed[i] =
2916 dd->port->mmio + i*0x80 + PORT_SDBV;
2917 }
2918
Asai Thambi S P45038362012-04-09 08:35:38 +02002919 timetaken = jiffies;
2920 timeout = jiffies + msecs_to_jiffies(30000);
2921 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2922 time_before(jiffies, timeout)) {
2923 mdelay(100);
2924 }
2925 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
2926 timetaken = jiffies - timetaken;
2927 dev_warn(&dd->pdev->dev,
2928 "Surprise removal detected at %u ms\n",
2929 jiffies_to_msecs(timetaken));
2930 rv = -ENODEV;
2931 goto out2 ;
2932 }
2933 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2934 timetaken = jiffies - timetaken;
2935 dev_warn(&dd->pdev->dev,
2936 "Removal detected at %u ms\n",
2937 jiffies_to_msecs(timetaken));
2938 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002939 goto out2;
2940 }
2941
Asai Thambi S P45038362012-04-09 08:35:38 +02002942 /* Conditionally reset the HBA. */
2943 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
2944 if (mtip_hba_reset(dd) < 0) {
2945 dev_err(&dd->pdev->dev,
2946 "Card did not reset within timeout\n");
2947 rv = -EIO;
2948 goto out2;
2949 }
2950 } else {
2951 /* Clear any pending interrupts on the HBA */
2952 writel(readl(dd->mmio + HOST_IRQ_STAT),
2953 dd->mmio + HOST_IRQ_STAT);
2954 }
2955
Sam Bradshaw88523a62011-08-30 08:34:26 -06002956 mtip_init_port(dd->port);
2957 mtip_start_port(dd->port);
2958
2959 /* Setup the ISR and enable interrupts. */
2960 rv = devm_request_irq(&dd->pdev->dev,
2961 dd->pdev->irq,
2962 mtip_irq_handler,
2963 IRQF_SHARED,
2964 dev_driver_string(&dd->pdev->dev),
2965 dd);
2966
2967 if (rv) {
2968 dev_err(&dd->pdev->dev,
2969 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2970 goto out2;
2971 }
2972
2973 /* Enable interrupts on the HBA. */
2974 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2975 dd->mmio + HOST_CTL);
2976
2977 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002978 init_waitqueue_head(&dd->port->svc_wait);
2979
Sam Bradshaw88523a62011-08-30 08:34:26 -06002980 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2981 dd->port->cmd_timer.function = mtip_timeout_function;
2982 mod_timer(&dd->port->cmd_timer,
2983 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2984
Asai Thambi S P45038362012-04-09 08:35:38 +02002985
2986 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
2987 rv = -EFAULT;
2988 goto out3;
2989 }
2990
Sam Bradshaw88523a62011-08-30 08:34:26 -06002991 if (mtip_get_identify(dd->port, NULL) < 0) {
2992 rv = -EFAULT;
2993 goto out3;
2994 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002995
2996 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2997 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002998 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2999 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003000 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003001 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003002
3003 /* check write protect, over temp and rebuild statuses */
3004 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3005 dd->port->log_buf,
3006 dd->port->log_buf_dma, 1);
3007 if (rv) {
3008 dev_warn(&dd->pdev->dev,
3009 "Error in READ LOG EXT (10h) command\n");
3010 /* non-critical error, don't fail the load */
3011 } else {
3012 buf = (unsigned char *)dd->port->log_buf;
3013 if (buf[259] & 0x1) {
3014 dev_info(&dd->pdev->dev,
3015 "Write protect bit is set.\n");
3016 set_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag);
3017 }
3018 if (buf[288] == 0xF7) {
3019 dev_info(&dd->pdev->dev,
3020 "Exceeded Tmax, drive in thermal shutdown.\n");
3021 set_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag);
3022 }
3023 if (buf[288] == 0xBF) {
3024 dev_info(&dd->pdev->dev,
3025 "Drive indicates rebuild has failed.\n");
3026 /* TODO */
3027 }
3028 }
3029
3030 /* get write protect progess */
3031 memset(&attr242, 0, sizeof(struct smart_attr));
3032 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3033 dev_warn(&dd->pdev->dev,
3034 "Unable to check write protect progress\n");
3035 else
3036 dev_info(&dd->pdev->dev,
3037 "Write protect progress: %d%% (%d blocks)\n",
3038 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003039 return rv;
3040
3041out3:
3042 del_timer_sync(&dd->port->cmd_timer);
3043
3044 /* Disable interrupts on the HBA. */
3045 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3046 dd->mmio + HOST_CTL);
3047
3048 /*Release the IRQ. */
3049 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3050
3051out2:
3052 mtip_deinit_port(dd->port);
3053
3054 /* Free the command/command header memory. */
3055 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003056 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003057 dd->port->command_list,
3058 dd->port->command_list_dma);
3059out1:
3060 /* Free the memory allocated for the for structure. */
3061 kfree(dd->port);
3062
3063 return rv;
3064}
3065
3066/*
3067 * Called to deinitialize an interface.
3068 *
3069 * @dd Pointer to the driver data structure.
3070 *
3071 * return value
3072 * 0
3073 */
Jens Axboe63166682011-09-27 21:27:43 -06003074static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003075{
3076 /*
3077 * Send standby immediate (E0h) to the drive so that it
3078 * saves its state.
3079 */
Asai Thambi S P45038362012-04-09 08:35:38 +02003080 if (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003081
Asai Thambi S P8182b492012-04-09 08:35:38 +02003082 if (!test_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003083 if (mtip_standby_immediate(dd->port))
3084 dev_warn(&dd->pdev->dev,
3085 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003086
3087 /* de-initialize the port. */
3088 mtip_deinit_port(dd->port);
3089
3090 /* Disable interrupts on the HBA. */
3091 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3092 dd->mmio + HOST_CTL);
3093 }
3094
3095 del_timer_sync(&dd->port->cmd_timer);
3096
Sam Bradshaw88523a62011-08-30 08:34:26 -06003097 /* Release the IRQ. */
3098 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3099
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003100 /* Stop the bottom half tasklet. */
3101 tasklet_kill(&dd->tasklet);
3102
Sam Bradshaw88523a62011-08-30 08:34:26 -06003103 /* Free the command/command header memory. */
3104 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003105 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003106 dd->port->command_list,
3107 dd->port->command_list_dma);
3108 /* Free the memory allocated for the for structure. */
3109 kfree(dd->port);
3110
3111 return 0;
3112}
3113
3114/*
3115 * Issue a Standby Immediate command to the device.
3116 *
3117 * This function is called by the Block Layer just before the
3118 * system powers off during a shutdown.
3119 *
3120 * @dd Pointer to the driver data structure.
3121 *
3122 * return value
3123 * 0
3124 */
Jens Axboe63166682011-09-27 21:27:43 -06003125static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003126{
3127 /*
3128 * Send standby immediate (E0h) to the drive so that it
3129 * saves its state.
3130 */
3131 mtip_standby_immediate(dd->port);
3132
3133 return 0;
3134}
3135
3136/*
3137 * Suspend function
3138 *
3139 * This function is called by the Block Layer just before the
3140 * system hibernates.
3141 *
3142 * @dd Pointer to the driver data structure.
3143 *
3144 * return value
3145 * 0 Suspend was successful
3146 * -EFAULT Suspend was not successful
3147 */
Jens Axboe63166682011-09-27 21:27:43 -06003148static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003149{
3150 /*
3151 * Send standby immediate (E0h) to the drive
3152 * so that it saves its state.
3153 */
3154 if (mtip_standby_immediate(dd->port) != 0) {
3155 dev_err(&dd->pdev->dev,
3156 "Failed standby-immediate command\n");
3157 return -EFAULT;
3158 }
3159
3160 /* Disable interrupts on the HBA.*/
3161 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3162 dd->mmio + HOST_CTL);
3163 mtip_deinit_port(dd->port);
3164
3165 return 0;
3166}
3167
3168/*
3169 * Resume function
3170 *
3171 * This function is called by the Block Layer as the
3172 * system resumes.
3173 *
3174 * @dd Pointer to the driver data structure.
3175 *
3176 * return value
3177 * 0 Resume was successful
3178 * -EFAULT Resume was not successful
3179 */
Jens Axboe63166682011-09-27 21:27:43 -06003180static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003181{
3182 /* Perform any needed hardware setup steps */
3183 hba_setup(dd);
3184
3185 /* Reset the HBA */
3186 if (mtip_hba_reset(dd) != 0) {
3187 dev_err(&dd->pdev->dev,
3188 "Unable to reset the HBA\n");
3189 return -EFAULT;
3190 }
3191
3192 /*
3193 * Enable the port, DMA engine, and FIS reception specific
3194 * h/w in controller.
3195 */
3196 mtip_init_port(dd->port);
3197 mtip_start_port(dd->port);
3198
3199 /* Enable interrupts on the HBA.*/
3200 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3201 dd->mmio + HOST_CTL);
3202
3203 return 0;
3204}
3205
3206/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003207 * Helper function for reusing disk name
3208 * upon hot insertion.
3209 */
3210static int rssd_disk_name_format(char *prefix,
3211 int index,
3212 char *buf,
3213 int buflen)
3214{
3215 const int base = 'z' - 'a' + 1;
3216 char *begin = buf + strlen(prefix);
3217 char *end = buf + buflen;
3218 char *p;
3219 int unit;
3220
3221 p = end - 1;
3222 *p = '\0';
3223 unit = base;
3224 do {
3225 if (p == begin)
3226 return -EINVAL;
3227 *--p = 'a' + (index % unit);
3228 index = (index / unit) - 1;
3229 } while (index >= 0);
3230
3231 memmove(begin, p, end - p);
3232 memcpy(buf, prefix, strlen(prefix));
3233
3234 return 0;
3235}
3236
3237/*
3238 * Block layer IOCTL handler.
3239 *
3240 * @dev Pointer to the block_device structure.
3241 * @mode ignored
3242 * @cmd IOCTL command passed from the user application.
3243 * @arg Argument passed from the user application.
3244 *
3245 * return value
3246 * 0 IOCTL completed successfully.
3247 * -ENOTTY IOCTL not supported or invalid driver data
3248 * structure pointer.
3249 */
3250static int mtip_block_ioctl(struct block_device *dev,
3251 fmode_t mode,
3252 unsigned cmd,
3253 unsigned long arg)
3254{
3255 struct driver_data *dd = dev->bd_disk->private_data;
3256
3257 if (!capable(CAP_SYS_ADMIN))
3258 return -EACCES;
3259
3260 if (!dd)
3261 return -ENOTTY;
3262
Asai Thambi S P45038362012-04-09 08:35:38 +02003263 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
3264 return -ENOTTY;
3265
Sam Bradshaw88523a62011-08-30 08:34:26 -06003266 switch (cmd) {
3267 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003268 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003269 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003270 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003271 }
3272}
3273
Jens Axboe16d02c02011-09-27 15:50:01 -06003274#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003275/*
3276 * Block layer compat IOCTL handler.
3277 *
3278 * @dev Pointer to the block_device structure.
3279 * @mode ignored
3280 * @cmd IOCTL command passed from the user application.
3281 * @arg Argument passed from the user application.
3282 *
3283 * return value
3284 * 0 IOCTL completed successfully.
3285 * -ENOTTY IOCTL not supported or invalid driver data
3286 * structure pointer.
3287 */
3288static int mtip_block_compat_ioctl(struct block_device *dev,
3289 fmode_t mode,
3290 unsigned cmd,
3291 unsigned long arg)
3292{
3293 struct driver_data *dd = dev->bd_disk->private_data;
3294
3295 if (!capable(CAP_SYS_ADMIN))
3296 return -EACCES;
3297
3298 if (!dd)
3299 return -ENOTTY;
3300
Asai Thambi S P45038362012-04-09 08:35:38 +02003301 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
3302 return -ENOTTY;
3303
Sam Bradshaw88523a62011-08-30 08:34:26 -06003304 switch (cmd) {
3305 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003306 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003307 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003308 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003309 ide_task_request_t req_task;
3310 int compat_tasksize, outtotal, ret;
3311
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003312 compat_tasksize =
3313 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003314
3315 compat_req_task =
3316 (struct mtip_compat_ide_task_request_s __user *) arg;
3317
3318 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003319 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003320 return -EFAULT;
3321
3322 if (get_user(req_task.out_size, &compat_req_task->out_size))
3323 return -EFAULT;
3324
3325 if (get_user(req_task.in_size, &compat_req_task->in_size))
3326 return -EFAULT;
3327
3328 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3329
3330 ret = exec_drive_taskfile(dd, (void __user *) arg,
3331 &req_task, outtotal);
3332
3333 if (copy_to_user((void __user *) arg, &req_task,
3334 compat_tasksize -
3335 (2 * sizeof(compat_long_t))))
3336 return -EFAULT;
3337
3338 if (put_user(req_task.out_size, &compat_req_task->out_size))
3339 return -EFAULT;
3340
3341 if (put_user(req_task.in_size, &compat_req_task->in_size))
3342 return -EFAULT;
3343
3344 return ret;
3345 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003346 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003347 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003348 }
3349}
Jens Axboe16d02c02011-09-27 15:50:01 -06003350#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003351
3352/*
3353 * Obtain the geometry of the device.
3354 *
3355 * You may think that this function is obsolete, but some applications,
3356 * fdisk for example still used CHS values. This function describes the
3357 * device as having 224 heads and 56 sectors per cylinder. These values are
3358 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3359 * partition is described in terms of a start and end cylinder this means
3360 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3361 * affects performance.
3362 *
3363 * @dev Pointer to the block_device strucutre.
3364 * @geo Pointer to a hd_geometry structure.
3365 *
3366 * return value
3367 * 0 Operation completed successfully.
3368 * -ENOTTY An error occurred while reading the drive capacity.
3369 */
3370static int mtip_block_getgeo(struct block_device *dev,
3371 struct hd_geometry *geo)
3372{
3373 struct driver_data *dd = dev->bd_disk->private_data;
3374 sector_t capacity;
3375
3376 if (!dd)
3377 return -ENOTTY;
3378
3379 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3380 dev_warn(&dd->pdev->dev,
3381 "Could not get drive capacity.\n");
3382 return -ENOTTY;
3383 }
3384
3385 geo->heads = 224;
3386 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003387 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003388 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003389 return 0;
3390}
3391
3392/*
3393 * Block device operation function.
3394 *
3395 * This structure contains pointers to the functions required by the block
3396 * layer.
3397 */
3398static const struct block_device_operations mtip_block_ops = {
3399 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003400#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003401 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003402#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003403 .getgeo = mtip_block_getgeo,
3404 .owner = THIS_MODULE
3405};
3406
3407/*
3408 * Block layer make request function.
3409 *
3410 * This function is called by the kernel to process a BIO for
3411 * the P320 device.
3412 *
3413 * @queue Pointer to the request queue. Unused other than to obtain
3414 * the driver data structure.
3415 * @bio Pointer to the BIO.
3416 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003417 */
Jens Axboea71f4832011-11-05 08:36:21 +01003418static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003419{
3420 struct driver_data *dd = queue->queuedata;
3421 struct scatterlist *sg;
3422 struct bio_vec *bvec;
3423 int nents = 0;
3424 int tag = 0;
3425
Asai Thambi S P45038362012-04-09 08:35:38 +02003426 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3427 bio_endio(bio, -ENXIO);
3428 return;
3429 }
3430
Sam Bradshaw88523a62011-08-30 08:34:26 -06003431 if (unlikely(!bio_has_data(bio))) {
3432 blk_queue_flush(queue, 0);
3433 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003434 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003435 }
3436
Sam Bradshaw88523a62011-08-30 08:34:26 -06003437 sg = mtip_hw_get_scatterlist(dd, &tag);
3438 if (likely(sg != NULL)) {
3439 blk_queue_bounce(queue, &bio);
3440
3441 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3442 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003443 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003444 bio_io_error(bio);
3445 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003446 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003447 }
3448
3449 /* Create the scatter list for this bio. */
3450 bio_for_each_segment(bvec, bio, nents) {
3451 sg_set_page(&sg[nents],
3452 bvec->bv_page,
3453 bvec->bv_len,
3454 bvec->bv_offset);
3455 }
3456
3457 /* Issue the read/write. */
3458 mtip_hw_submit_io(dd,
3459 bio->bi_sector,
3460 bio_sectors(bio),
3461 nents,
3462 tag,
3463 bio_endio,
3464 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003465 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003466 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003467 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003468}
3469
3470/*
3471 * Block layer initialization function.
3472 *
3473 * This function is called once by the PCI layer for each P320
3474 * device that is connected to the system.
3475 *
3476 * @dd Pointer to the driver data structure.
3477 *
3478 * return value
3479 * 0 on success else an error code.
3480 */
Jens Axboe63166682011-09-27 21:27:43 -06003481static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003482{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003483 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003484 sector_t capacity;
3485 unsigned int index = 0;
3486 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003487 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003488
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003489 if (dd->disk)
3490 goto skip_create_disk; /* hw init done, before rebuild */
3491
Sam Bradshaw88523a62011-08-30 08:34:26 -06003492 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003493 wait_for_rebuild = mtip_hw_init(dd);
3494 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003495 dev_err(&dd->pdev->dev,
3496 "Protocol layer initialization failed\n");
3497 rv = -EINVAL;
3498 goto protocol_init_error;
3499 }
3500
Sam Bradshaw88523a62011-08-30 08:34:26 -06003501 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3502 if (dd->disk == NULL) {
3503 dev_err(&dd->pdev->dev,
3504 "Unable to allocate gendisk structure\n");
3505 rv = -EINVAL;
3506 goto alloc_disk_error;
3507 }
3508
3509 /* Generate the disk name, implemented same as in sd.c */
3510 do {
3511 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3512 goto ida_get_error;
3513
3514 spin_lock(&rssd_index_lock);
3515 rv = ida_get_new(&rssd_index_ida, &index);
3516 spin_unlock(&rssd_index_lock);
3517 } while (rv == -EAGAIN);
3518
3519 if (rv)
3520 goto ida_get_error;
3521
3522 rv = rssd_disk_name_format("rssd",
3523 index,
3524 dd->disk->disk_name,
3525 DISK_NAME_LEN);
3526 if (rv)
3527 goto disk_index_error;
3528
3529 dd->disk->driverfs_dev = &dd->pdev->dev;
3530 dd->disk->major = dd->major;
3531 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3532 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003533 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534 dd->index = index;
3535
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003536 /*
3537 * if rebuild pending, start the service thread, and delay the block
3538 * queue creation and add_disk()
3539 */
3540 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3541 goto start_service_thread;
3542
3543skip_create_disk:
3544 /* Allocate the request queue. */
3545 dd->queue = blk_alloc_queue(GFP_KERNEL);
3546 if (dd->queue == NULL) {
3547 dev_err(&dd->pdev->dev,
3548 "Unable to allocate request queue\n");
3549 rv = -ENOMEM;
3550 goto block_queue_alloc_init_error;
3551 }
3552
3553 /* Attach our request function to the request queue. */
3554 blk_queue_make_request(dd->queue, mtip_make_request);
3555
3556 dd->disk->queue = dd->queue;
3557 dd->queue->queuedata = dd;
3558
3559 /* Set device limits. */
3560 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3561 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3562 blk_queue_physical_block_size(dd->queue, 4096);
3563 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003564 /*
3565 * write back cache is not supported in the device. FUA depends on
3566 * write back cache support, hence setting flush support to zero.
3567 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003568 blk_queue_flush(dd->queue, 0);
3569
Sam Bradshaw88523a62011-08-30 08:34:26 -06003570 /* Set the capacity of the device in 512 byte sectors. */
3571 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3572 dev_warn(&dd->pdev->dev,
3573 "Could not read drive capacity\n");
3574 rv = -EIO;
3575 goto read_capacity_error;
3576 }
3577 set_capacity(dd->disk, capacity);
3578
3579 /* Enable the block device and add it to /dev */
3580 add_disk(dd->disk);
3581
3582 /*
3583 * Now that the disk is active, initialize any sysfs attributes
3584 * managed by the protocol layer.
3585 */
3586 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3587 if (kobj) {
3588 mtip_hw_sysfs_init(dd, kobj);
3589 kobject_put(kobj);
3590 }
3591
Asai Thambi S P45038362012-04-09 08:35:38 +02003592 if (dd->mtip_svc_handler) {
3593 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003594 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003595 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003596
3597start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003598 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3599
3600 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3601 dd, thd_name);
3602
3603 if (IS_ERR(dd->mtip_svc_handler)) {
3604 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3605 dd->mtip_svc_handler = NULL;
3606 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003607 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003608 }
3609
Asai Thambi S P45038362012-04-09 08:35:38 +02003610 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3611 rv = wait_for_rebuild;
3612
Sam Bradshaw88523a62011-08-30 08:34:26 -06003613 return rv;
3614
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003615kthread_run_error:
3616 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003617 del_gendisk(dd->disk);
3618
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003619read_capacity_error:
3620 blk_cleanup_queue(dd->queue);
3621
3622block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003623disk_index_error:
3624 spin_lock(&rssd_index_lock);
3625 ida_remove(&rssd_index_ida, index);
3626 spin_unlock(&rssd_index_lock);
3627
3628ida_get_error:
3629 put_disk(dd->disk);
3630
3631alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003632 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003633
3634protocol_init_error:
3635 return rv;
3636}
3637
3638/*
3639 * Block layer deinitialization function.
3640 *
3641 * Called by the PCI layer as each P320 device is removed.
3642 *
3643 * @dd Pointer to the driver data structure.
3644 *
3645 * return value
3646 * 0
3647 */
Jens Axboe63166682011-09-27 21:27:43 -06003648static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003649{
3650 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003651
3652 if (dd->mtip_svc_handler) {
3653 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3654 wake_up_interruptible(&dd->port->svc_wait);
3655 kthread_stop(dd->mtip_svc_handler);
3656 }
3657
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003658 /* Clean up the sysfs attributes, if created */
Asai Thambi S P45038362012-04-09 08:35:38 +02003659 if (test_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag)) {
3660 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3661 if (kobj) {
3662 mtip_hw_sysfs_exit(dd, kobj);
3663 kobject_put(kobj);
3664 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003665 }
3666
3667 /*
3668 * Delete our gendisk structure. This also removes the device
3669 * from /dev
3670 */
3671 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003672
3673 spin_lock(&rssd_index_lock);
3674 ida_remove(&rssd_index_ida, dd->index);
3675 spin_unlock(&rssd_index_lock);
3676
Sam Bradshaw88523a62011-08-30 08:34:26 -06003677 blk_cleanup_queue(dd->queue);
3678 dd->disk = NULL;
3679 dd->queue = NULL;
3680
3681 /* De-initialize the protocol layer. */
3682 mtip_hw_exit(dd);
3683
3684 return 0;
3685}
3686
3687/*
3688 * Function called by the PCI layer when just before the
3689 * machine shuts down.
3690 *
3691 * If a protocol layer shutdown function is present it will be called
3692 * by this function.
3693 *
3694 * @dd Pointer to the driver data structure.
3695 *
3696 * return value
3697 * 0
3698 */
Jens Axboe63166682011-09-27 21:27:43 -06003699static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003700{
3701 dev_info(&dd->pdev->dev,
3702 "Shutting down %s ...\n", dd->disk->disk_name);
3703
3704 /* Delete our gendisk structure, and cleanup the blk queue. */
3705 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003706
3707 spin_lock(&rssd_index_lock);
3708 ida_remove(&rssd_index_ida, dd->index);
3709 spin_unlock(&rssd_index_lock);
3710
Sam Bradshaw88523a62011-08-30 08:34:26 -06003711 blk_cleanup_queue(dd->queue);
3712 dd->disk = NULL;
3713 dd->queue = NULL;
3714
3715 mtip_hw_shutdown(dd);
3716 return 0;
3717}
3718
Jens Axboe63166682011-09-27 21:27:43 -06003719static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003720{
3721 dev_info(&dd->pdev->dev,
3722 "Suspending %s ...\n", dd->disk->disk_name);
3723 mtip_hw_suspend(dd);
3724 return 0;
3725}
3726
Jens Axboe63166682011-09-27 21:27:43 -06003727static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003728{
3729 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3730 dd->disk->disk_name);
3731 mtip_hw_resume(dd);
3732 return 0;
3733}
3734
3735/*
3736 * Called for each supported PCI device detected.
3737 *
3738 * This function allocates the private data structure, enables the
3739 * PCI device and then calls the block layer initialization function.
3740 *
3741 * return value
3742 * 0 on success else an error code.
3743 */
3744static int mtip_pci_probe(struct pci_dev *pdev,
3745 const struct pci_device_id *ent)
3746{
3747 int rv = 0;
3748 struct driver_data *dd = NULL;
3749
3750 /* Allocate memory for this devices private data. */
3751 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3752 if (dd == NULL) {
3753 dev_err(&pdev->dev,
3754 "Unable to allocate memory for driver data\n");
3755 return -ENOMEM;
3756 }
3757
Sam Bradshaw88523a62011-08-30 08:34:26 -06003758 /* Attach the private data to this PCI device. */
3759 pci_set_drvdata(pdev, dd);
3760
3761 rv = pcim_enable_device(pdev);
3762 if (rv < 0) {
3763 dev_err(&pdev->dev, "Unable to enable device\n");
3764 goto iomap_err;
3765 }
3766
3767 /* Map BAR5 to memory. */
3768 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3769 if (rv < 0) {
3770 dev_err(&pdev->dev, "Unable to map regions\n");
3771 goto iomap_err;
3772 }
3773
3774 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3775 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3776
3777 if (rv) {
3778 rv = pci_set_consistent_dma_mask(pdev,
3779 DMA_BIT_MASK(32));
3780 if (rv) {
3781 dev_warn(&pdev->dev,
3782 "64-bit DMA enable failed\n");
3783 goto setmask_err;
3784 }
3785 }
3786 }
3787
3788 pci_set_master(pdev);
3789
3790 if (pci_enable_msi(pdev)) {
3791 dev_warn(&pdev->dev,
3792 "Unable to enable MSI interrupt.\n");
3793 goto block_initialize_err;
3794 }
3795
3796 /* Copy the info we may need later into the private data structure. */
3797 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003798 dd->instance = instance;
3799 dd->pdev = pdev;
3800
3801 /* Initialize the block layer. */
3802 rv = mtip_block_initialize(dd);
3803 if (rv < 0) {
3804 dev_err(&pdev->dev,
3805 "Unable to initialize block layer\n");
3806 goto block_initialize_err;
3807 }
3808
3809 /*
3810 * Increment the instance count so that each device has a unique
3811 * instance number.
3812 */
3813 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003814 if (rv != MTIP_FTL_REBUILD_MAGIC)
3815 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003816 goto done;
3817
3818block_initialize_err:
3819 pci_disable_msi(pdev);
3820
3821setmask_err:
3822 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3823
3824iomap_err:
3825 kfree(dd);
3826 pci_set_drvdata(pdev, NULL);
3827 return rv;
3828done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003829 return rv;
3830}
3831
3832/*
3833 * Called for each probed device when the device is removed or the
3834 * driver is unloaded.
3835 *
3836 * return value
3837 * None
3838 */
3839static void mtip_pci_remove(struct pci_dev *pdev)
3840{
3841 struct driver_data *dd = pci_get_drvdata(pdev);
3842 int counter = 0;
3843
Asai Thambi S P45038362012-04-09 08:35:38 +02003844 set_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag);
3845
Sam Bradshaw88523a62011-08-30 08:34:26 -06003846 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003847 while (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003848 counter++;
3849 msleep(20);
3850 if (counter == 10) {
3851 /* Cleanup the outstanding commands */
3852 mtip_command_cleanup(dd);
3853 break;
3854 }
3855 }
3856 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003857
3858 /* Clean up the block layer. */
3859 mtip_block_remove(dd);
3860
3861 pci_disable_msi(pdev);
3862
3863 kfree(dd);
3864 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3865}
3866
3867/*
3868 * Called for each probed device when the device is suspended.
3869 *
3870 * return value
3871 * 0 Success
3872 * <0 Error
3873 */
3874static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3875{
3876 int rv = 0;
3877 struct driver_data *dd = pci_get_drvdata(pdev);
3878
3879 if (!dd) {
3880 dev_err(&pdev->dev,
3881 "Driver private datastructure is NULL\n");
3882 return -EFAULT;
3883 }
3884
Asai Thambi S P45038362012-04-09 08:35:38 +02003885 set_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003886
3887 /* Disable ports & interrupts then send standby immediate */
3888 rv = mtip_block_suspend(dd);
3889 if (rv < 0) {
3890 dev_err(&pdev->dev,
3891 "Failed to suspend controller\n");
3892 return rv;
3893 }
3894
3895 /*
3896 * Save the pci config space to pdev structure &
3897 * disable the device
3898 */
3899 pci_save_state(pdev);
3900 pci_disable_device(pdev);
3901
3902 /* Move to Low power state*/
3903 pci_set_power_state(pdev, PCI_D3hot);
3904
3905 return rv;
3906}
3907
3908/*
3909 * Called for each probed device when the device is resumed.
3910 *
3911 * return value
3912 * 0 Success
3913 * <0 Error
3914 */
3915static int mtip_pci_resume(struct pci_dev *pdev)
3916{
3917 int rv = 0;
3918 struct driver_data *dd;
3919
3920 dd = pci_get_drvdata(pdev);
3921 if (!dd) {
3922 dev_err(&pdev->dev,
3923 "Driver private datastructure is NULL\n");
3924 return -EFAULT;
3925 }
3926
3927 /* Move the device to active State */
3928 pci_set_power_state(pdev, PCI_D0);
3929
3930 /* Restore PCI configuration space */
3931 pci_restore_state(pdev);
3932
3933 /* Enable the PCI device*/
3934 rv = pcim_enable_device(pdev);
3935 if (rv < 0) {
3936 dev_err(&pdev->dev,
3937 "Failed to enable card during resume\n");
3938 goto err;
3939 }
3940 pci_set_master(pdev);
3941
3942 /*
3943 * Calls hbaReset, initPort, & startPort function
3944 * then enables interrupts
3945 */
3946 rv = mtip_block_resume(dd);
3947 if (rv < 0)
3948 dev_err(&pdev->dev, "Unable to resume\n");
3949
3950err:
Asai Thambi S P45038362012-04-09 08:35:38 +02003951 clear_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003952
3953 return rv;
3954}
3955
3956/*
3957 * Shutdown routine
3958 *
3959 * return value
3960 * None
3961 */
3962static void mtip_pci_shutdown(struct pci_dev *pdev)
3963{
3964 struct driver_data *dd = pci_get_drvdata(pdev);
3965 if (dd)
3966 mtip_block_shutdown(dd);
3967}
3968
Sam Bradshaw88523a62011-08-30 08:34:26 -06003969/* Table of device ids supported by this driver. */
3970static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3971 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3972 { 0 }
3973};
3974
3975/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003976static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003977 .name = MTIP_DRV_NAME,
3978 .id_table = mtip_pci_tbl,
3979 .probe = mtip_pci_probe,
3980 .remove = mtip_pci_remove,
3981 .suspend = mtip_pci_suspend,
3982 .resume = mtip_pci_resume,
3983 .shutdown = mtip_pci_shutdown,
3984};
3985
3986MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3987
3988/*
3989 * Module initialization function.
3990 *
3991 * Called once when the module is loaded. This function allocates a major
3992 * block device number to the Cyclone devices and registers the PCI layer
3993 * of the driver.
3994 *
3995 * Return value
3996 * 0 on success else error code.
3997 */
3998static int __init mtip_init(void)
3999{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004000 int error;
4001
Sam Bradshaw88523a62011-08-30 08:34:26 -06004002 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4003
4004 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004005 error = register_blkdev(0, MTIP_DRV_NAME);
4006 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004007 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004008 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004009 return -EBUSY;
4010 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004011 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004012
4013 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004014 error = pci_register_driver(&mtip_pci_driver);
4015 if (error)
4016 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4017
4018 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004019}
4020
4021/*
4022 * Module de-initialization function.
4023 *
4024 * Called once when the module is unloaded. This function deallocates
4025 * the major block device number allocated by mtip_init() and
4026 * unregisters the PCI layer of the driver.
4027 *
4028 * Return value
4029 * none
4030 */
4031static void __exit mtip_exit(void)
4032{
4033 /* Release the allocated major block device number. */
4034 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4035
4036 /* Unregister the PCI driver. */
4037 pci_unregister_driver(&mtip_pci_driver);
4038}
4039
4040MODULE_AUTHOR("Micron Technology, Inc");
4041MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4042MODULE_LICENSE("GPL");
4043MODULE_VERSION(MTIP_DRV_VERSION);
4044
4045module_init(mtip_init);
4046module_exit(mtip_exit);