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>
29 #include <linux/pm_qos.h>
31 #include <asm/unaligned.h>
36 #define NVME_MINORS (1U << MINORBITS)
38 unsigned char admin_timeout
= 60;
39 module_param(admin_timeout
, byte
, 0644);
40 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
41 EXPORT_SYMBOL_GPL(admin_timeout
);
43 unsigned char nvme_io_timeout
= 30;
44 module_param_named(io_timeout
, nvme_io_timeout
, byte
, 0644);
45 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
46 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
48 unsigned char shutdown_timeout
= 5;
49 module_param(shutdown_timeout
, byte
, 0644);
50 MODULE_PARM_DESC(shutdown_timeout
, "timeout in seconds for controller shutdown");
52 static u8 nvme_max_retries
= 5;
53 module_param_named(max_retries
, nvme_max_retries
, byte
, 0644);
54 MODULE_PARM_DESC(max_retries
, "max number of retries a command may have");
56 static int nvme_char_major
;
57 module_param(nvme_char_major
, int, 0);
59 static unsigned long default_ps_max_latency_us
= 25000;
60 module_param(default_ps_max_latency_us
, ulong
, 0644);
61 MODULE_PARM_DESC(default_ps_max_latency_us
,
62 "max power saving latency for new devices; use PM QOS to change per device");
64 static bool force_apst
;
65 module_param(force_apst
, bool, 0644);
66 MODULE_PARM_DESC(force_apst
, "allow APST for newly enumerated devices even if quirked off");
68 static LIST_HEAD(nvme_ctrl_list
);
69 static DEFINE_SPINLOCK(dev_list_lock
);
71 static struct class *nvme_class
;
73 static int nvme_error_status(struct request
*req
)
75 switch (nvme_req(req
)->status
& 0x7ff) {
78 case NVME_SC_CAP_EXCEEDED
:
84 * XXX: these errors are a nasty side-band protocol to
85 * drivers/md/dm-mpath.c:noretry_error() that aren't documented
88 case NVME_SC_CMD_SEQ_ERROR
:
90 case NVME_SC_ONCS_NOT_SUPPORTED
:
92 case NVME_SC_WRITE_FAULT
:
93 case NVME_SC_READ_ERROR
:
94 case NVME_SC_UNWRITTEN_BLOCK
:
99 static inline bool nvme_req_needs_retry(struct request
*req
)
101 if (blk_noretry_request(req
))
103 if (nvme_req(req
)->status
& NVME_SC_DNR
)
105 if (jiffies
- req
->start_time
>= req
->timeout
)
107 if (nvme_req(req
)->retries
>= nvme_max_retries
)
112 void nvme_complete_rq(struct request
*req
)
114 if (unlikely(nvme_req(req
)->status
&& nvme_req_needs_retry(req
))) {
115 nvme_req(req
)->retries
++;
116 blk_mq_requeue_request(req
, !blk_mq_queue_stopped(req
->q
));
120 blk_mq_end_request(req
, nvme_error_status(req
));
122 EXPORT_SYMBOL_GPL(nvme_complete_rq
);
124 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
128 if (!blk_mq_request_started(req
))
131 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
132 "Cancelling I/O %d", req
->tag
);
134 status
= NVME_SC_ABORT_REQ
;
135 if (blk_queue_dying(req
->q
))
136 status
|= NVME_SC_DNR
;
137 nvme_req(req
)->status
= status
;
138 blk_mq_complete_request(req
);
141 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
143 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
144 enum nvme_ctrl_state new_state
)
146 enum nvme_ctrl_state old_state
;
147 bool changed
= false;
149 spin_lock_irq(&ctrl
->lock
);
151 old_state
= ctrl
->state
;
156 case NVME_CTRL_RESETTING
:
157 case NVME_CTRL_RECONNECTING
:
164 case NVME_CTRL_RESETTING
:
168 case NVME_CTRL_RECONNECTING
:
175 case NVME_CTRL_RECONNECTING
:
184 case NVME_CTRL_DELETING
:
187 case NVME_CTRL_RESETTING
:
188 case NVME_CTRL_RECONNECTING
:
197 case NVME_CTRL_DELETING
:
209 ctrl
->state
= new_state
;
211 spin_unlock_irq(&ctrl
->lock
);
215 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
217 static void nvme_free_ns(struct kref
*kref
)
219 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
222 nvme_nvm_unregister(ns
);
225 spin_lock(&dev_list_lock
);
226 ns
->disk
->private_data
= NULL
;
227 spin_unlock(&dev_list_lock
);
231 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
232 nvme_put_ctrl(ns
->ctrl
);
236 static void nvme_put_ns(struct nvme_ns
*ns
)
238 kref_put(&ns
->kref
, nvme_free_ns
);
241 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
)
245 spin_lock(&dev_list_lock
);
246 ns
= disk
->private_data
;
248 if (!kref_get_unless_zero(&ns
->kref
))
250 if (!try_module_get(ns
->ctrl
->ops
->module
))
253 spin_unlock(&dev_list_lock
);
258 kref_put(&ns
->kref
, nvme_free_ns
);
260 spin_unlock(&dev_list_lock
);
264 struct request
*nvme_alloc_request(struct request_queue
*q
,
265 struct nvme_command
*cmd
, unsigned int flags
, int qid
)
267 unsigned op
= nvme_is_write(cmd
) ? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
;
270 if (qid
== NVME_QID_ANY
) {
271 req
= blk_mq_alloc_request(q
, op
, flags
);
273 req
= blk_mq_alloc_request_hctx(q
, op
, flags
,
279 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
280 nvme_req(req
)->cmd
= cmd
;
284 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
286 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
287 struct nvme_command
*cmnd
)
289 memset(cmnd
, 0, sizeof(*cmnd
));
290 cmnd
->common
.opcode
= nvme_cmd_flush
;
291 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
294 static inline int nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
295 struct nvme_command
*cmnd
)
297 unsigned short segments
= blk_rq_nr_discard_segments(req
), n
= 0;
298 struct nvme_dsm_range
*range
;
301 range
= kmalloc_array(segments
, sizeof(*range
), GFP_ATOMIC
);
303 return BLK_MQ_RQ_QUEUE_BUSY
;
305 __rq_for_each_bio(bio
, req
) {
306 u64 slba
= nvme_block_nr(ns
, bio
->bi_iter
.bi_sector
);
307 u32 nlb
= bio
->bi_iter
.bi_size
>> ns
->lba_shift
;
309 range
[n
].cattr
= cpu_to_le32(0);
310 range
[n
].nlb
= cpu_to_le32(nlb
);
311 range
[n
].slba
= cpu_to_le64(slba
);
315 if (WARN_ON_ONCE(n
!= segments
)) {
317 return BLK_MQ_RQ_QUEUE_ERROR
;
320 memset(cmnd
, 0, sizeof(*cmnd
));
321 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
322 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
323 cmnd
->dsm
.nr
= cpu_to_le32(segments
- 1);
324 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
326 req
->special_vec
.bv_page
= virt_to_page(range
);
327 req
->special_vec
.bv_offset
= offset_in_page(range
);
328 req
->special_vec
.bv_len
= sizeof(*range
) * segments
;
329 req
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
331 return BLK_MQ_RQ_QUEUE_OK
;
334 static inline void nvme_setup_rw(struct nvme_ns
*ns
, struct request
*req
,
335 struct nvme_command
*cmnd
)
340 if (req
->cmd_flags
& REQ_FUA
)
341 control
|= NVME_RW_FUA
;
342 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
343 control
|= NVME_RW_LR
;
345 if (req
->cmd_flags
& REQ_RAHEAD
)
346 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
348 memset(cmnd
, 0, sizeof(*cmnd
));
349 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
350 cmnd
->rw
.nsid
= cpu_to_le32(ns
->ns_id
);
351 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
352 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
355 switch (ns
->pi_type
) {
356 case NVME_NS_DPS_PI_TYPE3
:
357 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
359 case NVME_NS_DPS_PI_TYPE1
:
360 case NVME_NS_DPS_PI_TYPE2
:
361 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
362 NVME_RW_PRINFO_PRCHK_REF
;
363 cmnd
->rw
.reftag
= cpu_to_le32(
364 nvme_block_nr(ns
, blk_rq_pos(req
)));
367 if (!blk_integrity_rq(req
))
368 control
|= NVME_RW_PRINFO_PRACT
;
371 cmnd
->rw
.control
= cpu_to_le16(control
);
372 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
375 int nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
376 struct nvme_command
*cmd
)
378 int ret
= BLK_MQ_RQ_QUEUE_OK
;
380 if (!(req
->rq_flags
& RQF_DONTPREP
)) {
381 nvme_req(req
)->retries
= 0;
382 nvme_req(req
)->flags
= 0;
383 req
->rq_flags
|= RQF_DONTPREP
;
386 switch (req_op(req
)) {
389 memcpy(cmd
, nvme_req(req
)->cmd
, sizeof(*cmd
));
392 nvme_setup_flush(ns
, cmd
);
394 case REQ_OP_WRITE_ZEROES
:
395 /* currently only aliased to deallocate for a few ctrls: */
397 ret
= nvme_setup_discard(ns
, req
, cmd
);
401 nvme_setup_rw(ns
, req
, cmd
);
405 return BLK_MQ_RQ_QUEUE_ERROR
;
408 cmd
->common
.command_id
= req
->tag
;
411 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
414 * Returns 0 on success. If the result is negative, it's a Linux error code;
415 * if the result is positive, it's an NVM Express status code
417 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
418 union nvme_result
*result
, void *buffer
, unsigned bufflen
,
419 unsigned timeout
, int qid
, int at_head
, int flags
)
424 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
428 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
430 if (buffer
&& bufflen
) {
431 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
436 blk_execute_rq(req
->q
, NULL
, req
, at_head
);
438 *result
= nvme_req(req
)->result
;
439 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
442 ret
= nvme_req(req
)->status
;
444 blk_mq_free_request(req
);
447 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
449 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
450 void *buffer
, unsigned bufflen
)
452 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
455 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
457 int __nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
458 void __user
*ubuffer
, unsigned bufflen
,
459 void __user
*meta_buffer
, unsigned meta_len
, u32 meta_seed
,
460 u32
*result
, unsigned timeout
)
462 bool write
= nvme_is_write(cmd
);
463 struct nvme_ns
*ns
= q
->queuedata
;
464 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
466 struct bio
*bio
= NULL
;
470 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
474 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
476 if (ubuffer
&& bufflen
) {
477 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
485 bio
->bi_bdev
= bdget_disk(disk
, 0);
491 if (meta_buffer
&& meta_len
) {
492 struct bio_integrity_payload
*bip
;
494 meta
= kmalloc(meta_len
, GFP_KERNEL
);
501 if (copy_from_user(meta
, meta_buffer
,
508 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
514 bip
->bip_iter
.bi_size
= meta_len
;
515 bip
->bip_iter
.bi_sector
= meta_seed
;
517 ret
= bio_integrity_add_page(bio
, virt_to_page(meta
),
518 meta_len
, offset_in_page(meta
));
519 if (ret
!= meta_len
) {
526 blk_execute_rq(req
->q
, disk
, req
, 0);
527 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
530 ret
= nvme_req(req
)->status
;
532 *result
= le32_to_cpu(nvme_req(req
)->result
.u32
);
533 if (meta
&& !ret
&& !write
) {
534 if (copy_to_user(meta_buffer
, meta
, meta_len
))
541 if (disk
&& bio
->bi_bdev
)
543 blk_rq_unmap_user(bio
);
546 blk_mq_free_request(req
);
550 int nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
551 void __user
*ubuffer
, unsigned bufflen
, u32
*result
,
554 return __nvme_submit_user_cmd(q
, cmd
, ubuffer
, bufflen
, NULL
, 0, 0,
558 static void nvme_keep_alive_end_io(struct request
*rq
, int error
)
560 struct nvme_ctrl
*ctrl
= rq
->end_io_data
;
562 blk_mq_free_request(rq
);
565 dev_err(ctrl
->device
,
566 "failed nvme_keep_alive_end_io error=%d\n", error
);
570 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
573 static int nvme_keep_alive(struct nvme_ctrl
*ctrl
)
575 struct nvme_command c
;
578 memset(&c
, 0, sizeof(c
));
579 c
.common
.opcode
= nvme_admin_keep_alive
;
581 rq
= nvme_alloc_request(ctrl
->admin_q
, &c
, BLK_MQ_REQ_RESERVED
,
586 rq
->timeout
= ctrl
->kato
* HZ
;
587 rq
->end_io_data
= ctrl
;
589 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 0, nvme_keep_alive_end_io
);
594 static void nvme_keep_alive_work(struct work_struct
*work
)
596 struct nvme_ctrl
*ctrl
= container_of(to_delayed_work(work
),
597 struct nvme_ctrl
, ka_work
);
599 if (nvme_keep_alive(ctrl
)) {
600 /* allocation failure, reset the controller */
601 dev_err(ctrl
->device
, "keep-alive failed\n");
602 ctrl
->ops
->reset_ctrl(ctrl
);
607 void nvme_start_keep_alive(struct nvme_ctrl
*ctrl
)
609 if (unlikely(ctrl
->kato
== 0))
612 INIT_DELAYED_WORK(&ctrl
->ka_work
, nvme_keep_alive_work
);
613 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
615 EXPORT_SYMBOL_GPL(nvme_start_keep_alive
);
617 void nvme_stop_keep_alive(struct nvme_ctrl
*ctrl
)
619 if (unlikely(ctrl
->kato
== 0))
622 cancel_delayed_work_sync(&ctrl
->ka_work
);
624 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive
);
626 int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
628 struct nvme_command c
= { };
631 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
632 c
.identify
.opcode
= nvme_admin_identify
;
633 c
.identify
.cns
= NVME_ID_CNS_CTRL
;
635 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
639 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
640 sizeof(struct nvme_id_ctrl
));
646 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
648 struct nvme_command c
= { };
650 c
.identify
.opcode
= nvme_admin_identify
;
651 c
.identify
.cns
= NVME_ID_CNS_NS_ACTIVE_LIST
;
652 c
.identify
.nsid
= cpu_to_le32(nsid
);
653 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
656 int nvme_identify_ns(struct nvme_ctrl
*dev
, unsigned nsid
,
657 struct nvme_id_ns
**id
)
659 struct nvme_command c
= { };
662 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
663 c
.identify
.opcode
= nvme_admin_identify
;
664 c
.identify
.nsid
= cpu_to_le32(nsid
);
665 c
.identify
.cns
= NVME_ID_CNS_NS
;
667 *id
= kmalloc(sizeof(struct nvme_id_ns
), GFP_KERNEL
);
671 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
672 sizeof(struct nvme_id_ns
));
678 int nvme_get_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned nsid
,
679 void *buffer
, size_t buflen
, u32
*result
)
681 struct nvme_command c
;
682 union nvme_result res
;
685 memset(&c
, 0, sizeof(c
));
686 c
.features
.opcode
= nvme_admin_get_features
;
687 c
.features
.nsid
= cpu_to_le32(nsid
);
688 c
.features
.fid
= cpu_to_le32(fid
);
690 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
, buffer
, buflen
, 0,
692 if (ret
>= 0 && result
)
693 *result
= le32_to_cpu(res
.u32
);
697 int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
698 void *buffer
, size_t buflen
, u32
*result
)
700 struct nvme_command c
;
701 union nvme_result res
;
704 memset(&c
, 0, sizeof(c
));
705 c
.features
.opcode
= nvme_admin_set_features
;
706 c
.features
.fid
= cpu_to_le32(fid
);
707 c
.features
.dword11
= cpu_to_le32(dword11
);
709 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
,
710 buffer
, buflen
, 0, NVME_QID_ANY
, 0, 0);
711 if (ret
>= 0 && result
)
712 *result
= le32_to_cpu(res
.u32
);
716 int nvme_get_log_page(struct nvme_ctrl
*dev
, struct nvme_smart_log
**log
)
718 struct nvme_command c
= { };
721 c
.common
.opcode
= nvme_admin_get_log_page
,
722 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF),
723 c
.common
.cdw10
[0] = cpu_to_le32(
724 (((sizeof(struct nvme_smart_log
) / 4) - 1) << 16) |
727 *log
= kmalloc(sizeof(struct nvme_smart_log
), GFP_KERNEL
);
731 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *log
,
732 sizeof(struct nvme_smart_log
));
738 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
740 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
742 int status
, nr_io_queues
;
744 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, NULL
, 0,
750 * Degraded controllers might return an error when setting the queue
751 * count. We still want to be able to bring them online and offer
752 * access to the admin queue, as that might be only way to fix them up.
755 dev_err(ctrl
->dev
, "Could not set queue count (%d)\n", status
);
758 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
759 *count
= min(*count
, nr_io_queues
);
764 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
766 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
768 struct nvme_user_io io
;
769 struct nvme_command c
;
770 unsigned length
, meta_len
;
771 void __user
*metadata
;
773 if (copy_from_user(&io
, uio
, sizeof(io
)))
781 case nvme_cmd_compare
:
787 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
788 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
789 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
794 } else if (meta_len
) {
795 if ((io
.metadata
& 3) || !io
.metadata
)
799 memset(&c
, 0, sizeof(c
));
800 c
.rw
.opcode
= io
.opcode
;
801 c
.rw
.flags
= io
.flags
;
802 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
803 c
.rw
.slba
= cpu_to_le64(io
.slba
);
804 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
805 c
.rw
.control
= cpu_to_le16(io
.control
);
806 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
807 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
808 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
809 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
811 return __nvme_submit_user_cmd(ns
->queue
, &c
,
812 (void __user
*)(uintptr_t)io
.addr
, length
,
813 metadata
, meta_len
, io
.slba
, NULL
, 0);
816 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
817 struct nvme_passthru_cmd __user
*ucmd
)
819 struct nvme_passthru_cmd cmd
;
820 struct nvme_command c
;
821 unsigned timeout
= 0;
824 if (!capable(CAP_SYS_ADMIN
))
826 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
831 memset(&c
, 0, sizeof(c
));
832 c
.common
.opcode
= cmd
.opcode
;
833 c
.common
.flags
= cmd
.flags
;
834 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
835 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
836 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
837 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
838 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
839 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
840 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
841 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
842 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
845 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
847 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
848 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
849 &cmd
.result
, timeout
);
851 if (put_user(cmd
.result
, &ucmd
->result
))
858 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
859 unsigned int cmd
, unsigned long arg
)
861 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
865 force_successful_syscall_return();
867 case NVME_IOCTL_ADMIN_CMD
:
868 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
869 case NVME_IOCTL_IO_CMD
:
870 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
871 case NVME_IOCTL_SUBMIT_IO
:
872 return nvme_submit_io(ns
, (void __user
*)arg
);
873 #ifdef CONFIG_BLK_DEV_NVME_SCSI
874 case SG_GET_VERSION_NUM
:
875 return nvme_sg_get_version_num((void __user
*)arg
);
877 return nvme_sg_io(ns
, (void __user
*)arg
);
882 return nvme_nvm_ioctl(ns
, cmd
, arg
);
884 if (is_sed_ioctl(cmd
))
885 return sed_ioctl(ns
->ctrl
->opal_dev
, cmd
,
886 (void __user
*) arg
);
892 static int nvme_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
893 unsigned int cmd
, unsigned long arg
)
899 return nvme_ioctl(bdev
, mode
, cmd
, arg
);
902 #define nvme_compat_ioctl NULL
905 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
907 return nvme_get_ns_from_disk(bdev
->bd_disk
) ? 0 : -ENXIO
;
910 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
912 struct nvme_ns
*ns
= disk
->private_data
;
914 module_put(ns
->ctrl
->ops
->module
);
918 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
920 /* some standard values */
922 geo
->sectors
= 1 << 5;
923 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
927 #ifdef CONFIG_BLK_DEV_INTEGRITY
928 static void nvme_prep_integrity(struct gendisk
*disk
, struct nvme_id_ns
*id
,
931 struct nvme_ns
*ns
= disk
->private_data
;
935 ns
->ms
= le16_to_cpu(id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ms
);
936 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
938 /* PI implementation requires metadata equal t10 pi tuple size */
939 if (ns
->ms
== sizeof(struct t10_pi_tuple
))
940 pi_type
= id
->dps
& NVME_NS_DPS_PI_MASK
;
942 if (blk_get_integrity(disk
) &&
943 (ns
->pi_type
!= pi_type
|| ns
->ms
!= old_ms
||
944 bs
!= queue_logical_block_size(disk
->queue
) ||
945 (ns
->ms
&& ns
->ext
)))
946 blk_integrity_unregister(disk
);
948 ns
->pi_type
= pi_type
;
951 static void nvme_init_integrity(struct nvme_ns
*ns
)
953 struct blk_integrity integrity
;
955 memset(&integrity
, 0, sizeof(integrity
));
956 switch (ns
->pi_type
) {
957 case NVME_NS_DPS_PI_TYPE3
:
958 integrity
.profile
= &t10_pi_type3_crc
;
959 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
960 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
962 case NVME_NS_DPS_PI_TYPE1
:
963 case NVME_NS_DPS_PI_TYPE2
:
964 integrity
.profile
= &t10_pi_type1_crc
;
965 integrity
.tag_size
= sizeof(u16
);
966 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
969 integrity
.profile
= NULL
;
972 integrity
.tuple_size
= ns
->ms
;
973 blk_integrity_register(ns
->disk
, &integrity
);
974 blk_queue_max_integrity_segments(ns
->queue
, 1);
977 static void nvme_prep_integrity(struct gendisk
*disk
, struct nvme_id_ns
*id
,
981 static void nvme_init_integrity(struct nvme_ns
*ns
)
984 #endif /* CONFIG_BLK_DEV_INTEGRITY */
986 static void nvme_config_discard(struct nvme_ns
*ns
)
988 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
989 u32 logical_block_size
= queue_logical_block_size(ns
->queue
);
991 BUILD_BUG_ON(PAGE_SIZE
/ sizeof(struct nvme_dsm_range
) <
992 NVME_DSM_MAX_RANGES
);
994 ns
->queue
->limits
.discard_alignment
= logical_block_size
;
995 ns
->queue
->limits
.discard_granularity
= logical_block_size
;
996 blk_queue_max_discard_sectors(ns
->queue
, UINT_MAX
);
997 blk_queue_max_discard_segments(ns
->queue
, NVME_DSM_MAX_RANGES
);
998 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, ns
->queue
);
1000 if (ctrl
->quirks
& NVME_QUIRK_DEALLOCATE_ZEROES
)
1001 blk_queue_max_write_zeroes_sectors(ns
->queue
, UINT_MAX
);
1004 static int nvme_revalidate_ns(struct nvme_ns
*ns
, struct nvme_id_ns
**id
)
1006 if (nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, id
)) {
1007 dev_warn(ns
->ctrl
->dev
, "%s: Identify failure\n", __func__
);
1011 if ((*id
)->ncap
== 0) {
1016 if (ns
->ctrl
->vs
>= NVME_VS(1, 1, 0))
1017 memcpy(ns
->eui
, (*id
)->eui64
, sizeof(ns
->eui
));
1018 if (ns
->ctrl
->vs
>= NVME_VS(1, 2, 0))
1019 memcpy(ns
->uuid
, (*id
)->nguid
, sizeof(ns
->uuid
));
1024 static void __nvme_revalidate_disk(struct gendisk
*disk
, struct nvme_id_ns
*id
)
1026 struct nvme_ns
*ns
= disk
->private_data
;
1030 * If identify namespace failed, use default 512 byte block size so
1031 * block layer can use before failing read/write for 0 capacity.
1033 ns
->lba_shift
= id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ds
;
1034 if (ns
->lba_shift
== 0)
1036 bs
= 1 << ns
->lba_shift
;
1038 blk_mq_freeze_queue(disk
->queue
);
1040 if (ns
->ctrl
->ops
->flags
& NVME_F_METADATA_SUPPORTED
)
1041 nvme_prep_integrity(disk
, id
, bs
);
1042 blk_queue_logical_block_size(ns
->queue
, bs
);
1043 if (ns
->ms
&& !blk_get_integrity(disk
) && !ns
->ext
)
1044 nvme_init_integrity(ns
);
1045 if (ns
->ms
&& !(ns
->ms
== 8 && ns
->pi_type
) && !blk_get_integrity(disk
))
1046 set_capacity(disk
, 0);
1048 set_capacity(disk
, le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9));
1050 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
1051 nvme_config_discard(ns
);
1052 blk_mq_unfreeze_queue(disk
->queue
);
1055 static int nvme_revalidate_disk(struct gendisk
*disk
)
1057 struct nvme_ns
*ns
= disk
->private_data
;
1058 struct nvme_id_ns
*id
= NULL
;
1061 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
1062 set_capacity(disk
, 0);
1066 ret
= nvme_revalidate_ns(ns
, &id
);
1070 __nvme_revalidate_disk(disk
, id
);
1076 static char nvme_pr_type(enum pr_type type
)
1079 case PR_WRITE_EXCLUSIVE
:
1081 case PR_EXCLUSIVE_ACCESS
:
1083 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
1085 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
1087 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
1089 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
1096 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
1097 u64 key
, u64 sa_key
, u8 op
)
1099 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1100 struct nvme_command c
;
1101 u8 data
[16] = { 0, };
1103 put_unaligned_le64(key
, &data
[0]);
1104 put_unaligned_le64(sa_key
, &data
[8]);
1106 memset(&c
, 0, sizeof(c
));
1107 c
.common
.opcode
= op
;
1108 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
1109 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
1111 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
1114 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
1115 u64
new, unsigned flags
)
1119 if (flags
& ~PR_FL_IGNORE_KEY
)
1122 cdw10
= old
? 2 : 0;
1123 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
1124 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
1125 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
1128 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
1129 enum pr_type type
, unsigned flags
)
1133 if (flags
& ~PR_FL_IGNORE_KEY
)
1136 cdw10
= nvme_pr_type(type
) << 8;
1137 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
1138 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
1141 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
1142 enum pr_type type
, bool abort
)
1144 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
1145 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
1148 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
1150 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
1151 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
1154 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1156 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
1157 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
1160 static const struct pr_ops nvme_pr_ops
= {
1161 .pr_register
= nvme_pr_register
,
1162 .pr_reserve
= nvme_pr_reserve
,
1163 .pr_release
= nvme_pr_release
,
1164 .pr_preempt
= nvme_pr_preempt
,
1165 .pr_clear
= nvme_pr_clear
,
1168 #ifdef CONFIG_BLK_SED_OPAL
1169 int nvme_sec_submit(void *data
, u16 spsp
, u8 secp
, void *buffer
, size_t len
,
1172 struct nvme_ctrl
*ctrl
= data
;
1173 struct nvme_command cmd
;
1175 memset(&cmd
, 0, sizeof(cmd
));
1177 cmd
.common
.opcode
= nvme_admin_security_send
;
1179 cmd
.common
.opcode
= nvme_admin_security_recv
;
1180 cmd
.common
.nsid
= 0;
1181 cmd
.common
.cdw10
[0] = cpu_to_le32(((u32
)secp
) << 24 | ((u32
)spsp
) << 8);
1182 cmd
.common
.cdw10
[1] = cpu_to_le32(len
);
1184 return __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, buffer
, len
,
1185 ADMIN_TIMEOUT
, NVME_QID_ANY
, 1, 0);
1187 EXPORT_SYMBOL_GPL(nvme_sec_submit
);
1188 #endif /* CONFIG_BLK_SED_OPAL */
1190 static const struct block_device_operations nvme_fops
= {
1191 .owner
= THIS_MODULE
,
1192 .ioctl
= nvme_ioctl
,
1193 .compat_ioctl
= nvme_compat_ioctl
,
1195 .release
= nvme_release
,
1196 .getgeo
= nvme_getgeo
,
1197 .revalidate_disk
= nvme_revalidate_disk
,
1198 .pr_ops
= &nvme_pr_ops
,
1201 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
1203 unsigned long timeout
=
1204 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
1205 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
1208 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1211 if ((csts
& NVME_CSTS_RDY
) == bit
)
1215 if (fatal_signal_pending(current
))
1217 if (time_after(jiffies
, timeout
)) {
1218 dev_err(ctrl
->device
,
1219 "Device not ready; aborting %s\n", enabled
?
1220 "initialisation" : "reset");
1229 * If the device has been passed off to us in an enabled state, just clear
1230 * the enabled bit. The spec says we should set the 'shutdown notification
1231 * bits', but doing so may cause the device to complete commands to the
1232 * admin queue ... and we don't know what memory that might be pointing at!
1234 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1238 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1239 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1241 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1245 if (ctrl
->quirks
& NVME_QUIRK_DELAY_BEFORE_CHK_RDY
)
1246 msleep(NVME_QUIRK_DELAY_AMOUNT
);
1248 return nvme_wait_ready(ctrl
, cap
, false);
1250 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1252 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1255 * Default to a 4K page size, with the intention to update this
1256 * path in the future to accomodate architectures with differing
1257 * kernel and IO page sizes.
1259 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1262 if (page_shift
< dev_page_min
) {
1263 dev_err(ctrl
->device
,
1264 "Minimum device page size %u too large for host (%u)\n",
1265 1 << dev_page_min
, 1 << page_shift
);
1269 ctrl
->page_size
= 1 << page_shift
;
1271 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1272 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1273 ctrl
->ctrl_config
|= NVME_CC_ARB_RR
| NVME_CC_SHN_NONE
;
1274 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1275 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1277 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1280 return nvme_wait_ready(ctrl
, cap
, true);
1282 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1284 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1286 unsigned long timeout
= SHUTDOWN_TIMEOUT
+ jiffies
;
1290 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1291 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1293 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1297 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1298 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1302 if (fatal_signal_pending(current
))
1304 if (time_after(jiffies
, timeout
)) {
1305 dev_err(ctrl
->device
,
1306 "Device shutdown incomplete; abort shutdown\n");
1313 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1315 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1316 struct request_queue
*q
)
1320 if (ctrl
->max_hw_sectors
) {
1322 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1324 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1325 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1327 if (ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
)
1328 blk_queue_chunk_sectors(q
, ctrl
->max_hw_sectors
);
1329 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1330 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1332 blk_queue_write_cache(q
, vwc
, vwc
);
1335 static void nvme_configure_apst(struct nvme_ctrl
*ctrl
)
1338 * APST (Autonomous Power State Transition) lets us program a
1339 * table of power state transitions that the controller will
1340 * perform automatically. We configure it with a simple
1341 * heuristic: we are willing to spend at most 2% of the time
1342 * transitioning between power states. Therefore, when running
1343 * in any given state, we will enter the next lower-power
1344 * non-operational state after waiting 50 * (enlat + exlat)
1345 * microseconds, as long as that state's exit latency is under
1346 * the requested maximum latency.
1348 * We will not autonomously enter any non-operational state for
1349 * which the total latency exceeds ps_max_latency_us. Users
1350 * can set ps_max_latency_us to zero to turn off APST.
1354 struct nvme_feat_auto_pst
*table
;
1360 * If APST isn't supported or if we haven't been initialized yet,
1361 * then don't do anything.
1366 if (ctrl
->npss
> 31) {
1367 dev_warn(ctrl
->device
, "NPSS is invalid; not using APST\n");
1371 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
1375 if (ctrl
->ps_max_latency_us
== 0) {
1376 /* Turn off APST. */
1378 dev_dbg(ctrl
->device
, "APST disabled\n");
1380 __le64 target
= cpu_to_le64(0);
1384 * Walk through all states from lowest- to highest-power.
1385 * According to the spec, lower-numbered states use more
1386 * power. NPSS, despite the name, is the index of the
1387 * lowest-power state, not the number of states.
1389 for (state
= (int)ctrl
->npss
; state
>= 0; state
--) {
1390 u64 total_latency_us
, exit_latency_us
, transition_ms
;
1393 table
->entries
[state
] = target
;
1396 * Don't allow transitions to the deepest state
1397 * if it's quirked off.
1399 if (state
== ctrl
->npss
&&
1400 (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
))
1404 * Is this state a useful non-operational state for
1405 * higher-power states to autonomously transition to?
1407 if (!(ctrl
->psd
[state
].flags
&
1408 NVME_PS_FLAGS_NON_OP_STATE
))
1412 (u64
)le32_to_cpu(ctrl
->psd
[state
].exit_lat
);
1413 if (exit_latency_us
> ctrl
->ps_max_latency_us
)
1418 le32_to_cpu(ctrl
->psd
[state
].entry_lat
);
1421 * This state is good. Use it as the APST idle
1422 * target for higher power states.
1424 transition_ms
= total_latency_us
+ 19;
1425 do_div(transition_ms
, 20);
1426 if (transition_ms
> (1 << 24) - 1)
1427 transition_ms
= (1 << 24) - 1;
1429 target
= cpu_to_le64((state
<< 3) |
1430 (transition_ms
<< 8));
1435 if (total_latency_us
> max_lat_us
)
1436 max_lat_us
= total_latency_us
;
1442 dev_dbg(ctrl
->device
, "APST enabled but no non-operational states are available\n");
1444 dev_dbg(ctrl
->device
, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1445 max_ps
, max_lat_us
, (int)sizeof(*table
), table
);
1449 ret
= nvme_set_features(ctrl
, NVME_FEAT_AUTO_PST
, apste
,
1450 table
, sizeof(*table
), NULL
);
1452 dev_err(ctrl
->device
, "failed to set APST feature (%d)\n", ret
);
1457 static void nvme_set_latency_tolerance(struct device
*dev
, s32 val
)
1459 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1463 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
:
1464 case PM_QOS_LATENCY_ANY
:
1472 if (ctrl
->ps_max_latency_us
!= latency
) {
1473 ctrl
->ps_max_latency_us
= latency
;
1474 nvme_configure_apst(ctrl
);
1478 struct nvme_core_quirk_entry
{
1480 * NVMe model and firmware strings are padded with spaces. For
1481 * simplicity, strings in the quirk table are padded with NULLs
1487 unsigned long quirks
;
1490 static const struct nvme_core_quirk_entry core_quirks
[] = {
1493 * This Toshiba device seems to die using any APST states. See:
1494 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1497 .mn
= "THNSF5256GPUK TOSHIBA",
1498 .quirks
= NVME_QUIRK_NO_APST
,
1502 /* match is null-terminated but idstr is space-padded. */
1503 static bool string_matches(const char *idstr
, const char *match
, size_t len
)
1510 matchlen
= strlen(match
);
1511 WARN_ON_ONCE(matchlen
> len
);
1513 if (memcmp(idstr
, match
, matchlen
))
1516 for (; matchlen
< len
; matchlen
++)
1517 if (idstr
[matchlen
] != ' ')
1523 static bool quirk_matches(const struct nvme_id_ctrl
*id
,
1524 const struct nvme_core_quirk_entry
*q
)
1526 return q
->vid
== le16_to_cpu(id
->vid
) &&
1527 string_matches(id
->mn
, q
->mn
, sizeof(id
->mn
)) &&
1528 string_matches(id
->fr
, q
->fr
, sizeof(id
->fr
));
1532 * Initialize the cached copies of the Identify data and various controller
1533 * register in our nvme_ctrl structure. This should be called as soon as
1534 * the admin queue is fully up and running.
1536 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1538 struct nvme_id_ctrl
*id
;
1540 int ret
, page_shift
;
1544 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1546 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1550 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1552 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1555 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1557 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1558 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1560 ret
= nvme_identify_ctrl(ctrl
, &id
);
1562 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1566 if (!ctrl
->identified
) {
1568 * Check for quirks. Quirk can depend on firmware version,
1569 * so, in principle, the set of quirks present can change
1570 * across a reset. As a possible future enhancement, we
1571 * could re-scan for quirks every time we reinitialize
1572 * the device, but we'd have to make sure that the driver
1573 * behaves intelligently if the quirks change.
1578 for (i
= 0; i
< ARRAY_SIZE(core_quirks
); i
++) {
1579 if (quirk_matches(id
, &core_quirks
[i
]))
1580 ctrl
->quirks
|= core_quirks
[i
].quirks
;
1584 if (force_apst
&& (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
)) {
1585 dev_warn(ctrl
->dev
, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
1586 ctrl
->quirks
&= ~NVME_QUIRK_NO_DEEPEST_PS
;
1589 ctrl
->oacs
= le16_to_cpu(id
->oacs
);
1590 ctrl
->vid
= le16_to_cpu(id
->vid
);
1591 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1592 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1593 ctrl
->vwc
= id
->vwc
;
1594 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1595 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1596 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1597 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1599 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1601 max_hw_sectors
= UINT_MAX
;
1602 ctrl
->max_hw_sectors
=
1603 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
1605 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1606 ctrl
->sgls
= le32_to_cpu(id
->sgls
);
1607 ctrl
->kas
= le16_to_cpu(id
->kas
);
1609 ctrl
->npss
= id
->npss
;
1610 prev_apsta
= ctrl
->apsta
;
1611 if (ctrl
->quirks
& NVME_QUIRK_NO_APST
) {
1612 if (force_apst
&& id
->apsta
) {
1613 dev_warn(ctrl
->dev
, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
1619 ctrl
->apsta
= id
->apsta
;
1621 memcpy(ctrl
->psd
, id
->psd
, sizeof(ctrl
->psd
));
1623 if (ctrl
->ops
->flags
& NVME_F_FABRICS
) {
1624 ctrl
->icdoff
= le16_to_cpu(id
->icdoff
);
1625 ctrl
->ioccsz
= le32_to_cpu(id
->ioccsz
);
1626 ctrl
->iorcsz
= le32_to_cpu(id
->iorcsz
);
1627 ctrl
->maxcmd
= le16_to_cpu(id
->maxcmd
);
1630 * In fabrics we need to verify the cntlid matches the
1633 if (ctrl
->cntlid
!= le16_to_cpu(id
->cntlid
))
1636 if (!ctrl
->opts
->discovery_nqn
&& !ctrl
->kas
) {
1638 "keep-alive support is mandatory for fabrics\n");
1642 ctrl
->cntlid
= le16_to_cpu(id
->cntlid
);
1647 if (ctrl
->apsta
&& !prev_apsta
)
1648 dev_pm_qos_expose_latency_tolerance(ctrl
->device
);
1649 else if (!ctrl
->apsta
&& prev_apsta
)
1650 dev_pm_qos_hide_latency_tolerance(ctrl
->device
);
1652 nvme_configure_apst(ctrl
);
1654 ctrl
->identified
= true;
1658 EXPORT_SYMBOL_GPL(nvme_init_identify
);
1660 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
1662 struct nvme_ctrl
*ctrl
;
1663 int instance
= iminor(inode
);
1666 spin_lock(&dev_list_lock
);
1667 list_for_each_entry(ctrl
, &nvme_ctrl_list
, node
) {
1668 if (ctrl
->instance
!= instance
)
1671 if (!ctrl
->admin_q
) {
1675 if (!kref_get_unless_zero(&ctrl
->kref
))
1677 file
->private_data
= ctrl
;
1681 spin_unlock(&dev_list_lock
);
1686 static int nvme_dev_release(struct inode
*inode
, struct file
*file
)
1688 nvme_put_ctrl(file
->private_data
);
1692 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
1697 mutex_lock(&ctrl
->namespaces_mutex
);
1698 if (list_empty(&ctrl
->namespaces
)) {
1703 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
1704 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
1705 dev_warn(ctrl
->device
,
1706 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1711 dev_warn(ctrl
->device
,
1712 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1713 kref_get(&ns
->kref
);
1714 mutex_unlock(&ctrl
->namespaces_mutex
);
1716 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
1721 mutex_unlock(&ctrl
->namespaces_mutex
);
1725 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
1728 struct nvme_ctrl
*ctrl
= file
->private_data
;
1729 void __user
*argp
= (void __user
*)arg
;
1732 case NVME_IOCTL_ADMIN_CMD
:
1733 return nvme_user_cmd(ctrl
, NULL
, argp
);
1734 case NVME_IOCTL_IO_CMD
:
1735 return nvme_dev_user_cmd(ctrl
, argp
);
1736 case NVME_IOCTL_RESET
:
1737 dev_warn(ctrl
->device
, "resetting controller\n");
1738 return ctrl
->ops
->reset_ctrl(ctrl
);
1739 case NVME_IOCTL_SUBSYS_RESET
:
1740 return nvme_reset_subsystem(ctrl
);
1741 case NVME_IOCTL_RESCAN
:
1742 nvme_queue_scan(ctrl
);
1749 static const struct file_operations nvme_dev_fops
= {
1750 .owner
= THIS_MODULE
,
1751 .open
= nvme_dev_open
,
1752 .release
= nvme_dev_release
,
1753 .unlocked_ioctl
= nvme_dev_ioctl
,
1754 .compat_ioctl
= nvme_dev_ioctl
,
1757 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
1758 struct device_attribute
*attr
, const char *buf
,
1761 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1764 ret
= ctrl
->ops
->reset_ctrl(ctrl
);
1769 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
1771 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
1772 struct device_attribute
*attr
, const char *buf
,
1775 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1777 nvme_queue_scan(ctrl
);
1780 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
1782 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
1785 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1786 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1787 int serial_len
= sizeof(ctrl
->serial
);
1788 int model_len
= sizeof(ctrl
->model
);
1790 if (memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1791 return sprintf(buf
, "eui.%16phN\n", ns
->uuid
);
1793 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1794 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
1796 while (ctrl
->serial
[serial_len
- 1] == ' ')
1798 while (ctrl
->model
[model_len
- 1] == ' ')
1801 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
1802 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
1804 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
1806 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
1809 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1810 return sprintf(buf
, "%pU\n", ns
->uuid
);
1812 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
1814 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
1817 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1818 return sprintf(buf
, "%8phd\n", ns
->eui
);
1820 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
1822 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
1825 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1826 return sprintf(buf
, "%d\n", ns
->ns_id
);
1828 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
1830 static struct attribute
*nvme_ns_attrs
[] = {
1831 &dev_attr_wwid
.attr
,
1832 &dev_attr_uuid
.attr
,
1834 &dev_attr_nsid
.attr
,
1838 static umode_t
nvme_ns_attrs_are_visible(struct kobject
*kobj
,
1839 struct attribute
*a
, int n
)
1841 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1842 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1844 if (a
== &dev_attr_uuid
.attr
) {
1845 if (!memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1848 if (a
== &dev_attr_eui
.attr
) {
1849 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1855 static const struct attribute_group nvme_ns_attr_group
= {
1856 .attrs
= nvme_ns_attrs
,
1857 .is_visible
= nvme_ns_attrs_are_visible
,
1860 #define nvme_show_str_function(field) \
1861 static ssize_t field##_show(struct device *dev, \
1862 struct device_attribute *attr, char *buf) \
1864 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1865 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1867 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1869 #define nvme_show_int_function(field) \
1870 static ssize_t field##_show(struct device *dev, \
1871 struct device_attribute *attr, char *buf) \
1873 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1874 return sprintf(buf, "%d\n", ctrl->field); \
1876 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1878 nvme_show_str_function(model
);
1879 nvme_show_str_function(serial
);
1880 nvme_show_str_function(firmware_rev
);
1881 nvme_show_int_function(cntlid
);
1883 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
1884 struct device_attribute
*attr
, const char *buf
,
1887 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1889 if (device_remove_file_self(dev
, attr
))
1890 ctrl
->ops
->delete_ctrl(ctrl
);
1893 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
1895 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
1896 struct device_attribute
*attr
,
1899 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1901 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
1903 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
1905 static ssize_t
nvme_sysfs_show_state(struct device
*dev
,
1906 struct device_attribute
*attr
,
1909 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1910 static const char *const state_name
[] = {
1911 [NVME_CTRL_NEW
] = "new",
1912 [NVME_CTRL_LIVE
] = "live",
1913 [NVME_CTRL_RESETTING
] = "resetting",
1914 [NVME_CTRL_RECONNECTING
]= "reconnecting",
1915 [NVME_CTRL_DELETING
] = "deleting",
1916 [NVME_CTRL_DEAD
] = "dead",
1919 if ((unsigned)ctrl
->state
< ARRAY_SIZE(state_name
) &&
1920 state_name
[ctrl
->state
])
1921 return sprintf(buf
, "%s\n", state_name
[ctrl
->state
]);
1923 return sprintf(buf
, "unknown state\n");
1926 static DEVICE_ATTR(state
, S_IRUGO
, nvme_sysfs_show_state
, NULL
);
1928 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
1929 struct device_attribute
*attr
,
1932 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1934 return snprintf(buf
, PAGE_SIZE
, "%s\n",
1935 ctrl
->ops
->get_subsysnqn(ctrl
));
1937 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
1939 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
1940 struct device_attribute
*attr
,
1943 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1945 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
1947 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
1949 static struct attribute
*nvme_dev_attrs
[] = {
1950 &dev_attr_reset_controller
.attr
,
1951 &dev_attr_rescan_controller
.attr
,
1952 &dev_attr_model
.attr
,
1953 &dev_attr_serial
.attr
,
1954 &dev_attr_firmware_rev
.attr
,
1955 &dev_attr_cntlid
.attr
,
1956 &dev_attr_delete_controller
.attr
,
1957 &dev_attr_transport
.attr
,
1958 &dev_attr_subsysnqn
.attr
,
1959 &dev_attr_address
.attr
,
1960 &dev_attr_state
.attr
,
1964 #define CHECK_ATTR(ctrl, a, name) \
1965 if ((a) == &dev_attr_##name.attr && \
1966 !(ctrl)->ops->get_##name) \
1969 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
1970 struct attribute
*a
, int n
)
1972 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1973 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1975 if (a
== &dev_attr_delete_controller
.attr
) {
1976 if (!ctrl
->ops
->delete_ctrl
)
1980 CHECK_ATTR(ctrl
, a
, subsysnqn
);
1981 CHECK_ATTR(ctrl
, a
, address
);
1986 static struct attribute_group nvme_dev_attrs_group
= {
1987 .attrs
= nvme_dev_attrs
,
1988 .is_visible
= nvme_dev_attrs_are_visible
,
1991 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
1992 &nvme_dev_attrs_group
,
1996 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1998 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
1999 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
2001 return nsa
->ns_id
- nsb
->ns_id
;
2004 static struct nvme_ns
*nvme_find_get_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2006 struct nvme_ns
*ns
, *ret
= NULL
;
2008 mutex_lock(&ctrl
->namespaces_mutex
);
2009 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2010 if (ns
->ns_id
== nsid
) {
2011 kref_get(&ns
->kref
);
2015 if (ns
->ns_id
> nsid
)
2018 mutex_unlock(&ctrl
->namespaces_mutex
);
2022 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2025 struct gendisk
*disk
;
2026 struct nvme_id_ns
*id
;
2027 char disk_name
[DISK_NAME_LEN
];
2028 int node
= dev_to_node(ctrl
->dev
);
2030 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
2034 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
2035 if (ns
->instance
< 0)
2038 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
2039 if (IS_ERR(ns
->queue
))
2040 goto out_release_instance
;
2041 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
2042 ns
->queue
->queuedata
= ns
;
2045 kref_init(&ns
->kref
);
2047 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
2049 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
2050 nvme_set_queue_limits(ctrl
, ns
->queue
);
2052 sprintf(disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
2054 if (nvme_revalidate_ns(ns
, &id
))
2055 goto out_free_queue
;
2057 if (nvme_nvm_ns_supported(ns
, id
) &&
2058 nvme_nvm_register(ns
, disk_name
, node
)) {
2059 dev_warn(ctrl
->dev
, "%s: LightNVM init failure\n", __func__
);
2063 disk
= alloc_disk_node(0, node
);
2067 disk
->fops
= &nvme_fops
;
2068 disk
->private_data
= ns
;
2069 disk
->queue
= ns
->queue
;
2070 disk
->flags
= GENHD_FL_EXT_DEVT
;
2071 memcpy(disk
->disk_name
, disk_name
, DISK_NAME_LEN
);
2074 __nvme_revalidate_disk(disk
, id
);
2076 mutex_lock(&ctrl
->namespaces_mutex
);
2077 list_add_tail(&ns
->list
, &ctrl
->namespaces
);
2078 mutex_unlock(&ctrl
->namespaces_mutex
);
2080 kref_get(&ctrl
->kref
);
2084 device_add_disk(ctrl
->device
, ns
->disk
);
2085 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
2086 &nvme_ns_attr_group
))
2087 pr_warn("%s: failed to create sysfs group for identification\n",
2088 ns
->disk
->disk_name
);
2089 if (ns
->ndev
&& nvme_nvm_register_sysfs(ns
))
2090 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2091 ns
->disk
->disk_name
);
2096 blk_cleanup_queue(ns
->queue
);
2097 out_release_instance
:
2098 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
2103 static void nvme_ns_remove(struct nvme_ns
*ns
)
2105 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
2108 if (ns
->disk
&& ns
->disk
->flags
& GENHD_FL_UP
) {
2109 if (blk_get_integrity(ns
->disk
))
2110 blk_integrity_unregister(ns
->disk
);
2111 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
2112 &nvme_ns_attr_group
);
2114 nvme_nvm_unregister_sysfs(ns
);
2115 del_gendisk(ns
->disk
);
2116 blk_cleanup_queue(ns
->queue
);
2119 mutex_lock(&ns
->ctrl
->namespaces_mutex
);
2120 list_del_init(&ns
->list
);
2121 mutex_unlock(&ns
->ctrl
->namespaces_mutex
);
2126 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2130 ns
= nvme_find_get_ns(ctrl
, nsid
);
2132 if (ns
->disk
&& revalidate_disk(ns
->disk
))
2136 nvme_alloc_ns(ctrl
, nsid
);
2139 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
2142 struct nvme_ns
*ns
, *next
;
2144 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
2145 if (ns
->ns_id
> nsid
)
2150 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
2154 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
2157 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
2161 for (i
= 0; i
< num_lists
; i
++) {
2162 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
2166 for (j
= 0; j
< min(nn
, 1024U); j
++) {
2167 nsid
= le32_to_cpu(ns_list
[j
]);
2171 nvme_validate_ns(ctrl
, nsid
);
2173 while (++prev
< nsid
) {
2174 ns
= nvme_find_get_ns(ctrl
, prev
);
2184 nvme_remove_invalid_namespaces(ctrl
, prev
);
2190 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
2194 for (i
= 1; i
<= nn
; i
++)
2195 nvme_validate_ns(ctrl
, i
);
2197 nvme_remove_invalid_namespaces(ctrl
, nn
);
2200 static void nvme_scan_work(struct work_struct
*work
)
2202 struct nvme_ctrl
*ctrl
=
2203 container_of(work
, struct nvme_ctrl
, scan_work
);
2204 struct nvme_id_ctrl
*id
;
2207 if (ctrl
->state
!= NVME_CTRL_LIVE
)
2210 if (nvme_identify_ctrl(ctrl
, &id
))
2213 nn
= le32_to_cpu(id
->nn
);
2214 if (ctrl
->vs
>= NVME_VS(1, 1, 0) &&
2215 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
2216 if (!nvme_scan_ns_list(ctrl
, nn
))
2219 nvme_scan_ns_sequential(ctrl
, nn
);
2221 mutex_lock(&ctrl
->namespaces_mutex
);
2222 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
2223 mutex_unlock(&ctrl
->namespaces_mutex
);
2227 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
2230 * Do not queue new scan work when a controller is reset during
2233 if (ctrl
->state
== NVME_CTRL_LIVE
)
2234 schedule_work(&ctrl
->scan_work
);
2236 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
2239 * This function iterates the namespace list unlocked to allow recovery from
2240 * controller failure. It is up to the caller to ensure the namespace list is
2241 * not modified by scan work while this function is executing.
2243 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
2245 struct nvme_ns
*ns
, *next
;
2248 * The dead states indicates the controller was not gracefully
2249 * disconnected. In that case, we won't be able to flush any data while
2250 * removing the namespaces' disks; fail all the queues now to avoid
2251 * potentially having to clean up the failed sync later.
2253 if (ctrl
->state
== NVME_CTRL_DEAD
)
2254 nvme_kill_queues(ctrl
);
2256 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
2259 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
2261 static void nvme_async_event_work(struct work_struct
*work
)
2263 struct nvme_ctrl
*ctrl
=
2264 container_of(work
, struct nvme_ctrl
, async_event_work
);
2266 spin_lock_irq(&ctrl
->lock
);
2267 while (ctrl
->event_limit
> 0) {
2268 int aer_idx
= --ctrl
->event_limit
;
2270 spin_unlock_irq(&ctrl
->lock
);
2271 ctrl
->ops
->submit_async_event(ctrl
, aer_idx
);
2272 spin_lock_irq(&ctrl
->lock
);
2274 spin_unlock_irq(&ctrl
->lock
);
2277 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
, __le16 status
,
2278 union nvme_result
*res
)
2280 u32 result
= le32_to_cpu(res
->u32
);
2283 switch (le16_to_cpu(status
) >> 1) {
2284 case NVME_SC_SUCCESS
:
2287 case NVME_SC_ABORT_REQ
:
2288 ++ctrl
->event_limit
;
2289 schedule_work(&ctrl
->async_event_work
);
2298 switch (result
& 0xff07) {
2299 case NVME_AER_NOTICE_NS_CHANGED
:
2300 dev_info(ctrl
->device
, "rescanning\n");
2301 nvme_queue_scan(ctrl
);
2304 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
2307 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
2309 void nvme_queue_async_events(struct nvme_ctrl
*ctrl
)
2311 ctrl
->event_limit
= NVME_NR_AERS
;
2312 schedule_work(&ctrl
->async_event_work
);
2314 EXPORT_SYMBOL_GPL(nvme_queue_async_events
);
2316 static DEFINE_IDA(nvme_instance_ida
);
2318 static int nvme_set_instance(struct nvme_ctrl
*ctrl
)
2320 int instance
, error
;
2323 if (!ida_pre_get(&nvme_instance_ida
, GFP_KERNEL
))
2326 spin_lock(&dev_list_lock
);
2327 error
= ida_get_new(&nvme_instance_ida
, &instance
);
2328 spin_unlock(&dev_list_lock
);
2329 } while (error
== -EAGAIN
);
2334 ctrl
->instance
= instance
;
2338 static void nvme_release_instance(struct nvme_ctrl
*ctrl
)
2340 spin_lock(&dev_list_lock
);
2341 ida_remove(&nvme_instance_ida
, ctrl
->instance
);
2342 spin_unlock(&dev_list_lock
);
2345 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
2347 flush_work(&ctrl
->async_event_work
);
2348 flush_work(&ctrl
->scan_work
);
2349 nvme_remove_namespaces(ctrl
);
2351 device_destroy(nvme_class
, MKDEV(nvme_char_major
, ctrl
->instance
));
2353 spin_lock(&dev_list_lock
);
2354 list_del(&ctrl
->node
);
2355 spin_unlock(&dev_list_lock
);
2357 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
2359 static void nvme_free_ctrl(struct kref
*kref
)
2361 struct nvme_ctrl
*ctrl
= container_of(kref
, struct nvme_ctrl
, kref
);
2363 put_device(ctrl
->device
);
2364 nvme_release_instance(ctrl
);
2365 ida_destroy(&ctrl
->ns_ida
);
2367 ctrl
->ops
->free_ctrl(ctrl
);
2370 void nvme_put_ctrl(struct nvme_ctrl
*ctrl
)
2372 kref_put(&ctrl
->kref
, nvme_free_ctrl
);
2374 EXPORT_SYMBOL_GPL(nvme_put_ctrl
);
2377 * Initialize a NVMe controller structures. This needs to be called during
2378 * earliest initialization so that we have the initialized structured around
2381 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
2382 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
2386 ctrl
->state
= NVME_CTRL_NEW
;
2387 spin_lock_init(&ctrl
->lock
);
2388 INIT_LIST_HEAD(&ctrl
->namespaces
);
2389 mutex_init(&ctrl
->namespaces_mutex
);
2390 kref_init(&ctrl
->kref
);
2393 ctrl
->quirks
= quirks
;
2394 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
2395 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
2397 ret
= nvme_set_instance(ctrl
);
2401 ctrl
->device
= device_create_with_groups(nvme_class
, ctrl
->dev
,
2402 MKDEV(nvme_char_major
, ctrl
->instance
),
2403 ctrl
, nvme_dev_attr_groups
,
2404 "nvme%d", ctrl
->instance
);
2405 if (IS_ERR(ctrl
->device
)) {
2406 ret
= PTR_ERR(ctrl
->device
);
2407 goto out_release_instance
;
2409 get_device(ctrl
->device
);
2410 ida_init(&ctrl
->ns_ida
);
2412 spin_lock(&dev_list_lock
);
2413 list_add_tail(&ctrl
->node
, &nvme_ctrl_list
);
2414 spin_unlock(&dev_list_lock
);
2417 * Initialize latency tolerance controls. The sysfs files won't
2418 * be visible to userspace unless the device actually supports APST.
2420 ctrl
->device
->power
.set_latency_tolerance
= nvme_set_latency_tolerance
;
2421 dev_pm_qos_update_user_latency_tolerance(ctrl
->device
,
2422 min(default_ps_max_latency_us
, (unsigned long)S32_MAX
));
2425 out_release_instance
:
2426 nvme_release_instance(ctrl
);
2430 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
2433 * nvme_kill_queues(): Ends all namespace queues
2434 * @ctrl: the dead controller that needs to end
2436 * Call this function when the driver determines it is unable to get the
2437 * controller in a state capable of servicing IO.
2439 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
2443 mutex_lock(&ctrl
->namespaces_mutex
);
2445 /* Forcibly start all queues to avoid having stuck requests */
2446 blk_mq_start_hw_queues(ctrl
->admin_q
);
2448 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2450 * Revalidating a dead namespace sets capacity to 0. This will
2451 * end buffered writers dirtying pages that can't be synced.
2453 if (!ns
->disk
|| test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
2455 revalidate_disk(ns
->disk
);
2456 blk_set_queue_dying(ns
->queue
);
2459 * Forcibly start all queues to avoid having stuck requests.
2460 * Note that we must ensure the queues are not stopped
2461 * when the final removal happens.
2463 blk_mq_start_hw_queues(ns
->queue
);
2465 /* draining requests in requeue list */
2466 blk_mq_kick_requeue_list(ns
->queue
);
2468 mutex_unlock(&ctrl
->namespaces_mutex
);
2470 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
2472 void nvme_unfreeze(struct nvme_ctrl
*ctrl
)
2476 mutex_lock(&ctrl
->namespaces_mutex
);
2477 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2478 blk_mq_unfreeze_queue(ns
->queue
);
2479 mutex_unlock(&ctrl
->namespaces_mutex
);
2481 EXPORT_SYMBOL_GPL(nvme_unfreeze
);
2483 void nvme_wait_freeze_timeout(struct nvme_ctrl
*ctrl
, long timeout
)
2487 mutex_lock(&ctrl
->namespaces_mutex
);
2488 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2489 timeout
= blk_mq_freeze_queue_wait_timeout(ns
->queue
, timeout
);
2493 mutex_unlock(&ctrl
->namespaces_mutex
);
2495 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout
);
2497 void nvme_wait_freeze(struct nvme_ctrl
*ctrl
)
2501 mutex_lock(&ctrl
->namespaces_mutex
);
2502 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2503 blk_mq_freeze_queue_wait(ns
->queue
);
2504 mutex_unlock(&ctrl
->namespaces_mutex
);
2506 EXPORT_SYMBOL_GPL(nvme_wait_freeze
);
2508 void nvme_start_freeze(struct nvme_ctrl
*ctrl
)
2512 mutex_lock(&ctrl
->namespaces_mutex
);
2513 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2514 blk_freeze_queue_start(ns
->queue
);
2515 mutex_unlock(&ctrl
->namespaces_mutex
);
2517 EXPORT_SYMBOL_GPL(nvme_start_freeze
);
2519 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
2523 mutex_lock(&ctrl
->namespaces_mutex
);
2524 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2525 blk_mq_quiesce_queue(ns
->queue
);
2526 mutex_unlock(&ctrl
->namespaces_mutex
);
2528 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
2530 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
2534 mutex_lock(&ctrl
->namespaces_mutex
);
2535 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2536 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
2537 blk_mq_kick_requeue_list(ns
->queue
);
2539 mutex_unlock(&ctrl
->namespaces_mutex
);
2541 EXPORT_SYMBOL_GPL(nvme_start_queues
);
2543 int __init
nvme_core_init(void)
2547 result
= __register_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme",
2551 else if (result
> 0)
2552 nvme_char_major
= result
;
2554 nvme_class
= class_create(THIS_MODULE
, "nvme");
2555 if (IS_ERR(nvme_class
)) {
2556 result
= PTR_ERR(nvme_class
);
2557 goto unregister_chrdev
;
2563 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2567 void nvme_core_exit(void)
2569 class_destroy(nvme_class
);
2570 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2573 MODULE_LICENSE("GPL");
2574 MODULE_VERSION("1.0");
2575 module_init(nvme_core_init
);
2576 module_exit(nvme_core_exit
);