xref: /linux/drivers/usb/gadget/function/tcm.h (revision fab183d632628381b466a41479489541ac0e29a0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TARGET_USB_GADGET_H__
3 #define __TARGET_USB_GADGET_H__
4 
5 #include <linux/kref.h>
6 #include <linux/spinlock.h>
7 /* #include <linux/usb/uas.h> */
8 #include <linux/hashtable.h>
9 #include <linux/usb/composite.h>
10 #include <linux/usb/uas.h>
11 #include <linux/usb/storage.h>
12 #include <target/target_core_base.h>
13 #include <target/target_core_fabric.h>
14 
15 #define USBG_NAMELEN 32
16 
17 #define fuas_to_gadget(f)	(f->function.config->cdev->gadget)
18 #define UASP_SS_EP_COMP_LOG_STREAMS 5
19 #define UASP_SS_EP_COMP_NUM_STREAMS (1 << UASP_SS_EP_COMP_LOG_STREAMS)
20 
21 #define USBG_NUM_CMDS		(UASP_SS_EP_COMP_NUM_STREAMS + 1)
22 
23 enum {
24 	USB_G_STR_INT_UAS = 0,
25 	USB_G_STR_INT_BBB,
26 };
27 
28 #define USB_G_ALT_INT_BBB       0
29 #define USB_G_ALT_INT_UAS       1
30 
31 #define USB_G_DEFAULT_SESSION_TAGS	USBG_NUM_CMDS
32 
33 enum {
34 	USBG_DELAYED_SET_ALT_IDLE = 0,
35 	USBG_DELAYED_SET_ALT_QUEUED,
36 	USBG_DELAYED_SET_ALT_RUNNING,
37 };
38 
39 struct tcm_usbg_nexus {
40 	struct se_session *tvn_se_sess;
41 };
42 
43 struct usbg_tpg {
44 	struct mutex tpg_mutex;
45 	/* SAS port target portal group tag for TCM */
46 	u16 tport_tpgt;
47 	/* Pointer back to usbg_tport */
48 	struct usbg_tport *tport;
49 	struct workqueue_struct *workqueue;
50 	/* Returned by usbg_make_tpg() */
51 	struct se_portal_group se_tpg;
52 	u32 gadget_connect;
53 	struct tcm_usbg_nexus *tpg_nexus;
54 	atomic_t tpg_port_count;
55 
56 	struct usb_function_instance *fi;
57 };
58 
59 struct usbg_tport {
60 	/* Binary World Wide unique Port Name for SAS Target port */
61 	u64 tport_wwpn;
62 	/* ASCII formatted WWPN for SAS Target port */
63 	char tport_name[USBG_NAMELEN];
64 	/* Returned by usbg_make_tport() */
65 	struct se_wwn tport_wwn;
66 };
67 
68 enum uas_state {
69 	UASP_SEND_DATA,
70 	UASP_RECEIVE_DATA,
71 	UASP_SEND_STATUS,
72 	UASP_QUEUE_COMMAND,
73 };
74 
75 #define USBG_MAX_CMD    64
76 struct usbg_cmd {
77 	/* common */
78 	u8 cmd_buf[USBG_MAX_CMD];
79 	u32 data_len;
80 	struct work_struct work;
81 	int unpacked_lun;
82 	struct se_cmd se_cmd;
83 	void *data_buf; /* used if no sg support available */
84 	struct f_uas *fu;
85 	struct kref ref;
86 
87 	struct usb_request *req;
88 
89 	u32 flags;
90 #define USBG_CMD_PENDING_DATA_WRITE	BIT(0)
91 
92 	/* UAS only */
93 	u16 tag;
94 	u16 prio_attr;
95 	struct sense_iu sense_iu;
96 	struct response_iu response_iu;
97 	enum uas_state state;
98 
99 	int tmr_func;
100 	int tmr_rsp;
101 #define	RC_RESPONSE_UNKNOWN	0xff
102 
103 	/* BOT only */
104 	__le32 bot_tag;
105 	unsigned int csw_code;
106 	unsigned is_read:1;
107 
108 };
109 
110 struct uas_stream {
111 	struct usb_request	*req_in;
112 	struct usb_request	*req_out;
113 	struct usb_request	*req_status;
114 
115 	struct completion	cmd_completion;
116 	struct hlist_node	node;
117 };
118 
119 struct usbg_cdb {
120 	struct usb_request	*req;
121 	void			*buf;
122 };
123 
124 struct bot_status {
125 	struct usb_request	*req;
126 	struct bulk_cs_wrap	csw;
127 };
128 
129 struct f_uas {
130 	struct usbg_tpg		*tpg;
131 	struct usb_function	function;
132 	u16			iface;
133 
134 	u32			flags;
135 #define USBG_ENABLED		(1 << 0)
136 #define USBG_IS_UAS		(1 << 1)
137 #define USBG_USE_STREAMS	(1 << 2)
138 #define USBG_IS_BOT		(1 << 3)
139 #define USBG_BOT_CMD_PEND	(1 << 4)
140 #define USBG_BOT_WEDGED		(1 << 5)
141 
142 	struct work_struct	delayed_set_alt;
143 	spinlock_t		delayed_set_alt_lock; /* protects delayed_set_alt_* */
144 	unsigned int		delayed_alt;
145 	unsigned int		delayed_set_alt_state;
146 	bool			delayed_set_alt_cancel;
147 
148 	struct usbg_cdb		cmd[USBG_NUM_CMDS];
149 	struct usb_ep		*ep_in;
150 	struct usb_ep		*ep_out;
151 
152 	/* UAS */
153 	struct usb_ep		*ep_status;
154 	struct usb_ep		*ep_cmd;
155 	struct uas_stream	stream[USBG_NUM_CMDS];
156 	DECLARE_HASHTABLE(stream_hash, UASP_SS_EP_COMP_LOG_STREAMS);
157 
158 	/* BOT */
159 	struct bot_status	bot_status;
160 	struct usb_request	*bot_req_in;
161 	struct usb_request	*bot_req_out;
162 };
163 
164 #endif /* __TARGET_USB_GADGET_H__ */
165