1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * QLogic iSCSI Offload Driver
4 * Copyright (c) 2016 Cavium Inc.
5 */
6
7#include <linux/module.h>
8#include <linux/pci.h>
9#include <linux/kernel.h>
10#include <linux/if_arp.h>
11#include <scsi/iscsi_if.h>
12#include <linux/inet.h>
13#include <net/arp.h>
14#include <linux/list.h>
15#include <linux/kthread.h>
16#include <linux/mm.h>
17#include <linux/if_vlan.h>
18#include <linux/cpu.h>
19#include <linux/iscsi_boot_sysfs.h>
20
21#include <scsi/scsi_cmnd.h>
22#include <scsi/scsi_device.h>
23#include <scsi/scsi_eh.h>
24#include <scsi/scsi_host.h>
25#include <scsi/scsi.h>
26
27#include "qedi.h"
28#include "qedi_gbl.h"
29#include "qedi_iscsi.h"
30
31static uint qedi_qed_debug;
32module_param(qedi_qed_debug, uint, 0644);
33MODULE_PARM_DESC(qedi_qed_debug, " QED debug level 0 (default)");
34
35static uint qedi_fw_debug;
36module_param(qedi_fw_debug, uint, 0644);
37MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
38
39uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
40module_param(qedi_dbg_log, uint, 0644);
41MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
42
43uint qedi_io_tracing;
44module_param(qedi_io_tracing, uint, 0644);
45MODULE_PARM_DESC(qedi_io_tracing,
46		 " Enable logging of SCSI requests/completions into trace buffer. (default off).");
47
48static uint qedi_ll2_buf_size = 0x400;
49module_param(qedi_ll2_buf_size, uint, 0644);
50MODULE_PARM_DESC(qedi_ll2_buf_size,
51		 "parameter to set ping packet size, default - 0x400, Jumbo packets - 0x2400.");
52
53static uint qedi_flags_override;
54module_param(qedi_flags_override, uint, 0644);
55MODULE_PARM_DESC(qedi_flags_override, "Disable/Enable MFW error flags bits action.");
56
57const struct qed_iscsi_ops *qedi_ops;
58static struct scsi_transport_template *qedi_scsi_transport;
59static struct pci_driver qedi_pci_driver;
60static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
61static LIST_HEAD(qedi_udev_list);
62/* Static function declaration */
63static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
64static void qedi_free_global_queues(struct qedi_ctx *qedi);
65static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
66static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
67static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
68static struct nvm_iscsi_block *qedi_get_nvram_block(struct qedi_ctx *qedi);
69static void qedi_recovery_handler(struct work_struct *work);
70static void qedi_schedule_hw_err_handler(void *dev,
71					 enum qed_hw_err_type err_type);
72static int qedi_suspend(struct pci_dev *pdev, pm_message_t state);
73
74static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
75{
76	struct qedi_ctx *qedi;
77	struct qedi_endpoint *qedi_ep;
78	struct iscsi_eqe_data *data;
79	int rval = 0;
80
81	if (!context || !fw_handle) {
82		QEDI_ERR(NULL, "Recv event with ctx NULL\n");
83		return -EINVAL;
84	}
85
86	qedi = (struct qedi_ctx *)context;
87	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
88		  "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
89
90	data = (struct iscsi_eqe_data *)fw_handle;
91	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
92		  "icid=0x%x conn_id=0x%x err-code=0x%x error-pdu-opcode-reserved=0x%x\n",
93		   data->icid, data->conn_id, data->error_code,
94		   data->error_pdu_opcode_reserved);
95
96	qedi_ep = qedi->ep_tbl[data->icid];
97
98	if (!qedi_ep) {
99		QEDI_WARN(&qedi->dbg_ctx,
100			  "Cannot process event, ep already disconnected, cid=0x%x\n",
101			   data->icid);
102		WARN_ON(1);
103		return -ENODEV;
104	}
105
106	switch (fw_event_code) {
107	case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
108		if (qedi_ep->state == EP_STATE_OFLDCONN_START)
109			qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
110
111		wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
112		break;
113	case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
114		qedi_ep->state = EP_STATE_DISCONN_COMPL;
115		wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
116		break;
117	case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
118		qedi_process_iscsi_error(qedi_ep, data);
119		break;
120	case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
121	case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
122	case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
123	case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
124	case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
125	case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
126	case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
127		qedi_process_tcp_error(qedi_ep, data);
128		break;
129	default:
130		QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
131			 fw_event_code);
132	}
133
134	return rval;
135}
136
137static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
138{
139	struct qedi_uio_dev *udev = uinfo->priv;
140	struct qedi_ctx *qedi = udev->qedi;
141
142	if (!capable(CAP_NET_ADMIN))
143		return -EPERM;
144
145	if (udev->uio_dev != -1)
146		return -EBUSY;
147
148	rtnl_lock();
149	udev->uio_dev = iminor(inode);
150	qedi_reset_uio_rings(udev);
151	set_bit(UIO_DEV_OPENED, &qedi->flags);
152	rtnl_unlock();
153
154	return 0;
155}
156
157static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
158{
159	struct qedi_uio_dev *udev = uinfo->priv;
160	struct qedi_ctx *qedi = udev->qedi;
161
162	udev->uio_dev = -1;
163	clear_bit(UIO_DEV_OPENED, &qedi->flags);
164	qedi_ll2_free_skbs(qedi);
165	return 0;
166}
167
168static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
169{
170	if (udev->uctrl) {
171		free_page((unsigned long)udev->uctrl);
172		udev->uctrl = NULL;
173	}
174
175	if (udev->ll2_ring) {
176		free_page((unsigned long)udev->ll2_ring);
177		udev->ll2_ring = NULL;
178	}
179
180	if (udev->ll2_buf) {
181		free_pages((unsigned long)udev->ll2_buf, 2);
182		udev->ll2_buf = NULL;
183	}
184}
185
186static void __qedi_free_uio(struct qedi_uio_dev *udev)
187{
188	uio_unregister_device(&udev->qedi_uinfo);
189
190	__qedi_free_uio_rings(udev);
191
192	pci_dev_put(udev->pdev);
193	kfree(udev);
194}
195
196static void qedi_free_uio(struct qedi_uio_dev *udev)
197{
198	if (!udev)
199		return;
200
201	list_del_init(&udev->list);
202	__qedi_free_uio(udev);
203}
204
205static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
206{
207	struct qedi_ctx *qedi = NULL;
208	struct qedi_uio_ctrl *uctrl = NULL;
209
210	qedi = udev->qedi;
211	uctrl = udev->uctrl;
212
213	spin_lock_bh(&qedi->ll2_lock);
214	uctrl->host_rx_cons = 0;
215	uctrl->hw_rx_prod = 0;
216	uctrl->hw_rx_bd_prod = 0;
217	uctrl->host_rx_bd_cons = 0;
218
219	memset(udev->ll2_ring, 0, udev->ll2_ring_size);
220	memset(udev->ll2_buf, 0, udev->ll2_buf_size);
221	spin_unlock_bh(&qedi->ll2_lock);
222}
223
224static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
225{
226	int rc = 0;
227
228	if (udev->ll2_ring || udev->ll2_buf)
229		return rc;
230
231	/* Memory for control area.  */
232	udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL);
233	if (!udev->uctrl)
234		return -ENOMEM;
235
236	/* Allocating memory for LL2 ring  */
237	udev->ll2_ring_size = QEDI_PAGE_SIZE;
238	udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
239	if (!udev->ll2_ring) {
240		rc = -ENOMEM;
241		goto exit_alloc_ring;
242	}
243
244	/* Allocating memory for Tx/Rx pkt buffer */
245	udev->ll2_buf_size = TX_RX_RING * qedi_ll2_buf_size;
246	udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
247	udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
248						 __GFP_ZERO, 2);
249	if (!udev->ll2_buf) {
250		rc = -ENOMEM;
251		goto exit_alloc_buf;
252	}
253	return rc;
254
255exit_alloc_buf:
256	free_page((unsigned long)udev->ll2_ring);
257	udev->ll2_ring = NULL;
258exit_alloc_ring:
259	return rc;
260}
261
262static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
263{
264	struct qedi_uio_dev *udev = NULL;
265	int rc = 0;
266
267	list_for_each_entry(udev, &qedi_udev_list, list) {
268		if (udev->pdev == qedi->pdev) {
269			udev->qedi = qedi;
270			if (__qedi_alloc_uio_rings(udev)) {
271				udev->qedi = NULL;
272				return -ENOMEM;
273			}
274			qedi->udev = udev;
275			return 0;
276		}
277	}
278
279	udev = kzalloc(sizeof(*udev), GFP_KERNEL);
280	if (!udev) {
281		rc = -ENOMEM;
282		goto err_udev;
283	}
284
285	udev->uio_dev = -1;
286
287	udev->qedi = qedi;
288	udev->pdev = qedi->pdev;
289
290	rc = __qedi_alloc_uio_rings(udev);
291	if (rc)
292		goto err_uctrl;
293
294	list_add(&udev->list, &qedi_udev_list);
295
296	pci_dev_get(udev->pdev);
297	qedi->udev = udev;
298
299	udev->tx_pkt = udev->ll2_buf;
300	udev->rx_pkt = udev->ll2_buf + qedi_ll2_buf_size;
301	return 0;
302
303 err_uctrl:
304	kfree(udev);
305 err_udev:
306	return -ENOMEM;
307}
308
309static int qedi_init_uio(struct qedi_ctx *qedi)
310{
311	struct qedi_uio_dev *udev = qedi->udev;
312	struct uio_info *uinfo;
313	int ret = 0;
314
315	if (!udev)
316		return -ENOMEM;
317
318	uinfo = &udev->qedi_uinfo;
319
320	uinfo->mem[0].addr = (unsigned long)udev->uctrl;
321	uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
322	uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
323
324	uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
325	uinfo->mem[1].size = udev->ll2_ring_size;
326	uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
327
328	uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
329	uinfo->mem[2].size = udev->ll2_buf_size;
330	uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
331
332	uinfo->name = "qedi_uio";
333	uinfo->version = QEDI_MODULE_VERSION;
334	uinfo->irq = UIO_IRQ_CUSTOM;
335
336	uinfo->open = qedi_uio_open;
337	uinfo->release = qedi_uio_close;
338
339	if (udev->uio_dev == -1) {
340		if (!uinfo->priv) {
341			uinfo->priv = udev;
342
343			ret = uio_register_device(&udev->pdev->dev, uinfo);
344			if (ret) {
345				QEDI_ERR(&qedi->dbg_ctx,
346					 "UIO registration failed\n");
347			}
348		}
349	}
350
351	return ret;
352}
353
354static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
355				  struct qed_sb_info *sb_info, u16 sb_id)
356{
357	struct status_block_e4 *sb_virt;
358	dma_addr_t sb_phys;
359	int ret;
360
361	sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
362				     sizeof(struct status_block_e4), &sb_phys,
363				     GFP_KERNEL);
364	if (!sb_virt) {
365		QEDI_ERR(&qedi->dbg_ctx,
366			 "Status block allocation failed for id = %d.\n",
367			  sb_id);
368		return -ENOMEM;
369	}
370
371	ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
372				       sb_id, QED_SB_TYPE_STORAGE);
373	if (ret) {
374		QEDI_ERR(&qedi->dbg_ctx,
375			 "Status block initialization failed for id = %d.\n",
376			  sb_id);
377		return ret;
378	}
379
380	return 0;
381}
382
383static void qedi_free_sb(struct qedi_ctx *qedi)
384{
385	struct qed_sb_info *sb_info;
386	int id;
387
388	for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
389		sb_info = &qedi->sb_array[id];
390		if (sb_info->sb_virt)
391			dma_free_coherent(&qedi->pdev->dev,
392					  sizeof(*sb_info->sb_virt),
393					  (void *)sb_info->sb_virt,
394					  sb_info->sb_phys);
395	}
396}
397
398static void qedi_free_fp(struct qedi_ctx *qedi)
399{
400	kfree(qedi->fp_array);
401	kfree(qedi->sb_array);
402}
403
404static void qedi_destroy_fp(struct qedi_ctx *qedi)
405{
406	qedi_free_sb(qedi);
407	qedi_free_fp(qedi);
408}
409
410static int qedi_alloc_fp(struct qedi_ctx *qedi)
411{
412	int ret = 0;
413
414	qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
415				 sizeof(struct qedi_fastpath), GFP_KERNEL);
416	if (!qedi->fp_array) {
417		QEDI_ERR(&qedi->dbg_ctx,
418			 "fastpath fp array allocation failed.\n");
419		return -ENOMEM;
420	}
421
422	qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
423				 sizeof(struct qed_sb_info), GFP_KERNEL);
424	if (!qedi->sb_array) {
425		QEDI_ERR(&qedi->dbg_ctx,
426			 "fastpath sb array allocation failed.\n");
427		ret = -ENOMEM;
428		goto free_fp;
429	}
430
431	return ret;
432
433free_fp:
434	qedi_free_fp(qedi);
435	return ret;
436}
437
438static void qedi_int_fp(struct qedi_ctx *qedi)
439{
440	struct qedi_fastpath *fp;
441	int id;
442
443	memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
444	       sizeof(*qedi->fp_array));
445	memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
446	       sizeof(*qedi->sb_array));
447
448	for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
449		fp = &qedi->fp_array[id];
450		fp->sb_info = &qedi->sb_array[id];
451		fp->sb_id = id;
452		fp->qedi = qedi;
453		snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
454			 "qedi", id);
455
456		/* fp_array[i] ---- irq cookie
457		 * So init data which is needed in int ctx
458		 */
459	}
460}
461
462static int qedi_prepare_fp(struct qedi_ctx *qedi)
463{
464	struct qedi_fastpath *fp;
465	int id, ret = 0;
466
467	ret = qedi_alloc_fp(qedi);
468	if (ret)
469		goto err;
470
471	qedi_int_fp(qedi);
472
473	for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
474		fp = &qedi->fp_array[id];
475		ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
476		if (ret) {
477			QEDI_ERR(&qedi->dbg_ctx,
478				 "SB allocation and initialization failed.\n");
479			ret = -EIO;
480			goto err_init;
481		}
482	}
483
484	return 0;
485
486err_init:
487	qedi_free_sb(qedi);
488	qedi_free_fp(qedi);
489err:
490	return ret;
491}
492
493static int qedi_setup_cid_que(struct qedi_ctx *qedi)
494{
495	int i;
496
497	qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
498						   sizeof(u32), GFP_KERNEL);
499	if (!qedi->cid_que.cid_que_base)
500		return -ENOMEM;
501
502	qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
503						   sizeof(struct qedi_conn *),
504						   GFP_KERNEL);
505	if (!qedi->cid_que.conn_cid_tbl) {
506		kfree(qedi->cid_que.cid_que_base);
507		qedi->cid_que.cid_que_base = NULL;
508		return -ENOMEM;
509	}
510
511	qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
512	qedi->cid_que.cid_q_prod_idx = 0;
513	qedi->cid_que.cid_q_cons_idx = 0;
514	qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
515	qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
516
517	for (i = 0; i < qedi->max_active_conns; i++) {
518		qedi->cid_que.cid_que[i] = i;
519		qedi->cid_que.conn_cid_tbl[i] = NULL;
520	}
521
522	return 0;
523}
524
525static void qedi_release_cid_que(struct qedi_ctx *qedi)
526{
527	kfree(qedi->cid_que.cid_que_base);
528	qedi->cid_que.cid_que_base = NULL;
529
530	kfree(qedi->cid_que.conn_cid_tbl);
531	qedi->cid_que.conn_cid_tbl = NULL;
532}
533
534static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
535			    u16 start_id, u16 next)
536{
537	id_tbl->start = start_id;
538	id_tbl->max = size;
539	id_tbl->next = next;
540	spin_lock_init(&id_tbl->lock);
541	id_tbl->table = kcalloc(BITS_TO_LONGS(size), sizeof(long), GFP_KERNEL);
542	if (!id_tbl->table)
543		return -ENOMEM;
544
545	return 0;
546}
547
548static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
549{
550	kfree(id_tbl->table);
551	id_tbl->table = NULL;
552}
553
554int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
555{
556	int ret = -1;
557
558	id -= id_tbl->start;
559	if (id >= id_tbl->max)
560		return ret;
561
562	spin_lock(&id_tbl->lock);
563	if (!test_bit(id, id_tbl->table)) {
564		set_bit(id, id_tbl->table);
565		ret = 0;
566	}
567	spin_unlock(&id_tbl->lock);
568	return ret;
569}
570
571u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
572{
573	u16 id;
574
575	spin_lock(&id_tbl->lock);
576	id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
577	if (id >= id_tbl->max) {
578		id = QEDI_LOCAL_PORT_INVALID;
579		if (id_tbl->next != 0) {
580			id = find_first_zero_bit(id_tbl->table, id_tbl->next);
581			if (id >= id_tbl->next)
582				id = QEDI_LOCAL_PORT_INVALID;
583		}
584	}
585
586	if (id < id_tbl->max) {
587		set_bit(id, id_tbl->table);
588		id_tbl->next = (id + 1) & (id_tbl->max - 1);
589		id += id_tbl->start;
590	}
591
592	spin_unlock(&id_tbl->lock);
593
594	return id;
595}
596
597void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
598{
599	if (id == QEDI_LOCAL_PORT_INVALID)
600		return;
601
602	id -= id_tbl->start;
603	if (id >= id_tbl->max)
604		return;
605
606	clear_bit(id, id_tbl->table);
607}
608
609static void qedi_cm_free_mem(struct qedi_ctx *qedi)
610{
611	kfree(qedi->ep_tbl);
612	qedi->ep_tbl = NULL;
613	qedi_free_id_tbl(&qedi->lcl_port_tbl);
614}
615
616static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
617{
618	u16 port_id;
619
620	qedi->ep_tbl = kzalloc((qedi->max_active_conns *
621				sizeof(struct qedi_endpoint *)), GFP_KERNEL);
622	if (!qedi->ep_tbl)
623		return -ENOMEM;
624	port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
625	if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
626			     QEDI_LOCAL_PORT_MIN, port_id)) {
627		qedi_cm_free_mem(qedi);
628		return -ENOMEM;
629	}
630
631	return 0;
632}
633
634static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
635{
636	struct Scsi_Host *shost;
637	struct qedi_ctx *qedi = NULL;
638
639	shost = iscsi_host_alloc(&qedi_host_template,
640				 sizeof(struct qedi_ctx), 0);
641	if (!shost) {
642		QEDI_ERR(NULL, "Could not allocate shost\n");
643		goto exit_setup_shost;
644	}
645
646	shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA - 1;
647	shost->max_channel = 0;
648	shost->max_lun = ~0;
649	shost->max_cmd_len = 16;
650	shost->transportt = qedi_scsi_transport;
651
652	qedi = iscsi_host_priv(shost);
653	memset(qedi, 0, sizeof(*qedi));
654	qedi->shost = shost;
655	qedi->dbg_ctx.host_no = shost->host_no;
656	qedi->pdev = pdev;
657	qedi->dbg_ctx.pdev = pdev;
658	qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
659	qedi->max_sqes = QEDI_SQ_SIZE;
660
661	shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
662
663	pci_set_drvdata(pdev, qedi);
664
665exit_setup_shost:
666	return qedi;
667}
668
669static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
670{
671	struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
672	struct skb_work_list *work;
673	struct ethhdr *eh;
674
675	if (!qedi) {
676		QEDI_ERR(NULL, "qedi is NULL\n");
677		return -1;
678	}
679
680	if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
681		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
682			  "UIO DEV is not opened\n");
683		kfree_skb(skb);
684		return 0;
685	}
686
687	eh = (struct ethhdr *)skb->data;
688	/* Undo VLAN encapsulation */
689	if (eh->h_proto == htons(ETH_P_8021Q)) {
690		memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
691		eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
692		skb_reset_mac_header(skb);
693	}
694
695	/* Filter out non FIP/FCoE frames here to free them faster */
696	if (eh->h_proto != htons(ETH_P_ARP) &&
697	    eh->h_proto != htons(ETH_P_IP) &&
698	    eh->h_proto != htons(ETH_P_IPV6)) {
699		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
700			  "Dropping frame ethertype [0x%x] len [0x%x].\n",
701			  eh->h_proto, skb->len);
702		kfree_skb(skb);
703		return 0;
704	}
705
706	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
707		  "Allowed frame ethertype [0x%x] len [0x%x].\n",
708		  eh->h_proto, skb->len);
709
710	work = kzalloc(sizeof(*work), GFP_ATOMIC);
711	if (!work) {
712		QEDI_WARN(&qedi->dbg_ctx,
713			  "Could not allocate work so dropping frame.\n");
714		kfree_skb(skb);
715		return 0;
716	}
717
718	INIT_LIST_HEAD(&work->list);
719	work->skb = skb;
720
721	if (skb_vlan_tag_present(skb))
722		work->vlan_id = skb_vlan_tag_get(skb);
723
724	if (work->vlan_id)
725		__vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
726
727	spin_lock_bh(&qedi->ll2_lock);
728	list_add_tail(&work->list, &qedi->ll2_skb_list);
729	spin_unlock_bh(&qedi->ll2_lock);
730
731	wake_up_process(qedi->ll2_recv_thread);
732
733	return 0;
734}
735
736/* map this skb to iscsiuio mmaped region */
737static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
738				u16 vlan_id)
739{
740	struct qedi_uio_dev *udev = NULL;
741	struct qedi_uio_ctrl *uctrl = NULL;
742	struct qedi_rx_bd rxbd;
743	struct qedi_rx_bd *p_rxbd;
744	u32 rx_bd_prod;
745	void *pkt;
746	int len = 0;
747	u32 prod;
748
749	if (!qedi) {
750		QEDI_ERR(NULL, "qedi is NULL\n");
751		return -1;
752	}
753
754	udev = qedi->udev;
755	uctrl = udev->uctrl;
756
757	++uctrl->hw_rx_prod_cnt;
758	prod = (uctrl->hw_rx_prod + 1) % RX_RING;
759
760	pkt = udev->rx_pkt + (prod * qedi_ll2_buf_size);
761	len = min_t(u32, skb->len, (u32)qedi_ll2_buf_size);
762	memcpy(pkt, skb->data, len);
763
764	memset(&rxbd, 0, sizeof(rxbd));
765	rxbd.rx_pkt_index = prod;
766	rxbd.rx_pkt_len = len;
767	rxbd.vlan_id = vlan_id;
768
769	uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
770	rx_bd_prod = uctrl->hw_rx_bd_prod;
771	p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
772	p_rxbd += rx_bd_prod;
773
774	memcpy(p_rxbd, &rxbd, sizeof(rxbd));
775
776	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
777		  "hw_rx_prod [%d] prod [%d] hw_rx_bd_prod [%d] rx_pkt_idx [%d] rx_len [%d].\n",
778		  uctrl->hw_rx_prod, prod, uctrl->hw_rx_bd_prod,
779		  rxbd.rx_pkt_index, rxbd.rx_pkt_len);
780	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
781		  "host_rx_cons [%d] hw_rx_bd_cons [%d].\n",
782		  uctrl->host_rx_cons, uctrl->host_rx_bd_cons);
783
784	uctrl->hw_rx_prod = prod;
785
786	/* notify the iscsiuio about new packet */
787	uio_event_notify(&udev->qedi_uinfo);
788
789	return 0;
790}
791
792static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
793{
794	struct skb_work_list *work, *work_tmp;
795
796	spin_lock_bh(&qedi->ll2_lock);
797	list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
798		list_del(&work->list);
799		kfree_skb(work->skb);
800		kfree(work);
801	}
802	spin_unlock_bh(&qedi->ll2_lock);
803}
804
805static int qedi_ll2_recv_thread(void *arg)
806{
807	struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
808	struct skb_work_list *work, *work_tmp;
809
810	set_user_nice(current, -20);
811
812	while (!kthread_should_stop()) {
813		spin_lock_bh(&qedi->ll2_lock);
814		list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
815					 list) {
816			list_del(&work->list);
817			qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
818			kfree_skb(work->skb);
819			kfree(work);
820		}
821		set_current_state(TASK_INTERRUPTIBLE);
822		spin_unlock_bh(&qedi->ll2_lock);
823		schedule();
824	}
825
826	__set_current_state(TASK_RUNNING);
827	return 0;
828}
829
830static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
831{
832	u8 num_sq_pages;
833	u32 log_page_size;
834	int rval = 0;
835
836
837	num_sq_pages = (MAX_OUTSTANDING_TASKS_PER_CON * 8) / QEDI_PAGE_SIZE;
838
839	qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
840
841	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
842		  "Number of CQ count is %d\n", qedi->num_queues);
843
844	memset(&qedi->pf_params.iscsi_pf_params, 0,
845	       sizeof(qedi->pf_params.iscsi_pf_params));
846
847	qedi->p_cpuq = dma_alloc_coherent(&qedi->pdev->dev,
848			qedi->num_queues * sizeof(struct qedi_glbl_q_params),
849			&qedi->hw_p_cpuq, GFP_KERNEL);
850	if (!qedi->p_cpuq) {
851		QEDI_ERR(&qedi->dbg_ctx, "dma_alloc_coherent fail\n");
852		rval = -1;
853		goto err_alloc_mem;
854	}
855
856	rval = qedi_alloc_global_queues(qedi);
857	if (rval) {
858		QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
859		rval = -1;
860		goto err_alloc_mem;
861	}
862
863	qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
864	qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
865	qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
866	qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
867	qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
868	qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
869	qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
870	qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
871	qedi->pf_params.iscsi_pf_params.two_msl_timer = 4000;
872	qedi->pf_params.iscsi_pf_params.max_fin_rt = 2;
873
874	for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
875		if ((1 << log_page_size) == QEDI_PAGE_SIZE)
876			break;
877	}
878	qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
879
880	qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
881							   (u64)qedi->hw_p_cpuq;
882
883	/* RQ BDQ initializations.
884	 * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
885	 * rqe_log_size: 8 for 256B RQE
886	 */
887	qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
888	/* BDQ address and size */
889	qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
890							qedi->bdq_pbl_list_dma;
891	qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
892						qedi->bdq_pbl_list_num_entries;
893	qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
894
895	/* cq_num_entries: num_tasks + rq_num_entries */
896	qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
897
898	qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
899	qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
900
901err_alloc_mem:
902	return rval;
903}
904
905/* Free DMA coherent memory for array of queue pointers we pass to qed */
906static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
907{
908	size_t size = 0;
909
910	if (qedi->p_cpuq) {
911		size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
912		dma_free_coherent(&qedi->pdev->dev, size, qedi->p_cpuq,
913				    qedi->hw_p_cpuq);
914	}
915
916	qedi_free_global_queues(qedi);
917
918	kfree(qedi->global_queues);
919}
920
921static void qedi_get_boot_tgt_info(struct nvm_iscsi_block *block,
922				   struct qedi_boot_target *tgt, u8 index)
923{
924	u32 ipv6_en;
925
926	ipv6_en = !!(block->generic.ctrl_flags &
927		     NVM_ISCSI_CFG_GEN_IPV6_ENABLED);
928
929	snprintf(tgt->iscsi_name, sizeof(tgt->iscsi_name), "%s",
930		 block->target[index].target_name.byte);
931
932	tgt->ipv6_en = ipv6_en;
933
934	if (ipv6_en)
935		snprintf(tgt->ip_addr, IPV6_LEN, "%pI6\n",
936			 block->target[index].ipv6_addr.byte);
937	else
938		snprintf(tgt->ip_addr, IPV4_LEN, "%pI4\n",
939			 block->target[index].ipv4_addr.byte);
940}
941
942static int qedi_find_boot_info(struct qedi_ctx *qedi,
943			       struct qed_mfw_tlv_iscsi *iscsi,
944			       struct nvm_iscsi_block *block)
945{
946	struct qedi_boot_target *pri_tgt = NULL, *sec_tgt = NULL;
947	u32 pri_ctrl_flags = 0, sec_ctrl_flags = 0, found = 0;
948	struct iscsi_cls_session *cls_sess;
949	struct iscsi_cls_conn *cls_conn;
950	struct qedi_conn *qedi_conn;
951	struct iscsi_session *sess;
952	struct iscsi_conn *conn;
953	char ep_ip_addr[64];
954	int i, ret = 0;
955
956	pri_ctrl_flags = !!(block->target[0].ctrl_flags &
957					NVM_ISCSI_CFG_TARGET_ENABLED);
958	if (pri_ctrl_flags) {
959		pri_tgt = kzalloc(sizeof(*pri_tgt), GFP_KERNEL);
960		if (!pri_tgt)
961			return -1;
962		qedi_get_boot_tgt_info(block, pri_tgt, 0);
963	}
964
965	sec_ctrl_flags = !!(block->target[1].ctrl_flags &
966					NVM_ISCSI_CFG_TARGET_ENABLED);
967	if (sec_ctrl_flags) {
968		sec_tgt = kzalloc(sizeof(*sec_tgt), GFP_KERNEL);
969		if (!sec_tgt) {
970			ret = -1;
971			goto free_tgt;
972		}
973		qedi_get_boot_tgt_info(block, sec_tgt, 1);
974	}
975
976	for (i = 0; i < qedi->max_active_conns; i++) {
977		qedi_conn = qedi_get_conn_from_id(qedi, i);
978		if (!qedi_conn)
979			continue;
980
981		if (qedi_conn->ep->ip_type == TCP_IPV4)
982			snprintf(ep_ip_addr, IPV4_LEN, "%pI4\n",
983				 qedi_conn->ep->dst_addr);
984		else
985			snprintf(ep_ip_addr, IPV6_LEN, "%pI6\n",
986				 qedi_conn->ep->dst_addr);
987
988		cls_conn = qedi_conn->cls_conn;
989		conn = cls_conn->dd_data;
990		cls_sess = iscsi_conn_to_session(cls_conn);
991		sess = cls_sess->dd_data;
992
993		if (!iscsi_is_session_online(cls_sess))
994			continue;
995
996		if (!sess->targetname)
997			continue;
998
999		if (pri_ctrl_flags) {
1000			if (!strcmp(pri_tgt->iscsi_name, sess->targetname) &&
1001			    !strcmp(pri_tgt->ip_addr, ep_ip_addr)) {
1002				found = 1;
1003				break;
1004			}
1005		}
1006
1007		if (sec_ctrl_flags) {
1008			if (!strcmp(sec_tgt->iscsi_name, sess->targetname) &&
1009			    !strcmp(sec_tgt->ip_addr, ep_ip_addr)) {
1010				found = 1;
1011				break;
1012			}
1013		}
1014	}
1015
1016	if (found) {
1017		if (conn->hdrdgst_en) {
1018			iscsi->header_digest_set = true;
1019			iscsi->header_digest = 1;
1020		}
1021
1022		if (conn->datadgst_en) {
1023			iscsi->data_digest_set = true;
1024			iscsi->data_digest = 1;
1025		}
1026		iscsi->boot_taget_portal_set = true;
1027		iscsi->boot_taget_portal = sess->tpgt;
1028
1029	} else {
1030		ret = -1;
1031	}
1032
1033	if (sec_ctrl_flags)
1034		kfree(sec_tgt);
1035free_tgt:
1036	if (pri_ctrl_flags)
1037		kfree(pri_tgt);
1038
1039	return ret;
1040}
1041
1042static void qedi_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
1043{
1044	struct qedi_ctx *qedi;
1045
1046	if (!dev) {
1047		QEDI_INFO(NULL, QEDI_LOG_EVT,
1048			  "dev is NULL so ignoring get_generic_tlv_data request.\n");
1049		return;
1050	}
1051	qedi = (struct qedi_ctx *)dev;
1052
1053	memset(data, 0, sizeof(struct qed_generic_tlvs));
1054	ether_addr_copy(data->mac[0], qedi->mac);
1055}
1056
1057/*
1058 * Protocol TLV handler
1059 */
1060static void qedi_get_protocol_tlv_data(void *dev, void *data)
1061{
1062	struct qed_mfw_tlv_iscsi *iscsi = data;
1063	struct qed_iscsi_stats *fw_iscsi_stats;
1064	struct nvm_iscsi_block *block = NULL;
1065	u32 chap_en = 0, mchap_en = 0;
1066	struct qedi_ctx *qedi = dev;
1067	int rval = 0;
1068
1069	fw_iscsi_stats = kmalloc(sizeof(*fw_iscsi_stats), GFP_KERNEL);
1070	if (!fw_iscsi_stats) {
1071		QEDI_ERR(&qedi->dbg_ctx,
1072			 "Could not allocate memory for fw_iscsi_stats.\n");
1073		goto exit_get_data;
1074	}
1075
1076	mutex_lock(&qedi->stats_lock);
1077	/* Query firmware for offload stats */
1078	qedi_ops->get_stats(qedi->cdev, fw_iscsi_stats);
1079	mutex_unlock(&qedi->stats_lock);
1080
1081	iscsi->rx_frames_set = true;
1082	iscsi->rx_frames = fw_iscsi_stats->iscsi_rx_packet_cnt;
1083	iscsi->rx_bytes_set = true;
1084	iscsi->rx_bytes = fw_iscsi_stats->iscsi_rx_bytes_cnt;
1085	iscsi->tx_frames_set = true;
1086	iscsi->tx_frames = fw_iscsi_stats->iscsi_tx_packet_cnt;
1087	iscsi->tx_bytes_set = true;
1088	iscsi->tx_bytes = fw_iscsi_stats->iscsi_tx_bytes_cnt;
1089	iscsi->frame_size_set = true;
1090	iscsi->frame_size = qedi->ll2_mtu;
1091	block = qedi_get_nvram_block(qedi);
1092	if (block) {
1093		chap_en = !!(block->generic.ctrl_flags &
1094			     NVM_ISCSI_CFG_GEN_CHAP_ENABLED);
1095		mchap_en = !!(block->generic.ctrl_flags &
1096			      NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED);
1097
1098		iscsi->auth_method_set = (chap_en || mchap_en) ? true : false;
1099		iscsi->auth_method = 1;
1100		if (chap_en)
1101			iscsi->auth_method = 2;
1102		if (mchap_en)
1103			iscsi->auth_method = 3;
1104
1105		iscsi->tx_desc_size_set = true;
1106		iscsi->tx_desc_size = QEDI_SQ_SIZE;
1107		iscsi->rx_desc_size_set = true;
1108		iscsi->rx_desc_size = QEDI_CQ_SIZE;
1109
1110		/* tpgt, hdr digest, data digest */
1111		rval = qedi_find_boot_info(qedi, iscsi, block);
1112		if (rval)
1113			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1114				  "Boot target not set");
1115	}
1116
1117	kfree(fw_iscsi_stats);
1118exit_get_data:
1119	return;
1120}
1121
1122void qedi_schedule_hw_err_handler(void *dev,
1123				  enum qed_hw_err_type err_type)
1124{
1125	struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
1126	unsigned long override_flags = qedi_flags_override;
1127
1128	if (override_flags && test_bit(QEDI_ERR_OVERRIDE_EN, &override_flags))
1129		qedi->qedi_err_flags = qedi_flags_override;
1130
1131	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1132		  "HW error handler scheduled, err=%d err_flags=0x%x\n",
1133		  err_type, qedi->qedi_err_flags);
1134
1135	switch (err_type) {
1136	case QED_HW_ERR_FAN_FAIL:
1137		schedule_delayed_work(&qedi->board_disable_work, 0);
1138		break;
1139	case QED_HW_ERR_MFW_RESP_FAIL:
1140	case QED_HW_ERR_HW_ATTN:
1141	case QED_HW_ERR_DMAE_FAIL:
1142	case QED_HW_ERR_RAMROD_FAIL:
1143	case QED_HW_ERR_FW_ASSERT:
1144		/* Prevent HW attentions from being reasserted */
1145		if (test_bit(QEDI_ERR_ATTN_CLR_EN, &qedi->qedi_err_flags))
1146			qedi_ops->common->attn_clr_enable(qedi->cdev, true);
1147
1148		if (err_type == QED_HW_ERR_RAMROD_FAIL &&
1149		    test_bit(QEDI_ERR_IS_RECOVERABLE, &qedi->qedi_err_flags))
1150			qedi_ops->common->recovery_process(qedi->cdev);
1151
1152		break;
1153	default:
1154		break;
1155	}
1156}
1157
1158static void qedi_schedule_recovery_handler(void *dev)
1159{
1160	struct qedi_ctx *qedi = dev;
1161
1162	QEDI_ERR(&qedi->dbg_ctx, "Recovery handler scheduled.\n");
1163
1164	if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags))
1165		return;
1166
1167	atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1168
1169	schedule_delayed_work(&qedi->recovery_work, 0);
1170}
1171
1172static void qedi_set_conn_recovery(struct iscsi_cls_session *cls_session)
1173{
1174	struct iscsi_session *session = cls_session->dd_data;
1175	struct iscsi_conn *conn = session->leadconn;
1176	struct qedi_conn *qedi_conn = conn->dd_data;
1177
1178	qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1179}
1180
1181static void qedi_link_update(void *dev, struct qed_link_output *link)
1182{
1183	struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
1184
1185	if (link->link_up) {
1186		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
1187		atomic_set(&qedi->link_state, QEDI_LINK_UP);
1188	} else {
1189		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1190			  "Link Down event.\n");
1191		atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1192		iscsi_host_for_each_session(qedi->shost, qedi_set_conn_recovery);
1193	}
1194}
1195
1196static struct qed_iscsi_cb_ops qedi_cb_ops = {
1197	{
1198		.link_update =		qedi_link_update,
1199		.schedule_recovery_handler = qedi_schedule_recovery_handler,
1200		.schedule_hw_err_handler = qedi_schedule_hw_err_handler,
1201		.get_protocol_tlv_data = qedi_get_protocol_tlv_data,
1202		.get_generic_tlv_data = qedi_get_generic_tlv_data,
1203	}
1204};
1205
1206static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
1207			  u16 que_idx, struct qedi_percpu_s *p)
1208{
1209	struct qedi_work *qedi_work;
1210	struct qedi_conn *q_conn;
1211	struct qedi_cmd *qedi_cmd;
1212	u32 iscsi_cid;
1213	int rc = 0;
1214
1215	iscsi_cid  = cqe->cqe_common.conn_id;
1216	q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
1217	if (!q_conn) {
1218		QEDI_WARN(&qedi->dbg_ctx,
1219			  "Session no longer exists for cid=0x%x!!\n",
1220			  iscsi_cid);
1221		return -1;
1222	}
1223
1224	switch (cqe->cqe_common.cqe_type) {
1225	case ISCSI_CQE_TYPE_SOLICITED:
1226	case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
1227		qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
1228		if (!qedi_cmd) {
1229			rc = -1;
1230			break;
1231		}
1232		INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
1233		qedi_cmd->cqe_work.qedi = qedi;
1234		memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
1235		qedi_cmd->cqe_work.que_idx = que_idx;
1236		qedi_cmd->cqe_work.is_solicited = true;
1237		list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
1238		break;
1239	case ISCSI_CQE_TYPE_UNSOLICITED:
1240	case ISCSI_CQE_TYPE_DUMMY:
1241	case ISCSI_CQE_TYPE_TASK_CLEANUP:
1242		qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
1243		if (!qedi_work) {
1244			rc = -1;
1245			break;
1246		}
1247		INIT_LIST_HEAD(&qedi_work->list);
1248		qedi_work->qedi = qedi;
1249		memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
1250		qedi_work->que_idx = que_idx;
1251		qedi_work->is_solicited = false;
1252		list_add_tail(&qedi_work->list, &p->work_list);
1253		break;
1254	default:
1255		rc = -1;
1256		QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
1257	}
1258	return rc;
1259}
1260
1261static bool qedi_process_completions(struct qedi_fastpath *fp)
1262{
1263	struct qedi_ctx *qedi = fp->qedi;
1264	struct qed_sb_info *sb_info = fp->sb_info;
1265	struct status_block_e4 *sb = sb_info->sb_virt;
1266	struct qedi_percpu_s *p = NULL;
1267	struct global_queue *que;
1268	u16 prod_idx;
1269	unsigned long flags;
1270	union iscsi_cqe *cqe;
1271	int cpu;
1272	int ret;
1273
1274	/* Get the current firmware producer index */
1275	prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1276
1277	if (prod_idx >= QEDI_CQ_SIZE)
1278		prod_idx = prod_idx % QEDI_CQ_SIZE;
1279
1280	que = qedi->global_queues[fp->sb_id];
1281	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1282		  "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
1283		  que, prod_idx, que->cq_cons_idx, fp->sb_id);
1284
1285	qedi->intr_cpu = fp->sb_id;
1286	cpu = smp_processor_id();
1287	p = &per_cpu(qedi_percpu, cpu);
1288
1289	if (unlikely(!p->iothread))
1290		WARN_ON(1);
1291
1292	spin_lock_irqsave(&p->p_work_lock, flags);
1293	while (que->cq_cons_idx != prod_idx) {
1294		cqe = &que->cq[que->cq_cons_idx];
1295
1296		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1297			  "cqe=%p prod_idx=%d cons_idx=%d.\n",
1298			  cqe, prod_idx, que->cq_cons_idx);
1299
1300		ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
1301		if (ret)
1302			QEDI_WARN(&qedi->dbg_ctx,
1303				  "Dropping CQE 0x%x for cid=0x%x.\n",
1304				  que->cq_cons_idx, cqe->cqe_common.conn_id);
1305
1306		que->cq_cons_idx++;
1307		if (que->cq_cons_idx == QEDI_CQ_SIZE)
1308			que->cq_cons_idx = 0;
1309	}
1310	wake_up_process(p->iothread);
1311	spin_unlock_irqrestore(&p->p_work_lock, flags);
1312
1313	return true;
1314}
1315
1316static bool qedi_fp_has_work(struct qedi_fastpath *fp)
1317{
1318	struct qedi_ctx *qedi = fp->qedi;
1319	struct global_queue *que;
1320	struct qed_sb_info *sb_info = fp->sb_info;
1321	struct status_block_e4 *sb = sb_info->sb_virt;
1322	u16 prod_idx;
1323
1324	barrier();
1325
1326	/* Get the current firmware producer index */
1327	prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1328
1329	/* Get the pointer to the global CQ this completion is on */
1330	que = qedi->global_queues[fp->sb_id];
1331
1332	/* prod idx wrap around uint16 */
1333	if (prod_idx >= QEDI_CQ_SIZE)
1334		prod_idx = prod_idx % QEDI_CQ_SIZE;
1335
1336	return (que->cq_cons_idx != prod_idx);
1337}
1338
1339/* MSI-X fastpath handler code */
1340static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
1341{
1342	struct qedi_fastpath *fp = dev_id;
1343	struct qedi_ctx *qedi = fp->qedi;
1344	bool wake_io_thread = true;
1345
1346	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
1347
1348process_again:
1349	wake_io_thread = qedi_process_completions(fp);
1350	if (wake_io_thread) {
1351		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1352			  "process already running\n");
1353	}
1354
1355	if (!qedi_fp_has_work(fp))
1356		qed_sb_update_sb_idx(fp->sb_info);
1357
1358	/* Check for more work */
1359	rmb();
1360
1361	if (!qedi_fp_has_work(fp))
1362		qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1363	else
1364		goto process_again;
1365
1366	return IRQ_HANDLED;
1367}
1368
1369/* simd handler for MSI/INTa */
1370static void qedi_simd_int_handler(void *cookie)
1371{
1372	/* Cookie is qedi_ctx struct */
1373	struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
1374
1375	QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
1376}
1377
1378#define QEDI_SIMD_HANDLER_NUM		0
1379static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
1380{
1381	int i;
1382	u16 idx;
1383
1384	if (qedi->int_info.msix_cnt) {
1385		for (i = 0; i < qedi->int_info.used_cnt; i++) {
1386			idx = i * qedi->dev_info.common.num_hwfns +
1387			qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1388
1389			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1390				  "Freeing IRQ #%d vector_idx=%d.\n", i, idx);
1391
1392			synchronize_irq(qedi->int_info.msix[idx].vector);
1393			irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1394					      NULL);
1395			free_irq(qedi->int_info.msix[idx].vector,
1396				 &qedi->fp_array[i]);
1397		}
1398	} else {
1399		qedi_ops->common->simd_handler_clean(qedi->cdev,
1400						     QEDI_SIMD_HANDLER_NUM);
1401	}
1402
1403	qedi->int_info.used_cnt = 0;
1404	qedi_ops->common->set_fp_int(qedi->cdev, 0);
1405}
1406
1407static int qedi_request_msix_irq(struct qedi_ctx *qedi)
1408{
1409	int i, rc, cpu;
1410	u16 idx;
1411
1412	cpu = cpumask_first(cpu_online_mask);
1413	for (i = 0; i < qedi->msix_count; i++) {
1414		idx = i * qedi->dev_info.common.num_hwfns +
1415			  qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1416
1417		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1418			  "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
1419			  qedi->dev_info.common.num_hwfns,
1420			  qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
1421
1422		rc = request_irq(qedi->int_info.msix[idx].vector,
1423				 qedi_msix_handler, 0, "qedi",
1424				 &qedi->fp_array[i]);
1425		if (rc) {
1426			QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
1427			qedi_sync_free_irqs(qedi);
1428			return rc;
1429		}
1430		qedi->int_info.used_cnt++;
1431		rc = irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1432					   get_cpu_mask(cpu));
1433		cpu = cpumask_next(cpu, cpu_online_mask);
1434	}
1435
1436	return 0;
1437}
1438
1439static int qedi_setup_int(struct qedi_ctx *qedi)
1440{
1441	int rc = 0;
1442
1443	rc = qedi_ops->common->set_fp_int(qedi->cdev, qedi->num_queues);
1444	if (rc < 0)
1445		goto exit_setup_int;
1446
1447	qedi->msix_count = rc;
1448
1449	rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
1450	if (rc)
1451		goto exit_setup_int;
1452
1453	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1454		  "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
1455		   qedi->int_info.msix_cnt, num_online_cpus());
1456
1457	if (qedi->int_info.msix_cnt) {
1458		rc = qedi_request_msix_irq(qedi);
1459		goto exit_setup_int;
1460	} else {
1461		qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
1462						      QEDI_SIMD_HANDLER_NUM,
1463						      qedi_simd_int_handler);
1464		qedi->int_info.used_cnt = 1;
1465	}
1466
1467exit_setup_int:
1468	return rc;
1469}
1470
1471static void qedi_free_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1472{
1473	if (qedi->iscsi_image)
1474		dma_free_coherent(&qedi->pdev->dev,
1475				  sizeof(struct qedi_nvm_iscsi_image),
1476				  qedi->iscsi_image, qedi->nvm_buf_dma);
1477}
1478
1479static int qedi_alloc_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1480{
1481	qedi->iscsi_image = dma_alloc_coherent(&qedi->pdev->dev,
1482					       sizeof(struct qedi_nvm_iscsi_image),
1483					       &qedi->nvm_buf_dma, GFP_KERNEL);
1484	if (!qedi->iscsi_image) {
1485		QEDI_ERR(&qedi->dbg_ctx, "Could not allocate NVM BUF.\n");
1486		return -ENOMEM;
1487	}
1488	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1489		  "NVM BUF addr=0x%p dma=0x%llx.\n", qedi->iscsi_image,
1490		  qedi->nvm_buf_dma);
1491
1492	return 0;
1493}
1494
1495static void qedi_free_bdq(struct qedi_ctx *qedi)
1496{
1497	int i;
1498
1499	if (qedi->bdq_pbl_list)
1500		dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
1501				  qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
1502
1503	if (qedi->bdq_pbl)
1504		dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
1505				  qedi->bdq_pbl, qedi->bdq_pbl_dma);
1506
1507	for (i = 0; i < QEDI_BDQ_NUM; i++) {
1508		if (qedi->bdq[i].buf_addr) {
1509			dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
1510					  qedi->bdq[i].buf_addr,
1511					  qedi->bdq[i].buf_dma);
1512		}
1513	}
1514}
1515
1516static void qedi_free_global_queues(struct qedi_ctx *qedi)
1517{
1518	int i;
1519	struct global_queue **gl = qedi->global_queues;
1520
1521	for (i = 0; i < qedi->num_queues; i++) {
1522		if (!gl[i])
1523			continue;
1524
1525		if (gl[i]->cq)
1526			dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
1527					  gl[i]->cq, gl[i]->cq_dma);
1528		if (gl[i]->cq_pbl)
1529			dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
1530					  gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
1531
1532		kfree(gl[i]);
1533	}
1534	qedi_free_bdq(qedi);
1535	qedi_free_nvm_iscsi_cfg(qedi);
1536}
1537
1538static int qedi_alloc_bdq(struct qedi_ctx *qedi)
1539{
1540	int i;
1541	struct scsi_bd *pbl;
1542	u64 *list;
1543	dma_addr_t page;
1544
1545	/* Alloc dma memory for BDQ buffers */
1546	for (i = 0; i < QEDI_BDQ_NUM; i++) {
1547		qedi->bdq[i].buf_addr =
1548				dma_alloc_coherent(&qedi->pdev->dev,
1549						   QEDI_BDQ_BUF_SIZE,
1550						   &qedi->bdq[i].buf_dma,
1551						   GFP_KERNEL);
1552		if (!qedi->bdq[i].buf_addr) {
1553			QEDI_ERR(&qedi->dbg_ctx,
1554				 "Could not allocate BDQ buffer %d.\n", i);
1555			return -ENOMEM;
1556		}
1557	}
1558
1559	/* Alloc dma memory for BDQ page buffer list */
1560	qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
1561	qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, QEDI_PAGE_SIZE);
1562	qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
1563
1564	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
1565		  qedi->rq_num_entries);
1566
1567	qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1568					   qedi->bdq_pbl_mem_size,
1569					   &qedi->bdq_pbl_dma, GFP_KERNEL);
1570	if (!qedi->bdq_pbl) {
1571		QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
1572		return -ENOMEM;
1573	}
1574
1575	/*
1576	 * Populate BDQ PBL with physical and virtual address of individual
1577	 * BDQ buffers
1578	 */
1579	pbl = (struct scsi_bd  *)qedi->bdq_pbl;
1580	for (i = 0; i < QEDI_BDQ_NUM; i++) {
1581		pbl->address.hi =
1582				cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
1583		pbl->address.lo =
1584				cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
1585		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1586			  "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
1587			  pbl, pbl->address.hi, pbl->address.lo, i);
1588		pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
1589		pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
1590		pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
1591		pbl->opaque.iscsi_opaque.opaque = cpu_to_le16(i);
1592		pbl++;
1593	}
1594
1595	/* Allocate list of PBL pages */
1596	qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
1597						QEDI_PAGE_SIZE,
1598						&qedi->bdq_pbl_list_dma,
1599						GFP_KERNEL);
1600	if (!qedi->bdq_pbl_list) {
1601		QEDI_ERR(&qedi->dbg_ctx,
1602			 "Could not allocate list of PBL pages.\n");
1603		return -ENOMEM;
1604	}
1605
1606	/*
1607	 * Now populate PBL list with pages that contain pointers to the
1608	 * individual buffers.
1609	 */
1610	qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size /
1611					 QEDI_PAGE_SIZE;
1612	list = (u64 *)qedi->bdq_pbl_list;
1613	page = qedi->bdq_pbl_list_dma;
1614	for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
1615		*list = qedi->bdq_pbl_dma;
1616		list++;
1617		page += QEDI_PAGE_SIZE;
1618	}
1619
1620	return 0;
1621}
1622
1623static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
1624{
1625	u32 *list;
1626	int i;
1627	int status;
1628	u32 *pbl;
1629	dma_addr_t page;
1630	int num_pages;
1631
1632	/*
1633	 * Number of global queues (CQ / RQ). This should
1634	 * be <= number of available MSIX vectors for the PF
1635	 */
1636	if (!qedi->num_queues) {
1637		QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
1638		return -ENOMEM;
1639	}
1640
1641	/* Make sure we allocated the PBL that will contain the physical
1642	 * addresses of our queues
1643	 */
1644	if (!qedi->p_cpuq) {
1645		status = -EINVAL;
1646		goto mem_alloc_failure;
1647	}
1648
1649	qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
1650				       qedi->num_queues), GFP_KERNEL);
1651	if (!qedi->global_queues) {
1652		QEDI_ERR(&qedi->dbg_ctx,
1653			 "Unable to allocate global queues array ptr memory\n");
1654		return -ENOMEM;
1655	}
1656	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1657		  "qedi->global_queues=%p.\n", qedi->global_queues);
1658
1659	/* Allocate DMA coherent buffers for BDQ */
1660	status = qedi_alloc_bdq(qedi);
1661	if (status)
1662		goto mem_alloc_failure;
1663
1664	/* Allocate DMA coherent buffers for NVM_ISCSI_CFG */
1665	status = qedi_alloc_nvm_iscsi_cfg(qedi);
1666	if (status)
1667		goto mem_alloc_failure;
1668
1669	/* Allocate a CQ and an associated PBL for each MSI-X
1670	 * vector.
1671	 */
1672	for (i = 0; i < qedi->num_queues; i++) {
1673		qedi->global_queues[i] =
1674					kzalloc(sizeof(*qedi->global_queues[0]),
1675						GFP_KERNEL);
1676		if (!qedi->global_queues[i]) {
1677			QEDI_ERR(&qedi->dbg_ctx,
1678				 "Unable to allocation global queue %d.\n", i);
1679			status = -ENOMEM;
1680			goto mem_alloc_failure;
1681		}
1682
1683		qedi->global_queues[i]->cq_mem_size =
1684		    (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
1685		qedi->global_queues[i]->cq_mem_size =
1686		    (qedi->global_queues[i]->cq_mem_size +
1687		    (QEDI_PAGE_SIZE - 1));
1688
1689		qedi->global_queues[i]->cq_pbl_size =
1690		    (qedi->global_queues[i]->cq_mem_size /
1691		    QEDI_PAGE_SIZE) * sizeof(void *);
1692		qedi->global_queues[i]->cq_pbl_size =
1693		    (qedi->global_queues[i]->cq_pbl_size +
1694		    (QEDI_PAGE_SIZE - 1));
1695
1696		qedi->global_queues[i]->cq = dma_alloc_coherent(&qedi->pdev->dev,
1697								qedi->global_queues[i]->cq_mem_size,
1698								&qedi->global_queues[i]->cq_dma,
1699								GFP_KERNEL);
1700
1701		if (!qedi->global_queues[i]->cq) {
1702			QEDI_WARN(&qedi->dbg_ctx,
1703				  "Could not allocate cq.\n");
1704			status = -ENOMEM;
1705			goto mem_alloc_failure;
1706		}
1707		qedi->global_queues[i]->cq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1708								    qedi->global_queues[i]->cq_pbl_size,
1709								    &qedi->global_queues[i]->cq_pbl_dma,
1710								    GFP_KERNEL);
1711
1712		if (!qedi->global_queues[i]->cq_pbl) {
1713			QEDI_WARN(&qedi->dbg_ctx,
1714				  "Could not allocate cq PBL.\n");
1715			status = -ENOMEM;
1716			goto mem_alloc_failure;
1717		}
1718
1719		/* Create PBL */
1720		num_pages = qedi->global_queues[i]->cq_mem_size /
1721		    QEDI_PAGE_SIZE;
1722		page = qedi->global_queues[i]->cq_dma;
1723		pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
1724
1725		while (num_pages--) {
1726			*pbl = (u32)page;
1727			pbl++;
1728			*pbl = (u32)((u64)page >> 32);
1729			pbl++;
1730			page += QEDI_PAGE_SIZE;
1731		}
1732	}
1733
1734	list = (u32 *)qedi->p_cpuq;
1735
1736	/*
1737	 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
1738	 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
1739	 * to the physical address which contains an array of pointers to the
1740	 * physical addresses of the specific queue pages.
1741	 */
1742	for (i = 0; i < qedi->num_queues; i++) {
1743		*list = (u32)qedi->global_queues[i]->cq_pbl_dma;
1744		list++;
1745		*list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
1746		list++;
1747
1748		*list = (u32)0;
1749		list++;
1750		*list = (u32)((u64)0 >> 32);
1751		list++;
1752	}
1753
1754	return 0;
1755
1756mem_alloc_failure:
1757	qedi_free_global_queues(qedi);
1758	return status;
1759}
1760
1761int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1762{
1763	int rval = 0;
1764	u32 *pbl;
1765	dma_addr_t page;
1766	int num_pages;
1767
1768	if (!ep)
1769		return -EIO;
1770
1771	/* Calculate appropriate queue and PBL sizes */
1772	ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
1773	ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
1774
1775	ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
1776	ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
1777
1778	ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
1779				    &ep->sq_dma, GFP_KERNEL);
1780	if (!ep->sq) {
1781		QEDI_WARN(&qedi->dbg_ctx,
1782			  "Could not allocate send queue.\n");
1783		rval = -ENOMEM;
1784		goto out;
1785	}
1786	ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
1787					&ep->sq_pbl_dma, GFP_KERNEL);
1788	if (!ep->sq_pbl) {
1789		QEDI_WARN(&qedi->dbg_ctx,
1790			  "Could not allocate send queue PBL.\n");
1791		rval = -ENOMEM;
1792		goto out_free_sq;
1793	}
1794
1795	/* Create PBL */
1796	num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
1797	page = ep->sq_dma;
1798	pbl = (u32 *)ep->sq_pbl;
1799
1800	while (num_pages--) {
1801		*pbl = (u32)page;
1802		pbl++;
1803		*pbl = (u32)((u64)page >> 32);
1804		pbl++;
1805		page += QEDI_PAGE_SIZE;
1806	}
1807
1808	return rval;
1809
1810out_free_sq:
1811	dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1812			  ep->sq_dma);
1813out:
1814	return rval;
1815}
1816
1817void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1818{
1819	if (ep->sq_pbl)
1820		dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
1821				  ep->sq_pbl_dma);
1822	if (ep->sq)
1823		dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1824				  ep->sq_dma);
1825}
1826
1827int qedi_get_task_idx(struct qedi_ctx *qedi)
1828{
1829	s16 tmp_idx;
1830
1831again:
1832	tmp_idx = find_first_zero_bit(qedi->task_idx_map,
1833				      MAX_ISCSI_TASK_ENTRIES);
1834
1835	if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
1836		QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
1837		tmp_idx = -1;
1838		goto err_idx;
1839	}
1840
1841	if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
1842		goto again;
1843
1844err_idx:
1845	return tmp_idx;
1846}
1847
1848void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
1849{
1850	if (!test_and_clear_bit(idx, qedi->task_idx_map))
1851		QEDI_ERR(&qedi->dbg_ctx,
1852			 "FW task context, already cleared, tid=0x%x\n", idx);
1853}
1854
1855void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
1856			 struct qedi_cmd *cmd)
1857{
1858	qedi->itt_map[tid].itt = proto_itt;
1859	qedi->itt_map[tid].p_cmd = cmd;
1860
1861	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1862		  "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
1863		  qedi->itt_map[tid].itt);
1864}
1865
1866void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
1867{
1868	u16 i;
1869
1870	for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
1871		if (qedi->itt_map[i].itt == itt) {
1872			*tid = i;
1873			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1874				  "Ref itt=0x%x, found at tid=0x%x\n",
1875				  itt, *tid);
1876			return;
1877		}
1878	}
1879
1880	WARN_ON(1);
1881}
1882
1883void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
1884{
1885	*proto_itt = qedi->itt_map[tid].itt;
1886	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1887		  "Get itt map tid [0x%x with proto itt[0x%x]",
1888		  tid, *proto_itt);
1889}
1890
1891struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
1892{
1893	struct qedi_cmd *cmd = NULL;
1894
1895	if (tid >= MAX_ISCSI_TASK_ENTRIES)
1896		return NULL;
1897
1898	cmd = qedi->itt_map[tid].p_cmd;
1899	if (cmd->task_id != tid)
1900		return NULL;
1901
1902	qedi->itt_map[tid].p_cmd = NULL;
1903
1904	return cmd;
1905}
1906
1907static int qedi_alloc_itt(struct qedi_ctx *qedi)
1908{
1909	qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
1910				sizeof(struct qedi_itt_map), GFP_KERNEL);
1911	if (!qedi->itt_map) {
1912		QEDI_ERR(&qedi->dbg_ctx,
1913			 "Unable to allocate itt map array memory\n");
1914		return -ENOMEM;
1915	}
1916	return 0;
1917}
1918
1919static void qedi_free_itt(struct qedi_ctx *qedi)
1920{
1921	kfree(qedi->itt_map);
1922}
1923
1924static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
1925	.rx_cb = qedi_ll2_rx,
1926	.tx_cb = NULL,
1927};
1928
1929static int qedi_percpu_io_thread(void *arg)
1930{
1931	struct qedi_percpu_s *p = arg;
1932	struct qedi_work *work, *tmp;
1933	unsigned long flags;
1934	LIST_HEAD(work_list);
1935
1936	set_user_nice(current, -20);
1937
1938	while (!kthread_should_stop()) {
1939		spin_lock_irqsave(&p->p_work_lock, flags);
1940		while (!list_empty(&p->work_list)) {
1941			list_splice_init(&p->work_list, &work_list);
1942			spin_unlock_irqrestore(&p->p_work_lock, flags);
1943
1944			list_for_each_entry_safe(work, tmp, &work_list, list) {
1945				list_del_init(&work->list);
1946				qedi_fp_process_cqes(work);
1947				if (!work->is_solicited)
1948					kfree(work);
1949			}
1950			cond_resched();
1951			spin_lock_irqsave(&p->p_work_lock, flags);
1952		}
1953		set_current_state(TASK_INTERRUPTIBLE);
1954		spin_unlock_irqrestore(&p->p_work_lock, flags);
1955		schedule();
1956	}
1957	__set_current_state(TASK_RUNNING);
1958
1959	return 0;
1960}
1961
1962static int qedi_cpu_online(unsigned int cpu)
1963{
1964	struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1965	struct task_struct *thread;
1966
1967	thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
1968					cpu_to_node(cpu),
1969					"qedi_thread/%d", cpu);
1970	if (IS_ERR(thread))
1971		return PTR_ERR(thread);
1972
1973	kthread_bind(thread, cpu);
1974	p->iothread = thread;
1975	wake_up_process(thread);
1976	return 0;
1977}
1978
1979static int qedi_cpu_offline(unsigned int cpu)
1980{
1981	struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1982	struct qedi_work *work, *tmp;
1983	struct task_struct *thread;
1984	unsigned long flags;
1985
1986	spin_lock_irqsave(&p->p_work_lock, flags);
1987	thread = p->iothread;
1988	p->iothread = NULL;
1989
1990	list_for_each_entry_safe(work, tmp, &p->work_list, list) {
1991		list_del_init(&work->list);
1992		qedi_fp_process_cqes(work);
1993		if (!work->is_solicited)
1994			kfree(work);
1995	}
1996
1997	spin_unlock_irqrestore(&p->p_work_lock, flags);
1998	if (thread)
1999		kthread_stop(thread);
2000	return 0;
2001}
2002
2003void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
2004{
2005	struct qed_ll2_params params;
2006
2007	qedi_recover_all_conns(qedi);
2008
2009	qedi_ops->ll2->stop(qedi->cdev);
2010	qedi_ll2_free_skbs(qedi);
2011
2012	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
2013		  qedi->ll2_mtu, mtu);
2014	memset(&params, 0, sizeof(params));
2015	qedi->ll2_mtu = mtu;
2016	params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
2017	params.drop_ttl0_packets = 0;
2018	params.rx_vlan_stripping = 1;
2019	ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
2020	qedi_ops->ll2->start(qedi->cdev, &params);
2021}
2022
2023/*
2024 * qedi_get_nvram_block: - Scan through the iSCSI NVRAM block (while accounting
2025 * for gaps) for the matching absolute-pf-id of the QEDI device.
2026 */
2027static struct nvm_iscsi_block *
2028qedi_get_nvram_block(struct qedi_ctx *qedi)
2029{
2030	int i;
2031	u8 pf;
2032	u32 flags;
2033	struct nvm_iscsi_block *block;
2034
2035	pf = qedi->dev_info.common.abs_pf_id;
2036	block = &qedi->iscsi_image->iscsi_cfg.block[0];
2037	for (i = 0; i < NUM_OF_ISCSI_PF_SUPPORTED; i++, block++) {
2038		flags = ((block->id) & NVM_ISCSI_CFG_BLK_CTRL_FLAG_MASK) >>
2039			NVM_ISCSI_CFG_BLK_CTRL_FLAG_OFFSET;
2040		if (flags & (NVM_ISCSI_CFG_BLK_CTRL_FLAG_IS_NOT_EMPTY |
2041				NVM_ISCSI_CFG_BLK_CTRL_FLAG_PF_MAPPED) &&
2042			(pf == (block->id & NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_MASK)
2043				>> NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_OFFSET))
2044			return block;
2045	}
2046	return NULL;
2047}
2048
2049static ssize_t qedi_show_boot_eth_info(void *data, int type, char *buf)
2050{
2051	struct qedi_ctx *qedi = data;
2052	struct nvm_iscsi_initiator *initiator;
2053	int rc = 1;
2054	u32 ipv6_en, dhcp_en, ip_len;
2055	struct nvm_iscsi_block *block;
2056	char *fmt, *ip, *sub, *gw;
2057
2058	block = qedi_get_nvram_block(qedi);
2059	if (!block)
2060		return 0;
2061
2062	initiator = &block->initiator;
2063	ipv6_en = block->generic.ctrl_flags &
2064		  NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2065	dhcp_en = block->generic.ctrl_flags &
2066		  NVM_ISCSI_CFG_GEN_DHCP_TCPIP_CONFIG_ENABLED;
2067	/* Static IP assignments. */
2068	fmt = ipv6_en ? "%pI6\n" : "%pI4\n";
2069	ip = ipv6_en ? initiator->ipv6.addr.byte : initiator->ipv4.addr.byte;
2070	ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2071	sub = ipv6_en ? initiator->ipv6.subnet_mask.byte :
2072	      initiator->ipv4.subnet_mask.byte;
2073	gw = ipv6_en ? initiator->ipv6.gateway.byte :
2074	     initiator->ipv4.gateway.byte;
2075	/* DHCP IP adjustments. */
2076	fmt = dhcp_en ? "%s\n" : fmt;
2077	if (dhcp_en) {
2078		ip = ipv6_en ? "0::0" : "0.0.0.0";
2079		sub = ip;
2080		gw = ip;
2081		ip_len = ipv6_en ? 5 : 8;
2082	}
2083
2084	switch (type) {
2085	case ISCSI_BOOT_ETH_IP_ADDR:
2086		rc = snprintf(buf, ip_len, fmt, ip);
2087		break;
2088	case ISCSI_BOOT_ETH_SUBNET_MASK:
2089		rc = snprintf(buf, ip_len, fmt, sub);
2090		break;
2091	case ISCSI_BOOT_ETH_GATEWAY:
2092		rc = snprintf(buf, ip_len, fmt, gw);
2093		break;
2094	case ISCSI_BOOT_ETH_FLAGS:
2095		rc = snprintf(buf, 3, "%hhd\n",
2096			      SYSFS_FLAG_FW_SEL_BOOT);
2097		break;
2098	case ISCSI_BOOT_ETH_INDEX:
2099		rc = snprintf(buf, 3, "0\n");
2100		break;
2101	case ISCSI_BOOT_ETH_MAC:
2102		rc = sysfs_format_mac(buf, qedi->mac, ETH_ALEN);
2103		break;
2104	case ISCSI_BOOT_ETH_VLAN:
2105		rc = snprintf(buf, 12, "%d\n",
2106			      GET_FIELD2(initiator->generic_cont0,
2107					 NVM_ISCSI_CFG_INITIATOR_VLAN));
2108		break;
2109	case ISCSI_BOOT_ETH_ORIGIN:
2110		if (dhcp_en)
2111			rc = snprintf(buf, 3, "3\n");
2112		break;
2113	default:
2114		rc = 0;
2115		break;
2116	}
2117
2118	return rc;
2119}
2120
2121static umode_t qedi_eth_get_attr_visibility(void *data, int type)
2122{
2123	int rc = 1;
2124
2125	switch (type) {
2126	case ISCSI_BOOT_ETH_FLAGS:
2127	case ISCSI_BOOT_ETH_MAC:
2128	case ISCSI_BOOT_ETH_INDEX:
2129	case ISCSI_BOOT_ETH_IP_ADDR:
2130	case ISCSI_BOOT_ETH_SUBNET_MASK:
2131	case ISCSI_BOOT_ETH_GATEWAY:
2132	case ISCSI_BOOT_ETH_ORIGIN:
2133	case ISCSI_BOOT_ETH_VLAN:
2134		rc = 0444;
2135		break;
2136	default:
2137		rc = 0;
2138		break;
2139	}
2140	return rc;
2141}
2142
2143static ssize_t qedi_show_boot_ini_info(void *data, int type, char *buf)
2144{
2145	struct qedi_ctx *qedi = data;
2146	struct nvm_iscsi_initiator *initiator;
2147	int rc;
2148	struct nvm_iscsi_block *block;
2149
2150	block = qedi_get_nvram_block(qedi);
2151	if (!block)
2152		return 0;
2153
2154	initiator = &block->initiator;
2155
2156	switch (type) {
2157	case ISCSI_BOOT_INI_INITIATOR_NAME:
2158		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2159			     initiator->initiator_name.byte);
2160		break;
2161	default:
2162		rc = 0;
2163		break;
2164	}
2165	return rc;
2166}
2167
2168static umode_t qedi_ini_get_attr_visibility(void *data, int type)
2169{
2170	int rc;
2171
2172	switch (type) {
2173	case ISCSI_BOOT_INI_INITIATOR_NAME:
2174		rc = 0444;
2175		break;
2176	default:
2177		rc = 0;
2178		break;
2179	}
2180	return rc;
2181}
2182
2183static ssize_t
2184qedi_show_boot_tgt_info(struct qedi_ctx *qedi, int type,
2185			char *buf, enum qedi_nvm_tgts idx)
2186{
2187	int rc = 1;
2188	u32 ctrl_flags, ipv6_en, chap_en, mchap_en, ip_len;
2189	struct nvm_iscsi_block *block;
2190	char *chap_name, *chap_secret;
2191	char *mchap_name, *mchap_secret;
2192
2193	block = qedi_get_nvram_block(qedi);
2194	if (!block)
2195		goto exit_show_tgt_info;
2196
2197	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2198		  "Port:%d, tgt_idx:%d\n",
2199		  GET_FIELD2(block->id, NVM_ISCSI_CFG_BLK_MAPPED_PF_ID), idx);
2200
2201	ctrl_flags = block->target[idx].ctrl_flags &
2202		     NVM_ISCSI_CFG_TARGET_ENABLED;
2203
2204	if (!ctrl_flags) {
2205		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2206			  "Target disabled\n");
2207		goto exit_show_tgt_info;
2208	}
2209
2210	ipv6_en = block->generic.ctrl_flags &
2211		  NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2212	ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2213	chap_en = block->generic.ctrl_flags &
2214		  NVM_ISCSI_CFG_GEN_CHAP_ENABLED;
2215	chap_name = chap_en ? block->initiator.chap_name.byte : NULL;
2216	chap_secret = chap_en ? block->initiator.chap_password.byte : NULL;
2217
2218	mchap_en = block->generic.ctrl_flags &
2219		  NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED;
2220	mchap_name = mchap_en ? block->target[idx].chap_name.byte : NULL;
2221	mchap_secret = mchap_en ? block->target[idx].chap_password.byte : NULL;
2222
2223	switch (type) {
2224	case ISCSI_BOOT_TGT_NAME:
2225		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2226			     block->target[idx].target_name.byte);
2227		break;
2228	case ISCSI_BOOT_TGT_IP_ADDR:
2229		if (ipv6_en)
2230			rc = snprintf(buf, ip_len, "%pI6\n",
2231				      block->target[idx].ipv6_addr.byte);
2232		else
2233			rc = snprintf(buf, ip_len, "%pI4\n",
2234				      block->target[idx].ipv4_addr.byte);
2235		break;
2236	case ISCSI_BOOT_TGT_PORT:
2237		rc = snprintf(buf, 12, "%d\n",
2238			      GET_FIELD2(block->target[idx].generic_cont0,
2239					 NVM_ISCSI_CFG_TARGET_TCP_PORT));
2240		break;
2241	case ISCSI_BOOT_TGT_LUN:
2242		rc = snprintf(buf, 22, "%.*d\n",
2243			      block->target[idx].lun.value[1],
2244			      block->target[idx].lun.value[0]);
2245		break;
2246	case ISCSI_BOOT_TGT_CHAP_NAME:
2247		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2248			     chap_name);
2249		break;
2250	case ISCSI_BOOT_TGT_CHAP_SECRET:
2251		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
2252			     chap_secret);
2253		break;
2254	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2255		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2256			     mchap_name);
2257		break;
2258	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2259		rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
2260			     mchap_secret);
2261		break;
2262	case ISCSI_BOOT_TGT_FLAGS:
2263		rc = snprintf(buf, 3, "%hhd\n", SYSFS_FLAG_FW_SEL_BOOT);
2264		break;
2265	case ISCSI_BOOT_TGT_NIC_ASSOC:
2266		rc = snprintf(buf, 3, "0\n");
2267		break;
2268	default:
2269		rc = 0;
2270		break;
2271	}
2272
2273exit_show_tgt_info:
2274	return rc;
2275}
2276
2277static ssize_t qedi_show_boot_tgt_pri_info(void *data, int type, char *buf)
2278{
2279	struct qedi_ctx *qedi = data;
2280
2281	return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_PRI);
2282}
2283
2284static ssize_t qedi_show_boot_tgt_sec_info(void *data, int type, char *buf)
2285{
2286	struct qedi_ctx *qedi = data;
2287
2288	return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_SEC);
2289}
2290
2291static umode_t qedi_tgt_get_attr_visibility(void *data, int type)
2292{
2293	int rc;
2294
2295	switch (type) {
2296	case ISCSI_BOOT_TGT_NAME:
2297	case ISCSI_BOOT_TGT_IP_ADDR:
2298	case ISCSI_BOOT_TGT_PORT:
2299	case ISCSI_BOOT_TGT_LUN:
2300	case ISCSI_BOOT_TGT_CHAP_NAME:
2301	case ISCSI_BOOT_TGT_CHAP_SECRET:
2302	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2303	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2304	case ISCSI_BOOT_TGT_NIC_ASSOC:
2305	case ISCSI_BOOT_TGT_FLAGS:
2306		rc = 0444;
2307		break;
2308	default:
2309		rc = 0;
2310		break;
2311	}
2312	return rc;
2313}
2314
2315static void qedi_boot_release(void *data)
2316{
2317	struct qedi_ctx *qedi = data;
2318
2319	scsi_host_put(qedi->shost);
2320}
2321
2322static int qedi_get_boot_info(struct qedi_ctx *qedi)
2323{
2324	int ret = 1;
2325
2326	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2327		  "Get NVM iSCSI CFG image\n");
2328	ret = qedi_ops->common->nvm_get_image(qedi->cdev,
2329					      QED_NVM_IMAGE_ISCSI_CFG,
2330					      (char *)qedi->iscsi_image,
2331					      sizeof(struct qedi_nvm_iscsi_image));
2332	if (ret)
2333		QEDI_ERR(&qedi->dbg_ctx,
2334			 "Could not get NVM image. ret = %d\n", ret);
2335
2336	return ret;
2337}
2338
2339static int qedi_setup_boot_info(struct qedi_ctx *qedi)
2340{
2341	struct iscsi_boot_kobj *boot_kobj;
2342
2343	if (qedi_get_boot_info(qedi))
2344		return -EPERM;
2345
2346	qedi->boot_kset = iscsi_boot_create_host_kset(qedi->shost->host_no);
2347	if (!qedi->boot_kset)
2348		goto kset_free;
2349
2350	if (!scsi_host_get(qedi->shost))
2351		goto kset_free;
2352
2353	boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 0, qedi,
2354					     qedi_show_boot_tgt_pri_info,
2355					     qedi_tgt_get_attr_visibility,
2356					     qedi_boot_release);
2357	if (!boot_kobj)
2358		goto put_host;
2359
2360	if (!scsi_host_get(qedi->shost))
2361		goto kset_free;
2362
2363	boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 1, qedi,
2364					     qedi_show_boot_tgt_sec_info,
2365					     qedi_tgt_get_attr_visibility,
2366					     qedi_boot_release);
2367	if (!boot_kobj)
2368		goto put_host;
2369
2370	if (!scsi_host_get(qedi->shost))
2371		goto kset_free;
2372
2373	boot_kobj = iscsi_boot_create_initiator(qedi->boot_kset, 0, qedi,
2374						qedi_show_boot_ini_info,
2375						qedi_ini_get_attr_visibility,
2376						qedi_boot_release);
2377	if (!boot_kobj)
2378		goto put_host;
2379
2380	if (!scsi_host_get(qedi->shost))
2381		goto kset_free;
2382
2383	boot_kobj = iscsi_boot_create_ethernet(qedi->boot_kset, 0, qedi,
2384					       qedi_show_boot_eth_info,
2385					       qedi_eth_get_attr_visibility,
2386					       qedi_boot_release);
2387	if (!boot_kobj)
2388		goto put_host;
2389
2390	return 0;
2391
2392put_host:
2393	scsi_host_put(qedi->shost);
2394kset_free:
2395	iscsi_boot_destroy_kset(qedi->boot_kset);
2396	return -ENOMEM;
2397}
2398
2399static pci_ers_result_t qedi_io_error_detected(struct pci_dev *pdev,
2400					       pci_channel_state_t state)
2401{
2402	struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2403
2404	QEDI_ERR(&qedi->dbg_ctx, "%s: PCI error detected [%d]\n",
2405		 __func__, state);
2406
2407	if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
2408		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2409			  "Recovery already in progress.\n");
2410		return PCI_ERS_RESULT_NONE;
2411	}
2412
2413	qedi_ops->common->recovery_process(qedi->cdev);
2414
2415	return PCI_ERS_RESULT_CAN_RECOVER;
2416}
2417
2418static void __qedi_remove(struct pci_dev *pdev, int mode)
2419{
2420	struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2421	int rval;
2422	u16 retry = 10;
2423
2424	if (mode == QEDI_MODE_SHUTDOWN)
2425		iscsi_host_for_each_session(qedi->shost,
2426					    qedi_clear_session_ctx);
2427
2428	if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2429		if (qedi->tmf_thread) {
2430			flush_workqueue(qedi->tmf_thread);
2431			destroy_workqueue(qedi->tmf_thread);
2432			qedi->tmf_thread = NULL;
2433		}
2434
2435		if (qedi->offload_thread) {
2436			flush_workqueue(qedi->offload_thread);
2437			destroy_workqueue(qedi->offload_thread);
2438			qedi->offload_thread = NULL;
2439		}
2440	}
2441
2442#ifdef CONFIG_DEBUG_FS
2443	qedi_dbg_host_exit(&qedi->dbg_ctx);
2444#endif
2445	if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
2446		qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2447
2448	qedi_sync_free_irqs(qedi);
2449
2450	if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2451		while (retry--) {
2452			rval = qedi_ops->stop(qedi->cdev);
2453			if (rval < 0)
2454				msleep(1000);
2455			else
2456				break;
2457		}
2458		qedi_ops->ll2->stop(qedi->cdev);
2459	}
2460
2461	cancel_delayed_work_sync(&qedi->recovery_work);
2462	cancel_delayed_work_sync(&qedi->board_disable_work);
2463
2464	qedi_free_iscsi_pf_param(qedi);
2465
2466	rval = qedi_ops->common->update_drv_state(qedi->cdev, false);
2467	if (rval)
2468		QEDI_ERR(&qedi->dbg_ctx, "Failed to send drv state to MFW\n");
2469
2470	if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2471		qedi_ops->common->slowpath_stop(qedi->cdev);
2472		qedi_ops->common->remove(qedi->cdev);
2473	}
2474
2475	qedi_destroy_fp(qedi);
2476
2477	if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2478		qedi_release_cid_que(qedi);
2479		qedi_cm_free_mem(qedi);
2480		qedi_free_uio(qedi->udev);
2481		qedi_free_itt(qedi);
2482
2483		if (qedi->ll2_recv_thread) {
2484			kthread_stop(qedi->ll2_recv_thread);
2485			qedi->ll2_recv_thread = NULL;
2486		}
2487		qedi_ll2_free_skbs(qedi);
2488
2489		if (qedi->boot_kset)
2490			iscsi_boot_destroy_kset(qedi->boot_kset);
2491
2492		iscsi_host_remove(qedi->shost);
2493		iscsi_host_free(qedi->shost);
2494	}
2495}
2496
2497static void qedi_board_disable_work(struct work_struct *work)
2498{
2499	struct qedi_ctx *qedi =
2500			container_of(work, struct qedi_ctx,
2501				     board_disable_work.work);
2502
2503	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2504		  "Fan failure, Unloading firmware context.\n");
2505
2506	if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
2507		return;
2508
2509	__qedi_remove(qedi->pdev, QEDI_MODE_SHUTDOWN);
2510}
2511
2512static void qedi_shutdown(struct pci_dev *pdev)
2513{
2514	struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2515
2516	QEDI_ERR(&qedi->dbg_ctx, "%s: Shutdown qedi\n", __func__);
2517	if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
2518		return;
2519	__qedi_remove(pdev, QEDI_MODE_SHUTDOWN);
2520}
2521
2522static int qedi_suspend(struct pci_dev *pdev, pm_message_t state)
2523{
2524	struct qedi_ctx *qedi;
2525
2526	if (!pdev) {
2527		QEDI_ERR(NULL, "pdev is NULL.\n");
2528		return -ENODEV;
2529	}
2530
2531	qedi = pci_get_drvdata(pdev);
2532
2533	QEDI_ERR(&qedi->dbg_ctx, "%s: Device does not support suspend operation\n", __func__);
2534
2535	return -EPERM;
2536}
2537
2538static int __qedi_probe(struct pci_dev *pdev, int mode)
2539{
2540	struct qedi_ctx *qedi;
2541	struct qed_ll2_params params;
2542	u8 dp_level = 0;
2543	bool is_vf = false;
2544	char host_buf[16];
2545	struct qed_link_params link_params;
2546	struct qed_slowpath_params sp_params;
2547	struct qed_probe_params qed_params;
2548	void *task_start, *task_end;
2549	int rc;
2550	u16 retry = 10;
2551
2552	if (mode != QEDI_MODE_RECOVERY) {
2553		qedi = qedi_host_alloc(pdev);
2554		if (!qedi) {
2555			rc = -ENOMEM;
2556			goto exit_probe;
2557		}
2558	} else {
2559		qedi = pci_get_drvdata(pdev);
2560	}
2561
2562retry_probe:
2563	if (mode == QEDI_MODE_RECOVERY)
2564		msleep(2000);
2565
2566	memset(&qed_params, 0, sizeof(qed_params));
2567	qed_params.protocol = QED_PROTOCOL_ISCSI;
2568	qed_params.dp_module = qedi_qed_debug;
2569	qed_params.dp_level = dp_level;
2570	qed_params.is_vf = is_vf;
2571	qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
2572	if (!qedi->cdev) {
2573		if (mode == QEDI_MODE_RECOVERY && retry) {
2574			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2575				  "Retry %d initialize hardware\n", retry);
2576			retry--;
2577			goto retry_probe;
2578		}
2579
2580		rc = -ENODEV;
2581		QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
2582		goto free_host;
2583	}
2584
2585	set_bit(QEDI_ERR_ATTN_CLR_EN, &qedi->qedi_err_flags);
2586	set_bit(QEDI_ERR_IS_RECOVERABLE, &qedi->qedi_err_flags);
2587	atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2588
2589	rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2590	if (rc)
2591		goto free_host;
2592
2593	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2594		  "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
2595		  qedi->dev_info.common.num_hwfns,
2596		  qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
2597
2598	rc = qedi_set_iscsi_pf_param(qedi);
2599	if (rc) {
2600		rc = -ENOMEM;
2601		QEDI_ERR(&qedi->dbg_ctx,
2602			 "Set iSCSI pf param fail\n");
2603		goto free_host;
2604	}
2605
2606	qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2607
2608	rc = qedi_prepare_fp(qedi);
2609	if (rc) {
2610		QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
2611		goto free_pf_params;
2612	}
2613
2614	/* Start the Slowpath-process */
2615	memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
2616	sp_params.int_mode = QED_INT_MODE_MSIX;
2617	sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
2618	sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
2619	sp_params.drv_rev = QEDI_DRIVER_REV_VER;
2620	sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
2621	strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
2622	rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
2623	if (rc) {
2624		QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
2625		goto stop_hw;
2626	}
2627
2628	/* update_pf_params needs to be called before and after slowpath
2629	 * start
2630	 */
2631	qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2632
2633	rc = qedi_setup_int(qedi);
2634	if (rc)
2635		goto stop_iscsi_func;
2636
2637	qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2638
2639	/* Learn information crucial for qedi to progress */
2640	rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2641	if (rc)
2642		goto stop_iscsi_func;
2643
2644	/* Record BDQ producer doorbell addresses */
2645	qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
2646	qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
2647	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2648		  "BDQ primary_prod=%p secondary_prod=%p.\n",
2649		  qedi->bdq_primary_prod,
2650		  qedi->bdq_secondary_prod);
2651
2652	/*
2653	 * We need to write the number of BDs in the BDQ we've preallocated so
2654	 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
2655	 * packet arrives.
2656	 */
2657	qedi->bdq_prod_idx = QEDI_BDQ_NUM;
2658	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2659		  "Writing %d to primary and secondary BDQ doorbell registers.\n",
2660		  qedi->bdq_prod_idx);
2661	writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
2662	readw(qedi->bdq_primary_prod);
2663	writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
2664	readw(qedi->bdq_secondary_prod);
2665
2666	ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
2667	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
2668		  qedi->mac);
2669
2670	snprintf(host_buf, sizeof(host_buf), "host_%d", qedi->shost->host_no);
2671	qedi_ops->common->set_name(qedi->cdev, host_buf);
2672
2673	qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
2674
2675	memset(&params, 0, sizeof(params));
2676	params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
2677	qedi->ll2_mtu = DEF_PATH_MTU;
2678	params.drop_ttl0_packets = 0;
2679	params.rx_vlan_stripping = 1;
2680	ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
2681
2682	if (mode != QEDI_MODE_RECOVERY) {
2683		/* set up rx path */
2684		INIT_LIST_HEAD(&qedi->ll2_skb_list);
2685		spin_lock_init(&qedi->ll2_lock);
2686		/* start qedi context */
2687		spin_lock_init(&qedi->hba_lock);
2688		spin_lock_init(&qedi->task_idx_lock);
2689		mutex_init(&qedi->stats_lock);
2690	}
2691	qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
2692	qedi_ops->ll2->start(qedi->cdev, &params);
2693
2694	if (mode != QEDI_MODE_RECOVERY) {
2695		qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
2696						    (void *)qedi,
2697						    "qedi_ll2_thread");
2698	}
2699
2700	rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
2701			     qedi, qedi_iscsi_event_cb);
2702	if (rc) {
2703		rc = -ENODEV;
2704		QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
2705		goto stop_slowpath;
2706	}
2707
2708	task_start = qedi_get_task_mem(&qedi->tasks, 0);
2709	task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
2710	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2711		  "Task context start=%p, end=%p block_size=%u.\n",
2712		   task_start, task_end, qedi->tasks.size);
2713
2714	memset(&link_params, 0, sizeof(link_params));
2715	link_params.link_up = true;
2716	rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
2717	if (rc) {
2718		QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
2719		atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2720	}
2721
2722#ifdef CONFIG_DEBUG_FS
2723	qedi_dbg_host_init(&qedi->dbg_ctx, qedi_debugfs_ops,
2724			   qedi_dbg_fops);
2725#endif
2726	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2727		  "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
2728		  QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
2729		  FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
2730
2731	if (mode == QEDI_MODE_NORMAL) {
2732		if (iscsi_host_add(qedi->shost, &pdev->dev)) {
2733			QEDI_ERR(&qedi->dbg_ctx,
2734				 "Could not add iscsi host\n");
2735			rc = -ENOMEM;
2736			goto remove_host;
2737		}
2738
2739		/* Allocate uio buffers */
2740		rc = qedi_alloc_uio_rings(qedi);
2741		if (rc) {
2742			QEDI_ERR(&qedi->dbg_ctx,
2743				 "UIO alloc ring failed err=%d\n", rc);
2744			goto remove_host;
2745		}
2746
2747		rc = qedi_init_uio(qedi);
2748		if (rc) {
2749			QEDI_ERR(&qedi->dbg_ctx,
2750				 "UIO init failed, err=%d\n", rc);
2751			goto free_uio;
2752		}
2753
2754		/* host the array on iscsi_conn */
2755		rc = qedi_setup_cid_que(qedi);
2756		if (rc) {
2757			QEDI_ERR(&qedi->dbg_ctx,
2758				 "Could not setup cid que\n");
2759			goto free_uio;
2760		}
2761
2762		rc = qedi_cm_alloc_mem(qedi);
2763		if (rc) {
2764			QEDI_ERR(&qedi->dbg_ctx,
2765				 "Could not alloc cm memory\n");
2766			goto free_cid_que;
2767		}
2768
2769		rc = qedi_alloc_itt(qedi);
2770		if (rc) {
2771			QEDI_ERR(&qedi->dbg_ctx,
2772				 "Could not alloc itt memory\n");
2773			goto free_cid_que;
2774		}
2775
2776		sprintf(host_buf, "host_%d", qedi->shost->host_no);
2777		qedi->tmf_thread = create_singlethread_workqueue(host_buf);
2778		if (!qedi->tmf_thread) {
2779			QEDI_ERR(&qedi->dbg_ctx,
2780				 "Unable to start tmf thread!\n");
2781			rc = -ENODEV;
2782			goto free_cid_que;
2783		}
2784
2785		sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
2786		qedi->offload_thread = create_workqueue(host_buf);
2787		if (!qedi->offload_thread) {
2788			QEDI_ERR(&qedi->dbg_ctx,
2789				 "Unable to start offload thread!\n");
2790			rc = -ENODEV;
2791			goto free_tmf_thread;
2792		}
2793
2794		INIT_DELAYED_WORK(&qedi->recovery_work, qedi_recovery_handler);
2795		INIT_DELAYED_WORK(&qedi->board_disable_work,
2796				  qedi_board_disable_work);
2797
2798		/* F/w needs 1st task context memory entry for performance */
2799		set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
2800		atomic_set(&qedi->num_offloads, 0);
2801
2802		if (qedi_setup_boot_info(qedi))
2803			QEDI_ERR(&qedi->dbg_ctx,
2804				 "No iSCSI boot target configured\n");
2805
2806		rc = qedi_ops->common->update_drv_state(qedi->cdev, true);
2807		if (rc)
2808			QEDI_ERR(&qedi->dbg_ctx,
2809				 "Failed to send drv state to MFW\n");
2810
2811	}
2812
2813	return 0;
2814
2815free_tmf_thread:
2816	destroy_workqueue(qedi->tmf_thread);
2817free_cid_que:
2818	qedi_release_cid_que(qedi);
2819free_uio:
2820	qedi_free_uio(qedi->udev);
2821remove_host:
2822#ifdef CONFIG_DEBUG_FS
2823	qedi_dbg_host_exit(&qedi->dbg_ctx);
2824#endif
2825	iscsi_host_remove(qedi->shost);
2826stop_iscsi_func:
2827	qedi_ops->stop(qedi->cdev);
2828stop_slowpath:
2829	qedi_ops->common->slowpath_stop(qedi->cdev);
2830stop_hw:
2831	qedi_ops->common->remove(qedi->cdev);
2832free_pf_params:
2833	qedi_free_iscsi_pf_param(qedi);
2834free_host:
2835	iscsi_host_free(qedi->shost);
2836exit_probe:
2837	return rc;
2838}
2839
2840static void qedi_mark_conn_recovery(struct iscsi_cls_session *cls_session)
2841{
2842	struct iscsi_session *session = cls_session->dd_data;
2843	struct iscsi_conn *conn = session->leadconn;
2844	struct qedi_conn *qedi_conn = conn->dd_data;
2845
2846	iscsi_conn_failure(qedi_conn->cls_conn->dd_data, ISCSI_ERR_CONN_FAILED);
2847}
2848
2849static void qedi_recovery_handler(struct work_struct *work)
2850{
2851	struct qedi_ctx *qedi =
2852			container_of(work, struct qedi_ctx, recovery_work.work);
2853
2854	iscsi_host_for_each_session(qedi->shost, qedi_mark_conn_recovery);
2855
2856	/* Call common_ops->recovery_prolog to allow the MFW to quiesce
2857	 * any PCI transactions.
2858	 */
2859	qedi_ops->common->recovery_prolog(qedi->cdev);
2860
2861	__qedi_remove(qedi->pdev, QEDI_MODE_RECOVERY);
2862	__qedi_probe(qedi->pdev, QEDI_MODE_RECOVERY);
2863	clear_bit(QEDI_IN_RECOVERY, &qedi->flags);
2864}
2865
2866static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2867{
2868	return __qedi_probe(pdev, QEDI_MODE_NORMAL);
2869}
2870
2871static void qedi_remove(struct pci_dev *pdev)
2872{
2873	__qedi_remove(pdev, QEDI_MODE_NORMAL);
2874}
2875
2876static struct pci_device_id qedi_pci_tbl[] = {
2877	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
2878	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
2879	{ 0 },
2880};
2881MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
2882
2883static enum cpuhp_state qedi_cpuhp_state;
2884
2885static struct pci_error_handlers qedi_err_handler = {
2886	.error_detected = qedi_io_error_detected,
2887};
2888
2889static struct pci_driver qedi_pci_driver = {
2890	.name = QEDI_MODULE_NAME,
2891	.id_table = qedi_pci_tbl,
2892	.probe = qedi_probe,
2893	.remove = qedi_remove,
2894	.shutdown = qedi_shutdown,
2895	.err_handler = &qedi_err_handler,
2896	.suspend = qedi_suspend,
2897};
2898
2899static int __init qedi_init(void)
2900{
2901	struct qedi_percpu_s *p;
2902	int cpu, rc = 0;
2903
2904	qedi_ops = qed_get_iscsi_ops();
2905	if (!qedi_ops) {
2906		QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
2907		return -EINVAL;
2908	}
2909
2910#ifdef CONFIG_DEBUG_FS
2911	qedi_dbg_init("qedi");
2912#endif
2913
2914	qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
2915	if (!qedi_scsi_transport) {
2916		QEDI_ERR(NULL, "Could not register qedi transport");
2917		rc = -ENOMEM;
2918		goto exit_qedi_init_1;
2919	}
2920
2921	for_each_possible_cpu(cpu) {
2922		p = &per_cpu(qedi_percpu, cpu);
2923		INIT_LIST_HEAD(&p->work_list);
2924		spin_lock_init(&p->p_work_lock);
2925		p->iothread = NULL;
2926	}
2927
2928	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
2929			       qedi_cpu_online, qedi_cpu_offline);
2930	if (rc < 0)
2931		goto exit_qedi_init_2;
2932	qedi_cpuhp_state = rc;
2933
2934	rc = pci_register_driver(&qedi_pci_driver);
2935	if (rc) {
2936		QEDI_ERR(NULL, "Failed to register driver\n");
2937		goto exit_qedi_hp;
2938	}
2939
2940	return 0;
2941
2942exit_qedi_hp:
2943	cpuhp_remove_state(qedi_cpuhp_state);
2944exit_qedi_init_2:
2945	iscsi_unregister_transport(&qedi_iscsi_transport);
2946exit_qedi_init_1:
2947#ifdef CONFIG_DEBUG_FS
2948	qedi_dbg_exit();
2949#endif
2950	qed_put_iscsi_ops();
2951	return rc;
2952}
2953
2954static void __exit qedi_cleanup(void)
2955{
2956	pci_unregister_driver(&qedi_pci_driver);
2957	cpuhp_remove_state(qedi_cpuhp_state);
2958	iscsi_unregister_transport(&qedi_iscsi_transport);
2959
2960#ifdef CONFIG_DEBUG_FS
2961	qedi_dbg_exit();
2962#endif
2963	qed_put_iscsi_ops();
2964}
2965
2966MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
2967MODULE_LICENSE("GPL");
2968MODULE_AUTHOR("QLogic Corporation");
2969MODULE_VERSION(QEDI_MODULE_VERSION);
2970module_init(qedi_init);
2971module_exit(qedi_cleanup);
2972