summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/qlogic/qed/qed_nvmetcp.c
blob: f19128c8d9cce20d61d5688ea24792d3c5061d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
/* Copyright 2021 Marvell. All rights reserved. */

#include <linux/types.h>
#include <asm/byteorder.h>
#include <asm/param.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/log2.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/qed/qed_nvmetcp_if.h>
#include "qed.h"
#include "qed_cxt.h"
#include "qed_dev_api.h"
#include "qed_hsi.h"
#include "qed_hw.h"
#include "qed_int.h"
#include "qed_nvmetcp.h"
#include "qed_ll2.h"
#include "qed_mcp.h"
#include "qed_sp.h"
#include "qed_reg_addr.h"
#include "qed_nvmetcp_fw_funcs.h"

static int qed_nvmetcp_async_event(struct qed_hwfn *p_hwfn, u8 fw_event_code,
				   u16 echo, union event_ring_data *data,
				   u8 fw_return_code)
{
	if (p_hwfn->p_nvmetcp_info->event_cb) {
		struct qed_nvmetcp_info *p_nvmetcp = p_hwfn->p_nvmetcp_info;

		return p_nvmetcp->event_cb(p_nvmetcp->event_context,
					 fw_event_code, data);
	} else {
		DP_NOTICE(p_hwfn, "nvmetcp async completion is not set\n");

		return -EINVAL;
	}
}

static int qed_sp_nvmetcp_func_start(struct qed_hwfn *p_hwfn,
				     enum spq_mode comp_mode,
				     struct qed_spq_comp_cb *p_comp_addr,
				     void *event_context,
				     nvmetcp_event_cb_t async_event_cb)
{
	struct nvmetcp_init_ramrod_params *p_ramrod = NULL;
	struct qed_nvmetcp_pf_params *p_params = NULL;
	struct scsi_init_func_queues *p_queue = NULL;
	struct nvmetcp_spe_func_init *p_init = NULL;
	struct qed_sp_init_data init_data = {};
	struct qed_spq_entry *p_ent = NULL;
	int rc = 0;
	u16 val;
	u8 i;

	/* Get SPQ entry */
	init_data.cid = qed_spq_get_cid(p_hwfn);
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;
	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_INIT_FUNC,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	p_ramrod = &p_ent->ramrod.nvmetcp_init;
	p_init = &p_ramrod->nvmetcp_init_spe;
	p_params = &p_hwfn->pf_params.nvmetcp_pf_params;
	p_queue = &p_init->q_params;
	p_init->num_sq_pages_in_ring = p_params->num_sq_pages_in_ring;
	p_init->num_r2tq_pages_in_ring = p_params->num_r2tq_pages_in_ring;
	p_init->num_uhq_pages_in_ring = p_params->num_uhq_pages_in_ring;
	p_init->ll2_rx_queue_id = RESC_START(p_hwfn, QED_LL2_RAM_QUEUE) +
					p_params->ll2_ooo_queue_id;
	SET_FIELD(p_init->flags, NVMETCP_SPE_FUNC_INIT_NVMETCP_MODE, 1);
	p_init->func_params.log_page_size = ilog2(PAGE_SIZE);
	p_init->func_params.num_tasks = cpu_to_le16(p_params->num_tasks);
	p_init->debug_flags = p_params->debug_mode;
	DMA_REGPAIR_LE(p_queue->glbl_q_params_addr,
		       p_params->glbl_q_params_addr);
	p_queue->cq_num_entries = cpu_to_le16(QED_NVMETCP_FW_CQ_SIZE);
	p_queue->num_queues = p_params->num_queues;
	val = RESC_START(p_hwfn, QED_CMDQS_CQS);
	p_queue->queue_relative_offset = cpu_to_le16((u16)val);
	p_queue->cq_sb_pi = p_params->gl_rq_pi;

	for (i = 0; i < p_params->num_queues; i++) {
		val = qed_get_igu_sb_id(p_hwfn, i);
		p_queue->cq_cmdq_sb_num_arr[i] = cpu_to_le16(val);
	}

	SET_FIELD(p_queue->q_validity,
		  SCSI_INIT_FUNC_QUEUES_CMD_VALID, 0);
	p_queue->cmdq_num_entries = 0;
	p_queue->bdq_resource_id = (u8)RESC_START(p_hwfn, QED_BDQ);
	p_ramrod->tcp_init.two_msl_timer = cpu_to_le32(QED_TCP_TWO_MSL_TIMER);
	p_ramrod->tcp_init.tx_sws_timer = cpu_to_le16(QED_TCP_SWS_TIMER);
	p_init->half_way_close_timeout = cpu_to_le16(QED_TCP_HALF_WAY_CLOSE_TIMEOUT);
	p_ramrod->tcp_init.max_fin_rt = QED_TCP_MAX_FIN_RT;
	SET_FIELD(p_ramrod->nvmetcp_init_spe.params,
		  NVMETCP_SPE_FUNC_INIT_MAX_SYN_RT, QED_TCP_MAX_FIN_RT);
	p_hwfn->p_nvmetcp_info->event_context = event_context;
	p_hwfn->p_nvmetcp_info->event_cb = async_event_cb;
	qed_spq_register_async_cb(p_hwfn, PROTOCOLID_TCP_ULP,
				  qed_nvmetcp_async_event);

	return qed_spq_post(p_hwfn, p_ent, NULL);
}

