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