blob: e57864a7912e675d33b5344f84d116d34d805b09 [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
Jens Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
34#include <linux/bio.h>
35#include <linux/dma-mapping.h>
36#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010037#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060038#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020039#include <linux/export.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060040#include "mtip32xx.h"
41
42#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
43#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
44#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
45#define HW_PORT_PRIV_DMA_SZ \
46 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
47
Asai Thambi S P45038362012-04-09 08:35:38 +020048#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060049#define HOST_HSORG 0xFC
50#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
51#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
52#define HSORG_HWREV 0xFF00
53#define HSORG_STYLE 0x8
54#define HSORG_SLOTGROUPS 0x7
55
56#define PORT_COMMAND_ISSUE 0x38
57#define PORT_SDBV 0x7C
58
59#define PORT_OFFSET 0x100
60#define PORT_MEM_SIZE 0x80
61
62#define PORT_IRQ_ERR \
63 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
64 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
65 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
66 PORT_IRQ_OVERFLOW)
67#define PORT_IRQ_LEGACY \
68 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
69#define PORT_IRQ_HANDLED \
70 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
71 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
72 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
73#define DEF_PORT_IRQ \
74 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
75
76/* product numbers */
77#define MTIP_PRODUCT_UNKNOWN 0x00
78#define MTIP_PRODUCT_ASICFPGA 0x11
79
80/* Device instance number, incremented each time a device is probed. */
81static int instance;
82
83/*
84 * Global variable used to hold the major block device number
85 * allocated in mtip_init().
86 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060087static int mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -060088
89static DEFINE_SPINLOCK(rssd_index_lock);
90static DEFINE_IDA(rssd_index_ida);
91
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010092static int mtip_block_initialize(struct driver_data *dd);
93
Jens Axboe16d02c02011-09-27 15:50:01 -060094#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060095struct mtip_compat_ide_task_request_s {
96 __u8 io_ports[8];
97 __u8 hob_ports[8];
98 ide_reg_valid_t out_flags;
99 ide_reg_valid_t in_flags;
100 int data_phase;
101 int req_cmd;
102 compat_ulong_t out_size;
103 compat_ulong_t in_size;
104};
Jens Axboe16d02c02011-09-27 15:50:01 -0600105#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600106
Sam Bradshaw88523a62011-08-30 08:34:26 -0600107/*
Jens Axboe63166682011-09-27 21:27:43 -0600108 * This function check_for_surprise_removal is called
109 * while card is removed from the system and it will
110 * read the vendor id from the configration space
111 *
112 * @pdev Pointer to the pci_dev structure.
113 *
114 * return value
115 * true if device removed, else false
116 */
117static bool mtip_check_surprise_removal(struct pci_dev *pdev)
118{
119 u16 vendor_id = 0;
120
121 /* Read the vendorID from the configuration space */
122 pci_read_config_word(pdev, 0x00, &vendor_id);
123 if (vendor_id == 0xFFFF)
124 return true; /* device removed */
125
126 return false; /* device present */
127}
128
129/*
130 * This function is called for clean the pending command in the
131 * command slot during the surprise removal of device and return
132 * error to the upper layer.
133 *
134 * @dd Pointer to the DRIVER_DATA structure.
135 *
136 * return value
137 * None
138 */
139static void mtip_command_cleanup(struct driver_data *dd)
140{
141 int group = 0, commandslot = 0, commandindex = 0;
142 struct mtip_cmd *command;
143 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200144 static int in_progress;
145
146 if (in_progress)
147 return;
148
149 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600150
151 for (group = 0; group < 4; group++) {
152 for (commandslot = 0; commandslot < 32; commandslot++) {
153 if (!(port->allocated[group] & (1 << commandslot)))
154 continue;
155
156 commandindex = group << 5 | commandslot;
157 command = &port->commands[commandindex];
158
159 if (atomic_read(&command->active)
160 && (command->async_callback)) {
161 command->async_callback(command->async_data,
162 -ENODEV);
163 command->async_callback = NULL;
164 command->async_data = NULL;
165 }
166
167 dma_unmap_sg(&port->dd->pdev->dev,
168 command->sg,
169 command->scatter_ents,
170 command->direction);
171 }
172 }
173
174 up(&port->cmd_slot);
175
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200176 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +0200177 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600178}
179
180/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600181 * Obtain an empty command slot.
182 *
183 * This function needs to be reentrant since it could be called
184 * at the same time on multiple CPUs. The allocation of the
185 * command slot must be atomic.
186 *
187 * @port Pointer to the port data structure.
188 *
189 * return value
190 * >= 0 Index of command slot obtained.
191 * -1 No command slots available.
192 */
193static int get_slot(struct mtip_port *port)
194{
195 int slot, i;
196 unsigned int num_command_slots = port->dd->slot_groups * 32;
197
198 /*
199 * Try 10 times, because there is a small race here.
200 * that's ok, because it's still cheaper than a lock.
201 *
202 * Race: Since this section is not protected by lock, same bit
203 * could be chosen by different process contexts running in
204 * different processor. So instead of costly lock, we are going
205 * with loop.
206 */
207 for (i = 0; i < 10; i++) {
208 slot = find_next_zero_bit(port->allocated,
209 num_command_slots, 1);
210 if ((slot < num_command_slots) &&
211 (!test_and_set_bit(slot, port->allocated)))
212 return slot;
213 }
214 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
215
216 if (mtip_check_surprise_removal(port->dd->pdev)) {
217 /* Device not present, clean outstanding commands */
218 mtip_command_cleanup(port->dd);
219 }
220 return -1;
221}
222
223/*
224 * Release a command slot.
225 *
226 * @port Pointer to the port data structure.
227 * @tag Tag of command to release
228 *
229 * return value
230 * None
231 */
232static inline void release_slot(struct mtip_port *port, int tag)
233{
234 smp_mb__before_clear_bit();
235 clear_bit(tag, port->allocated);
236 smp_mb__after_clear_bit();
237}
238
239/*
Jens Axboe63166682011-09-27 21:27:43 -0600240 * Reset the HBA (without sleeping)
241 *
242 * Just like hba_reset, except does not call sleep, so can be
243 * run from interrupt/tasklet context.
244 *
245 * @dd Pointer to the driver data structure.
246 *
247 * return value
248 * 0 The reset was successful.
249 * -1 The HBA Reset bit did not clear.
250 */
251static int hba_reset_nosleep(struct driver_data *dd)
252{
253 unsigned long timeout;
254
255 /* Chip quirk: quiesce any chip function */
256 mdelay(10);
257
258 /* Set the reset bit */
259 writel(HOST_RESET, dd->mmio + HOST_CTL);
260
261 /* Flush */
262 readl(dd->mmio + HOST_CTL);
263
264 /*
265 * Wait 10ms then spin for up to 1 second
266 * waiting for reset acknowledgement
267 */
268 timeout = jiffies + msecs_to_jiffies(1000);
269 mdelay(10);
270 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
271 && time_before(jiffies, timeout))
272 mdelay(1);
273
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200274 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200275 return -1;
276
Jens Axboe63166682011-09-27 21:27:43 -0600277 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
278 return -1;
279
280 return 0;
281}
282
283/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600284 * Issue a command to the hardware.
285 *
286 * Set the appropriate bit in the s_active and Command Issue hardware
287 * registers, causing hardware command processing to begin.
288 *
289 * @port Pointer to the port structure.
290 * @tag The tag of the command to be issued.
291 *
292 * return value
293 * None
294 */
295static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
296{
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 P8a857a82012-04-09 08:35:38 +0200470 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200471 return;
472
Jens Axboe63166682011-09-27 21:27:43 -0600473 /*
474 * Chip quirk: escalate to hba reset if
475 * PxCMD.CR not clear after 500 ms
476 */
477 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
478 dev_warn(&port->dd->pdev->dev,
479 "PxCMD.CR not clear, escalating reset\n");
480
481 if (hba_reset_nosleep(port->dd))
482 dev_err(&port->dd->pdev->dev,
483 "HBA reset escalation failed.\n");
484
485 /* 30 ms delay before com reset to quiesce chip */
486 mdelay(30);
487 }
488
489 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
490
491 /* Set PxSCTL.DET */
492 writel(readl(port->mmio + PORT_SCR_CTL) |
493 1, port->mmio + PORT_SCR_CTL);
494 readl(port->mmio + PORT_SCR_CTL);
495
496 /* Wait 1 ms to quiesce chip function */
497 timeout = jiffies + msecs_to_jiffies(1);
498 while (time_before(jiffies, timeout))
499 ;
500
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200501 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200502 return;
503
Jens Axboe63166682011-09-27 21:27:43 -0600504 /* Clear PxSCTL.DET */
505 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
506 port->mmio + PORT_SCR_CTL);
507 readl(port->mmio + PORT_SCR_CTL);
508
509 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
510 timeout = jiffies + msecs_to_jiffies(500);
511 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
512 && time_before(jiffies, timeout))
513 ;
514
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200515 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200516 return;
517
Jens Axboe63166682011-09-27 21:27:43 -0600518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
521
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100522 mtip_init_port(port);
523 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600524
Jens Axboe63166682011-09-27 21:27:43 -0600525}
526
527/*
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 P8a857a82012-04-09 08:35:38 +0200548 if (test_bit(MTIP_DDF_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 P8a857a82012-04-09 08:35:38 +0200575 set_bit(MTIP_PF_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 P8a857a82012-04-09 08:35:38 +0200613 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100614 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 P8a857a82012-04-09 08:35:38 +0200768 set_bit(MTIP_PF_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");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200829 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200830 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");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200836 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200837 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 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200923 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100924 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 P8a857a82012-04-09 08:35:38 +0200991 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100992 (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 P8a857a82012-04-09 08:35:38 +02001058 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001059 &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 P8a857a82012-04-09 08:35:38 +02001126 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1127 test_bit(MTIP_PF_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 P8a857a82012-04-09 08:35:38 +02001131 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001132 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133 /*
1134 * Ignore s_active bit 0 of array element 0.
1135 * This bit will always be set
1136 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001137 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001138 for (n = 1; n < port->dd->slot_groups; n++)
1139 active |= readl(port->s_active[n]);
1140
1141 if (!active)
1142 break;
1143
1144 msleep(20);
1145 } while (time_before(jiffies, to));
1146
1147 return active ? -EBUSY : 0;
1148}
1149
1150/*
1151 * Execute an internal command and wait for the completion.
1152 *
1153 * @port Pointer to the port data structure.
1154 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001155 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001156 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001157 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001158 * @opts Command header options, excluding the FIS length
1159 * and the number of PRD entries.
1160 * @timeout Time in ms to wait for the command to complete.
1161 *
1162 * return value
1163 * 0 Command completed successfully.
1164 * -EFAULT The buffer address is not correctly aligned.
1165 * -EBUSY Internal command or other IO in progress.
1166 * -EAGAIN Time out waiting for command to complete.
1167 */
1168static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001169 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001170 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001171 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001172 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001173 u32 opts,
1174 gfp_t atomic,
1175 unsigned long timeout)
1176{
1177 struct mtip_cmd_sg *command_sg;
1178 DECLARE_COMPLETION_ONSTACK(wait);
1179 int rv = 0;
1180 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1181
1182 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1183 if (buffer & 0x00000007) {
1184 dev_err(&port->dd->pdev->dev,
1185 "SG buffer is not 8 byte aligned\n");
1186 return -EFAULT;
1187 }
1188
1189 /* Only one internal command should be running at a time */
1190 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1191 dev_warn(&port->dd->pdev->dev,
1192 "Internal command already active\n");
1193 return -EBUSY;
1194 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001195 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001196
1197 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001198 if (fis->command != ATA_CMD_STANDBYNOW1) {
1199 /* wait for io to complete if non atomic */
1200 if (mtip_quiesce_io(port, 5000) < 0) {
1201 dev_warn(&port->dd->pdev->dev,
1202 "Failed to quiesce IO\n");
1203 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001204 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001205 wake_up_interruptible(&port->svc_wait);
1206 return -EBUSY;
1207 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001208 }
1209
1210 /* Set the completion function and data for the command. */
1211 int_cmd->comp_data = &wait;
1212 int_cmd->comp_func = mtip_completion;
1213
1214 } else {
1215 /* Clear completion - we're going to poll */
1216 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001217 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001218 }
1219
1220 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001221 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001222
1223 /* Populate the SG list */
1224 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001225 __force_bit2int cpu_to_le32(opts | fis_len);
1226 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001227 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1228
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001229 command_sg->info =
1230 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1231 command_sg->dba =
1232 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1233 command_sg->dba_upper =
1234 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001235
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001236 int_cmd->command_header->opts |=
1237 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001238 }
1239
1240 /* Populate the command header */
1241 int_cmd->command_header->byte_count = 0;
1242
1243 /* Issue the command to the hardware */
1244 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1245
1246 /* Poll if atomic, wait_for_completion otherwise */
1247 if (atomic == GFP_KERNEL) {
1248 /* Wait for the command to complete or timeout. */
1249 if (wait_for_completion_timeout(
1250 &wait,
1251 msecs_to_jiffies(timeout)) == 0) {
1252 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001253 "Internal command did not complete [%d] "
1254 "within timeout of %lu ms\n",
1255 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001256 if (mtip_check_surprise_removal(port->dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001257 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001258 &port->dd->dd_flag)) {
1259 rv = -ENXIO;
1260 goto exec_ic_exit;
1261 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001262 rv = -EAGAIN;
1263 }
1264
1265 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1266 & (1 << MTIP_TAG_INTERNAL)) {
1267 dev_warn(&port->dd->pdev->dev,
1268 "Retiring internal command but CI is 1.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001269 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001270 &port->dd->dd_flag)) {
1271 hba_reset_nosleep(port->dd);
1272 rv = -ENXIO;
1273 } else {
1274 mtip_restart_port(port);
1275 rv = -EAGAIN;
1276 }
1277 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001278 }
1279
1280 } else {
1281 /* Spin for <timeout> checking if command still outstanding */
1282 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001283 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1284 & (1 << MTIP_TAG_INTERNAL))
1285 && time_before(jiffies, timeout)) {
1286 if (mtip_check_surprise_removal(port->dd->pdev)) {
1287 rv = -ENXIO;
1288 goto exec_ic_exit;
1289 }
1290 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001291 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001292 &port->dd->dd_flag)) {
1293 rv = -ENXIO;
1294 goto exec_ic_exit;
1295 }
1296 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001297
1298 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1299 & (1 << MTIP_TAG_INTERNAL)) {
1300 dev_err(&port->dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001301 "Internal command did not complete [atomic]\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001302 rv = -EAGAIN;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001303 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02001304 &port->dd->dd_flag)) {
1305 hba_reset_nosleep(port->dd);
1306 rv = -ENXIO;
1307 } else {
1308 mtip_restart_port(port);
1309 rv = -EAGAIN;
1310 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001311 }
1312 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001313exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001314 /* Clear the allocated and active bits for the internal command. */
1315 atomic_set(&int_cmd->active, 0);
1316 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001317 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001318 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001319
1320 return rv;
1321}
1322
1323/*
1324 * Byte-swap ATA ID strings.
1325 *
1326 * ATA identify data contains strings in byte-swapped 16-bit words.
1327 * They must be swapped (on all architectures) to be usable as C strings.
1328 * This function swaps bytes in-place.
1329 *
1330 * @buf The buffer location of the string
1331 * @len The number of bytes to swap
1332 *
1333 * return value
1334 * None
1335 */
1336static inline void ata_swap_string(u16 *buf, unsigned int len)
1337{
1338 int i;
1339 for (i = 0; i < (len/2); i++)
1340 be16_to_cpus(&buf[i]);
1341}
1342
1343/*
1344 * Request the device identity information.
1345 *
1346 * If a user space buffer is not specified, i.e. is NULL, the
1347 * identify information is still read from the drive and placed
1348 * into the identify data buffer (@e port->identify) in the
1349 * port data structure.
1350 * When the identify buffer contains valid identify information @e
1351 * port->identify_valid is non-zero.
1352 *
1353 * @port Pointer to the port structure.
1354 * @user_buffer A user space buffer where the identify data should be
1355 * copied.
1356 *
1357 * return value
1358 * 0 Command completed successfully.
1359 * -EFAULT An error occurred while coping data to the user buffer.
1360 * -1 Command failed.
1361 */
1362static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1363{
1364 int rv = 0;
1365 struct host_to_dev_fis fis;
1366
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001367 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001368 return -EFAULT;
1369
Sam Bradshaw88523a62011-08-30 08:34:26 -06001370 /* Build the FIS. */
1371 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1372 fis.type = 0x27;
1373 fis.opts = 1 << 7;
1374 fis.command = ATA_CMD_ID_ATA;
1375
1376 /* Set the identify information as invalid. */
1377 port->identify_valid = 0;
1378
1379 /* Clear the identify information. */
1380 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1381
1382 /* Execute the command. */
1383 if (mtip_exec_internal_command(port,
1384 &fis,
1385 5,
1386 port->identify_dma,
1387 sizeof(u16) * ATA_ID_WORDS,
1388 0,
1389 GFP_KERNEL,
1390 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1391 < 0) {
1392 rv = -1;
1393 goto out;
1394 }
1395
1396 /*
1397 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1398 * perform field-sensitive swapping on the string fields.
1399 * See the kernel use of ata_id_string() for proof of this.
1400 */
1401#ifdef __LITTLE_ENDIAN
1402 ata_swap_string(port->identify + 27, 40); /* model string*/
1403 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1404 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1405#else
1406 {
1407 int i;
1408 for (i = 0; i < ATA_ID_WORDS; i++)
1409 port->identify[i] = le16_to_cpu(port->identify[i]);
1410 }
1411#endif
1412
1413 /* Set the identify buffer as valid. */
1414 port->identify_valid = 1;
1415
1416 if (user_buffer) {
1417 if (copy_to_user(
1418 user_buffer,
1419 port->identify,
1420 ATA_ID_WORDS * sizeof(u16))) {
1421 rv = -EFAULT;
1422 goto out;
1423 }
1424 }
1425
1426out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001427 return rv;
1428}
1429
1430/*
1431 * Issue a standby immediate command to the device.
1432 *
1433 * @port Pointer to the port structure.
1434 *
1435 * return value
1436 * 0 Command was executed successfully.
1437 * -1 An error occurred while executing the command.
1438 */
1439static int mtip_standby_immediate(struct mtip_port *port)
1440{
1441 int rv;
1442 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001443 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001444
Sam Bradshaw88523a62011-08-30 08:34:26 -06001445 /* Build the FIS. */
1446 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1447 fis.type = 0x27;
1448 fis.opts = 1 << 7;
1449 fis.command = ATA_CMD_STANDBYNOW1;
1450
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001451 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001452 rv = mtip_exec_internal_command(port,
1453 &fis,
1454 5,
1455 0,
1456 0,
1457 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001458 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001459 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001460 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1461 jiffies_to_msecs(jiffies - start));
1462 if (rv)
1463 dev_warn(&port->dd->pdev->dev,
1464 "STANDBY IMMEDIATE command failed.\n");
1465
1466 return rv;
1467}
1468
1469/*
1470 * Issue a READ LOG EXT command to the device.
1471 *
1472 * @port pointer to the port structure.
1473 * @page page number to fetch
1474 * @buffer pointer to buffer
1475 * @buffer_dma dma address corresponding to @buffer
1476 * @sectors page length to fetch, in sectors
1477 *
1478 * return value
1479 * @rv return value from mtip_exec_internal_command()
1480 */
1481static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1482 dma_addr_t buffer_dma, unsigned int sectors)
1483{
1484 struct host_to_dev_fis fis;
1485
1486 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1487 fis.type = 0x27;
1488 fis.opts = 1 << 7;
1489 fis.command = ATA_CMD_READ_LOG_EXT;
1490 fis.sect_count = sectors & 0xFF;
1491 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1492 fis.lba_low = page;
1493 fis.lba_mid = 0;
1494 fis.device = ATA_DEVICE_OBS;
1495
1496 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1497
1498 return mtip_exec_internal_command(port,
1499 &fis,
1500 5,
1501 buffer_dma,
1502 sectors * ATA_SECT_SIZE,
1503 0,
1504 GFP_ATOMIC,
1505 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1506}
1507
1508/*
1509 * Issue a SMART READ DATA command to the device.
1510 *
1511 * @port pointer to the port structure.
1512 * @buffer pointer to buffer
1513 * @buffer_dma dma address corresponding to @buffer
1514 *
1515 * return value
1516 * @rv return value from mtip_exec_internal_command()
1517 */
1518static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1519 dma_addr_t buffer_dma)
1520{
1521 struct host_to_dev_fis fis;
1522
1523 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1524 fis.type = 0x27;
1525 fis.opts = 1 << 7;
1526 fis.command = ATA_CMD_SMART;
1527 fis.features = 0xD0;
1528 fis.sect_count = 1;
1529 fis.lba_mid = 0x4F;
1530 fis.lba_hi = 0xC2;
1531 fis.device = ATA_DEVICE_OBS;
1532
1533 return mtip_exec_internal_command(port,
1534 &fis,
1535 5,
1536 buffer_dma,
1537 ATA_SECT_SIZE,
1538 0,
1539 GFP_ATOMIC,
1540 15000);
1541}
1542
1543/*
1544 * Get the value of a smart attribute
1545 *
1546 * @port pointer to the port structure
1547 * @id attribute number
1548 * @attrib pointer to return attrib information corresponding to @id
1549 *
1550 * return value
1551 * -EINVAL NULL buffer passed or unsupported attribute @id.
1552 * -EPERM Identify data not valid, SMART not supported or not enabled
1553 */
1554static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1555 struct smart_attr *attrib)
1556{
1557 int rv, i;
1558 struct smart_attr *pattr;
1559
1560 if (!attrib)
1561 return -EINVAL;
1562
1563 if (!port->identify_valid) {
1564 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1565 return -EPERM;
1566 }
1567 if (!(port->identify[82] & 0x1)) {
1568 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1569 return -EPERM;
1570 }
1571 if (!(port->identify[85] & 0x1)) {
1572 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1573 return -EPERM;
1574 }
1575
1576 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1577 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1578 if (rv) {
1579 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1580 return rv;
1581 }
1582
1583 pattr = (struct smart_attr *)(port->smart_buf + 2);
1584 for (i = 0; i < 29; i++, pattr++)
1585 if (pattr->attr_id == id) {
1586 memcpy(attrib, pattr, sizeof(struct smart_attr));
1587 break;
1588 }
1589
1590 if (i == 29) {
1591 dev_warn(&port->dd->pdev->dev,
1592 "Query for invalid SMART attribute ID\n");
1593 rv = -EINVAL;
1594 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001595
Sam Bradshaw88523a62011-08-30 08:34:26 -06001596 return rv;
1597}
1598
1599/*
1600 * Get the drive capacity.
1601 *
1602 * @dd Pointer to the device data structure.
1603 * @sectors Pointer to the variable that will receive the sector count.
1604 *
1605 * return value
1606 * 1 Capacity was returned successfully.
1607 * 0 The identify information is invalid.
1608 */
Jens Axboe63166682011-09-27 21:27:43 -06001609static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001610{
1611 struct mtip_port *port = dd->port;
1612 u64 total, raw0, raw1, raw2, raw3;
1613 raw0 = port->identify[100];
1614 raw1 = port->identify[101];
1615 raw2 = port->identify[102];
1616 raw3 = port->identify[103];
1617 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1618 *sectors = total;
1619 return (bool) !!port->identify_valid;
1620}
1621
1622/*
1623 * Reset the HBA.
1624 *
1625 * Resets the HBA by setting the HBA Reset bit in the Global
1626 * HBA Control register. After setting the HBA Reset bit the
1627 * function waits for 1 second before reading the HBA Reset
1628 * bit to make sure it has cleared. If HBA Reset is not clear
1629 * an error is returned. Cannot be used in non-blockable
1630 * context.
1631 *
1632 * @dd Pointer to the driver data structure.
1633 *
1634 * return value
1635 * 0 The reset was successful.
1636 * -1 The HBA Reset bit did not clear.
1637 */
1638static int mtip_hba_reset(struct driver_data *dd)
1639{
1640 mtip_deinit_port(dd->port);
1641
1642 /* Set the reset bit */
1643 writel(HOST_RESET, dd->mmio + HOST_CTL);
1644
1645 /* Flush */
1646 readl(dd->mmio + HOST_CTL);
1647
1648 /* Wait for reset to clear */
1649 ssleep(1);
1650
1651 /* Check the bit has cleared */
1652 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1653 dev_err(&dd->pdev->dev,
1654 "Reset bit did not clear.\n");
1655 return -1;
1656 }
1657
1658 return 0;
1659}
1660
1661/*
1662 * Display the identify command data.
1663 *
1664 * @port Pointer to the port data structure.
1665 *
1666 * return value
1667 * None
1668 */
1669static void mtip_dump_identify(struct mtip_port *port)
1670{
1671 sector_t sectors;
1672 unsigned short revid;
1673 char cbuf[42];
1674
1675 if (!port->identify_valid)
1676 return;
1677
1678 strlcpy(cbuf, (char *)(port->identify+10), 21);
1679 dev_info(&port->dd->pdev->dev,
1680 "Serial No.: %s\n", cbuf);
1681
1682 strlcpy(cbuf, (char *)(port->identify+23), 9);
1683 dev_info(&port->dd->pdev->dev,
1684 "Firmware Ver.: %s\n", cbuf);
1685
1686 strlcpy(cbuf, (char *)(port->identify+27), 41);
1687 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1688
1689 if (mtip_hw_get_capacity(port->dd, &sectors))
1690 dev_info(&port->dd->pdev->dev,
1691 "Capacity: %llu sectors (%llu MB)\n",
1692 (u64)sectors,
1693 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1694
1695 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001696 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001697 case 0x1:
1698 strlcpy(cbuf, "A0", 3);
1699 break;
1700 case 0x3:
1701 strlcpy(cbuf, "A2", 3);
1702 break;
1703 default:
1704 strlcpy(cbuf, "?", 2);
1705 break;
1706 }
1707 dev_info(&port->dd->pdev->dev,
1708 "Card Type: %s\n", cbuf);
1709}
1710
1711/*
1712 * Map the commands scatter list into the command table.
1713 *
1714 * @command Pointer to the command.
1715 * @nents Number of scatter list entries.
1716 *
1717 * return value
1718 * None
1719 */
1720static inline void fill_command_sg(struct driver_data *dd,
1721 struct mtip_cmd *command,
1722 int nents)
1723{
1724 int n;
1725 unsigned int dma_len;
1726 struct mtip_cmd_sg *command_sg;
1727 struct scatterlist *sg = command->sg;
1728
1729 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1730
1731 for (n = 0; n < nents; n++) {
1732 dma_len = sg_dma_len(sg);
1733 if (dma_len > 0x400000)
1734 dev_err(&dd->pdev->dev,
1735 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001736 command_sg->info = __force_bit2int
1737 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1738 command_sg->dba = __force_bit2int
1739 cpu_to_le32(sg_dma_address(sg));
1740 command_sg->dba_upper = __force_bit2int
1741 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001742 command_sg++;
1743 sg++;
1744 }
1745}
1746
1747/*
1748 * @brief Execute a drive command.
1749 *
1750 * return value 0 The command completed successfully.
1751 * return value -1 An error occurred while executing the command.
1752 */
Jens Axboe63166682011-09-27 21:27:43 -06001753static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001754{
1755 struct host_to_dev_fis fis;
1756 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1757
Sam Bradshaw88523a62011-08-30 08:34:26 -06001758 /* Build the FIS. */
1759 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1760 fis.type = 0x27;
1761 fis.opts = 1 << 7;
1762 fis.command = command[0];
1763 fis.features = command[1];
1764 fis.sect_count = command[2];
1765 fis.sector = command[3];
1766 fis.cyl_low = command[4];
1767 fis.cyl_hi = command[5];
1768 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1769
1770
Asai Thambi S P45038362012-04-09 08:35:38 +02001771 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 -06001772 __func__,
1773 command[0],
1774 command[1],
1775 command[2],
1776 command[3],
1777 command[4],
1778 command[5],
1779 command[6]);
1780
1781 /* Execute the command. */
1782 if (mtip_exec_internal_command(port,
1783 &fis,
1784 5,
1785 0,
1786 0,
1787 0,
1788 GFP_KERNEL,
1789 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001790 return -1;
1791 }
1792
1793 command[0] = reply->command; /* Status*/
1794 command[1] = reply->features; /* Error*/
1795 command[4] = reply->cyl_low;
1796 command[5] = reply->cyl_hi;
1797
Asai Thambi S P45038362012-04-09 08:35:38 +02001798 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 -06001799 __func__,
1800 command[0],
1801 command[1],
1802 command[4],
1803 command[5]);
1804
Sam Bradshaw88523a62011-08-30 08:34:26 -06001805 return 0;
1806}
1807
1808/*
1809 * @brief Execute a drive command.
1810 *
1811 * @param port Pointer to the port data structure.
1812 * @param command Pointer to the user specified command parameters.
1813 * @param user_buffer Pointer to the user space buffer where read sector
1814 * data should be copied.
1815 *
1816 * return value 0 The command completed successfully.
1817 * return value -EFAULT An error occurred while copying the completion
1818 * data to the user space buffer.
1819 * return value -1 An error occurred while executing the command.
1820 */
Jens Axboe63166682011-09-27 21:27:43 -06001821static int exec_drive_command(struct mtip_port *port, u8 *command,
1822 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001823{
1824 struct host_to_dev_fis fis;
1825 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1826
Sam Bradshaw88523a62011-08-30 08:34:26 -06001827 /* Build the FIS. */
1828 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1829 fis.type = 0x27;
1830 fis.opts = 1 << 7;
1831 fis.command = command[0];
1832 fis.features = command[2];
1833 fis.sect_count = command[3];
1834 if (fis.command == ATA_CMD_SMART) {
1835 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001836 fis.cyl_low = 0x4F;
1837 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001838 }
1839
1840 dbg_printk(MTIP_DRV_NAME
1841 "%s: User Command: cmd %x, sect %x, "
1842 "feat %x, sectcnt %x\n",
1843 __func__,
1844 command[0],
1845 command[1],
1846 command[2],
1847 command[3]);
1848
1849 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1850
1851 /* Execute the command. */
1852 if (mtip_exec_internal_command(port,
1853 &fis,
1854 5,
1855 port->sector_buffer_dma,
1856 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1857 0,
1858 GFP_KERNEL,
1859 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1860 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001861 return -1;
1862 }
1863
1864 /* Collect the completion status. */
1865 command[0] = reply->command; /* Status*/
1866 command[1] = reply->features; /* Error*/
1867 command[2] = command[3];
1868
1869 dbg_printk(MTIP_DRV_NAME
1870 "%s: Completion Status: stat %x, "
1871 "err %x, cmd %x\n",
1872 __func__,
1873 command[0],
1874 command[1],
1875 command[2]);
1876
1877 if (user_buffer && command[3]) {
1878 if (copy_to_user(user_buffer,
1879 port->sector_buffer,
1880 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001881 return -EFAULT;
1882 }
1883 }
1884
Sam Bradshaw88523a62011-08-30 08:34:26 -06001885 return 0;
1886}
1887
1888/*
1889 * Indicates whether a command has a single sector payload.
1890 *
1891 * @command passed to the device to perform the certain event.
1892 * @features passed to the device to perform the certain event.
1893 *
1894 * return value
1895 * 1 command is one that always has a single sector payload,
1896 * regardless of the value in the Sector Count field.
1897 * 0 otherwise
1898 *
1899 */
1900static unsigned int implicit_sector(unsigned char command,
1901 unsigned char features)
1902{
1903 unsigned int rv = 0;
1904
1905 /* list of commands that have an implicit sector count of 1 */
1906 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001907 case ATA_CMD_SEC_SET_PASS:
1908 case ATA_CMD_SEC_UNLOCK:
1909 case ATA_CMD_SEC_ERASE_PREP:
1910 case ATA_CMD_SEC_ERASE_UNIT:
1911 case ATA_CMD_SEC_FREEZE_LOCK:
1912 case ATA_CMD_SEC_DISABLE_PASS:
1913 case ATA_CMD_PMP_READ:
1914 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001915 rv = 1;
1916 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001917 case ATA_CMD_SET_MAX:
1918 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001919 rv = 1;
1920 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001921 case ATA_CMD_SMART:
1922 if ((features == ATA_SMART_READ_VALUES) ||
1923 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001924 rv = 1;
1925 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001926 case ATA_CMD_CONF_OVERLAY:
1927 if ((features == ATA_DCO_IDENTIFY) ||
1928 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001929 rv = 1;
1930 break;
1931 }
1932 return rv;
1933}
1934
1935/*
1936 * Executes a taskfile
1937 * See ide_taskfile_ioctl() for derivation
1938 */
1939static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001940 void __user *buf,
1941 ide_task_request_t *req_task,
1942 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943{
1944 struct host_to_dev_fis fis;
1945 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001946 u8 *outbuf = NULL;
1947 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001948 dma_addr_t outbuf_dma = 0;
1949 dma_addr_t inbuf_dma = 0;
1950 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001951 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001952 unsigned int taskin = 0;
1953 unsigned int taskout = 0;
1954 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001955 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1956 unsigned int force_single_sector;
1957 unsigned int transfer_size;
1958 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001959 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001960
1961 taskout = req_task->out_size;
1962 taskin = req_task->in_size;
1963 /* 130560 = 512 * 0xFF*/
1964 if (taskin > 130560 || taskout > 130560) {
1965 err = -EINVAL;
1966 goto abort;
1967 }
1968
1969 if (taskout) {
1970 outbuf = kzalloc(taskout, GFP_KERNEL);
1971 if (outbuf == NULL) {
1972 err = -ENOMEM;
1973 goto abort;
1974 }
1975 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1976 err = -EFAULT;
1977 goto abort;
1978 }
1979 outbuf_dma = pci_map_single(dd->pdev,
1980 outbuf,
1981 taskout,
1982 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001983 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001984 err = -ENOMEM;
1985 goto abort;
1986 }
1987 dma_buffer = outbuf_dma;
1988 }
1989
1990 if (taskin) {
1991 inbuf = kzalloc(taskin, GFP_KERNEL);
1992 if (inbuf == NULL) {
1993 err = -ENOMEM;
1994 goto abort;
1995 }
1996
1997 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1998 err = -EFAULT;
1999 goto abort;
2000 }
2001 inbuf_dma = pci_map_single(dd->pdev,
2002 inbuf,
2003 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002004 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005 err = -ENOMEM;
2006 goto abort;
2007 }
2008 dma_buffer = inbuf_dma;
2009 }
2010
2011 /* only supports PIO and non-data commands from this ioctl. */
2012 switch (req_task->data_phase) {
2013 case TASKFILE_OUT:
2014 nsect = taskout / ATA_SECT_SIZE;
2015 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2016 break;
2017 case TASKFILE_IN:
2018 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2019 break;
2020 case TASKFILE_NO_DATA:
2021 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2022 break;
2023 default:
2024 err = -EINVAL;
2025 goto abort;
2026 }
2027
Sam Bradshaw88523a62011-08-30 08:34:26 -06002028 /* Build the FIS. */
2029 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2030
2031 fis.type = 0x27;
2032 fis.opts = 1 << 7;
2033 fis.command = req_task->io_ports[7];
2034 fis.features = req_task->io_ports[1];
2035 fis.sect_count = req_task->io_ports[2];
2036 fis.lba_low = req_task->io_ports[3];
2037 fis.lba_mid = req_task->io_ports[4];
2038 fis.lba_hi = req_task->io_ports[5];
2039 /* Clear the dev bit*/
2040 fis.device = req_task->io_ports[6] & ~0x10;
2041
2042 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2043 req_task->in_flags.all =
2044 IDE_TASKFILE_STD_IN_FLAGS |
2045 (IDE_HOB_STD_IN_FLAGS << 8);
2046 fis.lba_low_ex = req_task->hob_ports[3];
2047 fis.lba_mid_ex = req_task->hob_ports[4];
2048 fis.lba_hi_ex = req_task->hob_ports[5];
2049 fis.features_ex = req_task->hob_ports[1];
2050 fis.sect_cnt_ex = req_task->hob_ports[2];
2051
2052 } else {
2053 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2054 }
2055
2056 force_single_sector = implicit_sector(fis.command, fis.features);
2057
2058 if ((taskin || taskout) && (!fis.sect_count)) {
2059 if (nsect)
2060 fis.sect_count = nsect;
2061 else {
2062 if (!force_single_sector) {
2063 dev_warn(&dd->pdev->dev,
2064 "data movement but "
2065 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002066 err = -EINVAL;
2067 goto abort;
2068 }
2069 }
2070 }
2071
2072 dbg_printk(MTIP_DRV_NAME
2073 "taskfile: cmd %x, feat %x, nsect %x,"
2074 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2075 " head/dev %x\n",
2076 fis.command,
2077 fis.features,
2078 fis.sect_count,
2079 fis.lba_low,
2080 fis.lba_mid,
2081 fis.lba_hi,
2082 fis.device);
2083
2084 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002085 case ATA_CMD_DOWNLOAD_MICRO:
2086 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002087 timeout = 60000;
2088 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002089 case ATA_CMD_SEC_ERASE_UNIT:
2090 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002091 timeout = 240000;
2092 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002093 case ATA_CMD_STANDBYNOW1:
2094 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002095 timeout = 10000;
2096 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002097 case 0xF7:
2098 case 0xFA:
2099 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002100 timeout = 10000;
2101 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002102 case ATA_CMD_SMART:
2103 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002104 timeout = 10000;
2105 break;
2106 default:
2107 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2108 break;
2109 }
2110
2111 /* Determine the correct transfer size.*/
2112 if (force_single_sector)
2113 transfer_size = ATA_SECT_SIZE;
2114 else
2115 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2116
2117 /* Execute the command.*/
2118 if (mtip_exec_internal_command(dd->port,
2119 &fis,
2120 5,
2121 dma_buffer,
2122 transfer_size,
2123 0,
2124 GFP_KERNEL,
2125 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002126 err = -EIO;
2127 goto abort;
2128 }
2129
2130 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2131
2132 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2133 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2134 req_task->io_ports[7] = reply->control;
2135 } else {
2136 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2137 req_task->io_ports[7] = reply->command;
2138 }
2139
2140 /* reclaim the DMA buffers.*/
2141 if (inbuf_dma)
2142 pci_unmap_single(dd->pdev, inbuf_dma,
2143 taskin, DMA_FROM_DEVICE);
2144 if (outbuf_dma)
2145 pci_unmap_single(dd->pdev, outbuf_dma,
2146 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002147 inbuf_dma = 0;
2148 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002149
2150 /* return the ATA registers to the caller.*/
2151 req_task->io_ports[1] = reply->features;
2152 req_task->io_ports[2] = reply->sect_count;
2153 req_task->io_ports[3] = reply->lba_low;
2154 req_task->io_ports[4] = reply->lba_mid;
2155 req_task->io_ports[5] = reply->lba_hi;
2156 req_task->io_ports[6] = reply->device;
2157
2158 if (req_task->out_flags.all & 1) {
2159
2160 req_task->hob_ports[3] = reply->lba_low_ex;
2161 req_task->hob_ports[4] = reply->lba_mid_ex;
2162 req_task->hob_ports[5] = reply->lba_hi_ex;
2163 req_task->hob_ports[1] = reply->features_ex;
2164 req_task->hob_ports[2] = reply->sect_cnt_ex;
2165 }
2166
2167 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002168 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06002169 ((fis.command == 0xFC) &&
2170 (fis.features == 0x27 || fis.features == 0x72 ||
2171 fis.features == 0x62 || fis.features == 0x26))) &&
2172 !(reply->command & 1)) {
2173 mtip_restart_port(dd->port);
2174 }
2175
2176 dbg_printk(MTIP_DRV_NAME
2177 "%s: Completion: stat %x,"
2178 "err %x, sect_cnt %x, lbalo %x,"
2179 "lbamid %x, lbahi %x, dev %x\n",
2180 __func__,
2181 req_task->io_ports[7],
2182 req_task->io_ports[1],
2183 req_task->io_ports[2],
2184 req_task->io_ports[3],
2185 req_task->io_ports[4],
2186 req_task->io_ports[5],
2187 req_task->io_ports[6]);
2188
Sam Bradshaw88523a62011-08-30 08:34:26 -06002189 if (taskout) {
2190 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2191 err = -EFAULT;
2192 goto abort;
2193 }
2194 }
2195 if (taskin) {
2196 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2197 err = -EFAULT;
2198 goto abort;
2199 }
2200 }
2201abort:
2202 if (inbuf_dma)
2203 pci_unmap_single(dd->pdev, inbuf_dma,
2204 taskin, DMA_FROM_DEVICE);
2205 if (outbuf_dma)
2206 pci_unmap_single(dd->pdev, outbuf_dma,
2207 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002208 kfree(outbuf);
2209 kfree(inbuf);
2210
2211 return err;
2212}
2213
2214/*
2215 * Handle IOCTL calls from the Block Layer.
2216 *
2217 * This function is called by the Block Layer when it receives an IOCTL
2218 * command that it does not understand. If the IOCTL command is not supported
2219 * this function returns -ENOTTY.
2220 *
2221 * @dd Pointer to the driver data structure.
2222 * @cmd IOCTL command passed from the Block Layer.
2223 * @arg IOCTL argument passed from the Block Layer.
2224 *
2225 * return value
2226 * 0 The IOCTL completed successfully.
2227 * -ENOTTY The specified command is not supported.
2228 * -EFAULT An error occurred copying data to a user space buffer.
2229 * -EIO An error occurred while executing the command.
2230 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002231static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2232 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002233{
2234 switch (cmd) {
2235 case HDIO_GET_IDENTITY:
2236 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
2237 dev_warn(&dd->pdev->dev,
2238 "Unable to read identity\n");
2239 return -EIO;
2240 }
2241
2242 break;
2243 case HDIO_DRIVE_CMD:
2244 {
2245 u8 drive_command[4];
2246
2247 /* Copy the user command info to our buffer. */
2248 if (copy_from_user(drive_command,
2249 (void __user *) arg,
2250 sizeof(drive_command)))
2251 return -EFAULT;
2252
2253 /* Execute the drive command. */
2254 if (exec_drive_command(dd->port,
2255 drive_command,
2256 (void __user *) (arg+4)))
2257 return -EIO;
2258
2259 /* Copy the status back to the users buffer. */
2260 if (copy_to_user((void __user *) arg,
2261 drive_command,
2262 sizeof(drive_command)))
2263 return -EFAULT;
2264
2265 break;
2266 }
2267 case HDIO_DRIVE_TASK:
2268 {
2269 u8 drive_command[7];
2270
2271 /* Copy the user command info to our buffer. */
2272 if (copy_from_user(drive_command,
2273 (void __user *) arg,
2274 sizeof(drive_command)))
2275 return -EFAULT;
2276
2277 /* Execute the drive command. */
2278 if (exec_drive_task(dd->port, drive_command))
2279 return -EIO;
2280
2281 /* Copy the status back to the users buffer. */
2282 if (copy_to_user((void __user *) arg,
2283 drive_command,
2284 sizeof(drive_command)))
2285 return -EFAULT;
2286
2287 break;
2288 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002289 case HDIO_DRIVE_TASKFILE: {
2290 ide_task_request_t req_task;
2291 int ret, outtotal;
2292
2293 if (copy_from_user(&req_task, (void __user *) arg,
2294 sizeof(req_task)))
2295 return -EFAULT;
2296
2297 outtotal = sizeof(req_task);
2298
2299 ret = exec_drive_taskfile(dd, (void __user *) arg,
2300 &req_task, outtotal);
2301
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002302 if (copy_to_user((void __user *) arg, &req_task,
2303 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002304 return -EFAULT;
2305
2306 return ret;
2307 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002308
2309 default:
2310 return -EINVAL;
2311 }
2312 return 0;
2313}
2314
2315/*
2316 * Submit an IO to the hw
2317 *
2318 * This function is called by the block layer to issue an io
2319 * to the device. Upon completion, the callback function will
2320 * be called with the data parameter passed as the callback data.
2321 *
2322 * @dd Pointer to the driver data structure.
2323 * @start First sector to read.
2324 * @nsect Number of sectors to read.
2325 * @nents Number of entries in scatter list for the read command.
2326 * @tag The tag of this read command.
2327 * @callback Pointer to the function that should be called
2328 * when the read completes.
2329 * @data Callback data passed to the callback function
2330 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002331 * @dir Direction (read or write)
2332 *
2333 * return value
2334 * None
2335 */
Jens Axboe63166682011-09-27 21:27:43 -06002336static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2337 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002338 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002339{
2340 struct host_to_dev_fis *fis;
2341 struct mtip_port *port = dd->port;
2342 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002343 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002344
2345 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002346 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002347
2348 command->scatter_ents = nents;
2349
2350 /*
2351 * The number of retries for this command before it is
2352 * reported as a failure to the upper layers.
2353 */
2354 command->retries = MTIP_MAX_RETRIES;
2355
2356 /* Fill out fis */
2357 fis = command->command;
2358 fis->type = 0x27;
2359 fis->opts = 1 << 7;
2360 fis->command =
2361 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002362 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2363 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002364 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002365 fis->features = nsect & 0xFF;
2366 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002367 fis->sect_count = ((tag << 3) | (tag >> 5));
2368 fis->sect_cnt_ex = 0;
2369 fis->control = 0;
2370 fis->res2 = 0;
2371 fis->res3 = 0;
2372 fill_command_sg(dd, command, nents);
2373
2374 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002375 command->command_header->opts =
2376 __force_bit2int cpu_to_le32(
2377 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002378 command->command_header->byte_count = 0;
2379
2380 /*
2381 * Set the completion function and data for the command
2382 * within this layer.
2383 */
2384 command->comp_data = dd;
2385 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002386 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002387
2388 /*
2389 * Set the completion function and data for the command passed
2390 * from the upper layer.
2391 */
2392 command->async_data = data;
2393 command->async_callback = callback;
2394
2395 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002396 * To prevent this command from being issued
2397 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002399 if (unlikely(test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) ||
2400 test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002401 set_bit(tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002402 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002403 return;
2404 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405
2406 /* Issue the command to the hardware */
2407 mtip_issue_ncq_command(port, tag);
2408
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002409 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002410}
2411
2412/*
2413 * Release a command slot.
2414 *
2415 * @dd Pointer to the driver data structure.
2416 * @tag Slot tag
2417 *
2418 * return value
2419 * None
2420 */
Jens Axboe63166682011-09-27 21:27:43 -06002421static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422{
2423 release_slot(dd->port, tag);
2424}
2425
2426/*
2427 * Obtain a command slot and return its associated scatter list.
2428 *
2429 * @dd Pointer to the driver data structure.
2430 * @tag Pointer to an int that will receive the allocated command
2431 * slot tag.
2432 *
2433 * return value
2434 * Pointer to the scatter list for the allocated command slot
2435 * or NULL if no command slots are available.
2436 */
Jens Axboe63166682011-09-27 21:27:43 -06002437static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2438 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002439{
2440 /*
2441 * It is possible that, even with this semaphore, a thread
2442 * may think that no command slots are available. Therefore, we
2443 * need to make an attempt to get_slot().
2444 */
2445 down(&dd->port->cmd_slot);
2446 *tag = get_slot(dd->port);
2447
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002448 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002449 up(&dd->port->cmd_slot);
2450 return NULL;
2451 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002452 if (unlikely(*tag < 0))
2453 return NULL;
2454
2455 return dd->port->commands[*tag].sg;
2456}
2457
2458/*
2459 * Sysfs register/status dump.
2460 *
2461 * @dev Pointer to the device structure, passed by the kernrel.
2462 * @attr Pointer to the device_attribute structure passed by the kernel.
2463 * @buf Pointer to the char buffer that will receive the stats info.
2464 *
2465 * return value
2466 * The size, in bytes, of the data copied into buf.
2467 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002468static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002469 struct device_attribute *attr,
2470 char *buf)
2471{
2472 u32 group_allocated;
2473 struct driver_data *dd = dev_to_disk(dev)->private_data;
2474 int size = 0;
2475 int n;
2476
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002477 size += sprintf(&buf[size], "S ACTive:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002478
2479 for (n = 0; n < dd->slot_groups; n++)
2480 size += sprintf(&buf[size], "0x%08x\n",
2481 readl(dd->port->s_active[n]));
2482
2483 size += sprintf(&buf[size], "Command Issue:\n");
2484
2485 for (n = 0; n < dd->slot_groups; n++)
2486 size += sprintf(&buf[size], "0x%08x\n",
2487 readl(dd->port->cmd_issue[n]));
2488
2489 size += sprintf(&buf[size], "Allocated:\n");
2490
2491 for (n = 0; n < dd->slot_groups; n++) {
2492 if (sizeof(long) > sizeof(u32))
2493 group_allocated =
2494 dd->port->allocated[n/2] >> (32*(n&1));
2495 else
2496 group_allocated = dd->port->allocated[n];
2497 size += sprintf(&buf[size], "0x%08x\n",
2498 group_allocated);
2499 }
2500
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002501 size += sprintf(&buf[size], "Completed:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002502
2503 for (n = 0; n < dd->slot_groups; n++)
2504 size += sprintf(&buf[size], "0x%08x\n",
2505 readl(dd->port->completed[n]));
2506
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002507 size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002508 readl(dd->port->mmio + PORT_IRQ_STAT));
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002509 size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002510 readl(dd->mmio + HOST_IRQ_STAT));
2511
2512 return size;
2513}
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002514
2515static ssize_t mtip_hw_show_status(struct device *dev,
2516 struct device_attribute *attr,
2517 char *buf)
2518{
2519 struct driver_data *dd = dev_to_disk(dev)->private_data;
2520 int size = 0;
2521
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002522 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002523 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002524 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002525 size += sprintf(buf, "%s", "write_protect\n");
2526 else
2527 size += sprintf(buf, "%s", "online\n");
2528
2529 return size;
2530}
2531
Asai Thambi S P45038362012-04-09 08:35:38 +02002532static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002533static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002534
2535/*
2536 * Create the sysfs related attributes.
2537 *
2538 * @dd Pointer to the driver data structure.
2539 * @kobj Pointer to the kobj for the block device.
2540 *
2541 * return value
2542 * 0 Operation completed successfully.
2543 * -EINVAL Invalid parameter.
2544 */
Jens Axboe63166682011-09-27 21:27:43 -06002545static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002546{
2547 if (!kobj || !dd)
2548 return -EINVAL;
2549
2550 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2551 dev_warn(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002552 "Error creating 'registers' sysfs entry\n");
2553 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2554 dev_warn(&dd->pdev->dev,
2555 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002556 return 0;
2557}
2558
2559/*
2560 * Remove the sysfs related attributes.
2561 *
2562 * @dd Pointer to the driver data structure.
2563 * @kobj Pointer to the kobj for the block device.
2564 *
2565 * return value
2566 * 0 Operation completed successfully.
2567 * -EINVAL Invalid parameter.
2568 */
Jens Axboe63166682011-09-27 21:27:43 -06002569static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002570{
2571 if (!kobj || !dd)
2572 return -EINVAL;
2573
2574 sysfs_remove_file(kobj, &dev_attr_registers.attr);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002575 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002576
2577 return 0;
2578}
2579
2580/*
2581 * Perform any init/resume time hardware setup
2582 *
2583 * @dd Pointer to the driver data structure.
2584 *
2585 * return value
2586 * None
2587 */
2588static inline void hba_setup(struct driver_data *dd)
2589{
2590 u32 hwdata;
2591 hwdata = readl(dd->mmio + HOST_HSORG);
2592
2593 /* interrupt bug workaround: use only 1 IS bit.*/
2594 writel(hwdata |
2595 HSORG_DISABLE_SLOTGRP_INTR |
2596 HSORG_DISABLE_SLOTGRP_PXIS,
2597 dd->mmio + HOST_HSORG);
2598}
2599
2600/*
2601 * Detect the details of the product, and store anything needed
2602 * into the driver data structure. This includes product type and
2603 * version and number of slot groups.
2604 *
2605 * @dd Pointer to the driver data structure.
2606 *
2607 * return value
2608 * None
2609 */
2610static void mtip_detect_product(struct driver_data *dd)
2611{
2612 u32 hwdata;
2613 unsigned int rev, slotgroups;
2614
2615 /*
2616 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2617 * info register:
2618 * [15:8] hardware/software interface rev#
2619 * [ 3] asic-style interface
2620 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2621 */
2622 hwdata = readl(dd->mmio + HOST_HSORG);
2623
2624 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2625 dd->slot_groups = 1;
2626
2627 if (hwdata & 0x8) {
2628 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2629 rev = (hwdata & HSORG_HWREV) >> 8;
2630 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2631 dev_info(&dd->pdev->dev,
2632 "ASIC-FPGA design, HS rev 0x%x, "
2633 "%i slot groups [%i slots]\n",
2634 rev,
2635 slotgroups,
2636 slotgroups * 32);
2637
2638 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2639 dev_warn(&dd->pdev->dev,
2640 "Warning: driver only supports "
2641 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2642 slotgroups = MTIP_MAX_SLOT_GROUPS;
2643 }
2644 dd->slot_groups = slotgroups;
2645 return;
2646 }
2647
2648 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2649}
2650
2651/*
2652 * Blocking wait for FTL rebuild to complete
2653 *
2654 * @dd Pointer to the DRIVER_DATA structure.
2655 *
2656 * return value
2657 * 0 FTL rebuild completed successfully
2658 * -EFAULT FTL rebuild error/timeout/interruption
2659 */
2660static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2661{
2662 unsigned long timeout, cnt = 0, start;
2663
2664 dev_warn(&dd->pdev->dev,
2665 "FTL rebuild in progress. Polling for completion.\n");
2666
2667 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002668 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2669
2670 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002671 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002672 &dd->dd_flag)))
2673 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002674 if (mtip_check_surprise_removal(dd->pdev))
2675 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002676
Sam Bradshaw88523a62011-08-30 08:34:26 -06002677 if (mtip_get_identify(dd->port, NULL) < 0)
2678 return -EFAULT;
2679
2680 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2681 MTIP_FTL_REBUILD_MAGIC) {
2682 ssleep(1);
2683 /* Print message every 3 minutes */
2684 if (cnt++ >= 180) {
2685 dev_warn(&dd->pdev->dev,
2686 "FTL rebuild in progress (%d secs).\n",
2687 jiffies_to_msecs(jiffies - start) / 1000);
2688 cnt = 0;
2689 }
2690 } else {
2691 dev_warn(&dd->pdev->dev,
2692 "FTL rebuild complete (%d secs).\n",
2693 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002694 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002695 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002696 }
2697 ssleep(10);
2698 } while (time_before(jiffies, timeout));
2699
2700 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002701 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002702 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2703 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002704 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002705}
2706
2707/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002708 * service thread to issue queued commands
2709 *
2710 * @data Pointer to the driver data structure.
2711 *
2712 * return value
2713 * 0
2714 */
2715
2716static int mtip_service_thread(void *data)
2717{
2718 struct driver_data *dd = (struct driver_data *)data;
2719 unsigned long slot, slot_start, slot_wrap;
2720 unsigned int num_cmd_slots = dd->slot_groups * 32;
2721 struct mtip_port *port = dd->port;
2722
2723 while (1) {
2724 /*
2725 * the condition is to check neither an internal command is
2726 * is in progress nor error handling is active
2727 */
2728 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002729 !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
2730 !test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags));
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002731
2732 if (kthread_should_stop())
2733 break;
2734
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002735 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002736 &dd->dd_flag)))
2737 break;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002738 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2739 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002740 slot = 1;
2741 /* used to restrict the loop to one iteration */
2742 slot_start = num_cmd_slots;
2743 slot_wrap = 0;
2744 while (1) {
2745 slot = find_next_bit(port->cmds_to_issue,
2746 num_cmd_slots, slot);
2747 if (slot_wrap == 1) {
2748 if ((slot_start >= slot) ||
2749 (slot >= num_cmd_slots))
2750 break;
2751 }
2752 if (unlikely(slot_start == num_cmd_slots))
2753 slot_start = slot;
2754
2755 if (unlikely(slot == num_cmd_slots)) {
2756 slot = 1;
2757 slot_wrap = 1;
2758 continue;
2759 }
2760
2761 /* Issue the command to the hardware */
2762 mtip_issue_ncq_command(port, slot);
2763
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002764 clear_bit(slot, port->cmds_to_issue);
2765 }
2766
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002767 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2768 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02002769 if (!mtip_ftl_rebuild_poll(dd))
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002770 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002771 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002772 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002773 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002774 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002775
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002776 if (test_bit(MTIP_PF_SVC_THD_SHOULD_STOP_BIT, &port->flags))
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002777 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002778 }
2779 return 0;
2780}
2781
2782/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002783 * Called once for each card.
2784 *
2785 * @dd Pointer to the driver data structure.
2786 *
2787 * return value
2788 * 0 on success, else an error code.
2789 */
Jens Axboe63166682011-09-27 21:27:43 -06002790static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002791{
2792 int i;
2793 int rv;
2794 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002795 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002796 unsigned char *buf;
2797 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002798
2799 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2800
2801 mtip_detect_product(dd);
2802 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2803 rv = -EIO;
2804 goto out1;
2805 }
2806 num_command_slots = dd->slot_groups * 32;
2807
2808 hba_setup(dd);
2809
Sam Bradshaw88523a62011-08-30 08:34:26 -06002810 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2811
2812 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2813 if (!dd->port) {
2814 dev_err(&dd->pdev->dev,
2815 "Memory allocation: port structure\n");
2816 return -ENOMEM;
2817 }
2818
2819 /* Counting semaphore to track command slot usage */
2820 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2821
2822 /* Spinlock to prevent concurrent issue */
2823 spin_lock_init(&dd->port->cmd_issue_lock);
2824
2825 /* Set the port mmio base address. */
2826 dd->port->mmio = dd->mmio + PORT_OFFSET;
2827 dd->port->dd = dd;
2828
2829 /* Allocate memory for the command list. */
2830 dd->port->command_list =
2831 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002832 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002833 &dd->port->command_list_dma,
2834 GFP_KERNEL);
2835 if (!dd->port->command_list) {
2836 dev_err(&dd->pdev->dev,
2837 "Memory allocation: command list\n");
2838 rv = -ENOMEM;
2839 goto out1;
2840 }
2841
2842 /* Clear the memory we have allocated. */
2843 memset(dd->port->command_list,
2844 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002845 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002846
2847 /* Setup the addresse of the RX FIS. */
2848 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2849 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2850
2851 /* Setup the address of the command tables. */
2852 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2853 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2854
2855 /* Setup the address of the identify data. */
2856 dd->port->identify = dd->port->command_table +
2857 HW_CMD_TBL_AR_SZ;
2858 dd->port->identify_dma = dd->port->command_tbl_dma +
2859 HW_CMD_TBL_AR_SZ;
2860
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002861 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002862 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2863 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2864
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002865 /* Setup the address of the log buf - for read log command */
2866 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
2867 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
2868
2869 /* Setup the address of the smart buf - for smart read data command */
2870 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
2871 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
2872
2873
Sam Bradshaw88523a62011-08-30 08:34:26 -06002874 /* Point the command headers at the command tables. */
2875 for (i = 0; i < num_command_slots; i++) {
2876 dd->port->commands[i].command_header =
2877 dd->port->command_list +
2878 (sizeof(struct mtip_cmd_hdr) * i);
2879 dd->port->commands[i].command_header_dma =
2880 dd->port->command_list_dma +
2881 (sizeof(struct mtip_cmd_hdr) * i);
2882
2883 dd->port->commands[i].command =
2884 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2885 dd->port->commands[i].command_dma =
2886 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2887
2888 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2889 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002890 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002891 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002892 dd->port->commands[i].command_header->ctba =
2893 __force_bit2int cpu_to_le32(
2894 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002895
2896 /*
2897 * If this is not done, a bug is reported by the stock
2898 * FC11 i386. Due to the fact that it has lots of kernel
2899 * debugging enabled.
2900 */
2901 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2902
2903 /* Mark all commands as currently inactive.*/
2904 atomic_set(&dd->port->commands[i].active, 0);
2905 }
2906
2907 /* Setup the pointers to the extended s_active and CI registers. */
2908 for (i = 0; i < dd->slot_groups; i++) {
2909 dd->port->s_active[i] =
2910 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2911 dd->port->cmd_issue[i] =
2912 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2913 dd->port->completed[i] =
2914 dd->port->mmio + i*0x80 + PORT_SDBV;
2915 }
2916
Asai Thambi S P45038362012-04-09 08:35:38 +02002917 timetaken = jiffies;
2918 timeout = jiffies + msecs_to_jiffies(30000);
2919 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2920 time_before(jiffies, timeout)) {
2921 mdelay(100);
2922 }
2923 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
2924 timetaken = jiffies - timetaken;
2925 dev_warn(&dd->pdev->dev,
2926 "Surprise removal detected at %u ms\n",
2927 jiffies_to_msecs(timetaken));
2928 rv = -ENODEV;
2929 goto out2 ;
2930 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002931 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002932 timetaken = jiffies - timetaken;
2933 dev_warn(&dd->pdev->dev,
2934 "Removal detected at %u ms\n",
2935 jiffies_to_msecs(timetaken));
2936 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002937 goto out2;
2938 }
2939
Asai Thambi S P45038362012-04-09 08:35:38 +02002940 /* Conditionally reset the HBA. */
2941 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
2942 if (mtip_hba_reset(dd) < 0) {
2943 dev_err(&dd->pdev->dev,
2944 "Card did not reset within timeout\n");
2945 rv = -EIO;
2946 goto out2;
2947 }
2948 } else {
2949 /* Clear any pending interrupts on the HBA */
2950 writel(readl(dd->mmio + HOST_IRQ_STAT),
2951 dd->mmio + HOST_IRQ_STAT);
2952 }
2953
Sam Bradshaw88523a62011-08-30 08:34:26 -06002954 mtip_init_port(dd->port);
2955 mtip_start_port(dd->port);
2956
2957 /* Setup the ISR and enable interrupts. */
2958 rv = devm_request_irq(&dd->pdev->dev,
2959 dd->pdev->irq,
2960 mtip_irq_handler,
2961 IRQF_SHARED,
2962 dev_driver_string(&dd->pdev->dev),
2963 dd);
2964
2965 if (rv) {
2966 dev_err(&dd->pdev->dev,
2967 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2968 goto out2;
2969 }
2970
2971 /* Enable interrupts on the HBA. */
2972 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2973 dd->mmio + HOST_CTL);
2974
2975 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002976 init_waitqueue_head(&dd->port->svc_wait);
2977
Sam Bradshaw88523a62011-08-30 08:34:26 -06002978 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2979 dd->port->cmd_timer.function = mtip_timeout_function;
2980 mod_timer(&dd->port->cmd_timer,
2981 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2982
Asai Thambi S P45038362012-04-09 08:35:38 +02002983
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002984 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02002985 rv = -EFAULT;
2986 goto out3;
2987 }
2988
Sam Bradshaw88523a62011-08-30 08:34:26 -06002989 if (mtip_get_identify(dd->port, NULL) < 0) {
2990 rv = -EFAULT;
2991 goto out3;
2992 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002993
2994 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2995 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002996 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002997 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002998 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002999 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003000
3001 /* check write protect, over temp and rebuild statuses */
3002 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3003 dd->port->log_buf,
3004 dd->port->log_buf_dma, 1);
3005 if (rv) {
3006 dev_warn(&dd->pdev->dev,
3007 "Error in READ LOG EXT (10h) command\n");
3008 /* non-critical error, don't fail the load */
3009 } else {
3010 buf = (unsigned char *)dd->port->log_buf;
3011 if (buf[259] & 0x1) {
3012 dev_info(&dd->pdev->dev,
3013 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003014 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003015 }
3016 if (buf[288] == 0xF7) {
3017 dev_info(&dd->pdev->dev,
3018 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003019 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003020 }
3021 if (buf[288] == 0xBF) {
3022 dev_info(&dd->pdev->dev,
3023 "Drive indicates rebuild has failed.\n");
3024 /* TODO */
3025 }
3026 }
3027
3028 /* get write protect progess */
3029 memset(&attr242, 0, sizeof(struct smart_attr));
3030 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3031 dev_warn(&dd->pdev->dev,
3032 "Unable to check write protect progress\n");
3033 else
3034 dev_info(&dd->pdev->dev,
3035 "Write protect progress: %d%% (%d blocks)\n",
3036 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003037 return rv;
3038
3039out3:
3040 del_timer_sync(&dd->port->cmd_timer);
3041
3042 /* Disable interrupts on the HBA. */
3043 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3044 dd->mmio + HOST_CTL);
3045
3046 /*Release the IRQ. */
3047 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3048
3049out2:
3050 mtip_deinit_port(dd->port);
3051
3052 /* Free the command/command header memory. */
3053 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003054 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003055 dd->port->command_list,
3056 dd->port->command_list_dma);
3057out1:
3058 /* Free the memory allocated for the for structure. */
3059 kfree(dd->port);
3060
3061 return rv;
3062}
3063
3064/*
3065 * Called to deinitialize an interface.
3066 *
3067 * @dd Pointer to the driver data structure.
3068 *
3069 * return value
3070 * 0
3071 */
Jens Axboe63166682011-09-27 21:27:43 -06003072static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003073{
3074 /*
3075 * Send standby immediate (E0h) to the drive so that it
3076 * saves its state.
3077 */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003078 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003079
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003080 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
Asai Thambi S P45038362012-04-09 08:35:38 +02003081 if (mtip_standby_immediate(dd->port))
3082 dev_warn(&dd->pdev->dev,
3083 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003084
3085 /* de-initialize the port. */
3086 mtip_deinit_port(dd->port);
3087
3088 /* Disable interrupts on the HBA. */
3089 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3090 dd->mmio + HOST_CTL);
3091 }
3092
3093 del_timer_sync(&dd->port->cmd_timer);
3094
Sam Bradshaw88523a62011-08-30 08:34:26 -06003095 /* Release the IRQ. */
3096 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3097
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003098 /* Stop the bottom half tasklet. */
3099 tasklet_kill(&dd->tasklet);
3100
Sam Bradshaw88523a62011-08-30 08:34:26 -06003101 /* Free the command/command header memory. */
3102 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003103 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003104 dd->port->command_list,
3105 dd->port->command_list_dma);
3106 /* Free the memory allocated for the for structure. */
3107 kfree(dd->port);
3108
3109 return 0;
3110}
3111
3112/*
3113 * Issue a Standby Immediate command to the device.
3114 *
3115 * This function is called by the Block Layer just before the
3116 * system powers off during a shutdown.
3117 *
3118 * @dd Pointer to the driver data structure.
3119 *
3120 * return value
3121 * 0
3122 */
Jens Axboe63166682011-09-27 21:27:43 -06003123static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003124{
3125 /*
3126 * Send standby immediate (E0h) to the drive so that it
3127 * saves its state.
3128 */
3129 mtip_standby_immediate(dd->port);
3130
3131 return 0;
3132}
3133
3134/*
3135 * Suspend function
3136 *
3137 * This function is called by the Block Layer just before the
3138 * system hibernates.
3139 *
3140 * @dd Pointer to the driver data structure.
3141 *
3142 * return value
3143 * 0 Suspend was successful
3144 * -EFAULT Suspend was not successful
3145 */
Jens Axboe63166682011-09-27 21:27:43 -06003146static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003147{
3148 /*
3149 * Send standby immediate (E0h) to the drive
3150 * so that it saves its state.
3151 */
3152 if (mtip_standby_immediate(dd->port) != 0) {
3153 dev_err(&dd->pdev->dev,
3154 "Failed standby-immediate command\n");
3155 return -EFAULT;
3156 }
3157
3158 /* Disable interrupts on the HBA.*/
3159 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3160 dd->mmio + HOST_CTL);
3161 mtip_deinit_port(dd->port);
3162
3163 return 0;
3164}
3165
3166/*
3167 * Resume function
3168 *
3169 * This function is called by the Block Layer as the
3170 * system resumes.
3171 *
3172 * @dd Pointer to the driver data structure.
3173 *
3174 * return value
3175 * 0 Resume was successful
3176 * -EFAULT Resume was not successful
3177 */
Jens Axboe63166682011-09-27 21:27:43 -06003178static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003179{
3180 /* Perform any needed hardware setup steps */
3181 hba_setup(dd);
3182
3183 /* Reset the HBA */
3184 if (mtip_hba_reset(dd) != 0) {
3185 dev_err(&dd->pdev->dev,
3186 "Unable to reset the HBA\n");
3187 return -EFAULT;
3188 }
3189
3190 /*
3191 * Enable the port, DMA engine, and FIS reception specific
3192 * h/w in controller.
3193 */
3194 mtip_init_port(dd->port);
3195 mtip_start_port(dd->port);
3196
3197 /* Enable interrupts on the HBA.*/
3198 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3199 dd->mmio + HOST_CTL);
3200
3201 return 0;
3202}
3203
3204/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003205 * Helper function for reusing disk name
3206 * upon hot insertion.
3207 */
3208static int rssd_disk_name_format(char *prefix,
3209 int index,
3210 char *buf,
3211 int buflen)
3212{
3213 const int base = 'z' - 'a' + 1;
3214 char *begin = buf + strlen(prefix);
3215 char *end = buf + buflen;
3216 char *p;
3217 int unit;
3218
3219 p = end - 1;
3220 *p = '\0';
3221 unit = base;
3222 do {
3223 if (p == begin)
3224 return -EINVAL;
3225 *--p = 'a' + (index % unit);
3226 index = (index / unit) - 1;
3227 } while (index >= 0);
3228
3229 memmove(begin, p, end - p);
3230 memcpy(buf, prefix, strlen(prefix));
3231
3232 return 0;
3233}
3234
3235/*
3236 * Block layer IOCTL handler.
3237 *
3238 * @dev Pointer to the block_device structure.
3239 * @mode ignored
3240 * @cmd IOCTL command passed from the user application.
3241 * @arg Argument passed from the user application.
3242 *
3243 * return value
3244 * 0 IOCTL completed successfully.
3245 * -ENOTTY IOCTL not supported or invalid driver data
3246 * structure pointer.
3247 */
3248static int mtip_block_ioctl(struct block_device *dev,
3249 fmode_t mode,
3250 unsigned cmd,
3251 unsigned long arg)
3252{
3253 struct driver_data *dd = dev->bd_disk->private_data;
3254
3255 if (!capable(CAP_SYS_ADMIN))
3256 return -EACCES;
3257
3258 if (!dd)
3259 return -ENOTTY;
3260
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003261 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003262 return -ENOTTY;
3263
Sam Bradshaw88523a62011-08-30 08:34:26 -06003264 switch (cmd) {
3265 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003266 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003267 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003268 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003269 }
3270}
3271
Jens Axboe16d02c02011-09-27 15:50:01 -06003272#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003273/*
3274 * Block layer compat IOCTL handler.
3275 *
3276 * @dev Pointer to the block_device structure.
3277 * @mode ignored
3278 * @cmd IOCTL command passed from the user application.
3279 * @arg Argument passed from the user application.
3280 *
3281 * return value
3282 * 0 IOCTL completed successfully.
3283 * -ENOTTY IOCTL not supported or invalid driver data
3284 * structure pointer.
3285 */
3286static int mtip_block_compat_ioctl(struct block_device *dev,
3287 fmode_t mode,
3288 unsigned cmd,
3289 unsigned long arg)
3290{
3291 struct driver_data *dd = dev->bd_disk->private_data;
3292
3293 if (!capable(CAP_SYS_ADMIN))
3294 return -EACCES;
3295
3296 if (!dd)
3297 return -ENOTTY;
3298
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003299 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003300 return -ENOTTY;
3301
Sam Bradshaw88523a62011-08-30 08:34:26 -06003302 switch (cmd) {
3303 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003304 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003305 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003306 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003307 ide_task_request_t req_task;
3308 int compat_tasksize, outtotal, ret;
3309
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003310 compat_tasksize =
3311 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003312
3313 compat_req_task =
3314 (struct mtip_compat_ide_task_request_s __user *) arg;
3315
3316 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003317 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003318 return -EFAULT;
3319
3320 if (get_user(req_task.out_size, &compat_req_task->out_size))
3321 return -EFAULT;
3322
3323 if (get_user(req_task.in_size, &compat_req_task->in_size))
3324 return -EFAULT;
3325
3326 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3327
3328 ret = exec_drive_taskfile(dd, (void __user *) arg,
3329 &req_task, outtotal);
3330
3331 if (copy_to_user((void __user *) arg, &req_task,
3332 compat_tasksize -
3333 (2 * sizeof(compat_long_t))))
3334 return -EFAULT;
3335
3336 if (put_user(req_task.out_size, &compat_req_task->out_size))
3337 return -EFAULT;
3338
3339 if (put_user(req_task.in_size, &compat_req_task->in_size))
3340 return -EFAULT;
3341
3342 return ret;
3343 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003344 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003345 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003346 }
3347}
Jens Axboe16d02c02011-09-27 15:50:01 -06003348#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003349
3350/*
3351 * Obtain the geometry of the device.
3352 *
3353 * You may think that this function is obsolete, but some applications,
3354 * fdisk for example still used CHS values. This function describes the
3355 * device as having 224 heads and 56 sectors per cylinder. These values are
3356 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3357 * partition is described in terms of a start and end cylinder this means
3358 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3359 * affects performance.
3360 *
3361 * @dev Pointer to the block_device strucutre.
3362 * @geo Pointer to a hd_geometry structure.
3363 *
3364 * return value
3365 * 0 Operation completed successfully.
3366 * -ENOTTY An error occurred while reading the drive capacity.
3367 */
3368static int mtip_block_getgeo(struct block_device *dev,
3369 struct hd_geometry *geo)
3370{
3371 struct driver_data *dd = dev->bd_disk->private_data;
3372 sector_t capacity;
3373
3374 if (!dd)
3375 return -ENOTTY;
3376
3377 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3378 dev_warn(&dd->pdev->dev,
3379 "Could not get drive capacity.\n");
3380 return -ENOTTY;
3381 }
3382
3383 geo->heads = 224;
3384 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003385 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003386 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003387 return 0;
3388}
3389
3390/*
3391 * Block device operation function.
3392 *
3393 * This structure contains pointers to the functions required by the block
3394 * layer.
3395 */
3396static const struct block_device_operations mtip_block_ops = {
3397 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003398#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003399 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003400#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003401 .getgeo = mtip_block_getgeo,
3402 .owner = THIS_MODULE
3403};
3404
3405/*
3406 * Block layer make request function.
3407 *
3408 * This function is called by the kernel to process a BIO for
3409 * the P320 device.
3410 *
3411 * @queue Pointer to the request queue. Unused other than to obtain
3412 * the driver data structure.
3413 * @bio Pointer to the BIO.
3414 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003415 */
Jens Axboea71f4832011-11-05 08:36:21 +01003416static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003417{
3418 struct driver_data *dd = queue->queuedata;
3419 struct scatterlist *sg;
3420 struct bio_vec *bvec;
3421 int nents = 0;
3422 int tag = 0;
3423
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003424 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003425 bio_endio(bio, -ENXIO);
3426 return;
3427 }
3428
Sam Bradshaw88523a62011-08-30 08:34:26 -06003429 if (unlikely(!bio_has_data(bio))) {
3430 blk_queue_flush(queue, 0);
3431 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003432 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003433 }
3434
Sam Bradshaw88523a62011-08-30 08:34:26 -06003435 sg = mtip_hw_get_scatterlist(dd, &tag);
3436 if (likely(sg != NULL)) {
3437 blk_queue_bounce(queue, &bio);
3438
3439 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3440 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003441 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003442 bio_io_error(bio);
3443 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003444 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003445 }
3446
3447 /* Create the scatter list for this bio. */
3448 bio_for_each_segment(bvec, bio, nents) {
3449 sg_set_page(&sg[nents],
3450 bvec->bv_page,
3451 bvec->bv_len,
3452 bvec->bv_offset);
3453 }
3454
3455 /* Issue the read/write. */
3456 mtip_hw_submit_io(dd,
3457 bio->bi_sector,
3458 bio_sectors(bio),
3459 nents,
3460 tag,
3461 bio_endio,
3462 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003463 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003464 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003465 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003466}
3467
3468/*
3469 * Block layer initialization function.
3470 *
3471 * This function is called once by the PCI layer for each P320
3472 * device that is connected to the system.
3473 *
3474 * @dd Pointer to the driver data structure.
3475 *
3476 * return value
3477 * 0 on success else an error code.
3478 */
Jens Axboe63166682011-09-27 21:27:43 -06003479static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003480{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003481 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003482 sector_t capacity;
3483 unsigned int index = 0;
3484 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003485 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003486
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003487 if (dd->disk)
3488 goto skip_create_disk; /* hw init done, before rebuild */
3489
Sam Bradshaw88523a62011-08-30 08:34:26 -06003490 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003491 wait_for_rebuild = mtip_hw_init(dd);
3492 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003493 dev_err(&dd->pdev->dev,
3494 "Protocol layer initialization failed\n");
3495 rv = -EINVAL;
3496 goto protocol_init_error;
3497 }
3498
Sam Bradshaw88523a62011-08-30 08:34:26 -06003499 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3500 if (dd->disk == NULL) {
3501 dev_err(&dd->pdev->dev,
3502 "Unable to allocate gendisk structure\n");
3503 rv = -EINVAL;
3504 goto alloc_disk_error;
3505 }
3506
3507 /* Generate the disk name, implemented same as in sd.c */
3508 do {
3509 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3510 goto ida_get_error;
3511
3512 spin_lock(&rssd_index_lock);
3513 rv = ida_get_new(&rssd_index_ida, &index);
3514 spin_unlock(&rssd_index_lock);
3515 } while (rv == -EAGAIN);
3516
3517 if (rv)
3518 goto ida_get_error;
3519
3520 rv = rssd_disk_name_format("rssd",
3521 index,
3522 dd->disk->disk_name,
3523 DISK_NAME_LEN);
3524 if (rv)
3525 goto disk_index_error;
3526
3527 dd->disk->driverfs_dev = &dd->pdev->dev;
3528 dd->disk->major = dd->major;
3529 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3530 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003531 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003532 dd->index = index;
3533
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003534 /*
3535 * if rebuild pending, start the service thread, and delay the block
3536 * queue creation and add_disk()
3537 */
3538 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3539 goto start_service_thread;
3540
3541skip_create_disk:
3542 /* Allocate the request queue. */
3543 dd->queue = blk_alloc_queue(GFP_KERNEL);
3544 if (dd->queue == NULL) {
3545 dev_err(&dd->pdev->dev,
3546 "Unable to allocate request queue\n");
3547 rv = -ENOMEM;
3548 goto block_queue_alloc_init_error;
3549 }
3550
3551 /* Attach our request function to the request queue. */
3552 blk_queue_make_request(dd->queue, mtip_make_request);
3553
3554 dd->disk->queue = dd->queue;
3555 dd->queue->queuedata = dd;
3556
3557 /* Set device limits. */
3558 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3559 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3560 blk_queue_physical_block_size(dd->queue, 4096);
3561 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003562 /*
3563 * write back cache is not supported in the device. FUA depends on
3564 * write back cache support, hence setting flush support to zero.
3565 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003566 blk_queue_flush(dd->queue, 0);
3567
Sam Bradshaw88523a62011-08-30 08:34:26 -06003568 /* Set the capacity of the device in 512 byte sectors. */
3569 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3570 dev_warn(&dd->pdev->dev,
3571 "Could not read drive capacity\n");
3572 rv = -EIO;
3573 goto read_capacity_error;
3574 }
3575 set_capacity(dd->disk, capacity);
3576
3577 /* Enable the block device and add it to /dev */
3578 add_disk(dd->disk);
3579
3580 /*
3581 * Now that the disk is active, initialize any sysfs attributes
3582 * managed by the protocol layer.
3583 */
3584 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3585 if (kobj) {
3586 mtip_hw_sysfs_init(dd, kobj);
3587 kobject_put(kobj);
3588 }
3589
Asai Thambi S P45038362012-04-09 08:35:38 +02003590 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003591 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003592 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003593 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003594
3595start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003596 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3597
3598 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3599 dd, thd_name);
3600
3601 if (IS_ERR(dd->mtip_svc_handler)) {
3602 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3603 dd->mtip_svc_handler = NULL;
3604 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003605 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003606 }
3607
Asai Thambi S P45038362012-04-09 08:35:38 +02003608 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3609 rv = wait_for_rebuild;
3610
Sam Bradshaw88523a62011-08-30 08:34:26 -06003611 return rv;
3612
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003613kthread_run_error:
3614 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003615 del_gendisk(dd->disk);
3616
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003617read_capacity_error:
3618 blk_cleanup_queue(dd->queue);
3619
3620block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003621disk_index_error:
3622 spin_lock(&rssd_index_lock);
3623 ida_remove(&rssd_index_ida, index);
3624 spin_unlock(&rssd_index_lock);
3625
3626ida_get_error:
3627 put_disk(dd->disk);
3628
3629alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003630 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003631
3632protocol_init_error:
3633 return rv;
3634}
3635
3636/*
3637 * Block layer deinitialization function.
3638 *
3639 * Called by the PCI layer as each P320 device is removed.
3640 *
3641 * @dd Pointer to the driver data structure.
3642 *
3643 * return value
3644 * 0
3645 */
Jens Axboe63166682011-09-27 21:27:43 -06003646static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003647{
3648 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003649
3650 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003651 set_bit(MTIP_PF_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003652 wake_up_interruptible(&dd->port->svc_wait);
3653 kthread_stop(dd->mtip_svc_handler);
3654 }
3655
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003656 /* Clean up the sysfs attributes, if created */
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003657 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003658 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3659 if (kobj) {
3660 mtip_hw_sysfs_exit(dd, kobj);
3661 kobject_put(kobj);
3662 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003663 }
3664
3665 /*
3666 * Delete our gendisk structure. This also removes the device
3667 * from /dev
3668 */
3669 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003670
3671 spin_lock(&rssd_index_lock);
3672 ida_remove(&rssd_index_ida, dd->index);
3673 spin_unlock(&rssd_index_lock);
3674
Sam Bradshaw88523a62011-08-30 08:34:26 -06003675 blk_cleanup_queue(dd->queue);
3676 dd->disk = NULL;
3677 dd->queue = NULL;
3678
3679 /* De-initialize the protocol layer. */
3680 mtip_hw_exit(dd);
3681
3682 return 0;
3683}
3684
3685/*
3686 * Function called by the PCI layer when just before the
3687 * machine shuts down.
3688 *
3689 * If a protocol layer shutdown function is present it will be called
3690 * by this function.
3691 *
3692 * @dd Pointer to the driver data structure.
3693 *
3694 * return value
3695 * 0
3696 */
Jens Axboe63166682011-09-27 21:27:43 -06003697static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003698{
3699 dev_info(&dd->pdev->dev,
3700 "Shutting down %s ...\n", dd->disk->disk_name);
3701
3702 /* Delete our gendisk structure, and cleanup the blk queue. */
3703 del_gendisk(dd->disk);
Asai Thambi S P8182b492012-04-09 08:35:38 +02003704
3705 spin_lock(&rssd_index_lock);
3706 ida_remove(&rssd_index_ida, dd->index);
3707 spin_unlock(&rssd_index_lock);
3708
Sam Bradshaw88523a62011-08-30 08:34:26 -06003709 blk_cleanup_queue(dd->queue);
3710 dd->disk = NULL;
3711 dd->queue = NULL;
3712
3713 mtip_hw_shutdown(dd);
3714 return 0;
3715}
3716
Jens Axboe63166682011-09-27 21:27:43 -06003717static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003718{
3719 dev_info(&dd->pdev->dev,
3720 "Suspending %s ...\n", dd->disk->disk_name);
3721 mtip_hw_suspend(dd);
3722 return 0;
3723}
3724
Jens Axboe63166682011-09-27 21:27:43 -06003725static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003726{
3727 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3728 dd->disk->disk_name);
3729 mtip_hw_resume(dd);
3730 return 0;
3731}
3732
3733/*
3734 * Called for each supported PCI device detected.
3735 *
3736 * This function allocates the private data structure, enables the
3737 * PCI device and then calls the block layer initialization function.
3738 *
3739 * return value
3740 * 0 on success else an error code.
3741 */
3742static int mtip_pci_probe(struct pci_dev *pdev,
3743 const struct pci_device_id *ent)
3744{
3745 int rv = 0;
3746 struct driver_data *dd = NULL;
3747
3748 /* Allocate memory for this devices private data. */
3749 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3750 if (dd == NULL) {
3751 dev_err(&pdev->dev,
3752 "Unable to allocate memory for driver data\n");
3753 return -ENOMEM;
3754 }
3755
Sam Bradshaw88523a62011-08-30 08:34:26 -06003756 /* Attach the private data to this PCI device. */
3757 pci_set_drvdata(pdev, dd);
3758
3759 rv = pcim_enable_device(pdev);
3760 if (rv < 0) {
3761 dev_err(&pdev->dev, "Unable to enable device\n");
3762 goto iomap_err;
3763 }
3764
3765 /* Map BAR5 to memory. */
3766 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3767 if (rv < 0) {
3768 dev_err(&pdev->dev, "Unable to map regions\n");
3769 goto iomap_err;
3770 }
3771
3772 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3773 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3774
3775 if (rv) {
3776 rv = pci_set_consistent_dma_mask(pdev,
3777 DMA_BIT_MASK(32));
3778 if (rv) {
3779 dev_warn(&pdev->dev,
3780 "64-bit DMA enable failed\n");
3781 goto setmask_err;
3782 }
3783 }
3784 }
3785
3786 pci_set_master(pdev);
3787
3788 if (pci_enable_msi(pdev)) {
3789 dev_warn(&pdev->dev,
3790 "Unable to enable MSI interrupt.\n");
3791 goto block_initialize_err;
3792 }
3793
3794 /* Copy the info we may need later into the private data structure. */
3795 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003796 dd->instance = instance;
3797 dd->pdev = pdev;
3798
3799 /* Initialize the block layer. */
3800 rv = mtip_block_initialize(dd);
3801 if (rv < 0) {
3802 dev_err(&pdev->dev,
3803 "Unable to initialize block layer\n");
3804 goto block_initialize_err;
3805 }
3806
3807 /*
3808 * Increment the instance count so that each device has a unique
3809 * instance number.
3810 */
3811 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003812 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003813 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003814 goto done;
3815
3816block_initialize_err:
3817 pci_disable_msi(pdev);
3818
3819setmask_err:
3820 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3821
3822iomap_err:
3823 kfree(dd);
3824 pci_set_drvdata(pdev, NULL);
3825 return rv;
3826done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003827 return rv;
3828}
3829
3830/*
3831 * Called for each probed device when the device is removed or the
3832 * driver is unloaded.
3833 *
3834 * return value
3835 * None
3836 */
3837static void mtip_pci_remove(struct pci_dev *pdev)
3838{
3839 struct driver_data *dd = pci_get_drvdata(pdev);
3840 int counter = 0;
3841
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003842 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02003843
Sam Bradshaw88523a62011-08-30 08:34:26 -06003844 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003845 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003846 counter++;
3847 msleep(20);
3848 if (counter == 10) {
3849 /* Cleanup the outstanding commands */
3850 mtip_command_cleanup(dd);
3851 break;
3852 }
3853 }
3854 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003855
3856 /* Clean up the block layer. */
3857 mtip_block_remove(dd);
3858
3859 pci_disable_msi(pdev);
3860
3861 kfree(dd);
3862 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3863}
3864
3865/*
3866 * Called for each probed device when the device is suspended.
3867 *
3868 * return value
3869 * 0 Success
3870 * <0 Error
3871 */
3872static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3873{
3874 int rv = 0;
3875 struct driver_data *dd = pci_get_drvdata(pdev);
3876
3877 if (!dd) {
3878 dev_err(&pdev->dev,
3879 "Driver private datastructure is NULL\n");
3880 return -EFAULT;
3881 }
3882
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003883 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003884
3885 /* Disable ports & interrupts then send standby immediate */
3886 rv = mtip_block_suspend(dd);
3887 if (rv < 0) {
3888 dev_err(&pdev->dev,
3889 "Failed to suspend controller\n");
3890 return rv;
3891 }
3892
3893 /*
3894 * Save the pci config space to pdev structure &
3895 * disable the device
3896 */
3897 pci_save_state(pdev);
3898 pci_disable_device(pdev);
3899
3900 /* Move to Low power state*/
3901 pci_set_power_state(pdev, PCI_D3hot);
3902
3903 return rv;
3904}
3905
3906/*
3907 * Called for each probed device when the device is resumed.
3908 *
3909 * return value
3910 * 0 Success
3911 * <0 Error
3912 */
3913static int mtip_pci_resume(struct pci_dev *pdev)
3914{
3915 int rv = 0;
3916 struct driver_data *dd;
3917
3918 dd = pci_get_drvdata(pdev);
3919 if (!dd) {
3920 dev_err(&pdev->dev,
3921 "Driver private datastructure is NULL\n");
3922 return -EFAULT;
3923 }
3924
3925 /* Move the device to active State */
3926 pci_set_power_state(pdev, PCI_D0);
3927
3928 /* Restore PCI configuration space */
3929 pci_restore_state(pdev);
3930
3931 /* Enable the PCI device*/
3932 rv = pcim_enable_device(pdev);
3933 if (rv < 0) {
3934 dev_err(&pdev->dev,
3935 "Failed to enable card during resume\n");
3936 goto err;
3937 }
3938 pci_set_master(pdev);
3939
3940 /*
3941 * Calls hbaReset, initPort, & startPort function
3942 * then enables interrupts
3943 */
3944 rv = mtip_block_resume(dd);
3945 if (rv < 0)
3946 dev_err(&pdev->dev, "Unable to resume\n");
3947
3948err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003949 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003950
3951 return rv;
3952}
3953
3954/*
3955 * Shutdown routine
3956 *
3957 * return value
3958 * None
3959 */
3960static void mtip_pci_shutdown(struct pci_dev *pdev)
3961{
3962 struct driver_data *dd = pci_get_drvdata(pdev);
3963 if (dd)
3964 mtip_block_shutdown(dd);
3965}
3966
Sam Bradshaw88523a62011-08-30 08:34:26 -06003967/* Table of device ids supported by this driver. */
3968static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3969 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3970 { 0 }
3971};
3972
3973/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003974static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003975 .name = MTIP_DRV_NAME,
3976 .id_table = mtip_pci_tbl,
3977 .probe = mtip_pci_probe,
3978 .remove = mtip_pci_remove,
3979 .suspend = mtip_pci_suspend,
3980 .resume = mtip_pci_resume,
3981 .shutdown = mtip_pci_shutdown,
3982};
3983
3984MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3985
3986/*
3987 * Module initialization function.
3988 *
3989 * Called once when the module is loaded. This function allocates a major
3990 * block device number to the Cyclone devices and registers the PCI layer
3991 * of the driver.
3992 *
3993 * Return value
3994 * 0 on success else error code.
3995 */
3996static int __init mtip_init(void)
3997{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003998 int error;
3999
Sam Bradshaw88523a62011-08-30 08:34:26 -06004000 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4001
4002 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004003 error = register_blkdev(0, MTIP_DRV_NAME);
4004 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004005 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004006 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004007 return -EBUSY;
4008 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004009 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004010
4011 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004012 error = pci_register_driver(&mtip_pci_driver);
4013 if (error)
4014 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4015
4016 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004017}
4018
4019/*
4020 * Module de-initialization function.
4021 *
4022 * Called once when the module is unloaded. This function deallocates
4023 * the major block device number allocated by mtip_init() and
4024 * unregisters the PCI layer of the driver.
4025 *
4026 * Return value
4027 * none
4028 */
4029static void __exit mtip_exit(void)
4030{
4031 /* Release the allocated major block device number. */
4032 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4033
4034 /* Unregister the PCI driver. */
4035 pci_unregister_driver(&mtip_pci_driver);
4036}
4037
4038MODULE_AUTHOR("Micron Technology, Inc");
4039MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4040MODULE_LICENSE("GPL");
4041MODULE_VERSION(MTIP_DRV_VERSION);
4042
4043module_init(mtip_init);
4044module_exit(mtip_exit);