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