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