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