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