lib/ratelimit.c: use deferred printk() version
[linux-stable.git] / net / ncsi / ncsi-rsp.c
blob265b9a892d4171bc74572891edb631dabac98c41
1 /*
2 * Copyright Gavin Shan, IBM Corporation 2016.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
16 #include <net/ncsi.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
20 #include "internal.h"
21 #include "ncsi-pkt.h"
23 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
24 unsigned short payload)
26 struct ncsi_rsp_pkt_hdr *h;
27 u32 checksum;
28 __be32 *pchecksum;
30 /* Check NCSI packet header. We don't need validate
31 * the packet type, which should have been checked
32 * before calling this function.
34 h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
35 if (h->common.revision != NCSI_PKT_REVISION)
36 return -EINVAL;
37 if (ntohs(h->common.length) != payload)
38 return -EINVAL;
40 /* Check on code and reason */
41 if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
42 ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR)
43 return -EINVAL;
45 /* Validate checksum, which might be zeroes if the
46 * sender doesn't support checksum according to NCSI
47 * specification.
49 pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
50 if (ntohl(*pchecksum) == 0)
51 return 0;
53 checksum = ncsi_calculate_checksum((unsigned char *)h,
54 sizeof(*h) + payload - 4);
55 if (*pchecksum != htonl(checksum))
56 return -EINVAL;
58 return 0;
61 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
63 struct ncsi_rsp_pkt *rsp;
64 struct ncsi_dev_priv *ndp = nr->ndp;
65 struct ncsi_package *np;
66 struct ncsi_channel *nc;
67 unsigned char id;
69 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
70 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
71 if (!nc) {
72 if (ndp->flags & NCSI_DEV_PROBED)
73 return -ENXIO;
75 id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
76 nc = ncsi_add_channel(np, id);
79 return nc ? 0 : -ENODEV;
82 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
84 struct ncsi_rsp_pkt *rsp;
85 struct ncsi_dev_priv *ndp = nr->ndp;
86 struct ncsi_package *np;
87 unsigned char id;
89 /* Add the package if it's not existing. Otherwise,
90 * to change the state of its child channels.
92 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
93 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
94 &np, NULL);
95 if (!np) {
96 if (ndp->flags & NCSI_DEV_PROBED)
97 return -ENXIO;
99 id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
100 np = ncsi_add_package(ndp, id);
101 if (!np)
102 return -ENODEV;
105 return 0;
108 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
110 struct ncsi_rsp_pkt *rsp;
111 struct ncsi_dev_priv *ndp = nr->ndp;
112 struct ncsi_package *np;
113 struct ncsi_channel *nc;
114 unsigned long flags;
116 /* Find the package */
117 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
118 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
119 &np, NULL);
120 if (!np)
121 return -ENODEV;
123 /* Change state of all channels attached to the package */
124 NCSI_FOR_EACH_CHANNEL(np, nc) {
125 spin_lock_irqsave(&nc->lock, flags);
126 nc->state = NCSI_CHANNEL_INACTIVE;
127 spin_unlock_irqrestore(&nc->lock, flags);
130 return 0;
133 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
135 struct ncsi_rsp_pkt *rsp;
136 struct ncsi_dev_priv *ndp = nr->ndp;
137 struct ncsi_channel *nc;
138 struct ncsi_channel_mode *ncm;
140 /* Find the package and channel */
141 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
142 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
143 NULL, &nc);
144 if (!nc)
145 return -ENODEV;
147 ncm = &nc->modes[NCSI_MODE_ENABLE];
148 if (ncm->enable)
149 return -EBUSY;
151 ncm->enable = 1;
152 return 0;
155 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
157 struct ncsi_rsp_pkt *rsp;
158 struct ncsi_dev_priv *ndp = nr->ndp;
159 struct ncsi_channel *nc;
160 struct ncsi_channel_mode *ncm;
161 int ret;
163 ret = ncsi_validate_rsp_pkt(nr, 4);
164 if (ret)
165 return ret;
167 /* Find the package and channel */
168 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
169 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
170 NULL, &nc);
171 if (!nc)
172 return -ENODEV;
174 ncm = &nc->modes[NCSI_MODE_ENABLE];
175 if (!ncm->enable)
176 return -EBUSY;
178 ncm->enable = 0;
179 return 0;
182 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
184 struct ncsi_rsp_pkt *rsp;
185 struct ncsi_dev_priv *ndp = nr->ndp;
186 struct ncsi_channel *nc;
187 unsigned long flags;
189 /* Find the package and channel */
190 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
191 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
192 NULL, &nc);
193 if (!nc)
194 return -ENODEV;
196 /* Update state for the specified channel */
197 spin_lock_irqsave(&nc->lock, flags);
198 nc->state = NCSI_CHANNEL_INACTIVE;
199 spin_unlock_irqrestore(&nc->lock, flags);
201 return 0;
204 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
206 struct ncsi_rsp_pkt *rsp;
207 struct ncsi_dev_priv *ndp = nr->ndp;
208 struct ncsi_channel *nc;
209 struct ncsi_channel_mode *ncm;
211 /* Find the package and channel */
212 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
213 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
214 NULL, &nc);
215 if (!nc)
216 return -ENODEV;
218 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
219 if (ncm->enable)
220 return -EBUSY;
222 ncm->enable = 1;
223 return 0;
226 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
228 struct ncsi_rsp_pkt *rsp;
229 struct ncsi_dev_priv *ndp = nr->ndp;
230 struct ncsi_channel *nc;
231 struct ncsi_channel_mode *ncm;
233 /* Find the package and channel */
234 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
235 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
236 NULL, &nc);
237 if (!nc)
238 return -ENODEV;
240 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
241 if (!ncm->enable)
242 return -EBUSY;
244 ncm->enable = 1;
245 return 0;
248 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
250 struct ncsi_cmd_ae_pkt *cmd;
251 struct ncsi_rsp_pkt *rsp;
252 struct ncsi_dev_priv *ndp = nr->ndp;
253 struct ncsi_channel *nc;
254 struct ncsi_channel_mode *ncm;
256 /* Find the package and channel */
257 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
258 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
259 NULL, &nc);
260 if (!nc)
261 return -ENODEV;
263 /* Check if the AEN has been enabled */
264 ncm = &nc->modes[NCSI_MODE_AEN];
265 if (ncm->enable)
266 return -EBUSY;
268 /* Update to AEN configuration */
269 cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
270 ncm->enable = 1;
271 ncm->data[0] = cmd->mc_id;
272 ncm->data[1] = ntohl(cmd->mode);
274 return 0;
277 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
279 struct ncsi_cmd_sl_pkt *cmd;
280 struct ncsi_rsp_pkt *rsp;
281 struct ncsi_dev_priv *ndp = nr->ndp;
282 struct ncsi_channel *nc;
283 struct ncsi_channel_mode *ncm;
285 /* Find the package and channel */
286 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
287 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
288 NULL, &nc);
289 if (!nc)
290 return -ENODEV;
292 cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
293 ncm = &nc->modes[NCSI_MODE_LINK];
294 ncm->data[0] = ntohl(cmd->mode);
295 ncm->data[1] = ntohl(cmd->oem_mode);
297 return 0;
300 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
302 struct ncsi_rsp_gls_pkt *rsp;
303 struct ncsi_dev_priv *ndp = nr->ndp;
304 struct ncsi_channel *nc;
305 struct ncsi_channel_mode *ncm;
306 unsigned long flags;
308 /* Find the package and channel */
309 rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
310 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
311 NULL, &nc);
312 if (!nc)
313 return -ENODEV;
315 ncm = &nc->modes[NCSI_MODE_LINK];
316 ncm->data[2] = ntohl(rsp->status);
317 ncm->data[3] = ntohl(rsp->other);
318 ncm->data[4] = ntohl(rsp->oem_status);
320 if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
321 return 0;
323 /* Reset the channel monitor if it has been enabled */
324 spin_lock_irqsave(&nc->lock, flags);
325 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
326 spin_unlock_irqrestore(&nc->lock, flags);
328 return 0;
331 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
333 struct ncsi_cmd_svf_pkt *cmd;
334 struct ncsi_rsp_pkt *rsp;
335 struct ncsi_dev_priv *ndp = nr->ndp;
336 struct ncsi_channel *nc;
337 struct ncsi_channel_filter *ncf;
338 unsigned short vlan;
339 int ret;
341 /* Find the package and channel */
342 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
343 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
344 NULL, &nc);
345 if (!nc)
346 return -ENODEV;
348 cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
349 ncf = nc->filters[NCSI_FILTER_VLAN];
350 if (!ncf)
351 return -ENOENT;
352 if (cmd->index >= ncf->total)
353 return -ERANGE;
355 /* Add or remove the VLAN filter */
356 if (!(cmd->enable & 0x1)) {
357 /* HW indexes from 1 */
358 ret = ncsi_remove_filter(nc, NCSI_FILTER_VLAN, cmd->index - 1);
359 } else {
360 vlan = ntohs(cmd->vlan);
361 ret = ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan);
364 return ret;
367 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
369 struct ncsi_cmd_ev_pkt *cmd;
370 struct ncsi_rsp_pkt *rsp;
371 struct ncsi_dev_priv *ndp = nr->ndp;
372 struct ncsi_channel *nc;
373 struct ncsi_channel_mode *ncm;
375 /* Find the package and channel */
376 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
377 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
378 NULL, &nc);
379 if (!nc)
380 return -ENODEV;
382 /* Check if VLAN mode has been enabled */
383 ncm = &nc->modes[NCSI_MODE_VLAN];
384 if (ncm->enable)
385 return -EBUSY;
387 /* Update to VLAN mode */
388 cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
389 ncm->enable = 1;
390 ncm->data[0] = ntohl(cmd->mode);
392 return 0;
395 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
397 struct ncsi_rsp_pkt *rsp;
398 struct ncsi_dev_priv *ndp = nr->ndp;
399 struct ncsi_channel *nc;
400 struct ncsi_channel_mode *ncm;
402 /* Find the package and channel */
403 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
404 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
405 NULL, &nc);
406 if (!nc)
407 return -ENODEV;
409 /* Check if VLAN mode has been enabled */
410 ncm = &nc->modes[NCSI_MODE_VLAN];
411 if (!ncm->enable)
412 return -EBUSY;
414 /* Update to VLAN mode */
415 ncm->enable = 0;
416 return 0;
419 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
421 struct ncsi_cmd_sma_pkt *cmd;
422 struct ncsi_rsp_pkt *rsp;
423 struct ncsi_dev_priv *ndp = nr->ndp;
424 struct ncsi_channel *nc;
425 struct ncsi_channel_filter *ncf;
426 void *bitmap;
428 /* Find the package and channel */
429 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
430 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
431 NULL, &nc);
432 if (!nc)
433 return -ENODEV;
435 /* According to NCSI spec 1.01, the mixed filter table
436 * isn't supported yet.
438 cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
439 switch (cmd->at_e >> 5) {
440 case 0x0: /* UC address */
441 ncf = nc->filters[NCSI_FILTER_UC];
442 break;
443 case 0x1: /* MC address */
444 ncf = nc->filters[NCSI_FILTER_MC];
445 break;
446 default:
447 return -EINVAL;
450 /* Sanity check on the filter */
451 if (!ncf)
452 return -ENOENT;
453 else if (cmd->index >= ncf->total)
454 return -ERANGE;
456 bitmap = &ncf->bitmap;
457 if (cmd->at_e & 0x1) {
458 if (test_and_set_bit(cmd->index, bitmap))
459 return -EBUSY;
460 memcpy(ncf->data + 6 * cmd->index, cmd->mac, 6);
461 } else {
462 if (!test_and_clear_bit(cmd->index, bitmap))
463 return -EBUSY;
465 memset(ncf->data + 6 * cmd->index, 0, 6);
468 return 0;
471 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
473 struct ncsi_cmd_ebf_pkt *cmd;
474 struct ncsi_rsp_pkt *rsp;
475 struct ncsi_dev_priv *ndp = nr->ndp;
476 struct ncsi_channel *nc;
477 struct ncsi_channel_mode *ncm;
479 /* Find the package and channel */
480 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
481 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
482 if (!nc)
483 return -ENODEV;
485 /* Check if broadcast filter has been enabled */
486 ncm = &nc->modes[NCSI_MODE_BC];
487 if (ncm->enable)
488 return -EBUSY;
490 /* Update to broadcast filter mode */
491 cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
492 ncm->enable = 1;
493 ncm->data[0] = ntohl(cmd->mode);
495 return 0;
498 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
500 struct ncsi_rsp_pkt *rsp;
501 struct ncsi_dev_priv *ndp = nr->ndp;
502 struct ncsi_channel *nc;
503 struct ncsi_channel_mode *ncm;
505 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
506 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
507 NULL, &nc);
508 if (!nc)
509 return -ENODEV;
511 /* Check if broadcast filter isn't enabled */
512 ncm = &nc->modes[NCSI_MODE_BC];
513 if (!ncm->enable)
514 return -EBUSY;
516 /* Update to broadcast filter mode */
517 ncm->enable = 0;
518 ncm->data[0] = 0;
520 return 0;
523 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
525 struct ncsi_cmd_egmf_pkt *cmd;
526 struct ncsi_rsp_pkt *rsp;
527 struct ncsi_dev_priv *ndp = nr->ndp;
528 struct ncsi_channel *nc;
529 struct ncsi_channel_mode *ncm;
531 /* Find the channel */
532 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
533 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
534 NULL, &nc);
535 if (!nc)
536 return -ENODEV;
538 /* Check if multicast filter has been enabled */
539 ncm = &nc->modes[NCSI_MODE_MC];
540 if (ncm->enable)
541 return -EBUSY;
543 /* Update to multicast filter mode */
544 cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
545 ncm->enable = 1;
546 ncm->data[0] = ntohl(cmd->mode);
548 return 0;
551 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
553 struct ncsi_rsp_pkt *rsp;
554 struct ncsi_dev_priv *ndp = nr->ndp;
555 struct ncsi_channel *nc;
556 struct ncsi_channel_mode *ncm;
558 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
559 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
560 NULL, &nc);
561 if (!nc)
562 return -ENODEV;
564 /* Check if multicast filter has been enabled */
565 ncm = &nc->modes[NCSI_MODE_MC];
566 if (!ncm->enable)
567 return -EBUSY;
569 /* Update to multicast filter mode */
570 ncm->enable = 0;
571 ncm->data[0] = 0;
573 return 0;
576 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
578 struct ncsi_cmd_snfc_pkt *cmd;
579 struct ncsi_rsp_pkt *rsp;
580 struct ncsi_dev_priv *ndp = nr->ndp;
581 struct ncsi_channel *nc;
582 struct ncsi_channel_mode *ncm;
584 /* Find the channel */
585 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
586 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
587 NULL, &nc);
588 if (!nc)
589 return -ENODEV;
591 /* Check if flow control has been enabled */
592 ncm = &nc->modes[NCSI_MODE_FC];
593 if (ncm->enable)
594 return -EBUSY;
596 /* Update to flow control mode */
597 cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
598 ncm->enable = 1;
599 ncm->data[0] = cmd->mode;
601 return 0;
604 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
606 struct ncsi_rsp_gvi_pkt *rsp;
607 struct ncsi_dev_priv *ndp = nr->ndp;
608 struct ncsi_channel *nc;
609 struct ncsi_channel_version *ncv;
610 int i;
612 /* Find the channel */
613 rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
614 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
615 NULL, &nc);
616 if (!nc)
617 return -ENODEV;
619 /* Update to channel's version info */
620 ncv = &nc->version;
621 ncv->version = ntohl(rsp->ncsi_version);
622 ncv->alpha2 = rsp->alpha2;
623 memcpy(ncv->fw_name, rsp->fw_name, 12);
624 ncv->fw_version = ntohl(rsp->fw_version);
625 for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
626 ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
627 ncv->mf_id = ntohl(rsp->mf_id);
629 return 0;
632 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
634 struct ncsi_rsp_gc_pkt *rsp;
635 struct ncsi_dev_priv *ndp = nr->ndp;
636 struct ncsi_channel *nc;
637 struct ncsi_channel_filter *ncf;
638 size_t size, entry_size;
639 int cnt, i;
641 /* Find the channel */
642 rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
643 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
644 NULL, &nc);
645 if (!nc)
646 return -ENODEV;
648 /* Update channel's capabilities */
649 nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
650 NCSI_CAP_GENERIC_MASK;
651 nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
652 NCSI_CAP_BC_MASK;
653 nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
654 NCSI_CAP_MC_MASK;
655 nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
656 nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
657 NCSI_CAP_AEN_MASK;
658 nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
659 NCSI_CAP_VLAN_MASK;
661 /* Build filters */
662 for (i = 0; i < NCSI_FILTER_MAX; i++) {
663 switch (i) {
664 case NCSI_FILTER_VLAN:
665 cnt = rsp->vlan_cnt;
666 entry_size = 2;
667 break;
668 case NCSI_FILTER_MIXED:
669 cnt = rsp->mixed_cnt;
670 entry_size = 6;
671 break;
672 case NCSI_FILTER_MC:
673 cnt = rsp->mc_cnt;
674 entry_size = 6;
675 break;
676 case NCSI_FILTER_UC:
677 cnt = rsp->uc_cnt;
678 entry_size = 6;
679 break;
680 default:
681 continue;
684 if (!cnt || nc->filters[i])
685 continue;
687 size = sizeof(*ncf) + cnt * entry_size;
688 ncf = kzalloc(size, GFP_ATOMIC);
689 if (!ncf) {
690 pr_warn("%s: Cannot alloc filter table (%d)\n",
691 __func__, i);
692 return -ENOMEM;
695 ncf->index = i;
696 ncf->total = cnt;
697 if (i == NCSI_FILTER_VLAN) {
698 /* Set VLAN filters active so they are cleared in
699 * first configuration state
701 ncf->bitmap = U64_MAX;
702 } else {
703 ncf->bitmap = 0x0ul;
705 nc->filters[i] = ncf;
708 return 0;
711 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
713 struct ncsi_rsp_gp_pkt *rsp;
714 struct ncsi_dev_priv *ndp = nr->ndp;
715 struct ncsi_channel *nc;
716 unsigned short enable, vlan;
717 unsigned char *pdata;
718 int table, i;
720 /* Find the channel */
721 rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
722 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
723 NULL, &nc);
724 if (!nc)
725 return -ENODEV;
727 /* Modes with explicit enabled indications */
728 if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
729 nc->modes[NCSI_MODE_BC].enable = 1;
730 nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
732 if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
733 nc->modes[NCSI_MODE_ENABLE].enable = 1;
734 if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
735 nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
736 if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
737 nc->modes[NCSI_MODE_MC].enable = 1;
739 /* Modes without explicit enabled indications */
740 nc->modes[NCSI_MODE_LINK].enable = 1;
741 nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
742 nc->modes[NCSI_MODE_VLAN].enable = 1;
743 nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
744 nc->modes[NCSI_MODE_FC].enable = 1;
745 nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
746 nc->modes[NCSI_MODE_AEN].enable = 1;
747 nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
749 /* MAC addresses filter table */
750 pdata = (unsigned char *)rsp + 48;
751 enable = rsp->mac_enable;
752 for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
753 if (i >= (nc->filters[NCSI_FILTER_UC]->total +
754 nc->filters[NCSI_FILTER_MC]->total))
755 table = NCSI_FILTER_MIXED;
756 else if (i >= nc->filters[NCSI_FILTER_UC]->total)
757 table = NCSI_FILTER_MC;
758 else
759 table = NCSI_FILTER_UC;
761 if (!(enable & (0x1 << i)))
762 continue;
764 if (ncsi_find_filter(nc, table, pdata) >= 0)
765 continue;
767 ncsi_add_filter(nc, table, pdata);
770 /* VLAN filter table */
771 enable = ntohs(rsp->vlan_enable);
772 for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
773 if (!(enable & (0x1 << i)))
774 continue;
776 vlan = ntohs(*(__be16 *)pdata);
777 if (ncsi_find_filter(nc, NCSI_FILTER_VLAN, &vlan) >= 0)
778 continue;
780 ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan);
783 return 0;
786 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
788 struct ncsi_rsp_gcps_pkt *rsp;
789 struct ncsi_dev_priv *ndp = nr->ndp;
790 struct ncsi_channel *nc;
791 struct ncsi_channel_stats *ncs;
793 /* Find the channel */
794 rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
795 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
796 NULL, &nc);
797 if (!nc)
798 return -ENODEV;
800 /* Update HNC's statistics */
801 ncs = &nc->stats;
802 ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
803 ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
804 ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
805 ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
806 ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
807 ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
808 ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
809 ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
810 ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
811 ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
812 ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
813 ncs->hnc_align_err = ntohl(rsp->align_err);
814 ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
815 ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
816 ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
817 ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
818 ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
819 ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
820 ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
821 ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
822 ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
823 ncs->hnc_l_collision = ntohl(rsp->l_collision);
824 ncs->hnc_e_collision = ntohl(rsp->e_collision);
825 ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
826 ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
827 ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
828 ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
829 ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
830 ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
831 ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
832 ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
833 ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
834 ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
835 ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
836 ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
837 ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
838 ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
839 ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
840 ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
841 ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
842 ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
844 return 0;
847 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
849 struct ncsi_rsp_gns_pkt *rsp;
850 struct ncsi_dev_priv *ndp = nr->ndp;
851 struct ncsi_channel *nc;
852 struct ncsi_channel_stats *ncs;
854 /* Find the channel */
855 rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
856 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
857 NULL, &nc);
858 if (!nc)
859 return -ENODEV;
861 /* Update HNC's statistics */
862 ncs = &nc->stats;
863 ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
864 ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
865 ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
866 ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
867 ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
868 ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
869 ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
871 return 0;
874 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
876 struct ncsi_rsp_gnpts_pkt *rsp;
877 struct ncsi_dev_priv *ndp = nr->ndp;
878 struct ncsi_channel *nc;
879 struct ncsi_channel_stats *ncs;
881 /* Find the channel */
882 rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
883 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
884 NULL, &nc);
885 if (!nc)
886 return -ENODEV;
888 /* Update HNC's statistics */
889 ncs = &nc->stats;
890 ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
891 ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
892 ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
893 ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
894 ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
895 ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
896 ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
897 ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
898 ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
900 return 0;
903 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
905 struct ncsi_rsp_gps_pkt *rsp;
906 struct ncsi_dev_priv *ndp = nr->ndp;
907 struct ncsi_package *np;
909 /* Find the package */
910 rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
911 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
912 &np, NULL);
913 if (!np)
914 return -ENODEV;
916 return 0;
919 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
921 struct ncsi_rsp_gpuuid_pkt *rsp;
922 struct ncsi_dev_priv *ndp = nr->ndp;
923 struct ncsi_package *np;
925 /* Find the package */
926 rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
927 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
928 &np, NULL);
929 if (!np)
930 return -ENODEV;
932 memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
934 return 0;
937 static struct ncsi_rsp_handler {
938 unsigned char type;
939 int payload;
940 int (*handler)(struct ncsi_request *nr);
941 } ncsi_rsp_handlers[] = {
942 { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
943 { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
944 { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
945 { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
946 { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
947 { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
948 { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
949 { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
950 { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
951 { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
952 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
953 { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
954 { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
955 { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
956 { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
957 { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
958 { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
959 { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
960 { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
961 { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
962 { NCSI_PKT_RSP_GVI, 36, ncsi_rsp_handler_gvi },
963 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
964 { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
965 { NCSI_PKT_RSP_GCPS, 172, ncsi_rsp_handler_gcps },
966 { NCSI_PKT_RSP_GNS, 172, ncsi_rsp_handler_gns },
967 { NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts },
968 { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
969 { NCSI_PKT_RSP_OEM, 0, NULL },
970 { NCSI_PKT_RSP_PLDM, 0, NULL },
971 { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid }
974 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
975 struct packet_type *pt, struct net_device *orig_dev)
977 struct ncsi_rsp_handler *nrh = NULL;
978 struct ncsi_dev *nd;
979 struct ncsi_dev_priv *ndp;
980 struct ncsi_request *nr;
981 struct ncsi_pkt_hdr *hdr;
982 unsigned long flags;
983 int payload, i, ret;
985 /* Find the NCSI device */
986 nd = ncsi_find_dev(dev);
987 ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
988 if (!ndp)
989 return -ENODEV;
991 /* Check if it is AEN packet */
992 hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
993 if (hdr->type == NCSI_PKT_AEN)
994 return ncsi_aen_handler(ndp, skb);
996 /* Find the handler */
997 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
998 if (ncsi_rsp_handlers[i].type == hdr->type) {
999 if (ncsi_rsp_handlers[i].handler)
1000 nrh = &ncsi_rsp_handlers[i];
1001 else
1002 nrh = NULL;
1004 break;
1008 if (!nrh) {
1009 netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1010 hdr->type);
1011 return -ENOENT;
1014 /* Associate with the request */
1015 spin_lock_irqsave(&ndp->lock, flags);
1016 nr = &ndp->requests[hdr->id];
1017 if (!nr->used) {
1018 spin_unlock_irqrestore(&ndp->lock, flags);
1019 return -ENODEV;
1022 nr->rsp = skb;
1023 if (!nr->enabled) {
1024 spin_unlock_irqrestore(&ndp->lock, flags);
1025 ret = -ENOENT;
1026 goto out;
1029 /* Validate the packet */
1030 spin_unlock_irqrestore(&ndp->lock, flags);
1031 payload = nrh->payload;
1032 if (payload < 0)
1033 payload = ntohs(hdr->length);
1034 ret = ncsi_validate_rsp_pkt(nr, payload);
1035 if (ret)
1036 goto out;
1038 /* Process the packet */
1039 ret = nrh->handler(nr);
1040 out:
1041 ncsi_free_request(nr);
1042 return ret;