static int qed_sp_nvmetcp_func_stop(struct qed_hwfn *p_hwfn,
				    enum spq_mode comp_mode,
				    struct qed_spq_comp_cb *p_comp_addr)
{
	struct qed_spq_entry *p_ent = NULL;
	struct qed_sp_init_data init_data;
	int rc;

	/* Get SPQ entry */
	memset(&init_data, 0, sizeof(init_data));
	init_data.cid = qed_spq_get_cid(p_hwfn);
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;
	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_DESTROY_FUNC,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	rc = qed_spq_post(p_hwfn, p_ent, NULL);
	qed_spq_unregister_async_cb(p_hwfn, PROTOCOLID_TCP_ULP);

	return rc;
}

static int qed_fill_nvmetcp_dev_info(struct qed_dev *cdev,
				     struct qed_dev_nvmetcp_info *info)
{
	struct qed_hwfn *hwfn = QED_AFFIN_HWFN(cdev);
	int rc;

	memset(info, 0, sizeof(*info));
	rc = qed_fill_dev_info(cdev, &info->common);
	info->port_id = MFW_PORT(hwfn);
	info->num_cqs = FEAT_NUM(hwfn, QED_NVMETCP_CQ);

	return rc;
}

static void qed_register_nvmetcp_ops(struct qed_dev *cdev,
				     struct qed_nvmetcp_cb_ops *ops,
				     void *cookie)
{
	cdev->protocol_ops.nvmetcp = ops;
	cdev->ops_cookie = cookie;
}

static int qed_nvmetcp_stop(struct qed_dev *cdev)
{
	int rc;

	if (!(cdev->flags & QED_FLAG_STORAGE_STARTED)) {
		DP_NOTICE(cdev, "nvmetcp already stopped\n");

		return 0;
	}

	if (!hash_empty(cdev->connections)) {
		DP_NOTICE(cdev,
			  "Can't stop nvmetcp - not all connections were returned\n");

		return -EINVAL;
	}

	/* Stop the nvmetcp */
	rc = qed_sp_nvmetcp_func_stop(QED_AFFIN_HWFN(cdev), QED_SPQ_MODE_EBLOCK,
				      NULL);
	cdev->flags &= ~QED_FLAG_STORAGE_STARTED;

	return rc;
}

static int qed_nvmetcp_start(struct qed_dev *cdev,
			     struct qed_nvmetcp_tid *tasks,
			     void *event_context,
			     nvmetcp_event_cb_t async_event_cb)
{
	struct qed_tid_mem *tid_info;
	int rc;

	if (cdev->flags & QED_FLAG_STORAGE_STARTED) {
		DP_NOTICE(cdev, "nvmetcp already started;\n");

		return 0;
	}

	rc = qed_sp_nvmetcp_func_start(QED_AFFIN_HWFN(cdev),
				       QED_SPQ_MODE_EBLOCK, NULL,
				       event_context, async_event_cb);
	if (rc) {
		DP_NOTICE(cdev, "Failed to start nvmetcp\n");

		return rc;
	}

	cdev->flags |= QED_FLAG_STORAGE_STARTED;
	hash_init(cdev->connections);

	if (!tasks)
		return 0;

	tid_info = kzalloc(sizeof(*tid_info), GFP_KERNEL);
	if (!tid_info) {
		qed_nvmetcp_stop(cdev);

		return -ENOMEM;
	}

	rc = qed_cxt_get_tid_mem_info(QED_AFFIN_HWFN(cdev), tid_info);
	if (rc) {
		DP_NOTICE(cdev, "Failed to gather task information\n");
		qed_nvmetcp_stop(cdev);
		kfree(tid_info);

		return rc;
	}

	/* Fill task information */
	tasks->size = tid_info->tid_size;
	tasks->num_tids_per_block = tid_info->num_tids_per_block;
	memcpy(tasks->blocks, tid_info->blocks,
	       MAX_TID_BLOCKS_NVMETCP * sizeof(u8 *));
	kfree(tid_info);

	return 0;
}

