]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/nvme/host/nvme.h
nvme: move block_device_operations and ns/ctrl freeing to common code
[mirror_ubuntu-jammy-kernel.git] / drivers / nvme / host / nvme.h
CommitLineData
f11bb3e2
CH
1/*
2 * Copyright (c) 2011-2014, Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 */
13
14#ifndef _NVME_H
15#define _NVME_H
16
17#include <linux/nvme.h>
18#include <linux/pci.h>
19#include <linux/kref.h>
20#include <linux/blk-mq.h>
21
1673f1f0
CH
22struct nvme_passthru_cmd;
23
f11bb3e2
CH
24extern unsigned char nvme_io_timeout;
25#define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
26
21d34711
CH
27extern unsigned char admin_timeout;
28#define ADMIN_TIMEOUT (admin_timeout * HZ)
29
ca064085
MB
30enum {
31 NVME_NS_LBA = 0,
32 NVME_NS_LIGHTNVM = 1,
33};
34
1c63dc66
CH
35struct nvme_ctrl {
36 const struct nvme_ctrl_ops *ops;
f11bb3e2 37 struct request_queue *admin_q;
f11bb3e2 38 struct device *dev;
1673f1f0 39 struct kref kref;
f11bb3e2 40 int instance;
1c63dc66 41
f11bb3e2
CH
42 char name[12];
43 char serial[20];
44 char model[40];
45 char firmware_rev[8];
f11bb3e2
CH
46 u16 oncs;
47 u16 abort_limit;
48 u8 event_limit;
49 u8 vwc;
50};
51
52/*
53 * An NVM Express namespace is equivalent to a SCSI LUN
54 */
55struct nvme_ns {
56 struct list_head list;
57
1c63dc66 58 struct nvme_ctrl *ctrl;
f11bb3e2
CH
59 struct request_queue *queue;
60 struct gendisk *disk;
61 struct kref kref;
62
63 unsigned ns_id;
64 int lba_shift;
65 u16 ms;
66 bool ext;
67 u8 pi_type;
ca064085 68 int type;
f11bb3e2
CH
69 u64 mode_select_num_blocks;
70 u32 mode_select_block_len;
71};
72
1c63dc66
CH
73struct nvme_ctrl_ops {
74 int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
1673f1f0 75 void (*free_ctrl)(struct nvme_ctrl *ctrl);
1c63dc66
CH
76};
77
78static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
79{
80 u32 val = 0;
81
82 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
83 return false;
84 return val & NVME_CSTS_RDY;
85}
86
f11bb3e2
CH
87static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
88{
89 return (sector >> (ns->lba_shift - 9));
90}
91
22944e99
CH
92static inline void nvme_setup_flush(struct nvme_ns *ns,
93 struct nvme_command *cmnd)
94{
95 memset(cmnd, 0, sizeof(*cmnd));
96 cmnd->common.opcode = nvme_cmd_flush;
97 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
98}
99
100static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
101 struct nvme_command *cmnd)
102{
103 u16 control = 0;
104 u32 dsmgmt = 0;
105
106 if (req->cmd_flags & REQ_FUA)
107 control |= NVME_RW_FUA;
108 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
109 control |= NVME_RW_LR;
110
111 if (req->cmd_flags & REQ_RAHEAD)
112 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
113
114 memset(cmnd, 0, sizeof(*cmnd));
115 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
116 cmnd->rw.command_id = req->tag;
117 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
118 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
119 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
120
121 if (ns->ms) {
122 switch (ns->pi_type) {
123 case NVME_NS_DPS_PI_TYPE3:
124 control |= NVME_RW_PRINFO_PRCHK_GUARD;
125 break;
126 case NVME_NS_DPS_PI_TYPE1:
127 case NVME_NS_DPS_PI_TYPE2:
128 control |= NVME_RW_PRINFO_PRCHK_GUARD |
129 NVME_RW_PRINFO_PRCHK_REF;
130 cmnd->rw.reftag = cpu_to_le32(
131 nvme_block_nr(ns, blk_rq_pos(req)));
132 break;
133 }
134 if (!blk_integrity_rq(req))
135 control |= NVME_RW_PRINFO_PRACT;
136 }
137
138 cmnd->rw.control = cpu_to_le16(control);
139 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
140}
141
142
15a190f7
CH
143static inline int nvme_error_status(u16 status)
144{
145 switch (status & 0x7ff) {
146 case NVME_SC_SUCCESS:
147 return 0;
148 case NVME_SC_CAP_EXCEEDED:
149 return -ENOSPC;
150 default:
151 return -EIO;
152 }
153}
154
1673f1f0
CH
155void nvme_put_ctrl(struct nvme_ctrl *ctrl);
156void nvme_put_ns(struct nvme_ns *ns);
157
4160982e
CH
158struct request *nvme_alloc_request(struct request_queue *q,
159 struct nvme_command *cmd, unsigned int flags);
f11bb3e2
CH
160int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
161 void *buf, unsigned bufflen);
162int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
4160982e
CH
163 void *buffer, unsigned bufflen, u32 *result, unsigned timeout);
164int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
165 void __user *ubuffer, unsigned bufflen, u32 *result,
166 unsigned timeout);
0b7f1f26
KB
167int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
168 void __user *ubuffer, unsigned bufflen,
169 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
170 u32 *result, unsigned timeout);
1c63dc66
CH
171int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id);
172int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
f11bb3e2 173 struct nvme_id_ns **id);
1c63dc66
CH
174int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log);
175int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
f11bb3e2 176 dma_addr_t dma_addr, u32 *result);
1c63dc66 177int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
f11bb3e2
CH
178 dma_addr_t dma_addr, u32 *result);
179
1673f1f0
CH
180extern const struct block_device_operations nvme_fops;
181extern spinlock_t dev_list_lock;
182
183int nvme_revalidate_disk(struct gendisk *disk);
184int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
185 struct nvme_passthru_cmd __user *ucmd);
186
f11bb3e2
CH
187struct sg_io_hdr;
188
189int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
190int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
191int nvme_sg_get_version_num(int __user *ip);
192
ca064085
MB
193int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
194int nvme_nvm_register(struct request_queue *q, char *disk_name);
195void nvme_nvm_unregister(struct request_queue *q, char *disk_name);
196
f11bb3e2 197#endif /* _NVME_H */