Commit | Line | Data |
---|---|---|
21d34711 CH |
1 | /* |
2 | * NVM Express device driver | |
3 | * Copyright (c) 2011-2014, Intel Corporation. | |
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. | |
13 | */ | |
14 | ||
15 | #include <linux/blkdev.h> | |
16 | #include <linux/blk-mq.h> | |
5fd4ce1b | 17 | #include <linux/delay.h> |
21d34711 | 18 | #include <linux/errno.h> |
1673f1f0 | 19 | #include <linux/hdreg.h> |
21d34711 | 20 | #include <linux/kernel.h> |
5bae7f73 CH |
21 | #include <linux/module.h> |
22 | #include <linux/list_sort.h> | |
21d34711 CH |
23 | #include <linux/slab.h> |
24 | #include <linux/types.h> | |
1673f1f0 CH |
25 | #include <linux/pr.h> |
26 | #include <linux/ptrace.h> | |
27 | #include <linux/nvme_ioctl.h> | |
28 | #include <linux/t10-pi.h> | |
29 | #include <scsi/sg.h> | |
30 | #include <asm/unaligned.h> | |
21d34711 CH |
31 | |
32 | #include "nvme.h" | |
038bd4cb | 33 | #include "fabrics.h" |
21d34711 | 34 | |
f3ca80fc CH |
35 | #define NVME_MINORS (1U << MINORBITS) |
36 | ||
ba0ba7d3 ML |
37 | unsigned char admin_timeout = 60; |
38 | module_param(admin_timeout, byte, 0644); | |
39 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); | |
576d55d6 | 40 | EXPORT_SYMBOL_GPL(admin_timeout); |
ba0ba7d3 ML |
41 | |
42 | unsigned char nvme_io_timeout = 30; | |
43 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); | |
44 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); | |
576d55d6 | 45 | EXPORT_SYMBOL_GPL(nvme_io_timeout); |
ba0ba7d3 ML |
46 | |
47 | unsigned char shutdown_timeout = 5; | |
48 | module_param(shutdown_timeout, byte, 0644); | |
49 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); | |
50 | ||
f80ec966 KB |
51 | unsigned int nvme_max_retries = 5; |
52 | module_param_named(max_retries, nvme_max_retries, uint, 0644); | |
53 | MODULE_PARM_DESC(max_retries, "max number of retries a command may have"); | |
54 | EXPORT_SYMBOL_GPL(nvme_max_retries); | |
5bae7f73 | 55 | |
f3ca80fc CH |
56 | static int nvme_char_major; |
57 | module_param(nvme_char_major, int, 0); | |
58 | ||
59 | static LIST_HEAD(nvme_ctrl_list); | |
9f2482b9 | 60 | static DEFINE_SPINLOCK(dev_list_lock); |
1673f1f0 | 61 | |
f3ca80fc CH |
62 | static struct class *nvme_class; |
63 | ||
c55a2fd4 ML |
64 | void nvme_cancel_request(struct request *req, void *data, bool reserved) |
65 | { | |
66 | int status; | |
67 | ||
68 | if (!blk_mq_request_started(req)) | |
69 | return; | |
70 | ||
71 | dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, | |
72 | "Cancelling I/O %d", req->tag); | |
73 | ||
74 | status = NVME_SC_ABORT_REQ; | |
75 | if (blk_queue_dying(req->q)) | |
76 | status |= NVME_SC_DNR; | |
77 | blk_mq_complete_request(req, status); | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(nvme_cancel_request); | |
80 | ||
bb8d261e CH |
81 | bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, |
82 | enum nvme_ctrl_state new_state) | |
83 | { | |
f6b6a28e | 84 | enum nvme_ctrl_state old_state; |
bb8d261e CH |
85 | bool changed = false; |
86 | ||
87 | spin_lock_irq(&ctrl->lock); | |
f6b6a28e GKB |
88 | |
89 | old_state = ctrl->state; | |
bb8d261e CH |
90 | switch (new_state) { |
91 | case NVME_CTRL_LIVE: | |
92 | switch (old_state) { | |
7d2e8008 | 93 | case NVME_CTRL_NEW: |
bb8d261e | 94 | case NVME_CTRL_RESETTING: |
def61eca | 95 | case NVME_CTRL_RECONNECTING: |
bb8d261e CH |
96 | changed = true; |
97 | /* FALLTHRU */ | |
98 | default: | |
99 | break; | |
100 | } | |
101 | break; | |
102 | case NVME_CTRL_RESETTING: | |
103 | switch (old_state) { | |
104 | case NVME_CTRL_NEW: | |
def61eca CH |
105 | case NVME_CTRL_LIVE: |
106 | case NVME_CTRL_RECONNECTING: | |
107 | changed = true; | |
108 | /* FALLTHRU */ | |
109 | default: | |
110 | break; | |
111 | } | |
112 | break; | |
113 | case NVME_CTRL_RECONNECTING: | |
114 | switch (old_state) { | |
bb8d261e CH |
115 | case NVME_CTRL_LIVE: |
116 | changed = true; | |
117 | /* FALLTHRU */ | |
118 | default: | |
119 | break; | |
120 | } | |
121 | break; | |
122 | case NVME_CTRL_DELETING: | |
123 | switch (old_state) { | |
124 | case NVME_CTRL_LIVE: | |
125 | case NVME_CTRL_RESETTING: | |
def61eca | 126 | case NVME_CTRL_RECONNECTING: |
bb8d261e CH |
127 | changed = true; |
128 | /* FALLTHRU */ | |
129 | default: | |
130 | break; | |
131 | } | |
132 | break; | |
0ff9d4e1 KB |
133 | case NVME_CTRL_DEAD: |
134 | switch (old_state) { | |
135 | case NVME_CTRL_DELETING: | |
136 | changed = true; | |
137 | /* FALLTHRU */ | |
138 | default: | |
139 | break; | |
140 | } | |
141 | break; | |
bb8d261e CH |
142 | default: |
143 | break; | |
144 | } | |
bb8d261e CH |
145 | |
146 | if (changed) | |
147 | ctrl->state = new_state; | |
148 | ||
f6b6a28e GKB |
149 | spin_unlock_irq(&ctrl->lock); |
150 | ||
bb8d261e CH |
151 | return changed; |
152 | } | |
153 | EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); | |
154 | ||
1673f1f0 CH |
155 | static void nvme_free_ns(struct kref *kref) |
156 | { | |
157 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); | |
158 | ||
b0b4e09c MB |
159 | if (ns->ndev) |
160 | nvme_nvm_unregister(ns); | |
1673f1f0 | 161 | |
b0b4e09c MB |
162 | if (ns->disk) { |
163 | spin_lock(&dev_list_lock); | |
164 | ns->disk->private_data = NULL; | |
165 | spin_unlock(&dev_list_lock); | |
166 | } | |
1673f1f0 | 167 | |
1673f1f0 | 168 | put_disk(ns->disk); |
075790eb KB |
169 | ida_simple_remove(&ns->ctrl->ns_ida, ns->instance); |
170 | nvme_put_ctrl(ns->ctrl); | |
1673f1f0 CH |
171 | kfree(ns); |
172 | } | |
173 | ||
5bae7f73 | 174 | static void nvme_put_ns(struct nvme_ns *ns) |
1673f1f0 CH |
175 | { |
176 | kref_put(&ns->kref, nvme_free_ns); | |
177 | } | |
178 | ||
179 | static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk) | |
180 | { | |
181 | struct nvme_ns *ns; | |
182 | ||
183 | spin_lock(&dev_list_lock); | |
184 | ns = disk->private_data; | |
e439bb12 SG |
185 | if (ns) { |
186 | if (!kref_get_unless_zero(&ns->kref)) | |
187 | goto fail; | |
188 | if (!try_module_get(ns->ctrl->ops->module)) | |
189 | goto fail_put_ns; | |
190 | } | |
1673f1f0 CH |
191 | spin_unlock(&dev_list_lock); |
192 | ||
193 | return ns; | |
e439bb12 SG |
194 | |
195 | fail_put_ns: | |
196 | kref_put(&ns->kref, nvme_free_ns); | |
197 | fail: | |
198 | spin_unlock(&dev_list_lock); | |
199 | return NULL; | |
1673f1f0 CH |
200 | } |
201 | ||
7688faa6 CH |
202 | void nvme_requeue_req(struct request *req) |
203 | { | |
a6eaa884 | 204 | blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q)); |
7688faa6 | 205 | } |
576d55d6 | 206 | EXPORT_SYMBOL_GPL(nvme_requeue_req); |
7688faa6 | 207 | |
4160982e | 208 | struct request *nvme_alloc_request(struct request_queue *q, |
eb71f435 | 209 | struct nvme_command *cmd, unsigned int flags, int qid) |
21d34711 | 210 | { |
aebf526b | 211 | unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN; |
21d34711 | 212 | struct request *req; |
21d34711 | 213 | |
eb71f435 | 214 | if (qid == NVME_QID_ANY) { |
aebf526b | 215 | req = blk_mq_alloc_request(q, op, flags); |
eb71f435 | 216 | } else { |
aebf526b | 217 | req = blk_mq_alloc_request_hctx(q, op, flags, |
eb71f435 CH |
218 | qid ? qid - 1 : 0); |
219 | } | |
21d34711 | 220 | if (IS_ERR(req)) |
4160982e | 221 | return req; |
21d34711 | 222 | |
21d34711 | 223 | req->cmd_flags |= REQ_FAILFAST_DRIVER; |
d49187e9 | 224 | nvme_req(req)->cmd = cmd; |
21d34711 | 225 | |
4160982e CH |
226 | return req; |
227 | } | |
576d55d6 | 228 | EXPORT_SYMBOL_GPL(nvme_alloc_request); |
4160982e | 229 | |
8093f7ca ML |
230 | static inline void nvme_setup_flush(struct nvme_ns *ns, |
231 | struct nvme_command *cmnd) | |
232 | { | |
233 | memset(cmnd, 0, sizeof(*cmnd)); | |
234 | cmnd->common.opcode = nvme_cmd_flush; | |
235 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); | |
236 | } | |
237 | ||
238 | static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req, | |
239 | struct nvme_command *cmnd) | |
240 | { | |
b35ba01e | 241 | unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; |
8093f7ca | 242 | struct nvme_dsm_range *range; |
b35ba01e | 243 | struct bio *bio; |
8093f7ca | 244 | |
b35ba01e | 245 | range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC); |
8093f7ca ML |
246 | if (!range) |
247 | return BLK_MQ_RQ_QUEUE_BUSY; | |
248 | ||
b35ba01e CH |
249 | __rq_for_each_bio(bio, req) { |
250 | u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector); | |
251 | u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; | |
252 | ||
253 | range[n].cattr = cpu_to_le32(0); | |
254 | range[n].nlb = cpu_to_le32(nlb); | |
255 | range[n].slba = cpu_to_le64(slba); | |
256 | n++; | |
257 | } | |
258 | ||
259 | if (WARN_ON_ONCE(n != segments)) { | |
260 | kfree(range); | |
261 | return BLK_MQ_RQ_QUEUE_ERROR; | |
262 | } | |
8093f7ca ML |
263 | |
264 | memset(cmnd, 0, sizeof(*cmnd)); | |
265 | cmnd->dsm.opcode = nvme_cmd_dsm; | |
266 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); | |
b35ba01e | 267 | cmnd->dsm.nr = segments - 1; |
8093f7ca ML |
268 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
269 | ||
f9d03f96 CH |
270 | req->special_vec.bv_page = virt_to_page(range); |
271 | req->special_vec.bv_offset = offset_in_page(range); | |
b35ba01e | 272 | req->special_vec.bv_len = sizeof(*range) * segments; |
f9d03f96 | 273 | req->rq_flags |= RQF_SPECIAL_PAYLOAD; |
8093f7ca | 274 | |
bac0000a | 275 | return BLK_MQ_RQ_QUEUE_OK; |
8093f7ca | 276 | } |
8093f7ca | 277 | |
8093f7ca ML |
278 | static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req, |
279 | struct nvme_command *cmnd) | |
280 | { | |
281 | u16 control = 0; | |
282 | u32 dsmgmt = 0; | |
283 | ||
284 | if (req->cmd_flags & REQ_FUA) | |
285 | control |= NVME_RW_FUA; | |
286 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) | |
287 | control |= NVME_RW_LR; | |
288 | ||
289 | if (req->cmd_flags & REQ_RAHEAD) | |
290 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; | |
291 | ||
292 | memset(cmnd, 0, sizeof(*cmnd)); | |
293 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); | |
8093f7ca ML |
294 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
295 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); | |
296 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); | |
297 | ||
298 | if (ns->ms) { | |
299 | switch (ns->pi_type) { | |
300 | case NVME_NS_DPS_PI_TYPE3: | |
301 | control |= NVME_RW_PRINFO_PRCHK_GUARD; | |
302 | break; | |
303 | case NVME_NS_DPS_PI_TYPE1: | |
304 | case NVME_NS_DPS_PI_TYPE2: | |
305 | control |= NVME_RW_PRINFO_PRCHK_GUARD | | |
306 | NVME_RW_PRINFO_PRCHK_REF; | |
307 | cmnd->rw.reftag = cpu_to_le32( | |
308 | nvme_block_nr(ns, blk_rq_pos(req))); | |
309 | break; | |
310 | } | |
311 | if (!blk_integrity_rq(req)) | |
312 | control |= NVME_RW_PRINFO_PRACT; | |
313 | } | |
314 | ||
315 | cmnd->rw.control = cpu_to_le16(control); | |
316 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); | |
317 | } | |
318 | ||
319 | int nvme_setup_cmd(struct nvme_ns *ns, struct request *req, | |
320 | struct nvme_command *cmd) | |
321 | { | |
bac0000a | 322 | int ret = BLK_MQ_RQ_QUEUE_OK; |
8093f7ca | 323 | |
aebf526b CH |
324 | switch (req_op(req)) { |
325 | case REQ_OP_DRV_IN: | |
326 | case REQ_OP_DRV_OUT: | |
d49187e9 | 327 | memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); |
aebf526b CH |
328 | break; |
329 | case REQ_OP_FLUSH: | |
8093f7ca | 330 | nvme_setup_flush(ns, cmd); |
aebf526b CH |
331 | break; |
332 | case REQ_OP_DISCARD: | |
8093f7ca | 333 | ret = nvme_setup_discard(ns, req, cmd); |
aebf526b CH |
334 | break; |
335 | case REQ_OP_READ: | |
336 | case REQ_OP_WRITE: | |
8093f7ca | 337 | nvme_setup_rw(ns, req, cmd); |
aebf526b CH |
338 | break; |
339 | default: | |
340 | WARN_ON_ONCE(1); | |
341 | return BLK_MQ_RQ_QUEUE_ERROR; | |
342 | } | |
8093f7ca | 343 | |
721b3917 | 344 | cmd->common.command_id = req->tag; |
8093f7ca ML |
345 | return ret; |
346 | } | |
347 | EXPORT_SYMBOL_GPL(nvme_setup_cmd); | |
348 | ||
4160982e CH |
349 | /* |
350 | * Returns 0 on success. If the result is negative, it's a Linux error code; | |
351 | * if the result is positive, it's an NVM Express status code | |
352 | */ | |
353 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, | |
d49187e9 | 354 | union nvme_result *result, void *buffer, unsigned bufflen, |
eb71f435 | 355 | unsigned timeout, int qid, int at_head, int flags) |
4160982e CH |
356 | { |
357 | struct request *req; | |
358 | int ret; | |
359 | ||
eb71f435 | 360 | req = nvme_alloc_request(q, cmd, flags, qid); |
4160982e CH |
361 | if (IS_ERR(req)) |
362 | return PTR_ERR(req); | |
363 | ||
364 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; | |
365 | ||
21d34711 CH |
366 | if (buffer && bufflen) { |
367 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); | |
368 | if (ret) | |
369 | goto out; | |
4160982e CH |
370 | } |
371 | ||
eb71f435 | 372 | blk_execute_rq(req->q, NULL, req, at_head); |
d49187e9 CH |
373 | if (result) |
374 | *result = nvme_req(req)->result; | |
4160982e CH |
375 | ret = req->errors; |
376 | out: | |
377 | blk_mq_free_request(req); | |
378 | return ret; | |
379 | } | |
eb71f435 | 380 | EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); |
4160982e CH |
381 | |
382 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, | |
383 | void *buffer, unsigned bufflen) | |
384 | { | |
eb71f435 CH |
385 | return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, |
386 | NVME_QID_ANY, 0, 0); | |
4160982e | 387 | } |
576d55d6 | 388 | EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); |
4160982e | 389 | |
0b7f1f26 KB |
390 | int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
391 | void __user *ubuffer, unsigned bufflen, | |
392 | void __user *meta_buffer, unsigned meta_len, u32 meta_seed, | |
393 | u32 *result, unsigned timeout) | |
4160982e | 394 | { |
7a5abb4b | 395 | bool write = nvme_is_write(cmd); |
0b7f1f26 KB |
396 | struct nvme_ns *ns = q->queuedata; |
397 | struct gendisk *disk = ns ? ns->disk : NULL; | |
4160982e | 398 | struct request *req; |
0b7f1f26 KB |
399 | struct bio *bio = NULL; |
400 | void *meta = NULL; | |
4160982e CH |
401 | int ret; |
402 | ||
eb71f435 | 403 | req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); |
4160982e CH |
404 | if (IS_ERR(req)) |
405 | return PTR_ERR(req); | |
406 | ||
407 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; | |
408 | ||
409 | if (ubuffer && bufflen) { | |
21d34711 CH |
410 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
411 | GFP_KERNEL); | |
412 | if (ret) | |
413 | goto out; | |
414 | bio = req->bio; | |
21d34711 | 415 | |
0b7f1f26 KB |
416 | if (!disk) |
417 | goto submit; | |
418 | bio->bi_bdev = bdget_disk(disk, 0); | |
419 | if (!bio->bi_bdev) { | |
420 | ret = -ENODEV; | |
421 | goto out_unmap; | |
422 | } | |
423 | ||
e9fc63d6 | 424 | if (meta_buffer && meta_len) { |
0b7f1f26 KB |
425 | struct bio_integrity_payload *bip; |
426 | ||
427 | meta = kmalloc(meta_len, GFP_KERNEL); | |
428 | if (!meta) { | |
429 | ret = -ENOMEM; | |
430 | goto out_unmap; | |
431 | } | |
432 | ||
433 | if (write) { | |
434 | if (copy_from_user(meta, meta_buffer, | |
435 | meta_len)) { | |
436 | ret = -EFAULT; | |
437 | goto out_free_meta; | |
438 | } | |
439 | } | |
440 | ||
441 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); | |
06c1e390 KB |
442 | if (IS_ERR(bip)) { |
443 | ret = PTR_ERR(bip); | |
0b7f1f26 KB |
444 | goto out_free_meta; |
445 | } | |
446 | ||
447 | bip->bip_iter.bi_size = meta_len; | |
448 | bip->bip_iter.bi_sector = meta_seed; | |
449 | ||
450 | ret = bio_integrity_add_page(bio, virt_to_page(meta), | |
451 | meta_len, offset_in_page(meta)); | |
452 | if (ret != meta_len) { | |
453 | ret = -ENOMEM; | |
454 | goto out_free_meta; | |
455 | } | |
456 | } | |
457 | } | |
458 | submit: | |
459 | blk_execute_rq(req->q, disk, req, 0); | |
460 | ret = req->errors; | |
21d34711 | 461 | if (result) |
d49187e9 | 462 | *result = le32_to_cpu(nvme_req(req)->result.u32); |
0b7f1f26 KB |
463 | if (meta && !ret && !write) { |
464 | if (copy_to_user(meta_buffer, meta, meta_len)) | |
465 | ret = -EFAULT; | |
466 | } | |
467 | out_free_meta: | |
468 | kfree(meta); | |
469 | out_unmap: | |
470 | if (bio) { | |
471 | if (disk && bio->bi_bdev) | |
472 | bdput(bio->bi_bdev); | |
473 | blk_rq_unmap_user(bio); | |
474 | } | |
21d34711 CH |
475 | out: |
476 | blk_mq_free_request(req); | |
477 | return ret; | |
478 | } | |
479 | ||
0b7f1f26 KB |
480 | int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
481 | void __user *ubuffer, unsigned bufflen, u32 *result, | |
482 | unsigned timeout) | |
483 | { | |
484 | return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0, | |
485 | result, timeout); | |
486 | } | |
487 | ||
038bd4cb SG |
488 | static void nvme_keep_alive_end_io(struct request *rq, int error) |
489 | { | |
490 | struct nvme_ctrl *ctrl = rq->end_io_data; | |
491 | ||
492 | blk_mq_free_request(rq); | |
493 | ||
494 | if (error) { | |
495 | dev_err(ctrl->device, | |
496 | "failed nvme_keep_alive_end_io error=%d\n", error); | |
497 | return; | |
498 | } | |
499 | ||
500 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); | |
501 | } | |
502 | ||
503 | static int nvme_keep_alive(struct nvme_ctrl *ctrl) | |
504 | { | |
505 | struct nvme_command c; | |
506 | struct request *rq; | |
507 | ||
508 | memset(&c, 0, sizeof(c)); | |
509 | c.common.opcode = nvme_admin_keep_alive; | |
510 | ||
511 | rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED, | |
512 | NVME_QID_ANY); | |
513 | if (IS_ERR(rq)) | |
514 | return PTR_ERR(rq); | |
515 | ||
516 | rq->timeout = ctrl->kato * HZ; | |
517 | rq->end_io_data = ctrl; | |
518 | ||
519 | blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
524 | static void nvme_keep_alive_work(struct work_struct *work) | |
525 | { | |
526 | struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), | |
527 | struct nvme_ctrl, ka_work); | |
528 | ||
529 | if (nvme_keep_alive(ctrl)) { | |
530 | /* allocation failure, reset the controller */ | |
531 | dev_err(ctrl->device, "keep-alive failed\n"); | |
532 | ctrl->ops->reset_ctrl(ctrl); | |
533 | return; | |
534 | } | |
535 | } | |
536 | ||
537 | void nvme_start_keep_alive(struct nvme_ctrl *ctrl) | |
538 | { | |
539 | if (unlikely(ctrl->kato == 0)) | |
540 | return; | |
541 | ||
542 | INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); | |
543 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); | |
544 | } | |
545 | EXPORT_SYMBOL_GPL(nvme_start_keep_alive); | |
546 | ||
547 | void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) | |
548 | { | |
549 | if (unlikely(ctrl->kato == 0)) | |
550 | return; | |
551 | ||
552 | cancel_delayed_work_sync(&ctrl->ka_work); | |
553 | } | |
554 | EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); | |
555 | ||
1c63dc66 | 556 | int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) |
21d34711 CH |
557 | { |
558 | struct nvme_command c = { }; | |
559 | int error; | |
560 | ||
561 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ | |
562 | c.identify.opcode = nvme_admin_identify; | |
fa606826 | 563 | c.identify.cns = cpu_to_le32(NVME_ID_CNS_CTRL); |
21d34711 CH |
564 | |
565 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); | |
566 | if (!*id) | |
567 | return -ENOMEM; | |
568 | ||
569 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, | |
570 | sizeof(struct nvme_id_ctrl)); | |
571 | if (error) | |
572 | kfree(*id); | |
573 | return error; | |
574 | } | |
575 | ||
540c801c KB |
576 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
577 | { | |
578 | struct nvme_command c = { }; | |
579 | ||
580 | c.identify.opcode = nvme_admin_identify; | |
fa606826 | 581 | c.identify.cns = cpu_to_le32(NVME_ID_CNS_NS_ACTIVE_LIST); |
540c801c KB |
582 | c.identify.nsid = cpu_to_le32(nsid); |
583 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); | |
584 | } | |
585 | ||
1c63dc66 | 586 | int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid, |
21d34711 CH |
587 | struct nvme_id_ns **id) |
588 | { | |
589 | struct nvme_command c = { }; | |
590 | int error; | |
591 | ||
592 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ | |
593 | c.identify.opcode = nvme_admin_identify, | |
594 | c.identify.nsid = cpu_to_le32(nsid), | |
595 | ||
596 | *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); | |
597 | if (!*id) | |
598 | return -ENOMEM; | |
599 | ||
600 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, | |
601 | sizeof(struct nvme_id_ns)); | |
602 | if (error) | |
603 | kfree(*id); | |
604 | return error; | |
605 | } | |
606 | ||
1c63dc66 | 607 | int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid, |
1a6fe74d | 608 | void *buffer, size_t buflen, u32 *result) |
21d34711 CH |
609 | { |
610 | struct nvme_command c; | |
d49187e9 | 611 | union nvme_result res; |
1cb3cce5 | 612 | int ret; |
21d34711 CH |
613 | |
614 | memset(&c, 0, sizeof(c)); | |
615 | c.features.opcode = nvme_admin_get_features; | |
616 | c.features.nsid = cpu_to_le32(nsid); | |
21d34711 CH |
617 | c.features.fid = cpu_to_le32(fid); |
618 | ||
d49187e9 | 619 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0, |
eb71f435 | 620 | NVME_QID_ANY, 0, 0); |
9b47f77a | 621 | if (ret >= 0 && result) |
d49187e9 | 622 | *result = le32_to_cpu(res.u32); |
1cb3cce5 | 623 | return ret; |
21d34711 CH |
624 | } |
625 | ||
1c63dc66 | 626 | int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
1a6fe74d | 627 | void *buffer, size_t buflen, u32 *result) |
21d34711 CH |
628 | { |
629 | struct nvme_command c; | |
d49187e9 | 630 | union nvme_result res; |
1cb3cce5 | 631 | int ret; |
21d34711 CH |
632 | |
633 | memset(&c, 0, sizeof(c)); | |
634 | c.features.opcode = nvme_admin_set_features; | |
21d34711 CH |
635 | c.features.fid = cpu_to_le32(fid); |
636 | c.features.dword11 = cpu_to_le32(dword11); | |
637 | ||
d49187e9 | 638 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, |
1a6fe74d | 639 | buffer, buflen, 0, NVME_QID_ANY, 0, 0); |
9b47f77a | 640 | if (ret >= 0 && result) |
d49187e9 | 641 | *result = le32_to_cpu(res.u32); |
1cb3cce5 | 642 | return ret; |
21d34711 CH |
643 | } |
644 | ||
1c63dc66 | 645 | int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log) |
21d34711 CH |
646 | { |
647 | struct nvme_command c = { }; | |
648 | int error; | |
649 | ||
650 | c.common.opcode = nvme_admin_get_log_page, | |
651 | c.common.nsid = cpu_to_le32(0xFFFFFFFF), | |
652 | c.common.cdw10[0] = cpu_to_le32( | |
653 | (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | | |
654 | NVME_LOG_SMART), | |
655 | ||
656 | *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); | |
657 | if (!*log) | |
658 | return -ENOMEM; | |
659 | ||
660 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, | |
661 | sizeof(struct nvme_smart_log)); | |
662 | if (error) | |
663 | kfree(*log); | |
664 | return error; | |
665 | } | |
1673f1f0 | 666 | |
9a0be7ab CH |
667 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
668 | { | |
669 | u32 q_count = (*count - 1) | ((*count - 1) << 16); | |
670 | u32 result; | |
671 | int status, nr_io_queues; | |
672 | ||
1a6fe74d | 673 | status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0, |
9a0be7ab | 674 | &result); |
f5fa90dc | 675 | if (status < 0) |
9a0be7ab CH |
676 | return status; |
677 | ||
f5fa90dc CH |
678 | /* |
679 | * Degraded controllers might return an error when setting the queue | |
680 | * count. We still want to be able to bring them online and offer | |
681 | * access to the admin queue, as that might be only way to fix them up. | |
682 | */ | |
683 | if (status > 0) { | |
684 | dev_err(ctrl->dev, "Could not set queue count (%d)\n", status); | |
685 | *count = 0; | |
686 | } else { | |
687 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; | |
688 | *count = min(*count, nr_io_queues); | |
689 | } | |
690 | ||
9a0be7ab CH |
691 | return 0; |
692 | } | |
576d55d6 | 693 | EXPORT_SYMBOL_GPL(nvme_set_queue_count); |
9a0be7ab | 694 | |
1673f1f0 CH |
695 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
696 | { | |
697 | struct nvme_user_io io; | |
698 | struct nvme_command c; | |
699 | unsigned length, meta_len; | |
700 | void __user *metadata; | |
701 | ||
702 | if (copy_from_user(&io, uio, sizeof(io))) | |
703 | return -EFAULT; | |
63088ec7 KB |
704 | if (io.flags) |
705 | return -EINVAL; | |
1673f1f0 CH |
706 | |
707 | switch (io.opcode) { | |
708 | case nvme_cmd_write: | |
709 | case nvme_cmd_read: | |
710 | case nvme_cmd_compare: | |
711 | break; | |
712 | default: | |
713 | return -EINVAL; | |
714 | } | |
715 | ||
716 | length = (io.nblocks + 1) << ns->lba_shift; | |
717 | meta_len = (io.nblocks + 1) * ns->ms; | |
718 | metadata = (void __user *)(uintptr_t)io.metadata; | |
719 | ||
720 | if (ns->ext) { | |
721 | length += meta_len; | |
722 | meta_len = 0; | |
723 | } else if (meta_len) { | |
724 | if ((io.metadata & 3) || !io.metadata) | |
725 | return -EINVAL; | |
726 | } | |
727 | ||
728 | memset(&c, 0, sizeof(c)); | |
729 | c.rw.opcode = io.opcode; | |
730 | c.rw.flags = io.flags; | |
731 | c.rw.nsid = cpu_to_le32(ns->ns_id); | |
732 | c.rw.slba = cpu_to_le64(io.slba); | |
733 | c.rw.length = cpu_to_le16(io.nblocks); | |
734 | c.rw.control = cpu_to_le16(io.control); | |
735 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); | |
736 | c.rw.reftag = cpu_to_le32(io.reftag); | |
737 | c.rw.apptag = cpu_to_le16(io.apptag); | |
738 | c.rw.appmask = cpu_to_le16(io.appmask); | |
739 | ||
740 | return __nvme_submit_user_cmd(ns->queue, &c, | |
741 | (void __user *)(uintptr_t)io.addr, length, | |
742 | metadata, meta_len, io.slba, NULL, 0); | |
743 | } | |
744 | ||
f3ca80fc | 745 | static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
1673f1f0 CH |
746 | struct nvme_passthru_cmd __user *ucmd) |
747 | { | |
748 | struct nvme_passthru_cmd cmd; | |
749 | struct nvme_command c; | |
750 | unsigned timeout = 0; | |
751 | int status; | |
752 | ||
753 | if (!capable(CAP_SYS_ADMIN)) | |
754 | return -EACCES; | |
755 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) | |
756 | return -EFAULT; | |
63088ec7 KB |
757 | if (cmd.flags) |
758 | return -EINVAL; | |
1673f1f0 CH |
759 | |
760 | memset(&c, 0, sizeof(c)); | |
761 | c.common.opcode = cmd.opcode; | |
762 | c.common.flags = cmd.flags; | |
763 | c.common.nsid = cpu_to_le32(cmd.nsid); | |
764 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); | |
765 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); | |
766 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); | |
767 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); | |
768 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); | |
769 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); | |
770 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); | |
771 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); | |
772 | ||
773 | if (cmd.timeout_ms) | |
774 | timeout = msecs_to_jiffies(cmd.timeout_ms); | |
775 | ||
776 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, | |
d1ea7be5 | 777 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
1673f1f0 CH |
778 | &cmd.result, timeout); |
779 | if (status >= 0) { | |
780 | if (put_user(cmd.result, &ucmd->result)) | |
781 | return -EFAULT; | |
782 | } | |
783 | ||
784 | return status; | |
785 | } | |
786 | ||
787 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, | |
788 | unsigned int cmd, unsigned long arg) | |
789 | { | |
790 | struct nvme_ns *ns = bdev->bd_disk->private_data; | |
791 | ||
792 | switch (cmd) { | |
793 | case NVME_IOCTL_ID: | |
794 | force_successful_syscall_return(); | |
795 | return ns->ns_id; | |
796 | case NVME_IOCTL_ADMIN_CMD: | |
797 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); | |
798 | case NVME_IOCTL_IO_CMD: | |
799 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); | |
800 | case NVME_IOCTL_SUBMIT_IO: | |
801 | return nvme_submit_io(ns, (void __user *)arg); | |
44907332 | 802 | #ifdef CONFIG_BLK_DEV_NVME_SCSI |
1673f1f0 CH |
803 | case SG_GET_VERSION_NUM: |
804 | return nvme_sg_get_version_num((void __user *)arg); | |
805 | case SG_IO: | |
806 | return nvme_sg_io(ns, (void __user *)arg); | |
44907332 | 807 | #endif |
1673f1f0 CH |
808 | default: |
809 | return -ENOTTY; | |
810 | } | |
811 | } | |
812 | ||
813 | #ifdef CONFIG_COMPAT | |
814 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
815 | unsigned int cmd, unsigned long arg) | |
816 | { | |
817 | switch (cmd) { | |
818 | case SG_IO: | |
819 | return -ENOIOCTLCMD; | |
820 | } | |
821 | return nvme_ioctl(bdev, mode, cmd, arg); | |
822 | } | |
823 | #else | |
824 | #define nvme_compat_ioctl NULL | |
825 | #endif | |
826 | ||
827 | static int nvme_open(struct block_device *bdev, fmode_t mode) | |
828 | { | |
829 | return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO; | |
830 | } | |
831 | ||
832 | static void nvme_release(struct gendisk *disk, fmode_t mode) | |
833 | { | |
e439bb12 SG |
834 | struct nvme_ns *ns = disk->private_data; |
835 | ||
836 | module_put(ns->ctrl->ops->module); | |
837 | nvme_put_ns(ns); | |
1673f1f0 CH |
838 | } |
839 | ||
840 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) | |
841 | { | |
842 | /* some standard values */ | |
843 | geo->heads = 1 << 6; | |
844 | geo->sectors = 1 << 5; | |
845 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; | |
846 | return 0; | |
847 | } | |
848 | ||
849 | #ifdef CONFIG_BLK_DEV_INTEGRITY | |
850 | static void nvme_init_integrity(struct nvme_ns *ns) | |
851 | { | |
852 | struct blk_integrity integrity; | |
853 | ||
fa9a89fc | 854 | memset(&integrity, 0, sizeof(integrity)); |
1673f1f0 CH |
855 | switch (ns->pi_type) { |
856 | case NVME_NS_DPS_PI_TYPE3: | |
857 | integrity.profile = &t10_pi_type3_crc; | |
ba36c21b NB |
858 | integrity.tag_size = sizeof(u16) + sizeof(u32); |
859 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; | |
1673f1f0 CH |
860 | break; |
861 | case NVME_NS_DPS_PI_TYPE1: | |
862 | case NVME_NS_DPS_PI_TYPE2: | |
863 | integrity.profile = &t10_pi_type1_crc; | |
ba36c21b NB |
864 | integrity.tag_size = sizeof(u16); |
865 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; | |
1673f1f0 CH |
866 | break; |
867 | default: | |
868 | integrity.profile = NULL; | |
869 | break; | |
870 | } | |
871 | integrity.tuple_size = ns->ms; | |
872 | blk_integrity_register(ns->disk, &integrity); | |
873 | blk_queue_max_integrity_segments(ns->queue, 1); | |
874 | } | |
875 | #else | |
876 | static void nvme_init_integrity(struct nvme_ns *ns) | |
877 | { | |
878 | } | |
879 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
880 | ||
881 | static void nvme_config_discard(struct nvme_ns *ns) | |
882 | { | |
08095e70 | 883 | struct nvme_ctrl *ctrl = ns->ctrl; |
1673f1f0 | 884 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
08095e70 | 885 | |
b35ba01e CH |
886 | BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < |
887 | NVME_DSM_MAX_RANGES); | |
888 | ||
08095e70 KB |
889 | if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES) |
890 | ns->queue->limits.discard_zeroes_data = 1; | |
891 | else | |
892 | ns->queue->limits.discard_zeroes_data = 0; | |
893 | ||
1673f1f0 CH |
894 | ns->queue->limits.discard_alignment = logical_block_size; |
895 | ns->queue->limits.discard_granularity = logical_block_size; | |
bd0fc288 | 896 | blk_queue_max_discard_sectors(ns->queue, UINT_MAX); |
b35ba01e | 897 | blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES); |
1673f1f0 CH |
898 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
899 | } | |
900 | ||
ac81bfa9 | 901 | static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id) |
1673f1f0 | 902 | { |
ac81bfa9 | 903 | if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) { |
b0b4e09c | 904 | dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__); |
1673f1f0 CH |
905 | return -ENODEV; |
906 | } | |
1673f1f0 | 907 | |
ac81bfa9 MB |
908 | if ((*id)->ncap == 0) { |
909 | kfree(*id); | |
910 | return -ENODEV; | |
1673f1f0 CH |
911 | } |
912 | ||
8ef2074d | 913 | if (ns->ctrl->vs >= NVME_VS(1, 1, 0)) |
ac81bfa9 | 914 | memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui)); |
8ef2074d | 915 | if (ns->ctrl->vs >= NVME_VS(1, 2, 0)) |
ac81bfa9 MB |
916 | memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid)); |
917 | ||
918 | return 0; | |
919 | } | |
920 | ||
921 | static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) | |
922 | { | |
923 | struct nvme_ns *ns = disk->private_data; | |
924 | u8 lbaf, pi_type; | |
925 | u16 old_ms; | |
926 | unsigned short bs; | |
2b9b6e86 | 927 | |
1673f1f0 CH |
928 | old_ms = ns->ms; |
929 | lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK; | |
930 | ns->lba_shift = id->lbaf[lbaf].ds; | |
931 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); | |
932 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); | |
933 | ||
934 | /* | |
935 | * If identify namespace failed, use default 512 byte block size so | |
936 | * block layer can use before failing read/write for 0 capacity. | |
937 | */ | |
938 | if (ns->lba_shift == 0) | |
939 | ns->lba_shift = 9; | |
940 | bs = 1 << ns->lba_shift; | |
1673f1f0 CH |
941 | /* XXX: PI implementation requires metadata equal t10 pi tuple size */ |
942 | pi_type = ns->ms == sizeof(struct t10_pi_tuple) ? | |
943 | id->dps & NVME_NS_DPS_PI_MASK : 0; | |
944 | ||
945 | blk_mq_freeze_queue(disk->queue); | |
946 | if (blk_get_integrity(disk) && (ns->pi_type != pi_type || | |
947 | ns->ms != old_ms || | |
948 | bs != queue_logical_block_size(disk->queue) || | |
949 | (ns->ms && ns->ext))) | |
950 | blk_integrity_unregister(disk); | |
951 | ||
952 | ns->pi_type = pi_type; | |
953 | blk_queue_logical_block_size(ns->queue, bs); | |
954 | ||
4b9d5b15 | 955 | if (ns->ms && !blk_get_integrity(disk) && !ns->ext) |
1673f1f0 | 956 | nvme_init_integrity(ns); |
1673f1f0 CH |
957 | if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk)) |
958 | set_capacity(disk, 0); | |
959 | else | |
960 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); | |
961 | ||
962 | if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) | |
963 | nvme_config_discard(ns); | |
964 | blk_mq_unfreeze_queue(disk->queue); | |
ac81bfa9 | 965 | } |
1673f1f0 | 966 | |
ac81bfa9 MB |
967 | static int nvme_revalidate_disk(struct gendisk *disk) |
968 | { | |
969 | struct nvme_ns *ns = disk->private_data; | |
970 | struct nvme_id_ns *id = NULL; | |
971 | int ret; | |
972 | ||
973 | if (test_bit(NVME_NS_DEAD, &ns->flags)) { | |
974 | set_capacity(disk, 0); | |
975 | return -ENODEV; | |
976 | } | |
977 | ||
978 | ret = nvme_revalidate_ns(ns, &id); | |
979 | if (ret) | |
980 | return ret; | |
981 | ||
982 | __nvme_revalidate_disk(disk, id); | |
1673f1f0 | 983 | kfree(id); |
ac81bfa9 | 984 | |
1673f1f0 CH |
985 | return 0; |
986 | } | |
987 | ||
988 | static char nvme_pr_type(enum pr_type type) | |
989 | { | |
990 | switch (type) { | |
991 | case PR_WRITE_EXCLUSIVE: | |
992 | return 1; | |
993 | case PR_EXCLUSIVE_ACCESS: | |
994 | return 2; | |
995 | case PR_WRITE_EXCLUSIVE_REG_ONLY: | |
996 | return 3; | |
997 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: | |
998 | return 4; | |
999 | case PR_WRITE_EXCLUSIVE_ALL_REGS: | |
1000 | return 5; | |
1001 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: | |
1002 | return 6; | |
1003 | default: | |
1004 | return 0; | |
1005 | } | |
1006 | }; | |
1007 | ||
1008 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, | |
1009 | u64 key, u64 sa_key, u8 op) | |
1010 | { | |
1011 | struct nvme_ns *ns = bdev->bd_disk->private_data; | |
1012 | struct nvme_command c; | |
1013 | u8 data[16] = { 0, }; | |
1014 | ||
1015 | put_unaligned_le64(key, &data[0]); | |
1016 | put_unaligned_le64(sa_key, &data[8]); | |
1017 | ||
1018 | memset(&c, 0, sizeof(c)); | |
1019 | c.common.opcode = op; | |
1020 | c.common.nsid = cpu_to_le32(ns->ns_id); | |
1021 | c.common.cdw10[0] = cpu_to_le32(cdw10); | |
1022 | ||
1023 | return nvme_submit_sync_cmd(ns->queue, &c, data, 16); | |
1024 | } | |
1025 | ||
1026 | static int nvme_pr_register(struct block_device *bdev, u64 old, | |
1027 | u64 new, unsigned flags) | |
1028 | { | |
1029 | u32 cdw10; | |
1030 | ||
1031 | if (flags & ~PR_FL_IGNORE_KEY) | |
1032 | return -EOPNOTSUPP; | |
1033 | ||
1034 | cdw10 = old ? 2 : 0; | |
1035 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; | |
1036 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ | |
1037 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); | |
1038 | } | |
1039 | ||
1040 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, | |
1041 | enum pr_type type, unsigned flags) | |
1042 | { | |
1043 | u32 cdw10; | |
1044 | ||
1045 | if (flags & ~PR_FL_IGNORE_KEY) | |
1046 | return -EOPNOTSUPP; | |
1047 | ||
1048 | cdw10 = nvme_pr_type(type) << 8; | |
1049 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); | |
1050 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); | |
1051 | } | |
1052 | ||
1053 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, | |
1054 | enum pr_type type, bool abort) | |
1055 | { | |
1056 | u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; | |
1057 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); | |
1058 | } | |
1059 | ||
1060 | static int nvme_pr_clear(struct block_device *bdev, u64 key) | |
1061 | { | |
8c0b3915 | 1062 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
1673f1f0 CH |
1063 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
1064 | } | |
1065 | ||
1066 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) | |
1067 | { | |
1068 | u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; | |
1069 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); | |
1070 | } | |
1071 | ||
1072 | static const struct pr_ops nvme_pr_ops = { | |
1073 | .pr_register = nvme_pr_register, | |
1074 | .pr_reserve = nvme_pr_reserve, | |
1075 | .pr_release = nvme_pr_release, | |
1076 | .pr_preempt = nvme_pr_preempt, | |
1077 | .pr_clear = nvme_pr_clear, | |
1078 | }; | |
1079 | ||
5bae7f73 | 1080 | static const struct block_device_operations nvme_fops = { |
1673f1f0 CH |
1081 | .owner = THIS_MODULE, |
1082 | .ioctl = nvme_ioctl, | |
1083 | .compat_ioctl = nvme_compat_ioctl, | |
1084 | .open = nvme_open, | |
1085 | .release = nvme_release, | |
1086 | .getgeo = nvme_getgeo, | |
1087 | .revalidate_disk= nvme_revalidate_disk, | |
1088 | .pr_ops = &nvme_pr_ops, | |
1089 | }; | |
1090 | ||
5fd4ce1b CH |
1091 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
1092 | { | |
1093 | unsigned long timeout = | |
1094 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; | |
1095 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; | |
1096 | int ret; | |
1097 | ||
1098 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { | |
0df1e4f5 KB |
1099 | if (csts == ~0) |
1100 | return -ENODEV; | |
5fd4ce1b CH |
1101 | if ((csts & NVME_CSTS_RDY) == bit) |
1102 | break; | |
1103 | ||
1104 | msleep(100); | |
1105 | if (fatal_signal_pending(current)) | |
1106 | return -EINTR; | |
1107 | if (time_after(jiffies, timeout)) { | |
1b3c47c1 | 1108 | dev_err(ctrl->device, |
5fd4ce1b CH |
1109 | "Device not ready; aborting %s\n", enabled ? |
1110 | "initialisation" : "reset"); | |
1111 | return -ENODEV; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | return ret; | |
1116 | } | |
1117 | ||
1118 | /* | |
1119 | * If the device has been passed off to us in an enabled state, just clear | |
1120 | * the enabled bit. The spec says we should set the 'shutdown notification | |
1121 | * bits', but doing so may cause the device to complete commands to the | |
1122 | * admin queue ... and we don't know what memory that might be pointing at! | |
1123 | */ | |
1124 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) | |
1125 | { | |
1126 | int ret; | |
1127 | ||
1128 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; | |
1129 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; | |
1130 | ||
1131 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
1132 | if (ret) | |
1133 | return ret; | |
54adc010 | 1134 | |
b5a10c5f | 1135 | if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) |
54adc010 GP |
1136 | msleep(NVME_QUIRK_DELAY_AMOUNT); |
1137 | ||
5fd4ce1b CH |
1138 | return nvme_wait_ready(ctrl, cap, false); |
1139 | } | |
576d55d6 | 1140 | EXPORT_SYMBOL_GPL(nvme_disable_ctrl); |
5fd4ce1b CH |
1141 | |
1142 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) | |
1143 | { | |
1144 | /* | |
1145 | * Default to a 4K page size, with the intention to update this | |
1146 | * path in the future to accomodate architectures with differing | |
1147 | * kernel and IO page sizes. | |
1148 | */ | |
1149 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; | |
1150 | int ret; | |
1151 | ||
1152 | if (page_shift < dev_page_min) { | |
1b3c47c1 | 1153 | dev_err(ctrl->device, |
5fd4ce1b CH |
1154 | "Minimum device page size %u too large for host (%u)\n", |
1155 | 1 << dev_page_min, 1 << page_shift); | |
1156 | return -ENODEV; | |
1157 | } | |
1158 | ||
1159 | ctrl->page_size = 1 << page_shift; | |
1160 | ||
1161 | ctrl->ctrl_config = NVME_CC_CSS_NVM; | |
1162 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; | |
1163 | ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; | |
1164 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; | |
1165 | ctrl->ctrl_config |= NVME_CC_ENABLE; | |
1166 | ||
1167 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
1168 | if (ret) | |
1169 | return ret; | |
1170 | return nvme_wait_ready(ctrl, cap, true); | |
1171 | } | |
576d55d6 | 1172 | EXPORT_SYMBOL_GPL(nvme_enable_ctrl); |
5fd4ce1b CH |
1173 | |
1174 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) | |
1175 | { | |
1176 | unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies; | |
1177 | u32 csts; | |
1178 | int ret; | |
1179 | ||
1180 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; | |
1181 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; | |
1182 | ||
1183 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
1184 | if (ret) | |
1185 | return ret; | |
1186 | ||
1187 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { | |
1188 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) | |
1189 | break; | |
1190 | ||
1191 | msleep(100); | |
1192 | if (fatal_signal_pending(current)) | |
1193 | return -EINTR; | |
1194 | if (time_after(jiffies, timeout)) { | |
1b3c47c1 | 1195 | dev_err(ctrl->device, |
5fd4ce1b CH |
1196 | "Device shutdown incomplete; abort shutdown\n"); |
1197 | return -ENODEV; | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | return ret; | |
1202 | } | |
576d55d6 | 1203 | EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); |
5fd4ce1b | 1204 | |
da35825d CH |
1205 | static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, |
1206 | struct request_queue *q) | |
1207 | { | |
7c88cb00 JA |
1208 | bool vwc = false; |
1209 | ||
da35825d | 1210 | if (ctrl->max_hw_sectors) { |
45686b61 CH |
1211 | u32 max_segments = |
1212 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; | |
1213 | ||
da35825d | 1214 | blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); |
45686b61 | 1215 | blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); |
da35825d | 1216 | } |
e6282aef KB |
1217 | if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) |
1218 | blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); | |
da35825d | 1219 | blk_queue_virt_boundary(q, ctrl->page_size - 1); |
7c88cb00 JA |
1220 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
1221 | vwc = true; | |
1222 | blk_queue_write_cache(q, vwc, vwc); | |
da35825d CH |
1223 | } |
1224 | ||
7fd8930f CH |
1225 | /* |
1226 | * Initialize the cached copies of the Identify data and various controller | |
1227 | * register in our nvme_ctrl structure. This should be called as soon as | |
1228 | * the admin queue is fully up and running. | |
1229 | */ | |
1230 | int nvme_init_identify(struct nvme_ctrl *ctrl) | |
1231 | { | |
1232 | struct nvme_id_ctrl *id; | |
1233 | u64 cap; | |
1234 | int ret, page_shift; | |
a229dbf6 | 1235 | u32 max_hw_sectors; |
7fd8930f | 1236 | |
f3ca80fc CH |
1237 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
1238 | if (ret) { | |
1b3c47c1 | 1239 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
f3ca80fc CH |
1240 | return ret; |
1241 | } | |
1242 | ||
7fd8930f CH |
1243 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
1244 | if (ret) { | |
1b3c47c1 | 1245 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
7fd8930f CH |
1246 | return ret; |
1247 | } | |
1248 | page_shift = NVME_CAP_MPSMIN(cap) + 12; | |
1249 | ||
8ef2074d | 1250 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
f3ca80fc CH |
1251 | ctrl->subsystem = NVME_CAP_NSSRC(cap); |
1252 | ||
7fd8930f CH |
1253 | ret = nvme_identify_ctrl(ctrl, &id); |
1254 | if (ret) { | |
1b3c47c1 | 1255 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
7fd8930f CH |
1256 | return -EIO; |
1257 | } | |
1258 | ||
118472ab | 1259 | ctrl->vid = le16_to_cpu(id->vid); |
7fd8930f | 1260 | ctrl->oncs = le16_to_cpup(&id->oncs); |
6bf25d16 | 1261 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
7fd8930f | 1262 | ctrl->vwc = id->vwc; |
931e1c22 | 1263 | ctrl->cntlid = le16_to_cpup(&id->cntlid); |
7fd8930f CH |
1264 | memcpy(ctrl->serial, id->sn, sizeof(id->sn)); |
1265 | memcpy(ctrl->model, id->mn, sizeof(id->mn)); | |
1266 | memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr)); | |
1267 | if (id->mdts) | |
a229dbf6 | 1268 | max_hw_sectors = 1 << (id->mdts + page_shift - 9); |
7fd8930f | 1269 | else |
a229dbf6 CH |
1270 | max_hw_sectors = UINT_MAX; |
1271 | ctrl->max_hw_sectors = | |
1272 | min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); | |
7fd8930f | 1273 | |
da35825d | 1274 | nvme_set_queue_limits(ctrl, ctrl->admin_q); |
07bfcd09 | 1275 | ctrl->sgls = le32_to_cpu(id->sgls); |
038bd4cb | 1276 | ctrl->kas = le16_to_cpu(id->kas); |
07bfcd09 CH |
1277 | |
1278 | if (ctrl->ops->is_fabrics) { | |
1279 | ctrl->icdoff = le16_to_cpu(id->icdoff); | |
1280 | ctrl->ioccsz = le32_to_cpu(id->ioccsz); | |
1281 | ctrl->iorcsz = le32_to_cpu(id->iorcsz); | |
1282 | ctrl->maxcmd = le16_to_cpu(id->maxcmd); | |
1283 | ||
1284 | /* | |
1285 | * In fabrics we need to verify the cntlid matches the | |
1286 | * admin connect | |
1287 | */ | |
1288 | if (ctrl->cntlid != le16_to_cpu(id->cntlid)) | |
1289 | ret = -EINVAL; | |
038bd4cb SG |
1290 | |
1291 | if (!ctrl->opts->discovery_nqn && !ctrl->kas) { | |
1292 | dev_err(ctrl->dev, | |
1293 | "keep-alive support is mandatory for fabrics\n"); | |
1294 | ret = -EINVAL; | |
1295 | } | |
07bfcd09 CH |
1296 | } else { |
1297 | ctrl->cntlid = le16_to_cpu(id->cntlid); | |
1298 | } | |
da35825d | 1299 | |
7fd8930f | 1300 | kfree(id); |
07bfcd09 | 1301 | return ret; |
7fd8930f | 1302 | } |
576d55d6 | 1303 | EXPORT_SYMBOL_GPL(nvme_init_identify); |
7fd8930f | 1304 | |
f3ca80fc | 1305 | static int nvme_dev_open(struct inode *inode, struct file *file) |
1673f1f0 | 1306 | { |
f3ca80fc CH |
1307 | struct nvme_ctrl *ctrl; |
1308 | int instance = iminor(inode); | |
1309 | int ret = -ENODEV; | |
1673f1f0 | 1310 | |
f3ca80fc CH |
1311 | spin_lock(&dev_list_lock); |
1312 | list_for_each_entry(ctrl, &nvme_ctrl_list, node) { | |
1313 | if (ctrl->instance != instance) | |
1314 | continue; | |
1315 | ||
1316 | if (!ctrl->admin_q) { | |
1317 | ret = -EWOULDBLOCK; | |
1318 | break; | |
1319 | } | |
1320 | if (!kref_get_unless_zero(&ctrl->kref)) | |
1321 | break; | |
1322 | file->private_data = ctrl; | |
1323 | ret = 0; | |
1324 | break; | |
1325 | } | |
1326 | spin_unlock(&dev_list_lock); | |
1327 | ||
1328 | return ret; | |
1673f1f0 CH |
1329 | } |
1330 | ||
f3ca80fc | 1331 | static int nvme_dev_release(struct inode *inode, struct file *file) |
1673f1f0 | 1332 | { |
f3ca80fc CH |
1333 | nvme_put_ctrl(file->private_data); |
1334 | return 0; | |
1335 | } | |
1336 | ||
bfd89471 CH |
1337 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
1338 | { | |
1339 | struct nvme_ns *ns; | |
1340 | int ret; | |
1341 | ||
1342 | mutex_lock(&ctrl->namespaces_mutex); | |
1343 | if (list_empty(&ctrl->namespaces)) { | |
1344 | ret = -ENOTTY; | |
1345 | goto out_unlock; | |
1346 | } | |
1347 | ||
1348 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); | |
1349 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { | |
1b3c47c1 | 1350 | dev_warn(ctrl->device, |
bfd89471 CH |
1351 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
1352 | ret = -EINVAL; | |
1353 | goto out_unlock; | |
1354 | } | |
1355 | ||
1b3c47c1 | 1356 | dev_warn(ctrl->device, |
bfd89471 CH |
1357 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
1358 | kref_get(&ns->kref); | |
1359 | mutex_unlock(&ctrl->namespaces_mutex); | |
1360 | ||
1361 | ret = nvme_user_cmd(ctrl, ns, argp); | |
1362 | nvme_put_ns(ns); | |
1363 | return ret; | |
1364 | ||
1365 | out_unlock: | |
1366 | mutex_unlock(&ctrl->namespaces_mutex); | |
1367 | return ret; | |
1368 | } | |
1369 | ||
f3ca80fc CH |
1370 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
1371 | unsigned long arg) | |
1372 | { | |
1373 | struct nvme_ctrl *ctrl = file->private_data; | |
1374 | void __user *argp = (void __user *)arg; | |
f3ca80fc CH |
1375 | |
1376 | switch (cmd) { | |
1377 | case NVME_IOCTL_ADMIN_CMD: | |
1378 | return nvme_user_cmd(ctrl, NULL, argp); | |
1379 | case NVME_IOCTL_IO_CMD: | |
bfd89471 | 1380 | return nvme_dev_user_cmd(ctrl, argp); |
f3ca80fc | 1381 | case NVME_IOCTL_RESET: |
1b3c47c1 | 1382 | dev_warn(ctrl->device, "resetting controller\n"); |
f3ca80fc CH |
1383 | return ctrl->ops->reset_ctrl(ctrl); |
1384 | case NVME_IOCTL_SUBSYS_RESET: | |
1385 | return nvme_reset_subsystem(ctrl); | |
9ec3bb2f KB |
1386 | case NVME_IOCTL_RESCAN: |
1387 | nvme_queue_scan(ctrl); | |
1388 | return 0; | |
f3ca80fc CH |
1389 | default: |
1390 | return -ENOTTY; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | static const struct file_operations nvme_dev_fops = { | |
1395 | .owner = THIS_MODULE, | |
1396 | .open = nvme_dev_open, | |
1397 | .release = nvme_dev_release, | |
1398 | .unlocked_ioctl = nvme_dev_ioctl, | |
1399 | .compat_ioctl = nvme_dev_ioctl, | |
1400 | }; | |
1401 | ||
1402 | static ssize_t nvme_sysfs_reset(struct device *dev, | |
1403 | struct device_attribute *attr, const char *buf, | |
1404 | size_t count) | |
1405 | { | |
1406 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1407 | int ret; | |
1408 | ||
1409 | ret = ctrl->ops->reset_ctrl(ctrl); | |
1410 | if (ret < 0) | |
1411 | return ret; | |
1412 | return count; | |
1673f1f0 | 1413 | } |
f3ca80fc | 1414 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
1673f1f0 | 1415 | |
9ec3bb2f KB |
1416 | static ssize_t nvme_sysfs_rescan(struct device *dev, |
1417 | struct device_attribute *attr, const char *buf, | |
1418 | size_t count) | |
1419 | { | |
1420 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1421 | ||
1422 | nvme_queue_scan(ctrl); | |
1423 | return count; | |
1424 | } | |
1425 | static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); | |
1426 | ||
118472ab KB |
1427 | static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, |
1428 | char *buf) | |
1429 | { | |
40267efd | 1430 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
118472ab KB |
1431 | struct nvme_ctrl *ctrl = ns->ctrl; |
1432 | int serial_len = sizeof(ctrl->serial); | |
1433 | int model_len = sizeof(ctrl->model); | |
1434 | ||
1435 | if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) | |
1436 | return sprintf(buf, "eui.%16phN\n", ns->uuid); | |
1437 | ||
1438 | if (memchr_inv(ns->eui, 0, sizeof(ns->eui))) | |
1439 | return sprintf(buf, "eui.%8phN\n", ns->eui); | |
1440 | ||
1441 | while (ctrl->serial[serial_len - 1] == ' ') | |
1442 | serial_len--; | |
1443 | while (ctrl->model[model_len - 1] == ' ') | |
1444 | model_len--; | |
1445 | ||
1446 | return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid, | |
1447 | serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id); | |
1448 | } | |
1449 | static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL); | |
1450 | ||
2b9b6e86 KB |
1451 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
1452 | char *buf) | |
1453 | { | |
40267efd | 1454 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
2b9b6e86 KB |
1455 | return sprintf(buf, "%pU\n", ns->uuid); |
1456 | } | |
1457 | static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); | |
1458 | ||
1459 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, | |
1460 | char *buf) | |
1461 | { | |
40267efd | 1462 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
2b9b6e86 KB |
1463 | return sprintf(buf, "%8phd\n", ns->eui); |
1464 | } | |
1465 | static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); | |
1466 | ||
1467 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, | |
1468 | char *buf) | |
1469 | { | |
40267efd | 1470 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
2b9b6e86 KB |
1471 | return sprintf(buf, "%d\n", ns->ns_id); |
1472 | } | |
1473 | static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); | |
1474 | ||
1475 | static struct attribute *nvme_ns_attrs[] = { | |
118472ab | 1476 | &dev_attr_wwid.attr, |
2b9b6e86 KB |
1477 | &dev_attr_uuid.attr, |
1478 | &dev_attr_eui.attr, | |
1479 | &dev_attr_nsid.attr, | |
1480 | NULL, | |
1481 | }; | |
1482 | ||
1a353d85 | 1483 | static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj, |
2b9b6e86 KB |
1484 | struct attribute *a, int n) |
1485 | { | |
1486 | struct device *dev = container_of(kobj, struct device, kobj); | |
40267efd | 1487 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
2b9b6e86 KB |
1488 | |
1489 | if (a == &dev_attr_uuid.attr) { | |
1490 | if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) | |
1491 | return 0; | |
1492 | } | |
1493 | if (a == &dev_attr_eui.attr) { | |
1494 | if (!memchr_inv(ns->eui, 0, sizeof(ns->eui))) | |
1495 | return 0; | |
1496 | } | |
1497 | return a->mode; | |
1498 | } | |
1499 | ||
1500 | static const struct attribute_group nvme_ns_attr_group = { | |
1501 | .attrs = nvme_ns_attrs, | |
1a353d85 | 1502 | .is_visible = nvme_ns_attrs_are_visible, |
2b9b6e86 KB |
1503 | }; |
1504 | ||
931e1c22 | 1505 | #define nvme_show_str_function(field) \ |
779ff756 KB |
1506 | static ssize_t field##_show(struct device *dev, \ |
1507 | struct device_attribute *attr, char *buf) \ | |
1508 | { \ | |
1509 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ | |
1510 | return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \ | |
1511 | } \ | |
1512 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); | |
1513 | ||
931e1c22 ML |
1514 | #define nvme_show_int_function(field) \ |
1515 | static ssize_t field##_show(struct device *dev, \ | |
1516 | struct device_attribute *attr, char *buf) \ | |
1517 | { \ | |
1518 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ | |
1519 | return sprintf(buf, "%d\n", ctrl->field); \ | |
1520 | } \ | |
1521 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); | |
1522 | ||
1523 | nvme_show_str_function(model); | |
1524 | nvme_show_str_function(serial); | |
1525 | nvme_show_str_function(firmware_rev); | |
1526 | nvme_show_int_function(cntlid); | |
779ff756 | 1527 | |
1a353d85 ML |
1528 | static ssize_t nvme_sysfs_delete(struct device *dev, |
1529 | struct device_attribute *attr, const char *buf, | |
1530 | size_t count) | |
1531 | { | |
1532 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1533 | ||
1534 | if (device_remove_file_self(dev, attr)) | |
1535 | ctrl->ops->delete_ctrl(ctrl); | |
1536 | return count; | |
1537 | } | |
1538 | static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); | |
1539 | ||
1540 | static ssize_t nvme_sysfs_show_transport(struct device *dev, | |
1541 | struct device_attribute *attr, | |
1542 | char *buf) | |
1543 | { | |
1544 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1545 | ||
1546 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); | |
1547 | } | |
1548 | static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); | |
1549 | ||
1550 | static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, | |
1551 | struct device_attribute *attr, | |
1552 | char *buf) | |
1553 | { | |
1554 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1555 | ||
1556 | return snprintf(buf, PAGE_SIZE, "%s\n", | |
1557 | ctrl->ops->get_subsysnqn(ctrl)); | |
1558 | } | |
1559 | static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); | |
1560 | ||
1561 | static ssize_t nvme_sysfs_show_address(struct device *dev, | |
1562 | struct device_attribute *attr, | |
1563 | char *buf) | |
1564 | { | |
1565 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1566 | ||
1567 | return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); | |
1568 | } | |
1569 | static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); | |
1570 | ||
779ff756 KB |
1571 | static struct attribute *nvme_dev_attrs[] = { |
1572 | &dev_attr_reset_controller.attr, | |
9ec3bb2f | 1573 | &dev_attr_rescan_controller.attr, |
779ff756 KB |
1574 | &dev_attr_model.attr, |
1575 | &dev_attr_serial.attr, | |
1576 | &dev_attr_firmware_rev.attr, | |
931e1c22 | 1577 | &dev_attr_cntlid.attr, |
1a353d85 ML |
1578 | &dev_attr_delete_controller.attr, |
1579 | &dev_attr_transport.attr, | |
1580 | &dev_attr_subsysnqn.attr, | |
1581 | &dev_attr_address.attr, | |
779ff756 KB |
1582 | NULL |
1583 | }; | |
1584 | ||
1a353d85 ML |
1585 | #define CHECK_ATTR(ctrl, a, name) \ |
1586 | if ((a) == &dev_attr_##name.attr && \ | |
1587 | !(ctrl)->ops->get_##name) \ | |
1588 | return 0 | |
1589 | ||
1590 | static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, | |
1591 | struct attribute *a, int n) | |
1592 | { | |
1593 | struct device *dev = container_of(kobj, struct device, kobj); | |
1594 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1595 | ||
1596 | if (a == &dev_attr_delete_controller.attr) { | |
1597 | if (!ctrl->ops->delete_ctrl) | |
1598 | return 0; | |
1599 | } | |
1600 | ||
1601 | CHECK_ATTR(ctrl, a, subsysnqn); | |
1602 | CHECK_ATTR(ctrl, a, address); | |
1603 | ||
1604 | return a->mode; | |
1605 | } | |
1606 | ||
779ff756 | 1607 | static struct attribute_group nvme_dev_attrs_group = { |
1a353d85 ML |
1608 | .attrs = nvme_dev_attrs, |
1609 | .is_visible = nvme_dev_attrs_are_visible, | |
779ff756 KB |
1610 | }; |
1611 | ||
1612 | static const struct attribute_group *nvme_dev_attr_groups[] = { | |
1613 | &nvme_dev_attrs_group, | |
1614 | NULL, | |
1615 | }; | |
1616 | ||
5bae7f73 CH |
1617 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
1618 | { | |
1619 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); | |
1620 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); | |
1621 | ||
1622 | return nsa->ns_id - nsb->ns_id; | |
1623 | } | |
1624 | ||
32f0c4af | 1625 | static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
5bae7f73 | 1626 | { |
32f0c4af | 1627 | struct nvme_ns *ns, *ret = NULL; |
69d3b8ac | 1628 | |
32f0c4af | 1629 | mutex_lock(&ctrl->namespaces_mutex); |
5bae7f73 | 1630 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
32f0c4af KB |
1631 | if (ns->ns_id == nsid) { |
1632 | kref_get(&ns->kref); | |
1633 | ret = ns; | |
1634 | break; | |
1635 | } | |
5bae7f73 CH |
1636 | if (ns->ns_id > nsid) |
1637 | break; | |
1638 | } | |
32f0c4af KB |
1639 | mutex_unlock(&ctrl->namespaces_mutex); |
1640 | return ret; | |
5bae7f73 CH |
1641 | } |
1642 | ||
1643 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) | |
1644 | { | |
1645 | struct nvme_ns *ns; | |
1646 | struct gendisk *disk; | |
ac81bfa9 MB |
1647 | struct nvme_id_ns *id; |
1648 | char disk_name[DISK_NAME_LEN]; | |
5bae7f73 CH |
1649 | int node = dev_to_node(ctrl->dev); |
1650 | ||
1651 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); | |
1652 | if (!ns) | |
1653 | return; | |
1654 | ||
075790eb KB |
1655 | ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL); |
1656 | if (ns->instance < 0) | |
1657 | goto out_free_ns; | |
1658 | ||
5bae7f73 CH |
1659 | ns->queue = blk_mq_init_queue(ctrl->tagset); |
1660 | if (IS_ERR(ns->queue)) | |
075790eb | 1661 | goto out_release_instance; |
5bae7f73 CH |
1662 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
1663 | ns->queue->queuedata = ns; | |
1664 | ns->ctrl = ctrl; | |
1665 | ||
5bae7f73 CH |
1666 | kref_init(&ns->kref); |
1667 | ns->ns_id = nsid; | |
5bae7f73 | 1668 | ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ |
5bae7f73 CH |
1669 | |
1670 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); | |
da35825d | 1671 | nvme_set_queue_limits(ctrl, ns->queue); |
5bae7f73 | 1672 | |
ac81bfa9 | 1673 | sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance); |
5bae7f73 | 1674 | |
ac81bfa9 MB |
1675 | if (nvme_revalidate_ns(ns, &id)) |
1676 | goto out_free_queue; | |
1677 | ||
3dc87dd0 MB |
1678 | if (nvme_nvm_ns_supported(ns, id) && |
1679 | nvme_nvm_register(ns, disk_name, node)) { | |
1680 | dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__); | |
1681 | goto out_free_id; | |
1682 | } | |
ac81bfa9 | 1683 | |
3dc87dd0 MB |
1684 | disk = alloc_disk_node(0, node); |
1685 | if (!disk) | |
1686 | goto out_free_id; | |
ac81bfa9 | 1687 | |
3dc87dd0 MB |
1688 | disk->fops = &nvme_fops; |
1689 | disk->private_data = ns; | |
1690 | disk->queue = ns->queue; | |
1691 | disk->flags = GENHD_FL_EXT_DEVT; | |
1692 | memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); | |
1693 | ns->disk = disk; | |
1694 | ||
1695 | __nvme_revalidate_disk(disk, id); | |
5bae7f73 | 1696 | |
32f0c4af KB |
1697 | mutex_lock(&ctrl->namespaces_mutex); |
1698 | list_add_tail(&ns->list, &ctrl->namespaces); | |
1699 | mutex_unlock(&ctrl->namespaces_mutex); | |
1700 | ||
5bae7f73 | 1701 | kref_get(&ctrl->kref); |
ac81bfa9 MB |
1702 | |
1703 | kfree(id); | |
1704 | ||
0d52c756 | 1705 | device_add_disk(ctrl->device, ns->disk); |
2b9b6e86 KB |
1706 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
1707 | &nvme_ns_attr_group)) | |
1708 | pr_warn("%s: failed to create sysfs group for identification\n", | |
1709 | ns->disk->disk_name); | |
3dc87dd0 MB |
1710 | if (ns->ndev && nvme_nvm_register_sysfs(ns)) |
1711 | pr_warn("%s: failed to register lightnvm sysfs group for identification\n", | |
1712 | ns->disk->disk_name); | |
5bae7f73 | 1713 | return; |
ac81bfa9 MB |
1714 | out_free_id: |
1715 | kfree(id); | |
5bae7f73 CH |
1716 | out_free_queue: |
1717 | blk_cleanup_queue(ns->queue); | |
075790eb KB |
1718 | out_release_instance: |
1719 | ida_simple_remove(&ctrl->ns_ida, ns->instance); | |
5bae7f73 CH |
1720 | out_free_ns: |
1721 | kfree(ns); | |
1722 | } | |
1723 | ||
1724 | static void nvme_ns_remove(struct nvme_ns *ns) | |
1725 | { | |
646017a6 KB |
1726 | if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) |
1727 | return; | |
69d3b8ac | 1728 | |
b0b4e09c | 1729 | if (ns->disk && ns->disk->flags & GENHD_FL_UP) { |
5bae7f73 CH |
1730 | if (blk_get_integrity(ns->disk)) |
1731 | blk_integrity_unregister(ns->disk); | |
2b9b6e86 KB |
1732 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
1733 | &nvme_ns_attr_group); | |
3dc87dd0 MB |
1734 | if (ns->ndev) |
1735 | nvme_nvm_unregister_sysfs(ns); | |
5bae7f73 | 1736 | del_gendisk(ns->disk); |
5bae7f73 CH |
1737 | blk_mq_abort_requeue_list(ns->queue); |
1738 | blk_cleanup_queue(ns->queue); | |
1739 | } | |
32f0c4af KB |
1740 | |
1741 | mutex_lock(&ns->ctrl->namespaces_mutex); | |
5bae7f73 | 1742 | list_del_init(&ns->list); |
32f0c4af KB |
1743 | mutex_unlock(&ns->ctrl->namespaces_mutex); |
1744 | ||
5bae7f73 CH |
1745 | nvme_put_ns(ns); |
1746 | } | |
1747 | ||
540c801c KB |
1748 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
1749 | { | |
1750 | struct nvme_ns *ns; | |
1751 | ||
32f0c4af | 1752 | ns = nvme_find_get_ns(ctrl, nsid); |
540c801c | 1753 | if (ns) { |
b0b4e09c | 1754 | if (ns->disk && revalidate_disk(ns->disk)) |
540c801c | 1755 | nvme_ns_remove(ns); |
32f0c4af | 1756 | nvme_put_ns(ns); |
540c801c KB |
1757 | } else |
1758 | nvme_alloc_ns(ctrl, nsid); | |
1759 | } | |
1760 | ||
47b0e50a SB |
1761 | static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, |
1762 | unsigned nsid) | |
1763 | { | |
1764 | struct nvme_ns *ns, *next; | |
1765 | ||
1766 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { | |
1767 | if (ns->ns_id > nsid) | |
1768 | nvme_ns_remove(ns); | |
1769 | } | |
1770 | } | |
1771 | ||
540c801c KB |
1772 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) |
1773 | { | |
1774 | struct nvme_ns *ns; | |
1775 | __le32 *ns_list; | |
1776 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); | |
1777 | int ret = 0; | |
1778 | ||
1779 | ns_list = kzalloc(0x1000, GFP_KERNEL); | |
1780 | if (!ns_list) | |
1781 | return -ENOMEM; | |
1782 | ||
1783 | for (i = 0; i < num_lists; i++) { | |
1784 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); | |
1785 | if (ret) | |
47b0e50a | 1786 | goto free; |
540c801c KB |
1787 | |
1788 | for (j = 0; j < min(nn, 1024U); j++) { | |
1789 | nsid = le32_to_cpu(ns_list[j]); | |
1790 | if (!nsid) | |
1791 | goto out; | |
1792 | ||
1793 | nvme_validate_ns(ctrl, nsid); | |
1794 | ||
1795 | while (++prev < nsid) { | |
32f0c4af KB |
1796 | ns = nvme_find_get_ns(ctrl, prev); |
1797 | if (ns) { | |
540c801c | 1798 | nvme_ns_remove(ns); |
32f0c4af KB |
1799 | nvme_put_ns(ns); |
1800 | } | |
540c801c KB |
1801 | } |
1802 | } | |
1803 | nn -= j; | |
1804 | } | |
1805 | out: | |
47b0e50a SB |
1806 | nvme_remove_invalid_namespaces(ctrl, prev); |
1807 | free: | |
540c801c KB |
1808 | kfree(ns_list); |
1809 | return ret; | |
1810 | } | |
1811 | ||
5955be21 | 1812 | static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) |
5bae7f73 | 1813 | { |
5bae7f73 CH |
1814 | unsigned i; |
1815 | ||
540c801c KB |
1816 | for (i = 1; i <= nn; i++) |
1817 | nvme_validate_ns(ctrl, i); | |
1818 | ||
47b0e50a | 1819 | nvme_remove_invalid_namespaces(ctrl, nn); |
5bae7f73 CH |
1820 | } |
1821 | ||
5955be21 | 1822 | static void nvme_scan_work(struct work_struct *work) |
5bae7f73 | 1823 | { |
5955be21 CH |
1824 | struct nvme_ctrl *ctrl = |
1825 | container_of(work, struct nvme_ctrl, scan_work); | |
5bae7f73 | 1826 | struct nvme_id_ctrl *id; |
540c801c | 1827 | unsigned nn; |
5bae7f73 | 1828 | |
5955be21 CH |
1829 | if (ctrl->state != NVME_CTRL_LIVE) |
1830 | return; | |
1831 | ||
5bae7f73 CH |
1832 | if (nvme_identify_ctrl(ctrl, &id)) |
1833 | return; | |
540c801c KB |
1834 | |
1835 | nn = le32_to_cpu(id->nn); | |
8ef2074d | 1836 | if (ctrl->vs >= NVME_VS(1, 1, 0) && |
540c801c KB |
1837 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { |
1838 | if (!nvme_scan_ns_list(ctrl, nn)) | |
1839 | goto done; | |
1840 | } | |
5955be21 | 1841 | nvme_scan_ns_sequential(ctrl, nn); |
540c801c | 1842 | done: |
32f0c4af | 1843 | mutex_lock(&ctrl->namespaces_mutex); |
540c801c | 1844 | list_sort(NULL, &ctrl->namespaces, ns_cmp); |
69d3b8ac | 1845 | mutex_unlock(&ctrl->namespaces_mutex); |
5bae7f73 CH |
1846 | kfree(id); |
1847 | } | |
5955be21 CH |
1848 | |
1849 | void nvme_queue_scan(struct nvme_ctrl *ctrl) | |
1850 | { | |
1851 | /* | |
1852 | * Do not queue new scan work when a controller is reset during | |
1853 | * removal. | |
1854 | */ | |
1855 | if (ctrl->state == NVME_CTRL_LIVE) | |
1856 | schedule_work(&ctrl->scan_work); | |
1857 | } | |
1858 | EXPORT_SYMBOL_GPL(nvme_queue_scan); | |
5bae7f73 | 1859 | |
32f0c4af KB |
1860 | /* |
1861 | * This function iterates the namespace list unlocked to allow recovery from | |
1862 | * controller failure. It is up to the caller to ensure the namespace list is | |
1863 | * not modified by scan work while this function is executing. | |
1864 | */ | |
5bae7f73 CH |
1865 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) |
1866 | { | |
1867 | struct nvme_ns *ns, *next; | |
1868 | ||
0ff9d4e1 KB |
1869 | /* |
1870 | * The dead states indicates the controller was not gracefully | |
1871 | * disconnected. In that case, we won't be able to flush any data while | |
1872 | * removing the namespaces' disks; fail all the queues now to avoid | |
1873 | * potentially having to clean up the failed sync later. | |
1874 | */ | |
1875 | if (ctrl->state == NVME_CTRL_DEAD) | |
1876 | nvme_kill_queues(ctrl); | |
1877 | ||
5bae7f73 CH |
1878 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) |
1879 | nvme_ns_remove(ns); | |
1880 | } | |
576d55d6 | 1881 | EXPORT_SYMBOL_GPL(nvme_remove_namespaces); |
5bae7f73 | 1882 | |
f866fc42 CH |
1883 | static void nvme_async_event_work(struct work_struct *work) |
1884 | { | |
1885 | struct nvme_ctrl *ctrl = | |
1886 | container_of(work, struct nvme_ctrl, async_event_work); | |
1887 | ||
1888 | spin_lock_irq(&ctrl->lock); | |
1889 | while (ctrl->event_limit > 0) { | |
1890 | int aer_idx = --ctrl->event_limit; | |
1891 | ||
1892 | spin_unlock_irq(&ctrl->lock); | |
1893 | ctrl->ops->submit_async_event(ctrl, aer_idx); | |
1894 | spin_lock_irq(&ctrl->lock); | |
1895 | } | |
1896 | spin_unlock_irq(&ctrl->lock); | |
1897 | } | |
1898 | ||
7bf58533 CH |
1899 | void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, |
1900 | union nvme_result *res) | |
f866fc42 | 1901 | { |
7bf58533 CH |
1902 | u32 result = le32_to_cpu(res->u32); |
1903 | bool done = true; | |
f866fc42 | 1904 | |
7bf58533 CH |
1905 | switch (le16_to_cpu(status) >> 1) { |
1906 | case NVME_SC_SUCCESS: | |
1907 | done = false; | |
1908 | /*FALLTHRU*/ | |
1909 | case NVME_SC_ABORT_REQ: | |
f866fc42 CH |
1910 | ++ctrl->event_limit; |
1911 | schedule_work(&ctrl->async_event_work); | |
7bf58533 CH |
1912 | break; |
1913 | default: | |
1914 | break; | |
f866fc42 CH |
1915 | } |
1916 | ||
7bf58533 | 1917 | if (done) |
f866fc42 CH |
1918 | return; |
1919 | ||
1920 | switch (result & 0xff07) { | |
1921 | case NVME_AER_NOTICE_NS_CHANGED: | |
1922 | dev_info(ctrl->device, "rescanning\n"); | |
1923 | nvme_queue_scan(ctrl); | |
1924 | break; | |
1925 | default: | |
1926 | dev_warn(ctrl->device, "async event result %08x\n", result); | |
1927 | } | |
1928 | } | |
1929 | EXPORT_SYMBOL_GPL(nvme_complete_async_event); | |
1930 | ||
1931 | void nvme_queue_async_events(struct nvme_ctrl *ctrl) | |
1932 | { | |
1933 | ctrl->event_limit = NVME_NR_AERS; | |
1934 | schedule_work(&ctrl->async_event_work); | |
1935 | } | |
1936 | EXPORT_SYMBOL_GPL(nvme_queue_async_events); | |
1937 | ||
f3ca80fc CH |
1938 | static DEFINE_IDA(nvme_instance_ida); |
1939 | ||
1940 | static int nvme_set_instance(struct nvme_ctrl *ctrl) | |
1941 | { | |
1942 | int instance, error; | |
1943 | ||
1944 | do { | |
1945 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) | |
1946 | return -ENODEV; | |
1947 | ||
1948 | spin_lock(&dev_list_lock); | |
1949 | error = ida_get_new(&nvme_instance_ida, &instance); | |
1950 | spin_unlock(&dev_list_lock); | |
1951 | } while (error == -EAGAIN); | |
1952 | ||
1953 | if (error) | |
1954 | return -ENODEV; | |
1955 | ||
1956 | ctrl->instance = instance; | |
1957 | return 0; | |
1958 | } | |
1959 | ||
1960 | static void nvme_release_instance(struct nvme_ctrl *ctrl) | |
1961 | { | |
1962 | spin_lock(&dev_list_lock); | |
1963 | ida_remove(&nvme_instance_ida, ctrl->instance); | |
1964 | spin_unlock(&dev_list_lock); | |
1965 | } | |
1966 | ||
53029b04 | 1967 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
576d55d6 | 1968 | { |
f866fc42 | 1969 | flush_work(&ctrl->async_event_work); |
5955be21 CH |
1970 | flush_work(&ctrl->scan_work); |
1971 | nvme_remove_namespaces(ctrl); | |
1972 | ||
53029b04 | 1973 | device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance)); |
f3ca80fc CH |
1974 | |
1975 | spin_lock(&dev_list_lock); | |
1976 | list_del(&ctrl->node); | |
1977 | spin_unlock(&dev_list_lock); | |
53029b04 | 1978 | } |
576d55d6 | 1979 | EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); |
53029b04 KB |
1980 | |
1981 | static void nvme_free_ctrl(struct kref *kref) | |
1982 | { | |
1983 | struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref); | |
f3ca80fc CH |
1984 | |
1985 | put_device(ctrl->device); | |
1986 | nvme_release_instance(ctrl); | |
075790eb | 1987 | ida_destroy(&ctrl->ns_ida); |
f3ca80fc CH |
1988 | |
1989 | ctrl->ops->free_ctrl(ctrl); | |
1990 | } | |
1991 | ||
1992 | void nvme_put_ctrl(struct nvme_ctrl *ctrl) | |
1993 | { | |
1994 | kref_put(&ctrl->kref, nvme_free_ctrl); | |
1995 | } | |
576d55d6 | 1996 | EXPORT_SYMBOL_GPL(nvme_put_ctrl); |
f3ca80fc CH |
1997 | |
1998 | /* | |
1999 | * Initialize a NVMe controller structures. This needs to be called during | |
2000 | * earliest initialization so that we have the initialized structured around | |
2001 | * during probing. | |
2002 | */ | |
2003 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, | |
2004 | const struct nvme_ctrl_ops *ops, unsigned long quirks) | |
2005 | { | |
2006 | int ret; | |
2007 | ||
bb8d261e CH |
2008 | ctrl->state = NVME_CTRL_NEW; |
2009 | spin_lock_init(&ctrl->lock); | |
f3ca80fc | 2010 | INIT_LIST_HEAD(&ctrl->namespaces); |
69d3b8ac | 2011 | mutex_init(&ctrl->namespaces_mutex); |
f3ca80fc CH |
2012 | kref_init(&ctrl->kref); |
2013 | ctrl->dev = dev; | |
2014 | ctrl->ops = ops; | |
2015 | ctrl->quirks = quirks; | |
5955be21 | 2016 | INIT_WORK(&ctrl->scan_work, nvme_scan_work); |
f866fc42 | 2017 | INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); |
f3ca80fc CH |
2018 | |
2019 | ret = nvme_set_instance(ctrl); | |
2020 | if (ret) | |
2021 | goto out; | |
2022 | ||
779ff756 | 2023 | ctrl->device = device_create_with_groups(nvme_class, ctrl->dev, |
f3ca80fc | 2024 | MKDEV(nvme_char_major, ctrl->instance), |
f4f0f63e | 2025 | ctrl, nvme_dev_attr_groups, |
779ff756 | 2026 | "nvme%d", ctrl->instance); |
f3ca80fc CH |
2027 | if (IS_ERR(ctrl->device)) { |
2028 | ret = PTR_ERR(ctrl->device); | |
2029 | goto out_release_instance; | |
2030 | } | |
2031 | get_device(ctrl->device); | |
075790eb | 2032 | ida_init(&ctrl->ns_ida); |
f3ca80fc | 2033 | |
f3ca80fc CH |
2034 | spin_lock(&dev_list_lock); |
2035 | list_add_tail(&ctrl->node, &nvme_ctrl_list); | |
2036 | spin_unlock(&dev_list_lock); | |
2037 | ||
2038 | return 0; | |
f3ca80fc CH |
2039 | out_release_instance: |
2040 | nvme_release_instance(ctrl); | |
2041 | out: | |
2042 | return ret; | |
2043 | } | |
576d55d6 | 2044 | EXPORT_SYMBOL_GPL(nvme_init_ctrl); |
f3ca80fc | 2045 | |
69d9a99c KB |
2046 | /** |
2047 | * nvme_kill_queues(): Ends all namespace queues | |
2048 | * @ctrl: the dead controller that needs to end | |
2049 | * | |
2050 | * Call this function when the driver determines it is unable to get the | |
2051 | * controller in a state capable of servicing IO. | |
2052 | */ | |
2053 | void nvme_kill_queues(struct nvme_ctrl *ctrl) | |
2054 | { | |
2055 | struct nvme_ns *ns; | |
2056 | ||
32f0c4af KB |
2057 | mutex_lock(&ctrl->namespaces_mutex); |
2058 | list_for_each_entry(ns, &ctrl->namespaces, list) { | |
69d9a99c KB |
2059 | /* |
2060 | * Revalidating a dead namespace sets capacity to 0. This will | |
2061 | * end buffered writers dirtying pages that can't be synced. | |
2062 | */ | |
b0b4e09c | 2063 | if (ns->disk && !test_and_set_bit(NVME_NS_DEAD, &ns->flags)) |
69d9a99c KB |
2064 | revalidate_disk(ns->disk); |
2065 | ||
2066 | blk_set_queue_dying(ns->queue); | |
2067 | blk_mq_abort_requeue_list(ns->queue); | |
2068 | blk_mq_start_stopped_hw_queues(ns->queue, true); | |
69d9a99c | 2069 | } |
32f0c4af | 2070 | mutex_unlock(&ctrl->namespaces_mutex); |
69d9a99c | 2071 | } |
237045fc | 2072 | EXPORT_SYMBOL_GPL(nvme_kill_queues); |
69d9a99c | 2073 | |
25646264 | 2074 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
363c9aac SG |
2075 | { |
2076 | struct nvme_ns *ns; | |
2077 | ||
32f0c4af | 2078 | mutex_lock(&ctrl->namespaces_mutex); |
a6eaa884 | 2079 | list_for_each_entry(ns, &ctrl->namespaces, list) |
3174dd33 | 2080 | blk_mq_quiesce_queue(ns->queue); |
32f0c4af | 2081 | mutex_unlock(&ctrl->namespaces_mutex); |
363c9aac | 2082 | } |
576d55d6 | 2083 | EXPORT_SYMBOL_GPL(nvme_stop_queues); |
363c9aac | 2084 | |
25646264 | 2085 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
363c9aac SG |
2086 | { |
2087 | struct nvme_ns *ns; | |
2088 | ||
32f0c4af KB |
2089 | mutex_lock(&ctrl->namespaces_mutex); |
2090 | list_for_each_entry(ns, &ctrl->namespaces, list) { | |
363c9aac SG |
2091 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
2092 | blk_mq_kick_requeue_list(ns->queue); | |
2093 | } | |
32f0c4af | 2094 | mutex_unlock(&ctrl->namespaces_mutex); |
363c9aac | 2095 | } |
576d55d6 | 2096 | EXPORT_SYMBOL_GPL(nvme_start_queues); |
363c9aac | 2097 | |
5bae7f73 CH |
2098 | int __init nvme_core_init(void) |
2099 | { | |
2100 | int result; | |
2101 | ||
f3ca80fc CH |
2102 | result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", |
2103 | &nvme_dev_fops); | |
2104 | if (result < 0) | |
b09dcf58 | 2105 | return result; |
f3ca80fc CH |
2106 | else if (result > 0) |
2107 | nvme_char_major = result; | |
2108 | ||
2109 | nvme_class = class_create(THIS_MODULE, "nvme"); | |
2110 | if (IS_ERR(nvme_class)) { | |
2111 | result = PTR_ERR(nvme_class); | |
2112 | goto unregister_chrdev; | |
2113 | } | |
2114 | ||
5bae7f73 | 2115 | return 0; |
f3ca80fc CH |
2116 | |
2117 | unregister_chrdev: | |
2118 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); | |
f3ca80fc | 2119 | return result; |
5bae7f73 CH |
2120 | } |
2121 | ||
2122 | void nvme_core_exit(void) | |
2123 | { | |
f3ca80fc CH |
2124 | class_destroy(nvme_class); |
2125 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); | |
5bae7f73 | 2126 | } |
576d55d6 ML |
2127 | |
2128 | MODULE_LICENSE("GPL"); | |
2129 | MODULE_VERSION("1.0"); | |
2130 | module_init(nvme_core_init); | |
2131 | module_exit(nvme_core_exit); |