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