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" | |
33 | ||
f3ca80fc CH |
34 | #define NVME_MINORS (1U << MINORBITS) |
35 | ||
5bae7f73 CH |
36 | static int nvme_major; |
37 | module_param(nvme_major, int, 0); | |
38 | ||
f3ca80fc CH |
39 | static int nvme_char_major; |
40 | module_param(nvme_char_major, int, 0); | |
41 | ||
42 | static LIST_HEAD(nvme_ctrl_list); | |
1673f1f0 CH |
43 | DEFINE_SPINLOCK(dev_list_lock); |
44 | ||
f3ca80fc CH |
45 | static struct class *nvme_class; |
46 | ||
1673f1f0 CH |
47 | static void nvme_free_ns(struct kref *kref) |
48 | { | |
49 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); | |
50 | ||
51 | if (ns->type == NVME_NS_LIGHTNVM) | |
52 | nvme_nvm_unregister(ns->queue, ns->disk->disk_name); | |
53 | ||
54 | spin_lock(&dev_list_lock); | |
55 | ns->disk->private_data = NULL; | |
56 | spin_unlock(&dev_list_lock); | |
57 | ||
58 | nvme_put_ctrl(ns->ctrl); | |
59 | put_disk(ns->disk); | |
60 | kfree(ns); | |
61 | } | |
62 | ||
5bae7f73 | 63 | static void nvme_put_ns(struct nvme_ns *ns) |
1673f1f0 CH |
64 | { |
65 | kref_put(&ns->kref, nvme_free_ns); | |
66 | } | |
67 | ||
68 | static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk) | |
69 | { | |
70 | struct nvme_ns *ns; | |
71 | ||
72 | spin_lock(&dev_list_lock); | |
73 | ns = disk->private_data; | |
e439bb12 SG |
74 | if (ns) { |
75 | if (!kref_get_unless_zero(&ns->kref)) | |
76 | goto fail; | |
77 | if (!try_module_get(ns->ctrl->ops->module)) | |
78 | goto fail_put_ns; | |
79 | } | |
1673f1f0 CH |
80 | spin_unlock(&dev_list_lock); |
81 | ||
82 | return ns; | |
e439bb12 SG |
83 | |
84 | fail_put_ns: | |
85 | kref_put(&ns->kref, nvme_free_ns); | |
86 | fail: | |
87 | spin_unlock(&dev_list_lock); | |
88 | return NULL; | |
1673f1f0 CH |
89 | } |
90 | ||
7688faa6 CH |
91 | void nvme_requeue_req(struct request *req) |
92 | { | |
93 | unsigned long flags; | |
94 | ||
95 | blk_mq_requeue_request(req); | |
96 | spin_lock_irqsave(req->q->queue_lock, flags); | |
97 | if (!blk_queue_stopped(req->q)) | |
98 | blk_mq_kick_requeue_list(req->q); | |
99 | spin_unlock_irqrestore(req->q->queue_lock, flags); | |
100 | } | |
101 | ||
4160982e CH |
102 | struct request *nvme_alloc_request(struct request_queue *q, |
103 | struct nvme_command *cmd, unsigned int flags) | |
21d34711 CH |
104 | { |
105 | bool write = cmd->common.opcode & 1; | |
21d34711 | 106 | struct request *req; |
21d34711 | 107 | |
4160982e | 108 | req = blk_mq_alloc_request(q, write, flags); |
21d34711 | 109 | if (IS_ERR(req)) |
4160982e | 110 | return req; |
21d34711 CH |
111 | |
112 | req->cmd_type = REQ_TYPE_DRV_PRIV; | |
113 | req->cmd_flags |= REQ_FAILFAST_DRIVER; | |
114 | req->__data_len = 0; | |
115 | req->__sector = (sector_t) -1; | |
116 | req->bio = req->biotail = NULL; | |
117 | ||
21d34711 CH |
118 | req->cmd = (unsigned char *)cmd; |
119 | req->cmd_len = sizeof(struct nvme_command); | |
120 | req->special = (void *)0; | |
121 | ||
4160982e CH |
122 | return req; |
123 | } | |
124 | ||
125 | /* | |
126 | * Returns 0 on success. If the result is negative, it's a Linux error code; | |
127 | * if the result is positive, it's an NVM Express status code | |
128 | */ | |
129 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, | |
130 | void *buffer, unsigned bufflen, u32 *result, unsigned timeout) | |
131 | { | |
132 | struct request *req; | |
133 | int ret; | |
134 | ||
135 | req = nvme_alloc_request(q, cmd, 0); | |
136 | if (IS_ERR(req)) | |
137 | return PTR_ERR(req); | |
138 | ||
139 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; | |
140 | ||
21d34711 CH |
141 | if (buffer && bufflen) { |
142 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); | |
143 | if (ret) | |
144 | goto out; | |
4160982e CH |
145 | } |
146 | ||
147 | blk_execute_rq(req->q, NULL, req, 0); | |
148 | if (result) | |
149 | *result = (u32)(uintptr_t)req->special; | |
150 | ret = req->errors; | |
151 | out: | |
152 | blk_mq_free_request(req); | |
153 | return ret; | |
154 | } | |
155 | ||
156 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, | |
157 | void *buffer, unsigned bufflen) | |
158 | { | |
159 | return __nvme_submit_sync_cmd(q, cmd, buffer, bufflen, NULL, 0); | |
160 | } | |
161 | ||
0b7f1f26 KB |
162 | int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
163 | void __user *ubuffer, unsigned bufflen, | |
164 | void __user *meta_buffer, unsigned meta_len, u32 meta_seed, | |
165 | u32 *result, unsigned timeout) | |
4160982e | 166 | { |
0b7f1f26 KB |
167 | bool write = cmd->common.opcode & 1; |
168 | struct nvme_ns *ns = q->queuedata; | |
169 | struct gendisk *disk = ns ? ns->disk : NULL; | |
4160982e | 170 | struct request *req; |
0b7f1f26 KB |
171 | struct bio *bio = NULL; |
172 | void *meta = NULL; | |
4160982e CH |
173 | int ret; |
174 | ||
175 | req = nvme_alloc_request(q, cmd, 0); | |
176 | if (IS_ERR(req)) | |
177 | return PTR_ERR(req); | |
178 | ||
179 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; | |
180 | ||
181 | if (ubuffer && bufflen) { | |
21d34711 CH |
182 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
183 | GFP_KERNEL); | |
184 | if (ret) | |
185 | goto out; | |
186 | bio = req->bio; | |
21d34711 | 187 | |
0b7f1f26 KB |
188 | if (!disk) |
189 | goto submit; | |
190 | bio->bi_bdev = bdget_disk(disk, 0); | |
191 | if (!bio->bi_bdev) { | |
192 | ret = -ENODEV; | |
193 | goto out_unmap; | |
194 | } | |
195 | ||
196 | if (meta_buffer) { | |
197 | struct bio_integrity_payload *bip; | |
198 | ||
199 | meta = kmalloc(meta_len, GFP_KERNEL); | |
200 | if (!meta) { | |
201 | ret = -ENOMEM; | |
202 | goto out_unmap; | |
203 | } | |
204 | ||
205 | if (write) { | |
206 | if (copy_from_user(meta, meta_buffer, | |
207 | meta_len)) { | |
208 | ret = -EFAULT; | |
209 | goto out_free_meta; | |
210 | } | |
211 | } | |
212 | ||
213 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); | |
06c1e390 KB |
214 | if (IS_ERR(bip)) { |
215 | ret = PTR_ERR(bip); | |
0b7f1f26 KB |
216 | goto out_free_meta; |
217 | } | |
218 | ||
219 | bip->bip_iter.bi_size = meta_len; | |
220 | bip->bip_iter.bi_sector = meta_seed; | |
221 | ||
222 | ret = bio_integrity_add_page(bio, virt_to_page(meta), | |
223 | meta_len, offset_in_page(meta)); | |
224 | if (ret != meta_len) { | |
225 | ret = -ENOMEM; | |
226 | goto out_free_meta; | |
227 | } | |
228 | } | |
229 | } | |
230 | submit: | |
231 | blk_execute_rq(req->q, disk, req, 0); | |
232 | ret = req->errors; | |
21d34711 CH |
233 | if (result) |
234 | *result = (u32)(uintptr_t)req->special; | |
0b7f1f26 KB |
235 | if (meta && !ret && !write) { |
236 | if (copy_to_user(meta_buffer, meta, meta_len)) | |
237 | ret = -EFAULT; | |
238 | } | |
239 | out_free_meta: | |
240 | kfree(meta); | |
241 | out_unmap: | |
242 | if (bio) { | |
243 | if (disk && bio->bi_bdev) | |
244 | bdput(bio->bi_bdev); | |
245 | blk_rq_unmap_user(bio); | |
246 | } | |
21d34711 CH |
247 | out: |
248 | blk_mq_free_request(req); | |
249 | return ret; | |
250 | } | |
251 | ||
0b7f1f26 KB |
252 | int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
253 | void __user *ubuffer, unsigned bufflen, u32 *result, | |
254 | unsigned timeout) | |
255 | { | |
256 | return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0, | |
257 | result, timeout); | |
258 | } | |
259 | ||
1c63dc66 | 260 | int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) |
21d34711 CH |
261 | { |
262 | struct nvme_command c = { }; | |
263 | int error; | |
264 | ||
265 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ | |
266 | c.identify.opcode = nvme_admin_identify; | |
267 | c.identify.cns = cpu_to_le32(1); | |
268 | ||
269 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); | |
270 | if (!*id) | |
271 | return -ENOMEM; | |
272 | ||
273 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, | |
274 | sizeof(struct nvme_id_ctrl)); | |
275 | if (error) | |
276 | kfree(*id); | |
277 | return error; | |
278 | } | |
279 | ||
540c801c KB |
280 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
281 | { | |
282 | struct nvme_command c = { }; | |
283 | ||
284 | c.identify.opcode = nvme_admin_identify; | |
285 | c.identify.cns = cpu_to_le32(2); | |
286 | c.identify.nsid = cpu_to_le32(nsid); | |
287 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); | |
288 | } | |
289 | ||
1c63dc66 | 290 | int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid, |
21d34711 CH |
291 | struct nvme_id_ns **id) |
292 | { | |
293 | struct nvme_command c = { }; | |
294 | int error; | |
295 | ||
296 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ | |
297 | c.identify.opcode = nvme_admin_identify, | |
298 | c.identify.nsid = cpu_to_le32(nsid), | |
299 | ||
300 | *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); | |
301 | if (!*id) | |
302 | return -ENOMEM; | |
303 | ||
304 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, | |
305 | sizeof(struct nvme_id_ns)); | |
306 | if (error) | |
307 | kfree(*id); | |
308 | return error; | |
309 | } | |
310 | ||
1c63dc66 | 311 | int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid, |
21d34711 CH |
312 | dma_addr_t dma_addr, u32 *result) |
313 | { | |
314 | struct nvme_command c; | |
315 | ||
316 | memset(&c, 0, sizeof(c)); | |
317 | c.features.opcode = nvme_admin_get_features; | |
318 | c.features.nsid = cpu_to_le32(nsid); | |
319 | c.features.prp1 = cpu_to_le64(dma_addr); | |
320 | c.features.fid = cpu_to_le32(fid); | |
321 | ||
4160982e | 322 | return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0); |
21d34711 CH |
323 | } |
324 | ||
1c63dc66 | 325 | int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
21d34711 CH |
326 | dma_addr_t dma_addr, u32 *result) |
327 | { | |
328 | struct nvme_command c; | |
329 | ||
330 | memset(&c, 0, sizeof(c)); | |
331 | c.features.opcode = nvme_admin_set_features; | |
332 | c.features.prp1 = cpu_to_le64(dma_addr); | |
333 | c.features.fid = cpu_to_le32(fid); | |
334 | c.features.dword11 = cpu_to_le32(dword11); | |
335 | ||
4160982e | 336 | return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0); |
21d34711 CH |
337 | } |
338 | ||
1c63dc66 | 339 | int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log) |
21d34711 CH |
340 | { |
341 | struct nvme_command c = { }; | |
342 | int error; | |
343 | ||
344 | c.common.opcode = nvme_admin_get_log_page, | |
345 | c.common.nsid = cpu_to_le32(0xFFFFFFFF), | |
346 | c.common.cdw10[0] = cpu_to_le32( | |
347 | (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | | |
348 | NVME_LOG_SMART), | |
349 | ||
350 | *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); | |
351 | if (!*log) | |
352 | return -ENOMEM; | |
353 | ||
354 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, | |
355 | sizeof(struct nvme_smart_log)); | |
356 | if (error) | |
357 | kfree(*log); | |
358 | return error; | |
359 | } | |
1673f1f0 | 360 | |
9a0be7ab CH |
361 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
362 | { | |
363 | u32 q_count = (*count - 1) | ((*count - 1) << 16); | |
364 | u32 result; | |
365 | int status, nr_io_queues; | |
366 | ||
367 | status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0, | |
368 | &result); | |
369 | if (status) | |
370 | return status; | |
371 | ||
372 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; | |
373 | *count = min(*count, nr_io_queues); | |
374 | return 0; | |
375 | } | |
376 | ||
1673f1f0 CH |
377 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
378 | { | |
379 | struct nvme_user_io io; | |
380 | struct nvme_command c; | |
381 | unsigned length, meta_len; | |
382 | void __user *metadata; | |
383 | ||
384 | if (copy_from_user(&io, uio, sizeof(io))) | |
385 | return -EFAULT; | |
386 | ||
387 | switch (io.opcode) { | |
388 | case nvme_cmd_write: | |
389 | case nvme_cmd_read: | |
390 | case nvme_cmd_compare: | |
391 | break; | |
392 | default: | |
393 | return -EINVAL; | |
394 | } | |
395 | ||
396 | length = (io.nblocks + 1) << ns->lba_shift; | |
397 | meta_len = (io.nblocks + 1) * ns->ms; | |
398 | metadata = (void __user *)(uintptr_t)io.metadata; | |
399 | ||
400 | if (ns->ext) { | |
401 | length += meta_len; | |
402 | meta_len = 0; | |
403 | } else if (meta_len) { | |
404 | if ((io.metadata & 3) || !io.metadata) | |
405 | return -EINVAL; | |
406 | } | |
407 | ||
408 | memset(&c, 0, sizeof(c)); | |
409 | c.rw.opcode = io.opcode; | |
410 | c.rw.flags = io.flags; | |
411 | c.rw.nsid = cpu_to_le32(ns->ns_id); | |
412 | c.rw.slba = cpu_to_le64(io.slba); | |
413 | c.rw.length = cpu_to_le16(io.nblocks); | |
414 | c.rw.control = cpu_to_le16(io.control); | |
415 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); | |
416 | c.rw.reftag = cpu_to_le32(io.reftag); | |
417 | c.rw.apptag = cpu_to_le16(io.apptag); | |
418 | c.rw.appmask = cpu_to_le16(io.appmask); | |
419 | ||
420 | return __nvme_submit_user_cmd(ns->queue, &c, | |
421 | (void __user *)(uintptr_t)io.addr, length, | |
422 | metadata, meta_len, io.slba, NULL, 0); | |
423 | } | |
424 | ||
f3ca80fc | 425 | static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
1673f1f0 CH |
426 | struct nvme_passthru_cmd __user *ucmd) |
427 | { | |
428 | struct nvme_passthru_cmd cmd; | |
429 | struct nvme_command c; | |
430 | unsigned timeout = 0; | |
431 | int status; | |
432 | ||
433 | if (!capable(CAP_SYS_ADMIN)) | |
434 | return -EACCES; | |
435 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) | |
436 | return -EFAULT; | |
437 | ||
438 | memset(&c, 0, sizeof(c)); | |
439 | c.common.opcode = cmd.opcode; | |
440 | c.common.flags = cmd.flags; | |
441 | c.common.nsid = cpu_to_le32(cmd.nsid); | |
442 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); | |
443 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); | |
444 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); | |
445 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); | |
446 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); | |
447 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); | |
448 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); | |
449 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); | |
450 | ||
451 | if (cmd.timeout_ms) | |
452 | timeout = msecs_to_jiffies(cmd.timeout_ms); | |
453 | ||
454 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, | |
d1ea7be5 | 455 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
1673f1f0 CH |
456 | &cmd.result, timeout); |
457 | if (status >= 0) { | |
458 | if (put_user(cmd.result, &ucmd->result)) | |
459 | return -EFAULT; | |
460 | } | |
461 | ||
462 | return status; | |
463 | } | |
464 | ||
465 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, | |
466 | unsigned int cmd, unsigned long arg) | |
467 | { | |
468 | struct nvme_ns *ns = bdev->bd_disk->private_data; | |
469 | ||
470 | switch (cmd) { | |
471 | case NVME_IOCTL_ID: | |
472 | force_successful_syscall_return(); | |
473 | return ns->ns_id; | |
474 | case NVME_IOCTL_ADMIN_CMD: | |
475 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); | |
476 | case NVME_IOCTL_IO_CMD: | |
477 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); | |
478 | case NVME_IOCTL_SUBMIT_IO: | |
479 | return nvme_submit_io(ns, (void __user *)arg); | |
44907332 | 480 | #ifdef CONFIG_BLK_DEV_NVME_SCSI |
1673f1f0 CH |
481 | case SG_GET_VERSION_NUM: |
482 | return nvme_sg_get_version_num((void __user *)arg); | |
483 | case SG_IO: | |
484 | return nvme_sg_io(ns, (void __user *)arg); | |
44907332 | 485 | #endif |
1673f1f0 CH |
486 | default: |
487 | return -ENOTTY; | |
488 | } | |
489 | } | |
490 | ||
491 | #ifdef CONFIG_COMPAT | |
492 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
493 | unsigned int cmd, unsigned long arg) | |
494 | { | |
495 | switch (cmd) { | |
496 | case SG_IO: | |
497 | return -ENOIOCTLCMD; | |
498 | } | |
499 | return nvme_ioctl(bdev, mode, cmd, arg); | |
500 | } | |
501 | #else | |
502 | #define nvme_compat_ioctl NULL | |
503 | #endif | |
504 | ||
505 | static int nvme_open(struct block_device *bdev, fmode_t mode) | |
506 | { | |
507 | return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO; | |
508 | } | |
509 | ||
510 | static void nvme_release(struct gendisk *disk, fmode_t mode) | |
511 | { | |
e439bb12 SG |
512 | struct nvme_ns *ns = disk->private_data; |
513 | ||
514 | module_put(ns->ctrl->ops->module); | |
515 | nvme_put_ns(ns); | |
1673f1f0 CH |
516 | } |
517 | ||
518 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) | |
519 | { | |
520 | /* some standard values */ | |
521 | geo->heads = 1 << 6; | |
522 | geo->sectors = 1 << 5; | |
523 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; | |
524 | return 0; | |
525 | } | |
526 | ||
527 | #ifdef CONFIG_BLK_DEV_INTEGRITY | |
528 | static void nvme_init_integrity(struct nvme_ns *ns) | |
529 | { | |
530 | struct blk_integrity integrity; | |
531 | ||
532 | switch (ns->pi_type) { | |
533 | case NVME_NS_DPS_PI_TYPE3: | |
534 | integrity.profile = &t10_pi_type3_crc; | |
535 | break; | |
536 | case NVME_NS_DPS_PI_TYPE1: | |
537 | case NVME_NS_DPS_PI_TYPE2: | |
538 | integrity.profile = &t10_pi_type1_crc; | |
539 | break; | |
540 | default: | |
541 | integrity.profile = NULL; | |
542 | break; | |
543 | } | |
544 | integrity.tuple_size = ns->ms; | |
545 | blk_integrity_register(ns->disk, &integrity); | |
546 | blk_queue_max_integrity_segments(ns->queue, 1); | |
547 | } | |
548 | #else | |
549 | static void nvme_init_integrity(struct nvme_ns *ns) | |
550 | { | |
551 | } | |
552 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
553 | ||
554 | static void nvme_config_discard(struct nvme_ns *ns) | |
555 | { | |
556 | u32 logical_block_size = queue_logical_block_size(ns->queue); | |
557 | ns->queue->limits.discard_zeroes_data = 0; | |
558 | ns->queue->limits.discard_alignment = logical_block_size; | |
559 | ns->queue->limits.discard_granularity = logical_block_size; | |
560 | blk_queue_max_discard_sectors(ns->queue, 0xffffffff); | |
561 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); | |
562 | } | |
563 | ||
5bae7f73 | 564 | static int nvme_revalidate_disk(struct gendisk *disk) |
1673f1f0 CH |
565 | { |
566 | struct nvme_ns *ns = disk->private_data; | |
567 | struct nvme_id_ns *id; | |
568 | u8 lbaf, pi_type; | |
569 | u16 old_ms; | |
570 | unsigned short bs; | |
571 | ||
572 | if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) { | |
1b3c47c1 SG |
573 | dev_warn(disk_to_dev(ns->disk), "%s: Identify failure\n", |
574 | __func__); | |
1673f1f0 CH |
575 | return -ENODEV; |
576 | } | |
577 | if (id->ncap == 0) { | |
578 | kfree(id); | |
579 | return -ENODEV; | |
580 | } | |
581 | ||
582 | if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) { | |
583 | if (nvme_nvm_register(ns->queue, disk->disk_name)) { | |
1b3c47c1 | 584 | dev_warn(disk_to_dev(ns->disk), |
1673f1f0 CH |
585 | "%s: LightNVM init failure\n", __func__); |
586 | kfree(id); | |
587 | return -ENODEV; | |
588 | } | |
589 | ns->type = NVME_NS_LIGHTNVM; | |
590 | } | |
591 | ||
2b9b6e86 KB |
592 | if (ns->ctrl->vs >= NVME_VS(1, 1)) |
593 | memcpy(ns->eui, id->eui64, sizeof(ns->eui)); | |
594 | if (ns->ctrl->vs >= NVME_VS(1, 2)) | |
595 | memcpy(ns->uuid, id->nguid, sizeof(ns->uuid)); | |
596 | ||
1673f1f0 CH |
597 | old_ms = ns->ms; |
598 | lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK; | |
599 | ns->lba_shift = id->lbaf[lbaf].ds; | |
600 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); | |
601 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); | |
602 | ||
603 | /* | |
604 | * If identify namespace failed, use default 512 byte block size so | |
605 | * block layer can use before failing read/write for 0 capacity. | |
606 | */ | |
607 | if (ns->lba_shift == 0) | |
608 | ns->lba_shift = 9; | |
609 | bs = 1 << ns->lba_shift; | |
1673f1f0 CH |
610 | /* XXX: PI implementation requires metadata equal t10 pi tuple size */ |
611 | pi_type = ns->ms == sizeof(struct t10_pi_tuple) ? | |
612 | id->dps & NVME_NS_DPS_PI_MASK : 0; | |
613 | ||
614 | blk_mq_freeze_queue(disk->queue); | |
615 | if (blk_get_integrity(disk) && (ns->pi_type != pi_type || | |
616 | ns->ms != old_ms || | |
617 | bs != queue_logical_block_size(disk->queue) || | |
618 | (ns->ms && ns->ext))) | |
619 | blk_integrity_unregister(disk); | |
620 | ||
621 | ns->pi_type = pi_type; | |
622 | blk_queue_logical_block_size(ns->queue, bs); | |
623 | ||
4b9d5b15 | 624 | if (ns->ms && !blk_get_integrity(disk) && !ns->ext) |
1673f1f0 | 625 | nvme_init_integrity(ns); |
1673f1f0 CH |
626 | if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk)) |
627 | set_capacity(disk, 0); | |
628 | else | |
629 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); | |
630 | ||
631 | if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) | |
632 | nvme_config_discard(ns); | |
633 | blk_mq_unfreeze_queue(disk->queue); | |
634 | ||
635 | kfree(id); | |
636 | return 0; | |
637 | } | |
638 | ||
639 | static char nvme_pr_type(enum pr_type type) | |
640 | { | |
641 | switch (type) { | |
642 | case PR_WRITE_EXCLUSIVE: | |
643 | return 1; | |
644 | case PR_EXCLUSIVE_ACCESS: | |
645 | return 2; | |
646 | case PR_WRITE_EXCLUSIVE_REG_ONLY: | |
647 | return 3; | |
648 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: | |
649 | return 4; | |
650 | case PR_WRITE_EXCLUSIVE_ALL_REGS: | |
651 | return 5; | |
652 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: | |
653 | return 6; | |
654 | default: | |
655 | return 0; | |
656 | } | |
657 | }; | |
658 | ||
659 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, | |
660 | u64 key, u64 sa_key, u8 op) | |
661 | { | |
662 | struct nvme_ns *ns = bdev->bd_disk->private_data; | |
663 | struct nvme_command c; | |
664 | u8 data[16] = { 0, }; | |
665 | ||
666 | put_unaligned_le64(key, &data[0]); | |
667 | put_unaligned_le64(sa_key, &data[8]); | |
668 | ||
669 | memset(&c, 0, sizeof(c)); | |
670 | c.common.opcode = op; | |
671 | c.common.nsid = cpu_to_le32(ns->ns_id); | |
672 | c.common.cdw10[0] = cpu_to_le32(cdw10); | |
673 | ||
674 | return nvme_submit_sync_cmd(ns->queue, &c, data, 16); | |
675 | } | |
676 | ||
677 | static int nvme_pr_register(struct block_device *bdev, u64 old, | |
678 | u64 new, unsigned flags) | |
679 | { | |
680 | u32 cdw10; | |
681 | ||
682 | if (flags & ~PR_FL_IGNORE_KEY) | |
683 | return -EOPNOTSUPP; | |
684 | ||
685 | cdw10 = old ? 2 : 0; | |
686 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; | |
687 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ | |
688 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); | |
689 | } | |
690 | ||
691 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, | |
692 | enum pr_type type, unsigned flags) | |
693 | { | |
694 | u32 cdw10; | |
695 | ||
696 | if (flags & ~PR_FL_IGNORE_KEY) | |
697 | return -EOPNOTSUPP; | |
698 | ||
699 | cdw10 = nvme_pr_type(type) << 8; | |
700 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); | |
701 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); | |
702 | } | |
703 | ||
704 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, | |
705 | enum pr_type type, bool abort) | |
706 | { | |
707 | u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; | |
708 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); | |
709 | } | |
710 | ||
711 | static int nvme_pr_clear(struct block_device *bdev, u64 key) | |
712 | { | |
8c0b3915 | 713 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
1673f1f0 CH |
714 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
715 | } | |
716 | ||
717 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) | |
718 | { | |
719 | u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; | |
720 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); | |
721 | } | |
722 | ||
723 | static const struct pr_ops nvme_pr_ops = { | |
724 | .pr_register = nvme_pr_register, | |
725 | .pr_reserve = nvme_pr_reserve, | |
726 | .pr_release = nvme_pr_release, | |
727 | .pr_preempt = nvme_pr_preempt, | |
728 | .pr_clear = nvme_pr_clear, | |
729 | }; | |
730 | ||
5bae7f73 | 731 | static const struct block_device_operations nvme_fops = { |
1673f1f0 CH |
732 | .owner = THIS_MODULE, |
733 | .ioctl = nvme_ioctl, | |
734 | .compat_ioctl = nvme_compat_ioctl, | |
735 | .open = nvme_open, | |
736 | .release = nvme_release, | |
737 | .getgeo = nvme_getgeo, | |
738 | .revalidate_disk= nvme_revalidate_disk, | |
739 | .pr_ops = &nvme_pr_ops, | |
740 | }; | |
741 | ||
5fd4ce1b CH |
742 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
743 | { | |
744 | unsigned long timeout = | |
745 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; | |
746 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; | |
747 | int ret; | |
748 | ||
749 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { | |
750 | if ((csts & NVME_CSTS_RDY) == bit) | |
751 | break; | |
752 | ||
753 | msleep(100); | |
754 | if (fatal_signal_pending(current)) | |
755 | return -EINTR; | |
756 | if (time_after(jiffies, timeout)) { | |
1b3c47c1 | 757 | dev_err(ctrl->device, |
5fd4ce1b CH |
758 | "Device not ready; aborting %s\n", enabled ? |
759 | "initialisation" : "reset"); | |
760 | return -ENODEV; | |
761 | } | |
762 | } | |
763 | ||
764 | return ret; | |
765 | } | |
766 | ||
767 | /* | |
768 | * If the device has been passed off to us in an enabled state, just clear | |
769 | * the enabled bit. The spec says we should set the 'shutdown notification | |
770 | * bits', but doing so may cause the device to complete commands to the | |
771 | * admin queue ... and we don't know what memory that might be pointing at! | |
772 | */ | |
773 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) | |
774 | { | |
775 | int ret; | |
776 | ||
777 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; | |
778 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; | |
779 | ||
780 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
781 | if (ret) | |
782 | return ret; | |
783 | return nvme_wait_ready(ctrl, cap, false); | |
784 | } | |
785 | ||
786 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) | |
787 | { | |
788 | /* | |
789 | * Default to a 4K page size, with the intention to update this | |
790 | * path in the future to accomodate architectures with differing | |
791 | * kernel and IO page sizes. | |
792 | */ | |
793 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; | |
794 | int ret; | |
795 | ||
796 | if (page_shift < dev_page_min) { | |
1b3c47c1 | 797 | dev_err(ctrl->device, |
5fd4ce1b CH |
798 | "Minimum device page size %u too large for host (%u)\n", |
799 | 1 << dev_page_min, 1 << page_shift); | |
800 | return -ENODEV; | |
801 | } | |
802 | ||
803 | ctrl->page_size = 1 << page_shift; | |
804 | ||
805 | ctrl->ctrl_config = NVME_CC_CSS_NVM; | |
806 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; | |
807 | ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; | |
808 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; | |
809 | ctrl->ctrl_config |= NVME_CC_ENABLE; | |
810 | ||
811 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
812 | if (ret) | |
813 | return ret; | |
814 | return nvme_wait_ready(ctrl, cap, true); | |
815 | } | |
816 | ||
817 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) | |
818 | { | |
819 | unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies; | |
820 | u32 csts; | |
821 | int ret; | |
822 | ||
823 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; | |
824 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; | |
825 | ||
826 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); | |
827 | if (ret) | |
828 | return ret; | |
829 | ||
830 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { | |
831 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) | |
832 | break; | |
833 | ||
834 | msleep(100); | |
835 | if (fatal_signal_pending(current)) | |
836 | return -EINTR; | |
837 | if (time_after(jiffies, timeout)) { | |
1b3c47c1 | 838 | dev_err(ctrl->device, |
5fd4ce1b CH |
839 | "Device shutdown incomplete; abort shutdown\n"); |
840 | return -ENODEV; | |
841 | } | |
842 | } | |
843 | ||
844 | return ret; | |
845 | } | |
846 | ||
7fd8930f CH |
847 | /* |
848 | * Initialize the cached copies of the Identify data and various controller | |
849 | * register in our nvme_ctrl structure. This should be called as soon as | |
850 | * the admin queue is fully up and running. | |
851 | */ | |
852 | int nvme_init_identify(struct nvme_ctrl *ctrl) | |
853 | { | |
854 | struct nvme_id_ctrl *id; | |
855 | u64 cap; | |
856 | int ret, page_shift; | |
857 | ||
f3ca80fc CH |
858 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
859 | if (ret) { | |
1b3c47c1 | 860 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
f3ca80fc CH |
861 | return ret; |
862 | } | |
863 | ||
7fd8930f CH |
864 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
865 | if (ret) { | |
1b3c47c1 | 866 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
7fd8930f CH |
867 | return ret; |
868 | } | |
869 | page_shift = NVME_CAP_MPSMIN(cap) + 12; | |
870 | ||
f3ca80fc CH |
871 | if (ctrl->vs >= NVME_VS(1, 1)) |
872 | ctrl->subsystem = NVME_CAP_NSSRC(cap); | |
873 | ||
7fd8930f CH |
874 | ret = nvme_identify_ctrl(ctrl, &id); |
875 | if (ret) { | |
1b3c47c1 | 876 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
7fd8930f CH |
877 | return -EIO; |
878 | } | |
879 | ||
880 | ctrl->oncs = le16_to_cpup(&id->oncs); | |
6bf25d16 | 881 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
7fd8930f CH |
882 | ctrl->vwc = id->vwc; |
883 | memcpy(ctrl->serial, id->sn, sizeof(id->sn)); | |
884 | memcpy(ctrl->model, id->mn, sizeof(id->mn)); | |
885 | memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr)); | |
886 | if (id->mdts) | |
887 | ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9); | |
888 | else | |
889 | ctrl->max_hw_sectors = UINT_MAX; | |
890 | ||
891 | if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) { | |
892 | unsigned int max_hw_sectors; | |
893 | ||
894 | ctrl->stripe_size = 1 << (id->vs[3] + page_shift); | |
895 | max_hw_sectors = ctrl->stripe_size >> (page_shift - 9); | |
896 | if (ctrl->max_hw_sectors) { | |
897 | ctrl->max_hw_sectors = min(max_hw_sectors, | |
898 | ctrl->max_hw_sectors); | |
899 | } else { | |
900 | ctrl->max_hw_sectors = max_hw_sectors; | |
901 | } | |
902 | } | |
903 | ||
904 | kfree(id); | |
905 | return 0; | |
906 | } | |
907 | ||
f3ca80fc | 908 | static int nvme_dev_open(struct inode *inode, struct file *file) |
1673f1f0 | 909 | { |
f3ca80fc CH |
910 | struct nvme_ctrl *ctrl; |
911 | int instance = iminor(inode); | |
912 | int ret = -ENODEV; | |
1673f1f0 | 913 | |
f3ca80fc CH |
914 | spin_lock(&dev_list_lock); |
915 | list_for_each_entry(ctrl, &nvme_ctrl_list, node) { | |
916 | if (ctrl->instance != instance) | |
917 | continue; | |
918 | ||
919 | if (!ctrl->admin_q) { | |
920 | ret = -EWOULDBLOCK; | |
921 | break; | |
922 | } | |
923 | if (!kref_get_unless_zero(&ctrl->kref)) | |
924 | break; | |
925 | file->private_data = ctrl; | |
926 | ret = 0; | |
927 | break; | |
928 | } | |
929 | spin_unlock(&dev_list_lock); | |
930 | ||
931 | return ret; | |
1673f1f0 CH |
932 | } |
933 | ||
f3ca80fc | 934 | static int nvme_dev_release(struct inode *inode, struct file *file) |
1673f1f0 | 935 | { |
f3ca80fc CH |
936 | nvme_put_ctrl(file->private_data); |
937 | return 0; | |
938 | } | |
939 | ||
bfd89471 CH |
940 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
941 | { | |
942 | struct nvme_ns *ns; | |
943 | int ret; | |
944 | ||
945 | mutex_lock(&ctrl->namespaces_mutex); | |
946 | if (list_empty(&ctrl->namespaces)) { | |
947 | ret = -ENOTTY; | |
948 | goto out_unlock; | |
949 | } | |
950 | ||
951 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); | |
952 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { | |
1b3c47c1 | 953 | dev_warn(ctrl->device, |
bfd89471 CH |
954 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
955 | ret = -EINVAL; | |
956 | goto out_unlock; | |
957 | } | |
958 | ||
1b3c47c1 | 959 | dev_warn(ctrl->device, |
bfd89471 CH |
960 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
961 | kref_get(&ns->kref); | |
962 | mutex_unlock(&ctrl->namespaces_mutex); | |
963 | ||
964 | ret = nvme_user_cmd(ctrl, ns, argp); | |
965 | nvme_put_ns(ns); | |
966 | return ret; | |
967 | ||
968 | out_unlock: | |
969 | mutex_unlock(&ctrl->namespaces_mutex); | |
970 | return ret; | |
971 | } | |
972 | ||
f3ca80fc CH |
973 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
974 | unsigned long arg) | |
975 | { | |
976 | struct nvme_ctrl *ctrl = file->private_data; | |
977 | void __user *argp = (void __user *)arg; | |
f3ca80fc CH |
978 | |
979 | switch (cmd) { | |
980 | case NVME_IOCTL_ADMIN_CMD: | |
981 | return nvme_user_cmd(ctrl, NULL, argp); | |
982 | case NVME_IOCTL_IO_CMD: | |
bfd89471 | 983 | return nvme_dev_user_cmd(ctrl, argp); |
f3ca80fc | 984 | case NVME_IOCTL_RESET: |
1b3c47c1 | 985 | dev_warn(ctrl->device, "resetting controller\n"); |
f3ca80fc CH |
986 | return ctrl->ops->reset_ctrl(ctrl); |
987 | case NVME_IOCTL_SUBSYS_RESET: | |
988 | return nvme_reset_subsystem(ctrl); | |
989 | default: | |
990 | return -ENOTTY; | |
991 | } | |
992 | } | |
993 | ||
994 | static const struct file_operations nvme_dev_fops = { | |
995 | .owner = THIS_MODULE, | |
996 | .open = nvme_dev_open, | |
997 | .release = nvme_dev_release, | |
998 | .unlocked_ioctl = nvme_dev_ioctl, | |
999 | .compat_ioctl = nvme_dev_ioctl, | |
1000 | }; | |
1001 | ||
1002 | static ssize_t nvme_sysfs_reset(struct device *dev, | |
1003 | struct device_attribute *attr, const char *buf, | |
1004 | size_t count) | |
1005 | { | |
1006 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); | |
1007 | int ret; | |
1008 | ||
1009 | ret = ctrl->ops->reset_ctrl(ctrl); | |
1010 | if (ret < 0) | |
1011 | return ret; | |
1012 | return count; | |
1673f1f0 | 1013 | } |
f3ca80fc | 1014 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
1673f1f0 | 1015 | |
2b9b6e86 KB |
1016 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
1017 | char *buf) | |
1018 | { | |
1019 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; | |
1020 | return sprintf(buf, "%pU\n", ns->uuid); | |
1021 | } | |
1022 | static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); | |
1023 | ||
1024 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, | |
1025 | char *buf) | |
1026 | { | |
1027 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; | |
1028 | return sprintf(buf, "%8phd\n", ns->eui); | |
1029 | } | |
1030 | static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); | |
1031 | ||
1032 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, | |
1033 | char *buf) | |
1034 | { | |
1035 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; | |
1036 | return sprintf(buf, "%d\n", ns->ns_id); | |
1037 | } | |
1038 | static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); | |
1039 | ||
1040 | static struct attribute *nvme_ns_attrs[] = { | |
1041 | &dev_attr_uuid.attr, | |
1042 | &dev_attr_eui.attr, | |
1043 | &dev_attr_nsid.attr, | |
1044 | NULL, | |
1045 | }; | |
1046 | ||
1047 | static umode_t nvme_attrs_are_visible(struct kobject *kobj, | |
1048 | struct attribute *a, int n) | |
1049 | { | |
1050 | struct device *dev = container_of(kobj, struct device, kobj); | |
1051 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; | |
1052 | ||
1053 | if (a == &dev_attr_uuid.attr) { | |
1054 | if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) | |
1055 | return 0; | |
1056 | } | |
1057 | if (a == &dev_attr_eui.attr) { | |
1058 | if (!memchr_inv(ns->eui, 0, sizeof(ns->eui))) | |
1059 | return 0; | |
1060 | } | |
1061 | return a->mode; | |
1062 | } | |
1063 | ||
1064 | static const struct attribute_group nvme_ns_attr_group = { | |
1065 | .attrs = nvme_ns_attrs, | |
1066 | .is_visible = nvme_attrs_are_visible, | |
1067 | }; | |
1068 | ||
779ff756 KB |
1069 | #define nvme_show_function(field) \ |
1070 | static ssize_t field##_show(struct device *dev, \ | |
1071 | struct device_attribute *attr, char *buf) \ | |
1072 | { \ | |
1073 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ | |
1074 | return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \ | |
1075 | } \ | |
1076 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); | |
1077 | ||
1078 | nvme_show_function(model); | |
1079 | nvme_show_function(serial); | |
1080 | nvme_show_function(firmware_rev); | |
1081 | ||
1082 | static struct attribute *nvme_dev_attrs[] = { | |
1083 | &dev_attr_reset_controller.attr, | |
1084 | &dev_attr_model.attr, | |
1085 | &dev_attr_serial.attr, | |
1086 | &dev_attr_firmware_rev.attr, | |
1087 | NULL | |
1088 | }; | |
1089 | ||
1090 | static struct attribute_group nvme_dev_attrs_group = { | |
1091 | .attrs = nvme_dev_attrs, | |
1092 | }; | |
1093 | ||
1094 | static const struct attribute_group *nvme_dev_attr_groups[] = { | |
1095 | &nvme_dev_attrs_group, | |
1096 | NULL, | |
1097 | }; | |
1098 | ||
5bae7f73 CH |
1099 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
1100 | { | |
1101 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); | |
1102 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); | |
1103 | ||
1104 | return nsa->ns_id - nsb->ns_id; | |
1105 | } | |
1106 | ||
1107 | static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid) | |
1108 | { | |
1109 | struct nvme_ns *ns; | |
1110 | ||
69d3b8ac CH |
1111 | lockdep_assert_held(&ctrl->namespaces_mutex); |
1112 | ||
5bae7f73 CH |
1113 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
1114 | if (ns->ns_id == nsid) | |
1115 | return ns; | |
1116 | if (ns->ns_id > nsid) | |
1117 | break; | |
1118 | } | |
1119 | return NULL; | |
1120 | } | |
1121 | ||
1122 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) | |
1123 | { | |
1124 | struct nvme_ns *ns; | |
1125 | struct gendisk *disk; | |
1126 | int node = dev_to_node(ctrl->dev); | |
1127 | ||
69d3b8ac CH |
1128 | lockdep_assert_held(&ctrl->namespaces_mutex); |
1129 | ||
5bae7f73 CH |
1130 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
1131 | if (!ns) | |
1132 | return; | |
1133 | ||
1134 | ns->queue = blk_mq_init_queue(ctrl->tagset); | |
1135 | if (IS_ERR(ns->queue)) | |
1136 | goto out_free_ns; | |
1137 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); | |
1138 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); | |
1139 | ns->queue->queuedata = ns; | |
1140 | ns->ctrl = ctrl; | |
1141 | ||
1142 | disk = alloc_disk_node(0, node); | |
1143 | if (!disk) | |
1144 | goto out_free_queue; | |
1145 | ||
1146 | kref_init(&ns->kref); | |
1147 | ns->ns_id = nsid; | |
1148 | ns->disk = disk; | |
1149 | ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ | |
5bae7f73 CH |
1150 | |
1151 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); | |
1152 | if (ctrl->max_hw_sectors) { | |
1153 | blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors); | |
1154 | blk_queue_max_segments(ns->queue, | |
1155 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1); | |
1156 | } | |
1157 | if (ctrl->stripe_size) | |
1158 | blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9); | |
1159 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) | |
1160 | blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA); | |
1161 | blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1); | |
1162 | ||
1163 | disk->major = nvme_major; | |
1164 | disk->first_minor = 0; | |
1165 | disk->fops = &nvme_fops; | |
1166 | disk->private_data = ns; | |
1167 | disk->queue = ns->queue; | |
1168 | disk->driverfs_dev = ctrl->device; | |
1169 | disk->flags = GENHD_FL_EXT_DEVT; | |
1170 | sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, nsid); | |
1171 | ||
5bae7f73 CH |
1172 | if (nvme_revalidate_disk(ns->disk)) |
1173 | goto out_free_disk; | |
1174 | ||
4b9d5b15 | 1175 | list_add_tail(&ns->list, &ctrl->namespaces); |
5bae7f73 | 1176 | kref_get(&ctrl->kref); |
2b9b6e86 KB |
1177 | if (ns->type == NVME_NS_LIGHTNVM) |
1178 | return; | |
5bae7f73 | 1179 | |
2b9b6e86 KB |
1180 | add_disk(ns->disk); |
1181 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, | |
1182 | &nvme_ns_attr_group)) | |
1183 | pr_warn("%s: failed to create sysfs group for identification\n", | |
1184 | ns->disk->disk_name); | |
5bae7f73 CH |
1185 | return; |
1186 | out_free_disk: | |
1187 | kfree(disk); | |
5bae7f73 CH |
1188 | out_free_queue: |
1189 | blk_cleanup_queue(ns->queue); | |
1190 | out_free_ns: | |
1191 | kfree(ns); | |
1192 | } | |
1193 | ||
1194 | static void nvme_ns_remove(struct nvme_ns *ns) | |
1195 | { | |
1196 | bool kill = nvme_io_incapable(ns->ctrl) && | |
1197 | !blk_queue_dying(ns->queue); | |
1198 | ||
69d3b8ac CH |
1199 | lockdep_assert_held(&ns->ctrl->namespaces_mutex); |
1200 | ||
3e1e21c7 | 1201 | if (kill) { |
5bae7f73 | 1202 | blk_set_queue_dying(ns->queue); |
3e1e21c7 LT |
1203 | |
1204 | /* | |
1205 | * The controller was shutdown first if we got here through | |
1206 | * device removal. The shutdown may requeue outstanding | |
1207 | * requests. These need to be aborted immediately so | |
1208 | * del_gendisk doesn't block indefinitely for their completion. | |
1209 | */ | |
1210 | blk_mq_abort_requeue_list(ns->queue); | |
1211 | } | |
5bae7f73 CH |
1212 | if (ns->disk->flags & GENHD_FL_UP) { |
1213 | if (blk_get_integrity(ns->disk)) | |
1214 | blk_integrity_unregister(ns->disk); | |
2b9b6e86 KB |
1215 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
1216 | &nvme_ns_attr_group); | |
5bae7f73 CH |
1217 | del_gendisk(ns->disk); |
1218 | } | |
1219 | if (kill || !blk_queue_dying(ns->queue)) { | |
1220 | blk_mq_abort_requeue_list(ns->queue); | |
1221 | blk_cleanup_queue(ns->queue); | |
1222 | } | |
1223 | list_del_init(&ns->list); | |
1224 | nvme_put_ns(ns); | |
1225 | } | |
1226 | ||
540c801c KB |
1227 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
1228 | { | |
1229 | struct nvme_ns *ns; | |
1230 | ||
1231 | ns = nvme_find_ns(ctrl, nsid); | |
1232 | if (ns) { | |
1233 | if (revalidate_disk(ns->disk)) | |
1234 | nvme_ns_remove(ns); | |
1235 | } else | |
1236 | nvme_alloc_ns(ctrl, nsid); | |
1237 | } | |
1238 | ||
1239 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) | |
1240 | { | |
1241 | struct nvme_ns *ns; | |
1242 | __le32 *ns_list; | |
1243 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); | |
1244 | int ret = 0; | |
1245 | ||
1246 | ns_list = kzalloc(0x1000, GFP_KERNEL); | |
1247 | if (!ns_list) | |
1248 | return -ENOMEM; | |
1249 | ||
1250 | for (i = 0; i < num_lists; i++) { | |
1251 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); | |
1252 | if (ret) | |
1253 | goto out; | |
1254 | ||
1255 | for (j = 0; j < min(nn, 1024U); j++) { | |
1256 | nsid = le32_to_cpu(ns_list[j]); | |
1257 | if (!nsid) | |
1258 | goto out; | |
1259 | ||
1260 | nvme_validate_ns(ctrl, nsid); | |
1261 | ||
1262 | while (++prev < nsid) { | |
1263 | ns = nvme_find_ns(ctrl, prev); | |
1264 | if (ns) | |
1265 | nvme_ns_remove(ns); | |
1266 | } | |
1267 | } | |
1268 | nn -= j; | |
1269 | } | |
1270 | out: | |
1271 | kfree(ns_list); | |
1272 | return ret; | |
1273 | } | |
1274 | ||
5bae7f73 CH |
1275 | static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn) |
1276 | { | |
1277 | struct nvme_ns *ns, *next; | |
1278 | unsigned i; | |
1279 | ||
69d3b8ac CH |
1280 | lockdep_assert_held(&ctrl->namespaces_mutex); |
1281 | ||
540c801c KB |
1282 | for (i = 1; i <= nn; i++) |
1283 | nvme_validate_ns(ctrl, i); | |
1284 | ||
5bae7f73 CH |
1285 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { |
1286 | if (ns->ns_id > nn) | |
1287 | nvme_ns_remove(ns); | |
1288 | } | |
5bae7f73 CH |
1289 | } |
1290 | ||
1291 | void nvme_scan_namespaces(struct nvme_ctrl *ctrl) | |
1292 | { | |
1293 | struct nvme_id_ctrl *id; | |
540c801c | 1294 | unsigned nn; |
5bae7f73 CH |
1295 | |
1296 | if (nvme_identify_ctrl(ctrl, &id)) | |
1297 | return; | |
540c801c | 1298 | |
69d3b8ac | 1299 | mutex_lock(&ctrl->namespaces_mutex); |
540c801c KB |
1300 | nn = le32_to_cpu(id->nn); |
1301 | if (ctrl->vs >= NVME_VS(1, 1) && | |
1302 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { | |
1303 | if (!nvme_scan_ns_list(ctrl, nn)) | |
1304 | goto done; | |
1305 | } | |
5bae7f73 | 1306 | __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn)); |
540c801c KB |
1307 | done: |
1308 | list_sort(NULL, &ctrl->namespaces, ns_cmp); | |
69d3b8ac | 1309 | mutex_unlock(&ctrl->namespaces_mutex); |
5bae7f73 CH |
1310 | kfree(id); |
1311 | } | |
1312 | ||
1313 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) | |
1314 | { | |
1315 | struct nvme_ns *ns, *next; | |
1316 | ||
69d3b8ac | 1317 | mutex_lock(&ctrl->namespaces_mutex); |
5bae7f73 CH |
1318 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) |
1319 | nvme_ns_remove(ns); | |
69d3b8ac | 1320 | mutex_unlock(&ctrl->namespaces_mutex); |
5bae7f73 CH |
1321 | } |
1322 | ||
f3ca80fc CH |
1323 | static DEFINE_IDA(nvme_instance_ida); |
1324 | ||
1325 | static int nvme_set_instance(struct nvme_ctrl *ctrl) | |
1326 | { | |
1327 | int instance, error; | |
1328 | ||
1329 | do { | |
1330 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) | |
1331 | return -ENODEV; | |
1332 | ||
1333 | spin_lock(&dev_list_lock); | |
1334 | error = ida_get_new(&nvme_instance_ida, &instance); | |
1335 | spin_unlock(&dev_list_lock); | |
1336 | } while (error == -EAGAIN); | |
1337 | ||
1338 | if (error) | |
1339 | return -ENODEV; | |
1340 | ||
1341 | ctrl->instance = instance; | |
1342 | return 0; | |
1343 | } | |
1344 | ||
1345 | static void nvme_release_instance(struct nvme_ctrl *ctrl) | |
1346 | { | |
1347 | spin_lock(&dev_list_lock); | |
1348 | ida_remove(&nvme_instance_ida, ctrl->instance); | |
1349 | spin_unlock(&dev_list_lock); | |
1350 | } | |
1351 | ||
53029b04 KB |
1352 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
1353 | { | |
53029b04 | 1354 | device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance)); |
f3ca80fc CH |
1355 | |
1356 | spin_lock(&dev_list_lock); | |
1357 | list_del(&ctrl->node); | |
1358 | spin_unlock(&dev_list_lock); | |
53029b04 KB |
1359 | } |
1360 | ||
1361 | static void nvme_free_ctrl(struct kref *kref) | |
1362 | { | |
1363 | struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref); | |
f3ca80fc CH |
1364 | |
1365 | put_device(ctrl->device); | |
1366 | nvme_release_instance(ctrl); | |
f3ca80fc CH |
1367 | |
1368 | ctrl->ops->free_ctrl(ctrl); | |
1369 | } | |
1370 | ||
1371 | void nvme_put_ctrl(struct nvme_ctrl *ctrl) | |
1372 | { | |
1373 | kref_put(&ctrl->kref, nvme_free_ctrl); | |
1374 | } | |
1375 | ||
1376 | /* | |
1377 | * Initialize a NVMe controller structures. This needs to be called during | |
1378 | * earliest initialization so that we have the initialized structured around | |
1379 | * during probing. | |
1380 | */ | |
1381 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, | |
1382 | const struct nvme_ctrl_ops *ops, unsigned long quirks) | |
1383 | { | |
1384 | int ret; | |
1385 | ||
1386 | INIT_LIST_HEAD(&ctrl->namespaces); | |
69d3b8ac | 1387 | mutex_init(&ctrl->namespaces_mutex); |
f3ca80fc CH |
1388 | kref_init(&ctrl->kref); |
1389 | ctrl->dev = dev; | |
1390 | ctrl->ops = ops; | |
1391 | ctrl->quirks = quirks; | |
1392 | ||
1393 | ret = nvme_set_instance(ctrl); | |
1394 | if (ret) | |
1395 | goto out; | |
1396 | ||
779ff756 | 1397 | ctrl->device = device_create_with_groups(nvme_class, ctrl->dev, |
f3ca80fc | 1398 | MKDEV(nvme_char_major, ctrl->instance), |
f4f0f63e | 1399 | ctrl, nvme_dev_attr_groups, |
779ff756 | 1400 | "nvme%d", ctrl->instance); |
f3ca80fc CH |
1401 | if (IS_ERR(ctrl->device)) { |
1402 | ret = PTR_ERR(ctrl->device); | |
1403 | goto out_release_instance; | |
1404 | } | |
1405 | get_device(ctrl->device); | |
f3ca80fc | 1406 | |
f3ca80fc CH |
1407 | spin_lock(&dev_list_lock); |
1408 | list_add_tail(&ctrl->node, &nvme_ctrl_list); | |
1409 | spin_unlock(&dev_list_lock); | |
1410 | ||
1411 | return 0; | |
f3ca80fc CH |
1412 | out_release_instance: |
1413 | nvme_release_instance(ctrl); | |
1414 | out: | |
1415 | return ret; | |
1416 | } | |
1417 | ||
25646264 | 1418 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
363c9aac SG |
1419 | { |
1420 | struct nvme_ns *ns; | |
1421 | ||
69d3b8ac | 1422 | mutex_lock(&ctrl->namespaces_mutex); |
363c9aac | 1423 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
363c9aac SG |
1424 | spin_lock_irq(ns->queue->queue_lock); |
1425 | queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue); | |
1426 | spin_unlock_irq(ns->queue->queue_lock); | |
1427 | ||
1428 | blk_mq_cancel_requeue_work(ns->queue); | |
1429 | blk_mq_stop_hw_queues(ns->queue); | |
1430 | } | |
69d3b8ac | 1431 | mutex_unlock(&ctrl->namespaces_mutex); |
363c9aac SG |
1432 | } |
1433 | ||
25646264 | 1434 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
363c9aac SG |
1435 | { |
1436 | struct nvme_ns *ns; | |
1437 | ||
69d3b8ac | 1438 | mutex_lock(&ctrl->namespaces_mutex); |
363c9aac SG |
1439 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
1440 | queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue); | |
363c9aac SG |
1441 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
1442 | blk_mq_kick_requeue_list(ns->queue); | |
1443 | } | |
69d3b8ac | 1444 | mutex_unlock(&ctrl->namespaces_mutex); |
363c9aac SG |
1445 | } |
1446 | ||
5bae7f73 CH |
1447 | int __init nvme_core_init(void) |
1448 | { | |
1449 | int result; | |
1450 | ||
1451 | result = register_blkdev(nvme_major, "nvme"); | |
1452 | if (result < 0) | |
1453 | return result; | |
1454 | else if (result > 0) | |
1455 | nvme_major = result; | |
1456 | ||
f3ca80fc CH |
1457 | result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", |
1458 | &nvme_dev_fops); | |
1459 | if (result < 0) | |
1460 | goto unregister_blkdev; | |
1461 | else if (result > 0) | |
1462 | nvme_char_major = result; | |
1463 | ||
1464 | nvme_class = class_create(THIS_MODULE, "nvme"); | |
1465 | if (IS_ERR(nvme_class)) { | |
1466 | result = PTR_ERR(nvme_class); | |
1467 | goto unregister_chrdev; | |
1468 | } | |
1469 | ||
5bae7f73 | 1470 | return 0; |
f3ca80fc CH |
1471 | |
1472 | unregister_chrdev: | |
1473 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); | |
1474 | unregister_blkdev: | |
1475 | unregister_blkdev(nvme_major, "nvme"); | |
1476 | return result; | |
5bae7f73 CH |
1477 | } |
1478 | ||
1479 | void nvme_core_exit(void) | |
1480 | { | |
1481 | unregister_blkdev(nvme_major, "nvme"); | |
f3ca80fc CH |
1482 | class_destroy(nvme_class); |
1483 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); | |
5bae7f73 | 1484 | } |