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