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