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