root/include/target/target_core_backend.h
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef TARGET_CORE_BACKEND_H
#define TARGET_CORE_BACKEND_H

#include <linux/types.h>
#include <linux/unaligned.h>
#include <target/target_core_base.h>

#define TRANSPORT_FLAG_PASSTHROUGH              0x1
/*
 * ALUA commands, state checks and setup operations are handled by the
 * backend module.
 */
#define TRANSPORT_FLAG_PASSTHROUGH_ALUA         0x2
#define TRANSPORT_FLAG_PASSTHROUGH_PGR          0x4

struct block_device;
struct scatterlist;

struct target_backend_ops {
        char name[16];
        char inquiry_prod[16];
        char inquiry_rev[4];
        struct module *owner;

        u8 transport_flags_default;
        u8 transport_flags_changeable;

        int (*attach_hba)(struct se_hba *, u32);
        void (*detach_hba)(struct se_hba *);
        int (*pmode_enable_hba)(struct se_hba *, unsigned long);

        struct se_device *(*alloc_device)(struct se_hba *, const char *);
        int (*configure_device)(struct se_device *);
        void (*destroy_device)(struct se_device *);
        void (*free_device)(struct se_device *device);
        struct se_dev_plug *(*plug_device)(struct se_device *se_dev);
        void (*unplug_device)(struct se_dev_plug *se_plug);

        bool (*configure_unmap)(struct se_device *se_dev);
        ssize_t (*set_configfs_dev_params)(struct se_device *,
                                           const char *, ssize_t);
        ssize_t (*show_configfs_dev_params)(struct se_device *, char *);

        sense_reason_t (*parse_cdb)(struct se_cmd *cmd);
        void (*tmr_notify)(struct se_device *se_dev, enum tcm_tmreq_table,
                           struct list_head *aborted_cmds);
        u32 (*get_device_type)(struct se_device *);
        sector_t (*get_blocks)(struct se_device *);
        sector_t (*get_alignment_offset_lbas)(struct se_device *);
        /* lbppbe = logical blocks per physical block exponent. see SBC-3 */
        unsigned int (*get_lbppbe)(struct se_device *);
        unsigned int (*get_io_min)(struct se_device *);
        unsigned int (*get_io_opt)(struct se_device *);
        unsigned char *(*get_sense_buffer)(struct se_cmd *);
        bool (*get_write_cache)(struct se_device *);
        int (*init_prot)(struct se_device *);
        int (*format_prot)(struct se_device *);
        void (*free_prot)(struct se_device *);

        struct configfs_attribute **tb_dev_attrib_attrs;
        struct configfs_attribute **tb_dev_action_attrs;
};

struct exec_cmd_ops {
        sense_reason_t (*execute_rw)(struct se_cmd *cmd, struct scatterlist *,
                                     u32, enum dma_data_direction);
        sense_reason_t (*execute_sync_cache)(struct se_cmd *cmd);
        sense_reason_t (*execute_write_same)(struct se_cmd *cmd);
        sense_reason_t (*execute_unmap)(struct se_cmd *cmd,
                                sector_t lba, sector_t nolb);
        sense_reason_t (*execute_pr_out)(struct se_cmd *cmd, u8 sa, u64 key,
                                         u64 sa_key, u8 type, bool aptpl);
        sense_reason_t (*execute_pr_in)(struct se_cmd *cmd, u8 sa,
                                        unsigned char *param_data);
};

int     transport_backend_register(const struct target_backend_ops *);
void    target_backend_unregister(const struct target_backend_ops *);

void    target_complete_cmd(struct se_cmd *, u8);
void    target_set_cmd_data_length(struct se_cmd *, int);
void    target_complete_cmd_with_sense(struct se_cmd *, u8, sense_reason_t);
void    target_complete_cmd_with_length(struct se_cmd *, u8, int);

void    transport_copy_sense_to_cmd(struct se_cmd *, unsigned char *);

sense_reason_t  spc_parse_cdb(struct se_cmd *cmd, unsigned int *size);
sense_reason_t  spc_emulate_report_luns(struct se_cmd *cmd);
sense_reason_t  spc_emulate_inquiry_std(struct se_cmd *, unsigned char *);
sense_reason_t  spc_emulate_evpd_83(struct se_cmd *, unsigned char *);

sense_reason_t  sbc_parse_cdb(struct se_cmd *cmd, struct exec_cmd_ops *ops);
u32     sbc_get_device_rev(struct se_device *dev);
u32     sbc_get_device_type(struct se_device *dev);
sector_t        sbc_get_write_same_sectors(struct se_cmd *cmd);
void    sbc_dif_generate(struct se_cmd *);
sense_reason_t  sbc_dif_verify(struct se_cmd *, sector_t, unsigned int,
                                     unsigned int, struct scatterlist *, int);
void sbc_dif_copy_prot(struct se_cmd *, unsigned int, bool,
                       struct scatterlist *, int);
void    transport_set_vpd_proto_id(struct t10_vpd *, unsigned char *);
int     transport_set_vpd_assoc(struct t10_vpd *, unsigned char *);
int     transport_set_vpd_ident_type(struct t10_vpd *, unsigned char *);
int     transport_set_vpd_ident(struct t10_vpd *, unsigned char *);

extern struct configfs_attribute *sbc_attrib_attrs[];
extern struct configfs_attribute *passthrough_attrib_attrs[];
extern struct configfs_attribute *passthrough_pr_attrib_attrs[];

/* core helpers also used by command snooping in pscsi */
void    *transport_kmap_data_sg(struct se_cmd *);
void    transport_kunmap_data_sg(struct se_cmd *);
/* core helpers also used by xcopy during internal command setup */
sense_reason_t  transport_generic_map_mem_to_cmd(struct se_cmd *,
                struct scatterlist *, u32, struct scatterlist *, u32);

bool    target_lun_is_rdonly(struct se_cmd *);
sense_reason_t passthrough_parse_cdb(struct se_cmd *cmd,
        sense_reason_t (*exec_cmd)(struct se_cmd *cmd));

bool target_sense_desc_format(struct se_device *dev);
sector_t target_to_linux_sector(struct se_device *dev, sector_t lb);
bool target_configure_unmap_from_bdev(struct se_dev_attrib *attrib,
                                      struct block_device *bdev);
void target_configure_write_atomic_from_bdev(struct se_dev_attrib *attrib,
                                             struct block_device *bdev);

static inline bool target_dev_configured(struct se_device *se_dev)
{
        return !!(se_dev->dev_flags & DF_CONFIGURED);
}

#endif /* TARGET_CORE_BACKEND_H */