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