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