blob: 8eb81c96608fbc47aadc382015a0e44db2b4599a [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
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010090static int mtip_block_initialize(struct driver_data *dd);
91
Jens Axboe16d02c02011-09-27 15:50:01 -060092#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060093struct mtip_compat_ide_task_request_s {
94 __u8 io_ports[8];
95 __u8 hob_ports[8];
96 ide_reg_valid_t out_flags;
97 ide_reg_valid_t in_flags;
98 int data_phase;
99 int req_cmd;
100 compat_ulong_t out_size;
101 compat_ulong_t in_size;
102};
Jens Axboe16d02c02011-09-27 15:50:01 -0600103#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600104
Sam Bradshaw88523a62011-08-30 08:34:26 -0600105/*
Jens Axboe63166682011-09-27 21:27:43 -0600106 * This function check_for_surprise_removal is called
107 * while card is removed from the system and it will
108 * read the vendor id from the configration space
109 *
110 * @pdev Pointer to the pci_dev structure.
111 *
112 * return value
113 * true if device removed, else false
114 */
115static bool mtip_check_surprise_removal(struct pci_dev *pdev)
116{
117 u16 vendor_id = 0;
118
119 /* Read the vendorID from the configuration space */
120 pci_read_config_word(pdev, 0x00, &vendor_id);
121 if (vendor_id == 0xFFFF)
122 return true; /* device removed */
123
124 return false; /* device present */
125}
126
127/*
128 * This function is called for clean the pending command in the
129 * command slot during the surprise removal of device and return
130 * error to the upper layer.
131 *
132 * @dd Pointer to the DRIVER_DATA structure.
133 *
134 * return value
135 * None
136 */
137static void mtip_command_cleanup(struct driver_data *dd)
138{
139 int group = 0, commandslot = 0, commandindex = 0;
140 struct mtip_cmd *command;
141 struct mtip_port *port = dd->port;
142
143 for (group = 0; group < 4; group++) {
144 for (commandslot = 0; commandslot < 32; commandslot++) {
145 if (!(port->allocated[group] & (1 << commandslot)))
146 continue;
147
148 commandindex = group << 5 | commandslot;
149 command = &port->commands[commandindex];
150
151 if (atomic_read(&command->active)
152 && (command->async_callback)) {
153 command->async_callback(command->async_data,
154 -ENODEV);
155 command->async_callback = NULL;
156 command->async_data = NULL;
157 }
158
159 dma_unmap_sg(&port->dd->pdev->dev,
160 command->sg,
161 command->scatter_ents,
162 command->direction);
163 }
164 }
165
166 up(&port->cmd_slot);
167
168 atomic_set(&dd->drv_cleanup_done, true);
169}
170
171/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600172 * Obtain an empty command slot.
173 *
174 * This function needs to be reentrant since it could be called
175 * at the same time on multiple CPUs. The allocation of the
176 * command slot must be atomic.
177 *
178 * @port Pointer to the port data structure.
179 *
180 * return value
181 * >= 0 Index of command slot obtained.
182 * -1 No command slots available.
183 */
184static int get_slot(struct mtip_port *port)
185{
186 int slot, i;
187 unsigned int num_command_slots = port->dd->slot_groups * 32;
188
189 /*
190 * Try 10 times, because there is a small race here.
191 * that's ok, because it's still cheaper than a lock.
192 *
193 * Race: Since this section is not protected by lock, same bit
194 * could be chosen by different process contexts running in
195 * different processor. So instead of costly lock, we are going
196 * with loop.
197 */
198 for (i = 0; i < 10; i++) {
199 slot = find_next_zero_bit(port->allocated,
200 num_command_slots, 1);
201 if ((slot < num_command_slots) &&
202 (!test_and_set_bit(slot, port->allocated)))
203 return slot;
204 }
205 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
206
207 if (mtip_check_surprise_removal(port->dd->pdev)) {
208 /* Device not present, clean outstanding commands */
209 mtip_command_cleanup(port->dd);
210 }
211 return -1;
212}
213
214/*
215 * Release a command slot.
216 *
217 * @port Pointer to the port data structure.
218 * @tag Tag of command to release
219 *
220 * return value
221 * None
222 */
223static inline void release_slot(struct mtip_port *port, int tag)
224{
225 smp_mb__before_clear_bit();
226 clear_bit(tag, port->allocated);
227 smp_mb__after_clear_bit();
228}
229
230/*
Jens Axboe63166682011-09-27 21:27:43 -0600231 * Reset the HBA (without sleeping)
232 *
233 * Just like hba_reset, except does not call sleep, so can be
234 * run from interrupt/tasklet context.
235 *
236 * @dd Pointer to the driver data structure.
237 *
238 * return value
239 * 0 The reset was successful.
240 * -1 The HBA Reset bit did not clear.
241 */
242static int hba_reset_nosleep(struct driver_data *dd)
243{
244 unsigned long timeout;
245
246 /* Chip quirk: quiesce any chip function */
247 mdelay(10);
248
249 /* Set the reset bit */
250 writel(HOST_RESET, dd->mmio + HOST_CTL);
251
252 /* Flush */
253 readl(dd->mmio + HOST_CTL);
254
255 /*
256 * Wait 10ms then spin for up to 1 second
257 * waiting for reset acknowledgement
258 */
259 timeout = jiffies + msecs_to_jiffies(1000);
260 mdelay(10);
261 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
262 && time_before(jiffies, timeout))
263 mdelay(1);
264
265 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
266 return -1;
267
268 return 0;
269}
270
271/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600272 * Issue a command to the hardware.
273 *
274 * Set the appropriate bit in the s_active and Command Issue hardware
275 * registers, causing hardware command processing to begin.
276 *
277 * @port Pointer to the port structure.
278 * @tag The tag of the command to be issued.
279 *
280 * return value
281 * None
282 */
283static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
284{
285 unsigned long flags = 0;
286
287 atomic_set(&port->commands[tag].active, 1);
288
289 spin_lock_irqsave(&port->cmd_issue_lock, flags);
290
291 writel((1 << MTIP_TAG_BIT(tag)),
292 port->s_active[MTIP_TAG_INDEX(tag)]);
293 writel((1 << MTIP_TAG_BIT(tag)),
294 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
295
296 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
297}
298
299/*
Jens Axboe63166682011-09-27 21:27:43 -0600300 * Enable/disable the reception of FIS
301 *
302 * @port Pointer to the port data structure
303 * @enable 1 to enable, 0 to disable
304 *
305 * return value
306 * Previous state: 1 enabled, 0 disabled
307 */
308static int mtip_enable_fis(struct mtip_port *port, int enable)
309{
310 u32 tmp;
311
312 /* enable FIS reception */
313 tmp = readl(port->mmio + PORT_CMD);
314 if (enable)
315 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
316 else
317 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
318
319 /* Flush */
320 readl(port->mmio + PORT_CMD);
321
322 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
323}
324
325/*
326 * Enable/disable the DMA engine
327 *
328 * @port Pointer to the port data structure
329 * @enable 1 to enable, 0 to disable
330 *
331 * return value
332 * Previous state: 1 enabled, 0 disabled.
333 */
334static int mtip_enable_engine(struct mtip_port *port, int enable)
335{
336 u32 tmp;
337
338 /* enable FIS reception */
339 tmp = readl(port->mmio + PORT_CMD);
340 if (enable)
341 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
342 else
343 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
344
345 readl(port->mmio + PORT_CMD);
346 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
347}
348
349/*
350 * Enables the port DMA engine and FIS reception.
351 *
352 * return value
353 * None
354 */
355static inline void mtip_start_port(struct mtip_port *port)
356{
357 /* Enable FIS reception */
358 mtip_enable_fis(port, 1);
359
360 /* Enable the DMA engine */
361 mtip_enable_engine(port, 1);
362}
363
364/*
365 * Deinitialize a port by disabling port interrupts, the DMA engine,
366 * and FIS reception.
367 *
368 * @port Pointer to the port structure
369 *
370 * return value
371 * None
372 */
373static inline void mtip_deinit_port(struct mtip_port *port)
374{
375 /* Disable interrupts on this port */
376 writel(0, port->mmio + PORT_IRQ_MASK);
377
378 /* Disable the DMA engine */
379 mtip_enable_engine(port, 0);
380
381 /* Disable FIS reception */
382 mtip_enable_fis(port, 0);
383}
384
385/*
386 * Initialize a port.
387 *
388 * This function deinitializes the port by calling mtip_deinit_port() and
389 * then initializes it by setting the command header and RX FIS addresses,
390 * clearing the SError register and any pending port interrupts before
391 * re-enabling the default set of port interrupts.
392 *
393 * @port Pointer to the port structure.
394 *
395 * return value
396 * None
397 */
398static void mtip_init_port(struct mtip_port *port)
399{
400 int i;
401 mtip_deinit_port(port);
402
403 /* Program the command list base and FIS base addresses */
404 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
405 writel((port->command_list_dma >> 16) >> 16,
406 port->mmio + PORT_LST_ADDR_HI);
407 writel((port->rxfis_dma >> 16) >> 16,
408 port->mmio + PORT_FIS_ADDR_HI);
409 }
410
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100411 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600412 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100413 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600414
415 /* Clear SError */
416 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
417
418 /* reset the completed registers.*/
419 for (i = 0; i < port->dd->slot_groups; i++)
420 writel(0xFFFFFFFF, port->completed[i]);
421
422 /* Clear any pending interrupts for this port */
423 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
424
425 /* Enable port interrupts */
426 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
427}
428
429/*
430 * Restart a port
431 *
432 * @port Pointer to the port data structure.
433 *
434 * return value
435 * None
436 */
437static void mtip_restart_port(struct mtip_port *port)
438{
439 unsigned long timeout;
440
441 /* Disable the DMA engine */
442 mtip_enable_engine(port, 0);
443
444 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
445 timeout = jiffies + msecs_to_jiffies(500);
446 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
447 && time_before(jiffies, timeout))
448 ;
449
450 /*
451 * Chip quirk: escalate to hba reset if
452 * PxCMD.CR not clear after 500 ms
453 */
454 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
455 dev_warn(&port->dd->pdev->dev,
456 "PxCMD.CR not clear, escalating reset\n");
457
458 if (hba_reset_nosleep(port->dd))
459 dev_err(&port->dd->pdev->dev,
460 "HBA reset escalation failed.\n");
461
462 /* 30 ms delay before com reset to quiesce chip */
463 mdelay(30);
464 }
465
466 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
467
468 /* Set PxSCTL.DET */
469 writel(readl(port->mmio + PORT_SCR_CTL) |
470 1, port->mmio + PORT_SCR_CTL);
471 readl(port->mmio + PORT_SCR_CTL);
472
473 /* Wait 1 ms to quiesce chip function */
474 timeout = jiffies + msecs_to_jiffies(1);
475 while (time_before(jiffies, timeout))
476 ;
477
478 /* Clear PxSCTL.DET */
479 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
480 port->mmio + PORT_SCR_CTL);
481 readl(port->mmio + PORT_SCR_CTL);
482
483 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
484 timeout = jiffies + msecs_to_jiffies(500);
485 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
486 && time_before(jiffies, timeout))
487 ;
488
489 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
490 dev_warn(&port->dd->pdev->dev,
491 "COM reset failed\n");
492
493 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
494 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
495
496 /* Enable the DMA engine */
497 mtip_enable_engine(port, 1);
498}
499
500/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600501 * Called periodically to see if any read/write commands are
502 * taking too long to complete.
503 *
504 * @data Pointer to the PORT data structure.
505 *
506 * return value
507 * None
508 */
Jens Axboe63166682011-09-27 21:27:43 -0600509static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600510{
511 struct mtip_port *port = (struct mtip_port *) data;
512 struct host_to_dev_fis *fis;
513 struct mtip_cmd *command;
514 int tag, cmdto_cnt = 0;
515 unsigned int bit, group;
516 unsigned int num_command_slots = port->dd->slot_groups * 32;
517
518 if (unlikely(!port))
519 return;
520
521 if (atomic_read(&port->dd->resumeflag) == true) {
522 mod_timer(&port->cmd_timer,
523 jiffies + msecs_to_jiffies(30000));
524 return;
525 }
526
527 for (tag = 0; tag < num_command_slots; tag++) {
528 /*
529 * Skip internal command slot as it has
530 * its own timeout mechanism
531 */
532 if (tag == MTIP_TAG_INTERNAL)
533 continue;
534
535 if (atomic_read(&port->commands[tag].active) &&
536 (time_after(jiffies, port->commands[tag].comp_time))) {
537 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100538 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600539
540 command = &port->commands[tag];
541 fis = (struct host_to_dev_fis *) command->command;
542
543 dev_warn(&port->dd->pdev->dev,
544 "Timeout for command tag %d\n", tag);
545
546 cmdto_cnt++;
547 if (cmdto_cnt == 1)
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100548 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600549
550 /*
551 * Clear the completed bit. This should prevent
552 * any interrupt handlers from trying to retire
553 * the command.
554 */
555 writel(1 << bit, port->completed[group]);
556
557 /* Call the async completion callback. */
558 if (likely(command->async_callback))
559 command->async_callback(command->async_data,
560 -EIO);
561 command->async_callback = NULL;
562 command->comp_func = NULL;
563
564 /* Unmap the DMA scatter list entries */
565 dma_unmap_sg(&port->dd->pdev->dev,
566 command->sg,
567 command->scatter_ents,
568 command->direction);
569
570 /*
571 * Clear the allocated bit and active tag for the
572 * command.
573 */
574 atomic_set(&port->commands[tag].active, 0);
575 release_slot(port, tag);
576
577 up(&port->cmd_slot);
578 }
579 }
580
581 if (cmdto_cnt) {
582 dev_warn(&port->dd->pdev->dev,
583 "%d commands timed out: restarting port",
584 cmdto_cnt);
585 mtip_restart_port(port);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100586 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
587 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600588 }
589
590 /* Restart the timer */
591 mod_timer(&port->cmd_timer,
592 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
593}
594
595/*
596 * IO completion function.
597 *
598 * This completion function is called by the driver ISR when a
599 * command that was issued by the kernel completes. It first calls the
600 * asynchronous completion function which normally calls back into the block
601 * layer passing the asynchronous callback data, then unmaps the
602 * scatter list associated with the completed command, and finally
603 * clears the allocated bit associated with the completed command.
604 *
605 * @port Pointer to the port data structure.
606 * @tag Tag of the command.
607 * @data Pointer to driver_data.
608 * @status Completion status.
609 *
610 * return value
611 * None
612 */
613static void mtip_async_complete(struct mtip_port *port,
614 int tag,
615 void *data,
616 int status)
617{
618 struct mtip_cmd *command;
619 struct driver_data *dd = data;
620 int cb_status = status ? -EIO : 0;
621
622 if (unlikely(!dd) || unlikely(!port))
623 return;
624
625 command = &port->commands[tag];
626
627 if (unlikely(status == PORT_IRQ_TF_ERR)) {
628 dev_warn(&port->dd->pdev->dev,
629 "Command tag %d failed due to TFE\n", tag);
630 }
631
632 /* Upper layer callback */
633 if (likely(command->async_callback))
634 command->async_callback(command->async_data, cb_status);
635
636 command->async_callback = NULL;
637 command->comp_func = NULL;
638
639 /* Unmap the DMA scatter list entries */
640 dma_unmap_sg(&dd->pdev->dev,
641 command->sg,
642 command->scatter_ents,
643 command->direction);
644
645 /* Clear the allocated and active bits for the command */
646 atomic_set(&port->commands[tag].active, 0);
647 release_slot(port, tag);
648
649 up(&port->cmd_slot);
650}
651
652/*
653 * Internal command completion callback function.
654 *
655 * This function is normally called by the driver ISR when an internal
656 * command completed. This function signals the command completion by
657 * calling complete().
658 *
659 * @port Pointer to the port data structure.
660 * @tag Tag of the command that has completed.
661 * @data Pointer to a completion structure.
662 * @status Completion status.
663 *
664 * return value
665 * None
666 */
667static void mtip_completion(struct mtip_port *port,
668 int tag,
669 void *data,
670 int status)
671{
672 struct mtip_cmd *command = &port->commands[tag];
673 struct completion *waiting = data;
674 if (unlikely(status == PORT_IRQ_TF_ERR))
675 dev_warn(&port->dd->pdev->dev,
676 "Internal command %d completed with TFE\n", tag);
677
678 command->async_callback = NULL;
679 command->comp_func = NULL;
680
681 complete(waiting);
682}
683
684/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600685 * Helper function for tag logging
686 */
687static void print_tags(struct driver_data *dd,
688 char *msg,
689 unsigned long *tagbits)
690{
691 unsigned int tag, count = 0;
692
693 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
694 if (test_bit(tag, tagbits))
695 count++;
696 }
697 if (count)
698 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
699}
700
701/*
702 * Handle an error.
703 *
704 * @dd Pointer to the DRIVER_DATA structure.
705 *
706 * return value
707 * None
708 */
709static void mtip_handle_tfe(struct driver_data *dd)
710{
711 int group, tag, bit, reissue;
712 struct mtip_port *port;
713 struct mtip_cmd *command;
714 u32 completed;
715 struct host_to_dev_fis *fis;
716 unsigned long tagaccum[SLOTBITS_IN_LONGS];
717
718 dev_warn(&dd->pdev->dev, "Taskfile error\n");
719
720 port = dd->port;
721
722 /* Stop the timer to prevent command timeouts. */
723 del_timer(&port->cmd_timer);
724
725 /* Set eh_active */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100726 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600727
728 /* Loop through all the groups */
729 for (group = 0; group < dd->slot_groups; group++) {
730 completed = readl(port->completed[group]);
731
732 /* clear completed status register in the hardware.*/
733 writel(completed, port->completed[group]);
734
735 /* clear the tag accumulator */
736 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
737
738 /* Process successfully completed commands */
739 for (bit = 0; bit < 32 && completed; bit++) {
740 if (!(completed & (1<<bit)))
741 continue;
742 tag = (group << 5) + bit;
743
744 /* Skip the internal command slot */
745 if (tag == MTIP_TAG_INTERNAL)
746 continue;
747
748 command = &port->commands[tag];
749 if (likely(command->comp_func)) {
750 set_bit(tag, tagaccum);
751 atomic_set(&port->commands[tag].active, 0);
752 command->comp_func(port,
753 tag,
754 command->comp_data,
755 0);
756 } else {
757 dev_err(&port->dd->pdev->dev,
758 "Missing completion func for tag %d",
759 tag);
760 if (mtip_check_surprise_removal(dd->pdev)) {
761 mtip_command_cleanup(dd);
762 /* don't proceed further */
763 return;
764 }
765 }
766 }
767 }
768 print_tags(dd, "TFE tags completed:", tagaccum);
769
770 /* Restart the port */
771 mdelay(20);
772 mtip_restart_port(port);
773
774 /* clear the tag accumulator */
775 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
776
777 /* Loop through all the groups */
778 for (group = 0; group < dd->slot_groups; group++) {
779 for (bit = 0; bit < 32; bit++) {
780 reissue = 1;
781 tag = (group << 5) + bit;
782
783 /* If the active bit is set re-issue the command */
784 if (atomic_read(&port->commands[tag].active) == 0)
785 continue;
786
787 fis = (struct host_to_dev_fis *)
788 port->commands[tag].command;
789
790 /* Should re-issue? */
791 if (tag == MTIP_TAG_INTERNAL ||
792 fis->command == ATA_CMD_SET_FEATURES)
793 reissue = 0;
794
795 /*
796 * First check if this command has
797 * exceeded its retries.
798 */
799 if (reissue &&
800 (port->commands[tag].retries-- > 0)) {
801
802 set_bit(tag, tagaccum);
803
804 /* Update the timeout value. */
805 port->commands[tag].comp_time =
806 jiffies + msecs_to_jiffies(
807 MTIP_NCQ_COMMAND_TIMEOUT_MS);
808 /* Re-issue the command. */
809 mtip_issue_ncq_command(port, tag);
810
811 continue;
812 }
813
814 /* Retire a command that will not be reissued */
815 dev_warn(&port->dd->pdev->dev,
816 "retiring tag %d\n", tag);
817 atomic_set(&port->commands[tag].active, 0);
818
819 if (port->commands[tag].comp_func)
820 port->commands[tag].comp_func(
821 port,
822 tag,
823 port->commands[tag].comp_data,
824 PORT_IRQ_TF_ERR);
825 else
826 dev_warn(&port->dd->pdev->dev,
827 "Bad completion for tag %d\n",
828 tag);
829 }
830 }
831 print_tags(dd, "TFE tags reissued:", tagaccum);
832
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100833 /* clear eh_active */
834 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
835 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600836
837 mod_timer(&port->cmd_timer,
838 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
839}
840
841/*
842 * Handle a set device bits interrupt
843 */
844static inline void mtip_process_sdbf(struct driver_data *dd)
845{
846 struct mtip_port *port = dd->port;
847 int group, tag, bit;
848 u32 completed;
849 struct mtip_cmd *command;
850
851 /* walk all bits in all slot groups */
852 for (group = 0; group < dd->slot_groups; group++) {
853 completed = readl(port->completed[group]);
854
855 /* clear completed status register in the hardware.*/
856 writel(completed, port->completed[group]);
857
858 /* Process completed commands. */
859 for (bit = 0;
860 (bit < 32) && completed;
861 bit++, completed >>= 1) {
862 if (completed & 0x01) {
863 tag = (group << 5) | bit;
864
865 /* skip internal command slot. */
866 if (unlikely(tag == MTIP_TAG_INTERNAL))
867 continue;
868
869 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600870 /* make internal callback */
871 if (likely(command->comp_func)) {
872 command->comp_func(
873 port,
874 tag,
875 command->comp_data,
876 0);
877 } else {
878 dev_warn(&dd->pdev->dev,
879 "Null completion "
880 "for tag %d",
881 tag);
882
883 if (mtip_check_surprise_removal(
884 dd->pdev)) {
885 mtip_command_cleanup(dd);
886 return;
887 }
888 }
889 }
890 }
891 }
892}
893
894/*
895 * Process legacy pio and d2h interrupts
896 */
897static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
898{
899 struct mtip_port *port = dd->port;
900 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
901
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100902 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
903 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600904 & (1 << MTIP_TAG_INTERNAL))) {
905 if (cmd->comp_func) {
906 cmd->comp_func(port,
907 MTIP_TAG_INTERNAL,
908 cmd->comp_data,
909 0);
910 return;
911 }
912 }
913
914 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
915
916 return;
917}
918
919/*
920 * Demux and handle errors
921 */
922static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
923{
924 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
925 mtip_handle_tfe(dd);
926
927 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
928 dev_warn(&dd->pdev->dev,
929 "Clearing PxSERR.DIAG.x\n");
930 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
931 }
932
933 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
934 dev_warn(&dd->pdev->dev,
935 "Clearing PxSERR.DIAG.n\n");
936 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
937 }
938
939 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
940 dev_warn(&dd->pdev->dev,
941 "Port stat errors %x unhandled\n",
942 (port_stat & ~PORT_IRQ_HANDLED));
943 }
944}
945
946static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
947{
948 struct driver_data *dd = (struct driver_data *) data;
949 struct mtip_port *port = dd->port;
950 u32 hba_stat, port_stat;
951 int rv = IRQ_NONE;
952
953 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
954 if (hba_stat) {
955 rv = IRQ_HANDLED;
956
957 /* Acknowledge the interrupt status on the port.*/
958 port_stat = readl(port->mmio + PORT_IRQ_STAT);
959 writel(port_stat, port->mmio + PORT_IRQ_STAT);
960
961 /* Demux port status */
962 if (likely(port_stat & PORT_IRQ_SDB_FIS))
963 mtip_process_sdbf(dd);
964
965 if (unlikely(port_stat & PORT_IRQ_ERR)) {
966 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
967 mtip_command_cleanup(dd);
968 /* don't proceed further */
969 return IRQ_HANDLED;
970 }
971
972 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
973 }
974
975 if (unlikely(port_stat & PORT_IRQ_LEGACY))
976 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
977 }
978
979 /* acknowledge interrupt */
980 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
981
982 return rv;
983}
984
985/*
986 * Wrapper for mtip_handle_irq
987 * (ignores return code)
988 */
989static void mtip_tasklet(unsigned long data)
990{
991 mtip_handle_irq((struct driver_data *) data);
992}
993
994/*
995 * HBA interrupt subroutine.
996 *
997 * @irq IRQ number.
998 * @instance Pointer to the driver data structure.
999 *
1000 * return value
1001 * IRQ_HANDLED A HBA interrupt was pending and handled.
1002 * IRQ_NONE This interrupt was not for the HBA.
1003 */
1004static irqreturn_t mtip_irq_handler(int irq, void *instance)
1005{
1006 struct driver_data *dd = instance;
1007 tasklet_schedule(&dd->tasklet);
1008 return IRQ_HANDLED;
1009}
1010
1011static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1012{
1013 atomic_set(&port->commands[tag].active, 1);
1014 writel(1 << MTIP_TAG_BIT(tag),
1015 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1016}
1017
1018/*
1019 * Wait for port to quiesce
1020 *
1021 * @port Pointer to port data structure
1022 * @timeout Max duration to wait (ms)
1023 *
1024 * return value
1025 * 0 Success
1026 * -EBUSY Commands still active
1027 */
1028static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1029{
1030 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001031 unsigned int n;
1032 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033
1034 to = jiffies + msecs_to_jiffies(timeout);
1035 do {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01001036 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1037 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001038 msleep(20);
1039 continue; /* svc thd is actively issuing commands */
1040 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001041 /*
1042 * Ignore s_active bit 0 of array element 0.
1043 * This bit will always be set
1044 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001045 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001046 for (n = 1; n < port->dd->slot_groups; n++)
1047 active |= readl(port->s_active[n]);
1048
1049 if (!active)
1050 break;
1051
1052 msleep(20);
1053 } while (time_before(jiffies, to));
1054
1055 return active ? -EBUSY : 0;
1056}
1057
1058/*
1059 * Execute an internal command and wait for the completion.
1060 *
1061 * @port Pointer to the port data structure.
1062 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001063 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001064 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001065 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001066 * @opts Command header options, excluding the FIS length
1067 * and the number of PRD entries.
1068 * @timeout Time in ms to wait for the command to complete.
1069 *
1070 * return value
1071 * 0 Command completed successfully.
1072 * -EFAULT The buffer address is not correctly aligned.
1073 * -EBUSY Internal command or other IO in progress.
1074 * -EAGAIN Time out waiting for command to complete.
1075 */
1076static int mtip_exec_internal_command(struct mtip_port *port,
1077 void *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001078 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001079 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001080 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 u32 opts,
1082 gfp_t atomic,
1083 unsigned long timeout)
1084{
1085 struct mtip_cmd_sg *command_sg;
1086 DECLARE_COMPLETION_ONSTACK(wait);
1087 int rv = 0;
1088 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1089
1090 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1091 if (buffer & 0x00000007) {
1092 dev_err(&port->dd->pdev->dev,
1093 "SG buffer is not 8 byte aligned\n");
1094 return -EFAULT;
1095 }
1096
1097 /* Only one internal command should be running at a time */
1098 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1099 dev_warn(&port->dd->pdev->dev,
1100 "Internal command already active\n");
1101 return -EBUSY;
1102 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001103 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001104
1105 if (atomic == GFP_KERNEL) {
1106 /* wait for io to complete if non atomic */
1107 if (mtip_quiesce_io(port, 5000) < 0) {
1108 dev_warn(&port->dd->pdev->dev,
1109 "Failed to quiesce IO\n");
1110 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001111 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1112 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001113 return -EBUSY;
1114 }
1115
1116 /* Set the completion function and data for the command. */
1117 int_cmd->comp_data = &wait;
1118 int_cmd->comp_func = mtip_completion;
1119
1120 } else {
1121 /* Clear completion - we're going to poll */
1122 int_cmd->comp_data = NULL;
1123 int_cmd->comp_func = NULL;
1124 }
1125
1126 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001127 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001128
1129 /* Populate the SG list */
1130 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001131 __force_bit2int cpu_to_le32(opts | fis_len);
1132 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1134
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001135 command_sg->info =
1136 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1137 command_sg->dba =
1138 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1139 command_sg->dba_upper =
1140 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001141
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001142 int_cmd->command_header->opts |=
1143 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001144 }
1145
1146 /* Populate the command header */
1147 int_cmd->command_header->byte_count = 0;
1148
1149 /* Issue the command to the hardware */
1150 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1151
1152 /* Poll if atomic, wait_for_completion otherwise */
1153 if (atomic == GFP_KERNEL) {
1154 /* Wait for the command to complete or timeout. */
1155 if (wait_for_completion_timeout(
1156 &wait,
1157 msecs_to_jiffies(timeout)) == 0) {
1158 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001159 "Internal command did not complete [%d] "
1160 "within timeout of %lu ms\n",
1161 atomic, timeout);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001162 rv = -EAGAIN;
1163 }
1164
1165 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1166 & (1 << MTIP_TAG_INTERNAL)) {
1167 dev_warn(&port->dd->pdev->dev,
1168 "Retiring internal command but CI is 1.\n");
1169 }
1170
1171 } else {
1172 /* Spin for <timeout> checking if command still outstanding */
1173 timeout = jiffies + msecs_to_jiffies(timeout);
1174
1175 while ((readl(
1176 port->cmd_issue[MTIP_TAG_INTERNAL])
1177 & (1 << MTIP_TAG_INTERNAL))
1178 && time_before(jiffies, timeout))
1179 ;
1180
1181 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1182 & (1 << MTIP_TAG_INTERNAL)) {
1183 dev_err(&port->dd->pdev->dev,
1184 "Internal command did not complete [%d]\n",
1185 atomic);
1186 rv = -EAGAIN;
1187 }
1188 }
1189
1190 /* Clear the allocated and active bits for the internal command. */
1191 atomic_set(&int_cmd->active, 0);
1192 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001193 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1194 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001195
1196 return rv;
1197}
1198
1199/*
1200 * Byte-swap ATA ID strings.
1201 *
1202 * ATA identify data contains strings in byte-swapped 16-bit words.
1203 * They must be swapped (on all architectures) to be usable as C strings.
1204 * This function swaps bytes in-place.
1205 *
1206 * @buf The buffer location of the string
1207 * @len The number of bytes to swap
1208 *
1209 * return value
1210 * None
1211 */
1212static inline void ata_swap_string(u16 *buf, unsigned int len)
1213{
1214 int i;
1215 for (i = 0; i < (len/2); i++)
1216 be16_to_cpus(&buf[i]);
1217}
1218
1219/*
1220 * Request the device identity information.
1221 *
1222 * If a user space buffer is not specified, i.e. is NULL, the
1223 * identify information is still read from the drive and placed
1224 * into the identify data buffer (@e port->identify) in the
1225 * port data structure.
1226 * When the identify buffer contains valid identify information @e
1227 * port->identify_valid is non-zero.
1228 *
1229 * @port Pointer to the port structure.
1230 * @user_buffer A user space buffer where the identify data should be
1231 * copied.
1232 *
1233 * return value
1234 * 0 Command completed successfully.
1235 * -EFAULT An error occurred while coping data to the user buffer.
1236 * -1 Command failed.
1237 */
1238static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1239{
1240 int rv = 0;
1241 struct host_to_dev_fis fis;
1242
Sam Bradshaw88523a62011-08-30 08:34:26 -06001243 /* Build the FIS. */
1244 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1245 fis.type = 0x27;
1246 fis.opts = 1 << 7;
1247 fis.command = ATA_CMD_ID_ATA;
1248
1249 /* Set the identify information as invalid. */
1250 port->identify_valid = 0;
1251
1252 /* Clear the identify information. */
1253 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1254
1255 /* Execute the command. */
1256 if (mtip_exec_internal_command(port,
1257 &fis,
1258 5,
1259 port->identify_dma,
1260 sizeof(u16) * ATA_ID_WORDS,
1261 0,
1262 GFP_KERNEL,
1263 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1264 < 0) {
1265 rv = -1;
1266 goto out;
1267 }
1268
1269 /*
1270 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1271 * perform field-sensitive swapping on the string fields.
1272 * See the kernel use of ata_id_string() for proof of this.
1273 */
1274#ifdef __LITTLE_ENDIAN
1275 ata_swap_string(port->identify + 27, 40); /* model string*/
1276 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1277 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1278#else
1279 {
1280 int i;
1281 for (i = 0; i < ATA_ID_WORDS; i++)
1282 port->identify[i] = le16_to_cpu(port->identify[i]);
1283 }
1284#endif
1285
1286 /* Set the identify buffer as valid. */
1287 port->identify_valid = 1;
1288
1289 if (user_buffer) {
1290 if (copy_to_user(
1291 user_buffer,
1292 port->identify,
1293 ATA_ID_WORDS * sizeof(u16))) {
1294 rv = -EFAULT;
1295 goto out;
1296 }
1297 }
1298
1299out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001300 return rv;
1301}
1302
1303/*
1304 * Issue a standby immediate command to the device.
1305 *
1306 * @port Pointer to the port structure.
1307 *
1308 * return value
1309 * 0 Command was executed successfully.
1310 * -1 An error occurred while executing the command.
1311 */
1312static int mtip_standby_immediate(struct mtip_port *port)
1313{
1314 int rv;
1315 struct host_to_dev_fis fis;
1316
Sam Bradshaw88523a62011-08-30 08:34:26 -06001317 /* Build the FIS. */
1318 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1319 fis.type = 0x27;
1320 fis.opts = 1 << 7;
1321 fis.command = ATA_CMD_STANDBYNOW1;
1322
1323 /* Execute the command. Use a 15-second timeout for large drives. */
1324 rv = mtip_exec_internal_command(port,
1325 &fis,
1326 5,
1327 0,
1328 0,
1329 0,
1330 GFP_KERNEL,
1331 15000);
1332
Sam Bradshaw88523a62011-08-30 08:34:26 -06001333 return rv;
1334}
1335
1336/*
1337 * Get the drive capacity.
1338 *
1339 * @dd Pointer to the device data structure.
1340 * @sectors Pointer to the variable that will receive the sector count.
1341 *
1342 * return value
1343 * 1 Capacity was returned successfully.
1344 * 0 The identify information is invalid.
1345 */
Jens Axboe63166682011-09-27 21:27:43 -06001346static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001347{
1348 struct mtip_port *port = dd->port;
1349 u64 total, raw0, raw1, raw2, raw3;
1350 raw0 = port->identify[100];
1351 raw1 = port->identify[101];
1352 raw2 = port->identify[102];
1353 raw3 = port->identify[103];
1354 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1355 *sectors = total;
1356 return (bool) !!port->identify_valid;
1357}
1358
1359/*
1360 * Reset the HBA.
1361 *
1362 * Resets the HBA by setting the HBA Reset bit in the Global
1363 * HBA Control register. After setting the HBA Reset bit the
1364 * function waits for 1 second before reading the HBA Reset
1365 * bit to make sure it has cleared. If HBA Reset is not clear
1366 * an error is returned. Cannot be used in non-blockable
1367 * context.
1368 *
1369 * @dd Pointer to the driver data structure.
1370 *
1371 * return value
1372 * 0 The reset was successful.
1373 * -1 The HBA Reset bit did not clear.
1374 */
1375static int mtip_hba_reset(struct driver_data *dd)
1376{
1377 mtip_deinit_port(dd->port);
1378
1379 /* Set the reset bit */
1380 writel(HOST_RESET, dd->mmio + HOST_CTL);
1381
1382 /* Flush */
1383 readl(dd->mmio + HOST_CTL);
1384
1385 /* Wait for reset to clear */
1386 ssleep(1);
1387
1388 /* Check the bit has cleared */
1389 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1390 dev_err(&dd->pdev->dev,
1391 "Reset bit did not clear.\n");
1392 return -1;
1393 }
1394
1395 return 0;
1396}
1397
1398/*
1399 * Display the identify command data.
1400 *
1401 * @port Pointer to the port data structure.
1402 *
1403 * return value
1404 * None
1405 */
1406static void mtip_dump_identify(struct mtip_port *port)
1407{
1408 sector_t sectors;
1409 unsigned short revid;
1410 char cbuf[42];
1411
1412 if (!port->identify_valid)
1413 return;
1414
1415 strlcpy(cbuf, (char *)(port->identify+10), 21);
1416 dev_info(&port->dd->pdev->dev,
1417 "Serial No.: %s\n", cbuf);
1418
1419 strlcpy(cbuf, (char *)(port->identify+23), 9);
1420 dev_info(&port->dd->pdev->dev,
1421 "Firmware Ver.: %s\n", cbuf);
1422
1423 strlcpy(cbuf, (char *)(port->identify+27), 41);
1424 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1425
1426 if (mtip_hw_get_capacity(port->dd, &sectors))
1427 dev_info(&port->dd->pdev->dev,
1428 "Capacity: %llu sectors (%llu MB)\n",
1429 (u64)sectors,
1430 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1431
1432 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001433 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001434 case 0x1:
1435 strlcpy(cbuf, "A0", 3);
1436 break;
1437 case 0x3:
1438 strlcpy(cbuf, "A2", 3);
1439 break;
1440 default:
1441 strlcpy(cbuf, "?", 2);
1442 break;
1443 }
1444 dev_info(&port->dd->pdev->dev,
1445 "Card Type: %s\n", cbuf);
1446}
1447
1448/*
1449 * Map the commands scatter list into the command table.
1450 *
1451 * @command Pointer to the command.
1452 * @nents Number of scatter list entries.
1453 *
1454 * return value
1455 * None
1456 */
1457static inline void fill_command_sg(struct driver_data *dd,
1458 struct mtip_cmd *command,
1459 int nents)
1460{
1461 int n;
1462 unsigned int dma_len;
1463 struct mtip_cmd_sg *command_sg;
1464 struct scatterlist *sg = command->sg;
1465
1466 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1467
1468 for (n = 0; n < nents; n++) {
1469 dma_len = sg_dma_len(sg);
1470 if (dma_len > 0x400000)
1471 dev_err(&dd->pdev->dev,
1472 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001473 command_sg->info = __force_bit2int
1474 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1475 command_sg->dba = __force_bit2int
1476 cpu_to_le32(sg_dma_address(sg));
1477 command_sg->dba_upper = __force_bit2int
1478 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001479 command_sg++;
1480 sg++;
1481 }
1482}
1483
1484/*
1485 * @brief Execute a drive command.
1486 *
1487 * return value 0 The command completed successfully.
1488 * return value -1 An error occurred while executing the command.
1489 */
Jens Axboe63166682011-09-27 21:27:43 -06001490static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001491{
1492 struct host_to_dev_fis fis;
1493 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1494
Sam Bradshaw88523a62011-08-30 08:34:26 -06001495 /* Build the FIS. */
1496 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1497 fis.type = 0x27;
1498 fis.opts = 1 << 7;
1499 fis.command = command[0];
1500 fis.features = command[1];
1501 fis.sect_count = command[2];
1502 fis.sector = command[3];
1503 fis.cyl_low = command[4];
1504 fis.cyl_hi = command[5];
1505 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1506
1507
1508 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1509 "nsect %x, sect %x, lcyl %x, "
1510 "hcyl %x, sel %x\n",
1511 __func__,
1512 command[0],
1513 command[1],
1514 command[2],
1515 command[3],
1516 command[4],
1517 command[5],
1518 command[6]);
1519
1520 /* Execute the command. */
1521 if (mtip_exec_internal_command(port,
1522 &fis,
1523 5,
1524 0,
1525 0,
1526 0,
1527 GFP_KERNEL,
1528 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001529 return -1;
1530 }
1531
1532 command[0] = reply->command; /* Status*/
1533 command[1] = reply->features; /* Error*/
1534 command[4] = reply->cyl_low;
1535 command[5] = reply->cyl_hi;
1536
1537 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1538 "err %x , cyl_lo %x cyl_hi %x\n",
1539 __func__,
1540 command[0],
1541 command[1],
1542 command[4],
1543 command[5]);
1544
Sam Bradshaw88523a62011-08-30 08:34:26 -06001545 return 0;
1546}
1547
1548/*
1549 * @brief Execute a drive command.
1550 *
1551 * @param port Pointer to the port data structure.
1552 * @param command Pointer to the user specified command parameters.
1553 * @param user_buffer Pointer to the user space buffer where read sector
1554 * data should be copied.
1555 *
1556 * return value 0 The command completed successfully.
1557 * return value -EFAULT An error occurred while copying the completion
1558 * data to the user space buffer.
1559 * return value -1 An error occurred while executing the command.
1560 */
Jens Axboe63166682011-09-27 21:27:43 -06001561static int exec_drive_command(struct mtip_port *port, u8 *command,
1562 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001563{
1564 struct host_to_dev_fis fis;
1565 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1566
Sam Bradshaw88523a62011-08-30 08:34:26 -06001567 /* Build the FIS. */
1568 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1569 fis.type = 0x27;
1570 fis.opts = 1 << 7;
1571 fis.command = command[0];
1572 fis.features = command[2];
1573 fis.sect_count = command[3];
1574 if (fis.command == ATA_CMD_SMART) {
1575 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001576 fis.cyl_low = 0x4F;
1577 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001578 }
1579
1580 dbg_printk(MTIP_DRV_NAME
1581 "%s: User Command: cmd %x, sect %x, "
1582 "feat %x, sectcnt %x\n",
1583 __func__,
1584 command[0],
1585 command[1],
1586 command[2],
1587 command[3]);
1588
1589 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1590
1591 /* Execute the command. */
1592 if (mtip_exec_internal_command(port,
1593 &fis,
1594 5,
1595 port->sector_buffer_dma,
1596 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1597 0,
1598 GFP_KERNEL,
1599 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1600 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001601 return -1;
1602 }
1603
1604 /* Collect the completion status. */
1605 command[0] = reply->command; /* Status*/
1606 command[1] = reply->features; /* Error*/
1607 command[2] = command[3];
1608
1609 dbg_printk(MTIP_DRV_NAME
1610 "%s: Completion Status: stat %x, "
1611 "err %x, cmd %x\n",
1612 __func__,
1613 command[0],
1614 command[1],
1615 command[2]);
1616
1617 if (user_buffer && command[3]) {
1618 if (copy_to_user(user_buffer,
1619 port->sector_buffer,
1620 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001621 return -EFAULT;
1622 }
1623 }
1624
Sam Bradshaw88523a62011-08-30 08:34:26 -06001625 return 0;
1626}
1627
1628/*
1629 * Indicates whether a command has a single sector payload.
1630 *
1631 * @command passed to the device to perform the certain event.
1632 * @features passed to the device to perform the certain event.
1633 *
1634 * return value
1635 * 1 command is one that always has a single sector payload,
1636 * regardless of the value in the Sector Count field.
1637 * 0 otherwise
1638 *
1639 */
1640static unsigned int implicit_sector(unsigned char command,
1641 unsigned char features)
1642{
1643 unsigned int rv = 0;
1644
1645 /* list of commands that have an implicit sector count of 1 */
1646 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001647 case ATA_CMD_SEC_SET_PASS:
1648 case ATA_CMD_SEC_UNLOCK:
1649 case ATA_CMD_SEC_ERASE_PREP:
1650 case ATA_CMD_SEC_ERASE_UNIT:
1651 case ATA_CMD_SEC_FREEZE_LOCK:
1652 case ATA_CMD_SEC_DISABLE_PASS:
1653 case ATA_CMD_PMP_READ:
1654 case ATA_CMD_PMP_WRITE:
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_SET_MAX:
1658 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001659 rv = 1;
1660 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001661 case ATA_CMD_SMART:
1662 if ((features == ATA_SMART_READ_VALUES) ||
1663 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001664 rv = 1;
1665 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001666 case ATA_CMD_CONF_OVERLAY:
1667 if ((features == ATA_DCO_IDENTIFY) ||
1668 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001669 rv = 1;
1670 break;
1671 }
1672 return rv;
1673}
1674
1675/*
1676 * Executes a taskfile
1677 * See ide_taskfile_ioctl() for derivation
1678 */
1679static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001680 void __user *buf,
1681 ide_task_request_t *req_task,
1682 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001683{
1684 struct host_to_dev_fis fis;
1685 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001686 u8 *outbuf = NULL;
1687 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001688 dma_addr_t outbuf_dma = 0;
1689 dma_addr_t inbuf_dma = 0;
1690 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001691 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001692 unsigned int taskin = 0;
1693 unsigned int taskout = 0;
1694 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001695 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1696 unsigned int force_single_sector;
1697 unsigned int transfer_size;
1698 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001699 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001700
1701 taskout = req_task->out_size;
1702 taskin = req_task->in_size;
1703 /* 130560 = 512 * 0xFF*/
1704 if (taskin > 130560 || taskout > 130560) {
1705 err = -EINVAL;
1706 goto abort;
1707 }
1708
1709 if (taskout) {
1710 outbuf = kzalloc(taskout, GFP_KERNEL);
1711 if (outbuf == NULL) {
1712 err = -ENOMEM;
1713 goto abort;
1714 }
1715 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1716 err = -EFAULT;
1717 goto abort;
1718 }
1719 outbuf_dma = pci_map_single(dd->pdev,
1720 outbuf,
1721 taskout,
1722 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001723 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001724 err = -ENOMEM;
1725 goto abort;
1726 }
1727 dma_buffer = outbuf_dma;
1728 }
1729
1730 if (taskin) {
1731 inbuf = kzalloc(taskin, GFP_KERNEL);
1732 if (inbuf == NULL) {
1733 err = -ENOMEM;
1734 goto abort;
1735 }
1736
1737 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1738 err = -EFAULT;
1739 goto abort;
1740 }
1741 inbuf_dma = pci_map_single(dd->pdev,
1742 inbuf,
1743 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001744 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001745 err = -ENOMEM;
1746 goto abort;
1747 }
1748 dma_buffer = inbuf_dma;
1749 }
1750
1751 /* only supports PIO and non-data commands from this ioctl. */
1752 switch (req_task->data_phase) {
1753 case TASKFILE_OUT:
1754 nsect = taskout / ATA_SECT_SIZE;
1755 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1756 break;
1757 case TASKFILE_IN:
1758 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1759 break;
1760 case TASKFILE_NO_DATA:
1761 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1762 break;
1763 default:
1764 err = -EINVAL;
1765 goto abort;
1766 }
1767
Sam Bradshaw88523a62011-08-30 08:34:26 -06001768 /* Build the FIS. */
1769 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1770
1771 fis.type = 0x27;
1772 fis.opts = 1 << 7;
1773 fis.command = req_task->io_ports[7];
1774 fis.features = req_task->io_ports[1];
1775 fis.sect_count = req_task->io_ports[2];
1776 fis.lba_low = req_task->io_ports[3];
1777 fis.lba_mid = req_task->io_ports[4];
1778 fis.lba_hi = req_task->io_ports[5];
1779 /* Clear the dev bit*/
1780 fis.device = req_task->io_ports[6] & ~0x10;
1781
1782 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1783 req_task->in_flags.all =
1784 IDE_TASKFILE_STD_IN_FLAGS |
1785 (IDE_HOB_STD_IN_FLAGS << 8);
1786 fis.lba_low_ex = req_task->hob_ports[3];
1787 fis.lba_mid_ex = req_task->hob_ports[4];
1788 fis.lba_hi_ex = req_task->hob_ports[5];
1789 fis.features_ex = req_task->hob_ports[1];
1790 fis.sect_cnt_ex = req_task->hob_ports[2];
1791
1792 } else {
1793 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1794 }
1795
1796 force_single_sector = implicit_sector(fis.command, fis.features);
1797
1798 if ((taskin || taskout) && (!fis.sect_count)) {
1799 if (nsect)
1800 fis.sect_count = nsect;
1801 else {
1802 if (!force_single_sector) {
1803 dev_warn(&dd->pdev->dev,
1804 "data movement but "
1805 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001806 err = -EINVAL;
1807 goto abort;
1808 }
1809 }
1810 }
1811
1812 dbg_printk(MTIP_DRV_NAME
1813 "taskfile: cmd %x, feat %x, nsect %x,"
1814 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1815 " head/dev %x\n",
1816 fis.command,
1817 fis.features,
1818 fis.sect_count,
1819 fis.lba_low,
1820 fis.lba_mid,
1821 fis.lba_hi,
1822 fis.device);
1823
1824 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001825 case ATA_CMD_DOWNLOAD_MICRO:
1826 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001827 timeout = 60000;
1828 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001829 case ATA_CMD_SEC_ERASE_UNIT:
1830 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001831 timeout = 240000;
1832 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001833 case ATA_CMD_STANDBYNOW1:
1834 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06001835 timeout = 10000;
1836 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001837 case 0xF7:
1838 case 0xFA:
1839 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001840 timeout = 10000;
1841 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001842 case ATA_CMD_SMART:
1843 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001844 timeout = 10000;
1845 break;
1846 default:
1847 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1848 break;
1849 }
1850
1851 /* Determine the correct transfer size.*/
1852 if (force_single_sector)
1853 transfer_size = ATA_SECT_SIZE;
1854 else
1855 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1856
1857 /* Execute the command.*/
1858 if (mtip_exec_internal_command(dd->port,
1859 &fis,
1860 5,
1861 dma_buffer,
1862 transfer_size,
1863 0,
1864 GFP_KERNEL,
1865 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001866 err = -EIO;
1867 goto abort;
1868 }
1869
1870 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1871
1872 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1873 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1874 req_task->io_ports[7] = reply->control;
1875 } else {
1876 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1877 req_task->io_ports[7] = reply->command;
1878 }
1879
1880 /* reclaim the DMA buffers.*/
1881 if (inbuf_dma)
1882 pci_unmap_single(dd->pdev, inbuf_dma,
1883 taskin, DMA_FROM_DEVICE);
1884 if (outbuf_dma)
1885 pci_unmap_single(dd->pdev, outbuf_dma,
1886 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001887 inbuf_dma = 0;
1888 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001889
1890 /* return the ATA registers to the caller.*/
1891 req_task->io_ports[1] = reply->features;
1892 req_task->io_ports[2] = reply->sect_count;
1893 req_task->io_ports[3] = reply->lba_low;
1894 req_task->io_ports[4] = reply->lba_mid;
1895 req_task->io_ports[5] = reply->lba_hi;
1896 req_task->io_ports[6] = reply->device;
1897
1898 if (req_task->out_flags.all & 1) {
1899
1900 req_task->hob_ports[3] = reply->lba_low_ex;
1901 req_task->hob_ports[4] = reply->lba_mid_ex;
1902 req_task->hob_ports[5] = reply->lba_hi_ex;
1903 req_task->hob_ports[1] = reply->features_ex;
1904 req_task->hob_ports[2] = reply->sect_cnt_ex;
1905 }
1906
1907 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001908 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06001909 ((fis.command == 0xFC) &&
1910 (fis.features == 0x27 || fis.features == 0x72 ||
1911 fis.features == 0x62 || fis.features == 0x26))) &&
1912 !(reply->command & 1)) {
1913 mtip_restart_port(dd->port);
1914 }
1915
1916 dbg_printk(MTIP_DRV_NAME
1917 "%s: Completion: stat %x,"
1918 "err %x, sect_cnt %x, lbalo %x,"
1919 "lbamid %x, lbahi %x, dev %x\n",
1920 __func__,
1921 req_task->io_ports[7],
1922 req_task->io_ports[1],
1923 req_task->io_ports[2],
1924 req_task->io_ports[3],
1925 req_task->io_ports[4],
1926 req_task->io_ports[5],
1927 req_task->io_ports[6]);
1928
Sam Bradshaw88523a62011-08-30 08:34:26 -06001929 if (taskout) {
1930 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1931 err = -EFAULT;
1932 goto abort;
1933 }
1934 }
1935 if (taskin) {
1936 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1937 err = -EFAULT;
1938 goto abort;
1939 }
1940 }
1941abort:
1942 if (inbuf_dma)
1943 pci_unmap_single(dd->pdev, inbuf_dma,
1944 taskin, DMA_FROM_DEVICE);
1945 if (outbuf_dma)
1946 pci_unmap_single(dd->pdev, outbuf_dma,
1947 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001948 kfree(outbuf);
1949 kfree(inbuf);
1950
1951 return err;
1952}
1953
1954/*
1955 * Handle IOCTL calls from the Block Layer.
1956 *
1957 * This function is called by the Block Layer when it receives an IOCTL
1958 * command that it does not understand. If the IOCTL command is not supported
1959 * this function returns -ENOTTY.
1960 *
1961 * @dd Pointer to the driver data structure.
1962 * @cmd IOCTL command passed from the Block Layer.
1963 * @arg IOCTL argument passed from the Block Layer.
1964 *
1965 * return value
1966 * 0 The IOCTL completed successfully.
1967 * -ENOTTY The specified command is not supported.
1968 * -EFAULT An error occurred copying data to a user space buffer.
1969 * -EIO An error occurred while executing the command.
1970 */
Jens Axboeef0f1582011-09-27 21:19:53 -06001971static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1972 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001973{
1974 switch (cmd) {
1975 case HDIO_GET_IDENTITY:
1976 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1977 dev_warn(&dd->pdev->dev,
1978 "Unable to read identity\n");
1979 return -EIO;
1980 }
1981
1982 break;
1983 case HDIO_DRIVE_CMD:
1984 {
1985 u8 drive_command[4];
1986
1987 /* Copy the user command info to our buffer. */
1988 if (copy_from_user(drive_command,
1989 (void __user *) arg,
1990 sizeof(drive_command)))
1991 return -EFAULT;
1992
1993 /* Execute the drive command. */
1994 if (exec_drive_command(dd->port,
1995 drive_command,
1996 (void __user *) (arg+4)))
1997 return -EIO;
1998
1999 /* Copy the status back to the users buffer. */
2000 if (copy_to_user((void __user *) arg,
2001 drive_command,
2002 sizeof(drive_command)))
2003 return -EFAULT;
2004
2005 break;
2006 }
2007 case HDIO_DRIVE_TASK:
2008 {
2009 u8 drive_command[7];
2010
2011 /* Copy the user command info to our buffer. */
2012 if (copy_from_user(drive_command,
2013 (void __user *) arg,
2014 sizeof(drive_command)))
2015 return -EFAULT;
2016
2017 /* Execute the drive command. */
2018 if (exec_drive_task(dd->port, drive_command))
2019 return -EIO;
2020
2021 /* Copy the status back to the users buffer. */
2022 if (copy_to_user((void __user *) arg,
2023 drive_command,
2024 sizeof(drive_command)))
2025 return -EFAULT;
2026
2027 break;
2028 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002029 case HDIO_DRIVE_TASKFILE: {
2030 ide_task_request_t req_task;
2031 int ret, outtotal;
2032
2033 if (copy_from_user(&req_task, (void __user *) arg,
2034 sizeof(req_task)))
2035 return -EFAULT;
2036
2037 outtotal = sizeof(req_task);
2038
2039 ret = exec_drive_taskfile(dd, (void __user *) arg,
2040 &req_task, outtotal);
2041
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002042 if (copy_to_user((void __user *) arg, &req_task,
2043 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002044 return -EFAULT;
2045
2046 return ret;
2047 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002048
2049 default:
2050 return -EINVAL;
2051 }
2052 return 0;
2053}
2054
2055/*
2056 * Submit an IO to the hw
2057 *
2058 * This function is called by the block layer to issue an io
2059 * to the device. Upon completion, the callback function will
2060 * be called with the data parameter passed as the callback data.
2061 *
2062 * @dd Pointer to the driver data structure.
2063 * @start First sector to read.
2064 * @nsect Number of sectors to read.
2065 * @nents Number of entries in scatter list for the read command.
2066 * @tag The tag of this read command.
2067 * @callback Pointer to the function that should be called
2068 * when the read completes.
2069 * @data Callback data passed to the callback function
2070 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002071 * @dir Direction (read or write)
2072 *
2073 * return value
2074 * None
2075 */
Jens Axboe63166682011-09-27 21:27:43 -06002076static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2077 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002078 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002079{
2080 struct host_to_dev_fis *fis;
2081 struct mtip_port *port = dd->port;
2082 struct mtip_cmd *command = &port->commands[tag];
2083
2084 /* Map the scatter list for DMA access */
2085 if (dir == READ)
2086 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2087 nents, DMA_FROM_DEVICE);
2088 else
2089 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2090 nents, DMA_TO_DEVICE);
2091
2092 command->scatter_ents = nents;
2093
2094 /*
2095 * The number of retries for this command before it is
2096 * reported as a failure to the upper layers.
2097 */
2098 command->retries = MTIP_MAX_RETRIES;
2099
2100 /* Fill out fis */
2101 fis = command->command;
2102 fis->type = 0x27;
2103 fis->opts = 1 << 7;
2104 fis->command =
2105 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002106 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2107 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002108 fis->device = 1 << 6;
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;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002412 mtip_block_initialize(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002413 break;
2414 }
2415 ssleep(10);
2416 } while (time_before(jiffies, timeout));
2417
2418 /* Check for timeout */
2419 if (dd->ftlrebuildflag) {
2420 dev_err(&dd->pdev->dev,
2421 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2422 jiffies_to_msecs(jiffies - start) / 1000);
2423 return -EFAULT;
2424 }
2425
2426 return 0;
2427}
2428
2429/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002430 * service thread to issue queued commands
2431 *
2432 * @data Pointer to the driver data structure.
2433 *
2434 * return value
2435 * 0
2436 */
2437
2438static int mtip_service_thread(void *data)
2439{
2440 struct driver_data *dd = (struct driver_data *)data;
2441 unsigned long slot, slot_start, slot_wrap;
2442 unsigned int num_cmd_slots = dd->slot_groups * 32;
2443 struct mtip_port *port = dd->port;
2444
2445 while (1) {
2446 /*
2447 * the condition is to check neither an internal command is
2448 * is in progress nor error handling is active
2449 */
2450 wait_event_interruptible(port->svc_wait, (port->flags) &&
2451 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2452 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2453
2454 if (kthread_should_stop())
2455 break;
2456
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002457 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002458 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002459 slot = 1;
2460 /* used to restrict the loop to one iteration */
2461 slot_start = num_cmd_slots;
2462 slot_wrap = 0;
2463 while (1) {
2464 slot = find_next_bit(port->cmds_to_issue,
2465 num_cmd_slots, slot);
2466 if (slot_wrap == 1) {
2467 if ((slot_start >= slot) ||
2468 (slot >= num_cmd_slots))
2469 break;
2470 }
2471 if (unlikely(slot_start == num_cmd_slots))
2472 slot_start = slot;
2473
2474 if (unlikely(slot == num_cmd_slots)) {
2475 slot = 1;
2476 slot_wrap = 1;
2477 continue;
2478 }
2479
2480 /* Issue the command to the hardware */
2481 mtip_issue_ncq_command(port, slot);
2482
2483 /* Set the command's timeout value.*/
2484 port->commands[slot].comp_time = jiffies +
2485 msecs_to_jiffies(MTIP_NCQ_COMMAND_TIMEOUT_MS);
2486
2487 clear_bit(slot, port->cmds_to_issue);
2488 }
2489
2490 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002491 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
2492 mtip_ftl_rebuild_poll(dd);
2493 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002494 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002495 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2496
2497 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2498 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002499 }
2500 return 0;
2501}
2502
2503/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002504 * Called once for each card.
2505 *
2506 * @dd Pointer to the driver data structure.
2507 *
2508 * return value
2509 * 0 on success, else an error code.
2510 */
Jens Axboe63166682011-09-27 21:27:43 -06002511static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002512{
2513 int i;
2514 int rv;
2515 unsigned int num_command_slots;
2516
2517 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2518
2519 mtip_detect_product(dd);
2520 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2521 rv = -EIO;
2522 goto out1;
2523 }
2524 num_command_slots = dd->slot_groups * 32;
2525
2526 hba_setup(dd);
2527
Sam Bradshaw88523a62011-08-30 08:34:26 -06002528 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2529
2530 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2531 if (!dd->port) {
2532 dev_err(&dd->pdev->dev,
2533 "Memory allocation: port structure\n");
2534 return -ENOMEM;
2535 }
2536
2537 /* Counting semaphore to track command slot usage */
2538 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2539
2540 /* Spinlock to prevent concurrent issue */
2541 spin_lock_init(&dd->port->cmd_issue_lock);
2542
2543 /* Set the port mmio base address. */
2544 dd->port->mmio = dd->mmio + PORT_OFFSET;
2545 dd->port->dd = dd;
2546
2547 /* Allocate memory for the command list. */
2548 dd->port->command_list =
2549 dmam_alloc_coherent(&dd->pdev->dev,
2550 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2551 &dd->port->command_list_dma,
2552 GFP_KERNEL);
2553 if (!dd->port->command_list) {
2554 dev_err(&dd->pdev->dev,
2555 "Memory allocation: command list\n");
2556 rv = -ENOMEM;
2557 goto out1;
2558 }
2559
2560 /* Clear the memory we have allocated. */
2561 memset(dd->port->command_list,
2562 0,
2563 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2564
2565 /* Setup the addresse of the RX FIS. */
2566 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2567 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2568
2569 /* Setup the address of the command tables. */
2570 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2571 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2572
2573 /* Setup the address of the identify data. */
2574 dd->port->identify = dd->port->command_table +
2575 HW_CMD_TBL_AR_SZ;
2576 dd->port->identify_dma = dd->port->command_tbl_dma +
2577 HW_CMD_TBL_AR_SZ;
2578
2579 /* Setup the address of the sector buffer. */
2580 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2581 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2582
2583 /* Point the command headers at the command tables. */
2584 for (i = 0; i < num_command_slots; i++) {
2585 dd->port->commands[i].command_header =
2586 dd->port->command_list +
2587 (sizeof(struct mtip_cmd_hdr) * i);
2588 dd->port->commands[i].command_header_dma =
2589 dd->port->command_list_dma +
2590 (sizeof(struct mtip_cmd_hdr) * i);
2591
2592 dd->port->commands[i].command =
2593 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2594 dd->port->commands[i].command_dma =
2595 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2596
2597 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2598 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002599 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002600 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002601 dd->port->commands[i].command_header->ctba =
2602 __force_bit2int cpu_to_le32(
2603 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002604
2605 /*
2606 * If this is not done, a bug is reported by the stock
2607 * FC11 i386. Due to the fact that it has lots of kernel
2608 * debugging enabled.
2609 */
2610 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2611
2612 /* Mark all commands as currently inactive.*/
2613 atomic_set(&dd->port->commands[i].active, 0);
2614 }
2615
2616 /* Setup the pointers to the extended s_active and CI registers. */
2617 for (i = 0; i < dd->slot_groups; i++) {
2618 dd->port->s_active[i] =
2619 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2620 dd->port->cmd_issue[i] =
2621 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2622 dd->port->completed[i] =
2623 dd->port->mmio + i*0x80 + PORT_SDBV;
2624 }
2625
2626 /* Reset the HBA. */
2627 if (mtip_hba_reset(dd) < 0) {
2628 dev_err(&dd->pdev->dev,
2629 "Card did not reset within timeout\n");
2630 rv = -EIO;
2631 goto out2;
2632 }
2633
2634 mtip_init_port(dd->port);
2635 mtip_start_port(dd->port);
2636
2637 /* Setup the ISR and enable interrupts. */
2638 rv = devm_request_irq(&dd->pdev->dev,
2639 dd->pdev->irq,
2640 mtip_irq_handler,
2641 IRQF_SHARED,
2642 dev_driver_string(&dd->pdev->dev),
2643 dd);
2644
2645 if (rv) {
2646 dev_err(&dd->pdev->dev,
2647 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2648 goto out2;
2649 }
2650
2651 /* Enable interrupts on the HBA. */
2652 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2653 dd->mmio + HOST_CTL);
2654
2655 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002656 init_waitqueue_head(&dd->port->svc_wait);
2657
Sam Bradshaw88523a62011-08-30 08:34:26 -06002658 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2659 dd->port->cmd_timer.function = mtip_timeout_function;
2660 mod_timer(&dd->port->cmd_timer,
2661 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2662
2663 if (mtip_get_identify(dd->port, NULL) < 0) {
2664 rv = -EFAULT;
2665 goto out3;
2666 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002667
2668 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2669 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002670 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2671 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002672 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002673 mtip_dump_identify(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002674 return rv;
2675
2676out3:
2677 del_timer_sync(&dd->port->cmd_timer);
2678
2679 /* Disable interrupts on the HBA. */
2680 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2681 dd->mmio + HOST_CTL);
2682
2683 /*Release the IRQ. */
2684 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2685
2686out2:
2687 mtip_deinit_port(dd->port);
2688
2689 /* Free the command/command header memory. */
2690 dmam_free_coherent(&dd->pdev->dev,
2691 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2692 dd->port->command_list,
2693 dd->port->command_list_dma);
2694out1:
2695 /* Free the memory allocated for the for structure. */
2696 kfree(dd->port);
2697
2698 return rv;
2699}
2700
2701/*
2702 * Called to deinitialize an interface.
2703 *
2704 * @dd Pointer to the driver data structure.
2705 *
2706 * return value
2707 * 0
2708 */
Jens Axboe63166682011-09-27 21:27:43 -06002709static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002710{
2711 /*
2712 * Send standby immediate (E0h) to the drive so that it
2713 * saves its state.
2714 */
2715 if (atomic_read(&dd->drv_cleanup_done) != true) {
2716
2717 mtip_standby_immediate(dd->port);
2718
2719 /* de-initialize the port. */
2720 mtip_deinit_port(dd->port);
2721
2722 /* Disable interrupts on the HBA. */
2723 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2724 dd->mmio + HOST_CTL);
2725 }
2726
2727 del_timer_sync(&dd->port->cmd_timer);
2728
Sam Bradshaw88523a62011-08-30 08:34:26 -06002729 /* Release the IRQ. */
2730 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2731
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002732 /* Stop the bottom half tasklet. */
2733 tasklet_kill(&dd->tasklet);
2734
Sam Bradshaw88523a62011-08-30 08:34:26 -06002735 /* Free the command/command header memory. */
2736 dmam_free_coherent(&dd->pdev->dev,
2737 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2738 dd->port->command_list,
2739 dd->port->command_list_dma);
2740 /* Free the memory allocated for the for structure. */
2741 kfree(dd->port);
2742
2743 return 0;
2744}
2745
2746/*
2747 * Issue a Standby Immediate command to the device.
2748 *
2749 * This function is called by the Block Layer just before the
2750 * system powers off during a shutdown.
2751 *
2752 * @dd Pointer to the driver data structure.
2753 *
2754 * return value
2755 * 0
2756 */
Jens Axboe63166682011-09-27 21:27:43 -06002757static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002758{
2759 /*
2760 * Send standby immediate (E0h) to the drive so that it
2761 * saves its state.
2762 */
2763 mtip_standby_immediate(dd->port);
2764
2765 return 0;
2766}
2767
2768/*
2769 * Suspend function
2770 *
2771 * This function is called by the Block Layer just before the
2772 * system hibernates.
2773 *
2774 * @dd Pointer to the driver data structure.
2775 *
2776 * return value
2777 * 0 Suspend was successful
2778 * -EFAULT Suspend was not successful
2779 */
Jens Axboe63166682011-09-27 21:27:43 -06002780static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002781{
2782 /*
2783 * Send standby immediate (E0h) to the drive
2784 * so that it saves its state.
2785 */
2786 if (mtip_standby_immediate(dd->port) != 0) {
2787 dev_err(&dd->pdev->dev,
2788 "Failed standby-immediate command\n");
2789 return -EFAULT;
2790 }
2791
2792 /* Disable interrupts on the HBA.*/
2793 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2794 dd->mmio + HOST_CTL);
2795 mtip_deinit_port(dd->port);
2796
2797 return 0;
2798}
2799
2800/*
2801 * Resume function
2802 *
2803 * This function is called by the Block Layer as the
2804 * system resumes.
2805 *
2806 * @dd Pointer to the driver data structure.
2807 *
2808 * return value
2809 * 0 Resume was successful
2810 * -EFAULT Resume was not successful
2811 */
Jens Axboe63166682011-09-27 21:27:43 -06002812static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002813{
2814 /* Perform any needed hardware setup steps */
2815 hba_setup(dd);
2816
2817 /* Reset the HBA */
2818 if (mtip_hba_reset(dd) != 0) {
2819 dev_err(&dd->pdev->dev,
2820 "Unable to reset the HBA\n");
2821 return -EFAULT;
2822 }
2823
2824 /*
2825 * Enable the port, DMA engine, and FIS reception specific
2826 * h/w in controller.
2827 */
2828 mtip_init_port(dd->port);
2829 mtip_start_port(dd->port);
2830
2831 /* Enable interrupts on the HBA.*/
2832 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2833 dd->mmio + HOST_CTL);
2834
2835 return 0;
2836}
2837
2838/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002839 * Helper function for reusing disk name
2840 * upon hot insertion.
2841 */
2842static int rssd_disk_name_format(char *prefix,
2843 int index,
2844 char *buf,
2845 int buflen)
2846{
2847 const int base = 'z' - 'a' + 1;
2848 char *begin = buf + strlen(prefix);
2849 char *end = buf + buflen;
2850 char *p;
2851 int unit;
2852
2853 p = end - 1;
2854 *p = '\0';
2855 unit = base;
2856 do {
2857 if (p == begin)
2858 return -EINVAL;
2859 *--p = 'a' + (index % unit);
2860 index = (index / unit) - 1;
2861 } while (index >= 0);
2862
2863 memmove(begin, p, end - p);
2864 memcpy(buf, prefix, strlen(prefix));
2865
2866 return 0;
2867}
2868
2869/*
2870 * Block layer IOCTL handler.
2871 *
2872 * @dev Pointer to the block_device structure.
2873 * @mode ignored
2874 * @cmd IOCTL command passed from the user application.
2875 * @arg Argument passed from the user application.
2876 *
2877 * return value
2878 * 0 IOCTL completed successfully.
2879 * -ENOTTY IOCTL not supported or invalid driver data
2880 * structure pointer.
2881 */
2882static int mtip_block_ioctl(struct block_device *dev,
2883 fmode_t mode,
2884 unsigned cmd,
2885 unsigned long arg)
2886{
2887 struct driver_data *dd = dev->bd_disk->private_data;
2888
2889 if (!capable(CAP_SYS_ADMIN))
2890 return -EACCES;
2891
2892 if (!dd)
2893 return -ENOTTY;
2894
2895 switch (cmd) {
2896 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002897 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002898 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06002899 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002900 }
2901}
2902
Jens Axboe16d02c02011-09-27 15:50:01 -06002903#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06002904/*
2905 * Block layer compat IOCTL handler.
2906 *
2907 * @dev Pointer to the block_device structure.
2908 * @mode ignored
2909 * @cmd IOCTL command passed from the user application.
2910 * @arg Argument passed from the user application.
2911 *
2912 * return value
2913 * 0 IOCTL completed successfully.
2914 * -ENOTTY IOCTL not supported or invalid driver data
2915 * structure pointer.
2916 */
2917static int mtip_block_compat_ioctl(struct block_device *dev,
2918 fmode_t mode,
2919 unsigned cmd,
2920 unsigned long arg)
2921{
2922 struct driver_data *dd = dev->bd_disk->private_data;
2923
2924 if (!capable(CAP_SYS_ADMIN))
2925 return -EACCES;
2926
2927 if (!dd)
2928 return -ENOTTY;
2929
2930 switch (cmd) {
2931 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002932 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06002933 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002934 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06002935 ide_task_request_t req_task;
2936 int compat_tasksize, outtotal, ret;
2937
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002938 compat_tasksize =
2939 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06002940
2941 compat_req_task =
2942 (struct mtip_compat_ide_task_request_s __user *) arg;
2943
2944 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002945 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06002946 return -EFAULT;
2947
2948 if (get_user(req_task.out_size, &compat_req_task->out_size))
2949 return -EFAULT;
2950
2951 if (get_user(req_task.in_size, &compat_req_task->in_size))
2952 return -EFAULT;
2953
2954 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2955
2956 ret = exec_drive_taskfile(dd, (void __user *) arg,
2957 &req_task, outtotal);
2958
2959 if (copy_to_user((void __user *) arg, &req_task,
2960 compat_tasksize -
2961 (2 * sizeof(compat_long_t))))
2962 return -EFAULT;
2963
2964 if (put_user(req_task.out_size, &compat_req_task->out_size))
2965 return -EFAULT;
2966
2967 if (put_user(req_task.in_size, &compat_req_task->in_size))
2968 return -EFAULT;
2969
2970 return ret;
2971 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002972 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06002973 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002974 }
2975}
Jens Axboe16d02c02011-09-27 15:50:01 -06002976#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06002977
2978/*
2979 * Obtain the geometry of the device.
2980 *
2981 * You may think that this function is obsolete, but some applications,
2982 * fdisk for example still used CHS values. This function describes the
2983 * device as having 224 heads and 56 sectors per cylinder. These values are
2984 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2985 * partition is described in terms of a start and end cylinder this means
2986 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2987 * affects performance.
2988 *
2989 * @dev Pointer to the block_device strucutre.
2990 * @geo Pointer to a hd_geometry structure.
2991 *
2992 * return value
2993 * 0 Operation completed successfully.
2994 * -ENOTTY An error occurred while reading the drive capacity.
2995 */
2996static int mtip_block_getgeo(struct block_device *dev,
2997 struct hd_geometry *geo)
2998{
2999 struct driver_data *dd = dev->bd_disk->private_data;
3000 sector_t capacity;
3001
3002 if (!dd)
3003 return -ENOTTY;
3004
3005 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3006 dev_warn(&dd->pdev->dev,
3007 "Could not get drive capacity.\n");
3008 return -ENOTTY;
3009 }
3010
3011 geo->heads = 224;
3012 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003013 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003014 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003015 return 0;
3016}
3017
3018/*
3019 * Block device operation function.
3020 *
3021 * This structure contains pointers to the functions required by the block
3022 * layer.
3023 */
3024static const struct block_device_operations mtip_block_ops = {
3025 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003026#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003027 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003028#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003029 .getgeo = mtip_block_getgeo,
3030 .owner = THIS_MODULE
3031};
3032
3033/*
3034 * Block layer make request function.
3035 *
3036 * This function is called by the kernel to process a BIO for
3037 * the P320 device.
3038 *
3039 * @queue Pointer to the request queue. Unused other than to obtain
3040 * the driver data structure.
3041 * @bio Pointer to the BIO.
3042 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003043 */
Jens Axboea71f4832011-11-05 08:36:21 +01003044static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003045{
3046 struct driver_data *dd = queue->queuedata;
3047 struct scatterlist *sg;
3048 struct bio_vec *bvec;
3049 int nents = 0;
3050 int tag = 0;
3051
3052 if (unlikely(!bio_has_data(bio))) {
3053 blk_queue_flush(queue, 0);
3054 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003055 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003056 }
3057
Sam Bradshaw88523a62011-08-30 08:34:26 -06003058 sg = mtip_hw_get_scatterlist(dd, &tag);
3059 if (likely(sg != NULL)) {
3060 blk_queue_bounce(queue, &bio);
3061
3062 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3063 dev_warn(&dd->pdev->dev,
3064 "Maximum number of SGL entries exceeded");
3065 bio_io_error(bio);
3066 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003067 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003068 }
3069
3070 /* Create the scatter list for this bio. */
3071 bio_for_each_segment(bvec, bio, nents) {
3072 sg_set_page(&sg[nents],
3073 bvec->bv_page,
3074 bvec->bv_len,
3075 bvec->bv_offset);
3076 }
3077
3078 /* Issue the read/write. */
3079 mtip_hw_submit_io(dd,
3080 bio->bi_sector,
3081 bio_sectors(bio),
3082 nents,
3083 tag,
3084 bio_endio,
3085 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003086 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003087 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003088 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003089}
3090
3091/*
3092 * Block layer initialization function.
3093 *
3094 * This function is called once by the PCI layer for each P320
3095 * device that is connected to the system.
3096 *
3097 * @dd Pointer to the driver data structure.
3098 *
3099 * return value
3100 * 0 on success else an error code.
3101 */
Jens Axboe63166682011-09-27 21:27:43 -06003102static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003103{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003104 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003105 sector_t capacity;
3106 unsigned int index = 0;
3107 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003108 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003109
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003110 if (dd->disk)
3111 goto skip_create_disk; /* hw init done, before rebuild */
3112
Sam Bradshaw88523a62011-08-30 08:34:26 -06003113 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003114 wait_for_rebuild = mtip_hw_init(dd);
3115 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003116 dev_err(&dd->pdev->dev,
3117 "Protocol layer initialization failed\n");
3118 rv = -EINVAL;
3119 goto protocol_init_error;
3120 }
3121
Sam Bradshaw88523a62011-08-30 08:34:26 -06003122 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3123 if (dd->disk == NULL) {
3124 dev_err(&dd->pdev->dev,
3125 "Unable to allocate gendisk structure\n");
3126 rv = -EINVAL;
3127 goto alloc_disk_error;
3128 }
3129
3130 /* Generate the disk name, implemented same as in sd.c */
3131 do {
3132 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3133 goto ida_get_error;
3134
3135 spin_lock(&rssd_index_lock);
3136 rv = ida_get_new(&rssd_index_ida, &index);
3137 spin_unlock(&rssd_index_lock);
3138 } while (rv == -EAGAIN);
3139
3140 if (rv)
3141 goto ida_get_error;
3142
3143 rv = rssd_disk_name_format("rssd",
3144 index,
3145 dd->disk->disk_name,
3146 DISK_NAME_LEN);
3147 if (rv)
3148 goto disk_index_error;
3149
3150 dd->disk->driverfs_dev = &dd->pdev->dev;
3151 dd->disk->major = dd->major;
3152 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3153 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003154 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003155 dd->index = index;
3156
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003157 /*
3158 * if rebuild pending, start the service thread, and delay the block
3159 * queue creation and add_disk()
3160 */
3161 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3162 goto start_service_thread;
3163
3164skip_create_disk:
3165 /* Allocate the request queue. */
3166 dd->queue = blk_alloc_queue(GFP_KERNEL);
3167 if (dd->queue == NULL) {
3168 dev_err(&dd->pdev->dev,
3169 "Unable to allocate request queue\n");
3170 rv = -ENOMEM;
3171 goto block_queue_alloc_init_error;
3172 }
3173
3174 /* Attach our request function to the request queue. */
3175 blk_queue_make_request(dd->queue, mtip_make_request);
3176
3177 dd->disk->queue = dd->queue;
3178 dd->queue->queuedata = dd;
3179
3180 /* Set device limits. */
3181 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3182 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3183 blk_queue_physical_block_size(dd->queue, 4096);
3184 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003185 /*
3186 * write back cache is not supported in the device. FUA depends on
3187 * write back cache support, hence setting flush support to zero.
3188 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003189 blk_queue_flush(dd->queue, 0);
3190
Sam Bradshaw88523a62011-08-30 08:34:26 -06003191 /* Set the capacity of the device in 512 byte sectors. */
3192 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3193 dev_warn(&dd->pdev->dev,
3194 "Could not read drive capacity\n");
3195 rv = -EIO;
3196 goto read_capacity_error;
3197 }
3198 set_capacity(dd->disk, capacity);
3199
3200 /* Enable the block device and add it to /dev */
3201 add_disk(dd->disk);
3202
3203 /*
3204 * Now that the disk is active, initialize any sysfs attributes
3205 * managed by the protocol layer.
3206 */
3207 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3208 if (kobj) {
3209 mtip_hw_sysfs_init(dd, kobj);
3210 kobject_put(kobj);
3211 }
3212
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003213 if (dd->mtip_svc_handler)
3214 return rv; /* service thread created for handling rebuild */
3215
3216start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003217 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3218
3219 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3220 dd, thd_name);
3221
3222 if (IS_ERR(dd->mtip_svc_handler)) {
3223 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3224 dd->mtip_svc_handler = NULL;
3225 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003226 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003227 }
3228
Sam Bradshaw88523a62011-08-30 08:34:26 -06003229 return rv;
3230
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003231kthread_run_error:
3232 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003233 del_gendisk(dd->disk);
3234
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003235read_capacity_error:
3236 blk_cleanup_queue(dd->queue);
3237
3238block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003239disk_index_error:
3240 spin_lock(&rssd_index_lock);
3241 ida_remove(&rssd_index_ida, index);
3242 spin_unlock(&rssd_index_lock);
3243
3244ida_get_error:
3245 put_disk(dd->disk);
3246
3247alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003248 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003249
3250protocol_init_error:
3251 return rv;
3252}
3253
3254/*
3255 * Block layer deinitialization function.
3256 *
3257 * Called by the PCI layer as each P320 device is removed.
3258 *
3259 * @dd Pointer to the driver data structure.
3260 *
3261 * return value
3262 * 0
3263 */
Jens Axboe63166682011-09-27 21:27:43 -06003264static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003265{
3266 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003267
3268 if (dd->mtip_svc_handler) {
3269 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3270 wake_up_interruptible(&dd->port->svc_wait);
3271 kthread_stop(dd->mtip_svc_handler);
3272 }
3273
Sam Bradshaw88523a62011-08-30 08:34:26 -06003274 /* Clean up the sysfs attributes managed by the protocol layer. */
3275 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3276 if (kobj) {
3277 mtip_hw_sysfs_exit(dd, kobj);
3278 kobject_put(kobj);
3279 }
3280
3281 /*
3282 * Delete our gendisk structure. This also removes the device
3283 * from /dev
3284 */
3285 del_gendisk(dd->disk);
3286 blk_cleanup_queue(dd->queue);
3287 dd->disk = NULL;
3288 dd->queue = NULL;
3289
3290 /* De-initialize the protocol layer. */
3291 mtip_hw_exit(dd);
3292
3293 return 0;
3294}
3295
3296/*
3297 * Function called by the PCI layer when just before the
3298 * machine shuts down.
3299 *
3300 * If a protocol layer shutdown function is present it will be called
3301 * by this function.
3302 *
3303 * @dd Pointer to the driver data structure.
3304 *
3305 * return value
3306 * 0
3307 */
Jens Axboe63166682011-09-27 21:27:43 -06003308static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003309{
3310 dev_info(&dd->pdev->dev,
3311 "Shutting down %s ...\n", dd->disk->disk_name);
3312
3313 /* Delete our gendisk structure, and cleanup the blk queue. */
3314 del_gendisk(dd->disk);
3315 blk_cleanup_queue(dd->queue);
3316 dd->disk = NULL;
3317 dd->queue = NULL;
3318
3319 mtip_hw_shutdown(dd);
3320 return 0;
3321}
3322
Jens Axboe63166682011-09-27 21:27:43 -06003323static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003324{
3325 dev_info(&dd->pdev->dev,
3326 "Suspending %s ...\n", dd->disk->disk_name);
3327 mtip_hw_suspend(dd);
3328 return 0;
3329}
3330
Jens Axboe63166682011-09-27 21:27:43 -06003331static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003332{
3333 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3334 dd->disk->disk_name);
3335 mtip_hw_resume(dd);
3336 return 0;
3337}
3338
3339/*
3340 * Called for each supported PCI device detected.
3341 *
3342 * This function allocates the private data structure, enables the
3343 * PCI device and then calls the block layer initialization function.
3344 *
3345 * return value
3346 * 0 on success else an error code.
3347 */
3348static int mtip_pci_probe(struct pci_dev *pdev,
3349 const struct pci_device_id *ent)
3350{
3351 int rv = 0;
3352 struct driver_data *dd = NULL;
3353
3354 /* Allocate memory for this devices private data. */
3355 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3356 if (dd == NULL) {
3357 dev_err(&pdev->dev,
3358 "Unable to allocate memory for driver data\n");
3359 return -ENOMEM;
3360 }
3361
3362 /* Set the atomic variable as 1 in case of SRSI */
3363 atomic_set(&dd->drv_cleanup_done, true);
3364
3365 atomic_set(&dd->resumeflag, false);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003366
3367 /* Attach the private data to this PCI device. */
3368 pci_set_drvdata(pdev, dd);
3369
3370 rv = pcim_enable_device(pdev);
3371 if (rv < 0) {
3372 dev_err(&pdev->dev, "Unable to enable device\n");
3373 goto iomap_err;
3374 }
3375
3376 /* Map BAR5 to memory. */
3377 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3378 if (rv < 0) {
3379 dev_err(&pdev->dev, "Unable to map regions\n");
3380 goto iomap_err;
3381 }
3382
3383 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3384 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3385
3386 if (rv) {
3387 rv = pci_set_consistent_dma_mask(pdev,
3388 DMA_BIT_MASK(32));
3389 if (rv) {
3390 dev_warn(&pdev->dev,
3391 "64-bit DMA enable failed\n");
3392 goto setmask_err;
3393 }
3394 }
3395 }
3396
3397 pci_set_master(pdev);
3398
3399 if (pci_enable_msi(pdev)) {
3400 dev_warn(&pdev->dev,
3401 "Unable to enable MSI interrupt.\n");
3402 goto block_initialize_err;
3403 }
3404
3405 /* Copy the info we may need later into the private data structure. */
3406 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003407 dd->instance = instance;
3408 dd->pdev = pdev;
3409
3410 /* Initialize the block layer. */
3411 rv = mtip_block_initialize(dd);
3412 if (rv < 0) {
3413 dev_err(&pdev->dev,
3414 "Unable to initialize block layer\n");
3415 goto block_initialize_err;
3416 }
3417
3418 /*
3419 * Increment the instance count so that each device has a unique
3420 * instance number.
3421 */
3422 instance++;
3423
3424 goto done;
3425
3426block_initialize_err:
3427 pci_disable_msi(pdev);
3428
3429setmask_err:
3430 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3431
3432iomap_err:
3433 kfree(dd);
3434 pci_set_drvdata(pdev, NULL);
3435 return rv;
3436done:
3437 /* Set the atomic variable as 0 in case of SRSI */
3438 atomic_set(&dd->drv_cleanup_done, true);
3439
3440 return rv;
3441}
3442
3443/*
3444 * Called for each probed device when the device is removed or the
3445 * driver is unloaded.
3446 *
3447 * return value
3448 * None
3449 */
3450static void mtip_pci_remove(struct pci_dev *pdev)
3451{
3452 struct driver_data *dd = pci_get_drvdata(pdev);
3453 int counter = 0;
3454
3455 if (mtip_check_surprise_removal(pdev)) {
3456 while (atomic_read(&dd->drv_cleanup_done) == false) {
3457 counter++;
3458 msleep(20);
3459 if (counter == 10) {
3460 /* Cleanup the outstanding commands */
3461 mtip_command_cleanup(dd);
3462 break;
3463 }
3464 }
3465 }
3466 /* Set the atomic variable as 1 in case of SRSI */
3467 atomic_set(&dd->drv_cleanup_done, true);
3468
3469 /* Clean up the block layer. */
3470 mtip_block_remove(dd);
3471
3472 pci_disable_msi(pdev);
3473
3474 kfree(dd);
3475 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3476}
3477
3478/*
3479 * Called for each probed device when the device is suspended.
3480 *
3481 * return value
3482 * 0 Success
3483 * <0 Error
3484 */
3485static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3486{
3487 int rv = 0;
3488 struct driver_data *dd = pci_get_drvdata(pdev);
3489
3490 if (!dd) {
3491 dev_err(&pdev->dev,
3492 "Driver private datastructure is NULL\n");
3493 return -EFAULT;
3494 }
3495
3496 atomic_set(&dd->resumeflag, true);
3497
3498 /* Disable ports & interrupts then send standby immediate */
3499 rv = mtip_block_suspend(dd);
3500 if (rv < 0) {
3501 dev_err(&pdev->dev,
3502 "Failed to suspend controller\n");
3503 return rv;
3504 }
3505
3506 /*
3507 * Save the pci config space to pdev structure &
3508 * disable the device
3509 */
3510 pci_save_state(pdev);
3511 pci_disable_device(pdev);
3512
3513 /* Move to Low power state*/
3514 pci_set_power_state(pdev, PCI_D3hot);
3515
3516 return rv;
3517}
3518
3519/*
3520 * Called for each probed device when the device is resumed.
3521 *
3522 * return value
3523 * 0 Success
3524 * <0 Error
3525 */
3526static int mtip_pci_resume(struct pci_dev *pdev)
3527{
3528 int rv = 0;
3529 struct driver_data *dd;
3530
3531 dd = pci_get_drvdata(pdev);
3532 if (!dd) {
3533 dev_err(&pdev->dev,
3534 "Driver private datastructure is NULL\n");
3535 return -EFAULT;
3536 }
3537
3538 /* Move the device to active State */
3539 pci_set_power_state(pdev, PCI_D0);
3540
3541 /* Restore PCI configuration space */
3542 pci_restore_state(pdev);
3543
3544 /* Enable the PCI device*/
3545 rv = pcim_enable_device(pdev);
3546 if (rv < 0) {
3547 dev_err(&pdev->dev,
3548 "Failed to enable card during resume\n");
3549 goto err;
3550 }
3551 pci_set_master(pdev);
3552
3553 /*
3554 * Calls hbaReset, initPort, & startPort function
3555 * then enables interrupts
3556 */
3557 rv = mtip_block_resume(dd);
3558 if (rv < 0)
3559 dev_err(&pdev->dev, "Unable to resume\n");
3560
3561err:
3562 atomic_set(&dd->resumeflag, false);
3563
3564 return rv;
3565}
3566
3567/*
3568 * Shutdown routine
3569 *
3570 * return value
3571 * None
3572 */
3573static void mtip_pci_shutdown(struct pci_dev *pdev)
3574{
3575 struct driver_data *dd = pci_get_drvdata(pdev);
3576 if (dd)
3577 mtip_block_shutdown(dd);
3578}
3579
Sam Bradshaw88523a62011-08-30 08:34:26 -06003580/* Table of device ids supported by this driver. */
3581static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3582 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3583 { 0 }
3584};
3585
3586/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003587static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003588 .name = MTIP_DRV_NAME,
3589 .id_table = mtip_pci_tbl,
3590 .probe = mtip_pci_probe,
3591 .remove = mtip_pci_remove,
3592 .suspend = mtip_pci_suspend,
3593 .resume = mtip_pci_resume,
3594 .shutdown = mtip_pci_shutdown,
3595};
3596
3597MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3598
3599/*
3600 * Module initialization function.
3601 *
3602 * Called once when the module is loaded. This function allocates a major
3603 * block device number to the Cyclone devices and registers the PCI layer
3604 * of the driver.
3605 *
3606 * Return value
3607 * 0 on success else error code.
3608 */
3609static int __init mtip_init(void)
3610{
3611 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3612
3613 /* Allocate a major block device number to use with this driver. */
3614 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3615 if (mtip_major < 0) {
3616 printk(KERN_ERR "Unable to register block device (%d)\n",
3617 mtip_major);
3618 return -EBUSY;
3619 }
3620
3621 /* Register our PCI operations. */
3622 return pci_register_driver(&mtip_pci_driver);
3623}
3624
3625/*
3626 * Module de-initialization function.
3627 *
3628 * Called once when the module is unloaded. This function deallocates
3629 * the major block device number allocated by mtip_init() and
3630 * unregisters the PCI layer of the driver.
3631 *
3632 * Return value
3633 * none
3634 */
3635static void __exit mtip_exit(void)
3636{
3637 /* Release the allocated major block device number. */
3638 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3639
3640 /* Unregister the PCI driver. */
3641 pci_unregister_driver(&mtip_pci_driver);
3642}
3643
3644MODULE_AUTHOR("Micron Technology, Inc");
3645MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3646MODULE_LICENSE("GPL");
3647MODULE_VERSION(MTIP_DRV_VERSION);
3648
3649module_init(mtip_init);
3650module_exit(mtip_exit);