1 /* QLogic FCoE Offload Driver
2  * Copyright (c) 2016-2017 Cavium Inc.
3  *
4  * This software is available under the terms of the GNU General Public License
5  * (GPL) Version 2, available from the file COPYING in the main directory of
6  * this source tree.
7  */
8 #include "drv_fcoe_fw_funcs.h"
9 #include "drv_scsi_fw_funcs.h"
10 
11 #define FCOE_RX_ID (0xFFFFu)
12 
13 static inline void init_common_sqe(struct fcoe_task_params *task_params,
14 				   enum fcoe_sqe_request_type request_type)
15 {
16 	memset(task_params->sqe, 0, sizeof(*(task_params->sqe)));
17 	SET_FIELD(task_params->sqe->flags, FCOE_WQE_REQ_TYPE,
18 		  request_type);
19 	task_params->sqe->task_id = task_params->itid;
20 }
21 
22 int init_initiator_rw_fcoe_task(struct fcoe_task_params *task_params,
23 				struct scsi_sgl_task_params *sgl_task_params,
24 				struct regpair sense_data_buffer_phys_addr,
25 				u32 task_retry_id,
26 				u8 fcp_cmd_payload[32])
27 {
28 	struct e4_fcoe_task_context *ctx = task_params->context;
29 	const u8 val_byte = ctx->ystorm_ag_context.byte0;
30 	struct e4_ustorm_fcoe_task_ag_ctx *u_ag_ctx;
31 	struct ystorm_fcoe_task_st_ctx *y_st_ctx;
32 	struct tstorm_fcoe_task_st_ctx *t_st_ctx;
33 	struct mstorm_fcoe_task_st_ctx *m_st_ctx;
34 	u32 io_size, val;
35 	bool slow_sgl;
36 
37 	memset(ctx, 0, sizeof(*(ctx)));
38 	ctx->ystorm_ag_context.byte0 = val_byte;
39 	slow_sgl = scsi_is_slow_sgl(sgl_task_params->num_sges,
40 				    sgl_task_params->small_mid_sge);
41 	io_size = (task_params->task_type == FCOE_TASK_TYPE_WRITE_INITIATOR ?
42 		   task_params->tx_io_size : task_params->rx_io_size);
43 
44 	/* Ystorm ctx */
45 	y_st_ctx = &ctx->ystorm_st_context;
46 	y_st_ctx->data_2_trns_rem = cpu_to_le32(io_size);
47 	y_st_ctx->task_rety_identifier = cpu_to_le32(task_retry_id);
48 	y_st_ctx->task_type = (u8)task_params->task_type;
49 	memcpy(&y_st_ctx->tx_info_union.fcp_cmd_payload,
50 	       fcp_cmd_payload, sizeof(struct fcoe_fcp_cmd_payload));
51 
52 	/* Tstorm ctx */
53 	t_st_ctx = &ctx->tstorm_st_context;
54 	t_st_ctx->read_only.dev_type = (u8)(task_params->is_tape_device == 1 ?
55 					    FCOE_TASK_DEV_TYPE_TAPE :
56 					    FCOE_TASK_DEV_TYPE_DISK);
57 	t_st_ctx->read_only.cid = cpu_to_le32(task_params->conn_cid);
58 	val = cpu_to_le32(task_params->cq_rss_number);
59 	t_st_ctx->read_only.glbl_q_num = val;
60 	t_st_ctx->read_only.fcp_cmd_trns_size = cpu_to_le32(io_size);
61 	t_st_ctx->read_only.task_type = (u8)task_params->task_type;
62 	SET_FIELD(t_st_ctx->read_write.flags,
63 		  FCOE_TSTORM_FCOE_TASK_ST_CTX_READ_WRITE_EXP_FIRST_FRAME, 1);
64 	t_st_ctx->read_write.rx_id = cpu_to_le16(FCOE_RX_ID);
65 
66 	/* Ustorm ctx */
67 	u_ag_ctx = &ctx->ustorm_ag_context;
68 	u_ag_ctx->global_cq_num = cpu_to_le32(task_params->cq_rss_number);
69 
70 	/* Mstorm buffer for sense/rsp data placement */
71 	m_st_ctx = &ctx->mstorm_st_context;
72 	val = cpu_to_le32(sense_data_buffer_phys_addr.hi);
73 	m_st_ctx->rsp_buf_addr.hi = val;
74 	val = cpu_to_le32(sense_data_buffer_phys_addr.lo);
75 	m_st_ctx->rsp_buf_addr.lo = val;
76 
77 	if (task_params->task_type == FCOE_TASK_TYPE_WRITE_INITIATOR) {
78 		/* Ystorm ctx */
79 		y_st_ctx->expect_first_xfer = 1;
80 
81 		/* Set the amount of super SGEs. Can be up to 4. */
82 		SET_FIELD(y_st_ctx->sgl_mode,
83 			  YSTORM_FCOE_TASK_ST_CTX_TX_SGL_MODE,
84 			  (slow_sgl ? SCSI_TX_SLOW_SGL : SCSI_FAST_SGL));
85 		init_scsi_sgl_context(&y_st_ctx->sgl_params,
86 				      &y_st_ctx->data_desc,
87 				      sgl_task_params);
88 
89 		/* Mstorm ctx */
90 		SET_FIELD(m_st_ctx->flags,
91 			  MSTORM_FCOE_TASK_ST_CTX_TX_SGL_MODE,
92 			  (slow_sgl ? SCSI_TX_SLOW_SGL : SCSI_FAST_SGL));
93 		m_st_ctx->sgl_params.sgl_num_sges =
94 			cpu_to_le16(sgl_task_params->num_sges);
95 	} else {
96 		/* Tstorm ctx */
97 		SET_FIELD(t_st_ctx->read_write.flags,
98 			  FCOE_TSTORM_FCOE_TASK_ST_CTX_READ_WRITE_RX_SGL_MODE,
99 			  (slow_sgl ? SCSI_TX_SLOW_SGL : SCSI_FAST_SGL));
100 
101 		/* Mstorm ctx */
102 		m_st_ctx->data_2_trns_rem = cpu_to_le32(io_size);
103 		init_scsi_sgl_context(&m_st_ctx->sgl_params,
104 				      &m_st_ctx->data_desc,
105 				      sgl_task_params);
106 	}
107 
108 	/* Init Sqe */
109 	init_common_sqe(task_params, SEND_FCOE_CMD);
110 
111 	return 0;
112 }
113 
114 int init_initiator_midpath_unsolicited_fcoe_task(
115 	struct fcoe_task_params *task_params,
116 	struct fcoe_tx_mid_path_params *mid_path_fc_header,
117 	struct scsi_sgl_task_params *tx_sgl_task_params,
118 	struct scsi_sgl_task_params *rx_sgl_task_params,
119 	u8 fw_to_place_fc_header)
120 {
121 	struct e4_fcoe_task_context *ctx = task_params->context;
122 	const u8 val_byte = ctx->ystorm_ag_context.byte0;
123 	struct e4_ustorm_fcoe_task_ag_ctx *u_ag_ctx;
124 	struct ystorm_fcoe_task_st_ctx *y_st_ctx;
125 	struct tstorm_fcoe_task_st_ctx *t_st_ctx;
126 	struct mstorm_fcoe_task_st_ctx *m_st_ctx;
127 	u32 val;
128 
129 	memset(ctx, 0, sizeof(*(ctx)));
130 	ctx->ystorm_ag_context.byte0 = val_byte;
131 
132 	/* Init Ystorm */
133 	y_st_ctx = &ctx->ystorm_st_context;
134 	init_scsi_sgl_context(&y_st_ctx->sgl_params,
135 			      &y_st_ctx->data_desc,
136 			      tx_sgl_task_params);
137 	SET_FIELD(y_st_ctx->sgl_mode,
138 		  YSTORM_FCOE_TASK_ST_CTX_TX_SGL_MODE, SCSI_FAST_SGL);
139 	y_st_ctx->data_2_trns_rem = cpu_to_le32(task_params->tx_io_size);
140 	y_st_ctx->task_type = (u8)task_params->task_type;
141 	memcpy(&y_st_ctx->tx_info_union.tx_params.mid_path,
142 	       mid_path_fc_header, sizeof(struct fcoe_tx_mid_path_params));
143 
144 	/* Init Mstorm */
145 	m_st_ctx = &ctx->mstorm_st_context;
146 	init_scsi_sgl_context(&m_st_ctx->sgl_params,
147 			      &m_st_ctx->data_desc,
148 			      rx_sgl_task_params);
149 	SET_FIELD(m_st_ctx->flags,
150 		  MSTORM_FCOE_TASK_ST_CTX_MP_INCLUDE_FC_HEADER,
151 		  fw_to_place_fc_header);
152 	m_st_ctx->data_2_trns_rem = cpu_to_le32(task_params->rx_io_size);
153 
154 	/* Init Tstorm */
155 	t_st_ctx = &ctx->tstorm_st_context;
156 	t_st_ctx->read_only.cid = cpu_to_le32(task_params->conn_cid);
157 	val = cpu_to_le32(task_params->cq_rss_number);
158 	t_st_ctx->read_only.glbl_q_num = val;
159 	t_st_ctx->read_only.task_type = (u8)task_params->task_type;
160 	SET_FIELD(t_st_ctx->read_write.flags,
161 		  FCOE_TSTORM_FCOE_TASK_ST_CTX_READ_WRITE_EXP_FIRST_FRAME, 1);
162 	t_st_ctx->read_write.rx_id = cpu_to_le16(FCOE_RX_ID);
163 
164 	/* Init Ustorm */
165 	u_ag_ctx = &ctx->ustorm_ag_context;
166 	u_ag_ctx->global_cq_num = cpu_to_le32(task_params->cq_rss_number);
167 
168 	/* Init SQE */
169 	init_common_sqe(task_params, SEND_FCOE_MIDPATH);
170 	task_params->sqe->additional_info_union.burst_length =
171 				    tx_sgl_task_params->total_buffer_size;
172 	SET_FIELD(task_params->sqe->flags,
173 		  FCOE_WQE_NUM_SGES, tx_sgl_task_params->num_sges);
174 	SET_FIELD(task_params->sqe->flags, FCOE_WQE_SGL_MODE,
175 		  SCSI_FAST_SGL);
176 
177 	return 0;
178 }
179 
180 int init_initiator_abort_fcoe_task(struct fcoe_task_params *task_params)
181 {
182 	init_common_sqe(task_params, SEND_FCOE_ABTS_REQUEST);
183 	return 0;
184 }
185 
186 int init_initiator_cleanup_fcoe_task(struct fcoe_task_params *task_params)
187 {
188 	init_common_sqe(task_params, FCOE_EXCHANGE_CLEANUP);
189 	return 0;
190 }
191 
192 int init_initiator_sequence_recovery_fcoe_task(
193 	struct fcoe_task_params *task_params, u32 desired_offset)
194 {
195 	init_common_sqe(task_params, FCOE_SEQUENCE_RECOVERY);
196 	task_params->sqe->additional_info_union.seq_rec_updated_offset =
197 								desired_offset;
198 	return 0;
199 }
200