1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* QLogic qed NIC Driver
3  * Copyright (c) 2015-2017  QLogic Corporation
4  * Copyright (c) 2019-2020 Marvell International Ltd.
5  */
6 
7 #include <linux/types.h>
8 #include <asm/byteorder.h>
9 #include <asm/param.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/etherdevice.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/stddef.h>
20 #include <linux/string.h>
21 #include <linux/workqueue.h>
22 #include <linux/errno.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/qed/qed_iscsi_if.h>
26 #include "qed.h"
27 #include "qed_cxt.h"
28 #include "qed_dev_api.h"
29 #include "qed_hsi.h"
30 #include "qed_hw.h"
31 #include "qed_int.h"
32 #include "qed_iscsi.h"
33 #include "qed_ll2.h"
34 #include "qed_mcp.h"
35 #include "qed_sp.h"
36 #include "qed_sriov.h"
37 #include "qed_reg_addr.h"
38 
39 struct qed_iscsi_conn {
40 	struct list_head list_entry;
41 	bool free_on_delete;
42 
43 	u16 conn_id;
44 	u32 icid;
45 	u32 fw_cid;
46 
47 	u8 layer_code;
48 	u8 offl_flags;
49 	u8 connect_mode;
50 	u32 initial_ack;
51 	dma_addr_t sq_pbl_addr;
52 	struct qed_chain r2tq;
53 	struct qed_chain xhq;
54 	struct qed_chain uhq;
55 
56 	struct tcp_upload_params *tcp_upload_params_virt_addr;
57 	dma_addr_t tcp_upload_params_phys_addr;
58 	struct scsi_terminate_extra_params *queue_cnts_virt_addr;
59 	dma_addr_t queue_cnts_phys_addr;
60 	dma_addr_t syn_phy_addr;
61 
62 	u16 syn_ip_payload_length;
63 	u8 local_mac[6];
64 	u8 remote_mac[6];
65 	u16 vlan_id;
66 	u16 tcp_flags;
67 	u8 ip_version;
68 	u32 remote_ip[4];
69 	u32 local_ip[4];
70 	u8 ka_max_probe_cnt;
71 	u8 dup_ack_theshold;
72 	u32 rcv_next;
73 	u32 snd_una;
74 	u32 snd_next;
75 	u32 snd_max;
76 	u32 snd_wnd;
77 	u32 rcv_wnd;
78 	u32 snd_wl1;
79 	u32 cwnd;
80 	u32 ss_thresh;
81 	u16 srtt;
82 	u16 rtt_var;
83 	u32 ts_recent;
84 	u32 ts_recent_age;
85 	u32 total_rt;
86 	u32 ka_timeout_delta;
87 	u32 rt_timeout_delta;
88 	u8 dup_ack_cnt;
89 	u8 snd_wnd_probe_cnt;
90 	u8 ka_probe_cnt;
91 	u8 rt_cnt;
92 	u32 flow_label;
93 	u32 ka_timeout;
94 	u32 ka_interval;
95 	u32 max_rt_time;
96 	u32 initial_rcv_wnd;
97 	u8 ttl;
98 	u8 tos_or_tc;
99 	u16 remote_port;
100 	u16 local_port;
101 	u16 mss;
102 	u8 snd_wnd_scale;
103 	u8 rcv_wnd_scale;
104 	u16 da_timeout_value;
105 	u8 ack_frequency;
106 
107 	u8 update_flag;
108 	u8 default_cq;
109 	u32 max_seq_size;
110 	u32 max_recv_pdu_length;
111 	u32 max_send_pdu_length;
112 	u32 first_seq_length;
113 	u32 exp_stat_sn;
114 	u32 stat_sn;
115 	u16 physical_q0;
116 	u16 physical_q1;
117 	u8 abortive_dsconnect;
118 };
119 
120 static int qed_iscsi_async_event(struct qed_hwfn *p_hwfn, u8 fw_event_code,
121 				 __le16 echo, union event_ring_data *data,
122 				 u8 fw_return_code)
123 {
124 	if (p_hwfn->p_iscsi_info->event_cb) {
125 		struct qed_iscsi_info *p_iscsi = p_hwfn->p_iscsi_info;
126 
127 		return p_iscsi->event_cb(p_iscsi->event_context,
128 					 fw_event_code, data);
129 	} else {
130 		DP_NOTICE(p_hwfn, "iSCSI async completion is not set\n");
131 		return -EINVAL;
132 	}
133 }
134 
135 static int
136 qed_sp_iscsi_func_start(struct qed_hwfn *p_hwfn,
137 			enum spq_mode comp_mode,
138 			struct qed_spq_comp_cb *p_comp_addr,
139 			void *event_context, iscsi_event_cb_t async_event_cb)
140 {
141 	struct iscsi_init_ramrod_params *p_ramrod = NULL;
142 	struct scsi_init_func_queues *p_queue = NULL;
143 	struct qed_iscsi_pf_params *p_params = NULL;
144 	struct iscsi_spe_func_init *p_init = NULL;
145 	struct qed_spq_entry *p_ent = NULL;
146 	struct qed_sp_init_data init_data;
147 	int rc = 0;
148 	u32 dval;
149 	u16 val;
150 	u8 i;
151 
152 	/* Get SPQ entry */
153 	memset(&init_data, 0, sizeof(init_data));
154 	init_data.cid = qed_spq_get_cid(p_hwfn);
155 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
156 	init_data.comp_mode = comp_mode;
157 	init_data.p_comp_data = p_comp_addr;
158 
159 	rc = qed_sp_init_request(p_hwfn, &p_ent,
160 				 ISCSI_RAMROD_CMD_ID_INIT_FUNC,
161 				 PROTOCOLID_TCP_ULP, &init_data);
162 	if (rc)
163 		return rc;
164 
165 	p_ramrod = &p_ent->ramrod.iscsi_init;
166 	p_init = &p_ramrod->iscsi_init_spe;
167 	p_params = &p_hwfn->pf_params.iscsi_pf_params;
168 	p_queue = &p_init->q_params;
169 
170 	/* Sanity */
171 	if (p_params->num_queues > p_hwfn->hw_info.feat_num[QED_ISCSI_CQ]) {
172 		DP_ERR(p_hwfn,
173 		       "Cannot satisfy CQ amount. Queues requested %d, CQs available %d. Aborting function start\n",
174 		       p_params->num_queues,
175 		       p_hwfn->hw_info.feat_num[QED_ISCSI_CQ]);
176 		qed_sp_destroy_request(p_hwfn, p_ent);
177 		return -EINVAL;
178 	}
179 
180 	val = p_params->half_way_close_timeout;
181 	p_init->half_way_close_timeout = cpu_to_le16(val);
182 	p_init->num_sq_pages_in_ring = p_params->num_sq_pages_in_ring;
183 	p_init->num_r2tq_pages_in_ring = p_params->num_r2tq_pages_in_ring;
184 	p_init->num_uhq_pages_in_ring = p_params->num_uhq_pages_in_ring;
185 	p_init->ll2_rx_queue_id =
186 	    p_hwfn->hw_info.resc_start[QED_LL2_RAM_QUEUE] +
187 	    p_params->ll2_ooo_queue_id;
188 
189 	p_init->func_params.log_page_size = p_params->log_page_size;
190 	val = p_params->num_tasks;
191 	p_init->func_params.num_tasks = cpu_to_le16(val);
192 	p_init->debug_mode.flags = p_params->debug_mode;
193 
194 	DMA_REGPAIR_LE(p_queue->glbl_q_params_addr,
195 		       p_params->glbl_q_params_addr);
196 
197 	val = p_params->cq_num_entries;
198 	p_queue->cq_num_entries = cpu_to_le16(val);
199 	val = p_params->cmdq_num_entries;
200 	p_queue->cmdq_num_entries = cpu_to_le16(val);
201 	p_queue->num_queues = p_params->num_queues;
202 	dval = (u8)p_hwfn->hw_info.resc_start[QED_CMDQS_CQS];
203 	p_queue->queue_relative_offset = (u8)dval;
204 	p_queue->cq_sb_pi = p_params->gl_rq_pi;
205 	p_queue->cmdq_sb_pi = p_params->gl_cmd_pi;
206 
207 	for (i = 0; i < p_params->num_queues; i++) {
208 		val = qed_get_igu_sb_id(p_hwfn, i);
209 		p_queue->cq_cmdq_sb_num_arr[i] = cpu_to_le16(val);
210 	}
211 
212 	p_queue->bdq_resource_id = (u8)RESC_START(p_hwfn, QED_BDQ);
213 
214 	DMA_REGPAIR_LE(p_queue->bdq_pbl_base_address[BDQ_ID_RQ],
215 		       p_params->bdq_pbl_base_addr[BDQ_ID_RQ]);
216 	p_queue->bdq_pbl_num_entries[BDQ_ID_RQ] =
217 	    p_params->bdq_pbl_num_entries[BDQ_ID_RQ];
218 	val = p_params->bdq_xoff_threshold[BDQ_ID_RQ];
219 	p_queue->bdq_xoff_threshold[BDQ_ID_RQ] = cpu_to_le16(val);
220 	val = p_params->bdq_xon_threshold[BDQ_ID_RQ];
221 	p_queue->bdq_xon_threshold[BDQ_ID_RQ] = cpu_to_le16(val);
222 
223 	DMA_REGPAIR_LE(p_queue->bdq_pbl_base_address[BDQ_ID_IMM_DATA],
224 		       p_params->bdq_pbl_base_addr[BDQ_ID_IMM_DATA]);
225 	p_queue->bdq_pbl_num_entries[BDQ_ID_IMM_DATA] =
226 	    p_params->bdq_pbl_num_entries[BDQ_ID_IMM_DATA];
227 	val = p_params->bdq_xoff_threshold[BDQ_ID_IMM_DATA];
228 	p_queue->bdq_xoff_threshold[BDQ_ID_IMM_DATA] = cpu_to_le16(val);
229 	val = p_params->bdq_xon_threshold[BDQ_ID_IMM_DATA];
230 	p_queue->bdq_xon_threshold[BDQ_ID_IMM_DATA] = cpu_to_le16(val);
231 	val = p_params->rq_buffer_size;
232 	p_queue->rq_buffer_size = cpu_to_le16(val);
233 	if (p_params->is_target) {
234 		SET_FIELD(p_queue->q_validity,
235 			  SCSI_INIT_FUNC_QUEUES_RQ_VALID, 1);
236 		if (p_queue->bdq_pbl_num_entries[BDQ_ID_IMM_DATA])
237 			SET_FIELD(p_queue->q_validity,
238 				  SCSI_INIT_FUNC_QUEUES_IMM_DATA_VALID, 1);
239 		SET_FIELD(p_queue->q_validity,
240 			  SCSI_INIT_FUNC_QUEUES_CMD_VALID, 1);
241 	} else {
242 		SET_FIELD(p_queue->q_validity,
243 			  SCSI_INIT_FUNC_QUEUES_RQ_VALID, 1);
244 	}
245 	p_ramrod->tcp_init.two_msl_timer = cpu_to_le32(p_params->two_msl_timer);
246 	val = p_params->tx_sws_timer;
247 	p_ramrod->tcp_init.tx_sws_timer = cpu_to_le16(val);
248 	p_ramrod->tcp_init.max_fin_rt = p_params->max_fin_rt;
249 
250 	p_hwfn->p_iscsi_info->event_context = event_context;
251 	p_hwfn->p_iscsi_info->event_cb = async_event_cb;
252 
253 	qed_spq_register_async_cb(p_hwfn, PROTOCOLID_TCP_ULP,
254 				  qed_iscsi_async_event);
255 
256 	return qed_spq_post(p_hwfn, p_ent, NULL);
257 }
258 
259 static int qed_sp_iscsi_conn_offload(struct qed_hwfn *p_hwfn,
260 				     struct qed_iscsi_conn *p_conn,
261 				     enum spq_mode comp_mode,
262 				     struct qed_spq_comp_cb *p_comp_addr)
263 {
264 	struct iscsi_spe_conn_offload *p_ramrod = NULL;
265 	struct tcp_offload_params_opt2 *p_tcp2 = NULL;
266 	struct tcp_offload_params *p_tcp = NULL;
267 	struct qed_spq_entry *p_ent = NULL;
268 	struct qed_sp_init_data init_data;
269 	dma_addr_t r2tq_pbl_addr;
270 	dma_addr_t xhq_pbl_addr;
271 	dma_addr_t uhq_pbl_addr;
272 	u16 physical_q;
273 	__le16 tmp;
274 	int rc = 0;
275 	u32 dval;
276 	u16 wval;
277 	u16 *p;
278 	u8 i;
279 
280 	/* Get SPQ entry */
281 	memset(&init_data, 0, sizeof(init_data));
282 	init_data.cid = p_conn->icid;
283 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
284 	init_data.comp_mode = comp_mode;
285 	init_data.p_comp_data = p_comp_addr;
286 
287 	rc = qed_sp_init_request(p_hwfn, &p_ent,
288 				 ISCSI_RAMROD_CMD_ID_OFFLOAD_CONN,
289 				 PROTOCOLID_TCP_ULP, &init_data);
290 	if (rc)
291 		return rc;
292 
293 	p_ramrod = &p_ent->ramrod.iscsi_conn_offload;
294 
295 	/* Transmission PQ is the first of the PF */
296 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
297 	p_conn->physical_q0 = physical_q;
298 	p_ramrod->iscsi.physical_q0 = cpu_to_le16(physical_q);
299 
300 	/* iSCSI Pure-ACK PQ */
301 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
302 	p_conn->physical_q1 = physical_q;
303 	p_ramrod->iscsi.physical_q1 = cpu_to_le16(physical_q);
304 
305 	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
306 
307 	DMA_REGPAIR_LE(p_ramrod->iscsi.sq_pbl_addr, p_conn->sq_pbl_addr);
308 
309 	r2tq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->r2tq);
310 	DMA_REGPAIR_LE(p_ramrod->iscsi.r2tq_pbl_addr, r2tq_pbl_addr);
311 
312 	xhq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->xhq);
313 	DMA_REGPAIR_LE(p_ramrod->iscsi.xhq_pbl_addr, xhq_pbl_addr);
314 
315 	uhq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->uhq);
316 	DMA_REGPAIR_LE(p_ramrod->iscsi.uhq_pbl_addr, uhq_pbl_addr);
317 
318 	p_ramrod->iscsi.initial_ack = cpu_to_le32(p_conn->initial_ack);
319 	p_ramrod->iscsi.flags = p_conn->offl_flags;
320 	p_ramrod->iscsi.default_cq = p_conn->default_cq;
321 	p_ramrod->iscsi.stat_sn = cpu_to_le32(p_conn->stat_sn);
322 
323 	if (!GET_FIELD(p_ramrod->iscsi.flags,
324 		       ISCSI_CONN_OFFLOAD_PARAMS_TCP_ON_CHIP_1B)) {
325 		p_tcp = &p_ramrod->tcp;
326 
327 		p = (u16 *)p_conn->local_mac;
328 		tmp = cpu_to_le16(get_unaligned_be16(p));
329 		p_tcp->local_mac_addr_hi = tmp;
330 		tmp = cpu_to_le16(get_unaligned_be16(p + 1));
331 		p_tcp->local_mac_addr_mid = tmp;
332 		tmp = cpu_to_le16(get_unaligned_be16(p + 2));
333 		p_tcp->local_mac_addr_lo = tmp;
334 
335 		p = (u16 *)p_conn->remote_mac;
336 		tmp = cpu_to_le16(get_unaligned_be16(p));
337 		p_tcp->remote_mac_addr_hi = tmp;
338 		tmp = cpu_to_le16(get_unaligned_be16(p + 1));
339 		p_tcp->remote_mac_addr_mid = tmp;
340 		tmp = cpu_to_le16(get_unaligned_be16(p + 2));
341 		p_tcp->remote_mac_addr_lo = tmp;
342 
343 		p_tcp->vlan_id = cpu_to_le16(p_conn->vlan_id);
344 
345 		p_tcp->flags = cpu_to_le16(p_conn->tcp_flags);
346 		p_tcp->ip_version = p_conn->ip_version;
347 		for (i = 0; i < 4; i++) {
348 			dval = p_conn->remote_ip[i];
349 			p_tcp->remote_ip[i] = cpu_to_le32(dval);
350 			dval = p_conn->local_ip[i];
351 			p_tcp->local_ip[i] = cpu_to_le32(dval);
352 		}
353 		p_tcp->ka_max_probe_cnt = p_conn->ka_max_probe_cnt;
354 		p_tcp->dup_ack_theshold = p_conn->dup_ack_theshold;
355 
356 		p_tcp->rcv_next = cpu_to_le32(p_conn->rcv_next);
357 		p_tcp->snd_una = cpu_to_le32(p_conn->snd_una);
358 		p_tcp->snd_next = cpu_to_le32(p_conn->snd_next);
359 		p_tcp->snd_max = cpu_to_le32(p_conn->snd_max);
360 		p_tcp->snd_wnd = cpu_to_le32(p_conn->snd_wnd);
361 		p_tcp->rcv_wnd = cpu_to_le32(p_conn->rcv_wnd);
362 		p_tcp->snd_wl1 = cpu_to_le32(p_conn->snd_wl1);
363 		p_tcp->cwnd = cpu_to_le32(p_conn->cwnd);
364 		p_tcp->ss_thresh = cpu_to_le32(p_conn->ss_thresh);
365 		p_tcp->srtt = cpu_to_le16(p_conn->srtt);
366 		p_tcp->rtt_var = cpu_to_le16(p_conn->rtt_var);
367 		p_tcp->ts_recent = cpu_to_le32(p_conn->ts_recent);
368 		p_tcp->ts_recent_age = cpu_to_le32(p_conn->ts_recent_age);
369 		p_tcp->total_rt = cpu_to_le32(p_conn->total_rt);
370 		dval = p_conn->ka_timeout_delta;
371 		p_tcp->ka_timeout_delta = cpu_to_le32(dval);
372 		dval = p_conn->rt_timeout_delta;
373 		p_tcp->rt_timeout_delta = cpu_to_le32(dval);
374 		p_tcp->dup_ack_cnt = p_conn->dup_ack_cnt;
375 		p_tcp->snd_wnd_probe_cnt = p_conn->snd_wnd_probe_cnt;
376 		p_tcp->ka_probe_cnt = p_conn->ka_probe_cnt;
377 		p_tcp->rt_cnt = p_conn->rt_cnt;
378 		p_tcp->flow_label = cpu_to_le32(p_conn->flow_label);
379 		p_tcp->ka_timeout = cpu_to_le32(p_conn->ka_timeout);
380 		p_tcp->ka_interval = cpu_to_le32(p_conn->ka_interval);
381 		p_tcp->max_rt_time = cpu_to_le32(p_conn->max_rt_time);
382 		dval = p_conn->initial_rcv_wnd;
383 		p_tcp->initial_rcv_wnd = cpu_to_le32(dval);
384 		p_tcp->ttl = p_conn->ttl;
385 		p_tcp->tos_or_tc = p_conn->tos_or_tc;
386 		p_tcp->remote_port = cpu_to_le16(p_conn->remote_port);
387 		p_tcp->local_port = cpu_to_le16(p_conn->local_port);
388 		p_tcp->mss = cpu_to_le16(p_conn->mss);
389 		p_tcp->snd_wnd_scale = p_conn->snd_wnd_scale;
390 		p_tcp->rcv_wnd_scale = p_conn->rcv_wnd_scale;
391 		wval = p_conn->da_timeout_value;
392 		p_tcp->da_timeout_value = cpu_to_le16(wval);
393 		p_tcp->ack_frequency = p_conn->ack_frequency;
394 		p_tcp->connect_mode = p_conn->connect_mode;
395 	} else {
396 		p_tcp2 =
397 		    &((struct iscsi_spe_conn_offload_option2 *)p_ramrod)->tcp;
398 
399 		p = (u16 *)p_conn->local_mac;
400 		tmp = cpu_to_le16(get_unaligned_be16(p));
401 		p_tcp2->local_mac_addr_hi = tmp;
402 		tmp = cpu_to_le16(get_unaligned_be16(p + 1));
403 		p_tcp2->local_mac_addr_mid = tmp;
404 		tmp = cpu_to_le16(get_unaligned_be16(p + 2));
405 		p_tcp2->local_mac_addr_lo = tmp;
406 
407 		p = (u16 *)p_conn->remote_mac;
408 		tmp = cpu_to_le16(get_unaligned_be16(p));
409 		p_tcp2->remote_mac_addr_hi = tmp;
410 		tmp = cpu_to_le16(get_unaligned_be16(p + 1));
411 		p_tcp2->remote_mac_addr_mid = tmp;
412 		tmp = cpu_to_le16(get_unaligned_be16(p + 2));
413 		p_tcp2->remote_mac_addr_lo = tmp;
414 
415 		p_tcp2->vlan_id = cpu_to_le16(p_conn->vlan_id);
416 		p_tcp2->flags = cpu_to_le16(p_conn->tcp_flags);
417 
418 		p_tcp2->ip_version = p_conn->ip_version;
419 		for (i = 0; i < 4; i++) {
420 			dval = p_conn->remote_ip[i];
421 			p_tcp2->remote_ip[i] = cpu_to_le32(dval);
422 			dval = p_conn->local_ip[i];
423 			p_tcp2->local_ip[i] = cpu_to_le32(dval);
424 		}
425 
426 		p_tcp2->flow_label = cpu_to_le32(p_conn->flow_label);
427 		p_tcp2->ttl = p_conn->ttl;
428 		p_tcp2->tos_or_tc = p_conn->tos_or_tc;
429 		p_tcp2->remote_port = cpu_to_le16(p_conn->remote_port);
430 		p_tcp2->local_port = cpu_to_le16(p_conn->local_port);
431 		p_tcp2->mss = cpu_to_le16(p_conn->mss);
432 		p_tcp2->rcv_wnd_scale = p_conn->rcv_wnd_scale;
433 		p_tcp2->connect_mode = p_conn->connect_mode;
434 		wval = p_conn->syn_ip_payload_length;
435 		p_tcp2->syn_ip_payload_length = cpu_to_le16(wval);
436 		p_tcp2->syn_phy_addr_lo = DMA_LO_LE(p_conn->syn_phy_addr);
437 		p_tcp2->syn_phy_addr_hi = DMA_HI_LE(p_conn->syn_phy_addr);
438 		p_tcp2->cwnd = cpu_to_le32(p_conn->cwnd);
439 		p_tcp2->ka_max_probe_cnt = p_conn->ka_probe_cnt;
440 		p_tcp2->ka_timeout = cpu_to_le32(p_conn->ka_timeout);
441 		p_tcp2->max_rt_time = cpu_to_le32(p_conn->max_rt_time);
442 		p_tcp2->ka_interval = cpu_to_le32(p_conn->ka_interval);
443 	}
444 
445 	return qed_spq_post(p_hwfn, p_ent, NULL);
446 }
447 
448 static int qed_sp_iscsi_conn_update(struct qed_hwfn *p_hwfn,
449 				    struct qed_iscsi_conn *p_conn,
450 				    enum spq_mode comp_mode,
451 				    struct qed_spq_comp_cb *p_comp_addr)
452 {
453 	struct iscsi_conn_update_ramrod_params *p_ramrod = NULL;
454 	struct qed_spq_entry *p_ent = NULL;
455 	struct qed_sp_init_data init_data;
456 	int rc;
457 	u32 dval;
458 
459 	/* Get SPQ entry */
460 	memset(&init_data, 0, sizeof(init_data));
461 	init_data.cid = p_conn->icid;
462 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
463 	init_data.comp_mode = comp_mode;
464 	init_data.p_comp_data = p_comp_addr;
465 
466 	rc = qed_sp_init_request(p_hwfn, &p_ent,
467 				 ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
468 				 PROTOCOLID_TCP_ULP, &init_data);
469 	if (rc)
470 		return rc;
471 
472 	p_ramrod = &p_ent->ramrod.iscsi_conn_update;
473 
474 	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
475 	p_ramrod->flags = p_conn->update_flag;
476 	p_ramrod->max_seq_size = cpu_to_le32(p_conn->max_seq_size);
477 	dval = p_conn->max_recv_pdu_length;
478 	p_ramrod->max_recv_pdu_length = cpu_to_le32(dval);
479 	dval = p_conn->max_send_pdu_length;
480 	p_ramrod->max_send_pdu_length = cpu_to_le32(dval);
481 	dval = p_conn->first_seq_length;
482 	p_ramrod->first_seq_length = cpu_to_le32(dval);
483 	p_ramrod->exp_stat_sn = cpu_to_le32(p_conn->exp_stat_sn);
484 
485 	return qed_spq_post(p_hwfn, p_ent, NULL);
486 }
487 
488 static int
489 qed_sp_iscsi_mac_update(struct qed_hwfn *p_hwfn,
490 			struct qed_iscsi_conn *p_conn,
491 			enum spq_mode comp_mode,
492 			struct qed_spq_comp_cb *p_comp_addr)
493 {
494 	struct iscsi_spe_conn_mac_update *p_ramrod = NULL;
495 	struct qed_spq_entry *p_ent = NULL;
496 	struct qed_sp_init_data init_data;
497 	int rc = -EINVAL;
498 	u8 ucval;
499 
500 	/* Get SPQ entry */
501 	memset(&init_data, 0, sizeof(init_data));
502 	init_data.cid = p_conn->icid;
503 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
504 	init_data.comp_mode = comp_mode;
505 	init_data.p_comp_data = p_comp_addr;
506 
507 	rc = qed_sp_init_request(p_hwfn, &p_ent,
508 				 ISCSI_RAMROD_CMD_ID_MAC_UPDATE,
509 				 PROTOCOLID_TCP_ULP, &init_data);
510 	if (rc)
511 		return rc;
512 
513 	p_ramrod = &p_ent->ramrod.iscsi_conn_mac_update;
514 
515 	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
516 	ucval = p_conn->remote_mac[1];
517 	((u8 *)(&p_ramrod->remote_mac_addr_hi))[0] = ucval;
518 	ucval = p_conn->remote_mac[0];
519 	((u8 *)(&p_ramrod->remote_mac_addr_hi))[1] = ucval;
520 	ucval = p_conn->remote_mac[3];
521 	((u8 *)(&p_ramrod->remote_mac_addr_mid))[0] = ucval;
522 	ucval = p_conn->remote_mac[2];
523 	((u8 *)(&p_ramrod->remote_mac_addr_mid))[1] = ucval;
524 	ucval = p_conn->remote_mac[5];
525 	((u8 *)(&p_ramrod->remote_mac_addr_lo))[0] = ucval;
526 	ucval = p_conn->remote_mac[4];
527 	((u8 *)(&p_ramrod->remote_mac_addr_lo))[1] = ucval;
528 
529 	return qed_spq_post(p_hwfn, p_ent, NULL);
530 }
531 
532 static int qed_sp_iscsi_conn_terminate(struct qed_hwfn *p_hwfn,
533 				       struct qed_iscsi_conn *p_conn,
534 				       enum spq_mode comp_mode,
535 				       struct qed_spq_comp_cb *p_comp_addr)
536 {
537 	struct iscsi_spe_conn_termination *p_ramrod = NULL;
538 	struct qed_spq_entry *p_ent = NULL;
539 	struct qed_sp_init_data init_data;
540 	int rc = -EINVAL;
541 
542 	/* Get SPQ entry */
543 	memset(&init_data, 0, sizeof(init_data));
544 	init_data.cid = p_conn->icid;
545 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
546 	init_data.comp_mode = comp_mode;
547 	init_data.p_comp_data = p_comp_addr;
548 
549 	rc = qed_sp_init_request(p_hwfn, &p_ent,
550 				 ISCSI_RAMROD_CMD_ID_TERMINATION_CONN,
551 				 PROTOCOLID_TCP_ULP, &init_data);
552 	if (rc)
553 		return rc;
554 
555 	p_ramrod = &p_ent->ramrod.iscsi_conn_terminate;
556 
557 	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
558 	p_ramrod->abortive = p_conn->abortive_dsconnect;
559 
560 	DMA_REGPAIR_LE(p_ramrod->query_params_addr,
561 		       p_conn->tcp_upload_params_phys_addr);
562 	DMA_REGPAIR_LE(p_ramrod->queue_cnts_addr, p_conn->queue_cnts_phys_addr);
563 
564 	return qed_spq_post(p_hwfn, p_ent, NULL);
565 }
566 
567 static int qed_sp_iscsi_conn_clear_sq(struct qed_hwfn *p_hwfn,
568 				      struct qed_iscsi_conn *p_conn,
569 				      enum spq_mode comp_mode,
570 				      struct qed_spq_comp_cb *p_comp_addr)
571 {
572 	struct qed_spq_entry *p_ent = NULL;
573 	struct qed_sp_init_data init_data;
574 	int rc = -EINVAL;
575 
576 	/* Get SPQ entry */
577 	memset(&init_data, 0, sizeof(init_data));
578 	init_data.cid = p_conn->icid;
579 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
580 	init_data.comp_mode = comp_mode;
581 	init_data.p_comp_data = p_comp_addr;
582 
583 	rc = qed_sp_init_request(p_hwfn, &p_ent,
584 				 ISCSI_RAMROD_CMD_ID_CLEAR_SQ,
585 				 PROTOCOLID_TCP_ULP, &init_data);
586 	if (rc)
587 		return rc;
588 
589 	return qed_spq_post(p_hwfn, p_ent, NULL);
590 }
591 
592 static int qed_sp_iscsi_func_stop(struct qed_hwfn *p_hwfn,
593 				  enum spq_mode comp_mode,
594 				  struct qed_spq_comp_cb *p_comp_addr)
595 {
596 	struct qed_spq_entry *p_ent = NULL;
597 	struct qed_sp_init_data init_data;
598 	int rc = 0;
599 
600 	/* Get SPQ entry */
601 	memset(&init_data, 0, sizeof(init_data));
602 	init_data.cid = qed_spq_get_cid(p_hwfn);
603 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
604 	init_data.comp_mode = comp_mode;
605 	init_data.p_comp_data = p_comp_addr;
606 
607 	rc = qed_sp_init_request(p_hwfn, &p_ent,
608 				 ISCSI_RAMROD_CMD_ID_DESTROY_FUNC,
609 				 PROTOCOLID_TCP_ULP, &init_data);
610 	if (rc)
611 		return rc;
612 
613 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
614 
615 	qed_spq_unregister_async_cb(p_hwfn, PROTOCOLID_TCP_ULP);
616 	return rc;
617 }
618 
619 static void __iomem *qed_iscsi_get_db_addr(struct qed_hwfn *p_hwfn, u32 cid)
620 {
621 	return (u8 __iomem *)p_hwfn->doorbells +
622 			     qed_db_addr(cid, DQ_DEMS_LEGACY);
623 }
624 
625 static void __iomem *qed_iscsi_get_primary_bdq_prod(struct qed_hwfn *p_hwfn,
626 						    u8 bdq_id)
627 {
628 	if (RESC_NUM(p_hwfn, QED_BDQ)) {
629 		return (u8 __iomem *)p_hwfn->regview +
630 		       GTT_BAR0_MAP_REG_MSDM_RAM +
631 		       MSTORM_SCSI_BDQ_EXT_PROD_OFFSET(RESC_START(p_hwfn,
632 								  QED_BDQ),
633 						       bdq_id);
634 	} else {
635 		DP_NOTICE(p_hwfn, "BDQ is not allocated!\n");
636 		return NULL;
637 	}
638 }
639 
640 static void __iomem *qed_iscsi_get_secondary_bdq_prod(struct qed_hwfn *p_hwfn,
641 						      u8 bdq_id)
642 {
643 	if (RESC_NUM(p_hwfn, QED_BDQ)) {
644 		return (u8 __iomem *)p_hwfn->regview +
645 		       GTT_BAR0_MAP_REG_TSDM_RAM +
646 		       TSTORM_SCSI_BDQ_EXT_PROD_OFFSET(RESC_START(p_hwfn,
647 								  QED_BDQ),
648 						       bdq_id);
649 	} else {
650 		DP_NOTICE(p_hwfn, "BDQ is not allocated!\n");
651 		return NULL;
652 	}
653 }
654 
655 static int qed_iscsi_setup_connection(struct qed_iscsi_conn *p_conn)
656 {
657 	if (!p_conn->queue_cnts_virt_addr)
658 		goto nomem;
659 	memset(p_conn->queue_cnts_virt_addr, 0,
660 	       sizeof(*p_conn->queue_cnts_virt_addr));
661 
662 	if (!p_conn->tcp_upload_params_virt_addr)
663 		goto nomem;
664 	memset(p_conn->tcp_upload_params_virt_addr, 0,
665 	       sizeof(*p_conn->tcp_upload_params_virt_addr));
666 
667 	if (!p_conn->r2tq.p_virt_addr)
668 		goto nomem;
669 	qed_chain_pbl_zero_mem(&p_conn->r2tq);
670 
671 	if (!p_conn->uhq.p_virt_addr)
672 		goto nomem;
673 	qed_chain_pbl_zero_mem(&p_conn->uhq);
674 
675 	if (!p_conn->xhq.p_virt_addr)
676 		goto nomem;
677 	qed_chain_pbl_zero_mem(&p_conn->xhq);
678 
679 	return 0;
680 nomem:
681 	return -ENOMEM;
682 }
683 
684 static int qed_iscsi_allocate_connection(struct qed_hwfn *p_hwfn,
685 					 struct qed_iscsi_conn **p_out_conn)
686 {
687 	struct scsi_terminate_extra_params *p_q_cnts = NULL;
688 	struct qed_iscsi_pf_params *p_params = NULL;
689 	struct qed_chain_init_params params = {
690 		.mode		= QED_CHAIN_MODE_PBL,
691 		.intended_use	= QED_CHAIN_USE_TO_CONSUME_PRODUCE,
692 		.cnt_type	= QED_CHAIN_CNT_TYPE_U16,
693 	};
694 	struct tcp_upload_params *p_tcp = NULL;
695 	struct qed_iscsi_conn *p_conn = NULL;
696 	int rc = 0;
697 
698 	/* Try finding a free connection that can be used */
699 	spin_lock_bh(&p_hwfn->p_iscsi_info->lock);
700 	if (!list_empty(&p_hwfn->p_iscsi_info->free_list))
701 		p_conn = list_first_entry(&p_hwfn->p_iscsi_info->free_list,
702 					  struct qed_iscsi_conn, list_entry);
703 	if (p_conn) {
704 		list_del(&p_conn->list_entry);
705 		spin_unlock_bh(&p_hwfn->p_iscsi_info->lock);
706 		*p_out_conn = p_conn;
707 		return 0;
708 	}
709 	spin_unlock_bh(&p_hwfn->p_iscsi_info->lock);
710 
711 	/* Need to allocate a new connection */
712 	p_params = &p_hwfn->pf_params.iscsi_pf_params;
713 
714 	p_conn = kzalloc(sizeof(*p_conn), GFP_KERNEL);
715 	if (!p_conn)
716 		return -ENOMEM;
717 
718 	p_q_cnts = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
719 				      sizeof(*p_q_cnts),
720 				      &p_conn->queue_cnts_phys_addr,
721 				      GFP_KERNEL);
722 	if (!p_q_cnts)
723 		goto nomem_queue_cnts_param;
724 	p_conn->queue_cnts_virt_addr = p_q_cnts;
725 
726 	p_tcp = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
727 				   sizeof(*p_tcp),
728 				   &p_conn->tcp_upload_params_phys_addr,
729 				   GFP_KERNEL);
730 	if (!p_tcp)
731 		goto nomem_upload_param;
732 	p_conn->tcp_upload_params_virt_addr = p_tcp;
733 
734 	params.num_elems = p_params->num_r2tq_pages_in_ring *
735 			   QED_CHAIN_PAGE_SIZE / sizeof(struct iscsi_wqe);
736 	params.elem_size = sizeof(struct iscsi_wqe);
737 
738 	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->r2tq, &params);
739 	if (rc)
740 		goto nomem_r2tq;
741 
742 	params.num_elems = p_params->num_uhq_pages_in_ring *
743 			   QED_CHAIN_PAGE_SIZE / sizeof(struct iscsi_uhqe);
744 	params.elem_size = sizeof(struct iscsi_uhqe);
745 
746 	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->uhq, &params);
747 	if (rc)
748 		goto nomem_uhq;
749 
750 	params.elem_size = sizeof(struct iscsi_xhqe);
751 
752 	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->xhq, &params);
753 	if (rc)
754 		goto nomem;
755 
756 	p_conn->free_on_delete = true;
757 	*p_out_conn = p_conn;
758 	return 0;
759 
760 nomem:
761 	qed_chain_free(p_hwfn->cdev, &p_conn->uhq);
762 nomem_uhq:
763 	qed_chain_free(p_hwfn->cdev, &p_conn->r2tq);
764 nomem_r2tq:
765 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
766 			  sizeof(struct tcp_upload_params),
767 			  p_conn->tcp_upload_params_virt_addr,
768 			  p_conn->tcp_upload_params_phys_addr);
769 nomem_upload_param:
770 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
771 			  sizeof(struct scsi_terminate_extra_params),
772 			  p_conn->queue_cnts_virt_addr,
773 			  p_conn->queue_cnts_phys_addr);
774 nomem_queue_cnts_param:
775 	kfree(p_conn);
776 
777 	return -ENOMEM;
778 }
779 
780 static int qed_iscsi_acquire_connection(struct qed_hwfn *p_hwfn,
781 					struct qed_iscsi_conn *p_in_conn,
782 					struct qed_iscsi_conn **p_out_conn)
783 {
784 	struct qed_iscsi_conn *p_conn = NULL;
785 	int rc = 0;
786 	u32 icid;
787 
788 	spin_lock_bh(&p_hwfn->p_iscsi_info->lock);
789 	rc = qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_TCP_ULP, &icid);
790 	spin_unlock_bh(&p_hwfn->p_iscsi_info->lock);
791 	if (rc)
792 		return rc;
793 
794 	/* Use input connection or allocate a new one */
795 	if (p_in_conn)
796 		p_conn = p_in_conn;
797 	else
798 		rc = qed_iscsi_allocate_connection(p_hwfn, &p_conn);
799 
800 	if (!rc)
801 		rc = qed_iscsi_setup_connection(p_conn);
802 
803 	if (rc) {
804 		spin_lock_bh(&p_hwfn->p_iscsi_info->lock);
805 		qed_cxt_release_cid(p_hwfn, icid);
806 		spin_unlock_bh(&p_hwfn->p_iscsi_info->lock);
807 		return rc;
808 	}
809 
810 	p_conn->icid = icid;
811 	p_conn->conn_id = (u16)icid;
812 	p_conn->fw_cid = (p_hwfn->hw_info.opaque_fid << 16) | icid;
813 
814 	*p_out_conn = p_conn;
815 
816 	return rc;
817 }
818 
819 static void qed_iscsi_release_connection(struct qed_hwfn *p_hwfn,
820 					 struct qed_iscsi_conn *p_conn)
821 {
822 	spin_lock_bh(&p_hwfn->p_iscsi_info->lock);
823 	list_add_tail(&p_conn->list_entry, &p_hwfn->p_iscsi_info->free_list);
824 	qed_cxt_release_cid(p_hwfn, p_conn->icid);
825 	spin_unlock_bh(&p_hwfn->p_iscsi_info->lock);
826 }
827 
828 static void qed_iscsi_free_connection(struct qed_hwfn *p_hwfn,
829 				      struct qed_iscsi_conn *p_conn)
830 {
831 	qed_chain_free(p_hwfn->cdev, &p_conn->xhq);
832 	qed_chain_free(p_hwfn->cdev, &p_conn->uhq);
833 	qed_chain_free(p_hwfn->cdev, &p_conn->r2tq);
834 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
835 			  sizeof(struct tcp_upload_params),
836 			  p_conn->tcp_upload_params_virt_addr,
837 			  p_conn->tcp_upload_params_phys_addr);
838 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
839 			  sizeof(struct scsi_terminate_extra_params),
840 			  p_conn->queue_cnts_virt_addr,
841 			  p_conn->queue_cnts_phys_addr);
842 	kfree(p_conn);
843 }
844 
845 int qed_iscsi_alloc(struct qed_hwfn *p_hwfn)
846 {
847 	struct qed_iscsi_info *p_iscsi_info;
848 
849 	p_iscsi_info = kzalloc(sizeof(*p_iscsi_info), GFP_KERNEL);
850 	if (!p_iscsi_info)
851 		return -ENOMEM;
852 
853 	INIT_LIST_HEAD(&p_iscsi_info->free_list);
854 
855 	p_hwfn->p_iscsi_info = p_iscsi_info;
856 	return 0;
857 }
858 
859 void qed_iscsi_setup(struct qed_hwfn *p_hwfn)
860 {
861 	spin_lock_init(&p_hwfn->p_iscsi_info->lock);
862 }
863 
864 void qed_iscsi_free(struct qed_hwfn *p_hwfn)
865 {
866 	struct qed_iscsi_conn *p_conn = NULL;
867 
868 	if (!p_hwfn->p_iscsi_info)
869 		return;
870 
871 	while (!list_empty(&p_hwfn->p_iscsi_info->free_list)) {
872 		p_conn = list_first_entry(&p_hwfn->p_iscsi_info->free_list,
873 					  struct qed_iscsi_conn, list_entry);
874 		if (p_conn) {
875 			list_del(&p_conn->list_entry);
876 			qed_iscsi_free_connection(p_hwfn, p_conn);
877 		}
878 	}
879 
880 	kfree(p_hwfn->p_iscsi_info);
881 	p_hwfn->p_iscsi_info = NULL;
882 }
883 
884 static void _qed_iscsi_get_tstats(struct qed_hwfn *p_hwfn,
885 				  struct qed_ptt *p_ptt,
886 				  struct qed_iscsi_stats *p_stats)
887 {
888 	struct tstorm_iscsi_stats_drv tstats;
889 	u32 tstats_addr;
890 
891 	memset(&tstats, 0, sizeof(tstats));
892 	tstats_addr = BAR0_MAP_REG_TSDM_RAM +
893 		      TSTORM_ISCSI_RX_STATS_OFFSET(p_hwfn->rel_pf_id);
894 	qed_memcpy_from(p_hwfn, p_ptt, &tstats, tstats_addr, sizeof(tstats));
895 
896 	p_stats->iscsi_rx_bytes_cnt =
897 	    HILO_64_REGPAIR(tstats.iscsi_rx_bytes_cnt);
898 	p_stats->iscsi_rx_packet_cnt =
899 	    HILO_64_REGPAIR(tstats.iscsi_rx_packet_cnt);
900 	p_stats->iscsi_rx_new_ooo_isle_events_cnt =
901 	    HILO_64_REGPAIR(tstats.iscsi_rx_new_ooo_isle_events_cnt);
902 	p_stats->iscsi_cmdq_threshold_cnt =
903 	    le32_to_cpu(tstats.iscsi_cmdq_threshold_cnt);
904 	p_stats->iscsi_rq_threshold_cnt =
905 	    le32_to_cpu(tstats.iscsi_rq_threshold_cnt);
906 	p_stats->iscsi_immq_threshold_cnt =
907 	    le32_to_cpu(tstats.iscsi_immq_threshold_cnt);
908 }
909 
910 static void _qed_iscsi_get_mstats(struct qed_hwfn *p_hwfn,
911 				  struct qed_ptt *p_ptt,
912 				  struct qed_iscsi_stats *p_stats)
913 {
914 	struct mstorm_iscsi_stats_drv mstats;
915 	u32 mstats_addr;
916 
917 	memset(&mstats, 0, sizeof(mstats));
918 	mstats_addr = BAR0_MAP_REG_MSDM_RAM +
919 		      MSTORM_ISCSI_RX_STATS_OFFSET(p_hwfn->rel_pf_id);
920 	qed_memcpy_from(p_hwfn, p_ptt, &mstats, mstats_addr, sizeof(mstats));
921 
922 	p_stats->iscsi_rx_dropped_pdus_task_not_valid =
923 	    HILO_64_REGPAIR(mstats.iscsi_rx_dropped_pdus_task_not_valid);
924 }
925 
926 static void _qed_iscsi_get_ustats(struct qed_hwfn *p_hwfn,
927 				  struct qed_ptt *p_ptt,
928 				  struct qed_iscsi_stats *p_stats)
929 {
930 	struct ustorm_iscsi_stats_drv ustats;
931 	u32 ustats_addr;
932 
933 	memset(&ustats, 0, sizeof(ustats));
934 	ustats_addr = BAR0_MAP_REG_USDM_RAM +
935 		      USTORM_ISCSI_RX_STATS_OFFSET(p_hwfn->rel_pf_id);
936 	qed_memcpy_from(p_hwfn, p_ptt, &ustats, ustats_addr, sizeof(ustats));
937 
938 	p_stats->iscsi_rx_data_pdu_cnt =
939 	    HILO_64_REGPAIR(ustats.iscsi_rx_data_pdu_cnt);
940 	p_stats->iscsi_rx_r2t_pdu_cnt =
941 	    HILO_64_REGPAIR(ustats.iscsi_rx_r2t_pdu_cnt);
942 	p_stats->iscsi_rx_total_pdu_cnt =
943 	    HILO_64_REGPAIR(ustats.iscsi_rx_total_pdu_cnt);
944 }
945 
946 static void _qed_iscsi_get_xstats(struct qed_hwfn *p_hwfn,
947 				  struct qed_ptt *p_ptt,
948 				  struct qed_iscsi_stats *p_stats)
949 {
950 	struct xstorm_iscsi_stats_drv xstats;
951 	u32 xstats_addr;
952 
953 	memset(&xstats, 0, sizeof(xstats));
954 	xstats_addr = BAR0_MAP_REG_XSDM_RAM +
955 		      XSTORM_ISCSI_TX_STATS_OFFSET(p_hwfn->rel_pf_id);
956 	qed_memcpy_from(p_hwfn, p_ptt, &xstats, xstats_addr, sizeof(xstats));
957 
958 	p_stats->iscsi_tx_go_to_slow_start_event_cnt =
959 	    HILO_64_REGPAIR(xstats.iscsi_tx_go_to_slow_start_event_cnt);
960 	p_stats->iscsi_tx_fast_retransmit_event_cnt =
961 	    HILO_64_REGPAIR(xstats.iscsi_tx_fast_retransmit_event_cnt);
962 }
963 
964 static void _qed_iscsi_get_ystats(struct qed_hwfn *p_hwfn,
965 				  struct qed_ptt *p_ptt,
966 				  struct qed_iscsi_stats *p_stats)
967 {
968 	struct ystorm_iscsi_stats_drv ystats;
969 	u32 ystats_addr;
970 
971 	memset(&ystats, 0, sizeof(ystats));
972 	ystats_addr = BAR0_MAP_REG_YSDM_RAM +
973 		      YSTORM_ISCSI_TX_STATS_OFFSET(p_hwfn->rel_pf_id);
974 	qed_memcpy_from(p_hwfn, p_ptt, &ystats, ystats_addr, sizeof(ystats));
975 
976 	p_stats->iscsi_tx_data_pdu_cnt =
977 	    HILO_64_REGPAIR(ystats.iscsi_tx_data_pdu_cnt);
978 	p_stats->iscsi_tx_r2t_pdu_cnt =
979 	    HILO_64_REGPAIR(ystats.iscsi_tx_r2t_pdu_cnt);
980 	p_stats->iscsi_tx_total_pdu_cnt =
981 	    HILO_64_REGPAIR(ystats.iscsi_tx_total_pdu_cnt);
982 }
983 
984 static void _qed_iscsi_get_pstats(struct qed_hwfn *p_hwfn,
985 				  struct qed_ptt *p_ptt,
986 				  struct qed_iscsi_stats *p_stats)
987 {
988 	struct pstorm_iscsi_stats_drv pstats;
989 	u32 pstats_addr;
990 
991 	memset(&pstats, 0, sizeof(pstats));
992 	pstats_addr = BAR0_MAP_REG_PSDM_RAM +
993 		      PSTORM_ISCSI_TX_STATS_OFFSET(p_hwfn->rel_pf_id);
994 	qed_memcpy_from(p_hwfn, p_ptt, &pstats, pstats_addr, sizeof(pstats));
995 
996 	p_stats->iscsi_tx_bytes_cnt =
997 	    HILO_64_REGPAIR(pstats.iscsi_tx_bytes_cnt);
998 	p_stats->iscsi_tx_packet_cnt =
999 	    HILO_64_REGPAIR(pstats.iscsi_tx_packet_cnt);
1000 }
1001 
1002 static int qed_iscsi_get_stats(struct qed_hwfn *p_hwfn,
1003 			       struct qed_iscsi_stats *stats)
1004 {
1005 	struct qed_ptt *p_ptt;
1006 
1007 	memset(stats, 0, sizeof(*stats));
1008 
1009 	p_ptt = qed_ptt_acquire(p_hwfn);
1010 	if (!p_ptt) {
1011 		DP_ERR(p_hwfn, "Failed to acquire ptt\n");
1012 		return -EAGAIN;
1013 	}
1014 
1015 	_qed_iscsi_get_tstats(p_hwfn, p_ptt, stats);
1016 	_qed_iscsi_get_mstats(p_hwfn, p_ptt, stats);
1017 	_qed_iscsi_get_ustats(p_hwfn, p_ptt, stats);
1018 
1019 	_qed_iscsi_get_xstats(p_hwfn, p_ptt, stats);
1020 	_qed_iscsi_get_ystats(p_hwfn, p_ptt, stats);
1021 	_qed_iscsi_get_pstats(p_hwfn, p_ptt, stats);
1022 
1023 	qed_ptt_release(p_hwfn, p_ptt);
1024 
1025 	return 0;
1026 }
1027 
1028 struct qed_hash_iscsi_con {
1029 	struct hlist_node node;
1030 	struct qed_iscsi_conn *con;
1031 };
1032 
1033 static int qed_fill_iscsi_dev_info(struct qed_dev *cdev,
1034 				   struct qed_dev_iscsi_info *info)
1035 {
1036 	struct qed_hwfn *hwfn = QED_AFFIN_HWFN(cdev);
1037 
1038 	int rc;
1039 
1040 	memset(info, 0, sizeof(*info));
1041 	rc = qed_fill_dev_info(cdev, &info->common);
1042 
1043 	info->primary_dbq_rq_addr =
1044 	    qed_iscsi_get_primary_bdq_prod(hwfn, BDQ_ID_RQ);
1045 	info->secondary_bdq_rq_addr =
1046 	    qed_iscsi_get_secondary_bdq_prod(hwfn, BDQ_ID_RQ);
1047 
1048 	info->num_cqs = FEAT_NUM(hwfn, QED_ISCSI_CQ);
1049 
1050 	return rc;
1051 }
1052 
1053 static void qed_register_iscsi_ops(struct qed_dev *cdev,
1054 				   struct qed_iscsi_cb_ops *ops, void *cookie)
1055 {
1056 	cdev->protocol_ops.iscsi = ops;
1057 	cdev->ops_cookie = cookie;
1058 }
1059 
1060 static struct qed_hash_iscsi_con *qed_iscsi_get_hash(struct qed_dev *cdev,
1061 						     u32 handle)
1062 {
1063 	struct qed_hash_iscsi_con *hash_con = NULL;
1064 
1065 	if (!(cdev->flags & QED_FLAG_STORAGE_STARTED))
1066 		return NULL;
1067 
1068 	hash_for_each_possible(cdev->connections, hash_con, node, handle) {
1069 		if (hash_con->con->icid == handle)
1070 			break;
1071 	}
1072 
1073 	if (!hash_con || (hash_con->con->icid != handle))
1074 		return NULL;
1075 
1076 	return hash_con;
1077 }
1078 
1079 static int qed_iscsi_stop(struct qed_dev *cdev)
1080 {
1081 	int rc;
1082 
1083 	if (!(cdev->flags & QED_FLAG_STORAGE_STARTED)) {
1084 		DP_NOTICE(cdev, "iscsi already stopped\n");
1085 		return 0;
1086 	}
1087 
1088 	if (!hash_empty(cdev->connections)) {
1089 		DP_NOTICE(cdev,
1090 			  "Can't stop iscsi - not all connections were returned\n");
1091 		return -EINVAL;
1092 	}
1093 
1094 	/* Stop the iscsi */
1095 	rc = qed_sp_iscsi_func_stop(QED_AFFIN_HWFN(cdev), QED_SPQ_MODE_EBLOCK,
1096 				    NULL);
1097 	cdev->flags &= ~QED_FLAG_STORAGE_STARTED;
1098 
1099 	return rc;
1100 }
1101 
1102 static int qed_iscsi_start(struct qed_dev *cdev,
1103 			   struct qed_iscsi_tid *tasks,
1104 			   void *event_context,
1105 			   iscsi_event_cb_t async_event_cb)
1106 {
1107 	int rc;
1108 	struct qed_tid_mem *tid_info;
1109 
1110 	if (cdev->flags & QED_FLAG_STORAGE_STARTED) {
1111 		DP_NOTICE(cdev, "iscsi already started;\n");
1112 		return 0;
1113 	}
1114 
1115 	rc = qed_sp_iscsi_func_start(QED_AFFIN_HWFN(cdev), QED_SPQ_MODE_EBLOCK,
1116 				     NULL, event_context, async_event_cb);
1117 	if (rc) {
1118 		DP_NOTICE(cdev, "Failed to start iscsi\n");
1119 		return rc;
1120 	}
1121 
1122 	cdev->flags |= QED_FLAG_STORAGE_STARTED;
1123 	hash_init(cdev->connections);
1124 
1125 	if (!tasks)
1126 		return 0;
1127 
1128 	tid_info = kzalloc(sizeof(*tid_info), GFP_KERNEL);
1129 
1130 	if (!tid_info) {
1131 		qed_iscsi_stop(cdev);
1132 		return -ENOMEM;
1133 	}
1134 
1135 	rc = qed_cxt_get_tid_mem_info(QED_AFFIN_HWFN(cdev), tid_info);
1136 	if (rc) {
1137 		DP_NOTICE(cdev, "Failed to gather task information\n");
1138 		qed_iscsi_stop(cdev);
1139 		kfree(tid_info);
1140 		return rc;
1141 	}
1142 
1143 	/* Fill task information */
1144 	tasks->size = tid_info->tid_size;
1145 	tasks->num_tids_per_block = tid_info->num_tids_per_block;
1146 	memcpy(tasks->blocks, tid_info->blocks,
1147 	       MAX_TID_BLOCKS_ISCSI * sizeof(u8 *));
1148 
1149 	kfree(tid_info);
1150 
1151 	return 0;
1152 }
1153 
1154 static int qed_iscsi_acquire_conn(struct qed_dev *cdev,
1155 				  u32 *handle,
1156 				  u32 *fw_cid, void __iomem **p_doorbell)
1157 {
1158 	struct qed_hash_iscsi_con *hash_con;
1159 	int rc;
1160 
1161 	/* Allocate a hashed connection */
1162 	hash_con = kzalloc(sizeof(*hash_con), GFP_ATOMIC);
1163 	if (!hash_con)
1164 		return -ENOMEM;
1165 
1166 	/* Acquire the connection */
1167 	rc = qed_iscsi_acquire_connection(QED_AFFIN_HWFN(cdev), NULL,
1168 					  &hash_con->con);
1169 	if (rc) {
1170 		DP_NOTICE(cdev, "Failed to acquire Connection\n");
1171 		kfree(hash_con);
1172 		return rc;
1173 	}
1174 
1175 	/* Added the connection to hash table */
1176 	*handle = hash_con->con->icid;
1177 	*fw_cid = hash_con->con->fw_cid;
1178 	hash_add(cdev->connections, &hash_con->node, *handle);
1179 
1180 	if (p_doorbell)
1181 		*p_doorbell = qed_iscsi_get_db_addr(QED_AFFIN_HWFN(cdev),
1182 						    *handle);
1183 
1184 	return 0;
1185 }
1186 
1187 static int qed_iscsi_release_conn(struct qed_dev *cdev, u32 handle)
1188 {
1189 	struct qed_hash_iscsi_con *hash_con;
1190 
1191 	hash_con = qed_iscsi_get_hash(cdev, handle);
1192 	if (!hash_con) {
1193 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1194 			  handle);
1195 		return -EINVAL;
1196 	}
1197 
1198 	hlist_del(&hash_con->node);
1199 	qed_iscsi_release_connection(QED_AFFIN_HWFN(cdev), hash_con->con);
1200 	kfree(hash_con);
1201 
1202 	return 0;
1203 }
1204 
1205 static int qed_iscsi_offload_conn(struct qed_dev *cdev,
1206 				  u32 handle,
1207 				  struct qed_iscsi_params_offload *conn_info)
1208 {
1209 	struct qed_hash_iscsi_con *hash_con;
1210 	struct qed_iscsi_conn *con;
1211 
1212 	hash_con = qed_iscsi_get_hash(cdev, handle);
1213 	if (!hash_con) {
1214 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1215 			  handle);
1216 		return -EINVAL;
1217 	}
1218 
1219 	/* Update the connection with information from the params */
1220 	con = hash_con->con;
1221 
1222 	ether_addr_copy(con->local_mac, conn_info->src.mac);
1223 	ether_addr_copy(con->remote_mac, conn_info->dst.mac);
1224 	memcpy(con->local_ip, conn_info->src.ip, sizeof(con->local_ip));
1225 	memcpy(con->remote_ip, conn_info->dst.ip, sizeof(con->remote_ip));
1226 	con->local_port = conn_info->src.port;
1227 	con->remote_port = conn_info->dst.port;
1228 
1229 	con->layer_code = conn_info->layer_code;
1230 	con->sq_pbl_addr = conn_info->sq_pbl_addr;
1231 	con->initial_ack = conn_info->initial_ack;
1232 	con->vlan_id = conn_info->vlan_id;
1233 	con->tcp_flags = conn_info->tcp_flags;
1234 	con->ip_version = conn_info->ip_version;
1235 	con->default_cq = conn_info->default_cq;
1236 	con->ka_max_probe_cnt = conn_info->ka_max_probe_cnt;
1237 	con->dup_ack_theshold = conn_info->dup_ack_theshold;
1238 	con->rcv_next = conn_info->rcv_next;
1239 	con->snd_una = conn_info->snd_una;
1240 	con->snd_next = conn_info->snd_next;
1241 	con->snd_max = conn_info->snd_max;
1242 	con->snd_wnd = conn_info->snd_wnd;
1243 	con->rcv_wnd = conn_info->rcv_wnd;
1244 	con->snd_wl1 = conn_info->snd_wl1;
1245 	con->cwnd = conn_info->cwnd;
1246 	con->ss_thresh = conn_info->ss_thresh;
1247 	con->srtt = conn_info->srtt;
1248 	con->rtt_var = conn_info->rtt_var;
1249 	con->ts_recent = conn_info->ts_recent;
1250 	con->ts_recent_age = conn_info->ts_recent_age;
1251 	con->total_rt = conn_info->total_rt;
1252 	con->ka_timeout_delta = conn_info->ka_timeout_delta;
1253 	con->rt_timeout_delta = conn_info->rt_timeout_delta;
1254 	con->dup_ack_cnt = conn_info->dup_ack_cnt;
1255 	con->snd_wnd_probe_cnt = conn_info->snd_wnd_probe_cnt;
1256 	con->ka_probe_cnt = conn_info->ka_probe_cnt;
1257 	con->rt_cnt = conn_info->rt_cnt;
1258 	con->flow_label = conn_info->flow_label;
1259 	con->ka_timeout = conn_info->ka_timeout;
1260 	con->ka_interval = conn_info->ka_interval;
1261 	con->max_rt_time = conn_info->max_rt_time;
1262 	con->initial_rcv_wnd = conn_info->initial_rcv_wnd;
1263 	con->ttl = conn_info->ttl;
1264 	con->tos_or_tc = conn_info->tos_or_tc;
1265 	con->remote_port = conn_info->remote_port;
1266 	con->local_port = conn_info->local_port;
1267 	con->mss = conn_info->mss;
1268 	con->snd_wnd_scale = conn_info->snd_wnd_scale;
1269 	con->rcv_wnd_scale = conn_info->rcv_wnd_scale;
1270 	con->da_timeout_value = conn_info->da_timeout_value;
1271 	con->ack_frequency = conn_info->ack_frequency;
1272 
1273 	/* Set default values on other connection fields */
1274 	con->offl_flags = 0x1;
1275 
1276 	return qed_sp_iscsi_conn_offload(QED_AFFIN_HWFN(cdev), con,
1277 					 QED_SPQ_MODE_EBLOCK, NULL);
1278 }
1279 
1280 static int qed_iscsi_update_conn(struct qed_dev *cdev,
1281 				 u32 handle,
1282 				 struct qed_iscsi_params_update *conn_info)
1283 {
1284 	struct qed_hash_iscsi_con *hash_con;
1285 	struct qed_iscsi_conn *con;
1286 
1287 	hash_con = qed_iscsi_get_hash(cdev, handle);
1288 	if (!hash_con) {
1289 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1290 			  handle);
1291 		return -EINVAL;
1292 	}
1293 
1294 	/* Update the connection with information from the params */
1295 	con = hash_con->con;
1296 	con->update_flag = conn_info->update_flag;
1297 	con->max_seq_size = conn_info->max_seq_size;
1298 	con->max_recv_pdu_length = conn_info->max_recv_pdu_length;
1299 	con->max_send_pdu_length = conn_info->max_send_pdu_length;
1300 	con->first_seq_length = conn_info->first_seq_length;
1301 	con->exp_stat_sn = conn_info->exp_stat_sn;
1302 
1303 	return qed_sp_iscsi_conn_update(QED_AFFIN_HWFN(cdev), con,
1304 					QED_SPQ_MODE_EBLOCK, NULL);
1305 }
1306 
1307 static int qed_iscsi_clear_conn_sq(struct qed_dev *cdev, u32 handle)
1308 {
1309 	struct qed_hash_iscsi_con *hash_con;
1310 
1311 	hash_con = qed_iscsi_get_hash(cdev, handle);
1312 	if (!hash_con) {
1313 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1314 			  handle);
1315 		return -EINVAL;
1316 	}
1317 
1318 	return qed_sp_iscsi_conn_clear_sq(QED_AFFIN_HWFN(cdev), hash_con->con,
1319 					  QED_SPQ_MODE_EBLOCK, NULL);
1320 }
1321 
1322 static int qed_iscsi_destroy_conn(struct qed_dev *cdev,
1323 				  u32 handle, u8 abrt_conn)
1324 {
1325 	struct qed_hash_iscsi_con *hash_con;
1326 
1327 	hash_con = qed_iscsi_get_hash(cdev, handle);
1328 	if (!hash_con) {
1329 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1330 			  handle);
1331 		return -EINVAL;
1332 	}
1333 
1334 	hash_con->con->abortive_dsconnect = abrt_conn;
1335 
1336 	return qed_sp_iscsi_conn_terminate(QED_AFFIN_HWFN(cdev), hash_con->con,
1337 					   QED_SPQ_MODE_EBLOCK, NULL);
1338 }
1339 
1340 static int qed_iscsi_stats(struct qed_dev *cdev, struct qed_iscsi_stats *stats)
1341 {
1342 	return qed_iscsi_get_stats(QED_AFFIN_HWFN(cdev), stats);
1343 }
1344 
1345 static int qed_iscsi_change_mac(struct qed_dev *cdev,
1346 				u32 handle, const u8 *mac)
1347 {
1348 	struct qed_hash_iscsi_con *hash_con;
1349 
1350 	hash_con = qed_iscsi_get_hash(cdev, handle);
1351 	if (!hash_con) {
1352 		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
1353 			  handle);
1354 		return -EINVAL;
1355 	}
1356 
1357 	return qed_sp_iscsi_mac_update(QED_AFFIN_HWFN(cdev), hash_con->con,
1358 				       QED_SPQ_MODE_EBLOCK, NULL);
1359 }
1360 
1361 void qed_get_protocol_stats_iscsi(struct qed_dev *cdev,
1362 				  struct qed_mcp_iscsi_stats *stats)
1363 {
1364 	struct qed_iscsi_stats proto_stats;
1365 
1366 	/* Retrieve FW statistics */
1367 	memset(&proto_stats, 0, sizeof(proto_stats));
1368 	if (qed_iscsi_stats(cdev, &proto_stats)) {
1369 		DP_VERBOSE(cdev, QED_MSG_STORAGE,
1370 			   "Failed to collect ISCSI statistics\n");
1371 		return;
1372 	}
1373 
1374 	/* Translate FW statistics into struct */
1375 	stats->rx_pdus = proto_stats.iscsi_rx_total_pdu_cnt;
1376 	stats->tx_pdus = proto_stats.iscsi_tx_total_pdu_cnt;
1377 	stats->rx_bytes = proto_stats.iscsi_rx_bytes_cnt;
1378 	stats->tx_bytes = proto_stats.iscsi_tx_bytes_cnt;
1379 }
1380 
1381 static const struct qed_iscsi_ops qed_iscsi_ops_pass = {
1382 	.common = &qed_common_ops_pass,
1383 	.ll2 = &qed_ll2_ops_pass,
1384 	.fill_dev_info = &qed_fill_iscsi_dev_info,
1385 	.register_ops = &qed_register_iscsi_ops,
1386 	.start = &qed_iscsi_start,
1387 	.stop = &qed_iscsi_stop,
1388 	.acquire_conn = &qed_iscsi_acquire_conn,
1389 	.release_conn = &qed_iscsi_release_conn,
1390 	.offload_conn = &qed_iscsi_offload_conn,
1391 	.update_conn = &qed_iscsi_update_conn,
1392 	.destroy_conn = &qed_iscsi_destroy_conn,
1393 	.clear_sq = &qed_iscsi_clear_conn_sq,
1394 	.get_stats = &qed_iscsi_stats,
1395 	.change_mac = &qed_iscsi_change_mac,
1396 };
1397 
1398 const struct qed_iscsi_ops *qed_get_iscsi_ops(void)
1399 {
1400 	return &qed_iscsi_ops_pass;
1401 }
1402 EXPORT_SYMBOL(qed_get_iscsi_ops);
1403 
1404 void qed_put_iscsi_ops(void)
1405 {
1406 }
1407 EXPORT_SYMBOL(qed_put_iscsi_ops);
1408