1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _SBP_BASE_H
3#define _SBP_BASE_H
4
5#include <linux/firewire.h>
6#include <linux/spinlock.h>
7#include <linux/types.h>
8#include <linux/workqueue.h>
9#include <target/target_core_base.h>
10
11#define SBP_VERSION  "v0.1"
12#define SBP_NAMELEN 32
13
14#define SBP_ORB_FETCH_SIZE	8
15
16#define MANAGEMENT_AGENT_STATE_IDLE	0
17#define MANAGEMENT_AGENT_STATE_BUSY	1
18
19#define ORB_NOTIFY(v)			(((v) >> 31) & 0x01)
20#define ORB_REQUEST_FORMAT(v)		(((v) >> 29) & 0x03)
21
22#define MANAGEMENT_ORB_FUNCTION(v)	(((v) >> 16) & 0x0f)
23
24#define MANAGEMENT_ORB_FUNCTION_LOGIN			0x0
25#define MANAGEMENT_ORB_FUNCTION_QUERY_LOGINS		0x1
26#define MANAGEMENT_ORB_FUNCTION_RECONNECT		0x3
27#define MANAGEMENT_ORB_FUNCTION_SET_PASSWORD		0x4
28#define MANAGEMENT_ORB_FUNCTION_LOGOUT			0x7
29#define MANAGEMENT_ORB_FUNCTION_ABORT_TASK		0xb
30#define MANAGEMENT_ORB_FUNCTION_ABORT_TASK_SET		0xc
31#define MANAGEMENT_ORB_FUNCTION_LOGICAL_UNIT_RESET	0xe
32#define MANAGEMENT_ORB_FUNCTION_TARGET_RESET		0xf
33
34#define LOGIN_ORB_EXCLUSIVE(v)		(((v) >> 28) &   0x01)
35#define LOGIN_ORB_RESERVED(v)		(((v) >> 24) &   0x0f)
36#define LOGIN_ORB_RECONNECT(v)		(((v) >> 20) &   0x0f)
37#define LOGIN_ORB_LUN(v)		(((v) >>  0) & 0xffff)
38#define LOGIN_ORB_PASSWORD_LENGTH(v)	(((v) >> 16) & 0xffff)
39#define LOGIN_ORB_RESPONSE_LENGTH(v)	(((v) >>  0) & 0xffff)
40
41#define RECONNECT_ORB_LOGIN_ID(v)	(((v) >>  0) & 0xffff)
42#define LOGOUT_ORB_LOGIN_ID(v)		(((v) >>  0) & 0xffff)
43
44#define CMDBLK_ORB_DIRECTION(v)		(((v) >> 27) &   0x01)
45#define CMDBLK_ORB_SPEED(v)		(((v) >> 24) &   0x07)
46#define CMDBLK_ORB_MAX_PAYLOAD(v)	(((v) >> 20) &   0x0f)
47#define CMDBLK_ORB_PG_TBL_PRESENT(v)	(((v) >> 19) &   0x01)
48#define CMDBLK_ORB_PG_SIZE(v)		(((v) >> 16) &   0x07)
49#define CMDBLK_ORB_DATA_SIZE(v)		(((v) >>  0) & 0xffff)
50
51#define STATUS_BLOCK_SRC(v)		(((v) &   0x03) << 30)
52#define STATUS_BLOCK_RESP(v)		(((v) &   0x03) << 28)
53#define STATUS_BLOCK_DEAD(v)		(((v) ? 1 : 0)  << 27)
54#define STATUS_BLOCK_LEN(v)		(((v) &   0x07) << 24)
55#define STATUS_BLOCK_SBP_STATUS(v)	(((v) &   0xff) << 16)
56#define STATUS_BLOCK_ORB_OFFSET_HIGH(v)	(((v) & 0xffff) <<  0)
57
58#define STATUS_SRC_ORB_CONTINUING	0
59#define STATUS_SRC_ORB_FINISHED		1
60#define STATUS_SRC_UNSOLICITED		2
61
62#define STATUS_RESP_REQUEST_COMPLETE	0
63#define STATUS_RESP_TRANSPORT_FAILURE	1
64#define STATUS_RESP_ILLEGAL_REQUEST	2
65#define STATUS_RESP_VENDOR_DEPENDENT	3
66
67#define SBP_STATUS_OK			0
68#define SBP_STATUS_REQ_TYPE_NOTSUPP	1
69#define SBP_STATUS_SPEED_NOTSUPP	2
70#define SBP_STATUS_PAGE_SIZE_NOTSUPP	3
71#define SBP_STATUS_ACCESS_DENIED	4
72#define SBP_STATUS_LUN_NOTSUPP		5
73#define SBP_STATUS_PAYLOAD_TOO_SMALL	6
74/* 7 is reserved */
75#define SBP_STATUS_RESOURCES_UNAVAIL	8
76#define SBP_STATUS_FUNCTION_REJECTED	9
77#define SBP_STATUS_LOGIN_ID_UNKNOWN	10
78#define SBP_STATUS_DUMMY_ORB_COMPLETE	11
79#define SBP_STATUS_REQUEST_ABORTED	12
80#define SBP_STATUS_UNSPECIFIED_ERROR	0xff
81
82#define AGENT_STATE_RESET	0
83#define AGENT_STATE_ACTIVE	1
84#define AGENT_STATE_SUSPENDED	2
85#define AGENT_STATE_DEAD	3
86
87struct sbp2_pointer {
88	__be32 high;
89	__be32 low;
90};
91
92struct sbp_command_block_orb {
93	struct sbp2_pointer next_orb;
94	struct sbp2_pointer data_descriptor;
95	__be32 misc;
96	u8 command_block[12];
97};
98
99struct sbp_page_table_entry {
100	__be16 segment_length;
101	__be16 segment_base_hi;
102	__be32 segment_base_lo;
103};
104
105struct sbp_management_orb {
106	struct sbp2_pointer ptr1;
107	struct sbp2_pointer ptr2;
108	__be32 misc;
109	__be32 length;
110	struct sbp2_pointer status_fifo;
111};
112
113struct sbp_status_block {
114	__be32 status;
115	__be32 orb_low;
116	u8 data[24];
117};
118
119struct sbp_login_response_block {
120	__be32 misc;
121	struct sbp2_pointer command_block_agent;
122	__be32 reconnect_hold;
123};
124
125struct sbp_login_descriptor {
126	struct sbp_session *sess;
127	struct list_head link;
128
129	u32 login_lun;
130
131	u64 status_fifo_addr;
132	int exclusive;
133	u16 login_id;
134
135	struct sbp_target_agent *tgt_agt;
136};
137
138struct sbp_session {
139	spinlock_t lock;
140	struct se_session *se_sess;
141	struct list_head login_list;
142	struct delayed_work maint_work;
143
144	u64 guid; /* login_owner_EUI_64 */
145	int node_id; /* login_owner_ID */
146
147	struct fw_card *card;
148	int generation;
149	int speed;
150
151	int reconnect_hold;
152	u64 reconnect_expires;
153};
154
155struct sbp_tpg {
156	/* Target portal group tag for TCM */
157	u16 tport_tpgt;
158	/* Pointer back to sbp_tport */
159	struct sbp_tport *tport;
160	/* Returned by sbp_make_tpg() */
161	struct se_portal_group se_tpg;
162};
163
164struct sbp_tport {
165	/* Target Unit Identifier (EUI-64) */
166	u64 guid;
167	/* Target port name */
168	char tport_name[SBP_NAMELEN];
169	/* Returned by sbp_make_tport() */
170	struct se_wwn tport_wwn;
171
172	struct sbp_tpg *tpg;
173
174	/* FireWire unit directory */
175	struct fw_descriptor unit_directory;
176
177	/* SBP Management Agent */
178	struct sbp_management_agent *mgt_agt;
179
180	/* Parameters */
181	int enable;
182	s32 directory_id;
183	int mgt_orb_timeout;
184	int max_reconnect_timeout;
185	int max_logins_per_lun;
186};
187
188static inline u64 sbp2_pointer_to_addr(const struct sbp2_pointer *ptr)
189{
190	return (u64)(be32_to_cpu(ptr->high) & 0x0000ffff) << 32 |
191		(be32_to_cpu(ptr->low) & 0xfffffffc);
192}
193
194static inline void addr_to_sbp2_pointer(u64 addr, struct sbp2_pointer *ptr)
195{
196	ptr->high = cpu_to_be32(addr >> 32);
197	ptr->low = cpu_to_be32(addr);
198}
199
200struct sbp_target_agent {
201	spinlock_t lock;
202	struct fw_address_handler handler;
203	struct sbp_login_descriptor *login;
204	int state;
205	struct work_struct work;
206	u64 orb_pointer;
207	bool doorbell;
208};
209
210struct sbp_target_request {
211	struct sbp_login_descriptor *login;
212	u64 orb_pointer;
213	struct sbp_command_block_orb orb;
214	struct sbp_status_block status;
215	struct work_struct work;
216
217	struct se_cmd se_cmd;
218	struct sbp_page_table_entry *pg_tbl;
219	void *cmd_buf;
220
221	unsigned char sense_buf[TRANSPORT_SENSE_BUFFER];
222};
223
224struct sbp_management_agent {
225	spinlock_t lock;
226	struct sbp_tport *tport;
227	struct fw_address_handler handler;
228	int state;
229	struct work_struct work;
230	u64 orb_offset;
231	struct sbp_management_request *request;
232};
233
234struct sbp_management_request {
235	struct sbp_management_orb orb;
236	struct sbp_status_block status;
237	struct fw_card *card;
238	int generation;
239	int node_addr;
240	int speed;
241};
242
243#endif
244