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>
32 #define CREATE_TRACE_POINTS
38 #define NVME_MINORS (1U << MINORBITS)
40 unsigned int admin_timeout
= 60;
41 module_param(admin_timeout
, uint
, 0644);
42 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
43 EXPORT_SYMBOL_GPL(admin_timeout
);
45 unsigned int nvme_io_timeout
= 30;
46 module_param_named(io_timeout
, nvme_io_timeout
, uint
, 0644);
47 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
48 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
50 static unsigned char shutdown_timeout
= 5;
51 module_param(shutdown_timeout
, byte
, 0644);
52 MODULE_PARM_DESC(shutdown_timeout
, "timeout in seconds for controller shutdown");
54 static u8 nvme_max_retries
= 5;
55 module_param_named(max_retries
, nvme_max_retries
, byte
, 0644);
56 MODULE_PARM_DESC(max_retries
, "max number of retries a command may have");
58 static unsigned long default_ps_max_latency_us
= 100000;
59 module_param(default_ps_max_latency_us
, ulong
, 0644);
60 MODULE_PARM_DESC(default_ps_max_latency_us
,
61 "max power saving latency for new devices; use PM QOS to change per device");
63 static bool force_apst
;
64 module_param(force_apst
, bool, 0644);
65 MODULE_PARM_DESC(force_apst
, "allow APST for newly enumerated devices even if quirked off");
68 module_param(streams
, bool, 0644);
69 MODULE_PARM_DESC(streams
, "turn on support for Streams write directives");
72 * nvme_wq - hosts nvme related works that are not reset or delete
73 * nvme_reset_wq - hosts nvme reset works
74 * nvme_delete_wq - hosts nvme delete works
76 * nvme_wq will host works such are scan, aen handling, fw activation,
77 * keep-alive error recovery, periodic reconnects etc. nvme_reset_wq
78 * runs reset works which also flush works hosted on nvme_wq for
79 * serialization purposes. nvme_delete_wq host controller deletion
80 * works which flush reset works for serialization.
82 struct workqueue_struct
*nvme_wq
;
83 EXPORT_SYMBOL_GPL(nvme_wq
);
85 struct workqueue_struct
*nvme_reset_wq
;
86 EXPORT_SYMBOL_GPL(nvme_reset_wq
);
88 struct workqueue_struct
*nvme_delete_wq
;
89 EXPORT_SYMBOL_GPL(nvme_delete_wq
);
91 static DEFINE_IDA(nvme_subsystems_ida
);
92 static LIST_HEAD(nvme_subsystems
);
93 static DEFINE_MUTEX(nvme_subsystems_lock
);
95 static DEFINE_IDA(nvme_instance_ida
);
96 static dev_t nvme_chr_devt
;
97 static struct class *nvme_class
;
98 static struct class *nvme_subsys_class
;
100 static void nvme_ns_remove(struct nvme_ns
*ns
);
101 static int nvme_revalidate_disk(struct gendisk
*disk
);
102 static void nvme_put_subsystem(struct nvme_subsystem
*subsys
);
103 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
106 static void nvme_set_queue_dying(struct nvme_ns
*ns
)
109 * Revalidating a dead namespace sets capacity to 0. This will end
110 * buffered writers dirtying pages that can't be synced.
112 if (!ns
->disk
|| test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
114 blk_set_queue_dying(ns
->queue
);
115 /* Forcibly unquiesce queues to avoid blocking dispatch */
116 blk_mq_unquiesce_queue(ns
->queue
);
118 * Revalidate after unblocking dispatchers that may be holding bd_butex
120 revalidate_disk(ns
->disk
);
123 static void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
126 * Only new queue scan work when admin and IO queues are both alive
128 if (ctrl
->state
== NVME_CTRL_LIVE
)
129 queue_work(nvme_wq
, &ctrl
->scan_work
);
132 int nvme_reset_ctrl(struct nvme_ctrl
*ctrl
)
134 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_RESETTING
))
136 if (!queue_work(nvme_reset_wq
, &ctrl
->reset_work
))
140 EXPORT_SYMBOL_GPL(nvme_reset_ctrl
);
142 int nvme_reset_ctrl_sync(struct nvme_ctrl
*ctrl
)
146 ret
= nvme_reset_ctrl(ctrl
);
148 flush_work(&ctrl
->reset_work
);
149 if (ctrl
->state
!= NVME_CTRL_LIVE
&&
150 ctrl
->state
!= NVME_CTRL_ADMIN_ONLY
)
156 EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync
);
158 static void nvme_delete_ctrl_work(struct work_struct
*work
)
160 struct nvme_ctrl
*ctrl
=
161 container_of(work
, struct nvme_ctrl
, delete_work
);
163 dev_info(ctrl
->device
,
164 "Removing ctrl: NQN \"%s\"\n", ctrl
->opts
->subsysnqn
);
166 flush_work(&ctrl
->reset_work
);
167 nvme_stop_ctrl(ctrl
);
168 nvme_remove_namespaces(ctrl
);
169 ctrl
->ops
->delete_ctrl(ctrl
);
170 nvme_uninit_ctrl(ctrl
);
174 int nvme_delete_ctrl(struct nvme_ctrl
*ctrl
)
176 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_DELETING
))
178 if (!queue_work(nvme_delete_wq
, &ctrl
->delete_work
))
182 EXPORT_SYMBOL_GPL(nvme_delete_ctrl
);
184 int nvme_delete_ctrl_sync(struct nvme_ctrl
*ctrl
)
189 * Keep a reference until the work is flushed since ->delete_ctrl
190 * can free the controller.
193 ret
= nvme_delete_ctrl(ctrl
);
195 flush_work(&ctrl
->delete_work
);
199 EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync
);
201 static inline bool nvme_ns_has_pi(struct nvme_ns
*ns
)
203 return ns
->pi_type
&& ns
->ms
== sizeof(struct t10_pi_tuple
);
206 static blk_status_t
nvme_error_status(struct request
*req
)
208 switch (nvme_req(req
)->status
& 0x7ff) {
209 case NVME_SC_SUCCESS
:
211 case NVME_SC_CAP_EXCEEDED
:
212 return BLK_STS_NOSPC
;
213 case NVME_SC_LBA_RANGE
:
214 return BLK_STS_TARGET
;
215 case NVME_SC_BAD_ATTRIBUTES
:
216 case NVME_SC_ONCS_NOT_SUPPORTED
:
217 case NVME_SC_INVALID_OPCODE
:
218 case NVME_SC_INVALID_FIELD
:
219 case NVME_SC_INVALID_NS
:
220 return BLK_STS_NOTSUPP
;
221 case NVME_SC_WRITE_FAULT
:
222 case NVME_SC_READ_ERROR
:
223 case NVME_SC_UNWRITTEN_BLOCK
:
224 case NVME_SC_ACCESS_DENIED
:
225 case NVME_SC_READ_ONLY
:
226 case NVME_SC_COMPARE_FAILED
:
227 return BLK_STS_MEDIUM
;
228 case NVME_SC_GUARD_CHECK
:
229 case NVME_SC_APPTAG_CHECK
:
230 case NVME_SC_REFTAG_CHECK
:
231 case NVME_SC_INVALID_PI
:
232 return BLK_STS_PROTECTION
;
233 case NVME_SC_RESERVATION_CONFLICT
:
234 return BLK_STS_NEXUS
;
236 return BLK_STS_IOERR
;
240 static inline bool nvme_req_needs_retry(struct request
*req
)
242 if (blk_noretry_request(req
))
244 if (nvme_req(req
)->status
& NVME_SC_DNR
)
246 if (nvme_req(req
)->retries
>= nvme_max_retries
)
251 void nvme_complete_rq(struct request
*req
)
253 blk_status_t status
= nvme_error_status(req
);
255 trace_nvme_complete_rq(req
);
257 if (unlikely(status
!= BLK_STS_OK
&& nvme_req_needs_retry(req
))) {
258 if ((req
->cmd_flags
& REQ_NVME_MPATH
) &&
259 blk_path_error(status
)) {
260 nvme_failover_req(req
);
264 if (!blk_queue_dying(req
->q
)) {
265 nvme_req(req
)->retries
++;
266 blk_mq_requeue_request(req
, true);
270 blk_mq_end_request(req
, status
);
272 EXPORT_SYMBOL_GPL(nvme_complete_rq
);
274 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
276 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
277 "Cancelling I/O %d", req
->tag
);
279 nvme_req(req
)->status
= NVME_SC_ABORT_REQ
;
280 blk_mq_complete_request(req
);
283 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
285 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
286 enum nvme_ctrl_state new_state
)
288 enum nvme_ctrl_state old_state
;
290 bool changed
= false;
292 spin_lock_irqsave(&ctrl
->lock
, flags
);
294 old_state
= ctrl
->state
;
296 case NVME_CTRL_ADMIN_ONLY
:
298 case NVME_CTRL_CONNECTING
:
308 case NVME_CTRL_RESETTING
:
309 case NVME_CTRL_CONNECTING
:
316 case NVME_CTRL_RESETTING
:
320 case NVME_CTRL_ADMIN_ONLY
:
327 case NVME_CTRL_CONNECTING
:
330 case NVME_CTRL_RESETTING
:
337 case NVME_CTRL_DELETING
:
340 case NVME_CTRL_ADMIN_ONLY
:
341 case NVME_CTRL_RESETTING
:
342 case NVME_CTRL_CONNECTING
:
351 case NVME_CTRL_DELETING
:
363 ctrl
->state
= new_state
;
365 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
366 if (changed
&& ctrl
->state
== NVME_CTRL_LIVE
)
367 nvme_kick_requeue_lists(ctrl
);
370 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
372 static void nvme_free_ns_head(struct kref
*ref
)
374 struct nvme_ns_head
*head
=
375 container_of(ref
, struct nvme_ns_head
, ref
);
377 nvme_mpath_remove_disk(head
);
378 ida_simple_remove(&head
->subsys
->ns_ida
, head
->instance
);
379 list_del_init(&head
->entry
);
380 cleanup_srcu_struct_quiesced(&head
->srcu
);
381 nvme_put_subsystem(head
->subsys
);
385 static void nvme_put_ns_head(struct nvme_ns_head
*head
)
387 kref_put(&head
->ref
, nvme_free_ns_head
);
390 static void nvme_free_ns(struct kref
*kref
)
392 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
395 nvme_nvm_unregister(ns
);
398 nvme_put_ns_head(ns
->head
);
399 nvme_put_ctrl(ns
->ctrl
);
403 static void nvme_put_ns(struct nvme_ns
*ns
)
405 kref_put(&ns
->kref
, nvme_free_ns
);
408 static inline void nvme_clear_nvme_request(struct request
*req
)
410 if (!(req
->rq_flags
& RQF_DONTPREP
)) {
411 nvme_req(req
)->retries
= 0;
412 nvme_req(req
)->flags
= 0;
413 req
->rq_flags
|= RQF_DONTPREP
;
417 struct request
*nvme_alloc_request(struct request_queue
*q
,
418 struct nvme_command
*cmd
, blk_mq_req_flags_t flags
, int qid
)
420 unsigned op
= nvme_is_write(cmd
) ? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
;
423 if (qid
== NVME_QID_ANY
) {
424 req
= blk_mq_alloc_request(q
, op
, flags
);
426 req
= blk_mq_alloc_request_hctx(q
, op
, flags
,
432 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
433 nvme_clear_nvme_request(req
);
434 nvme_req(req
)->cmd
= cmd
;
438 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
440 static int nvme_toggle_streams(struct nvme_ctrl
*ctrl
, bool enable
)
442 struct nvme_command c
;
444 memset(&c
, 0, sizeof(c
));
446 c
.directive
.opcode
= nvme_admin_directive_send
;
447 c
.directive
.nsid
= cpu_to_le32(NVME_NSID_ALL
);
448 c
.directive
.doper
= NVME_DIR_SND_ID_OP_ENABLE
;
449 c
.directive
.dtype
= NVME_DIR_IDENTIFY
;
450 c
.directive
.tdtype
= NVME_DIR_STREAMS
;
451 c
.directive
.endir
= enable
? NVME_DIR_ENDIR
: 0;
453 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, NULL
, 0);
456 static int nvme_disable_streams(struct nvme_ctrl
*ctrl
)
458 return nvme_toggle_streams(ctrl
, false);
461 static int nvme_enable_streams(struct nvme_ctrl
*ctrl
)
463 return nvme_toggle_streams(ctrl
, true);
466 static int nvme_get_stream_params(struct nvme_ctrl
*ctrl
,
467 struct streams_directive_params
*s
, u32 nsid
)
469 struct nvme_command c
;
471 memset(&c
, 0, sizeof(c
));
472 memset(s
, 0, sizeof(*s
));
474 c
.directive
.opcode
= nvme_admin_directive_recv
;
475 c
.directive
.nsid
= cpu_to_le32(nsid
);
476 c
.directive
.numd
= cpu_to_le32((sizeof(*s
) >> 2) - 1);
477 c
.directive
.doper
= NVME_DIR_RCV_ST_OP_PARAM
;
478 c
.directive
.dtype
= NVME_DIR_STREAMS
;
480 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, s
, sizeof(*s
));
483 static int nvme_configure_directives(struct nvme_ctrl
*ctrl
)
485 struct streams_directive_params s
;
488 if (!(ctrl
->oacs
& NVME_CTRL_OACS_DIRECTIVES
))
493 ret
= nvme_enable_streams(ctrl
);
497 ret
= nvme_get_stream_params(ctrl
, &s
, NVME_NSID_ALL
);
501 ctrl
->nssa
= le16_to_cpu(s
.nssa
);
502 if (ctrl
->nssa
< BLK_MAX_WRITE_HINTS
- 1) {
503 dev_info(ctrl
->device
, "too few streams (%u) available\n",
505 nvme_disable_streams(ctrl
);
509 ctrl
->nr_streams
= min_t(unsigned, ctrl
->nssa
, BLK_MAX_WRITE_HINTS
- 1);
510 dev_info(ctrl
->device
, "Using %u streams\n", ctrl
->nr_streams
);
515 * Check if 'req' has a write hint associated with it. If it does, assign
516 * a valid namespace stream to the write.
518 static void nvme_assign_write_stream(struct nvme_ctrl
*ctrl
,
519 struct request
*req
, u16
*control
,
522 enum rw_hint streamid
= req
->write_hint
;
524 if (streamid
== WRITE_LIFE_NOT_SET
|| streamid
== WRITE_LIFE_NONE
)
528 if (WARN_ON_ONCE(streamid
> ctrl
->nr_streams
))
531 *control
|= NVME_RW_DTYPE_STREAMS
;
532 *dsmgmt
|= streamid
<< 16;
535 if (streamid
< ARRAY_SIZE(req
->q
->write_hints
))
536 req
->q
->write_hints
[streamid
] += blk_rq_bytes(req
) >> 9;
539 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
540 struct nvme_command
*cmnd
)
542 memset(cmnd
, 0, sizeof(*cmnd
));
543 cmnd
->common
.opcode
= nvme_cmd_flush
;
544 cmnd
->common
.nsid
= cpu_to_le32(ns
->head
->ns_id
);
547 static blk_status_t
nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
548 struct nvme_command
*cmnd
)
550 unsigned short segments
= blk_rq_nr_discard_segments(req
), n
= 0;
551 struct nvme_dsm_range
*range
;
555 * Some devices do not consider the DSM 'Number of Ranges' field when
556 * determining how much data to DMA. Always allocate memory for maximum
557 * number of segments to prevent device reading beyond end of buffer.
559 static const size_t alloc_size
= sizeof(*range
) * NVME_DSM_MAX_RANGES
;
561 range
= kzalloc(alloc_size
, GFP_ATOMIC
| __GFP_NOWARN
);
564 * If we fail allocation our range, fallback to the controller
565 * discard page. If that's also busy, it's safe to return
566 * busy, as we know we can make progress once that's freed.
568 if (test_and_set_bit_lock(0, &ns
->ctrl
->discard_page_busy
))
569 return BLK_STS_RESOURCE
;
571 range
= page_address(ns
->ctrl
->discard_page
);
574 __rq_for_each_bio(bio
, req
) {
575 u64 slba
= nvme_block_nr(ns
, bio
->bi_iter
.bi_sector
);
576 u32 nlb
= bio
->bi_iter
.bi_size
>> ns
->lba_shift
;
579 range
[n
].cattr
= cpu_to_le32(0);
580 range
[n
].nlb
= cpu_to_le32(nlb
);
581 range
[n
].slba
= cpu_to_le64(slba
);
586 if (WARN_ON_ONCE(n
!= segments
)) {
587 if (virt_to_page(range
) == ns
->ctrl
->discard_page
)
588 clear_bit_unlock(0, &ns
->ctrl
->discard_page_busy
);
591 return BLK_STS_IOERR
;
594 memset(cmnd
, 0, sizeof(*cmnd
));
595 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
596 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->head
->ns_id
);
597 cmnd
->dsm
.nr
= cpu_to_le32(segments
- 1);
598 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
600 req
->special_vec
.bv_page
= virt_to_page(range
);
601 req
->special_vec
.bv_offset
= offset_in_page(range
);
602 req
->special_vec
.bv_len
= alloc_size
;
603 req
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
608 static inline blk_status_t
nvme_setup_rw(struct nvme_ns
*ns
,
609 struct request
*req
, struct nvme_command
*cmnd
)
611 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
615 if (req
->cmd_flags
& REQ_FUA
)
616 control
|= NVME_RW_FUA
;
617 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
618 control
|= NVME_RW_LR
;
620 if (req
->cmd_flags
& REQ_RAHEAD
)
621 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
623 memset(cmnd
, 0, sizeof(*cmnd
));
624 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
625 cmnd
->rw
.nsid
= cpu_to_le32(ns
->head
->ns_id
);
626 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
627 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
629 if (req_op(req
) == REQ_OP_WRITE
&& ctrl
->nr_streams
)
630 nvme_assign_write_stream(ctrl
, req
, &control
, &dsmgmt
);
634 * If formated with metadata, the block layer always provides a
635 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
636 * we enable the PRACT bit for protection information or set the
637 * namespace capacity to zero to prevent any I/O.
639 if (!blk_integrity_rq(req
)) {
640 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns
)))
641 return BLK_STS_NOTSUPP
;
642 control
|= NVME_RW_PRINFO_PRACT
;
643 } else if (req_op(req
) == REQ_OP_WRITE
) {
644 t10_pi_prepare(req
, ns
->pi_type
);
647 switch (ns
->pi_type
) {
648 case NVME_NS_DPS_PI_TYPE3
:
649 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
651 case NVME_NS_DPS_PI_TYPE1
:
652 case NVME_NS_DPS_PI_TYPE2
:
653 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
654 NVME_RW_PRINFO_PRCHK_REF
;
655 cmnd
->rw
.reftag
= cpu_to_le32(t10_pi_ref_tag(req
));
660 cmnd
->rw
.control
= cpu_to_le16(control
);
661 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
665 void nvme_cleanup_cmd(struct request
*req
)
667 if (blk_integrity_rq(req
) && req_op(req
) == REQ_OP_READ
&&
668 nvme_req(req
)->status
== 0) {
669 struct nvme_ns
*ns
= req
->rq_disk
->private_data
;
671 t10_pi_complete(req
, ns
->pi_type
,
672 blk_rq_bytes(req
) >> ns
->lba_shift
);
674 if (req
->rq_flags
& RQF_SPECIAL_PAYLOAD
) {
675 struct nvme_ns
*ns
= req
->rq_disk
->private_data
;
676 struct page
*page
= req
->special_vec
.bv_page
;
678 if (page
== ns
->ctrl
->discard_page
)
679 clear_bit_unlock(0, &ns
->ctrl
->discard_page_busy
);
681 kfree(page_address(page
) + req
->special_vec
.bv_offset
);
684 EXPORT_SYMBOL_GPL(nvme_cleanup_cmd
);
686 blk_status_t
nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
687 struct nvme_command
*cmd
)
689 blk_status_t ret
= BLK_STS_OK
;
691 nvme_clear_nvme_request(req
);
693 switch (req_op(req
)) {
696 memcpy(cmd
, nvme_req(req
)->cmd
, sizeof(*cmd
));
699 nvme_setup_flush(ns
, cmd
);
701 case REQ_OP_WRITE_ZEROES
:
702 /* currently only aliased to deallocate for a few ctrls: */
704 ret
= nvme_setup_discard(ns
, req
, cmd
);
708 ret
= nvme_setup_rw(ns
, req
, cmd
);
712 return BLK_STS_IOERR
;
715 cmd
->common
.command_id
= req
->tag
;
716 trace_nvme_setup_cmd(req
, cmd
);
719 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
722 * Returns 0 on success. If the result is negative, it's a Linux error code;
723 * if the result is positive, it's an NVM Express status code
725 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
726 union nvme_result
*result
, void *buffer
, unsigned bufflen
,
727 unsigned timeout
, int qid
, int at_head
,
728 blk_mq_req_flags_t flags
)
733 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
737 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
739 if (buffer
&& bufflen
) {
740 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
745 blk_execute_rq(req
->q
, NULL
, req
, at_head
);
747 *result
= nvme_req(req
)->result
;
748 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
751 ret
= nvme_req(req
)->status
;
753 blk_mq_free_request(req
);
756 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
758 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
759 void *buffer
, unsigned bufflen
)
761 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
764 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
766 static void *nvme_add_user_metadata(struct bio
*bio
, void __user
*ubuf
,
767 unsigned len
, u32 seed
, bool write
)
769 struct bio_integrity_payload
*bip
;
773 buf
= kmalloc(len
, GFP_KERNEL
);
778 if (write
&& copy_from_user(buf
, ubuf
, len
))
781 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
787 bip
->bip_iter
.bi_size
= len
;
788 bip
->bip_iter
.bi_sector
= seed
;
789 ret
= bio_integrity_add_page(bio
, virt_to_page(buf
), len
,
790 offset_in_page(buf
));
800 static int nvme_submit_user_cmd(struct request_queue
*q
,
801 struct nvme_command
*cmd
, void __user
*ubuffer
,
802 unsigned bufflen
, void __user
*meta_buffer
, unsigned meta_len
,
803 u32 meta_seed
, u32
*result
, unsigned timeout
)
805 bool write
= nvme_is_write(cmd
);
806 struct nvme_ns
*ns
= q
->queuedata
;
807 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
809 struct bio
*bio
= NULL
;
813 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
817 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
818 nvme_req(req
)->flags
|= NVME_REQ_USERCMD
;
820 if (ubuffer
&& bufflen
) {
821 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
827 if (disk
&& meta_buffer
&& meta_len
) {
828 meta
= nvme_add_user_metadata(bio
, meta_buffer
, meta_len
,
834 req
->cmd_flags
|= REQ_INTEGRITY
;
838 blk_execute_rq(req
->q
, disk
, req
, 0);
839 if (nvme_req(req
)->flags
& NVME_REQ_CANCELLED
)
842 ret
= nvme_req(req
)->status
;
844 *result
= le32_to_cpu(nvme_req(req
)->result
.u32
);
845 if (meta
&& !ret
&& !write
) {
846 if (copy_to_user(meta_buffer
, meta
, meta_len
))
852 blk_rq_unmap_user(bio
);
854 blk_mq_free_request(req
);
858 static void nvme_keep_alive_end_io(struct request
*rq
, blk_status_t status
)
860 struct nvme_ctrl
*ctrl
= rq
->end_io_data
;
862 bool startka
= false;
864 blk_mq_free_request(rq
);
867 dev_err(ctrl
->device
,
868 "failed nvme_keep_alive_end_io error=%d\n",
873 spin_lock_irqsave(&ctrl
->lock
, flags
);
874 if (ctrl
->state
== NVME_CTRL_LIVE
||
875 ctrl
->state
== NVME_CTRL_CONNECTING
)
877 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
879 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
882 static int nvme_keep_alive(struct nvme_ctrl
*ctrl
)
886 rq
= nvme_alloc_request(ctrl
->admin_q
, &ctrl
->ka_cmd
, BLK_MQ_REQ_RESERVED
,
891 rq
->timeout
= ctrl
->kato
* HZ
;
892 rq
->end_io_data
= ctrl
;
894 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 0, nvme_keep_alive_end_io
);
899 static void nvme_keep_alive_work(struct work_struct
*work
)
901 struct nvme_ctrl
*ctrl
= container_of(to_delayed_work(work
),
902 struct nvme_ctrl
, ka_work
);
904 if (nvme_keep_alive(ctrl
)) {
905 /* allocation failure, reset the controller */
906 dev_err(ctrl
->device
, "keep-alive failed\n");
907 nvme_reset_ctrl(ctrl
);
912 static void nvme_start_keep_alive(struct nvme_ctrl
*ctrl
)
914 if (unlikely(ctrl
->kato
== 0))
917 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
920 void nvme_stop_keep_alive(struct nvme_ctrl
*ctrl
)
922 if (unlikely(ctrl
->kato
== 0))
925 cancel_delayed_work_sync(&ctrl
->ka_work
);
927 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive
);
930 * In NVMe 1.0 the CNS field was just a binary controller or namespace
931 * flag, thus sending any new CNS opcodes has a big chance of not working.
932 * Qemu unfortunately had that bug after reporting a 1.1 version compliance
933 * (but not for any later version).
935 static bool nvme_ctrl_limited_cns(struct nvme_ctrl
*ctrl
)
937 if (ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)
938 return ctrl
->vs
< NVME_VS(1, 2, 0);
939 return ctrl
->vs
< NVME_VS(1, 1, 0);
942 static int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
944 struct nvme_command c
= { };
947 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
948 c
.identify
.opcode
= nvme_admin_identify
;
949 c
.identify
.cns
= NVME_ID_CNS_CTRL
;
951 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
955 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
956 sizeof(struct nvme_id_ctrl
));
962 static int nvme_identify_ns_descs(struct nvme_ctrl
*ctrl
, unsigned nsid
,
963 struct nvme_ns_ids
*ids
)
965 struct nvme_command c
= { };
971 c
.identify
.opcode
= nvme_admin_identify
;
972 c
.identify
.nsid
= cpu_to_le32(nsid
);
973 c
.identify
.cns
= NVME_ID_CNS_NS_DESC_LIST
;
975 data
= kzalloc(NVME_IDENTIFY_DATA_SIZE
, GFP_KERNEL
);
979 status
= nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, data
,
980 NVME_IDENTIFY_DATA_SIZE
);
984 for (pos
= 0; pos
< NVME_IDENTIFY_DATA_SIZE
; pos
+= len
) {
985 struct nvme_ns_id_desc
*cur
= data
+ pos
;
991 case NVME_NIDT_EUI64
:
992 if (cur
->nidl
!= NVME_NIDT_EUI64_LEN
) {
993 dev_warn(ctrl
->device
,
994 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
998 len
= NVME_NIDT_EUI64_LEN
;
999 memcpy(ids
->eui64
, data
+ pos
+ sizeof(*cur
), len
);
1001 case NVME_NIDT_NGUID
:
1002 if (cur
->nidl
!= NVME_NIDT_NGUID_LEN
) {
1003 dev_warn(ctrl
->device
,
1004 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
1008 len
= NVME_NIDT_NGUID_LEN
;
1009 memcpy(ids
->nguid
, data
+ pos
+ sizeof(*cur
), len
);
1011 case NVME_NIDT_UUID
:
1012 if (cur
->nidl
!= NVME_NIDT_UUID_LEN
) {
1013 dev_warn(ctrl
->device
,
1014 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
1018 len
= NVME_NIDT_UUID_LEN
;
1019 uuid_copy(&ids
->uuid
, data
+ pos
+ sizeof(*cur
));
1022 /* Skip unnkown types */
1027 len
+= sizeof(*cur
);
1034 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
1036 struct nvme_command c
= { };
1038 c
.identify
.opcode
= nvme_admin_identify
;
1039 c
.identify
.cns
= NVME_ID_CNS_NS_ACTIVE_LIST
;
1040 c
.identify
.nsid
= cpu_to_le32(nsid
);
1041 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
,
1042 NVME_IDENTIFY_DATA_SIZE
);
1045 static struct nvme_id_ns
*nvme_identify_ns(struct nvme_ctrl
*ctrl
,
1048 struct nvme_id_ns
*id
;
1049 struct nvme_command c
= { };
1052 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
1053 c
.identify
.opcode
= nvme_admin_identify
;
1054 c
.identify
.nsid
= cpu_to_le32(nsid
);
1055 c
.identify
.cns
= NVME_ID_CNS_NS
;
1057 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1061 error
= nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, id
, sizeof(*id
));
1063 dev_warn(ctrl
->device
, "Identify namespace failed\n");
1071 static int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
1072 void *buffer
, size_t buflen
, u32
*result
)
1074 union nvme_result res
= { 0 };
1075 struct nvme_command c
;
1078 memset(&c
, 0, sizeof(c
));
1079 c
.features
.opcode
= nvme_admin_set_features
;
1080 c
.features
.fid
= cpu_to_le32(fid
);
1081 c
.features
.dword11
= cpu_to_le32(dword11
);
1083 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
,
1084 buffer
, buflen
, 0, NVME_QID_ANY
, 0, 0);
1085 if (ret
>= 0 && result
)
1086 *result
= le32_to_cpu(res
.u32
);
1090 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
1092 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
1094 int status
, nr_io_queues
;
1096 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, NULL
, 0,
1102 * Degraded controllers might return an error when setting the queue
1103 * count. We still want to be able to bring them online and offer
1104 * access to the admin queue, as that might be only way to fix them up.
1107 dev_err(ctrl
->device
, "Could not set queue count (%d)\n", status
);
1110 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
1111 *count
= min(*count
, nr_io_queues
);
1116 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
1118 #define NVME_AEN_SUPPORTED \
1119 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT | NVME_AEN_CFG_ANA_CHANGE)
1121 static void nvme_enable_aen(struct nvme_ctrl
*ctrl
)
1123 u32 result
, supported_aens
= ctrl
->oaes
& NVME_AEN_SUPPORTED
;
1126 if (!supported_aens
)
1129 status
= nvme_set_features(ctrl
, NVME_FEAT_ASYNC_EVENT
, supported_aens
,
1132 dev_warn(ctrl
->device
, "Failed to configure AEN (cfg %x)\n",
1136 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
1138 struct nvme_user_io io
;
1139 struct nvme_command c
;
1140 unsigned length
, meta_len
;
1141 void __user
*metadata
;
1143 if (copy_from_user(&io
, uio
, sizeof(io
)))
1148 switch (io
.opcode
) {
1149 case nvme_cmd_write
:
1151 case nvme_cmd_compare
:
1157 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
1158 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
1159 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
1164 } else if (meta_len
) {
1165 if ((io
.metadata
& 3) || !io
.metadata
)
1169 memset(&c
, 0, sizeof(c
));
1170 c
.rw
.opcode
= io
.opcode
;
1171 c
.rw
.flags
= io
.flags
;
1172 c
.rw
.nsid
= cpu_to_le32(ns
->head
->ns_id
);
1173 c
.rw
.slba
= cpu_to_le64(io
.slba
);
1174 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
1175 c
.rw
.control
= cpu_to_le16(io
.control
);
1176 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
1177 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
1178 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
1179 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
1181 return nvme_submit_user_cmd(ns
->queue
, &c
,
1182 (void __user
*)(uintptr_t)io
.addr
, length
,
1183 metadata
, meta_len
, io
.slba
, NULL
, 0);
1186 static u32
nvme_known_admin_effects(u8 opcode
)
1189 case nvme_admin_format_nvm
:
1190 return NVME_CMD_EFFECTS_CSUPP
| NVME_CMD_EFFECTS_LBCC
|
1191 NVME_CMD_EFFECTS_CSE_MASK
;
1192 case nvme_admin_sanitize_nvm
:
1193 return NVME_CMD_EFFECTS_CSE_MASK
;
1200 static u32
nvme_passthru_start(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
1207 effects
= le32_to_cpu(ctrl
->effects
->iocs
[opcode
]);
1208 if (effects
& ~NVME_CMD_EFFECTS_CSUPP
)
1209 dev_warn(ctrl
->device
,
1210 "IO command:%02x has unhandled effects:%08x\n",
1216 effects
= le32_to_cpu(ctrl
->effects
->acs
[opcode
]);
1218 effects
= nvme_known_admin_effects(opcode
);
1221 * For simplicity, IO to all namespaces is quiesced even if the command
1222 * effects say only one namespace is affected.
1224 if (effects
& (NVME_CMD_EFFECTS_LBCC
| NVME_CMD_EFFECTS_CSE_MASK
)) {
1225 mutex_lock(&ctrl
->scan_lock
);
1226 mutex_lock(&ctrl
->subsys
->lock
);
1227 nvme_mpath_start_freeze(ctrl
->subsys
);
1228 nvme_mpath_wait_freeze(ctrl
->subsys
);
1229 nvme_start_freeze(ctrl
);
1230 nvme_wait_freeze(ctrl
);
1235 static void nvme_update_formats(struct nvme_ctrl
*ctrl
)
1239 down_read(&ctrl
->namespaces_rwsem
);
1240 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
1241 if (ns
->disk
&& nvme_revalidate_disk(ns
->disk
))
1242 nvme_set_queue_dying(ns
);
1243 up_read(&ctrl
->namespaces_rwsem
);
1245 nvme_remove_invalid_namespaces(ctrl
, NVME_NSID_ALL
);
1248 static void nvme_passthru_end(struct nvme_ctrl
*ctrl
, u32 effects
)
1251 * Revalidate LBA changes prior to unfreezing. This is necessary to
1252 * prevent memory corruption if a logical block size was changed by
1255 if (effects
& NVME_CMD_EFFECTS_LBCC
)
1256 nvme_update_formats(ctrl
);
1257 if (effects
& (NVME_CMD_EFFECTS_LBCC
| NVME_CMD_EFFECTS_CSE_MASK
)) {
1258 nvme_unfreeze(ctrl
);
1259 nvme_mpath_unfreeze(ctrl
->subsys
);
1260 mutex_unlock(&ctrl
->subsys
->lock
);
1261 mutex_unlock(&ctrl
->scan_lock
);
1263 if (effects
& NVME_CMD_EFFECTS_CCC
)
1264 nvme_init_identify(ctrl
);
1265 if (effects
& (NVME_CMD_EFFECTS_NIC
| NVME_CMD_EFFECTS_NCC
))
1266 nvme_queue_scan(ctrl
);
1269 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
1270 struct nvme_passthru_cmd __user
*ucmd
)
1272 struct nvme_passthru_cmd cmd
;
1273 struct nvme_command c
;
1274 unsigned timeout
= 0;
1278 if (!capable(CAP_SYS_ADMIN
))
1280 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
1285 memset(&c
, 0, sizeof(c
));
1286 c
.common
.opcode
= cmd
.opcode
;
1287 c
.common
.flags
= cmd
.flags
;
1288 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
1289 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
1290 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
1291 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
1292 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
1293 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
1294 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
1295 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
1296 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
1299 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
1301 effects
= nvme_passthru_start(ctrl
, ns
, cmd
.opcode
);
1302 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
1303 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
1304 (void __user
*)(uintptr_t)cmd
.metadata
, cmd
.metadata_len
,
1305 0, &cmd
.result
, timeout
);
1306 nvme_passthru_end(ctrl
, effects
);
1309 if (put_user(cmd
.result
, &ucmd
->result
))
1317 * Issue ioctl requests on the first available path. Note that unlike normal
1318 * block layer requests we will not retry failed request on another controller.
1320 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
,
1321 struct nvme_ns_head
**head
, int *srcu_idx
)
1323 #ifdef CONFIG_NVME_MULTIPATH
1324 if (disk
->fops
== &nvme_ns_head_ops
) {
1327 *head
= disk
->private_data
;
1328 *srcu_idx
= srcu_read_lock(&(*head
)->srcu
);
1329 ns
= nvme_find_path(*head
);
1331 srcu_read_unlock(&(*head
)->srcu
, *srcu_idx
);
1337 return disk
->private_data
;
1340 static void nvme_put_ns_from_disk(struct nvme_ns_head
*head
, int idx
)
1343 srcu_read_unlock(&head
->srcu
, idx
);
1346 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
1347 unsigned int cmd
, unsigned long arg
)
1349 struct nvme_ns_head
*head
= NULL
;
1350 void __user
*argp
= (void __user
*)arg
;
1354 ns
= nvme_get_ns_from_disk(bdev
->bd_disk
, &head
, &srcu_idx
);
1356 return -EWOULDBLOCK
;
1359 * Handle ioctls that apply to the controller instead of the namespace
1360 * seperately and drop the ns SRCU reference early. This avoids a
1361 * deadlock when deleting namespaces using the passthrough interface.
1363 if (cmd
== NVME_IOCTL_ADMIN_CMD
|| is_sed_ioctl(cmd
)) {
1364 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1366 nvme_get_ctrl(ns
->ctrl
);
1367 nvme_put_ns_from_disk(head
, srcu_idx
);
1369 if (cmd
== NVME_IOCTL_ADMIN_CMD
)
1370 ret
= nvme_user_cmd(ctrl
, NULL
, argp
);
1372 ret
= sed_ioctl(ctrl
->opal_dev
, cmd
, argp
);
1374 nvme_put_ctrl(ctrl
);
1380 force_successful_syscall_return();
1381 ret
= ns
->head
->ns_id
;
1383 case NVME_IOCTL_IO_CMD
:
1384 ret
= nvme_user_cmd(ns
->ctrl
, ns
, argp
);
1386 case NVME_IOCTL_SUBMIT_IO
:
1387 ret
= nvme_submit_io(ns
, argp
);
1391 ret
= nvme_nvm_ioctl(ns
, cmd
, arg
);
1396 nvme_put_ns_from_disk(head
, srcu_idx
);
1400 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
1402 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1404 #ifdef CONFIG_NVME_MULTIPATH
1405 /* should never be called due to GENHD_FL_HIDDEN */
1406 if (WARN_ON_ONCE(ns
->head
->disk
))
1409 if (!kref_get_unless_zero(&ns
->kref
))
1411 if (!try_module_get(ns
->ctrl
->ops
->module
))
1422 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
1424 struct nvme_ns
*ns
= disk
->private_data
;
1426 module_put(ns
->ctrl
->ops
->module
);
1430 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1432 /* some standard values */
1433 geo
->heads
= 1 << 6;
1434 geo
->sectors
= 1 << 5;
1435 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
1439 #ifdef CONFIG_BLK_DEV_INTEGRITY
1440 static void nvme_init_integrity(struct gendisk
*disk
, u16 ms
, u8 pi_type
)
1442 struct blk_integrity integrity
;
1444 memset(&integrity
, 0, sizeof(integrity
));
1446 case NVME_NS_DPS_PI_TYPE3
:
1447 integrity
.profile
= &t10_pi_type3_crc
;
1448 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
1449 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
1451 case NVME_NS_DPS_PI_TYPE1
:
1452 case NVME_NS_DPS_PI_TYPE2
:
1453 integrity
.profile
= &t10_pi_type1_crc
;
1454 integrity
.tag_size
= sizeof(u16
);
1455 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
1458 integrity
.profile
= NULL
;
1461 integrity
.tuple_size
= ms
;
1462 blk_integrity_register(disk
, &integrity
);
1463 blk_queue_max_integrity_segments(disk
->queue
, 1);
1466 static void nvme_init_integrity(struct gendisk
*disk
, u16 ms
, u8 pi_type
)
1469 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1471 static void nvme_set_chunk_size(struct nvme_ns
*ns
)
1473 u32 chunk_size
= (((u32
)ns
->noiob
) << (ns
->lba_shift
- 9));
1474 blk_queue_chunk_sectors(ns
->queue
, rounddown_pow_of_two(chunk_size
));
1477 static void nvme_config_discard(struct nvme_ns
*ns
)
1479 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1480 struct request_queue
*queue
= ns
->queue
;
1481 u32 size
= queue_logical_block_size(queue
);
1483 if (!(ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)) {
1484 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
, queue
);
1488 if (ctrl
->nr_streams
&& ns
->sws
&& ns
->sgs
)
1489 size
*= ns
->sws
* ns
->sgs
;
1491 BUILD_BUG_ON(PAGE_SIZE
/ sizeof(struct nvme_dsm_range
) <
1492 NVME_DSM_MAX_RANGES
);
1494 queue
->limits
.discard_alignment
= 0;
1495 queue
->limits
.discard_granularity
= size
;
1497 /* If discard is already enabled, don't reset queue limits */
1498 if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD
, queue
))
1501 blk_queue_max_discard_sectors(queue
, UINT_MAX
);
1502 blk_queue_max_discard_segments(queue
, NVME_DSM_MAX_RANGES
);
1504 if (ctrl
->quirks
& NVME_QUIRK_DEALLOCATE_ZEROES
)
1505 blk_queue_max_write_zeroes_sectors(queue
, UINT_MAX
);
1508 static void nvme_report_ns_ids(struct nvme_ctrl
*ctrl
, unsigned int nsid
,
1509 struct nvme_id_ns
*id
, struct nvme_ns_ids
*ids
)
1511 memset(ids
, 0, sizeof(*ids
));
1513 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1514 memcpy(ids
->eui64
, id
->eui64
, sizeof(id
->eui64
));
1515 if (ctrl
->vs
>= NVME_VS(1, 2, 0))
1516 memcpy(ids
->nguid
, id
->nguid
, sizeof(id
->nguid
));
1517 if (ctrl
->vs
>= NVME_VS(1, 3, 0)) {
1518 /* Don't treat error as fatal we potentially
1519 * already have a NGUID or EUI-64
1521 if (nvme_identify_ns_descs(ctrl
, nsid
, ids
))
1522 dev_warn(ctrl
->device
,
1523 "%s: Identify Descriptors failed\n", __func__
);
1527 static bool nvme_ns_ids_valid(struct nvme_ns_ids
*ids
)
1529 return !uuid_is_null(&ids
->uuid
) ||
1530 memchr_inv(ids
->nguid
, 0, sizeof(ids
->nguid
)) ||
1531 memchr_inv(ids
->eui64
, 0, sizeof(ids
->eui64
));
1534 static bool nvme_ns_ids_equal(struct nvme_ns_ids
*a
, struct nvme_ns_ids
*b
)
1536 return uuid_equal(&a
->uuid
, &b
->uuid
) &&
1537 memcmp(&a
->nguid
, &b
->nguid
, sizeof(a
->nguid
)) == 0 &&
1538 memcmp(&a
->eui64
, &b
->eui64
, sizeof(a
->eui64
)) == 0;
1541 static void nvme_update_disk_info(struct gendisk
*disk
,
1542 struct nvme_ns
*ns
, struct nvme_id_ns
*id
)
1544 sector_t capacity
= le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9);
1545 unsigned short bs
= 1 << ns
->lba_shift
;
1547 if (ns
->lba_shift
> PAGE_SHIFT
) {
1548 /* unsupported block size, set capacity to 0 later */
1551 blk_mq_freeze_queue(disk
->queue
);
1552 blk_integrity_unregister(disk
);
1554 blk_queue_logical_block_size(disk
->queue
, bs
);
1555 blk_queue_physical_block_size(disk
->queue
, bs
);
1556 blk_queue_io_min(disk
->queue
, bs
);
1558 if (ns
->ms
&& !ns
->ext
&&
1559 (ns
->ctrl
->ops
->flags
& NVME_F_METADATA_SUPPORTED
))
1560 nvme_init_integrity(disk
, ns
->ms
, ns
->pi_type
);
1561 if ((ns
->ms
&& !nvme_ns_has_pi(ns
) && !blk_get_integrity(disk
)) ||
1562 ns
->lba_shift
> PAGE_SHIFT
)
1565 set_capacity(disk
, capacity
);
1566 nvme_config_discard(ns
);
1568 if (id
->nsattr
& (1 << 0))
1569 set_disk_ro(disk
, true);
1571 set_disk_ro(disk
, false);
1573 blk_mq_unfreeze_queue(disk
->queue
);
1576 static void __nvme_revalidate_disk(struct gendisk
*disk
, struct nvme_id_ns
*id
)
1578 struct nvme_ns
*ns
= disk
->private_data
;
1581 * If identify namespace failed, use default 512 byte block size so
1582 * block layer can use before failing read/write for 0 capacity.
1584 ns
->lba_shift
= id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ds
;
1585 if (ns
->lba_shift
== 0)
1587 ns
->noiob
= le16_to_cpu(id
->noiob
);
1588 ns
->ms
= le16_to_cpu(id
->lbaf
[id
->flbas
& NVME_NS_FLBAS_LBA_MASK
].ms
);
1589 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
1590 /* the PI implementation requires metadata equal t10 pi tuple size */
1591 if (ns
->ms
== sizeof(struct t10_pi_tuple
))
1592 ns
->pi_type
= id
->dps
& NVME_NS_DPS_PI_MASK
;
1597 nvme_set_chunk_size(ns
);
1598 nvme_update_disk_info(disk
, ns
, id
);
1600 nvme_nvm_update_nvm_info(ns
);
1601 #ifdef CONFIG_NVME_MULTIPATH
1602 if (ns
->head
->disk
) {
1603 nvme_update_disk_info(ns
->head
->disk
, ns
, id
);
1604 blk_queue_stack_limits(ns
->head
->disk
->queue
, ns
->queue
);
1605 revalidate_disk(ns
->head
->disk
);
1610 static int nvme_revalidate_disk(struct gendisk
*disk
)
1612 struct nvme_ns
*ns
= disk
->private_data
;
1613 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1614 struct nvme_id_ns
*id
;
1615 struct nvme_ns_ids ids
;
1618 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
1619 set_capacity(disk
, 0);
1623 id
= nvme_identify_ns(ctrl
, ns
->head
->ns_id
);
1627 if (id
->ncap
== 0) {
1632 __nvme_revalidate_disk(disk
, id
);
1633 nvme_report_ns_ids(ctrl
, ns
->head
->ns_id
, id
, &ids
);
1634 if (!nvme_ns_ids_equal(&ns
->head
->ids
, &ids
)) {
1635 dev_err(ctrl
->device
,
1636 "identifiers changed for nsid %d\n", ns
->head
->ns_id
);
1645 static char nvme_pr_type(enum pr_type type
)
1648 case PR_WRITE_EXCLUSIVE
:
1650 case PR_EXCLUSIVE_ACCESS
:
1652 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
1654 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
1656 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
1658 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
1665 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
1666 u64 key
, u64 sa_key
, u8 op
)
1668 struct nvme_ns_head
*head
= NULL
;
1670 struct nvme_command c
;
1672 u8 data
[16] = { 0, };
1674 ns
= nvme_get_ns_from_disk(bdev
->bd_disk
, &head
, &srcu_idx
);
1676 return -EWOULDBLOCK
;
1678 put_unaligned_le64(key
, &data
[0]);
1679 put_unaligned_le64(sa_key
, &data
[8]);
1681 memset(&c
, 0, sizeof(c
));
1682 c
.common
.opcode
= op
;
1683 c
.common
.nsid
= cpu_to_le32(ns
->head
->ns_id
);
1684 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
1686 ret
= nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
1687 nvme_put_ns_from_disk(head
, srcu_idx
);
1691 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
1692 u64
new, unsigned flags
)
1696 if (flags
& ~PR_FL_IGNORE_KEY
)
1699 cdw10
= old
? 2 : 0;
1700 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
1701 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
1702 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
1705 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
1706 enum pr_type type
, unsigned flags
)
1710 if (flags
& ~PR_FL_IGNORE_KEY
)
1713 cdw10
= nvme_pr_type(type
) << 8;
1714 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
1715 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
1718 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
1719 enum pr_type type
, bool abort
)
1721 u32 cdw10
= nvme_pr_type(type
) << 8 | (abort
? 2 : 1);
1722 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
1725 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
1727 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
1728 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
1731 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1733 u32 cdw10
= nvme_pr_type(type
) << 8 | (key
? 1 << 3 : 0);
1734 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
1737 static const struct pr_ops nvme_pr_ops
= {
1738 .pr_register
= nvme_pr_register
,
1739 .pr_reserve
= nvme_pr_reserve
,
1740 .pr_release
= nvme_pr_release
,
1741 .pr_preempt
= nvme_pr_preempt
,
1742 .pr_clear
= nvme_pr_clear
,
1745 #ifdef CONFIG_BLK_SED_OPAL
1746 int nvme_sec_submit(void *data
, u16 spsp
, u8 secp
, void *buffer
, size_t len
,
1749 struct nvme_ctrl
*ctrl
= data
;
1750 struct nvme_command cmd
;
1752 memset(&cmd
, 0, sizeof(cmd
));
1754 cmd
.common
.opcode
= nvme_admin_security_send
;
1756 cmd
.common
.opcode
= nvme_admin_security_recv
;
1757 cmd
.common
.nsid
= 0;
1758 cmd
.common
.cdw10
[0] = cpu_to_le32(((u32
)secp
) << 24 | ((u32
)spsp
) << 8);
1759 cmd
.common
.cdw10
[1] = cpu_to_le32(len
);
1761 return __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, buffer
, len
,
1762 ADMIN_TIMEOUT
, NVME_QID_ANY
, 1, 0);
1764 EXPORT_SYMBOL_GPL(nvme_sec_submit
);
1765 #endif /* CONFIG_BLK_SED_OPAL */
1767 static const struct block_device_operations nvme_fops
= {
1768 .owner
= THIS_MODULE
,
1769 .ioctl
= nvme_ioctl
,
1770 .compat_ioctl
= nvme_ioctl
,
1772 .release
= nvme_release
,
1773 .getgeo
= nvme_getgeo
,
1774 .revalidate_disk
= nvme_revalidate_disk
,
1775 .pr_ops
= &nvme_pr_ops
,
1778 #ifdef CONFIG_NVME_MULTIPATH
1779 static int nvme_ns_head_open(struct block_device
*bdev
, fmode_t mode
)
1781 struct nvme_ns_head
*head
= bdev
->bd_disk
->private_data
;
1783 if (!kref_get_unless_zero(&head
->ref
))
1788 static void nvme_ns_head_release(struct gendisk
*disk
, fmode_t mode
)
1790 nvme_put_ns_head(disk
->private_data
);
1793 const struct block_device_operations nvme_ns_head_ops
= {
1794 .owner
= THIS_MODULE
,
1795 .open
= nvme_ns_head_open
,
1796 .release
= nvme_ns_head_release
,
1797 .ioctl
= nvme_ioctl
,
1798 .compat_ioctl
= nvme_ioctl
,
1799 .getgeo
= nvme_getgeo
,
1800 .pr_ops
= &nvme_pr_ops
,
1802 #endif /* CONFIG_NVME_MULTIPATH */
1804 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
1806 unsigned long timeout
=
1807 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
1808 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
1811 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1814 if ((csts
& NVME_CSTS_RDY
) == bit
)
1818 if (fatal_signal_pending(current
))
1820 if (time_after(jiffies
, timeout
)) {
1821 dev_err(ctrl
->device
,
1822 "Device not ready; aborting %s\n", enabled
?
1823 "initialisation" : "reset");
1832 * If the device has been passed off to us in an enabled state, just clear
1833 * the enabled bit. The spec says we should set the 'shutdown notification
1834 * bits', but doing so may cause the device to complete commands to the
1835 * admin queue ... and we don't know what memory that might be pointing at!
1837 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1841 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1842 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1844 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1848 if (ctrl
->quirks
& NVME_QUIRK_DELAY_BEFORE_CHK_RDY
)
1849 msleep(NVME_QUIRK_DELAY_AMOUNT
);
1851 return nvme_wait_ready(ctrl
, cap
, false);
1853 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1855 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1858 * Default to a 4K page size, with the intention to update this
1859 * path in the future to accomodate architectures with differing
1860 * kernel and IO page sizes.
1862 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1865 if (page_shift
< dev_page_min
) {
1866 dev_err(ctrl
->device
,
1867 "Minimum device page size %u too large for host (%u)\n",
1868 1 << dev_page_min
, 1 << page_shift
);
1872 ctrl
->page_size
= 1 << page_shift
;
1874 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1875 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1876 ctrl
->ctrl_config
|= NVME_CC_AMS_RR
| NVME_CC_SHN_NONE
;
1877 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1878 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1880 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1883 return nvme_wait_ready(ctrl
, cap
, true);
1885 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1887 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1889 unsigned long timeout
= jiffies
+ (ctrl
->shutdown_timeout
* HZ
);
1893 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1894 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1896 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1900 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1901 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1905 if (fatal_signal_pending(current
))
1907 if (time_after(jiffies
, timeout
)) {
1908 dev_err(ctrl
->device
,
1909 "Device shutdown incomplete; abort shutdown\n");
1916 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1918 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1919 struct request_queue
*q
)
1923 if (ctrl
->max_hw_sectors
) {
1925 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1927 max_segments
= min_not_zero(max_segments
, ctrl
->max_segments
);
1928 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1929 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1931 if ((ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
) &&
1932 is_power_of_2(ctrl
->max_hw_sectors
))
1933 blk_queue_chunk_sectors(q
, ctrl
->max_hw_sectors
);
1934 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1935 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1937 blk_queue_write_cache(q
, vwc
, vwc
);
1940 static int nvme_configure_timestamp(struct nvme_ctrl
*ctrl
)
1945 if (!(ctrl
->oncs
& NVME_CTRL_ONCS_TIMESTAMP
))
1948 ts
= cpu_to_le64(ktime_to_ms(ktime_get_real()));
1949 ret
= nvme_set_features(ctrl
, NVME_FEAT_TIMESTAMP
, 0, &ts
, sizeof(ts
),
1952 dev_warn_once(ctrl
->device
,
1953 "could not set timestamp (%d)\n", ret
);
1957 static int nvme_configure_apst(struct nvme_ctrl
*ctrl
)
1960 * APST (Autonomous Power State Transition) lets us program a
1961 * table of power state transitions that the controller will
1962 * perform automatically. We configure it with a simple
1963 * heuristic: we are willing to spend at most 2% of the time
1964 * transitioning between power states. Therefore, when running
1965 * in any given state, we will enter the next lower-power
1966 * non-operational state after waiting 50 * (enlat + exlat)
1967 * microseconds, as long as that state's exit latency is under
1968 * the requested maximum latency.
1970 * We will not autonomously enter any non-operational state for
1971 * which the total latency exceeds ps_max_latency_us. Users
1972 * can set ps_max_latency_us to zero to turn off APST.
1976 struct nvme_feat_auto_pst
*table
;
1982 * If APST isn't supported or if we haven't been initialized yet,
1983 * then don't do anything.
1988 if (ctrl
->npss
> 31) {
1989 dev_warn(ctrl
->device
, "NPSS is invalid; not using APST\n");
1993 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
1997 if (!ctrl
->apst_enabled
|| ctrl
->ps_max_latency_us
== 0) {
1998 /* Turn off APST. */
2000 dev_dbg(ctrl
->device
, "APST disabled\n");
2002 __le64 target
= cpu_to_le64(0);
2006 * Walk through all states from lowest- to highest-power.
2007 * According to the spec, lower-numbered states use more
2008 * power. NPSS, despite the name, is the index of the
2009 * lowest-power state, not the number of states.
2011 for (state
= (int)ctrl
->npss
; state
>= 0; state
--) {
2012 u64 total_latency_us
, exit_latency_us
, transition_ms
;
2015 table
->entries
[state
] = target
;
2018 * Don't allow transitions to the deepest state
2019 * if it's quirked off.
2021 if (state
== ctrl
->npss
&&
2022 (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
))
2026 * Is this state a useful non-operational state for
2027 * higher-power states to autonomously transition to?
2029 if (!(ctrl
->psd
[state
].flags
&
2030 NVME_PS_FLAGS_NON_OP_STATE
))
2034 (u64
)le32_to_cpu(ctrl
->psd
[state
].exit_lat
);
2035 if (exit_latency_us
> ctrl
->ps_max_latency_us
)
2040 le32_to_cpu(ctrl
->psd
[state
].entry_lat
);
2043 * This state is good. Use it as the APST idle
2044 * target for higher power states.
2046 transition_ms
= total_latency_us
+ 19;
2047 do_div(transition_ms
, 20);
2048 if (transition_ms
> (1 << 24) - 1)
2049 transition_ms
= (1 << 24) - 1;
2051 target
= cpu_to_le64((state
<< 3) |
2052 (transition_ms
<< 8));
2057 if (total_latency_us
> max_lat_us
)
2058 max_lat_us
= total_latency_us
;
2064 dev_dbg(ctrl
->device
, "APST enabled but no non-operational states are available\n");
2066 dev_dbg(ctrl
->device
, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
2067 max_ps
, max_lat_us
, (int)sizeof(*table
), table
);
2071 ret
= nvme_set_features(ctrl
, NVME_FEAT_AUTO_PST
, apste
,
2072 table
, sizeof(*table
), NULL
);
2074 dev_err(ctrl
->device
, "failed to set APST feature (%d)\n", ret
);
2080 static void nvme_set_latency_tolerance(struct device
*dev
, s32 val
)
2082 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2086 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
:
2087 case PM_QOS_LATENCY_ANY
:
2095 if (ctrl
->ps_max_latency_us
!= latency
) {
2096 ctrl
->ps_max_latency_us
= latency
;
2097 nvme_configure_apst(ctrl
);
2101 struct nvme_core_quirk_entry
{
2103 * NVMe model and firmware strings are padded with spaces. For
2104 * simplicity, strings in the quirk table are padded with NULLs
2110 unsigned long quirks
;
2113 static const struct nvme_core_quirk_entry core_quirks
[] = {
2116 * This Toshiba device seems to die using any APST states. See:
2117 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
2120 .mn
= "THNSF5256GPUK TOSHIBA",
2121 .quirks
= NVME_QUIRK_NO_APST
,
2125 /* match is null-terminated but idstr is space-padded. */
2126 static bool string_matches(const char *idstr
, const char *match
, size_t len
)
2133 matchlen
= strlen(match
);
2134 WARN_ON_ONCE(matchlen
> len
);
2136 if (memcmp(idstr
, match
, matchlen
))
2139 for (; matchlen
< len
; matchlen
++)
2140 if (idstr
[matchlen
] != ' ')
2146 static bool quirk_matches(const struct nvme_id_ctrl
*id
,
2147 const struct nvme_core_quirk_entry
*q
)
2149 return q
->vid
== le16_to_cpu(id
->vid
) &&
2150 string_matches(id
->mn
, q
->mn
, sizeof(id
->mn
)) &&
2151 string_matches(id
->fr
, q
->fr
, sizeof(id
->fr
));
2154 static void nvme_init_subnqn(struct nvme_subsystem
*subsys
, struct nvme_ctrl
*ctrl
,
2155 struct nvme_id_ctrl
*id
)
2160 nqnlen
= strnlen(id
->subnqn
, NVMF_NQN_SIZE
);
2161 if (nqnlen
> 0 && nqnlen
< NVMF_NQN_SIZE
) {
2162 strncpy(subsys
->subnqn
, id
->subnqn
, NVMF_NQN_SIZE
);
2166 if (ctrl
->vs
>= NVME_VS(1, 2, 1))
2167 dev_warn(ctrl
->device
, "missing or invalid SUBNQN field.\n");
2169 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
2170 off
= snprintf(subsys
->subnqn
, NVMF_NQN_SIZE
,
2171 "nqn.2014.08.org.nvmexpress:%04x%04x",
2172 le16_to_cpu(id
->vid
), le16_to_cpu(id
->ssvid
));
2173 memcpy(subsys
->subnqn
+ off
, id
->sn
, sizeof(id
->sn
));
2174 off
+= sizeof(id
->sn
);
2175 memcpy(subsys
->subnqn
+ off
, id
->mn
, sizeof(id
->mn
));
2176 off
+= sizeof(id
->mn
);
2177 memset(subsys
->subnqn
+ off
, 0, sizeof(subsys
->subnqn
) - off
);
2180 static void __nvme_release_subsystem(struct nvme_subsystem
*subsys
)
2182 ida_simple_remove(&nvme_subsystems_ida
, subsys
->instance
);
2186 static void nvme_release_subsystem(struct device
*dev
)
2188 __nvme_release_subsystem(container_of(dev
, struct nvme_subsystem
, dev
));
2191 static void nvme_destroy_subsystem(struct kref
*ref
)
2193 struct nvme_subsystem
*subsys
=
2194 container_of(ref
, struct nvme_subsystem
, ref
);
2196 mutex_lock(&nvme_subsystems_lock
);
2197 list_del(&subsys
->entry
);
2198 mutex_unlock(&nvme_subsystems_lock
);
2200 ida_destroy(&subsys
->ns_ida
);
2201 device_del(&subsys
->dev
);
2202 put_device(&subsys
->dev
);
2205 static void nvme_put_subsystem(struct nvme_subsystem
*subsys
)
2207 kref_put(&subsys
->ref
, nvme_destroy_subsystem
);
2210 static struct nvme_subsystem
*__nvme_find_get_subsystem(const char *subsysnqn
)
2212 struct nvme_subsystem
*subsys
;
2214 lockdep_assert_held(&nvme_subsystems_lock
);
2217 * Fail matches for discovery subsystems. This results
2218 * in each discovery controller bound to a unique subsystem.
2219 * This avoids issues with validating controller values
2220 * that can only be true when there is a single unique subsystem.
2221 * There may be multiple and completely independent entities
2222 * that provide discovery controllers.
2224 if (!strcmp(subsysnqn
, NVME_DISC_SUBSYS_NAME
))
2227 list_for_each_entry(subsys
, &nvme_subsystems
, entry
) {
2228 if (strcmp(subsys
->subnqn
, subsysnqn
))
2230 if (!kref_get_unless_zero(&subsys
->ref
))
2238 #define SUBSYS_ATTR_RO(_name, _mode, _show) \
2239 struct device_attribute subsys_attr_##_name = \
2240 __ATTR(_name, _mode, _show, NULL)
2242 static ssize_t
nvme_subsys_show_nqn(struct device
*dev
,
2243 struct device_attribute
*attr
,
2246 struct nvme_subsystem
*subsys
=
2247 container_of(dev
, struct nvme_subsystem
, dev
);
2249 return snprintf(buf
, PAGE_SIZE
, "%s\n", subsys
->subnqn
);
2251 static SUBSYS_ATTR_RO(subsysnqn
, S_IRUGO
, nvme_subsys_show_nqn
);
2253 #define nvme_subsys_show_str_function(field) \
2254 static ssize_t subsys_##field##_show(struct device *dev, \
2255 struct device_attribute *attr, char *buf) \
2257 struct nvme_subsystem *subsys = \
2258 container_of(dev, struct nvme_subsystem, dev); \
2259 return sprintf(buf, "%.*s\n", \
2260 (int)sizeof(subsys->field), subsys->field); \
2262 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2264 nvme_subsys_show_str_function(model
);
2265 nvme_subsys_show_str_function(serial
);
2266 nvme_subsys_show_str_function(firmware_rev
);
2268 static struct attribute
*nvme_subsys_attrs
[] = {
2269 &subsys_attr_model
.attr
,
2270 &subsys_attr_serial
.attr
,
2271 &subsys_attr_firmware_rev
.attr
,
2272 &subsys_attr_subsysnqn
.attr
,
2276 static struct attribute_group nvme_subsys_attrs_group
= {
2277 .attrs
= nvme_subsys_attrs
,
2280 static const struct attribute_group
*nvme_subsys_attrs_groups
[] = {
2281 &nvme_subsys_attrs_group
,
2285 static int nvme_active_ctrls(struct nvme_subsystem
*subsys
)
2288 struct nvme_ctrl
*ctrl
;
2290 mutex_lock(&subsys
->lock
);
2291 list_for_each_entry(ctrl
, &subsys
->ctrls
, subsys_entry
) {
2292 if (ctrl
->state
!= NVME_CTRL_DELETING
&&
2293 ctrl
->state
!= NVME_CTRL_DEAD
)
2296 mutex_unlock(&subsys
->lock
);
2301 static int nvme_init_subsystem(struct nvme_ctrl
*ctrl
, struct nvme_id_ctrl
*id
)
2303 struct nvme_subsystem
*subsys
, *found
;
2306 subsys
= kzalloc(sizeof(*subsys
), GFP_KERNEL
);
2309 ret
= ida_simple_get(&nvme_subsystems_ida
, 0, 0, GFP_KERNEL
);
2314 subsys
->instance
= ret
;
2315 mutex_init(&subsys
->lock
);
2316 kref_init(&subsys
->ref
);
2317 INIT_LIST_HEAD(&subsys
->ctrls
);
2318 INIT_LIST_HEAD(&subsys
->nsheads
);
2319 nvme_init_subnqn(subsys
, ctrl
, id
);
2320 memcpy(subsys
->serial
, id
->sn
, sizeof(subsys
->serial
));
2321 memcpy(subsys
->model
, id
->mn
, sizeof(subsys
->model
));
2322 memcpy(subsys
->firmware_rev
, id
->fr
, sizeof(subsys
->firmware_rev
));
2323 subsys
->vendor_id
= le16_to_cpu(id
->vid
);
2324 subsys
->cmic
= id
->cmic
;
2326 subsys
->dev
.class = nvme_subsys_class
;
2327 subsys
->dev
.release
= nvme_release_subsystem
;
2328 subsys
->dev
.groups
= nvme_subsys_attrs_groups
;
2329 dev_set_name(&subsys
->dev
, "nvme-subsys%d", subsys
->instance
);
2330 device_initialize(&subsys
->dev
);
2332 mutex_lock(&nvme_subsystems_lock
);
2333 found
= __nvme_find_get_subsystem(subsys
->subnqn
);
2336 * Verify that the subsystem actually supports multiple
2337 * controllers, else bail out.
2339 if (!(ctrl
->opts
&& ctrl
->opts
->discovery_nqn
) &&
2340 nvme_active_ctrls(found
) && !(id
->cmic
& (1 << 1))) {
2341 dev_err(ctrl
->device
,
2342 "ignoring ctrl due to duplicate subnqn (%s).\n",
2344 nvme_put_subsystem(found
);
2349 __nvme_release_subsystem(subsys
);
2352 ret
= device_add(&subsys
->dev
);
2354 dev_err(ctrl
->device
,
2355 "failed to register subsystem device.\n");
2358 ida_init(&subsys
->ns_ida
);
2359 list_add_tail(&subsys
->entry
, &nvme_subsystems
);
2362 ctrl
->subsys
= subsys
;
2363 mutex_unlock(&nvme_subsystems_lock
);
2365 if (sysfs_create_link(&subsys
->dev
.kobj
, &ctrl
->device
->kobj
,
2366 dev_name(ctrl
->device
))) {
2367 dev_err(ctrl
->device
,
2368 "failed to create sysfs link from subsystem.\n");
2369 /* the transport driver will eventually put the subsystem */
2373 mutex_lock(&subsys
->lock
);
2374 list_add_tail(&ctrl
->subsys_entry
, &subsys
->ctrls
);
2375 mutex_unlock(&subsys
->lock
);
2380 mutex_unlock(&nvme_subsystems_lock
);
2381 put_device(&subsys
->dev
);
2385 int nvme_get_log(struct nvme_ctrl
*ctrl
, u32 nsid
, u8 log_page
, u8 lsp
,
2386 void *log
, size_t size
, u64 offset
)
2388 struct nvme_command c
= { };
2389 unsigned long dwlen
= size
/ 4 - 1;
2391 c
.get_log_page
.opcode
= nvme_admin_get_log_page
;
2392 c
.get_log_page
.nsid
= cpu_to_le32(nsid
);
2393 c
.get_log_page
.lid
= log_page
;
2394 c
.get_log_page
.lsp
= lsp
;
2395 c
.get_log_page
.numdl
= cpu_to_le16(dwlen
& ((1 << 16) - 1));
2396 c
.get_log_page
.numdu
= cpu_to_le16(dwlen
>> 16);
2397 c
.get_log_page
.lpol
= cpu_to_le32(lower_32_bits(offset
));
2398 c
.get_log_page
.lpou
= cpu_to_le32(upper_32_bits(offset
));
2400 return nvme_submit_sync_cmd(ctrl
->admin_q
, &c
, log
, size
);
2403 static int nvme_get_effects_log(struct nvme_ctrl
*ctrl
)
2408 ctrl
->effects
= kzalloc(sizeof(*ctrl
->effects
), GFP_KERNEL
);
2413 ret
= nvme_get_log(ctrl
, NVME_NSID_ALL
, NVME_LOG_CMD_EFFECTS
, 0,
2414 ctrl
->effects
, sizeof(*ctrl
->effects
), 0);
2416 kfree(ctrl
->effects
);
2417 ctrl
->effects
= NULL
;
2423 * Initialize the cached copies of the Identify data and various controller
2424 * register in our nvme_ctrl structure. This should be called as soon as
2425 * the admin queue is fully up and running.
2427 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
2429 struct nvme_id_ctrl
*id
;
2431 int ret
, page_shift
;
2433 bool prev_apst_enabled
;
2435 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
2437 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
2441 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
2443 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
2446 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
2448 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
2449 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
2451 ret
= nvme_identify_ctrl(ctrl
, &id
);
2453 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
2457 if (id
->lpa
& NVME_CTRL_LPA_CMD_EFFECTS_LOG
) {
2458 ret
= nvme_get_effects_log(ctrl
);
2463 if (!ctrl
->identified
) {
2466 ret
= nvme_init_subsystem(ctrl
, id
);
2471 * Check for quirks. Quirk can depend on firmware version,
2472 * so, in principle, the set of quirks present can change
2473 * across a reset. As a possible future enhancement, we
2474 * could re-scan for quirks every time we reinitialize
2475 * the device, but we'd have to make sure that the driver
2476 * behaves intelligently if the quirks change.
2478 for (i
= 0; i
< ARRAY_SIZE(core_quirks
); i
++) {
2479 if (quirk_matches(id
, &core_quirks
[i
]))
2480 ctrl
->quirks
|= core_quirks
[i
].quirks
;
2484 if (force_apst
&& (ctrl
->quirks
& NVME_QUIRK_NO_DEEPEST_PS
)) {
2485 dev_warn(ctrl
->device
, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
2486 ctrl
->quirks
&= ~NVME_QUIRK_NO_DEEPEST_PS
;
2489 ctrl
->oacs
= le16_to_cpu(id
->oacs
);
2490 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
2491 ctrl
->oaes
= le32_to_cpu(id
->oaes
);
2492 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
2493 ctrl
->vwc
= id
->vwc
;
2494 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
2496 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
2498 max_hw_sectors
= UINT_MAX
;
2499 ctrl
->max_hw_sectors
=
2500 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
2502 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
2503 ctrl
->sgls
= le32_to_cpu(id
->sgls
);
2504 ctrl
->kas
= le16_to_cpu(id
->kas
);
2505 ctrl
->max_namespaces
= le32_to_cpu(id
->mnan
);
2509 u32 transition_time
= le32_to_cpu(id
->rtd3e
) / 1000000;
2511 ctrl
->shutdown_timeout
= clamp_t(unsigned int, transition_time
,
2512 shutdown_timeout
, 60);
2514 if (ctrl
->shutdown_timeout
!= shutdown_timeout
)
2515 dev_info(ctrl
->device
,
2516 "Shutdown timeout set to %u seconds\n",
2517 ctrl
->shutdown_timeout
);
2519 ctrl
->shutdown_timeout
= shutdown_timeout
;
2521 ctrl
->npss
= id
->npss
;
2522 ctrl
->apsta
= id
->apsta
;
2523 prev_apst_enabled
= ctrl
->apst_enabled
;
2524 if (ctrl
->quirks
& NVME_QUIRK_NO_APST
) {
2525 if (force_apst
&& id
->apsta
) {
2526 dev_warn(ctrl
->device
, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
2527 ctrl
->apst_enabled
= true;
2529 ctrl
->apst_enabled
= false;
2532 ctrl
->apst_enabled
= id
->apsta
;
2534 memcpy(ctrl
->psd
, id
->psd
, sizeof(ctrl
->psd
));
2536 if (ctrl
->ops
->flags
& NVME_F_FABRICS
) {
2537 ctrl
->icdoff
= le16_to_cpu(id
->icdoff
);
2538 ctrl
->ioccsz
= le32_to_cpu(id
->ioccsz
);
2539 ctrl
->iorcsz
= le32_to_cpu(id
->iorcsz
);
2540 ctrl
->maxcmd
= le16_to_cpu(id
->maxcmd
);
2543 * In fabrics we need to verify the cntlid matches the
2546 if (ctrl
->cntlid
!= le16_to_cpu(id
->cntlid
)) {
2551 if (!ctrl
->opts
->discovery_nqn
&& !ctrl
->kas
) {
2552 dev_err(ctrl
->device
,
2553 "keep-alive support is mandatory for fabrics\n");
2558 ctrl
->cntlid
= le16_to_cpu(id
->cntlid
);
2559 ctrl
->hmpre
= le32_to_cpu(id
->hmpre
);
2560 ctrl
->hmmin
= le32_to_cpu(id
->hmmin
);
2561 ctrl
->hmminds
= le32_to_cpu(id
->hmminds
);
2562 ctrl
->hmmaxd
= le16_to_cpu(id
->hmmaxd
);
2565 ret
= nvme_mpath_init(ctrl
, id
);
2571 if (ctrl
->apst_enabled
&& !prev_apst_enabled
)
2572 dev_pm_qos_expose_latency_tolerance(ctrl
->device
);
2573 else if (!ctrl
->apst_enabled
&& prev_apst_enabled
)
2574 dev_pm_qos_hide_latency_tolerance(ctrl
->device
);
2576 ret
= nvme_configure_apst(ctrl
);
2580 ret
= nvme_configure_timestamp(ctrl
);
2584 ret
= nvme_configure_directives(ctrl
);
2588 ctrl
->identified
= true;
2596 EXPORT_SYMBOL_GPL(nvme_init_identify
);
2598 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
2600 struct nvme_ctrl
*ctrl
=
2601 container_of(inode
->i_cdev
, struct nvme_ctrl
, cdev
);
2603 switch (ctrl
->state
) {
2604 case NVME_CTRL_LIVE
:
2605 case NVME_CTRL_ADMIN_ONLY
:
2608 return -EWOULDBLOCK
;
2611 file
->private_data
= ctrl
;
2615 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
2620 down_read(&ctrl
->namespaces_rwsem
);
2621 if (list_empty(&ctrl
->namespaces
)) {
2626 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
2627 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
2628 dev_warn(ctrl
->device
,
2629 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2634 dev_warn(ctrl
->device
,
2635 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2636 kref_get(&ns
->kref
);
2637 up_read(&ctrl
->namespaces_rwsem
);
2639 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
2644 up_read(&ctrl
->namespaces_rwsem
);
2648 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
2651 struct nvme_ctrl
*ctrl
= file
->private_data
;
2652 void __user
*argp
= (void __user
*)arg
;
2655 case NVME_IOCTL_ADMIN_CMD
:
2656 return nvme_user_cmd(ctrl
, NULL
, argp
);
2657 case NVME_IOCTL_IO_CMD
:
2658 return nvme_dev_user_cmd(ctrl
, argp
);
2659 case NVME_IOCTL_RESET
:
2660 dev_warn(ctrl
->device
, "resetting controller\n");
2661 return nvme_reset_ctrl_sync(ctrl
);
2662 case NVME_IOCTL_SUBSYS_RESET
:
2663 return nvme_reset_subsystem(ctrl
);
2664 case NVME_IOCTL_RESCAN
:
2665 nvme_queue_scan(ctrl
);
2672 static const struct file_operations nvme_dev_fops
= {
2673 .owner
= THIS_MODULE
,
2674 .open
= nvme_dev_open
,
2675 .unlocked_ioctl
= nvme_dev_ioctl
,
2676 .compat_ioctl
= nvme_dev_ioctl
,
2679 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
2680 struct device_attribute
*attr
, const char *buf
,
2683 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2686 ret
= nvme_reset_ctrl_sync(ctrl
);
2691 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
2693 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
2694 struct device_attribute
*attr
, const char *buf
,
2697 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2699 nvme_queue_scan(ctrl
);
2702 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
2704 static inline struct nvme_ns_head
*dev_to_ns_head(struct device
*dev
)
2706 struct gendisk
*disk
= dev_to_disk(dev
);
2708 if (disk
->fops
== &nvme_fops
)
2709 return nvme_get_ns_from_dev(dev
)->head
;
2711 return disk
->private_data
;
2714 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
2717 struct nvme_ns_head
*head
= dev_to_ns_head(dev
);
2718 struct nvme_ns_ids
*ids
= &head
->ids
;
2719 struct nvme_subsystem
*subsys
= head
->subsys
;
2720 int serial_len
= sizeof(subsys
->serial
);
2721 int model_len
= sizeof(subsys
->model
);
2723 if (!uuid_is_null(&ids
->uuid
))
2724 return sprintf(buf
, "uuid.%pU\n", &ids
->uuid
);
2726 if (memchr_inv(ids
->nguid
, 0, sizeof(ids
->nguid
)))
2727 return sprintf(buf
, "eui.%16phN\n", ids
->nguid
);
2729 if (memchr_inv(ids
->eui64
, 0, sizeof(ids
->eui64
)))
2730 return sprintf(buf
, "eui.%8phN\n", ids
->eui64
);
2732 while (serial_len
> 0 && (subsys
->serial
[serial_len
- 1] == ' ' ||
2733 subsys
->serial
[serial_len
- 1] == '\0'))
2735 while (model_len
> 0 && (subsys
->model
[model_len
- 1] == ' ' ||
2736 subsys
->model
[model_len
- 1] == '\0'))
2739 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", subsys
->vendor_id
,
2740 serial_len
, subsys
->serial
, model_len
, subsys
->model
,
2743 static DEVICE_ATTR_RO(wwid
);
2745 static ssize_t
nguid_show(struct device
*dev
, struct device_attribute
*attr
,
2748 return sprintf(buf
, "%pU\n", dev_to_ns_head(dev
)->ids
.nguid
);
2750 static DEVICE_ATTR_RO(nguid
);
2752 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
2755 struct nvme_ns_ids
*ids
= &dev_to_ns_head(dev
)->ids
;
2757 /* For backward compatibility expose the NGUID to userspace if
2758 * we have no UUID set
2760 if (uuid_is_null(&ids
->uuid
)) {
2761 printk_ratelimited(KERN_WARNING
2762 "No UUID available providing old NGUID\n");
2763 return sprintf(buf
, "%pU\n", ids
->nguid
);
2765 return sprintf(buf
, "%pU\n", &ids
->uuid
);
2767 static DEVICE_ATTR_RO(uuid
);
2769 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
2772 return sprintf(buf
, "%8ph\n", dev_to_ns_head(dev
)->ids
.eui64
);
2774 static DEVICE_ATTR_RO(eui
);
2776 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
2779 return sprintf(buf
, "%d\n", dev_to_ns_head(dev
)->ns_id
);
2781 static DEVICE_ATTR_RO(nsid
);
2783 static struct attribute
*nvme_ns_id_attrs
[] = {
2784 &dev_attr_wwid
.attr
,
2785 &dev_attr_uuid
.attr
,
2786 &dev_attr_nguid
.attr
,
2788 &dev_attr_nsid
.attr
,
2789 #ifdef CONFIG_NVME_MULTIPATH
2790 &dev_attr_ana_grpid
.attr
,
2791 &dev_attr_ana_state
.attr
,
2796 static umode_t
nvme_ns_id_attrs_are_visible(struct kobject
*kobj
,
2797 struct attribute
*a
, int n
)
2799 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2800 struct nvme_ns_ids
*ids
= &dev_to_ns_head(dev
)->ids
;
2802 if (a
== &dev_attr_uuid
.attr
) {
2803 if (uuid_is_null(&ids
->uuid
) &&
2804 !memchr_inv(ids
->nguid
, 0, sizeof(ids
->nguid
)))
2807 if (a
== &dev_attr_nguid
.attr
) {
2808 if (!memchr_inv(ids
->nguid
, 0, sizeof(ids
->nguid
)))
2811 if (a
== &dev_attr_eui
.attr
) {
2812 if (!memchr_inv(ids
->eui64
, 0, sizeof(ids
->eui64
)))
2815 #ifdef CONFIG_NVME_MULTIPATH
2816 if (a
== &dev_attr_ana_grpid
.attr
|| a
== &dev_attr_ana_state
.attr
) {
2817 if (dev_to_disk(dev
)->fops
!= &nvme_fops
) /* per-path attr */
2819 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev
)->ctrl
))
2826 const struct attribute_group nvme_ns_id_attr_group
= {
2827 .attrs
= nvme_ns_id_attrs
,
2828 .is_visible
= nvme_ns_id_attrs_are_visible
,
2831 #define nvme_show_str_function(field) \
2832 static ssize_t field##_show(struct device *dev, \
2833 struct device_attribute *attr, char *buf) \
2835 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2836 return sprintf(buf, "%.*s\n", \
2837 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
2839 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2841 nvme_show_str_function(model
);
2842 nvme_show_str_function(serial
);
2843 nvme_show_str_function(firmware_rev
);
2845 #define nvme_show_int_function(field) \
2846 static ssize_t field##_show(struct device *dev, \
2847 struct device_attribute *attr, char *buf) \
2849 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2850 return sprintf(buf, "%d\n", ctrl->field); \
2852 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2854 nvme_show_int_function(cntlid
);
2856 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
2857 struct device_attribute
*attr
, const char *buf
,
2860 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2862 if (device_remove_file_self(dev
, attr
))
2863 nvme_delete_ctrl_sync(ctrl
);
2866 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
2868 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
2869 struct device_attribute
*attr
,
2872 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2874 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
2876 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
2878 static ssize_t
nvme_sysfs_show_state(struct device
*dev
,
2879 struct device_attribute
*attr
,
2882 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2883 static const char *const state_name
[] = {
2884 [NVME_CTRL_NEW
] = "new",
2885 [NVME_CTRL_LIVE
] = "live",
2886 [NVME_CTRL_ADMIN_ONLY
] = "only-admin",
2887 [NVME_CTRL_RESETTING
] = "resetting",
2888 [NVME_CTRL_CONNECTING
] = "connecting",
2889 [NVME_CTRL_DELETING
] = "deleting",
2890 [NVME_CTRL_DEAD
] = "dead",
2893 if ((unsigned)ctrl
->state
< ARRAY_SIZE(state_name
) &&
2894 state_name
[ctrl
->state
])
2895 return sprintf(buf
, "%s\n", state_name
[ctrl
->state
]);
2897 return sprintf(buf
, "unknown state\n");
2900 static DEVICE_ATTR(state
, S_IRUGO
, nvme_sysfs_show_state
, NULL
);
2902 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
2903 struct device_attribute
*attr
,
2906 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2908 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->subsys
->subnqn
);
2910 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
2912 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
2913 struct device_attribute
*attr
,
2916 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2918 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
2920 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
2922 static struct attribute
*nvme_dev_attrs
[] = {
2923 &dev_attr_reset_controller
.attr
,
2924 &dev_attr_rescan_controller
.attr
,
2925 &dev_attr_model
.attr
,
2926 &dev_attr_serial
.attr
,
2927 &dev_attr_firmware_rev
.attr
,
2928 &dev_attr_cntlid
.attr
,
2929 &dev_attr_delete_controller
.attr
,
2930 &dev_attr_transport
.attr
,
2931 &dev_attr_subsysnqn
.attr
,
2932 &dev_attr_address
.attr
,
2933 &dev_attr_state
.attr
,
2937 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
2938 struct attribute
*a
, int n
)
2940 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2941 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
2943 if (a
== &dev_attr_delete_controller
.attr
&& !ctrl
->ops
->delete_ctrl
)
2945 if (a
== &dev_attr_address
.attr
&& !ctrl
->ops
->get_address
)
2951 static struct attribute_group nvme_dev_attrs_group
= {
2952 .attrs
= nvme_dev_attrs
,
2953 .is_visible
= nvme_dev_attrs_are_visible
,
2956 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
2957 &nvme_dev_attrs_group
,
2961 static struct nvme_ns_head
*__nvme_find_ns_head(struct nvme_subsystem
*subsys
,
2964 struct nvme_ns_head
*h
;
2966 lockdep_assert_held(&subsys
->lock
);
2968 list_for_each_entry(h
, &subsys
->nsheads
, entry
) {
2969 if (h
->ns_id
== nsid
&& kref_get_unless_zero(&h
->ref
))
2976 static int __nvme_check_ids(struct nvme_subsystem
*subsys
,
2977 struct nvme_ns_head
*new)
2979 struct nvme_ns_head
*h
;
2981 lockdep_assert_held(&subsys
->lock
);
2983 list_for_each_entry(h
, &subsys
->nsheads
, entry
) {
2984 if (nvme_ns_ids_valid(&new->ids
) &&
2985 !list_empty(&h
->list
) &&
2986 nvme_ns_ids_equal(&new->ids
, &h
->ids
))
2993 static struct nvme_ns_head
*nvme_alloc_ns_head(struct nvme_ctrl
*ctrl
,
2994 unsigned nsid
, struct nvme_id_ns
*id
)
2996 struct nvme_ns_head
*head
;
2999 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
3002 ret
= ida_simple_get(&ctrl
->subsys
->ns_ida
, 1, 0, GFP_KERNEL
);
3005 head
->instance
= ret
;
3006 INIT_LIST_HEAD(&head
->list
);
3007 ret
= init_srcu_struct(&head
->srcu
);
3009 goto out_ida_remove
;
3010 head
->subsys
= ctrl
->subsys
;
3012 kref_init(&head
->ref
);
3014 nvme_report_ns_ids(ctrl
, nsid
, id
, &head
->ids
);
3016 ret
= __nvme_check_ids(ctrl
->subsys
, head
);
3018 dev_err(ctrl
->device
,
3019 "duplicate IDs for nsid %d\n", nsid
);
3020 goto out_cleanup_srcu
;
3023 ret
= nvme_mpath_alloc_disk(ctrl
, head
);
3025 goto out_cleanup_srcu
;
3027 list_add_tail(&head
->entry
, &ctrl
->subsys
->nsheads
);
3029 kref_get(&ctrl
->subsys
->ref
);
3033 cleanup_srcu_struct(&head
->srcu
);
3035 ida_simple_remove(&ctrl
->subsys
->ns_ida
, head
->instance
);
3039 return ERR_PTR(ret
);
3042 static int nvme_init_ns_head(struct nvme_ns
*ns
, unsigned nsid
,
3043 struct nvme_id_ns
*id
)
3045 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
3046 bool is_shared
= id
->nmic
& (1 << 0);
3047 struct nvme_ns_head
*head
= NULL
;
3050 mutex_lock(&ctrl
->subsys
->lock
);
3052 head
= __nvme_find_ns_head(ctrl
->subsys
, nsid
);
3054 head
= nvme_alloc_ns_head(ctrl
, nsid
, id
);
3056 ret
= PTR_ERR(head
);
3060 struct nvme_ns_ids ids
;
3062 nvme_report_ns_ids(ctrl
, nsid
, id
, &ids
);
3063 if (!nvme_ns_ids_equal(&head
->ids
, &ids
)) {
3064 dev_err(ctrl
->device
,
3065 "IDs don't match for shared namespace %d\n",
3072 list_add_tail(&ns
->siblings
, &head
->list
);
3076 mutex_unlock(&ctrl
->subsys
->lock
);
3080 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
3082 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
3083 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
3085 return nsa
->head
->ns_id
- nsb
->head
->ns_id
;
3088 static struct nvme_ns
*nvme_find_get_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
3090 struct nvme_ns
*ns
, *ret
= NULL
;
3092 down_read(&ctrl
->namespaces_rwsem
);
3093 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
3094 if (ns
->head
->ns_id
== nsid
) {
3095 if (!kref_get_unless_zero(&ns
->kref
))
3100 if (ns
->head
->ns_id
> nsid
)
3103 up_read(&ctrl
->namespaces_rwsem
);
3107 static int nvme_setup_streams_ns(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
)
3109 struct streams_directive_params s
;
3112 if (!ctrl
->nr_streams
)
3115 ret
= nvme_get_stream_params(ctrl
, &s
, ns
->head
->ns_id
);
3119 ns
->sws
= le32_to_cpu(s
.sws
);
3120 ns
->sgs
= le16_to_cpu(s
.sgs
);
3123 unsigned int bs
= 1 << ns
->lba_shift
;
3125 blk_queue_io_min(ns
->queue
, bs
* ns
->sws
);
3127 blk_queue_io_opt(ns
->queue
, bs
* ns
->sws
* ns
->sgs
);
3133 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
3136 struct gendisk
*disk
;
3137 struct nvme_id_ns
*id
;
3138 char disk_name
[DISK_NAME_LEN
];
3139 int node
= dev_to_node(ctrl
->dev
), flags
= GENHD_FL_EXT_DEVT
;
3141 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
3145 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
3146 if (IS_ERR(ns
->queue
))
3148 blk_queue_flag_set(QUEUE_FLAG_NONROT
, ns
->queue
);
3149 ns
->queue
->queuedata
= ns
;
3152 kref_init(&ns
->kref
);
3153 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
3155 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
3156 nvme_set_queue_limits(ctrl
, ns
->queue
);
3158 id
= nvme_identify_ns(ctrl
, nsid
);
3160 goto out_free_queue
;
3165 if (nvme_init_ns_head(ns
, nsid
, id
))
3167 nvme_setup_streams_ns(ctrl
, ns
);
3168 nvme_set_disk_name(disk_name
, ns
, ctrl
, &flags
);
3170 if ((ctrl
->quirks
& NVME_QUIRK_LIGHTNVM
) && id
->vs
[0] == 0x1) {
3171 if (nvme_nvm_register(ns
, disk_name
, node
)) {
3172 dev_warn(ctrl
->device
, "LightNVM init failure\n");
3177 disk
= alloc_disk_node(0, node
);
3181 disk
->fops
= &nvme_fops
;
3182 disk
->private_data
= ns
;
3183 disk
->queue
= ns
->queue
;
3184 disk
->flags
= flags
;
3185 memcpy(disk
->disk_name
, disk_name
, DISK_NAME_LEN
);
3188 __nvme_revalidate_disk(disk
, id
);
3190 down_write(&ctrl
->namespaces_rwsem
);
3191 list_add_tail(&ns
->list
, &ctrl
->namespaces
);
3192 up_write(&ctrl
->namespaces_rwsem
);
3194 nvme_get_ctrl(ctrl
);
3196 device_add_disk(ctrl
->device
, ns
->disk
);
3197 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
3198 &nvme_ns_id_attr_group
))
3199 pr_warn("%s: failed to create sysfs group for identification\n",
3200 ns
->disk
->disk_name
);
3201 if (ns
->ndev
&& nvme_nvm_register_sysfs(ns
))
3202 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
3203 ns
->disk
->disk_name
);
3205 nvme_mpath_add_disk(ns
, id
);
3206 nvme_fault_inject_init(ns
);
3211 mutex_lock(&ctrl
->subsys
->lock
);
3212 list_del_rcu(&ns
->siblings
);
3213 mutex_unlock(&ctrl
->subsys
->lock
);
3217 blk_cleanup_queue(ns
->queue
);
3222 static void nvme_ns_remove(struct nvme_ns
*ns
)
3224 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
3227 nvme_fault_inject_fini(ns
);
3229 mutex_lock(&ns
->ctrl
->subsys
->lock
);
3230 list_del_rcu(&ns
->siblings
);
3231 mutex_unlock(&ns
->ctrl
->subsys
->lock
);
3232 synchronize_rcu(); /* guarantee not available in head->list */
3233 nvme_mpath_clear_current_path(ns
);
3234 synchronize_srcu(&ns
->head
->srcu
); /* wait for concurrent submissions */
3236 if (ns
->disk
&& ns
->disk
->flags
& GENHD_FL_UP
) {
3237 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
3238 &nvme_ns_id_attr_group
);
3240 nvme_nvm_unregister_sysfs(ns
);
3241 del_gendisk(ns
->disk
);
3242 blk_cleanup_queue(ns
->queue
);
3243 if (blk_get_integrity(ns
->disk
))
3244 blk_integrity_unregister(ns
->disk
);
3247 down_write(&ns
->ctrl
->namespaces_rwsem
);
3248 list_del_init(&ns
->list
);
3249 up_write(&ns
->ctrl
->namespaces_rwsem
);
3251 nvme_mpath_check_last_path(ns
);
3255 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
3259 ns
= nvme_find_get_ns(ctrl
, nsid
);
3261 if (ns
->disk
&& revalidate_disk(ns
->disk
))
3265 nvme_alloc_ns(ctrl
, nsid
);
3268 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
3271 struct nvme_ns
*ns
, *next
;
3274 down_write(&ctrl
->namespaces_rwsem
);
3275 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
3276 if (ns
->head
->ns_id
> nsid
|| test_bit(NVME_NS_DEAD
, &ns
->flags
))
3277 list_move_tail(&ns
->list
, &rm_list
);
3279 up_write(&ctrl
->namespaces_rwsem
);
3281 list_for_each_entry_safe(ns
, next
, &rm_list
, list
)
3286 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
3290 unsigned i
, j
, nsid
, prev
= 0;
3291 unsigned num_lists
= DIV_ROUND_UP_ULL((u64
)nn
, 1024);
3294 ns_list
= kzalloc(NVME_IDENTIFY_DATA_SIZE
, GFP_KERNEL
);
3298 for (i
= 0; i
< num_lists
; i
++) {
3299 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
3303 for (j
= 0; j
< min(nn
, 1024U); j
++) {
3304 nsid
= le32_to_cpu(ns_list
[j
]);
3308 nvme_validate_ns(ctrl
, nsid
);
3310 while (++prev
< nsid
) {
3311 ns
= nvme_find_get_ns(ctrl
, prev
);
3321 nvme_remove_invalid_namespaces(ctrl
, prev
);
3327 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
3331 for (i
= 1; i
<= nn
; i
++)
3332 nvme_validate_ns(ctrl
, i
);
3334 nvme_remove_invalid_namespaces(ctrl
, nn
);
3337 static void nvme_clear_changed_ns_log(struct nvme_ctrl
*ctrl
)
3339 size_t log_size
= NVME_MAX_CHANGED_NAMESPACES
* sizeof(__le32
);
3343 log
= kzalloc(log_size
, GFP_KERNEL
);
3348 * We need to read the log to clear the AEN, but we don't want to rely
3349 * on it for the changed namespace information as userspace could have
3350 * raced with us in reading the log page, which could cause us to miss
3353 error
= nvme_get_log(ctrl
, NVME_NSID_ALL
, NVME_LOG_CHANGED_NS
, 0, log
,
3356 dev_warn(ctrl
->device
,
3357 "reading changed ns log failed: %d\n", error
);
3362 static void nvme_scan_work(struct work_struct
*work
)
3364 struct nvme_ctrl
*ctrl
=
3365 container_of(work
, struct nvme_ctrl
, scan_work
);
3366 struct nvme_id_ctrl
*id
;
3369 if (ctrl
->state
!= NVME_CTRL_LIVE
)
3372 WARN_ON_ONCE(!ctrl
->tagset
);
3374 if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED
, &ctrl
->events
)) {
3375 dev_info(ctrl
->device
, "rescanning namespaces.\n");
3376 nvme_clear_changed_ns_log(ctrl
);
3379 if (nvme_identify_ctrl(ctrl
, &id
))
3382 mutex_lock(&ctrl
->scan_lock
);
3383 nn
= le32_to_cpu(id
->nn
);
3384 if (!nvme_ctrl_limited_cns(ctrl
)) {
3385 if (!nvme_scan_ns_list(ctrl
, nn
))
3388 nvme_scan_ns_sequential(ctrl
, nn
);
3390 mutex_unlock(&ctrl
->scan_lock
);
3392 down_write(&ctrl
->namespaces_rwsem
);
3393 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
3394 up_write(&ctrl
->namespaces_rwsem
);
3398 * This function iterates the namespace list unlocked to allow recovery from
3399 * controller failure. It is up to the caller to ensure the namespace list is
3400 * not modified by scan work while this function is executing.
3402 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
3404 struct nvme_ns
*ns
, *next
;
3407 /* prevent racing with ns scanning */
3408 flush_work(&ctrl
->scan_work
);
3411 * The dead states indicates the controller was not gracefully
3412 * disconnected. In that case, we won't be able to flush any data while
3413 * removing the namespaces' disks; fail all the queues now to avoid
3414 * potentially having to clean up the failed sync later.
3416 if (ctrl
->state
== NVME_CTRL_DEAD
)
3417 nvme_kill_queues(ctrl
);
3419 down_write(&ctrl
->namespaces_rwsem
);
3420 list_splice_init(&ctrl
->namespaces
, &ns_list
);
3421 up_write(&ctrl
->namespaces_rwsem
);
3423 list_for_each_entry_safe(ns
, next
, &ns_list
, list
)
3426 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
3428 static void nvme_aen_uevent(struct nvme_ctrl
*ctrl
)
3430 char *envp
[2] = { NULL
, NULL
};
3431 u32 aen_result
= ctrl
->aen_result
;
3433 ctrl
->aen_result
= 0;
3437 envp
[0] = kasprintf(GFP_KERNEL
, "NVME_AEN=%#08x", aen_result
);
3440 kobject_uevent_env(&ctrl
->device
->kobj
, KOBJ_CHANGE
, envp
);
3444 static void nvme_async_event_work(struct work_struct
*work
)
3446 struct nvme_ctrl
*ctrl
=
3447 container_of(work
, struct nvme_ctrl
, async_event_work
);
3449 nvme_aen_uevent(ctrl
);
3450 ctrl
->ops
->submit_async_event(ctrl
);
3453 static bool nvme_ctrl_pp_status(struct nvme_ctrl
*ctrl
)
3458 if (ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
))
3464 return ((ctrl
->ctrl_config
& NVME_CC_ENABLE
) && (csts
& NVME_CSTS_PP
));
3467 static void nvme_get_fw_slot_info(struct nvme_ctrl
*ctrl
)
3469 struct nvme_fw_slot_info_log
*log
;
3471 log
= kmalloc(sizeof(*log
), GFP_KERNEL
);
3475 if (nvme_get_log(ctrl
, NVME_NSID_ALL
, NVME_LOG_FW_SLOT
, 0, log
,
3477 dev_warn(ctrl
->device
, "Get FW SLOT INFO log error\n");
3481 static void nvme_fw_act_work(struct work_struct
*work
)
3483 struct nvme_ctrl
*ctrl
= container_of(work
,
3484 struct nvme_ctrl
, fw_act_work
);
3485 unsigned long fw_act_timeout
;
3488 fw_act_timeout
= jiffies
+
3489 msecs_to_jiffies(ctrl
->mtfa
* 100);
3491 fw_act_timeout
= jiffies
+
3492 msecs_to_jiffies(admin_timeout
* 1000);
3494 nvme_stop_queues(ctrl
);
3495 while (nvme_ctrl_pp_status(ctrl
)) {
3496 if (time_after(jiffies
, fw_act_timeout
)) {
3497 dev_warn(ctrl
->device
,
3498 "Fw activation timeout, reset controller\n");
3499 nvme_reset_ctrl(ctrl
);
3505 if (ctrl
->state
!= NVME_CTRL_LIVE
)
3508 nvme_start_queues(ctrl
);
3509 /* read FW slot information to clear the AER */
3510 nvme_get_fw_slot_info(ctrl
);
3513 static void nvme_handle_aen_notice(struct nvme_ctrl
*ctrl
, u32 result
)
3515 switch ((result
& 0xff00) >> 8) {
3516 case NVME_AER_NOTICE_NS_CHANGED
:
3517 set_bit(NVME_AER_NOTICE_NS_CHANGED
, &ctrl
->events
);
3518 nvme_queue_scan(ctrl
);
3520 case NVME_AER_NOTICE_FW_ACT_STARTING
:
3521 queue_work(nvme_wq
, &ctrl
->fw_act_work
);
3523 #ifdef CONFIG_NVME_MULTIPATH
3524 case NVME_AER_NOTICE_ANA
:
3525 if (!ctrl
->ana_log_buf
)
3527 queue_work(nvme_wq
, &ctrl
->ana_work
);
3531 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
3535 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
, __le16 status
,
3536 volatile union nvme_result
*res
)
3538 u32 result
= le32_to_cpu(res
->u32
);
3540 if (le16_to_cpu(status
) >> 1 != NVME_SC_SUCCESS
)
3543 switch (result
& 0x7) {
3544 case NVME_AER_NOTICE
:
3545 nvme_handle_aen_notice(ctrl
, result
);
3547 case NVME_AER_ERROR
:
3548 case NVME_AER_SMART
:
3551 ctrl
->aen_result
= result
;
3556 queue_work(nvme_wq
, &ctrl
->async_event_work
);
3558 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
3560 void nvme_stop_ctrl(struct nvme_ctrl
*ctrl
)
3562 nvme_mpath_stop(ctrl
);
3563 nvme_stop_keep_alive(ctrl
);
3564 flush_work(&ctrl
->async_event_work
);
3565 cancel_work_sync(&ctrl
->fw_act_work
);
3566 if (ctrl
->ops
->stop_ctrl
)
3567 ctrl
->ops
->stop_ctrl(ctrl
);
3569 EXPORT_SYMBOL_GPL(nvme_stop_ctrl
);
3571 void nvme_start_ctrl(struct nvme_ctrl
*ctrl
)
3574 nvme_start_keep_alive(ctrl
);
3576 if (ctrl
->queue_count
> 1) {
3577 nvme_queue_scan(ctrl
);
3578 nvme_enable_aen(ctrl
);
3579 queue_work(nvme_wq
, &ctrl
->async_event_work
);
3580 nvme_start_queues(ctrl
);
3583 EXPORT_SYMBOL_GPL(nvme_start_ctrl
);
3585 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
3587 dev_pm_qos_hide_latency_tolerance(ctrl
->device
);
3588 cdev_device_del(&ctrl
->cdev
, ctrl
->device
);
3590 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
3592 static void nvme_free_ctrl(struct device
*dev
)
3594 struct nvme_ctrl
*ctrl
=
3595 container_of(dev
, struct nvme_ctrl
, ctrl_device
);
3596 struct nvme_subsystem
*subsys
= ctrl
->subsys
;
3598 ida_simple_remove(&nvme_instance_ida
, ctrl
->instance
);
3599 kfree(ctrl
->effects
);
3600 nvme_mpath_uninit(ctrl
);
3601 __free_page(ctrl
->discard_page
);
3604 mutex_lock(&subsys
->lock
);
3605 list_del(&ctrl
->subsys_entry
);
3606 mutex_unlock(&subsys
->lock
);
3607 sysfs_remove_link(&subsys
->dev
.kobj
, dev_name(ctrl
->device
));
3610 ctrl
->ops
->free_ctrl(ctrl
);
3613 nvme_put_subsystem(subsys
);
3617 * Initialize a NVMe controller structures. This needs to be called during
3618 * earliest initialization so that we have the initialized structured around
3621 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
3622 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
3626 ctrl
->state
= NVME_CTRL_NEW
;
3627 spin_lock_init(&ctrl
->lock
);
3628 mutex_init(&ctrl
->scan_lock
);
3629 INIT_LIST_HEAD(&ctrl
->namespaces
);
3630 init_rwsem(&ctrl
->namespaces_rwsem
);
3633 ctrl
->quirks
= quirks
;
3634 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
3635 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
3636 INIT_WORK(&ctrl
->fw_act_work
, nvme_fw_act_work
);
3637 INIT_WORK(&ctrl
->delete_work
, nvme_delete_ctrl_work
);
3639 INIT_DELAYED_WORK(&ctrl
->ka_work
, nvme_keep_alive_work
);
3640 memset(&ctrl
->ka_cmd
, 0, sizeof(ctrl
->ka_cmd
));
3641 ctrl
->ka_cmd
.common
.opcode
= nvme_admin_keep_alive
;
3643 BUILD_BUG_ON(NVME_DSM_MAX_RANGES
* sizeof(struct nvme_dsm_range
) >
3645 ctrl
->discard_page
= alloc_page(GFP_KERNEL
);
3646 if (!ctrl
->discard_page
) {
3651 ret
= ida_simple_get(&nvme_instance_ida
, 0, 0, GFP_KERNEL
);
3654 ctrl
->instance
= ret
;
3656 device_initialize(&ctrl
->ctrl_device
);
3657 ctrl
->device
= &ctrl
->ctrl_device
;
3658 ctrl
->device
->devt
= MKDEV(MAJOR(nvme_chr_devt
), ctrl
->instance
);
3659 ctrl
->device
->class = nvme_class
;
3660 ctrl
->device
->parent
= ctrl
->dev
;
3661 ctrl
->device
->groups
= nvme_dev_attr_groups
;
3662 ctrl
->device
->release
= nvme_free_ctrl
;
3663 dev_set_drvdata(ctrl
->device
, ctrl
);
3664 ret
= dev_set_name(ctrl
->device
, "nvme%d", ctrl
->instance
);
3666 goto out_release_instance
;
3668 cdev_init(&ctrl
->cdev
, &nvme_dev_fops
);
3669 ctrl
->cdev
.owner
= ops
->module
;
3670 ret
= cdev_device_add(&ctrl
->cdev
, ctrl
->device
);
3675 * Initialize latency tolerance controls. The sysfs files won't
3676 * be visible to userspace unless the device actually supports APST.
3678 ctrl
->device
->power
.set_latency_tolerance
= nvme_set_latency_tolerance
;
3679 dev_pm_qos_update_user_latency_tolerance(ctrl
->device
,
3680 min(default_ps_max_latency_us
, (unsigned long)S32_MAX
));
3684 kfree_const(ctrl
->device
->kobj
.name
);
3685 out_release_instance
:
3686 ida_simple_remove(&nvme_instance_ida
, ctrl
->instance
);
3688 if (ctrl
->discard_page
)
3689 __free_page(ctrl
->discard_page
);
3692 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
3695 * nvme_kill_queues(): Ends all namespace queues
3696 * @ctrl: the dead controller that needs to end
3698 * Call this function when the driver determines it is unable to get the
3699 * controller in a state capable of servicing IO.
3701 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
3705 down_read(&ctrl
->namespaces_rwsem
);
3707 /* Forcibly unquiesce queues to avoid blocking dispatch */
3708 if (ctrl
->admin_q
&& !blk_queue_dying(ctrl
->admin_q
))
3709 blk_mq_unquiesce_queue(ctrl
->admin_q
);
3711 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3712 nvme_set_queue_dying(ns
);
3714 up_read(&ctrl
->namespaces_rwsem
);
3716 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
3718 void nvme_unfreeze(struct nvme_ctrl
*ctrl
)
3722 down_read(&ctrl
->namespaces_rwsem
);
3723 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3724 blk_mq_unfreeze_queue(ns
->queue
);
3725 up_read(&ctrl
->namespaces_rwsem
);
3727 EXPORT_SYMBOL_GPL(nvme_unfreeze
);
3729 void nvme_wait_freeze_timeout(struct nvme_ctrl
*ctrl
, long timeout
)
3733 down_read(&ctrl
->namespaces_rwsem
);
3734 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
3735 timeout
= blk_mq_freeze_queue_wait_timeout(ns
->queue
, timeout
);
3739 up_read(&ctrl
->namespaces_rwsem
);
3741 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout
);
3743 void nvme_wait_freeze(struct nvme_ctrl
*ctrl
)
3747 down_read(&ctrl
->namespaces_rwsem
);
3748 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3749 blk_mq_freeze_queue_wait(ns
->queue
);
3750 up_read(&ctrl
->namespaces_rwsem
);
3752 EXPORT_SYMBOL_GPL(nvme_wait_freeze
);
3754 void nvme_start_freeze(struct nvme_ctrl
*ctrl
)
3758 down_read(&ctrl
->namespaces_rwsem
);
3759 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3760 blk_freeze_queue_start(ns
->queue
);
3761 up_read(&ctrl
->namespaces_rwsem
);
3763 EXPORT_SYMBOL_GPL(nvme_start_freeze
);
3765 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
3769 down_read(&ctrl
->namespaces_rwsem
);
3770 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3771 blk_mq_quiesce_queue(ns
->queue
);
3772 up_read(&ctrl
->namespaces_rwsem
);
3774 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
3776 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
3780 down_read(&ctrl
->namespaces_rwsem
);
3781 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
3782 blk_mq_unquiesce_queue(ns
->queue
);
3783 up_read(&ctrl
->namespaces_rwsem
);
3785 EXPORT_SYMBOL_GPL(nvme_start_queues
);
3787 int __init
nvme_core_init(void)
3789 int result
= -ENOMEM
;
3791 nvme_wq
= alloc_workqueue("nvme-wq",
3792 WQ_UNBOUND
| WQ_MEM_RECLAIM
| WQ_SYSFS
, 0);
3796 nvme_reset_wq
= alloc_workqueue("nvme-reset-wq",
3797 WQ_UNBOUND
| WQ_MEM_RECLAIM
| WQ_SYSFS
, 0);
3801 nvme_delete_wq
= alloc_workqueue("nvme-delete-wq",
3802 WQ_UNBOUND
| WQ_MEM_RECLAIM
| WQ_SYSFS
, 0);
3803 if (!nvme_delete_wq
)
3804 goto destroy_reset_wq
;
3806 result
= alloc_chrdev_region(&nvme_chr_devt
, 0, NVME_MINORS
, "nvme");
3808 goto destroy_delete_wq
;
3810 nvme_class
= class_create(THIS_MODULE
, "nvme");
3811 if (IS_ERR(nvme_class
)) {
3812 result
= PTR_ERR(nvme_class
);
3813 goto unregister_chrdev
;
3816 nvme_subsys_class
= class_create(THIS_MODULE
, "nvme-subsystem");
3817 if (IS_ERR(nvme_subsys_class
)) {
3818 result
= PTR_ERR(nvme_subsys_class
);
3824 class_destroy(nvme_class
);
3826 unregister_chrdev_region(nvme_chr_devt
, NVME_MINORS
);
3828 destroy_workqueue(nvme_delete_wq
);
3830 destroy_workqueue(nvme_reset_wq
);
3832 destroy_workqueue(nvme_wq
);
3837 void nvme_core_exit(void)
3839 ida_destroy(&nvme_subsystems_ida
);
3840 class_destroy(nvme_subsys_class
);
3841 class_destroy(nvme_class
);
3842 unregister_chrdev_region(nvme_chr_devt
, NVME_MINORS
);
3843 destroy_workqueue(nvme_delete_wq
);
3844 destroy_workqueue(nvme_reset_wq
);
3845 destroy_workqueue(nvme_wq
);
3848 MODULE_LICENSE("GPL");
3849 MODULE_VERSION("1.0");
3850 module_init(nvme_core_init
);
3851 module_exit(nvme_core_exit
);