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>
35 #define NVME_MINORS (1U << MINORBITS)
37 unsigned char admin_timeout
= 60;
38 module_param(admin_timeout
, byte
, 0644);
39 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
40 EXPORT_SYMBOL_GPL(admin_timeout
);
42 unsigned char nvme_io_timeout
= 30;
43 module_param_named(io_timeout
, nvme_io_timeout
, byte
, 0644);
44 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
45 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
47 unsigned char shutdown_timeout
= 5;
48 module_param(shutdown_timeout
, byte
, 0644);
49 MODULE_PARM_DESC(shutdown_timeout
, "timeout in seconds for controller shutdown");
51 unsigned int nvme_max_retries
= 5;
52 module_param_named(max_retries
, nvme_max_retries
, uint
, 0644);
53 MODULE_PARM_DESC(max_retries
, "max number of retries a command may have");
54 EXPORT_SYMBOL_GPL(nvme_max_retries
);
56 static int nvme_char_major
;
57 module_param(nvme_char_major
, int, 0);
59 static LIST_HEAD(nvme_ctrl_list
);
60 static DEFINE_SPINLOCK(dev_list_lock
);
62 static struct class *nvme_class
;
64 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
68 if (!blk_mq_request_started(req
))
71 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
72 "Cancelling I/O %d", req
->tag
);
74 status
= NVME_SC_ABORT_REQ
;
75 if (blk_queue_dying(req
->q
))
76 status
|= NVME_SC_DNR
;
77 blk_mq_complete_request(req
, status
);
79 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
81 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
82 enum nvme_ctrl_state new_state
)
84 enum nvme_ctrl_state old_state
;
87 spin_lock_irq(&ctrl
->lock
);
89 old_state
= ctrl
->state
;
94 case NVME_CTRL_RESETTING
:
95 case NVME_CTRL_RECONNECTING
:
102 case NVME_CTRL_RESETTING
:
106 case NVME_CTRL_RECONNECTING
:
113 case NVME_CTRL_RECONNECTING
:
122 case NVME_CTRL_DELETING
:
125 case NVME_CTRL_RESETTING
:
126 case NVME_CTRL_RECONNECTING
:
135 case NVME_CTRL_DELETING
:
147 ctrl
->state
= new_state
;
149 spin_unlock_irq(&ctrl
->lock
);
153 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
155 static void nvme_free_ns(struct kref
*kref
)
157 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
160 nvme_nvm_unregister(ns
);
163 spin_lock(&dev_list_lock
);
164 ns
->disk
->private_data
= NULL
;
165 spin_unlock(&dev_list_lock
);
169 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
170 nvme_put_ctrl(ns
->ctrl
);
174 static void nvme_put_ns(struct nvme_ns
*ns
)
176 kref_put(&ns
->kref
, nvme_free_ns
);
179 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
)
183 spin_lock(&dev_list_lock
);
184 ns
= disk
->private_data
;
186 if (!kref_get_unless_zero(&ns
->kref
))
188 if (!try_module_get(ns
->ctrl
->ops
->module
))
191 spin_unlock(&dev_list_lock
);
196 kref_put(&ns
->kref
, nvme_free_ns
);
198 spin_unlock(&dev_list_lock
);
202 void nvme_requeue_req(struct request
*req
)
204 blk_mq_requeue_request(req
, !blk_mq_queue_stopped(req
->q
));
206 EXPORT_SYMBOL_GPL(nvme_requeue_req
);
208 struct request
*nvme_alloc_request(struct request_queue
*q
,
209 struct nvme_command
*cmd
, unsigned int flags
, int qid
)
213 if (qid
== NVME_QID_ANY
) {
214 req
= blk_mq_alloc_request(q
, nvme_is_write(cmd
), flags
);
216 req
= blk_mq_alloc_request_hctx(q
, nvme_is_write(cmd
), flags
,
222 req
->cmd_type
= REQ_TYPE_DRV_PRIV
;
223 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
224 nvme_req(req
)->cmd
= cmd
;
228 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
230 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
231 struct nvme_command
*cmnd
)
233 memset(cmnd
, 0, sizeof(*cmnd
));
234 cmnd
->common
.opcode
= nvme_cmd_flush
;
235 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
238 static inline int nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
239 struct nvme_command
*cmnd
)
241 struct nvme_dsm_range
*range
;
242 unsigned int nr_bytes
= blk_rq_bytes(req
);
244 range
= kmalloc(sizeof(*range
), GFP_ATOMIC
);
246 return BLK_MQ_RQ_QUEUE_BUSY
;
248 range
->cattr
= cpu_to_le32(0);
249 range
->nlb
= cpu_to_le32(nr_bytes
>> ns
->lba_shift
);
250 range
->slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
252 memset(cmnd
, 0, sizeof(*cmnd
));
253 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
254 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
256 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
258 req
->special_vec
.bv_page
= virt_to_page(range
);
259 req
->special_vec
.bv_offset
= offset_in_page(range
);
260 req
->special_vec
.bv_len
= sizeof(*range
);
261 req
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
263 return BLK_MQ_RQ_QUEUE_OK
;
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
.nsid
= cpu_to_le32(ns
->ns_id
);
283 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
284 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
287 switch (ns
->pi_type
) {
288 case NVME_NS_DPS_PI_TYPE3
:
289 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
291 case NVME_NS_DPS_PI_TYPE1
:
292 case NVME_NS_DPS_PI_TYPE2
:
293 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
294 NVME_RW_PRINFO_PRCHK_REF
;
295 cmnd
->rw
.reftag
= cpu_to_le32(
296 nvme_block_nr(ns
, blk_rq_pos(req
)));
299 if (!blk_integrity_rq(req
))
300 control
|= NVME_RW_PRINFO_PRACT
;
303 cmnd
->rw
.control
= cpu_to_le16(control
);
304 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
307 int nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
308 struct nvme_command
*cmd
)
310 int ret
= BLK_MQ_RQ_QUEUE_OK
;
312 if (req
->cmd_type
== REQ_TYPE_DRV_PRIV
)
313 memcpy(cmd
, nvme_req(req
)->cmd
, sizeof(*cmd
));
314 else if (req_op(req
) == REQ_OP_FLUSH
)
315 nvme_setup_flush(ns
, cmd
);
316 else if (req_op(req
) == REQ_OP_DISCARD
)
317 ret
= nvme_setup_discard(ns
, req
, cmd
);
319 nvme_setup_rw(ns
, req
, cmd
);
321 cmd
->common
.command_id
= req
->tag
;
325 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
328 * Returns 0 on success. If the result is negative, it's a Linux error code;
329 * if the result is positive, it's an NVM Express status code
331 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
332 union nvme_result
*result
, void *buffer
, unsigned bufflen
,
333 unsigned timeout
, int qid
, int at_head
, int flags
)
338 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
342 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
);
352 *result
= nvme_req(req
)->result
;
355 blk_mq_free_request(req
);
358 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
360 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
361 void *buffer
, unsigned bufflen
)
363 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
366 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
368 int __nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
369 void __user
*ubuffer
, unsigned bufflen
,
370 void __user
*meta_buffer
, unsigned meta_len
, u32 meta_seed
,
371 u32
*result
, unsigned timeout
)
373 bool write
= nvme_is_write(cmd
);
374 struct nvme_ns
*ns
= q
->queuedata
;
375 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
377 struct bio
*bio
= NULL
;
381 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
385 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(nvme_req(req
)->result
.u32
);
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 static void nvme_keep_alive_end_io(struct request
*rq
, int error
)
468 struct nvme_ctrl
*ctrl
= rq
->end_io_data
;
470 blk_mq_free_request(rq
);
473 dev_err(ctrl
->device
,
474 "failed nvme_keep_alive_end_io error=%d\n", error
);
478 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
481 static int nvme_keep_alive(struct nvme_ctrl
*ctrl
)
483 struct nvme_command c
;
486 memset(&c
, 0, sizeof(c
));
487 c
.common
.opcode
= nvme_admin_keep_alive
;
489 rq
= nvme_alloc_request(ctrl
->admin_q
, &c
, BLK_MQ_REQ_RESERVED
,
494 rq
->timeout
= ctrl
->kato
* HZ
;
495 rq
->end_io_data
= ctrl
;
497 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 0, nvme_keep_alive_end_io
);
502 static void nvme_keep_alive_work(struct work_struct
*work
)
504 struct nvme_ctrl
*ctrl
= container_of(to_delayed_work(work
),
505 struct nvme_ctrl
, ka_work
);
507 if (nvme_keep_alive(ctrl
)) {
508 /* allocation failure, reset the controller */
509 dev_err(ctrl
->device
, "keep-alive failed\n");
510 ctrl
->ops
->reset_ctrl(ctrl
);
515 void nvme_start_keep_alive(struct nvme_ctrl
*ctrl
)
517 if (unlikely(ctrl
->kato
== 0))
520 INIT_DELAYED_WORK(&ctrl
->ka_work
, nvme_keep_alive_work
);
521 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
523 EXPORT_SYMBOL_GPL(nvme_start_keep_alive
);
525 void nvme_stop_keep_alive(struct nvme_ctrl
*ctrl
)
527 if (unlikely(ctrl
->kato
== 0))
530 cancel_delayed_work_sync(&ctrl
->ka_work
);
532 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive
);
534 int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
536 struct nvme_command c
= { };
539 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
540 c
.identify
.opcode
= nvme_admin_identify
;
541 c
.identify
.cns
= cpu_to_le32(NVME_ID_CNS_CTRL
);
543 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
547 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
548 sizeof(struct nvme_id_ctrl
));
554 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
556 struct nvme_command c
= { };
558 c
.identify
.opcode
= nvme_admin_identify
;
559 c
.identify
.cns
= cpu_to_le32(NVME_ID_CNS_NS_ACTIVE_LIST
);
560 c
.identify
.nsid
= cpu_to_le32(nsid
);
561 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
564 int nvme_identify_ns(struct nvme_ctrl
*dev
, unsigned nsid
,
565 struct nvme_id_ns
**id
)
567 struct nvme_command c
= { };
570 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
571 c
.identify
.opcode
= nvme_admin_identify
,
572 c
.identify
.nsid
= cpu_to_le32(nsid
),
574 *id
= kmalloc(sizeof(struct nvme_id_ns
), GFP_KERNEL
);
578 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
579 sizeof(struct nvme_id_ns
));
585 int nvme_get_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned nsid
,
586 void *buffer
, size_t buflen
, u32
*result
)
588 struct nvme_command c
;
589 union nvme_result res
;
592 memset(&c
, 0, sizeof(c
));
593 c
.features
.opcode
= nvme_admin_get_features
;
594 c
.features
.nsid
= cpu_to_le32(nsid
);
595 c
.features
.fid
= cpu_to_le32(fid
);
597 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
, buffer
, buflen
, 0,
599 if (ret
>= 0 && result
)
600 *result
= le32_to_cpu(res
.u32
);
604 int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
605 void *buffer
, size_t buflen
, u32
*result
)
607 struct nvme_command c
;
608 union nvme_result res
;
611 memset(&c
, 0, sizeof(c
));
612 c
.features
.opcode
= nvme_admin_set_features
;
613 c
.features
.fid
= cpu_to_le32(fid
);
614 c
.features
.dword11
= cpu_to_le32(dword11
);
616 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
,
617 buffer
, buflen
, 0, NVME_QID_ANY
, 0, 0);
618 if (ret
>= 0 && result
)
619 *result
= le32_to_cpu(res
.u32
);
623 int nvme_get_log_page(struct nvme_ctrl
*dev
, struct nvme_smart_log
**log
)
625 struct nvme_command c
= { };
628 c
.common
.opcode
= nvme_admin_get_log_page
,
629 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF),
630 c
.common
.cdw10
[0] = cpu_to_le32(
631 (((sizeof(struct nvme_smart_log
) / 4) - 1) << 16) |
634 *log
= kmalloc(sizeof(struct nvme_smart_log
), GFP_KERNEL
);
638 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *log
,
639 sizeof(struct nvme_smart_log
));
645 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
647 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
649 int status
, nr_io_queues
;
651 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, NULL
, 0,
657 * Degraded controllers might return an error when setting the queue
658 * count. We still want to be able to bring them online and offer
659 * access to the admin queue, as that might be only way to fix them up.
662 dev_err(ctrl
->dev
, "Could not set queue count (%d)\n", status
);
665 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
666 *count
= min(*count
, nr_io_queues
);
671 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
673 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
675 struct nvme_user_io io
;
676 struct nvme_command c
;
677 unsigned length
, meta_len
;
678 void __user
*metadata
;
680 if (copy_from_user(&io
, uio
, sizeof(io
)))
688 case nvme_cmd_compare
:
694 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
695 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
696 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
701 } else if (meta_len
) {
702 if ((io
.metadata
& 3) || !io
.metadata
)
706 memset(&c
, 0, sizeof(c
));
707 c
.rw
.opcode
= io
.opcode
;
708 c
.rw
.flags
= io
.flags
;
709 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
710 c
.rw
.slba
= cpu_to_le64(io
.slba
);
711 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
712 c
.rw
.control
= cpu_to_le16(io
.control
);
713 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
714 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
715 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
716 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
718 return __nvme_submit_user_cmd(ns
->queue
, &c
,
719 (void __user
*)(uintptr_t)io
.addr
, length
,
720 metadata
, meta_len
, io
.slba
, NULL
, 0);
723 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
724 struct nvme_passthru_cmd __user
*ucmd
)
726 struct nvme_passthru_cmd cmd
;
727 struct nvme_command c
;
728 unsigned timeout
= 0;
731 if (!capable(CAP_SYS_ADMIN
))
733 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
738 memset(&c
, 0, sizeof(c
));
739 c
.common
.opcode
= cmd
.opcode
;
740 c
.common
.flags
= cmd
.flags
;
741 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
742 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
743 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
744 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
745 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
746 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
747 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
748 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
749 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
752 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
754 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
755 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
756 &cmd
.result
, timeout
);
758 if (put_user(cmd
.result
, &ucmd
->result
))
765 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
766 unsigned int cmd
, unsigned long arg
)
768 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
772 force_successful_syscall_return();
774 case NVME_IOCTL_ADMIN_CMD
:
775 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
776 case NVME_IOCTL_IO_CMD
:
777 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
778 case NVME_IOCTL_SUBMIT_IO
:
779 return nvme_submit_io(ns
, (void __user
*)arg
);
780 #ifdef CONFIG_BLK_DEV_NVME_SCSI
781 case SG_GET_VERSION_NUM
:
782 return nvme_sg_get_version_num((void __user
*)arg
);
784 return nvme_sg_io(ns
, (void __user
*)arg
);
792 static int nvme_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
793 unsigned int cmd
, unsigned long arg
)
799 return nvme_ioctl(bdev
, mode
, cmd
, arg
);
802 #define nvme_compat_ioctl NULL
805 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
807 return nvme_get_ns_from_disk(bdev
->bd_disk
) ? 0 : -ENXIO
;
810 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
812 struct nvme_ns
*ns
= disk
->private_data
;
814 module_put(ns
->ctrl
->ops
->module
);
818 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
820 /* some standard values */
822 geo
->sectors
= 1 << 5;
823 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
827 #ifdef CONFIG_BLK_DEV_INTEGRITY
828 static void nvme_init_integrity(struct nvme_ns
*ns
)
830 struct blk_integrity integrity
;
832 memset(&integrity
, 0, sizeof(integrity
));
833 switch (ns
->pi_type
) {
834 case NVME_NS_DPS_PI_TYPE3
:
835 integrity
.profile
= &t10_pi_type3_crc
;
836 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
837 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
839 case NVME_NS_DPS_PI_TYPE1
:
840 case NVME_NS_DPS_PI_TYPE2
:
841 integrity
.profile
= &t10_pi_type1_crc
;
842 integrity
.tag_size
= sizeof(u16
);
843 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
846 integrity
.profile
= NULL
;
849 integrity
.tuple_size
= ns
->ms
;
850 blk_integrity_register(ns
->disk
, &integrity
);
851 blk_queue_max_integrity_segments(ns
->queue
, 1);
854 static void nvme_init_integrity(struct nvme_ns
*ns
)
857 #endif /* CONFIG_BLK_DEV_INTEGRITY */
859 static void nvme_config_discard(struct nvme_ns
*ns
)
861 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
862 u32 logical_block_size
= queue_logical_block_size(ns
->queue
);
864 if (ctrl
->quirks
& NVME_QUIRK_DISCARD_ZEROES
)
865 ns
->queue
->limits
.discard_zeroes_data
= 1;
867 ns
->queue
->limits
.discard_zeroes_data
= 0;
869 ns
->queue
->limits
.discard_alignment
= logical_block_size
;
870 ns
->queue
->limits
.discard_granularity
= logical_block_size
;
871 blk_queue_max_discard_sectors(ns
->queue
, UINT_MAX
);
872 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, ns
->queue
);
875 static int nvme_revalidate_ns(struct nvme_ns
*ns
, struct nvme_id_ns
**id
)
877 if (nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, id
)) {
878 dev_warn(ns
->ctrl
->dev
, "%s: Identify failure\n", __func__
);
882 if ((*id
)->ncap
== 0) {
887 if (ns
->ctrl
->vs
>= NVME_VS(1, 1, 0))
888 memcpy(ns
->eui
, (*id
)->eui64
, sizeof(ns
->eui
));
889 if (ns
->ctrl
->vs
>= NVME_VS(1, 2, 0))
890 memcpy(ns
->uuid
, (*id
)->nguid
, sizeof(ns
->uuid
));
895 static void __nvme_revalidate_disk(struct gendisk
*disk
, struct nvme_id_ns
*id
)
897 struct nvme_ns
*ns
= disk
->private_data
;
903 lbaf
= id
->flbas
& NVME_NS_FLBAS_LBA_MASK
;
904 ns
->lba_shift
= id
->lbaf
[lbaf
].ds
;
905 ns
->ms
= le16_to_cpu(id
->lbaf
[lbaf
].ms
);
906 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
909 * If identify namespace failed, use default 512 byte block size so
910 * block layer can use before failing read/write for 0 capacity.
912 if (ns
->lba_shift
== 0)
914 bs
= 1 << ns
->lba_shift
;
915 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
916 pi_type
= ns
->ms
== sizeof(struct t10_pi_tuple
) ?
917 id
->dps
& NVME_NS_DPS_PI_MASK
: 0;
919 blk_mq_freeze_queue(disk
->queue
);
920 if (blk_get_integrity(disk
) && (ns
->pi_type
!= pi_type
||
922 bs
!= queue_logical_block_size(disk
->queue
) ||
923 (ns
->ms
&& ns
->ext
)))
924 blk_integrity_unregister(disk
);
926 ns
->pi_type
= pi_type
;
927 blk_queue_logical_block_size(ns
->queue
, bs
);
929 if (ns
->ms
&& !blk_get_integrity(disk
) && !ns
->ext
)
930 nvme_init_integrity(ns
);
931 if (ns
->ms
&& !(ns
->ms
== 8 && ns
->pi_type
) && !blk_get_integrity(disk
))
932 set_capacity(disk
, 0);
934 set_capacity(disk
, le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9));
936 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
937 nvme_config_discard(ns
);
938 blk_mq_unfreeze_queue(disk
->queue
);
941 static int nvme_revalidate_disk(struct gendisk
*disk
)
943 struct nvme_ns
*ns
= disk
->private_data
;
944 struct nvme_id_ns
*id
= NULL
;
947 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
948 set_capacity(disk
, 0);
952 ret
= nvme_revalidate_ns(ns
, &id
);
956 __nvme_revalidate_disk(disk
, id
);
962 static char nvme_pr_type(enum pr_type type
)
965 case PR_WRITE_EXCLUSIVE
:
967 case PR_EXCLUSIVE_ACCESS
:
969 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
971 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
973 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
975 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
982 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
983 u64 key
, u64 sa_key
, u8 op
)
985 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
986 struct nvme_command c
;
987 u8 data
[16] = { 0, };
989 put_unaligned_le64(key
, &data
[0]);
990 put_unaligned_le64(sa_key
, &data
[8]);
992 memset(&c
, 0, sizeof(c
));
993 c
.common
.opcode
= op
;
994 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
995 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
997 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
1000 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
1001 u64
new, unsigned flags
)
1005 if (flags
& ~PR_FL_IGNORE_KEY
)
1008 cdw10
= old
? 2 : 0;
1009 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
1010 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
1011 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
1014 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
1015 enum pr_type type
, unsigned flags
)
1019 if (flags
& ~PR_FL_IGNORE_KEY
)
1022 cdw10
= nvme_pr_type(type
) << 8;
1023 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
1024 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
1027 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
1028 enum pr_type type
, bool abort
)
1030 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
1031 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
1034 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
1036 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
1037 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
1040 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1042 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
1043 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
1046 static const struct pr_ops nvme_pr_ops
= {
1047 .pr_register
= nvme_pr_register
,
1048 .pr_reserve
= nvme_pr_reserve
,
1049 .pr_release
= nvme_pr_release
,
1050 .pr_preempt
= nvme_pr_preempt
,
1051 .pr_clear
= nvme_pr_clear
,
1054 static const struct block_device_operations nvme_fops
= {
1055 .owner
= THIS_MODULE
,
1056 .ioctl
= nvme_ioctl
,
1057 .compat_ioctl
= nvme_compat_ioctl
,
1059 .release
= nvme_release
,
1060 .getgeo
= nvme_getgeo
,
1061 .revalidate_disk
= nvme_revalidate_disk
,
1062 .pr_ops
= &nvme_pr_ops
,
1065 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
1067 unsigned long timeout
=
1068 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
1069 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
1072 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1075 if ((csts
& NVME_CSTS_RDY
) == bit
)
1079 if (fatal_signal_pending(current
))
1081 if (time_after(jiffies
, timeout
)) {
1082 dev_err(ctrl
->device
,
1083 "Device not ready; aborting %s\n", enabled
?
1084 "initialisation" : "reset");
1093 * If the device has been passed off to us in an enabled state, just clear
1094 * the enabled bit. The spec says we should set the 'shutdown notification
1095 * bits', but doing so may cause the device to complete commands to the
1096 * admin queue ... and we don't know what memory that might be pointing at!
1098 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1102 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1103 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1105 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1109 if (ctrl
->quirks
& NVME_QUIRK_DELAY_BEFORE_CHK_RDY
)
1110 msleep(NVME_QUIRK_DELAY_AMOUNT
);
1112 return nvme_wait_ready(ctrl
, cap
, false);
1114 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1116 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1119 * Default to a 4K page size, with the intention to update this
1120 * path in the future to accomodate architectures with differing
1121 * kernel and IO page sizes.
1123 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1126 if (page_shift
< dev_page_min
) {
1127 dev_err(ctrl
->device
,
1128 "Minimum device page size %u too large for host (%u)\n",
1129 1 << dev_page_min
, 1 << page_shift
);
1133 ctrl
->page_size
= 1 << page_shift
;
1135 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1136 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1137 ctrl
->ctrl_config
|= NVME_CC_ARB_RR
| NVME_CC_SHN_NONE
;
1138 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1139 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1141 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1144 return nvme_wait_ready(ctrl
, cap
, true);
1146 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1148 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1150 unsigned long timeout
= SHUTDOWN_TIMEOUT
+ jiffies
;
1154 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1155 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1157 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1161 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1162 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1166 if (fatal_signal_pending(current
))
1168 if (time_after(jiffies
, timeout
)) {
1169 dev_err(ctrl
->device
,
1170 "Device shutdown incomplete; abort shutdown\n");
1177 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1179 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1180 struct request_queue
*q
)
1184 if (ctrl
->max_hw_sectors
) {
1186 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1188 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1189 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1191 if (ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
)
1192 blk_queue_chunk_sectors(q
, ctrl
->max_hw_sectors
);
1193 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1194 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1196 blk_queue_write_cache(q
, vwc
, vwc
);
1199 struct nvme_core_quirk_entry
{
1201 * NVMe model and firmware strings are padded with spaces. For
1202 * simplicity, strings in the quirk table are padded with NULLs
1208 unsigned long quirks
;
1211 static const struct nvme_core_quirk_entry core_quirks
[] = {
1214 /* match is null-terminated but idstr is space-padded. */
1215 static bool string_matches(const char *idstr
, const char *match
, size_t len
)
1222 matchlen
= strlen(match
);
1223 WARN_ON_ONCE(matchlen
> len
);
1225 if (memcmp(idstr
, match
, matchlen
))
1228 for (; matchlen
< len
; matchlen
++)
1229 if (idstr
[matchlen
] != ' ')
1235 static bool quirk_matches(const struct nvme_id_ctrl
*id
,
1236 const struct nvme_core_quirk_entry
*q
)
1238 return q
->vid
== le16_to_cpu(id
->vid
) &&
1239 string_matches(id
->mn
, q
->mn
, sizeof(id
->mn
)) &&
1240 string_matches(id
->fr
, q
->fr
, sizeof(id
->fr
));
1244 * Initialize the cached copies of the Identify data and various controller
1245 * register in our nvme_ctrl structure. This should be called as soon as
1246 * the admin queue is fully up and running.
1248 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1250 struct nvme_id_ctrl
*id
;
1252 int ret
, page_shift
;
1255 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1257 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1261 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1263 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1266 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1268 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1269 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1271 ret
= nvme_identify_ctrl(ctrl
, &id
);
1273 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1277 if (!ctrl
->identified
) {
1279 * Check for quirks. Quirk can depend on firmware version,
1280 * so, in principle, the set of quirks present can change
1281 * across a reset. As a possible future enhancement, we
1282 * could re-scan for quirks every time we reinitialize
1283 * the device, but we'd have to make sure that the driver
1284 * behaves intelligently if the quirks change.
1289 for (i
= 0; i
< ARRAY_SIZE(core_quirks
); i
++) {
1290 if (quirk_matches(id
, &core_quirks
[i
]))
1291 ctrl
->quirks
|= core_quirks
[i
].quirks
;
1295 ctrl
->vid
= le16_to_cpu(id
->vid
);
1296 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1297 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1298 ctrl
->vwc
= id
->vwc
;
1299 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1300 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1301 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1302 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1304 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1306 max_hw_sectors
= UINT_MAX
;
1307 ctrl
->max_hw_sectors
=
1308 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
1310 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1311 ctrl
->sgls
= le32_to_cpu(id
->sgls
);
1312 ctrl
->kas
= le16_to_cpu(id
->kas
);
1314 if (ctrl
->ops
->is_fabrics
) {
1315 ctrl
->icdoff
= le16_to_cpu(id
->icdoff
);
1316 ctrl
->ioccsz
= le32_to_cpu(id
->ioccsz
);
1317 ctrl
->iorcsz
= le32_to_cpu(id
->iorcsz
);
1318 ctrl
->maxcmd
= le16_to_cpu(id
->maxcmd
);
1321 * In fabrics we need to verify the cntlid matches the
1324 if (ctrl
->cntlid
!= le16_to_cpu(id
->cntlid
))
1327 if (!ctrl
->opts
->discovery_nqn
&& !ctrl
->kas
) {
1329 "keep-alive support is mandatory for fabrics\n");
1333 ctrl
->cntlid
= le16_to_cpu(id
->cntlid
);
1338 ctrl
->identified
= true;
1341 EXPORT_SYMBOL_GPL(nvme_init_identify
);
1343 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
1345 struct nvme_ctrl
*ctrl
;
1346 int instance
= iminor(inode
);
1349 spin_lock(&dev_list_lock
);
1350 list_for_each_entry(ctrl
, &nvme_ctrl_list
, node
) {
1351 if (ctrl
->instance
!= instance
)
1354 if (!ctrl
->admin_q
) {
1358 if (!kref_get_unless_zero(&ctrl
->kref
))
1360 file
->private_data
= ctrl
;
1364 spin_unlock(&dev_list_lock
);
1369 static int nvme_dev_release(struct inode
*inode
, struct file
*file
)
1371 nvme_put_ctrl(file
->private_data
);
1375 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
1380 mutex_lock(&ctrl
->namespaces_mutex
);
1381 if (list_empty(&ctrl
->namespaces
)) {
1386 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
1387 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
1388 dev_warn(ctrl
->device
,
1389 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1394 dev_warn(ctrl
->device
,
1395 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1396 kref_get(&ns
->kref
);
1397 mutex_unlock(&ctrl
->namespaces_mutex
);
1399 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
1404 mutex_unlock(&ctrl
->namespaces_mutex
);
1408 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
1411 struct nvme_ctrl
*ctrl
= file
->private_data
;
1412 void __user
*argp
= (void __user
*)arg
;
1415 case NVME_IOCTL_ADMIN_CMD
:
1416 return nvme_user_cmd(ctrl
, NULL
, argp
);
1417 case NVME_IOCTL_IO_CMD
:
1418 return nvme_dev_user_cmd(ctrl
, argp
);
1419 case NVME_IOCTL_RESET
:
1420 dev_warn(ctrl
->device
, "resetting controller\n");
1421 return ctrl
->ops
->reset_ctrl(ctrl
);
1422 case NVME_IOCTL_SUBSYS_RESET
:
1423 return nvme_reset_subsystem(ctrl
);
1424 case NVME_IOCTL_RESCAN
:
1425 nvme_queue_scan(ctrl
);
1432 static const struct file_operations nvme_dev_fops
= {
1433 .owner
= THIS_MODULE
,
1434 .open
= nvme_dev_open
,
1435 .release
= nvme_dev_release
,
1436 .unlocked_ioctl
= nvme_dev_ioctl
,
1437 .compat_ioctl
= nvme_dev_ioctl
,
1440 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
1441 struct device_attribute
*attr
, const char *buf
,
1444 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1447 ret
= ctrl
->ops
->reset_ctrl(ctrl
);
1452 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
1454 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
1455 struct device_attribute
*attr
, const char *buf
,
1458 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1460 nvme_queue_scan(ctrl
);
1463 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
1465 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
1468 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1469 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1470 int serial_len
= sizeof(ctrl
->serial
);
1471 int model_len
= sizeof(ctrl
->model
);
1473 if (memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1474 return sprintf(buf
, "eui.%16phN\n", ns
->uuid
);
1476 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1477 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
1479 while (ctrl
->serial
[serial_len
- 1] == ' ')
1481 while (ctrl
->model
[model_len
- 1] == ' ')
1484 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
1485 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
1487 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
1489 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
1492 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1493 return sprintf(buf
, "%pU\n", ns
->uuid
);
1495 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
1497 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
1500 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1501 return sprintf(buf
, "%8phd\n", ns
->eui
);
1503 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
1505 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
1508 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1509 return sprintf(buf
, "%d\n", ns
->ns_id
);
1511 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
1513 static struct attribute
*nvme_ns_attrs
[] = {
1514 &dev_attr_wwid
.attr
,
1515 &dev_attr_uuid
.attr
,
1517 &dev_attr_nsid
.attr
,
1521 static umode_t
nvme_ns_attrs_are_visible(struct kobject
*kobj
,
1522 struct attribute
*a
, int n
)
1524 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1525 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1527 if (a
== &dev_attr_uuid
.attr
) {
1528 if (!memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1531 if (a
== &dev_attr_eui
.attr
) {
1532 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1538 static const struct attribute_group nvme_ns_attr_group
= {
1539 .attrs
= nvme_ns_attrs
,
1540 .is_visible
= nvme_ns_attrs_are_visible
,
1543 #define nvme_show_str_function(field) \
1544 static ssize_t field##_show(struct device *dev, \
1545 struct device_attribute *attr, char *buf) \
1547 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1548 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1550 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1552 #define nvme_show_int_function(field) \
1553 static ssize_t field##_show(struct device *dev, \
1554 struct device_attribute *attr, char *buf) \
1556 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1557 return sprintf(buf, "%d\n", ctrl->field); \
1559 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1561 nvme_show_str_function(model
);
1562 nvme_show_str_function(serial
);
1563 nvme_show_str_function(firmware_rev
);
1564 nvme_show_int_function(cntlid
);
1566 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
1567 struct device_attribute
*attr
, const char *buf
,
1570 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1572 if (device_remove_file_self(dev
, attr
))
1573 ctrl
->ops
->delete_ctrl(ctrl
);
1576 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
1578 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
1579 struct device_attribute
*attr
,
1582 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1584 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
1586 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
1588 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
1589 struct device_attribute
*attr
,
1592 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1594 return snprintf(buf
, PAGE_SIZE
, "%s\n",
1595 ctrl
->ops
->get_subsysnqn(ctrl
));
1597 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
1599 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
1600 struct device_attribute
*attr
,
1603 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1605 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
1607 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
1609 static struct attribute
*nvme_dev_attrs
[] = {
1610 &dev_attr_reset_controller
.attr
,
1611 &dev_attr_rescan_controller
.attr
,
1612 &dev_attr_model
.attr
,
1613 &dev_attr_serial
.attr
,
1614 &dev_attr_firmware_rev
.attr
,
1615 &dev_attr_cntlid
.attr
,
1616 &dev_attr_delete_controller
.attr
,
1617 &dev_attr_transport
.attr
,
1618 &dev_attr_subsysnqn
.attr
,
1619 &dev_attr_address
.attr
,
1623 #define CHECK_ATTR(ctrl, a, name) \
1624 if ((a) == &dev_attr_##name.attr && \
1625 !(ctrl)->ops->get_##name) \
1628 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
1629 struct attribute
*a
, int n
)
1631 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1632 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1634 if (a
== &dev_attr_delete_controller
.attr
) {
1635 if (!ctrl
->ops
->delete_ctrl
)
1639 CHECK_ATTR(ctrl
, a
, subsysnqn
);
1640 CHECK_ATTR(ctrl
, a
, address
);
1645 static struct attribute_group nvme_dev_attrs_group
= {
1646 .attrs
= nvme_dev_attrs
,
1647 .is_visible
= nvme_dev_attrs_are_visible
,
1650 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
1651 &nvme_dev_attrs_group
,
1655 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1657 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
1658 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
1660 return nsa
->ns_id
- nsb
->ns_id
;
1663 static struct nvme_ns
*nvme_find_get_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1665 struct nvme_ns
*ns
, *ret
= NULL
;
1667 mutex_lock(&ctrl
->namespaces_mutex
);
1668 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
1669 if (ns
->ns_id
== nsid
) {
1670 kref_get(&ns
->kref
);
1674 if (ns
->ns_id
> nsid
)
1677 mutex_unlock(&ctrl
->namespaces_mutex
);
1681 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1684 struct gendisk
*disk
;
1685 struct nvme_id_ns
*id
;
1686 char disk_name
[DISK_NAME_LEN
];
1687 int node
= dev_to_node(ctrl
->dev
);
1689 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
1693 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
1694 if (ns
->instance
< 0)
1697 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
1698 if (IS_ERR(ns
->queue
))
1699 goto out_release_instance
;
1700 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
1701 ns
->queue
->queuedata
= ns
;
1704 kref_init(&ns
->kref
);
1706 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
1708 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
1709 nvme_set_queue_limits(ctrl
, ns
->queue
);
1711 sprintf(disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
1713 if (nvme_revalidate_ns(ns
, &id
))
1714 goto out_free_queue
;
1716 if (nvme_nvm_ns_supported(ns
, id
) &&
1717 nvme_nvm_register(ns
, disk_name
, node
)) {
1718 dev_warn(ctrl
->dev
, "%s: LightNVM init failure\n", __func__
);
1722 disk
= alloc_disk_node(0, node
);
1726 disk
->fops
= &nvme_fops
;
1727 disk
->private_data
= ns
;
1728 disk
->queue
= ns
->queue
;
1729 disk
->flags
= GENHD_FL_EXT_DEVT
;
1730 memcpy(disk
->disk_name
, disk_name
, DISK_NAME_LEN
);
1733 __nvme_revalidate_disk(disk
, id
);
1735 mutex_lock(&ctrl
->namespaces_mutex
);
1736 list_add_tail(&ns
->list
, &ctrl
->namespaces
);
1737 mutex_unlock(&ctrl
->namespaces_mutex
);
1739 kref_get(&ctrl
->kref
);
1743 device_add_disk(ctrl
->device
, ns
->disk
);
1744 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
1745 &nvme_ns_attr_group
))
1746 pr_warn("%s: failed to create sysfs group for identification\n",
1747 ns
->disk
->disk_name
);
1748 if (ns
->ndev
&& nvme_nvm_register_sysfs(ns
))
1749 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
1750 ns
->disk
->disk_name
);
1755 blk_cleanup_queue(ns
->queue
);
1756 out_release_instance
:
1757 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
1762 static void nvme_ns_remove(struct nvme_ns
*ns
)
1764 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
1767 if (ns
->disk
&& ns
->disk
->flags
& GENHD_FL_UP
) {
1768 if (blk_get_integrity(ns
->disk
))
1769 blk_integrity_unregister(ns
->disk
);
1770 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
1771 &nvme_ns_attr_group
);
1773 nvme_nvm_unregister_sysfs(ns
);
1774 del_gendisk(ns
->disk
);
1775 blk_mq_abort_requeue_list(ns
->queue
);
1776 blk_cleanup_queue(ns
->queue
);
1779 mutex_lock(&ns
->ctrl
->namespaces_mutex
);
1780 list_del_init(&ns
->list
);
1781 mutex_unlock(&ns
->ctrl
->namespaces_mutex
);
1786 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1790 ns
= nvme_find_get_ns(ctrl
, nsid
);
1792 if (ns
->disk
&& revalidate_disk(ns
->disk
))
1796 nvme_alloc_ns(ctrl
, nsid
);
1799 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
1802 struct nvme_ns
*ns
, *next
;
1804 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
1805 if (ns
->ns_id
> nsid
)
1810 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
1814 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
1817 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
1821 for (i
= 0; i
< num_lists
; i
++) {
1822 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
1826 for (j
= 0; j
< min(nn
, 1024U); j
++) {
1827 nsid
= le32_to_cpu(ns_list
[j
]);
1831 nvme_validate_ns(ctrl
, nsid
);
1833 while (++prev
< nsid
) {
1834 ns
= nvme_find_get_ns(ctrl
, prev
);
1844 nvme_remove_invalid_namespaces(ctrl
, prev
);
1850 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
1854 for (i
= 1; i
<= nn
; i
++)
1855 nvme_validate_ns(ctrl
, i
);
1857 nvme_remove_invalid_namespaces(ctrl
, nn
);
1860 static void nvme_scan_work(struct work_struct
*work
)
1862 struct nvme_ctrl
*ctrl
=
1863 container_of(work
, struct nvme_ctrl
, scan_work
);
1864 struct nvme_id_ctrl
*id
;
1867 if (ctrl
->state
!= NVME_CTRL_LIVE
)
1870 if (nvme_identify_ctrl(ctrl
, &id
))
1873 nn
= le32_to_cpu(id
->nn
);
1874 if (ctrl
->vs
>= NVME_VS(1, 1, 0) &&
1875 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
1876 if (!nvme_scan_ns_list(ctrl
, nn
))
1879 nvme_scan_ns_sequential(ctrl
, nn
);
1881 mutex_lock(&ctrl
->namespaces_mutex
);
1882 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
1883 mutex_unlock(&ctrl
->namespaces_mutex
);
1887 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
1890 * Do not queue new scan work when a controller is reset during
1893 if (ctrl
->state
== NVME_CTRL_LIVE
)
1894 schedule_work(&ctrl
->scan_work
);
1896 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
1899 * This function iterates the namespace list unlocked to allow recovery from
1900 * controller failure. It is up to the caller to ensure the namespace list is
1901 * not modified by scan work while this function is executing.
1903 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
1905 struct nvme_ns
*ns
, *next
;
1908 * The dead states indicates the controller was not gracefully
1909 * disconnected. In that case, we won't be able to flush any data while
1910 * removing the namespaces' disks; fail all the queues now to avoid
1911 * potentially having to clean up the failed sync later.
1913 if (ctrl
->state
== NVME_CTRL_DEAD
)
1914 nvme_kill_queues(ctrl
);
1916 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
1919 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
1921 static void nvme_async_event_work(struct work_struct
*work
)
1923 struct nvme_ctrl
*ctrl
=
1924 container_of(work
, struct nvme_ctrl
, async_event_work
);
1926 spin_lock_irq(&ctrl
->lock
);
1927 while (ctrl
->event_limit
> 0) {
1928 int aer_idx
= --ctrl
->event_limit
;
1930 spin_unlock_irq(&ctrl
->lock
);
1931 ctrl
->ops
->submit_async_event(ctrl
, aer_idx
);
1932 spin_lock_irq(&ctrl
->lock
);
1934 spin_unlock_irq(&ctrl
->lock
);
1937 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
, __le16 status
,
1938 union nvme_result
*res
)
1940 u32 result
= le32_to_cpu(res
->u32
);
1943 switch (le16_to_cpu(status
) >> 1) {
1944 case NVME_SC_SUCCESS
:
1947 case NVME_SC_ABORT_REQ
:
1948 ++ctrl
->event_limit
;
1949 schedule_work(&ctrl
->async_event_work
);
1958 switch (result
& 0xff07) {
1959 case NVME_AER_NOTICE_NS_CHANGED
:
1960 dev_info(ctrl
->device
, "rescanning\n");
1961 nvme_queue_scan(ctrl
);
1964 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
1967 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
1969 void nvme_queue_async_events(struct nvme_ctrl
*ctrl
)
1971 ctrl
->event_limit
= NVME_NR_AERS
;
1972 schedule_work(&ctrl
->async_event_work
);
1974 EXPORT_SYMBOL_GPL(nvme_queue_async_events
);
1976 static DEFINE_IDA(nvme_instance_ida
);
1978 static int nvme_set_instance(struct nvme_ctrl
*ctrl
)
1980 int instance
, error
;
1983 if (!ida_pre_get(&nvme_instance_ida
, GFP_KERNEL
))
1986 spin_lock(&dev_list_lock
);
1987 error
= ida_get_new(&nvme_instance_ida
, &instance
);
1988 spin_unlock(&dev_list_lock
);
1989 } while (error
== -EAGAIN
);
1994 ctrl
->instance
= instance
;
1998 static void nvme_release_instance(struct nvme_ctrl
*ctrl
)
2000 spin_lock(&dev_list_lock
);
2001 ida_remove(&nvme_instance_ida
, ctrl
->instance
);
2002 spin_unlock(&dev_list_lock
);
2005 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
2007 flush_work(&ctrl
->async_event_work
);
2008 flush_work(&ctrl
->scan_work
);
2009 nvme_remove_namespaces(ctrl
);
2011 device_destroy(nvme_class
, MKDEV(nvme_char_major
, ctrl
->instance
));
2013 spin_lock(&dev_list_lock
);
2014 list_del(&ctrl
->node
);
2015 spin_unlock(&dev_list_lock
);
2017 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
2019 static void nvme_free_ctrl(struct kref
*kref
)
2021 struct nvme_ctrl
*ctrl
= container_of(kref
, struct nvme_ctrl
, kref
);
2023 put_device(ctrl
->device
);
2024 nvme_release_instance(ctrl
);
2025 ida_destroy(&ctrl
->ns_ida
);
2027 ctrl
->ops
->free_ctrl(ctrl
);
2030 void nvme_put_ctrl(struct nvme_ctrl
*ctrl
)
2032 kref_put(&ctrl
->kref
, nvme_free_ctrl
);
2034 EXPORT_SYMBOL_GPL(nvme_put_ctrl
);
2037 * Initialize a NVMe controller structures. This needs to be called during
2038 * earliest initialization so that we have the initialized structured around
2041 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
2042 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
2046 ctrl
->state
= NVME_CTRL_NEW
;
2047 spin_lock_init(&ctrl
->lock
);
2048 INIT_LIST_HEAD(&ctrl
->namespaces
);
2049 mutex_init(&ctrl
->namespaces_mutex
);
2050 kref_init(&ctrl
->kref
);
2053 ctrl
->quirks
= quirks
;
2054 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
2055 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
2057 ret
= nvme_set_instance(ctrl
);
2061 ctrl
->device
= device_create_with_groups(nvme_class
, ctrl
->dev
,
2062 MKDEV(nvme_char_major
, ctrl
->instance
),
2063 ctrl
, nvme_dev_attr_groups
,
2064 "nvme%d", ctrl
->instance
);
2065 if (IS_ERR(ctrl
->device
)) {
2066 ret
= PTR_ERR(ctrl
->device
);
2067 goto out_release_instance
;
2069 get_device(ctrl
->device
);
2070 ida_init(&ctrl
->ns_ida
);
2072 spin_lock(&dev_list_lock
);
2073 list_add_tail(&ctrl
->node
, &nvme_ctrl_list
);
2074 spin_unlock(&dev_list_lock
);
2077 out_release_instance
:
2078 nvme_release_instance(ctrl
);
2082 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
2085 * nvme_kill_queues(): Ends all namespace queues
2086 * @ctrl: the dead controller that needs to end
2088 * Call this function when the driver determines it is unable to get the
2089 * controller in a state capable of servicing IO.
2091 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
2095 mutex_lock(&ctrl
->namespaces_mutex
);
2096 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2098 * Revalidating a dead namespace sets capacity to 0. This will
2099 * end buffered writers dirtying pages that can't be synced.
2101 if (ns
->disk
&& !test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
2102 revalidate_disk(ns
->disk
);
2104 blk_set_queue_dying(ns
->queue
);
2105 blk_mq_abort_requeue_list(ns
->queue
);
2106 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
2108 mutex_unlock(&ctrl
->namespaces_mutex
);
2110 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
2112 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
2116 mutex_lock(&ctrl
->namespaces_mutex
);
2117 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2118 blk_mq_quiesce_queue(ns
->queue
);
2119 mutex_unlock(&ctrl
->namespaces_mutex
);
2121 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
2123 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
2127 mutex_lock(&ctrl
->namespaces_mutex
);
2128 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2129 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
2130 blk_mq_kick_requeue_list(ns
->queue
);
2132 mutex_unlock(&ctrl
->namespaces_mutex
);
2134 EXPORT_SYMBOL_GPL(nvme_start_queues
);
2136 int __init
nvme_core_init(void)
2140 result
= __register_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme",
2144 else if (result
> 0)
2145 nvme_char_major
= result
;
2147 nvme_class
= class_create(THIS_MODULE
, "nvme");
2148 if (IS_ERR(nvme_class
)) {
2149 result
= PTR_ERR(nvme_class
);
2150 goto unregister_chrdev
;
2156 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2160 void nvme_core_exit(void)
2162 class_destroy(nvme_class
);
2163 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2166 MODULE_LICENSE("GPL");
2167 MODULE_VERSION("1.0");
2168 module_init(nvme_core_init
);
2169 module_exit(nvme_core_exit
);