]>
Commit | Line | Data |
---|---|---|
01f2e4ea SF |
1 | /* |
2 | * Copyright 2008 Cisco Systems, Inc. All rights reserved. | |
3 | * Copyright 2007 Nuova Systems, Inc. All rights reserved. | |
4 | * | |
5 | * This program is free software; you may redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2 of the License. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
10 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
11 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
12 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
13 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
15 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
16 | * SOFTWARE. | |
17 | * | |
18 | */ | |
19 | ||
20 | #include <linux/kernel.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/types.h> | |
23 | #include <linux/pci.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/if_ether.h> | |
26 | ||
27 | #include "vnic_resource.h" | |
28 | #include "vnic_devcmd.h" | |
29 | #include "vnic_dev.h" | |
30 | #include "vnic_stats.h" | |
31 | ||
32 | struct vnic_res { | |
33 | void __iomem *vaddr; | |
27e6c7d3 | 34 | dma_addr_t bus_addr; |
01f2e4ea SF |
35 | unsigned int count; |
36 | }; | |
37 | ||
4cdc44a2 SF |
38 | #define VNIC_DEV_CAP_INIT 0x0001 |
39 | #define VNIC_DEV_CAP_PERBI 0x0002 | |
40 | ||
01f2e4ea SF |
41 | struct vnic_dev { |
42 | void *priv; | |
43 | struct pci_dev *pdev; | |
44 | struct vnic_res res[RES_TYPE_MAX]; | |
45 | enum vnic_dev_intr_mode intr_mode; | |
46 | struct vnic_devcmd __iomem *devcmd; | |
47 | struct vnic_devcmd_notify *notify; | |
48 | struct vnic_devcmd_notify notify_copy; | |
49 | dma_addr_t notify_pa; | |
27372bf5 | 50 | u32 notify_sz; |
01f2e4ea SF |
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; | |
4cdc44a2 | 57 | u32 cap_flags; |
01f2e4ea SF |
58 | }; |
59 | ||
60 | #define VNIC_MAX_RES_HDR_SIZE \ | |
61 | (sizeof(struct vnic_resource_header) + \ | |
62 | sizeof(struct vnic_resource) * RES_TYPE_MAX) | |
63 | #define VNIC_RES_STRIDE 128 | |
64 | ||
65 | void *vnic_dev_priv(struct vnic_dev *vdev) | |
66 | { | |
67 | return vdev->priv; | |
68 | } | |
69 | ||
70 | static int vnic_dev_discover_res(struct vnic_dev *vdev, | |
27e6c7d3 | 71 | struct vnic_dev_bar *bar, unsigned int num_bars) |
01f2e4ea SF |
72 | { |
73 | struct vnic_resource_header __iomem *rh; | |
74 | struct vnic_resource __iomem *r; | |
75 | u8 type; | |
76 | ||
27e6c7d3 SF |
77 | if (num_bars == 0) |
78 | return -EINVAL; | |
79 | ||
01f2e4ea SF |
80 | if (bar->len < VNIC_MAX_RES_HDR_SIZE) { |
81 | printk(KERN_ERR "vNIC BAR0 res hdr length error\n"); | |
82 | return -EINVAL; | |
83 | } | |
84 | ||
85 | rh = bar->vaddr; | |
86 | if (!rh) { | |
87 | printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n"); | |
88 | return -EINVAL; | |
89 | } | |
90 | ||
91 | if (ioread32(&rh->magic) != VNIC_RES_MAGIC || | |
92 | ioread32(&rh->version) != VNIC_RES_VERSION) { | |
93 | printk(KERN_ERR "vNIC BAR0 res magic/version error " | |
94 | "exp (%lx/%lx) curr (%x/%x)\n", | |
95 | VNIC_RES_MAGIC, VNIC_RES_VERSION, | |
96 | ioread32(&rh->magic), ioread32(&rh->version)); | |
97 | return -EINVAL; | |
98 | } | |
99 | ||
100 | r = (struct vnic_resource __iomem *)(rh + 1); | |
101 | ||
102 | while ((type = ioread8(&r->type)) != RES_TYPE_EOL) { | |
103 | ||
104 | u8 bar_num = ioread8(&r->bar); | |
105 | u32 bar_offset = ioread32(&r->bar_offset); | |
106 | u32 count = ioread32(&r->count); | |
107 | u32 len; | |
108 | ||
109 | r++; | |
110 | ||
27e6c7d3 SF |
111 | if (bar_num >= num_bars) |
112 | continue; | |
113 | ||
114 | if (!bar[bar_num].len || !bar[bar_num].vaddr) | |
01f2e4ea SF |
115 | continue; |
116 | ||
117 | switch (type) { | |
118 | case RES_TYPE_WQ: | |
119 | case RES_TYPE_RQ: | |
120 | case RES_TYPE_CQ: | |
121 | case RES_TYPE_INTR_CTRL: | |
122 | /* each count is stride bytes long */ | |
123 | len = count * VNIC_RES_STRIDE; | |
27e6c7d3 | 124 | if (len + bar_offset > bar[bar_num].len) { |
01f2e4ea SF |
125 | printk(KERN_ERR "vNIC BAR0 resource %d " |
126 | "out-of-bounds, offset 0x%x + " | |
127 | "size 0x%x > bar len 0x%lx\n", | |
128 | type, bar_offset, | |
129 | len, | |
27e6c7d3 | 130 | bar[bar_num].len); |
01f2e4ea SF |
131 | return -EINVAL; |
132 | } | |
133 | break; | |
134 | case RES_TYPE_INTR_PBA_LEGACY: | |
135 | case RES_TYPE_DEVCMD: | |
136 | len = count; | |
137 | break; | |
138 | default: | |
139 | continue; | |
140 | } | |
141 | ||
142 | vdev->res[type].count = count; | |
27e6c7d3 SF |
143 | vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr + |
144 | bar_offset; | |
145 | vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset; | |
01f2e4ea SF |
146 | } |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev, | |
152 | enum vnic_res_type type) | |
153 | { | |
154 | return vdev->res[type].count; | |
155 | } | |
156 | ||
157 | void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type, | |
158 | unsigned int index) | |
159 | { | |
160 | if (!vdev->res[type].vaddr) | |
161 | return NULL; | |
162 | ||
163 | switch (type) { | |
164 | case RES_TYPE_WQ: | |
165 | case RES_TYPE_RQ: | |
166 | case RES_TYPE_CQ: | |
167 | case RES_TYPE_INTR_CTRL: | |
168 | return (char __iomem *)vdev->res[type].vaddr + | |
169 | index * VNIC_RES_STRIDE; | |
170 | default: | |
171 | return (char __iomem *)vdev->res[type].vaddr; | |
172 | } | |
173 | } | |
174 | ||
27e6c7d3 SF |
175 | dma_addr_t vnic_dev_get_res_bus_addr(struct vnic_dev *vdev, |
176 | enum vnic_res_type type, unsigned int index) | |
177 | { | |
178 | switch (type) { | |
179 | case RES_TYPE_WQ: | |
180 | case RES_TYPE_RQ: | |
181 | case RES_TYPE_CQ: | |
182 | case RES_TYPE_INTR_CTRL: | |
183 | return vdev->res[type].bus_addr + | |
184 | index * VNIC_RES_STRIDE; | |
185 | default: | |
186 | return vdev->res[type].bus_addr; | |
187 | } | |
188 | } | |
189 | ||
01f2e4ea SF |
190 | unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring, |
191 | unsigned int desc_count, unsigned int desc_size) | |
192 | { | |
193 | /* The base address of the desc rings must be 512 byte aligned. | |
194 | * Descriptor count is aligned to groups of 32 descriptors. A | |
195 | * count of 0 means the maximum 4096 descriptors. Descriptor | |
196 | * size is aligned to 16 bytes. | |
197 | */ | |
198 | ||
199 | unsigned int count_align = 32; | |
200 | unsigned int desc_align = 16; | |
201 | ||
202 | ring->base_align = 512; | |
203 | ||
204 | if (desc_count == 0) | |
205 | desc_count = 4096; | |
206 | ||
207 | ring->desc_count = ALIGN(desc_count, count_align); | |
208 | ||
209 | ring->desc_size = ALIGN(desc_size, desc_align); | |
210 | ||
211 | ring->size = ring->desc_count * ring->desc_size; | |
212 | ring->size_unaligned = ring->size + ring->base_align; | |
213 | ||
214 | return ring->size_unaligned; | |
215 | } | |
216 | ||
217 | void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring) | |
218 | { | |
219 | memset(ring->descs, 0, ring->size); | |
220 | } | |
221 | ||
222 | int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring, | |
223 | unsigned int desc_count, unsigned int desc_size) | |
224 | { | |
225 | vnic_dev_desc_ring_size(ring, desc_count, desc_size); | |
226 | ||
227 | ring->descs_unaligned = pci_alloc_consistent(vdev->pdev, | |
228 | ring->size_unaligned, | |
229 | &ring->base_addr_unaligned); | |
230 | ||
231 | if (!ring->descs_unaligned) { | |
232 | printk(KERN_ERR | |
233 | "Failed to allocate ring (size=%d), aborting\n", | |
234 | (int)ring->size); | |
235 | return -ENOMEM; | |
236 | } | |
237 | ||
238 | ring->base_addr = ALIGN(ring->base_addr_unaligned, | |
239 | ring->base_align); | |
240 | ring->descs = (u8 *)ring->descs_unaligned + | |
241 | (ring->base_addr - ring->base_addr_unaligned); | |
242 | ||
243 | vnic_dev_clear_desc_ring(ring); | |
244 | ||
245 | ring->desc_avail = ring->desc_count - 1; | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring) | |
251 | { | |
252 | if (ring->descs) { | |
253 | pci_free_consistent(vdev->pdev, | |
254 | ring->size_unaligned, | |
255 | ring->descs_unaligned, | |
256 | ring->base_addr_unaligned); | |
257 | ring->descs = NULL; | |
258 | } | |
259 | } | |
260 | ||
261 | int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, | |
262 | u64 *a0, u64 *a1, int wait) | |
263 | { | |
264 | struct vnic_devcmd __iomem *devcmd = vdev->devcmd; | |
265 | int delay; | |
266 | u32 status; | |
01f2e4ea SF |
267 | int err; |
268 | ||
269 | status = ioread32(&devcmd->status); | |
270 | if (status & STAT_BUSY) { | |
271 | printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd)); | |
272 | return -EBUSY; | |
273 | } | |
274 | ||
275 | if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { | |
276 | writeq(*a0, &devcmd->args[0]); | |
277 | writeq(*a1, &devcmd->args[1]); | |
278 | wmb(); | |
279 | } | |
280 | ||
281 | iowrite32(cmd, &devcmd->cmd); | |
282 | ||
283 | if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) | |
27e6c7d3 | 284 | return 0; |
01f2e4ea SF |
285 | |
286 | for (delay = 0; delay < wait; delay++) { | |
287 | ||
288 | udelay(100); | |
289 | ||
290 | status = ioread32(&devcmd->status); | |
291 | if (!(status & STAT_BUSY)) { | |
292 | ||
293 | if (status & STAT_ERROR) { | |
27372bf5 SF |
294 | err = (int)readq(&devcmd->args[0]); |
295 | if (err != ERR_ECMDUNKNOWN || | |
296 | cmd != CMD_CAPABILITY) | |
297 | printk(KERN_ERR "Error %d devcmd %d\n", | |
298 | err, _CMD_N(cmd)); | |
299 | return err; | |
01f2e4ea SF |
300 | } |
301 | ||
302 | if (_CMD_DIR(cmd) & _CMD_DIR_READ) { | |
303 | rmb(); | |
304 | *a0 = readq(&devcmd->args[0]); | |
305 | *a1 = readq(&devcmd->args[1]); | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | } | |
311 | ||
312 | printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd)); | |
313 | return -ETIMEDOUT; | |
314 | } | |
315 | ||
5e4232ee | 316 | static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) |
27372bf5 SF |
317 | { |
318 | u64 a0 = (u32)cmd, a1 = 0; | |
319 | int wait = 1000; | |
320 | int err; | |
321 | ||
322 | err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); | |
323 | ||
324 | return !(err || a0); | |
325 | } | |
326 | ||
01f2e4ea SF |
327 | int vnic_dev_fw_info(struct vnic_dev *vdev, |
328 | struct vnic_devcmd_fw_info **fw_info) | |
329 | { | |
330 | u64 a0, a1 = 0; | |
331 | int wait = 1000; | |
332 | int err = 0; | |
333 | ||
334 | if (!vdev->fw_info) { | |
335 | vdev->fw_info = pci_alloc_consistent(vdev->pdev, | |
336 | sizeof(struct vnic_devcmd_fw_info), | |
337 | &vdev->fw_info_pa); | |
338 | if (!vdev->fw_info) | |
339 | return -ENOMEM; | |
340 | ||
341 | a0 = vdev->fw_info_pa; | |
342 | ||
343 | /* only get fw_info once and cache it */ | |
344 | err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait); | |
345 | } | |
346 | ||
347 | *fw_info = vdev->fw_info; | |
348 | ||
349 | return err; | |
350 | } | |
351 | ||
352 | int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size, | |
353 | void *value) | |
354 | { | |
355 | u64 a0, a1; | |
356 | int wait = 1000; | |
357 | int err; | |
358 | ||
359 | a0 = offset; | |
360 | a1 = size; | |
361 | ||
362 | err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); | |
363 | ||
364 | switch (size) { | |
365 | case 1: *(u8 *)value = (u8)a0; break; | |
366 | case 2: *(u16 *)value = (u16)a0; break; | |
367 | case 4: *(u32 *)value = (u32)a0; break; | |
368 | case 8: *(u64 *)value = a0; break; | |
369 | default: BUG(); break; | |
370 | } | |
371 | ||
372 | return err; | |
373 | } | |
374 | ||
375 | int vnic_dev_stats_clear(struct vnic_dev *vdev) | |
376 | { | |
377 | u64 a0 = 0, a1 = 0; | |
378 | int wait = 1000; | |
379 | return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait); | |
380 | } | |
381 | ||
382 | int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) | |
383 | { | |
384 | u64 a0, a1; | |
385 | int wait = 1000; | |
386 | ||
387 | if (!vdev->stats) { | |
388 | vdev->stats = pci_alloc_consistent(vdev->pdev, | |
389 | sizeof(struct vnic_stats), &vdev->stats_pa); | |
390 | if (!vdev->stats) | |
391 | return -ENOMEM; | |
392 | } | |
393 | ||
394 | *stats = vdev->stats; | |
395 | a0 = vdev->stats_pa; | |
396 | a1 = sizeof(struct vnic_stats); | |
397 | ||
398 | return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); | |
399 | } | |
400 | ||
401 | int vnic_dev_close(struct vnic_dev *vdev) | |
402 | { | |
403 | u64 a0 = 0, a1 = 0; | |
404 | int wait = 1000; | |
405 | return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); | |
406 | } | |
407 | ||
408 | int vnic_dev_enable(struct vnic_dev *vdev) | |
409 | { | |
410 | u64 a0 = 0, a1 = 0; | |
411 | int wait = 1000; | |
412 | return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); | |
413 | } | |
414 | ||
415 | int vnic_dev_disable(struct vnic_dev *vdev) | |
416 | { | |
417 | u64 a0 = 0, a1 = 0; | |
418 | int wait = 1000; | |
419 | return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); | |
420 | } | |
421 | ||
422 | int vnic_dev_open(struct vnic_dev *vdev, int arg) | |
423 | { | |
424 | u64 a0 = (u32)arg, a1 = 0; | |
425 | int wait = 1000; | |
426 | return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); | |
427 | } | |
428 | ||
429 | int vnic_dev_open_done(struct vnic_dev *vdev, int *done) | |
430 | { | |
431 | u64 a0 = 0, a1 = 0; | |
432 | int wait = 1000; | |
433 | int err; | |
434 | ||
435 | *done = 0; | |
436 | ||
437 | err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); | |
438 | if (err) | |
439 | return err; | |
440 | ||
441 | *done = (a0 == 0); | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) | |
447 | { | |
448 | u64 a0 = (u32)arg, a1 = 0; | |
449 | int wait = 1000; | |
450 | return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); | |
451 | } | |
452 | ||
453 | int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) | |
454 | { | |
455 | u64 a0 = 0, a1 = 0; | |
456 | int wait = 1000; | |
457 | int err; | |
458 | ||
459 | *done = 0; | |
460 | ||
461 | err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); | |
462 | if (err) | |
463 | return err; | |
464 | ||
465 | *done = (a0 == 0); | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | int vnic_dev_hang_notify(struct vnic_dev *vdev) | |
471 | { | |
472 | u64 a0, a1; | |
473 | int wait = 1000; | |
474 | return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait); | |
475 | } | |
476 | ||
477 | int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) | |
478 | { | |
479 | u64 a0, a1; | |
480 | int wait = 1000; | |
481 | int err, i; | |
482 | ||
483 | for (i = 0; i < ETH_ALEN; i++) | |
484 | mac_addr[i] = 0; | |
485 | ||
486 | err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); | |
487 | if (err) | |
488 | return err; | |
489 | ||
490 | for (i = 0; i < ETH_ALEN; i++) | |
491 | mac_addr[i] = ((u8 *)&a0)[i]; | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, | |
497 | int broadcast, int promisc, int allmulti) | |
498 | { | |
499 | u64 a0, a1 = 0; | |
500 | int wait = 1000; | |
501 | int err; | |
502 | ||
503 | a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | | |
504 | (multicast ? CMD_PFILTER_MULTICAST : 0) | | |
505 | (broadcast ? CMD_PFILTER_BROADCAST : 0) | | |
506 | (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | | |
507 | (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); | |
508 | ||
509 | err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); | |
510 | if (err) | |
511 | printk(KERN_ERR "Can't set packet filter\n"); | |
512 | } | |
513 | ||
514 | void vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr) | |
515 | { | |
516 | u64 a0 = 0, a1 = 0; | |
517 | int wait = 1000; | |
518 | int err; | |
519 | int i; | |
520 | ||
521 | for (i = 0; i < ETH_ALEN; i++) | |
522 | ((u8 *)&a0)[i] = addr[i]; | |
523 | ||
524 | err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); | |
525 | if (err) | |
7c510e4b | 526 | printk(KERN_ERR "Can't add addr [%pM], %d\n", addr, err); |
01f2e4ea SF |
527 | } |
528 | ||
529 | void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr) | |
530 | { | |
531 | u64 a0 = 0, a1 = 0; | |
532 | int wait = 1000; | |
533 | int err; | |
534 | int i; | |
535 | ||
536 | for (i = 0; i < ETH_ALEN; i++) | |
537 | ((u8 *)&a0)[i] = addr[i]; | |
538 | ||
539 | err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); | |
540 | if (err) | |
7c510e4b | 541 | printk(KERN_ERR "Can't del addr [%pM], %d\n", addr, err); |
01f2e4ea SF |
542 | } |
543 | ||
544 | int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) | |
545 | { | |
546 | u64 a0, a1; | |
547 | int wait = 1000; | |
27372bf5 | 548 | int r; |
01f2e4ea SF |
549 | |
550 | if (!vdev->notify) { | |
551 | vdev->notify = pci_alloc_consistent(vdev->pdev, | |
552 | sizeof(struct vnic_devcmd_notify), | |
553 | &vdev->notify_pa); | |
554 | if (!vdev->notify) | |
555 | return -ENOMEM; | |
27372bf5 | 556 | memset(vdev->notify, 0, sizeof(struct vnic_devcmd_notify)); |
01f2e4ea SF |
557 | } |
558 | ||
559 | a0 = vdev->notify_pa; | |
560 | a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; | |
561 | a1 += sizeof(struct vnic_devcmd_notify); | |
562 | ||
27372bf5 SF |
563 | r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); |
564 | vdev->notify_sz = (r == 0) ? (u32)a1 : 0; | |
565 | return r; | |
01f2e4ea SF |
566 | } |
567 | ||
568 | void vnic_dev_notify_unset(struct vnic_dev *vdev) | |
569 | { | |
570 | u64 a0, a1; | |
571 | int wait = 1000; | |
572 | ||
573 | a0 = 0; /* paddr = 0 to unset notify buffer */ | |
574 | a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ | |
575 | a1 += sizeof(struct vnic_devcmd_notify); | |
576 | ||
577 | vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); | |
27372bf5 | 578 | vdev->notify_sz = 0; |
01f2e4ea SF |
579 | } |
580 | ||
581 | static int vnic_dev_notify_ready(struct vnic_dev *vdev) | |
582 | { | |
583 | u32 *words; | |
27372bf5 | 584 | unsigned int nwords = vdev->notify_sz / 4; |
01f2e4ea SF |
585 | unsigned int i; |
586 | u32 csum; | |
587 | ||
27372bf5 | 588 | if (!vdev->notify || !vdev->notify_sz) |
01f2e4ea SF |
589 | return 0; |
590 | ||
591 | do { | |
592 | csum = 0; | |
27372bf5 | 593 | memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); |
01f2e4ea SF |
594 | words = (u32 *)&vdev->notify_copy; |
595 | for (i = 1; i < nwords; i++) | |
596 | csum += words[i]; | |
597 | } while (csum != words[0]); | |
598 | ||
599 | return 1; | |
600 | } | |
601 | ||
602 | int vnic_dev_init(struct vnic_dev *vdev, int arg) | |
603 | { | |
604 | u64 a0 = (u32)arg, a1 = 0; | |
605 | int wait = 1000; | |
4cdc44a2 | 606 | int r = 0; |
27372bf5 | 607 | |
4cdc44a2 | 608 | if (vdev->cap_flags & VNIC_DEV_CAP_INIT) |
27372bf5 SF |
609 | r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); |
610 | else { | |
611 | vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); | |
612 | if (a0 & CMD_INITF_DEFAULT_MAC) { | |
613 | // Emulate these for old CMD_INIT_v1 which | |
614 | // didn't pass a0 so no CMD_INITF_*. | |
615 | vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); | |
616 | vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); | |
617 | } | |
4cdc44a2 SF |
618 | } |
619 | return r; | |
01f2e4ea SF |
620 | } |
621 | ||
622 | int vnic_dev_link_status(struct vnic_dev *vdev) | |
623 | { | |
624 | if (vdev->linkstatus) | |
625 | return *vdev->linkstatus; | |
626 | ||
627 | if (!vnic_dev_notify_ready(vdev)) | |
628 | return 0; | |
629 | ||
630 | return vdev->notify_copy.link_state; | |
631 | } | |
632 | ||
633 | u32 vnic_dev_port_speed(struct vnic_dev *vdev) | |
634 | { | |
635 | if (!vnic_dev_notify_ready(vdev)) | |
636 | return 0; | |
637 | ||
638 | return vdev->notify_copy.port_speed; | |
639 | } | |
640 | ||
641 | u32 vnic_dev_msg_lvl(struct vnic_dev *vdev) | |
642 | { | |
643 | if (!vnic_dev_notify_ready(vdev)) | |
644 | return 0; | |
645 | ||
646 | return vdev->notify_copy.msglvl; | |
647 | } | |
648 | ||
649 | u32 vnic_dev_mtu(struct vnic_dev *vdev) | |
650 | { | |
651 | if (!vnic_dev_notify_ready(vdev)) | |
652 | return 0; | |
653 | ||
654 | return vdev->notify_copy.mtu; | |
655 | } | |
656 | ||
4cdc44a2 SF |
657 | u32 vnic_dev_link_down_cnt(struct vnic_dev *vdev) |
658 | { | |
659 | if (!vnic_dev_notify_ready(vdev)) | |
660 | return 0; | |
661 | ||
662 | return vdev->notify_copy.link_down_cnt; | |
663 | } | |
664 | ||
665 | u32 vnic_dev_notify_status(struct vnic_dev *vdev) | |
666 | { | |
667 | if (!vnic_dev_notify_ready(vdev)) | |
668 | return 0; | |
669 | ||
670 | return vdev->notify_copy.status; | |
671 | } | |
672 | ||
01f2e4ea SF |
673 | void vnic_dev_set_intr_mode(struct vnic_dev *vdev, |
674 | enum vnic_dev_intr_mode intr_mode) | |
675 | { | |
676 | vdev->intr_mode = intr_mode; | |
677 | } | |
678 | ||
679 | enum vnic_dev_intr_mode vnic_dev_get_intr_mode( | |
680 | struct vnic_dev *vdev) | |
681 | { | |
682 | return vdev->intr_mode; | |
683 | } | |
684 | ||
685 | void vnic_dev_unregister(struct vnic_dev *vdev) | |
686 | { | |
687 | if (vdev) { | |
688 | if (vdev->notify) | |
689 | pci_free_consistent(vdev->pdev, | |
690 | sizeof(struct vnic_devcmd_notify), | |
691 | vdev->notify, | |
692 | vdev->notify_pa); | |
693 | if (vdev->linkstatus) | |
694 | pci_free_consistent(vdev->pdev, | |
695 | sizeof(u32), | |
696 | vdev->linkstatus, | |
697 | vdev->linkstatus_pa); | |
698 | if (vdev->stats) | |
699 | pci_free_consistent(vdev->pdev, | |
700 | sizeof(struct vnic_dev), | |
701 | vdev->stats, vdev->stats_pa); | |
702 | if (vdev->fw_info) | |
703 | pci_free_consistent(vdev->pdev, | |
704 | sizeof(struct vnic_devcmd_fw_info), | |
705 | vdev->fw_info, vdev->fw_info_pa); | |
706 | kfree(vdev); | |
707 | } | |
708 | } | |
709 | ||
710 | struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, | |
27e6c7d3 SF |
711 | void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar, |
712 | unsigned int num_bars) | |
01f2e4ea SF |
713 | { |
714 | if (!vdev) { | |
715 | vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC); | |
716 | if (!vdev) | |
717 | return NULL; | |
718 | } | |
719 | ||
720 | vdev->priv = priv; | |
721 | vdev->pdev = pdev; | |
722 | ||
27e6c7d3 | 723 | if (vnic_dev_discover_res(vdev, bar, num_bars)) |
01f2e4ea SF |
724 | goto err_out; |
725 | ||
726 | vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); | |
727 | if (!vdev->devcmd) | |
728 | goto err_out; | |
729 | ||
4cdc44a2 SF |
730 | vdev->cap_flags = 0; |
731 | ||
732 | if (vnic_dev_capable(vdev, CMD_INIT)) | |
733 | vdev->cap_flags |= VNIC_DEV_CAP_INIT; | |
734 | ||
01f2e4ea SF |
735 | return vdev; |
736 | ||
737 | err_out: | |
738 | vnic_dev_unregister(vdev); | |
739 | return NULL; | |
740 | } | |
741 | ||
27372bf5 | 742 |