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