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