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