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