blob: aaa82453ae741d96e7f91369be5442e99b5e6cee [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
Jens Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
34#include <linux/bio.h>
35#include <linux/dma-mapping.h>
36#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010037#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060038#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020039#include <linux/export.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060040#include "mtip32xx.h"
41
42#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
43#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
44#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
45#define HW_PORT_PRIV_DMA_SZ \
46 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
47
Asai Thambi S P45038362012-04-09 08:35:38 +020048#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060049#define HOST_HSORG 0xFC
50#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
51#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
52#define HSORG_HWREV 0xFF00
53#define HSORG_STYLE 0x8
54#define HSORG_SLOTGROUPS 0x7
55
56#define PORT_COMMAND_ISSUE 0x38
57#define PORT_SDBV 0x7C
58
59#define PORT_OFFSET 0x100
60#define PORT_MEM_SIZE 0x80
61
62#define PORT_IRQ_ERR \
63 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
64 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
65 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
66 PORT_IRQ_OVERFLOW)
67#define PORT_IRQ_LEGACY \
68 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
69#define PORT_IRQ_HANDLED \
70 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
71 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
72 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
73#define DEF_PORT_IRQ \
74 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
75
76/* product numbers */
77#define MTIP_PRODUCT_UNKNOWN 0x00
78#define MTIP_PRODUCT_ASICFPGA 0x11
79
80/* Device instance number, incremented each time a device is probed. */
81static int instance;
82
83/*
84 * Global variable used to hold the major block device number
85 * allocated in mtip_init().
86 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060087static int mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -060088
89static DEFINE_SPINLOCK(rssd_index_lock);
90static DEFINE_IDA(rssd_index_ida);
91
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010092static int mtip_block_initialize(struct driver_data *dd);
93
Jens Axboe16d02c02011-09-27 15:50:01 -060094#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060095struct mtip_compat_ide_task_request_s {
96 __u8 io_ports[8];
97 __u8 hob_ports[8];
98 ide_reg_valid_t out_flags;
99 ide_reg_valid_t in_flags;
100 int data_phase;
101 int req_cmd;
102 compat_ulong_t out_size;
103 compat_ulong_t in_size;
104};
Jens Axboe16d02c02011-09-27 15:50:01 -0600105#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600106
Sam Bradshaw88523a62011-08-30 08:34:26 -0600107/*
Jens Axboe63166682011-09-27 21:27:43 -0600108 * This function check_for_surprise_removal is called
109 * while card is removed from the system and it will
110 * read the vendor id from the configration space
111 *
112 * @pdev Pointer to the pci_dev structure.
113 *
114 * return value
115 * true if device removed, else false
116 */
117static bool mtip_check_surprise_removal(struct pci_dev *pdev)
118{
119 u16 vendor_id = 0;
120
121 /* Read the vendorID from the configuration space */
122 pci_read_config_word(pdev, 0x00, &vendor_id);
123 if (vendor_id == 0xFFFF)
124 return true; /* device removed */
125
126 return false; /* device present */
127}
128
129/*
130 * This function is called for clean the pending command in the
131 * command slot during the surprise removal of device and return
132 * error to the upper layer.
133 *
134 * @dd Pointer to the DRIVER_DATA structure.
135 *
136 * return value
137 * None
138 */
139static void mtip_command_cleanup(struct driver_data *dd)
140{
141 int group = 0, commandslot = 0, commandindex = 0;
142 struct mtip_cmd *command;
143 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200144 static int in_progress;
145
146 if (in_progress)
147 return;
148
149 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600150
151 for (group = 0; group < 4; group++) {
152 for (commandslot = 0; commandslot < 32; commandslot++) {
153 if (!(port->allocated[group] & (1 << commandslot)))
154 continue;
155
156 commandindex = group << 5 | commandslot;
157 command = &port->commands[commandindex];
158
159 if (atomic_read(&command->active)
160 && (command->async_callback)) {
161 command->async_callback(command->async_data,
162 -ENODEV);
163 command->async_callback = NULL;
164 command->async_data = NULL;
165 }
166
167 dma_unmap_sg(&port->dd->pdev->dev,
168 command->sg,
169 command->scatter_ents,
170 command->direction);
171 }
172 }
173
174 up(&port->cmd_slot);
175
Asai Thambi S P45038362012-04-09 08:35:38 +0200176 set_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag);
177 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600178}
179
180/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600181 * Obtain an empty command slot.
182 *
183 * This function needs to be reentrant since it could be called
184 * at the same time on multiple CPUs. The allocation of the
185 * command slot must be atomic.
186 *
187 * @port Pointer to the port data structure.
188 *
189 * return value
190 * >= 0 Index of command slot obtained.
191 * -1 No command slots available.
192 */
193static int get_slot(struct mtip_port *port)
194{
195 int slot, i;
196 unsigned int num_command_slots = port->dd->slot_groups * 32;
197
198 /*
199 * Try 10 times, because there is a small race here.
200 * that's ok, because it's still cheaper than a lock.
201 *
202 * Race: Since this section is not protected by lock, same bit
203 * could be chosen by different process contexts running in
204 * different processor. So instead of costly lock, we are going
205 * with loop.
206 */
207 for (i = 0; i < 10; i++) {
208 slot = find_next_zero_bit(port->allocated,
209 num_command_slots, 1);
210 if ((slot < num_command_slots) &&
211 (!test_and_set_bit(slot, port->allocated)))
212 return slot;
213 }
214 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
215
216 if (mtip_check_surprise_removal(port->dd->pdev)) {
217 /* Device not present, clean outstanding commands */
218 mtip_command_cleanup(port->dd);
219 }
220 return -1;
221}
222
223/*
224 * Release a command slot.
225 *
226 * @port Pointer to the port data structure.
227 * @tag Tag of command to release
228 *
229 * return value
230 * None
231 */
232static inline void release_slot(struct mtip_port *port, int tag)
233{
234 smp_mb__before_clear_bit();
235 clear_bit(tag, port->allocated);
236 smp_mb__after_clear_bit();
237}
238
239/*
Jens Axboe63166682011-09-27 21:27:43 -0600240 * Reset the HBA (without sleeping)
241 *
242 * Just like hba_reset, except does not call sleep, so can be
243 * run from interrupt/tasklet context.
244 *
245 * @dd Pointer to the driver data structure.
246 *
247 * return value
248 * 0 The reset was successful.
249 * -1 The HBA Reset bit did not clear.
250 */
251static int hba_reset_nosleep(struct driver_data *dd)
252{
253 unsigned long timeout;
254
255 /* Chip quirk: quiesce any chip function */
256 mdelay(10);
257
258 /* Set the reset bit */
259 writel(HOST_RESET, dd->mmio + HOST_CTL);
260
261 /* Flush */
262 readl(dd->mmio + HOST_CTL);
263
264 /*
265 * Wait 10ms then spin for up to 1 second
266 * waiting for reset acknowledgement
267 */
268 timeout = jiffies + msecs_to_jiffies(1000);
269 mdelay(10);
270 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
271 && time_before(jiffies, timeout))
272 mdelay(1);
273
Asai Thambi S P45038362012-04-09 08:35:38 +0200274 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))
275 return -1;
276
Jens Axboe63166682011-09-27 21:27:43 -0600277 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
278 return -1;
279
280 return 0;
281}
282
283/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600284 * Issue a command to the hardware.
285 *
286 * Set the appropriate bit in the s_active and Command Issue hardware
287 * registers, causing hardware command processing to begin.
288 *
289 * @port Pointer to the port structure.
290 * @tag The tag of the command to be issued.
291 *
292 * return value
293 * None
294 */
295static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
296{
297 unsigned long flags = 0;
298
299 atomic_set(&port->commands[tag].active, 1);
300
301 spin_lock_irqsave(&port->cmd_issue_lock, flags);
302
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
307
308 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200309
310 /* Set the command's timeout value.*/
311 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
312 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600313}
314
315/*
Jens Axboe63166682011-09-27 21:27:43 -0600316 * Enable/disable the reception of FIS
317 *
318 * @port Pointer to the port data structure
319 * @enable 1 to enable, 0 to disable
320 *
321 * return value
322 * Previous state: 1 enabled, 0 disabled
323 */
324static int mtip_enable_fis(struct mtip_port *port, int enable)
325{
326 u32 tmp;
327
328 /* enable FIS reception */
329 tmp = readl(port->mmio + PORT_CMD);
330 if (enable)
331 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332 else
333 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
334
335 /* Flush */
336 readl(port->mmio + PORT_CMD);
337
338 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
339}
340
341/*
342 * Enable/disable the DMA engine
343 *
344 * @port Pointer to the port data structure
345 * @enable 1 to enable, 0 to disable
346 *
347 * return value
348 * Previous state: 1 enabled, 0 disabled.
349 */
350static int mtip_enable_engine(struct mtip_port *port, int enable)
351{
352 u32 tmp;
353
354 /* enable FIS reception */
355 tmp = readl(port->mmio + PORT_CMD);
356 if (enable)
357 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
358 else
359 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
360
361 readl(port->mmio + PORT_CMD);
362 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
363}
364
365/*
366 * Enables the port DMA engine and FIS reception.
367 *
368 * return value
369 * None
370 */
371static inline void mtip_start_port(struct mtip_port *port)
372{
373 /* Enable FIS reception */
374 mtip_enable_fis(port, 1);
375
376 /* Enable the DMA engine */
377 mtip_enable_engine(port, 1);
378}
379
380/*
381 * Deinitialize a port by disabling port interrupts, the DMA engine,
382 * and FIS reception.
383 *
384 * @port Pointer to the port structure
385 *
386 * return value
387 * None
388 */
389static inline void mtip_deinit_port(struct mtip_port *port)
390{
391 /* Disable interrupts on this port */
392 writel(0, port->mmio + PORT_IRQ_MASK);
393
394 /* Disable the DMA engine */
395 mtip_enable_engine(port, 0);
396
397 /* Disable FIS reception */
398 mtip_enable_fis(port, 0);
399}
400
401/*
402 * Initialize a port.
403 *
404 * This function deinitializes the port by calling mtip_deinit_port() and
405 * then initializes it by setting the command header and RX FIS addresses,
406 * clearing the SError register and any pending port interrupts before
407 * re-enabling the default set of port interrupts.
408 *
409 * @port Pointer to the port structure.
410 *
411 * return value
412 * None
413 */
414static void mtip_init_port(struct mtip_port *port)
415{
416 int i;
417 mtip_deinit_port(port);
418
419 /* Program the command list base and FIS base addresses */
420 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
421 writel((port->command_list_dma >> 16) >> 16,
422 port->mmio + PORT_LST_ADDR_HI);
423 writel((port->rxfis_dma >> 16) >> 16,
424 port->mmio + PORT_FIS_ADDR_HI);
425 }
426
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600428 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100429 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600430
431 /* Clear SError */
432 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
433
434 /* reset the completed registers.*/
435 for (i = 0; i < port->dd->slot_groups; i++)
436 writel(0xFFFFFFFF, port->completed[i]);
437
438 /* Clear any pending interrupts for this port */
439 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
440
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100441 /* Clear any pending interrupts on the HBA. */
442 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
443 port->dd->mmio + HOST_IRQ_STAT);
444
Jens Axboe63166682011-09-27 21:27:43 -0600445 /* Enable port interrupts */
446 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
447}
448
449/*
450 * Restart a port
451 *
452 * @port Pointer to the port data structure.
453 *
454 * return value
455 * None
456 */
457static void mtip_restart_port(struct mtip_port *port)
458{
459 unsigned long timeout;
460
461 /* Disable the DMA engine */
462 mtip_enable_engine(port, 0);
463
464 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
465 timeout = jiffies + msecs_to_jiffies(500);
466 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
467 && time_before(jiffies, timeout))
468 ;
469
Asai Thambi S P45038362012-04-09 08:35:38 +0200470 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
471 return;
472
Jens Axboe63166682011-09-27 21:27:43 -0600473 /*
474 * Chip quirk: escalate to hba reset if
475 * PxCMD.CR not clear after 500 ms
476 */
477 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
478 dev_warn(&port->dd->pdev->dev,
479 "PxCMD.CR not clear, escalating reset\n");
480
481 if (hba_reset_nosleep(port->dd))
482 dev_err(&port->dd->pdev->dev,
483 "HBA reset escalation failed.\n");
484
485 /* 30 ms delay before com reset to quiesce chip */
486 mdelay(30);
487 }
488
489 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
490
491 /* Set PxSCTL.DET */
492 writel(readl(port->mmio + PORT_SCR_CTL) |
493 1, port->mmio + PORT_SCR_CTL);
494 readl(port->mmio + PORT_SCR_CTL);
495
496 /* Wait 1 ms to quiesce chip function */
497 timeout = jiffies + msecs_to_jiffies(1);
498 while (time_before(jiffies, timeout))
499 ;
500
Asai Thambi S P45038362012-04-09 08:35:38 +0200501 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
502 return;
503
Jens Axboe63166682011-09-27 21:27:43 -0600504 /* Clear PxSCTL.DET */
505 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
506 port->mmio + PORT_SCR_CTL);
507 readl(port->mmio + PORT_SCR_CTL);
508
509 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
510 timeout = jiffies + msecs_to_jiffies(500);
511 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
512 && time_before(jiffies, timeout))
513 ;
514
Asai Thambi S P45038362012-04-09 08:35:38 +0200515 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
516 return;
517
Jens Axboe63166682011-09-27 21:27:43 -0600518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
521
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100522 mtip_init_port(port);
523 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600524
Jens Axboe63166682011-09-27 21:27:43 -0600525}
526
527/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600528 * Called periodically to see if any read/write commands are
529 * taking too long to complete.
530 *
531 * @data Pointer to the PORT data structure.
532 *
533 * return value
534 * None
535 */
Jens Axboe63166682011-09-27 21:27:43 -0600536static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600537{
538 struct mtip_port *port = (struct mtip_port *) data;
539 struct host_to_dev_fis *fis;
540 struct mtip_cmd *command;
541 int tag, cmdto_cnt = 0;
542 unsigned int bit, group;
543 unsigned int num_command_slots = port->dd->slot_groups * 32;
544
545 if (unlikely(!port))
546 return;
547
Asai Thambi S P45038362012-04-09 08:35:38 +0200548 if (test_bit(MTIP_DD_FLAG_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600549 mod_timer(&port->cmd_timer,
550 jiffies + msecs_to_jiffies(30000));
551 return;
552 }
553
554 for (tag = 0; tag < num_command_slots; tag++) {
555 /*
556 * Skip internal command slot as it has
557 * its own timeout mechanism
558 */
559 if (tag == MTIP_TAG_INTERNAL)
560 continue;
561
562 if (atomic_read(&port->commands[tag].active) &&
563 (time_after(jiffies, port->commands[tag].comp_time))) {
564 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100565 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600566
567 command = &port->commands[tag];
568 fis = (struct host_to_dev_fis *) command->command;
569
570 dev_warn(&port->dd->pdev->dev,
571 "Timeout for command tag %d\n", tag);
572
573 cmdto_cnt++;
574 if (cmdto_cnt == 1)
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100575 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600576
577 /*
578 * Clear the completed bit. This should prevent
579 * any interrupt handlers from trying to retire
580 * the command.
581 */
582 writel(1 << bit, port->completed[group]);
583
584 /* Call the async completion callback. */
585 if (likely(command->async_callback))
586 command->async_callback(command->async_data,
587 -EIO);
588 command->async_callback = NULL;
589 command->comp_func = NULL;
590
591 /* Unmap the DMA scatter list entries */
592 dma_unmap_sg(&port->dd->pdev->dev,
593 command->sg,
594 command->scatter_ents,
595 command->direction);
596
597 /*
598 * Clear the allocated bit and active tag for the
599 * command.
600 */
601 atomic_set(&port->commands[tag].active, 0);
602 release_slot(port, tag);
603
604 up(&port->cmd_slot);
605 }
606 }
607
608 if (cmdto_cnt) {
609 dev_warn(&port->dd->pdev->dev,
610 "%d commands timed out: restarting port",
611 cmdto_cnt);
612 mtip_restart_port(port);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100613 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
614 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600615 }
616
617 /* Restart the timer */
618 mod_timer(&port->cmd_timer,
619 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
620}
621
622/*
623 * IO completion function.
624 *
625 * This completion function is called by the driver ISR when a
626 * command that was issued by the kernel completes. It first calls the
627 * asynchronous completion function which normally calls back into the block
628 * layer passing the asynchronous callback data, then unmaps the
629 * scatter list associated with the completed command, and finally
630 * clears the allocated bit associated with the completed command.
631 *
632 * @port Pointer to the port data structure.
633 * @tag Tag of the command.
634 * @data Pointer to driver_data.
635 * @status Completion status.
636 *
637 * return value
638 * None
639 */
640static void mtip_async_complete(struct mtip_port *port,
641 int tag,
642 void *data,
643 int status)
644{
645 struct mtip_cmd *command;
646 struct driver_data *dd = data;
647 int cb_status = status ? -EIO : 0;
648
649 if (unlikely(!dd) || unlikely(!port))
650 return;
651
652 command = &port->commands[tag];
653
654 if (unlikely(status == PORT_IRQ_TF_ERR)) {
655 dev_warn(&port->dd->pdev->dev,
656 "Command tag %d failed due to TFE\n", tag);
657 }
658
659 /* Upper layer callback */
660 if (likely(command->async_callback))
661 command->async_callback(command->async_data, cb_status);
662
663 command->async_callback = NULL;
664 command->comp_func = NULL;
665
666 /* Unmap the DMA scatter list entries */
667 dma_unmap_sg(&dd->pdev->dev,
668 command->sg,
669 command->scatter_ents,
670 command->direction);
671
672 /* Clear the allocated and active bits for the command */
673 atomic_set(&port->commands[tag].active, 0);
674 release_slot(port, tag);
675
676 up(&port->cmd_slot);
677}
678
679/*
680 * Internal command completion callback function.
681 *
682 * This function is normally called by the driver ISR when an internal
683 * command completed. This function signals the command completion by
684 * calling complete().
685 *
686 * @port Pointer to the port data structure.
687 * @tag Tag of the command that has completed.
688 * @data Pointer to a completion structure.
689 * @status Completion status.
690 *
691 * return value
692 * None
693 */
694static void mtip_completion(struct mtip_port *port,
695 int tag,
696 void *data,
697 int status)
698{
699 struct mtip_cmd *command = &port->commands[tag];
700 struct completion *waiting = data;
701 if (unlikely(status == PORT_IRQ_TF_ERR))
702 dev_warn(&port->dd->pdev->dev,
703 "Internal command %d completed with TFE\n", tag);
704
705 command->async_callback = NULL;
706 command->comp_func = NULL;
707
708 complete(waiting);
709}
710
711/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600712 * Helper function for tag logging
713 */
714static void print_tags(struct driver_data *dd,
715 char *msg,
716 unsigned long *tagbits)
717{
718 unsigned int tag, count = 0;
719
720 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
721 if (test_bit(tag, tagbits))
722 count++;
723 }
724 if (count)
725 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
726}
727
728/*
729 * Handle an error.
730 *
731 * @dd Pointer to the DRIVER_DATA structure.
732 *
733 * return value
734 * None
735 */
736static void mtip_handle_tfe(struct driver_data *dd)
737{
738 int group, tag, bit, reissue;
739 struct mtip_port *port;
740 struct mtip_cmd *command;
741 u32 completed;
742 struct host_to_dev_fis *fis;
743 unsigned long tagaccum[SLOTBITS_IN_LONGS];
744
745 dev_warn(&dd->pdev->dev, "Taskfile error\n");
746
747 port = dd->port;
748
749 /* Stop the timer to prevent command timeouts. */
750 del_timer(&port->cmd_timer);
751
752 /* Set eh_active */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100753 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600754
755 /* Loop through all the groups */
756 for (group = 0; group < dd->slot_groups; group++) {
757 completed = readl(port->completed[group]);
758
759 /* clear completed status register in the hardware.*/
760 writel(completed, port->completed[group]);
761
762 /* clear the tag accumulator */
763 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
764
765 /* Process successfully completed commands */
766 for (bit = 0; bit < 32 && completed; bit++) {
767 if (!(completed & (1<<bit)))
768 continue;
769 tag = (group << 5) + bit;
770
771 /* Skip the internal command slot */
772 if (tag == MTIP_TAG_INTERNAL)
773 continue;
774
775 command = &port->commands[tag];
776 if (likely(command->comp_func)) {
777 set_bit(tag, tagaccum);
778 atomic_set(&port->commands[tag].active, 0);
779 command->comp_func(port,
780 tag,
781 command->comp_data,
782 0);
783 } else {
784 dev_err(&port->dd->pdev->dev,
785 "Missing completion func for tag %d",
786 tag);
787 if (mtip_check_surprise_removal(dd->pdev)) {
788 mtip_command_cleanup(dd);
789 /* don't proceed further */
790 return;
791 }
792 }
793 }
794 }
795 print_tags(dd, "TFE tags completed:", tagaccum);
796
797 /* Restart the port */
798 mdelay(20);
799 mtip_restart_port(port);
800
801 /* clear the tag accumulator */
802 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
803
804 /* Loop through all the groups */
805 for (group = 0; group < dd->slot_groups; group++) {
806 for (bit = 0; bit < 32; bit++) {
807 reissue = 1;
808 tag = (group << 5) + bit;
809
810 /* If the active bit is set re-issue the command */
811 if (atomic_read(&port->commands[tag].active) == 0)
812 continue;
813
814 fis = (struct host_to_dev_fis *)
815 port->commands[tag].command;
816
817 /* Should re-issue? */
818 if (tag == MTIP_TAG_INTERNAL ||
819 fis->command == ATA_CMD_SET_FEATURES)
820 reissue = 0;
821
822 /*
823 * First check if this command has
824 * exceeded its retries.
825 */
826 if (reissue &&
827 (port->commands[tag].retries-- > 0)) {
828
829 set_bit(tag, tagaccum);
830
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831 /* Re-issue the command. */
832 mtip_issue_ncq_command(port, tag);
833
834 continue;
835 }
836
837 /* Retire a command that will not be reissued */
838 dev_warn(&port->dd->pdev->dev,
839 "retiring tag %d\n", tag);
840 atomic_set(&port->commands[tag].active, 0);
841
842 if (port->commands[tag].comp_func)
843 port->commands[tag].comp_func(
844 port,
845 tag,
846 port->commands[tag].comp_data,
847 PORT_IRQ_TF_ERR);
848 else
849 dev_warn(&port->dd->pdev->dev,
850 "Bad completion for tag %d\n",
851 tag);
852 }
853 }
854 print_tags(dd, "TFE tags reissued:", tagaccum);
855
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100856 /* clear eh_active */
857 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
858 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600859
860 mod_timer(&port->cmd_timer,
861 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
862}
863
864/*
865 * Handle a set device bits interrupt
866 */
867static inline void mtip_process_sdbf(struct driver_data *dd)
868{
869 struct mtip_port *port = dd->port;
870 int group, tag, bit;
871 u32 completed;
872 struct mtip_cmd *command;
873
874 /* walk all bits in all slot groups */
875 for (group = 0; group < dd->slot_groups; group++) {
876 completed = readl(port->completed[group]);
877
878 /* clear completed status register in the hardware.*/
879 writel(completed, port->completed[group]);
880
881 /* Process completed commands. */
882 for (bit = 0;
883 (bit < 32) && completed;
884 bit++, completed >>= 1) {
885 if (completed & 0x01) {
886 tag = (group << 5) | bit;
887
888 /* skip internal command slot. */
889 if (unlikely(tag == MTIP_TAG_INTERNAL))
890 continue;
891
892 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600893 /* make internal callback */
894 if (likely(command->comp_func)) {
895 command->comp_func(
896 port,
897 tag,
898 command->comp_data,
899 0);
900 } else {
901 dev_warn(&dd->pdev->dev,
902 "Null completion "
903 "for tag %d",
904 tag);
905
906 if (mtip_check_surprise_removal(
907 dd->pdev)) {
908 mtip_command_cleanup(dd);
909 return;
910 }
911 }
912 }
913 }
914 }
915}
916
917/*
918 * Process legacy pio and d2h interrupts
919 */
920static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
921{
922 struct mtip_port *port = dd->port;
923 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
924
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100925 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
926 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600927 & (1 << MTIP_TAG_INTERNAL))) {
928 if (cmd->comp_func) {
929 cmd->comp_func(port,
930 MTIP_TAG_INTERNAL,
931 cmd->comp_data,
932 0);
933 return;
934 }
935 }
936
937 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
938
939 return;
940}
941
942/*
943 * Demux and handle errors
944 */
945static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
946{
947 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
948 mtip_handle_tfe(dd);
949
950 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
951 dev_warn(&dd->pdev->dev,
952 "Clearing PxSERR.DIAG.x\n");
953 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
954 }
955
956 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
957 dev_warn(&dd->pdev->dev,
958 "Clearing PxSERR.DIAG.n\n");
959 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
960 }
961
962 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
963 dev_warn(&dd->pdev->dev,
964 "Port stat errors %x unhandled\n",
965 (port_stat & ~PORT_IRQ_HANDLED));
966 }
967}
968
969static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
970{
971 struct driver_data *dd = (struct driver_data *) data;
972 struct mtip_port *port = dd->port;
973 u32 hba_stat, port_stat;
974 int rv = IRQ_NONE;
975
976 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
977 if (hba_stat) {
978 rv = IRQ_HANDLED;
979
980 /* Acknowledge the interrupt status on the port.*/
981 port_stat = readl(port->mmio + PORT_IRQ_STAT);
982 writel(port_stat, port->mmio + PORT_IRQ_STAT);
983
984 /* Demux port status */
985 if (likely(port_stat & PORT_IRQ_SDB_FIS))
986 mtip_process_sdbf(dd);
987
988 if (unlikely(port_stat & PORT_IRQ_ERR)) {
989 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
990 mtip_command_cleanup(dd);
991 /* don't proceed further */
992 return IRQ_HANDLED;
993 }
Asai Thambi S P45038362012-04-09 08:35:38 +0200994 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
995 &dd->dd_flag))
996 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600997
998 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
999 }
1000
1001 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1002 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1003 }
1004
1005 /* acknowledge interrupt */
1006 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1007
1008 return rv;
1009}
1010
1011/*
1012 * Wrapper for mtip_handle_irq
1013 * (ignores return code)
1014 */
1015static void mtip_tasklet(unsigned long data)
1016{
1017 mtip_handle_irq((struct driver_data *) data);
1018}
1019
1020/*
1021 * HBA interrupt subroutine.
1022 *
1023 * @irq IRQ number.
1024 * @instance Pointer to the driver data structure.
1025 *
1026 * return value
1027 * IRQ_HANDLED A HBA interrupt was pending and handled.
1028 * IRQ_NONE This interrupt was not for the HBA.
1029 */
1030static irqreturn_t mtip_irq_handler(int irq, void *instance)
1031{
1032 struct driver_data *dd = instance;
1033 tasklet_schedule(&dd->tasklet);
1034 return IRQ_HANDLED;
1035}
1036
1037static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1038{
1039 atomic_set(&port->commands[tag].active, 1);
1040 writel(1 << MTIP_TAG_BIT(tag),
1041 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1042}
1043
1044/*
1045 * Wait for port to quiesce
1046 *
1047 * @port Pointer to port data structure
1048 * @timeout Max duration to wait (ms)
1049 *
1050 * return value
1051 * 0 Success
1052 * -EBUSY Commands still active
1053 */
1054static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1055{
1056 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001057 unsigned int n;
1058 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001059
1060 to = jiffies + msecs_to_jiffies(timeout);
1061 do {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01001062 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1063 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001064 msleep(20);
1065 continue; /* svc thd is actively issuing commands */
1066 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001067 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1068 &port->dd->dd_flag))
1069 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001070 /*
1071 * Ignore s_active bit 0 of array element 0.
1072 * This bit will always be set
1073 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001074 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001075 for (n = 1; n < port->dd->slot_groups; n++)
1076 active |= readl(port->s_active[n]);
1077
1078 if (!active)
1079 break;
1080
1081 msleep(20);
1082 } while (time_before(jiffies, to));
1083
1084 return active ? -EBUSY : 0;
1085}
1086
1087/*
1088 * Execute an internal command and wait for the completion.
1089 *
1090 * @port Pointer to the port data structure.
1091 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001092 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001093 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001094 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001095 * @opts Command header options, excluding the FIS length
1096 * and the number of PRD entries.
1097 * @timeout Time in ms to wait for the command to complete.
1098 *
1099 * return value
1100 * 0 Command completed successfully.
1101 * -EFAULT The buffer address is not correctly aligned.
1102 * -EBUSY Internal command or other IO in progress.
1103 * -EAGAIN Time out waiting for command to complete.
1104 */
1105static int mtip_exec_internal_command(struct mtip_port *port,
1106 void *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001107 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001108 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001109 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001110 u32 opts,
1111 gfp_t atomic,
1112 unsigned long timeout)
1113{
1114 struct mtip_cmd_sg *command_sg;
1115 DECLARE_COMPLETION_ONSTACK(wait);
1116 int rv = 0;
1117 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1118
1119 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1120 if (buffer & 0x00000007) {
1121 dev_err(&port->dd->pdev->dev,
1122 "SG buffer is not 8 byte aligned\n");
1123 return -EFAULT;
1124 }
1125
1126 /* Only one internal command should be running at a time */
1127 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1128 dev_warn(&port->dd->pdev->dev,
1129 "Internal command already active\n");
1130 return -EBUSY;
1131 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001132 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133
1134 if (atomic == GFP_KERNEL) {
1135 /* wait for io to complete if non atomic */
1136 if (mtip_quiesce_io(port, 5000) < 0) {
1137 dev_warn(&port->dd->pdev->dev,
1138 "Failed to quiesce IO\n");
1139 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001140 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1141 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001142 return -EBUSY;
1143 }
1144
1145 /* Set the completion function and data for the command. */
1146 int_cmd->comp_data = &wait;
1147 int_cmd->comp_func = mtip_completion;
1148
1149 } else {
1150 /* Clear completion - we're going to poll */
1151 int_cmd->comp_data = NULL;
1152 int_cmd->comp_func = NULL;
1153 }
1154
1155 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001156 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001157
1158 /* Populate the SG list */
1159 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001160 __force_bit2int cpu_to_le32(opts | fis_len);
1161 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001162 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1163
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001164 command_sg->info =
1165 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1166 command_sg->dba =
1167 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1168 command_sg->dba_upper =
1169 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001170
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001171 int_cmd->command_header->opts |=
1172 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001173 }
1174
1175 /* Populate the command header */
1176 int_cmd->command_header->byte_count = 0;
1177
1178 /* Issue the command to the hardware */
1179 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1180
1181 /* Poll if atomic, wait_for_completion otherwise */
1182 if (atomic == GFP_KERNEL) {
1183 /* Wait for the command to complete or timeout. */
1184 if (wait_for_completion_timeout(
1185 &wait,
1186 msecs_to_jiffies(timeout)) == 0) {
1187 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001188 "Internal command did not complete [%d] "
1189 "within timeout of %lu ms\n",
1190 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001191 if (mtip_check_surprise_removal(port->dd->pdev) ||
1192 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1193 &port->dd->dd_flag)) {
1194 rv = -ENXIO;
1195 goto exec_ic_exit;
1196 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 rv = -EAGAIN;
1198 }
1199
1200 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1201 & (1 << MTIP_TAG_INTERNAL)) {
1202 dev_warn(&port->dd->pdev->dev,
1203 "Retiring internal command but CI is 1.\n");
Asai Thambi S P45038362012-04-09 08:35:38 +02001204 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1205 &port->dd->dd_flag)) {
1206 hba_reset_nosleep(port->dd);
1207 rv = -ENXIO;
1208 } else {
1209 mtip_restart_port(port);
1210 rv = -EAGAIN;
1211 }
1212 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001213 }
1214
1215 } else {
1216 /* Spin for <timeout> checking if command still outstanding */
1217 timeout = jiffies + msecs_to_jiffies(timeout);
1218
1219 while ((readl(
1220 port->cmd_issue[MTIP_TAG_INTERNAL])
1221 & (1 << MTIP_TAG_INTERNAL))
Asai Thambi S P45038362012-04-09 08:35:38 +02001222 && time_before(jiffies, timeout)) {
1223 if (mtip_check_surprise_removal(port->dd->pdev) ||
1224 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1225 &port->dd->dd_flag)) {
1226 rv = -ENXIO;
1227 goto exec_ic_exit;
1228 }
1229 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001230
1231 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1232 & (1 << MTIP_TAG_INTERNAL)) {
1233 dev_err(&port->dd->pdev->dev,
1234 "Internal command did not complete [%d]\n",
1235 atomic);
1236 rv = -EAGAIN;
Asai Thambi S P45038362012-04-09 08:35:38 +02001237 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1238 &port->dd->dd_flag)) {
1239 hba_reset_nosleep(port->dd);
1240 rv = -ENXIO;
1241 } else {
1242 mtip_restart_port(port);
1243 rv = -EAGAIN;
1244 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001245 }
1246 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001247exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001248 /* Clear the allocated and active bits for the internal command. */
1249 atomic_set(&int_cmd->active, 0);
1250 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001251 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1252 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001253
1254 return rv;
1255}
1256
1257/*
1258 * Byte-swap ATA ID strings.
1259 *
1260 * ATA identify data contains strings in byte-swapped 16-bit words.
1261 * They must be swapped (on all architectures) to be usable as C strings.
1262 * This function swaps bytes in-place.
1263 *
1264 * @buf The buffer location of the string
1265 * @len The number of bytes to swap
1266 *
1267 * return value
1268 * None
1269 */
1270static inline void ata_swap_string(u16 *buf, unsigned int len)
1271{
1272 int i;
1273 for (i = 0; i < (len/2); i++)
1274 be16_to_cpus(&buf[i]);
1275}
1276
1277/*
1278 * Request the device identity information.
1279 *
1280 * If a user space buffer is not specified, i.e. is NULL, the
1281 * identify information is still read from the drive and placed
1282 * into the identify data buffer (@e port->identify) in the
1283 * port data structure.
1284 * When the identify buffer contains valid identify information @e
1285 * port->identify_valid is non-zero.
1286 *
1287 * @port Pointer to the port structure.
1288 * @user_buffer A user space buffer where the identify data should be
1289 * copied.
1290 *
1291 * return value
1292 * 0 Command completed successfully.
1293 * -EFAULT An error occurred while coping data to the user buffer.
1294 * -1 Command failed.
1295 */
1296static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1297{
1298 int rv = 0;
1299 struct host_to_dev_fis fis;
1300
Asai Thambi S P45038362012-04-09 08:35:38 +02001301 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1302 return -EFAULT;
1303
Sam Bradshaw88523a62011-08-30 08:34:26 -06001304 /* Build the FIS. */
1305 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1306 fis.type = 0x27;
1307 fis.opts = 1 << 7;
1308 fis.command = ATA_CMD_ID_ATA;
1309
1310 /* Set the identify information as invalid. */
1311 port->identify_valid = 0;
1312
1313 /* Clear the identify information. */
1314 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1315
1316 /* Execute the command. */
1317 if (mtip_exec_internal_command(port,
1318 &fis,
1319 5,
1320 port->identify_dma,
1321 sizeof(u16) * ATA_ID_WORDS,
1322 0,
1323 GFP_KERNEL,
1324 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1325 < 0) {
1326 rv = -1;
1327 goto out;
1328 }
1329
1330 /*
1331 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1332 * perform field-sensitive swapping on the string fields.
1333 * See the kernel use of ata_id_string() for proof of this.
1334 */
1335#ifdef __LITTLE_ENDIAN
1336 ata_swap_string(port->identify + 27, 40); /* model string*/
1337 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1338 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1339#else
1340 {
1341 int i;
1342 for (i = 0; i < ATA_ID_WORDS; i++)
1343 port->identify[i] = le16_to_cpu(port->identify[i]);
1344 }
1345#endif
1346
1347 /* Set the identify buffer as valid. */
1348 port->identify_valid = 1;
1349
1350 if (user_buffer) {
1351 if (copy_to_user(
1352 user_buffer,
1353 port->identify,
1354 ATA_ID_WORDS * sizeof(u16))) {
1355 rv = -EFAULT;
1356 goto out;
1357 }
1358 }
1359
1360out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001361 return rv;
1362}
1363
1364/*
1365 * Issue a standby immediate command to the device.
1366 *
1367 * @port Pointer to the port structure.
1368 *
1369 * return value
1370 * 0 Command was executed successfully.
1371 * -1 An error occurred while executing the command.
1372 */
1373static int mtip_standby_immediate(struct mtip_port *port)
1374{
1375 int rv;
1376 struct host_to_dev_fis fis;
1377
Sam Bradshaw88523a62011-08-30 08:34:26 -06001378 /* Build the FIS. */
1379 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1380 fis.type = 0x27;
1381 fis.opts = 1 << 7;
1382 fis.command = ATA_CMD_STANDBYNOW1;
1383
1384 /* Execute the command. Use a 15-second timeout for large drives. */
1385 rv = mtip_exec_internal_command(port,
1386 &fis,
1387 5,
1388 0,
1389 0,
1390 0,
1391 GFP_KERNEL,
1392 15000);
1393
Sam Bradshaw88523a62011-08-30 08:34:26 -06001394 return rv;
1395}
1396
1397/*
1398 * Get the drive capacity.
1399 *
1400 * @dd Pointer to the device data structure.
1401 * @sectors Pointer to the variable that will receive the sector count.
1402 *
1403 * return value
1404 * 1 Capacity was returned successfully.
1405 * 0 The identify information is invalid.
1406 */
Jens Axboe63166682011-09-27 21:27:43 -06001407static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001408{
1409 struct mtip_port *port = dd->port;
1410 u64 total, raw0, raw1, raw2, raw3;
1411 raw0 = port->identify[100];
1412 raw1 = port->identify[101];
1413 raw2 = port->identify[102];
1414 raw3 = port->identify[103];
1415 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1416 *sectors = total;
1417 return (bool) !!port->identify_valid;
1418}
1419
1420/*
1421 * Reset the HBA.
1422 *
1423 * Resets the HBA by setting the HBA Reset bit in the Global
1424 * HBA Control register. After setting the HBA Reset bit the
1425 * function waits for 1 second before reading the HBA Reset
1426 * bit to make sure it has cleared. If HBA Reset is not clear
1427 * an error is returned. Cannot be used in non-blockable
1428 * context.
1429 *
1430 * @dd Pointer to the driver data structure.
1431 *
1432 * return value
1433 * 0 The reset was successful.
1434 * -1 The HBA Reset bit did not clear.
1435 */
1436static int mtip_hba_reset(struct driver_data *dd)
1437{
1438 mtip_deinit_port(dd->port);
1439
1440 /* Set the reset bit */
1441 writel(HOST_RESET, dd->mmio + HOST_CTL);
1442
1443 /* Flush */
1444 readl(dd->mmio + HOST_CTL);
1445
1446 /* Wait for reset to clear */
1447 ssleep(1);
1448
1449 /* Check the bit has cleared */
1450 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1451 dev_err(&dd->pdev->dev,
1452 "Reset bit did not clear.\n");
1453 return -1;
1454 }
1455
1456 return 0;
1457}
1458
1459/*
1460 * Display the identify command data.
1461 *
1462 * @port Pointer to the port data structure.
1463 *
1464 * return value
1465 * None
1466 */
1467static void mtip_dump_identify(struct mtip_port *port)
1468{
1469 sector_t sectors;
1470 unsigned short revid;
1471 char cbuf[42];
1472
1473 if (!port->identify_valid)
1474 return;
1475
1476 strlcpy(cbuf, (char *)(port->identify+10), 21);
1477 dev_info(&port->dd->pdev->dev,
1478 "Serial No.: %s\n", cbuf);
1479
1480 strlcpy(cbuf, (char *)(port->identify+23), 9);
1481 dev_info(&port->dd->pdev->dev,
1482 "Firmware Ver.: %s\n", cbuf);
1483
1484 strlcpy(cbuf, (char *)(port->identify+27), 41);
1485 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1486
1487 if (mtip_hw_get_capacity(port->dd, &sectors))
1488 dev_info(&port->dd->pdev->dev,
1489 "Capacity: %llu sectors (%llu MB)\n",
1490 (u64)sectors,
1491 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1492
1493 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001494 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001495 case 0x1:
1496 strlcpy(cbuf, "A0", 3);
1497 break;
1498 case 0x3:
1499 strlcpy(cbuf, "A2", 3);
1500 break;
1501 default:
1502 strlcpy(cbuf, "?", 2);
1503 break;
1504 }
1505 dev_info(&port->dd->pdev->dev,
1506 "Card Type: %s\n", cbuf);
1507}
1508
1509/*
1510 * Map the commands scatter list into the command table.
1511 *
1512 * @command Pointer to the command.
1513 * @nents Number of scatter list entries.
1514 *
1515 * return value
1516 * None
1517 */
1518static inline void fill_command_sg(struct driver_data *dd,
1519 struct mtip_cmd *command,
1520 int nents)
1521{
1522 int n;
1523 unsigned int dma_len;
1524 struct mtip_cmd_sg *command_sg;
1525 struct scatterlist *sg = command->sg;
1526
1527 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1528
1529 for (n = 0; n < nents; n++) {
1530 dma_len = sg_dma_len(sg);
1531 if (dma_len > 0x400000)
1532 dev_err(&dd->pdev->dev,
1533 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001534 command_sg->info = __force_bit2int
1535 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1536 command_sg->dba = __force_bit2int
1537 cpu_to_le32(sg_dma_address(sg));
1538 command_sg->dba_upper = __force_bit2int
1539 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001540 command_sg++;
1541 sg++;
1542 }
1543}
1544
1545/*
1546 * @brief Execute a drive command.
1547 *
1548 * return value 0 The command completed successfully.
1549 * return value -1 An error occurred while executing the command.
1550 */
Jens Axboe63166682011-09-27 21:27:43 -06001551static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001552{
1553 struct host_to_dev_fis fis;
1554 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1555
Sam Bradshaw88523a62011-08-30 08:34:26 -06001556 /* Build the FIS. */
1557 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1558 fis.type = 0x27;
1559 fis.opts = 1 << 7;
1560 fis.command = command[0];
1561 fis.features = command[1];
1562 fis.sect_count = command[2];
1563 fis.sector = command[3];
1564 fis.cyl_low = command[4];
1565 fis.cyl_hi = command[5];
1566 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1567
1568
Asai Thambi S P45038362012-04-09 08:35:38 +02001569 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 -06001570 __func__,
1571 command[0],
1572 command[1],
1573 command[2],
1574 command[3],
1575 command[4],
1576 command[5],
1577 command[6]);
1578
1579 /* Execute the command. */
1580 if (mtip_exec_internal_command(port,
1581 &fis,
1582 5,
1583 0,
1584 0,
1585 0,
1586 GFP_KERNEL,
1587 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001588 return -1;
1589 }
1590
1591 command[0] = reply->command; /* Status*/
1592 command[1] = reply->features; /* Error*/
1593 command[4] = reply->cyl_low;
1594 command[5] = reply->cyl_hi;
1595
Asai Thambi S P45038362012-04-09 08:35:38 +02001596 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 -06001597 __func__,
1598 command[0],
1599 command[1],
1600 command[4],
1601 command[5]);
1602
Sam Bradshaw88523a62011-08-30 08:34:26 -06001603 return 0;
1604}
1605
1606/*
1607 * @brief Execute a drive command.
1608 *
1609 * @param port Pointer to the port data structure.
1610 * @param command Pointer to the user specified command parameters.
1611 * @param user_buffer Pointer to the user space buffer where read sector
1612 * data should be copied.
1613 *
1614 * return value 0 The command completed successfully.
1615 * return value -EFAULT An error occurred while copying the completion
1616 * data to the user space buffer.
1617 * return value -1 An error occurred while executing the command.
1618 */
Jens Axboe63166682011-09-27 21:27:43 -06001619static int exec_drive_command(struct mtip_port *port, u8 *command,
1620 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001621{
1622 struct host_to_dev_fis fis;
1623 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1624
Sam Bradshaw88523a62011-08-30 08:34:26 -06001625 /* Build the FIS. */
1626 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1627 fis.type = 0x27;
1628 fis.opts = 1 << 7;
1629 fis.command = command[0];
1630 fis.features = command[2];
1631 fis.sect_count = command[3];
1632 if (fis.command == ATA_CMD_SMART) {
1633 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001634 fis.cyl_low = 0x4F;
1635 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001636 }
1637
1638 dbg_printk(MTIP_DRV_NAME
1639 "%s: User Command: cmd %x, sect %x, "
1640 "feat %x, sectcnt %x\n",
1641 __func__,
1642 command[0],
1643 command[1],
1644 command[2],
1645 command[3]);
1646
1647 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1648
1649 /* Execute the command. */
1650 if (mtip_exec_internal_command(port,
1651 &fis,
1652 5,
1653 port->sector_buffer_dma,
1654 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1655 0,
1656 GFP_KERNEL,
1657 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1658 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001659 return -1;
1660 }
1661
1662 /* Collect the completion status. */
1663 command[0] = reply->command; /* Status*/
1664 command[1] = reply->features; /* Error*/
1665 command[2] = command[3];
1666
1667 dbg_printk(MTIP_DRV_NAME
1668 "%s: Completion Status: stat %x, "
1669 "err %x, cmd %x\n",
1670 __func__,
1671 command[0],
1672 command[1],
1673 command[2]);
1674
1675 if (user_buffer && command[3]) {
1676 if (copy_to_user(user_buffer,
1677 port->sector_buffer,
1678 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679 return -EFAULT;
1680 }
1681 }
1682
Sam Bradshaw88523a62011-08-30 08:34:26 -06001683 return 0;
1684}
1685
1686/*
1687 * Indicates whether a command has a single sector payload.
1688 *
1689 * @command passed to the device to perform the certain event.
1690 * @features passed to the device to perform the certain event.
1691 *
1692 * return value
1693 * 1 command is one that always has a single sector payload,
1694 * regardless of the value in the Sector Count field.
1695 * 0 otherwise
1696 *
1697 */
1698static unsigned int implicit_sector(unsigned char command,
1699 unsigned char features)
1700{
1701 unsigned int rv = 0;
1702
1703 /* list of commands that have an implicit sector count of 1 */
1704 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001705 case ATA_CMD_SEC_SET_PASS:
1706 case ATA_CMD_SEC_UNLOCK:
1707 case ATA_CMD_SEC_ERASE_PREP:
1708 case ATA_CMD_SEC_ERASE_UNIT:
1709 case ATA_CMD_SEC_FREEZE_LOCK:
1710 case ATA_CMD_SEC_DISABLE_PASS:
1711 case ATA_CMD_PMP_READ:
1712 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001713 rv = 1;
1714 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001715 case ATA_CMD_SET_MAX:
1716 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001717 rv = 1;
1718 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001719 case ATA_CMD_SMART:
1720 if ((features == ATA_SMART_READ_VALUES) ||
1721 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001722 rv = 1;
1723 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001724 case ATA_CMD_CONF_OVERLAY:
1725 if ((features == ATA_DCO_IDENTIFY) ||
1726 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001727 rv = 1;
1728 break;
1729 }
1730 return rv;
1731}
1732
1733/*
1734 * Executes a taskfile
1735 * See ide_taskfile_ioctl() for derivation
1736 */
1737static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001738 void __user *buf,
1739 ide_task_request_t *req_task,
1740 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001741{
1742 struct host_to_dev_fis fis;
1743 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001744 u8 *outbuf = NULL;
1745 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001746 dma_addr_t outbuf_dma = 0;
1747 dma_addr_t inbuf_dma = 0;
1748 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001749 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001750 unsigned int taskin = 0;
1751 unsigned int taskout = 0;
1752 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001753 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1754 unsigned int force_single_sector;
1755 unsigned int transfer_size;
1756 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001757 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001758
1759 taskout = req_task->out_size;
1760 taskin = req_task->in_size;
1761 /* 130560 = 512 * 0xFF*/
1762 if (taskin > 130560 || taskout > 130560) {
1763 err = -EINVAL;
1764 goto abort;
1765 }
1766
1767 if (taskout) {
1768 outbuf = kzalloc(taskout, GFP_KERNEL);
1769 if (outbuf == NULL) {
1770 err = -ENOMEM;
1771 goto abort;
1772 }
1773 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1774 err = -EFAULT;
1775 goto abort;
1776 }
1777 outbuf_dma = pci_map_single(dd->pdev,
1778 outbuf,
1779 taskout,
1780 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001781 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001782 err = -ENOMEM;
1783 goto abort;
1784 }
1785 dma_buffer = outbuf_dma;
1786 }
1787
1788 if (taskin) {
1789 inbuf = kzalloc(taskin, GFP_KERNEL);
1790 if (inbuf == NULL) {
1791 err = -ENOMEM;
1792 goto abort;
1793 }
1794
1795 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1796 err = -EFAULT;
1797 goto abort;
1798 }
1799 inbuf_dma = pci_map_single(dd->pdev,
1800 inbuf,
1801 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001802 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001803 err = -ENOMEM;
1804 goto abort;
1805 }
1806 dma_buffer = inbuf_dma;
1807 }
1808
1809 /* only supports PIO and non-data commands from this ioctl. */
1810 switch (req_task->data_phase) {
1811 case TASKFILE_OUT:
1812 nsect = taskout / ATA_SECT_SIZE;
1813 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1814 break;
1815 case TASKFILE_IN:
1816 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1817 break;
1818 case TASKFILE_NO_DATA:
1819 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1820 break;
1821 default:
1822 err = -EINVAL;
1823 goto abort;
1824 }
1825
Sam Bradshaw88523a62011-08-30 08:34:26 -06001826 /* Build the FIS. */
1827 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1828
1829 fis.type = 0x27;
1830 fis.opts = 1 << 7;
1831 fis.command = req_task->io_ports[7];
1832 fis.features = req_task->io_ports[1];
1833 fis.sect_count = req_task->io_ports[2];
1834 fis.lba_low = req_task->io_ports[3];
1835 fis.lba_mid = req_task->io_ports[4];
1836 fis.lba_hi = req_task->io_ports[5];
1837 /* Clear the dev bit*/
1838 fis.device = req_task->io_ports[6] & ~0x10;
1839
1840 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1841 req_task->in_flags.all =
1842 IDE_TASKFILE_STD_IN_FLAGS |
1843 (IDE_HOB_STD_IN_FLAGS << 8);
1844 fis.lba_low_ex = req_task->hob_ports[3];
1845 fis.lba_mid_ex = req_task->hob_ports[4];
1846 fis.lba_hi_ex = req_task->hob_ports[5];
1847 fis.features_ex = req_task->hob_ports[1];
1848 fis.sect_cnt_ex = req_task->hob_ports[2];
1849
1850 } else {
1851 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1852 }
1853
1854 force_single_sector = implicit_sector(fis.command, fis.features);
1855
1856 if ((taskin || taskout) && (!fis.sect_count)) {
1857 if (nsect)
1858 fis.sect_count = nsect;
1859 else {
1860 if (!force_single_sector) {
1861 dev_warn(&dd->pdev->dev,
1862 "data movement but "
1863 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001864 err = -EINVAL;
1865 goto abort;
1866 }
1867 }
1868 }
1869
1870 dbg_printk(MTIP_DRV_NAME
1871 "taskfile: cmd %x, feat %x, nsect %x,"
1872 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1873 " head/dev %x\n",
1874 fis.command,
1875 fis.features,
1876 fis.sect_count,
1877 fis.lba_low,
1878 fis.lba_mid,
1879 fis.lba_hi,
1880 fis.device);
1881
1882 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001883 case ATA_CMD_DOWNLOAD_MICRO:
1884 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001885 timeout = 60000;
1886 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001887 case ATA_CMD_SEC_ERASE_UNIT:
1888 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001889 timeout = 240000;
1890 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001891 case ATA_CMD_STANDBYNOW1:
1892 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001893 timeout = 10000;
1894 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001895 case 0xF7:
1896 case 0xFA:
1897 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001898 timeout = 10000;
1899 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001900 case ATA_CMD_SMART:
1901 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001902 timeout = 10000;
1903 break;
1904 default:
1905 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1906 break;
1907 }
1908
1909 /* Determine the correct transfer size.*/
1910 if (force_single_sector)
1911 transfer_size = ATA_SECT_SIZE;
1912 else
1913 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1914
1915 /* Execute the command.*/
1916 if (mtip_exec_internal_command(dd->port,
1917 &fis,
1918 5,
1919 dma_buffer,
1920 transfer_size,
1921 0,
1922 GFP_KERNEL,
1923 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001924 err = -EIO;
1925 goto abort;
1926 }
1927
1928 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1929
1930 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1931 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1932 req_task->io_ports[7] = reply->control;
1933 } else {
1934 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1935 req_task->io_ports[7] = reply->command;
1936 }
1937
1938 /* reclaim the DMA buffers.*/
1939 if (inbuf_dma)
1940 pci_unmap_single(dd->pdev, inbuf_dma,
1941 taskin, DMA_FROM_DEVICE);
1942 if (outbuf_dma)
1943 pci_unmap_single(dd->pdev, outbuf_dma,
1944 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001945 inbuf_dma = 0;
1946 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001947
1948 /* return the ATA registers to the caller.*/
1949 req_task->io_ports[1] = reply->features;
1950 req_task->io_ports[2] = reply->sect_count;
1951 req_task->io_ports[3] = reply->lba_low;
1952 req_task->io_ports[4] = reply->lba_mid;
1953 req_task->io_ports[5] = reply->lba_hi;
1954 req_task->io_ports[6] = reply->device;
1955
1956 if (req_task->out_flags.all & 1) {
1957
1958 req_task->hob_ports[3] = reply->lba_low_ex;
1959 req_task->hob_ports[4] = reply->lba_mid_ex;
1960 req_task->hob_ports[5] = reply->lba_hi_ex;
1961 req_task->hob_ports[1] = reply->features_ex;
1962 req_task->hob_ports[2] = reply->sect_cnt_ex;
1963 }
1964
1965 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001966 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06001967 ((fis.command == 0xFC) &&
1968 (fis.features == 0x27 || fis.features == 0x72 ||
1969 fis.features == 0x62 || fis.features == 0x26))) &&
1970 !(reply->command & 1)) {
1971 mtip_restart_port(dd->port);
1972 }
1973
1974 dbg_printk(MTIP_DRV_NAME
1975 "%s: Completion: stat %x,"
1976 "err %x, sect_cnt %x, lbalo %x,"
1977 "lbamid %x, lbahi %x, dev %x\n",
1978 __func__,
1979 req_task->io_ports[7],
1980 req_task->io_ports[1],
1981 req_task->io_ports[2],
1982 req_task->io_ports[3],
1983 req_task->io_ports[4],
1984 req_task->io_ports[5],
1985 req_task->io_ports[6]);
1986
Sam Bradshaw88523a62011-08-30 08:34:26 -06001987 if (taskout) {
1988 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1989 err = -EFAULT;
1990 goto abort;
1991 }
1992 }
1993 if (taskin) {
1994 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1995 err = -EFAULT;
1996 goto abort;
1997 }
1998 }
1999abort:
2000 if (inbuf_dma)
2001 pci_unmap_single(dd->pdev, inbuf_dma,
2002 taskin, DMA_FROM_DEVICE);
2003 if (outbuf_dma)
2004 pci_unmap_single(dd->pdev, outbuf_dma,
2005 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002006 kfree(outbuf);
2007 kfree(inbuf);
2008
2009 return err;
2010}
2011
2012/*
2013 * Handle IOCTL calls from the Block Layer.
2014 *
2015 * This function is called by the Block Layer when it receives an IOCTL
2016 * command that it does not understand. If the IOCTL command is not supported
2017 * this function returns -ENOTTY.
2018 *
2019 * @dd Pointer to the driver data structure.
2020 * @cmd IOCTL command passed from the Block Layer.
2021 * @arg IOCTL argument passed from the Block Layer.
2022 *
2023 * return value
2024 * 0 The IOCTL completed successfully.
2025 * -ENOTTY The specified command is not supported.
2026 * -EFAULT An error occurred copying data to a user space buffer.
2027 * -EIO An error occurred while executing the command.
2028 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002029static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2030 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002031{
2032 switch (cmd) {
2033 case HDIO_GET_IDENTITY:
2034 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
2035 dev_warn(&dd->pdev->dev,
2036 "Unable to read identity\n");
2037 return -EIO;
2038 }
2039
2040 break;
2041 case HDIO_DRIVE_CMD:
2042 {
2043 u8 drive_command[4];
2044
2045 /* Copy the user command info to our buffer. */
2046 if (copy_from_user(drive_command,
2047 (void __user *) arg,
2048 sizeof(drive_command)))
2049 return -EFAULT;
2050
2051 /* Execute the drive command. */
2052 if (exec_drive_command(dd->port,
2053 drive_command,
2054 (void __user *) (arg+4)))
2055 return -EIO;
2056
2057 /* Copy the status back to the users buffer. */
2058 if (copy_to_user((void __user *) arg,
2059 drive_command,
2060 sizeof(drive_command)))
2061 return -EFAULT;
2062
2063 break;
2064 }
2065 case HDIO_DRIVE_TASK:
2066 {
2067 u8 drive_command[7];
2068
2069 /* Copy the user command info to our buffer. */
2070 if (copy_from_user(drive_command,
2071 (void __user *) arg,
2072 sizeof(drive_command)))
2073 return -EFAULT;
2074
2075 /* Execute the drive command. */
2076 if (exec_drive_task(dd->port, drive_command))
2077 return -EIO;
2078
2079 /* Copy the status back to the users buffer. */
2080 if (copy_to_user((void __user *) arg,
2081 drive_command,
2082 sizeof(drive_command)))
2083 return -EFAULT;
2084
2085 break;
2086 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002087 case HDIO_DRIVE_TASKFILE: {
2088 ide_task_request_t req_task;
2089 int ret, outtotal;
2090
2091 if (copy_from_user(&req_task, (void __user *) arg,
2092 sizeof(req_task)))
2093 return -EFAULT;
2094
2095 outtotal = sizeof(req_task);
2096
2097 ret = exec_drive_taskfile(dd, (void __user *) arg,
2098 &req_task, outtotal);
2099
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002100 if (copy_to_user((void __user *) arg, &req_task,
2101 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002102 return -EFAULT;
2103
2104 return ret;
2105 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002106
2107 default:
2108 return -EINVAL;
2109 }
2110 return 0;
2111}
2112
2113/*
2114 * Submit an IO to the hw
2115 *
2116 * This function is called by the block layer to issue an io
2117 * to the device. Upon completion, the callback function will
2118 * be called with the data parameter passed as the callback data.
2119 *
2120 * @dd Pointer to the driver data structure.
2121 * @start First sector to read.
2122 * @nsect Number of sectors to read.
2123 * @nents Number of entries in scatter list for the read command.
2124 * @tag The tag of this read command.
2125 * @callback Pointer to the function that should be called
2126 * when the read completes.
2127 * @data Callback data passed to the callback function
2128 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002129 * @dir Direction (read or write)
2130 *
2131 * return value
2132 * None
2133 */
Jens Axboe63166682011-09-27 21:27:43 -06002134static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2135 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002136 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002137{
2138 struct host_to_dev_fis *fis;
2139 struct mtip_port *port = dd->port;
2140 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002141 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142
2143 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002144 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002145
2146 command->scatter_ents = nents;
2147
2148 /*
2149 * The number of retries for this command before it is
2150 * reported as a failure to the upper layers.
2151 */
2152 command->retries = MTIP_MAX_RETRIES;
2153
2154 /* Fill out fis */
2155 fis = command->command;
2156 fis->type = 0x27;
2157 fis->opts = 1 << 7;
2158 fis->command =
2159 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002160 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2161 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002162 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002163 fis->features = nsect & 0xFF;
2164 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002165 fis->sect_count = ((tag << 3) | (tag >> 5));
2166 fis->sect_cnt_ex = 0;
2167 fis->control = 0;
2168 fis->res2 = 0;
2169 fis->res3 = 0;
2170 fill_command_sg(dd, command, nents);
2171
2172 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002173 command->command_header->opts =
2174 __force_bit2int cpu_to_le32(
2175 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002176 command->command_header->byte_count = 0;
2177
2178 /*
2179 * Set the completion function and data for the command
2180 * within this layer.
2181 */
2182 command->comp_data = dd;
2183 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002184 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002185
2186 /*
2187 * Set the completion function and data for the command passed
2188 * from the upper layer.
2189 */
2190 command->async_data = data;
2191 command->async_callback = callback;
2192
2193 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002194 * To prevent this command from being issued
2195 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002197 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2198 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2199 set_bit(tag, port->cmds_to_issue);
2200 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2201 return;
2202 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002203
2204 /* Issue the command to the hardware */
2205 mtip_issue_ncq_command(port, tag);
2206
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002207 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002208}
2209
2210/*
2211 * Release a command slot.
2212 *
2213 * @dd Pointer to the driver data structure.
2214 * @tag Slot tag
2215 *
2216 * return value
2217 * None
2218 */
Jens Axboe63166682011-09-27 21:27:43 -06002219static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002220{
2221 release_slot(dd->port, tag);
2222}
2223
2224/*
2225 * Obtain a command slot and return its associated scatter list.
2226 *
2227 * @dd Pointer to the driver data structure.
2228 * @tag Pointer to an int that will receive the allocated command
2229 * slot tag.
2230 *
2231 * return value
2232 * Pointer to the scatter list for the allocated command slot
2233 * or NULL if no command slots are available.
2234 */
Jens Axboe63166682011-09-27 21:27:43 -06002235static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2236 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002237{
2238 /*
2239 * It is possible that, even with this semaphore, a thread
2240 * may think that no command slots are available. Therefore, we
2241 * need to make an attempt to get_slot().
2242 */
2243 down(&dd->port->cmd_slot);
2244 *tag = get_slot(dd->port);
2245
Asai Thambi S P45038362012-04-09 08:35:38 +02002246 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2247 up(&dd->port->cmd_slot);
2248 return NULL;
2249 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002250 if (unlikely(*tag < 0))
2251 return NULL;
2252
2253 return dd->port->commands[*tag].sg;
2254}
2255
2256/*
2257 * Sysfs register/status dump.
2258 *
2259 * @dev Pointer to the device structure, passed by the kernrel.
2260 * @attr Pointer to the device_attribute structure passed by the kernel.
2261 * @buf Pointer to the char buffer that will receive the stats info.
2262 *
2263 * return value
2264 * The size, in bytes, of the data copied into buf.
2265 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002266static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002267 struct device_attribute *attr,
2268 char *buf)
2269{
2270 u32 group_allocated;
2271 struct driver_data *dd = dev_to_disk(dev)->private_data;
2272 int size = 0;
2273 int n;
2274
2275 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2276
2277 for (n = 0; n < dd->slot_groups; n++)
2278 size += sprintf(&buf[size], "0x%08x\n",
2279 readl(dd->port->s_active[n]));
2280
2281 size += sprintf(&buf[size], "Command Issue:\n");
2282
2283 for (n = 0; n < dd->slot_groups; n++)
2284 size += sprintf(&buf[size], "0x%08x\n",
2285 readl(dd->port->cmd_issue[n]));
2286
2287 size += sprintf(&buf[size], "Allocated:\n");
2288
2289 for (n = 0; n < dd->slot_groups; n++) {
2290 if (sizeof(long) > sizeof(u32))
2291 group_allocated =
2292 dd->port->allocated[n/2] >> (32*(n&1));
2293 else
2294 group_allocated = dd->port->allocated[n];
2295 size += sprintf(&buf[size], "0x%08x\n",
2296 group_allocated);
2297 }
2298
2299 size += sprintf(&buf[size], "completed:\n");
2300
2301 for (n = 0; n < dd->slot_groups; n++)
2302 size += sprintf(&buf[size], "0x%08x\n",
2303 readl(dd->port->completed[n]));
2304
2305 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2306 readl(dd->port->mmio + PORT_IRQ_STAT));
2307 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2308 readl(dd->mmio + HOST_IRQ_STAT));
2309
2310 return size;
2311}
Asai Thambi S P45038362012-04-09 08:35:38 +02002312static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002313
2314/*
2315 * Create the sysfs related attributes.
2316 *
2317 * @dd Pointer to the driver data structure.
2318 * @kobj Pointer to the kobj for the block device.
2319 *
2320 * return value
2321 * 0 Operation completed successfully.
2322 * -EINVAL Invalid parameter.
2323 */
Jens Axboe63166682011-09-27 21:27:43 -06002324static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002325{
2326 if (!kobj || !dd)
2327 return -EINVAL;
2328
2329 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2330 dev_warn(&dd->pdev->dev,
2331 "Error creating registers sysfs entry\n");
2332 return 0;
2333}
2334
2335/*
2336 * Remove the sysfs related attributes.
2337 *
2338 * @dd Pointer to the driver data structure.
2339 * @kobj Pointer to the kobj for the block device.
2340 *
2341 * return value
2342 * 0 Operation completed successfully.
2343 * -EINVAL Invalid parameter.
2344 */
Jens Axboe63166682011-09-27 21:27:43 -06002345static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002346{
2347 if (!kobj || !dd)
2348 return -EINVAL;
2349
2350 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2351
2352 return 0;
2353}
2354
2355/*
2356 * Perform any init/resume time hardware setup
2357 *
2358 * @dd Pointer to the driver data structure.
2359 *
2360 * return value
2361 * None
2362 */
2363static inline void hba_setup(struct driver_data *dd)
2364{
2365 u32 hwdata;
2366 hwdata = readl(dd->mmio + HOST_HSORG);
2367
2368 /* interrupt bug workaround: use only 1 IS bit.*/
2369 writel(hwdata |
2370 HSORG_DISABLE_SLOTGRP_INTR |
2371 HSORG_DISABLE_SLOTGRP_PXIS,
2372 dd->mmio + HOST_HSORG);
2373}
2374
2375/*
2376 * Detect the details of the product, and store anything needed
2377 * into the driver data structure. This includes product type and
2378 * version and number of slot groups.
2379 *
2380 * @dd Pointer to the driver data structure.
2381 *
2382 * return value
2383 * None
2384 */
2385static void mtip_detect_product(struct driver_data *dd)
2386{
2387 u32 hwdata;
2388 unsigned int rev, slotgroups;
2389
2390 /*
2391 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2392 * info register:
2393 * [15:8] hardware/software interface rev#
2394 * [ 3] asic-style interface
2395 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2396 */
2397 hwdata = readl(dd->mmio + HOST_HSORG);
2398
2399 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2400 dd->slot_groups = 1;
2401
2402 if (hwdata & 0x8) {
2403 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2404 rev = (hwdata & HSORG_HWREV) >> 8;
2405 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2406 dev_info(&dd->pdev->dev,
2407 "ASIC-FPGA design, HS rev 0x%x, "
2408 "%i slot groups [%i slots]\n",
2409 rev,
2410 slotgroups,
2411 slotgroups * 32);
2412
2413 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2414 dev_warn(&dd->pdev->dev,
2415 "Warning: driver only supports "
2416 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2417 slotgroups = MTIP_MAX_SLOT_GROUPS;
2418 }
2419 dd->slot_groups = slotgroups;
2420 return;
2421 }
2422
2423 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2424}
2425
2426/*
2427 * Blocking wait for FTL rebuild to complete
2428 *
2429 * @dd Pointer to the DRIVER_DATA structure.
2430 *
2431 * return value
2432 * 0 FTL rebuild completed successfully
2433 * -EFAULT FTL rebuild error/timeout/interruption
2434 */
2435static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2436{
2437 unsigned long timeout, cnt = 0, start;
2438
2439 dev_warn(&dd->pdev->dev,
2440 "FTL rebuild in progress. Polling for completion.\n");
2441
2442 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002443 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2444
2445 do {
Asai Thambi S P45038362012-04-09 08:35:38 +02002446 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2447 &dd->dd_flag)))
2448 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002449 if (mtip_check_surprise_removal(dd->pdev))
2450 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002451
Sam Bradshaw88523a62011-08-30 08:34:26 -06002452 if (mtip_get_identify(dd->port, NULL) < 0)
2453 return -EFAULT;
2454
2455 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2456 MTIP_FTL_REBUILD_MAGIC) {
2457 ssleep(1);
2458 /* Print message every 3 minutes */
2459 if (cnt++ >= 180) {
2460 dev_warn(&dd->pdev->dev,
2461 "FTL rebuild in progress (%d secs).\n",
2462 jiffies_to_msecs(jiffies - start) / 1000);
2463 cnt = 0;
2464 }
2465 } else {
2466 dev_warn(&dd->pdev->dev,
2467 "FTL rebuild complete (%d secs).\n",
2468 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002469 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002470 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002471 }
2472 ssleep(10);
2473 } while (time_before(jiffies, timeout));
2474
2475 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002476 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002477 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2478 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002479 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002480}
2481
2482/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002483 * service thread to issue queued commands
2484 *
2485 * @data Pointer to the driver data structure.
2486 *
2487 * return value
2488 * 0
2489 */
2490
2491static int mtip_service_thread(void *data)
2492{
2493 struct driver_data *dd = (struct driver_data *)data;
2494 unsigned long slot, slot_start, slot_wrap;
2495 unsigned int num_cmd_slots = dd->slot_groups * 32;
2496 struct mtip_port *port = dd->port;
2497
2498 while (1) {
2499 /*
2500 * the condition is to check neither an internal command is
2501 * is in progress nor error handling is active
2502 */
2503 wait_event_interruptible(port->svc_wait, (port->flags) &&
2504 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2505 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2506
2507 if (kthread_should_stop())
2508 break;
2509
Asai Thambi S P45038362012-04-09 08:35:38 +02002510 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2511 &dd->dd_flag)))
2512 break;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002513 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002514 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002515 slot = 1;
2516 /* used to restrict the loop to one iteration */
2517 slot_start = num_cmd_slots;
2518 slot_wrap = 0;
2519 while (1) {
2520 slot = find_next_bit(port->cmds_to_issue,
2521 num_cmd_slots, slot);
2522 if (slot_wrap == 1) {
2523 if ((slot_start >= slot) ||
2524 (slot >= num_cmd_slots))
2525 break;
2526 }
2527 if (unlikely(slot_start == num_cmd_slots))
2528 slot_start = slot;
2529
2530 if (unlikely(slot == num_cmd_slots)) {
2531 slot = 1;
2532 slot_wrap = 1;
2533 continue;
2534 }
2535
2536 /* Issue the command to the hardware */
2537 mtip_issue_ncq_command(port, slot);
2538
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002539 clear_bit(slot, port->cmds_to_issue);
2540 }
2541
2542 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002543 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
2544 mtip_ftl_rebuild_poll(dd);
2545 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002546 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002547 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2548
2549 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2550 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002551 }
2552 return 0;
2553}
2554
2555/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002556 * Called once for each card.
2557 *
2558 * @dd Pointer to the driver data structure.
2559 *
2560 * return value
2561 * 0 on success, else an error code.
2562 */
Jens Axboe63166682011-09-27 21:27:43 -06002563static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002564{
2565 int i;
2566 int rv;
2567 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002568 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002569
2570 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2571
2572 mtip_detect_product(dd);
2573 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2574 rv = -EIO;
2575 goto out1;
2576 }
2577 num_command_slots = dd->slot_groups * 32;
2578
2579 hba_setup(dd);
2580
Sam Bradshaw88523a62011-08-30 08:34:26 -06002581 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2582
2583 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2584 if (!dd->port) {
2585 dev_err(&dd->pdev->dev,
2586 "Memory allocation: port structure\n");
2587 return -ENOMEM;
2588 }
2589
2590 /* Counting semaphore to track command slot usage */
2591 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2592
2593 /* Spinlock to prevent concurrent issue */
2594 spin_lock_init(&dd->port->cmd_issue_lock);
2595
2596 /* Set the port mmio base address. */
2597 dd->port->mmio = dd->mmio + PORT_OFFSET;
2598 dd->port->dd = dd;
2599
2600 /* Allocate memory for the command list. */
2601 dd->port->command_list =
2602 dmam_alloc_coherent(&dd->pdev->dev,
2603 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2604 &dd->port->command_list_dma,
2605 GFP_KERNEL);
2606 if (!dd->port->command_list) {
2607 dev_err(&dd->pdev->dev,
2608 "Memory allocation: command list\n");
2609 rv = -ENOMEM;
2610 goto out1;
2611 }
2612
2613 /* Clear the memory we have allocated. */
2614 memset(dd->port->command_list,
2615 0,
2616 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2617
2618 /* Setup the addresse of the RX FIS. */
2619 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2620 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2621
2622 /* Setup the address of the command tables. */
2623 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2624 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2625
2626 /* Setup the address of the identify data. */
2627 dd->port->identify = dd->port->command_table +
2628 HW_CMD_TBL_AR_SZ;
2629 dd->port->identify_dma = dd->port->command_tbl_dma +
2630 HW_CMD_TBL_AR_SZ;
2631
2632 /* Setup the address of the sector buffer. */
2633 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2634 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2635
2636 /* Point the command headers at the command tables. */
2637 for (i = 0; i < num_command_slots; i++) {
2638 dd->port->commands[i].command_header =
2639 dd->port->command_list +
2640 (sizeof(struct mtip_cmd_hdr) * i);
2641 dd->port->commands[i].command_header_dma =
2642 dd->port->command_list_dma +
2643 (sizeof(struct mtip_cmd_hdr) * i);
2644
2645 dd->port->commands[i].command =
2646 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2647 dd->port->commands[i].command_dma =
2648 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2649
2650 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2651 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002652 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002653 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002654 dd->port->commands[i].command_header->ctba =
2655 __force_bit2int cpu_to_le32(
2656 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002657
2658 /*
2659 * If this is not done, a bug is reported by the stock
2660 * FC11 i386. Due to the fact that it has lots of kernel
2661 * debugging enabled.
2662 */
2663 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2664
2665 /* Mark all commands as currently inactive.*/
2666 atomic_set(&dd->port->commands[i].active, 0);
2667 }
2668
2669 /* Setup the pointers to the extended s_active and CI registers. */
2670 for (i = 0; i < dd->slot_groups; i++) {
2671 dd->port->s_active[i] =
2672 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2673 dd->port->cmd_issue[i] =
2674 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2675 dd->port->completed[i] =
2676 dd->port->mmio + i*0x80 + PORT_SDBV;
2677 }
2678
Asai Thambi S P45038362012-04-09 08:35:38 +02002679 timetaken = jiffies;
2680 timeout = jiffies + msecs_to_jiffies(30000);
2681 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2682 time_before(jiffies, timeout)) {
2683 mdelay(100);
2684 }
2685 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
2686 timetaken = jiffies - timetaken;
2687 dev_warn(&dd->pdev->dev,
2688 "Surprise removal detected at %u ms\n",
2689 jiffies_to_msecs(timetaken));
2690 rv = -ENODEV;
2691 goto out2 ;
2692 }
2693 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2694 timetaken = jiffies - timetaken;
2695 dev_warn(&dd->pdev->dev,
2696 "Removal detected at %u ms\n",
2697 jiffies_to_msecs(timetaken));
2698 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002699 goto out2;
2700 }
2701
Asai Thambi S P45038362012-04-09 08:35:38 +02002702 /* Conditionally reset the HBA. */
2703 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
2704 if (mtip_hba_reset(dd) < 0) {
2705 dev_err(&dd->pdev->dev,
2706 "Card did not reset within timeout\n");
2707 rv = -EIO;
2708 goto out2;
2709 }
2710 } else {
2711 /* Clear any pending interrupts on the HBA */
2712 writel(readl(dd->mmio + HOST_IRQ_STAT),
2713 dd->mmio + HOST_IRQ_STAT);
2714 }
2715
Sam Bradshaw88523a62011-08-30 08:34:26 -06002716 mtip_init_port(dd->port);
2717 mtip_start_port(dd->port);
2718
2719 /* Setup the ISR and enable interrupts. */
2720 rv = devm_request_irq(&dd->pdev->dev,
2721 dd->pdev->irq,
2722 mtip_irq_handler,
2723 IRQF_SHARED,
2724 dev_driver_string(&dd->pdev->dev),
2725 dd);
2726
2727 if (rv) {
2728 dev_err(&dd->pdev->dev,
2729 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2730 goto out2;
2731 }
2732
2733 /* Enable interrupts on the HBA. */
2734 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2735 dd->mmio + HOST_CTL);
2736
2737 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002738 init_waitqueue_head(&dd->port->svc_wait);
2739
Sam Bradshaw88523a62011-08-30 08:34:26 -06002740 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2741 dd->port->cmd_timer.function = mtip_timeout_function;
2742 mod_timer(&dd->port->cmd_timer,
2743 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2744
Asai Thambi S P45038362012-04-09 08:35:38 +02002745
2746 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
2747 rv = -EFAULT;
2748 goto out3;
2749 }
2750
Sam Bradshaw88523a62011-08-30 08:34:26 -06002751 if (mtip_get_identify(dd->port, NULL) < 0) {
2752 rv = -EFAULT;
2753 goto out3;
2754 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002755
2756 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2757 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002758 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2759 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002760 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002761 mtip_dump_identify(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002762 return rv;
2763
2764out3:
2765 del_timer_sync(&dd->port->cmd_timer);
2766
2767 /* Disable interrupts on the HBA. */
2768 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2769 dd->mmio + HOST_CTL);
2770
2771 /*Release the IRQ. */
2772 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2773
2774out2:
2775 mtip_deinit_port(dd->port);
2776
2777 /* Free the command/command header memory. */
2778 dmam_free_coherent(&dd->pdev->dev,
2779 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2780 dd->port->command_list,
2781 dd->port->command_list_dma);
2782out1:
2783 /* Free the memory allocated for the for structure. */
2784 kfree(dd->port);
2785
2786 return rv;
2787}
2788
2789/*
2790 * Called to deinitialize an interface.
2791 *
2792 * @dd Pointer to the driver data structure.
2793 *
2794 * return value
2795 * 0
2796 */
Jens Axboe63166682011-09-27 21:27:43 -06002797static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002798{
2799 /*
2800 * Send standby immediate (E0h) to the drive so that it
2801 * saves its state.
2802 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002803 if (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002804
Asai Thambi S P45038362012-04-09 08:35:38 +02002805 if (test_bit(MTIP_FLAG_REBUILD_BIT, &dd->dd_flag))
2806 if (mtip_standby_immediate(dd->port))
2807 dev_warn(&dd->pdev->dev,
2808 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002809
2810 /* de-initialize the port. */
2811 mtip_deinit_port(dd->port);
2812
2813 /* Disable interrupts on the HBA. */
2814 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2815 dd->mmio + HOST_CTL);
2816 }
2817
2818 del_timer_sync(&dd->port->cmd_timer);
2819
Sam Bradshaw88523a62011-08-30 08:34:26 -06002820 /* Release the IRQ. */
2821 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2822
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002823 /* Stop the bottom half tasklet. */
2824 tasklet_kill(&dd->tasklet);
2825
Sam Bradshaw88523a62011-08-30 08:34:26 -06002826 /* Free the command/command header memory. */
2827 dmam_free_coherent(&dd->pdev->dev,
2828 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2829 dd->port->command_list,
2830 dd->port->command_list_dma);
2831 /* Free the memory allocated for the for structure. */
2832 kfree(dd->port);
2833
2834 return 0;
2835}
2836
2837/*
2838 * Issue a Standby Immediate command to the device.
2839 *
2840 * This function is called by the Block Layer just before the
2841 * system powers off during a shutdown.
2842 *
2843 * @dd Pointer to the driver data structure.
2844 *
2845 * return value
2846 * 0
2847 */
Jens Axboe63166682011-09-27 21:27:43 -06002848static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002849{
2850 /*
2851 * Send standby immediate (E0h) to the drive so that it
2852 * saves its state.
2853 */
2854 mtip_standby_immediate(dd->port);
2855
2856 return 0;
2857}
2858
2859/*
2860 * Suspend function
2861 *
2862 * This function is called by the Block Layer just before the
2863 * system hibernates.
2864 *
2865 * @dd Pointer to the driver data structure.
2866 *
2867 * return value
2868 * 0 Suspend was successful
2869 * -EFAULT Suspend was not successful
2870 */
Jens Axboe63166682011-09-27 21:27:43 -06002871static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002872{
2873 /*
2874 * Send standby immediate (E0h) to the drive
2875 * so that it saves its state.
2876 */
2877 if (mtip_standby_immediate(dd->port) != 0) {
2878 dev_err(&dd->pdev->dev,
2879 "Failed standby-immediate command\n");
2880 return -EFAULT;
2881 }
2882
2883 /* Disable interrupts on the HBA.*/
2884 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2885 dd->mmio + HOST_CTL);
2886 mtip_deinit_port(dd->port);
2887
2888 return 0;
2889}
2890
2891/*
2892 * Resume function
2893 *
2894 * This function is called by the Block Layer as the
2895 * system resumes.
2896 *
2897 * @dd Pointer to the driver data structure.
2898 *
2899 * return value
2900 * 0 Resume was successful
2901 * -EFAULT Resume was not successful
2902 */
Jens Axboe63166682011-09-27 21:27:43 -06002903static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002904{
2905 /* Perform any needed hardware setup steps */
2906 hba_setup(dd);
2907
2908 /* Reset the HBA */
2909 if (mtip_hba_reset(dd) != 0) {
2910 dev_err(&dd->pdev->dev,
2911 "Unable to reset the HBA\n");
2912 return -EFAULT;
2913 }
2914
2915 /*
2916 * Enable the port, DMA engine, and FIS reception specific
2917 * h/w in controller.
2918 */
2919 mtip_init_port(dd->port);
2920 mtip_start_port(dd->port);
2921
2922 /* Enable interrupts on the HBA.*/
2923 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2924 dd->mmio + HOST_CTL);
2925
2926 return 0;
2927}
2928
2929/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002930 * Helper function for reusing disk name
2931 * upon hot insertion.
2932 */
2933static int rssd_disk_name_format(char *prefix,
2934 int index,
2935 char *buf,
2936 int buflen)
2937{
2938 const int base = 'z' - 'a' + 1;
2939 char *begin = buf + strlen(prefix);
2940 char *end = buf + buflen;
2941 char *p;
2942 int unit;
2943
2944 p = end - 1;
2945 *p = '\0';
2946 unit = base;
2947 do {
2948 if (p == begin)
2949 return -EINVAL;
2950 *--p = 'a' + (index % unit);
2951 index = (index / unit) - 1;
2952 } while (index >= 0);
2953
2954 memmove(begin, p, end - p);
2955 memcpy(buf, prefix, strlen(prefix));
2956
2957 return 0;
2958}
2959
2960/*
2961 * Block layer IOCTL handler.
2962 *
2963 * @dev Pointer to the block_device structure.
2964 * @mode ignored
2965 * @cmd IOCTL command passed from the user application.
2966 * @arg Argument passed from the user application.
2967 *
2968 * return value
2969 * 0 IOCTL completed successfully.
2970 * -ENOTTY IOCTL not supported or invalid driver data
2971 * structure pointer.
2972 */
2973static int mtip_block_ioctl(struct block_device *dev,
2974 fmode_t mode,
2975 unsigned cmd,
2976 unsigned long arg)
2977{
2978 struct driver_data *dd = dev->bd_disk->private_data;
2979
2980 if (!capable(CAP_SYS_ADMIN))
2981 return -EACCES;
2982
2983 if (!dd)
2984 return -ENOTTY;
2985
Asai Thambi S P45038362012-04-09 08:35:38 +02002986 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
2987 return -ENOTTY;
2988
Sam Bradshaw88523a62011-08-30 08:34:26 -06002989 switch (cmd) {
2990 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002991 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002992 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06002993 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002994 }
2995}
2996
Jens Axboe16d02c02011-09-27 15:50:01 -06002997#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06002998/*
2999 * Block layer compat IOCTL handler.
3000 *
3001 * @dev Pointer to the block_device structure.
3002 * @mode ignored
3003 * @cmd IOCTL command passed from the user application.
3004 * @arg Argument passed from the user application.
3005 *
3006 * return value
3007 * 0 IOCTL completed successfully.
3008 * -ENOTTY IOCTL not supported or invalid driver data
3009 * structure pointer.
3010 */
3011static int mtip_block_compat_ioctl(struct block_device *dev,
3012 fmode_t mode,
3013 unsigned cmd,
3014 unsigned long arg)
3015{
3016 struct driver_data *dd = dev->bd_disk->private_data;
3017
3018 if (!capable(CAP_SYS_ADMIN))
3019 return -EACCES;
3020
3021 if (!dd)
3022 return -ENOTTY;
3023
Asai Thambi S P45038362012-04-09 08:35:38 +02003024 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
3025 return -ENOTTY;
3026
Sam Bradshaw88523a62011-08-30 08:34:26 -06003027 switch (cmd) {
3028 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003029 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003030 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003031 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003032 ide_task_request_t req_task;
3033 int compat_tasksize, outtotal, ret;
3034
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003035 compat_tasksize =
3036 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003037
3038 compat_req_task =
3039 (struct mtip_compat_ide_task_request_s __user *) arg;
3040
3041 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003042 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003043 return -EFAULT;
3044
3045 if (get_user(req_task.out_size, &compat_req_task->out_size))
3046 return -EFAULT;
3047
3048 if (get_user(req_task.in_size, &compat_req_task->in_size))
3049 return -EFAULT;
3050
3051 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3052
3053 ret = exec_drive_taskfile(dd, (void __user *) arg,
3054 &req_task, outtotal);
3055
3056 if (copy_to_user((void __user *) arg, &req_task,
3057 compat_tasksize -
3058 (2 * sizeof(compat_long_t))))
3059 return -EFAULT;
3060
3061 if (put_user(req_task.out_size, &compat_req_task->out_size))
3062 return -EFAULT;
3063
3064 if (put_user(req_task.in_size, &compat_req_task->in_size))
3065 return -EFAULT;
3066
3067 return ret;
3068 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003069 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003070 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003071 }
3072}
Jens Axboe16d02c02011-09-27 15:50:01 -06003073#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003074
3075/*
3076 * Obtain the geometry of the device.
3077 *
3078 * You may think that this function is obsolete, but some applications,
3079 * fdisk for example still used CHS values. This function describes the
3080 * device as having 224 heads and 56 sectors per cylinder. These values are
3081 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3082 * partition is described in terms of a start and end cylinder this means
3083 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3084 * affects performance.
3085 *
3086 * @dev Pointer to the block_device strucutre.
3087 * @geo Pointer to a hd_geometry structure.
3088 *
3089 * return value
3090 * 0 Operation completed successfully.
3091 * -ENOTTY An error occurred while reading the drive capacity.
3092 */
3093static int mtip_block_getgeo(struct block_device *dev,
3094 struct hd_geometry *geo)
3095{
3096 struct driver_data *dd = dev->bd_disk->private_data;
3097 sector_t capacity;
3098
3099 if (!dd)
3100 return -ENOTTY;
3101
3102 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3103 dev_warn(&dd->pdev->dev,
3104 "Could not get drive capacity.\n");
3105 return -ENOTTY;
3106 }
3107
3108 geo->heads = 224;
3109 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003110 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003111 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003112 return 0;
3113}
3114
3115/*
3116 * Block device operation function.
3117 *
3118 * This structure contains pointers to the functions required by the block
3119 * layer.
3120 */
3121static const struct block_device_operations mtip_block_ops = {
3122 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003123#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003124 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003125#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003126 .getgeo = mtip_block_getgeo,
3127 .owner = THIS_MODULE
3128};
3129
3130/*
3131 * Block layer make request function.
3132 *
3133 * This function is called by the kernel to process a BIO for
3134 * the P320 device.
3135 *
3136 * @queue Pointer to the request queue. Unused other than to obtain
3137 * the driver data structure.
3138 * @bio Pointer to the BIO.
3139 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003140 */
Jens Axboea71f4832011-11-05 08:36:21 +01003141static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003142{
3143 struct driver_data *dd = queue->queuedata;
3144 struct scatterlist *sg;
3145 struct bio_vec *bvec;
3146 int nents = 0;
3147 int tag = 0;
3148
Asai Thambi S P45038362012-04-09 08:35:38 +02003149 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3150 bio_endio(bio, -ENXIO);
3151 return;
3152 }
3153
Sam Bradshaw88523a62011-08-30 08:34:26 -06003154 if (unlikely(!bio_has_data(bio))) {
3155 blk_queue_flush(queue, 0);
3156 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003157 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003158 }
3159
Sam Bradshaw88523a62011-08-30 08:34:26 -06003160 sg = mtip_hw_get_scatterlist(dd, &tag);
3161 if (likely(sg != NULL)) {
3162 blk_queue_bounce(queue, &bio);
3163
3164 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3165 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003166 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003167 bio_io_error(bio);
3168 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003169 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003170 }
3171
3172 /* Create the scatter list for this bio. */
3173 bio_for_each_segment(bvec, bio, nents) {
3174 sg_set_page(&sg[nents],
3175 bvec->bv_page,
3176 bvec->bv_len,
3177 bvec->bv_offset);
3178 }
3179
3180 /* Issue the read/write. */
3181 mtip_hw_submit_io(dd,
3182 bio->bi_sector,
3183 bio_sectors(bio),
3184 nents,
3185 tag,
3186 bio_endio,
3187 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003188 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003189 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003190 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003191}
3192
3193/*
3194 * Block layer initialization function.
3195 *
3196 * This function is called once by the PCI layer for each P320
3197 * device that is connected to the system.
3198 *
3199 * @dd Pointer to the driver data structure.
3200 *
3201 * return value
3202 * 0 on success else an error code.
3203 */
Jens Axboe63166682011-09-27 21:27:43 -06003204static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003205{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003206 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003207 sector_t capacity;
3208 unsigned int index = 0;
3209 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003210 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003211
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003212 if (dd->disk)
3213 goto skip_create_disk; /* hw init done, before rebuild */
3214
Sam Bradshaw88523a62011-08-30 08:34:26 -06003215 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003216 wait_for_rebuild = mtip_hw_init(dd);
3217 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003218 dev_err(&dd->pdev->dev,
3219 "Protocol layer initialization failed\n");
3220 rv = -EINVAL;
3221 goto protocol_init_error;
3222 }
3223
Sam Bradshaw88523a62011-08-30 08:34:26 -06003224 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3225 if (dd->disk == NULL) {
3226 dev_err(&dd->pdev->dev,
3227 "Unable to allocate gendisk structure\n");
3228 rv = -EINVAL;
3229 goto alloc_disk_error;
3230 }
3231
3232 /* Generate the disk name, implemented same as in sd.c */
3233 do {
3234 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3235 goto ida_get_error;
3236
3237 spin_lock(&rssd_index_lock);
3238 rv = ida_get_new(&rssd_index_ida, &index);
3239 spin_unlock(&rssd_index_lock);
3240 } while (rv == -EAGAIN);
3241
3242 if (rv)
3243 goto ida_get_error;
3244
3245 rv = rssd_disk_name_format("rssd",
3246 index,
3247 dd->disk->disk_name,
3248 DISK_NAME_LEN);
3249 if (rv)
3250 goto disk_index_error;
3251
3252 dd->disk->driverfs_dev = &dd->pdev->dev;
3253 dd->disk->major = dd->major;
3254 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3255 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003256 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003257 dd->index = index;
3258
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003259 /*
3260 * if rebuild pending, start the service thread, and delay the block
3261 * queue creation and add_disk()
3262 */
3263 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3264 goto start_service_thread;
3265
3266skip_create_disk:
3267 /* Allocate the request queue. */
3268 dd->queue = blk_alloc_queue(GFP_KERNEL);
3269 if (dd->queue == NULL) {
3270 dev_err(&dd->pdev->dev,
3271 "Unable to allocate request queue\n");
3272 rv = -ENOMEM;
3273 goto block_queue_alloc_init_error;
3274 }
3275
3276 /* Attach our request function to the request queue. */
3277 blk_queue_make_request(dd->queue, mtip_make_request);
3278
3279 dd->disk->queue = dd->queue;
3280 dd->queue->queuedata = dd;
3281
3282 /* Set device limits. */
3283 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3284 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3285 blk_queue_physical_block_size(dd->queue, 4096);
3286 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003287 /*
3288 * write back cache is not supported in the device. FUA depends on
3289 * write back cache support, hence setting flush support to zero.
3290 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003291 blk_queue_flush(dd->queue, 0);
3292
Sam Bradshaw88523a62011-08-30 08:34:26 -06003293 /* Set the capacity of the device in 512 byte sectors. */
3294 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3295 dev_warn(&dd->pdev->dev,
3296 "Could not read drive capacity\n");
3297 rv = -EIO;
3298 goto read_capacity_error;
3299 }
3300 set_capacity(dd->disk, capacity);
3301
3302 /* Enable the block device and add it to /dev */
3303 add_disk(dd->disk);
3304
3305 /*
3306 * Now that the disk is active, initialize any sysfs attributes
3307 * managed by the protocol layer.
3308 */
3309 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3310 if (kobj) {
3311 mtip_hw_sysfs_init(dd, kobj);
3312 kobject_put(kobj);
3313 }
3314
Asai Thambi S P45038362012-04-09 08:35:38 +02003315 if (dd->mtip_svc_handler) {
3316 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003317 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003318 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003319
3320start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003321 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3322
3323 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3324 dd, thd_name);
3325
3326 if (IS_ERR(dd->mtip_svc_handler)) {
3327 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3328 dd->mtip_svc_handler = NULL;
3329 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003330 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003331 }
3332
Asai Thambi S P45038362012-04-09 08:35:38 +02003333 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3334 rv = wait_for_rebuild;
3335
Sam Bradshaw88523a62011-08-30 08:34:26 -06003336 return rv;
3337
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003338kthread_run_error:
3339 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003340 del_gendisk(dd->disk);
3341
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003342read_capacity_error:
3343 blk_cleanup_queue(dd->queue);
3344
3345block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003346disk_index_error:
3347 spin_lock(&rssd_index_lock);
3348 ida_remove(&rssd_index_ida, index);
3349 spin_unlock(&rssd_index_lock);
3350
3351ida_get_error:
3352 put_disk(dd->disk);
3353
3354alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003355 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003356
3357protocol_init_error:
3358 return rv;
3359}
3360
3361/*
3362 * Block layer deinitialization function.
3363 *
3364 * Called by the PCI layer as each P320 device is removed.
3365 *
3366 * @dd Pointer to the driver data structure.
3367 *
3368 * return value
3369 * 0
3370 */
Jens Axboe63166682011-09-27 21:27:43 -06003371static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003372{
3373 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003374
3375 if (dd->mtip_svc_handler) {
3376 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3377 wake_up_interruptible(&dd->port->svc_wait);
3378 kthread_stop(dd->mtip_svc_handler);
3379 }
3380
Sam Bradshaw88523a62011-08-30 08:34:26 -06003381 /* Clean up the sysfs attributes managed by the protocol layer. */
Asai Thambi S P45038362012-04-09 08:35:38 +02003382 if (test_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag)) {
3383 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3384 if (kobj) {
3385 mtip_hw_sysfs_exit(dd, kobj);
3386 kobject_put(kobj);
3387 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003388 }
3389
3390 /*
3391 * Delete our gendisk structure. This also removes the device
3392 * from /dev
3393 */
3394 del_gendisk(dd->disk);
3395 blk_cleanup_queue(dd->queue);
3396 dd->disk = NULL;
3397 dd->queue = NULL;
3398
3399 /* De-initialize the protocol layer. */
3400 mtip_hw_exit(dd);
3401
3402 return 0;
3403}
3404
3405/*
3406 * Function called by the PCI layer when just before the
3407 * machine shuts down.
3408 *
3409 * If a protocol layer shutdown function is present it will be called
3410 * by this function.
3411 *
3412 * @dd Pointer to the driver data structure.
3413 *
3414 * return value
3415 * 0
3416 */
Jens Axboe63166682011-09-27 21:27:43 -06003417static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003418{
3419 dev_info(&dd->pdev->dev,
3420 "Shutting down %s ...\n", dd->disk->disk_name);
3421
3422 /* Delete our gendisk structure, and cleanup the blk queue. */
3423 del_gendisk(dd->disk);
3424 blk_cleanup_queue(dd->queue);
3425 dd->disk = NULL;
3426 dd->queue = NULL;
3427
3428 mtip_hw_shutdown(dd);
3429 return 0;
3430}
3431
Jens Axboe63166682011-09-27 21:27:43 -06003432static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003433{
3434 dev_info(&dd->pdev->dev,
3435 "Suspending %s ...\n", dd->disk->disk_name);
3436 mtip_hw_suspend(dd);
3437 return 0;
3438}
3439
Jens Axboe63166682011-09-27 21:27:43 -06003440static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003441{
3442 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3443 dd->disk->disk_name);
3444 mtip_hw_resume(dd);
3445 return 0;
3446}
3447
3448/*
3449 * Called for each supported PCI device detected.
3450 *
3451 * This function allocates the private data structure, enables the
3452 * PCI device and then calls the block layer initialization function.
3453 *
3454 * return value
3455 * 0 on success else an error code.
3456 */
3457static int mtip_pci_probe(struct pci_dev *pdev,
3458 const struct pci_device_id *ent)
3459{
3460 int rv = 0;
3461 struct driver_data *dd = NULL;
3462
3463 /* Allocate memory for this devices private data. */
3464 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3465 if (dd == NULL) {
3466 dev_err(&pdev->dev,
3467 "Unable to allocate memory for driver data\n");
3468 return -ENOMEM;
3469 }
3470
Sam Bradshaw88523a62011-08-30 08:34:26 -06003471 /* Attach the private data to this PCI device. */
3472 pci_set_drvdata(pdev, dd);
3473
3474 rv = pcim_enable_device(pdev);
3475 if (rv < 0) {
3476 dev_err(&pdev->dev, "Unable to enable device\n");
3477 goto iomap_err;
3478 }
3479
3480 /* Map BAR5 to memory. */
3481 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3482 if (rv < 0) {
3483 dev_err(&pdev->dev, "Unable to map regions\n");
3484 goto iomap_err;
3485 }
3486
3487 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3488 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3489
3490 if (rv) {
3491 rv = pci_set_consistent_dma_mask(pdev,
3492 DMA_BIT_MASK(32));
3493 if (rv) {
3494 dev_warn(&pdev->dev,
3495 "64-bit DMA enable failed\n");
3496 goto setmask_err;
3497 }
3498 }
3499 }
3500
3501 pci_set_master(pdev);
3502
3503 if (pci_enable_msi(pdev)) {
3504 dev_warn(&pdev->dev,
3505 "Unable to enable MSI interrupt.\n");
3506 goto block_initialize_err;
3507 }
3508
3509 /* Copy the info we may need later into the private data structure. */
3510 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003511 dd->instance = instance;
3512 dd->pdev = pdev;
3513
3514 /* Initialize the block layer. */
3515 rv = mtip_block_initialize(dd);
3516 if (rv < 0) {
3517 dev_err(&pdev->dev,
3518 "Unable to initialize block layer\n");
3519 goto block_initialize_err;
3520 }
3521
3522 /*
3523 * Increment the instance count so that each device has a unique
3524 * instance number.
3525 */
3526 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003527 if (rv != MTIP_FTL_REBUILD_MAGIC)
3528 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003529 goto done;
3530
3531block_initialize_err:
3532 pci_disable_msi(pdev);
3533
3534setmask_err:
3535 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3536
3537iomap_err:
3538 kfree(dd);
3539 pci_set_drvdata(pdev, NULL);
3540 return rv;
3541done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003542 return rv;
3543}
3544
3545/*
3546 * Called for each probed device when the device is removed or the
3547 * driver is unloaded.
3548 *
3549 * return value
3550 * None
3551 */
3552static void mtip_pci_remove(struct pci_dev *pdev)
3553{
3554 struct driver_data *dd = pci_get_drvdata(pdev);
3555 int counter = 0;
3556
Asai Thambi S P45038362012-04-09 08:35:38 +02003557 set_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag);
3558
Sam Bradshaw88523a62011-08-30 08:34:26 -06003559 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003560 while (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003561 counter++;
3562 msleep(20);
3563 if (counter == 10) {
3564 /* Cleanup the outstanding commands */
3565 mtip_command_cleanup(dd);
3566 break;
3567 }
3568 }
3569 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003570
3571 /* Clean up the block layer. */
3572 mtip_block_remove(dd);
3573
3574 pci_disable_msi(pdev);
3575
3576 kfree(dd);
3577 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3578}
3579
3580/*
3581 * Called for each probed device when the device is suspended.
3582 *
3583 * return value
3584 * 0 Success
3585 * <0 Error
3586 */
3587static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3588{
3589 int rv = 0;
3590 struct driver_data *dd = pci_get_drvdata(pdev);
3591
3592 if (!dd) {
3593 dev_err(&pdev->dev,
3594 "Driver private datastructure is NULL\n");
3595 return -EFAULT;
3596 }
3597
Asai Thambi S P45038362012-04-09 08:35:38 +02003598 set_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003599
3600 /* Disable ports & interrupts then send standby immediate */
3601 rv = mtip_block_suspend(dd);
3602 if (rv < 0) {
3603 dev_err(&pdev->dev,
3604 "Failed to suspend controller\n");
3605 return rv;
3606 }
3607
3608 /*
3609 * Save the pci config space to pdev structure &
3610 * disable the device
3611 */
3612 pci_save_state(pdev);
3613 pci_disable_device(pdev);
3614
3615 /* Move to Low power state*/
3616 pci_set_power_state(pdev, PCI_D3hot);
3617
3618 return rv;
3619}
3620
3621/*
3622 * Called for each probed device when the device is resumed.
3623 *
3624 * return value
3625 * 0 Success
3626 * <0 Error
3627 */
3628static int mtip_pci_resume(struct pci_dev *pdev)
3629{
3630 int rv = 0;
3631 struct driver_data *dd;
3632
3633 dd = pci_get_drvdata(pdev);
3634 if (!dd) {
3635 dev_err(&pdev->dev,
3636 "Driver private datastructure is NULL\n");
3637 return -EFAULT;
3638 }
3639
3640 /* Move the device to active State */
3641 pci_set_power_state(pdev, PCI_D0);
3642
3643 /* Restore PCI configuration space */
3644 pci_restore_state(pdev);
3645
3646 /* Enable the PCI device*/
3647 rv = pcim_enable_device(pdev);
3648 if (rv < 0) {
3649 dev_err(&pdev->dev,
3650 "Failed to enable card during resume\n");
3651 goto err;
3652 }
3653 pci_set_master(pdev);
3654
3655 /*
3656 * Calls hbaReset, initPort, & startPort function
3657 * then enables interrupts
3658 */
3659 rv = mtip_block_resume(dd);
3660 if (rv < 0)
3661 dev_err(&pdev->dev, "Unable to resume\n");
3662
3663err:
Asai Thambi S P45038362012-04-09 08:35:38 +02003664 clear_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003665
3666 return rv;
3667}
3668
3669/*
3670 * Shutdown routine
3671 *
3672 * return value
3673 * None
3674 */
3675static void mtip_pci_shutdown(struct pci_dev *pdev)
3676{
3677 struct driver_data *dd = pci_get_drvdata(pdev);
3678 if (dd)
3679 mtip_block_shutdown(dd);
3680}
3681
Sam Bradshaw88523a62011-08-30 08:34:26 -06003682/* Table of device ids supported by this driver. */
3683static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3684 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3685 { 0 }
3686};
3687
3688/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003689static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003690 .name = MTIP_DRV_NAME,
3691 .id_table = mtip_pci_tbl,
3692 .probe = mtip_pci_probe,
3693 .remove = mtip_pci_remove,
3694 .suspend = mtip_pci_suspend,
3695 .resume = mtip_pci_resume,
3696 .shutdown = mtip_pci_shutdown,
3697};
3698
3699MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3700
3701/*
3702 * Module initialization function.
3703 *
3704 * Called once when the module is loaded. This function allocates a major
3705 * block device number to the Cyclone devices and registers the PCI layer
3706 * of the driver.
3707 *
3708 * Return value
3709 * 0 on success else error code.
3710 */
3711static int __init mtip_init(void)
3712{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003713 int error;
3714
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3716
3717 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003718 error = register_blkdev(0, MTIP_DRV_NAME);
3719 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003720 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003721 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003722 return -EBUSY;
3723 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003724 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003725
3726 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003727 error = pci_register_driver(&mtip_pci_driver);
3728 if (error)
3729 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3730
3731 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003732}
3733
3734/*
3735 * Module de-initialization function.
3736 *
3737 * Called once when the module is unloaded. This function deallocates
3738 * the major block device number allocated by mtip_init() and
3739 * unregisters the PCI layer of the driver.
3740 *
3741 * Return value
3742 * none
3743 */
3744static void __exit mtip_exit(void)
3745{
3746 /* Release the allocated major block device number. */
3747 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3748
3749 /* Unregister the PCI driver. */
3750 pci_unregister_driver(&mtip_pci_driver);
3751}
3752
3753MODULE_AUTHOR("Micron Technology, Inc");
3754MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3755MODULE_LICENSE("GPL");
3756MODULE_VERSION(MTIP_DRV_VERSION);
3757
3758module_init(mtip_init);
3759module_exit(mtip_exit);