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