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