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>
30 #include <asm/unaligned.h>
35 #define NVME_MINORS (1U << MINORBITS)
37 unsigned int admin_timeout
= 60;
38 module_param(admin_timeout
, uint
, 0644);
39 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
40 EXPORT_SYMBOL_GPL(admin_timeout
);
42 unsigned int nvme_io_timeout
= 30;
43 module_param_named(io_timeout
, nvme_io_timeout
, uint
, 0644);
44 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
45 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
47 static 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 static u8 nvme_max_retries
= 5;
52 module_param_named(max_retries
, nvme_max_retries
, byte
, 0644);
53 MODULE_PARM_DESC(max_retries
, "max number of retries a command may have");
55 static unsigned long default_ps_max_latency_us
= 100000;
56 module_param(default_ps_max_latency_us
, ulong
, 0644);
57 MODULE_PARM_DESC(default_ps_max_latency_us
,
58 "max power saving latency for new devices; use PM QOS to change per device");
60 static bool force_apst
;
61 module_param(force_apst
, bool, 0644);
62 MODULE_PARM_DESC(force_apst
, "allow APST for newly enumerated devices even if quirked off");
65 module_param(streams
, bool, 0644);
66 MODULE_PARM_DESC(streams
, "turn on support for Streams write directives");
68 struct workqueue_struct
*nvme_wq
;
69 EXPORT_SYMBOL_GPL(nvme_wq
);
71 static DEFINE_IDA(nvme_instance_ida
);
72 static dev_t nvme_chr_devt
;
73 static struct class *nvme_class
;
75 static void nvme_ns_remove(struct nvme_ns
*ns
);
76 static int nvme_revalidate_disk(struct gendisk
*disk
);
78 static __le32
nvme_get_log_dw10(u8 lid
, size_t size
)
80 return cpu_to_le32((((size
/ 4) - 1) << 16) | lid
);
83 int nvme_reset_ctrl(struct nvme_ctrl
*ctrl
)
85 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_RESETTING
))
87 if (!queue_work(nvme_wq
, &ctrl
->reset_work
))
91 EXPORT_SYMBOL_GPL(nvme_reset_ctrl
);
93 static int nvme_reset_ctrl_sync(struct nvme_ctrl
*ctrl
)
97 ret
= nvme_reset_ctrl(ctrl
);
99 flush_work(&ctrl
->reset_work
);
103 static void nvme_delete_ctrl_work(struct work_struct
*work
)
105 struct nvme_ctrl
*ctrl
=
106 container_of(work
, struct nvme_ctrl
, delete_work
);
108 flush_work(&ctrl
->reset_work
);
109 nvme_stop_ctrl(ctrl
);
110 nvme_remove_namespaces(ctrl
);
111 ctrl
->ops
->delete_ctrl(ctrl
);
112 nvme_uninit_ctrl(ctrl
);
116 int nvme_delete_ctrl(struct nvme_ctrl
*ctrl
)
118 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_DELETING
))
120 if (!queue_work(nvme_wq
, &ctrl
->delete_work
))
124 EXPORT_SYMBOL_GPL(nvme_delete_ctrl
);
126 int nvme_delete_ctrl_sync(struct nvme_ctrl
*ctrl
)
131 * Keep a reference until the work is flushed since ->delete_ctrl
132 * can free the controller.
135 ret
= nvme_delete_ctrl(ctrl
);
137 flush_work(&ctrl
->delete_work
);
141 EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync
);
143 static inline bool nvme_ns_has_pi(struct nvme_ns
*ns
)
145 return ns
->pi_type
&& ns
->ms
== sizeof(struct t10_pi_tuple
);
148 static blk_status_t
nvme_error_status(struct request
*req
)
150 switch (nvme_req(req
)->status
& 0x7ff) {
151 case NVME_SC_SUCCESS
:
153 case NVME_SC_CAP_EXCEEDED
:
154 return BLK_STS_NOSPC
;
155 case NVME_SC_ONCS_NOT_SUPPORTED
:
156 return BLK_STS_NOTSUPP
;
157 case NVME_SC_WRITE_FAULT
:
158 case NVME_SC_READ_ERROR
:
159 case NVME_SC_UNWRITTEN_BLOCK
:
160 case NVME_SC_ACCESS_DENIED
:
161 case NVME_SC_READ_ONLY
:
162 return BLK_STS_MEDIUM
;
163 case NVME_SC_GUARD_CHECK
:
164 case NVME_SC_APPTAG_CHECK
:
165 case NVME_SC_REFTAG_CHECK
:
166 case NVME_SC_INVALID_PI
:
167 return BLK_STS_PROTECTION
;
168 case NVME_SC_RESERVATION_CONFLICT
:
169 return BLK_STS_NEXUS
;
171 return BLK_STS_IOERR
;
175 static inline bool nvme_req_needs_retry(struct request
*req
)
177 if (blk_noretry_request(req
))
179 if (nvme_req(req
)->status
& NVME_SC_DNR
)
181 if (nvme_req(req
)->retries
>= nvme_max_retries
)
183 if (blk_queue_dying(req
->q
))
188 void nvme_complete_rq(struct request
*req
)
190 if (unlikely(nvme_req(req
)->status
&& nvme_req_needs_retry(req
))) {
191 nvme_req(req
)->retries
++;
192 blk_mq_requeue_request(req
, true);
196 blk_mq_end_request(req
, nvme_error_status(req
));
198 EXPORT_SYMBOL_GPL(nvme_complete_rq
);
200 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
202 if (!blk_mq_request_started(req
))
205 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
206 "Cancelling I/O %d", req
->tag
);
208 nvme_req(req
)->status
= NVME_SC_ABORT_REQ
;
209 blk_mq_complete_request(req
);
212 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
214 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
215 enum nvme_ctrl_state new_state
)
217 enum nvme_ctrl_state old_state
;
219 bool changed
= false;
221 spin_lock_irqsave(&ctrl
->lock
, flags
);
223 old_state
= ctrl
->state
;
228 case NVME_CTRL_RESETTING
:
229 case NVME_CTRL_RECONNECTING
:
236 case NVME_CTRL_RESETTING
:
246 case NVME_CTRL_RECONNECTING
:
249 case NVME_CTRL_RESETTING
:
256 case NVME_CTRL_DELETING
:
259 case NVME_CTRL_RESETTING
:
260 case NVME_CTRL_RECONNECTING
:
269 case NVME_CTRL_DELETING
:
281 ctrl
->state
= new_state
;
283 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
287 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
289 static void nvme_free_ns(struct kref
*kref
)
291 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
294 nvme_nvm_unregister(ns
);
297 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
298 nvme_put_ctrl(ns
->ctrl
);
302 static void nvme_put_ns(struct nvme_ns
*ns
)
304 kref_put(&ns
->kref
, nvme_free_ns
);
307 struct request
*nvme_alloc_request(struct request_queue
*q
,
308 struct nvme_command
*cmd
, unsigned int flags
, int qid
)
310 unsigned op
= nvme_is_write(cmd
) ? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
;
313 if (qid
== NVME_QID_ANY
) {
314 req
= blk_mq_alloc_request(q
, op
, flags
);
316 req
= blk_mq_alloc_request_hctx(q
, op
, flags
,
322 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
323 nvme_req(req
)->cmd
= cmd
;
327 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
329 static int nvme_toggle_streams(struct nvme_ctrl
*ctrl
, bool enable
)
331 struct nvme_command c
;
333 memset(&c
, 0, sizeof(c
));
335 c
.directive
.opcode
= nvme_admin_directive_send
;
336 c
.directive
.nsid
= cpu_to_le32(NVME_NSID_ALL
);
337 c
.directive
.doper
= NVME_DIR_SND_ID_OP_ENABLE
;
338 c
.directive
.dtype
= NVME_DIR_IDENTIFY
;
339 c
.directive
.tdtype
= NVME_DIR_STREAMS
;
340 c
.directive
.endir
= enable
? NVME_DIR_ENDIR
: 0;
342 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, NULL
, 0);
345 static int nvme_disable_streams(struct nvme_ctrl
*ctrl
)
347 return nvme_toggle_streams(ctrl
, false);
350 static int nvme_enable_streams(struct nvme_ctrl
*ctrl
)
352 return nvme_toggle_streams(ctrl
, true);
355 static int nvme_get_stream_params(struct nvme_ctrl
*ctrl
,
356 struct streams_directive_params
*s
, u32 nsid
)
358 struct nvme_command c
;
360 memset(&c
, 0, sizeof(c
));
361 memset(s
, 0, sizeof(*s
));
363 c
.directive
.opcode
= nvme_admin_directive_recv
;
364 c
.directive
.nsid
= cpu_to_le32(nsid
);
365 c
.directive
.numd
= cpu_to_le32((sizeof(*s
) >> 2) - 1);
366 c
.directive
.doper
= NVME_DIR_RCV_ST_OP_PARAM
;
367 c
.directive
.dtype
= NVME_DIR_STREAMS
;
369 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, s
, sizeof(*s
));
372 static int nvme_configure_directives(struct nvme_ctrl
*ctrl
)
374 struct streams_directive_params s
;
377 if (!(ctrl
->oacs
& NVME_CTRL_OACS_DIRECTIVES
))
382 ret
= nvme_enable_streams(ctrl
);
386 ret
= nvme_get_stream_params(ctrl
, &s
, NVME_NSID_ALL
);
390 ctrl
->nssa
= le16_to_cpu(s
.nssa
);
391 if (ctrl
->nssa
< BLK_MAX_WRITE_HINTS
- 1) {
392 dev_info(ctrl
->device
, "too few streams (%u) available\n",
394 nvme_disable_streams(ctrl
);
398 ctrl
->nr_streams
= min_t(unsigned, ctrl
->nssa
, BLK_MAX_WRITE_HINTS
- 1);
399 dev_info(ctrl
->device
, "Using %u streams\n", ctrl
->nr_streams
);
404 * Check if 'req' has a write hint associated with it. If it does, assign
405 * a valid namespace stream to the write.
407 static void nvme_assign_write_stream(struct nvme_ctrl
*ctrl
,
408 struct request
*req
, u16
*control
,
411 enum rw_hint streamid
= req
->write_hint
;
413 if (streamid
== WRITE_LIFE_NOT_SET
|| streamid
== WRITE_LIFE_NONE
)
417 if (WARN_ON_ONCE(streamid
> ctrl
->nr_streams
))
420 *control
|= NVME_RW_DTYPE_STREAMS
;
421 *dsmgmt
|= streamid
<< 16;
424 if (streamid
< ARRAY_SIZE(req
->q
->write_hints
))
425 req
->q
->write_hints
[streamid
] += blk_rq_bytes(req
) >> 9;
428 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
429 struct nvme_command
*cmnd
)
431 memset(cmnd
, 0, sizeof(*cmnd
));
432 cmnd
->common
.opcode
= nvme_cmd_flush
;
433 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
436 static blk_status_t
nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
437 struct nvme_command
*cmnd
)
439 unsigned short segments
= blk_rq_nr_discard_segments(req
), n
= 0;
440 struct nvme_dsm_range
*range
;
443 range
= kmalloc_array(segments
, sizeof(*range
), GFP_ATOMIC
);
445 return BLK_STS_RESOURCE
;
447 __rq_for_each_bio(bio
, req
) {
448 u64 slba
= nvme_block_nr(ns
, bio
->bi_iter
.bi_sector
);
449 u32 nlb
= bio
->bi_iter
.bi_size
>> ns
->lba_shift
;
451 range
[n
].cattr
= cpu_to_le32(0);
452 range
[n
].nlb
= cpu_to_le32(nlb
);
453 range
[n
].slba
= cpu_to_le64(slba
);
457 if (WARN_ON_ONCE(n
!= segments
)) {
459 return BLK_STS_IOERR
;
462 memset(cmnd
, 0, sizeof(*cmnd
));
463 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
464 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
465 cmnd
->dsm
.nr
= cpu_to_le32(segments
- 1);
466 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
468 req
->special_vec
.bv_page
= virt_to_page(range
);
469 req
->special_vec
.bv_offset
= offset_in_page(range
);
470 req
->special_vec
.bv_len
= sizeof(*range
) * segments
;
471 req
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
476 static inline blk_status_t
nvme_setup_rw(struct nvme_ns
*ns
,
477 struct request
*req
, struct nvme_command
*cmnd
)
479 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
483 if (req
->cmd_flags
& REQ_FUA
)
484 control
|= NVME_RW_FUA
;
485 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
486 control
|= NVME_RW_LR
;
488 if (req
->cmd_flags
& REQ_RAHEAD
)
489 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
491 memset(cmnd
, 0, sizeof(*cmnd
));
492 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
493 cmnd
->rw
.nsid
= cpu_to_le32(ns
->ns_id
);
494 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
495 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
497 if (req_op(req
) == REQ_OP_WRITE
&& ctrl
->nr_streams
)
498 nvme_assign_write_stream(ctrl
, req
, &control
, &dsmgmt
);
502 * If formated with metadata, the block layer always provides a
503 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
504 * we enable the PRACT bit for protection information or set the
505 * namespace capacity to zero to prevent any I/O.
507 if (!blk_integrity_rq(req
)) {
508 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns
)))
509 return BLK_STS_NOTSUPP
;
510 control
|= NVME_RW_PRINFO_PRACT
;
513 switch (ns
->pi_type
) {
514 case NVME_NS_DPS_PI_TYPE3
:
515 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
517 case NVME_NS_DPS_PI_TYPE1
:
518 case NVME_NS_DPS_PI_TYPE2
:
519 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
520 NVME_RW_PRINFO_PRCHK_REF
;
521 cmnd
->rw
.reftag
= cpu_to_le32(
522 nvme_block_nr(ns
, blk_rq_pos(req
)));
527 cmnd
->rw
.control
= cpu_to_le16(control
);
528 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
532 blk_status_t
nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
533 struct nvme_command
*cmd
)
535 blk_status_t ret
= BLK_STS_OK
;
537 if (!(req
->rq_flags
& RQF_DONTPREP
)) {
538 nvme_req(req
)->retries
= 0;
539 nvme_req(req
)->flags
= 0;
540 req
->rq_flags
|= RQF_DONTPREP
;
543 switch (req_op(req
)) {
546 memcpy(cmd
, nvme_req(req
)->cmd
, sizeof(*cmd
));
549 nvme_setup_flush(ns
, cmd
);
551 case REQ_OP_WRITE_ZEROES
:
552 /* currently only aliased to deallocate for a few ctrls: */
554 ret
= nvme_setup_discard(ns
, req
, cmd
);
558 ret
= nvme_setup_rw(ns
, req
, cmd
);
562 return BLK_STS_IOERR
;
565 cmd
->common
.command_id
= req
->tag
;
568 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
571 * Returns 0 on success. If the result is negative, it's a Linux error code;
572 * if the result is positive, it's an NVM Express status code
574 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
575 union nvme_result
*result
, void *buffer
, unsigned bufflen
,
576 unsigned timeout
, int qid
, int at_head
, int flags
)
581 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
585 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
587 if (buffer
&& bufflen
) {
588 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
593 blk_execute_rq(req
->q
, NULL
, req
, at_head
);
595 *result
= nvme_req(req
)->result
;
596 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
599 ret
= nvme_req(req
)->status
;
601 blk_mq_free_request(req
);
604 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
606 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
607 void *buffer
, unsigned bufflen
)
609 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
612 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
614 static void *nvme_add_user_metadata(struct bio
*bio
, void __user
*ubuf
,
615 unsigned len
, u32 seed
, bool write
)
617 struct bio_integrity_payload
*bip
;
621 buf
= kmalloc(len
, GFP_KERNEL
);
626 if (write
&& copy_from_user(buf
, ubuf
, len
))
629 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
635 bip
->bip_iter
.bi_size
= len
;
636 bip
->bip_iter
.bi_sector
= seed
;
637 ret
= bio_integrity_add_page(bio
, virt_to_page(buf
), len
,
638 offset_in_page(buf
));
648 static int nvme_submit_user_cmd(struct request_queue
*q
,
649 struct nvme_command
*cmd
, void __user
*ubuffer
,
650 unsigned bufflen
, void __user
*meta_buffer
, unsigned meta_len
,
651 u32 meta_seed
, u32
*result
, unsigned timeout
)
653 bool write
= nvme_is_write(cmd
);
654 struct nvme_ns
*ns
= q
->queuedata
;
655 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
657 struct bio
*bio
= NULL
;
661 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
665 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
667 if (ubuffer
&& bufflen
) {
668 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
674 if (disk
&& meta_buffer
&& meta_len
) {
675 meta
= nvme_add_user_metadata(bio
, meta_buffer
, meta_len
,
684 blk_execute_rq(req
->q
, disk
, req
, 0);
685 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
688 ret
= nvme_req(req
)->status
;
690 *result
= le32_to_cpu(nvme_req(req
)->result
.u32
);
691 if (meta
&& !ret
&& !write
) {
692 if (copy_to_user(meta_buffer
, meta
, meta_len
))
698 blk_rq_unmap_user(bio
);
700 blk_mq_free_request(req
);
704 static void nvme_keep_alive_end_io(struct request
*rq
, blk_status_t status
)
706 struct nvme_ctrl
*ctrl
= rq
->end_io_data
;
708 blk_mq_free_request(rq
);
711 dev_err(ctrl
->device
,
712 "failed nvme_keep_alive_end_io error=%d\n",
717 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
720 static int nvme_keep_alive(struct nvme_ctrl
*ctrl
)
722 struct nvme_command c
;
725 memset(&c
, 0, sizeof(c
));
726 c
.common
.opcode
= nvme_admin_keep_alive
;
728 rq
= nvme_alloc_request(ctrl
->admin_q
, &c
, BLK_MQ_REQ_RESERVED
,
733 rq
->timeout
= ctrl
->kato
* HZ
;
734 rq
->end_io_data
= ctrl
;
736 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 0, nvme_keep_alive_end_io
);
741 static void nvme_keep_alive_work(struct work_struct
*work
)
743 struct nvme_ctrl
*ctrl
= container_of(to_delayed_work(work
),
744 struct nvme_ctrl
, ka_work
);
746 if (nvme_keep_alive(ctrl
)) {
747 /* allocation failure, reset the controller */
748 dev_err(ctrl
->device
, "keep-alive failed\n");
749 nvme_reset_ctrl(ctrl
);
754 void nvme_start_keep_alive(struct nvme_ctrl
*ctrl
)
756 if (unlikely(ctrl
->kato
== 0))
759 INIT_DELAYED_WORK(&ctrl
->ka_work
, nvme_keep_alive_work
);
760 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
762 EXPORT_SYMBOL_GPL(nvme_start_keep_alive
);
764 void nvme_stop_keep_alive(struct nvme_ctrl
*ctrl
)
766 if (unlikely(ctrl
->kato
== 0))
769 cancel_delayed_work_sync(&ctrl
->ka_work
);
771 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive
);
773 static int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
775 struct nvme_command c
= { };
778 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
779 c
.identify
.opcode
= nvme_admin_identify
;
780 c
.identify
.cns
= NVME_ID_CNS_CTRL
;
782 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
786 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
787 sizeof(struct nvme_id_ctrl
));
793 static int nvme_identify_ns_descs(struct nvme_ctrl
*ctrl
, unsigned nsid
,
794 u8
*eui64
, u8
*nguid
, uuid_t
*uuid
)
796 struct nvme_command c
= { };
802 c
.identify
.opcode
= nvme_admin_identify
;
803 c
.identify
.nsid
= cpu_to_le32(nsid
);
804 c
.identify
.cns
= NVME_ID_CNS_NS_DESC_LIST
;
806 data
= kzalloc(NVME_IDENTIFY_DATA_SIZE
, GFP_KERNEL
);
810 status
= nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, data
,
811 NVME_IDENTIFY_DATA_SIZE
);
815 for (pos
= 0; pos
< NVME_IDENTIFY_DATA_SIZE
; pos
+= len
) {
816 struct nvme_ns_id_desc
*cur
= data
+ pos
;
822 case NVME_NIDT_EUI64
:
823 if (cur
->nidl
!= NVME_NIDT_EUI64_LEN
) {
824 dev_warn(ctrl
->device
,
825 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
829 len
= NVME_NIDT_EUI64_LEN
;
830 memcpy(eui64
, data
+ pos
+ sizeof(*cur
), len
);
832 case NVME_NIDT_NGUID
:
833 if (cur
->nidl
!= NVME_NIDT_NGUID_LEN
) {
834 dev_warn(ctrl
->device
,
835 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
839 len
= NVME_NIDT_NGUID_LEN
;
840 memcpy(nguid
, data
+ pos
+ sizeof(*cur
), len
);
843 if (cur
->nidl
!= NVME_NIDT_UUID_LEN
) {
844 dev_warn(ctrl
->device
,
845 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
849 len
= NVME_NIDT_UUID_LEN
;
850 uuid_copy(uuid
, data
+ pos
+ sizeof(*cur
));
853 /* Skip unnkown types */
865 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
867 struct nvme_command c
= { };
869 c
.identify
.opcode
= nvme_admin_identify
;
870 c
.identify
.cns
= NVME_ID_CNS_NS_ACTIVE_LIST
;
871 c
.identify
.nsid
= cpu_to_le32(nsid
);
872 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
875 static struct nvme_id_ns
*nvme_identify_ns(struct nvme_ctrl
*ctrl
,
878 struct nvme_id_ns
*id
;
879 struct nvme_command c
= { };
882 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
883 c
.identify
.opcode
= nvme_admin_identify
;
884 c
.identify
.nsid
= cpu_to_le32(nsid
);
885 c
.identify
.cns
= NVME_ID_CNS_NS
;
887 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
891 error
= nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, id
, sizeof(*id
));
893 dev_warn(ctrl
->device
, "Identify namespace failed\n");
901 static int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
902 void *buffer
, size_t buflen
, u32
*result
)
904 struct nvme_command c
;
905 union nvme_result res
;
908 memset(&c
, 0, sizeof(c
));
909 c
.features
.opcode
= nvme_admin_set_features
;
910 c
.features
.fid
= cpu_to_le32(fid
);
911 c
.features
.dword11
= cpu_to_le32(dword11
);
913 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
,
914 buffer
, buflen
, 0, NVME_QID_ANY
, 0, 0);
915 if (ret
>= 0 && result
)
916 *result
= le32_to_cpu(res
.u32
);
920 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
922 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
924 int status
, nr_io_queues
;
926 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, NULL
, 0,
932 * Degraded controllers might return an error when setting the queue
933 * count. We still want to be able to bring them online and offer
934 * access to the admin queue, as that might be only way to fix them up.
937 dev_err(ctrl
->device
, "Could not set queue count (%d)\n", status
);
940 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
941 *count
= min(*count
, nr_io_queues
);
946 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
948 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
950 struct nvme_user_io io
;
951 struct nvme_command c
;
952 unsigned length
, meta_len
;
953 void __user
*metadata
;
955 if (copy_from_user(&io
, uio
, sizeof(io
)))
963 case nvme_cmd_compare
:
969 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
970 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
971 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
976 } else if (meta_len
) {
977 if ((io
.metadata
& 3) || !io
.metadata
)
981 memset(&c
, 0, sizeof(c
));
982 c
.rw
.opcode
= io
.opcode
;
983 c
.rw
.flags
= io
.flags
;
984 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
985 c
.rw
.slba
= cpu_to_le64(io
.slba
);
986 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
987 c
.rw
.control
= cpu_to_le16(io
.control
);
988 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
989 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
990 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
991 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
993 return nvme_submit_user_cmd(ns
->queue
, &c
,
994 (void __user
*)(uintptr_t)io
.addr
, length
,
995 metadata
, meta_len
, io
.slba
, NULL
, 0);
998 static u32
nvme_known_admin_effects(u8 opcode
)
1001 case nvme_admin_format_nvm
:
1002 return NVME_CMD_EFFECTS_CSUPP
| NVME_CMD_EFFECTS_LBCC
|
1003 NVME_CMD_EFFECTS_CSE_MASK
;
1004 case nvme_admin_sanitize_nvm
:
1005 return NVME_CMD_EFFECTS_CSE_MASK
;
1012 static u32
nvme_passthru_start(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
1019 effects
= le32_to_cpu(ctrl
->effects
->iocs
[opcode
]);
1020 if (effects
& ~NVME_CMD_EFFECTS_CSUPP
)
1021 dev_warn(ctrl
->device
,
1022 "IO command:%02x has unhandled effects:%08x\n",
1028 effects
= le32_to_cpu(ctrl
->effects
->iocs
[opcode
]);
1030 effects
= nvme_known_admin_effects(opcode
);
1033 * For simplicity, IO to all namespaces is quiesced even if the command
1034 * effects say only one namespace is affected.
1036 if (effects
& (NVME_CMD_EFFECTS_LBCC
| NVME_CMD_EFFECTS_CSE_MASK
)) {
1037 nvme_start_freeze(ctrl
);
1038 nvme_wait_freeze(ctrl
);
1043 static void nvme_update_formats(struct nvme_ctrl
*ctrl
)
1047 mutex_lock(&ctrl
->namespaces_mutex
);
1048 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
1049 if (ns
->disk
&& nvme_revalidate_disk(ns
->disk
))
1052 mutex_unlock(&ctrl
->namespaces_mutex
);
1055 static void nvme_passthru_end(struct nvme_ctrl
*ctrl
, u32 effects
)
1058 * Revalidate LBA changes prior to unfreezing. This is necessary to
1059 * prevent memory corruption if a logical block size was changed by
1062 if (effects
& NVME_CMD_EFFECTS_LBCC
)
1063 nvme_update_formats(ctrl
);
1064 if (effects
& (NVME_CMD_EFFECTS_LBCC
| NVME_CMD_EFFECTS_CSE_MASK
))
1065 nvme_unfreeze(ctrl
);
1066 if (effects
& NVME_CMD_EFFECTS_CCC
)
1067 nvme_init_identify(ctrl
);
1068 if (effects
& (NVME_CMD_EFFECTS_NIC
| NVME_CMD_EFFECTS_NCC
))
1069 nvme_queue_scan(ctrl
);
1072 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
1073 struct nvme_passthru_cmd __user
*ucmd
)
1075 struct nvme_passthru_cmd cmd
;
1076 struct nvme_command c
;
1077 unsigned timeout
= 0;
1081 if (!capable(CAP_SYS_ADMIN
))
1083 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
1088 memset(&c
, 0, sizeof(c
));
1089 c
.common
.opcode
= cmd
.opcode
;
1090 c
.common
.flags
= cmd
.flags
;
1091 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
1092 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
1093 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
1094 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
1095 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
1096 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
1097 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
1098 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
1099 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
1102 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
1104 effects
= nvme_passthru_start(ctrl
, ns
, cmd
.opcode
);
1105 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
1106 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
1107 (void __user
*)(uintptr_t)cmd
.metadata
, cmd
.metadata
,
1108 0, &cmd
.result
, timeout
);
1109 nvme_passthru_end(ctrl
, effects
);
1112 if (put_user(cmd
.result
, &ucmd
->result
))
1119 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
1120 unsigned int cmd
, unsigned long arg
)
1122 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1126 force_successful_syscall_return();
1128 case NVME_IOCTL_ADMIN_CMD
:
1129 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
1130 case NVME_IOCTL_IO_CMD
:
1131 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
1132 case NVME_IOCTL_SUBMIT_IO
:
1133 return nvme_submit_io(ns
, (void __user
*)arg
);
1137 return nvme_nvm_ioctl(ns
, cmd
, arg
);
1139 if (is_sed_ioctl(cmd
))
1140 return sed_ioctl(ns
->ctrl
->opal_dev
, cmd
,
1141 (void __user
*) arg
);
1146 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
1148 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1150 if (!kref_get_unless_zero(&ns
->kref
))
1155 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
1157 nvme_put_ns(disk
->private_data
);
1160 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1162 /* some standard values */
1163 geo
->heads
= 1 << 6;
1164 geo
->sectors
= 1 << 5;
1165 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
1169 #ifdef CONFIG_BLK_DEV_INTEGRITY
1170 static void nvme_init_integrity(struct gendisk
*disk
, u16 ms
, u8 pi_type
)
1172 struct blk_integrity integrity
;
1174 memset(&integrity
, 0, sizeof(integrity
));
1176 case NVME_NS_DPS_PI_TYPE3
:
1177 integrity
.profile
= &t10_pi_type3_crc
;
1178 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
1179 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
1181 case NVME_NS_DPS_PI_TYPE1
:
1182 case NVME_NS_DPS_PI_TYPE2
:
1183 integrity
.profile
= &t10_pi_type1_crc
;
1184 integrity
.tag_size
= sizeof(u16
);
1185 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
1188 integrity
.profile
= NULL
;
1191 integrity
.tuple_size
= ms
;
1192 blk_integrity_register(disk
, &integrity
);
1193 blk_queue_max_integrity_segments(disk
->queue
, 1);
1196 static void nvme_init_integrity(struct gendisk
*disk
, u16 ms
, u8 pi_type
)
1199 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1201 static void nvme_set_chunk_size(struct nvme_ns
*ns
)
1203 u32 chunk_size
= (((u32
)ns
->noiob
) << (ns
->lba_shift
- 9));
1204 blk_queue_chunk_sectors(ns
->queue
, rounddown_pow_of_two(chunk_size
));
1207 static void nvme_config_discard(struct nvme_ctrl
*ctrl
,
1208 unsigned stream_alignment
, struct request_queue
*queue
)
1210 u32 size
= queue_logical_block_size(queue
);
1212 if (stream_alignment
)
1213 size
*= stream_alignment
;
1215 BUILD_BUG_ON(PAGE_SIZE
/ sizeof(struct nvme_dsm_range
) <
1216 NVME_DSM_MAX_RANGES
);
1218 queue
->limits
.discard_alignment
= size
;
1219 queue
->limits
.discard_granularity
= size
;
1221 blk_queue_max_discard_sectors(queue
, UINT_MAX
);
1222 blk_queue_max_discard_segments(queue
, NVME_DSM_MAX_RANGES
);
1223 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, queue
);
1225 if (ctrl
->quirks
& NVME_QUIRK_DEALLOCATE_ZEROES
)
1226 blk_queue_max_write_zeroes_sectors(queue
, UINT_MAX
);
1229 static void nvme_report_ns_ids(struct nvme_ctrl
*ctrl
, unsigned int nsid
,
1230 struct nvme_id_ns
*id
, u8
*eui64
, u8
*nguid
, uuid_t
*uuid
)
1232 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1233 memcpy(eui64
, id
->eui64
, sizeof(id
->eui64
));
1234 if (ctrl
->vs
>= NVME_VS(1, 2, 0))
1235 memcpy(nguid
, id
->nguid
, sizeof(id
->nguid
));
1236 if (ctrl
->vs
>= NVME_VS(1, 3, 0)) {
1237 /* Don't treat error as fatal we potentially
1238 * already have a NGUID or EUI-64
1240 if (nvme_identify_ns_descs(ctrl
, nsid
, eui64
, nguid
, uuid
))
1241 dev_warn(ctrl
->device
,
1242 "%s: Identify Descriptors failed\n", __func__
);
1246 static void nvme_update_disk_info(struct gendisk
*disk
,
1247 struct nvme_ns
*ns
, struct nvme_id_ns
*id
)
1249 sector_t capacity
= le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9);
1250 unsigned stream_alignment
= 0;
1252 if (ns
->ctrl
->nr_streams
&& ns
->sws
&& ns
->sgs
)
1253 stream_alignment
= ns
->sws
* ns
->sgs
;
1255 blk_mq_freeze_queue(disk
->queue
);
1256 blk_integrity_unregister(disk
);
1258 blk_queue_logical_block_size(disk
->queue
, 1 << ns
->lba_shift
);
1259 if (ns
->ms
&& !ns
->ext
&&
1260 (ns
->ctrl
->ops
->flags
& NVME_F_METADATA_SUPPORTED
))
1261 nvme_init_integrity(disk
, ns
->ms
, ns
->pi_type
);
1262 if (ns
->ms
&& !nvme_ns_has_pi(ns
) && !blk_get_integrity(disk
))
1264 set_capacity(disk
, capacity
);
1266 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
1267 nvme_config_discard(ns
->ctrl
, stream_alignment
, disk
->queue
);
1268 blk_mq_unfreeze_queue(disk
->queue
);
1271 static void __nvme_revalidate_disk(struct gendisk
*disk
, struct nvme_id_ns
*id
)
1273 struct nvme_ns
*ns
= disk
->private_data
;
1276 * If identify namespace failed, use default 512 byte block size so
1277 * block layer can use before failing read/write for 0 capacity.
1279 ns
->lba_shift
= id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ds
;
1280 if (ns
->lba_shift
== 0)
1282 ns
->noiob
= le16_to_cpu(id
->noiob
);
1283 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
1284 ns
->ms
= le16_to_cpu(id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ms
);
1285 /* the PI implementation requires metadata equal t10 pi tuple size */
1286 if (ns
->ms
== sizeof(struct t10_pi_tuple
))
1287 ns
->pi_type
= id
->dps
& NVME_NS_DPS_PI_MASK
;
1292 nvme_set_chunk_size(ns
);
1293 nvme_update_disk_info(disk
, ns
, id
);
1296 static int nvme_revalidate_disk(struct gendisk
*disk
)
1298 struct nvme_ns
*ns
= disk
->private_data
;
1299 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1300 struct nvme_id_ns
*id
;
1301 u8 eui64
[8] = { 0 }, nguid
[16] = { 0 };
1302 uuid_t uuid
= uuid_null
;
1305 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
1306 set_capacity(disk
, 0);
1310 id
= nvme_identify_ns(ctrl
, ns
->ns_id
);
1314 if (id
->ncap
== 0) {
1319 nvme_report_ns_ids(ctrl
, ns
->ns_id
, id
, eui64
, nguid
, &uuid
);
1320 if (!uuid_equal(&ns
->uuid
, &uuid
) ||
1321 memcmp(&ns
->nguid
, &nguid
, sizeof(ns
->nguid
)) ||
1322 memcmp(&ns
->eui
, &eui64
, sizeof(ns
->eui
))) {
1323 dev_err(ctrl
->device
,
1324 "identifiers changed for nsid %d\n", ns
->ns_id
);
1333 static char nvme_pr_type(enum pr_type type
)
1336 case PR_WRITE_EXCLUSIVE
:
1338 case PR_EXCLUSIVE_ACCESS
:
1340 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
1342 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
1344 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
1346 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
1353 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
1354 u64 key
, u64 sa_key
, u8 op
)
1356 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1357 struct nvme_command c
;
1358 u8 data
[16] = { 0, };
1360 put_unaligned_le64(key
, &data
[0]);
1361 put_unaligned_le64(sa_key
, &data
[8]);
1363 memset(&c
, 0, sizeof(c
));
1364 c
.common
.opcode
= op
;
1365 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
1366 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
1368 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
1371 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
1372 u64
new, unsigned flags
)
1376 if (flags
& ~PR_FL_IGNORE_KEY
)
1379 cdw10
= old
? 2 : 0;
1380 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
1381 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
1382 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
1385 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
1386 enum pr_type type
, unsigned flags
)
1390 if (flags
& ~PR_FL_IGNORE_KEY
)
1393 cdw10
= nvme_pr_type(type
) << 8;
1394 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
1395 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
1398 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
1399 enum pr_type type
, bool abort
)
1401 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
1402 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
1405 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
1407 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
1408 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
1411 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1413 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
1414 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
1417 static const struct pr_ops nvme_pr_ops
= {
1418 .pr_register
= nvme_pr_register
,
1419 .pr_reserve
= nvme_pr_reserve
,
1420 .pr_release
= nvme_pr_release
,
1421 .pr_preempt
= nvme_pr_preempt
,
1422 .pr_clear
= nvme_pr_clear
,
1425 #ifdef CONFIG_BLK_SED_OPAL
1426 int nvme_sec_submit(void *data
, u16 spsp
, u8 secp
, void *buffer
, size_t len
,
1429 struct nvme_ctrl
*ctrl
= data
;
1430 struct nvme_command cmd
;
1432 memset(&cmd
, 0, sizeof(cmd
));
1434 cmd
.common
.opcode
= nvme_admin_security_send
;
1436 cmd
.common
.opcode
= nvme_admin_security_recv
;
1437 cmd
.common
.nsid
= 0;
1438 cmd
.common
.cdw10
[0] = cpu_to_le32(((u32
)secp
) << 24 | ((u32
)spsp
) << 8);
1439 cmd
.common
.cdw10
[1] = cpu_to_le32(len
);
1441 return __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, buffer
, len
,
1442 ADMIN_TIMEOUT
, NVME_QID_ANY
, 1, 0);
1444 EXPORT_SYMBOL_GPL(nvme_sec_submit
);
1445 #endif /* CONFIG_BLK_SED_OPAL */
1447 static const struct block_device_operations nvme_fops
= {
1448 .owner
= THIS_MODULE
,
1449 .ioctl
= nvme_ioctl
,
1450 .compat_ioctl
= nvme_ioctl
,
1452 .release
= nvme_release
,
1453 .getgeo
= nvme_getgeo
,
1454 .revalidate_disk
= nvme_revalidate_disk
,
1455 .pr_ops
= &nvme_pr_ops
,
1458 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
1460 unsigned long timeout
=
1461 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
1462 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
1465 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1468 if ((csts
& NVME_CSTS_RDY
) == bit
)
1472 if (fatal_signal_pending(current
))
1474 if (time_after(jiffies
, timeout
)) {
1475 dev_err(ctrl
->device
,
1476 "Device not ready; aborting %s\n", enabled
?
1477 "initialisation" : "reset");
1486 * If the device has been passed off to us in an enabled state, just clear
1487 * the enabled bit. The spec says we should set the 'shutdown notification
1488 * bits', but doing so may cause the device to complete commands to the
1489 * admin queue ... and we don't know what memory that might be pointing at!
1491 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1495 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1496 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1498 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1502 if (ctrl
->quirks
& NVME_QUIRK_DELAY_BEFORE_CHK_RDY
)
1503 msleep(NVME_QUIRK_DELAY_AMOUNT
);
1505 return nvme_wait_ready(ctrl
, cap
, false);
1507 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1509 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1512 * Default to a 4K page size, with the intention to update this
1513 * path in the future to accomodate architectures with differing
1514 * kernel and IO page sizes.
1516 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1519 if (page_shift
< dev_page_min
) {
1520 dev_err(ctrl
->device
,
1521 "Minimum device page size %u too large for host (%u)\n",
1522 1 << dev_page_min
, 1 << page_shift
);
1526 ctrl
->page_size
= 1 << page_shift
;
1528 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1529 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1530 ctrl
->ctrl_config
|= NVME_CC_AMS_RR
| NVME_CC_SHN_NONE
;
1531 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1532 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1534 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1537 return nvme_wait_ready(ctrl
, cap
, true);
1539 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1541 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1543 unsigned long timeout
= jiffies
+ (ctrl
->shutdown_timeout
* HZ
);
1547 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1548 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1550 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1554 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1555 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1559 if (fatal_signal_pending(current
))
1561 if (time_after(jiffies
, timeout
)) {
1562 dev_err(ctrl
->device
,
1563 "Device shutdown incomplete; abort shutdown\n");
1570 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1572 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1573 struct request_queue
*q
)
1577 if (ctrl
->max_hw_sectors
) {
1579 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1581 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1582 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1584 if (ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
)
1585 blk_queue_chunk_sectors(q
, ctrl
->max_hw_sectors
);
1586 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1587 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1589 blk_queue_write_cache(q
, vwc
, vwc
);
1592 static int nvme_configure_timestamp(struct nvme_ctrl
*ctrl
)
1597 if (!(ctrl
->oncs
& NVME_CTRL_ONCS_TIMESTAMP
))
1600 ts
= cpu_to_le64(ktime_to_ms(ktime_get_real()));
1601 ret
= nvme_set_features(ctrl
, NVME_FEAT_TIMESTAMP
, 0, &ts
, sizeof(ts
),
1604 dev_warn_once(ctrl
->device
,
1605 "could not set timestamp (%d)\n", ret
);
1609 static int nvme_configure_apst(struct nvme_ctrl
*ctrl
)
1612 * APST (Autonomous Power State Transition) lets us program a
1613 * table of power state transitions that the controller will
1614 * perform automatically. We configure it with a simple
1615 * heuristic: we are willing to spend at most 2% of the time
1616 * transitioning between power states. Therefore, when running
1617 * in any given state, we will enter the next lower-power
1618 * non-operational state after waiting 50 * (enlat + exlat)
1619 * microseconds, as long as that state's exit latency is under
1620 * the requested maximum latency.
1622 * We will not autonomously enter any non-operational state for
1623 * which the total latency exceeds ps_max_latency_us. Users
1624 * can set ps_max_latency_us to zero to turn off APST.
1628 struct nvme_feat_auto_pst
*table
;
1634 * If APST isn't supported or if we haven't been initialized yet,
1635 * then don't do anything.
1640 if (ctrl
->npss
> 31) {
1641 dev_warn(ctrl
->device
, "NPSS is invalid; not using APST\n");
1645 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
1649 if (!ctrl
->apst_enabled
|| ctrl
->ps_max_latency_us
== 0) {
1650 /* Turn off APST. */
1652 dev_dbg(ctrl
->device
, "APST disabled\n");
1654 __le64 target
= cpu_to_le64(0);
1658 * Walk through all states from lowest- to highest-power.
1659 * According to the spec, lower-numbered states use more
1660 * power. NPSS, despite the name, is the index of the
1661 * lowest-power state, not the number of states.
1663 for (state
= (int)ctrl
->npss
; state
>= 0; state
--) {
1664 u64 total_latency_us
, exit_latency_us
, transition_ms
;
1667 table
->entries
[state
] = target
;
1670 * Don't allow transitions to the deepest state
1671 * if it's quirked off.
1673 if (state
== ctrl
->npss
&&
1674 (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
))
1678 * Is this state a useful non-operational state for
1679 * higher-power states to autonomously transition to?
1681 if (!(ctrl
->psd
[state
].flags
&
1682 NVME_PS_FLAGS_NON_OP_STATE
))
1686 (u64
)le32_to_cpu(ctrl
->psd
[state
].exit_lat
);
1687 if (exit_latency_us
> ctrl
->ps_max_latency_us
)
1692 le32_to_cpu(ctrl
->psd
[state
].entry_lat
);
1695 * This state is good. Use it as the APST idle
1696 * target for higher power states.
1698 transition_ms
= total_latency_us
+ 19;
1699 do_div(transition_ms
, 20);
1700 if (transition_ms
> (1 << 24) - 1)
1701 transition_ms
= (1 << 24) - 1;
1703 target
= cpu_to_le64((state
<< 3) |
1704 (transition_ms
<< 8));
1709 if (total_latency_us
> max_lat_us
)
1710 max_lat_us
= total_latency_us
;
1716 dev_dbg(ctrl
->device
, "APST enabled but no non-operational states are available\n");
1718 dev_dbg(ctrl
->device
, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1719 max_ps
, max_lat_us
, (int)sizeof(*table
), table
);
1723 ret
= nvme_set_features(ctrl
, NVME_FEAT_AUTO_PST
, apste
,
1724 table
, sizeof(*table
), NULL
);
1726 dev_err(ctrl
->device
, "failed to set APST feature (%d)\n", ret
);
1732 static void nvme_set_latency_tolerance(struct device
*dev
, s32 val
)
1734 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1738 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
:
1739 case PM_QOS_LATENCY_ANY
:
1747 if (ctrl
->ps_max_latency_us
!= latency
) {
1748 ctrl
->ps_max_latency_us
= latency
;
1749 nvme_configure_apst(ctrl
);
1753 struct nvme_core_quirk_entry
{
1755 * NVMe model and firmware strings are padded with spaces. For
1756 * simplicity, strings in the quirk table are padded with NULLs
1762 unsigned long quirks
;
1765 static const struct nvme_core_quirk_entry core_quirks
[] = {
1768 * This Toshiba device seems to die using any APST states. See:
1769 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1772 .mn
= "THNSF5256GPUK TOSHIBA",
1773 .quirks
= NVME_QUIRK_NO_APST
,
1777 /* match is null-terminated but idstr is space-padded. */
1778 static bool string_matches(const char *idstr
, const char *match
, size_t len
)
1785 matchlen
= strlen(match
);
1786 WARN_ON_ONCE(matchlen
> len
);
1788 if (memcmp(idstr
, match
, matchlen
))
1791 for (; matchlen
< len
; matchlen
++)
1792 if (idstr
[matchlen
] != ' ')
1798 static bool quirk_matches(const struct nvme_id_ctrl
*id
,
1799 const struct nvme_core_quirk_entry
*q
)
1801 return q
->vid
== le16_to_cpu(id
->vid
) &&
1802 string_matches(id
->mn
, q
->mn
, sizeof(id
->mn
)) &&
1803 string_matches(id
->fr
, q
->fr
, sizeof(id
->fr
));
1806 static void nvme_init_subnqn(struct nvme_ctrl
*ctrl
, struct nvme_id_ctrl
*id
)
1811 nqnlen
= strnlen(id
->subnqn
, NVMF_NQN_SIZE
);
1812 if (nqnlen
> 0 && nqnlen
< NVMF_NQN_SIZE
) {
1813 strncpy(ctrl
->subnqn
, id
->subnqn
, NVMF_NQN_SIZE
);
1817 if (ctrl
->vs
>= NVME_VS(1, 2, 1))
1818 dev_warn(ctrl
->device
, "missing or invalid SUBNQN field.\n");
1820 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1821 off
= snprintf(ctrl
->subnqn
, NVMF_NQN_SIZE
,
1822 "nqn.2014.08.org.nvmexpress:%4x%4x",
1823 le16_to_cpu(id
->vid
), le16_to_cpu(id
->ssvid
));
1824 memcpy(ctrl
->subnqn
+ off
, id
->sn
, sizeof(id
->sn
));
1825 off
+= sizeof(id
->sn
);
1826 memcpy(ctrl
->subnqn
+ off
, id
->mn
, sizeof(id
->mn
));
1827 off
+= sizeof(id
->mn
);
1828 memset(ctrl
->subnqn
+ off
, 0, sizeof(ctrl
->subnqn
) - off
);
1831 static int nvme_get_log(struct nvme_ctrl
*ctrl
, u8 log_page
, void *log
,
1834 struct nvme_command c
= { };
1836 c
.common
.opcode
= nvme_admin_get_log_page
;
1837 c
.common
.nsid
= cpu_to_le32(NVME_NSID_ALL
);
1838 c
.common
.cdw10
[0] = nvme_get_log_dw10(log_page
, size
);
1840 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, log
, size
);
1843 static int nvme_get_effects_log(struct nvme_ctrl
*ctrl
)
1848 ctrl
->effects
= kzalloc(sizeof(*ctrl
->effects
), GFP_KERNEL
);
1853 ret
= nvme_get_log(ctrl
, NVME_LOG_CMD_EFFECTS
, ctrl
->effects
,
1854 sizeof(*ctrl
->effects
));
1856 kfree(ctrl
->effects
);
1857 ctrl
->effects
= NULL
;
1863 * Initialize the cached copies of the Identify data and various controller
1864 * register in our nvme_ctrl structure. This should be called as soon as
1865 * the admin queue is fully up and running.
1867 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1869 struct nvme_id_ctrl
*id
;
1871 int ret
, page_shift
;
1873 bool prev_apst_enabled
;
1875 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1877 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1881 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1883 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1886 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1888 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1889 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1891 ret
= nvme_identify_ctrl(ctrl
, &id
);
1893 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1897 if (id
->lpa
& NVME_CTRL_LPA_CMD_EFFECTS_LOG
) {
1898 ret
= nvme_get_effects_log(ctrl
);
1903 nvme_init_subnqn(ctrl
, id
);
1905 if (!ctrl
->identified
) {
1907 * Check for quirks. Quirk can depend on firmware version,
1908 * so, in principle, the set of quirks present can change
1909 * across a reset. As a possible future enhancement, we
1910 * could re-scan for quirks every time we reinitialize
1911 * the device, but we'd have to make sure that the driver
1912 * behaves intelligently if the quirks change.
1917 for (i
= 0; i
< ARRAY_SIZE(core_quirks
); i
++) {
1918 if (quirk_matches(id
, &core_quirks
[i
]))
1919 ctrl
->quirks
|= core_quirks
[i
].quirks
;
1923 if (force_apst
&& (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
)) {
1924 dev_warn(ctrl
->device
, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
1925 ctrl
->quirks
&= ~NVME_QUIRK_NO_DEEPEST_PS
;
1928 ctrl
->oacs
= le16_to_cpu(id
->oacs
);
1929 ctrl
->vid
= le16_to_cpu(id
->vid
);
1930 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1931 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1932 ctrl
->vwc
= id
->vwc
;
1933 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1934 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1935 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1936 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1938 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1940 max_hw_sectors
= UINT_MAX
;
1941 ctrl
->max_hw_sectors
=
1942 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
1944 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1945 ctrl
->sgls
= le32_to_cpu(id
->sgls
);
1946 ctrl
->kas
= le16_to_cpu(id
->kas
);
1950 u32 transition_time
= le32_to_cpu(id
->rtd3e
) / 1000000;
1952 ctrl
->shutdown_timeout
= clamp_t(unsigned int, transition_time
,
1953 shutdown_timeout
, 60);
1955 if (ctrl
->shutdown_timeout
!= shutdown_timeout
)
1956 dev_warn(ctrl
->device
,
1957 "Shutdown timeout set to %u seconds\n",
1958 ctrl
->shutdown_timeout
);
1960 ctrl
->shutdown_timeout
= shutdown_timeout
;
1962 ctrl
->npss
= id
->npss
;
1963 ctrl
->apsta
= id
->apsta
;
1964 prev_apst_enabled
= ctrl
->apst_enabled
;
1965 if (ctrl
->quirks
& NVME_QUIRK_NO_APST
) {
1966 if (force_apst
&& id
->apsta
) {
1967 dev_warn(ctrl
->device
, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
1968 ctrl
->apst_enabled
= true;
1970 ctrl
->apst_enabled
= false;
1973 ctrl
->apst_enabled
= id
->apsta
;
1975 memcpy(ctrl
->psd
, id
->psd
, sizeof(ctrl
->psd
));
1977 if (ctrl
->ops
->flags
& NVME_F_FABRICS
) {
1978 ctrl
->icdoff
= le16_to_cpu(id
->icdoff
);
1979 ctrl
->ioccsz
= le32_to_cpu(id
->ioccsz
);
1980 ctrl
->iorcsz
= le32_to_cpu(id
->iorcsz
);
1981 ctrl
->maxcmd
= le16_to_cpu(id
->maxcmd
);
1984 * In fabrics we need to verify the cntlid matches the
1987 if (ctrl
->cntlid
!= le16_to_cpu(id
->cntlid
)) {
1992 if (!ctrl
->opts
->discovery_nqn
&& !ctrl
->kas
) {
1993 dev_err(ctrl
->device
,
1994 "keep-alive support is mandatory for fabrics\n");
1999 ctrl
->cntlid
= le16_to_cpu(id
->cntlid
);
2000 ctrl
->hmpre
= le32_to_cpu(id
->hmpre
);
2001 ctrl
->hmmin
= le32_to_cpu(id
->hmmin
);
2002 ctrl
->hmminds
= le32_to_cpu(id
->hmminds
);
2003 ctrl
->hmmaxd
= le16_to_cpu(id
->hmmaxd
);
2008 if (ctrl
->apst_enabled
&& !prev_apst_enabled
)
2009 dev_pm_qos_expose_latency_tolerance(ctrl
->device
);
2010 else if (!ctrl
->apst_enabled
&& prev_apst_enabled
)
2011 dev_pm_qos_hide_latency_tolerance(ctrl
->device
);
2013 ret
= nvme_configure_apst(ctrl
);
2017 ret
= nvme_configure_timestamp(ctrl
);
2021 ret
= nvme_configure_directives(ctrl
);
2025 ctrl
->identified
= true;
2033 EXPORT_SYMBOL_GPL(nvme_init_identify
);
2035 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
2037 struct nvme_ctrl
*ctrl
=
2038 container_of(inode
->i_cdev
, struct nvme_ctrl
, cdev
);
2040 if (ctrl
->state
!= NVME_CTRL_LIVE
)
2041 return -EWOULDBLOCK
;
2042 file
->private_data
= ctrl
;
2046 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
2051 mutex_lock(&ctrl
->namespaces_mutex
);
2052 if (list_empty(&ctrl
->namespaces
)) {
2057 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
2058 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
2059 dev_warn(ctrl
->device
,
2060 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2065 dev_warn(ctrl
->device
,
2066 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2067 kref_get(&ns
->kref
);
2068 mutex_unlock(&ctrl
->namespaces_mutex
);
2070 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
2075 mutex_unlock(&ctrl
->namespaces_mutex
);
2079 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
2082 struct nvme_ctrl
*ctrl
= file
->private_data
;
2083 void __user
*argp
= (void __user
*)arg
;
2086 case NVME_IOCTL_ADMIN_CMD
:
2087 return nvme_user_cmd(ctrl
, NULL
, argp
);
2088 case NVME_IOCTL_IO_CMD
:
2089 return nvme_dev_user_cmd(ctrl
, argp
);
2090 case NVME_IOCTL_RESET
:
2091 dev_warn(ctrl
->device
, "resetting controller\n");
2092 return nvme_reset_ctrl_sync(ctrl
);
2093 case NVME_IOCTL_SUBSYS_RESET
:
2094 return nvme_reset_subsystem(ctrl
);
2095 case NVME_IOCTL_RESCAN
:
2096 nvme_queue_scan(ctrl
);
2103 static const struct file_operations nvme_dev_fops
= {
2104 .owner
= THIS_MODULE
,
2105 .open
= nvme_dev_open
,
2106 .unlocked_ioctl
= nvme_dev_ioctl
,
2107 .compat_ioctl
= nvme_dev_ioctl
,
2110 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
2111 struct device_attribute
*attr
, const char *buf
,
2114 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2117 ret
= nvme_reset_ctrl_sync(ctrl
);
2122 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
2124 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
2125 struct device_attribute
*attr
, const char *buf
,
2128 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2130 nvme_queue_scan(ctrl
);
2133 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
2135 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
2138 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2139 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
2140 int serial_len
= sizeof(ctrl
->serial
);
2141 int model_len
= sizeof(ctrl
->model
);
2143 if (!uuid_is_null(&ns
->uuid
))
2144 return sprintf(buf
, "uuid.%pU\n", &ns
->uuid
);
2146 if (memchr_inv(ns
->nguid
, 0, sizeof(ns
->nguid
)))
2147 return sprintf(buf
, "eui.%16phN\n", ns
->nguid
);
2149 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
2150 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
2152 while (serial_len
> 0 && (ctrl
->serial
[serial_len
- 1] == ' ' ||
2153 ctrl
->serial
[serial_len
- 1] == '\0'))
2155 while (model_len
> 0 && (ctrl
->model
[model_len
- 1] == ' ' ||
2156 ctrl
->model
[model_len
- 1] == '\0'))
2159 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
2160 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
2162 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
2164 static ssize_t
nguid_show(struct device
*dev
, struct device_attribute
*attr
,
2167 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2168 return sprintf(buf
, "%pU\n", ns
->nguid
);
2170 static DEVICE_ATTR(nguid
, S_IRUGO
, nguid_show
, NULL
);
2172 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
2175 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2177 /* For backward compatibility expose the NGUID to userspace if
2178 * we have no UUID set
2180 if (uuid_is_null(&ns
->uuid
)) {
2181 printk_ratelimited(KERN_WARNING
2182 "No UUID available providing old NGUID\n");
2183 return sprintf(buf
, "%pU\n", ns
->nguid
);
2185 return sprintf(buf
, "%pU\n", &ns
->uuid
);
2187 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
2189 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
2192 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2193 return sprintf(buf
, "%8ph\n", ns
->eui
);
2195 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
2197 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
2200 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2201 return sprintf(buf
, "%d\n", ns
->ns_id
);
2203 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
2205 static struct attribute
*nvme_ns_attrs
[] = {
2206 &dev_attr_wwid
.attr
,
2207 &dev_attr_uuid
.attr
,
2208 &dev_attr_nguid
.attr
,
2210 &dev_attr_nsid
.attr
,
2214 static umode_t
nvme_ns_attrs_are_visible(struct kobject
*kobj
,
2215 struct attribute
*a
, int n
)
2217 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2218 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
2220 if (a
== &dev_attr_uuid
.attr
) {
2221 if (uuid_is_null(&ns
->uuid
) ||
2222 !memchr_inv(ns
->nguid
, 0, sizeof(ns
->nguid
)))
2225 if (a
== &dev_attr_nguid
.attr
) {
2226 if (!memchr_inv(ns
->nguid
, 0, sizeof(ns
->nguid
)))
2229 if (a
== &dev_attr_eui
.attr
) {
2230 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
2236 static const struct attribute_group nvme_ns_attr_group
= {
2237 .attrs
= nvme_ns_attrs
,
2238 .is_visible
= nvme_ns_attrs_are_visible
,
2241 #define nvme_show_str_function(field) \
2242 static ssize_t field##_show(struct device *dev, \
2243 struct device_attribute *attr, char *buf) \
2245 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2246 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2248 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2250 #define nvme_show_int_function(field) \
2251 static ssize_t field##_show(struct device *dev, \
2252 struct device_attribute *attr, char *buf) \
2254 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2255 return sprintf(buf, "%d\n", ctrl->field); \
2257 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2259 nvme_show_str_function(model
);
2260 nvme_show_str_function(serial
);
2261 nvme_show_str_function(firmware_rev
);
2262 nvme_show_int_function(cntlid
);
2264 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
2265 struct device_attribute
*attr
, const char *buf
,
2268 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2270 if (device_remove_file_self(dev
, attr
))
2271 nvme_delete_ctrl_sync(ctrl
);
2274 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
2276 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
2277 struct device_attribute
*attr
,
2280 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2282 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
2284 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
2286 static ssize_t
nvme_sysfs_show_state(struct device
*dev
,
2287 struct device_attribute
*attr
,
2290 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2291 static const char *const state_name
[] = {
2292 [NVME_CTRL_NEW
] = "new",
2293 [NVME_CTRL_LIVE
] = "live",
2294 [NVME_CTRL_RESETTING
] = "resetting",
2295 [NVME_CTRL_RECONNECTING
]= "reconnecting",
2296 [NVME_CTRL_DELETING
] = "deleting",
2297 [NVME_CTRL_DEAD
] = "dead",
2300 if ((unsigned)ctrl
->state
< ARRAY_SIZE(state_name
) &&
2301 state_name
[ctrl
->state
])
2302 return sprintf(buf
, "%s\n", state_name
[ctrl
->state
]);
2304 return sprintf(buf
, "unknown state\n");
2307 static DEVICE_ATTR(state
, S_IRUGO
, nvme_sysfs_show_state
, NULL
);
2309 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
2310 struct device_attribute
*attr
,
2313 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2315 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->subnqn
);
2317 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
2319 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
2320 struct device_attribute
*attr
,
2323 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2325 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
2327 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
2329 static struct attribute
*nvme_dev_attrs
[] = {
2330 &dev_attr_reset_controller
.attr
,
2331 &dev_attr_rescan_controller
.attr
,
2332 &dev_attr_model
.attr
,
2333 &dev_attr_serial
.attr
,
2334 &dev_attr_firmware_rev
.attr
,
2335 &dev_attr_cntlid
.attr
,
2336 &dev_attr_delete_controller
.attr
,
2337 &dev_attr_transport
.attr
,
2338 &dev_attr_subsysnqn
.attr
,
2339 &dev_attr_address
.attr
,
2340 &dev_attr_state
.attr
,
2344 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
2345 struct attribute
*a
, int n
)
2347 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2348 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2350 if (a
== &dev_attr_delete_controller
.attr
&& !ctrl
->ops
->delete_ctrl
)
2352 if (a
== &dev_attr_address
.attr
&& !ctrl
->ops
->get_address
)
2358 static struct attribute_group nvme_dev_attrs_group
= {
2359 .attrs
= nvme_dev_attrs
,
2360 .is_visible
= nvme_dev_attrs_are_visible
,
2363 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
2364 &nvme_dev_attrs_group
,
2368 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
2370 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
2371 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
2373 return nsa
->ns_id
- nsb
->ns_id
;
2376 static struct nvme_ns
*nvme_find_get_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2378 struct nvme_ns
*ns
, *ret
= NULL
;
2380 mutex_lock(&ctrl
->namespaces_mutex
);
2381 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2382 if (ns
->ns_id
== nsid
) {
2383 if (!kref_get_unless_zero(&ns
->kref
))
2388 if (ns
->ns_id
> nsid
)
2391 mutex_unlock(&ctrl
->namespaces_mutex
);
2395 static int nvme_setup_streams_ns(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
)
2397 struct streams_directive_params s
;
2400 if (!ctrl
->nr_streams
)
2403 ret
= nvme_get_stream_params(ctrl
, &s
, ns
->ns_id
);
2407 ns
->sws
= le32_to_cpu(s
.sws
);
2408 ns
->sgs
= le16_to_cpu(s
.sgs
);
2411 unsigned int bs
= 1 << ns
->lba_shift
;
2413 blk_queue_io_min(ns
->queue
, bs
* ns
->sws
);
2415 blk_queue_io_opt(ns
->queue
, bs
* ns
->sws
* ns
->sgs
);
2421 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2424 struct gendisk
*disk
;
2425 struct nvme_id_ns
*id
;
2426 char disk_name
[DISK_NAME_LEN
];
2427 int node
= dev_to_node(ctrl
->dev
);
2429 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
2433 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
2434 if (ns
->instance
< 0)
2437 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
2438 if (IS_ERR(ns
->queue
))
2439 goto out_release_instance
;
2440 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
2441 ns
->queue
->queuedata
= ns
;
2444 kref_init(&ns
->kref
);
2446 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
2448 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
2449 nvme_set_queue_limits(ctrl
, ns
->queue
);
2450 nvme_setup_streams_ns(ctrl
, ns
);
2452 sprintf(disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
2454 id
= nvme_identify_ns(ctrl
, nsid
);
2456 goto out_free_queue
;
2461 nvme_report_ns_ids(ctrl
, ns
->ns_id
, id
, ns
->eui
, ns
->nguid
, &ns
->uuid
);
2463 if ((ctrl
->quirks
& NVME_QUIRK_LIGHTNVM
) && id
->vs
[0] == 0x1) {
2464 if (nvme_nvm_register(ns
, disk_name
, node
)) {
2465 dev_warn(ctrl
->device
, "LightNVM init failure\n");
2470 disk
= alloc_disk_node(0, node
);
2474 disk
->fops
= &nvme_fops
;
2475 disk
->private_data
= ns
;
2476 disk
->queue
= ns
->queue
;
2477 disk
->flags
= GENHD_FL_EXT_DEVT
;
2478 memcpy(disk
->disk_name
, disk_name
, DISK_NAME_LEN
);
2481 __nvme_revalidate_disk(disk
, id
);
2483 mutex_lock(&ctrl
->namespaces_mutex
);
2484 list_add_tail(&ns
->list
, &ctrl
->namespaces
);
2485 mutex_unlock(&ctrl
->namespaces_mutex
);
2487 nvme_get_ctrl(ctrl
);
2491 device_add_disk(ctrl
->device
, ns
->disk
);
2492 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
2493 &nvme_ns_attr_group
))
2494 pr_warn("%s: failed to create sysfs group for identification\n",
2495 ns
->disk
->disk_name
);
2496 if (ns
->ndev
&& nvme_nvm_register_sysfs(ns
))
2497 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2498 ns
->disk
->disk_name
);
2503 blk_cleanup_queue(ns
->queue
);
2504 out_release_instance
:
2505 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
2510 static void nvme_ns_remove(struct nvme_ns
*ns
)
2512 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
2515 if (ns
->disk
&& ns
->disk
->flags
& GENHD_FL_UP
) {
2516 if (blk_get_integrity(ns
->disk
))
2517 blk_integrity_unregister(ns
->disk
);
2518 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
2519 &nvme_ns_attr_group
);
2521 nvme_nvm_unregister_sysfs(ns
);
2522 del_gendisk(ns
->disk
);
2523 blk_cleanup_queue(ns
->queue
);
2526 mutex_lock(&ns
->ctrl
->namespaces_mutex
);
2527 list_del_init(&ns
->list
);
2528 mutex_unlock(&ns
->ctrl
->namespaces_mutex
);
2533 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2537 ns
= nvme_find_get_ns(ctrl
, nsid
);
2539 if (ns
->disk
&& revalidate_disk(ns
->disk
))
2543 nvme_alloc_ns(ctrl
, nsid
);
2546 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
2549 struct nvme_ns
*ns
, *next
;
2551 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
2552 if (ns
->ns_id
> nsid
)
2557 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
2561 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
2564 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
2568 for (i
= 0; i
< num_lists
; i
++) {
2569 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
2573 for (j
= 0; j
< min(nn
, 1024U); j
++) {
2574 nsid
= le32_to_cpu(ns_list
[j
]);
2578 nvme_validate_ns(ctrl
, nsid
);
2580 while (++prev
< nsid
) {
2581 ns
= nvme_find_get_ns(ctrl
, prev
);
2591 nvme_remove_invalid_namespaces(ctrl
, prev
);
2597 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
2601 for (i
= 1; i
<= nn
; i
++)
2602 nvme_validate_ns(ctrl
, i
);
2604 nvme_remove_invalid_namespaces(ctrl
, nn
);
2607 static void nvme_scan_work(struct work_struct
*work
)
2609 struct nvme_ctrl
*ctrl
=
2610 container_of(work
, struct nvme_ctrl
, scan_work
);
2611 struct nvme_id_ctrl
*id
;
2614 if (ctrl
->state
!= NVME_CTRL_LIVE
)
2617 if (nvme_identify_ctrl(ctrl
, &id
))
2620 nn
= le32_to_cpu(id
->nn
);
2621 if (ctrl
->vs
>= NVME_VS(1, 1, 0) &&
2622 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
2623 if (!nvme_scan_ns_list(ctrl
, nn
))
2626 nvme_scan_ns_sequential(ctrl
, nn
);
2628 mutex_lock(&ctrl
->namespaces_mutex
);
2629 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
2630 mutex_unlock(&ctrl
->namespaces_mutex
);
2634 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
2637 * Do not queue new scan work when a controller is reset during
2640 if (ctrl
->state
== NVME_CTRL_LIVE
)
2641 queue_work(nvme_wq
, &ctrl
->scan_work
);
2643 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
2646 * This function iterates the namespace list unlocked to allow recovery from
2647 * controller failure. It is up to the caller to ensure the namespace list is
2648 * not modified by scan work while this function is executing.
2650 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
2652 struct nvme_ns
*ns
, *next
;
2655 * The dead states indicates the controller was not gracefully
2656 * disconnected. In that case, we won't be able to flush any data while
2657 * removing the namespaces' disks; fail all the queues now to avoid
2658 * potentially having to clean up the failed sync later.
2660 if (ctrl
->state
== NVME_CTRL_DEAD
)
2661 nvme_kill_queues(ctrl
);
2663 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
2666 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
2668 static void nvme_async_event_work(struct work_struct
*work
)
2670 struct nvme_ctrl
*ctrl
=
2671 container_of(work
, struct nvme_ctrl
, async_event_work
);
2673 ctrl
->ops
->submit_async_event(ctrl
);
2676 static bool nvme_ctrl_pp_status(struct nvme_ctrl
*ctrl
)
2681 if (ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
))
2687 return ((ctrl
->ctrl_config
& NVME_CC_ENABLE
) && (csts
& NVME_CSTS_PP
));
2690 static void nvme_get_fw_slot_info(struct nvme_ctrl
*ctrl
)
2692 struct nvme_fw_slot_info_log
*log
;
2694 log
= kmalloc(sizeof(*log
), GFP_KERNEL
);
2698 if (nvme_get_log(ctrl
, NVME_LOG_FW_SLOT
, log
, sizeof(*log
)))
2699 dev_warn(ctrl
->device
,
2700 "Get FW SLOT INFO log error\n");
2704 static void nvme_fw_act_work(struct work_struct
*work
)
2706 struct nvme_ctrl
*ctrl
= container_of(work
,
2707 struct nvme_ctrl
, fw_act_work
);
2708 unsigned long fw_act_timeout
;
2711 fw_act_timeout
= jiffies
+
2712 msecs_to_jiffies(ctrl
->mtfa
* 100);
2714 fw_act_timeout
= jiffies
+
2715 msecs_to_jiffies(admin_timeout
* 1000);
2717 nvme_stop_queues(ctrl
);
2718 while (nvme_ctrl_pp_status(ctrl
)) {
2719 if (time_after(jiffies
, fw_act_timeout
)) {
2720 dev_warn(ctrl
->device
,
2721 "Fw activation timeout, reset controller\n");
2722 nvme_reset_ctrl(ctrl
);
2728 if (ctrl
->state
!= NVME_CTRL_LIVE
)
2731 nvme_start_queues(ctrl
);
2732 /* read FW slot information to clear the AER */
2733 nvme_get_fw_slot_info(ctrl
);
2736 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
, __le16 status
,
2737 union nvme_result
*res
)
2739 u32 result
= le32_to_cpu(res
->u32
);
2741 if (le16_to_cpu(status
) >> 1 != NVME_SC_SUCCESS
)
2744 switch (result
& 0xff07) {
2745 case NVME_AER_NOTICE_NS_CHANGED
:
2746 dev_info(ctrl
->device
, "rescanning\n");
2747 nvme_queue_scan(ctrl
);
2749 case NVME_AER_NOTICE_FW_ACT_STARTING
:
2750 queue_work(nvme_wq
, &ctrl
->fw_act_work
);
2753 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
2755 queue_work(nvme_wq
, &ctrl
->async_event_work
);
2757 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
2759 void nvme_stop_ctrl(struct nvme_ctrl
*ctrl
)
2761 nvme_stop_keep_alive(ctrl
);
2762 flush_work(&ctrl
->async_event_work
);
2763 flush_work(&ctrl
->scan_work
);
2764 cancel_work_sync(&ctrl
->fw_act_work
);
2766 EXPORT_SYMBOL_GPL(nvme_stop_ctrl
);
2768 void nvme_start_ctrl(struct nvme_ctrl
*ctrl
)
2771 nvme_start_keep_alive(ctrl
);
2773 if (ctrl
->queue_count
> 1) {
2774 nvme_queue_scan(ctrl
);
2775 queue_work(nvme_wq
, &ctrl
->async_event_work
);
2776 nvme_start_queues(ctrl
);
2779 EXPORT_SYMBOL_GPL(nvme_start_ctrl
);
2781 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
2783 cdev_device_del(&ctrl
->cdev
, ctrl
->device
);
2785 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
2787 static void nvme_free_ctrl(struct device
*dev
)
2789 struct nvme_ctrl
*ctrl
=
2790 container_of(dev
, struct nvme_ctrl
, ctrl_device
);
2792 ida_simple_remove(&nvme_instance_ida
, ctrl
->instance
);
2793 ida_destroy(&ctrl
->ns_ida
);
2794 kfree(ctrl
->effects
);
2796 ctrl
->ops
->free_ctrl(ctrl
);
2800 * Initialize a NVMe controller structures. This needs to be called during
2801 * earliest initialization so that we have the initialized structured around
2804 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
2805 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
2809 ctrl
->state
= NVME_CTRL_NEW
;
2810 spin_lock_init(&ctrl
->lock
);
2811 INIT_LIST_HEAD(&ctrl
->namespaces
);
2812 mutex_init(&ctrl
->namespaces_mutex
);
2815 ctrl
->quirks
= quirks
;
2816 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
2817 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
2818 INIT_WORK(&ctrl
->fw_act_work
, nvme_fw_act_work
);
2819 INIT_WORK(&ctrl
->delete_work
, nvme_delete_ctrl_work
);
2821 ret
= ida_simple_get(&nvme_instance_ida
, 0, 0, GFP_KERNEL
);
2824 ctrl
->instance
= ret
;
2826 device_initialize(&ctrl
->ctrl_device
);
2827 ctrl
->device
= &ctrl
->ctrl_device
;
2828 ctrl
->device
->devt
= MKDEV(MAJOR(nvme_chr_devt
), ctrl
->instance
);
2829 ctrl
->device
->class = nvme_class
;
2830 ctrl
->device
->parent
= ctrl
->dev
;
2831 ctrl
->device
->groups
= nvme_dev_attr_groups
;
2832 ctrl
->device
->release
= nvme_free_ctrl
;
2833 dev_set_drvdata(ctrl
->device
, ctrl
);
2834 ret
= dev_set_name(ctrl
->device
, "nvme%d", ctrl
->instance
);
2836 goto out_release_instance
;
2838 cdev_init(&ctrl
->cdev
, &nvme_dev_fops
);
2839 ctrl
->cdev
.owner
= ops
->module
;
2840 ret
= cdev_device_add(&ctrl
->cdev
, ctrl
->device
);
2844 ida_init(&ctrl
->ns_ida
);
2847 * Initialize latency tolerance controls. The sysfs files won't
2848 * be visible to userspace unless the device actually supports APST.
2850 ctrl
->device
->power
.set_latency_tolerance
= nvme_set_latency_tolerance
;
2851 dev_pm_qos_update_user_latency_tolerance(ctrl
->device
,
2852 min(default_ps_max_latency_us
, (unsigned long)S32_MAX
));
2856 kfree_const(dev
->kobj
.name
);
2857 out_release_instance
:
2858 ida_simple_remove(&nvme_instance_ida
, ctrl
->instance
);
2862 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
2865 * nvme_kill_queues(): Ends all namespace queues
2866 * @ctrl: the dead controller that needs to end
2868 * Call this function when the driver determines it is unable to get the
2869 * controller in a state capable of servicing IO.
2871 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
2875 mutex_lock(&ctrl
->namespaces_mutex
);
2877 /* Forcibly unquiesce queues to avoid blocking dispatch */
2879 blk_mq_unquiesce_queue(ctrl
->admin_q
);
2881 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2883 * Revalidating a dead namespace sets capacity to 0. This will
2884 * end buffered writers dirtying pages that can't be synced.
2886 if (!ns
->disk
|| test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
2888 revalidate_disk(ns
->disk
);
2889 blk_set_queue_dying(ns
->queue
);
2891 /* Forcibly unquiesce queues to avoid blocking dispatch */
2892 blk_mq_unquiesce_queue(ns
->queue
);
2894 mutex_unlock(&ctrl
->namespaces_mutex
);
2896 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
2898 void nvme_unfreeze(struct nvme_ctrl
*ctrl
)
2902 mutex_lock(&ctrl
->namespaces_mutex
);
2903 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2904 blk_mq_unfreeze_queue(ns
->queue
);
2905 mutex_unlock(&ctrl
->namespaces_mutex
);
2907 EXPORT_SYMBOL_GPL(nvme_unfreeze
);
2909 void nvme_wait_freeze_timeout(struct nvme_ctrl
*ctrl
, long timeout
)
2913 mutex_lock(&ctrl
->namespaces_mutex
);
2914 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2915 timeout
= blk_mq_freeze_queue_wait_timeout(ns
->queue
, timeout
);
2919 mutex_unlock(&ctrl
->namespaces_mutex
);
2921 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout
);
2923 void nvme_wait_freeze(struct nvme_ctrl
*ctrl
)
2927 mutex_lock(&ctrl
->namespaces_mutex
);
2928 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2929 blk_mq_freeze_queue_wait(ns
->queue
);
2930 mutex_unlock(&ctrl
->namespaces_mutex
);
2932 EXPORT_SYMBOL_GPL(nvme_wait_freeze
);
2934 void nvme_start_freeze(struct nvme_ctrl
*ctrl
)
2938 mutex_lock(&ctrl
->namespaces_mutex
);
2939 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2940 blk_freeze_queue_start(ns
->queue
);
2941 mutex_unlock(&ctrl
->namespaces_mutex
);
2943 EXPORT_SYMBOL_GPL(nvme_start_freeze
);
2945 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
2949 mutex_lock(&ctrl
->namespaces_mutex
);
2950 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2951 blk_mq_quiesce_queue(ns
->queue
);
2952 mutex_unlock(&ctrl
->namespaces_mutex
);
2954 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
2956 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
2960 mutex_lock(&ctrl
->namespaces_mutex
);
2961 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2962 blk_mq_unquiesce_queue(ns
->queue
);
2963 mutex_unlock(&ctrl
->namespaces_mutex
);
2965 EXPORT_SYMBOL_GPL(nvme_start_queues
);
2967 int nvme_reinit_tagset(struct nvme_ctrl
*ctrl
, struct blk_mq_tag_set
*set
)
2969 if (!ctrl
->ops
->reinit_request
)
2972 return blk_mq_tagset_iter(set
, set
->driver_data
,
2973 ctrl
->ops
->reinit_request
);
2975 EXPORT_SYMBOL_GPL(nvme_reinit_tagset
);
2977 int __init
nvme_core_init(void)
2981 nvme_wq
= alloc_workqueue("nvme-wq",
2982 WQ_UNBOUND
| WQ_MEM_RECLAIM
| WQ_SYSFS
, 0);
2986 result
= alloc_chrdev_region(&nvme_chr_devt
, 0, NVME_MINORS
, "nvme");
2990 nvme_class
= class_create(THIS_MODULE
, "nvme");
2991 if (IS_ERR(nvme_class
)) {
2992 result
= PTR_ERR(nvme_class
);
2993 goto unregister_chrdev
;
2999 unregister_chrdev_region(nvme_chr_devt
, NVME_MINORS
);
3001 destroy_workqueue(nvme_wq
);
3005 void nvme_core_exit(void)
3007 class_destroy(nvme_class
);
3008 unregister_chrdev_region(nvme_chr_devt
, NVME_MINORS
);
3009 destroy_workqueue(nvme_wq
);
3012 MODULE_LICENSE("GPL");
3013 MODULE_VERSION("1.0");
3014 module_init(nvme_core_init
);
3015 module_exit(nvme_core_exit
);