static struct qed_hash_nvmetcp_con *qed_nvmetcp_get_hash(struct qed_dev *cdev,
							 u32 handle)
{
	struct qed_hash_nvmetcp_con *hash_con = NULL;

	if (!(cdev->flags & QED_FLAG_STORAGE_STARTED))
		return NULL;

	hash_for_each_possible(cdev->connections, hash_con, node, handle) {
		if (hash_con->con->icid == handle)
			break;
	}

	if (!hash_con || hash_con->con->icid != handle)
		return NULL;

	return hash_con;
}

static int qed_sp_nvmetcp_conn_offload(struct qed_hwfn *p_hwfn,
				       struct qed_nvmetcp_conn *p_conn,
				       enum spq_mode comp_mode,
				       struct qed_spq_comp_cb *p_comp_addr)
{
	struct nvmetcp_spe_conn_offload *p_ramrod = NULL;
	struct tcp_offload_params_opt2 *p_tcp = NULL;
	struct qed_sp_init_data init_data = { 0 };
	struct qed_spq_entry *p_ent = NULL;
	dma_addr_t r2tq_pbl_addr;
	dma_addr_t xhq_pbl_addr;
	dma_addr_t uhq_pbl_addr;
	u16 physical_q;
	int rc = 0;
	u8 i;

	/* Get SPQ entry */
	init_data.cid = p_conn->icid;
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;
	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_OFFLOAD_CONN,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	p_ramrod = &p_ent->ramrod.nvmetcp_conn_offload;

	/* Transmission PQ is the first of the PF */
	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
	p_conn->physical_q0 = cpu_to_le16(physical_q);
	p_ramrod->nvmetcp.physical_q0 = cpu_to_le16(physical_q);

