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