blob: b5d843a02bfade83fb815b06d6aef87a4584671f [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>
39#include "mtip32xx.h"
40
41#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
42#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
43#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
44#define HW_PORT_PRIV_DMA_SZ \
45 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
46
47#define HOST_HSORG 0xFC
48#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
49#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
50#define HSORG_HWREV 0xFF00
51#define HSORG_STYLE 0x8
52#define HSORG_SLOTGROUPS 0x7
53
54#define PORT_COMMAND_ISSUE 0x38
55#define PORT_SDBV 0x7C
56
57#define PORT_OFFSET 0x100
58#define PORT_MEM_SIZE 0x80
59
60#define PORT_IRQ_ERR \
61 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
62 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
63 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
64 PORT_IRQ_OVERFLOW)
65#define PORT_IRQ_LEGACY \
66 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
67#define PORT_IRQ_HANDLED \
68 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
69 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
70 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
71#define DEF_PORT_IRQ \
72 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
73
74/* product numbers */
75#define MTIP_PRODUCT_UNKNOWN 0x00
76#define MTIP_PRODUCT_ASICFPGA 0x11
77
78/* Device instance number, incremented each time a device is probed. */
79static int instance;
80
81/*
82 * Global variable used to hold the major block device number
83 * allocated in mtip_init().
84 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060085static int mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -060086
87static DEFINE_SPINLOCK(rssd_index_lock);
88static DEFINE_IDA(rssd_index_ida);
89
Jens Axboe16d02c02011-09-27 15:50:01 -060090#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060091struct mtip_compat_ide_task_request_s {
92 __u8 io_ports[8];
93 __u8 hob_ports[8];
94 ide_reg_valid_t out_flags;
95 ide_reg_valid_t in_flags;
96 int data_phase;
97 int req_cmd;
98 compat_ulong_t out_size;
99 compat_ulong_t in_size;
100};
Jens Axboe16d02c02011-09-27 15:50:01 -0600101#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600102
Sam Bradshaw88523a62011-08-30 08:34:26 -0600103/*
Jens Axboe63166682011-09-27 21:27:43 -0600104 * This function check_for_surprise_removal is called
105 * while card is removed from the system and it will
106 * read the vendor id from the configration space
107 *
108 * @pdev Pointer to the pci_dev structure.
109 *
110 * return value
111 * true if device removed, else false
112 */
113static bool mtip_check_surprise_removal(struct pci_dev *pdev)
114{
115 u16 vendor_id = 0;
116
117 /* Read the vendorID from the configuration space */
118 pci_read_config_word(pdev, 0x00, &vendor_id);
119 if (vendor_id == 0xFFFF)
120 return true; /* device removed */
121
122 return false; /* device present */
123}
124
125/*
126 * This function is called for clean the pending command in the
127 * command slot during the surprise removal of device and return
128 * error to the upper layer.
129 *
130 * @dd Pointer to the DRIVER_DATA structure.
131 *
132 * return value
133 * None
134 */
135static void mtip_command_cleanup(struct driver_data *dd)
136{
137 int group = 0, commandslot = 0, commandindex = 0;
138 struct mtip_cmd *command;
139 struct mtip_port *port = dd->port;
140
141 for (group = 0; group < 4; group++) {
142 for (commandslot = 0; commandslot < 32; commandslot++) {
143 if (!(port->allocated[group] & (1 << commandslot)))
144 continue;
145
146 commandindex = group << 5 | commandslot;
147 command = &port->commands[commandindex];
148
149 if (atomic_read(&command->active)
150 && (command->async_callback)) {
151 command->async_callback(command->async_data,
152 -ENODEV);
153 command->async_callback = NULL;
154 command->async_data = NULL;
155 }
156
157 dma_unmap_sg(&port->dd->pdev->dev,
158 command->sg,
159 command->scatter_ents,
160 command->direction);
161 }
162 }
163
164 up(&port->cmd_slot);
165
166 atomic_set(&dd->drv_cleanup_done, true);
167}
168
169/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600170 * Obtain an empty command slot.
171 *
172 * This function needs to be reentrant since it could be called
173 * at the same time on multiple CPUs. The allocation of the
174 * command slot must be atomic.
175 *
176 * @port Pointer to the port data structure.
177 *
178 * return value
179 * >= 0 Index of command slot obtained.
180 * -1 No command slots available.
181 */
182static int get_slot(struct mtip_port *port)
183{
184 int slot, i;
185 unsigned int num_command_slots = port->dd->slot_groups * 32;
186
187 /*
188 * Try 10 times, because there is a small race here.
189 * that's ok, because it's still cheaper than a lock.
190 *
191 * Race: Since this section is not protected by lock, same bit
192 * could be chosen by different process contexts running in
193 * different processor. So instead of costly lock, we are going
194 * with loop.
195 */
196 for (i = 0; i < 10; i++) {
197 slot = find_next_zero_bit(port->allocated,
198 num_command_slots, 1);
199 if ((slot < num_command_slots) &&
200 (!test_and_set_bit(slot, port->allocated)))
201 return slot;
202 }
203 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
204
205 if (mtip_check_surprise_removal(port->dd->pdev)) {
206 /* Device not present, clean outstanding commands */
207 mtip_command_cleanup(port->dd);
208 }
209 return -1;
210}
211
212/*
213 * Release a command slot.
214 *
215 * @port Pointer to the port data structure.
216 * @tag Tag of command to release
217 *
218 * return value
219 * None
220 */
221static inline void release_slot(struct mtip_port *port, int tag)
222{
223 smp_mb__before_clear_bit();
224 clear_bit(tag, port->allocated);
225 smp_mb__after_clear_bit();
226}
227
228/*
Jens Axboe63166682011-09-27 21:27:43 -0600229 * Reset the HBA (without sleeping)
230 *
231 * Just like hba_reset, except does not call sleep, so can be
232 * run from interrupt/tasklet context.
233 *
234 * @dd Pointer to the driver data structure.
235 *
236 * return value
237 * 0 The reset was successful.
238 * -1 The HBA Reset bit did not clear.
239 */
240static int hba_reset_nosleep(struct driver_data *dd)
241{
242 unsigned long timeout;
243
244 /* Chip quirk: quiesce any chip function */
245 mdelay(10);
246
247 /* Set the reset bit */
248 writel(HOST_RESET, dd->mmio + HOST_CTL);
249
250 /* Flush */
251 readl(dd->mmio + HOST_CTL);
252
253 /*
254 * Wait 10ms then spin for up to 1 second
255 * waiting for reset acknowledgement
256 */
257 timeout = jiffies + msecs_to_jiffies(1000);
258 mdelay(10);
259 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
260 && time_before(jiffies, timeout))
261 mdelay(1);
262
263 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
264 return -1;
265
266 return 0;
267}
268
269/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600270 * Issue a command to the hardware.
271 *
272 * Set the appropriate bit in the s_active and Command Issue hardware
273 * registers, causing hardware command processing to begin.
274 *
275 * @port Pointer to the port structure.
276 * @tag The tag of the command to be issued.
277 *
278 * return value
279 * None
280 */
281static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
282{
283 unsigned long flags = 0;
284
285 atomic_set(&port->commands[tag].active, 1);
286
287 spin_lock_irqsave(&port->cmd_issue_lock, flags);
288
289 writel((1 << MTIP_TAG_BIT(tag)),
290 port->s_active[MTIP_TAG_INDEX(tag)]);
291 writel((1 << MTIP_TAG_BIT(tag)),
292 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
293
294 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
295}
296
297/*
Jens Axboe63166682011-09-27 21:27:43 -0600298 * Enable/disable the reception of FIS
299 *
300 * @port Pointer to the port data structure
301 * @enable 1 to enable, 0 to disable
302 *
303 * return value
304 * Previous state: 1 enabled, 0 disabled
305 */
306static int mtip_enable_fis(struct mtip_port *port, int enable)
307{
308 u32 tmp;
309
310 /* enable FIS reception */
311 tmp = readl(port->mmio + PORT_CMD);
312 if (enable)
313 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
314 else
315 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
316
317 /* Flush */
318 readl(port->mmio + PORT_CMD);
319
320 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
321}
322
323/*
324 * Enable/disable the DMA engine
325 *
326 * @port Pointer to the port data structure
327 * @enable 1 to enable, 0 to disable
328 *
329 * return value
330 * Previous state: 1 enabled, 0 disabled.
331 */
332static int mtip_enable_engine(struct mtip_port *port, int enable)
333{
334 u32 tmp;
335
336 /* enable FIS reception */
337 tmp = readl(port->mmio + PORT_CMD);
338 if (enable)
339 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
340 else
341 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
342
343 readl(port->mmio + PORT_CMD);
344 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
345}
346
347/*
348 * Enables the port DMA engine and FIS reception.
349 *
350 * return value
351 * None
352 */
353static inline void mtip_start_port(struct mtip_port *port)
354{
355 /* Enable FIS reception */
356 mtip_enable_fis(port, 1);
357
358 /* Enable the DMA engine */
359 mtip_enable_engine(port, 1);
360}
361
362/*
363 * Deinitialize a port by disabling port interrupts, the DMA engine,
364 * and FIS reception.
365 *
366 * @port Pointer to the port structure
367 *
368 * return value
369 * None
370 */
371static inline void mtip_deinit_port(struct mtip_port *port)
372{
373 /* Disable interrupts on this port */
374 writel(0, port->mmio + PORT_IRQ_MASK);
375
376 /* Disable the DMA engine */
377 mtip_enable_engine(port, 0);
378
379 /* Disable FIS reception */
380 mtip_enable_fis(port, 0);
381}
382
383/*
384 * Initialize a port.
385 *
386 * This function deinitializes the port by calling mtip_deinit_port() and
387 * then initializes it by setting the command header and RX FIS addresses,
388 * clearing the SError register and any pending port interrupts before
389 * re-enabling the default set of port interrupts.
390 *
391 * @port Pointer to the port structure.
392 *
393 * return value
394 * None
395 */
396static void mtip_init_port(struct mtip_port *port)
397{
398 int i;
399 mtip_deinit_port(port);
400
401 /* Program the command list base and FIS base addresses */
402 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
403 writel((port->command_list_dma >> 16) >> 16,
404 port->mmio + PORT_LST_ADDR_HI);
405 writel((port->rxfis_dma >> 16) >> 16,
406 port->mmio + PORT_FIS_ADDR_HI);
407 }
408
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100409 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600410 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100411 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600412
413 /* Clear SError */
414 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
415
416 /* reset the completed registers.*/
417 for (i = 0; i < port->dd->slot_groups; i++)
418 writel(0xFFFFFFFF, port->completed[i]);
419
420 /* Clear any pending interrupts for this port */
421 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
422
423 /* Enable port interrupts */
424 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
425}
426
427/*
428 * Restart a port
429 *
430 * @port Pointer to the port data structure.
431 *
432 * return value
433 * None
434 */
435static void mtip_restart_port(struct mtip_port *port)
436{
437 unsigned long timeout;
438
439 /* Disable the DMA engine */
440 mtip_enable_engine(port, 0);
441
442 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
443 timeout = jiffies + msecs_to_jiffies(500);
444 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
445 && time_before(jiffies, timeout))
446 ;
447
448 /*
449 * Chip quirk: escalate to hba reset if
450 * PxCMD.CR not clear after 500 ms
451 */
452 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
453 dev_warn(&port->dd->pdev->dev,
454 "PxCMD.CR not clear, escalating reset\n");
455
456 if (hba_reset_nosleep(port->dd))
457 dev_err(&port->dd->pdev->dev,
458 "HBA reset escalation failed.\n");
459
460 /* 30 ms delay before com reset to quiesce chip */
461 mdelay(30);
462 }
463
464 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
465
466 /* Set PxSCTL.DET */
467 writel(readl(port->mmio + PORT_SCR_CTL) |
468 1, port->mmio + PORT_SCR_CTL);
469 readl(port->mmio + PORT_SCR_CTL);
470
471 /* Wait 1 ms to quiesce chip function */
472 timeout = jiffies + msecs_to_jiffies(1);
473 while (time_before(jiffies, timeout))
474 ;
475
476 /* Clear PxSCTL.DET */
477 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
478 port->mmio + PORT_SCR_CTL);
479 readl(port->mmio + PORT_SCR_CTL);
480
481 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
482 timeout = jiffies + msecs_to_jiffies(500);
483 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
484 && time_before(jiffies, timeout))
485 ;
486
487 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
488 dev_warn(&port->dd->pdev->dev,
489 "COM reset failed\n");
490
491 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
492 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
493
494 /* Enable the DMA engine */
495 mtip_enable_engine(port, 1);
496}
497
498/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600499 * Called periodically to see if any read/write commands are
500 * taking too long to complete.
501 *
502 * @data Pointer to the PORT data structure.
503 *
504 * return value
505 * None
506 */
Jens Axboe63166682011-09-27 21:27:43 -0600507static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600508{
509 struct mtip_port *port = (struct mtip_port *) data;
510 struct host_to_dev_fis *fis;
511 struct mtip_cmd *command;
512 int tag, cmdto_cnt = 0;
513 unsigned int bit, group;
514 unsigned int num_command_slots = port->dd->slot_groups * 32;
515
516 if (unlikely(!port))
517 return;
518
519 if (atomic_read(&port->dd->resumeflag) == true) {
520 mod_timer(&port->cmd_timer,
521 jiffies + msecs_to_jiffies(30000));
522 return;
523 }
524
525 for (tag = 0; tag < num_command_slots; tag++) {
526 /*
527 * Skip internal command slot as it has
528 * its own timeout mechanism
529 */
530 if (tag == MTIP_TAG_INTERNAL)
531 continue;
532
533 if (atomic_read(&port->commands[tag].active) &&
534 (time_after(jiffies, port->commands[tag].comp_time))) {
535 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100536 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600537
538 command = &port->commands[tag];
539 fis = (struct host_to_dev_fis *) command->command;
540
541 dev_warn(&port->dd->pdev->dev,
542 "Timeout for command tag %d\n", tag);
543
544 cmdto_cnt++;
545 if (cmdto_cnt == 1)
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100546 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600547
548 /*
549 * Clear the completed bit. This should prevent
550 * any interrupt handlers from trying to retire
551 * the command.
552 */
553 writel(1 << bit, port->completed[group]);
554
555 /* Call the async completion callback. */
556 if (likely(command->async_callback))
557 command->async_callback(command->async_data,
558 -EIO);
559 command->async_callback = NULL;
560 command->comp_func = NULL;
561
562 /* Unmap the DMA scatter list entries */
563 dma_unmap_sg(&port->dd->pdev->dev,
564 command->sg,
565 command->scatter_ents,
566 command->direction);
567
568 /*
569 * Clear the allocated bit and active tag for the
570 * command.
571 */
572 atomic_set(&port->commands[tag].active, 0);
573 release_slot(port, tag);
574
575 up(&port->cmd_slot);
576 }
577 }
578
579 if (cmdto_cnt) {
580 dev_warn(&port->dd->pdev->dev,
581 "%d commands timed out: restarting port",
582 cmdto_cnt);
583 mtip_restart_port(port);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100584 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
585 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600586 }
587
588 /* Restart the timer */
589 mod_timer(&port->cmd_timer,
590 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
591}
592
593/*
594 * IO completion function.
595 *
596 * This completion function is called by the driver ISR when a
597 * command that was issued by the kernel completes. It first calls the
598 * asynchronous completion function which normally calls back into the block
599 * layer passing the asynchronous callback data, then unmaps the
600 * scatter list associated with the completed command, and finally
601 * clears the allocated bit associated with the completed command.
602 *
603 * @port Pointer to the port data structure.
604 * @tag Tag of the command.
605 * @data Pointer to driver_data.
606 * @status Completion status.
607 *
608 * return value
609 * None
610 */
611static void mtip_async_complete(struct mtip_port *port,
612 int tag,
613 void *data,
614 int status)
615{
616 struct mtip_cmd *command;
617 struct driver_data *dd = data;
618 int cb_status = status ? -EIO : 0;
619
620 if (unlikely(!dd) || unlikely(!port))
621 return;
622
623 command = &port->commands[tag];
624
625 if (unlikely(status == PORT_IRQ_TF_ERR)) {
626 dev_warn(&port->dd->pdev->dev,
627 "Command tag %d failed due to TFE\n", tag);
628 }
629
630 /* Upper layer callback */
631 if (likely(command->async_callback))
632 command->async_callback(command->async_data, cb_status);
633
634 command->async_callback = NULL;
635 command->comp_func = NULL;
636
637 /* Unmap the DMA scatter list entries */
638 dma_unmap_sg(&dd->pdev->dev,
639 command->sg,
640 command->scatter_ents,
641 command->direction);
642
643 /* Clear the allocated and active bits for the command */
644 atomic_set(&port->commands[tag].active, 0);
645 release_slot(port, tag);
646
647 up(&port->cmd_slot);
648}
649
650/*
651 * Internal command completion callback function.
652 *
653 * This function is normally called by the driver ISR when an internal
654 * command completed. This function signals the command completion by
655 * calling complete().
656 *
657 * @port Pointer to the port data structure.
658 * @tag Tag of the command that has completed.
659 * @data Pointer to a completion structure.
660 * @status Completion status.
661 *
662 * return value
663 * None
664 */
665static void mtip_completion(struct mtip_port *port,
666 int tag,
667 void *data,
668 int status)
669{
670 struct mtip_cmd *command = &port->commands[tag];
671 struct completion *waiting = data;
672 if (unlikely(status == PORT_IRQ_TF_ERR))
673 dev_warn(&port->dd->pdev->dev,
674 "Internal command %d completed with TFE\n", tag);
675
676 command->async_callback = NULL;
677 command->comp_func = NULL;
678
679 complete(waiting);
680}
681
682/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600683 * Helper function for tag logging
684 */
685static void print_tags(struct driver_data *dd,
686 char *msg,
687 unsigned long *tagbits)
688{
689 unsigned int tag, count = 0;
690
691 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
692 if (test_bit(tag, tagbits))
693 count++;
694 }
695 if (count)
696 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
697}
698
699/*
700 * Handle an error.
701 *
702 * @dd Pointer to the DRIVER_DATA structure.
703 *
704 * return value
705 * None
706 */
707static void mtip_handle_tfe(struct driver_data *dd)
708{
709 int group, tag, bit, reissue;
710 struct mtip_port *port;
711 struct mtip_cmd *command;
712 u32 completed;
713 struct host_to_dev_fis *fis;
714 unsigned long tagaccum[SLOTBITS_IN_LONGS];
715
716 dev_warn(&dd->pdev->dev, "Taskfile error\n");
717
718 port = dd->port;
719
720 /* Stop the timer to prevent command timeouts. */
721 del_timer(&port->cmd_timer);
722
723 /* Set eh_active */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100724 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600725
726 /* Loop through all the groups */
727 for (group = 0; group < dd->slot_groups; group++) {
728 completed = readl(port->completed[group]);
729
730 /* clear completed status register in the hardware.*/
731 writel(completed, port->completed[group]);
732
733 /* clear the tag accumulator */
734 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
735
736 /* Process successfully completed commands */
737 for (bit = 0; bit < 32 && completed; bit++) {
738 if (!(completed & (1<<bit)))
739 continue;
740 tag = (group << 5) + bit;
741
742 /* Skip the internal command slot */
743 if (tag == MTIP_TAG_INTERNAL)
744 continue;
745
746 command = &port->commands[tag];
747 if (likely(command->comp_func)) {
748 set_bit(tag, tagaccum);
749 atomic_set(&port->commands[tag].active, 0);
750 command->comp_func(port,
751 tag,
752 command->comp_data,
753 0);
754 } else {
755 dev_err(&port->dd->pdev->dev,
756 "Missing completion func for tag %d",
757 tag);
758 if (mtip_check_surprise_removal(dd->pdev)) {
759 mtip_command_cleanup(dd);
760 /* don't proceed further */
761 return;
762 }
763 }
764 }
765 }
766 print_tags(dd, "TFE tags completed:", tagaccum);
767
768 /* Restart the port */
769 mdelay(20);
770 mtip_restart_port(port);
771
772 /* clear the tag accumulator */
773 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
774
775 /* Loop through all the groups */
776 for (group = 0; group < dd->slot_groups; group++) {
777 for (bit = 0; bit < 32; bit++) {
778 reissue = 1;
779 tag = (group << 5) + bit;
780
781 /* If the active bit is set re-issue the command */
782 if (atomic_read(&port->commands[tag].active) == 0)
783 continue;
784
785 fis = (struct host_to_dev_fis *)
786 port->commands[tag].command;
787
788 /* Should re-issue? */
789 if (tag == MTIP_TAG_INTERNAL ||
790 fis->command == ATA_CMD_SET_FEATURES)
791 reissue = 0;
792
793 /*
794 * First check if this command has
795 * exceeded its retries.
796 */
797 if (reissue &&
798 (port->commands[tag].retries-- > 0)) {
799
800 set_bit(tag, tagaccum);
801
802 /* Update the timeout value. */
803 port->commands[tag].comp_time =
804 jiffies + msecs_to_jiffies(
805 MTIP_NCQ_COMMAND_TIMEOUT_MS);
806 /* Re-issue the command. */
807 mtip_issue_ncq_command(port, tag);
808
809 continue;
810 }
811
812 /* Retire a command that will not be reissued */
813 dev_warn(&port->dd->pdev->dev,
814 "retiring tag %d\n", tag);
815 atomic_set(&port->commands[tag].active, 0);
816
817 if (port->commands[tag].comp_func)
818 port->commands[tag].comp_func(
819 port,
820 tag,
821 port->commands[tag].comp_data,
822 PORT_IRQ_TF_ERR);
823 else
824 dev_warn(&port->dd->pdev->dev,
825 "Bad completion for tag %d\n",
826 tag);
827 }
828 }
829 print_tags(dd, "TFE tags reissued:", tagaccum);
830
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100831 /* clear eh_active */
832 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
833 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600834
835 mod_timer(&port->cmd_timer,
836 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
837}
838
839/*
840 * Handle a set device bits interrupt
841 */
842static inline void mtip_process_sdbf(struct driver_data *dd)
843{
844 struct mtip_port *port = dd->port;
845 int group, tag, bit;
846 u32 completed;
847 struct mtip_cmd *command;
848
849 /* walk all bits in all slot groups */
850 for (group = 0; group < dd->slot_groups; group++) {
851 completed = readl(port->completed[group]);
852
853 /* clear completed status register in the hardware.*/
854 writel(completed, port->completed[group]);
855
856 /* Process completed commands. */
857 for (bit = 0;
858 (bit < 32) && completed;
859 bit++, completed >>= 1) {
860 if (completed & 0x01) {
861 tag = (group << 5) | bit;
862
863 /* skip internal command slot. */
864 if (unlikely(tag == MTIP_TAG_INTERNAL))
865 continue;
866
867 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600868 /* make internal callback */
869 if (likely(command->comp_func)) {
870 command->comp_func(
871 port,
872 tag,
873 command->comp_data,
874 0);
875 } else {
876 dev_warn(&dd->pdev->dev,
877 "Null completion "
878 "for tag %d",
879 tag);
880
881 if (mtip_check_surprise_removal(
882 dd->pdev)) {
883 mtip_command_cleanup(dd);
884 return;
885 }
886 }
887 }
888 }
889 }
890}
891
892/*
893 * Process legacy pio and d2h interrupts
894 */
895static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
896{
897 struct mtip_port *port = dd->port;
898 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
899
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100900 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
901 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600902 & (1 << MTIP_TAG_INTERNAL))) {
903 if (cmd->comp_func) {
904 cmd->comp_func(port,
905 MTIP_TAG_INTERNAL,
906 cmd->comp_data,
907 0);
908 return;
909 }
910 }
911
912 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
913
914 return;
915}
916
917/*
918 * Demux and handle errors
919 */
920static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
921{
922 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
923 mtip_handle_tfe(dd);
924
925 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
926 dev_warn(&dd->pdev->dev,
927 "Clearing PxSERR.DIAG.x\n");
928 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
929 }
930
931 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
932 dev_warn(&dd->pdev->dev,
933 "Clearing PxSERR.DIAG.n\n");
934 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
935 }
936
937 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
938 dev_warn(&dd->pdev->dev,
939 "Port stat errors %x unhandled\n",
940 (port_stat & ~PORT_IRQ_HANDLED));
941 }
942}
943
944static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
945{
946 struct driver_data *dd = (struct driver_data *) data;
947 struct mtip_port *port = dd->port;
948 u32 hba_stat, port_stat;
949 int rv = IRQ_NONE;
950
951 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
952 if (hba_stat) {
953 rv = IRQ_HANDLED;
954
955 /* Acknowledge the interrupt status on the port.*/
956 port_stat = readl(port->mmio + PORT_IRQ_STAT);
957 writel(port_stat, port->mmio + PORT_IRQ_STAT);
958
959 /* Demux port status */
960 if (likely(port_stat & PORT_IRQ_SDB_FIS))
961 mtip_process_sdbf(dd);
962
963 if (unlikely(port_stat & PORT_IRQ_ERR)) {
964 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
965 mtip_command_cleanup(dd);
966 /* don't proceed further */
967 return IRQ_HANDLED;
968 }
969
970 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
971 }
972
973 if (unlikely(port_stat & PORT_IRQ_LEGACY))
974 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
975 }
976
977 /* acknowledge interrupt */
978 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
979
980 return rv;
981}
982
983/*
984 * Wrapper for mtip_handle_irq
985 * (ignores return code)
986 */
987static void mtip_tasklet(unsigned long data)
988{
989 mtip_handle_irq((struct driver_data *) data);
990}
991
992/*
993 * HBA interrupt subroutine.
994 *
995 * @irq IRQ number.
996 * @instance Pointer to the driver data structure.
997 *
998 * return value
999 * IRQ_HANDLED A HBA interrupt was pending and handled.
1000 * IRQ_NONE This interrupt was not for the HBA.
1001 */
1002static irqreturn_t mtip_irq_handler(int irq, void *instance)
1003{
1004 struct driver_data *dd = instance;
1005 tasklet_schedule(&dd->tasklet);
1006 return IRQ_HANDLED;
1007}
1008
1009static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1010{
1011 atomic_set(&port->commands[tag].active, 1);
1012 writel(1 << MTIP_TAG_BIT(tag),
1013 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1014}
1015
1016/*
1017 * Wait for port to quiesce
1018 *
1019 * @port Pointer to port data structure
1020 * @timeout Max duration to wait (ms)
1021 *
1022 * return value
1023 * 0 Success
1024 * -EBUSY Commands still active
1025 */
1026static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1027{
1028 unsigned long to;
1029 unsigned int n, active;
1030
1031 to = jiffies + msecs_to_jiffies(timeout);
1032 do {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001033 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags)) {
1034 msleep(20);
1035 continue; /* svc thd is actively issuing commands */
1036 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001037 /*
1038 * Ignore s_active bit 0 of array element 0.
1039 * This bit will always be set
1040 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001041 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001042 for (n = 1; n < port->dd->slot_groups; n++)
1043 active |= readl(port->s_active[n]);
1044
1045 if (!active)
1046 break;
1047
1048 msleep(20);
1049 } while (time_before(jiffies, to));
1050
1051 return active ? -EBUSY : 0;
1052}
1053
1054/*
1055 * Execute an internal command and wait for the completion.
1056 *
1057 * @port Pointer to the port data structure.
1058 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001059 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001060 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001061 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001062 * @opts Command header options, excluding the FIS length
1063 * and the number of PRD entries.
1064 * @timeout Time in ms to wait for the command to complete.
1065 *
1066 * return value
1067 * 0 Command completed successfully.
1068 * -EFAULT The buffer address is not correctly aligned.
1069 * -EBUSY Internal command or other IO in progress.
1070 * -EAGAIN Time out waiting for command to complete.
1071 */
1072static int mtip_exec_internal_command(struct mtip_port *port,
1073 void *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001074 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001075 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001076 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001077 u32 opts,
1078 gfp_t atomic,
1079 unsigned long timeout)
1080{
1081 struct mtip_cmd_sg *command_sg;
1082 DECLARE_COMPLETION_ONSTACK(wait);
1083 int rv = 0;
1084 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1085
1086 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1087 if (buffer & 0x00000007) {
1088 dev_err(&port->dd->pdev->dev,
1089 "SG buffer is not 8 byte aligned\n");
1090 return -EFAULT;
1091 }
1092
1093 /* Only one internal command should be running at a time */
1094 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1095 dev_warn(&port->dd->pdev->dev,
1096 "Internal command already active\n");
1097 return -EBUSY;
1098 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001099 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001100
1101 if (atomic == GFP_KERNEL) {
1102 /* wait for io to complete if non atomic */
1103 if (mtip_quiesce_io(port, 5000) < 0) {
1104 dev_warn(&port->dd->pdev->dev,
1105 "Failed to quiesce IO\n");
1106 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001107 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1108 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001109 return -EBUSY;
1110 }
1111
1112 /* Set the completion function and data for the command. */
1113 int_cmd->comp_data = &wait;
1114 int_cmd->comp_func = mtip_completion;
1115
1116 } else {
1117 /* Clear completion - we're going to poll */
1118 int_cmd->comp_data = NULL;
1119 int_cmd->comp_func = NULL;
1120 }
1121
1122 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001123 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001124
1125 /* Populate the SG list */
1126 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001127 __force_bit2int cpu_to_le32(opts | fis_len);
1128 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001129 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1130
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001131 command_sg->info =
1132 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1133 command_sg->dba =
1134 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1135 command_sg->dba_upper =
1136 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001137
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001138 int_cmd->command_header->opts |=
1139 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001140 }
1141
1142 /* Populate the command header */
1143 int_cmd->command_header->byte_count = 0;
1144
1145 /* Issue the command to the hardware */
1146 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1147
1148 /* Poll if atomic, wait_for_completion otherwise */
1149 if (atomic == GFP_KERNEL) {
1150 /* Wait for the command to complete or timeout. */
1151 if (wait_for_completion_timeout(
1152 &wait,
1153 msecs_to_jiffies(timeout)) == 0) {
1154 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001155 "Internal command did not complete [%d] "
1156 "within timeout of %lu ms\n",
1157 atomic, timeout);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001158 rv = -EAGAIN;
1159 }
1160
1161 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1162 & (1 << MTIP_TAG_INTERNAL)) {
1163 dev_warn(&port->dd->pdev->dev,
1164 "Retiring internal command but CI is 1.\n");
1165 }
1166
1167 } else {
1168 /* Spin for <timeout> checking if command still outstanding */
1169 timeout = jiffies + msecs_to_jiffies(timeout);
1170
1171 while ((readl(
1172 port->cmd_issue[MTIP_TAG_INTERNAL])
1173 & (1 << MTIP_TAG_INTERNAL))
1174 && time_before(jiffies, timeout))
1175 ;
1176
1177 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1178 & (1 << MTIP_TAG_INTERNAL)) {
1179 dev_err(&port->dd->pdev->dev,
1180 "Internal command did not complete [%d]\n",
1181 atomic);
1182 rv = -EAGAIN;
1183 }
1184 }
1185
1186 /* Clear the allocated and active bits for the internal command. */
1187 atomic_set(&int_cmd->active, 0);
1188 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001189 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1190 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001191
1192 return rv;
1193}
1194
1195/*
1196 * Byte-swap ATA ID strings.
1197 *
1198 * ATA identify data contains strings in byte-swapped 16-bit words.
1199 * They must be swapped (on all architectures) to be usable as C strings.
1200 * This function swaps bytes in-place.
1201 *
1202 * @buf The buffer location of the string
1203 * @len The number of bytes to swap
1204 *
1205 * return value
1206 * None
1207 */
1208static inline void ata_swap_string(u16 *buf, unsigned int len)
1209{
1210 int i;
1211 for (i = 0; i < (len/2); i++)
1212 be16_to_cpus(&buf[i]);
1213}
1214
1215/*
1216 * Request the device identity information.
1217 *
1218 * If a user space buffer is not specified, i.e. is NULL, the
1219 * identify information is still read from the drive and placed
1220 * into the identify data buffer (@e port->identify) in the
1221 * port data structure.
1222 * When the identify buffer contains valid identify information @e
1223 * port->identify_valid is non-zero.
1224 *
1225 * @port Pointer to the port structure.
1226 * @user_buffer A user space buffer where the identify data should be
1227 * copied.
1228 *
1229 * return value
1230 * 0 Command completed successfully.
1231 * -EFAULT An error occurred while coping data to the user buffer.
1232 * -1 Command failed.
1233 */
1234static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1235{
1236 int rv = 0;
1237 struct host_to_dev_fis fis;
1238
Sam Bradshaw88523a62011-08-30 08:34:26 -06001239 /* Build the FIS. */
1240 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1241 fis.type = 0x27;
1242 fis.opts = 1 << 7;
1243 fis.command = ATA_CMD_ID_ATA;
1244
1245 /* Set the identify information as invalid. */
1246 port->identify_valid = 0;
1247
1248 /* Clear the identify information. */
1249 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1250
1251 /* Execute the command. */
1252 if (mtip_exec_internal_command(port,
1253 &fis,
1254 5,
1255 port->identify_dma,
1256 sizeof(u16) * ATA_ID_WORDS,
1257 0,
1258 GFP_KERNEL,
1259 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1260 < 0) {
1261 rv = -1;
1262 goto out;
1263 }
1264
1265 /*
1266 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1267 * perform field-sensitive swapping on the string fields.
1268 * See the kernel use of ata_id_string() for proof of this.
1269 */
1270#ifdef __LITTLE_ENDIAN
1271 ata_swap_string(port->identify + 27, 40); /* model string*/
1272 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1273 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1274#else
1275 {
1276 int i;
1277 for (i = 0; i < ATA_ID_WORDS; i++)
1278 port->identify[i] = le16_to_cpu(port->identify[i]);
1279 }
1280#endif
1281
1282 /* Set the identify buffer as valid. */
1283 port->identify_valid = 1;
1284
1285 if (user_buffer) {
1286 if (copy_to_user(
1287 user_buffer,
1288 port->identify,
1289 ATA_ID_WORDS * sizeof(u16))) {
1290 rv = -EFAULT;
1291 goto out;
1292 }
1293 }
1294
1295out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001296 return rv;
1297}
1298
1299/*
1300 * Issue a standby immediate command to the device.
1301 *
1302 * @port Pointer to the port structure.
1303 *
1304 * return value
1305 * 0 Command was executed successfully.
1306 * -1 An error occurred while executing the command.
1307 */
1308static int mtip_standby_immediate(struct mtip_port *port)
1309{
1310 int rv;
1311 struct host_to_dev_fis fis;
1312
Sam Bradshaw88523a62011-08-30 08:34:26 -06001313 /* Build the FIS. */
1314 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1315 fis.type = 0x27;
1316 fis.opts = 1 << 7;
1317 fis.command = ATA_CMD_STANDBYNOW1;
1318
1319 /* Execute the command. Use a 15-second timeout for large drives. */
1320 rv = mtip_exec_internal_command(port,
1321 &fis,
1322 5,
1323 0,
1324 0,
1325 0,
1326 GFP_KERNEL,
1327 15000);
1328
Sam Bradshaw88523a62011-08-30 08:34:26 -06001329 return rv;
1330}
1331
1332/*
1333 * Get the drive capacity.
1334 *
1335 * @dd Pointer to the device data structure.
1336 * @sectors Pointer to the variable that will receive the sector count.
1337 *
1338 * return value
1339 * 1 Capacity was returned successfully.
1340 * 0 The identify information is invalid.
1341 */
Jens Axboe63166682011-09-27 21:27:43 -06001342static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001343{
1344 struct mtip_port *port = dd->port;
1345 u64 total, raw0, raw1, raw2, raw3;
1346 raw0 = port->identify[100];
1347 raw1 = port->identify[101];
1348 raw2 = port->identify[102];
1349 raw3 = port->identify[103];
1350 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1351 *sectors = total;
1352 return (bool) !!port->identify_valid;
1353}
1354
1355/*
1356 * Reset the HBA.
1357 *
1358 * Resets the HBA by setting the HBA Reset bit in the Global
1359 * HBA Control register. After setting the HBA Reset bit the
1360 * function waits for 1 second before reading the HBA Reset
1361 * bit to make sure it has cleared. If HBA Reset is not clear
1362 * an error is returned. Cannot be used in non-blockable
1363 * context.
1364 *
1365 * @dd Pointer to the driver data structure.
1366 *
1367 * return value
1368 * 0 The reset was successful.
1369 * -1 The HBA Reset bit did not clear.
1370 */
1371static int mtip_hba_reset(struct driver_data *dd)
1372{
1373 mtip_deinit_port(dd->port);
1374
1375 /* Set the reset bit */
1376 writel(HOST_RESET, dd->mmio + HOST_CTL);
1377
1378 /* Flush */
1379 readl(dd->mmio + HOST_CTL);
1380
1381 /* Wait for reset to clear */
1382 ssleep(1);
1383
1384 /* Check the bit has cleared */
1385 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1386 dev_err(&dd->pdev->dev,
1387 "Reset bit did not clear.\n");
1388 return -1;
1389 }
1390
1391 return 0;
1392}
1393
1394/*
1395 * Display the identify command data.
1396 *
1397 * @port Pointer to the port data structure.
1398 *
1399 * return value
1400 * None
1401 */
1402static void mtip_dump_identify(struct mtip_port *port)
1403{
1404 sector_t sectors;
1405 unsigned short revid;
1406 char cbuf[42];
1407
1408 if (!port->identify_valid)
1409 return;
1410
1411 strlcpy(cbuf, (char *)(port->identify+10), 21);
1412 dev_info(&port->dd->pdev->dev,
1413 "Serial No.: %s\n", cbuf);
1414
1415 strlcpy(cbuf, (char *)(port->identify+23), 9);
1416 dev_info(&port->dd->pdev->dev,
1417 "Firmware Ver.: %s\n", cbuf);
1418
1419 strlcpy(cbuf, (char *)(port->identify+27), 41);
1420 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1421
1422 if (mtip_hw_get_capacity(port->dd, &sectors))
1423 dev_info(&port->dd->pdev->dev,
1424 "Capacity: %llu sectors (%llu MB)\n",
1425 (u64)sectors,
1426 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1427
1428 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001429 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001430 case 0x1:
1431 strlcpy(cbuf, "A0", 3);
1432 break;
1433 case 0x3:
1434 strlcpy(cbuf, "A2", 3);
1435 break;
1436 default:
1437 strlcpy(cbuf, "?", 2);
1438 break;
1439 }
1440 dev_info(&port->dd->pdev->dev,
1441 "Card Type: %s\n", cbuf);
1442}
1443
1444/*
1445 * Map the commands scatter list into the command table.
1446 *
1447 * @command Pointer to the command.
1448 * @nents Number of scatter list entries.
1449 *
1450 * return value
1451 * None
1452 */
1453static inline void fill_command_sg(struct driver_data *dd,
1454 struct mtip_cmd *command,
1455 int nents)
1456{
1457 int n;
1458 unsigned int dma_len;
1459 struct mtip_cmd_sg *command_sg;
1460 struct scatterlist *sg = command->sg;
1461
1462 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1463
1464 for (n = 0; n < nents; n++) {
1465 dma_len = sg_dma_len(sg);
1466 if (dma_len > 0x400000)
1467 dev_err(&dd->pdev->dev,
1468 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001469 command_sg->info = __force_bit2int
1470 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1471 command_sg->dba = __force_bit2int
1472 cpu_to_le32(sg_dma_address(sg));
1473 command_sg->dba_upper = __force_bit2int
1474 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001475 command_sg++;
1476 sg++;
1477 }
1478}
1479
1480/*
1481 * @brief Execute a drive command.
1482 *
1483 * return value 0 The command completed successfully.
1484 * return value -1 An error occurred while executing the command.
1485 */
Jens Axboe63166682011-09-27 21:27:43 -06001486static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001487{
1488 struct host_to_dev_fis fis;
1489 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1490
Sam Bradshaw88523a62011-08-30 08:34:26 -06001491 /* Build the FIS. */
1492 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1493 fis.type = 0x27;
1494 fis.opts = 1 << 7;
1495 fis.command = command[0];
1496 fis.features = command[1];
1497 fis.sect_count = command[2];
1498 fis.sector = command[3];
1499 fis.cyl_low = command[4];
1500 fis.cyl_hi = command[5];
1501 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1502
1503
1504 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1505 "nsect %x, sect %x, lcyl %x, "
1506 "hcyl %x, sel %x\n",
1507 __func__,
1508 command[0],
1509 command[1],
1510 command[2],
1511 command[3],
1512 command[4],
1513 command[5],
1514 command[6]);
1515
1516 /* Execute the command. */
1517 if (mtip_exec_internal_command(port,
1518 &fis,
1519 5,
1520 0,
1521 0,
1522 0,
1523 GFP_KERNEL,
1524 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001525 return -1;
1526 }
1527
1528 command[0] = reply->command; /* Status*/
1529 command[1] = reply->features; /* Error*/
1530 command[4] = reply->cyl_low;
1531 command[5] = reply->cyl_hi;
1532
1533 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1534 "err %x , cyl_lo %x cyl_hi %x\n",
1535 __func__,
1536 command[0],
1537 command[1],
1538 command[4],
1539 command[5]);
1540
Sam Bradshaw88523a62011-08-30 08:34:26 -06001541 return 0;
1542}
1543
1544/*
1545 * @brief Execute a drive command.
1546 *
1547 * @param port Pointer to the port data structure.
1548 * @param command Pointer to the user specified command parameters.
1549 * @param user_buffer Pointer to the user space buffer where read sector
1550 * data should be copied.
1551 *
1552 * return value 0 The command completed successfully.
1553 * return value -EFAULT An error occurred while copying the completion
1554 * data to the user space buffer.
1555 * return value -1 An error occurred while executing the command.
1556 */
Jens Axboe63166682011-09-27 21:27:43 -06001557static int exec_drive_command(struct mtip_port *port, u8 *command,
1558 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001559{
1560 struct host_to_dev_fis fis;
1561 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1562
Sam Bradshaw88523a62011-08-30 08:34:26 -06001563 /* Build the FIS. */
1564 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1565 fis.type = 0x27;
1566 fis.opts = 1 << 7;
1567 fis.command = command[0];
1568 fis.features = command[2];
1569 fis.sect_count = command[3];
1570 if (fis.command == ATA_CMD_SMART) {
1571 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001572 fis.cyl_low = 0x4F;
1573 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001574 }
1575
1576 dbg_printk(MTIP_DRV_NAME
1577 "%s: User Command: cmd %x, sect %x, "
1578 "feat %x, sectcnt %x\n",
1579 __func__,
1580 command[0],
1581 command[1],
1582 command[2],
1583 command[3]);
1584
1585 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1586
1587 /* Execute the command. */
1588 if (mtip_exec_internal_command(port,
1589 &fis,
1590 5,
1591 port->sector_buffer_dma,
1592 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1593 0,
1594 GFP_KERNEL,
1595 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1596 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001597 return -1;
1598 }
1599
1600 /* Collect the completion status. */
1601 command[0] = reply->command; /* Status*/
1602 command[1] = reply->features; /* Error*/
1603 command[2] = command[3];
1604
1605 dbg_printk(MTIP_DRV_NAME
1606 "%s: Completion Status: stat %x, "
1607 "err %x, cmd %x\n",
1608 __func__,
1609 command[0],
1610 command[1],
1611 command[2]);
1612
1613 if (user_buffer && command[3]) {
1614 if (copy_to_user(user_buffer,
1615 port->sector_buffer,
1616 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001617 return -EFAULT;
1618 }
1619 }
1620
Sam Bradshaw88523a62011-08-30 08:34:26 -06001621 return 0;
1622}
1623
1624/*
1625 * Indicates whether a command has a single sector payload.
1626 *
1627 * @command passed to the device to perform the certain event.
1628 * @features passed to the device to perform the certain event.
1629 *
1630 * return value
1631 * 1 command is one that always has a single sector payload,
1632 * regardless of the value in the Sector Count field.
1633 * 0 otherwise
1634 *
1635 */
1636static unsigned int implicit_sector(unsigned char command,
1637 unsigned char features)
1638{
1639 unsigned int rv = 0;
1640
1641 /* list of commands that have an implicit sector count of 1 */
1642 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001643 case ATA_CMD_SEC_SET_PASS:
1644 case ATA_CMD_SEC_UNLOCK:
1645 case ATA_CMD_SEC_ERASE_PREP:
1646 case ATA_CMD_SEC_ERASE_UNIT:
1647 case ATA_CMD_SEC_FREEZE_LOCK:
1648 case ATA_CMD_SEC_DISABLE_PASS:
1649 case ATA_CMD_PMP_READ:
1650 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001651 rv = 1;
1652 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001653 case ATA_CMD_SET_MAX:
1654 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001655 rv = 1;
1656 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001657 case ATA_CMD_SMART:
1658 if ((features == ATA_SMART_READ_VALUES) ||
1659 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001660 rv = 1;
1661 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001662 case ATA_CMD_CONF_OVERLAY:
1663 if ((features == ATA_DCO_IDENTIFY) ||
1664 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001665 rv = 1;
1666 break;
1667 }
1668 return rv;
1669}
1670
1671/*
1672 * Executes a taskfile
1673 * See ide_taskfile_ioctl() for derivation
1674 */
1675static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001676 void __user *buf,
1677 ide_task_request_t *req_task,
1678 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679{
1680 struct host_to_dev_fis fis;
1681 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001682 u8 *outbuf = NULL;
1683 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001684 dma_addr_t outbuf_dma = 0;
1685 dma_addr_t inbuf_dma = 0;
1686 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001687 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001688 unsigned int taskin = 0;
1689 unsigned int taskout = 0;
1690 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001691 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1692 unsigned int force_single_sector;
1693 unsigned int transfer_size;
1694 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001695 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001696
1697 taskout = req_task->out_size;
1698 taskin = req_task->in_size;
1699 /* 130560 = 512 * 0xFF*/
1700 if (taskin > 130560 || taskout > 130560) {
1701 err = -EINVAL;
1702 goto abort;
1703 }
1704
1705 if (taskout) {
1706 outbuf = kzalloc(taskout, GFP_KERNEL);
1707 if (outbuf == NULL) {
1708 err = -ENOMEM;
1709 goto abort;
1710 }
1711 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1712 err = -EFAULT;
1713 goto abort;
1714 }
1715 outbuf_dma = pci_map_single(dd->pdev,
1716 outbuf,
1717 taskout,
1718 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001719 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001720 err = -ENOMEM;
1721 goto abort;
1722 }
1723 dma_buffer = outbuf_dma;
1724 }
1725
1726 if (taskin) {
1727 inbuf = kzalloc(taskin, GFP_KERNEL);
1728 if (inbuf == NULL) {
1729 err = -ENOMEM;
1730 goto abort;
1731 }
1732
1733 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1734 err = -EFAULT;
1735 goto abort;
1736 }
1737 inbuf_dma = pci_map_single(dd->pdev,
1738 inbuf,
1739 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001740 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001741 err = -ENOMEM;
1742 goto abort;
1743 }
1744 dma_buffer = inbuf_dma;
1745 }
1746
1747 /* only supports PIO and non-data commands from this ioctl. */
1748 switch (req_task->data_phase) {
1749 case TASKFILE_OUT:
1750 nsect = taskout / ATA_SECT_SIZE;
1751 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1752 break;
1753 case TASKFILE_IN:
1754 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1755 break;
1756 case TASKFILE_NO_DATA:
1757 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1758 break;
1759 default:
1760 err = -EINVAL;
1761 goto abort;
1762 }
1763
Sam Bradshaw88523a62011-08-30 08:34:26 -06001764 /* Build the FIS. */
1765 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1766
1767 fis.type = 0x27;
1768 fis.opts = 1 << 7;
1769 fis.command = req_task->io_ports[7];
1770 fis.features = req_task->io_ports[1];
1771 fis.sect_count = req_task->io_ports[2];
1772 fis.lba_low = req_task->io_ports[3];
1773 fis.lba_mid = req_task->io_ports[4];
1774 fis.lba_hi = req_task->io_ports[5];
1775 /* Clear the dev bit*/
1776 fis.device = req_task->io_ports[6] & ~0x10;
1777
1778 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1779 req_task->in_flags.all =
1780 IDE_TASKFILE_STD_IN_FLAGS |
1781 (IDE_HOB_STD_IN_FLAGS << 8);
1782 fis.lba_low_ex = req_task->hob_ports[3];
1783 fis.lba_mid_ex = req_task->hob_ports[4];
1784 fis.lba_hi_ex = req_task->hob_ports[5];
1785 fis.features_ex = req_task->hob_ports[1];
1786 fis.sect_cnt_ex = req_task->hob_ports[2];
1787
1788 } else {
1789 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1790 }
1791
1792 force_single_sector = implicit_sector(fis.command, fis.features);
1793
1794 if ((taskin || taskout) && (!fis.sect_count)) {
1795 if (nsect)
1796 fis.sect_count = nsect;
1797 else {
1798 if (!force_single_sector) {
1799 dev_warn(&dd->pdev->dev,
1800 "data movement but "
1801 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001802 err = -EINVAL;
1803 goto abort;
1804 }
1805 }
1806 }
1807
1808 dbg_printk(MTIP_DRV_NAME
1809 "taskfile: cmd %x, feat %x, nsect %x,"
1810 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1811 " head/dev %x\n",
1812 fis.command,
1813 fis.features,
1814 fis.sect_count,
1815 fis.lba_low,
1816 fis.lba_mid,
1817 fis.lba_hi,
1818 fis.device);
1819
1820 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001821 case ATA_CMD_DOWNLOAD_MICRO:
1822 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001823 timeout = 60000;
1824 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001825 case ATA_CMD_SEC_ERASE_UNIT:
1826 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001827 timeout = 240000;
1828 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001829 case ATA_CMD_STANDBYNOW1:
1830 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001831 timeout = 10000;
1832 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001833 case 0xF7:
1834 case 0xFA:
1835 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001836 timeout = 10000;
1837 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001838 case ATA_CMD_SMART:
1839 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001840 timeout = 10000;
1841 break;
1842 default:
1843 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1844 break;
1845 }
1846
1847 /* Determine the correct transfer size.*/
1848 if (force_single_sector)
1849 transfer_size = ATA_SECT_SIZE;
1850 else
1851 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1852
1853 /* Execute the command.*/
1854 if (mtip_exec_internal_command(dd->port,
1855 &fis,
1856 5,
1857 dma_buffer,
1858 transfer_size,
1859 0,
1860 GFP_KERNEL,
1861 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001862 err = -EIO;
1863 goto abort;
1864 }
1865
1866 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1867
1868 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1869 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1870 req_task->io_ports[7] = reply->control;
1871 } else {
1872 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1873 req_task->io_ports[7] = reply->command;
1874 }
1875
1876 /* reclaim the DMA buffers.*/
1877 if (inbuf_dma)
1878 pci_unmap_single(dd->pdev, inbuf_dma,
1879 taskin, DMA_FROM_DEVICE);
1880 if (outbuf_dma)
1881 pci_unmap_single(dd->pdev, outbuf_dma,
1882 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001883 inbuf_dma = 0;
1884 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001885
1886 /* return the ATA registers to the caller.*/
1887 req_task->io_ports[1] = reply->features;
1888 req_task->io_ports[2] = reply->sect_count;
1889 req_task->io_ports[3] = reply->lba_low;
1890 req_task->io_ports[4] = reply->lba_mid;
1891 req_task->io_ports[5] = reply->lba_hi;
1892 req_task->io_ports[6] = reply->device;
1893
1894 if (req_task->out_flags.all & 1) {
1895
1896 req_task->hob_ports[3] = reply->lba_low_ex;
1897 req_task->hob_ports[4] = reply->lba_mid_ex;
1898 req_task->hob_ports[5] = reply->lba_hi_ex;
1899 req_task->hob_ports[1] = reply->features_ex;
1900 req_task->hob_ports[2] = reply->sect_cnt_ex;
1901 }
1902
1903 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001904 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06001905 ((fis.command == 0xFC) &&
1906 (fis.features == 0x27 || fis.features == 0x72 ||
1907 fis.features == 0x62 || fis.features == 0x26))) &&
1908 !(reply->command & 1)) {
1909 mtip_restart_port(dd->port);
1910 }
1911
1912 dbg_printk(MTIP_DRV_NAME
1913 "%s: Completion: stat %x,"
1914 "err %x, sect_cnt %x, lbalo %x,"
1915 "lbamid %x, lbahi %x, dev %x\n",
1916 __func__,
1917 req_task->io_ports[7],
1918 req_task->io_ports[1],
1919 req_task->io_ports[2],
1920 req_task->io_ports[3],
1921 req_task->io_ports[4],
1922 req_task->io_ports[5],
1923 req_task->io_ports[6]);
1924
Sam Bradshaw88523a62011-08-30 08:34:26 -06001925 if (taskout) {
1926 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1927 err = -EFAULT;
1928 goto abort;
1929 }
1930 }
1931 if (taskin) {
1932 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1933 err = -EFAULT;
1934 goto abort;
1935 }
1936 }
1937abort:
1938 if (inbuf_dma)
1939 pci_unmap_single(dd->pdev, inbuf_dma,
1940 taskin, DMA_FROM_DEVICE);
1941 if (outbuf_dma)
1942 pci_unmap_single(dd->pdev, outbuf_dma,
1943 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001944 kfree(outbuf);
1945 kfree(inbuf);
1946
1947 return err;
1948}
1949
1950/*
1951 * Handle IOCTL calls from the Block Layer.
1952 *
1953 * This function is called by the Block Layer when it receives an IOCTL
1954 * command that it does not understand. If the IOCTL command is not supported
1955 * this function returns -ENOTTY.
1956 *
1957 * @dd Pointer to the driver data structure.
1958 * @cmd IOCTL command passed from the Block Layer.
1959 * @arg IOCTL argument passed from the Block Layer.
1960 *
1961 * return value
1962 * 0 The IOCTL completed successfully.
1963 * -ENOTTY The specified command is not supported.
1964 * -EFAULT An error occurred copying data to a user space buffer.
1965 * -EIO An error occurred while executing the command.
1966 */
Jens Axboeef0f1582011-09-27 21:19:53 -06001967static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1968 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001969{
1970 switch (cmd) {
1971 case HDIO_GET_IDENTITY:
1972 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1973 dev_warn(&dd->pdev->dev,
1974 "Unable to read identity\n");
1975 return -EIO;
1976 }
1977
1978 break;
1979 case HDIO_DRIVE_CMD:
1980 {
1981 u8 drive_command[4];
1982
1983 /* Copy the user command info to our buffer. */
1984 if (copy_from_user(drive_command,
1985 (void __user *) arg,
1986 sizeof(drive_command)))
1987 return -EFAULT;
1988
1989 /* Execute the drive command. */
1990 if (exec_drive_command(dd->port,
1991 drive_command,
1992 (void __user *) (arg+4)))
1993 return -EIO;
1994
1995 /* Copy the status back to the users buffer. */
1996 if (copy_to_user((void __user *) arg,
1997 drive_command,
1998 sizeof(drive_command)))
1999 return -EFAULT;
2000
2001 break;
2002 }
2003 case HDIO_DRIVE_TASK:
2004 {
2005 u8 drive_command[7];
2006
2007 /* Copy the user command info to our buffer. */
2008 if (copy_from_user(drive_command,
2009 (void __user *) arg,
2010 sizeof(drive_command)))
2011 return -EFAULT;
2012
2013 /* Execute the drive command. */
2014 if (exec_drive_task(dd->port, drive_command))
2015 return -EIO;
2016
2017 /* Copy the status back to the users buffer. */
2018 if (copy_to_user((void __user *) arg,
2019 drive_command,
2020 sizeof(drive_command)))
2021 return -EFAULT;
2022
2023 break;
2024 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002025 case HDIO_DRIVE_TASKFILE: {
2026 ide_task_request_t req_task;
2027 int ret, outtotal;
2028
2029 if (copy_from_user(&req_task, (void __user *) arg,
2030 sizeof(req_task)))
2031 return -EFAULT;
2032
2033 outtotal = sizeof(req_task);
2034
2035 ret = exec_drive_taskfile(dd, (void __user *) arg,
2036 &req_task, outtotal);
2037
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002038 if (copy_to_user((void __user *) arg, &req_task,
2039 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002040 return -EFAULT;
2041
2042 return ret;
2043 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002044
2045 default:
2046 return -EINVAL;
2047 }
2048 return 0;
2049}
2050
2051/*
2052 * Submit an IO to the hw
2053 *
2054 * This function is called by the block layer to issue an io
2055 * to the device. Upon completion, the callback function will
2056 * be called with the data parameter passed as the callback data.
2057 *
2058 * @dd Pointer to the driver data structure.
2059 * @start First sector to read.
2060 * @nsect Number of sectors to read.
2061 * @nents Number of entries in scatter list for the read command.
2062 * @tag The tag of this read command.
2063 * @callback Pointer to the function that should be called
2064 * when the read completes.
2065 * @data Callback data passed to the callback function
2066 * when the read completes.
2067 * @barrier If non-zero, this command must be completed before
2068 * issuing any other commands.
2069 * @dir Direction (read or write)
2070 *
2071 * return value
2072 * None
2073 */
Jens Axboe63166682011-09-27 21:27:43 -06002074static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2075 int nsect, int nents, int tag, void *callback,
2076 void *data, int barrier, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002077{
2078 struct host_to_dev_fis *fis;
2079 struct mtip_port *port = dd->port;
2080 struct mtip_cmd *command = &port->commands[tag];
2081
2082 /* Map the scatter list for DMA access */
2083 if (dir == READ)
2084 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2085 nents, DMA_FROM_DEVICE);
2086 else
2087 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2088 nents, DMA_TO_DEVICE);
2089
2090 command->scatter_ents = nents;
2091
2092 /*
2093 * The number of retries for this command before it is
2094 * reported as a failure to the upper layers.
2095 */
2096 command->retries = MTIP_MAX_RETRIES;
2097
2098 /* Fill out fis */
2099 fis = command->command;
2100 fis->type = 0x27;
2101 fis->opts = 1 << 7;
2102 fis->command =
2103 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002104 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2105 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002106 fis->device = 1 << 6;
2107 if (barrier)
2108 fis->device |= FUA_BIT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002109 fis->features = nsect & 0xFF;
2110 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002111 fis->sect_count = ((tag << 3) | (tag >> 5));
2112 fis->sect_cnt_ex = 0;
2113 fis->control = 0;
2114 fis->res2 = 0;
2115 fis->res3 = 0;
2116 fill_command_sg(dd, command, nents);
2117
2118 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002119 command->command_header->opts =
2120 __force_bit2int cpu_to_le32(
2121 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002122 command->command_header->byte_count = 0;
2123
2124 /*
2125 * Set the completion function and data for the command
2126 * within this layer.
2127 */
2128 command->comp_data = dd;
2129 command->comp_func = mtip_async_complete;
2130 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2131
2132 /*
2133 * Set the completion function and data for the command passed
2134 * from the upper layer.
2135 */
2136 command->async_data = data;
2137 command->async_callback = callback;
2138
2139 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002140 * To prevent this command from being issued
2141 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002142 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002143 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2144 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2145 set_bit(tag, port->cmds_to_issue);
2146 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2147 return;
2148 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002149
2150 /* Issue the command to the hardware */
2151 mtip_issue_ncq_command(port, tag);
2152
2153 /* Set the command's timeout value.*/
2154 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2155 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002156}
2157
2158/*
2159 * Release a command slot.
2160 *
2161 * @dd Pointer to the driver data structure.
2162 * @tag Slot tag
2163 *
2164 * return value
2165 * None
2166 */
Jens Axboe63166682011-09-27 21:27:43 -06002167static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002168{
2169 release_slot(dd->port, tag);
2170}
2171
2172/*
2173 * Obtain a command slot and return its associated scatter list.
2174 *
2175 * @dd Pointer to the driver data structure.
2176 * @tag Pointer to an int that will receive the allocated command
2177 * slot tag.
2178 *
2179 * return value
2180 * Pointer to the scatter list for the allocated command slot
2181 * or NULL if no command slots are available.
2182 */
Jens Axboe63166682011-09-27 21:27:43 -06002183static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2184 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002185{
2186 /*
2187 * It is possible that, even with this semaphore, a thread
2188 * may think that no command slots are available. Therefore, we
2189 * need to make an attempt to get_slot().
2190 */
2191 down(&dd->port->cmd_slot);
2192 *tag = get_slot(dd->port);
2193
2194 if (unlikely(*tag < 0))
2195 return NULL;
2196
2197 return dd->port->commands[*tag].sg;
2198}
2199
2200/*
2201 * Sysfs register/status dump.
2202 *
2203 * @dev Pointer to the device structure, passed by the kernrel.
2204 * @attr Pointer to the device_attribute structure passed by the kernel.
2205 * @buf Pointer to the char buffer that will receive the stats info.
2206 *
2207 * return value
2208 * The size, in bytes, of the data copied into buf.
2209 */
2210static ssize_t hw_show_registers(struct device *dev,
2211 struct device_attribute *attr,
2212 char *buf)
2213{
2214 u32 group_allocated;
2215 struct driver_data *dd = dev_to_disk(dev)->private_data;
2216 int size = 0;
2217 int n;
2218
2219 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2220
2221 for (n = 0; n < dd->slot_groups; n++)
2222 size += sprintf(&buf[size], "0x%08x\n",
2223 readl(dd->port->s_active[n]));
2224
2225 size += sprintf(&buf[size], "Command Issue:\n");
2226
2227 for (n = 0; n < dd->slot_groups; n++)
2228 size += sprintf(&buf[size], "0x%08x\n",
2229 readl(dd->port->cmd_issue[n]));
2230
2231 size += sprintf(&buf[size], "Allocated:\n");
2232
2233 for (n = 0; n < dd->slot_groups; n++) {
2234 if (sizeof(long) > sizeof(u32))
2235 group_allocated =
2236 dd->port->allocated[n/2] >> (32*(n&1));
2237 else
2238 group_allocated = dd->port->allocated[n];
2239 size += sprintf(&buf[size], "0x%08x\n",
2240 group_allocated);
2241 }
2242
2243 size += sprintf(&buf[size], "completed:\n");
2244
2245 for (n = 0; n < dd->slot_groups; n++)
2246 size += sprintf(&buf[size], "0x%08x\n",
2247 readl(dd->port->completed[n]));
2248
2249 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2250 readl(dd->port->mmio + PORT_IRQ_STAT));
2251 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2252 readl(dd->mmio + HOST_IRQ_STAT));
2253
2254 return size;
2255}
2256static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2257
2258/*
2259 * Create the sysfs related attributes.
2260 *
2261 * @dd Pointer to the driver data structure.
2262 * @kobj Pointer to the kobj for the block device.
2263 *
2264 * return value
2265 * 0 Operation completed successfully.
2266 * -EINVAL Invalid parameter.
2267 */
Jens Axboe63166682011-09-27 21:27:43 -06002268static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002269{
2270 if (!kobj || !dd)
2271 return -EINVAL;
2272
2273 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2274 dev_warn(&dd->pdev->dev,
2275 "Error creating registers sysfs entry\n");
2276 return 0;
2277}
2278
2279/*
2280 * Remove the sysfs related attributes.
2281 *
2282 * @dd Pointer to the driver data structure.
2283 * @kobj Pointer to the kobj for the block device.
2284 *
2285 * return value
2286 * 0 Operation completed successfully.
2287 * -EINVAL Invalid parameter.
2288 */
Jens Axboe63166682011-09-27 21:27:43 -06002289static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002290{
2291 if (!kobj || !dd)
2292 return -EINVAL;
2293
2294 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2295
2296 return 0;
2297}
2298
2299/*
2300 * Perform any init/resume time hardware setup
2301 *
2302 * @dd Pointer to the driver data structure.
2303 *
2304 * return value
2305 * None
2306 */
2307static inline void hba_setup(struct driver_data *dd)
2308{
2309 u32 hwdata;
2310 hwdata = readl(dd->mmio + HOST_HSORG);
2311
2312 /* interrupt bug workaround: use only 1 IS bit.*/
2313 writel(hwdata |
2314 HSORG_DISABLE_SLOTGRP_INTR |
2315 HSORG_DISABLE_SLOTGRP_PXIS,
2316 dd->mmio + HOST_HSORG);
2317}
2318
2319/*
2320 * Detect the details of the product, and store anything needed
2321 * into the driver data structure. This includes product type and
2322 * version and number of slot groups.
2323 *
2324 * @dd Pointer to the driver data structure.
2325 *
2326 * return value
2327 * None
2328 */
2329static void mtip_detect_product(struct driver_data *dd)
2330{
2331 u32 hwdata;
2332 unsigned int rev, slotgroups;
2333
2334 /*
2335 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2336 * info register:
2337 * [15:8] hardware/software interface rev#
2338 * [ 3] asic-style interface
2339 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2340 */
2341 hwdata = readl(dd->mmio + HOST_HSORG);
2342
2343 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2344 dd->slot_groups = 1;
2345
2346 if (hwdata & 0x8) {
2347 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2348 rev = (hwdata & HSORG_HWREV) >> 8;
2349 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2350 dev_info(&dd->pdev->dev,
2351 "ASIC-FPGA design, HS rev 0x%x, "
2352 "%i slot groups [%i slots]\n",
2353 rev,
2354 slotgroups,
2355 slotgroups * 32);
2356
2357 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2358 dev_warn(&dd->pdev->dev,
2359 "Warning: driver only supports "
2360 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2361 slotgroups = MTIP_MAX_SLOT_GROUPS;
2362 }
2363 dd->slot_groups = slotgroups;
2364 return;
2365 }
2366
2367 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2368}
2369
2370/*
2371 * Blocking wait for FTL rebuild to complete
2372 *
2373 * @dd Pointer to the DRIVER_DATA structure.
2374 *
2375 * return value
2376 * 0 FTL rebuild completed successfully
2377 * -EFAULT FTL rebuild error/timeout/interruption
2378 */
2379static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2380{
2381 unsigned long timeout, cnt = 0, start;
2382
2383 dev_warn(&dd->pdev->dev,
2384 "FTL rebuild in progress. Polling for completion.\n");
2385
2386 start = jiffies;
2387 dd->ftlrebuildflag = 1;
2388 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2389
2390 do {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002391 if (mtip_check_surprise_removal(dd->pdev))
2392 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002393
Sam Bradshaw88523a62011-08-30 08:34:26 -06002394 if (mtip_get_identify(dd->port, NULL) < 0)
2395 return -EFAULT;
2396
2397 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2398 MTIP_FTL_REBUILD_MAGIC) {
2399 ssleep(1);
2400 /* Print message every 3 minutes */
2401 if (cnt++ >= 180) {
2402 dev_warn(&dd->pdev->dev,
2403 "FTL rebuild in progress (%d secs).\n",
2404 jiffies_to_msecs(jiffies - start) / 1000);
2405 cnt = 0;
2406 }
2407 } else {
2408 dev_warn(&dd->pdev->dev,
2409 "FTL rebuild complete (%d secs).\n",
2410 jiffies_to_msecs(jiffies - start) / 1000);
2411 dd->ftlrebuildflag = 0;
2412 break;
2413 }
2414 ssleep(10);
2415 } while (time_before(jiffies, timeout));
2416
2417 /* Check for timeout */
2418 if (dd->ftlrebuildflag) {
2419 dev_err(&dd->pdev->dev,
2420 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2421 jiffies_to_msecs(jiffies - start) / 1000);
2422 return -EFAULT;
2423 }
2424
2425 return 0;
2426}
2427
2428/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002429 * service thread to issue queued commands
2430 *
2431 * @data Pointer to the driver data structure.
2432 *
2433 * return value
2434 * 0
2435 */
2436
2437static int mtip_service_thread(void *data)
2438{
2439 struct driver_data *dd = (struct driver_data *)data;
2440 unsigned long slot, slot_start, slot_wrap;
2441 unsigned int num_cmd_slots = dd->slot_groups * 32;
2442 struct mtip_port *port = dd->port;
2443
2444 while (1) {
2445 /*
2446 * the condition is to check neither an internal command is
2447 * is in progress nor error handling is active
2448 */
2449 wait_event_interruptible(port->svc_wait, (port->flags) &&
2450 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2451 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2452
2453 if (kthread_should_stop())
2454 break;
2455
2456 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
2457 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2458 slot = 1;
2459 /* used to restrict the loop to one iteration */
2460 slot_start = num_cmd_slots;
2461 slot_wrap = 0;
2462 while (1) {
2463 slot = find_next_bit(port->cmds_to_issue,
2464 num_cmd_slots, slot);
2465 if (slot_wrap == 1) {
2466 if ((slot_start >= slot) ||
2467 (slot >= num_cmd_slots))
2468 break;
2469 }
2470 if (unlikely(slot_start == num_cmd_slots))
2471 slot_start = slot;
2472
2473 if (unlikely(slot == num_cmd_slots)) {
2474 slot = 1;
2475 slot_wrap = 1;
2476 continue;
2477 }
2478
2479 /* Issue the command to the hardware */
2480 mtip_issue_ncq_command(port, slot);
2481
2482 /* Set the command's timeout value.*/
2483 port->commands[slot].comp_time = jiffies +
2484 msecs_to_jiffies(MTIP_NCQ_COMMAND_TIMEOUT_MS);
2485
2486 clear_bit(slot, port->cmds_to_issue);
2487 }
2488
2489 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2490 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2491 }
2492 }
2493 return 0;
2494}
2495
2496/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002497 * Called once for each card.
2498 *
2499 * @dd Pointer to the driver data structure.
2500 *
2501 * return value
2502 * 0 on success, else an error code.
2503 */
Jens Axboe63166682011-09-27 21:27:43 -06002504static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002505{
2506 int i;
2507 int rv;
2508 unsigned int num_command_slots;
2509
2510 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2511
2512 mtip_detect_product(dd);
2513 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2514 rv = -EIO;
2515 goto out1;
2516 }
2517 num_command_slots = dd->slot_groups * 32;
2518
2519 hba_setup(dd);
2520
Sam Bradshaw88523a62011-08-30 08:34:26 -06002521 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2522
2523 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2524 if (!dd->port) {
2525 dev_err(&dd->pdev->dev,
2526 "Memory allocation: port structure\n");
2527 return -ENOMEM;
2528 }
2529
2530 /* Counting semaphore to track command slot usage */
2531 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2532
2533 /* Spinlock to prevent concurrent issue */
2534 spin_lock_init(&dd->port->cmd_issue_lock);
2535
2536 /* Set the port mmio base address. */
2537 dd->port->mmio = dd->mmio + PORT_OFFSET;
2538 dd->port->dd = dd;
2539
2540 /* Allocate memory for the command list. */
2541 dd->port->command_list =
2542 dmam_alloc_coherent(&dd->pdev->dev,
2543 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2544 &dd->port->command_list_dma,
2545 GFP_KERNEL);
2546 if (!dd->port->command_list) {
2547 dev_err(&dd->pdev->dev,
2548 "Memory allocation: command list\n");
2549 rv = -ENOMEM;
2550 goto out1;
2551 }
2552
2553 /* Clear the memory we have allocated. */
2554 memset(dd->port->command_list,
2555 0,
2556 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2557
2558 /* Setup the addresse of the RX FIS. */
2559 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2560 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2561
2562 /* Setup the address of the command tables. */
2563 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2564 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2565
2566 /* Setup the address of the identify data. */
2567 dd->port->identify = dd->port->command_table +
2568 HW_CMD_TBL_AR_SZ;
2569 dd->port->identify_dma = dd->port->command_tbl_dma +
2570 HW_CMD_TBL_AR_SZ;
2571
2572 /* Setup the address of the sector buffer. */
2573 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2574 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2575
2576 /* Point the command headers at the command tables. */
2577 for (i = 0; i < num_command_slots; i++) {
2578 dd->port->commands[i].command_header =
2579 dd->port->command_list +
2580 (sizeof(struct mtip_cmd_hdr) * i);
2581 dd->port->commands[i].command_header_dma =
2582 dd->port->command_list_dma +
2583 (sizeof(struct mtip_cmd_hdr) * i);
2584
2585 dd->port->commands[i].command =
2586 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2587 dd->port->commands[i].command_dma =
2588 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2589
2590 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2591 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002592 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002593 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002594 dd->port->commands[i].command_header->ctba =
2595 __force_bit2int cpu_to_le32(
2596 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002597
2598 /*
2599 * If this is not done, a bug is reported by the stock
2600 * FC11 i386. Due to the fact that it has lots of kernel
2601 * debugging enabled.
2602 */
2603 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2604
2605 /* Mark all commands as currently inactive.*/
2606 atomic_set(&dd->port->commands[i].active, 0);
2607 }
2608
2609 /* Setup the pointers to the extended s_active and CI registers. */
2610 for (i = 0; i < dd->slot_groups; i++) {
2611 dd->port->s_active[i] =
2612 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2613 dd->port->cmd_issue[i] =
2614 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2615 dd->port->completed[i] =
2616 dd->port->mmio + i*0x80 + PORT_SDBV;
2617 }
2618
2619 /* Reset the HBA. */
2620 if (mtip_hba_reset(dd) < 0) {
2621 dev_err(&dd->pdev->dev,
2622 "Card did not reset within timeout\n");
2623 rv = -EIO;
2624 goto out2;
2625 }
2626
2627 mtip_init_port(dd->port);
2628 mtip_start_port(dd->port);
2629
2630 /* Setup the ISR and enable interrupts. */
2631 rv = devm_request_irq(&dd->pdev->dev,
2632 dd->pdev->irq,
2633 mtip_irq_handler,
2634 IRQF_SHARED,
2635 dev_driver_string(&dd->pdev->dev),
2636 dd);
2637
2638 if (rv) {
2639 dev_err(&dd->pdev->dev,
2640 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2641 goto out2;
2642 }
2643
2644 /* Enable interrupts on the HBA. */
2645 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2646 dd->mmio + HOST_CTL);
2647
2648 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002649 init_waitqueue_head(&dd->port->svc_wait);
2650
Sam Bradshaw88523a62011-08-30 08:34:26 -06002651 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2652 dd->port->cmd_timer.function = mtip_timeout_function;
2653 mod_timer(&dd->port->cmd_timer,
2654 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2655
2656 if (mtip_get_identify(dd->port, NULL) < 0) {
2657 rv = -EFAULT;
2658 goto out3;
2659 }
2660 mtip_dump_identify(dd->port);
2661
2662 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2663 MTIP_FTL_REBUILD_MAGIC) {
2664 return mtip_ftl_rebuild_poll(dd);
2665 }
2666 return rv;
2667
2668out3:
2669 del_timer_sync(&dd->port->cmd_timer);
2670
2671 /* Disable interrupts on the HBA. */
2672 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2673 dd->mmio + HOST_CTL);
2674
2675 /*Release the IRQ. */
2676 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2677
2678out2:
2679 mtip_deinit_port(dd->port);
2680
2681 /* Free the command/command header memory. */
2682 dmam_free_coherent(&dd->pdev->dev,
2683 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2684 dd->port->command_list,
2685 dd->port->command_list_dma);
2686out1:
2687 /* Free the memory allocated for the for structure. */
2688 kfree(dd->port);
2689
2690 return rv;
2691}
2692
2693/*
2694 * Called to deinitialize an interface.
2695 *
2696 * @dd Pointer to the driver data structure.
2697 *
2698 * return value
2699 * 0
2700 */
Jens Axboe63166682011-09-27 21:27:43 -06002701static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002702{
2703 /*
2704 * Send standby immediate (E0h) to the drive so that it
2705 * saves its state.
2706 */
2707 if (atomic_read(&dd->drv_cleanup_done) != true) {
2708
2709 mtip_standby_immediate(dd->port);
2710
2711 /* de-initialize the port. */
2712 mtip_deinit_port(dd->port);
2713
2714 /* Disable interrupts on the HBA. */
2715 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2716 dd->mmio + HOST_CTL);
2717 }
2718
2719 del_timer_sync(&dd->port->cmd_timer);
2720
Sam Bradshaw88523a62011-08-30 08:34:26 -06002721 /* Release the IRQ. */
2722 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2723
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002724 /* Stop the bottom half tasklet. */
2725 tasklet_kill(&dd->tasklet);
2726
Sam Bradshaw88523a62011-08-30 08:34:26 -06002727 /* Free the command/command header memory. */
2728 dmam_free_coherent(&dd->pdev->dev,
2729 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2730 dd->port->command_list,
2731 dd->port->command_list_dma);
2732 /* Free the memory allocated for the for structure. */
2733 kfree(dd->port);
2734
2735 return 0;
2736}
2737
2738/*
2739 * Issue a Standby Immediate command to the device.
2740 *
2741 * This function is called by the Block Layer just before the
2742 * system powers off during a shutdown.
2743 *
2744 * @dd Pointer to the driver data structure.
2745 *
2746 * return value
2747 * 0
2748 */
Jens Axboe63166682011-09-27 21:27:43 -06002749static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002750{
2751 /*
2752 * Send standby immediate (E0h) to the drive so that it
2753 * saves its state.
2754 */
2755 mtip_standby_immediate(dd->port);
2756
2757 return 0;
2758}
2759
2760/*
2761 * Suspend function
2762 *
2763 * This function is called by the Block Layer just before the
2764 * system hibernates.
2765 *
2766 * @dd Pointer to the driver data structure.
2767 *
2768 * return value
2769 * 0 Suspend was successful
2770 * -EFAULT Suspend was not successful
2771 */
Jens Axboe63166682011-09-27 21:27:43 -06002772static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002773{
2774 /*
2775 * Send standby immediate (E0h) to the drive
2776 * so that it saves its state.
2777 */
2778 if (mtip_standby_immediate(dd->port) != 0) {
2779 dev_err(&dd->pdev->dev,
2780 "Failed standby-immediate command\n");
2781 return -EFAULT;
2782 }
2783
2784 /* Disable interrupts on the HBA.*/
2785 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2786 dd->mmio + HOST_CTL);
2787 mtip_deinit_port(dd->port);
2788
2789 return 0;
2790}
2791
2792/*
2793 * Resume function
2794 *
2795 * This function is called by the Block Layer as the
2796 * system resumes.
2797 *
2798 * @dd Pointer to the driver data structure.
2799 *
2800 * return value
2801 * 0 Resume was successful
2802 * -EFAULT Resume was not successful
2803 */
Jens Axboe63166682011-09-27 21:27:43 -06002804static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002805{
2806 /* Perform any needed hardware setup steps */
2807 hba_setup(dd);
2808
2809 /* Reset the HBA */
2810 if (mtip_hba_reset(dd) != 0) {
2811 dev_err(&dd->pdev->dev,
2812 "Unable to reset the HBA\n");
2813 return -EFAULT;
2814 }
2815
2816 /*
2817 * Enable the port, DMA engine, and FIS reception specific
2818 * h/w in controller.
2819 */
2820 mtip_init_port(dd->port);
2821 mtip_start_port(dd->port);
2822
2823 /* Enable interrupts on the HBA.*/
2824 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2825 dd->mmio + HOST_CTL);
2826
2827 return 0;
2828}
2829
2830/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002831 * Helper function for reusing disk name
2832 * upon hot insertion.
2833 */
2834static int rssd_disk_name_format(char *prefix,
2835 int index,
2836 char *buf,
2837 int buflen)
2838{
2839 const int base = 'z' - 'a' + 1;
2840 char *begin = buf + strlen(prefix);
2841 char *end = buf + buflen;
2842 char *p;
2843 int unit;
2844
2845 p = end - 1;
2846 *p = '\0';
2847 unit = base;
2848 do {
2849 if (p == begin)
2850 return -EINVAL;
2851 *--p = 'a' + (index % unit);
2852 index = (index / unit) - 1;
2853 } while (index >= 0);
2854
2855 memmove(begin, p, end - p);
2856 memcpy(buf, prefix, strlen(prefix));
2857
2858 return 0;
2859}
2860
2861/*
2862 * Block layer IOCTL handler.
2863 *
2864 * @dev Pointer to the block_device structure.
2865 * @mode ignored
2866 * @cmd IOCTL command passed from the user application.
2867 * @arg Argument passed from the user application.
2868 *
2869 * return value
2870 * 0 IOCTL completed successfully.
2871 * -ENOTTY IOCTL not supported or invalid driver data
2872 * structure pointer.
2873 */
2874static int mtip_block_ioctl(struct block_device *dev,
2875 fmode_t mode,
2876 unsigned cmd,
2877 unsigned long arg)
2878{
2879 struct driver_data *dd = dev->bd_disk->private_data;
2880
2881 if (!capable(CAP_SYS_ADMIN))
2882 return -EACCES;
2883
2884 if (!dd)
2885 return -ENOTTY;
2886
2887 switch (cmd) {
2888 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002889 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002890 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06002891 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002892 }
2893}
2894
Jens Axboe16d02c02011-09-27 15:50:01 -06002895#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06002896/*
2897 * Block layer compat IOCTL handler.
2898 *
2899 * @dev Pointer to the block_device structure.
2900 * @mode ignored
2901 * @cmd IOCTL command passed from the user application.
2902 * @arg Argument passed from the user application.
2903 *
2904 * return value
2905 * 0 IOCTL completed successfully.
2906 * -ENOTTY IOCTL not supported or invalid driver data
2907 * structure pointer.
2908 */
2909static int mtip_block_compat_ioctl(struct block_device *dev,
2910 fmode_t mode,
2911 unsigned cmd,
2912 unsigned long arg)
2913{
2914 struct driver_data *dd = dev->bd_disk->private_data;
2915
2916 if (!capable(CAP_SYS_ADMIN))
2917 return -EACCES;
2918
2919 if (!dd)
2920 return -ENOTTY;
2921
2922 switch (cmd) {
2923 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002924 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06002925 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002926 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06002927 ide_task_request_t req_task;
2928 int compat_tasksize, outtotal, ret;
2929
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002930 compat_tasksize =
2931 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06002932
2933 compat_req_task =
2934 (struct mtip_compat_ide_task_request_s __user *) arg;
2935
2936 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002937 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06002938 return -EFAULT;
2939
2940 if (get_user(req_task.out_size, &compat_req_task->out_size))
2941 return -EFAULT;
2942
2943 if (get_user(req_task.in_size, &compat_req_task->in_size))
2944 return -EFAULT;
2945
2946 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2947
2948 ret = exec_drive_taskfile(dd, (void __user *) arg,
2949 &req_task, outtotal);
2950
2951 if (copy_to_user((void __user *) arg, &req_task,
2952 compat_tasksize -
2953 (2 * sizeof(compat_long_t))))
2954 return -EFAULT;
2955
2956 if (put_user(req_task.out_size, &compat_req_task->out_size))
2957 return -EFAULT;
2958
2959 if (put_user(req_task.in_size, &compat_req_task->in_size))
2960 return -EFAULT;
2961
2962 return ret;
2963 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002964 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06002965 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002966 }
2967}
Jens Axboe16d02c02011-09-27 15:50:01 -06002968#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06002969
2970/*
2971 * Obtain the geometry of the device.
2972 *
2973 * You may think that this function is obsolete, but some applications,
2974 * fdisk for example still used CHS values. This function describes the
2975 * device as having 224 heads and 56 sectors per cylinder. These values are
2976 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2977 * partition is described in terms of a start and end cylinder this means
2978 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2979 * affects performance.
2980 *
2981 * @dev Pointer to the block_device strucutre.
2982 * @geo Pointer to a hd_geometry structure.
2983 *
2984 * return value
2985 * 0 Operation completed successfully.
2986 * -ENOTTY An error occurred while reading the drive capacity.
2987 */
2988static int mtip_block_getgeo(struct block_device *dev,
2989 struct hd_geometry *geo)
2990{
2991 struct driver_data *dd = dev->bd_disk->private_data;
2992 sector_t capacity;
2993
2994 if (!dd)
2995 return -ENOTTY;
2996
2997 if (!(mtip_hw_get_capacity(dd, &capacity))) {
2998 dev_warn(&dd->pdev->dev,
2999 "Could not get drive capacity.\n");
3000 return -ENOTTY;
3001 }
3002
3003 geo->heads = 224;
3004 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003005 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003006 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003007 return 0;
3008}
3009
3010/*
3011 * Block device operation function.
3012 *
3013 * This structure contains pointers to the functions required by the block
3014 * layer.
3015 */
3016static const struct block_device_operations mtip_block_ops = {
3017 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003018#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003019 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003020#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003021 .getgeo = mtip_block_getgeo,
3022 .owner = THIS_MODULE
3023};
3024
3025/*
3026 * Block layer make request function.
3027 *
3028 * This function is called by the kernel to process a BIO for
3029 * the P320 device.
3030 *
3031 * @queue Pointer to the request queue. Unused other than to obtain
3032 * the driver data structure.
3033 * @bio Pointer to the BIO.
3034 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003035 */
Jens Axboea71f4832011-11-05 08:36:21 +01003036static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003037{
3038 struct driver_data *dd = queue->queuedata;
3039 struct scatterlist *sg;
3040 struct bio_vec *bvec;
3041 int nents = 0;
3042 int tag = 0;
3043
3044 if (unlikely(!bio_has_data(bio))) {
3045 blk_queue_flush(queue, 0);
3046 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003047 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003048 }
3049
Sam Bradshaw88523a62011-08-30 08:34:26 -06003050 sg = mtip_hw_get_scatterlist(dd, &tag);
3051 if (likely(sg != NULL)) {
3052 blk_queue_bounce(queue, &bio);
3053
3054 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3055 dev_warn(&dd->pdev->dev,
3056 "Maximum number of SGL entries exceeded");
3057 bio_io_error(bio);
3058 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003059 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003060 }
3061
3062 /* Create the scatter list for this bio. */
3063 bio_for_each_segment(bvec, bio, nents) {
3064 sg_set_page(&sg[nents],
3065 bvec->bv_page,
3066 bvec->bv_len,
3067 bvec->bv_offset);
3068 }
3069
3070 /* Issue the read/write. */
3071 mtip_hw_submit_io(dd,
3072 bio->bi_sector,
3073 bio_sectors(bio),
3074 nents,
3075 tag,
3076 bio_endio,
3077 bio,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003078 bio->bi_rw & REQ_FUA,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003079 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003080 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003081 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003082}
3083
3084/*
3085 * Block layer initialization function.
3086 *
3087 * This function is called once by the PCI layer for each P320
3088 * device that is connected to the system.
3089 *
3090 * @dd Pointer to the driver data structure.
3091 *
3092 * return value
3093 * 0 on success else an error code.
3094 */
Jens Axboe63166682011-09-27 21:27:43 -06003095static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003096{
3097 int rv = 0;
3098 sector_t capacity;
3099 unsigned int index = 0;
3100 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003101 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003102
3103 /* Initialize the protocol layer. */
3104 rv = mtip_hw_init(dd);
3105 if (rv < 0) {
3106 dev_err(&dd->pdev->dev,
3107 "Protocol layer initialization failed\n");
3108 rv = -EINVAL;
3109 goto protocol_init_error;
3110 }
3111
3112 /* Allocate the request queue. */
3113 dd->queue = blk_alloc_queue(GFP_KERNEL);
3114 if (dd->queue == NULL) {
3115 dev_err(&dd->pdev->dev,
3116 "Unable to allocate request queue\n");
3117 rv = -ENOMEM;
3118 goto block_queue_alloc_init_error;
3119 }
3120
3121 /* Attach our request function to the request queue. */
3122 blk_queue_make_request(dd->queue, mtip_make_request);
3123
3124 /* Set device limits. */
3125 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3126 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3127 blk_queue_physical_block_size(dd->queue, 4096);
3128 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003129 blk_queue_flush(dd->queue, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003130
3131 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3132 if (dd->disk == NULL) {
3133 dev_err(&dd->pdev->dev,
3134 "Unable to allocate gendisk structure\n");
3135 rv = -EINVAL;
3136 goto alloc_disk_error;
3137 }
3138
3139 /* Generate the disk name, implemented same as in sd.c */
3140 do {
3141 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3142 goto ida_get_error;
3143
3144 spin_lock(&rssd_index_lock);
3145 rv = ida_get_new(&rssd_index_ida, &index);
3146 spin_unlock(&rssd_index_lock);
3147 } while (rv == -EAGAIN);
3148
3149 if (rv)
3150 goto ida_get_error;
3151
3152 rv = rssd_disk_name_format("rssd",
3153 index,
3154 dd->disk->disk_name,
3155 DISK_NAME_LEN);
3156 if (rv)
3157 goto disk_index_error;
3158
3159 dd->disk->driverfs_dev = &dd->pdev->dev;
3160 dd->disk->major = dd->major;
3161 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3162 dd->disk->fops = &mtip_block_ops;
3163 dd->disk->queue = dd->queue;
3164 dd->disk->private_data = dd;
3165 dd->queue->queuedata = dd;
3166 dd->index = index;
3167
3168 /* Set the capacity of the device in 512 byte sectors. */
3169 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3170 dev_warn(&dd->pdev->dev,
3171 "Could not read drive capacity\n");
3172 rv = -EIO;
3173 goto read_capacity_error;
3174 }
3175 set_capacity(dd->disk, capacity);
3176
3177 /* Enable the block device and add it to /dev */
3178 add_disk(dd->disk);
3179
3180 /*
3181 * Now that the disk is active, initialize any sysfs attributes
3182 * managed by the protocol layer.
3183 */
3184 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3185 if (kobj) {
3186 mtip_hw_sysfs_init(dd, kobj);
3187 kobject_put(kobj);
3188 }
3189
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003190 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3191
3192 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3193 dd, thd_name);
3194
3195 if (IS_ERR(dd->mtip_svc_handler)) {
3196 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3197 dd->mtip_svc_handler = NULL;
3198 rv = -EFAULT;
3199 goto read_capacity_error;
3200 }
3201
Sam Bradshaw88523a62011-08-30 08:34:26 -06003202 return rv;
3203
3204read_capacity_error:
3205 /*
3206 * Delete our gendisk structure. This also removes the device
3207 * from /dev
3208 */
3209 del_gendisk(dd->disk);
3210
3211disk_index_error:
3212 spin_lock(&rssd_index_lock);
3213 ida_remove(&rssd_index_ida, index);
3214 spin_unlock(&rssd_index_lock);
3215
3216ida_get_error:
3217 put_disk(dd->disk);
3218
3219alloc_disk_error:
3220 blk_cleanup_queue(dd->queue);
3221
3222block_queue_alloc_init_error:
3223 /* De-initialize the protocol layer. */
3224 mtip_hw_exit(dd);
3225
3226protocol_init_error:
3227 return rv;
3228}
3229
3230/*
3231 * Block layer deinitialization function.
3232 *
3233 * Called by the PCI layer as each P320 device is removed.
3234 *
3235 * @dd Pointer to the driver data structure.
3236 *
3237 * return value
3238 * 0
3239 */
Jens Axboe63166682011-09-27 21:27:43 -06003240static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003241{
3242 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003243
3244 if (dd->mtip_svc_handler) {
3245 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3246 wake_up_interruptible(&dd->port->svc_wait);
3247 kthread_stop(dd->mtip_svc_handler);
3248 }
3249
Sam Bradshaw88523a62011-08-30 08:34:26 -06003250 /* Clean up the sysfs attributes managed by the protocol layer. */
3251 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3252 if (kobj) {
3253 mtip_hw_sysfs_exit(dd, kobj);
3254 kobject_put(kobj);
3255 }
3256
3257 /*
3258 * Delete our gendisk structure. This also removes the device
3259 * from /dev
3260 */
3261 del_gendisk(dd->disk);
3262 blk_cleanup_queue(dd->queue);
3263 dd->disk = NULL;
3264 dd->queue = NULL;
3265
3266 /* De-initialize the protocol layer. */
3267 mtip_hw_exit(dd);
3268
3269 return 0;
3270}
3271
3272/*
3273 * Function called by the PCI layer when just before the
3274 * machine shuts down.
3275 *
3276 * If a protocol layer shutdown function is present it will be called
3277 * by this function.
3278 *
3279 * @dd Pointer to the driver data structure.
3280 *
3281 * return value
3282 * 0
3283 */
Jens Axboe63166682011-09-27 21:27:43 -06003284static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003285{
3286 dev_info(&dd->pdev->dev,
3287 "Shutting down %s ...\n", dd->disk->disk_name);
3288
3289 /* Delete our gendisk structure, and cleanup the blk queue. */
3290 del_gendisk(dd->disk);
3291 blk_cleanup_queue(dd->queue);
3292 dd->disk = NULL;
3293 dd->queue = NULL;
3294
3295 mtip_hw_shutdown(dd);
3296 return 0;
3297}
3298
Jens Axboe63166682011-09-27 21:27:43 -06003299static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003300{
3301 dev_info(&dd->pdev->dev,
3302 "Suspending %s ...\n", dd->disk->disk_name);
3303 mtip_hw_suspend(dd);
3304 return 0;
3305}
3306
Jens Axboe63166682011-09-27 21:27:43 -06003307static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003308{
3309 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3310 dd->disk->disk_name);
3311 mtip_hw_resume(dd);
3312 return 0;
3313}
3314
3315/*
3316 * Called for each supported PCI device detected.
3317 *
3318 * This function allocates the private data structure, enables the
3319 * PCI device and then calls the block layer initialization function.
3320 *
3321 * return value
3322 * 0 on success else an error code.
3323 */
3324static int mtip_pci_probe(struct pci_dev *pdev,
3325 const struct pci_device_id *ent)
3326{
3327 int rv = 0;
3328 struct driver_data *dd = NULL;
3329
3330 /* Allocate memory for this devices private data. */
3331 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3332 if (dd == NULL) {
3333 dev_err(&pdev->dev,
3334 "Unable to allocate memory for driver data\n");
3335 return -ENOMEM;
3336 }
3337
3338 /* Set the atomic variable as 1 in case of SRSI */
3339 atomic_set(&dd->drv_cleanup_done, true);
3340
3341 atomic_set(&dd->resumeflag, false);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003342
3343 /* Attach the private data to this PCI device. */
3344 pci_set_drvdata(pdev, dd);
3345
3346 rv = pcim_enable_device(pdev);
3347 if (rv < 0) {
3348 dev_err(&pdev->dev, "Unable to enable device\n");
3349 goto iomap_err;
3350 }
3351
3352 /* Map BAR5 to memory. */
3353 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3354 if (rv < 0) {
3355 dev_err(&pdev->dev, "Unable to map regions\n");
3356 goto iomap_err;
3357 }
3358
3359 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3360 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3361
3362 if (rv) {
3363 rv = pci_set_consistent_dma_mask(pdev,
3364 DMA_BIT_MASK(32));
3365 if (rv) {
3366 dev_warn(&pdev->dev,
3367 "64-bit DMA enable failed\n");
3368 goto setmask_err;
3369 }
3370 }
3371 }
3372
3373 pci_set_master(pdev);
3374
3375 if (pci_enable_msi(pdev)) {
3376 dev_warn(&pdev->dev,
3377 "Unable to enable MSI interrupt.\n");
3378 goto block_initialize_err;
3379 }
3380
3381 /* Copy the info we may need later into the private data structure. */
3382 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003383 dd->instance = instance;
3384 dd->pdev = pdev;
3385
3386 /* Initialize the block layer. */
3387 rv = mtip_block_initialize(dd);
3388 if (rv < 0) {
3389 dev_err(&pdev->dev,
3390 "Unable to initialize block layer\n");
3391 goto block_initialize_err;
3392 }
3393
3394 /*
3395 * Increment the instance count so that each device has a unique
3396 * instance number.
3397 */
3398 instance++;
3399
3400 goto done;
3401
3402block_initialize_err:
3403 pci_disable_msi(pdev);
3404
3405setmask_err:
3406 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3407
3408iomap_err:
3409 kfree(dd);
3410 pci_set_drvdata(pdev, NULL);
3411 return rv;
3412done:
3413 /* Set the atomic variable as 0 in case of SRSI */
3414 atomic_set(&dd->drv_cleanup_done, true);
3415
3416 return rv;
3417}
3418
3419/*
3420 * Called for each probed device when the device is removed or the
3421 * driver is unloaded.
3422 *
3423 * return value
3424 * None
3425 */
3426static void mtip_pci_remove(struct pci_dev *pdev)
3427{
3428 struct driver_data *dd = pci_get_drvdata(pdev);
3429 int counter = 0;
3430
3431 if (mtip_check_surprise_removal(pdev)) {
3432 while (atomic_read(&dd->drv_cleanup_done) == false) {
3433 counter++;
3434 msleep(20);
3435 if (counter == 10) {
3436 /* Cleanup the outstanding commands */
3437 mtip_command_cleanup(dd);
3438 break;
3439 }
3440 }
3441 }
3442 /* Set the atomic variable as 1 in case of SRSI */
3443 atomic_set(&dd->drv_cleanup_done, true);
3444
3445 /* Clean up the block layer. */
3446 mtip_block_remove(dd);
3447
3448 pci_disable_msi(pdev);
3449
3450 kfree(dd);
3451 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3452}
3453
3454/*
3455 * Called for each probed device when the device is suspended.
3456 *
3457 * return value
3458 * 0 Success
3459 * <0 Error
3460 */
3461static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3462{
3463 int rv = 0;
3464 struct driver_data *dd = pci_get_drvdata(pdev);
3465
3466 if (!dd) {
3467 dev_err(&pdev->dev,
3468 "Driver private datastructure is NULL\n");
3469 return -EFAULT;
3470 }
3471
3472 atomic_set(&dd->resumeflag, true);
3473
3474 /* Disable ports & interrupts then send standby immediate */
3475 rv = mtip_block_suspend(dd);
3476 if (rv < 0) {
3477 dev_err(&pdev->dev,
3478 "Failed to suspend controller\n");
3479 return rv;
3480 }
3481
3482 /*
3483 * Save the pci config space to pdev structure &
3484 * disable the device
3485 */
3486 pci_save_state(pdev);
3487 pci_disable_device(pdev);
3488
3489 /* Move to Low power state*/
3490 pci_set_power_state(pdev, PCI_D3hot);
3491
3492 return rv;
3493}
3494
3495/*
3496 * Called for each probed device when the device is resumed.
3497 *
3498 * return value
3499 * 0 Success
3500 * <0 Error
3501 */
3502static int mtip_pci_resume(struct pci_dev *pdev)
3503{
3504 int rv = 0;
3505 struct driver_data *dd;
3506
3507 dd = pci_get_drvdata(pdev);
3508 if (!dd) {
3509 dev_err(&pdev->dev,
3510 "Driver private datastructure is NULL\n");
3511 return -EFAULT;
3512 }
3513
3514 /* Move the device to active State */
3515 pci_set_power_state(pdev, PCI_D0);
3516
3517 /* Restore PCI configuration space */
3518 pci_restore_state(pdev);
3519
3520 /* Enable the PCI device*/
3521 rv = pcim_enable_device(pdev);
3522 if (rv < 0) {
3523 dev_err(&pdev->dev,
3524 "Failed to enable card during resume\n");
3525 goto err;
3526 }
3527 pci_set_master(pdev);
3528
3529 /*
3530 * Calls hbaReset, initPort, & startPort function
3531 * then enables interrupts
3532 */
3533 rv = mtip_block_resume(dd);
3534 if (rv < 0)
3535 dev_err(&pdev->dev, "Unable to resume\n");
3536
3537err:
3538 atomic_set(&dd->resumeflag, false);
3539
3540 return rv;
3541}
3542
3543/*
3544 * Shutdown routine
3545 *
3546 * return value
3547 * None
3548 */
3549static void mtip_pci_shutdown(struct pci_dev *pdev)
3550{
3551 struct driver_data *dd = pci_get_drvdata(pdev);
3552 if (dd)
3553 mtip_block_shutdown(dd);
3554}
3555
Sam Bradshaw88523a62011-08-30 08:34:26 -06003556/* Table of device ids supported by this driver. */
3557static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3558 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3559 { 0 }
3560};
3561
3562/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003563static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003564 .name = MTIP_DRV_NAME,
3565 .id_table = mtip_pci_tbl,
3566 .probe = mtip_pci_probe,
3567 .remove = mtip_pci_remove,
3568 .suspend = mtip_pci_suspend,
3569 .resume = mtip_pci_resume,
3570 .shutdown = mtip_pci_shutdown,
3571};
3572
3573MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3574
3575/*
3576 * Module initialization function.
3577 *
3578 * Called once when the module is loaded. This function allocates a major
3579 * block device number to the Cyclone devices and registers the PCI layer
3580 * of the driver.
3581 *
3582 * Return value
3583 * 0 on success else error code.
3584 */
3585static int __init mtip_init(void)
3586{
3587 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3588
3589 /* Allocate a major block device number to use with this driver. */
3590 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3591 if (mtip_major < 0) {
3592 printk(KERN_ERR "Unable to register block device (%d)\n",
3593 mtip_major);
3594 return -EBUSY;
3595 }
3596
3597 /* Register our PCI operations. */
3598 return pci_register_driver(&mtip_pci_driver);
3599}
3600
3601/*
3602 * Module de-initialization function.
3603 *
3604 * Called once when the module is unloaded. This function deallocates
3605 * the major block device number allocated by mtip_init() and
3606 * unregisters the PCI layer of the driver.
3607 *
3608 * Return value
3609 * none
3610 */
3611static void __exit mtip_exit(void)
3612{
3613 /* Release the allocated major block device number. */
3614 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3615
3616 /* Unregister the PCI driver. */
3617 pci_unregister_driver(&mtip_pci_driver);
3618}
3619
3620MODULE_AUTHOR("Micron Technology, Inc");
3621MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3622MODULE_LICENSE("GPL");
3623MODULE_VERSION(MTIP_DRV_VERSION);
3624
3625module_init(mtip_init);
3626module_exit(mtip_exit);