	/* nvmetcp Pure-ACK PQ */
	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
	p_conn->physical_q1 = cpu_to_le16(physical_q);
	p_ramrod->nvmetcp.physical_q1 = cpu_to_le16(physical_q);
	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
	DMA_REGPAIR_LE(p_ramrod->nvmetcp.sq_pbl_addr, p_conn->sq_pbl_addr);
	r2tq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->r2tq);
	DMA_REGPAIR_LE(p_ramrod->nvmetcp.r2tq_pbl_addr, r2tq_pbl_addr);
	xhq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->xhq);
	DMA_REGPAIR_LE(p_ramrod->nvmetcp.xhq_pbl_addr, xhq_pbl_addr);
	uhq_pbl_addr = qed_chain_get_pbl_phys(&p_conn->uhq);
	DMA_REGPAIR_LE(p_ramrod->nvmetcp.uhq_pbl_addr, uhq_pbl_addr);
	p_ramrod->nvmetcp.flags = p_conn->offl_flags;
	p_ramrod->nvmetcp.default_cq = p_conn->default_cq;
	p_ramrod->nvmetcp.initial_ack = 0;
	DMA_REGPAIR_LE(p_ramrod->nvmetcp.nvmetcp.cccid_itid_table_addr,
		       p_conn->nvmetcp_cccid_itid_table_addr);
	p_ramrod->nvmetcp.nvmetcp.cccid_max_range =
		 cpu_to_le16(p_conn->nvmetcp_cccid_max_range);
	p_tcp = &p_ramrod->tcp;
	qed_set_fw_mac_addr(&p_tcp->remote_mac_addr_hi,
			    &p_tcp->remote_mac_addr_mid,
			    &p_tcp->remote_mac_addr_lo, p_conn->remote_mac);
	qed_set_fw_mac_addr(&p_tcp->local_mac_addr_hi,
			    &p_tcp->local_mac_addr_mid,
			    &p_tcp->local_mac_addr_lo, p_conn->local_mac);
	p_tcp->vlan_id = cpu_to_le16(p_conn->vlan_id);
	p_tcp->flags = cpu_to_le16(p_conn->tcp_flags);
	p_tcp->ip_version = p_conn->ip_version;
	if (p_tcp->ip_version == TCP_IPV6) {
		for (i = 0; i < 4; i++) {
			p_tcp->remote_ip[i] = cpu_to_le32(p_conn->remote_ip[i]);
			p_tcp->local_ip[i] = cpu_to_le32(p_conn->local_ip[i]);
		}
	} else {
		p_tcp->remote_ip[0] = cpu_to_le32(p_conn->remote_ip[0]);
		p_tcp->local_ip[0] = cpu_to_le32(p_conn->local_ip[0]);
	}

	p_tcp->flow_label = cpu_to_le32(p_conn->flow_label);
	p_tcp->ttl = p_conn->ttl;
	p_tcp->tos_or_tc = p_conn->tos_or_tc;
	p_tcp->remote_port = cpu_to_le16(p_conn->remote_port);
	p_tcp->local_port = cpu_to_le16(p_conn->local_port);
	p_tcp->mss = cpu_to_le16(p_conn->mss);
	p_tcp->rcv_wnd_scale = p_conn->rcv_wnd_scale;
	p_tcp->connect_mode = p_conn->connect_mode;
	p_tcp->cwnd = cpu_to_le32(p_conn->cwnd);
	p_tcp->ka_max_probe_cnt = p_conn->ka_max_probe_cnt;
	p_tcp->ka_timeout = cpu_to_le32(p_conn->ka_timeout);
	p_tcp->max_rt_time = cpu_to_le32(p_conn->max_rt_time);
	p_tcp->ka_interval = cpu_to_le32(p_conn->ka_interval);

	return qed_spq_post(p_hwfn, p_ent, NULL);
}

static int qed_sp_nvmetcp_conn_update(struct qed_hwfn *p_hwfn,
				      struct qed_nvmetcp_conn *p_conn,
				      enum spq_mode comp_mode,
				      struct qed_spq_comp_cb *p_comp_addr)
{
	struct nvmetcp_conn_update_ramrod_params *p_ramrod = NULL;
	struct qed_spq_entry *p_ent = NULL;
	struct qed_sp_init_data init_data;
	int rc = -EINVAL;
	u32 dval;

	/* Get SPQ entry */
	memset(&init_data, 0, sizeof(init_data));
	init_data.cid = p_conn->icid;
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;

	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_UPDATE_CONN,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	p_ramrod = &p_ent->ramrod.nvmetcp_conn_update;
	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
	p_ramrod->flags = p_conn->update_flag;
	p_ramrod->max_seq_size = cpu_to_le32(p_conn->max_seq_size);
	dval = p_conn->max_recv_pdu_length;
	p_ramrod->max_recv_pdu_length = cpu_to_le32(dval);
	dval = p_conn->max_send_pdu_length;
	p_ramrod->max_send_pdu_length = cpu_to_le32(dval);
	p_ramrod->first_seq_length = cpu_to_le32(p_conn->first_seq_length);

	return qed_spq_post(p_hwfn, p_ent, NULL);
}

static int qed_sp_nvmetcp_conn_terminate(struct qed_hwfn *p_hwfn,
					 struct qed_nvmetcp_conn *p_conn,
					 enum spq_mode comp_mode,
					 struct qed_spq_comp_cb *p_comp_addr)
{
	struct nvmetcp_spe_conn_termination *p_ramrod = NULL;
	struct qed_spq_entry *p_ent = NULL;
	struct qed_sp_init_data init_data;
	int rc = -EINVAL;

	/* Get SPQ entry */
	memset(&init_data, 0, sizeof(init_data));
	init_data.cid = p_conn->icid;
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;
	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_TERMINATION_CONN,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	p_ramrod = &p_ent->ramrod.nvmetcp_conn_terminate;
	p_ramrod->conn_id = cpu_to_le16(p_conn->conn_id);
	p_ramrod->abortive = p_conn->abortive_dsconnect;

	return qed_spq_post(p_hwfn, p_ent, NULL);
}

