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 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
65 if (!blk_mq_request_started(req
))
68 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
69 "Cancelling I/O %d", req
->tag
);
71 status
= NVME_SC_ABORT_REQ
;
72 if (blk_queue_dying(req
->q
))
73 status
|= NVME_SC_DNR
;
74 blk_mq_complete_request(req
, status
);
76 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
78 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
79 enum nvme_ctrl_state new_state
)
81 enum nvme_ctrl_state old_state
= ctrl
->state
;
84 spin_lock_irq(&ctrl
->lock
);
89 case NVME_CTRL_RESETTING
:
96 case NVME_CTRL_RESETTING
:
106 case NVME_CTRL_DELETING
:
109 case NVME_CTRL_RESETTING
:
118 case NVME_CTRL_DELETING
:
128 spin_unlock_irq(&ctrl
->lock
);
131 ctrl
->state
= new_state
;
135 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
137 static void nvme_free_ns(struct kref
*kref
)
139 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
141 if (ns
->type
== NVME_NS_LIGHTNVM
)
142 nvme_nvm_unregister(ns
->queue
, ns
->disk
->disk_name
);
144 spin_lock(&dev_list_lock
);
145 ns
->disk
->private_data
= NULL
;
146 spin_unlock(&dev_list_lock
);
149 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
150 nvme_put_ctrl(ns
->ctrl
);
154 static void nvme_put_ns(struct nvme_ns
*ns
)
156 kref_put(&ns
->kref
, nvme_free_ns
);
159 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
)
163 spin_lock(&dev_list_lock
);
164 ns
= disk
->private_data
;
166 if (!kref_get_unless_zero(&ns
->kref
))
168 if (!try_module_get(ns
->ctrl
->ops
->module
))
171 spin_unlock(&dev_list_lock
);
176 kref_put(&ns
->kref
, nvme_free_ns
);
178 spin_unlock(&dev_list_lock
);
182 void nvme_requeue_req(struct request
*req
)
186 blk_mq_requeue_request(req
);
187 spin_lock_irqsave(req
->q
->queue_lock
, flags
);
188 if (!blk_queue_stopped(req
->q
))
189 blk_mq_kick_requeue_list(req
->q
);
190 spin_unlock_irqrestore(req
->q
->queue_lock
, flags
);
192 EXPORT_SYMBOL_GPL(nvme_requeue_req
);
194 struct request
*nvme_alloc_request(struct request_queue
*q
,
195 struct nvme_command
*cmd
, unsigned int flags
, int qid
)
199 if (qid
== NVME_QID_ANY
) {
200 req
= blk_mq_alloc_request(q
, nvme_is_write(cmd
), flags
);
202 req
= blk_mq_alloc_request_hctx(q
, nvme_is_write(cmd
), flags
,
208 req
->cmd_type
= REQ_TYPE_DRV_PRIV
;
209 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
211 req
->__sector
= (sector_t
) -1;
212 req
->bio
= req
->biotail
= NULL
;
214 req
->cmd
= (unsigned char *)cmd
;
215 req
->cmd_len
= sizeof(struct nvme_command
);
219 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
221 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
222 struct nvme_command
*cmnd
)
224 memset(cmnd
, 0, sizeof(*cmnd
));
225 cmnd
->common
.opcode
= nvme_cmd_flush
;
226 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
229 static inline int nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
230 struct nvme_command
*cmnd
)
232 struct nvme_dsm_range
*range
;
235 unsigned int nr_bytes
= blk_rq_bytes(req
);
237 range
= kmalloc(sizeof(*range
), GFP_ATOMIC
);
239 return BLK_MQ_RQ_QUEUE_BUSY
;
241 range
->cattr
= cpu_to_le32(0);
242 range
->nlb
= cpu_to_le32(nr_bytes
>> ns
->lba_shift
);
243 range
->slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
245 memset(cmnd
, 0, sizeof(*cmnd
));
246 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
247 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
249 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
251 req
->completion_data
= range
;
252 page
= virt_to_page(range
);
253 offset
= offset_in_page(range
);
254 blk_add_request_payload(req
, page
, offset
, sizeof(*range
));
257 * we set __data_len back to the size of the area to be discarded
258 * on disk. This allows us to report completion on the full amount
259 * of blocks described by the request.
261 req
->__data_len
= nr_bytes
;
266 static inline void nvme_setup_rw(struct nvme_ns
*ns
, struct request
*req
,
267 struct nvme_command
*cmnd
)
272 if (req
->cmd_flags
& REQ_FUA
)
273 control
|= NVME_RW_FUA
;
274 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
275 control
|= NVME_RW_LR
;
277 if (req
->cmd_flags
& REQ_RAHEAD
)
278 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
280 memset(cmnd
, 0, sizeof(*cmnd
));
281 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
282 cmnd
->rw
.command_id
= req
->tag
;
283 cmnd
->rw
.nsid
= cpu_to_le32(ns
->ns_id
);
284 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
285 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
288 switch (ns
->pi_type
) {
289 case NVME_NS_DPS_PI_TYPE3
:
290 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
292 case NVME_NS_DPS_PI_TYPE1
:
293 case NVME_NS_DPS_PI_TYPE2
:
294 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
295 NVME_RW_PRINFO_PRCHK_REF
;
296 cmnd
->rw
.reftag
= cpu_to_le32(
297 nvme_block_nr(ns
, blk_rq_pos(req
)));
300 if (!blk_integrity_rq(req
))
301 control
|= NVME_RW_PRINFO_PRACT
;
304 cmnd
->rw
.control
= cpu_to_le16(control
);
305 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
308 int nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
309 struct nvme_command
*cmd
)
313 if (req
->cmd_type
== REQ_TYPE_DRV_PRIV
)
314 memcpy(cmd
, req
->cmd
, sizeof(*cmd
));
315 else if (req_op(req
) == REQ_OP_FLUSH
)
316 nvme_setup_flush(ns
, cmd
);
317 else if (req_op(req
) == REQ_OP_DISCARD
)
318 ret
= nvme_setup_discard(ns
, req
, cmd
);
320 nvme_setup_rw(ns
, req
, cmd
);
324 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
327 * Returns 0 on success. If the result is negative, it's a Linux error code;
328 * if the result is positive, it's an NVM Express status code
330 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
331 struct nvme_completion
*cqe
, void *buffer
, unsigned bufflen
,
332 unsigned timeout
, int qid
, int at_head
, int flags
)
337 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
341 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
344 if (buffer
&& bufflen
) {
345 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
350 blk_execute_rq(req
->q
, NULL
, req
, at_head
);
353 blk_mq_free_request(req
);
356 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
358 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
359 void *buffer
, unsigned bufflen
)
361 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
364 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
366 int __nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
367 void __user
*ubuffer
, unsigned bufflen
,
368 void __user
*meta_buffer
, unsigned meta_len
, u32 meta_seed
,
369 u32
*result
, unsigned timeout
)
371 bool write
= nvme_is_write(cmd
);
372 struct nvme_completion cqe
;
373 struct nvme_ns
*ns
= q
->queuedata
;
374 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
376 struct bio
*bio
= NULL
;
380 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
384 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
387 if (ubuffer
&& bufflen
) {
388 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
396 bio
->bi_bdev
= bdget_disk(disk
, 0);
402 if (meta_buffer
&& meta_len
) {
403 struct bio_integrity_payload
*bip
;
405 meta
= kmalloc(meta_len
, GFP_KERNEL
);
412 if (copy_from_user(meta
, meta_buffer
,
419 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
425 bip
->bip_iter
.bi_size
= meta_len
;
426 bip
->bip_iter
.bi_sector
= meta_seed
;
428 ret
= bio_integrity_add_page(bio
, virt_to_page(meta
),
429 meta_len
, offset_in_page(meta
));
430 if (ret
!= meta_len
) {
437 blk_execute_rq(req
->q
, disk
, req
, 0);
440 *result
= le32_to_cpu(cqe
.result
);
441 if (meta
&& !ret
&& !write
) {
442 if (copy_to_user(meta_buffer
, meta
, meta_len
))
449 if (disk
&& bio
->bi_bdev
)
451 blk_rq_unmap_user(bio
);
454 blk_mq_free_request(req
);
458 int nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
459 void __user
*ubuffer
, unsigned bufflen
, u32
*result
,
462 return __nvme_submit_user_cmd(q
, cmd
, ubuffer
, bufflen
, NULL
, 0, 0,
466 int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
468 struct nvme_command c
= { };
471 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
472 c
.identify
.opcode
= nvme_admin_identify
;
473 c
.identify
.cns
= cpu_to_le32(1);
475 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
479 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
480 sizeof(struct nvme_id_ctrl
));
486 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
488 struct nvme_command c
= { };
490 c
.identify
.opcode
= nvme_admin_identify
;
491 c
.identify
.cns
= cpu_to_le32(2);
492 c
.identify
.nsid
= cpu_to_le32(nsid
);
493 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
496 int nvme_identify_ns(struct nvme_ctrl
*dev
, unsigned nsid
,
497 struct nvme_id_ns
**id
)
499 struct nvme_command c
= { };
502 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
503 c
.identify
.opcode
= nvme_admin_identify
,
504 c
.identify
.nsid
= cpu_to_le32(nsid
),
506 *id
= kmalloc(sizeof(struct nvme_id_ns
), GFP_KERNEL
);
510 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
511 sizeof(struct nvme_id_ns
));
517 int nvme_get_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned nsid
,
518 dma_addr_t dma_addr
, u32
*result
)
520 struct nvme_command c
;
521 struct nvme_completion cqe
;
524 memset(&c
, 0, sizeof(c
));
525 c
.features
.opcode
= nvme_admin_get_features
;
526 c
.features
.nsid
= cpu_to_le32(nsid
);
527 c
.features
.prp1
= cpu_to_le64(dma_addr
);
528 c
.features
.fid
= cpu_to_le32(fid
);
530 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &cqe
, NULL
, 0, 0,
533 *result
= le32_to_cpu(cqe
.result
);
537 int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
538 dma_addr_t dma_addr
, u32
*result
)
540 struct nvme_command c
;
541 struct nvme_completion cqe
;
544 memset(&c
, 0, sizeof(c
));
545 c
.features
.opcode
= nvme_admin_set_features
;
546 c
.features
.prp1
= cpu_to_le64(dma_addr
);
547 c
.features
.fid
= cpu_to_le32(fid
);
548 c
.features
.dword11
= cpu_to_le32(dword11
);
550 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &cqe
, NULL
, 0, 0,
553 *result
= le32_to_cpu(cqe
.result
);
557 int nvme_get_log_page(struct nvme_ctrl
*dev
, struct nvme_smart_log
**log
)
559 struct nvme_command c
= { };
562 c
.common
.opcode
= nvme_admin_get_log_page
,
563 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF),
564 c
.common
.cdw10
[0] = cpu_to_le32(
565 (((sizeof(struct nvme_smart_log
) / 4) - 1) << 16) |
568 *log
= kmalloc(sizeof(struct nvme_smart_log
), GFP_KERNEL
);
572 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *log
,
573 sizeof(struct nvme_smart_log
));
579 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
581 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
583 int status
, nr_io_queues
;
585 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, 0,
591 * Degraded controllers might return an error when setting the queue
592 * count. We still want to be able to bring them online and offer
593 * access to the admin queue, as that might be only way to fix them up.
596 dev_err(ctrl
->dev
, "Could not set queue count (%d)\n", status
);
599 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
600 *count
= min(*count
, nr_io_queues
);
605 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
607 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
609 struct nvme_user_io io
;
610 struct nvme_command c
;
611 unsigned length
, meta_len
;
612 void __user
*metadata
;
614 if (copy_from_user(&io
, uio
, sizeof(io
)))
622 case nvme_cmd_compare
:
628 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
629 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
630 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
635 } else if (meta_len
) {
636 if ((io
.metadata
& 3) || !io
.metadata
)
640 memset(&c
, 0, sizeof(c
));
641 c
.rw
.opcode
= io
.opcode
;
642 c
.rw
.flags
= io
.flags
;
643 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
644 c
.rw
.slba
= cpu_to_le64(io
.slba
);
645 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
646 c
.rw
.control
= cpu_to_le16(io
.control
);
647 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
648 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
649 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
650 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
652 return __nvme_submit_user_cmd(ns
->queue
, &c
,
653 (void __user
*)(uintptr_t)io
.addr
, length
,
654 metadata
, meta_len
, io
.slba
, NULL
, 0);
657 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
658 struct nvme_passthru_cmd __user
*ucmd
)
660 struct nvme_passthru_cmd cmd
;
661 struct nvme_command c
;
662 unsigned timeout
= 0;
665 if (!capable(CAP_SYS_ADMIN
))
667 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
672 memset(&c
, 0, sizeof(c
));
673 c
.common
.opcode
= cmd
.opcode
;
674 c
.common
.flags
= cmd
.flags
;
675 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
676 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
677 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
678 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
679 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
680 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
681 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
682 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
683 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
686 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
688 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
689 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
690 &cmd
.result
, timeout
);
692 if (put_user(cmd
.result
, &ucmd
->result
))
699 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
700 unsigned int cmd
, unsigned long arg
)
702 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
706 force_successful_syscall_return();
708 case NVME_IOCTL_ADMIN_CMD
:
709 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
710 case NVME_IOCTL_IO_CMD
:
711 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
712 case NVME_IOCTL_SUBMIT_IO
:
713 return nvme_submit_io(ns
, (void __user
*)arg
);
714 #ifdef CONFIG_BLK_DEV_NVME_SCSI
715 case SG_GET_VERSION_NUM
:
716 return nvme_sg_get_version_num((void __user
*)arg
);
718 return nvme_sg_io(ns
, (void __user
*)arg
);
726 static int nvme_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
727 unsigned int cmd
, unsigned long arg
)
733 return nvme_ioctl(bdev
, mode
, cmd
, arg
);
736 #define nvme_compat_ioctl NULL
739 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
741 return nvme_get_ns_from_disk(bdev
->bd_disk
) ? 0 : -ENXIO
;
744 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
746 struct nvme_ns
*ns
= disk
->private_data
;
748 module_put(ns
->ctrl
->ops
->module
);
752 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
754 /* some standard values */
756 geo
->sectors
= 1 << 5;
757 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
761 #ifdef CONFIG_BLK_DEV_INTEGRITY
762 static void nvme_init_integrity(struct nvme_ns
*ns
)
764 struct blk_integrity integrity
;
766 switch (ns
->pi_type
) {
767 case NVME_NS_DPS_PI_TYPE3
:
768 integrity
.profile
= &t10_pi_type3_crc
;
769 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
770 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
772 case NVME_NS_DPS_PI_TYPE1
:
773 case NVME_NS_DPS_PI_TYPE2
:
774 integrity
.profile
= &t10_pi_type1_crc
;
775 integrity
.tag_size
= sizeof(u16
);
776 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
779 integrity
.profile
= NULL
;
782 integrity
.tuple_size
= ns
->ms
;
783 blk_integrity_register(ns
->disk
, &integrity
);
784 blk_queue_max_integrity_segments(ns
->queue
, 1);
787 static void nvme_init_integrity(struct nvme_ns
*ns
)
790 #endif /* CONFIG_BLK_DEV_INTEGRITY */
792 static void nvme_config_discard(struct nvme_ns
*ns
)
794 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
795 u32 logical_block_size
= queue_logical_block_size(ns
->queue
);
797 if (ctrl
->quirks
& NVME_QUIRK_DISCARD_ZEROES
)
798 ns
->queue
->limits
.discard_zeroes_data
= 1;
800 ns
->queue
->limits
.discard_zeroes_data
= 0;
802 ns
->queue
->limits
.discard_alignment
= logical_block_size
;
803 ns
->queue
->limits
.discard_granularity
= logical_block_size
;
804 blk_queue_max_discard_sectors(ns
->queue
, UINT_MAX
);
805 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, ns
->queue
);
808 static int nvme_revalidate_disk(struct gendisk
*disk
)
810 struct nvme_ns
*ns
= disk
->private_data
;
811 struct nvme_id_ns
*id
;
816 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
817 set_capacity(disk
, 0);
820 if (nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id
)) {
821 dev_warn(disk_to_dev(ns
->disk
), "%s: Identify failure\n",
830 if (nvme_nvm_ns_supported(ns
, id
) && ns
->type
!= NVME_NS_LIGHTNVM
) {
831 if (nvme_nvm_register(ns
->queue
, disk
->disk_name
)) {
832 dev_warn(disk_to_dev(ns
->disk
),
833 "%s: LightNVM init failure\n", __func__
);
837 ns
->type
= NVME_NS_LIGHTNVM
;
840 if (ns
->ctrl
->vs
>= NVME_VS(1, 1))
841 memcpy(ns
->eui
, id
->eui64
, sizeof(ns
->eui
));
842 if (ns
->ctrl
->vs
>= NVME_VS(1, 2))
843 memcpy(ns
->uuid
, id
->nguid
, sizeof(ns
->uuid
));
846 lbaf
= id
->flbas
& NVME_NS_FLBAS_LBA_MASK
;
847 ns
->lba_shift
= id
->lbaf
[lbaf
].ds
;
848 ns
->ms
= le16_to_cpu(id
->lbaf
[lbaf
].ms
);
849 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
852 * If identify namespace failed, use default 512 byte block size so
853 * block layer can use before failing read/write for 0 capacity.
855 if (ns
->lba_shift
== 0)
857 bs
= 1 << ns
->lba_shift
;
858 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
859 pi_type
= ns
->ms
== sizeof(struct t10_pi_tuple
) ?
860 id
->dps
& NVME_NS_DPS_PI_MASK
: 0;
862 blk_mq_freeze_queue(disk
->queue
);
863 if (blk_get_integrity(disk
) && (ns
->pi_type
!= pi_type
||
865 bs
!= queue_logical_block_size(disk
->queue
) ||
866 (ns
->ms
&& ns
->ext
)))
867 blk_integrity_unregister(disk
);
869 ns
->pi_type
= pi_type
;
870 blk_queue_logical_block_size(ns
->queue
, bs
);
872 if (ns
->ms
&& !blk_get_integrity(disk
) && !ns
->ext
)
873 nvme_init_integrity(ns
);
874 if (ns
->ms
&& !(ns
->ms
== 8 && ns
->pi_type
) && !blk_get_integrity(disk
))
875 set_capacity(disk
, 0);
877 set_capacity(disk
, le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9));
879 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
880 nvme_config_discard(ns
);
881 blk_mq_unfreeze_queue(disk
->queue
);
887 static char nvme_pr_type(enum pr_type type
)
890 case PR_WRITE_EXCLUSIVE
:
892 case PR_EXCLUSIVE_ACCESS
:
894 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
896 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
898 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
900 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
907 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
908 u64 key
, u64 sa_key
, u8 op
)
910 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
911 struct nvme_command c
;
912 u8 data
[16] = { 0, };
914 put_unaligned_le64(key
, &data
[0]);
915 put_unaligned_le64(sa_key
, &data
[8]);
917 memset(&c
, 0, sizeof(c
));
918 c
.common
.opcode
= op
;
919 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
920 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
922 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
925 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
926 u64
new, unsigned flags
)
930 if (flags
& ~PR_FL_IGNORE_KEY
)
934 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
935 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
936 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
939 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
940 enum pr_type type
, unsigned flags
)
944 if (flags
& ~PR_FL_IGNORE_KEY
)
947 cdw10
= nvme_pr_type(type
) << 8;
948 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
949 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
952 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
953 enum pr_type type
, bool abort
)
955 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
956 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
959 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
961 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
962 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
965 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
967 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
968 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
971 static const struct pr_ops nvme_pr_ops
= {
972 .pr_register
= nvme_pr_register
,
973 .pr_reserve
= nvme_pr_reserve
,
974 .pr_release
= nvme_pr_release
,
975 .pr_preempt
= nvme_pr_preempt
,
976 .pr_clear
= nvme_pr_clear
,
979 static const struct block_device_operations nvme_fops
= {
980 .owner
= THIS_MODULE
,
982 .compat_ioctl
= nvme_compat_ioctl
,
984 .release
= nvme_release
,
985 .getgeo
= nvme_getgeo
,
986 .revalidate_disk
= nvme_revalidate_disk
,
987 .pr_ops
= &nvme_pr_ops
,
990 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
992 unsigned long timeout
=
993 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
994 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
997 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
998 if ((csts
& NVME_CSTS_RDY
) == bit
)
1002 if (fatal_signal_pending(current
))
1004 if (time_after(jiffies
, timeout
)) {
1005 dev_err(ctrl
->device
,
1006 "Device not ready; aborting %s\n", enabled
?
1007 "initialisation" : "reset");
1016 * If the device has been passed off to us in an enabled state, just clear
1017 * the enabled bit. The spec says we should set the 'shutdown notification
1018 * bits', but doing so may cause the device to complete commands to the
1019 * admin queue ... and we don't know what memory that might be pointing at!
1021 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1025 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1026 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1028 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1031 return nvme_wait_ready(ctrl
, cap
, false);
1033 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1035 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1038 * Default to a 4K page size, with the intention to update this
1039 * path in the future to accomodate architectures with differing
1040 * kernel and IO page sizes.
1042 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1045 if (page_shift
< dev_page_min
) {
1046 dev_err(ctrl
->device
,
1047 "Minimum device page size %u too large for host (%u)\n",
1048 1 << dev_page_min
, 1 << page_shift
);
1052 ctrl
->page_size
= 1 << page_shift
;
1054 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1055 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1056 ctrl
->ctrl_config
|= NVME_CC_ARB_RR
| NVME_CC_SHN_NONE
;
1057 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1058 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1060 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1063 return nvme_wait_ready(ctrl
, cap
, true);
1065 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1067 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1069 unsigned long timeout
= SHUTDOWN_TIMEOUT
+ jiffies
;
1073 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1074 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1076 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1080 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1081 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1085 if (fatal_signal_pending(current
))
1087 if (time_after(jiffies
, timeout
)) {
1088 dev_err(ctrl
->device
,
1089 "Device shutdown incomplete; abort shutdown\n");
1096 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1098 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1099 struct request_queue
*q
)
1103 if (ctrl
->max_hw_sectors
) {
1105 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1107 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1108 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1110 if (ctrl
->stripe_size
)
1111 blk_queue_chunk_sectors(q
, ctrl
->stripe_size
>> 9);
1112 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1113 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1115 blk_queue_write_cache(q
, vwc
, vwc
);
1119 * Initialize the cached copies of the Identify data and various controller
1120 * register in our nvme_ctrl structure. This should be called as soon as
1121 * the admin queue is fully up and running.
1123 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1125 struct nvme_id_ctrl
*id
;
1127 int ret
, page_shift
;
1130 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1132 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1136 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1138 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1141 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1143 if (ctrl
->vs
>= NVME_VS(1, 1))
1144 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1146 ret
= nvme_identify_ctrl(ctrl
, &id
);
1148 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1152 ctrl
->vid
= le16_to_cpu(id
->vid
);
1153 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1154 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1155 ctrl
->vwc
= id
->vwc
;
1156 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1157 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1158 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1159 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1161 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1163 max_hw_sectors
= UINT_MAX
;
1164 ctrl
->max_hw_sectors
=
1165 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
1167 if ((ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
) && id
->vs
[3]) {
1168 unsigned int max_hw_sectors
;
1170 ctrl
->stripe_size
= 1 << (id
->vs
[3] + page_shift
);
1171 max_hw_sectors
= ctrl
->stripe_size
>> (page_shift
- 9);
1172 if (ctrl
->max_hw_sectors
) {
1173 ctrl
->max_hw_sectors
= min(max_hw_sectors
,
1174 ctrl
->max_hw_sectors
);
1176 ctrl
->max_hw_sectors
= max_hw_sectors
;
1180 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1185 EXPORT_SYMBOL_GPL(nvme_init_identify
);
1187 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
1189 struct nvme_ctrl
*ctrl
;
1190 int instance
= iminor(inode
);
1193 spin_lock(&dev_list_lock
);
1194 list_for_each_entry(ctrl
, &nvme_ctrl_list
, node
) {
1195 if (ctrl
->instance
!= instance
)
1198 if (!ctrl
->admin_q
) {
1202 if (!kref_get_unless_zero(&ctrl
->kref
))
1204 file
->private_data
= ctrl
;
1208 spin_unlock(&dev_list_lock
);
1213 static int nvme_dev_release(struct inode
*inode
, struct file
*file
)
1215 nvme_put_ctrl(file
->private_data
);
1219 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
1224 mutex_lock(&ctrl
->namespaces_mutex
);
1225 if (list_empty(&ctrl
->namespaces
)) {
1230 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
1231 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
1232 dev_warn(ctrl
->device
,
1233 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1238 dev_warn(ctrl
->device
,
1239 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1240 kref_get(&ns
->kref
);
1241 mutex_unlock(&ctrl
->namespaces_mutex
);
1243 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
1248 mutex_unlock(&ctrl
->namespaces_mutex
);
1252 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
1255 struct nvme_ctrl
*ctrl
= file
->private_data
;
1256 void __user
*argp
= (void __user
*)arg
;
1259 case NVME_IOCTL_ADMIN_CMD
:
1260 return nvme_user_cmd(ctrl
, NULL
, argp
);
1261 case NVME_IOCTL_IO_CMD
:
1262 return nvme_dev_user_cmd(ctrl
, argp
);
1263 case NVME_IOCTL_RESET
:
1264 dev_warn(ctrl
->device
, "resetting controller\n");
1265 return ctrl
->ops
->reset_ctrl(ctrl
);
1266 case NVME_IOCTL_SUBSYS_RESET
:
1267 return nvme_reset_subsystem(ctrl
);
1268 case NVME_IOCTL_RESCAN
:
1269 nvme_queue_scan(ctrl
);
1276 static const struct file_operations nvme_dev_fops
= {
1277 .owner
= THIS_MODULE
,
1278 .open
= nvme_dev_open
,
1279 .release
= nvme_dev_release
,
1280 .unlocked_ioctl
= nvme_dev_ioctl
,
1281 .compat_ioctl
= nvme_dev_ioctl
,
1284 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
1285 struct device_attribute
*attr
, const char *buf
,
1288 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1291 ret
= ctrl
->ops
->reset_ctrl(ctrl
);
1296 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
1298 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
1299 struct device_attribute
*attr
, const char *buf
,
1302 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1304 nvme_queue_scan(ctrl
);
1307 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
1309 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
1312 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1313 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1314 int serial_len
= sizeof(ctrl
->serial
);
1315 int model_len
= sizeof(ctrl
->model
);
1317 if (memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1318 return sprintf(buf
, "eui.%16phN\n", ns
->uuid
);
1320 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1321 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
1323 while (ctrl
->serial
[serial_len
- 1] == ' ')
1325 while (ctrl
->model
[model_len
- 1] == ' ')
1328 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
1329 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
1331 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
1333 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
1336 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1337 return sprintf(buf
, "%pU\n", ns
->uuid
);
1339 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
1341 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
1344 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1345 return sprintf(buf
, "%8phd\n", ns
->eui
);
1347 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
1349 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
1352 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1353 return sprintf(buf
, "%d\n", ns
->ns_id
);
1355 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
1357 static struct attribute
*nvme_ns_attrs
[] = {
1358 &dev_attr_wwid
.attr
,
1359 &dev_attr_uuid
.attr
,
1361 &dev_attr_nsid
.attr
,
1365 static umode_t
nvme_ns_attrs_are_visible(struct kobject
*kobj
,
1366 struct attribute
*a
, int n
)
1368 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1369 struct nvme_ns
*ns
= dev_to_disk(dev
)->private_data
;
1371 if (a
== &dev_attr_uuid
.attr
) {
1372 if (!memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1375 if (a
== &dev_attr_eui
.attr
) {
1376 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1382 static const struct attribute_group nvme_ns_attr_group
= {
1383 .attrs
= nvme_ns_attrs
,
1384 .is_visible
= nvme_ns_attrs_are_visible
,
1387 #define nvme_show_str_function(field) \
1388 static ssize_t field##_show(struct device *dev, \
1389 struct device_attribute *attr, char *buf) \
1391 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1392 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1394 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1396 #define nvme_show_int_function(field) \
1397 static ssize_t field##_show(struct device *dev, \
1398 struct device_attribute *attr, char *buf) \
1400 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1401 return sprintf(buf, "%d\n", ctrl->field); \
1403 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1405 nvme_show_str_function(model
);
1406 nvme_show_str_function(serial
);
1407 nvme_show_str_function(firmware_rev
);
1408 nvme_show_int_function(cntlid
);
1410 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
1411 struct device_attribute
*attr
, const char *buf
,
1414 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1416 if (device_remove_file_self(dev
, attr
))
1417 ctrl
->ops
->delete_ctrl(ctrl
);
1420 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
1422 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
1423 struct device_attribute
*attr
,
1426 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1428 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
1430 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
1432 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
1433 struct device_attribute
*attr
,
1436 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1438 return snprintf(buf
, PAGE_SIZE
, "%s\n",
1439 ctrl
->ops
->get_subsysnqn(ctrl
));
1441 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
1443 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
1444 struct device_attribute
*attr
,
1447 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1449 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
1451 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
1453 static struct attribute
*nvme_dev_attrs
[] = {
1454 &dev_attr_reset_controller
.attr
,
1455 &dev_attr_rescan_controller
.attr
,
1456 &dev_attr_model
.attr
,
1457 &dev_attr_serial
.attr
,
1458 &dev_attr_firmware_rev
.attr
,
1459 &dev_attr_cntlid
.attr
,
1460 &dev_attr_delete_controller
.attr
,
1461 &dev_attr_transport
.attr
,
1462 &dev_attr_subsysnqn
.attr
,
1463 &dev_attr_address
.attr
,
1467 #define CHECK_ATTR(ctrl, a, name) \
1468 if ((a) == &dev_attr_##name.attr && \
1469 !(ctrl)->ops->get_##name) \
1472 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
1473 struct attribute
*a
, int n
)
1475 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1476 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1478 if (a
== &dev_attr_delete_controller
.attr
) {
1479 if (!ctrl
->ops
->delete_ctrl
)
1483 CHECK_ATTR(ctrl
, a
, subsysnqn
);
1484 CHECK_ATTR(ctrl
, a
, address
);
1489 static struct attribute_group nvme_dev_attrs_group
= {
1490 .attrs
= nvme_dev_attrs
,
1491 .is_visible
= nvme_dev_attrs_are_visible
,
1494 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
1495 &nvme_dev_attrs_group
,
1499 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1501 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
1502 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
1504 return nsa
->ns_id
- nsb
->ns_id
;
1507 static struct nvme_ns
*nvme_find_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1511 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1513 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
1514 if (ns
->ns_id
== nsid
)
1516 if (ns
->ns_id
> nsid
)
1522 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1525 struct gendisk
*disk
;
1526 int node
= dev_to_node(ctrl
->dev
);
1528 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1530 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
1534 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
1535 if (ns
->instance
< 0)
1538 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
1539 if (IS_ERR(ns
->queue
))
1540 goto out_release_instance
;
1541 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
1542 ns
->queue
->queuedata
= ns
;
1545 disk
= alloc_disk_node(0, node
);
1547 goto out_free_queue
;
1549 kref_init(&ns
->kref
);
1552 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
1555 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
1556 nvme_set_queue_limits(ctrl
, ns
->queue
);
1558 disk
->major
= nvme_major
;
1559 disk
->first_minor
= 0;
1560 disk
->fops
= &nvme_fops
;
1561 disk
->private_data
= ns
;
1562 disk
->queue
= ns
->queue
;
1563 disk
->driverfs_dev
= ctrl
->device
;
1564 disk
->flags
= GENHD_FL_EXT_DEVT
;
1565 sprintf(disk
->disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
1567 if (nvme_revalidate_disk(ns
->disk
))
1570 list_add_tail_rcu(&ns
->list
, &ctrl
->namespaces
);
1571 kref_get(&ctrl
->kref
);
1572 if (ns
->type
== NVME_NS_LIGHTNVM
)
1576 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
1577 &nvme_ns_attr_group
))
1578 pr_warn("%s: failed to create sysfs group for identification\n",
1579 ns
->disk
->disk_name
);
1584 blk_cleanup_queue(ns
->queue
);
1585 out_release_instance
:
1586 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
1591 static void nvme_ns_remove(struct nvme_ns
*ns
)
1593 lockdep_assert_held(&ns
->ctrl
->namespaces_mutex
);
1595 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
1598 if (ns
->disk
->flags
& GENHD_FL_UP
) {
1599 if (blk_get_integrity(ns
->disk
))
1600 blk_integrity_unregister(ns
->disk
);
1601 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
1602 &nvme_ns_attr_group
);
1603 del_gendisk(ns
->disk
);
1604 blk_mq_abort_requeue_list(ns
->queue
);
1605 blk_cleanup_queue(ns
->queue
);
1607 list_del_init(&ns
->list
);
1612 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1616 ns
= nvme_find_ns(ctrl
, nsid
);
1618 if (revalidate_disk(ns
->disk
))
1621 nvme_alloc_ns(ctrl
, nsid
);
1624 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
1627 struct nvme_ns
*ns
, *next
;
1629 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
1630 if (ns
->ns_id
> nsid
)
1635 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
1639 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
1642 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
1646 for (i
= 0; i
< num_lists
; i
++) {
1647 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
1651 for (j
= 0; j
< min(nn
, 1024U); j
++) {
1652 nsid
= le32_to_cpu(ns_list
[j
]);
1656 nvme_validate_ns(ctrl
, nsid
);
1658 while (++prev
< nsid
) {
1659 ns
= nvme_find_ns(ctrl
, prev
);
1667 nvme_remove_invalid_namespaces(ctrl
, prev
);
1673 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
1677 lockdep_assert_held(&ctrl
->namespaces_mutex
);
1679 for (i
= 1; i
<= nn
; i
++)
1680 nvme_validate_ns(ctrl
, i
);
1682 nvme_remove_invalid_namespaces(ctrl
, nn
);
1685 static void nvme_scan_work(struct work_struct
*work
)
1687 struct nvme_ctrl
*ctrl
=
1688 container_of(work
, struct nvme_ctrl
, scan_work
);
1689 struct nvme_id_ctrl
*id
;
1692 if (ctrl
->state
!= NVME_CTRL_LIVE
)
1695 if (nvme_identify_ctrl(ctrl
, &id
))
1698 mutex_lock(&ctrl
->namespaces_mutex
);
1699 nn
= le32_to_cpu(id
->nn
);
1700 if (ctrl
->vs
>= NVME_VS(1, 1) &&
1701 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
1702 if (!nvme_scan_ns_list(ctrl
, nn
))
1705 nvme_scan_ns_sequential(ctrl
, nn
);
1707 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
1708 mutex_unlock(&ctrl
->namespaces_mutex
);
1711 if (ctrl
->ops
->post_scan
)
1712 ctrl
->ops
->post_scan(ctrl
);
1715 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
1718 * Do not queue new scan work when a controller is reset during
1721 if (ctrl
->state
== NVME_CTRL_LIVE
)
1722 schedule_work(&ctrl
->scan_work
);
1724 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
1726 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
1728 struct nvme_ns
*ns
, *next
;
1731 * The dead states indicates the controller was not gracefully
1732 * disconnected. In that case, we won't be able to flush any data while
1733 * removing the namespaces' disks; fail all the queues now to avoid
1734 * potentially having to clean up the failed sync later.
1736 if (ctrl
->state
== NVME_CTRL_DEAD
)
1737 nvme_kill_queues(ctrl
);
1739 mutex_lock(&ctrl
->namespaces_mutex
);
1740 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
1742 mutex_unlock(&ctrl
->namespaces_mutex
);
1744 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
1746 static void nvme_async_event_work(struct work_struct
*work
)
1748 struct nvme_ctrl
*ctrl
=
1749 container_of(work
, struct nvme_ctrl
, async_event_work
);
1751 spin_lock_irq(&ctrl
->lock
);
1752 while (ctrl
->event_limit
> 0) {
1753 int aer_idx
= --ctrl
->event_limit
;
1755 spin_unlock_irq(&ctrl
->lock
);
1756 ctrl
->ops
->submit_async_event(ctrl
, aer_idx
);
1757 spin_lock_irq(&ctrl
->lock
);
1759 spin_unlock_irq(&ctrl
->lock
);
1762 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
,
1763 struct nvme_completion
*cqe
)
1765 u16 status
= le16_to_cpu(cqe
->status
) >> 1;
1766 u32 result
= le32_to_cpu(cqe
->result
);
1768 if (status
== NVME_SC_SUCCESS
|| status
== NVME_SC_ABORT_REQ
) {
1769 ++ctrl
->event_limit
;
1770 schedule_work(&ctrl
->async_event_work
);
1773 if (status
!= NVME_SC_SUCCESS
)
1776 switch (result
& 0xff07) {
1777 case NVME_AER_NOTICE_NS_CHANGED
:
1778 dev_info(ctrl
->device
, "rescanning\n");
1779 nvme_queue_scan(ctrl
);
1782 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
1785 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
1787 void nvme_queue_async_events(struct nvme_ctrl
*ctrl
)
1789 ctrl
->event_limit
= NVME_NR_AERS
;
1790 schedule_work(&ctrl
->async_event_work
);
1792 EXPORT_SYMBOL_GPL(nvme_queue_async_events
);
1794 static DEFINE_IDA(nvme_instance_ida
);
1796 static int nvme_set_instance(struct nvme_ctrl
*ctrl
)
1798 int instance
, error
;
1801 if (!ida_pre_get(&nvme_instance_ida
, GFP_KERNEL
))
1804 spin_lock(&dev_list_lock
);
1805 error
= ida_get_new(&nvme_instance_ida
, &instance
);
1806 spin_unlock(&dev_list_lock
);
1807 } while (error
== -EAGAIN
);
1812 ctrl
->instance
= instance
;
1816 static void nvme_release_instance(struct nvme_ctrl
*ctrl
)
1818 spin_lock(&dev_list_lock
);
1819 ida_remove(&nvme_instance_ida
, ctrl
->instance
);
1820 spin_unlock(&dev_list_lock
);
1823 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
1825 flush_work(&ctrl
->async_event_work
);
1826 flush_work(&ctrl
->scan_work
);
1827 nvme_remove_namespaces(ctrl
);
1829 device_destroy(nvme_class
, MKDEV(nvme_char_major
, ctrl
->instance
));
1831 spin_lock(&dev_list_lock
);
1832 list_del(&ctrl
->node
);
1833 spin_unlock(&dev_list_lock
);
1835 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
1837 static void nvme_free_ctrl(struct kref
*kref
)
1839 struct nvme_ctrl
*ctrl
= container_of(kref
, struct nvme_ctrl
, kref
);
1841 put_device(ctrl
->device
);
1842 nvme_release_instance(ctrl
);
1843 ida_destroy(&ctrl
->ns_ida
);
1845 ctrl
->ops
->free_ctrl(ctrl
);
1848 void nvme_put_ctrl(struct nvme_ctrl
*ctrl
)
1850 kref_put(&ctrl
->kref
, nvme_free_ctrl
);
1852 EXPORT_SYMBOL_GPL(nvme_put_ctrl
);
1855 * Initialize a NVMe controller structures. This needs to be called during
1856 * earliest initialization so that we have the initialized structured around
1859 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
1860 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
1864 ctrl
->state
= NVME_CTRL_NEW
;
1865 spin_lock_init(&ctrl
->lock
);
1866 INIT_LIST_HEAD(&ctrl
->namespaces
);
1867 mutex_init(&ctrl
->namespaces_mutex
);
1868 kref_init(&ctrl
->kref
);
1871 ctrl
->quirks
= quirks
;
1872 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
1873 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
1875 ret
= nvme_set_instance(ctrl
);
1879 ctrl
->device
= device_create_with_groups(nvme_class
, ctrl
->dev
,
1880 MKDEV(nvme_char_major
, ctrl
->instance
),
1881 ctrl
, nvme_dev_attr_groups
,
1882 "nvme%d", ctrl
->instance
);
1883 if (IS_ERR(ctrl
->device
)) {
1884 ret
= PTR_ERR(ctrl
->device
);
1885 goto out_release_instance
;
1887 get_device(ctrl
->device
);
1888 ida_init(&ctrl
->ns_ida
);
1890 spin_lock(&dev_list_lock
);
1891 list_add_tail(&ctrl
->node
, &nvme_ctrl_list
);
1892 spin_unlock(&dev_list_lock
);
1895 out_release_instance
:
1896 nvme_release_instance(ctrl
);
1900 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
1903 * nvme_kill_queues(): Ends all namespace queues
1904 * @ctrl: the dead controller that needs to end
1906 * Call this function when the driver determines it is unable to get the
1907 * controller in a state capable of servicing IO.
1909 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
1914 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1915 if (!kref_get_unless_zero(&ns
->kref
))
1919 * Revalidating a dead namespace sets capacity to 0. This will
1920 * end buffered writers dirtying pages that can't be synced.
1922 if (!test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
1923 revalidate_disk(ns
->disk
);
1925 blk_set_queue_dying(ns
->queue
);
1926 blk_mq_abort_requeue_list(ns
->queue
);
1927 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
1933 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
1935 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
1940 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1941 spin_lock_irq(ns
->queue
->queue_lock
);
1942 queue_flag_set(QUEUE_FLAG_STOPPED
, ns
->queue
);
1943 spin_unlock_irq(ns
->queue
->queue_lock
);
1945 blk_mq_cancel_requeue_work(ns
->queue
);
1946 blk_mq_stop_hw_queues(ns
->queue
);
1950 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
1952 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
1957 list_for_each_entry_rcu(ns
, &ctrl
->namespaces
, list
) {
1958 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED
, ns
->queue
);
1959 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
1960 blk_mq_kick_requeue_list(ns
->queue
);
1964 EXPORT_SYMBOL_GPL(nvme_start_queues
);
1966 int __init
nvme_core_init(void)
1970 result
= register_blkdev(nvme_major
, "nvme");
1973 else if (result
> 0)
1974 nvme_major
= result
;
1976 result
= __register_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme",
1979 goto unregister_blkdev
;
1980 else if (result
> 0)
1981 nvme_char_major
= result
;
1983 nvme_class
= class_create(THIS_MODULE
, "nvme");
1984 if (IS_ERR(nvme_class
)) {
1985 result
= PTR_ERR(nvme_class
);
1986 goto unregister_chrdev
;
1992 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
1994 unregister_blkdev(nvme_major
, "nvme");
1998 void nvme_core_exit(void)
2000 class_destroy(nvme_class
);
2001 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2002 unregister_blkdev(nvme_major
, "nvme");
2005 MODULE_LICENSE("GPL");
2006 MODULE_VERSION("1.0");
2007 module_init(nvme_core_init
);
2008 module_exit(nvme_core_exit
);