2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
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.
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
15 #include <linux/blkdev.h>
16 #include <linux/blk-mq.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/hdreg.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list_sort.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <linux/ptrace.h>
27 #include <linux/nvme_ioctl.h>
28 #include <linux/t10-pi.h>
30 #include <asm/unaligned.h>
34 #define NVME_MINORS (1U << MINORBITS)
36 unsigned char admin_timeout
= 60;
37 module_param(admin_timeout
, byte
, 0644);
38 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
39 EXPORT_SYMBOL_GPL(admin_timeout
);
41 unsigned char nvme_io_timeout
= 30;
42 module_param_named(io_timeout
, nvme_io_timeout
, byte
, 0644);
43 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
44 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
46 unsigned char shutdown_timeout
= 5;
47 module_param(shutdown_timeout
, byte
, 0644);
48 MODULE_PARM_DESC(shutdown_timeout
, "timeout in seconds for controller shutdown");
50 static int nvme_major
;
51 module_param(nvme_major
, int, 0);
53 static int nvme_char_major
;
54 module_param(nvme_char_major
, int, 0);
56 static LIST_HEAD(nvme_ctrl_list
);
57 static DEFINE_SPINLOCK(dev_list_lock
);
59 static struct class *nvme_class
;
61 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
62 enum nvme_ctrl_state new_state
)
64 enum nvme_ctrl_state old_state
= ctrl
->state
;
67 spin_lock_irq(&ctrl
->lock
);
71 case NVME_CTRL_RESETTING
:
78 case NVME_CTRL_RESETTING
:
88 case NVME_CTRL_DELETING
:
91 case NVME_CTRL_RESETTING
:
100 case NVME_CTRL_DELETING
:
110 spin_unlock_irq(&ctrl
->lock
);
113 ctrl
->state
= new_state
;
117 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
119 static void nvme_free_ns(struct kref
*kref
)
121 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
123 if (ns
->type
== NVME_NS_LIGHTNVM
)
124 nvme_nvm_unregister(ns
->queue
, ns
->disk
->disk_name
);
126 spin_lock(&dev_list_lock
);
127 ns
->disk
->private_data
= NULL
;
128 spin_unlock(&dev_list_lock
);
131 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
132 nvme_put_ctrl(ns
->ctrl
);
136 static void nvme_put_ns(struct nvme_ns
*ns
)
138 kref_put(&ns
->kref
, nvme_free_ns
);
141 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
)
145 spin_lock(&dev_list_lock
);
146 ns
= disk
->private_data
;
148 if (!kref_get_unless_zero(&ns
->kref
))
150 if (!try_module_get(ns
->ctrl
->ops
->module
))
153 spin_unlock(&dev_list_lock
);
158 kref_put(&ns
->kref
, nvme_free_ns
);
160 spin_unlock(&dev_list_lock
);
164 void nvme_requeue_req(struct request
*req
)
168 blk_mq_requeue_request(req
);
169 spin_lock_irqsave(req
->q
->queue_lock
, flags
);
170 if (!blk_queue_stopped(req
->q
))
171 blk_mq_kick_requeue_list(req
->q
);
172 spin_unlock_irqrestore(req
->q
->queue_lock
, flags
);
174 EXPORT_SYMBOL_GPL(nvme_requeue_req
);
176 struct request
*nvme_alloc_request(struct request_queue
*q
,
177 struct nvme_command
*cmd
, unsigned int flags
)
179 bool write
= cmd
->common
.opcode
& 1;
182 req
= blk_mq_alloc_request(q
, write
, flags
);
186 req
->cmd_type
= REQ_TYPE_DRV_PRIV
;
187 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
189 req
->__sector
= (sector_t
) -1;
190 req
->bio
= req
->biotail
= NULL
;
192 req
->cmd
= (unsigned char *)cmd
;
193 req
->cmd_len
= sizeof(struct nvme_command
);
197 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
199 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
200 struct nvme_command
*cmnd
)
202 memset(cmnd
, 0, sizeof(*cmnd
));
203 cmnd
->common
.opcode
= nvme_cmd_flush
;
204 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
207 static inline int nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
208 struct nvme_command
*cmnd
)
210 struct nvme_dsm_range
*range
;
213 unsigned int nr_bytes
= blk_rq_bytes(req
);
215 range
= kmalloc(sizeof(*range
), GFP_ATOMIC
);
217 return BLK_MQ_RQ_QUEUE_BUSY
;
219 range
->cattr
= cpu_to_le32(0);
220 range
->nlb
= cpu_to_le32(nr_bytes
>> ns
->lba_shift
);
221 range
->slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
223 memset(cmnd
, 0, sizeof(*cmnd
));
224 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
225 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
227 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
229 req
->completion_data
= range
;
230 page
= virt_to_page(range
);
231 offset
= offset_in_page(range
);
232 blk_add_request_payload(req
, page
, offset
, sizeof(*range
));
235 * we set __data_len back to the size of the area to be discarded
236 * on disk. This allows us to report completion on the full amount
237 * of blocks described by the request.
239 req
->__data_len
= nr_bytes
;
244 static inline void nvme_setup_rw(struct nvme_ns
*ns
, struct request
*req
,
245 struct nvme_command
*cmnd
)
250 if (req
->cmd_flags
& REQ_FUA
)
251 control
|= NVME_RW_FUA
;
252 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
253 control
|= NVME_RW_LR
;
255 if (req
->cmd_flags
& REQ_RAHEAD
)
256 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
258 memset(cmnd
, 0, sizeof(*cmnd
));
259 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
260 cmnd
->rw
.command_id
= req
->tag
;
261 cmnd
->rw
.nsid
= cpu_to_le32(ns
->ns_id
);
262 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
263 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
266 switch (ns
->pi_type
) {
267 case NVME_NS_DPS_PI_TYPE3
:
268 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
270 case NVME_NS_DPS_PI_TYPE1
:
271 case NVME_NS_DPS_PI_TYPE2
:
272 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
273 NVME_RW_PRINFO_PRCHK_REF
;
274 cmnd
->rw
.reftag
= cpu_to_le32(
275 nvme_block_nr(ns
, blk_rq_pos(req
)));
278 if (!blk_integrity_rq(req
))
279 control
|= NVME_RW_PRINFO_PRACT
;
282 cmnd
->rw
.control
= cpu_to_le16(control
);
283 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
286 int nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
287 struct nvme_command
*cmd
)
291 if (req
->cmd_type
== REQ_TYPE_DRV_PRIV
)
292 memcpy(cmd
, req
->cmd
, sizeof(*cmd
));
293 else if (req
->cmd_flags
& REQ_FLUSH
)
294 nvme_setup_flush(ns
, cmd
);
295 else if (req
->cmd_flags
& REQ_DISCARD
)
296 ret
= nvme_setup_discard(ns
, req
, cmd
);
298 nvme_setup_rw(ns
, req
, cmd
);
302 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
305 * Returns 0 on success. If the result is negative, it's a Linux error code;
306 * if the result is positive, it's an NVM Express status code
308 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
309 struct nvme_completion
*cqe
, void *buffer
, unsigned bufflen
,
315 req
= nvme_alloc_request(q
, cmd
, 0);
319 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
322 if (buffer
&& bufflen
) {
323 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
328 blk_execute_rq(req
->q
, NULL
, req
, 0);
331 blk_mq_free_request(req
);
335 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
336 void *buffer
, unsigned bufflen
)
338 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0);
340 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
342 int __nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
343 void __user
*ubuffer
, unsigned bufflen
,
344 void __user
*meta_buffer
, unsigned meta_len
, u32 meta_seed
,
345 u32
*result
, unsigned timeout
)
347 bool write
= cmd
->common
.opcode
& 1;
348 struct nvme_completion cqe
;
349 struct nvme_ns
*ns
= q
->queuedata
;
350 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
352 struct bio
*bio
= NULL
;
356 req
= nvme_alloc_request(q
, cmd
, 0);
360 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
363 if (ubuffer
&& bufflen
) {
364 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
372 bio
->bi_bdev
= bdget_disk(disk
, 0);
378 if (meta_buffer
&& meta_len
) {
379 struct bio_integrity_payload
*bip
;
381 meta
= kmalloc(meta_len
, GFP_KERNEL
);
388 if (copy_from_user(meta
, meta_buffer
,
395 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
401 bip
->bip_iter
.bi_size
= meta_len
;
402 bip
->bip_iter
.bi_sector
= meta_seed
;
404 ret
= bio_integrity_add_page(bio
, virt_to_page(meta
),
405 meta_len
, offset_in_page(meta
));
406 if (ret
!= meta_len
) {
413 blk_execute_rq(req
->q
, disk
, req
, 0);
416 *result
= le32_to_cpu(cqe
.result
);
417 if (meta
&& !ret
&& !write
) {
418 if (copy_to_user(meta_buffer
, meta
, meta_len
))
425 if (disk
&& bio
->bi_bdev
)
427 blk_rq_unmap_user(bio
);
430 blk_mq_free_request(req
);
434 int nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
435 void __user
*ubuffer
, unsigned bufflen
, u32
*result
,
438 return __nvme_submit_user_cmd(q
, cmd
, ubuffer
, bufflen
, NULL
, 0, 0,
442 int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
444 struct nvme_command c
= { };
447 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
448 c
.identify
.opcode
= nvme_admin_identify
;
449 c
.identify
.cns
= cpu_to_le32(1);
451 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
455 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
456 sizeof(struct nvme_id_ctrl
));
462 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
464 struct nvme_command c
= { };
466 c
.identify
.opcode
= nvme_admin_identify
;
467 c
.identify
.cns
= cpu_to_le32(2);
468 c
.identify
.nsid
= cpu_to_le32(nsid
);
469 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
472 int nvme_identify_ns(struct nvme_ctrl
*dev
, unsigned nsid
,
473 struct nvme_id_ns
**id
)
475 struct nvme_command c
= { };
478 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
479 c
.identify
.opcode
= nvme_admin_identify
,
480 c
.identify
.nsid
= cpu_to_le32(nsid
),
482 *id
= kmalloc(sizeof(struct nvme_id_ns
), GFP_KERNEL
);
486 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
487 sizeof(struct nvme_id_ns
));
493 int nvme_get_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned nsid
,
494 dma_addr_t dma_addr
, u32
*result
)
496 struct nvme_command c
;
497 struct nvme_completion cqe
;
500 memset(&c
, 0, sizeof(c
));
501 c
.features
.opcode
= nvme_admin_get_features
;
502 c
.features
.nsid
= cpu_to_le32(nsid
);
503 c
.features
.prp1
= cpu_to_le64(dma_addr
);
504 c
.features
.fid
= cpu_to_le32(fid
);
506 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &cqe
, NULL
, 0, 0);
508 *result
= le32_to_cpu(cqe
.result
);
512 int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
513 dma_addr_t dma_addr
, u32
*result
)
515 struct nvme_command c
;
516 struct nvme_completion cqe
;
519 memset(&c
, 0, sizeof(c
));
520 c
.features
.opcode
= nvme_admin_set_features
;
521 c
.features
.prp1
= cpu_to_le64(dma_addr
);
522 c
.features
.fid
= cpu_to_le32(fid
);
523 c
.features
.dword11
= cpu_to_le32(dword11
);
525 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &cqe
, NULL
, 0, 0);
527 *result
= le32_to_cpu(cqe
.result
);
531 int nvme_get_log_page(struct nvme_ctrl
*dev
, struct nvme_smart_log
**log
)
533 struct nvme_command c
= { };
536 c
.common
.opcode
= nvme_admin_get_log_page
,
537 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF),
538 c
.common
.cdw10
[0] = cpu_to_le32(
539 (((sizeof(struct nvme_smart_log
) / 4) - 1) << 16) |
542 *log
= kmalloc(sizeof(struct nvme_smart_log
), GFP_KERNEL
);
546 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *log
,
547 sizeof(struct nvme_smart_log
));
553 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
555 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
557 int status
, nr_io_queues
;
559 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, 0,
564 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
565 *count
= min(*count
, nr_io_queues
);
568 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
570 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
572 struct nvme_user_io io
;
573 struct nvme_command c
;
574 unsigned length
, meta_len
;
575 void __user
*metadata
;
577 if (copy_from_user(&io
, uio
, sizeof(io
)))
585 case nvme_cmd_compare
:
591 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
592 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
593 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
598 } else if (meta_len
) {
599 if ((io
.metadata
& 3) || !io
.metadata
)
603 memset(&c
, 0, sizeof(c
));
604 c
.rw
.opcode
= io
.opcode
;
605 c
.rw
.flags
= io
.flags
;
606 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
607 c
.rw
.slba
= cpu_to_le64(io
.slba
);
608 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
609 c
.rw
.control
= cpu_to_le16(io
.control
);
610 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
611 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
612 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
613 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
615 return __nvme_submit_user_cmd(ns
->queue
, &c
,
616 (void __user
*)(uintptr_t)io
.addr
, length
,
617 metadata
, meta_len
, io
.slba
, NULL
, 0);
620 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
621 struct nvme_passthru_cmd __user
*ucmd
)
623 struct nvme_passthru_cmd cmd
;
624 struct nvme_command c
;
625 unsigned timeout
= 0;
628 if (!capable(CAP_SYS_ADMIN
))
630 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
635 memset(&c
, 0, sizeof(c
));
636 c
.common
.opcode
= cmd
.opcode
;
637 c
.common
.flags
= cmd
.flags
;
638 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
639 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
640 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
641 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
642 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
643 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
644 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
645 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
646 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
649 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
651 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
652 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
653 &cmd
.result
, timeout
);
655 if (put_user(cmd
.result
, &ucmd
->result
))
662 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
663 unsigned int cmd
, unsigned long arg
)
665 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
669 force_successful_syscall_return();
671 case NVME_IOCTL_ADMIN_CMD
:
672 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
673 case NVME_IOCTL_IO_CMD
:
674 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
675 case NVME_IOCTL_SUBMIT_IO
:
676 return nvme_submit_io(ns
, (void __user
*)arg
);
677 #ifdef CONFIG_BLK_DEV_NVME_SCSI
678 case SG_GET_VERSION_NUM
:
679 return nvme_sg_get_version_num((void __user
*)arg
);
681 return nvme_sg_io(ns
, (void __user
*)arg
);
689 static int nvme_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
690 unsigned int cmd
, unsigned long arg
)
696 return nvme_ioctl(bdev
, mode
, cmd
, arg
);
699 #define nvme_compat_ioctl NULL
702 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
704 return nvme_get_ns_from_disk(bdev
->bd_disk
) ? 0 : -ENXIO
;
707 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
709 struct nvme_ns
*ns
= disk
->private_data
;
711 module_put(ns
->ctrl
->ops
->module
);
715 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
717 /* some standard values */
719 geo
->sectors
= 1 << 5;
720 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
724 #ifdef CONFIG_BLK_DEV_INTEGRITY
725 static void nvme_init_integrity(struct nvme_ns
*ns
)
727 struct blk_integrity integrity
;
729 switch (ns
->pi_type
) {
730 case NVME_NS_DPS_PI_TYPE3
:
731 integrity
.profile
= &t10_pi_type3_crc
;
732 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
733 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
735 case NVME_NS_DPS_PI_TYPE1
:
736 case NVME_NS_DPS_PI_TYPE2
:
737 integrity
.profile
= &t10_pi_type1_crc
;
738 integrity
.tag_size
= sizeof(u16
);
739 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
742 integrity
.profile
= NULL
;
745 integrity
.tuple_size
= ns
->ms
;
746 blk_integrity_register(ns
->disk
, &integrity
);
747 blk_queue_max_integrity_segments(ns
->queue
, 1);
750 static void nvme_init_integrity(struct nvme_ns
*ns
)
753 #endif /* CONFIG_BLK_DEV_INTEGRITY */
755 static void nvme_config_discard(struct nvme_ns
*ns
)
757 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
758 u32 logical_block_size
= queue_logical_block_size(ns
->queue
);
760 if (ctrl
->quirks
& NVME_QUIRK_DISCARD_ZEROES
)
761 ns
->queue
->limits
.discard_zeroes_data
= 1;
763 ns
->queue
->limits
.discard_zeroes_data
= 0;
765 ns
->queue
->limits
.discard_alignment
= logical_block_size
;
766 ns
->queue
->limits
.discard_granularity
= logical_block_size
;
767 blk_queue_max_discard_sectors(ns
->queue
, 0xffffffff);
768 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, ns
->queue
);
771 static int nvme_revalidate_disk(struct gendisk
*disk
)
773 struct nvme_ns
*ns
= disk
->private_data
;
774 struct nvme_id_ns
*id
;
779 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
780 set_capacity(disk
, 0);
783 if (nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id
)) {
784 dev_warn(disk_to_dev(ns
->disk
), "%s: Identify failure\n",
793 if (nvme_nvm_ns_supported(ns
, id
) && ns
->type
!= NVME_NS_LIGHTNVM
) {
794 if (nvme_nvm_register(ns
->queue
, disk
->disk_name
)) {
795 dev_warn(disk_to_dev(ns
->disk
),
796 "%s: LightNVM init failure\n", __func__
);
800 ns
->type
= NVME_NS_LIGHTNVM
;
803 if (ns
->ctrl
->vs
>= NVME_VS(1, 1))
804 memcpy(ns
->eui
, id
->eui64
, sizeof(ns
->eui
));
805 if (ns
->ctrl
->vs
>= NVME_VS(1, 2))
806 memcpy(ns
->uuid
, id
->nguid
, sizeof(ns
->uuid
));
809 lbaf
= id
->flbas
& NVME_NS_FLBAS_LBA_MASK
;
810 ns
->lba_shift
= id
->lbaf
[lbaf
].ds
;
811 ns
->ms
= le16_to_cpu(id
->lbaf
[lbaf
].ms
);
812 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
815 * If identify namespace failed, use default 512 byte block size so
816 * block layer can use before failing read/write for 0 capacity.
818 if (ns
->lba_shift
== 0)
820 bs
= 1 << ns
->lba_shift
;
821 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
822 pi_type
= ns
->ms
== sizeof(struct t10_pi_tuple
) ?
823 id
->dps
& NVME_NS_DPS_PI_MASK
: 0;
825 blk_mq_freeze_queue(disk
->queue
);
826 if (blk_get_integrity(disk
) && (ns
->pi_type
!= pi_type
||
828 bs
!= queue_logical_block_size(disk
->queue
) ||
829 (ns
->ms
&& ns
->ext
)))
830 blk_integrity_unregister(disk
);
832 ns
->pi_type
= pi_type
;
833 blk_queue_logical_block_size(ns
->queue
, bs
);
835 if (ns
->ms
&& !blk_get_integrity(disk
) && !ns
->ext
)
836 nvme_init_integrity(ns
);
837 if (ns
->ms
&& !(ns
->ms
== 8 && ns
->pi_type
) && !blk_get_integrity(disk
))
838 set_capacity(disk
, 0);
840 set_capacity(disk
, le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9));
842 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
843 nvme_config_discard(ns
);
844 blk_mq_unfreeze_queue(disk
->queue
);
850 static char nvme_pr_type(enum pr_type type
)
853 case PR_WRITE_EXCLUSIVE
:
855 case PR_EXCLUSIVE_ACCESS
:
857 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
859 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
861 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
863 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
870 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
871 u64 key
, u64 sa_key
, u8 op
)
873 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
874 struct nvme_command c
;
875 u8 data
[16] = { 0, };
877 put_unaligned_le64(key
, &data
[0]);
878 put_unaligned_le64(sa_key
, &data
[8]);
880 memset(&c
, 0, sizeof(c
));
881 c
.common
.opcode
= op
;
882 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
883 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
885 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
888 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
889 u64
new, unsigned flags
)
893 if (flags
& ~PR_FL_IGNORE_KEY
)
897 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
898 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
899 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
902 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
903 enum pr_type type
, unsigned flags
)
907 if (flags
& ~PR_FL_IGNORE_KEY
)
910 cdw10
= nvme_pr_type(type
) << 8;
911 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
912 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
915 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
916 enum pr_type type
, bool abort
)
918 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
919 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
922 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
924 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
925 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
928 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
930 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
931 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
934 static const struct pr_ops nvme_pr_ops
= {
935 .pr_register
= nvme_pr_register
,
936 .pr_reserve
= nvme_pr_reserve
,
937 .pr_release
= nvme_pr_release
,
938 .pr_preempt
= nvme_pr_preempt
,
939 .pr_clear
= nvme_pr_clear
,
942 static const struct block_device_operations nvme_fops
= {
943 .owner
= THIS_MODULE
,
945 .compat_ioctl
= nvme_compat_ioctl
,
947 .release
= nvme_release
,
948 .getgeo
= nvme_getgeo
,
949 .revalidate_disk
= nvme_revalidate_disk
,
950 .pr_ops
= &nvme_pr_ops
,
953 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
955 unsigned long timeout
=
956 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
957 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
960 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
961 if ((csts
& NVME_CSTS_RDY
) == bit
)
965 if (fatal_signal_pending(current
))
967 if (time_after(jiffies
, timeout
)) {
968 dev_err(ctrl
->device
,
969 "Device not ready; aborting %s\n", enabled
?
970 "initialisation" : "reset");
979 * If the device has been passed off to us in an enabled state, just clear
980 * the enabled bit. The spec says we should set the 'shutdown notification
981 * bits', but doing so may cause the device to complete commands to the
982 * admin queue ... and we don't know what memory that might be pointing at!
984 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
988 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
989 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
991 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
994 return nvme_wait_ready(ctrl
, cap
, false);
996 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
998 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1001 * Default to a 4K page size, with the intention to update this
1002 * path in the future to accomodate architectures with differing
1003 * kernel and IO page sizes.
1005 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1008 if (page_shift
< dev_page_min
) {
1009 dev_err(ctrl
->device
,
1010 "Minimum device page size %u too large for host (%u)\n",
1011 1 << dev_page_min
, 1 << page_shift
);
1015 ctrl
->page_size
= 1 << page_shift
;
1017 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1018 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1019 ctrl
->ctrl_config
|= NVME_CC_ARB_RR
| NVME_CC_SHN_NONE
;
1020 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1021 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1023 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1026 return nvme_wait_ready(ctrl
, cap
, true);
1028 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1030 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1032 unsigned long timeout
= SHUTDOWN_TIMEOUT
+ jiffies
;
1036 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1037 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1039 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1043 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1044 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1048 if (fatal_signal_pending(current
))
1050 if (time_after(jiffies
, timeout
)) {
1051 dev_err(ctrl
->device
,
1052 "Device shutdown incomplete; abort shutdown\n");
1059 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1061 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1062 struct request_queue
*q
)
1066 if (ctrl
->max_hw_sectors
) {
1068 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1070 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1071 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1073 if (ctrl
->stripe_size
)
1074 blk_queue_chunk_sectors(q
, ctrl
->stripe_size
>> 9);
1075 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1076 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1078 blk_queue_write_cache(q
, vwc
, vwc
);
1082 * Initialize the cached copies of the Identify data and various controller
1083 * register in our nvme_ctrl structure. This should be called as soon as
1084 * the admin queue is fully up and running.
1086 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1088 struct nvme_id_ctrl
*id
;
1090 int ret
, page_shift
;
1092 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1094 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1098 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1100 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1103 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1105 if (ctrl
->vs
>= NVME_VS(1, 1))
1106 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1108 ret
= nvme_identify_ctrl(ctrl
, &id
);
1110 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1114 ctrl
->vid
= le16_to_cpu(id
->vid
);
1115 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1116 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1117 ctrl
->vwc
= id
->vwc
;
1118 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1119 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1120 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1121 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1123 ctrl
->max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1125 ctrl
->max_hw_sectors
= UINT_MAX
;
1127 if ((ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
) && id
->vs
[3]) {
1128 unsigned int max_hw_sectors
;
1130 ctrl
->stripe_size
= 1 << (id
->vs
[3] + page_shift
);
1131 max_hw_sectors
= ctrl
->stripe_size
>> (page_shift
- 9);
1132 if (ctrl
->max_hw_sectors
) {
1133 ctrl
->max_hw_sectors
= min(max_hw_sectors
,
1134 ctrl
->max_hw_sectors
);
1136 ctrl
->max_hw_sectors
= max_hw_sectors
;
1140 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1145 EXPORT_SYMBOL_GPL(nvme_init_identify
);
1147 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
1149 struct nvme_ctrl
*ctrl
;
1150 int instance
= iminor(inode
);
1153 spin_lock(&dev_list_lock
);
1154 list_for_each_entry(ctrl
, &nvme_ctrl_list
, node
) {
1155 if (ctrl
->instance
!= instance
)
1158 if (!ctrl
->admin_q
) {
1162 if (!kref_get_unless_zero(&ctrl
->kref
))
1164 file
->private_data
= ctrl
;
1168 spin_unlock(&dev_list_lock
);
1173 static int nvme_dev_release(struct inode
*inode
, struct file
*file
)
1175 nvme_put_ctrl(file
->private_data
);
1179 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
1184 mutex_lock(&ctrl
->namespaces_mutex
);
1185 if (list_empty(&ctrl
->namespaces
)) {
1190 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
1191 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
1192 dev_warn(ctrl
->device
,
1193 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1198 dev_warn(ctrl
->device
,
1199 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1200 kref_get(&ns
->kref
);
1201 mutex_unlock(&ctrl
->namespaces_mutex
);
1203 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
1208 mutex_unlock(&ctrl
->namespaces_mutex
);
1212 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
1215 struct nvme_ctrl
*ctrl
= file
->private_data
;
1216 void __user
*argp
= (void __user
*)arg
;
1219 case NVME_IOCTL_ADMIN_CMD
:
1220 return nvme_user_cmd(ctrl
, NULL
, argp
);
1221 case NVME_IOCTL_IO_CMD
:
1222 return nvme_dev_user_cmd(ctrl
, argp
);
1223 case NVME_IOCTL_RESET
:
1224 dev_warn(ctrl
->device
, "resetting controller\n");
1225 return ctrl
->ops
->reset_ctrl(ctrl
);
1226 case NVME_IOCTL_SUBSYS_RESET
:
1227 return nvme_reset_subsystem(ctrl
);
1228 case NVME_IOCTL_RESCAN
:
1229 nvme_queue_scan(ctrl
);
1236 static const struct file_operations nvme_dev_fops
= {
1237 .owner
= THIS_MODULE
,
1238 .open
= nvme_dev_open
,
1239 .release
= nvme_dev_release
,
1240 .unlocked_ioctl
= nvme_dev_ioctl
,
1241 .compat_ioctl
= nvme_dev_ioctl
,
1244 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
1245 struct device_attribute
*attr
, const char *buf
,
1248 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1251 ret
= ctrl
->ops
->reset_ctrl(ctrl
);
1256 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
1258 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
1259 struct device_attribute
*attr
, const char *buf
,
1262 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1264 nvme_queue_scan(ctrl
);
1267 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
1269 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
1272 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1273 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1274 int serial_len
= sizeof(ctrl
->serial
);
1275 int model_len
= sizeof(ctrl
->model
);
1277 if (memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1278 return sprintf(buf
, "eui.%16phN\n", ns
->uuid
);
1280 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1281 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
1283 while (ctrl
->serial
[serial_len
- 1] == ' ')
1285 while (ctrl
->model
[model_len
- 1] == ' ')
1288 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
1289 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
1291 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
1293 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
1296 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1297 return sprintf(buf
, "%pU\n", ns
->uuid
);
1299 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
1301 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
1304 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1305 return sprintf(buf
, "%8phd\n", ns
->eui
);
1307 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
1309 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
1312 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1313 return sprintf(buf
, "%d\n", ns
->ns_id
);
1315 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
1317 static struct attribute
*nvme_ns_attrs
[] = {
1318 &dev_attr_wwid
.attr
,
1319 &dev_attr_uuid
.attr
,
1321 &dev_attr_nsid
.attr
,
1325 static umode_t
nvme_attrs_are_visible(struct kobject
*kobj
,
1326 struct attribute
*a
, int n
)
1328 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1329 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1331 if (a
== &dev_attr_uuid
.attr
) {
1332 if (!memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1335 if (a
== &dev_attr_eui
.attr
) {
1336 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1342 static const struct attribute_group nvme_ns_attr_group
= {
1343 .attrs
= nvme_ns_attrs
,
1344 .is_visible
= nvme_attrs_are_visible
,
1347 #define nvme_show_str_function(field) \
1348 static ssize_t field##_show(struct device *dev, \
1349 struct device_attribute *attr, char *buf) \
1351 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1352 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1354 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1356 #define nvme_show_int_function(field) \
1357 static ssize_t field##_show(struct device *dev, \
1358 struct device_attribute *attr, char *buf) \
1360 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1361 return sprintf(buf, "%d\n", ctrl->field); \
1363 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1365 nvme_show_str_function(model
);
1366 nvme_show_str_function(serial
);
1367 nvme_show_str_function(firmware_rev
);
1368 nvme_show_int_function(cntlid
);
1370 static struct attribute
*nvme_dev_attrs
[] = {
1371 &dev_attr_reset_controller
.attr
,
1372 &dev_attr_rescan_controller
.attr
,
1373 &dev_attr_model
.attr
,
1374 &dev_attr_serial
.attr
,
1375 &dev_attr_firmware_rev
.attr
,
1376 &dev_attr_cntlid
.attr
,
1380 static struct attribute_group nvme_dev_attrs_group
= {
1381 .attrs
= nvme_dev_attrs
,
1384 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
1385 &nvme_dev_attrs_group
,
1389 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1391 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
1392 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
1394 return nsa
->ns_id
- nsb
->ns_id
;
1397 static struct nvme_ns
*nvme_find_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1401 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1403 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
1404 if (ns
->ns_id
== nsid
)
1406 if (ns
->ns_id
> nsid
)
1412 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1415 struct gendisk
*disk
;
1416 int node
= dev_to_node(ctrl
->dev
);
1418 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1420 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
1424 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
1425 if (ns
->instance
< 0)
1428 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
1429 if (IS_ERR(ns
->queue
))
1430 goto out_release_instance
;
1431 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
1432 ns
->queue
->queuedata
= ns
;
1435 disk
= alloc_disk_node(0, node
);
1437 goto out_free_queue
;
1439 kref_init(&ns
->kref
);
1442 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
1445 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
1446 nvme_set_queue_limits(ctrl
, ns
->queue
);
1448 disk
->major
= nvme_major
;
1449 disk
->first_minor
= 0;
1450 disk
->fops
= &nvme_fops
;
1451 disk
->private_data
= ns
;
1452 disk
->queue
= ns
->queue
;
1453 disk
->driverfs_dev
= ctrl
->device
;
1454 disk
->flags
= GENHD_FL_EXT_DEVT
;
1455 sprintf(disk
->disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
1457 if (nvme_revalidate_disk(ns
->disk
))
1460 list_add_tail_rcu(&ns
->list
, &ctrl
->namespaces
);
1461 kref_get(&ctrl
->kref
);
1462 if (ns
->type
== NVME_NS_LIGHTNVM
)
1466 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
1467 &nvme_ns_attr_group
))
1468 pr_warn("%s: failed to create sysfs group for identification\n",
1469 ns
->disk
->disk_name
);
1474 blk_cleanup_queue(ns
->queue
);
1475 out_release_instance
:
1476 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
1481 static void nvme_ns_remove(struct nvme_ns
*ns
)
1483 lockdep_assert_held(&ns
->ctrl
->namespaces_mutex
);
1485 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
1488 if (ns
->disk
->flags
& GENHD_FL_UP
) {
1489 if (blk_get_integrity(ns
->disk
))
1490 blk_integrity_unregister(ns
->disk
);
1491 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
1492 &nvme_ns_attr_group
);
1493 del_gendisk(ns
->disk
);
1494 blk_mq_abort_requeue_list(ns
->queue
);
1495 blk_cleanup_queue(ns
->queue
);
1497 list_del_init(&ns
->list
);
1502 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1506 ns
= nvme_find_ns(ctrl
, nsid
);
1508 if (revalidate_disk(ns
->disk
))
1511 nvme_alloc_ns(ctrl
, nsid
);
1514 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
1518 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
1521 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
1525 for (i
= 0; i
< num_lists
; i
++) {
1526 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
1530 for (j
= 0; j
< min(nn
, 1024U); j
++) {
1531 nsid
= le32_to_cpu(ns_list
[j
]);
1535 nvme_validate_ns(ctrl
, nsid
);
1537 while (++prev
< nsid
) {
1538 ns
= nvme_find_ns(ctrl
, prev
);
1550 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
1552 struct nvme_ns
*ns
, *next
;
1555 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1557 for (i
= 1; i
<= nn
; i
++)
1558 nvme_validate_ns(ctrl
, i
);
1560 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
1566 static void nvme_scan_work(struct work_struct
*work
)
1568 struct nvme_ctrl
*ctrl
=
1569 container_of(work
, struct nvme_ctrl
, scan_work
);
1570 struct nvme_id_ctrl
*id
;
1573 if (ctrl
->state
!= NVME_CTRL_LIVE
)
1576 if (nvme_identify_ctrl(ctrl
, &id
))
1579 mutex_lock(&ctrl
->namespaces_mutex
);
1580 nn
= le32_to_cpu(id
->nn
);
1581 if (ctrl
->vs
>= NVME_VS(1, 1) &&
1582 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
1583 if (!nvme_scan_ns_list(ctrl
, nn
))
1586 nvme_scan_ns_sequential(ctrl
, nn
);
1588 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
1589 mutex_unlock(&ctrl
->namespaces_mutex
);
1592 if (ctrl
->ops
->post_scan
)
1593 ctrl
->ops
->post_scan(ctrl
);
1596 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
1599 * Do not queue new scan work when a controller is reset during
1602 if (ctrl
->state
== NVME_CTRL_LIVE
)
1603 schedule_work(&ctrl
->scan_work
);
1605 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
1607 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
1609 struct nvme_ns
*ns
, *next
;
1612 * The dead states indicates the controller was not gracefully
1613 * disconnected. In that case, we won't be able to flush any data while
1614 * removing the namespaces' disks; fail all the queues now to avoid
1615 * potentially having to clean up the failed sync later.
1617 if (ctrl
->state
== NVME_CTRL_DEAD
)
1618 nvme_kill_queues(ctrl
);
1620 mutex_lock(&ctrl
->namespaces_mutex
);
1621 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
1623 mutex_unlock(&ctrl
->namespaces_mutex
);
1625 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
1627 static void nvme_async_event_work(struct work_struct
*work
)
1629 struct nvme_ctrl
*ctrl
=
1630 container_of(work
, struct nvme_ctrl
, async_event_work
);
1632 spin_lock_irq(&ctrl
->lock
);
1633 while (ctrl
->event_limit
> 0) {
1634 int aer_idx
= --ctrl
->event_limit
;
1636 spin_unlock_irq(&ctrl
->lock
);
1637 ctrl
->ops
->submit_async_event(ctrl
, aer_idx
);
1638 spin_lock_irq(&ctrl
->lock
);
1640 spin_unlock_irq(&ctrl
->lock
);
1643 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
,
1644 struct nvme_completion
*cqe
)
1646 u16 status
= le16_to_cpu(cqe
->status
) >> 1;
1647 u32 result
= le32_to_cpu(cqe
->result
);
1649 if (status
== NVME_SC_SUCCESS
|| status
== NVME_SC_ABORT_REQ
) {
1650 ++ctrl
->event_limit
;
1651 schedule_work(&ctrl
->async_event_work
);
1654 if (status
!= NVME_SC_SUCCESS
)
1657 switch (result
& 0xff07) {
1658 case NVME_AER_NOTICE_NS_CHANGED
:
1659 dev_info(ctrl
->device
, "rescanning\n");
1660 nvme_queue_scan(ctrl
);
1663 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
1666 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
1668 void nvme_queue_async_events(struct nvme_ctrl
*ctrl
)
1670 ctrl
->event_limit
= NVME_NR_AERS
;
1671 schedule_work(&ctrl
->async_event_work
);
1673 EXPORT_SYMBOL_GPL(nvme_queue_async_events
);
1675 static DEFINE_IDA(nvme_instance_ida
);
1677 static int nvme_set_instance(struct nvme_ctrl
*ctrl
)
1679 int instance
, error
;
1682 if (!ida_pre_get(&nvme_instance_ida
, GFP_KERNEL
))
1685 spin_lock(&dev_list_lock
);
1686 error
= ida_get_new(&nvme_instance_ida
, &instance
);
1687 spin_unlock(&dev_list_lock
);
1688 } while (error
== -EAGAIN
);
1693 ctrl
->instance
= instance
;
1697 static void nvme_release_instance(struct nvme_ctrl
*ctrl
)
1699 spin_lock(&dev_list_lock
);
1700 ida_remove(&nvme_instance_ida
, ctrl
->instance
);
1701 spin_unlock(&dev_list_lock
);
1704 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
1706 flush_work(&ctrl
->async_event_work
);
1707 flush_work(&ctrl
->scan_work
);
1708 nvme_remove_namespaces(ctrl
);
1710 device_destroy(nvme_class
, MKDEV(nvme_char_major
, ctrl
->instance
));
1712 spin_lock(&dev_list_lock
);
1713 list_del(&ctrl
->node
);
1714 spin_unlock(&dev_list_lock
);
1716 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
1718 static void nvme_free_ctrl(struct kref
*kref
)
1720 struct nvme_ctrl
*ctrl
= container_of(kref
, struct nvme_ctrl
, kref
);
1722 put_device(ctrl
->device
);
1723 nvme_release_instance(ctrl
);
1724 ida_destroy(&ctrl
->ns_ida
);
1726 ctrl
->ops
->free_ctrl(ctrl
);
1729 void nvme_put_ctrl(struct nvme_ctrl
*ctrl
)
1731 kref_put(&ctrl
->kref
, nvme_free_ctrl
);
1733 EXPORT_SYMBOL_GPL(nvme_put_ctrl
);
1736 * Initialize a NVMe controller structures. This needs to be called during
1737 * earliest initialization so that we have the initialized structured around
1740 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
1741 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
1745 ctrl
->state
= NVME_CTRL_NEW
;
1746 spin_lock_init(&ctrl
->lock
);
1747 INIT_LIST_HEAD(&ctrl
->namespaces
);
1748 mutex_init(&ctrl
->namespaces_mutex
);
1749 kref_init(&ctrl
->kref
);
1752 ctrl
->quirks
= quirks
;
1753 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
1754 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
1756 ret
= nvme_set_instance(ctrl
);
1760 ctrl
->device
= device_create_with_groups(nvme_class
, ctrl
->dev
,
1761 MKDEV(nvme_char_major
, ctrl
->instance
),
1762 ctrl
, nvme_dev_attr_groups
,
1763 "nvme%d", ctrl
->instance
);
1764 if (IS_ERR(ctrl
->device
)) {
1765 ret
= PTR_ERR(ctrl
->device
);
1766 goto out_release_instance
;
1768 get_device(ctrl
->device
);
1769 ida_init(&ctrl
->ns_ida
);
1771 spin_lock(&dev_list_lock
);
1772 list_add_tail(&ctrl
->node
, &nvme_ctrl_list
);
1773 spin_unlock(&dev_list_lock
);
1776 out_release_instance
:
1777 nvme_release_instance(ctrl
);
1781 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
1784 * nvme_kill_queues(): Ends all namespace queues
1785 * @ctrl: the dead controller that needs to end
1787 * Call this function when the driver determines it is unable to get the
1788 * controller in a state capable of servicing IO.
1790 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
1795 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1796 if (!kref_get_unless_zero(&ns
->kref
))
1800 * Revalidating a dead namespace sets capacity to 0. This will
1801 * end buffered writers dirtying pages that can't be synced.
1803 if (!test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
1804 revalidate_disk(ns
->disk
);
1806 blk_set_queue_dying(ns
->queue
);
1807 blk_mq_abort_requeue_list(ns
->queue
);
1808 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
1814 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
1816 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
1821 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1822 spin_lock_irq(ns
->queue
->queue_lock
);
1823 queue_flag_set(QUEUE_FLAG_STOPPED
, ns
->queue
);
1824 spin_unlock_irq(ns
->queue
->queue_lock
);
1826 blk_mq_cancel_requeue_work(ns
->queue
);
1827 blk_mq_stop_hw_queues(ns
->queue
);
1831 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
1833 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
1838 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1839 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED
, ns
->queue
);
1840 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
1841 blk_mq_kick_requeue_list(ns
->queue
);
1845 EXPORT_SYMBOL_GPL(nvme_start_queues
);
1847 int __init
nvme_core_init(void)
1851 result
= register_blkdev(nvme_major
, "nvme");
1854 else if (result
> 0)
1855 nvme_major
= result
;
1857 result
= __register_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme",
1860 goto unregister_blkdev
;
1861 else if (result
> 0)
1862 nvme_char_major
= result
;
1864 nvme_class
= class_create(THIS_MODULE
, "nvme");
1865 if (IS_ERR(nvme_class
)) {
1866 result
= PTR_ERR(nvme_class
);
1867 goto unregister_chrdev
;
1873 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
1875 unregister_blkdev(nvme_major
, "nvme");
1879 void nvme_core_exit(void)
1881 class_destroy(nvme_class
);
1882 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
1883 unregister_blkdev(nvme_major
, "nvme");
1886 MODULE_LICENSE("GPL");
1887 MODULE_VERSION("1.0");
1888 module_init(nvme_core_init
);
1889 module_exit(nvme_core_exit
);