1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4 */ 5 6 #ifndef _NVMET_H 7 #define _NVMET_H 8 9 #include <linux/dma-mapping.h> 10 #include <linux/types.h> 11 #include <linux/device.h> 12 #include <linux/kref.h> 13 #include <linux/percpu-refcount.h> 14 #include <linux/list.h> 15 #include <linux/mutex.h> 16 #include <linux/uuid.h> 17 #include <linux/nvme.h> 18 #include <linux/configfs.h> 19 #include <linux/rcupdate.h> 20 #include <linux/blkdev.h> 21 #include <linux/radix-tree.h> 22 23 #define NVMET_ASYNC_EVENTS 4 24 #define NVMET_ERROR_LOG_SLOTS 128 25 #define NVMET_NO_ERROR_LOC ((u16)-1) 26 #define NVMET_DEFAULT_CTRL_MODEL "Linux" 27 28 /* 29 * Supported optional AENs: 30 */ 31 #define NVMET_AEN_CFG_OPTIONAL \ 32 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE) 33 #define NVMET_DISC_AEN_CFG_OPTIONAL \ 34 (NVME_AEN_CFG_DISC_CHANGE) 35 36 /* 37 * Plus mandatory SMART AENs (we'll never send them, but allow enabling them): 38 */ 39 #define NVMET_AEN_CFG_ALL \ 40 (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \ 41 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \ 42 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL) 43 44 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 45 * The 16 bit shift is to set IATTR bit to 1, which means offending 46 * offset starts in the data section of connect() 47 */ 48 #define IPO_IATTR_CONNECT_DATA(x) \ 49 (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 50 #define IPO_IATTR_CONNECT_SQE(x) \ 51 (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 52 53 struct nvmet_ns { 54 struct list_head dev_link; 55 struct percpu_ref ref; 56 struct block_device *bdev; 57 struct file *file; 58 bool readonly; 59 u32 nsid; 60 u32 blksize_shift; 61 loff_t size; 62 u8 nguid[16]; 63 uuid_t uuid; 64 u32 anagrpid; 65 66 bool buffered_io; 67 bool enabled; 68 struct nvmet_subsys *subsys; 69 const char *device_path; 70 71 struct config_group device_group; 72 struct config_group group; 73 74 struct completion disable_done; 75 mempool_t *bvec_pool; 76 struct kmem_cache *bvec_cache; 77 78 int use_p2pmem; 79 struct pci_dev *p2p_dev; 80 }; 81 82 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 83 { 84 return container_of(to_config_group(item), struct nvmet_ns, group); 85 } 86 87 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns) 88 { 89 return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL; 90 } 91 92 struct nvmet_cq { 93 u16 qid; 94 u16 size; 95 }; 96 97 struct nvmet_sq { 98 struct nvmet_ctrl *ctrl; 99 struct percpu_ref ref; 100 u16 qid; 101 u16 size; 102 u32 sqhd; 103 bool sqhd_disabled; 104 struct completion free_done; 105 struct completion confirm_done; 106 }; 107 108 struct nvmet_ana_group { 109 struct config_group group; 110 struct nvmet_port *port; 111 u32 grpid; 112 }; 113 114 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item) 115 { 116 return container_of(to_config_group(item), struct nvmet_ana_group, 117 group); 118 } 119 120 /** 121 * struct nvmet_port - Common structure to keep port 122 * information for the target. 123 * @entry: Entry into referrals or transport list. 124 * @disc_addr: Address information is stored in a format defined 125 * for a discovery log page entry. 126 * @group: ConfigFS group for this element's folder. 127 * @priv: Private data for the transport. 128 */ 129 struct nvmet_port { 130 struct list_head entry; 131 struct nvmf_disc_rsp_page_entry disc_addr; 132 struct config_group group; 133 struct config_group subsys_group; 134 struct list_head subsystems; 135 struct config_group referrals_group; 136 struct list_head referrals; 137 struct list_head global_entry; 138 struct config_group ana_groups_group; 139 struct nvmet_ana_group ana_default_group; 140 enum nvme_ana_state *ana_state; 141 void *priv; 142 bool enabled; 143 int inline_data_size; 144 const struct nvmet_fabrics_ops *tr_ops; 145 }; 146 147 static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 148 { 149 return container_of(to_config_group(item), struct nvmet_port, 150 group); 151 } 152 153 static inline struct nvmet_port *ana_groups_to_port( 154 struct config_item *item) 155 { 156 return container_of(to_config_group(item), struct nvmet_port, 157 ana_groups_group); 158 } 159 160 struct nvmet_ctrl { 161 struct nvmet_subsys *subsys; 162 struct nvmet_cq **cqs; 163 struct nvmet_sq **sqs; 164 165 bool cmd_seen; 166 167 struct mutex lock; 168 u64 cap; 169 u32 cc; 170 u32 csts; 171 172 uuid_t hostid; 173 u16 cntlid; 174 u32 kato; 175 176 struct nvmet_port *port; 177 178 u32 aen_enabled; 179 unsigned long aen_masked; 180 struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 181 unsigned int nr_async_event_cmds; 182 struct list_head async_events; 183 struct work_struct async_event_work; 184 185 struct list_head subsys_entry; 186 struct kref ref; 187 struct delayed_work ka_work; 188 struct work_struct fatal_err_work; 189 190 const struct nvmet_fabrics_ops *ops; 191 192 __le32 *changed_ns_list; 193 u32 nr_changed_ns; 194 195 char subsysnqn[NVMF_NQN_FIELD_LEN]; 196 char hostnqn[NVMF_NQN_FIELD_LEN]; 197 198 struct device *p2p_client; 199 struct radix_tree_root p2p_ns_map; 200 201 spinlock_t error_lock; 202 u64 err_counter; 203 struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; 204 }; 205 206 struct nvmet_subsys_model { 207 struct rcu_head rcuhead; 208 char number[]; 209 }; 210 211 struct nvmet_subsys { 212 enum nvme_subsys_type type; 213 214 struct mutex lock; 215 struct kref ref; 216 217 struct list_head namespaces; 218 unsigned int nr_namespaces; 219 unsigned int max_nsid; 220 u16 cntlid_min; 221 u16 cntlid_max; 222 223 struct list_head ctrls; 224 225 struct list_head hosts; 226 bool allow_any_host; 227 228 u16 max_qid; 229 230 u64 ver; 231 u64 serial; 232 char *subsysnqn; 233 234 struct config_group group; 235 236 struct config_group namespaces_group; 237 struct config_group allowed_hosts_group; 238 239 struct nvmet_subsys_model __rcu *model; 240 }; 241 242 static inline struct nvmet_subsys *to_subsys(struct config_item *item) 243 { 244 return container_of(to_config_group(item), struct nvmet_subsys, group); 245 } 246 247 static inline struct nvmet_subsys *namespaces_to_subsys( 248 struct config_item *item) 249 { 250 return container_of(to_config_group(item), struct nvmet_subsys, 251 namespaces_group); 252 } 253 254 struct nvmet_host { 255 struct config_group group; 256 }; 257 258 static inline struct nvmet_host *to_host(struct config_item *item) 259 { 260 return container_of(to_config_group(item), struct nvmet_host, group); 261 } 262 263 static inline char *nvmet_host_name(struct nvmet_host *host) 264 { 265 return config_item_name(&host->group.cg_item); 266 } 267 268 struct nvmet_host_link { 269 struct list_head entry; 270 struct nvmet_host *host; 271 }; 272 273 struct nvmet_subsys_link { 274 struct list_head entry; 275 struct nvmet_subsys *subsys; 276 }; 277 278 struct nvmet_req; 279 struct nvmet_fabrics_ops { 280 struct module *owner; 281 unsigned int type; 282 unsigned int msdbd; 283 bool has_keyed_sgls : 1; 284 void (*queue_response)(struct nvmet_req *req); 285 int (*add_port)(struct nvmet_port *port); 286 void (*remove_port)(struct nvmet_port *port); 287 void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 288 void (*disc_traddr)(struct nvmet_req *req, 289 struct nvmet_port *port, char *traddr); 290 u16 (*install_queue)(struct nvmet_sq *nvme_sq); 291 void (*discovery_chg)(struct nvmet_port *port); 292 u8 (*get_mdts)(const struct nvmet_ctrl *ctrl); 293 }; 294 295 #define NVMET_MAX_INLINE_BIOVEC 8 296 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE 297 298 struct nvmet_req { 299 struct nvme_command *cmd; 300 struct nvme_completion *cqe; 301 struct nvmet_sq *sq; 302 struct nvmet_cq *cq; 303 struct nvmet_ns *ns; 304 struct scatterlist *sg; 305 struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 306 union { 307 struct { 308 struct bio inline_bio; 309 } b; 310 struct { 311 bool mpool_alloc; 312 struct kiocb iocb; 313 struct bio_vec *bvec; 314 struct work_struct work; 315 } f; 316 }; 317 int sg_cnt; 318 /* data length as parsed from the SGL descriptor: */ 319 size_t transfer_len; 320 321 struct nvmet_port *port; 322 323 void (*execute)(struct nvmet_req *req); 324 const struct nvmet_fabrics_ops *ops; 325 326 struct pci_dev *p2p_dev; 327 struct device *p2p_client; 328 u16 error_loc; 329 u64 error_slba; 330 }; 331 332 extern struct workqueue_struct *buffered_io_wq; 333 334 static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 335 { 336 req->cqe->result.u32 = cpu_to_le32(result); 337 } 338 339 /* 340 * NVMe command writes actually are DMA reads for us on the target side. 341 */ 342 static inline enum dma_data_direction 343 nvmet_data_dir(struct nvmet_req *req) 344 { 345 return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 346 } 347 348 struct nvmet_async_event { 349 struct list_head entry; 350 u8 event_type; 351 u8 event_info; 352 u8 log_page; 353 }; 354 355 static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn) 356 { 357 int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15; 358 359 if (!rae) 360 clear_bit(bn, &req->sq->ctrl->aen_masked); 361 } 362 363 static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn) 364 { 365 if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn))) 366 return true; 367 return test_and_set_bit(bn, &ctrl->aen_masked); 368 } 369 370 void nvmet_get_feat_kato(struct nvmet_req *req); 371 void nvmet_get_feat_async_event(struct nvmet_req *req); 372 u16 nvmet_set_feat_kato(struct nvmet_req *req); 373 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask); 374 void nvmet_execute_async_event(struct nvmet_req *req); 375 376 u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 377 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id); 378 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req); 379 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req); 380 u16 nvmet_parse_admin_cmd(struct nvmet_req *req); 381 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req); 382 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req); 383 384 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq, 385 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops); 386 void nvmet_req_uninit(struct nvmet_req *req); 387 bool nvmet_check_data_len(struct nvmet_req *req, size_t data_len); 388 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len); 389 void nvmet_req_complete(struct nvmet_req *req, u16 status); 390 int nvmet_req_alloc_sgl(struct nvmet_req *req); 391 void nvmet_req_free_sgl(struct nvmet_req *req); 392 393 void nvmet_execute_keep_alive(struct nvmet_req *req); 394 395 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 396 u16 size); 397 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 398 u16 size); 399 void nvmet_sq_destroy(struct nvmet_sq *sq); 400 int nvmet_sq_init(struct nvmet_sq *sq); 401 402 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 403 404 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 405 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn, 406 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp); 407 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid, 408 struct nvmet_req *req, struct nvmet_ctrl **ret); 409 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 410 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd); 411 412 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 413 enum nvme_subsys_type type); 414 void nvmet_subsys_put(struct nvmet_subsys *subsys); 415 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 416 417 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid); 418 void nvmet_put_namespace(struct nvmet_ns *ns); 419 int nvmet_ns_enable(struct nvmet_ns *ns); 420 void nvmet_ns_disable(struct nvmet_ns *ns); 421 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 422 void nvmet_ns_free(struct nvmet_ns *ns); 423 424 void nvmet_send_ana_event(struct nvmet_subsys *subsys, 425 struct nvmet_port *port); 426 void nvmet_port_send_ana_event(struct nvmet_port *port); 427 428 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops); 429 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops); 430 431 void nvmet_port_del_ctrls(struct nvmet_port *port, 432 struct nvmet_subsys *subsys); 433 434 int nvmet_enable_port(struct nvmet_port *port); 435 void nvmet_disable_port(struct nvmet_port *port); 436 437 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 438 void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port); 439 440 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 441 size_t len); 442 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 443 size_t len); 444 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len); 445 446 u32 nvmet_get_log_page_len(struct nvme_command *cmd); 447 u64 nvmet_get_log_page_offset(struct nvme_command *cmd); 448 449 extern struct list_head *nvmet_ports; 450 void nvmet_port_disc_changed(struct nvmet_port *port, 451 struct nvmet_subsys *subsys); 452 void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys, 453 struct nvmet_host *host); 454 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, 455 u8 event_info, u8 log_page); 456 457 #define NVMET_QUEUE_SIZE 1024 458 #define NVMET_NR_QUEUES 128 459 #define NVMET_MAX_CMD NVMET_QUEUE_SIZE 460 461 /* 462 * Nice round number that makes a list of nsids fit into a page. 463 * Should become tunable at some point in the future. 464 */ 465 #define NVMET_MAX_NAMESPACES 1024 466 467 /* 468 * 0 is not a valid ANA group ID, so we start numbering at 1. 469 * 470 * ANA Group 1 exists without manual intervention, has namespaces assigned to it 471 * by default, and is available in an optimized state through all ports. 472 */ 473 #define NVMET_MAX_ANAGRPS 128 474 #define NVMET_DEFAULT_ANA_GRPID 1 475 476 #define NVMET_KAS 10 477 #define NVMET_DISC_KATO_MS 120000 478 479 int __init nvmet_init_configfs(void); 480 void __exit nvmet_exit_configfs(void); 481 482 int __init nvmet_init_discovery(void); 483 void nvmet_exit_discovery(void); 484 485 extern struct nvmet_subsys *nvmet_disc_subsys; 486 extern struct rw_semaphore nvmet_config_sem; 487 488 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 489 extern u64 nvmet_ana_chgcnt; 490 extern struct rw_semaphore nvmet_ana_sem; 491 492 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn); 493 494 int nvmet_bdev_ns_enable(struct nvmet_ns *ns); 495 int nvmet_file_ns_enable(struct nvmet_ns *ns); 496 void nvmet_bdev_ns_disable(struct nvmet_ns *ns); 497 void nvmet_file_ns_disable(struct nvmet_ns *ns); 498 u16 nvmet_bdev_flush(struct nvmet_req *req); 499 u16 nvmet_file_flush(struct nvmet_req *req); 500 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid); 501 502 static inline u32 nvmet_rw_len(struct nvmet_req *req) 503 { 504 return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) << 505 req->ns->blksize_shift; 506 } 507 508 static inline u32 nvmet_dsm_len(struct nvmet_req *req) 509 { 510 return (le32_to_cpu(req->cmd->dsm.nr) + 1) * 511 sizeof(struct nvme_dsm_range); 512 } 513 514 u16 errno_to_nvme_status(struct nvmet_req *req, int errno); 515 516 /* Convert a 32-bit number to a 16-bit 0's based number */ 517 static inline __le16 to0based(u32 a) 518 { 519 return cpu_to_le16(max(1U, min(1U << 16, a)) - 1); 520 } 521 522 #endif /* _NVMET_H */ 523