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