static int qed_sp_nvmetcp_conn_clear_sq(struct qed_hwfn *p_hwfn,
					struct qed_nvmetcp_conn *p_conn,
					enum spq_mode comp_mode,
					struct qed_spq_comp_cb *p_comp_addr)
{
	struct qed_spq_entry *p_ent = NULL;
	struct qed_sp_init_data init_data;
	int rc = -EINVAL;

	/* Get SPQ entry */
	memset(&init_data, 0, sizeof(init_data));
	init_data.cid = p_conn->icid;
	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
	init_data.comp_mode = comp_mode;
	init_data.p_comp_data = p_comp_addr;
	rc = qed_sp_init_request(p_hwfn, &p_ent,
				 NVMETCP_RAMROD_CMD_ID_CLEAR_SQ,
				 PROTOCOLID_TCP_ULP, &init_data);
	if (rc)
		return rc;

	return qed_spq_post(p_hwfn, p_ent, NULL);
}

static void __iomem *qed_nvmetcp_get_db_addr(struct qed_hwfn *p_hwfn, u32 cid)
{
	return (u8 __iomem *)p_hwfn->doorbells +
			     qed_db_addr(cid, DQ_DEMS_LEGACY);
}

static int qed_nvmetcp_allocate_connection(struct qed_hwfn *p_hwfn,
					   struct qed_nvmetcp_conn **p_out_conn)
{
	struct qed_chain_init_params params = {
		.mode		= QED_CHAIN_MODE_PBL,
		.intended_use	= QED_CHAIN_USE_TO_CONSUME_PRODUCE,
		.cnt_type	= QED_CHAIN_CNT_TYPE_U16,
	};
	struct qed_nvmetcp_pf_params *p_params = NULL;
	struct qed_nvmetcp_conn *p_conn = NULL;
	int rc = 0;

	/* Try finding a free connection that can be used */
	spin_lock_bh(&p_hwfn->p_nvmetcp_info->lock);
	if (!list_empty(&p_hwfn->p_nvmetcp_info->free_list))
		p_conn = list_first_entry(&p_hwfn->p_nvmetcp_info->free_list,
					  struct qed_nvmetcp_conn, list_entry);
	if (p_conn) {
		list_del(&p_conn->list_entry);
		spin_unlock_bh(&p_hwfn->p_nvmetcp_info->lock);
		*p_out_conn = p_conn;

		return 0;
	}
	spin_unlock_bh(&p_hwfn->p_nvmetcp_info->lock);

	/* Need to allocate a new connection */
	p_params = &p_hwfn->pf_params.nvmetcp_pf_params;
	p_conn = kzalloc(sizeof(*p_conn), GFP_KERNEL);
	if (!p_conn)
		return -ENOMEM;

	params.num_elems = p_params->num_r2tq_pages_in_ring *
			   QED_CHAIN_PAGE_SIZE / sizeof(struct nvmetcp_wqe);
	params.elem_size = sizeof(struct nvmetcp_wqe);
	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->r2tq, &params);
	if (rc)
		goto nomem_r2tq;

	params.num_elems = p_params->num_uhq_pages_in_ring *
			   QED_CHAIN_PAGE_SIZE / sizeof(struct iscsi_uhqe);
	params.elem_size = sizeof(struct iscsi_uhqe);
	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->uhq, &params);
	if (rc)
		goto nomem_uhq;

	params.elem_size = sizeof(struct iscsi_xhqe);
	rc = qed_chain_alloc(p_hwfn->cdev, &p_conn->xhq, &params);
	if (rc)
		goto nomem;

	p_conn->free_on_delete = true;
	*p_out_conn = p_conn;

	return 0;

nomem:
	qed_chain_free(p_hwfn->cdev, &p_conn->uhq);
nomem_uhq:
	qed_chain_free(p_hwfn->cdev, &p_conn->r2tq);
nomem_r2tq:
	kfree(p_conn);

	return -ENOMEM;
}

static int qed_nvmetcp_acquire_connection(struct qed_hwfn *p_hwfn,
					  struct qed_nvmetcp_conn **p_out_conn)
{
	struct qed_nvmetcp_conn *p_conn = NULL;
	int rc = 0;
	u32 icid;

