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