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