	spin_lock_bh(&p_hwfn->p_nvmetcp_info->lock);
	rc = qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_TCP_ULP, &icid);
	spin_unlock_bh(&p_hwfn->p_nvmetcp_info->lock);

	if (rc)
		return rc;

	rc = qed_nvmetcp_allocate_connection(p_hwfn, &p_conn);
	if (rc) {
		spin_lock_bh(&p_hwfn->p_nvmetcp_info->lock);
		qed_cxt_release_cid(p_hwfn, icid);
		spin_unlock_bh(&p_hwfn->p_nvmetcp_info->lock);

		return rc;
	}

	p_conn->icid = icid;
	p_conn->conn_id = (u16)icid;
	p_conn->fw_cid = (p_hwfn->hw_info.opaque_fid << 16) | icid;
	*p_out_conn = p_conn;

	return rc;
}

static void qed_nvmetcp_release_connection(struct qed_hwfn *p_hwfn,
					   struct qed_nvmetcp_conn *p_conn)
{
	spin_lock_bh(&p_hwfn->p_nvmetcp_info->lock);
	list_add_tail(&p_conn->list_entry, &p_hwfn->p_nvmetcp_info->free_list);
	qed_cxt_release_cid(p_hwfn, p_conn->icid);
	spin_unlock_bh(&p_hwfn->p_nvmetcp_info->lock);
}

static void qed_nvmetcp_free_connection(struct qed_hwfn *p_hwfn,
					struct qed_nvmetcp_conn *p_conn)
{
	qed_chain_free(p_hwfn->cdev, &p_conn->xhq);
	qed_chain_free(p_hwfn->cdev, &p_conn->uhq);
	qed_chain_free(p_hwfn->cdev, &p_conn->r2tq);
	kfree(p_conn);
}

int qed_nvmetcp_alloc(struct qed_hwfn *p_hwfn)
{
	struct qed_nvmetcp_info *p_nvmetcp_info;

	p_nvmetcp_info = kzalloc(sizeof(*p_nvmetcp_info), GFP_KERNEL);
	if (!p_nvmetcp_info)
		return -ENOMEM;

	INIT_LIST_HEAD(&p_nvmetcp_info->free_list);
	p_hwfn->p_nvmetcp_info = p_nvmetcp_info;

	return 0;
}

void qed_nvmetcp_setup(struct qed_hwfn *p_hwfn)
{
	spin_lock_init(&p_hwfn->p_nvmetcp_info->lock);
}

void qed_nvmetcp_free(struct qed_hwfn *p_hwfn)
{
	struct qed_nvmetcp_conn *p_conn = NULL;

	if (!p_hwfn->p_nvmetcp_info)
		return;

	while (!list_empty(&p_hwfn->p_nvmetcp_info->free_list)) {
		p_conn = list_first_entry(&p_hwfn->p_nvmetcp_info->free_list,
					  struct qed_nvmetcp_conn, list_entry);
		if (p_conn) {
			list_del(&p_conn->list_entry);
			qed_nvmetcp_free_connection(p_hwfn, p_conn);
		}
	}

	kfree(p_hwfn->p_nvmetcp_info);
	p_hwfn->p_nvmetcp_info = NULL;
}

static int qed_nvmetcp_acquire_conn(struct qed_dev *cdev,
				    u32 *handle,
				    u32 *fw_cid, void __iomem **p_doorbell)
{
	struct qed_hash_nvmetcp_con *hash_con;
	int rc;

	/* Allocate a hashed connection */
	hash_con = kzalloc(sizeof(*hash_con), GFP_ATOMIC);
	if (!hash_con)
		return -ENOMEM;

	/* Acquire the connection */
	rc = qed_nvmetcp_acquire_connection(QED_AFFIN_HWFN(cdev),
					    &hash_con->con);
	if (rc) {
		DP_NOTICE(cdev, "Failed to acquire Connection\n");
		kfree(hash_con);

		return rc;
	}

	/* Added the connection to hash table */
	*handle = hash_con->con->icid;
	*fw_cid = hash_con->con->fw_cid;
	hash_add(cdev->connections, &hash_con->node, *handle);
	if (p_doorbell)
		*p_doorbell = qed_nvmetcp_get_db_addr(QED_AFFIN_HWFN(cdev),
						      *handle);

	return 0;
}

