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