Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
0004  */
0005 
0006 #ifndef _NVMET_H
0007 #define _NVMET_H
0008 
0009 #include <linux/dma-mapping.h>
0010 #include <linux/types.h>
0011 #include <linux/device.h>
0012 #include <linux/kref.h>
0013 #include <linux/percpu-refcount.h>
0014 #include <linux/list.h>
0015 #include <linux/mutex.h>
0016 #include <linux/uuid.h>
0017 #include <linux/nvme.h>
0018 #include <linux/configfs.h>
0019 #include <linux/rcupdate.h>
0020 #include <linux/blkdev.h>
0021 #include <linux/radix-tree.h>
0022 #include <linux/t10-pi.h>
0023 
0024 #define NVMET_DEFAULT_VS        NVME_VS(1, 3, 0)
0025 
0026 #define NVMET_ASYNC_EVENTS      4
0027 #define NVMET_ERROR_LOG_SLOTS       128
0028 #define NVMET_NO_ERROR_LOC      ((u16)-1)
0029 #define NVMET_DEFAULT_CTRL_MODEL    "Linux"
0030 #define NVMET_MN_MAX_SIZE       40
0031 #define NVMET_SN_MAX_SIZE       20
0032 
0033 /*
0034  * Supported optional AENs:
0035  */
0036 #define NVMET_AEN_CFG_OPTIONAL \
0037     (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
0038 #define NVMET_DISC_AEN_CFG_OPTIONAL \
0039     (NVME_AEN_CFG_DISC_CHANGE)
0040 
0041 /*
0042  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
0043  */
0044 #define NVMET_AEN_CFG_ALL \
0045     (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
0046      NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
0047      NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
0048 
0049 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
0050  * The 16 bit shift is to set IATTR bit to 1, which means offending
0051  * offset starts in the data section of connect()
0052  */
0053 #define IPO_IATTR_CONNECT_DATA(x)   \
0054     (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
0055 #define IPO_IATTR_CONNECT_SQE(x)    \
0056     (cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
0057 
0058 struct nvmet_ns {
0059     struct percpu_ref   ref;
0060     struct block_device *bdev;
0061     struct file     *file;
0062     bool            readonly;
0063     u32         nsid;
0064     u32         blksize_shift;
0065     loff_t          size;
0066     u8          nguid[16];
0067     uuid_t          uuid;
0068     u32         anagrpid;
0069 
0070     bool            buffered_io;
0071     bool            enabled;
0072     struct nvmet_subsys *subsys;
0073     const char      *device_path;
0074 
0075     struct config_group device_group;
0076     struct config_group group;
0077 
0078     struct completion   disable_done;
0079     mempool_t       *bvec_pool;
0080     struct kmem_cache   *bvec_cache;
0081 
0082     int         use_p2pmem;
0083     struct pci_dev      *p2p_dev;
0084     int         pi_type;
0085     int         metadata_size;
0086     u8          csi;
0087 };
0088 
0089 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
0090 {
0091     return container_of(to_config_group(item), struct nvmet_ns, group);
0092 }
0093 
0094 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
0095 {
0096     return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
0097 }
0098 
0099 struct nvmet_cq {
0100     u16         qid;
0101     u16         size;
0102 };
0103 
0104 struct nvmet_sq {
0105     struct nvmet_ctrl   *ctrl;
0106     struct percpu_ref   ref;
0107     u16         qid;
0108     u16         size;
0109     u32         sqhd;
0110     bool            sqhd_disabled;
0111 #ifdef CONFIG_NVME_TARGET_AUTH
0112     struct delayed_work auth_expired_work;
0113     bool            authenticated;
0114     u16         dhchap_tid;
0115     u16         dhchap_status;
0116     int         dhchap_step;
0117     u8          *dhchap_c1;
0118     u8          *dhchap_c2;
0119     u32         dhchap_s1;
0120     u32         dhchap_s2;
0121     u8          *dhchap_skey;
0122     int         dhchap_skey_len;
0123 #endif
0124     struct completion   free_done;
0125     struct completion   confirm_done;
0126 };
0127 
0128 struct nvmet_ana_group {
0129     struct config_group group;
0130     struct nvmet_port   *port;
0131     u32         grpid;
0132 };
0133 
0134 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
0135 {
0136     return container_of(to_config_group(item), struct nvmet_ana_group,
0137             group);
0138 }
0139 
0140 /**
0141  * struct nvmet_port -  Common structure to keep port
0142  *              information for the target.
0143  * @entry:      Entry into referrals or transport list.
0144  * @disc_addr:      Address information is stored in a format defined
0145  *              for a discovery log page entry.
0146  * @group:      ConfigFS group for this element's folder.
0147  * @priv:       Private data for the transport.
0148  */
0149 struct nvmet_port {
0150     struct list_head        entry;
0151     struct nvmf_disc_rsp_page_entry disc_addr;
0152     struct config_group     group;
0153     struct config_group     subsys_group;
0154     struct list_head        subsystems;
0155     struct config_group     referrals_group;
0156     struct list_head        referrals;
0157     struct list_head        global_entry;
0158     struct config_group     ana_groups_group;
0159     struct nvmet_ana_group      ana_default_group;
0160     enum nvme_ana_state     *ana_state;
0161     void                *priv;
0162     bool                enabled;
0163     int             inline_data_size;
0164     const struct nvmet_fabrics_ops  *tr_ops;
0165     bool                pi_enable;
0166 };
0167 
0168 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
0169 {
0170     return container_of(to_config_group(item), struct nvmet_port,
0171             group);
0172 }
0173 
0174 static inline struct nvmet_port *ana_groups_to_port(
0175         struct config_item *item)
0176 {
0177     return container_of(to_config_group(item), struct nvmet_port,
0178             ana_groups_group);
0179 }
0180 
0181 struct nvmet_ctrl {
0182     struct nvmet_subsys *subsys;
0183     struct nvmet_sq     **sqs;
0184 
0185     bool            reset_tbkas;
0186 
0187     struct mutex        lock;
0188     u64         cap;
0189     u32         cc;
0190     u32         csts;
0191 
0192     uuid_t          hostid;
0193     u16         cntlid;
0194     u32         kato;
0195 
0196     struct nvmet_port   *port;
0197 
0198     u32         aen_enabled;
0199     unsigned long       aen_masked;
0200     struct nvmet_req    *async_event_cmds[NVMET_ASYNC_EVENTS];
0201     unsigned int        nr_async_event_cmds;
0202     struct list_head    async_events;
0203     struct work_struct  async_event_work;
0204 
0205     struct list_head    subsys_entry;
0206     struct kref     ref;
0207     struct delayed_work ka_work;
0208     struct work_struct  fatal_err_work;
0209 
0210     const struct nvmet_fabrics_ops *ops;
0211 
0212     __le32          *changed_ns_list;
0213     u32         nr_changed_ns;
0214 
0215     char            subsysnqn[NVMF_NQN_FIELD_LEN];
0216     char            hostnqn[NVMF_NQN_FIELD_LEN];
0217 
0218     struct device       *p2p_client;
0219     struct radix_tree_root  p2p_ns_map;
0220 
0221     spinlock_t      error_lock;
0222     u64         err_counter;
0223     struct nvme_error_slot  slots[NVMET_ERROR_LOG_SLOTS];
0224     bool            pi_support;
0225 #ifdef CONFIG_NVME_TARGET_AUTH
0226     struct nvme_dhchap_key  *host_key;
0227     struct nvme_dhchap_key  *ctrl_key;
0228     u8          shash_id;
0229     struct crypto_kpp   *dh_tfm;
0230     u8          dh_gid;
0231     u8          *dh_key;
0232     size_t          dh_keysize;
0233 #endif
0234 };
0235 
0236 struct nvmet_subsys {
0237     enum nvme_subsys_type   type;
0238 
0239     struct mutex        lock;
0240     struct kref     ref;
0241 
0242     struct xarray       namespaces;
0243     unsigned int        nr_namespaces;
0244     u32         max_nsid;
0245     u16         cntlid_min;
0246     u16         cntlid_max;
0247 
0248     struct list_head    ctrls;
0249 
0250     struct list_head    hosts;
0251     bool            allow_any_host;
0252 
0253     u16         max_qid;
0254 
0255     u64         ver;
0256     char            serial[NVMET_SN_MAX_SIZE];
0257     bool            subsys_discovered;
0258     char            *subsysnqn;
0259     bool            pi_support;
0260 
0261     struct config_group group;
0262 
0263     struct config_group namespaces_group;
0264     struct config_group allowed_hosts_group;
0265 
0266     char            *model_number;
0267 
0268 #ifdef CONFIG_NVME_TARGET_PASSTHRU
0269     struct nvme_ctrl    *passthru_ctrl;
0270     char            *passthru_ctrl_path;
0271     struct config_group passthru_group;
0272     unsigned int        admin_timeout;
0273     unsigned int        io_timeout;
0274     unsigned int        clear_ids;
0275 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
0276 
0277 #ifdef CONFIG_BLK_DEV_ZONED
0278     u8          zasl;
0279 #endif /* CONFIG_BLK_DEV_ZONED */
0280 };
0281 
0282 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
0283 {
0284     return container_of(to_config_group(item), struct nvmet_subsys, group);
0285 }
0286 
0287 static inline struct nvmet_subsys *namespaces_to_subsys(
0288         struct config_item *item)
0289 {
0290     return container_of(to_config_group(item), struct nvmet_subsys,
0291             namespaces_group);
0292 }
0293 
0294 struct nvmet_host {
0295     struct config_group group;
0296     u8          *dhchap_secret;
0297     u8          *dhchap_ctrl_secret;
0298     u8          dhchap_key_hash;
0299     u8          dhchap_ctrl_key_hash;
0300     u8          dhchap_hash_id;
0301     u8          dhchap_dhgroup_id;
0302 };
0303 
0304 static inline struct nvmet_host *to_host(struct config_item *item)
0305 {
0306     return container_of(to_config_group(item), struct nvmet_host, group);
0307 }
0308 
0309 static inline char *nvmet_host_name(struct nvmet_host *host)
0310 {
0311     return config_item_name(&host->group.cg_item);
0312 }
0313 
0314 struct nvmet_host_link {
0315     struct list_head    entry;
0316     struct nvmet_host   *host;
0317 };
0318 
0319 struct nvmet_subsys_link {
0320     struct list_head    entry;
0321     struct nvmet_subsys *subsys;
0322 };
0323 
0324 struct nvmet_req;
0325 struct nvmet_fabrics_ops {
0326     struct module *owner;
0327     unsigned int type;
0328     unsigned int msdbd;
0329     unsigned int flags;
0330 #define NVMF_KEYED_SGLS         (1 << 0)
0331 #define NVMF_METADATA_SUPPORTED     (1 << 1)
0332     void (*queue_response)(struct nvmet_req *req);
0333     int (*add_port)(struct nvmet_port *port);
0334     void (*remove_port)(struct nvmet_port *port);
0335     void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
0336     void (*disc_traddr)(struct nvmet_req *req,
0337             struct nvmet_port *port, char *traddr);
0338     u16 (*install_queue)(struct nvmet_sq *nvme_sq);
0339     void (*discovery_chg)(struct nvmet_port *port);
0340     u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
0341     u16 (*get_max_queue_size)(const struct nvmet_ctrl *ctrl);
0342 };
0343 
0344 #define NVMET_MAX_INLINE_BIOVEC 8
0345 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
0346 
0347 struct nvmet_req {
0348     struct nvme_command *cmd;
0349     struct nvme_completion  *cqe;
0350     struct nvmet_sq     *sq;
0351     struct nvmet_cq     *cq;
0352     struct nvmet_ns     *ns;
0353     struct scatterlist  *sg;
0354     struct scatterlist  *metadata_sg;
0355     struct bio_vec      inline_bvec[NVMET_MAX_INLINE_BIOVEC];
0356     union {
0357         struct {
0358             struct bio      inline_bio;
0359         } b;
0360         struct {
0361             bool            mpool_alloc;
0362             struct kiocb            iocb;
0363             struct bio_vec          *bvec;
0364             struct work_struct      work;
0365         } f;
0366         struct {
0367             struct bio      inline_bio;
0368             struct request      *rq;
0369             struct work_struct      work;
0370             bool            use_workqueue;
0371         } p;
0372 #ifdef CONFIG_BLK_DEV_ZONED
0373         struct {
0374             struct bio      inline_bio;
0375             struct work_struct  zmgmt_work;
0376         } z;
0377 #endif /* CONFIG_BLK_DEV_ZONED */
0378     };
0379     int         sg_cnt;
0380     int         metadata_sg_cnt;
0381     /* data length as parsed from the SGL descriptor: */
0382     size_t          transfer_len;
0383     size_t          metadata_len;
0384 
0385     struct nvmet_port   *port;
0386 
0387     void (*execute)(struct nvmet_req *req);
0388     const struct nvmet_fabrics_ops *ops;
0389 
0390     struct pci_dev      *p2p_dev;
0391     struct device       *p2p_client;
0392     u16         error_loc;
0393     u64         error_slba;
0394 };
0395 
0396 extern struct workqueue_struct *buffered_io_wq;
0397 extern struct workqueue_struct *zbd_wq;
0398 extern struct workqueue_struct *nvmet_wq;
0399 
0400 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
0401 {
0402     req->cqe->result.u32 = cpu_to_le32(result);
0403 }
0404 
0405 /*
0406  * NVMe command writes actually are DMA reads for us on the target side.
0407  */
0408 static inline enum dma_data_direction
0409 nvmet_data_dir(struct nvmet_req *req)
0410 {
0411     return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
0412 }
0413 
0414 struct nvmet_async_event {
0415     struct list_head    entry;
0416     u8          event_type;
0417     u8          event_info;
0418     u8          log_page;
0419 };
0420 
0421 static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
0422 {
0423     int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
0424 
0425     if (!rae)
0426         clear_bit(bn, &req->sq->ctrl->aen_masked);
0427 }
0428 
0429 static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
0430 {
0431     if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
0432         return true;
0433     return test_and_set_bit(bn, &ctrl->aen_masked);
0434 }
0435 
0436 void nvmet_get_feat_kato(struct nvmet_req *req);
0437 void nvmet_get_feat_async_event(struct nvmet_req *req);
0438 u16 nvmet_set_feat_kato(struct nvmet_req *req);
0439 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
0440 void nvmet_execute_async_event(struct nvmet_req *req);
0441 void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl);
0442 void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl);
0443 
0444 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
0445 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
0446 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
0447 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
0448 u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req);
0449 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
0450 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
0451 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req);
0452 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req);
0453 
0454 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
0455         struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
0456 void nvmet_req_uninit(struct nvmet_req *req);
0457 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
0458 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
0459 void nvmet_req_complete(struct nvmet_req *req, u16 status);
0460 int nvmet_req_alloc_sgls(struct nvmet_req *req);
0461 void nvmet_req_free_sgls(struct nvmet_req *req);
0462 
0463 void nvmet_execute_set_features(struct nvmet_req *req);
0464 void nvmet_execute_get_features(struct nvmet_req *req);
0465 void nvmet_execute_keep_alive(struct nvmet_req *req);
0466 
0467 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
0468         u16 size);
0469 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
0470         u16 size);
0471 void nvmet_sq_destroy(struct nvmet_sq *sq);
0472 int nvmet_sq_init(struct nvmet_sq *sq);
0473 
0474 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
0475 
0476 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
0477 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
0478         struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
0479 struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
0480                        const char *hostnqn, u16 cntlid,
0481                        struct nvmet_req *req);
0482 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
0483 u16 nvmet_check_ctrl_status(struct nvmet_req *req);
0484 
0485 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
0486         enum nvme_subsys_type type);
0487 void nvmet_subsys_put(struct nvmet_subsys *subsys);
0488 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
0489 
0490 u16 nvmet_req_find_ns(struct nvmet_req *req);
0491 void nvmet_put_namespace(struct nvmet_ns *ns);
0492 int nvmet_ns_enable(struct nvmet_ns *ns);
0493 void nvmet_ns_disable(struct nvmet_ns *ns);
0494 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
0495 void nvmet_ns_free(struct nvmet_ns *ns);
0496 
0497 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
0498         struct nvmet_port *port);
0499 void nvmet_port_send_ana_event(struct nvmet_port *port);
0500 
0501 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
0502 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
0503 
0504 void nvmet_port_del_ctrls(struct nvmet_port *port,
0505               struct nvmet_subsys *subsys);
0506 
0507 int nvmet_enable_port(struct nvmet_port *port);
0508 void nvmet_disable_port(struct nvmet_port *port);
0509 
0510 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
0511 void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
0512 
0513 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
0514         size_t len);
0515 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
0516         size_t len);
0517 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
0518 
0519 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
0520 u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
0521 
0522 extern struct list_head *nvmet_ports;
0523 void nvmet_port_disc_changed(struct nvmet_port *port,
0524         struct nvmet_subsys *subsys);
0525 void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
0526         struct nvmet_host *host);
0527 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
0528         u8 event_info, u8 log_page);
0529 
0530 #define NVMET_QUEUE_SIZE    1024
0531 #define NVMET_NR_QUEUES     128
0532 #define NVMET_MAX_CMD       NVMET_QUEUE_SIZE
0533 
0534 /*
0535  * Nice round number that makes a list of nsids fit into a page.
0536  * Should become tunable at some point in the future.
0537  */
0538 #define NVMET_MAX_NAMESPACES    1024
0539 
0540 /*
0541  * 0 is not a valid ANA group ID, so we start numbering at 1.
0542  *
0543  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
0544  * by default, and is available in an optimized state through all ports.
0545  */
0546 #define NVMET_MAX_ANAGRPS   128
0547 #define NVMET_DEFAULT_ANA_GRPID 1
0548 
0549 #define NVMET_KAS       10
0550 #define NVMET_DISC_KATO_MS      120000
0551 
0552 int __init nvmet_init_configfs(void);
0553 void __exit nvmet_exit_configfs(void);
0554 
0555 int __init nvmet_init_discovery(void);
0556 void nvmet_exit_discovery(void);
0557 
0558 extern struct nvmet_subsys *nvmet_disc_subsys;
0559 extern struct rw_semaphore nvmet_config_sem;
0560 
0561 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
0562 extern u64 nvmet_ana_chgcnt;
0563 extern struct rw_semaphore nvmet_ana_sem;
0564 
0565 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
0566 
0567 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
0568 int nvmet_file_ns_enable(struct nvmet_ns *ns);
0569 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
0570 void nvmet_file_ns_disable(struct nvmet_ns *ns);
0571 u16 nvmet_bdev_flush(struct nvmet_req *req);
0572 u16 nvmet_file_flush(struct nvmet_req *req);
0573 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
0574 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
0575 void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
0576 bool nvmet_ns_revalidate(struct nvmet_ns *ns);
0577 u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
0578 
0579 bool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
0580 void nvmet_execute_identify_cns_cs_ctrl(struct nvmet_req *req);
0581 void nvmet_execute_identify_cns_cs_ns(struct nvmet_req *req);
0582 void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
0583 void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req);
0584 void nvmet_bdev_execute_zone_append(struct nvmet_req *req);
0585 
0586 static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
0587 {
0588     return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
0589             req->ns->blksize_shift;
0590 }
0591 
0592 static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
0593 {
0594     if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
0595         return 0;
0596     return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
0597             req->ns->metadata_size;
0598 }
0599 
0600 static inline u32 nvmet_dsm_len(struct nvmet_req *req)
0601 {
0602     return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
0603         sizeof(struct nvme_dsm_range);
0604 }
0605 
0606 static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)
0607 {
0608     return req->sq->ctrl->subsys;
0609 }
0610 
0611 static inline bool nvmet_is_disc_subsys(struct nvmet_subsys *subsys)
0612 {
0613     return subsys->type != NVME_NQN_NVME;
0614 }
0615 
0616 #ifdef CONFIG_NVME_TARGET_PASSTHRU
0617 void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys);
0618 int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys);
0619 void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys);
0620 u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
0621 u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
0622 static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
0623 {
0624     return subsys->passthru_ctrl;
0625 }
0626 #else /* CONFIG_NVME_TARGET_PASSTHRU */
0627 static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
0628 {
0629 }
0630 static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
0631 {
0632 }
0633 static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
0634 {
0635     return 0;
0636 }
0637 static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
0638 {
0639     return 0;
0640 }
0641 static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
0642 {
0643     return NULL;
0644 }
0645 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
0646 
0647 static inline bool nvmet_is_passthru_req(struct nvmet_req *req)
0648 {
0649     return nvmet_is_passthru_subsys(nvmet_req_subsys(req));
0650 }
0651 
0652 void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl);
0653 
0654 u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
0655 u16 nvmet_report_invalid_opcode(struct nvmet_req *req);
0656 
0657 /* Convert a 32-bit number to a 16-bit 0's based number */
0658 static inline __le16 to0based(u32 a)
0659 {
0660     return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
0661 }
0662 
0663 static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
0664 {
0665     if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
0666         return false;
0667     return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
0668 }
0669 
0670 static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
0671 {
0672     return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
0673 }
0674 
0675 static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
0676 {
0677     return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
0678 }
0679 
0680 static inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
0681 {
0682     return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN &&
0683            req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC;
0684 }
0685 
0686 static inline void nvmet_req_cns_error_complete(struct nvmet_req *req)
0687 {
0688     pr_debug("unhandled identify cns %d on qid %d\n",
0689            req->cmd->identify.cns, req->sq->qid);
0690     req->error_loc = offsetof(struct nvme_identify, cns);
0691     nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_SC_DNR);
0692 }
0693 
0694 static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
0695 {
0696     if (bio != &req->b.inline_bio)
0697         bio_put(bio);
0698 }
0699 
0700 #ifdef CONFIG_NVME_TARGET_AUTH
0701 void nvmet_execute_auth_send(struct nvmet_req *req);
0702 void nvmet_execute_auth_receive(struct nvmet_req *req);
0703 int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
0704                bool set_ctrl);
0705 int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
0706 int nvmet_setup_auth(struct nvmet_ctrl *ctrl);
0707 void nvmet_init_auth(struct nvmet_ctrl *ctrl, struct nvmet_req *req);
0708 void nvmet_destroy_auth(struct nvmet_ctrl *ctrl);
0709 void nvmet_auth_sq_free(struct nvmet_sq *sq);
0710 int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id);
0711 bool nvmet_check_auth_status(struct nvmet_req *req);
0712 int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
0713              unsigned int hash_len);
0714 int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
0715              unsigned int hash_len);
0716 static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
0717 {
0718     return ctrl->host_key != NULL;
0719 }
0720 int nvmet_auth_ctrl_exponential(struct nvmet_req *req,
0721                 u8 *buf, int buf_size);
0722 int nvmet_auth_ctrl_sesskey(struct nvmet_req *req,
0723                 u8 *buf, int buf_size);
0724 #else
0725 static inline int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
0726 {
0727     return 0;
0728 }
0729 static inline void nvmet_init_auth(struct nvmet_ctrl *ctrl,
0730                    struct nvmet_req *req) {};
0731 static inline void nvmet_destroy_auth(struct nvmet_ctrl *ctrl) {};
0732 static inline void nvmet_auth_sq_free(struct nvmet_sq *sq) {};
0733 static inline bool nvmet_check_auth_status(struct nvmet_req *req)
0734 {
0735     return true;
0736 }
0737 static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
0738 {
0739     return false;
0740 }
0741 static inline const char *nvmet_dhchap_dhgroup_name(u8 dhgid) { return NULL; }
0742 #endif
0743 
0744 #endif /* _NVMET_H */