static int qed_nvmetcp_release_conn(struct qed_dev *cdev, u32 handle)
{
	struct qed_hash_nvmetcp_con *hash_con;

	hash_con = qed_nvmetcp_get_hash(cdev, handle);
	if (!hash_con) {
		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
			  handle);

		return -EINVAL;
	}

	hlist_del(&hash_con->node);
	qed_nvmetcp_release_connection(QED_AFFIN_HWFN(cdev), hash_con->con);
	kfree(hash_con);

	return 0;
}

static int qed_nvmetcp_offload_conn(struct qed_dev *cdev, u32 handle,
				    struct qed_nvmetcp_params_offload *conn_info)
{
	struct qed_hash_nvmetcp_con *hash_con;
	struct qed_nvmetcp_conn *con;

	hash_con = qed_nvmetcp_get_hash(cdev, handle);
	if (!hash_con) {
		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
			  handle);

		return -EINVAL;
	}

	/* Update the connection with information from the params */
	con = hash_con->con;

	/* FW initializations */
	con->layer_code = NVMETCP_SLOW_PATH_LAYER_CODE;
	con->sq_pbl_addr = conn_info->sq_pbl_addr;
	con->nvmetcp_cccid_max_range = conn_info->nvmetcp_cccid_max_range;
	con->nvmetcp_cccid_itid_table_addr = conn_info->nvmetcp_cccid_itid_table_addr;
	con->default_cq = conn_info->default_cq;
	SET_FIELD(con->offl_flags, NVMETCP_CONN_OFFLOAD_PARAMS_TARGET_MODE, 0);
	SET_FIELD(con->offl_flags, NVMETCP_CONN_OFFLOAD_PARAMS_NVMETCP_MODE, 1);
	SET_FIELD(con->offl_flags, NVMETCP_CONN_OFFLOAD_PARAMS_TCP_ON_CHIP_1B, 1);

	/* Networking and TCP stack initializations */
	ether_addr_copy(con->local_mac, conn_info->src.mac);
	ether_addr_copy(con->remote_mac, conn_info->dst.mac);
	memcpy(con->local_ip, conn_info->src.ip, sizeof(con->local_ip));
	memcpy(con->remote_ip, conn_info->dst.ip, sizeof(con->remote_ip));
	con->local_port = conn_info->src.port;
	con->remote_port = conn_info->dst.port;
	con->vlan_id = conn_info->vlan_id;

	if (conn_info->timestamp_en)
		SET_FIELD(con->tcp_flags, TCP_OFFLOAD_PARAMS_OPT2_TS_EN, 1);

	if (conn_info->delayed_ack_en)
		SET_FIELD(con->tcp_flags, TCP_OFFLOAD_PARAMS_OPT2_DA_EN, 1);

	if (conn_info->tcp_keep_alive_en)
		SET_FIELD(con->tcp_flags, TCP_OFFLOAD_PARAMS_OPT2_KA_EN, 1);

	if (conn_info->ecn_en)
		SET_FIELD(con->tcp_flags, TCP_OFFLOAD_PARAMS_OPT2_ECN_EN, 1);

	con->ip_version = conn_info->ip_version;
	con->flow_label = QED_TCP_FLOW_LABEL;
	con->ka_max_probe_cnt = conn_info->ka_max_probe_cnt;
	con->ka_timeout = conn_info->ka_timeout;
	con->ka_interval = conn_info->ka_interval;
	con->max_rt_time = conn_info->max_rt_time;
	con->ttl = conn_info->ttl;
	con->tos_or_tc = conn_info->tos_or_tc;
	con->mss = conn_info->mss;
	con->cwnd = conn_info->cwnd;
	con->rcv_wnd_scale = conn_info->rcv_wnd_scale;
	con->connect_mode = 0;

	return qed_sp_nvmetcp_conn_offload(QED_AFFIN_HWFN(cdev), con,
					 QED_SPQ_MODE_EBLOCK, NULL);
}

static int qed_nvmetcp_update_conn(struct qed_dev *cdev,
				   u32 handle,
				   struct qed_nvmetcp_params_update *conn_info)
{
	struct qed_hash_nvmetcp_con *hash_con;
	struct qed_nvmetcp_conn *con;

	hash_con = qed_nvmetcp_get_hash(cdev, handle);
	if (!hash_con) {
		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
			  handle);

