xref: /openbmc/linux/drivers/scsi/fnic/vnic_dev.c (revision 4b7444ff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
4  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/types.h>
10 #include <linux/pci.h>
11 #include <linux/delay.h>
12 #include <linux/if_ether.h>
13 #include <linux/slab.h>
14 #include "vnic_resource.h"
15 #include "vnic_devcmd.h"
16 #include "vnic_dev.h"
17 #include "vnic_stats.h"
18 #include "vnic_wq.h"
19 
20 struct devcmd2_controller {
21 	struct vnic_wq_ctrl *wq_ctrl;
22 	struct vnic_dev_ring results_ring;
23 	struct vnic_wq wq;
24 	struct vnic_devcmd2 *cmd_ring;
25 	struct devcmd2_result *result;
26 	u16 next_result;
27 	u16 result_size;
28 	int color;
29 };
30 
31 enum vnic_proxy_type {
32 	PROXY_NONE,
33 	PROXY_BY_BDF,
34 	PROXY_BY_INDEX,
35 };
36 
37 struct vnic_res {
38 	void __iomem *vaddr;
39 	unsigned int count;
40 };
41 
42 struct vnic_dev {
43 	void *priv;
44 	struct pci_dev *pdev;
45 	struct vnic_res res[RES_TYPE_MAX];
46 	enum vnic_dev_intr_mode intr_mode;
47 	struct vnic_devcmd __iomem *devcmd;
48 	struct vnic_devcmd_notify *notify;
49 	struct vnic_devcmd_notify notify_copy;
50 	dma_addr_t notify_pa;
51 	u32 *linkstatus;
52 	dma_addr_t linkstatus_pa;
53 	struct vnic_stats *stats;
54 	dma_addr_t stats_pa;
55 	struct vnic_devcmd_fw_info *fw_info;
56 	dma_addr_t fw_info_pa;
57 	enum vnic_proxy_type proxy;
58 	u32 proxy_index;
59 	u64 args[VNIC_DEVCMD_NARGS];
60 	struct devcmd2_controller *devcmd2;
61 	int (*devcmd_rtn)(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
62 			int wait);
63 };
64 
65 #define VNIC_MAX_RES_HDR_SIZE \
66 	(sizeof(struct vnic_resource_header) + \
67 	sizeof(struct vnic_resource) * RES_TYPE_MAX)
68 #define VNIC_RES_STRIDE	128
69 
70 void *vnic_dev_priv(struct vnic_dev *vdev)
71 {
72 	return vdev->priv;
73 }
74 
75 static int vnic_dev_discover_res(struct vnic_dev *vdev,
76 	struct vnic_dev_bar *bar)
77 {
78 	struct vnic_resource_header __iomem *rh;
79 	struct vnic_resource __iomem *r;
80 	u8 type;
81 
82 	if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
83 		printk(KERN_ERR "vNIC BAR0 res hdr length error\n");
84 		return -EINVAL;
85 	}
86 
87 	rh = bar->vaddr;
88 	if (!rh) {
89 		printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n");
90 		return -EINVAL;
91 	}
92 
93 	if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
94 	    ioread32(&rh->version) != VNIC_RES_VERSION) {
95 		printk(KERN_ERR "vNIC BAR0 res magic/version error "
96 			"exp (%lx/%lx) curr (%x/%x)\n",
97 			VNIC_RES_MAGIC, VNIC_RES_VERSION,
98 			ioread32(&rh->magic), ioread32(&rh->version));
99 		return -EINVAL;
100 	}
101 
102 	r = (struct vnic_resource __iomem *)(rh + 1);
103 
104 	while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
105 
106 		u8 bar_num = ioread8(&r->bar);
107 		u32 bar_offset = ioread32(&r->bar_offset);
108 		u32 count = ioread32(&r->count);
109 		u32 len;
110 
111 		r++;
112 
113 		if (bar_num != 0)  /* only mapping in BAR0 resources */
114 			continue;
115 
116 		switch (type) {
117 		case RES_TYPE_WQ:
118 		case RES_TYPE_RQ:
119 		case RES_TYPE_CQ:
120 		case RES_TYPE_INTR_CTRL:
121 			/* each count is stride bytes long */
122 			len = count * VNIC_RES_STRIDE;
123 			if (len + bar_offset > bar->len) {
124 				printk(KERN_ERR "vNIC BAR0 resource %d "
125 					"out-of-bounds, offset 0x%x + "
126 					"size 0x%x > bar len 0x%lx\n",
127 					type, bar_offset,
128 					len,
129 					bar->len);
130 				return -EINVAL;
131 			}
132 			break;
133 		case RES_TYPE_INTR_PBA_LEGACY:
134 		case RES_TYPE_DEVCMD2:
135 		case RES_TYPE_DEVCMD:
136 			len = count;
137 			break;
138 		default:
139 			continue;
140 		}
141 
142 		vdev->res[type].count = count;
143 		vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
144 	}
145 
146 	return 0;
147 }
148 
149 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
150 	enum vnic_res_type type)
151 {
152 	return vdev->res[type].count;
153 }
154 
155 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
156 	unsigned int index)
157 {
158 	if (!vdev->res[type].vaddr)
159 		return NULL;
160 
161 	switch (type) {
162 	case RES_TYPE_WQ:
163 	case RES_TYPE_RQ:
164 	case RES_TYPE_CQ:
165 	case RES_TYPE_INTR_CTRL:
166 		return (char __iomem *)vdev->res[type].vaddr +
167 					index * VNIC_RES_STRIDE;
168 	default:
169 		return (char __iomem *)vdev->res[type].vaddr;
170 	}
171 }
172 
173 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
174 				     unsigned int desc_count,
175 				     unsigned int desc_size)
176 {
177 	/* The base address of the desc rings must be 512 byte aligned.
178 	 * Descriptor count is aligned to groups of 32 descriptors.  A
179 	 * count of 0 means the maximum 4096 descriptors.  Descriptor
180 	 * size is aligned to 16 bytes.
181 	 */
182 
183 	unsigned int count_align = 32;
184 	unsigned int desc_align = 16;
185 
186 	ring->base_align = 512;
187 
188 	if (desc_count == 0)
189 		desc_count = 4096;
190 
191 	ring->desc_count = ALIGN(desc_count, count_align);
192 
193 	ring->desc_size = ALIGN(desc_size, desc_align);
194 
195 	ring->size = ring->desc_count * ring->desc_size;
196 	ring->size_unaligned = ring->size + ring->base_align;
197 
198 	return ring->size_unaligned;
199 }
200 
201 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
202 {
203 	memset(ring->descs, 0, ring->size);
204 }
205 
206 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
207 	unsigned int desc_count, unsigned int desc_size)
208 {
209 	vnic_dev_desc_ring_size(ring, desc_count, desc_size);
210 
211 	ring->descs_unaligned = dma_alloc_coherent(&vdev->pdev->dev,
212 		ring->size_unaligned,
213 		&ring->base_addr_unaligned, GFP_KERNEL);
214 
215 	if (!ring->descs_unaligned) {
216 		printk(KERN_ERR
217 		  "Failed to allocate ring (size=%d), aborting\n",
218 			(int)ring->size);
219 		return -ENOMEM;
220 	}
221 
222 	ring->base_addr = ALIGN(ring->base_addr_unaligned,
223 		ring->base_align);
224 	ring->descs = (u8 *)ring->descs_unaligned +
225 		(ring->base_addr - ring->base_addr_unaligned);
226 
227 	vnic_dev_clear_desc_ring(ring);
228 
229 	ring->desc_avail = ring->desc_count - 1;
230 
231 	return 0;
232 }
233 
234 void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
235 {
236 	if (ring->descs) {
237 		dma_free_coherent(&vdev->pdev->dev,
238 			ring->size_unaligned,
239 			ring->descs_unaligned,
240 			ring->base_addr_unaligned);
241 		ring->descs = NULL;
242 	}
243 }
244 
245 static int vnic_dev_cmd1(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, int wait)
246 {
247 	struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
248 	int delay;
249 	u32 status;
250 	static const int dev_cmd_err[] = {
251 		/* convert from fw's version of error.h to host's version */
252 		0,	/* ERR_SUCCESS */
253 		EINVAL,	/* ERR_EINVAL */
254 		EFAULT,	/* ERR_EFAULT */
255 		EPERM,	/* ERR_EPERM */
256 		EBUSY,  /* ERR_EBUSY */
257 	};
258 	int err;
259 	u64 *a0 = &vdev->args[0];
260 	u64 *a1 = &vdev->args[1];
261 
262 	status = ioread32(&devcmd->status);
263 	if (status & STAT_BUSY) {
264 		printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd));
265 		return -EBUSY;
266 	}
267 
268 	if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
269 		writeq(*a0, &devcmd->args[0]);
270 		writeq(*a1, &devcmd->args[1]);
271 		wmb();
272 	}
273 
274 	iowrite32(cmd, &devcmd->cmd);
275 
276 	if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
277 			return 0;
278 
279 	for (delay = 0; delay < wait; delay++) {
280 
281 		udelay(100);
282 
283 		status = ioread32(&devcmd->status);
284 		if (!(status & STAT_BUSY)) {
285 
286 			if (status & STAT_ERROR) {
287 				err = dev_cmd_err[(int)readq(&devcmd->args[0])];
288 				printk(KERN_ERR "Error %d devcmd %d\n",
289 					err, _CMD_N(cmd));
290 				return -err;
291 			}
292 
293 			if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
294 				rmb();
295 				*a0 = readq(&devcmd->args[0]);
296 				*a1 = readq(&devcmd->args[1]);
297 			}
298 
299 			return 0;
300 		}
301 	}
302 
303 	printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd));
304 	return -ETIMEDOUT;
305 }
306 
307 static int vnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
308 		int wait)
309 {
310 	struct devcmd2_controller *dc2c = vdev->devcmd2;
311 	struct devcmd2_result *result;
312 	u8 color;
313 	unsigned int i;
314 	int delay;
315 	int err;
316 	u32 fetch_index;
317 	u32 posted;
318 	u32 new_posted;
319 
320 	posted = ioread32(&dc2c->wq_ctrl->posted_index);
321 	fetch_index = ioread32(&dc2c->wq_ctrl->fetch_index);
322 
323 	if (posted == 0xFFFFFFFF || fetch_index == 0xFFFFFFFF) {
324 		/* Hardware surprise removal: return error */
325 		pr_err("%s: devcmd2 invalid posted or fetch index on cmd %d\n",
326 				pci_name(vdev->pdev), _CMD_N(cmd));
327 		pr_err("%s: fetch index: %u, posted index: %u\n",
328 				pci_name(vdev->pdev), fetch_index, posted);
329 
330 		return -ENODEV;
331 
332 	}
333 
334 	new_posted = (posted + 1) % DEVCMD2_RING_SIZE;
335 
336 	if (new_posted == fetch_index) {
337 		pr_err("%s: devcmd2 wq full while issuing cmd %d\n",
338 				pci_name(vdev->pdev), _CMD_N(cmd));
339 		pr_err("%s: fetch index: %u, posted index: %u\n",
340 				pci_name(vdev->pdev), fetch_index, posted);
341 		return -EBUSY;
342 
343 	}
344 	dc2c->cmd_ring[posted].cmd = cmd;
345 	dc2c->cmd_ring[posted].flags = 0;
346 
347 	if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
348 		dc2c->cmd_ring[posted].flags |= DEVCMD2_FNORESULT;
349 	if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
350 		for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
351 			dc2c->cmd_ring[posted].args[i] = vdev->args[i];
352 
353 	}
354 
355 	/* Adding write memory barrier prevents compiler and/or CPU
356 	 * reordering, thus avoiding descriptor posting before
357 	 * descriptor is initialized. Otherwise, hardware can read
358 	 * stale descriptor fields.
359 	 */
360 	wmb();
361 	iowrite32(new_posted, &dc2c->wq_ctrl->posted_index);
362 
363 	if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT)
364 		return 0;
365 
366 	result = dc2c->result + dc2c->next_result;
367 	color = dc2c->color;
368 
369 	dc2c->next_result++;
370 	if (dc2c->next_result == dc2c->result_size) {
371 		dc2c->next_result = 0;
372 		dc2c->color = dc2c->color ? 0 : 1;
373 	}
374 
375 	for (delay = 0; delay < wait; delay++) {
376 		udelay(100);
377 		if (result->color == color) {
378 			if (result->error) {
379 				err = -(int) result->error;
380 				if (err != ERR_ECMDUNKNOWN ||
381 						cmd != CMD_CAPABILITY)
382 					pr_err("%s:Error %d devcmd %d\n",
383 						pci_name(vdev->pdev),
384 						err, _CMD_N(cmd));
385 				return err;
386 			}
387 			if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
388 				rmb(); /*prevent reorder while reding result*/
389 				for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
390 					vdev->args[i] = result->results[i];
391 			}
392 			return 0;
393 		}
394 	}
395 
396 	pr_err("%s:Timed out devcmd %d\n", pci_name(vdev->pdev), _CMD_N(cmd));
397 
398 	return -ETIMEDOUT;
399 }
400 
401 
402 static int vnic_dev_init_devcmd1(struct vnic_dev *vdev)
403 {
404 	vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
405 	if (!vdev->devcmd)
406 		return -ENODEV;
407 
408 	vdev->devcmd_rtn = &vnic_dev_cmd1;
409 	return 0;
410 }
411 
412 
413 static int vnic_dev_init_devcmd2(struct vnic_dev *vdev)
414 {
415 	int err;
416 	unsigned int fetch_index;
417 
418 	if (vdev->devcmd2)
419 		return 0;
420 
421 	vdev->devcmd2 = kzalloc(sizeof(*vdev->devcmd2), GFP_ATOMIC);
422 	if (!vdev->devcmd2)
423 		return -ENOMEM;
424 
425 	vdev->devcmd2->color = 1;
426 	vdev->devcmd2->result_size = DEVCMD2_RING_SIZE;
427 	err = vnic_wq_devcmd2_alloc(vdev, &vdev->devcmd2->wq,
428 				DEVCMD2_RING_SIZE, DEVCMD2_DESC_SIZE);
429 	if (err)
430 		goto err_free_devcmd2;
431 
432 	fetch_index = ioread32(&vdev->devcmd2->wq.ctrl->fetch_index);
433 	if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone  */
434 		pr_err("error in devcmd2 init");
435 		err = -ENODEV;
436 		goto err_free_wq;
437 	}
438 
439 	/*
440 	 * Don't change fetch_index ever and
441 	 * set posted_index same as fetch_index
442 	 * when setting up the WQ for devcmd2.
443 	 */
444 	vnic_wq_init_start(&vdev->devcmd2->wq, 0, fetch_index,
445 			fetch_index, 0, 0);
446 
447 	vnic_wq_enable(&vdev->devcmd2->wq);
448 
449 	err = vnic_dev_alloc_desc_ring(vdev, &vdev->devcmd2->results_ring,
450 			DEVCMD2_RING_SIZE, DEVCMD2_DESC_SIZE);
451 	if (err)
452 		goto err_disable_wq;
453 
454 	vdev->devcmd2->result =
455 		(struct devcmd2_result *) vdev->devcmd2->results_ring.descs;
456 	vdev->devcmd2->cmd_ring =
457 		(struct vnic_devcmd2 *) vdev->devcmd2->wq.ring.descs;
458 	vdev->devcmd2->wq_ctrl = vdev->devcmd2->wq.ctrl;
459 	vdev->args[0] = (u64) vdev->devcmd2->results_ring.base_addr |
460 				VNIC_PADDR_TARGET;
461 	vdev->args[1] = DEVCMD2_RING_SIZE;
462 
463 	err = vnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, 1000);
464 	if (err)
465 		goto err_free_desc_ring;
466 
467 	vdev->devcmd_rtn = &vnic_dev_cmd2;
468 
469 	return 0;
470 
471 err_free_desc_ring:
472 	vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring);
473 err_disable_wq:
474 	vnic_wq_disable(&vdev->devcmd2->wq);
475 err_free_wq:
476 	vnic_wq_free(&vdev->devcmd2->wq);
477 err_free_devcmd2:
478 	kfree(vdev->devcmd2);
479 	vdev->devcmd2 = NULL;
480 
481 	return err;
482 }
483 
484 
485 static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev)
486 {
487 	vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring);
488 	vnic_wq_disable(&vdev->devcmd2->wq);
489 	vnic_wq_free(&vdev->devcmd2->wq);
490 	kfree(vdev->devcmd2);
491 	vdev->devcmd2 = NULL;
492 	vdev->devcmd_rtn = &vnic_dev_cmd1;
493 }
494 
495 
496 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
497 	enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
498 {
499 	int err;
500 
501 	vdev->args[0] = *a0;
502 	vdev->args[1] = *a1;
503 
504 	err = (*vdev->devcmd_rtn)(vdev, cmd, wait);
505 
506 	*a0 = vdev->args[0];
507 	*a1 = vdev->args[1];
508 
509 	return err;
510 }
511 
512 
513 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
514 	u64 *a0, u64 *a1, int wait)
515 {
516 	memset(vdev->args, 0, sizeof(vdev->args));
517 
518 	switch (vdev->proxy) {
519 	case PROXY_NONE:
520 	default:
521 		return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
522 	}
523 }
524 
525 
526 int vnic_dev_fw_info(struct vnic_dev *vdev,
527 	struct vnic_devcmd_fw_info **fw_info)
528 {
529 	u64 a0, a1 = 0;
530 	int wait = 1000;
531 	int err = 0;
532 
533 	if (!vdev->fw_info) {
534 		vdev->fw_info = dma_alloc_coherent(&vdev->pdev->dev,
535 			sizeof(struct vnic_devcmd_fw_info),
536 			&vdev->fw_info_pa, GFP_KERNEL);
537 		if (!vdev->fw_info)
538 			return -ENOMEM;
539 
540 		a0 = vdev->fw_info_pa;
541 
542 		/* only get fw_info once and cache it */
543 		err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
544 	}
545 
546 	*fw_info = vdev->fw_info;
547 
548 	return err;
549 }
550 
551 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
552 	void *value)
553 {
554 	u64 a0, a1;
555 	int wait = 1000;
556 	int err;
557 
558 	a0 = offset;
559 	a1 = size;
560 
561 	err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
562 
563 	switch (size) {
564 	case 1:
565 		*(u8 *)value = (u8)a0;
566 		break;
567 	case 2:
568 		*(u16 *)value = (u16)a0;
569 		break;
570 	case 4:
571 		*(u32 *)value = (u32)a0;
572 		break;
573 	case 8:
574 		*(u64 *)value = a0;
575 		break;
576 	default:
577 		BUG();
578 		break;
579 	}
580 
581 	return err;
582 }
583 
584 int vnic_dev_stats_clear(struct vnic_dev *vdev)
585 {
586 	u64 a0 = 0, a1 = 0;
587 	int wait = 1000;
588 	return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
589 }
590 
591 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
592 {
593 	u64 a0, a1;
594 	int wait = 1000;
595 
596 	if (!vdev->stats) {
597 		vdev->stats = dma_alloc_coherent(&vdev->pdev->dev,
598 			sizeof(struct vnic_stats), &vdev->stats_pa, GFP_KERNEL);
599 		if (!vdev->stats)
600 			return -ENOMEM;
601 	}
602 
603 	*stats = vdev->stats;
604 	a0 = vdev->stats_pa;
605 	a1 = sizeof(struct vnic_stats);
606 
607 	return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
608 }
609 
610 int vnic_dev_close(struct vnic_dev *vdev)
611 {
612 	u64 a0 = 0, a1 = 0;
613 	int wait = 1000;
614 	return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
615 }
616 
617 int vnic_dev_enable(struct vnic_dev *vdev)
618 {
619 	u64 a0 = 0, a1 = 0;
620 	int wait = 1000;
621 	return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
622 }
623 
624 int vnic_dev_disable(struct vnic_dev *vdev)
625 {
626 	u64 a0 = 0, a1 = 0;
627 	int wait = 1000;
628 	return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
629 }
630 
631 int vnic_dev_open(struct vnic_dev *vdev, int arg)
632 {
633 	u64 a0 = (u32)arg, a1 = 0;
634 	int wait = 1000;
635 	return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
636 }
637 
638 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
639 {
640 	u64 a0 = 0, a1 = 0;
641 	int wait = 1000;
642 	int err;
643 
644 	*done = 0;
645 
646 	err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
647 	if (err)
648 		return err;
649 
650 	*done = (a0 == 0);
651 
652 	return 0;
653 }
654 
655 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
656 {
657 	u64 a0 = (u32)arg, a1 = 0;
658 	int wait = 1000;
659 	return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
660 }
661 
662 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
663 {
664 	u64 a0 = 0, a1 = 0;
665 	int wait = 1000;
666 	int err;
667 
668 	*done = 0;
669 
670 	err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
671 	if (err)
672 		return err;
673 
674 	*done = (a0 == 0);
675 
676 	return 0;
677 }
678 
679 int vnic_dev_hang_notify(struct vnic_dev *vdev)
680 {
681 	u64 a0 = 0, a1 = 0;
682 	int wait = 1000;
683 	return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
684 }
685 
686 int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
687 {
688 	u64 a[2] = {};
689 	int wait = 1000;
690 	int err, i;
691 
692 	for (i = 0; i < ETH_ALEN; i++)
693 		mac_addr[i] = 0;
694 
695 	err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a[0], &a[1], wait);
696 	if (err)
697 		return err;
698 
699 	for (i = 0; i < ETH_ALEN; i++)
700 		mac_addr[i] = ((u8 *)&a)[i];
701 
702 	return 0;
703 }
704 
705 void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
706 	int broadcast, int promisc, int allmulti)
707 {
708 	u64 a0, a1 = 0;
709 	int wait = 1000;
710 	int err;
711 
712 	a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
713 	     (multicast ? CMD_PFILTER_MULTICAST : 0) |
714 	     (broadcast ? CMD_PFILTER_BROADCAST : 0) |
715 	     (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
716 	     (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
717 
718 	err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
719 	if (err)
720 		printk(KERN_ERR "Can't set packet filter\n");
721 }
722 
723 void vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
724 {
725 	u64 a[2] = {};
726 	int wait = 1000;
727 	int err;
728 	int i;
729 
730 	for (i = 0; i < ETH_ALEN; i++)
731 		((u8 *)&a)[i] = addr[i];
732 
733 	err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a[0], &a[1], wait);
734 	if (err)
735 		pr_err("Can't add addr [%pM], %d\n", addr, err);
736 }
737 
738 void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
739 {
740 	u64 a[2] = {};
741 	int wait = 1000;
742 	int err;
743 	int i;
744 
745 	for (i = 0; i < ETH_ALEN; i++)
746 		((u8 *)&a)[i] = addr[i];
747 
748 	err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a[0], &a[1], wait);
749 	if (err)
750 		pr_err("Can't del addr [%pM], %d\n", addr, err);
751 }
752 
753 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
754 {
755 	u64 a0, a1;
756 	int wait = 1000;
757 
758 	if (!vdev->notify) {
759 		vdev->notify = dma_alloc_coherent(&vdev->pdev->dev,
760 			sizeof(struct vnic_devcmd_notify),
761 			&vdev->notify_pa, GFP_KERNEL);
762 		if (!vdev->notify)
763 			return -ENOMEM;
764 	}
765 
766 	a0 = vdev->notify_pa;
767 	a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
768 	a1 += sizeof(struct vnic_devcmd_notify);
769 
770 	return vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
771 }
772 
773 void vnic_dev_notify_unset(struct vnic_dev *vdev)
774 {
775 	u64 a0, a1;
776 	int wait = 1000;
777 
778 	a0 = 0;  /* paddr = 0 to unset notify buffer */
779 	a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
780 	a1 += sizeof(struct vnic_devcmd_notify);
781 
782 	vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
783 }
784 
785 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
786 {
787 	u32 *words;
788 	unsigned int nwords = sizeof(struct vnic_devcmd_notify) / 4;
789 	unsigned int i;
790 	u32 csum;
791 
792 	if (!vdev->notify)
793 		return 0;
794 
795 	do {
796 		csum = 0;
797 		memcpy(&vdev->notify_copy, vdev->notify,
798 			sizeof(struct vnic_devcmd_notify));
799 		words = (u32 *)&vdev->notify_copy;
800 		for (i = 1; i < nwords; i++)
801 			csum += words[i];
802 	} while (csum != words[0]);
803 
804 	return 1;
805 }
806 
807 int vnic_dev_init(struct vnic_dev *vdev, int arg)
808 {
809 	u64 a0 = (u32)arg, a1 = 0;
810 	int wait = 1000;
811 	return vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
812 }
813 
814 u16 vnic_dev_set_default_vlan(struct vnic_dev *vdev, u16 new_default_vlan)
815 {
816 	u64 a0 = new_default_vlan, a1 = 0;
817 	int wait = 1000;
818 	int old_vlan = 0;
819 
820 	old_vlan = vnic_dev_cmd(vdev, CMD_SET_DEFAULT_VLAN, &a0, &a1, wait);
821 	return (u16)old_vlan;
822 }
823 
824 int vnic_dev_link_status(struct vnic_dev *vdev)
825 {
826 	if (vdev->linkstatus)
827 		return *vdev->linkstatus;
828 
829 	if (!vnic_dev_notify_ready(vdev))
830 		return 0;
831 
832 	return vdev->notify_copy.link_state;
833 }
834 
835 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
836 {
837 	if (!vnic_dev_notify_ready(vdev))
838 		return 0;
839 
840 	return vdev->notify_copy.port_speed;
841 }
842 
843 u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
844 {
845 	if (!vnic_dev_notify_ready(vdev))
846 		return 0;
847 
848 	return vdev->notify_copy.msglvl;
849 }
850 
851 u32 vnic_dev_mtu(struct vnic_dev *vdev)
852 {
853 	if (!vnic_dev_notify_ready(vdev))
854 		return 0;
855 
856 	return vdev->notify_copy.mtu;
857 }
858 
859 u32 vnic_dev_link_down_cnt(struct vnic_dev *vdev)
860 {
861 	if (!vnic_dev_notify_ready(vdev))
862 		return 0;
863 
864 	return vdev->notify_copy.link_down_cnt;
865 }
866 
867 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
868 	enum vnic_dev_intr_mode intr_mode)
869 {
870 	vdev->intr_mode = intr_mode;
871 }
872 
873 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
874 	struct vnic_dev *vdev)
875 {
876 	return vdev->intr_mode;
877 }
878 
879 void vnic_dev_unregister(struct vnic_dev *vdev)
880 {
881 	if (vdev) {
882 		if (vdev->notify)
883 			dma_free_coherent(&vdev->pdev->dev,
884 				sizeof(struct vnic_devcmd_notify),
885 				vdev->notify,
886 				vdev->notify_pa);
887 		if (vdev->linkstatus)
888 			dma_free_coherent(&vdev->pdev->dev,
889 				sizeof(u32),
890 				vdev->linkstatus,
891 				vdev->linkstatus_pa);
892 		if (vdev->stats)
893 			dma_free_coherent(&vdev->pdev->dev,
894 				sizeof(struct vnic_stats),
895 				vdev->stats, vdev->stats_pa);
896 		if (vdev->fw_info)
897 			dma_free_coherent(&vdev->pdev->dev,
898 				sizeof(struct vnic_devcmd_fw_info),
899 				vdev->fw_info, vdev->fw_info_pa);
900 		if (vdev->devcmd2)
901 			vnic_dev_deinit_devcmd2(vdev);
902 		kfree(vdev);
903 	}
904 }
905 
906 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
907 	void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar)
908 {
909 	if (!vdev) {
910 		vdev = kzalloc(sizeof(struct vnic_dev), GFP_KERNEL);
911 		if (!vdev)
912 			return NULL;
913 	}
914 
915 	vdev->priv = priv;
916 	vdev->pdev = pdev;
917 
918 	if (vnic_dev_discover_res(vdev, bar))
919 		goto err_out;
920 
921 	return vdev;
922 
923 err_out:
924 	vnic_dev_unregister(vdev);
925 	return NULL;
926 }
927 
928 int vnic_dev_cmd_init(struct vnic_dev *vdev)
929 {
930 	int err;
931 	void *p;
932 
933 	p = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
934 	if (p) {
935 		pr_err("fnic: DEVCMD2 resource found!\n");
936 		err = vnic_dev_init_devcmd2(vdev);
937 	} else {
938 		pr_err("fnic: DEVCMD2 not found, fall back to Devcmd\n");
939 		err = vnic_dev_init_devcmd1(vdev);
940 	}
941 
942 	return err;
943 }
944