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