		return -EINVAL;
	}

	/* Update the connection with information from the params */
	con = hash_con->con;
	SET_FIELD(con->update_flag,
		  ISCSI_CONN_UPDATE_RAMROD_PARAMS_INITIAL_R2T, 0);
	SET_FIELD(con->update_flag,
		  ISCSI_CONN_UPDATE_RAMROD_PARAMS_IMMEDIATE_DATA, 1);
	if (conn_info->hdr_digest_en)
		SET_FIELD(con->update_flag, ISCSI_CONN_UPDATE_RAMROD_PARAMS_HD_EN, 1);

	if (conn_info->data_digest_en)
		SET_FIELD(con->update_flag, ISCSI_CONN_UPDATE_RAMROD_PARAMS_DD_EN, 1);

	/* Placeholder - initialize pfv, cpda, hpda */

	con->max_seq_size = conn_info->max_io_size;
	con->max_recv_pdu_length = conn_info->max_recv_pdu_length;
	con->max_send_pdu_length = conn_info->max_send_pdu_length;
	con->first_seq_length = conn_info->max_io_size;

	return qed_sp_nvmetcp_conn_update(QED_AFFIN_HWFN(cdev), con,
					QED_SPQ_MODE_EBLOCK, NULL);
}

static int qed_nvmetcp_clear_conn_sq(struct qed_dev *cdev, u32 handle)
{
	struct qed_hash_nvmetcp_con *hash_con;

	hash_con = qed_nvmetcp_get_hash(cdev, handle);
	if (!hash_con) {
		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
			  handle);

		return -EINVAL;
	}

	return qed_sp_nvmetcp_conn_clear_sq(QED_AFFIN_HWFN(cdev), hash_con->con,
					    QED_SPQ_MODE_EBLOCK, NULL);
}

static int qed_nvmetcp_destroy_conn(struct qed_dev *cdev,
				    u32 handle, u8 abrt_conn)
{
	struct qed_hash_nvmetcp_con *hash_con;

	hash_con = qed_nvmetcp_get_hash(cdev, handle);
	if (!hash_con) {
		DP_NOTICE(cdev, "Failed to find connection for handle %d\n",
			  handle);

		return -EINVAL;
	}

	hash_con->con->abortive_dsconnect = abrt_conn;

	return qed_sp_nvmetcp_conn_terminate(QED_AFFIN_HWFN(cdev), hash_con->con,
					   QED_SPQ_MODE_EBLOCK, NULL);
}

static const struct qed_nvmetcp_ops qed_nvmetcp_ops_pass = {
	.common = &qed_common_ops_pass,
	.ll2 = &qed_ll2_ops_pass,
	.fill_dev_info = &qed_fill_nvmetcp_dev_info,
	.register_ops = &qed_register_nvmetcp_ops,
	.start = &qed_nvmetcp_start,
	.stop = &qed_nvmetcp_stop,
	.acquire_conn = &qed_nvmetcp_acquire_conn,
	.release_conn = &qed_nvmetcp_release_conn,
	.offload_conn = &qed_nvmetcp_offload_conn,
	.update_conn = &qed_nvmetcp_update_conn,
	.destroy_conn = &qed_nvmetcp_destroy_conn,
	.clear_sq = &qed_nvmetcp_clear_conn_sq,
	.add_src_tcp_port_filter = &qed_llh_add_src_tcp_port_filter,
	.remove_src_tcp_port_filter = &qed_llh_remove_src_tcp_port_filter,
	.add_dst_tcp_port_filter = &qed_llh_add_dst_tcp_port_filter,
	.remove_dst_tcp_port_filter = &qed_llh_remove_dst_tcp_port_filter,
	.clear_all_filters = &qed_llh_clear_all_filters,
	.init_read_io = &init_nvmetcp_host_read_task,
	.init_write_io = &init_nvmetcp_host_write_task,
	.init_icreq_exchange = &init_nvmetcp_init_conn_req_task,
	.init_task_cleanup = &init_cleanup_task_nvmetcp
};

const struct qed_nvmetcp_ops *qed_get_nvmetcp_ops(void)
{
	return &qed_nvmetcp_ops_pass;
}
EXPORT_SYMBOL(qed_get_nvmetcp_ops);

void qed_put_nvmetcp_ops(void)
{
}
EXPORT_SYMBOL(qed_put_nvmetcp_ops);