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