]>
Commit | Line | Data |
---|---|---|
b60503ba MW |
1 | /* |
2 | * NVM Express device driver | |
6eb0d698 | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
b60503ba MW |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
b60503ba MW |
13 | */ |
14 | ||
15 | #include <linux/nvme.h> | |
8de05535 | 16 | #include <linux/bitops.h> |
b60503ba | 17 | #include <linux/blkdev.h> |
a4aea562 | 18 | #include <linux/blk-mq.h> |
42f61420 | 19 | #include <linux/cpu.h> |
fd63e9ce | 20 | #include <linux/delay.h> |
b60503ba MW |
21 | #include <linux/errno.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/genhd.h> | |
4cc09e2d | 24 | #include <linux/hdreg.h> |
5aff9382 | 25 | #include <linux/idr.h> |
b60503ba MW |
26 | #include <linux/init.h> |
27 | #include <linux/interrupt.h> | |
28 | #include <linux/io.h> | |
29 | #include <linux/kdev_t.h> | |
1fa6aead | 30 | #include <linux/kthread.h> |
b60503ba MW |
31 | #include <linux/kernel.h> |
32 | #include <linux/mm.h> | |
33 | #include <linux/module.h> | |
34 | #include <linux/moduleparam.h> | |
35 | #include <linux/pci.h> | |
be7b6275 | 36 | #include <linux/poison.h> |
c3bfe717 | 37 | #include <linux/ptrace.h> |
b60503ba MW |
38 | #include <linux/sched.h> |
39 | #include <linux/slab.h> | |
40 | #include <linux/types.h> | |
5d0f6131 | 41 | #include <scsi/sg.h> |
797a796a HM |
42 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
43 | ||
9d43cf64 | 44 | #define NVME_Q_DEPTH 1024 |
a4aea562 | 45 | #define NVME_AQ_DEPTH 64 |
b60503ba MW |
46 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
47 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) | |
9d43cf64 | 48 | #define ADMIN_TIMEOUT (admin_timeout * HZ) |
2484f407 | 49 | #define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ) |
9d43cf64 KB |
50 | #define IOD_TIMEOUT (retry_time * HZ) |
51 | ||
52 | static unsigned char admin_timeout = 60; | |
53 | module_param(admin_timeout, byte, 0644); | |
54 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); | |
b60503ba | 55 | |
bd67608a MW |
56 | unsigned char nvme_io_timeout = 30; |
57 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); | |
b355084a | 58 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
b60503ba | 59 | |
61e4ce08 KB |
60 | static unsigned char retry_time = 30; |
61 | module_param(retry_time, byte, 0644); | |
62 | MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O"); | |
63 | ||
2484f407 DM |
64 | static unsigned char shutdown_timeout = 5; |
65 | module_param(shutdown_timeout, byte, 0644); | |
66 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); | |
67 | ||
b60503ba MW |
68 | static int nvme_major; |
69 | module_param(nvme_major, int, 0); | |
70 | ||
58ffacb5 MW |
71 | static int use_threaded_interrupts; |
72 | module_param(use_threaded_interrupts, int, 0); | |
73 | ||
1fa6aead MW |
74 | static DEFINE_SPINLOCK(dev_list_lock); |
75 | static LIST_HEAD(dev_list); | |
76 | static struct task_struct *nvme_thread; | |
9a6b9458 | 77 | static struct workqueue_struct *nvme_workq; |
b9afca3e | 78 | static wait_queue_head_t nvme_kthread_wait; |
f3db22fe | 79 | static struct notifier_block nvme_nb; |
1fa6aead | 80 | |
d4b4ff8e | 81 | static void nvme_reset_failed_dev(struct work_struct *ws); |
a4aea562 | 82 | static int nvme_process_cq(struct nvme_queue *nvmeq); |
d4b4ff8e | 83 | |
4d115420 KB |
84 | struct async_cmd_info { |
85 | struct kthread_work work; | |
86 | struct kthread_worker *worker; | |
a4aea562 | 87 | struct request *req; |
4d115420 KB |
88 | u32 result; |
89 | int status; | |
90 | void *ctx; | |
91 | }; | |
1fa6aead | 92 | |
b60503ba MW |
93 | /* |
94 | * An NVM Express queue. Each device has at least two (one for admin | |
95 | * commands and one for I/O commands). | |
96 | */ | |
97 | struct nvme_queue { | |
f435c282 | 98 | struct llist_node node; |
b60503ba | 99 | struct device *q_dmadev; |
091b6092 | 100 | struct nvme_dev *dev; |
3193f07b | 101 | char irqname[24]; /* nvme4294967295-65535\0 */ |
b60503ba MW |
102 | spinlock_t q_lock; |
103 | struct nvme_command *sq_cmds; | |
104 | volatile struct nvme_completion *cqes; | |
105 | dma_addr_t sq_dma_addr; | |
106 | dma_addr_t cq_dma_addr; | |
b60503ba MW |
107 | u32 __iomem *q_db; |
108 | u16 q_depth; | |
109 | u16 cq_vector; | |
110 | u16 sq_head; | |
111 | u16 sq_tail; | |
112 | u16 cq_head; | |
c30341dc | 113 | u16 qid; |
e9539f47 MW |
114 | u8 cq_phase; |
115 | u8 cqe_seen; | |
4d115420 | 116 | struct async_cmd_info cmdinfo; |
a4aea562 | 117 | struct blk_mq_hw_ctx *hctx; |
b60503ba MW |
118 | }; |
119 | ||
120 | /* | |
121 | * Check we didin't inadvertently grow the command struct | |
122 | */ | |
123 | static inline void _nvme_check_size(void) | |
124 | { | |
125 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); | |
126 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); | |
127 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); | |
128 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); | |
129 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); | |
f8ebf840 | 130 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); |
c30341dc | 131 | BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); |
b60503ba MW |
132 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
133 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); | |
134 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); | |
135 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); | |
6ecec745 | 136 | BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); |
b60503ba MW |
137 | } |
138 | ||
edd10d33 | 139 | typedef void (*nvme_completion_fn)(struct nvme_queue *, void *, |
c2f5b650 MW |
140 | struct nvme_completion *); |
141 | ||
e85248e5 | 142 | struct nvme_cmd_info { |
c2f5b650 MW |
143 | nvme_completion_fn fn; |
144 | void *ctx; | |
c30341dc | 145 | int aborted; |
a4aea562 | 146 | struct nvme_queue *nvmeq; |
e85248e5 MW |
147 | }; |
148 | ||
a4aea562 MB |
149 | static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
150 | unsigned int hctx_idx) | |
e85248e5 | 151 | { |
a4aea562 MB |
152 | struct nvme_dev *dev = data; |
153 | struct nvme_queue *nvmeq = dev->queues[0]; | |
154 | ||
155 | WARN_ON(nvmeq->hctx); | |
156 | nvmeq->hctx = hctx; | |
157 | hctx->driver_data = nvmeq; | |
158 | return 0; | |
e85248e5 MW |
159 | } |
160 | ||
a4aea562 MB |
161 | static int nvme_admin_init_request(void *data, struct request *req, |
162 | unsigned int hctx_idx, unsigned int rq_idx, | |
163 | unsigned int numa_node) | |
22404274 | 164 | { |
a4aea562 MB |
165 | struct nvme_dev *dev = data; |
166 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); | |
167 | struct nvme_queue *nvmeq = dev->queues[0]; | |
168 | ||
169 | BUG_ON(!nvmeq); | |
170 | cmd->nvmeq = nvmeq; | |
171 | return 0; | |
22404274 KB |
172 | } |
173 | ||
2c30540b JA |
174 | static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) |
175 | { | |
176 | struct nvme_queue *nvmeq = hctx->driver_data; | |
177 | ||
178 | nvmeq->hctx = NULL; | |
179 | } | |
180 | ||
a4aea562 MB |
181 | static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
182 | unsigned int hctx_idx) | |
b60503ba | 183 | { |
a4aea562 MB |
184 | struct nvme_dev *dev = data; |
185 | struct nvme_queue *nvmeq = dev->queues[ | |
186 | (hctx_idx % dev->queue_count) + 1]; | |
b60503ba | 187 | |
a4aea562 MB |
188 | if (!nvmeq->hctx) |
189 | nvmeq->hctx = hctx; | |
190 | ||
191 | /* nvmeq queues are shared between namespaces. We assume here that | |
192 | * blk-mq map the tags so they match up with the nvme queue tags. */ | |
193 | WARN_ON(nvmeq->hctx->tags != hctx->tags); | |
b60503ba | 194 | |
a4aea562 MB |
195 | hctx->driver_data = nvmeq; |
196 | return 0; | |
b60503ba MW |
197 | } |
198 | ||
a4aea562 MB |
199 | static int nvme_init_request(void *data, struct request *req, |
200 | unsigned int hctx_idx, unsigned int rq_idx, | |
201 | unsigned int numa_node) | |
b60503ba | 202 | { |
a4aea562 MB |
203 | struct nvme_dev *dev = data; |
204 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); | |
205 | struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1]; | |
206 | ||
207 | BUG_ON(!nvmeq); | |
208 | cmd->nvmeq = nvmeq; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx, | |
213 | nvme_completion_fn handler) | |
214 | { | |
215 | cmd->fn = handler; | |
216 | cmd->ctx = ctx; | |
217 | cmd->aborted = 0; | |
b60503ba MW |
218 | } |
219 | ||
c2f5b650 MW |
220 | /* Special values must be less than 0x1000 */ |
221 | #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) | |
d2d87034 MW |
222 | #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) |
223 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) | |
224 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) | |
be7b6275 | 225 | |
edd10d33 | 226 | static void special_completion(struct nvme_queue *nvmeq, void *ctx, |
c2f5b650 MW |
227 | struct nvme_completion *cqe) |
228 | { | |
229 | if (ctx == CMD_CTX_CANCELLED) | |
230 | return; | |
c2f5b650 | 231 | if (ctx == CMD_CTX_COMPLETED) { |
edd10d33 | 232 | dev_warn(nvmeq->q_dmadev, |
c2f5b650 MW |
233 | "completed id %d twice on queue %d\n", |
234 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); | |
235 | return; | |
236 | } | |
237 | if (ctx == CMD_CTX_INVALID) { | |
edd10d33 | 238 | dev_warn(nvmeq->q_dmadev, |
c2f5b650 MW |
239 | "invalid id %d completed on queue %d\n", |
240 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); | |
241 | return; | |
242 | } | |
edd10d33 | 243 | dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx); |
c2f5b650 MW |
244 | } |
245 | ||
a4aea562 | 246 | static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn) |
b60503ba | 247 | { |
c2f5b650 | 248 | void *ctx; |
b60503ba | 249 | |
859361a2 | 250 | if (fn) |
a4aea562 MB |
251 | *fn = cmd->fn; |
252 | ctx = cmd->ctx; | |
253 | cmd->fn = special_completion; | |
254 | cmd->ctx = CMD_CTX_CANCELLED; | |
c2f5b650 | 255 | return ctx; |
b60503ba MW |
256 | } |
257 | ||
a4aea562 MB |
258 | static void async_req_completion(struct nvme_queue *nvmeq, void *ctx, |
259 | struct nvme_completion *cqe) | |
3c0cf138 | 260 | { |
a4aea562 | 261 | struct request *req = ctx; |
3c0cf138 | 262 | |
a4aea562 MB |
263 | u32 result = le32_to_cpup(&cqe->result); |
264 | u16 status = le16_to_cpup(&cqe->status) >> 1; | |
265 | ||
266 | if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ) | |
267 | ++nvmeq->dev->event_limit; | |
268 | if (status == NVME_SC_SUCCESS) | |
269 | dev_warn(nvmeq->q_dmadev, | |
270 | "async event result %08x\n", result); | |
271 | ||
9d135bb8 | 272 | blk_mq_free_hctx_request(nvmeq->hctx, req); |
b60503ba MW |
273 | } |
274 | ||
a4aea562 MB |
275 | static void abort_completion(struct nvme_queue *nvmeq, void *ctx, |
276 | struct nvme_completion *cqe) | |
5a92e700 | 277 | { |
a4aea562 MB |
278 | struct request *req = ctx; |
279 | ||
280 | u16 status = le16_to_cpup(&cqe->status) >> 1; | |
281 | u32 result = le32_to_cpup(&cqe->result); | |
a51afb54 | 282 | |
9d135bb8 | 283 | blk_mq_free_hctx_request(nvmeq->hctx, req); |
a51afb54 | 284 | |
a4aea562 MB |
285 | dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result); |
286 | ++nvmeq->dev->abort_limit; | |
5a92e700 KB |
287 | } |
288 | ||
a4aea562 MB |
289 | static void async_completion(struct nvme_queue *nvmeq, void *ctx, |
290 | struct nvme_completion *cqe) | |
b60503ba | 291 | { |
a4aea562 MB |
292 | struct async_cmd_info *cmdinfo = ctx; |
293 | cmdinfo->result = le32_to_cpup(&cqe->result); | |
294 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; | |
295 | queue_kthread_work(cmdinfo->worker, &cmdinfo->work); | |
9d135bb8 | 296 | blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req); |
b60503ba MW |
297 | } |
298 | ||
a4aea562 MB |
299 | static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq, |
300 | unsigned int tag) | |
b60503ba | 301 | { |
a4aea562 MB |
302 | struct blk_mq_hw_ctx *hctx = nvmeq->hctx; |
303 | struct request *req = blk_mq_tag_to_rq(hctx->tags, tag); | |
a51afb54 | 304 | |
a4aea562 | 305 | return blk_mq_rq_to_pdu(req); |
4f5099af KB |
306 | } |
307 | ||
a4aea562 MB |
308 | /* |
309 | * Called with local interrupts disabled and the q_lock held. May not sleep. | |
310 | */ | |
311 | static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag, | |
312 | nvme_completion_fn *fn) | |
4f5099af | 313 | { |
a4aea562 MB |
314 | struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag); |
315 | void *ctx; | |
316 | if (tag >= nvmeq->q_depth) { | |
317 | *fn = special_completion; | |
318 | return CMD_CTX_INVALID; | |
319 | } | |
320 | if (fn) | |
321 | *fn = cmd->fn; | |
322 | ctx = cmd->ctx; | |
323 | cmd->fn = special_completion; | |
324 | cmd->ctx = CMD_CTX_COMPLETED; | |
325 | return ctx; | |
b60503ba MW |
326 | } |
327 | ||
328 | /** | |
714a7a22 | 329 | * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell |
b60503ba MW |
330 | * @nvmeq: The queue to use |
331 | * @cmd: The command to send | |
332 | * | |
333 | * Safe to use from interrupt context | |
334 | */ | |
a4aea562 | 335 | static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
b60503ba | 336 | { |
a4aea562 MB |
337 | u16 tail = nvmeq->sq_tail; |
338 | ||
b60503ba | 339 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
b60503ba MW |
340 | if (++tail == nvmeq->q_depth) |
341 | tail = 0; | |
7547881d | 342 | writel(tail, nvmeq->q_db); |
b60503ba | 343 | nvmeq->sq_tail = tail; |
b60503ba MW |
344 | |
345 | return 0; | |
346 | } | |
347 | ||
a4aea562 MB |
348 | static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
349 | { | |
350 | unsigned long flags; | |
351 | int ret; | |
352 | spin_lock_irqsave(&nvmeq->q_lock, flags); | |
353 | ret = __nvme_submit_cmd(nvmeq, cmd); | |
354 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); | |
355 | return ret; | |
356 | } | |
357 | ||
eca18b23 | 358 | static __le64 **iod_list(struct nvme_iod *iod) |
e025344c | 359 | { |
eca18b23 | 360 | return ((void *)iod) + iod->offset; |
e025344c SMM |
361 | } |
362 | ||
eca18b23 MW |
363 | /* |
364 | * Will slightly overestimate the number of pages needed. This is OK | |
365 | * as it only leads to a small amount of wasted memory for the lifetime of | |
366 | * the I/O. | |
367 | */ | |
1d090624 | 368 | static int nvme_npages(unsigned size, struct nvme_dev *dev) |
eca18b23 | 369 | { |
1d090624 KB |
370 | unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size); |
371 | return DIV_ROUND_UP(8 * nprps, dev->page_size - 8); | |
eca18b23 | 372 | } |
b60503ba | 373 | |
eca18b23 | 374 | static struct nvme_iod * |
1d090624 | 375 | nvme_alloc_iod(unsigned nseg, unsigned nbytes, struct nvme_dev *dev, gfp_t gfp) |
b60503ba | 376 | { |
eca18b23 | 377 | struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + |
1d090624 | 378 | sizeof(__le64 *) * nvme_npages(nbytes, dev) + |
eca18b23 MW |
379 | sizeof(struct scatterlist) * nseg, gfp); |
380 | ||
381 | if (iod) { | |
382 | iod->offset = offsetof(struct nvme_iod, sg[nseg]); | |
383 | iod->npages = -1; | |
384 | iod->length = nbytes; | |
2b196034 | 385 | iod->nents = 0; |
edd10d33 | 386 | iod->first_dma = 0ULL; |
eca18b23 MW |
387 | } |
388 | ||
389 | return iod; | |
b60503ba MW |
390 | } |
391 | ||
5d0f6131 | 392 | void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod) |
b60503ba | 393 | { |
1d090624 | 394 | const int last_prp = dev->page_size / 8 - 1; |
eca18b23 MW |
395 | int i; |
396 | __le64 **list = iod_list(iod); | |
397 | dma_addr_t prp_dma = iod->first_dma; | |
398 | ||
399 | if (iod->npages == 0) | |
400 | dma_pool_free(dev->prp_small_pool, list[0], prp_dma); | |
401 | for (i = 0; i < iod->npages; i++) { | |
402 | __le64 *prp_list = list[i]; | |
403 | dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); | |
404 | dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); | |
405 | prp_dma = next_prp_dma; | |
406 | } | |
407 | kfree(iod); | |
b60503ba MW |
408 | } |
409 | ||
b4ff9c8d KB |
410 | static int nvme_error_status(u16 status) |
411 | { | |
412 | switch (status & 0x7ff) { | |
413 | case NVME_SC_SUCCESS: | |
414 | return 0; | |
415 | case NVME_SC_CAP_EXCEEDED: | |
416 | return -ENOSPC; | |
417 | default: | |
418 | return -EIO; | |
419 | } | |
420 | } | |
421 | ||
a4aea562 | 422 | static void req_completion(struct nvme_queue *nvmeq, void *ctx, |
b60503ba MW |
423 | struct nvme_completion *cqe) |
424 | { | |
eca18b23 | 425 | struct nvme_iod *iod = ctx; |
a4aea562 MB |
426 | struct request *req = iod->private; |
427 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); | |
428 | ||
b60503ba MW |
429 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
430 | ||
edd10d33 | 431 | if (unlikely(status)) { |
a4aea562 MB |
432 | if (!(status & NVME_SC_DNR || blk_noretry_request(req)) |
433 | && (jiffies - req->start_time) < req->timeout) { | |
434 | blk_mq_requeue_request(req); | |
435 | blk_mq_kick_requeue_list(req->q); | |
edd10d33 KB |
436 | return; |
437 | } | |
a4aea562 MB |
438 | req->errors = nvme_error_status(status); |
439 | } else | |
440 | req->errors = 0; | |
441 | ||
442 | if (cmd_rq->aborted) | |
443 | dev_warn(&nvmeq->dev->pci_dev->dev, | |
444 | "completing aborted command with status:%04x\n", | |
445 | status); | |
446 | ||
447 | if (iod->nents) | |
448 | dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, iod->nents, | |
449 | rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
edd10d33 | 450 | nvme_free_iod(nvmeq->dev, iod); |
3291fa57 | 451 | |
a4aea562 | 452 | blk_mq_complete_request(req); |
b60503ba MW |
453 | } |
454 | ||
184d2944 | 455 | /* length is in bytes. gfp flags indicates whether we may sleep. */ |
edd10d33 KB |
456 | int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len, |
457 | gfp_t gfp) | |
ff22b54f | 458 | { |
99802a7a | 459 | struct dma_pool *pool; |
eca18b23 MW |
460 | int length = total_len; |
461 | struct scatterlist *sg = iod->sg; | |
ff22b54f MW |
462 | int dma_len = sg_dma_len(sg); |
463 | u64 dma_addr = sg_dma_address(sg); | |
464 | int offset = offset_in_page(dma_addr); | |
e025344c | 465 | __le64 *prp_list; |
eca18b23 | 466 | __le64 **list = iod_list(iod); |
e025344c | 467 | dma_addr_t prp_dma; |
eca18b23 | 468 | int nprps, i; |
1d090624 | 469 | u32 page_size = dev->page_size; |
ff22b54f | 470 | |
1d090624 | 471 | length -= (page_size - offset); |
ff22b54f | 472 | if (length <= 0) |
eca18b23 | 473 | return total_len; |
ff22b54f | 474 | |
1d090624 | 475 | dma_len -= (page_size - offset); |
ff22b54f | 476 | if (dma_len) { |
1d090624 | 477 | dma_addr += (page_size - offset); |
ff22b54f MW |
478 | } else { |
479 | sg = sg_next(sg); | |
480 | dma_addr = sg_dma_address(sg); | |
481 | dma_len = sg_dma_len(sg); | |
482 | } | |
483 | ||
1d090624 | 484 | if (length <= page_size) { |
edd10d33 | 485 | iod->first_dma = dma_addr; |
eca18b23 | 486 | return total_len; |
e025344c SMM |
487 | } |
488 | ||
1d090624 | 489 | nprps = DIV_ROUND_UP(length, page_size); |
99802a7a MW |
490 | if (nprps <= (256 / 8)) { |
491 | pool = dev->prp_small_pool; | |
eca18b23 | 492 | iod->npages = 0; |
99802a7a MW |
493 | } else { |
494 | pool = dev->prp_page_pool; | |
eca18b23 | 495 | iod->npages = 1; |
99802a7a MW |
496 | } |
497 | ||
b77954cb MW |
498 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
499 | if (!prp_list) { | |
edd10d33 | 500 | iod->first_dma = dma_addr; |
eca18b23 | 501 | iod->npages = -1; |
1d090624 | 502 | return (total_len - length) + page_size; |
b77954cb | 503 | } |
eca18b23 MW |
504 | list[0] = prp_list; |
505 | iod->first_dma = prp_dma; | |
e025344c SMM |
506 | i = 0; |
507 | for (;;) { | |
1d090624 | 508 | if (i == page_size >> 3) { |
e025344c | 509 | __le64 *old_prp_list = prp_list; |
b77954cb | 510 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
eca18b23 MW |
511 | if (!prp_list) |
512 | return total_len - length; | |
513 | list[iod->npages++] = prp_list; | |
7523d834 MW |
514 | prp_list[0] = old_prp_list[i - 1]; |
515 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); | |
516 | i = 1; | |
e025344c SMM |
517 | } |
518 | prp_list[i++] = cpu_to_le64(dma_addr); | |
1d090624 KB |
519 | dma_len -= page_size; |
520 | dma_addr += page_size; | |
521 | length -= page_size; | |
e025344c SMM |
522 | if (length <= 0) |
523 | break; | |
524 | if (dma_len > 0) | |
525 | continue; | |
526 | BUG_ON(dma_len < 0); | |
527 | sg = sg_next(sg); | |
528 | dma_addr = sg_dma_address(sg); | |
529 | dma_len = sg_dma_len(sg); | |
ff22b54f MW |
530 | } |
531 | ||
eca18b23 | 532 | return total_len; |
ff22b54f MW |
533 | } |
534 | ||
a4aea562 MB |
535 | /* |
536 | * We reuse the small pool to allocate the 16-byte range here as it is not | |
537 | * worth having a special pool for these or additional cases to handle freeing | |
538 | * the iod. | |
539 | */ | |
540 | static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, | |
541 | struct request *req, struct nvme_iod *iod) | |
0e5e4f0e | 542 | { |
edd10d33 KB |
543 | struct nvme_dsm_range *range = |
544 | (struct nvme_dsm_range *)iod_list(iod)[0]; | |
0e5e4f0e KB |
545 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
546 | ||
0e5e4f0e | 547 | range->cattr = cpu_to_le32(0); |
a4aea562 MB |
548 | range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift); |
549 | range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); | |
0e5e4f0e KB |
550 | |
551 | memset(cmnd, 0, sizeof(*cmnd)); | |
552 | cmnd->dsm.opcode = nvme_cmd_dsm; | |
a4aea562 | 553 | cmnd->dsm.command_id = req->tag; |
0e5e4f0e KB |
554 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
555 | cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); | |
556 | cmnd->dsm.nr = 0; | |
557 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); | |
558 | ||
559 | if (++nvmeq->sq_tail == nvmeq->q_depth) | |
560 | nvmeq->sq_tail = 0; | |
561 | writel(nvmeq->sq_tail, nvmeq->q_db); | |
0e5e4f0e KB |
562 | } |
563 | ||
a4aea562 | 564 | static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
00df5cb4 MW |
565 | int cmdid) |
566 | { | |
567 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; | |
568 | ||
569 | memset(cmnd, 0, sizeof(*cmnd)); | |
570 | cmnd->common.opcode = nvme_cmd_flush; | |
571 | cmnd->common.command_id = cmdid; | |
572 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); | |
573 | ||
574 | if (++nvmeq->sq_tail == nvmeq->q_depth) | |
575 | nvmeq->sq_tail = 0; | |
576 | writel(nvmeq->sq_tail, nvmeq->q_db); | |
00df5cb4 MW |
577 | } |
578 | ||
a4aea562 MB |
579 | static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod, |
580 | struct nvme_ns *ns) | |
b60503ba | 581 | { |
a4aea562 | 582 | struct request *req = iod->private; |
ff22b54f | 583 | struct nvme_command *cmnd; |
a4aea562 MB |
584 | u16 control = 0; |
585 | u32 dsmgmt = 0; | |
00df5cb4 | 586 | |
a4aea562 | 587 | if (req->cmd_flags & REQ_FUA) |
b60503ba | 588 | control |= NVME_RW_FUA; |
a4aea562 | 589 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
b60503ba MW |
590 | control |= NVME_RW_LR; |
591 | ||
a4aea562 | 592 | if (req->cmd_flags & REQ_RAHEAD) |
b60503ba MW |
593 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
594 | ||
ff22b54f | 595 | cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
b8deb62c | 596 | memset(cmnd, 0, sizeof(*cmnd)); |
b60503ba | 597 | |
a4aea562 MB |
598 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); |
599 | cmnd->rw.command_id = req->tag; | |
ff22b54f | 600 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
edd10d33 KB |
601 | cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); |
602 | cmnd->rw.prp2 = cpu_to_le64(iod->first_dma); | |
a4aea562 MB |
603 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
604 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); | |
ff22b54f MW |
605 | cmnd->rw.control = cpu_to_le16(control); |
606 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); | |
b60503ba | 607 | |
b60503ba MW |
608 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
609 | nvmeq->sq_tail = 0; | |
7547881d | 610 | writel(nvmeq->sq_tail, nvmeq->q_db); |
b60503ba | 611 | |
1974b1ae | 612 | return 0; |
edd10d33 KB |
613 | } |
614 | ||
a4aea562 MB |
615 | static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx, |
616 | const struct blk_mq_queue_data *bd) | |
edd10d33 | 617 | { |
a4aea562 MB |
618 | struct nvme_ns *ns = hctx->queue->queuedata; |
619 | struct nvme_queue *nvmeq = hctx->driver_data; | |
620 | struct request *req = bd->rq; | |
621 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); | |
edd10d33 | 622 | struct nvme_iod *iod; |
a4aea562 | 623 | int psegs = req->nr_phys_segments; |
a4aea562 MB |
624 | enum dma_data_direction dma_dir; |
625 | unsigned size = !(req->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(req) : | |
9dbbfab7 | 626 | sizeof(struct nvme_dsm_range); |
edd10d33 | 627 | |
9dbbfab7 | 628 | iod = nvme_alloc_iod(psegs, size, ns->dev, GFP_ATOMIC); |
edd10d33 | 629 | if (!iod) |
fe54303e | 630 | return BLK_MQ_RQ_QUEUE_BUSY; |
a4aea562 MB |
631 | |
632 | iod->private = req; | |
edd10d33 | 633 | |
a4aea562 | 634 | if (req->cmd_flags & REQ_DISCARD) { |
edd10d33 KB |
635 | void *range; |
636 | /* | |
637 | * We reuse the small pool to allocate the 16-byte range here | |
638 | * as it is not worth having a special pool for these or | |
639 | * additional cases to handle freeing the iod. | |
640 | */ | |
641 | range = dma_pool_alloc(nvmeq->dev->prp_small_pool, | |
642 | GFP_ATOMIC, | |
643 | &iod->first_dma); | |
a4aea562 | 644 | if (!range) |
fe54303e | 645 | goto retry_cmd; |
edd10d33 KB |
646 | iod_list(iod)[0] = (__le64 *)range; |
647 | iod->npages = 0; | |
648 | } else if (psegs) { | |
a4aea562 MB |
649 | dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
650 | ||
651 | sg_init_table(iod->sg, psegs); | |
652 | iod->nents = blk_rq_map_sg(req->q, req, iod->sg); | |
fe54303e JA |
653 | if (!iod->nents) |
654 | goto error_cmd; | |
a4aea562 MB |
655 | |
656 | if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir)) | |
fe54303e | 657 | goto retry_cmd; |
a4aea562 | 658 | |
fe54303e JA |
659 | if (blk_rq_bytes(req) != |
660 | nvme_setup_prps(nvmeq->dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) { | |
661 | dma_unmap_sg(&nvmeq->dev->pci_dev->dev, iod->sg, | |
662 | iod->nents, dma_dir); | |
663 | goto retry_cmd; | |
664 | } | |
edd10d33 | 665 | } |
1974b1ae | 666 | |
a4aea562 MB |
667 | blk_mq_start_request(req); |
668 | ||
9af8785a | 669 | nvme_set_info(cmd, iod, req_completion); |
a4aea562 MB |
670 | spin_lock_irq(&nvmeq->q_lock); |
671 | if (req->cmd_flags & REQ_DISCARD) | |
672 | nvme_submit_discard(nvmeq, ns, req, iod); | |
673 | else if (req->cmd_flags & REQ_FLUSH) | |
674 | nvme_submit_flush(nvmeq, ns, req->tag); | |
675 | else | |
676 | nvme_submit_iod(nvmeq, iod, ns); | |
677 | ||
678 | nvme_process_cq(nvmeq); | |
679 | spin_unlock_irq(&nvmeq->q_lock); | |
680 | return BLK_MQ_RQ_QUEUE_OK; | |
681 | ||
fe54303e JA |
682 | error_cmd: |
683 | nvme_free_iod(nvmeq->dev, iod); | |
684 | return BLK_MQ_RQ_QUEUE_ERROR; | |
685 | retry_cmd: | |
eca18b23 | 686 | nvme_free_iod(nvmeq->dev, iod); |
fe54303e | 687 | return BLK_MQ_RQ_QUEUE_BUSY; |
b60503ba MW |
688 | } |
689 | ||
e9539f47 | 690 | static int nvme_process_cq(struct nvme_queue *nvmeq) |
b60503ba | 691 | { |
82123460 | 692 | u16 head, phase; |
b60503ba | 693 | |
b60503ba | 694 | head = nvmeq->cq_head; |
82123460 | 695 | phase = nvmeq->cq_phase; |
b60503ba MW |
696 | |
697 | for (;;) { | |
c2f5b650 MW |
698 | void *ctx; |
699 | nvme_completion_fn fn; | |
b60503ba | 700 | struct nvme_completion cqe = nvmeq->cqes[head]; |
82123460 | 701 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
b60503ba MW |
702 | break; |
703 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); | |
704 | if (++head == nvmeq->q_depth) { | |
705 | head = 0; | |
82123460 | 706 | phase = !phase; |
b60503ba | 707 | } |
a4aea562 | 708 | ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn); |
edd10d33 | 709 | fn(nvmeq, ctx, &cqe); |
b60503ba MW |
710 | } |
711 | ||
712 | /* If the controller ignores the cq head doorbell and continuously | |
713 | * writes to the queue, it is theoretically possible to wrap around | |
714 | * the queue twice and mistakenly return IRQ_NONE. Linux only | |
715 | * requires that 0.1% of your interrupts are handled, so this isn't | |
716 | * a big problem. | |
717 | */ | |
82123460 | 718 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
e9539f47 | 719 | return 0; |
b60503ba | 720 | |
b80d5ccc | 721 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
b60503ba | 722 | nvmeq->cq_head = head; |
82123460 | 723 | nvmeq->cq_phase = phase; |
b60503ba | 724 | |
e9539f47 MW |
725 | nvmeq->cqe_seen = 1; |
726 | return 1; | |
b60503ba MW |
727 | } |
728 | ||
a4aea562 MB |
729 | /* Admin queue isn't initialized as a request queue. If at some point this |
730 | * happens anyway, make sure to notify the user */ | |
731 | static int nvme_admin_queue_rq(struct blk_mq_hw_ctx *hctx, | |
732 | const struct blk_mq_queue_data *bd) | |
7d822457 | 733 | { |
a4aea562 MB |
734 | WARN_ON_ONCE(1); |
735 | return BLK_MQ_RQ_QUEUE_ERROR; | |
7d822457 MW |
736 | } |
737 | ||
b60503ba | 738 | static irqreturn_t nvme_irq(int irq, void *data) |
58ffacb5 MW |
739 | { |
740 | irqreturn_t result; | |
741 | struct nvme_queue *nvmeq = data; | |
742 | spin_lock(&nvmeq->q_lock); | |
e9539f47 MW |
743 | nvme_process_cq(nvmeq); |
744 | result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE; | |
745 | nvmeq->cqe_seen = 0; | |
58ffacb5 MW |
746 | spin_unlock(&nvmeq->q_lock); |
747 | return result; | |
748 | } | |
749 | ||
750 | static irqreturn_t nvme_irq_check(int irq, void *data) | |
751 | { | |
752 | struct nvme_queue *nvmeq = data; | |
753 | struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; | |
754 | if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) | |
755 | return IRQ_NONE; | |
756 | return IRQ_WAKE_THREAD; | |
757 | } | |
758 | ||
a4aea562 MB |
759 | static void nvme_abort_cmd_info(struct nvme_queue *nvmeq, struct nvme_cmd_info * |
760 | cmd_info) | |
3c0cf138 MW |
761 | { |
762 | spin_lock_irq(&nvmeq->q_lock); | |
a4aea562 | 763 | cancel_cmd_info(cmd_info, NULL); |
3c0cf138 MW |
764 | spin_unlock_irq(&nvmeq->q_lock); |
765 | } | |
766 | ||
c2f5b650 MW |
767 | struct sync_cmd_info { |
768 | struct task_struct *task; | |
769 | u32 result; | |
770 | int status; | |
771 | }; | |
772 | ||
edd10d33 | 773 | static void sync_completion(struct nvme_queue *nvmeq, void *ctx, |
c2f5b650 MW |
774 | struct nvme_completion *cqe) |
775 | { | |
776 | struct sync_cmd_info *cmdinfo = ctx; | |
777 | cmdinfo->result = le32_to_cpup(&cqe->result); | |
778 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; | |
779 | wake_up_process(cmdinfo->task); | |
780 | } | |
781 | ||
b60503ba MW |
782 | /* |
783 | * Returns 0 on success. If the result is negative, it's a Linux error code; | |
784 | * if the result is positive, it's an NVM Express status code | |
785 | */ | |
a4aea562 | 786 | static int nvme_submit_sync_cmd(struct request *req, struct nvme_command *cmd, |
5d0f6131 | 787 | u32 *result, unsigned timeout) |
b60503ba | 788 | { |
a4aea562 | 789 | int ret; |
b60503ba | 790 | struct sync_cmd_info cmdinfo; |
a4aea562 MB |
791 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
792 | struct nvme_queue *nvmeq = cmd_rq->nvmeq; | |
b60503ba MW |
793 | |
794 | cmdinfo.task = current; | |
795 | cmdinfo.status = -EINTR; | |
796 | ||
a4aea562 MB |
797 | cmd->common.command_id = req->tag; |
798 | ||
799 | nvme_set_info(cmd_rq, &cmdinfo, sync_completion); | |
b60503ba | 800 | |
3c0cf138 | 801 | set_current_state(TASK_KILLABLE); |
4f5099af KB |
802 | ret = nvme_submit_cmd(nvmeq, cmd); |
803 | if (ret) { | |
a4aea562 | 804 | nvme_finish_cmd(nvmeq, req->tag, NULL); |
4f5099af | 805 | set_current_state(TASK_RUNNING); |
4f5099af | 806 | } |
849c6e77 | 807 | ret = schedule_timeout(timeout); |
b60503ba | 808 | |
849c6e77 JA |
809 | /* |
810 | * Ensure that sync_completion has either run, or that it will | |
811 | * never run. | |
812 | */ | |
813 | nvme_abort_cmd_info(nvmeq, blk_mq_rq_to_pdu(req)); | |
814 | ||
815 | /* | |
816 | * We never got the completion | |
817 | */ | |
818 | if (cmdinfo.status == -EINTR) | |
3c0cf138 | 819 | return -EINTR; |
3c0cf138 | 820 | |
b60503ba MW |
821 | if (result) |
822 | *result = cmdinfo.result; | |
823 | ||
824 | return cmdinfo.status; | |
825 | } | |
826 | ||
a4aea562 MB |
827 | static int nvme_submit_async_admin_req(struct nvme_dev *dev) |
828 | { | |
829 | struct nvme_queue *nvmeq = dev->queues[0]; | |
830 | struct nvme_command c; | |
831 | struct nvme_cmd_info *cmd_info; | |
832 | struct request *req; | |
833 | ||
6dcc0cf6 | 834 | req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, false); |
9f173b33 DC |
835 | if (IS_ERR(req)) |
836 | return PTR_ERR(req); | |
a4aea562 MB |
837 | |
838 | cmd_info = blk_mq_rq_to_pdu(req); | |
839 | nvme_set_info(cmd_info, req, async_req_completion); | |
840 | ||
841 | memset(&c, 0, sizeof(c)); | |
842 | c.common.opcode = nvme_admin_async_event; | |
843 | c.common.command_id = req->tag; | |
844 | ||
845 | return __nvme_submit_cmd(nvmeq, &c); | |
846 | } | |
847 | ||
848 | static int nvme_submit_admin_async_cmd(struct nvme_dev *dev, | |
4d115420 KB |
849 | struct nvme_command *cmd, |
850 | struct async_cmd_info *cmdinfo, unsigned timeout) | |
851 | { | |
a4aea562 MB |
852 | struct nvme_queue *nvmeq = dev->queues[0]; |
853 | struct request *req; | |
854 | struct nvme_cmd_info *cmd_rq; | |
4d115420 | 855 | |
a4aea562 | 856 | req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false); |
9f173b33 DC |
857 | if (IS_ERR(req)) |
858 | return PTR_ERR(req); | |
a4aea562 MB |
859 | |
860 | req->timeout = timeout; | |
861 | cmd_rq = blk_mq_rq_to_pdu(req); | |
862 | cmdinfo->req = req; | |
863 | nvme_set_info(cmd_rq, cmdinfo, async_completion); | |
4d115420 | 864 | cmdinfo->status = -EINTR; |
a4aea562 MB |
865 | |
866 | cmd->common.command_id = req->tag; | |
867 | ||
4f5099af | 868 | return nvme_submit_cmd(nvmeq, cmd); |
4d115420 KB |
869 | } |
870 | ||
a64e6bb4 | 871 | static int __nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
a4aea562 | 872 | u32 *result, unsigned timeout) |
b60503ba | 873 | { |
a4aea562 MB |
874 | int res; |
875 | struct request *req; | |
876 | ||
877 | req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false); | |
97fe3832 JA |
878 | if (IS_ERR(req)) |
879 | return PTR_ERR(req); | |
a4aea562 | 880 | res = nvme_submit_sync_cmd(req, cmd, result, timeout); |
9d135bb8 | 881 | blk_mq_free_request(req); |
a4aea562 | 882 | return res; |
4f5099af KB |
883 | } |
884 | ||
a4aea562 | 885 | int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
4f5099af KB |
886 | u32 *result) |
887 | { | |
a4aea562 | 888 | return __nvme_submit_admin_cmd(dev, cmd, result, ADMIN_TIMEOUT); |
b60503ba MW |
889 | } |
890 | ||
a4aea562 MB |
891 | int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_ns *ns, |
892 | struct nvme_command *cmd, u32 *result) | |
4d115420 | 893 | { |
a4aea562 MB |
894 | int res; |
895 | struct request *req; | |
896 | ||
897 | req = blk_mq_alloc_request(ns->queue, WRITE, (GFP_KERNEL|__GFP_WAIT), | |
898 | false); | |
97fe3832 JA |
899 | if (IS_ERR(req)) |
900 | return PTR_ERR(req); | |
a4aea562 | 901 | res = nvme_submit_sync_cmd(req, cmd, result, NVME_IO_TIMEOUT); |
9d135bb8 | 902 | blk_mq_free_request(req); |
a4aea562 | 903 | return res; |
4d115420 KB |
904 | } |
905 | ||
b60503ba MW |
906 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
907 | { | |
b60503ba MW |
908 | struct nvme_command c; |
909 | ||
910 | memset(&c, 0, sizeof(c)); | |
911 | c.delete_queue.opcode = opcode; | |
912 | c.delete_queue.qid = cpu_to_le16(id); | |
913 | ||
a4aea562 | 914 | return nvme_submit_admin_cmd(dev, &c, NULL); |
b60503ba MW |
915 | } |
916 | ||
917 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, | |
918 | struct nvme_queue *nvmeq) | |
919 | { | |
b60503ba MW |
920 | struct nvme_command c; |
921 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; | |
922 | ||
923 | memset(&c, 0, sizeof(c)); | |
924 | c.create_cq.opcode = nvme_admin_create_cq; | |
925 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); | |
926 | c.create_cq.cqid = cpu_to_le16(qid); | |
927 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); | |
928 | c.create_cq.cq_flags = cpu_to_le16(flags); | |
929 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); | |
930 | ||
a4aea562 | 931 | return nvme_submit_admin_cmd(dev, &c, NULL); |
b60503ba MW |
932 | } |
933 | ||
934 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, | |
935 | struct nvme_queue *nvmeq) | |
936 | { | |
b60503ba MW |
937 | struct nvme_command c; |
938 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; | |
939 | ||
940 | memset(&c, 0, sizeof(c)); | |
941 | c.create_sq.opcode = nvme_admin_create_sq; | |
942 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); | |
943 | c.create_sq.sqid = cpu_to_le16(qid); | |
944 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); | |
945 | c.create_sq.sq_flags = cpu_to_le16(flags); | |
946 | c.create_sq.cqid = cpu_to_le16(qid); | |
947 | ||
a4aea562 | 948 | return nvme_submit_admin_cmd(dev, &c, NULL); |
b60503ba MW |
949 | } |
950 | ||
951 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) | |
952 | { | |
953 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); | |
954 | } | |
955 | ||
956 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) | |
957 | { | |
958 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); | |
959 | } | |
960 | ||
5d0f6131 | 961 | int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns, |
bc5fc7e4 MW |
962 | dma_addr_t dma_addr) |
963 | { | |
964 | struct nvme_command c; | |
965 | ||
966 | memset(&c, 0, sizeof(c)); | |
967 | c.identify.opcode = nvme_admin_identify; | |
968 | c.identify.nsid = cpu_to_le32(nsid); | |
969 | c.identify.prp1 = cpu_to_le64(dma_addr); | |
970 | c.identify.cns = cpu_to_le32(cns); | |
971 | ||
972 | return nvme_submit_admin_cmd(dev, &c, NULL); | |
973 | } | |
974 | ||
5d0f6131 | 975 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
08df1e05 | 976 | dma_addr_t dma_addr, u32 *result) |
bc5fc7e4 MW |
977 | { |
978 | struct nvme_command c; | |
979 | ||
980 | memset(&c, 0, sizeof(c)); | |
981 | c.features.opcode = nvme_admin_get_features; | |
a42cecce | 982 | c.features.nsid = cpu_to_le32(nsid); |
bc5fc7e4 MW |
983 | c.features.prp1 = cpu_to_le64(dma_addr); |
984 | c.features.fid = cpu_to_le32(fid); | |
bc5fc7e4 | 985 | |
08df1e05 | 986 | return nvme_submit_admin_cmd(dev, &c, result); |
df348139 MW |
987 | } |
988 | ||
5d0f6131 VV |
989 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, |
990 | dma_addr_t dma_addr, u32 *result) | |
df348139 MW |
991 | { |
992 | struct nvme_command c; | |
993 | ||
994 | memset(&c, 0, sizeof(c)); | |
995 | c.features.opcode = nvme_admin_set_features; | |
996 | c.features.prp1 = cpu_to_le64(dma_addr); | |
997 | c.features.fid = cpu_to_le32(fid); | |
998 | c.features.dword11 = cpu_to_le32(dword11); | |
999 | ||
bc5fc7e4 MW |
1000 | return nvme_submit_admin_cmd(dev, &c, result); |
1001 | } | |
1002 | ||
c30341dc | 1003 | /** |
a4aea562 | 1004 | * nvme_abort_req - Attempt aborting a request |
c30341dc KB |
1005 | * |
1006 | * Schedule controller reset if the command was already aborted once before and | |
1007 | * still hasn't been returned to the driver, or if this is the admin queue. | |
1008 | */ | |
a4aea562 | 1009 | static void nvme_abort_req(struct request *req) |
c30341dc | 1010 | { |
a4aea562 MB |
1011 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
1012 | struct nvme_queue *nvmeq = cmd_rq->nvmeq; | |
c30341dc | 1013 | struct nvme_dev *dev = nvmeq->dev; |
a4aea562 MB |
1014 | struct request *abort_req; |
1015 | struct nvme_cmd_info *abort_cmd; | |
1016 | struct nvme_command cmd; | |
c30341dc | 1017 | |
a4aea562 | 1018 | if (!nvmeq->qid || cmd_rq->aborted) { |
c30341dc KB |
1019 | if (work_busy(&dev->reset_work)) |
1020 | return; | |
1021 | list_del_init(&dev->node); | |
1022 | dev_warn(&dev->pci_dev->dev, | |
a4aea562 MB |
1023 | "I/O %d QID %d timeout, reset controller\n", |
1024 | req->tag, nvmeq->qid); | |
9ca97374 | 1025 | dev->reset_workfn = nvme_reset_failed_dev; |
c30341dc KB |
1026 | queue_work(nvme_workq, &dev->reset_work); |
1027 | return; | |
1028 | } | |
1029 | ||
1030 | if (!dev->abort_limit) | |
1031 | return; | |
1032 | ||
a4aea562 MB |
1033 | abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, |
1034 | false); | |
9f173b33 | 1035 | if (IS_ERR(abort_req)) |
c30341dc KB |
1036 | return; |
1037 | ||
a4aea562 MB |
1038 | abort_cmd = blk_mq_rq_to_pdu(abort_req); |
1039 | nvme_set_info(abort_cmd, abort_req, abort_completion); | |
1040 | ||
c30341dc KB |
1041 | memset(&cmd, 0, sizeof(cmd)); |
1042 | cmd.abort.opcode = nvme_admin_abort_cmd; | |
a4aea562 | 1043 | cmd.abort.cid = req->tag; |
c30341dc | 1044 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
a4aea562 | 1045 | cmd.abort.command_id = abort_req->tag; |
c30341dc KB |
1046 | |
1047 | --dev->abort_limit; | |
a4aea562 | 1048 | cmd_rq->aborted = 1; |
c30341dc | 1049 | |
a4aea562 | 1050 | dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag, |
c30341dc | 1051 | nvmeq->qid); |
a4aea562 MB |
1052 | if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) { |
1053 | dev_warn(nvmeq->q_dmadev, | |
1054 | "Could not abort I/O %d QID %d", | |
1055 | req->tag, nvmeq->qid); | |
c87fd540 | 1056 | blk_mq_free_request(abort_req); |
a4aea562 | 1057 | } |
c30341dc KB |
1058 | } |
1059 | ||
a4aea562 MB |
1060 | static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx, |
1061 | struct request *req, void *data, bool reserved) | |
a09115b2 | 1062 | { |
a4aea562 MB |
1063 | struct nvme_queue *nvmeq = data; |
1064 | void *ctx; | |
1065 | nvme_completion_fn fn; | |
1066 | struct nvme_cmd_info *cmd; | |
1067 | static struct nvme_completion cqe = { | |
1068 | .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1), | |
1069 | }; | |
a09115b2 | 1070 | |
a4aea562 | 1071 | cmd = blk_mq_rq_to_pdu(req); |
a09115b2 | 1072 | |
a4aea562 MB |
1073 | if (cmd->ctx == CMD_CTX_CANCELLED) |
1074 | return; | |
1075 | ||
1076 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", | |
1077 | req->tag, nvmeq->qid); | |
1078 | ctx = cancel_cmd_info(cmd, &fn); | |
1079 | fn(nvmeq, ctx, &cqe); | |
a09115b2 MW |
1080 | } |
1081 | ||
a4aea562 | 1082 | static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) |
9e866774 | 1083 | { |
a4aea562 MB |
1084 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
1085 | struct nvme_queue *nvmeq = cmd->nvmeq; | |
1086 | ||
1087 | dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag, | |
1088 | nvmeq->qid); | |
1089 | if (nvmeq->dev->initialized) | |
1090 | nvme_abort_req(req); | |
1091 | ||
1092 | /* | |
1093 | * The aborted req will be completed on receiving the abort req. | |
1094 | * We enable the timer again. If hit twice, it'll cause a device reset, | |
1095 | * as the device then is in a faulty state. | |
1096 | */ | |
1097 | return BLK_EH_RESET_TIMER; | |
1098 | } | |
22404274 | 1099 | |
a4aea562 MB |
1100 | static void nvme_free_queue(struct nvme_queue *nvmeq) |
1101 | { | |
9e866774 MW |
1102 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
1103 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); | |
1104 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), | |
1105 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); | |
1106 | kfree(nvmeq); | |
1107 | } | |
1108 | ||
a1a5ef99 | 1109 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
22404274 | 1110 | { |
f435c282 KB |
1111 | LLIST_HEAD(q_list); |
1112 | struct nvme_queue *nvmeq, *next; | |
1113 | struct llist_node *entry; | |
22404274 KB |
1114 | int i; |
1115 | ||
a1a5ef99 | 1116 | for (i = dev->queue_count - 1; i >= lowest; i--) { |
a4aea562 | 1117 | struct nvme_queue *nvmeq = dev->queues[i]; |
f435c282 | 1118 | llist_add(&nvmeq->node, &q_list); |
22404274 | 1119 | dev->queue_count--; |
a4aea562 | 1120 | dev->queues[i] = NULL; |
22404274 | 1121 | } |
f435c282 KB |
1122 | synchronize_rcu(); |
1123 | entry = llist_del_all(&q_list); | |
1124 | llist_for_each_entry_safe(nvmeq, next, entry, node) | |
1125 | nvme_free_queue(nvmeq); | |
22404274 KB |
1126 | } |
1127 | ||
4d115420 KB |
1128 | /** |
1129 | * nvme_suspend_queue - put queue into suspended state | |
1130 | * @nvmeq - queue to suspend | |
4d115420 KB |
1131 | */ |
1132 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) | |
b60503ba | 1133 | { |
2b25d981 | 1134 | int vector; |
b60503ba | 1135 | |
a09115b2 | 1136 | spin_lock_irq(&nvmeq->q_lock); |
2b25d981 KB |
1137 | if (nvmeq->cq_vector == -1) { |
1138 | spin_unlock_irq(&nvmeq->q_lock); | |
1139 | return 1; | |
1140 | } | |
1141 | vector = nvmeq->dev->entry[nvmeq->cq_vector].vector; | |
42f61420 | 1142 | nvmeq->dev->online_queues--; |
2b25d981 | 1143 | nvmeq->cq_vector = -1; |
a09115b2 MW |
1144 | spin_unlock_irq(&nvmeq->q_lock); |
1145 | ||
aba2080f MW |
1146 | irq_set_affinity_hint(vector, NULL); |
1147 | free_irq(vector, nvmeq); | |
b60503ba | 1148 | |
4d115420 KB |
1149 | return 0; |
1150 | } | |
b60503ba | 1151 | |
4d115420 KB |
1152 | static void nvme_clear_queue(struct nvme_queue *nvmeq) |
1153 | { | |
a4aea562 MB |
1154 | struct blk_mq_hw_ctx *hctx = nvmeq->hctx; |
1155 | ||
22404274 KB |
1156 | spin_lock_irq(&nvmeq->q_lock); |
1157 | nvme_process_cq(nvmeq); | |
a4aea562 MB |
1158 | if (hctx && hctx->tags) |
1159 | blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq); | |
22404274 | 1160 | spin_unlock_irq(&nvmeq->q_lock); |
b60503ba MW |
1161 | } |
1162 | ||
4d115420 KB |
1163 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) |
1164 | { | |
a4aea562 | 1165 | struct nvme_queue *nvmeq = dev->queues[qid]; |
4d115420 KB |
1166 | |
1167 | if (!nvmeq) | |
1168 | return; | |
1169 | if (nvme_suspend_queue(nvmeq)) | |
1170 | return; | |
1171 | ||
0e53d180 KB |
1172 | /* Don't tell the adapter to delete the admin queue. |
1173 | * Don't tell a removed adapter to delete IO queues. */ | |
1174 | if (qid && readl(&dev->bar->csts) != -1) { | |
b60503ba MW |
1175 | adapter_delete_sq(dev, qid); |
1176 | adapter_delete_cq(dev, qid); | |
1177 | } | |
4d115420 | 1178 | nvme_clear_queue(nvmeq); |
b60503ba MW |
1179 | } |
1180 | ||
1181 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, | |
2b25d981 | 1182 | int depth) |
b60503ba MW |
1183 | { |
1184 | struct device *dmadev = &dev->pci_dev->dev; | |
a4aea562 | 1185 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL); |
b60503ba MW |
1186 | if (!nvmeq) |
1187 | return NULL; | |
1188 | ||
4d51abf9 JP |
1189 | nvmeq->cqes = dma_zalloc_coherent(dmadev, CQ_SIZE(depth), |
1190 | &nvmeq->cq_dma_addr, GFP_KERNEL); | |
b60503ba MW |
1191 | if (!nvmeq->cqes) |
1192 | goto free_nvmeq; | |
b60503ba MW |
1193 | |
1194 | nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth), | |
1195 | &nvmeq->sq_dma_addr, GFP_KERNEL); | |
1196 | if (!nvmeq->sq_cmds) | |
1197 | goto free_cqdma; | |
1198 | ||
1199 | nvmeq->q_dmadev = dmadev; | |
091b6092 | 1200 | nvmeq->dev = dev; |
3193f07b MW |
1201 | snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d", |
1202 | dev->instance, qid); | |
b60503ba MW |
1203 | spin_lock_init(&nvmeq->q_lock); |
1204 | nvmeq->cq_head = 0; | |
82123460 | 1205 | nvmeq->cq_phase = 1; |
b80d5ccc | 1206 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
b60503ba | 1207 | nvmeq->q_depth = depth; |
c30341dc | 1208 | nvmeq->qid = qid; |
22404274 | 1209 | dev->queue_count++; |
a4aea562 | 1210 | dev->queues[qid] = nvmeq; |
b60503ba MW |
1211 | |
1212 | return nvmeq; | |
1213 | ||
1214 | free_cqdma: | |
68b8eca5 | 1215 | dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes, |
b60503ba MW |
1216 | nvmeq->cq_dma_addr); |
1217 | free_nvmeq: | |
1218 | kfree(nvmeq); | |
1219 | return NULL; | |
1220 | } | |
1221 | ||
3001082c MW |
1222 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
1223 | const char *name) | |
1224 | { | |
58ffacb5 MW |
1225 | if (use_threaded_interrupts) |
1226 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, | |
481e5bad | 1227 | nvme_irq_check, nvme_irq, IRQF_SHARED, |
58ffacb5 | 1228 | name, nvmeq); |
3001082c | 1229 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
481e5bad | 1230 | IRQF_SHARED, name, nvmeq); |
3001082c MW |
1231 | } |
1232 | ||
22404274 | 1233 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
b60503ba | 1234 | { |
22404274 | 1235 | struct nvme_dev *dev = nvmeq->dev; |
b60503ba | 1236 | |
7be50e93 | 1237 | spin_lock_irq(&nvmeq->q_lock); |
22404274 KB |
1238 | nvmeq->sq_tail = 0; |
1239 | nvmeq->cq_head = 0; | |
1240 | nvmeq->cq_phase = 1; | |
b80d5ccc | 1241 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
22404274 | 1242 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); |
42f61420 | 1243 | dev->online_queues++; |
7be50e93 | 1244 | spin_unlock_irq(&nvmeq->q_lock); |
22404274 KB |
1245 | } |
1246 | ||
1247 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) | |
1248 | { | |
1249 | struct nvme_dev *dev = nvmeq->dev; | |
1250 | int result; | |
3f85d50b | 1251 | |
2b25d981 | 1252 | nvmeq->cq_vector = qid - 1; |
b60503ba MW |
1253 | result = adapter_alloc_cq(dev, qid, nvmeq); |
1254 | if (result < 0) | |
22404274 | 1255 | return result; |
b60503ba MW |
1256 | |
1257 | result = adapter_alloc_sq(dev, qid, nvmeq); | |
1258 | if (result < 0) | |
1259 | goto release_cq; | |
1260 | ||
3193f07b | 1261 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
b60503ba MW |
1262 | if (result < 0) |
1263 | goto release_sq; | |
1264 | ||
22404274 | 1265 | nvme_init_queue(nvmeq, qid); |
22404274 | 1266 | return result; |
b60503ba MW |
1267 | |
1268 | release_sq: | |
1269 | adapter_delete_sq(dev, qid); | |
1270 | release_cq: | |
1271 | adapter_delete_cq(dev, qid); | |
22404274 | 1272 | return result; |
b60503ba MW |
1273 | } |
1274 | ||
ba47e386 MW |
1275 | static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled) |
1276 | { | |
1277 | unsigned long timeout; | |
1278 | u32 bit = enabled ? NVME_CSTS_RDY : 0; | |
1279 | ||
1280 | timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; | |
1281 | ||
1282 | while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) { | |
1283 | msleep(100); | |
1284 | if (fatal_signal_pending(current)) | |
1285 | return -EINTR; | |
1286 | if (time_after(jiffies, timeout)) { | |
1287 | dev_err(&dev->pci_dev->dev, | |
27e8166c MW |
1288 | "Device not ready; aborting %s\n", enabled ? |
1289 | "initialisation" : "reset"); | |
ba47e386 MW |
1290 | return -ENODEV; |
1291 | } | |
1292 | } | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | /* | |
1298 | * If the device has been passed off to us in an enabled state, just clear | |
1299 | * the enabled bit. The spec says we should set the 'shutdown notification | |
1300 | * bits', but doing so may cause the device to complete commands to the | |
1301 | * admin queue ... and we don't know what memory that might be pointing at! | |
1302 | */ | |
1303 | static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap) | |
1304 | { | |
01079522 DM |
1305 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
1306 | dev->ctrl_config &= ~NVME_CC_ENABLE; | |
1307 | writel(dev->ctrl_config, &dev->bar->cc); | |
44af146a | 1308 | |
ba47e386 MW |
1309 | return nvme_wait_ready(dev, cap, false); |
1310 | } | |
1311 | ||
1312 | static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap) | |
1313 | { | |
01079522 DM |
1314 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
1315 | dev->ctrl_config |= NVME_CC_ENABLE; | |
1316 | writel(dev->ctrl_config, &dev->bar->cc); | |
1317 | ||
ba47e386 MW |
1318 | return nvme_wait_ready(dev, cap, true); |
1319 | } | |
1320 | ||
1894d8f1 KB |
1321 | static int nvme_shutdown_ctrl(struct nvme_dev *dev) |
1322 | { | |
1323 | unsigned long timeout; | |
1894d8f1 | 1324 | |
01079522 DM |
1325 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
1326 | dev->ctrl_config |= NVME_CC_SHN_NORMAL; | |
1327 | ||
1328 | writel(dev->ctrl_config, &dev->bar->cc); | |
1894d8f1 | 1329 | |
2484f407 | 1330 | timeout = SHUTDOWN_TIMEOUT + jiffies; |
1894d8f1 KB |
1331 | while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) != |
1332 | NVME_CSTS_SHST_CMPLT) { | |
1333 | msleep(100); | |
1334 | if (fatal_signal_pending(current)) | |
1335 | return -EINTR; | |
1336 | if (time_after(jiffies, timeout)) { | |
1337 | dev_err(&dev->pci_dev->dev, | |
1338 | "Device shutdown incomplete; abort shutdown\n"); | |
1339 | return -ENODEV; | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
a4aea562 MB |
1346 | static struct blk_mq_ops nvme_mq_admin_ops = { |
1347 | .queue_rq = nvme_admin_queue_rq, | |
1348 | .map_queue = blk_mq_map_queue, | |
1349 | .init_hctx = nvme_admin_init_hctx, | |
2c30540b | 1350 | .exit_hctx = nvme_exit_hctx, |
a4aea562 MB |
1351 | .init_request = nvme_admin_init_request, |
1352 | .timeout = nvme_timeout, | |
1353 | }; | |
1354 | ||
1355 | static struct blk_mq_ops nvme_mq_ops = { | |
1356 | .queue_rq = nvme_queue_rq, | |
1357 | .map_queue = blk_mq_map_queue, | |
1358 | .init_hctx = nvme_init_hctx, | |
2c30540b | 1359 | .exit_hctx = nvme_exit_hctx, |
a4aea562 MB |
1360 | .init_request = nvme_init_request, |
1361 | .timeout = nvme_timeout, | |
1362 | }; | |
1363 | ||
1364 | static int nvme_alloc_admin_tags(struct nvme_dev *dev) | |
1365 | { | |
1366 | if (!dev->admin_q) { | |
1367 | dev->admin_tagset.ops = &nvme_mq_admin_ops; | |
1368 | dev->admin_tagset.nr_hw_queues = 1; | |
1369 | dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1; | |
1370 | dev->admin_tagset.timeout = ADMIN_TIMEOUT; | |
1371 | dev->admin_tagset.numa_node = dev_to_node(&dev->pci_dev->dev); | |
1372 | dev->admin_tagset.cmd_size = sizeof(struct nvme_cmd_info); | |
1373 | dev->admin_tagset.driver_data = dev; | |
1374 | ||
1375 | if (blk_mq_alloc_tag_set(&dev->admin_tagset)) | |
1376 | return -ENOMEM; | |
1377 | ||
1378 | dev->admin_q = blk_mq_init_queue(&dev->admin_tagset); | |
35b489d3 | 1379 | if (IS_ERR(dev->admin_q)) { |
a4aea562 MB |
1380 | blk_mq_free_tag_set(&dev->admin_tagset); |
1381 | return -ENOMEM; | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | return 0; | |
1386 | } | |
1387 | ||
1388 | static void nvme_free_admin_tags(struct nvme_dev *dev) | |
1389 | { | |
1390 | if (dev->admin_q) | |
1391 | blk_mq_free_tag_set(&dev->admin_tagset); | |
1392 | } | |
1393 | ||
8d85fce7 | 1394 | static int nvme_configure_admin_queue(struct nvme_dev *dev) |
b60503ba | 1395 | { |
ba47e386 | 1396 | int result; |
b60503ba | 1397 | u32 aqa; |
ba47e386 | 1398 | u64 cap = readq(&dev->bar->cap); |
b60503ba | 1399 | struct nvme_queue *nvmeq; |
1d090624 KB |
1400 | unsigned page_shift = PAGE_SHIFT; |
1401 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12; | |
1402 | unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12; | |
1403 | ||
1404 | if (page_shift < dev_page_min) { | |
1405 | dev_err(&dev->pci_dev->dev, | |
1406 | "Minimum device page size (%u) too large for " | |
1407 | "host (%u)\n", 1 << dev_page_min, | |
1408 | 1 << page_shift); | |
1409 | return -ENODEV; | |
1410 | } | |
1411 | if (page_shift > dev_page_max) { | |
1412 | dev_info(&dev->pci_dev->dev, | |
1413 | "Device maximum page size (%u) smaller than " | |
1414 | "host (%u); enabling work-around\n", | |
1415 | 1 << dev_page_max, 1 << page_shift); | |
1416 | page_shift = dev_page_max; | |
1417 | } | |
b60503ba | 1418 | |
ba47e386 MW |
1419 | result = nvme_disable_ctrl(dev, cap); |
1420 | if (result < 0) | |
1421 | return result; | |
b60503ba | 1422 | |
a4aea562 | 1423 | nvmeq = dev->queues[0]; |
cd638946 | 1424 | if (!nvmeq) { |
2b25d981 | 1425 | nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); |
cd638946 KB |
1426 | if (!nvmeq) |
1427 | return -ENOMEM; | |
cd638946 | 1428 | } |
b60503ba MW |
1429 | |
1430 | aqa = nvmeq->q_depth - 1; | |
1431 | aqa |= aqa << 16; | |
1432 | ||
1d090624 KB |
1433 | dev->page_size = 1 << page_shift; |
1434 | ||
01079522 | 1435 | dev->ctrl_config = NVME_CC_CSS_NVM; |
1d090624 | 1436 | dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
b60503ba | 1437 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
7f53f9d2 | 1438 | dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
b60503ba MW |
1439 | |
1440 | writel(aqa, &dev->bar->aqa); | |
1441 | writeq(nvmeq->sq_dma_addr, &dev->bar->asq); | |
1442 | writeq(nvmeq->cq_dma_addr, &dev->bar->acq); | |
b60503ba | 1443 | |
ba47e386 | 1444 | result = nvme_enable_ctrl(dev, cap); |
025c557a | 1445 | if (result) |
a4aea562 MB |
1446 | goto free_nvmeq; |
1447 | ||
1448 | result = nvme_alloc_admin_tags(dev); | |
1449 | if (result) | |
1450 | goto free_nvmeq; | |
9e866774 | 1451 | |
2b25d981 | 1452 | nvmeq->cq_vector = 0; |
3193f07b | 1453 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
025c557a | 1454 | if (result) |
a4aea562 | 1455 | goto free_tags; |
025c557a | 1456 | |
b60503ba | 1457 | return result; |
a4aea562 MB |
1458 | |
1459 | free_tags: | |
1460 | nvme_free_admin_tags(dev); | |
1461 | free_nvmeq: | |
1462 | nvme_free_queues(dev, 0); | |
1463 | return result; | |
b60503ba MW |
1464 | } |
1465 | ||
5d0f6131 | 1466 | struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write, |
eca18b23 | 1467 | unsigned long addr, unsigned length) |
b60503ba | 1468 | { |
36c14ed9 | 1469 | int i, err, count, nents, offset; |
7fc3cdab MW |
1470 | struct scatterlist *sg; |
1471 | struct page **pages; | |
eca18b23 | 1472 | struct nvme_iod *iod; |
36c14ed9 MW |
1473 | |
1474 | if (addr & 3) | |
eca18b23 | 1475 | return ERR_PTR(-EINVAL); |
5460fc03 | 1476 | if (!length || length > INT_MAX - PAGE_SIZE) |
eca18b23 | 1477 | return ERR_PTR(-EINVAL); |
7fc3cdab | 1478 | |
36c14ed9 | 1479 | offset = offset_in_page(addr); |
7fc3cdab MW |
1480 | count = DIV_ROUND_UP(offset + length, PAGE_SIZE); |
1481 | pages = kcalloc(count, sizeof(*pages), GFP_KERNEL); | |
22fff826 DC |
1482 | if (!pages) |
1483 | return ERR_PTR(-ENOMEM); | |
36c14ed9 MW |
1484 | |
1485 | err = get_user_pages_fast(addr, count, 1, pages); | |
1486 | if (err < count) { | |
1487 | count = err; | |
1488 | err = -EFAULT; | |
1489 | goto put_pages; | |
1490 | } | |
7fc3cdab | 1491 | |
6808c5fb | 1492 | err = -ENOMEM; |
1d090624 | 1493 | iod = nvme_alloc_iod(count, length, dev, GFP_KERNEL); |
6808c5fb S |
1494 | if (!iod) |
1495 | goto put_pages; | |
1496 | ||
eca18b23 | 1497 | sg = iod->sg; |
36c14ed9 | 1498 | sg_init_table(sg, count); |
d0ba1e49 MW |
1499 | for (i = 0; i < count; i++) { |
1500 | sg_set_page(&sg[i], pages[i], | |
5460fc03 DC |
1501 | min_t(unsigned, length, PAGE_SIZE - offset), |
1502 | offset); | |
d0ba1e49 MW |
1503 | length -= (PAGE_SIZE - offset); |
1504 | offset = 0; | |
7fc3cdab | 1505 | } |
fe304c43 | 1506 | sg_mark_end(&sg[i - 1]); |
1c2ad9fa | 1507 | iod->nents = count; |
7fc3cdab | 1508 | |
7fc3cdab MW |
1509 | nents = dma_map_sg(&dev->pci_dev->dev, sg, count, |
1510 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
36c14ed9 | 1511 | if (!nents) |
eca18b23 | 1512 | goto free_iod; |
b60503ba | 1513 | |
7fc3cdab | 1514 | kfree(pages); |
eca18b23 | 1515 | return iod; |
b60503ba | 1516 | |
eca18b23 MW |
1517 | free_iod: |
1518 | kfree(iod); | |
7fc3cdab MW |
1519 | put_pages: |
1520 | for (i = 0; i < count; i++) | |
1521 | put_page(pages[i]); | |
1522 | kfree(pages); | |
eca18b23 | 1523 | return ERR_PTR(err); |
7fc3cdab | 1524 | } |
b60503ba | 1525 | |
5d0f6131 | 1526 | void nvme_unmap_user_pages(struct nvme_dev *dev, int write, |
1c2ad9fa | 1527 | struct nvme_iod *iod) |
7fc3cdab | 1528 | { |
1c2ad9fa | 1529 | int i; |
b60503ba | 1530 | |
1c2ad9fa MW |
1531 | dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents, |
1532 | write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
7fc3cdab | 1533 | |
1c2ad9fa MW |
1534 | for (i = 0; i < iod->nents; i++) |
1535 | put_page(sg_page(&iod->sg[i])); | |
7fc3cdab | 1536 | } |
b60503ba | 1537 | |
a53295b6 MW |
1538 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
1539 | { | |
1540 | struct nvme_dev *dev = ns->dev; | |
a53295b6 MW |
1541 | struct nvme_user_io io; |
1542 | struct nvme_command c; | |
f410c680 KB |
1543 | unsigned length, meta_len; |
1544 | int status, i; | |
1545 | struct nvme_iod *iod, *meta_iod = NULL; | |
1546 | dma_addr_t meta_dma_addr; | |
1547 | void *meta, *uninitialized_var(meta_mem); | |
a53295b6 MW |
1548 | |
1549 | if (copy_from_user(&io, uio, sizeof(io))) | |
1550 | return -EFAULT; | |
6c7d4945 | 1551 | length = (io.nblocks + 1) << ns->lba_shift; |
f410c680 KB |
1552 | meta_len = (io.nblocks + 1) * ns->ms; |
1553 | ||
1554 | if (meta_len && ((io.metadata & 3) || !io.metadata)) | |
1555 | return -EINVAL; | |
6c7d4945 MW |
1556 | |
1557 | switch (io.opcode) { | |
1558 | case nvme_cmd_write: | |
1559 | case nvme_cmd_read: | |
6bbf1acd | 1560 | case nvme_cmd_compare: |
eca18b23 | 1561 | iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length); |
6413214c | 1562 | break; |
6c7d4945 | 1563 | default: |
6bbf1acd | 1564 | return -EINVAL; |
6c7d4945 MW |
1565 | } |
1566 | ||
eca18b23 MW |
1567 | if (IS_ERR(iod)) |
1568 | return PTR_ERR(iod); | |
a53295b6 MW |
1569 | |
1570 | memset(&c, 0, sizeof(c)); | |
1571 | c.rw.opcode = io.opcode; | |
1572 | c.rw.flags = io.flags; | |
6c7d4945 | 1573 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
a53295b6 | 1574 | c.rw.slba = cpu_to_le64(io.slba); |
6c7d4945 | 1575 | c.rw.length = cpu_to_le16(io.nblocks); |
a53295b6 | 1576 | c.rw.control = cpu_to_le16(io.control); |
1c9b5265 MW |
1577 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
1578 | c.rw.reftag = cpu_to_le32(io.reftag); | |
1579 | c.rw.apptag = cpu_to_le16(io.apptag); | |
1580 | c.rw.appmask = cpu_to_le16(io.appmask); | |
f410c680 KB |
1581 | |
1582 | if (meta_len) { | |
1b56749e KB |
1583 | meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata, |
1584 | meta_len); | |
f410c680 KB |
1585 | if (IS_ERR(meta_iod)) { |
1586 | status = PTR_ERR(meta_iod); | |
1587 | meta_iod = NULL; | |
1588 | goto unmap; | |
1589 | } | |
1590 | ||
1591 | meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len, | |
1592 | &meta_dma_addr, GFP_KERNEL); | |
1593 | if (!meta_mem) { | |
1594 | status = -ENOMEM; | |
1595 | goto unmap; | |
1596 | } | |
1597 | ||
1598 | if (io.opcode & 1) { | |
1599 | int meta_offset = 0; | |
1600 | ||
1601 | for (i = 0; i < meta_iod->nents; i++) { | |
1602 | meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + | |
1603 | meta_iod->sg[i].offset; | |
1604 | memcpy(meta_mem + meta_offset, meta, | |
1605 | meta_iod->sg[i].length); | |
1606 | kunmap_atomic(meta); | |
1607 | meta_offset += meta_iod->sg[i].length; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | c.rw.metadata = cpu_to_le64(meta_dma_addr); | |
1612 | } | |
1613 | ||
edd10d33 KB |
1614 | length = nvme_setup_prps(dev, iod, length, GFP_KERNEL); |
1615 | c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); | |
1616 | c.rw.prp2 = cpu_to_le64(iod->first_dma); | |
a53295b6 | 1617 | |
b77954cb MW |
1618 | if (length != (io.nblocks + 1) << ns->lba_shift) |
1619 | status = -ENOMEM; | |
1620 | else | |
a4aea562 | 1621 | status = nvme_submit_io_cmd(dev, ns, &c, NULL); |
a53295b6 | 1622 | |
f410c680 KB |
1623 | if (meta_len) { |
1624 | if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) { | |
1625 | int meta_offset = 0; | |
1626 | ||
1627 | for (i = 0; i < meta_iod->nents; i++) { | |
1628 | meta = kmap_atomic(sg_page(&meta_iod->sg[i])) + | |
1629 | meta_iod->sg[i].offset; | |
1630 | memcpy(meta, meta_mem + meta_offset, | |
1631 | meta_iod->sg[i].length); | |
1632 | kunmap_atomic(meta); | |
1633 | meta_offset += meta_iod->sg[i].length; | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem, | |
1638 | meta_dma_addr); | |
1639 | } | |
1640 | ||
1641 | unmap: | |
1c2ad9fa | 1642 | nvme_unmap_user_pages(dev, io.opcode & 1, iod); |
eca18b23 | 1643 | nvme_free_iod(dev, iod); |
f410c680 KB |
1644 | |
1645 | if (meta_iod) { | |
1646 | nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod); | |
1647 | nvme_free_iod(dev, meta_iod); | |
1648 | } | |
1649 | ||
a53295b6 MW |
1650 | return status; |
1651 | } | |
1652 | ||
a4aea562 MB |
1653 | static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns, |
1654 | struct nvme_passthru_cmd __user *ucmd) | |
6ee44cdc | 1655 | { |
7963e521 | 1656 | struct nvme_passthru_cmd cmd; |
6ee44cdc | 1657 | struct nvme_command c; |
eca18b23 | 1658 | int status, length; |
c7d36ab8 | 1659 | struct nvme_iod *uninitialized_var(iod); |
94f370ca | 1660 | unsigned timeout; |
6ee44cdc | 1661 | |
6bbf1acd MW |
1662 | if (!capable(CAP_SYS_ADMIN)) |
1663 | return -EACCES; | |
1664 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) | |
6ee44cdc | 1665 | return -EFAULT; |
6ee44cdc MW |
1666 | |
1667 | memset(&c, 0, sizeof(c)); | |
6bbf1acd MW |
1668 | c.common.opcode = cmd.opcode; |
1669 | c.common.flags = cmd.flags; | |
1670 | c.common.nsid = cpu_to_le32(cmd.nsid); | |
1671 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); | |
1672 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); | |
1673 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); | |
1674 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); | |
1675 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); | |
1676 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); | |
1677 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); | |
1678 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); | |
1679 | ||
1680 | length = cmd.data_len; | |
1681 | if (cmd.data_len) { | |
49742188 MW |
1682 | iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr, |
1683 | length); | |
eca18b23 MW |
1684 | if (IS_ERR(iod)) |
1685 | return PTR_ERR(iod); | |
edd10d33 KB |
1686 | length = nvme_setup_prps(dev, iod, length, GFP_KERNEL); |
1687 | c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); | |
1688 | c.common.prp2 = cpu_to_le64(iod->first_dma); | |
6bbf1acd MW |
1689 | } |
1690 | ||
94f370ca KB |
1691 | timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) : |
1692 | ADMIN_TIMEOUT; | |
a4aea562 | 1693 | |
6bbf1acd | 1694 | if (length != cmd.data_len) |
b77954cb | 1695 | status = -ENOMEM; |
a4aea562 MB |
1696 | else if (ns) { |
1697 | struct request *req; | |
1698 | ||
1699 | req = blk_mq_alloc_request(ns->queue, WRITE, | |
1700 | (GFP_KERNEL|__GFP_WAIT), false); | |
97fe3832 JA |
1701 | if (IS_ERR(req)) |
1702 | status = PTR_ERR(req); | |
a4aea562 MB |
1703 | else { |
1704 | status = nvme_submit_sync_cmd(req, &c, &cmd.result, | |
1705 | timeout); | |
9d135bb8 | 1706 | blk_mq_free_request(req); |
a4aea562 MB |
1707 | } |
1708 | } else | |
1709 | status = __nvme_submit_admin_cmd(dev, &c, &cmd.result, timeout); | |
eca18b23 | 1710 | |
6bbf1acd | 1711 | if (cmd.data_len) { |
1c2ad9fa | 1712 | nvme_unmap_user_pages(dev, cmd.opcode & 1, iod); |
eca18b23 | 1713 | nvme_free_iod(dev, iod); |
6bbf1acd | 1714 | } |
f4f117f6 | 1715 | |
cf90bc48 | 1716 | if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result, |
f4f117f6 KB |
1717 | sizeof(cmd.result))) |
1718 | status = -EFAULT; | |
1719 | ||
6ee44cdc MW |
1720 | return status; |
1721 | } | |
1722 | ||
b60503ba MW |
1723 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, |
1724 | unsigned long arg) | |
1725 | { | |
1726 | struct nvme_ns *ns = bdev->bd_disk->private_data; | |
1727 | ||
1728 | switch (cmd) { | |
6bbf1acd | 1729 | case NVME_IOCTL_ID: |
c3bfe717 | 1730 | force_successful_syscall_return(); |
6bbf1acd MW |
1731 | return ns->ns_id; |
1732 | case NVME_IOCTL_ADMIN_CMD: | |
a4aea562 | 1733 | return nvme_user_cmd(ns->dev, NULL, (void __user *)arg); |
7963e521 | 1734 | case NVME_IOCTL_IO_CMD: |
a4aea562 | 1735 | return nvme_user_cmd(ns->dev, ns, (void __user *)arg); |
a53295b6 MW |
1736 | case NVME_IOCTL_SUBMIT_IO: |
1737 | return nvme_submit_io(ns, (void __user *)arg); | |
5d0f6131 VV |
1738 | case SG_GET_VERSION_NUM: |
1739 | return nvme_sg_get_version_num((void __user *)arg); | |
1740 | case SG_IO: | |
1741 | return nvme_sg_io(ns, (void __user *)arg); | |
b60503ba MW |
1742 | default: |
1743 | return -ENOTTY; | |
1744 | } | |
1745 | } | |
1746 | ||
320a3827 KB |
1747 | #ifdef CONFIG_COMPAT |
1748 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
1749 | unsigned int cmd, unsigned long arg) | |
1750 | { | |
320a3827 KB |
1751 | switch (cmd) { |
1752 | case SG_IO: | |
e179729a | 1753 | return -ENOIOCTLCMD; |
320a3827 KB |
1754 | } |
1755 | return nvme_ioctl(bdev, mode, cmd, arg); | |
1756 | } | |
1757 | #else | |
1758 | #define nvme_compat_ioctl NULL | |
1759 | #endif | |
1760 | ||
9ac27090 KB |
1761 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
1762 | { | |
9e60352c KB |
1763 | int ret = 0; |
1764 | struct nvme_ns *ns; | |
9ac27090 | 1765 | |
9e60352c KB |
1766 | spin_lock(&dev_list_lock); |
1767 | ns = bdev->bd_disk->private_data; | |
1768 | if (!ns) | |
1769 | ret = -ENXIO; | |
1770 | else if (!kref_get_unless_zero(&ns->dev->kref)) | |
1771 | ret = -ENXIO; | |
1772 | spin_unlock(&dev_list_lock); | |
1773 | ||
1774 | return ret; | |
9ac27090 KB |
1775 | } |
1776 | ||
1777 | static void nvme_free_dev(struct kref *kref); | |
1778 | ||
1779 | static void nvme_release(struct gendisk *disk, fmode_t mode) | |
1780 | { | |
1781 | struct nvme_ns *ns = disk->private_data; | |
1782 | struct nvme_dev *dev = ns->dev; | |
1783 | ||
1784 | kref_put(&dev->kref, nvme_free_dev); | |
1785 | } | |
1786 | ||
4cc09e2d KB |
1787 | static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo) |
1788 | { | |
1789 | /* some standard values */ | |
1790 | geo->heads = 1 << 6; | |
1791 | geo->sectors = 1 << 5; | |
1792 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; | |
1793 | return 0; | |
1794 | } | |
1795 | ||
1b9dbf7f KB |
1796 | static int nvme_revalidate_disk(struct gendisk *disk) |
1797 | { | |
1798 | struct nvme_ns *ns = disk->private_data; | |
1799 | struct nvme_dev *dev = ns->dev; | |
1800 | struct nvme_id_ns *id; | |
1801 | dma_addr_t dma_addr; | |
1802 | int lbaf; | |
1803 | ||
1804 | id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr, | |
1805 | GFP_KERNEL); | |
1806 | if (!id) { | |
1807 | dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n", | |
1808 | __func__); | |
1809 | return 0; | |
1810 | } | |
1811 | ||
1812 | if (nvme_identify(dev, ns->ns_id, 0, dma_addr)) | |
1813 | goto free; | |
1814 | ||
1815 | lbaf = id->flbas & 0xf; | |
1816 | ns->lba_shift = id->lbaf[lbaf].ds; | |
1817 | ||
1818 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); | |
1819 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); | |
1820 | free: | |
1821 | dma_free_coherent(&dev->pci_dev->dev, 4096, id, dma_addr); | |
1822 | return 0; | |
1823 | } | |
1824 | ||
b60503ba MW |
1825 | static const struct block_device_operations nvme_fops = { |
1826 | .owner = THIS_MODULE, | |
1827 | .ioctl = nvme_ioctl, | |
320a3827 | 1828 | .compat_ioctl = nvme_compat_ioctl, |
9ac27090 KB |
1829 | .open = nvme_open, |
1830 | .release = nvme_release, | |
4cc09e2d | 1831 | .getgeo = nvme_getgeo, |
1b9dbf7f | 1832 | .revalidate_disk= nvme_revalidate_disk, |
b60503ba MW |
1833 | }; |
1834 | ||
1fa6aead MW |
1835 | static int nvme_kthread(void *data) |
1836 | { | |
d4b4ff8e | 1837 | struct nvme_dev *dev, *next; |
1fa6aead MW |
1838 | |
1839 | while (!kthread_should_stop()) { | |
564a232c | 1840 | set_current_state(TASK_INTERRUPTIBLE); |
1fa6aead | 1841 | spin_lock(&dev_list_lock); |
d4b4ff8e | 1842 | list_for_each_entry_safe(dev, next, &dev_list, node) { |
1fa6aead | 1843 | int i; |
d4b4ff8e KB |
1844 | if (readl(&dev->bar->csts) & NVME_CSTS_CFS && |
1845 | dev->initialized) { | |
1846 | if (work_busy(&dev->reset_work)) | |
1847 | continue; | |
1848 | list_del_init(&dev->node); | |
1849 | dev_warn(&dev->pci_dev->dev, | |
a4aea562 MB |
1850 | "Failed status: %x, reset controller\n", |
1851 | readl(&dev->bar->csts)); | |
9ca97374 | 1852 | dev->reset_workfn = nvme_reset_failed_dev; |
d4b4ff8e KB |
1853 | queue_work(nvme_workq, &dev->reset_work); |
1854 | continue; | |
1855 | } | |
1fa6aead | 1856 | for (i = 0; i < dev->queue_count; i++) { |
a4aea562 | 1857 | struct nvme_queue *nvmeq = dev->queues[i]; |
740216fc MW |
1858 | if (!nvmeq) |
1859 | continue; | |
1fa6aead | 1860 | spin_lock_irq(&nvmeq->q_lock); |
bc57a0f7 | 1861 | nvme_process_cq(nvmeq); |
6fccf938 KB |
1862 | |
1863 | while ((i == 0) && (dev->event_limit > 0)) { | |
a4aea562 | 1864 | if (nvme_submit_async_admin_req(dev)) |
6fccf938 KB |
1865 | break; |
1866 | dev->event_limit--; | |
1867 | } | |
1fa6aead MW |
1868 | spin_unlock_irq(&nvmeq->q_lock); |
1869 | } | |
1870 | } | |
1871 | spin_unlock(&dev_list_lock); | |
acb7aa0d | 1872 | schedule_timeout(round_jiffies_relative(HZ)); |
1fa6aead MW |
1873 | } |
1874 | return 0; | |
1875 | } | |
1876 | ||
0e5e4f0e KB |
1877 | static void nvme_config_discard(struct nvme_ns *ns) |
1878 | { | |
1879 | u32 logical_block_size = queue_logical_block_size(ns->queue); | |
1880 | ns->queue->limits.discard_zeroes_data = 0; | |
1881 | ns->queue->limits.discard_alignment = logical_block_size; | |
1882 | ns->queue->limits.discard_granularity = logical_block_size; | |
1883 | ns->queue->limits.max_discard_sectors = 0xffffffff; | |
1884 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); | |
1885 | } | |
1886 | ||
c3bfe717 | 1887 | static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, |
b60503ba MW |
1888 | struct nvme_id_ns *id, struct nvme_lba_range_type *rt) |
1889 | { | |
1890 | struct nvme_ns *ns; | |
1891 | struct gendisk *disk; | |
a4aea562 | 1892 | int node = dev_to_node(&dev->pci_dev->dev); |
b60503ba MW |
1893 | int lbaf; |
1894 | ||
1895 | if (rt->attributes & NVME_LBART_ATTRIB_HIDE) | |
1896 | return NULL; | |
1897 | ||
a4aea562 | 1898 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
b60503ba MW |
1899 | if (!ns) |
1900 | return NULL; | |
a4aea562 | 1901 | ns->queue = blk_mq_init_queue(&dev->tagset); |
9f173b33 | 1902 | if (IS_ERR(ns->queue)) |
b60503ba | 1903 | goto out_free_ns; |
4eeb9215 MW |
1904 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); |
1905 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); | |
a4aea562 | 1906 | queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue); |
b60503ba MW |
1907 | ns->dev = dev; |
1908 | ns->queue->queuedata = ns; | |
1909 | ||
a4aea562 | 1910 | disk = alloc_disk_node(0, node); |
b60503ba MW |
1911 | if (!disk) |
1912 | goto out_free_queue; | |
a4aea562 | 1913 | |
5aff9382 | 1914 | ns->ns_id = nsid; |
b60503ba MW |
1915 | ns->disk = disk; |
1916 | lbaf = id->flbas & 0xf; | |
1917 | ns->lba_shift = id->lbaf[lbaf].ds; | |
f410c680 | 1918 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); |
e9ef4636 | 1919 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
8fc23e03 KB |
1920 | if (dev->max_hw_sectors) |
1921 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); | |
a4aea562 MB |
1922 | if (dev->stripe_size) |
1923 | blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9); | |
a7d2ce28 KB |
1924 | if (dev->vwc & NVME_CTRL_VWC_PRESENT) |
1925 | blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA); | |
b60503ba MW |
1926 | |
1927 | disk->major = nvme_major; | |
469071a3 | 1928 | disk->first_minor = 0; |
b60503ba MW |
1929 | disk->fops = &nvme_fops; |
1930 | disk->private_data = ns; | |
1931 | disk->queue = ns->queue; | |
388f037f | 1932 | disk->driverfs_dev = &dev->pci_dev->dev; |
469071a3 | 1933 | disk->flags = GENHD_FL_EXT_DEVT; |
5aff9382 | 1934 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); |
b60503ba MW |
1935 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
1936 | ||
0e5e4f0e KB |
1937 | if (dev->oncs & NVME_CTRL_ONCS_DSM) |
1938 | nvme_config_discard(ns); | |
1939 | ||
b60503ba MW |
1940 | return ns; |
1941 | ||
1942 | out_free_queue: | |
1943 | blk_cleanup_queue(ns->queue); | |
1944 | out_free_ns: | |
1945 | kfree(ns); | |
1946 | return NULL; | |
1947 | } | |
1948 | ||
42f61420 KB |
1949 | static void nvme_create_io_queues(struct nvme_dev *dev) |
1950 | { | |
a4aea562 | 1951 | unsigned i; |
42f61420 | 1952 | |
a4aea562 | 1953 | for (i = dev->queue_count; i <= dev->max_qid; i++) |
2b25d981 | 1954 | if (!nvme_alloc_queue(dev, i, dev->q_depth)) |
42f61420 KB |
1955 | break; |
1956 | ||
a4aea562 MB |
1957 | for (i = dev->online_queues; i <= dev->queue_count - 1; i++) |
1958 | if (nvme_create_queue(dev->queues[i], i)) | |
42f61420 KB |
1959 | break; |
1960 | } | |
1961 | ||
b3b06812 | 1962 | static int set_queue_count(struct nvme_dev *dev, int count) |
b60503ba MW |
1963 | { |
1964 | int status; | |
1965 | u32 result; | |
b3b06812 | 1966 | u32 q_count = (count - 1) | ((count - 1) << 16); |
b60503ba | 1967 | |
df348139 | 1968 | status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, |
bc5fc7e4 | 1969 | &result); |
27e8166c MW |
1970 | if (status < 0) |
1971 | return status; | |
1972 | if (status > 0) { | |
1973 | dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n", | |
1974 | status); | |
badc34d4 | 1975 | return 0; |
27e8166c | 1976 | } |
b60503ba MW |
1977 | return min(result & 0xffff, result >> 16) + 1; |
1978 | } | |
1979 | ||
9d713c2b KB |
1980 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
1981 | { | |
b80d5ccc | 1982 | return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride); |
9d713c2b KB |
1983 | } |
1984 | ||
8d85fce7 | 1985 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
b60503ba | 1986 | { |
a4aea562 | 1987 | struct nvme_queue *adminq = dev->queues[0]; |
fa08a396 | 1988 | struct pci_dev *pdev = dev->pci_dev; |
42f61420 | 1989 | int result, i, vecs, nr_io_queues, size; |
b60503ba | 1990 | |
42f61420 | 1991 | nr_io_queues = num_possible_cpus(); |
b348b7d5 | 1992 | result = set_queue_count(dev, nr_io_queues); |
badc34d4 | 1993 | if (result <= 0) |
1b23484b | 1994 | return result; |
b348b7d5 MW |
1995 | if (result < nr_io_queues) |
1996 | nr_io_queues = result; | |
b60503ba | 1997 | |
9d713c2b KB |
1998 | size = db_bar_size(dev, nr_io_queues); |
1999 | if (size > 8192) { | |
f1938f6e | 2000 | iounmap(dev->bar); |
9d713c2b KB |
2001 | do { |
2002 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); | |
2003 | if (dev->bar) | |
2004 | break; | |
2005 | if (!--nr_io_queues) | |
2006 | return -ENOMEM; | |
2007 | size = db_bar_size(dev, nr_io_queues); | |
2008 | } while (1); | |
f1938f6e | 2009 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
5a92e700 | 2010 | adminq->q_db = dev->dbs; |
f1938f6e MW |
2011 | } |
2012 | ||
9d713c2b | 2013 | /* Deregister the admin queue's interrupt */ |
3193f07b | 2014 | free_irq(dev->entry[0].vector, adminq); |
9d713c2b | 2015 | |
e32efbfc JA |
2016 | /* |
2017 | * If we enable msix early due to not intx, disable it again before | |
2018 | * setting up the full range we need. | |
2019 | */ | |
2020 | if (!pdev->irq) | |
2021 | pci_disable_msix(pdev); | |
2022 | ||
be577fab | 2023 | for (i = 0; i < nr_io_queues; i++) |
1b23484b | 2024 | dev->entry[i].entry = i; |
be577fab AG |
2025 | vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues); |
2026 | if (vecs < 0) { | |
2027 | vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32)); | |
2028 | if (vecs < 0) { | |
2029 | vecs = 1; | |
2030 | } else { | |
2031 | for (i = 0; i < vecs; i++) | |
2032 | dev->entry[i].vector = i + pdev->irq; | |
fa08a396 RRG |
2033 | } |
2034 | } | |
2035 | ||
063a8096 MW |
2036 | /* |
2037 | * Should investigate if there's a performance win from allocating | |
2038 | * more queues than interrupt vectors; it might allow the submission | |
2039 | * path to scale better, even if the receive path is limited by the | |
2040 | * number of interrupts. | |
2041 | */ | |
2042 | nr_io_queues = vecs; | |
42f61420 | 2043 | dev->max_qid = nr_io_queues; |
063a8096 | 2044 | |
3193f07b | 2045 | result = queue_request_irq(dev, adminq, adminq->irqname); |
a4aea562 | 2046 | if (result) |
22404274 | 2047 | goto free_queues; |
1b23484b | 2048 | |
cd638946 | 2049 | /* Free previously allocated queues that are no longer usable */ |
42f61420 | 2050 | nvme_free_queues(dev, nr_io_queues + 1); |
a4aea562 | 2051 | nvme_create_io_queues(dev); |
9ecdc946 | 2052 | |
22404274 | 2053 | return 0; |
b60503ba | 2054 | |
22404274 | 2055 | free_queues: |
a1a5ef99 | 2056 | nvme_free_queues(dev, 1); |
22404274 | 2057 | return result; |
b60503ba MW |
2058 | } |
2059 | ||
422ef0c7 MW |
2060 | /* |
2061 | * Return: error value if an error occurred setting up the queues or calling | |
2062 | * Identify Device. 0 if these succeeded, even if adding some of the | |
2063 | * namespaces failed. At the moment, these failures are silent. TBD which | |
2064 | * failures should be reported. | |
2065 | */ | |
8d85fce7 | 2066 | static int nvme_dev_add(struct nvme_dev *dev) |
b60503ba | 2067 | { |
68608c26 | 2068 | struct pci_dev *pdev = dev->pci_dev; |
c3bfe717 MW |
2069 | int res; |
2070 | unsigned nn, i; | |
cbb6218f | 2071 | struct nvme_ns *ns; |
51814232 | 2072 | struct nvme_id_ctrl *ctrl; |
bc5fc7e4 MW |
2073 | struct nvme_id_ns *id_ns; |
2074 | void *mem; | |
b60503ba | 2075 | dma_addr_t dma_addr; |
159b67d7 | 2076 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; |
b60503ba | 2077 | |
68608c26 | 2078 | mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL); |
a9ef4343 KB |
2079 | if (!mem) |
2080 | return -ENOMEM; | |
b60503ba | 2081 | |
bc5fc7e4 | 2082 | res = nvme_identify(dev, 0, 1, dma_addr); |
b60503ba | 2083 | if (res) { |
27e8166c | 2084 | dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res); |
b60503ba | 2085 | res = -EIO; |
cbb6218f | 2086 | goto out; |
b60503ba MW |
2087 | } |
2088 | ||
bc5fc7e4 | 2089 | ctrl = mem; |
51814232 | 2090 | nn = le32_to_cpup(&ctrl->nn); |
0e5e4f0e | 2091 | dev->oncs = le16_to_cpup(&ctrl->oncs); |
c30341dc | 2092 | dev->abort_limit = ctrl->acl + 1; |
a7d2ce28 | 2093 | dev->vwc = ctrl->vwc; |
6fccf938 | 2094 | dev->event_limit = min(ctrl->aerl + 1, 8); |
51814232 MW |
2095 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); |
2096 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); | |
2097 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); | |
159b67d7 | 2098 | if (ctrl->mdts) |
8fc23e03 | 2099 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); |
68608c26 | 2100 | if ((pdev->vendor == PCI_VENDOR_ID_INTEL) && |
a4aea562 MB |
2101 | (pdev->device == 0x0953) && ctrl->vs[3]) { |
2102 | unsigned int max_hw_sectors; | |
2103 | ||
159b67d7 | 2104 | dev->stripe_size = 1 << (ctrl->vs[3] + shift); |
a4aea562 MB |
2105 | max_hw_sectors = dev->stripe_size >> (shift - 9); |
2106 | if (dev->max_hw_sectors) { | |
2107 | dev->max_hw_sectors = min(max_hw_sectors, | |
2108 | dev->max_hw_sectors); | |
2109 | } else | |
2110 | dev->max_hw_sectors = max_hw_sectors; | |
2111 | } | |
2112 | ||
2113 | dev->tagset.ops = &nvme_mq_ops; | |
2114 | dev->tagset.nr_hw_queues = dev->online_queues - 1; | |
2115 | dev->tagset.timeout = NVME_IO_TIMEOUT; | |
2116 | dev->tagset.numa_node = dev_to_node(&dev->pci_dev->dev); | |
2117 | dev->tagset.queue_depth = | |
2118 | min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1; | |
2119 | dev->tagset.cmd_size = sizeof(struct nvme_cmd_info); | |
2120 | dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE; | |
2121 | dev->tagset.driver_data = dev; | |
2122 | ||
2123 | if (blk_mq_alloc_tag_set(&dev->tagset)) | |
2124 | goto out; | |
b60503ba | 2125 | |
bc5fc7e4 | 2126 | id_ns = mem; |
2b2c1896 | 2127 | for (i = 1; i <= nn; i++) { |
bc5fc7e4 | 2128 | res = nvme_identify(dev, i, 0, dma_addr); |
b60503ba MW |
2129 | if (res) |
2130 | continue; | |
2131 | ||
bc5fc7e4 | 2132 | if (id_ns->ncap == 0) |
b60503ba MW |
2133 | continue; |
2134 | ||
bc5fc7e4 | 2135 | res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i, |
08df1e05 | 2136 | dma_addr + 4096, NULL); |
b60503ba | 2137 | if (res) |
12209036 | 2138 | memset(mem + 4096, 0, 4096); |
b60503ba | 2139 | |
bc5fc7e4 | 2140 | ns = nvme_alloc_ns(dev, i, mem, mem + 4096); |
b60503ba MW |
2141 | if (ns) |
2142 | list_add_tail(&ns->list, &dev->namespaces); | |
2143 | } | |
2144 | list_for_each_entry(ns, &dev->namespaces, list) | |
2145 | add_disk(ns->disk); | |
422ef0c7 | 2146 | res = 0; |
b60503ba | 2147 | |
bc5fc7e4 | 2148 | out: |
684f5c20 | 2149 | dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr); |
b60503ba MW |
2150 | return res; |
2151 | } | |
2152 | ||
0877cb0d KB |
2153 | static int nvme_dev_map(struct nvme_dev *dev) |
2154 | { | |
42f61420 | 2155 | u64 cap; |
0877cb0d KB |
2156 | int bars, result = -ENOMEM; |
2157 | struct pci_dev *pdev = dev->pci_dev; | |
2158 | ||
2159 | if (pci_enable_device_mem(pdev)) | |
2160 | return result; | |
2161 | ||
2162 | dev->entry[0].vector = pdev->irq; | |
2163 | pci_set_master(pdev); | |
2164 | bars = pci_select_bars(pdev, IORESOURCE_MEM); | |
be7837e8 JA |
2165 | if (!bars) |
2166 | goto disable_pci; | |
2167 | ||
0877cb0d KB |
2168 | if (pci_request_selected_regions(pdev, bars, "nvme")) |
2169 | goto disable_pci; | |
2170 | ||
052d0efa RK |
2171 | if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) && |
2172 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) | |
2173 | goto disable; | |
0877cb0d | 2174 | |
0877cb0d KB |
2175 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
2176 | if (!dev->bar) | |
2177 | goto disable; | |
e32efbfc | 2178 | |
0e53d180 KB |
2179 | if (readl(&dev->bar->csts) == -1) { |
2180 | result = -ENODEV; | |
2181 | goto unmap; | |
2182 | } | |
e32efbfc JA |
2183 | |
2184 | /* | |
2185 | * Some devices don't advertse INTx interrupts, pre-enable a single | |
2186 | * MSIX vec for setup. We'll adjust this later. | |
2187 | */ | |
2188 | if (!pdev->irq) { | |
2189 | result = pci_enable_msix(pdev, dev->entry, 1); | |
2190 | if (result < 0) | |
2191 | goto unmap; | |
2192 | } | |
2193 | ||
42f61420 KB |
2194 | cap = readq(&dev->bar->cap); |
2195 | dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH); | |
2196 | dev->db_stride = 1 << NVME_CAP_STRIDE(cap); | |
0877cb0d KB |
2197 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
2198 | ||
2199 | return 0; | |
2200 | ||
0e53d180 KB |
2201 | unmap: |
2202 | iounmap(dev->bar); | |
2203 | dev->bar = NULL; | |
0877cb0d KB |
2204 | disable: |
2205 | pci_release_regions(pdev); | |
2206 | disable_pci: | |
2207 | pci_disable_device(pdev); | |
2208 | return result; | |
2209 | } | |
2210 | ||
2211 | static void nvme_dev_unmap(struct nvme_dev *dev) | |
2212 | { | |
2213 | if (dev->pci_dev->msi_enabled) | |
2214 | pci_disable_msi(dev->pci_dev); | |
2215 | else if (dev->pci_dev->msix_enabled) | |
2216 | pci_disable_msix(dev->pci_dev); | |
2217 | ||
2218 | if (dev->bar) { | |
2219 | iounmap(dev->bar); | |
2220 | dev->bar = NULL; | |
9a6b9458 | 2221 | pci_release_regions(dev->pci_dev); |
0877cb0d KB |
2222 | } |
2223 | ||
0877cb0d KB |
2224 | if (pci_is_enabled(dev->pci_dev)) |
2225 | pci_disable_device(dev->pci_dev); | |
2226 | } | |
2227 | ||
4d115420 KB |
2228 | struct nvme_delq_ctx { |
2229 | struct task_struct *waiter; | |
2230 | struct kthread_worker *worker; | |
2231 | atomic_t refcount; | |
2232 | }; | |
2233 | ||
2234 | static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev) | |
2235 | { | |
2236 | dq->waiter = current; | |
2237 | mb(); | |
2238 | ||
2239 | for (;;) { | |
2240 | set_current_state(TASK_KILLABLE); | |
2241 | if (!atomic_read(&dq->refcount)) | |
2242 | break; | |
2243 | if (!schedule_timeout(ADMIN_TIMEOUT) || | |
2244 | fatal_signal_pending(current)) { | |
2245 | set_current_state(TASK_RUNNING); | |
2246 | ||
2247 | nvme_disable_ctrl(dev, readq(&dev->bar->cap)); | |
2248 | nvme_disable_queue(dev, 0); | |
2249 | ||
2250 | send_sig(SIGKILL, dq->worker->task, 1); | |
2251 | flush_kthread_worker(dq->worker); | |
2252 | return; | |
2253 | } | |
2254 | } | |
2255 | set_current_state(TASK_RUNNING); | |
2256 | } | |
2257 | ||
2258 | static void nvme_put_dq(struct nvme_delq_ctx *dq) | |
2259 | { | |
2260 | atomic_dec(&dq->refcount); | |
2261 | if (dq->waiter) | |
2262 | wake_up_process(dq->waiter); | |
2263 | } | |
2264 | ||
2265 | static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq) | |
2266 | { | |
2267 | atomic_inc(&dq->refcount); | |
2268 | return dq; | |
2269 | } | |
2270 | ||
2271 | static void nvme_del_queue_end(struct nvme_queue *nvmeq) | |
2272 | { | |
2273 | struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx; | |
2274 | ||
2275 | nvme_clear_queue(nvmeq); | |
2276 | nvme_put_dq(dq); | |
2277 | } | |
2278 | ||
2279 | static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode, | |
2280 | kthread_work_func_t fn) | |
2281 | { | |
2282 | struct nvme_command c; | |
2283 | ||
2284 | memset(&c, 0, sizeof(c)); | |
2285 | c.delete_queue.opcode = opcode; | |
2286 | c.delete_queue.qid = cpu_to_le16(nvmeq->qid); | |
2287 | ||
2288 | init_kthread_work(&nvmeq->cmdinfo.work, fn); | |
a4aea562 MB |
2289 | return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo, |
2290 | ADMIN_TIMEOUT); | |
4d115420 KB |
2291 | } |
2292 | ||
2293 | static void nvme_del_cq_work_handler(struct kthread_work *work) | |
2294 | { | |
2295 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | |
2296 | cmdinfo.work); | |
2297 | nvme_del_queue_end(nvmeq); | |
2298 | } | |
2299 | ||
2300 | static int nvme_delete_cq(struct nvme_queue *nvmeq) | |
2301 | { | |
2302 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq, | |
2303 | nvme_del_cq_work_handler); | |
2304 | } | |
2305 | ||
2306 | static void nvme_del_sq_work_handler(struct kthread_work *work) | |
2307 | { | |
2308 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | |
2309 | cmdinfo.work); | |
2310 | int status = nvmeq->cmdinfo.status; | |
2311 | ||
2312 | if (!status) | |
2313 | status = nvme_delete_cq(nvmeq); | |
2314 | if (status) | |
2315 | nvme_del_queue_end(nvmeq); | |
2316 | } | |
2317 | ||
2318 | static int nvme_delete_sq(struct nvme_queue *nvmeq) | |
2319 | { | |
2320 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq, | |
2321 | nvme_del_sq_work_handler); | |
2322 | } | |
2323 | ||
2324 | static void nvme_del_queue_start(struct kthread_work *work) | |
2325 | { | |
2326 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | |
2327 | cmdinfo.work); | |
2328 | allow_signal(SIGKILL); | |
2329 | if (nvme_delete_sq(nvmeq)) | |
2330 | nvme_del_queue_end(nvmeq); | |
2331 | } | |
2332 | ||
2333 | static void nvme_disable_io_queues(struct nvme_dev *dev) | |
2334 | { | |
2335 | int i; | |
2336 | DEFINE_KTHREAD_WORKER_ONSTACK(worker); | |
2337 | struct nvme_delq_ctx dq; | |
2338 | struct task_struct *kworker_task = kthread_run(kthread_worker_fn, | |
2339 | &worker, "nvme%d", dev->instance); | |
2340 | ||
2341 | if (IS_ERR(kworker_task)) { | |
2342 | dev_err(&dev->pci_dev->dev, | |
2343 | "Failed to create queue del task\n"); | |
2344 | for (i = dev->queue_count - 1; i > 0; i--) | |
2345 | nvme_disable_queue(dev, i); | |
2346 | return; | |
2347 | } | |
2348 | ||
2349 | dq.waiter = NULL; | |
2350 | atomic_set(&dq.refcount, 0); | |
2351 | dq.worker = &worker; | |
2352 | for (i = dev->queue_count - 1; i > 0; i--) { | |
a4aea562 | 2353 | struct nvme_queue *nvmeq = dev->queues[i]; |
4d115420 KB |
2354 | |
2355 | if (nvme_suspend_queue(nvmeq)) | |
2356 | continue; | |
2357 | nvmeq->cmdinfo.ctx = nvme_get_dq(&dq); | |
2358 | nvmeq->cmdinfo.worker = dq.worker; | |
2359 | init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start); | |
2360 | queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work); | |
2361 | } | |
2362 | nvme_wait_dq(&dq, dev); | |
2363 | kthread_stop(kworker_task); | |
2364 | } | |
2365 | ||
b9afca3e DM |
2366 | /* |
2367 | * Remove the node from the device list and check | |
2368 | * for whether or not we need to stop the nvme_thread. | |
2369 | */ | |
2370 | static void nvme_dev_list_remove(struct nvme_dev *dev) | |
2371 | { | |
2372 | struct task_struct *tmp = NULL; | |
2373 | ||
2374 | spin_lock(&dev_list_lock); | |
2375 | list_del_init(&dev->node); | |
2376 | if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) { | |
2377 | tmp = nvme_thread; | |
2378 | nvme_thread = NULL; | |
2379 | } | |
2380 | spin_unlock(&dev_list_lock); | |
2381 | ||
2382 | if (tmp) | |
2383 | kthread_stop(tmp); | |
2384 | } | |
2385 | ||
f0b50732 | 2386 | static void nvme_dev_shutdown(struct nvme_dev *dev) |
b60503ba | 2387 | { |
22404274 | 2388 | int i; |
7c1b2450 | 2389 | u32 csts = -1; |
22404274 | 2390 | |
d4b4ff8e | 2391 | dev->initialized = 0; |
b9afca3e | 2392 | nvme_dev_list_remove(dev); |
1fa6aead | 2393 | |
7c1b2450 KB |
2394 | if (dev->bar) |
2395 | csts = readl(&dev->bar->csts); | |
2396 | if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) { | |
4d115420 | 2397 | for (i = dev->queue_count - 1; i >= 0; i--) { |
a4aea562 | 2398 | struct nvme_queue *nvmeq = dev->queues[i]; |
4d115420 KB |
2399 | nvme_suspend_queue(nvmeq); |
2400 | nvme_clear_queue(nvmeq); | |
2401 | } | |
2402 | } else { | |
2403 | nvme_disable_io_queues(dev); | |
1894d8f1 | 2404 | nvme_shutdown_ctrl(dev); |
4d115420 KB |
2405 | nvme_disable_queue(dev, 0); |
2406 | } | |
f0b50732 KB |
2407 | nvme_dev_unmap(dev); |
2408 | } | |
2409 | ||
a4aea562 MB |
2410 | static void nvme_dev_remove_admin(struct nvme_dev *dev) |
2411 | { | |
2412 | if (dev->admin_q && !blk_queue_dying(dev->admin_q)) | |
2413 | blk_cleanup_queue(dev->admin_q); | |
2414 | } | |
2415 | ||
f0b50732 KB |
2416 | static void nvme_dev_remove(struct nvme_dev *dev) |
2417 | { | |
9ac27090 | 2418 | struct nvme_ns *ns; |
f0b50732 | 2419 | |
9ac27090 KB |
2420 | list_for_each_entry(ns, &dev->namespaces, list) { |
2421 | if (ns->disk->flags & GENHD_FL_UP) | |
2422 | del_gendisk(ns->disk); | |
2423 | if (!blk_queue_dying(ns->queue)) | |
2424 | blk_cleanup_queue(ns->queue); | |
b60503ba | 2425 | } |
b60503ba MW |
2426 | } |
2427 | ||
091b6092 MW |
2428 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
2429 | { | |
2430 | struct device *dmadev = &dev->pci_dev->dev; | |
2431 | dev->prp_page_pool = dma_pool_create("prp list page", dmadev, | |
2432 | PAGE_SIZE, PAGE_SIZE, 0); | |
2433 | if (!dev->prp_page_pool) | |
2434 | return -ENOMEM; | |
2435 | ||
99802a7a MW |
2436 | /* Optimisation for I/Os between 4k and 128k */ |
2437 | dev->prp_small_pool = dma_pool_create("prp list 256", dmadev, | |
2438 | 256, 256, 0); | |
2439 | if (!dev->prp_small_pool) { | |
2440 | dma_pool_destroy(dev->prp_page_pool); | |
2441 | return -ENOMEM; | |
2442 | } | |
091b6092 MW |
2443 | return 0; |
2444 | } | |
2445 | ||
2446 | static void nvme_release_prp_pools(struct nvme_dev *dev) | |
2447 | { | |
2448 | dma_pool_destroy(dev->prp_page_pool); | |
99802a7a | 2449 | dma_pool_destroy(dev->prp_small_pool); |
091b6092 MW |
2450 | } |
2451 | ||
cd58ad7d QSA |
2452 | static DEFINE_IDA(nvme_instance_ida); |
2453 | ||
2454 | static int nvme_set_instance(struct nvme_dev *dev) | |
b60503ba | 2455 | { |
cd58ad7d QSA |
2456 | int instance, error; |
2457 | ||
2458 | do { | |
2459 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) | |
2460 | return -ENODEV; | |
2461 | ||
2462 | spin_lock(&dev_list_lock); | |
2463 | error = ida_get_new(&nvme_instance_ida, &instance); | |
2464 | spin_unlock(&dev_list_lock); | |
2465 | } while (error == -EAGAIN); | |
2466 | ||
2467 | if (error) | |
2468 | return -ENODEV; | |
2469 | ||
2470 | dev->instance = instance; | |
2471 | return 0; | |
b60503ba MW |
2472 | } |
2473 | ||
2474 | static void nvme_release_instance(struct nvme_dev *dev) | |
2475 | { | |
cd58ad7d QSA |
2476 | spin_lock(&dev_list_lock); |
2477 | ida_remove(&nvme_instance_ida, dev->instance); | |
2478 | spin_unlock(&dev_list_lock); | |
b60503ba MW |
2479 | } |
2480 | ||
9ac27090 KB |
2481 | static void nvme_free_namespaces(struct nvme_dev *dev) |
2482 | { | |
2483 | struct nvme_ns *ns, *next; | |
2484 | ||
2485 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { | |
2486 | list_del(&ns->list); | |
9e60352c KB |
2487 | |
2488 | spin_lock(&dev_list_lock); | |
2489 | ns->disk->private_data = NULL; | |
2490 | spin_unlock(&dev_list_lock); | |
2491 | ||
9ac27090 KB |
2492 | put_disk(ns->disk); |
2493 | kfree(ns); | |
2494 | } | |
2495 | } | |
2496 | ||
5e82e952 KB |
2497 | static void nvme_free_dev(struct kref *kref) |
2498 | { | |
2499 | struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); | |
9ac27090 | 2500 | |
a96d4f5c | 2501 | pci_dev_put(dev->pci_dev); |
9ac27090 | 2502 | nvme_free_namespaces(dev); |
285dffc9 | 2503 | nvme_release_instance(dev); |
a4aea562 | 2504 | blk_mq_free_tag_set(&dev->tagset); |
5e82e952 KB |
2505 | kfree(dev->queues); |
2506 | kfree(dev->entry); | |
2507 | kfree(dev); | |
2508 | } | |
2509 | ||
2510 | static int nvme_dev_open(struct inode *inode, struct file *f) | |
2511 | { | |
2512 | struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev, | |
2513 | miscdev); | |
2514 | kref_get(&dev->kref); | |
2515 | f->private_data = dev; | |
2516 | return 0; | |
2517 | } | |
2518 | ||
2519 | static int nvme_dev_release(struct inode *inode, struct file *f) | |
2520 | { | |
2521 | struct nvme_dev *dev = f->private_data; | |
2522 | kref_put(&dev->kref, nvme_free_dev); | |
2523 | return 0; | |
2524 | } | |
2525 | ||
2526 | static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg) | |
2527 | { | |
2528 | struct nvme_dev *dev = f->private_data; | |
a4aea562 MB |
2529 | struct nvme_ns *ns; |
2530 | ||
5e82e952 KB |
2531 | switch (cmd) { |
2532 | case NVME_IOCTL_ADMIN_CMD: | |
a4aea562 | 2533 | return nvme_user_cmd(dev, NULL, (void __user *)arg); |
7963e521 | 2534 | case NVME_IOCTL_IO_CMD: |
a4aea562 MB |
2535 | if (list_empty(&dev->namespaces)) |
2536 | return -ENOTTY; | |
2537 | ns = list_first_entry(&dev->namespaces, struct nvme_ns, list); | |
2538 | return nvme_user_cmd(dev, ns, (void __user *)arg); | |
5e82e952 KB |
2539 | default: |
2540 | return -ENOTTY; | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | static const struct file_operations nvme_dev_fops = { | |
2545 | .owner = THIS_MODULE, | |
2546 | .open = nvme_dev_open, | |
2547 | .release = nvme_dev_release, | |
2548 | .unlocked_ioctl = nvme_dev_ioctl, | |
2549 | .compat_ioctl = nvme_dev_ioctl, | |
2550 | }; | |
2551 | ||
a4aea562 MB |
2552 | static void nvme_set_irq_hints(struct nvme_dev *dev) |
2553 | { | |
2554 | struct nvme_queue *nvmeq; | |
2555 | int i; | |
2556 | ||
2557 | for (i = 0; i < dev->online_queues; i++) { | |
2558 | nvmeq = dev->queues[i]; | |
2559 | ||
2560 | if (!nvmeq->hctx) | |
2561 | continue; | |
2562 | ||
2563 | irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector, | |
2564 | nvmeq->hctx->cpumask); | |
2565 | } | |
2566 | } | |
2567 | ||
f0b50732 KB |
2568 | static int nvme_dev_start(struct nvme_dev *dev) |
2569 | { | |
2570 | int result; | |
b9afca3e | 2571 | bool start_thread = false; |
f0b50732 KB |
2572 | |
2573 | result = nvme_dev_map(dev); | |
2574 | if (result) | |
2575 | return result; | |
2576 | ||
2577 | result = nvme_configure_admin_queue(dev); | |
2578 | if (result) | |
2579 | goto unmap; | |
2580 | ||
2581 | spin_lock(&dev_list_lock); | |
b9afca3e DM |
2582 | if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) { |
2583 | start_thread = true; | |
2584 | nvme_thread = NULL; | |
2585 | } | |
f0b50732 KB |
2586 | list_add(&dev->node, &dev_list); |
2587 | spin_unlock(&dev_list_lock); | |
2588 | ||
b9afca3e DM |
2589 | if (start_thread) { |
2590 | nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); | |
387caa5a | 2591 | wake_up_all(&nvme_kthread_wait); |
b9afca3e DM |
2592 | } else |
2593 | wait_event_killable(nvme_kthread_wait, nvme_thread); | |
2594 | ||
2595 | if (IS_ERR_OR_NULL(nvme_thread)) { | |
2596 | result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR; | |
2597 | goto disable; | |
2598 | } | |
a4aea562 MB |
2599 | |
2600 | nvme_init_queue(dev->queues[0], 0); | |
b9afca3e | 2601 | |
f0b50732 | 2602 | result = nvme_setup_io_queues(dev); |
badc34d4 | 2603 | if (result) |
f0b50732 KB |
2604 | goto disable; |
2605 | ||
a4aea562 MB |
2606 | nvme_set_irq_hints(dev); |
2607 | ||
d82e8bfd | 2608 | return result; |
f0b50732 KB |
2609 | |
2610 | disable: | |
a1a5ef99 | 2611 | nvme_disable_queue(dev, 0); |
b9afca3e | 2612 | nvme_dev_list_remove(dev); |
f0b50732 KB |
2613 | unmap: |
2614 | nvme_dev_unmap(dev); | |
2615 | return result; | |
2616 | } | |
2617 | ||
9a6b9458 KB |
2618 | static int nvme_remove_dead_ctrl(void *arg) |
2619 | { | |
2620 | struct nvme_dev *dev = (struct nvme_dev *)arg; | |
2621 | struct pci_dev *pdev = dev->pci_dev; | |
2622 | ||
2623 | if (pci_get_drvdata(pdev)) | |
c81f4975 | 2624 | pci_stop_and_remove_bus_device_locked(pdev); |
9a6b9458 KB |
2625 | kref_put(&dev->kref, nvme_free_dev); |
2626 | return 0; | |
2627 | } | |
2628 | ||
2629 | static void nvme_remove_disks(struct work_struct *ws) | |
2630 | { | |
9a6b9458 KB |
2631 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
2632 | ||
5a92e700 | 2633 | nvme_free_queues(dev, 1); |
302c6727 | 2634 | nvme_dev_remove(dev); |
9a6b9458 KB |
2635 | } |
2636 | ||
2637 | static int nvme_dev_resume(struct nvme_dev *dev) | |
2638 | { | |
2639 | int ret; | |
2640 | ||
2641 | ret = nvme_dev_start(dev); | |
badc34d4 | 2642 | if (ret) |
9a6b9458 | 2643 | return ret; |
badc34d4 | 2644 | if (dev->online_queues < 2) { |
9a6b9458 | 2645 | spin_lock(&dev_list_lock); |
9ca97374 | 2646 | dev->reset_workfn = nvme_remove_disks; |
9a6b9458 KB |
2647 | queue_work(nvme_workq, &dev->reset_work); |
2648 | spin_unlock(&dev_list_lock); | |
2649 | } | |
d4b4ff8e | 2650 | dev->initialized = 1; |
9a6b9458 KB |
2651 | return 0; |
2652 | } | |
2653 | ||
2654 | static void nvme_dev_reset(struct nvme_dev *dev) | |
2655 | { | |
2656 | nvme_dev_shutdown(dev); | |
2657 | if (nvme_dev_resume(dev)) { | |
a4aea562 | 2658 | dev_warn(&dev->pci_dev->dev, "Device failed to resume\n"); |
9a6b9458 KB |
2659 | kref_get(&dev->kref); |
2660 | if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d", | |
2661 | dev->instance))) { | |
2662 | dev_err(&dev->pci_dev->dev, | |
2663 | "Failed to start controller remove task\n"); | |
2664 | kref_put(&dev->kref, nvme_free_dev); | |
2665 | } | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | static void nvme_reset_failed_dev(struct work_struct *ws) | |
2670 | { | |
2671 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); | |
2672 | nvme_dev_reset(dev); | |
2673 | } | |
2674 | ||
9ca97374 TH |
2675 | static void nvme_reset_workfn(struct work_struct *work) |
2676 | { | |
2677 | struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work); | |
2678 | dev->reset_workfn(work); | |
2679 | } | |
2680 | ||
8d85fce7 | 2681 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
b60503ba | 2682 | { |
a4aea562 | 2683 | int node, result = -ENOMEM; |
b60503ba MW |
2684 | struct nvme_dev *dev; |
2685 | ||
a4aea562 MB |
2686 | node = dev_to_node(&pdev->dev); |
2687 | if (node == NUMA_NO_NODE) | |
2688 | set_dev_node(&pdev->dev, 0); | |
2689 | ||
2690 | dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node); | |
b60503ba MW |
2691 | if (!dev) |
2692 | return -ENOMEM; | |
a4aea562 MB |
2693 | dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry), |
2694 | GFP_KERNEL, node); | |
b60503ba MW |
2695 | if (!dev->entry) |
2696 | goto free; | |
a4aea562 MB |
2697 | dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *), |
2698 | GFP_KERNEL, node); | |
b60503ba MW |
2699 | if (!dev->queues) |
2700 | goto free; | |
2701 | ||
2702 | INIT_LIST_HEAD(&dev->namespaces); | |
9ca97374 TH |
2703 | dev->reset_workfn = nvme_reset_failed_dev; |
2704 | INIT_WORK(&dev->reset_work, nvme_reset_workfn); | |
a96d4f5c | 2705 | dev->pci_dev = pci_dev_get(pdev); |
9a6b9458 | 2706 | pci_set_drvdata(pdev, dev); |
cd58ad7d QSA |
2707 | result = nvme_set_instance(dev); |
2708 | if (result) | |
a96d4f5c | 2709 | goto put_pci; |
b60503ba | 2710 | |
091b6092 MW |
2711 | result = nvme_setup_prp_pools(dev); |
2712 | if (result) | |
0877cb0d | 2713 | goto release; |
091b6092 | 2714 | |
fb35e914 | 2715 | kref_init(&dev->kref); |
f0b50732 | 2716 | result = nvme_dev_start(dev); |
badc34d4 | 2717 | if (result) |
0877cb0d | 2718 | goto release_pools; |
b60503ba | 2719 | |
badc34d4 KB |
2720 | if (dev->online_queues > 1) |
2721 | result = nvme_dev_add(dev); | |
d82e8bfd | 2722 | if (result) |
f0b50732 | 2723 | goto shutdown; |
740216fc | 2724 | |
5e82e952 KB |
2725 | scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance); |
2726 | dev->miscdev.minor = MISC_DYNAMIC_MINOR; | |
2727 | dev->miscdev.parent = &pdev->dev; | |
2728 | dev->miscdev.name = dev->name; | |
2729 | dev->miscdev.fops = &nvme_dev_fops; | |
2730 | result = misc_register(&dev->miscdev); | |
2731 | if (result) | |
2732 | goto remove; | |
2733 | ||
a4aea562 MB |
2734 | nvme_set_irq_hints(dev); |
2735 | ||
d4b4ff8e | 2736 | dev->initialized = 1; |
b60503ba MW |
2737 | return 0; |
2738 | ||
5e82e952 KB |
2739 | remove: |
2740 | nvme_dev_remove(dev); | |
a4aea562 | 2741 | nvme_dev_remove_admin(dev); |
9ac27090 | 2742 | nvme_free_namespaces(dev); |
f0b50732 KB |
2743 | shutdown: |
2744 | nvme_dev_shutdown(dev); | |
0877cb0d | 2745 | release_pools: |
a1a5ef99 | 2746 | nvme_free_queues(dev, 0); |
091b6092 | 2747 | nvme_release_prp_pools(dev); |
0877cb0d KB |
2748 | release: |
2749 | nvme_release_instance(dev); | |
a96d4f5c KB |
2750 | put_pci: |
2751 | pci_dev_put(dev->pci_dev); | |
b60503ba MW |
2752 | free: |
2753 | kfree(dev->queues); | |
2754 | kfree(dev->entry); | |
2755 | kfree(dev); | |
2756 | return result; | |
2757 | } | |
2758 | ||
f0d54a54 KB |
2759 | static void nvme_reset_notify(struct pci_dev *pdev, bool prepare) |
2760 | { | |
a6739479 | 2761 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
f0d54a54 | 2762 | |
a6739479 KB |
2763 | if (prepare) |
2764 | nvme_dev_shutdown(dev); | |
2765 | else | |
2766 | nvme_dev_resume(dev); | |
f0d54a54 KB |
2767 | } |
2768 | ||
09ece142 KB |
2769 | static void nvme_shutdown(struct pci_dev *pdev) |
2770 | { | |
2771 | struct nvme_dev *dev = pci_get_drvdata(pdev); | |
2772 | nvme_dev_shutdown(dev); | |
2773 | } | |
2774 | ||
8d85fce7 | 2775 | static void nvme_remove(struct pci_dev *pdev) |
b60503ba MW |
2776 | { |
2777 | struct nvme_dev *dev = pci_get_drvdata(pdev); | |
9a6b9458 KB |
2778 | |
2779 | spin_lock(&dev_list_lock); | |
2780 | list_del_init(&dev->node); | |
2781 | spin_unlock(&dev_list_lock); | |
2782 | ||
2783 | pci_set_drvdata(pdev, NULL); | |
2784 | flush_work(&dev->reset_work); | |
5e82e952 | 2785 | misc_deregister(&dev->miscdev); |
a4aea562 | 2786 | nvme_dev_remove(dev); |
9a6b9458 | 2787 | nvme_dev_shutdown(dev); |
a4aea562 | 2788 | nvme_dev_remove_admin(dev); |
a1a5ef99 | 2789 | nvme_free_queues(dev, 0); |
a4aea562 | 2790 | nvme_free_admin_tags(dev); |
9a6b9458 | 2791 | nvme_release_prp_pools(dev); |
5e82e952 | 2792 | kref_put(&dev->kref, nvme_free_dev); |
b60503ba MW |
2793 | } |
2794 | ||
2795 | /* These functions are yet to be implemented */ | |
2796 | #define nvme_error_detected NULL | |
2797 | #define nvme_dump_registers NULL | |
2798 | #define nvme_link_reset NULL | |
2799 | #define nvme_slot_reset NULL | |
2800 | #define nvme_error_resume NULL | |
cd638946 | 2801 | |
671a6018 | 2802 | #ifdef CONFIG_PM_SLEEP |
cd638946 KB |
2803 | static int nvme_suspend(struct device *dev) |
2804 | { | |
2805 | struct pci_dev *pdev = to_pci_dev(dev); | |
2806 | struct nvme_dev *ndev = pci_get_drvdata(pdev); | |
2807 | ||
2808 | nvme_dev_shutdown(ndev); | |
2809 | return 0; | |
2810 | } | |
2811 | ||
2812 | static int nvme_resume(struct device *dev) | |
2813 | { | |
2814 | struct pci_dev *pdev = to_pci_dev(dev); | |
2815 | struct nvme_dev *ndev = pci_get_drvdata(pdev); | |
cd638946 | 2816 | |
9a6b9458 | 2817 | if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) { |
9ca97374 | 2818 | ndev->reset_workfn = nvme_reset_failed_dev; |
9a6b9458 KB |
2819 | queue_work(nvme_workq, &ndev->reset_work); |
2820 | } | |
2821 | return 0; | |
cd638946 | 2822 | } |
671a6018 | 2823 | #endif |
cd638946 KB |
2824 | |
2825 | static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); | |
b60503ba | 2826 | |
1d352035 | 2827 | static const struct pci_error_handlers nvme_err_handler = { |
b60503ba MW |
2828 | .error_detected = nvme_error_detected, |
2829 | .mmio_enabled = nvme_dump_registers, | |
2830 | .link_reset = nvme_link_reset, | |
2831 | .slot_reset = nvme_slot_reset, | |
2832 | .resume = nvme_error_resume, | |
f0d54a54 | 2833 | .reset_notify = nvme_reset_notify, |
b60503ba MW |
2834 | }; |
2835 | ||
2836 | /* Move to pci_ids.h later */ | |
2837 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 | |
2838 | ||
6eb0d698 | 2839 | static const struct pci_device_id nvme_id_table[] = { |
b60503ba MW |
2840 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
2841 | { 0, } | |
2842 | }; | |
2843 | MODULE_DEVICE_TABLE(pci, nvme_id_table); | |
2844 | ||
2845 | static struct pci_driver nvme_driver = { | |
2846 | .name = "nvme", | |
2847 | .id_table = nvme_id_table, | |
2848 | .probe = nvme_probe, | |
8d85fce7 | 2849 | .remove = nvme_remove, |
09ece142 | 2850 | .shutdown = nvme_shutdown, |
cd638946 KB |
2851 | .driver = { |
2852 | .pm = &nvme_dev_pm_ops, | |
2853 | }, | |
b60503ba MW |
2854 | .err_handler = &nvme_err_handler, |
2855 | }; | |
2856 | ||
2857 | static int __init nvme_init(void) | |
2858 | { | |
0ac13140 | 2859 | int result; |
1fa6aead | 2860 | |
b9afca3e | 2861 | init_waitqueue_head(&nvme_kthread_wait); |
b60503ba | 2862 | |
9a6b9458 KB |
2863 | nvme_workq = create_singlethread_workqueue("nvme"); |
2864 | if (!nvme_workq) | |
b9afca3e | 2865 | return -ENOMEM; |
9a6b9458 | 2866 | |
5c42ea16 KB |
2867 | result = register_blkdev(nvme_major, "nvme"); |
2868 | if (result < 0) | |
9a6b9458 | 2869 | goto kill_workq; |
5c42ea16 | 2870 | else if (result > 0) |
0ac13140 | 2871 | nvme_major = result; |
b60503ba | 2872 | |
f3db22fe KB |
2873 | result = pci_register_driver(&nvme_driver); |
2874 | if (result) | |
a4aea562 | 2875 | goto unregister_blkdev; |
1fa6aead | 2876 | return 0; |
b60503ba | 2877 | |
1fa6aead | 2878 | unregister_blkdev: |
b60503ba | 2879 | unregister_blkdev(nvme_major, "nvme"); |
9a6b9458 KB |
2880 | kill_workq: |
2881 | destroy_workqueue(nvme_workq); | |
b60503ba MW |
2882 | return result; |
2883 | } | |
2884 | ||
2885 | static void __exit nvme_exit(void) | |
2886 | { | |
2887 | pci_unregister_driver(&nvme_driver); | |
f3db22fe | 2888 | unregister_hotcpu_notifier(&nvme_nb); |
b60503ba | 2889 | unregister_blkdev(nvme_major, "nvme"); |
9a6b9458 | 2890 | destroy_workqueue(nvme_workq); |
b9afca3e | 2891 | BUG_ON(nvme_thread && !IS_ERR(nvme_thread)); |
21bd78bc | 2892 | _nvme_check_size(); |
b60503ba MW |
2893 | } |
2894 | ||
2895 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); | |
2896 | MODULE_LICENSE("GPL"); | |
c78b4713 | 2897 | MODULE_VERSION("1.0"); |
b60503ba MW |
2898 | module_init(nvme_init); |
2899 | module_exit(nvme_exit); |