blob: 79fdb063f9c05d54533e187d0032e827fd7b2b2b [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
Jens Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
34#include <linux/bio.h>
35#include <linux/dma-mapping.h>
36#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010037#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060038#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020039#include <linux/export.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060040#include "mtip32xx.h"
41
42#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
43#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
44#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
45#define HW_PORT_PRIV_DMA_SZ \
46 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
47
Asai Thambi S P45038362012-04-09 08:35:38 +020048#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060049#define HOST_HSORG 0xFC
50#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
51#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
52#define HSORG_HWREV 0xFF00
53#define HSORG_STYLE 0x8
54#define HSORG_SLOTGROUPS 0x7
55
56#define PORT_COMMAND_ISSUE 0x38
57#define PORT_SDBV 0x7C
58
59#define PORT_OFFSET 0x100
60#define PORT_MEM_SIZE 0x80
61
62#define PORT_IRQ_ERR \
63 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
64 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
65 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
66 PORT_IRQ_OVERFLOW)
67#define PORT_IRQ_LEGACY \
68 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
69#define PORT_IRQ_HANDLED \
70 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
71 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
72 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
73#define DEF_PORT_IRQ \
74 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
75
76/* product numbers */
77#define MTIP_PRODUCT_UNKNOWN 0x00
78#define MTIP_PRODUCT_ASICFPGA 0x11
79
80/* Device instance number, incremented each time a device is probed. */
81static int instance;
82
83/*
84 * Global variable used to hold the major block device number
85 * allocated in mtip_init().
86 */
Jens Axboe3ff147d2011-09-27 21:33:53 -060087static int mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -060088
89static DEFINE_SPINLOCK(rssd_index_lock);
90static DEFINE_IDA(rssd_index_ida);
91
Asai Thambi S P62ee8c12012-01-04 22:01:32 +010092static int mtip_block_initialize(struct driver_data *dd);
93
Jens Axboe16d02c02011-09-27 15:50:01 -060094#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -060095struct mtip_compat_ide_task_request_s {
96 __u8 io_ports[8];
97 __u8 hob_ports[8];
98 ide_reg_valid_t out_flags;
99 ide_reg_valid_t in_flags;
100 int data_phase;
101 int req_cmd;
102 compat_ulong_t out_size;
103 compat_ulong_t in_size;
104};
Jens Axboe16d02c02011-09-27 15:50:01 -0600105#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600106
Sam Bradshaw88523a62011-08-30 08:34:26 -0600107/*
Jens Axboe63166682011-09-27 21:27:43 -0600108 * This function check_for_surprise_removal is called
109 * while card is removed from the system and it will
110 * read the vendor id from the configration space
111 *
112 * @pdev Pointer to the pci_dev structure.
113 *
114 * return value
115 * true if device removed, else false
116 */
117static bool mtip_check_surprise_removal(struct pci_dev *pdev)
118{
119 u16 vendor_id = 0;
120
121 /* Read the vendorID from the configuration space */
122 pci_read_config_word(pdev, 0x00, &vendor_id);
123 if (vendor_id == 0xFFFF)
124 return true; /* device removed */
125
126 return false; /* device present */
127}
128
129/*
130 * This function is called for clean the pending command in the
131 * command slot during the surprise removal of device and return
132 * error to the upper layer.
133 *
134 * @dd Pointer to the DRIVER_DATA structure.
135 *
136 * return value
137 * None
138 */
139static void mtip_command_cleanup(struct driver_data *dd)
140{
141 int group = 0, commandslot = 0, commandindex = 0;
142 struct mtip_cmd *command;
143 struct mtip_port *port = dd->port;
Asai Thambi S P45038362012-04-09 08:35:38 +0200144 static int in_progress;
145
146 if (in_progress)
147 return;
148
149 in_progress = 1;
Jens Axboe63166682011-09-27 21:27:43 -0600150
151 for (group = 0; group < 4; group++) {
152 for (commandslot = 0; commandslot < 32; commandslot++) {
153 if (!(port->allocated[group] & (1 << commandslot)))
154 continue;
155
156 commandindex = group << 5 | commandslot;
157 command = &port->commands[commandindex];
158
159 if (atomic_read(&command->active)
160 && (command->async_callback)) {
161 command->async_callback(command->async_data,
162 -ENODEV);
163 command->async_callback = NULL;
164 command->async_data = NULL;
165 }
166
167 dma_unmap_sg(&port->dd->pdev->dev,
168 command->sg,
169 command->scatter_ents,
170 command->direction);
171 }
172 }
173
174 up(&port->cmd_slot);
175
Asai Thambi S P45038362012-04-09 08:35:38 +0200176 set_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag);
177 in_progress = 0;
Jens Axboe63166682011-09-27 21:27:43 -0600178}
179
180/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600181 * Obtain an empty command slot.
182 *
183 * This function needs to be reentrant since it could be called
184 * at the same time on multiple CPUs. The allocation of the
185 * command slot must be atomic.
186 *
187 * @port Pointer to the port data structure.
188 *
189 * return value
190 * >= 0 Index of command slot obtained.
191 * -1 No command slots available.
192 */
193static int get_slot(struct mtip_port *port)
194{
195 int slot, i;
196 unsigned int num_command_slots = port->dd->slot_groups * 32;
197
198 /*
199 * Try 10 times, because there is a small race here.
200 * that's ok, because it's still cheaper than a lock.
201 *
202 * Race: Since this section is not protected by lock, same bit
203 * could be chosen by different process contexts running in
204 * different processor. So instead of costly lock, we are going
205 * with loop.
206 */
207 for (i = 0; i < 10; i++) {
208 slot = find_next_zero_bit(port->allocated,
209 num_command_slots, 1);
210 if ((slot < num_command_slots) &&
211 (!test_and_set_bit(slot, port->allocated)))
212 return slot;
213 }
214 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
215
216 if (mtip_check_surprise_removal(port->dd->pdev)) {
217 /* Device not present, clean outstanding commands */
218 mtip_command_cleanup(port->dd);
219 }
220 return -1;
221}
222
223/*
224 * Release a command slot.
225 *
226 * @port Pointer to the port data structure.
227 * @tag Tag of command to release
228 *
229 * return value
230 * None
231 */
232static inline void release_slot(struct mtip_port *port, int tag)
233{
234 smp_mb__before_clear_bit();
235 clear_bit(tag, port->allocated);
236 smp_mb__after_clear_bit();
237}
238
239/*
Jens Axboe63166682011-09-27 21:27:43 -0600240 * Reset the HBA (without sleeping)
241 *
242 * Just like hba_reset, except does not call sleep, so can be
243 * run from interrupt/tasklet context.
244 *
245 * @dd Pointer to the driver data structure.
246 *
247 * return value
248 * 0 The reset was successful.
249 * -1 The HBA Reset bit did not clear.
250 */
251static int hba_reset_nosleep(struct driver_data *dd)
252{
253 unsigned long timeout;
254
255 /* Chip quirk: quiesce any chip function */
256 mdelay(10);
257
258 /* Set the reset bit */
259 writel(HOST_RESET, dd->mmio + HOST_CTL);
260
261 /* Flush */
262 readl(dd->mmio + HOST_CTL);
263
264 /*
265 * Wait 10ms then spin for up to 1 second
266 * waiting for reset acknowledgement
267 */
268 timeout = jiffies + msecs_to_jiffies(1000);
269 mdelay(10);
270 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
271 && time_before(jiffies, timeout))
272 mdelay(1);
273
Asai Thambi S P45038362012-04-09 08:35:38 +0200274 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))
275 return -1;
276
Jens Axboe63166682011-09-27 21:27:43 -0600277 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
278 return -1;
279
280 return 0;
281}
282
283/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600284 * Issue a command to the hardware.
285 *
286 * Set the appropriate bit in the s_active and Command Issue hardware
287 * registers, causing hardware command processing to begin.
288 *
289 * @port Pointer to the port structure.
290 * @tag The tag of the command to be issued.
291 *
292 * return value
293 * None
294 */
295static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
296{
297 unsigned long flags = 0;
298
299 atomic_set(&port->commands[tag].active, 1);
300
301 spin_lock_irqsave(&port->cmd_issue_lock, flags);
302
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
307
308 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
Asai Thambi S Pdad40f12012-04-09 08:35:38 +0200309
310 /* Set the command's timeout value.*/
311 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
312 MTIP_NCQ_COMMAND_TIMEOUT_MS);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600313}
314
315/*
Jens Axboe63166682011-09-27 21:27:43 -0600316 * 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
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100427 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600428 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100429 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600430
431 /* Clear SError */
432 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
433
434 /* reset the completed registers.*/
435 for (i = 0; i < port->dd->slot_groups; i++)
436 writel(0xFFFFFFFF, port->completed[i]);
437
438 /* Clear any pending interrupts for this port */
439 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
440
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100441 /* Clear any pending interrupts on the HBA. */
442 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
443 port->dd->mmio + HOST_IRQ_STAT);
444
Jens Axboe63166682011-09-27 21:27:43 -0600445 /* Enable port interrupts */
446 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
447}
448
449/*
450 * Restart a port
451 *
452 * @port Pointer to the port data structure.
453 *
454 * return value
455 * None
456 */
457static void mtip_restart_port(struct mtip_port *port)
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
Asai Thambi S P45038362012-04-09 08:35:38 +0200470 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
471 return;
472
Jens Axboe63166682011-09-27 21:27:43 -0600473 /*
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
Asai Thambi S P45038362012-04-09 08:35:38 +0200501 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
502 return;
503
Jens Axboe63166682011-09-27 21:27:43 -0600504 /* 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
Asai Thambi S P45038362012-04-09 08:35:38 +0200515 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
516 return;
517
Jens Axboe63166682011-09-27 21:27:43 -0600518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
521
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100522 mtip_init_port(port);
523 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600524
Jens Axboe63166682011-09-27 21:27:43 -0600525}
526
527/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600528 * Called periodically to see if any read/write commands are
529 * taking too long to complete.
530 *
531 * @data Pointer to the PORT data structure.
532 *
533 * return value
534 * None
535 */
Jens Axboe63166682011-09-27 21:27:43 -0600536static void mtip_timeout_function(unsigned long int data)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600537{
538 struct mtip_port *port = (struct mtip_port *) data;
539 struct host_to_dev_fis *fis;
540 struct mtip_cmd *command;
541 int tag, cmdto_cnt = 0;
542 unsigned int bit, group;
543 unsigned int num_command_slots = port->dd->slot_groups * 32;
544
545 if (unlikely(!port))
546 return;
547
Asai Thambi S P45038362012-04-09 08:35:38 +0200548 if (test_bit(MTIP_DD_FLAG_RESUME_BIT, &port->dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600549 mod_timer(&port->cmd_timer,
550 jiffies + msecs_to_jiffies(30000));
551 return;
552 }
553
554 for (tag = 0; tag < num_command_slots; tag++) {
555 /*
556 * Skip internal command slot as it has
557 * its own timeout mechanism
558 */
559 if (tag == MTIP_TAG_INTERNAL)
560 continue;
561
562 if (atomic_read(&port->commands[tag].active) &&
563 (time_after(jiffies, port->commands[tag].comp_time))) {
564 group = tag >> 5;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100565 bit = tag & 0x1F;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600566
567 command = &port->commands[tag];
568 fis = (struct host_to_dev_fis *) command->command;
569
570 dev_warn(&port->dd->pdev->dev,
571 "Timeout for command tag %d\n", tag);
572
573 cmdto_cnt++;
574 if (cmdto_cnt == 1)
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100575 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600576
577 /*
578 * Clear the completed bit. This should prevent
579 * any interrupt handlers from trying to retire
580 * the command.
581 */
582 writel(1 << bit, port->completed[group]);
583
584 /* Call the async completion callback. */
585 if (likely(command->async_callback))
586 command->async_callback(command->async_data,
587 -EIO);
588 command->async_callback = NULL;
589 command->comp_func = NULL;
590
591 /* Unmap the DMA scatter list entries */
592 dma_unmap_sg(&port->dd->pdev->dev,
593 command->sg,
594 command->scatter_ents,
595 command->direction);
596
597 /*
598 * Clear the allocated bit and active tag for the
599 * command.
600 */
601 atomic_set(&port->commands[tag].active, 0);
602 release_slot(port, tag);
603
604 up(&port->cmd_slot);
605 }
606 }
607
608 if (cmdto_cnt) {
609 dev_warn(&port->dd->pdev->dev,
610 "%d commands timed out: restarting port",
611 cmdto_cnt);
612 mtip_restart_port(port);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100613 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
614 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600615 }
616
617 /* Restart the timer */
618 mod_timer(&port->cmd_timer,
619 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
620}
621
622/*
623 * IO completion function.
624 *
625 * This completion function is called by the driver ISR when a
626 * command that was issued by the kernel completes. It first calls the
627 * asynchronous completion function which normally calls back into the block
628 * layer passing the asynchronous callback data, then unmaps the
629 * scatter list associated with the completed command, and finally
630 * clears the allocated bit associated with the completed command.
631 *
632 * @port Pointer to the port data structure.
633 * @tag Tag of the command.
634 * @data Pointer to driver_data.
635 * @status Completion status.
636 *
637 * return value
638 * None
639 */
640static void mtip_async_complete(struct mtip_port *port,
641 int tag,
642 void *data,
643 int status)
644{
645 struct mtip_cmd *command;
646 struct driver_data *dd = data;
647 int cb_status = status ? -EIO : 0;
648
649 if (unlikely(!dd) || unlikely(!port))
650 return;
651
652 command = &port->commands[tag];
653
654 if (unlikely(status == PORT_IRQ_TF_ERR)) {
655 dev_warn(&port->dd->pdev->dev,
656 "Command tag %d failed due to TFE\n", tag);
657 }
658
659 /* Upper layer callback */
660 if (likely(command->async_callback))
661 command->async_callback(command->async_data, cb_status);
662
663 command->async_callback = NULL;
664 command->comp_func = NULL;
665
666 /* Unmap the DMA scatter list entries */
667 dma_unmap_sg(&dd->pdev->dev,
668 command->sg,
669 command->scatter_ents,
670 command->direction);
671
672 /* Clear the allocated and active bits for the command */
673 atomic_set(&port->commands[tag].active, 0);
674 release_slot(port, tag);
675
676 up(&port->cmd_slot);
677}
678
679/*
680 * Internal command completion callback function.
681 *
682 * This function is normally called by the driver ISR when an internal
683 * command completed. This function signals the command completion by
684 * calling complete().
685 *
686 * @port Pointer to the port data structure.
687 * @tag Tag of the command that has completed.
688 * @data Pointer to a completion structure.
689 * @status Completion status.
690 *
691 * return value
692 * None
693 */
694static void mtip_completion(struct mtip_port *port,
695 int tag,
696 void *data,
697 int status)
698{
699 struct mtip_cmd *command = &port->commands[tag];
700 struct completion *waiting = data;
701 if (unlikely(status == PORT_IRQ_TF_ERR))
702 dev_warn(&port->dd->pdev->dev,
703 "Internal command %d completed with TFE\n", tag);
704
705 command->async_callback = NULL;
706 command->comp_func = NULL;
707
708 complete(waiting);
709}
710
711/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600712 * Helper function for tag logging
713 */
714static void print_tags(struct driver_data *dd,
715 char *msg,
716 unsigned long *tagbits)
717{
718 unsigned int tag, count = 0;
719
720 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
721 if (test_bit(tag, tagbits))
722 count++;
723 }
724 if (count)
725 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
726}
727
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200728static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
729 dma_addr_t buffer_dma, unsigned int sectors);
730static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
731 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600732/*
733 * Handle an error.
734 *
735 * @dd Pointer to the DRIVER_DATA structure.
736 *
737 * return value
738 * None
739 */
740static void mtip_handle_tfe(struct driver_data *dd)
741{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200742 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600743 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200744 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600745 u32 completed;
746 struct host_to_dev_fis *fis;
747 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200748 unsigned char *buf;
749 char *fail_reason = NULL;
750 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600751
752 dev_warn(&dd->pdev->dev, "Taskfile error\n");
753
754 port = dd->port;
755
756 /* Stop the timer to prevent command timeouts. */
757 del_timer(&port->cmd_timer);
758
759 /* Set eh_active */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100760 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600761
762 /* Loop through all the groups */
763 for (group = 0; group < dd->slot_groups; group++) {
764 completed = readl(port->completed[group]);
765
766 /* clear completed status register in the hardware.*/
767 writel(completed, port->completed[group]);
768
769 /* clear the tag accumulator */
770 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
771
772 /* Process successfully completed commands */
773 for (bit = 0; bit < 32 && completed; bit++) {
774 if (!(completed & (1<<bit)))
775 continue;
776 tag = (group << 5) + bit;
777
778 /* Skip the internal command slot */
779 if (tag == MTIP_TAG_INTERNAL)
780 continue;
781
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200782 cmd = &port->commands[tag];
783 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600784 set_bit(tag, tagaccum);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200785 atomic_set(&cmd->active, 0);
786 cmd->comp_func(port,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600787 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200788 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600789 0);
790 } else {
791 dev_err(&port->dd->pdev->dev,
792 "Missing completion func for tag %d",
793 tag);
794 if (mtip_check_surprise_removal(dd->pdev)) {
795 mtip_command_cleanup(dd);
796 /* don't proceed further */
797 return;
798 }
799 }
800 }
801 }
802 print_tags(dd, "TFE tags completed:", tagaccum);
803
804 /* Restart the port */
805 mdelay(20);
806 mtip_restart_port(port);
807
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200808 /* Trying to determine the cause of the error */
809 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
810 dd->port->log_buf,
811 dd->port->log_buf_dma, 1);
812 if (rv) {
813 dev_warn(&dd->pdev->dev,
814 "Error in READ LOG EXT (10h) command\n");
815 /* non-critical error, don't fail the load */
816 } else {
817 buf = (unsigned char *)dd->port->log_buf;
818 if (buf[259] & 0x1) {
819 dev_info(&dd->pdev->dev,
820 "Write protect bit is set.\n");
821 set_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag);
822 fail_all_ncq_write = 1;
823 fail_reason = "write protect";
824 }
825 if (buf[288] == 0xF7) {
826 dev_info(&dd->pdev->dev,
827 "Exceeded Tmax, drive in thermal shutdown.\n");
828 set_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag);
829 fail_all_ncq_cmds = 1;
830 fail_reason = "thermal shutdown";
831 }
832 if (buf[288] == 0xBF) {
833 dev_info(&dd->pdev->dev,
834 "Drive indicates rebuild has failed.\n");
835 fail_all_ncq_cmds = 1;
836 fail_reason = "rebuild failed";
837 }
838 }
839
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840 /* clear the tag accumulator */
841 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
842
843 /* Loop through all the groups */
844 for (group = 0; group < dd->slot_groups; group++) {
845 for (bit = 0; bit < 32; bit++) {
846 reissue = 1;
847 tag = (group << 5) + bit;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200848 cmd = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600849
850 /* If the active bit is set re-issue the command */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200851 if (atomic_read(&cmd->active) == 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600852 continue;
853
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200854 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600855
856 /* Should re-issue? */
857 if (tag == MTIP_TAG_INTERNAL ||
858 fis->command == ATA_CMD_SET_FEATURES)
859 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200860 else {
861 if (fail_all_ncq_cmds ||
862 (fail_all_ncq_write &&
863 fis->command == ATA_CMD_FPDMA_WRITE)) {
864 dev_warn(&dd->pdev->dev,
865 " Fail: %s w/tag %d [%s].\n",
866 fis->command == ATA_CMD_FPDMA_WRITE ?
867 "write" : "read",
868 tag,
869 fail_reason != NULL ?
870 fail_reason : "unknown");
871 atomic_set(&cmd->active, 0);
872 if (cmd->comp_func) {
873 cmd->comp_func(port, tag,
874 cmd->comp_data,
875 -ENODATA);
876 }
877 continue;
878 }
879 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600880
881 /*
882 * First check if this command has
883 * exceeded its retries.
884 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200885 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600886
887 set_bit(tag, tagaccum);
888
Sam Bradshaw88523a62011-08-30 08:34:26 -0600889 /* Re-issue the command. */
890 mtip_issue_ncq_command(port, tag);
891
892 continue;
893 }
894
895 /* Retire a command that will not be reissued */
896 dev_warn(&port->dd->pdev->dev,
897 "retiring tag %d\n", tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200898 atomic_set(&cmd->active, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600899
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200900 if (cmd->comp_func)
901 cmd->comp_func(
Sam Bradshaw88523a62011-08-30 08:34:26 -0600902 port,
903 tag,
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200904 cmd->comp_data,
Sam Bradshaw88523a62011-08-30 08:34:26 -0600905 PORT_IRQ_TF_ERR);
906 else
907 dev_warn(&port->dd->pdev->dev,
908 "Bad completion for tag %d\n",
909 tag);
910 }
911 }
912 print_tags(dd, "TFE tags reissued:", tagaccum);
913
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100914 /* clear eh_active */
915 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
916 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600917
918 mod_timer(&port->cmd_timer,
919 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
920}
921
922/*
923 * Handle a set device bits interrupt
924 */
925static inline void mtip_process_sdbf(struct driver_data *dd)
926{
927 struct mtip_port *port = dd->port;
928 int group, tag, bit;
929 u32 completed;
930 struct mtip_cmd *command;
931
932 /* walk all bits in all slot groups */
933 for (group = 0; group < dd->slot_groups; group++) {
934 completed = readl(port->completed[group]);
935
936 /* clear completed status register in the hardware.*/
937 writel(completed, port->completed[group]);
938
939 /* Process completed commands. */
940 for (bit = 0;
941 (bit < 32) && completed;
942 bit++, completed >>= 1) {
943 if (completed & 0x01) {
944 tag = (group << 5) | bit;
945
946 /* skip internal command slot. */
947 if (unlikely(tag == MTIP_TAG_INTERNAL))
948 continue;
949
950 command = &port->commands[tag];
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951 /* make internal callback */
952 if (likely(command->comp_func)) {
953 command->comp_func(
954 port,
955 tag,
956 command->comp_data,
957 0);
958 } else {
959 dev_warn(&dd->pdev->dev,
960 "Null completion "
961 "for tag %d",
962 tag);
963
964 if (mtip_check_surprise_removal(
965 dd->pdev)) {
966 mtip_command_cleanup(dd);
967 return;
968 }
969 }
970 }
971 }
972 }
973}
974
975/*
976 * Process legacy pio and d2h interrupts
977 */
978static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
979{
980 struct mtip_port *port = dd->port;
981 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
982
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100983 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
984 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600985 & (1 << MTIP_TAG_INTERNAL))) {
986 if (cmd->comp_func) {
987 cmd->comp_func(port,
988 MTIP_TAG_INTERNAL,
989 cmd->comp_data,
990 0);
991 return;
992 }
993 }
994
995 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
996
997 return;
998}
999
1000/*
1001 * Demux and handle errors
1002 */
1003static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1004{
1005 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1006 mtip_handle_tfe(dd);
1007
1008 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1009 dev_warn(&dd->pdev->dev,
1010 "Clearing PxSERR.DIAG.x\n");
1011 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1012 }
1013
1014 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1015 dev_warn(&dd->pdev->dev,
1016 "Clearing PxSERR.DIAG.n\n");
1017 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1018 }
1019
1020 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1021 dev_warn(&dd->pdev->dev,
1022 "Port stat errors %x unhandled\n",
1023 (port_stat & ~PORT_IRQ_HANDLED));
1024 }
1025}
1026
1027static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1028{
1029 struct driver_data *dd = (struct driver_data *) data;
1030 struct mtip_port *port = dd->port;
1031 u32 hba_stat, port_stat;
1032 int rv = IRQ_NONE;
1033
1034 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1035 if (hba_stat) {
1036 rv = IRQ_HANDLED;
1037
1038 /* Acknowledge the interrupt status on the port.*/
1039 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1040 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1041
1042 /* Demux port status */
1043 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1044 mtip_process_sdbf(dd);
1045
1046 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1047 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1048 mtip_command_cleanup(dd);
1049 /* don't proceed further */
1050 return IRQ_HANDLED;
1051 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001052 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1053 &dd->dd_flag))
1054 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001055
1056 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1057 }
1058
1059 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1060 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1061 }
1062
1063 /* acknowledge interrupt */
1064 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1065
1066 return rv;
1067}
1068
1069/*
1070 * Wrapper for mtip_handle_irq
1071 * (ignores return code)
1072 */
1073static void mtip_tasklet(unsigned long data)
1074{
1075 mtip_handle_irq((struct driver_data *) data);
1076}
1077
1078/*
1079 * HBA interrupt subroutine.
1080 *
1081 * @irq IRQ number.
1082 * @instance Pointer to the driver data structure.
1083 *
1084 * return value
1085 * IRQ_HANDLED A HBA interrupt was pending and handled.
1086 * IRQ_NONE This interrupt was not for the HBA.
1087 */
1088static irqreturn_t mtip_irq_handler(int irq, void *instance)
1089{
1090 struct driver_data *dd = instance;
1091 tasklet_schedule(&dd->tasklet);
1092 return IRQ_HANDLED;
1093}
1094
1095static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1096{
1097 atomic_set(&port->commands[tag].active, 1);
1098 writel(1 << MTIP_TAG_BIT(tag),
1099 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1100}
1101
1102/*
1103 * Wait for port to quiesce
1104 *
1105 * @port Pointer to port data structure
1106 * @timeout Max duration to wait (ms)
1107 *
1108 * return value
1109 * 0 Success
1110 * -EBUSY Commands still active
1111 */
1112static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1113{
1114 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001115 unsigned int n;
1116 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001117
1118 to = jiffies + msecs_to_jiffies(timeout);
1119 do {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01001120 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1121 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001122 msleep(20);
1123 continue; /* svc thd is actively issuing commands */
1124 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001125 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1126 &port->dd->dd_flag))
1127 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001128 /*
1129 * Ignore s_active bit 0 of array element 0.
1130 * This bit will always be set
1131 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001132 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133 for (n = 1; n < port->dd->slot_groups; n++)
1134 active |= readl(port->s_active[n]);
1135
1136 if (!active)
1137 break;
1138
1139 msleep(20);
1140 } while (time_before(jiffies, to));
1141
1142 return active ? -EBUSY : 0;
1143}
1144
1145/*
1146 * Execute an internal command and wait for the completion.
1147 *
1148 * @port Pointer to the port data structure.
1149 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001150 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001151 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001152 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001153 * @opts Command header options, excluding the FIS length
1154 * and the number of PRD entries.
1155 * @timeout Time in ms to wait for the command to complete.
1156 *
1157 * return value
1158 * 0 Command completed successfully.
1159 * -EFAULT The buffer address is not correctly aligned.
1160 * -EBUSY Internal command or other IO in progress.
1161 * -EAGAIN Time out waiting for command to complete.
1162 */
1163static int mtip_exec_internal_command(struct mtip_port *port,
1164 void *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001165 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001166 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001167 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001168 u32 opts,
1169 gfp_t atomic,
1170 unsigned long timeout)
1171{
1172 struct mtip_cmd_sg *command_sg;
1173 DECLARE_COMPLETION_ONSTACK(wait);
1174 int rv = 0;
1175 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1176
1177 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1178 if (buffer & 0x00000007) {
1179 dev_err(&port->dd->pdev->dev,
1180 "SG buffer is not 8 byte aligned\n");
1181 return -EFAULT;
1182 }
1183
1184 /* Only one internal command should be running at a time */
1185 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1186 dev_warn(&port->dd->pdev->dev,
1187 "Internal command already active\n");
1188 return -EBUSY;
1189 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001190 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001191
1192 if (atomic == GFP_KERNEL) {
1193 /* wait for io to complete if non atomic */
1194 if (mtip_quiesce_io(port, 5000) < 0) {
1195 dev_warn(&port->dd->pdev->dev,
1196 "Failed to quiesce IO\n");
1197 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001198 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1199 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001200 return -EBUSY;
1201 }
1202
1203 /* Set the completion function and data for the command. */
1204 int_cmd->comp_data = &wait;
1205 int_cmd->comp_func = mtip_completion;
1206
1207 } else {
1208 /* Clear completion - we're going to poll */
1209 int_cmd->comp_data = NULL;
1210 int_cmd->comp_func = NULL;
1211 }
1212
1213 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001214 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001215
1216 /* Populate the SG list */
1217 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001218 __force_bit2int cpu_to_le32(opts | fis_len);
1219 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001220 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1221
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001222 command_sg->info =
1223 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1224 command_sg->dba =
1225 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1226 command_sg->dba_upper =
1227 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001228
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001229 int_cmd->command_header->opts |=
1230 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001231 }
1232
1233 /* Populate the command header */
1234 int_cmd->command_header->byte_count = 0;
1235
1236 /* Issue the command to the hardware */
1237 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1238
1239 /* Poll if atomic, wait_for_completion otherwise */
1240 if (atomic == GFP_KERNEL) {
1241 /* Wait for the command to complete or timeout. */
1242 if (wait_for_completion_timeout(
1243 &wait,
1244 msecs_to_jiffies(timeout)) == 0) {
1245 dev_err(&port->dd->pdev->dev,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001246 "Internal command did not complete [%d] "
1247 "within timeout of %lu ms\n",
1248 atomic, timeout);
Asai Thambi S P45038362012-04-09 08:35:38 +02001249 if (mtip_check_surprise_removal(port->dd->pdev) ||
1250 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1251 &port->dd->dd_flag)) {
1252 rv = -ENXIO;
1253 goto exec_ic_exit;
1254 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001255 rv = -EAGAIN;
1256 }
1257
1258 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1259 & (1 << MTIP_TAG_INTERNAL)) {
1260 dev_warn(&port->dd->pdev->dev,
1261 "Retiring internal command but CI is 1.\n");
Asai Thambi S P45038362012-04-09 08:35:38 +02001262 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1263 &port->dd->dd_flag)) {
1264 hba_reset_nosleep(port->dd);
1265 rv = -ENXIO;
1266 } else {
1267 mtip_restart_port(port);
1268 rv = -EAGAIN;
1269 }
1270 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001271 }
1272
1273 } else {
1274 /* Spin for <timeout> checking if command still outstanding */
1275 timeout = jiffies + msecs_to_jiffies(timeout);
1276
1277 while ((readl(
1278 port->cmd_issue[MTIP_TAG_INTERNAL])
1279 & (1 << MTIP_TAG_INTERNAL))
Asai Thambi S P45038362012-04-09 08:35:38 +02001280 && time_before(jiffies, timeout)) {
1281 if (mtip_check_surprise_removal(port->dd->pdev) ||
1282 test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1283 &port->dd->dd_flag)) {
1284 rv = -ENXIO;
1285 goto exec_ic_exit;
1286 }
1287 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001288
1289 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1290 & (1 << MTIP_TAG_INTERNAL)) {
1291 dev_err(&port->dd->pdev->dev,
1292 "Internal command did not complete [%d]\n",
1293 atomic);
1294 rv = -EAGAIN;
Asai Thambi S P45038362012-04-09 08:35:38 +02001295 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
1296 &port->dd->dd_flag)) {
1297 hba_reset_nosleep(port->dd);
1298 rv = -ENXIO;
1299 } else {
1300 mtip_restart_port(port);
1301 rv = -EAGAIN;
1302 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001303 }
1304 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001305exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001306 /* Clear the allocated and active bits for the internal command. */
1307 atomic_set(&int_cmd->active, 0);
1308 release_slot(port, MTIP_TAG_INTERNAL);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001309 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1310 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001311
1312 return rv;
1313}
1314
1315/*
1316 * Byte-swap ATA ID strings.
1317 *
1318 * ATA identify data contains strings in byte-swapped 16-bit words.
1319 * They must be swapped (on all architectures) to be usable as C strings.
1320 * This function swaps bytes in-place.
1321 *
1322 * @buf The buffer location of the string
1323 * @len The number of bytes to swap
1324 *
1325 * return value
1326 * None
1327 */
1328static inline void ata_swap_string(u16 *buf, unsigned int len)
1329{
1330 int i;
1331 for (i = 0; i < (len/2); i++)
1332 be16_to_cpus(&buf[i]);
1333}
1334
1335/*
1336 * Request the device identity information.
1337 *
1338 * If a user space buffer is not specified, i.e. is NULL, the
1339 * identify information is still read from the drive and placed
1340 * into the identify data buffer (@e port->identify) in the
1341 * port data structure.
1342 * When the identify buffer contains valid identify information @e
1343 * port->identify_valid is non-zero.
1344 *
1345 * @port Pointer to the port structure.
1346 * @user_buffer A user space buffer where the identify data should be
1347 * copied.
1348 *
1349 * return value
1350 * 0 Command completed successfully.
1351 * -EFAULT An error occurred while coping data to the user buffer.
1352 * -1 Command failed.
1353 */
1354static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1355{
1356 int rv = 0;
1357 struct host_to_dev_fis fis;
1358
Asai Thambi S P45038362012-04-09 08:35:38 +02001359 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1360 return -EFAULT;
1361
Sam Bradshaw88523a62011-08-30 08:34:26 -06001362 /* Build the FIS. */
1363 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1364 fis.type = 0x27;
1365 fis.opts = 1 << 7;
1366 fis.command = ATA_CMD_ID_ATA;
1367
1368 /* Set the identify information as invalid. */
1369 port->identify_valid = 0;
1370
1371 /* Clear the identify information. */
1372 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1373
1374 /* Execute the command. */
1375 if (mtip_exec_internal_command(port,
1376 &fis,
1377 5,
1378 port->identify_dma,
1379 sizeof(u16) * ATA_ID_WORDS,
1380 0,
1381 GFP_KERNEL,
1382 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1383 < 0) {
1384 rv = -1;
1385 goto out;
1386 }
1387
1388 /*
1389 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1390 * perform field-sensitive swapping on the string fields.
1391 * See the kernel use of ata_id_string() for proof of this.
1392 */
1393#ifdef __LITTLE_ENDIAN
1394 ata_swap_string(port->identify + 27, 40); /* model string*/
1395 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1396 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1397#else
1398 {
1399 int i;
1400 for (i = 0; i < ATA_ID_WORDS; i++)
1401 port->identify[i] = le16_to_cpu(port->identify[i]);
1402 }
1403#endif
1404
1405 /* Set the identify buffer as valid. */
1406 port->identify_valid = 1;
1407
1408 if (user_buffer) {
1409 if (copy_to_user(
1410 user_buffer,
1411 port->identify,
1412 ATA_ID_WORDS * sizeof(u16))) {
1413 rv = -EFAULT;
1414 goto out;
1415 }
1416 }
1417
1418out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001419 return rv;
1420}
1421
1422/*
1423 * Issue a standby immediate command to the device.
1424 *
1425 * @port Pointer to the port structure.
1426 *
1427 * return value
1428 * 0 Command was executed successfully.
1429 * -1 An error occurred while executing the command.
1430 */
1431static int mtip_standby_immediate(struct mtip_port *port)
1432{
1433 int rv;
1434 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001435 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001436
Sam Bradshaw88523a62011-08-30 08:34:26 -06001437 /* Build the FIS. */
1438 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1439 fis.type = 0x27;
1440 fis.opts = 1 << 7;
1441 fis.command = ATA_CMD_STANDBYNOW1;
1442
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001443 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001444 rv = mtip_exec_internal_command(port,
1445 &fis,
1446 5,
1447 0,
1448 0,
1449 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001450 GFP_ATOMIC,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001451 15000);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001452 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1453 jiffies_to_msecs(jiffies - start));
1454 if (rv)
1455 dev_warn(&port->dd->pdev->dev,
1456 "STANDBY IMMEDIATE command failed.\n");
1457
1458 return rv;
1459}
1460
1461/*
1462 * Issue a READ LOG EXT command to the device.
1463 *
1464 * @port pointer to the port structure.
1465 * @page page number to fetch
1466 * @buffer pointer to buffer
1467 * @buffer_dma dma address corresponding to @buffer
1468 * @sectors page length to fetch, in sectors
1469 *
1470 * return value
1471 * @rv return value from mtip_exec_internal_command()
1472 */
1473static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1474 dma_addr_t buffer_dma, unsigned int sectors)
1475{
1476 struct host_to_dev_fis fis;
1477
1478 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1479 fis.type = 0x27;
1480 fis.opts = 1 << 7;
1481 fis.command = ATA_CMD_READ_LOG_EXT;
1482 fis.sect_count = sectors & 0xFF;
1483 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1484 fis.lba_low = page;
1485 fis.lba_mid = 0;
1486 fis.device = ATA_DEVICE_OBS;
1487
1488 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1489
1490 return mtip_exec_internal_command(port,
1491 &fis,
1492 5,
1493 buffer_dma,
1494 sectors * ATA_SECT_SIZE,
1495 0,
1496 GFP_ATOMIC,
1497 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1498}
1499
1500/*
1501 * Issue a SMART READ DATA command to the device.
1502 *
1503 * @port pointer to the port structure.
1504 * @buffer pointer to buffer
1505 * @buffer_dma dma address corresponding to @buffer
1506 *
1507 * return value
1508 * @rv return value from mtip_exec_internal_command()
1509 */
1510static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1511 dma_addr_t buffer_dma)
1512{
1513 struct host_to_dev_fis fis;
1514
1515 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1516 fis.type = 0x27;
1517 fis.opts = 1 << 7;
1518 fis.command = ATA_CMD_SMART;
1519 fis.features = 0xD0;
1520 fis.sect_count = 1;
1521 fis.lba_mid = 0x4F;
1522 fis.lba_hi = 0xC2;
1523 fis.device = ATA_DEVICE_OBS;
1524
1525 return mtip_exec_internal_command(port,
1526 &fis,
1527 5,
1528 buffer_dma,
1529 ATA_SECT_SIZE,
1530 0,
1531 GFP_ATOMIC,
1532 15000);
1533}
1534
1535/*
1536 * Get the value of a smart attribute
1537 *
1538 * @port pointer to the port structure
1539 * @id attribute number
1540 * @attrib pointer to return attrib information corresponding to @id
1541 *
1542 * return value
1543 * -EINVAL NULL buffer passed or unsupported attribute @id.
1544 * -EPERM Identify data not valid, SMART not supported or not enabled
1545 */
1546static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1547 struct smart_attr *attrib)
1548{
1549 int rv, i;
1550 struct smart_attr *pattr;
1551
1552 if (!attrib)
1553 return -EINVAL;
1554
1555 if (!port->identify_valid) {
1556 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1557 return -EPERM;
1558 }
1559 if (!(port->identify[82] & 0x1)) {
1560 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1561 return -EPERM;
1562 }
1563 if (!(port->identify[85] & 0x1)) {
1564 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1565 return -EPERM;
1566 }
1567
1568 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1569 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1570 if (rv) {
1571 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1572 return rv;
1573 }
1574
1575 pattr = (struct smart_attr *)(port->smart_buf + 2);
1576 for (i = 0; i < 29; i++, pattr++)
1577 if (pattr->attr_id == id) {
1578 memcpy(attrib, pattr, sizeof(struct smart_attr));
1579 break;
1580 }
1581
1582 if (i == 29) {
1583 dev_warn(&port->dd->pdev->dev,
1584 "Query for invalid SMART attribute ID\n");
1585 rv = -EINVAL;
1586 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001587
Sam Bradshaw88523a62011-08-30 08:34:26 -06001588 return rv;
1589}
1590
1591/*
1592 * Get the drive capacity.
1593 *
1594 * @dd Pointer to the device data structure.
1595 * @sectors Pointer to the variable that will receive the sector count.
1596 *
1597 * return value
1598 * 1 Capacity was returned successfully.
1599 * 0 The identify information is invalid.
1600 */
Jens Axboe63166682011-09-27 21:27:43 -06001601static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001602{
1603 struct mtip_port *port = dd->port;
1604 u64 total, raw0, raw1, raw2, raw3;
1605 raw0 = port->identify[100];
1606 raw1 = port->identify[101];
1607 raw2 = port->identify[102];
1608 raw3 = port->identify[103];
1609 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1610 *sectors = total;
1611 return (bool) !!port->identify_valid;
1612}
1613
1614/*
1615 * Reset the HBA.
1616 *
1617 * Resets the HBA by setting the HBA Reset bit in the Global
1618 * HBA Control register. After setting the HBA Reset bit the
1619 * function waits for 1 second before reading the HBA Reset
1620 * bit to make sure it has cleared. If HBA Reset is not clear
1621 * an error is returned. Cannot be used in non-blockable
1622 * context.
1623 *
1624 * @dd Pointer to the driver data structure.
1625 *
1626 * return value
1627 * 0 The reset was successful.
1628 * -1 The HBA Reset bit did not clear.
1629 */
1630static int mtip_hba_reset(struct driver_data *dd)
1631{
1632 mtip_deinit_port(dd->port);
1633
1634 /* Set the reset bit */
1635 writel(HOST_RESET, dd->mmio + HOST_CTL);
1636
1637 /* Flush */
1638 readl(dd->mmio + HOST_CTL);
1639
1640 /* Wait for reset to clear */
1641 ssleep(1);
1642
1643 /* Check the bit has cleared */
1644 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1645 dev_err(&dd->pdev->dev,
1646 "Reset bit did not clear.\n");
1647 return -1;
1648 }
1649
1650 return 0;
1651}
1652
1653/*
1654 * Display the identify command data.
1655 *
1656 * @port Pointer to the port data structure.
1657 *
1658 * return value
1659 * None
1660 */
1661static void mtip_dump_identify(struct mtip_port *port)
1662{
1663 sector_t sectors;
1664 unsigned short revid;
1665 char cbuf[42];
1666
1667 if (!port->identify_valid)
1668 return;
1669
1670 strlcpy(cbuf, (char *)(port->identify+10), 21);
1671 dev_info(&port->dd->pdev->dev,
1672 "Serial No.: %s\n", cbuf);
1673
1674 strlcpy(cbuf, (char *)(port->identify+23), 9);
1675 dev_info(&port->dd->pdev->dev,
1676 "Firmware Ver.: %s\n", cbuf);
1677
1678 strlcpy(cbuf, (char *)(port->identify+27), 41);
1679 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1680
1681 if (mtip_hw_get_capacity(port->dd, &sectors))
1682 dev_info(&port->dd->pdev->dev,
1683 "Capacity: %llu sectors (%llu MB)\n",
1684 (u64)sectors,
1685 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1686
1687 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001688 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001689 case 0x1:
1690 strlcpy(cbuf, "A0", 3);
1691 break;
1692 case 0x3:
1693 strlcpy(cbuf, "A2", 3);
1694 break;
1695 default:
1696 strlcpy(cbuf, "?", 2);
1697 break;
1698 }
1699 dev_info(&port->dd->pdev->dev,
1700 "Card Type: %s\n", cbuf);
1701}
1702
1703/*
1704 * Map the commands scatter list into the command table.
1705 *
1706 * @command Pointer to the command.
1707 * @nents Number of scatter list entries.
1708 *
1709 * return value
1710 * None
1711 */
1712static inline void fill_command_sg(struct driver_data *dd,
1713 struct mtip_cmd *command,
1714 int nents)
1715{
1716 int n;
1717 unsigned int dma_len;
1718 struct mtip_cmd_sg *command_sg;
1719 struct scatterlist *sg = command->sg;
1720
1721 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1722
1723 for (n = 0; n < nents; n++) {
1724 dma_len = sg_dma_len(sg);
1725 if (dma_len > 0x400000)
1726 dev_err(&dd->pdev->dev,
1727 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001728 command_sg->info = __force_bit2int
1729 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1730 command_sg->dba = __force_bit2int
1731 cpu_to_le32(sg_dma_address(sg));
1732 command_sg->dba_upper = __force_bit2int
1733 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001734 command_sg++;
1735 sg++;
1736 }
1737}
1738
1739/*
1740 * @brief Execute a drive command.
1741 *
1742 * return value 0 The command completed successfully.
1743 * return value -1 An error occurred while executing the command.
1744 */
Jens Axboe63166682011-09-27 21:27:43 -06001745static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001746{
1747 struct host_to_dev_fis fis;
1748 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1749
Sam Bradshaw88523a62011-08-30 08:34:26 -06001750 /* Build the FIS. */
1751 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1752 fis.type = 0x27;
1753 fis.opts = 1 << 7;
1754 fis.command = command[0];
1755 fis.features = command[1];
1756 fis.sect_count = command[2];
1757 fis.sector = command[3];
1758 fis.cyl_low = command[4];
1759 fis.cyl_hi = command[5];
1760 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1761
1762
Asai Thambi S P45038362012-04-09 08:35:38 +02001763 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001764 __func__,
1765 command[0],
1766 command[1],
1767 command[2],
1768 command[3],
1769 command[4],
1770 command[5],
1771 command[6]);
1772
1773 /* Execute the command. */
1774 if (mtip_exec_internal_command(port,
1775 &fis,
1776 5,
1777 0,
1778 0,
1779 0,
1780 GFP_KERNEL,
1781 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001782 return -1;
1783 }
1784
1785 command[0] = reply->command; /* Status*/
1786 command[1] = reply->features; /* Error*/
1787 command[4] = reply->cyl_low;
1788 command[5] = reply->cyl_hi;
1789
Asai Thambi S P45038362012-04-09 08:35:38 +02001790 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001791 __func__,
1792 command[0],
1793 command[1],
1794 command[4],
1795 command[5]);
1796
Sam Bradshaw88523a62011-08-30 08:34:26 -06001797 return 0;
1798}
1799
1800/*
1801 * @brief Execute a drive command.
1802 *
1803 * @param port Pointer to the port data structure.
1804 * @param command Pointer to the user specified command parameters.
1805 * @param user_buffer Pointer to the user space buffer where read sector
1806 * data should be copied.
1807 *
1808 * return value 0 The command completed successfully.
1809 * return value -EFAULT An error occurred while copying the completion
1810 * data to the user space buffer.
1811 * return value -1 An error occurred while executing the command.
1812 */
Jens Axboe63166682011-09-27 21:27:43 -06001813static int exec_drive_command(struct mtip_port *port, u8 *command,
1814 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001815{
1816 struct host_to_dev_fis fis;
1817 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1818
Sam Bradshaw88523a62011-08-30 08:34:26 -06001819 /* Build the FIS. */
1820 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1821 fis.type = 0x27;
1822 fis.opts = 1 << 7;
1823 fis.command = command[0];
1824 fis.features = command[2];
1825 fis.sect_count = command[3];
1826 if (fis.command == ATA_CMD_SMART) {
1827 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001828 fis.cyl_low = 0x4F;
1829 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001830 }
1831
1832 dbg_printk(MTIP_DRV_NAME
1833 "%s: User Command: cmd %x, sect %x, "
1834 "feat %x, sectcnt %x\n",
1835 __func__,
1836 command[0],
1837 command[1],
1838 command[2],
1839 command[3]);
1840
1841 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1842
1843 /* Execute the command. */
1844 if (mtip_exec_internal_command(port,
1845 &fis,
1846 5,
1847 port->sector_buffer_dma,
1848 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1849 0,
1850 GFP_KERNEL,
1851 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1852 < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001853 return -1;
1854 }
1855
1856 /* Collect the completion status. */
1857 command[0] = reply->command; /* Status*/
1858 command[1] = reply->features; /* Error*/
1859 command[2] = command[3];
1860
1861 dbg_printk(MTIP_DRV_NAME
1862 "%s: Completion Status: stat %x, "
1863 "err %x, cmd %x\n",
1864 __func__,
1865 command[0],
1866 command[1],
1867 command[2]);
1868
1869 if (user_buffer && command[3]) {
1870 if (copy_to_user(user_buffer,
1871 port->sector_buffer,
1872 ATA_SECT_SIZE * command[3])) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001873 return -EFAULT;
1874 }
1875 }
1876
Sam Bradshaw88523a62011-08-30 08:34:26 -06001877 return 0;
1878}
1879
1880/*
1881 * Indicates whether a command has a single sector payload.
1882 *
1883 * @command passed to the device to perform the certain event.
1884 * @features passed to the device to perform the certain event.
1885 *
1886 * return value
1887 * 1 command is one that always has a single sector payload,
1888 * regardless of the value in the Sector Count field.
1889 * 0 otherwise
1890 *
1891 */
1892static unsigned int implicit_sector(unsigned char command,
1893 unsigned char features)
1894{
1895 unsigned int rv = 0;
1896
1897 /* list of commands that have an implicit sector count of 1 */
1898 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001899 case ATA_CMD_SEC_SET_PASS:
1900 case ATA_CMD_SEC_UNLOCK:
1901 case ATA_CMD_SEC_ERASE_PREP:
1902 case ATA_CMD_SEC_ERASE_UNIT:
1903 case ATA_CMD_SEC_FREEZE_LOCK:
1904 case ATA_CMD_SEC_DISABLE_PASS:
1905 case ATA_CMD_PMP_READ:
1906 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001907 rv = 1;
1908 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001909 case ATA_CMD_SET_MAX:
1910 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001911 rv = 1;
1912 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001913 case ATA_CMD_SMART:
1914 if ((features == ATA_SMART_READ_VALUES) ||
1915 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001916 rv = 1;
1917 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001918 case ATA_CMD_CONF_OVERLAY:
1919 if ((features == ATA_DCO_IDENTIFY) ||
1920 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001921 rv = 1;
1922 break;
1923 }
1924 return rv;
1925}
1926
1927/*
1928 * Executes a taskfile
1929 * See ide_taskfile_ioctl() for derivation
1930 */
1931static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001932 void __user *buf,
1933 ide_task_request_t *req_task,
1934 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001935{
1936 struct host_to_dev_fis fis;
1937 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001938 u8 *outbuf = NULL;
1939 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001940 dma_addr_t outbuf_dma = 0;
1941 dma_addr_t inbuf_dma = 0;
1942 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001944 unsigned int taskin = 0;
1945 unsigned int taskout = 0;
1946 u8 nsect = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001947 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1948 unsigned int force_single_sector;
1949 unsigned int transfer_size;
1950 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001951 int intotal = outtotal + req_task->out_size;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001952
1953 taskout = req_task->out_size;
1954 taskin = req_task->in_size;
1955 /* 130560 = 512 * 0xFF*/
1956 if (taskin > 130560 || taskout > 130560) {
1957 err = -EINVAL;
1958 goto abort;
1959 }
1960
1961 if (taskout) {
1962 outbuf = kzalloc(taskout, GFP_KERNEL);
1963 if (outbuf == NULL) {
1964 err = -ENOMEM;
1965 goto abort;
1966 }
1967 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1968 err = -EFAULT;
1969 goto abort;
1970 }
1971 outbuf_dma = pci_map_single(dd->pdev,
1972 outbuf,
1973 taskout,
1974 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001975 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001976 err = -ENOMEM;
1977 goto abort;
1978 }
1979 dma_buffer = outbuf_dma;
1980 }
1981
1982 if (taskin) {
1983 inbuf = kzalloc(taskin, GFP_KERNEL);
1984 if (inbuf == NULL) {
1985 err = -ENOMEM;
1986 goto abort;
1987 }
1988
1989 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1990 err = -EFAULT;
1991 goto abort;
1992 }
1993 inbuf_dma = pci_map_single(dd->pdev,
1994 inbuf,
1995 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06001996 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001997 err = -ENOMEM;
1998 goto abort;
1999 }
2000 dma_buffer = inbuf_dma;
2001 }
2002
2003 /* only supports PIO and non-data commands from this ioctl. */
2004 switch (req_task->data_phase) {
2005 case TASKFILE_OUT:
2006 nsect = taskout / ATA_SECT_SIZE;
2007 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2008 break;
2009 case TASKFILE_IN:
2010 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2011 break;
2012 case TASKFILE_NO_DATA:
2013 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2014 break;
2015 default:
2016 err = -EINVAL;
2017 goto abort;
2018 }
2019
Sam Bradshaw88523a62011-08-30 08:34:26 -06002020 /* Build the FIS. */
2021 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2022
2023 fis.type = 0x27;
2024 fis.opts = 1 << 7;
2025 fis.command = req_task->io_ports[7];
2026 fis.features = req_task->io_ports[1];
2027 fis.sect_count = req_task->io_ports[2];
2028 fis.lba_low = req_task->io_ports[3];
2029 fis.lba_mid = req_task->io_ports[4];
2030 fis.lba_hi = req_task->io_ports[5];
2031 /* Clear the dev bit*/
2032 fis.device = req_task->io_ports[6] & ~0x10;
2033
2034 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2035 req_task->in_flags.all =
2036 IDE_TASKFILE_STD_IN_FLAGS |
2037 (IDE_HOB_STD_IN_FLAGS << 8);
2038 fis.lba_low_ex = req_task->hob_ports[3];
2039 fis.lba_mid_ex = req_task->hob_ports[4];
2040 fis.lba_hi_ex = req_task->hob_ports[5];
2041 fis.features_ex = req_task->hob_ports[1];
2042 fis.sect_cnt_ex = req_task->hob_ports[2];
2043
2044 } else {
2045 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2046 }
2047
2048 force_single_sector = implicit_sector(fis.command, fis.features);
2049
2050 if ((taskin || taskout) && (!fis.sect_count)) {
2051 if (nsect)
2052 fis.sect_count = nsect;
2053 else {
2054 if (!force_single_sector) {
2055 dev_warn(&dd->pdev->dev,
2056 "data movement but "
2057 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002058 err = -EINVAL;
2059 goto abort;
2060 }
2061 }
2062 }
2063
2064 dbg_printk(MTIP_DRV_NAME
2065 "taskfile: cmd %x, feat %x, nsect %x,"
2066 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2067 " head/dev %x\n",
2068 fis.command,
2069 fis.features,
2070 fis.sect_count,
2071 fis.lba_low,
2072 fis.lba_mid,
2073 fis.lba_hi,
2074 fis.device);
2075
2076 switch (fis.command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002077 case ATA_CMD_DOWNLOAD_MICRO:
2078 /* Change timeout for Download Microcode to 60 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002079 timeout = 60000;
2080 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002081 case ATA_CMD_SEC_ERASE_UNIT:
2082 /* Change timeout for Security Erase Unit to 4 minutes.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002083 timeout = 240000;
2084 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002085 case ATA_CMD_STANDBYNOW1:
2086 /* Change timeout for standby immediate to 10 seconds.*/
Sam Bradshaw88523a62011-08-30 08:34:26 -06002087 timeout = 10000;
2088 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002089 case 0xF7:
2090 case 0xFA:
2091 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002092 timeout = 10000;
2093 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002094 case ATA_CMD_SMART:
2095 /* Change timeout for vendor unique command to 10 secs */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002096 timeout = 10000;
2097 break;
2098 default:
2099 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2100 break;
2101 }
2102
2103 /* Determine the correct transfer size.*/
2104 if (force_single_sector)
2105 transfer_size = ATA_SECT_SIZE;
2106 else
2107 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2108
2109 /* Execute the command.*/
2110 if (mtip_exec_internal_command(dd->port,
2111 &fis,
2112 5,
2113 dma_buffer,
2114 transfer_size,
2115 0,
2116 GFP_KERNEL,
2117 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002118 err = -EIO;
2119 goto abort;
2120 }
2121
2122 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2123
2124 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2125 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2126 req_task->io_ports[7] = reply->control;
2127 } else {
2128 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2129 req_task->io_ports[7] = reply->command;
2130 }
2131
2132 /* reclaim the DMA buffers.*/
2133 if (inbuf_dma)
2134 pci_unmap_single(dd->pdev, inbuf_dma,
2135 taskin, DMA_FROM_DEVICE);
2136 if (outbuf_dma)
2137 pci_unmap_single(dd->pdev, outbuf_dma,
2138 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002139 inbuf_dma = 0;
2140 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002141
2142 /* return the ATA registers to the caller.*/
2143 req_task->io_ports[1] = reply->features;
2144 req_task->io_ports[2] = reply->sect_count;
2145 req_task->io_ports[3] = reply->lba_low;
2146 req_task->io_ports[4] = reply->lba_mid;
2147 req_task->io_ports[5] = reply->lba_hi;
2148 req_task->io_ports[6] = reply->device;
2149
2150 if (req_task->out_flags.all & 1) {
2151
2152 req_task->hob_ports[3] = reply->lba_low_ex;
2153 req_task->hob_ports[4] = reply->lba_mid_ex;
2154 req_task->hob_ports[5] = reply->lba_hi_ex;
2155 req_task->hob_ports[1] = reply->features_ex;
2156 req_task->hob_ports[2] = reply->sect_cnt_ex;
2157 }
2158
2159 /* Com rest after secure erase or lowlevel format */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002160 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
Sam Bradshaw88523a62011-08-30 08:34:26 -06002161 ((fis.command == 0xFC) &&
2162 (fis.features == 0x27 || fis.features == 0x72 ||
2163 fis.features == 0x62 || fis.features == 0x26))) &&
2164 !(reply->command & 1)) {
2165 mtip_restart_port(dd->port);
2166 }
2167
2168 dbg_printk(MTIP_DRV_NAME
2169 "%s: Completion: stat %x,"
2170 "err %x, sect_cnt %x, lbalo %x,"
2171 "lbamid %x, lbahi %x, dev %x\n",
2172 __func__,
2173 req_task->io_ports[7],
2174 req_task->io_ports[1],
2175 req_task->io_ports[2],
2176 req_task->io_ports[3],
2177 req_task->io_ports[4],
2178 req_task->io_ports[5],
2179 req_task->io_ports[6]);
2180
Sam Bradshaw88523a62011-08-30 08:34:26 -06002181 if (taskout) {
2182 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2183 err = -EFAULT;
2184 goto abort;
2185 }
2186 }
2187 if (taskin) {
2188 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2189 err = -EFAULT;
2190 goto abort;
2191 }
2192 }
2193abort:
2194 if (inbuf_dma)
2195 pci_unmap_single(dd->pdev, inbuf_dma,
2196 taskin, DMA_FROM_DEVICE);
2197 if (outbuf_dma)
2198 pci_unmap_single(dd->pdev, outbuf_dma,
2199 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002200 kfree(outbuf);
2201 kfree(inbuf);
2202
2203 return err;
2204}
2205
2206/*
2207 * Handle IOCTL calls from the Block Layer.
2208 *
2209 * This function is called by the Block Layer when it receives an IOCTL
2210 * command that it does not understand. If the IOCTL command is not supported
2211 * this function returns -ENOTTY.
2212 *
2213 * @dd Pointer to the driver data structure.
2214 * @cmd IOCTL command passed from the Block Layer.
2215 * @arg IOCTL argument passed from the Block Layer.
2216 *
2217 * return value
2218 * 0 The IOCTL completed successfully.
2219 * -ENOTTY The specified command is not supported.
2220 * -EFAULT An error occurred copying data to a user space buffer.
2221 * -EIO An error occurred while executing the command.
2222 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002223static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2224 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002225{
2226 switch (cmd) {
2227 case HDIO_GET_IDENTITY:
2228 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
2229 dev_warn(&dd->pdev->dev,
2230 "Unable to read identity\n");
2231 return -EIO;
2232 }
2233
2234 break;
2235 case HDIO_DRIVE_CMD:
2236 {
2237 u8 drive_command[4];
2238
2239 /* Copy the user command info to our buffer. */
2240 if (copy_from_user(drive_command,
2241 (void __user *) arg,
2242 sizeof(drive_command)))
2243 return -EFAULT;
2244
2245 /* Execute the drive command. */
2246 if (exec_drive_command(dd->port,
2247 drive_command,
2248 (void __user *) (arg+4)))
2249 return -EIO;
2250
2251 /* Copy the status back to the users buffer. */
2252 if (copy_to_user((void __user *) arg,
2253 drive_command,
2254 sizeof(drive_command)))
2255 return -EFAULT;
2256
2257 break;
2258 }
2259 case HDIO_DRIVE_TASK:
2260 {
2261 u8 drive_command[7];
2262
2263 /* Copy the user command info to our buffer. */
2264 if (copy_from_user(drive_command,
2265 (void __user *) arg,
2266 sizeof(drive_command)))
2267 return -EFAULT;
2268
2269 /* Execute the drive command. */
2270 if (exec_drive_task(dd->port, drive_command))
2271 return -EIO;
2272
2273 /* Copy the status back to the users buffer. */
2274 if (copy_to_user((void __user *) arg,
2275 drive_command,
2276 sizeof(drive_command)))
2277 return -EFAULT;
2278
2279 break;
2280 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002281 case HDIO_DRIVE_TASKFILE: {
2282 ide_task_request_t req_task;
2283 int ret, outtotal;
2284
2285 if (copy_from_user(&req_task, (void __user *) arg,
2286 sizeof(req_task)))
2287 return -EFAULT;
2288
2289 outtotal = sizeof(req_task);
2290
2291 ret = exec_drive_taskfile(dd, (void __user *) arg,
2292 &req_task, outtotal);
2293
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002294 if (copy_to_user((void __user *) arg, &req_task,
2295 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002296 return -EFAULT;
2297
2298 return ret;
2299 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002300
2301 default:
2302 return -EINVAL;
2303 }
2304 return 0;
2305}
2306
2307/*
2308 * Submit an IO to the hw
2309 *
2310 * This function is called by the block layer to issue an io
2311 * to the device. Upon completion, the callback function will
2312 * be called with the data parameter passed as the callback data.
2313 *
2314 * @dd Pointer to the driver data structure.
2315 * @start First sector to read.
2316 * @nsect Number of sectors to read.
2317 * @nents Number of entries in scatter list for the read command.
2318 * @tag The tag of this read command.
2319 * @callback Pointer to the function that should be called
2320 * when the read completes.
2321 * @data Callback data passed to the callback function
2322 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002323 * @dir Direction (read or write)
2324 *
2325 * return value
2326 * None
2327 */
Jens Axboe63166682011-09-27 21:27:43 -06002328static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2329 int nsect, int nents, int tag, void *callback,
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01002330 void *data, int dir)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002331{
2332 struct host_to_dev_fis *fis;
2333 struct mtip_port *port = dd->port;
2334 struct mtip_cmd *command = &port->commands[tag];
Asai Thambi S P45038362012-04-09 08:35:38 +02002335 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002336
2337 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002338 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002339
2340 command->scatter_ents = nents;
2341
2342 /*
2343 * The number of retries for this command before it is
2344 * reported as a failure to the upper layers.
2345 */
2346 command->retries = MTIP_MAX_RETRIES;
2347
2348 /* Fill out fis */
2349 fis = command->command;
2350 fis->type = 0x27;
2351 fis->opts = 1 << 7;
2352 fis->command =
2353 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002354 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2355 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002356 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002357 fis->features = nsect & 0xFF;
2358 fis->features_ex = (nsect >> 8) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002359 fis->sect_count = ((tag << 3) | (tag >> 5));
2360 fis->sect_cnt_ex = 0;
2361 fis->control = 0;
2362 fis->res2 = 0;
2363 fis->res3 = 0;
2364 fill_command_sg(dd, command, nents);
2365
2366 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002367 command->command_header->opts =
2368 __force_bit2int cpu_to_le32(
2369 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002370 command->command_header->byte_count = 0;
2371
2372 /*
2373 * Set the completion function and data for the command
2374 * within this layer.
2375 */
2376 command->comp_data = dd;
2377 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002378 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002379
2380 /*
2381 * Set the completion function and data for the command passed
2382 * from the upper layer.
2383 */
2384 command->async_data = data;
2385 command->async_callback = callback;
2386
2387 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002388 * To prevent this command from being issued
2389 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002390 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002391 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2392 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2393 set_bit(tag, port->cmds_to_issue);
2394 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2395 return;
2396 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002397
2398 /* Issue the command to the hardware */
2399 mtip_issue_ncq_command(port, tag);
2400
Asai Thambi S Pdad40f12012-04-09 08:35:38 +02002401 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002402}
2403
2404/*
2405 * Release a command slot.
2406 *
2407 * @dd Pointer to the driver data structure.
2408 * @tag Slot tag
2409 *
2410 * return value
2411 * None
2412 */
Jens Axboe63166682011-09-27 21:27:43 -06002413static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002414{
2415 release_slot(dd->port, tag);
2416}
2417
2418/*
2419 * Obtain a command slot and return its associated scatter list.
2420 *
2421 * @dd Pointer to the driver data structure.
2422 * @tag Pointer to an int that will receive the allocated command
2423 * slot tag.
2424 *
2425 * return value
2426 * Pointer to the scatter list for the allocated command slot
2427 * or NULL if no command slots are available.
2428 */
Jens Axboe63166682011-09-27 21:27:43 -06002429static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2430 int *tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431{
2432 /*
2433 * It is possible that, even with this semaphore, a thread
2434 * may think that no command slots are available. Therefore, we
2435 * need to make an attempt to get_slot().
2436 */
2437 down(&dd->port->cmd_slot);
2438 *tag = get_slot(dd->port);
2439
Asai Thambi S P45038362012-04-09 08:35:38 +02002440 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2441 up(&dd->port->cmd_slot);
2442 return NULL;
2443 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002444 if (unlikely(*tag < 0))
2445 return NULL;
2446
2447 return dd->port->commands[*tag].sg;
2448}
2449
2450/*
2451 * Sysfs register/status dump.
2452 *
2453 * @dev Pointer to the device structure, passed by the kernrel.
2454 * @attr Pointer to the device_attribute structure passed by the kernel.
2455 * @buf Pointer to the char buffer that will receive the stats info.
2456 *
2457 * return value
2458 * The size, in bytes, of the data copied into buf.
2459 */
Asai Thambi S P45038362012-04-09 08:35:38 +02002460static ssize_t mtip_hw_show_registers(struct device *dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002461 struct device_attribute *attr,
2462 char *buf)
2463{
2464 u32 group_allocated;
2465 struct driver_data *dd = dev_to_disk(dev)->private_data;
2466 int size = 0;
2467 int n;
2468
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002469 size += sprintf(&buf[size], "S ACTive:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002470
2471 for (n = 0; n < dd->slot_groups; n++)
2472 size += sprintf(&buf[size], "0x%08x\n",
2473 readl(dd->port->s_active[n]));
2474
2475 size += sprintf(&buf[size], "Command Issue:\n");
2476
2477 for (n = 0; n < dd->slot_groups; n++)
2478 size += sprintf(&buf[size], "0x%08x\n",
2479 readl(dd->port->cmd_issue[n]));
2480
2481 size += sprintf(&buf[size], "Allocated:\n");
2482
2483 for (n = 0; n < dd->slot_groups; n++) {
2484 if (sizeof(long) > sizeof(u32))
2485 group_allocated =
2486 dd->port->allocated[n/2] >> (32*(n&1));
2487 else
2488 group_allocated = dd->port->allocated[n];
2489 size += sprintf(&buf[size], "0x%08x\n",
2490 group_allocated);
2491 }
2492
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002493 size += sprintf(&buf[size], "Completed:\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002494
2495 for (n = 0; n < dd->slot_groups; n++)
2496 size += sprintf(&buf[size], "0x%08x\n",
2497 readl(dd->port->completed[n]));
2498
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002499 size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002500 readl(dd->port->mmio + PORT_IRQ_STAT));
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002501 size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06002502 readl(dd->mmio + HOST_IRQ_STAT));
2503
2504 return size;
2505}
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002506
2507static ssize_t mtip_hw_show_status(struct device *dev,
2508 struct device_attribute *attr,
2509 char *buf)
2510{
2511 struct driver_data *dd = dev_to_disk(dev)->private_data;
2512 int size = 0;
2513
2514 if (test_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag))
2515 size += sprintf(buf, "%s", "thermal_shutdown\n");
2516 else if (test_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag))
2517 size += sprintf(buf, "%s", "write_protect\n");
2518 else
2519 size += sprintf(buf, "%s", "online\n");
2520
2521 return size;
2522}
2523
Asai Thambi S P45038362012-04-09 08:35:38 +02002524static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002525static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002526
2527/*
2528 * Create the sysfs related attributes.
2529 *
2530 * @dd Pointer to the driver data structure.
2531 * @kobj Pointer to the kobj for the block device.
2532 *
2533 * return value
2534 * 0 Operation completed successfully.
2535 * -EINVAL Invalid parameter.
2536 */
Jens Axboe63166682011-09-27 21:27:43 -06002537static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002538{
2539 if (!kobj || !dd)
2540 return -EINVAL;
2541
2542 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2543 dev_warn(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002544 "Error creating 'registers' sysfs entry\n");
2545 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2546 dev_warn(&dd->pdev->dev,
2547 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002548 return 0;
2549}
2550
2551/*
2552 * Remove the sysfs related attributes.
2553 *
2554 * @dd Pointer to the driver data structure.
2555 * @kobj Pointer to the kobj for the block device.
2556 *
2557 * return value
2558 * 0 Operation completed successfully.
2559 * -EINVAL Invalid parameter.
2560 */
Jens Axboe63166682011-09-27 21:27:43 -06002561static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002562{
2563 if (!kobj || !dd)
2564 return -EINVAL;
2565
2566 sysfs_remove_file(kobj, &dev_attr_registers.attr);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002567 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002568
2569 return 0;
2570}
2571
2572/*
2573 * Perform any init/resume time hardware setup
2574 *
2575 * @dd Pointer to the driver data structure.
2576 *
2577 * return value
2578 * None
2579 */
2580static inline void hba_setup(struct driver_data *dd)
2581{
2582 u32 hwdata;
2583 hwdata = readl(dd->mmio + HOST_HSORG);
2584
2585 /* interrupt bug workaround: use only 1 IS bit.*/
2586 writel(hwdata |
2587 HSORG_DISABLE_SLOTGRP_INTR |
2588 HSORG_DISABLE_SLOTGRP_PXIS,
2589 dd->mmio + HOST_HSORG);
2590}
2591
2592/*
2593 * Detect the details of the product, and store anything needed
2594 * into the driver data structure. This includes product type and
2595 * version and number of slot groups.
2596 *
2597 * @dd Pointer to the driver data structure.
2598 *
2599 * return value
2600 * None
2601 */
2602static void mtip_detect_product(struct driver_data *dd)
2603{
2604 u32 hwdata;
2605 unsigned int rev, slotgroups;
2606
2607 /*
2608 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2609 * info register:
2610 * [15:8] hardware/software interface rev#
2611 * [ 3] asic-style interface
2612 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2613 */
2614 hwdata = readl(dd->mmio + HOST_HSORG);
2615
2616 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2617 dd->slot_groups = 1;
2618
2619 if (hwdata & 0x8) {
2620 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2621 rev = (hwdata & HSORG_HWREV) >> 8;
2622 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2623 dev_info(&dd->pdev->dev,
2624 "ASIC-FPGA design, HS rev 0x%x, "
2625 "%i slot groups [%i slots]\n",
2626 rev,
2627 slotgroups,
2628 slotgroups * 32);
2629
2630 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2631 dev_warn(&dd->pdev->dev,
2632 "Warning: driver only supports "
2633 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2634 slotgroups = MTIP_MAX_SLOT_GROUPS;
2635 }
2636 dd->slot_groups = slotgroups;
2637 return;
2638 }
2639
2640 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2641}
2642
2643/*
2644 * Blocking wait for FTL rebuild to complete
2645 *
2646 * @dd Pointer to the DRIVER_DATA structure.
2647 *
2648 * return value
2649 * 0 FTL rebuild completed successfully
2650 * -EFAULT FTL rebuild error/timeout/interruption
2651 */
2652static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2653{
2654 unsigned long timeout, cnt = 0, start;
2655
2656 dev_warn(&dd->pdev->dev,
2657 "FTL rebuild in progress. Polling for completion.\n");
2658
2659 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002660 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2661
2662 do {
Asai Thambi S P45038362012-04-09 08:35:38 +02002663 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2664 &dd->dd_flag)))
2665 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002666 if (mtip_check_surprise_removal(dd->pdev))
2667 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002668
Sam Bradshaw88523a62011-08-30 08:34:26 -06002669 if (mtip_get_identify(dd->port, NULL) < 0)
2670 return -EFAULT;
2671
2672 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2673 MTIP_FTL_REBUILD_MAGIC) {
2674 ssleep(1);
2675 /* Print message every 3 minutes */
2676 if (cnt++ >= 180) {
2677 dev_warn(&dd->pdev->dev,
2678 "FTL rebuild in progress (%d secs).\n",
2679 jiffies_to_msecs(jiffies - start) / 1000);
2680 cnt = 0;
2681 }
2682 } else {
2683 dev_warn(&dd->pdev->dev,
2684 "FTL rebuild complete (%d secs).\n",
2685 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002686 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002687 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002688 }
2689 ssleep(10);
2690 } while (time_before(jiffies, timeout));
2691
2692 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002693 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002694 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2695 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002696 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002697}
2698
2699/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002700 * service thread to issue queued commands
2701 *
2702 * @data Pointer to the driver data structure.
2703 *
2704 * return value
2705 * 0
2706 */
2707
2708static int mtip_service_thread(void *data)
2709{
2710 struct driver_data *dd = (struct driver_data *)data;
2711 unsigned long slot, slot_start, slot_wrap;
2712 unsigned int num_cmd_slots = dd->slot_groups * 32;
2713 struct mtip_port *port = dd->port;
2714
2715 while (1) {
2716 /*
2717 * the condition is to check neither an internal command is
2718 * is in progress nor error handling is active
2719 */
2720 wait_event_interruptible(port->svc_wait, (port->flags) &&
2721 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2722 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2723
2724 if (kthread_should_stop())
2725 break;
2726
Asai Thambi S P45038362012-04-09 08:35:38 +02002727 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
2728 &dd->dd_flag)))
2729 break;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002730 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002731 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002732 slot = 1;
2733 /* used to restrict the loop to one iteration */
2734 slot_start = num_cmd_slots;
2735 slot_wrap = 0;
2736 while (1) {
2737 slot = find_next_bit(port->cmds_to_issue,
2738 num_cmd_slots, slot);
2739 if (slot_wrap == 1) {
2740 if ((slot_start >= slot) ||
2741 (slot >= num_cmd_slots))
2742 break;
2743 }
2744 if (unlikely(slot_start == num_cmd_slots))
2745 slot_start = slot;
2746
2747 if (unlikely(slot == num_cmd_slots)) {
2748 slot = 1;
2749 slot_wrap = 1;
2750 continue;
2751 }
2752
2753 /* Issue the command to the hardware */
2754 mtip_issue_ncq_command(port, slot);
2755
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002756 clear_bit(slot, port->cmds_to_issue);
2757 }
2758
2759 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002760 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
2761 mtip_ftl_rebuild_poll(dd);
2762 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002763 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002764 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2765
2766 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2767 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002768 }
2769 return 0;
2770}
2771
2772/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002773 * Called once for each card.
2774 *
2775 * @dd Pointer to the driver data structure.
2776 *
2777 * return value
2778 * 0 on success, else an error code.
2779 */
Jens Axboe63166682011-09-27 21:27:43 -06002780static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002781{
2782 int i;
2783 int rv;
2784 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02002785 unsigned long timeout, timetaken;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002786 unsigned char *buf;
2787 struct smart_attr attr242;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002788
2789 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2790
2791 mtip_detect_product(dd);
2792 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2793 rv = -EIO;
2794 goto out1;
2795 }
2796 num_command_slots = dd->slot_groups * 32;
2797
2798 hba_setup(dd);
2799
Sam Bradshaw88523a62011-08-30 08:34:26 -06002800 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2801
2802 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2803 if (!dd->port) {
2804 dev_err(&dd->pdev->dev,
2805 "Memory allocation: port structure\n");
2806 return -ENOMEM;
2807 }
2808
2809 /* Counting semaphore to track command slot usage */
2810 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2811
2812 /* Spinlock to prevent concurrent issue */
2813 spin_lock_init(&dd->port->cmd_issue_lock);
2814
2815 /* Set the port mmio base address. */
2816 dd->port->mmio = dd->mmio + PORT_OFFSET;
2817 dd->port->dd = dd;
2818
2819 /* Allocate memory for the command list. */
2820 dd->port->command_list =
2821 dmam_alloc_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002822 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06002823 &dd->port->command_list_dma,
2824 GFP_KERNEL);
2825 if (!dd->port->command_list) {
2826 dev_err(&dd->pdev->dev,
2827 "Memory allocation: command list\n");
2828 rv = -ENOMEM;
2829 goto out1;
2830 }
2831
2832 /* Clear the memory we have allocated. */
2833 memset(dd->port->command_list,
2834 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002835 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002836
2837 /* Setup the addresse of the RX FIS. */
2838 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2839 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2840
2841 /* Setup the address of the command tables. */
2842 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2843 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2844
2845 /* Setup the address of the identify data. */
2846 dd->port->identify = dd->port->command_table +
2847 HW_CMD_TBL_AR_SZ;
2848 dd->port->identify_dma = dd->port->command_tbl_dma +
2849 HW_CMD_TBL_AR_SZ;
2850
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002851 /* Setup the address of the sector buffer - for some non-ncq cmds */
Sam Bradshaw88523a62011-08-30 08:34:26 -06002852 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2853 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2854
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002855 /* Setup the address of the log buf - for read log command */
2856 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
2857 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
2858
2859 /* Setup the address of the smart buf - for smart read data command */
2860 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
2861 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
2862
2863
Sam Bradshaw88523a62011-08-30 08:34:26 -06002864 /* Point the command headers at the command tables. */
2865 for (i = 0; i < num_command_slots; i++) {
2866 dd->port->commands[i].command_header =
2867 dd->port->command_list +
2868 (sizeof(struct mtip_cmd_hdr) * i);
2869 dd->port->commands[i].command_header_dma =
2870 dd->port->command_list_dma +
2871 (sizeof(struct mtip_cmd_hdr) * i);
2872
2873 dd->port->commands[i].command =
2874 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2875 dd->port->commands[i].command_dma =
2876 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2877
2878 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2879 dd->port->commands[i].command_header->ctbau =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002880 __force_bit2int cpu_to_le32(
Sam Bradshaw88523a62011-08-30 08:34:26 -06002881 (dd->port->commands[i].command_dma >> 16) >> 16);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002882 dd->port->commands[i].command_header->ctba =
2883 __force_bit2int cpu_to_le32(
2884 dd->port->commands[i].command_dma & 0xFFFFFFFF);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002885
2886 /*
2887 * If this is not done, a bug is reported by the stock
2888 * FC11 i386. Due to the fact that it has lots of kernel
2889 * debugging enabled.
2890 */
2891 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2892
2893 /* Mark all commands as currently inactive.*/
2894 atomic_set(&dd->port->commands[i].active, 0);
2895 }
2896
2897 /* Setup the pointers to the extended s_active and CI registers. */
2898 for (i = 0; i < dd->slot_groups; i++) {
2899 dd->port->s_active[i] =
2900 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2901 dd->port->cmd_issue[i] =
2902 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2903 dd->port->completed[i] =
2904 dd->port->mmio + i*0x80 + PORT_SDBV;
2905 }
2906
Asai Thambi S P45038362012-04-09 08:35:38 +02002907 timetaken = jiffies;
2908 timeout = jiffies + msecs_to_jiffies(30000);
2909 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2910 time_before(jiffies, timeout)) {
2911 mdelay(100);
2912 }
2913 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
2914 timetaken = jiffies - timetaken;
2915 dev_warn(&dd->pdev->dev,
2916 "Surprise removal detected at %u ms\n",
2917 jiffies_to_msecs(timetaken));
2918 rv = -ENODEV;
2919 goto out2 ;
2920 }
2921 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2922 timetaken = jiffies - timetaken;
2923 dev_warn(&dd->pdev->dev,
2924 "Removal detected at %u ms\n",
2925 jiffies_to_msecs(timetaken));
2926 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002927 goto out2;
2928 }
2929
Asai Thambi S P45038362012-04-09 08:35:38 +02002930 /* Conditionally reset the HBA. */
2931 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
2932 if (mtip_hba_reset(dd) < 0) {
2933 dev_err(&dd->pdev->dev,
2934 "Card did not reset within timeout\n");
2935 rv = -EIO;
2936 goto out2;
2937 }
2938 } else {
2939 /* Clear any pending interrupts on the HBA */
2940 writel(readl(dd->mmio + HOST_IRQ_STAT),
2941 dd->mmio + HOST_IRQ_STAT);
2942 }
2943
Sam Bradshaw88523a62011-08-30 08:34:26 -06002944 mtip_init_port(dd->port);
2945 mtip_start_port(dd->port);
2946
2947 /* Setup the ISR and enable interrupts. */
2948 rv = devm_request_irq(&dd->pdev->dev,
2949 dd->pdev->irq,
2950 mtip_irq_handler,
2951 IRQF_SHARED,
2952 dev_driver_string(&dd->pdev->dev),
2953 dd);
2954
2955 if (rv) {
2956 dev_err(&dd->pdev->dev,
2957 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2958 goto out2;
2959 }
2960
2961 /* Enable interrupts on the HBA. */
2962 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2963 dd->mmio + HOST_CTL);
2964
2965 init_timer(&dd->port->cmd_timer);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002966 init_waitqueue_head(&dd->port->svc_wait);
2967
Sam Bradshaw88523a62011-08-30 08:34:26 -06002968 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2969 dd->port->cmd_timer.function = mtip_timeout_function;
2970 mod_timer(&dd->port->cmd_timer,
2971 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2972
Asai Thambi S P45038362012-04-09 08:35:38 +02002973
2974 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
2975 rv = -EFAULT;
2976 goto out3;
2977 }
2978
Sam Bradshaw88523a62011-08-30 08:34:26 -06002979 if (mtip_get_identify(dd->port, NULL) < 0) {
2980 rv = -EFAULT;
2981 goto out3;
2982 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002983
2984 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2985 MTIP_FTL_REBUILD_MAGIC) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002986 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2987 return MTIP_FTL_REBUILD_MAGIC;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002988 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002989 mtip_dump_identify(dd->port);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002990
2991 /* check write protect, over temp and rebuild statuses */
2992 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
2993 dd->port->log_buf,
2994 dd->port->log_buf_dma, 1);
2995 if (rv) {
2996 dev_warn(&dd->pdev->dev,
2997 "Error in READ LOG EXT (10h) command\n");
2998 /* non-critical error, don't fail the load */
2999 } else {
3000 buf = (unsigned char *)dd->port->log_buf;
3001 if (buf[259] & 0x1) {
3002 dev_info(&dd->pdev->dev,
3003 "Write protect bit is set.\n");
3004 set_bit(MTIP_DD_FLAG_WRITE_PROTECT_BIT, &dd->dd_flag);
3005 }
3006 if (buf[288] == 0xF7) {
3007 dev_info(&dd->pdev->dev,
3008 "Exceeded Tmax, drive in thermal shutdown.\n");
3009 set_bit(MTIP_DD_FLAG_OVER_TEMP_BIT, &dd->dd_flag);
3010 }
3011 if (buf[288] == 0xBF) {
3012 dev_info(&dd->pdev->dev,
3013 "Drive indicates rebuild has failed.\n");
3014 /* TODO */
3015 }
3016 }
3017
3018 /* get write protect progess */
3019 memset(&attr242, 0, sizeof(struct smart_attr));
3020 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3021 dev_warn(&dd->pdev->dev,
3022 "Unable to check write protect progress\n");
3023 else
3024 dev_info(&dd->pdev->dev,
3025 "Write protect progress: %d%% (%d blocks)\n",
3026 attr242.cur, attr242.data);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003027 return rv;
3028
3029out3:
3030 del_timer_sync(&dd->port->cmd_timer);
3031
3032 /* Disable interrupts on the HBA. */
3033 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3034 dd->mmio + HOST_CTL);
3035
3036 /*Release the IRQ. */
3037 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3038
3039out2:
3040 mtip_deinit_port(dd->port);
3041
3042 /* Free the command/command header memory. */
3043 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003044 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003045 dd->port->command_list,
3046 dd->port->command_list_dma);
3047out1:
3048 /* Free the memory allocated for the for structure. */
3049 kfree(dd->port);
3050
3051 return rv;
3052}
3053
3054/*
3055 * Called to deinitialize an interface.
3056 *
3057 * @dd Pointer to the driver data structure.
3058 *
3059 * return value
3060 * 0
3061 */
Jens Axboe63166682011-09-27 21:27:43 -06003062static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003063{
3064 /*
3065 * Send standby immediate (E0h) to the drive so that it
3066 * saves its state.
3067 */
Asai Thambi S P45038362012-04-09 08:35:38 +02003068 if (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003069
Asai Thambi S P45038362012-04-09 08:35:38 +02003070 if (test_bit(MTIP_FLAG_REBUILD_BIT, &dd->dd_flag))
3071 if (mtip_standby_immediate(dd->port))
3072 dev_warn(&dd->pdev->dev,
3073 "STANDBY IMMEDIATE failed\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003074
3075 /* de-initialize the port. */
3076 mtip_deinit_port(dd->port);
3077
3078 /* Disable interrupts on the HBA. */
3079 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3080 dd->mmio + HOST_CTL);
3081 }
3082
3083 del_timer_sync(&dd->port->cmd_timer);
3084
Sam Bradshaw88523a62011-08-30 08:34:26 -06003085 /* Release the IRQ. */
3086 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3087
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003088 /* Stop the bottom half tasklet. */
3089 tasklet_kill(&dd->tasklet);
3090
Sam Bradshaw88523a62011-08-30 08:34:26 -06003091 /* Free the command/command header memory. */
3092 dmam_free_coherent(&dd->pdev->dev,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003093 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
Sam Bradshaw88523a62011-08-30 08:34:26 -06003094 dd->port->command_list,
3095 dd->port->command_list_dma);
3096 /* Free the memory allocated for the for structure. */
3097 kfree(dd->port);
3098
3099 return 0;
3100}
3101
3102/*
3103 * Issue a Standby Immediate command to the device.
3104 *
3105 * This function is called by the Block Layer just before the
3106 * system powers off during a shutdown.
3107 *
3108 * @dd Pointer to the driver data structure.
3109 *
3110 * return value
3111 * 0
3112 */
Jens Axboe63166682011-09-27 21:27:43 -06003113static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003114{
3115 /*
3116 * Send standby immediate (E0h) to the drive so that it
3117 * saves its state.
3118 */
3119 mtip_standby_immediate(dd->port);
3120
3121 return 0;
3122}
3123
3124/*
3125 * Suspend function
3126 *
3127 * This function is called by the Block Layer just before the
3128 * system hibernates.
3129 *
3130 * @dd Pointer to the driver data structure.
3131 *
3132 * return value
3133 * 0 Suspend was successful
3134 * -EFAULT Suspend was not successful
3135 */
Jens Axboe63166682011-09-27 21:27:43 -06003136static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003137{
3138 /*
3139 * Send standby immediate (E0h) to the drive
3140 * so that it saves its state.
3141 */
3142 if (mtip_standby_immediate(dd->port) != 0) {
3143 dev_err(&dd->pdev->dev,
3144 "Failed standby-immediate command\n");
3145 return -EFAULT;
3146 }
3147
3148 /* Disable interrupts on the HBA.*/
3149 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3150 dd->mmio + HOST_CTL);
3151 mtip_deinit_port(dd->port);
3152
3153 return 0;
3154}
3155
3156/*
3157 * Resume function
3158 *
3159 * This function is called by the Block Layer as the
3160 * system resumes.
3161 *
3162 * @dd Pointer to the driver data structure.
3163 *
3164 * return value
3165 * 0 Resume was successful
3166 * -EFAULT Resume was not successful
3167 */
Jens Axboe63166682011-09-27 21:27:43 -06003168static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003169{
3170 /* Perform any needed hardware setup steps */
3171 hba_setup(dd);
3172
3173 /* Reset the HBA */
3174 if (mtip_hba_reset(dd) != 0) {
3175 dev_err(&dd->pdev->dev,
3176 "Unable to reset the HBA\n");
3177 return -EFAULT;
3178 }
3179
3180 /*
3181 * Enable the port, DMA engine, and FIS reception specific
3182 * h/w in controller.
3183 */
3184 mtip_init_port(dd->port);
3185 mtip_start_port(dd->port);
3186
3187 /* Enable interrupts on the HBA.*/
3188 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3189 dd->mmio + HOST_CTL);
3190
3191 return 0;
3192}
3193
3194/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003195 * Helper function for reusing disk name
3196 * upon hot insertion.
3197 */
3198static int rssd_disk_name_format(char *prefix,
3199 int index,
3200 char *buf,
3201 int buflen)
3202{
3203 const int base = 'z' - 'a' + 1;
3204 char *begin = buf + strlen(prefix);
3205 char *end = buf + buflen;
3206 char *p;
3207 int unit;
3208
3209 p = end - 1;
3210 *p = '\0';
3211 unit = base;
3212 do {
3213 if (p == begin)
3214 return -EINVAL;
3215 *--p = 'a' + (index % unit);
3216 index = (index / unit) - 1;
3217 } while (index >= 0);
3218
3219 memmove(begin, p, end - p);
3220 memcpy(buf, prefix, strlen(prefix));
3221
3222 return 0;
3223}
3224
3225/*
3226 * Block layer IOCTL handler.
3227 *
3228 * @dev Pointer to the block_device structure.
3229 * @mode ignored
3230 * @cmd IOCTL command passed from the user application.
3231 * @arg Argument passed from the user application.
3232 *
3233 * return value
3234 * 0 IOCTL completed successfully.
3235 * -ENOTTY IOCTL not supported or invalid driver data
3236 * structure pointer.
3237 */
3238static int mtip_block_ioctl(struct block_device *dev,
3239 fmode_t mode,
3240 unsigned cmd,
3241 unsigned long arg)
3242{
3243 struct driver_data *dd = dev->bd_disk->private_data;
3244
3245 if (!capable(CAP_SYS_ADMIN))
3246 return -EACCES;
3247
3248 if (!dd)
3249 return -ENOTTY;
3250
Asai Thambi S P45038362012-04-09 08:35:38 +02003251 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
3252 return -ENOTTY;
3253
Sam Bradshaw88523a62011-08-30 08:34:26 -06003254 switch (cmd) {
3255 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003256 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003257 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003258 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003259 }
3260}
3261
Jens Axboe16d02c02011-09-27 15:50:01 -06003262#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003263/*
3264 * Block layer compat IOCTL handler.
3265 *
3266 * @dev Pointer to the block_device structure.
3267 * @mode ignored
3268 * @cmd IOCTL command passed from the user application.
3269 * @arg Argument passed from the user application.
3270 *
3271 * return value
3272 * 0 IOCTL completed successfully.
3273 * -ENOTTY IOCTL not supported or invalid driver data
3274 * structure pointer.
3275 */
3276static int mtip_block_compat_ioctl(struct block_device *dev,
3277 fmode_t mode,
3278 unsigned cmd,
3279 unsigned long arg)
3280{
3281 struct driver_data *dd = dev->bd_disk->private_data;
3282
3283 if (!capable(CAP_SYS_ADMIN))
3284 return -EACCES;
3285
3286 if (!dd)
3287 return -ENOTTY;
3288
Asai Thambi S P45038362012-04-09 08:35:38 +02003289 if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
3290 return -ENOTTY;
3291
Sam Bradshaw88523a62011-08-30 08:34:26 -06003292 switch (cmd) {
3293 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003294 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003295 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003296 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003297 ide_task_request_t req_task;
3298 int compat_tasksize, outtotal, ret;
3299
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003300 compat_tasksize =
3301 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003302
3303 compat_req_task =
3304 (struct mtip_compat_ide_task_request_s __user *) arg;
3305
3306 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003307 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003308 return -EFAULT;
3309
3310 if (get_user(req_task.out_size, &compat_req_task->out_size))
3311 return -EFAULT;
3312
3313 if (get_user(req_task.in_size, &compat_req_task->in_size))
3314 return -EFAULT;
3315
3316 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3317
3318 ret = exec_drive_taskfile(dd, (void __user *) arg,
3319 &req_task, outtotal);
3320
3321 if (copy_to_user((void __user *) arg, &req_task,
3322 compat_tasksize -
3323 (2 * sizeof(compat_long_t))))
3324 return -EFAULT;
3325
3326 if (put_user(req_task.out_size, &compat_req_task->out_size))
3327 return -EFAULT;
3328
3329 if (put_user(req_task.in_size, &compat_req_task->in_size))
3330 return -EFAULT;
3331
3332 return ret;
3333 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003334 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003335 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003336 }
3337}
Jens Axboe16d02c02011-09-27 15:50:01 -06003338#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003339
3340/*
3341 * Obtain the geometry of the device.
3342 *
3343 * You may think that this function is obsolete, but some applications,
3344 * fdisk for example still used CHS values. This function describes the
3345 * device as having 224 heads and 56 sectors per cylinder. These values are
3346 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3347 * partition is described in terms of a start and end cylinder this means
3348 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3349 * affects performance.
3350 *
3351 * @dev Pointer to the block_device strucutre.
3352 * @geo Pointer to a hd_geometry structure.
3353 *
3354 * return value
3355 * 0 Operation completed successfully.
3356 * -ENOTTY An error occurred while reading the drive capacity.
3357 */
3358static int mtip_block_getgeo(struct block_device *dev,
3359 struct hd_geometry *geo)
3360{
3361 struct driver_data *dd = dev->bd_disk->private_data;
3362 sector_t capacity;
3363
3364 if (!dd)
3365 return -ENOTTY;
3366
3367 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3368 dev_warn(&dd->pdev->dev,
3369 "Could not get drive capacity.\n");
3370 return -ENOTTY;
3371 }
3372
3373 geo->heads = 224;
3374 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003375 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003376 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003377 return 0;
3378}
3379
3380/*
3381 * Block device operation function.
3382 *
3383 * This structure contains pointers to the functions required by the block
3384 * layer.
3385 */
3386static const struct block_device_operations mtip_block_ops = {
3387 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003388#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003389 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003390#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003391 .getgeo = mtip_block_getgeo,
3392 .owner = THIS_MODULE
3393};
3394
3395/*
3396 * Block layer make request function.
3397 *
3398 * This function is called by the kernel to process a BIO for
3399 * the P320 device.
3400 *
3401 * @queue Pointer to the request queue. Unused other than to obtain
3402 * the driver data structure.
3403 * @bio Pointer to the BIO.
3404 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003405 */
Jens Axboea71f4832011-11-05 08:36:21 +01003406static void mtip_make_request(struct request_queue *queue, struct bio *bio)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003407{
3408 struct driver_data *dd = queue->queuedata;
3409 struct scatterlist *sg;
3410 struct bio_vec *bvec;
3411 int nents = 0;
3412 int tag = 0;
3413
Asai Thambi S P45038362012-04-09 08:35:38 +02003414 if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3415 bio_endio(bio, -ENXIO);
3416 return;
3417 }
3418
Sam Bradshaw88523a62011-08-30 08:34:26 -06003419 if (unlikely(!bio_has_data(bio))) {
3420 blk_queue_flush(queue, 0);
3421 bio_endio(bio, 0);
Jens Axboea71f4832011-11-05 08:36:21 +01003422 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003423 }
3424
Sam Bradshaw88523a62011-08-30 08:34:26 -06003425 sg = mtip_hw_get_scatterlist(dd, &tag);
3426 if (likely(sg != NULL)) {
3427 blk_queue_bounce(queue, &bio);
3428
3429 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3430 dev_warn(&dd->pdev->dev,
Asai Thambi S P45038362012-04-09 08:35:38 +02003431 "Maximum number of SGL entries exceeded\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06003432 bio_io_error(bio);
3433 mtip_hw_release_scatterlist(dd, tag);
Jens Axboea71f4832011-11-05 08:36:21 +01003434 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003435 }
3436
3437 /* Create the scatter list for this bio. */
3438 bio_for_each_segment(bvec, bio, nents) {
3439 sg_set_page(&sg[nents],
3440 bvec->bv_page,
3441 bvec->bv_len,
3442 bvec->bv_offset);
3443 }
3444
3445 /* Issue the read/write. */
3446 mtip_hw_submit_io(dd,
3447 bio->bi_sector,
3448 bio_sectors(bio),
3449 nents,
3450 tag,
3451 bio_endio,
3452 bio,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003453 bio_data_dir(bio));
Jens Axboea71f4832011-11-05 08:36:21 +01003454 } else
Sam Bradshaw88523a62011-08-30 08:34:26 -06003455 bio_io_error(bio);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003456}
3457
3458/*
3459 * Block layer initialization function.
3460 *
3461 * This function is called once by the PCI layer for each P320
3462 * device that is connected to the system.
3463 *
3464 * @dd Pointer to the driver data structure.
3465 *
3466 * return value
3467 * 0 on success else an error code.
3468 */
Jens Axboe63166682011-09-27 21:27:43 -06003469static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003470{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003471 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003472 sector_t capacity;
3473 unsigned int index = 0;
3474 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003475 unsigned char thd_name[16];
Sam Bradshaw88523a62011-08-30 08:34:26 -06003476
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003477 if (dd->disk)
3478 goto skip_create_disk; /* hw init done, before rebuild */
3479
Sam Bradshaw88523a62011-08-30 08:34:26 -06003480 /* Initialize the protocol layer. */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003481 wait_for_rebuild = mtip_hw_init(dd);
3482 if (wait_for_rebuild < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003483 dev_err(&dd->pdev->dev,
3484 "Protocol layer initialization failed\n");
3485 rv = -EINVAL;
3486 goto protocol_init_error;
3487 }
3488
Sam Bradshaw88523a62011-08-30 08:34:26 -06003489 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3490 if (dd->disk == NULL) {
3491 dev_err(&dd->pdev->dev,
3492 "Unable to allocate gendisk structure\n");
3493 rv = -EINVAL;
3494 goto alloc_disk_error;
3495 }
3496
3497 /* Generate the disk name, implemented same as in sd.c */
3498 do {
3499 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3500 goto ida_get_error;
3501
3502 spin_lock(&rssd_index_lock);
3503 rv = ida_get_new(&rssd_index_ida, &index);
3504 spin_unlock(&rssd_index_lock);
3505 } while (rv == -EAGAIN);
3506
3507 if (rv)
3508 goto ida_get_error;
3509
3510 rv = rssd_disk_name_format("rssd",
3511 index,
3512 dd->disk->disk_name,
3513 DISK_NAME_LEN);
3514 if (rv)
3515 goto disk_index_error;
3516
3517 dd->disk->driverfs_dev = &dd->pdev->dev;
3518 dd->disk->major = dd->major;
3519 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3520 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003521 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003522 dd->index = index;
3523
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003524 /*
3525 * if rebuild pending, start the service thread, and delay the block
3526 * queue creation and add_disk()
3527 */
3528 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3529 goto start_service_thread;
3530
3531skip_create_disk:
3532 /* Allocate the request queue. */
3533 dd->queue = blk_alloc_queue(GFP_KERNEL);
3534 if (dd->queue == NULL) {
3535 dev_err(&dd->pdev->dev,
3536 "Unable to allocate request queue\n");
3537 rv = -ENOMEM;
3538 goto block_queue_alloc_init_error;
3539 }
3540
3541 /* Attach our request function to the request queue. */
3542 blk_queue_make_request(dd->queue, mtip_make_request);
3543
3544 dd->disk->queue = dd->queue;
3545 dd->queue->queuedata = dd;
3546
3547 /* Set device limits. */
3548 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3549 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3550 blk_queue_physical_block_size(dd->queue, 4096);
3551 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003552 /*
3553 * write back cache is not supported in the device. FUA depends on
3554 * write back cache support, hence setting flush support to zero.
3555 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003556 blk_queue_flush(dd->queue, 0);
3557
Sam Bradshaw88523a62011-08-30 08:34:26 -06003558 /* Set the capacity of the device in 512 byte sectors. */
3559 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3560 dev_warn(&dd->pdev->dev,
3561 "Could not read drive capacity\n");
3562 rv = -EIO;
3563 goto read_capacity_error;
3564 }
3565 set_capacity(dd->disk, capacity);
3566
3567 /* Enable the block device and add it to /dev */
3568 add_disk(dd->disk);
3569
3570 /*
3571 * Now that the disk is active, initialize any sysfs attributes
3572 * managed by the protocol layer.
3573 */
3574 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3575 if (kobj) {
3576 mtip_hw_sysfs_init(dd, kobj);
3577 kobject_put(kobj);
3578 }
3579
Asai Thambi S P45038362012-04-09 08:35:38 +02003580 if (dd->mtip_svc_handler) {
3581 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003582 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003583 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003584
3585start_service_thread:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003586 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3587
3588 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3589 dd, thd_name);
3590
3591 if (IS_ERR(dd->mtip_svc_handler)) {
3592 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3593 dd->mtip_svc_handler = NULL;
3594 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003595 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003596 }
3597
Asai Thambi S P45038362012-04-09 08:35:38 +02003598 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3599 rv = wait_for_rebuild;
3600
Sam Bradshaw88523a62011-08-30 08:34:26 -06003601 return rv;
3602
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003603kthread_run_error:
3604 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003605 del_gendisk(dd->disk);
3606
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003607read_capacity_error:
3608 blk_cleanup_queue(dd->queue);
3609
3610block_queue_alloc_init_error:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003611disk_index_error:
3612 spin_lock(&rssd_index_lock);
3613 ida_remove(&rssd_index_ida, index);
3614 spin_unlock(&rssd_index_lock);
3615
3616ida_get_error:
3617 put_disk(dd->disk);
3618
3619alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003620 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003621
3622protocol_init_error:
3623 return rv;
3624}
3625
3626/*
3627 * Block layer deinitialization function.
3628 *
3629 * Called by the PCI layer as each P320 device is removed.
3630 *
3631 * @dd Pointer to the driver data structure.
3632 *
3633 * return value
3634 * 0
3635 */
Jens Axboe63166682011-09-27 21:27:43 -06003636static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003637{
3638 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003639
3640 if (dd->mtip_svc_handler) {
3641 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3642 wake_up_interruptible(&dd->port->svc_wait);
3643 kthread_stop(dd->mtip_svc_handler);
3644 }
3645
Asai Thambi S Pf6587212012-04-09 08:35:38 +02003646 /* Clean up the sysfs attributes, if created */
Asai Thambi S P45038362012-04-09 08:35:38 +02003647 if (test_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag)) {
3648 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3649 if (kobj) {
3650 mtip_hw_sysfs_exit(dd, kobj);
3651 kobject_put(kobj);
3652 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003653 }
3654
3655 /*
3656 * Delete our gendisk structure. This also removes the device
3657 * from /dev
3658 */
3659 del_gendisk(dd->disk);
3660 blk_cleanup_queue(dd->queue);
3661 dd->disk = NULL;
3662 dd->queue = NULL;
3663
3664 /* De-initialize the protocol layer. */
3665 mtip_hw_exit(dd);
3666
3667 return 0;
3668}
3669
3670/*
3671 * Function called by the PCI layer when just before the
3672 * machine shuts down.
3673 *
3674 * If a protocol layer shutdown function is present it will be called
3675 * by this function.
3676 *
3677 * @dd Pointer to the driver data structure.
3678 *
3679 * return value
3680 * 0
3681 */
Jens Axboe63166682011-09-27 21:27:43 -06003682static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003683{
3684 dev_info(&dd->pdev->dev,
3685 "Shutting down %s ...\n", dd->disk->disk_name);
3686
3687 /* Delete our gendisk structure, and cleanup the blk queue. */
3688 del_gendisk(dd->disk);
3689 blk_cleanup_queue(dd->queue);
3690 dd->disk = NULL;
3691 dd->queue = NULL;
3692
3693 mtip_hw_shutdown(dd);
3694 return 0;
3695}
3696
Jens Axboe63166682011-09-27 21:27:43 -06003697static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003698{
3699 dev_info(&dd->pdev->dev,
3700 "Suspending %s ...\n", dd->disk->disk_name);
3701 mtip_hw_suspend(dd);
3702 return 0;
3703}
3704
Jens Axboe63166682011-09-27 21:27:43 -06003705static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003706{
3707 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3708 dd->disk->disk_name);
3709 mtip_hw_resume(dd);
3710 return 0;
3711}
3712
3713/*
3714 * Called for each supported PCI device detected.
3715 *
3716 * This function allocates the private data structure, enables the
3717 * PCI device and then calls the block layer initialization function.
3718 *
3719 * return value
3720 * 0 on success else an error code.
3721 */
3722static int mtip_pci_probe(struct pci_dev *pdev,
3723 const struct pci_device_id *ent)
3724{
3725 int rv = 0;
3726 struct driver_data *dd = NULL;
3727
3728 /* Allocate memory for this devices private data. */
3729 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3730 if (dd == NULL) {
3731 dev_err(&pdev->dev,
3732 "Unable to allocate memory for driver data\n");
3733 return -ENOMEM;
3734 }
3735
Sam Bradshaw88523a62011-08-30 08:34:26 -06003736 /* Attach the private data to this PCI device. */
3737 pci_set_drvdata(pdev, dd);
3738
3739 rv = pcim_enable_device(pdev);
3740 if (rv < 0) {
3741 dev_err(&pdev->dev, "Unable to enable device\n");
3742 goto iomap_err;
3743 }
3744
3745 /* Map BAR5 to memory. */
3746 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3747 if (rv < 0) {
3748 dev_err(&pdev->dev, "Unable to map regions\n");
3749 goto iomap_err;
3750 }
3751
3752 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3753 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3754
3755 if (rv) {
3756 rv = pci_set_consistent_dma_mask(pdev,
3757 DMA_BIT_MASK(32));
3758 if (rv) {
3759 dev_warn(&pdev->dev,
3760 "64-bit DMA enable failed\n");
3761 goto setmask_err;
3762 }
3763 }
3764 }
3765
3766 pci_set_master(pdev);
3767
3768 if (pci_enable_msi(pdev)) {
3769 dev_warn(&pdev->dev,
3770 "Unable to enable MSI interrupt.\n");
3771 goto block_initialize_err;
3772 }
3773
3774 /* Copy the info we may need later into the private data structure. */
3775 dd->major = mtip_major;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003776 dd->instance = instance;
3777 dd->pdev = pdev;
3778
3779 /* Initialize the block layer. */
3780 rv = mtip_block_initialize(dd);
3781 if (rv < 0) {
3782 dev_err(&pdev->dev,
3783 "Unable to initialize block layer\n");
3784 goto block_initialize_err;
3785 }
3786
3787 /*
3788 * Increment the instance count so that each device has a unique
3789 * instance number.
3790 */
3791 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02003792 if (rv != MTIP_FTL_REBUILD_MAGIC)
3793 set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003794 goto done;
3795
3796block_initialize_err:
3797 pci_disable_msi(pdev);
3798
3799setmask_err:
3800 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3801
3802iomap_err:
3803 kfree(dd);
3804 pci_set_drvdata(pdev, NULL);
3805 return rv;
3806done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003807 return rv;
3808}
3809
3810/*
3811 * Called for each probed device when the device is removed or the
3812 * driver is unloaded.
3813 *
3814 * return value
3815 * None
3816 */
3817static void mtip_pci_remove(struct pci_dev *pdev)
3818{
3819 struct driver_data *dd = pci_get_drvdata(pdev);
3820 int counter = 0;
3821
Asai Thambi S P45038362012-04-09 08:35:38 +02003822 set_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag);
3823
Sam Bradshaw88523a62011-08-30 08:34:26 -06003824 if (mtip_check_surprise_removal(pdev)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003825 while (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003826 counter++;
3827 msleep(20);
3828 if (counter == 10) {
3829 /* Cleanup the outstanding commands */
3830 mtip_command_cleanup(dd);
3831 break;
3832 }
3833 }
3834 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003835
3836 /* Clean up the block layer. */
3837 mtip_block_remove(dd);
3838
3839 pci_disable_msi(pdev);
3840
3841 kfree(dd);
3842 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3843}
3844
3845/*
3846 * Called for each probed device when the device is suspended.
3847 *
3848 * return value
3849 * 0 Success
3850 * <0 Error
3851 */
3852static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3853{
3854 int rv = 0;
3855 struct driver_data *dd = pci_get_drvdata(pdev);
3856
3857 if (!dd) {
3858 dev_err(&pdev->dev,
3859 "Driver private datastructure is NULL\n");
3860 return -EFAULT;
3861 }
3862
Asai Thambi S P45038362012-04-09 08:35:38 +02003863 set_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003864
3865 /* Disable ports & interrupts then send standby immediate */
3866 rv = mtip_block_suspend(dd);
3867 if (rv < 0) {
3868 dev_err(&pdev->dev,
3869 "Failed to suspend controller\n");
3870 return rv;
3871 }
3872
3873 /*
3874 * Save the pci config space to pdev structure &
3875 * disable the device
3876 */
3877 pci_save_state(pdev);
3878 pci_disable_device(pdev);
3879
3880 /* Move to Low power state*/
3881 pci_set_power_state(pdev, PCI_D3hot);
3882
3883 return rv;
3884}
3885
3886/*
3887 * Called for each probed device when the device is resumed.
3888 *
3889 * return value
3890 * 0 Success
3891 * <0 Error
3892 */
3893static int mtip_pci_resume(struct pci_dev *pdev)
3894{
3895 int rv = 0;
3896 struct driver_data *dd;
3897
3898 dd = pci_get_drvdata(pdev);
3899 if (!dd) {
3900 dev_err(&pdev->dev,
3901 "Driver private datastructure is NULL\n");
3902 return -EFAULT;
3903 }
3904
3905 /* Move the device to active State */
3906 pci_set_power_state(pdev, PCI_D0);
3907
3908 /* Restore PCI configuration space */
3909 pci_restore_state(pdev);
3910
3911 /* Enable the PCI device*/
3912 rv = pcim_enable_device(pdev);
3913 if (rv < 0) {
3914 dev_err(&pdev->dev,
3915 "Failed to enable card during resume\n");
3916 goto err;
3917 }
3918 pci_set_master(pdev);
3919
3920 /*
3921 * Calls hbaReset, initPort, & startPort function
3922 * then enables interrupts
3923 */
3924 rv = mtip_block_resume(dd);
3925 if (rv < 0)
3926 dev_err(&pdev->dev, "Unable to resume\n");
3927
3928err:
Asai Thambi S P45038362012-04-09 08:35:38 +02003929 clear_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003930
3931 return rv;
3932}
3933
3934/*
3935 * Shutdown routine
3936 *
3937 * return value
3938 * None
3939 */
3940static void mtip_pci_shutdown(struct pci_dev *pdev)
3941{
3942 struct driver_data *dd = pci_get_drvdata(pdev);
3943 if (dd)
3944 mtip_block_shutdown(dd);
3945}
3946
Sam Bradshaw88523a62011-08-30 08:34:26 -06003947/* Table of device ids supported by this driver. */
3948static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3949 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3950 { 0 }
3951};
3952
3953/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06003954static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003955 .name = MTIP_DRV_NAME,
3956 .id_table = mtip_pci_tbl,
3957 .probe = mtip_pci_probe,
3958 .remove = mtip_pci_remove,
3959 .suspend = mtip_pci_suspend,
3960 .resume = mtip_pci_resume,
3961 .shutdown = mtip_pci_shutdown,
3962};
3963
3964MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3965
3966/*
3967 * Module initialization function.
3968 *
3969 * Called once when the module is loaded. This function allocates a major
3970 * block device number to the Cyclone devices and registers the PCI layer
3971 * of the driver.
3972 *
3973 * Return value
3974 * 0 on success else error code.
3975 */
3976static int __init mtip_init(void)
3977{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003978 int error;
3979
Sam Bradshaw88523a62011-08-30 08:34:26 -06003980 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3981
3982 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003983 error = register_blkdev(0, MTIP_DRV_NAME);
3984 if (error <= 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003985 printk(KERN_ERR "Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003986 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003987 return -EBUSY;
3988 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003989 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003990
3991 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06003992 error = pci_register_driver(&mtip_pci_driver);
3993 if (error)
3994 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3995
3996 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003997}
3998
3999/*
4000 * Module de-initialization function.
4001 *
4002 * Called once when the module is unloaded. This function deallocates
4003 * the major block device number allocated by mtip_init() and
4004 * unregisters the PCI layer of the driver.
4005 *
4006 * Return value
4007 * none
4008 */
4009static void __exit mtip_exit(void)
4010{
4011 /* Release the allocated major block device number. */
4012 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4013
4014 /* Unregister the PCI driver. */
4015 pci_unregister_driver(&mtip_pci_driver);
4016}
4017
4018MODULE_AUTHOR("Micron Technology, Inc");
4019MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4020MODULE_LICENSE("GPL");
4021MODULE_VERSION(MTIP_DRV_VERSION);
4022
4023module_init(mtip_init);
4024module_exit(mtip_exit);