xref: /linux/net/ncsi/ncsi-rsp.c (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
1 /*
2  * Copyright Gavin Shan, IBM Corporation 2016.
3  *
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  */
9 
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>
15 
16 #include <net/ncsi.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 
20 #include "internal.h"
21 #include "ncsi-pkt.h"
22 
23 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
24 				 unsigned short payload)
25 {
26 	struct ncsi_rsp_pkt_hdr *h;
27 	u32 checksum;
28 	__be32 *pchecksum;
29 
30 	/* Check NCSI packet header. We don't need validate
31 	 * the packet type, which should have been checked
32 	 * before calling this function.
33 	 */
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;
39 
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;
44 
45 	/* Validate checksum, which might be zeroes if the
46 	 * sender doesn't support checksum according to NCSI
47 	 * specification.
48 	 */
49 	pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
50 	if (ntohl(*pchecksum) == 0)
51 		return 0;
52 
53 	checksum = ncsi_calculate_checksum((unsigned char *)h,
54 					   sizeof(*h) + payload - 4);
55 	if (*pchecksum != htonl(checksum))
56 		return -EINVAL;
57 
58 	return 0;
59 }
60 
61 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
62 {
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;
68 
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;
74 
75 		id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
76 		nc = ncsi_add_channel(np, id);
77 	}
78 
79 	return nc ? 0 : -ENODEV;
80 }
81 
82 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
83 {
84 	struct ncsi_rsp_pkt *rsp;
85 	struct ncsi_dev_priv *ndp = nr->ndp;
86 	struct ncsi_package *np;
87 	unsigned char id;
88 
89 	/* Add the package if it's not existing. Otherwise,
90 	 * to change the state of its child channels.
91 	 */
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;
98 
99 		id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
100 		np = ncsi_add_package(ndp, id);
101 		if (!np)
102 			return -ENODEV;
103 	}
104 
105 	return 0;
106 }
107 
108 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
109 {
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;
115 
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;
122 
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);
128 	}
129 
130 	return 0;
131 }
132 
133 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
134 {
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;
139 
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;
146 
147 	ncm = &nc->modes[NCSI_MODE_ENABLE];
148 	if (ncm->enable)
149 		return 0;
150 
151 	ncm->enable = 1;
152 	return 0;
153 }
154 
155 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
156 {
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;
162 
163 	ret = ncsi_validate_rsp_pkt(nr, 4);
164 	if (ret)
165 		return ret;
166 
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;
173 
174 	ncm = &nc->modes[NCSI_MODE_ENABLE];
175 	if (!ncm->enable)
176 		return 0;
177 
178 	ncm->enable = 0;
179 	return 0;
180 }
181 
182 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
183 {
184 	struct ncsi_rsp_pkt *rsp;
185 	struct ncsi_dev_priv *ndp = nr->ndp;
186 	struct ncsi_channel *nc;
187 	unsigned long flags;
188 
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;
195 
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);
200 
201 	return 0;
202 }
203 
204 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
205 {
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;
210 
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;
217 
218 	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
219 	if (ncm->enable)
220 		return 0;
221 
222 	ncm->enable = 1;
223 	return 0;
224 }
225 
226 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
227 {
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;
232 
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;
239 
240 	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
241 	if (!ncm->enable)
242 		return 0;
243 
244 	ncm->enable = 1;
245 	return 0;
246 }
247 
248 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
249 {
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;
255 
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;
262 
263 	/* Check if the AEN has been enabled */
264 	ncm = &nc->modes[NCSI_MODE_AEN];
265 	if (ncm->enable)
266 		return 0;
267 
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);
273 
274 	return 0;
275 }
276 
277 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
278 {
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;
284 
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;
291 
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);
296 
297 	return 0;
298 }
299 
300 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
301 {
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;
307 
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;
314 
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);
319 
320 	if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
321 		return 0;
322 
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);
327 
328 	return 0;
329 }
330 
331 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
332 {
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_vlan_filter *ncf;
338 	unsigned long flags;
339 	void *bitmap;
340 
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;
347 
348 	cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
349 	ncf = &nc->vlan_filter;
350 	if (cmd->index == 0 || cmd->index > ncf->n_vids)
351 		return -ERANGE;
352 
353 	/* Add or remove the VLAN filter. Remember HW indexes from 1 */
354 	spin_lock_irqsave(&nc->lock, flags);
355 	bitmap = &ncf->bitmap;
356 	if (!(cmd->enable & 0x1)) {
357 		if (test_and_clear_bit(cmd->index - 1, bitmap))
358 			ncf->vids[cmd->index - 1] = 0;
359 	} else {
360 		set_bit(cmd->index - 1, bitmap);
361 		ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
362 	}
363 	spin_unlock_irqrestore(&nc->lock, flags);
364 
365 	return 0;
366 }
367 
368 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
369 {
370 	struct ncsi_cmd_ev_pkt *cmd;
371 	struct ncsi_rsp_pkt *rsp;
372 	struct ncsi_dev_priv *ndp = nr->ndp;
373 	struct ncsi_channel *nc;
374 	struct ncsi_channel_mode *ncm;
375 
376 	/* Find the package and channel */
377 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
378 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
379 				      NULL, &nc);
380 	if (!nc)
381 		return -ENODEV;
382 
383 	/* Check if VLAN mode has been enabled */
384 	ncm = &nc->modes[NCSI_MODE_VLAN];
385 	if (ncm->enable)
386 		return 0;
387 
388 	/* Update to VLAN mode */
389 	cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
390 	ncm->enable = 1;
391 	ncm->data[0] = ntohl(cmd->mode);
392 
393 	return 0;
394 }
395 
396 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
397 {
398 	struct ncsi_rsp_pkt *rsp;
399 	struct ncsi_dev_priv *ndp = nr->ndp;
400 	struct ncsi_channel *nc;
401 	struct ncsi_channel_mode *ncm;
402 
403 	/* Find the package and channel */
404 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
405 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
406 				      NULL, &nc);
407 	if (!nc)
408 		return -ENODEV;
409 
410 	/* Check if VLAN mode has been enabled */
411 	ncm = &nc->modes[NCSI_MODE_VLAN];
412 	if (!ncm->enable)
413 		return 0;
414 
415 	/* Update to VLAN mode */
416 	ncm->enable = 0;
417 	return 0;
418 }
419 
420 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
421 {
422 	struct ncsi_cmd_sma_pkt *cmd;
423 	struct ncsi_rsp_pkt *rsp;
424 	struct ncsi_dev_priv *ndp = nr->ndp;
425 	struct ncsi_channel *nc;
426 	struct ncsi_channel_mac_filter *ncf;
427 	unsigned long flags;
428 	void *bitmap;
429 	bool enabled;
430 	int index;
431 
432 
433 	/* Find the package and channel */
434 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
435 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
436 				      NULL, &nc);
437 	if (!nc)
438 		return -ENODEV;
439 
440 	/* According to NCSI spec 1.01, the mixed filter table
441 	 * isn't supported yet.
442 	 */
443 	cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
444 	enabled = cmd->at_e & 0x1;
445 	ncf = &nc->mac_filter;
446 	bitmap = &ncf->bitmap;
447 
448 	if (cmd->index == 0 ||
449 	    cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
450 		return -ERANGE;
451 
452 	index = (cmd->index - 1) * ETH_ALEN;
453 	spin_lock_irqsave(&nc->lock, flags);
454 	if (enabled) {
455 		set_bit(cmd->index - 1, bitmap);
456 		memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
457 	} else {
458 		clear_bit(cmd->index - 1, bitmap);
459 		memset(&ncf->addrs[index], 0, ETH_ALEN);
460 	}
461 	spin_unlock_irqrestore(&nc->lock, flags);
462 
463 	return 0;
464 }
465 
466 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
467 {
468 	struct ncsi_cmd_ebf_pkt *cmd;
469 	struct ncsi_rsp_pkt *rsp;
470 	struct ncsi_dev_priv *ndp = nr->ndp;
471 	struct ncsi_channel *nc;
472 	struct ncsi_channel_mode *ncm;
473 
474 	/* Find the package and channel */
475 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
476 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
477 	if (!nc)
478 		return -ENODEV;
479 
480 	/* Check if broadcast filter has been enabled */
481 	ncm = &nc->modes[NCSI_MODE_BC];
482 	if (ncm->enable)
483 		return 0;
484 
485 	/* Update to broadcast filter mode */
486 	cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
487 	ncm->enable = 1;
488 	ncm->data[0] = ntohl(cmd->mode);
489 
490 	return 0;
491 }
492 
493 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
494 {
495 	struct ncsi_rsp_pkt *rsp;
496 	struct ncsi_dev_priv *ndp = nr->ndp;
497 	struct ncsi_channel *nc;
498 	struct ncsi_channel_mode *ncm;
499 
500 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
501 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
502 				      NULL, &nc);
503 	if (!nc)
504 		return -ENODEV;
505 
506 	/* Check if broadcast filter isn't enabled */
507 	ncm = &nc->modes[NCSI_MODE_BC];
508 	if (!ncm->enable)
509 		return 0;
510 
511 	/* Update to broadcast filter mode */
512 	ncm->enable = 0;
513 	ncm->data[0] = 0;
514 
515 	return 0;
516 }
517 
518 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
519 {
520 	struct ncsi_cmd_egmf_pkt *cmd;
521 	struct ncsi_rsp_pkt *rsp;
522 	struct ncsi_dev_priv *ndp = nr->ndp;
523 	struct ncsi_channel *nc;
524 	struct ncsi_channel_mode *ncm;
525 
526 	/* Find the channel */
527 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
528 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
529 				      NULL, &nc);
530 	if (!nc)
531 		return -ENODEV;
532 
533 	/* Check if multicast filter has been enabled */
534 	ncm = &nc->modes[NCSI_MODE_MC];
535 	if (ncm->enable)
536 		return 0;
537 
538 	/* Update to multicast filter mode */
539 	cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
540 	ncm->enable = 1;
541 	ncm->data[0] = ntohl(cmd->mode);
542 
543 	return 0;
544 }
545 
546 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
547 {
548 	struct ncsi_rsp_pkt *rsp;
549 	struct ncsi_dev_priv *ndp = nr->ndp;
550 	struct ncsi_channel *nc;
551 	struct ncsi_channel_mode *ncm;
552 
553 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
554 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
555 				      NULL, &nc);
556 	if (!nc)
557 		return -ENODEV;
558 
559 	/* Check if multicast filter has been enabled */
560 	ncm = &nc->modes[NCSI_MODE_MC];
561 	if (!ncm->enable)
562 		return 0;
563 
564 	/* Update to multicast filter mode */
565 	ncm->enable = 0;
566 	ncm->data[0] = 0;
567 
568 	return 0;
569 }
570 
571 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
572 {
573 	struct ncsi_cmd_snfc_pkt *cmd;
574 	struct ncsi_rsp_pkt *rsp;
575 	struct ncsi_dev_priv *ndp = nr->ndp;
576 	struct ncsi_channel *nc;
577 	struct ncsi_channel_mode *ncm;
578 
579 	/* Find the channel */
580 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
581 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
582 				      NULL, &nc);
583 	if (!nc)
584 		return -ENODEV;
585 
586 	/* Check if flow control has been enabled */
587 	ncm = &nc->modes[NCSI_MODE_FC];
588 	if (ncm->enable)
589 		return 0;
590 
591 	/* Update to flow control mode */
592 	cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
593 	ncm->enable = 1;
594 	ncm->data[0] = cmd->mode;
595 
596 	return 0;
597 }
598 
599 static struct ncsi_rsp_oem_handler {
600 	unsigned int	mfr_id;
601 	int		(*handler)(struct ncsi_request *nr);
602 } ncsi_rsp_oem_handlers[] = {
603 	{ NCSI_OEM_MFR_MLX_ID, NULL },
604 	{ NCSI_OEM_MFR_BCM_ID, NULL }
605 };
606 
607 /* Response handler for OEM command */
608 static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
609 {
610 	struct ncsi_rsp_oem_pkt *rsp;
611 	struct ncsi_rsp_oem_handler *nrh = NULL;
612 	unsigned int mfr_id, i;
613 
614 	/* Get the response header */
615 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
616 	mfr_id = ntohl(rsp->mfr_id);
617 
618 	/* Check for manufacturer id and Find the handler */
619 	for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
620 		if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
621 			if (ncsi_rsp_oem_handlers[i].handler)
622 				nrh = &ncsi_rsp_oem_handlers[i];
623 			else
624 				nrh = NULL;
625 
626 			break;
627 		}
628 	}
629 
630 	if (!nrh) {
631 		netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
632 			   mfr_id);
633 		return -ENOENT;
634 	}
635 
636 	/* Process the packet */
637 	return nrh->handler(nr);
638 }
639 
640 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
641 {
642 	struct ncsi_rsp_gvi_pkt *rsp;
643 	struct ncsi_dev_priv *ndp = nr->ndp;
644 	struct ncsi_channel *nc;
645 	struct ncsi_channel_version *ncv;
646 	int i;
647 
648 	/* Find the channel */
649 	rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
650 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
651 				      NULL, &nc);
652 	if (!nc)
653 		return -ENODEV;
654 
655 	/* Update to channel's version info */
656 	ncv = &nc->version;
657 	ncv->version = ntohl(rsp->ncsi_version);
658 	ncv->alpha2 = rsp->alpha2;
659 	memcpy(ncv->fw_name, rsp->fw_name, 12);
660 	ncv->fw_version = ntohl(rsp->fw_version);
661 	for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
662 		ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
663 	ncv->mf_id = ntohl(rsp->mf_id);
664 
665 	return 0;
666 }
667 
668 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
669 {
670 	struct ncsi_rsp_gc_pkt *rsp;
671 	struct ncsi_dev_priv *ndp = nr->ndp;
672 	struct ncsi_channel *nc;
673 	size_t size;
674 
675 	/* Find the channel */
676 	rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
677 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
678 				      NULL, &nc);
679 	if (!nc)
680 		return -ENODEV;
681 
682 	/* Update channel's capabilities */
683 	nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
684 					 NCSI_CAP_GENERIC_MASK;
685 	nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
686 				    NCSI_CAP_BC_MASK;
687 	nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
688 				    NCSI_CAP_MC_MASK;
689 	nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
690 	nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
691 				     NCSI_CAP_AEN_MASK;
692 	nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
693 				      NCSI_CAP_VLAN_MASK;
694 
695 	size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
696 	nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
697 	if (!nc->mac_filter.addrs)
698 		return -ENOMEM;
699 	nc->mac_filter.n_uc = rsp->uc_cnt;
700 	nc->mac_filter.n_mc = rsp->mc_cnt;
701 	nc->mac_filter.n_mixed = rsp->mixed_cnt;
702 
703 	nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
704 				       sizeof(*nc->vlan_filter.vids),
705 				       GFP_ATOMIC);
706 	if (!nc->vlan_filter.vids)
707 		return -ENOMEM;
708 	/* Set VLAN filters active so they are cleared in the first
709 	 * configuration state
710 	 */
711 	nc->vlan_filter.bitmap = U64_MAX;
712 	nc->vlan_filter.n_vids = rsp->vlan_cnt;
713 
714 	return 0;
715 }
716 
717 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
718 {
719 	struct ncsi_channel_vlan_filter *ncvf;
720 	struct ncsi_channel_mac_filter *ncmf;
721 	struct ncsi_dev_priv *ndp = nr->ndp;
722 	struct ncsi_rsp_gp_pkt *rsp;
723 	struct ncsi_channel *nc;
724 	unsigned short enable;
725 	unsigned char *pdata;
726 	unsigned long flags;
727 	void *bitmap;
728 	int i;
729 
730 	/* Find the channel */
731 	rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
732 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
733 				      NULL, &nc);
734 	if (!nc)
735 		return -ENODEV;
736 
737 	/* Modes with explicit enabled indications */
738 	if (ntohl(rsp->valid_modes) & 0x1) {	/* BC filter mode */
739 		nc->modes[NCSI_MODE_BC].enable = 1;
740 		nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
741 	}
742 	if (ntohl(rsp->valid_modes) & 0x2)	/* Channel enabled */
743 		nc->modes[NCSI_MODE_ENABLE].enable = 1;
744 	if (ntohl(rsp->valid_modes) & 0x4)	/* Channel Tx enabled */
745 		nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
746 	if (ntohl(rsp->valid_modes) & 0x8)	/* MC filter mode */
747 		nc->modes[NCSI_MODE_MC].enable = 1;
748 
749 	/* Modes without explicit enabled indications */
750 	nc->modes[NCSI_MODE_LINK].enable = 1;
751 	nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
752 	nc->modes[NCSI_MODE_VLAN].enable = 1;
753 	nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
754 	nc->modes[NCSI_MODE_FC].enable = 1;
755 	nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
756 	nc->modes[NCSI_MODE_AEN].enable = 1;
757 	nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
758 
759 	/* MAC addresses filter table */
760 	pdata = (unsigned char *)rsp + 48;
761 	enable = rsp->mac_enable;
762 	ncmf = &nc->mac_filter;
763 	spin_lock_irqsave(&nc->lock, flags);
764 	bitmap = &ncmf->bitmap;
765 	for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
766 		if (!(enable & (0x1 << i)))
767 			clear_bit(i, bitmap);
768 		else
769 			set_bit(i, bitmap);
770 
771 		memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
772 	}
773 	spin_unlock_irqrestore(&nc->lock, flags);
774 
775 	/* VLAN filter table */
776 	enable = ntohs(rsp->vlan_enable);
777 	ncvf = &nc->vlan_filter;
778 	bitmap = &ncvf->bitmap;
779 	spin_lock_irqsave(&nc->lock, flags);
780 	for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
781 		if (!(enable & (0x1 << i)))
782 			clear_bit(i, bitmap);
783 		else
784 			set_bit(i, bitmap);
785 
786 		ncvf->vids[i] = ntohs(*(__be16 *)pdata);
787 	}
788 	spin_unlock_irqrestore(&nc->lock, flags);
789 
790 	return 0;
791 }
792 
793 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
794 {
795 	struct ncsi_rsp_gcps_pkt *rsp;
796 	struct ncsi_dev_priv *ndp = nr->ndp;
797 	struct ncsi_channel *nc;
798 	struct ncsi_channel_stats *ncs;
799 
800 	/* Find the channel */
801 	rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
802 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
803 				      NULL, &nc);
804 	if (!nc)
805 		return -ENODEV;
806 
807 	/* Update HNC's statistics */
808 	ncs = &nc->stats;
809 	ncs->hnc_cnt_hi         = ntohl(rsp->cnt_hi);
810 	ncs->hnc_cnt_lo         = ntohl(rsp->cnt_lo);
811 	ncs->hnc_rx_bytes       = ntohl(rsp->rx_bytes);
812 	ncs->hnc_tx_bytes       = ntohl(rsp->tx_bytes);
813 	ncs->hnc_rx_uc_pkts     = ntohl(rsp->rx_uc_pkts);
814 	ncs->hnc_rx_mc_pkts     = ntohl(rsp->rx_mc_pkts);
815 	ncs->hnc_rx_bc_pkts     = ntohl(rsp->rx_bc_pkts);
816 	ncs->hnc_tx_uc_pkts     = ntohl(rsp->tx_uc_pkts);
817 	ncs->hnc_tx_mc_pkts     = ntohl(rsp->tx_mc_pkts);
818 	ncs->hnc_tx_bc_pkts     = ntohl(rsp->tx_bc_pkts);
819 	ncs->hnc_fcs_err        = ntohl(rsp->fcs_err);
820 	ncs->hnc_align_err      = ntohl(rsp->align_err);
821 	ncs->hnc_false_carrier  = ntohl(rsp->false_carrier);
822 	ncs->hnc_runt_pkts      = ntohl(rsp->runt_pkts);
823 	ncs->hnc_jabber_pkts    = ntohl(rsp->jabber_pkts);
824 	ncs->hnc_rx_pause_xon   = ntohl(rsp->rx_pause_xon);
825 	ncs->hnc_rx_pause_xoff  = ntohl(rsp->rx_pause_xoff);
826 	ncs->hnc_tx_pause_xon   = ntohl(rsp->tx_pause_xon);
827 	ncs->hnc_tx_pause_xoff  = ntohl(rsp->tx_pause_xoff);
828 	ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
829 	ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
830 	ncs->hnc_l_collision    = ntohl(rsp->l_collision);
831 	ncs->hnc_e_collision    = ntohl(rsp->e_collision);
832 	ncs->hnc_rx_ctl_frames  = ntohl(rsp->rx_ctl_frames);
833 	ncs->hnc_rx_64_frames   = ntohl(rsp->rx_64_frames);
834 	ncs->hnc_rx_127_frames  = ntohl(rsp->rx_127_frames);
835 	ncs->hnc_rx_255_frames  = ntohl(rsp->rx_255_frames);
836 	ncs->hnc_rx_511_frames  = ntohl(rsp->rx_511_frames);
837 	ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
838 	ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
839 	ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
840 	ncs->hnc_tx_64_frames   = ntohl(rsp->tx_64_frames);
841 	ncs->hnc_tx_127_frames  = ntohl(rsp->tx_127_frames);
842 	ncs->hnc_tx_255_frames  = ntohl(rsp->tx_255_frames);
843 	ncs->hnc_tx_511_frames  = ntohl(rsp->tx_511_frames);
844 	ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
845 	ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
846 	ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
847 	ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
848 	ncs->hnc_rx_runt_pkts   = ntohl(rsp->rx_runt_pkts);
849 	ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
850 
851 	return 0;
852 }
853 
854 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
855 {
856 	struct ncsi_rsp_gns_pkt *rsp;
857 	struct ncsi_dev_priv *ndp = nr->ndp;
858 	struct ncsi_channel *nc;
859 	struct ncsi_channel_stats *ncs;
860 
861 	/* Find the channel */
862 	rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
863 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
864 				      NULL, &nc);
865 	if (!nc)
866 		return -ENODEV;
867 
868 	/* Update HNC's statistics */
869 	ncs = &nc->stats;
870 	ncs->ncsi_rx_cmds       = ntohl(rsp->rx_cmds);
871 	ncs->ncsi_dropped_cmds  = ntohl(rsp->dropped_cmds);
872 	ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
873 	ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
874 	ncs->ncsi_rx_pkts       = ntohl(rsp->rx_pkts);
875 	ncs->ncsi_tx_pkts       = ntohl(rsp->tx_pkts);
876 	ncs->ncsi_tx_aen_pkts   = ntohl(rsp->tx_aen_pkts);
877 
878 	return 0;
879 }
880 
881 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
882 {
883 	struct ncsi_rsp_gnpts_pkt *rsp;
884 	struct ncsi_dev_priv *ndp = nr->ndp;
885 	struct ncsi_channel *nc;
886 	struct ncsi_channel_stats *ncs;
887 
888 	/* Find the channel */
889 	rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
890 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
891 				      NULL, &nc);
892 	if (!nc)
893 		return -ENODEV;
894 
895 	/* Update HNC's statistics */
896 	ncs = &nc->stats;
897 	ncs->pt_tx_pkts        = ntohl(rsp->tx_pkts);
898 	ncs->pt_tx_dropped     = ntohl(rsp->tx_dropped);
899 	ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
900 	ncs->pt_tx_us_err      = ntohl(rsp->tx_us_err);
901 	ncs->pt_rx_pkts        = ntohl(rsp->rx_pkts);
902 	ncs->pt_rx_dropped     = ntohl(rsp->rx_dropped);
903 	ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
904 	ncs->pt_rx_us_err      = ntohl(rsp->rx_us_err);
905 	ncs->pt_rx_os_err      = ntohl(rsp->rx_os_err);
906 
907 	return 0;
908 }
909 
910 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
911 {
912 	struct ncsi_rsp_gps_pkt *rsp;
913 	struct ncsi_dev_priv *ndp = nr->ndp;
914 	struct ncsi_package *np;
915 
916 	/* Find the package */
917 	rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
918 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
919 				      &np, NULL);
920 	if (!np)
921 		return -ENODEV;
922 
923 	return 0;
924 }
925 
926 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
927 {
928 	struct ncsi_rsp_gpuuid_pkt *rsp;
929 	struct ncsi_dev_priv *ndp = nr->ndp;
930 	struct ncsi_package *np;
931 
932 	/* Find the package */
933 	rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
934 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
935 				      &np, NULL);
936 	if (!np)
937 		return -ENODEV;
938 
939 	memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
940 
941 	return 0;
942 }
943 
944 static struct ncsi_rsp_handler {
945 	unsigned char	type;
946 	int             payload;
947 	int		(*handler)(struct ncsi_request *nr);
948 } ncsi_rsp_handlers[] = {
949 	{ NCSI_PKT_RSP_CIS,     4, ncsi_rsp_handler_cis     },
950 	{ NCSI_PKT_RSP_SP,      4, ncsi_rsp_handler_sp      },
951 	{ NCSI_PKT_RSP_DP,      4, ncsi_rsp_handler_dp      },
952 	{ NCSI_PKT_RSP_EC,      4, ncsi_rsp_handler_ec      },
953 	{ NCSI_PKT_RSP_DC,      4, ncsi_rsp_handler_dc      },
954 	{ NCSI_PKT_RSP_RC,      4, ncsi_rsp_handler_rc      },
955 	{ NCSI_PKT_RSP_ECNT,    4, ncsi_rsp_handler_ecnt    },
956 	{ NCSI_PKT_RSP_DCNT,    4, ncsi_rsp_handler_dcnt    },
957 	{ NCSI_PKT_RSP_AE,      4, ncsi_rsp_handler_ae      },
958 	{ NCSI_PKT_RSP_SL,      4, ncsi_rsp_handler_sl      },
959 	{ NCSI_PKT_RSP_GLS,    16, ncsi_rsp_handler_gls     },
960 	{ NCSI_PKT_RSP_SVF,     4, ncsi_rsp_handler_svf     },
961 	{ NCSI_PKT_RSP_EV,      4, ncsi_rsp_handler_ev      },
962 	{ NCSI_PKT_RSP_DV,      4, ncsi_rsp_handler_dv      },
963 	{ NCSI_PKT_RSP_SMA,     4, ncsi_rsp_handler_sma     },
964 	{ NCSI_PKT_RSP_EBF,     4, ncsi_rsp_handler_ebf     },
965 	{ NCSI_PKT_RSP_DBF,     4, ncsi_rsp_handler_dbf     },
966 	{ NCSI_PKT_RSP_EGMF,    4, ncsi_rsp_handler_egmf    },
967 	{ NCSI_PKT_RSP_DGMF,    4, ncsi_rsp_handler_dgmf    },
968 	{ NCSI_PKT_RSP_SNFC,    4, ncsi_rsp_handler_snfc    },
969 	{ NCSI_PKT_RSP_GVI,    40, ncsi_rsp_handler_gvi     },
970 	{ NCSI_PKT_RSP_GC,     32, ncsi_rsp_handler_gc      },
971 	{ NCSI_PKT_RSP_GP,     -1, ncsi_rsp_handler_gp      },
972 	{ NCSI_PKT_RSP_GCPS,  172, ncsi_rsp_handler_gcps    },
973 	{ NCSI_PKT_RSP_GNS,   172, ncsi_rsp_handler_gns     },
974 	{ NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts   },
975 	{ NCSI_PKT_RSP_GPS,     8, ncsi_rsp_handler_gps     },
976 	{ NCSI_PKT_RSP_OEM,    -1, ncsi_rsp_handler_oem     },
977 	{ NCSI_PKT_RSP_PLDM,    0, NULL                     },
978 	{ NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid  }
979 };
980 
981 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
982 		 struct packet_type *pt, struct net_device *orig_dev)
983 {
984 	struct ncsi_rsp_handler *nrh = NULL;
985 	struct ncsi_dev *nd;
986 	struct ncsi_dev_priv *ndp;
987 	struct ncsi_request *nr;
988 	struct ncsi_pkt_hdr *hdr;
989 	unsigned long flags;
990 	int payload, i, ret;
991 
992 	/* Find the NCSI device */
993 	nd = ncsi_find_dev(dev);
994 	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
995 	if (!ndp)
996 		return -ENODEV;
997 
998 	/* Check if it is AEN packet */
999 	hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
1000 	if (hdr->type == NCSI_PKT_AEN)
1001 		return ncsi_aen_handler(ndp, skb);
1002 
1003 	/* Find the handler */
1004 	for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
1005 		if (ncsi_rsp_handlers[i].type == hdr->type) {
1006 			if (ncsi_rsp_handlers[i].handler)
1007 				nrh = &ncsi_rsp_handlers[i];
1008 			else
1009 				nrh = NULL;
1010 
1011 			break;
1012 		}
1013 	}
1014 
1015 	if (!nrh) {
1016 		netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1017 			   hdr->type);
1018 		return -ENOENT;
1019 	}
1020 
1021 	/* Associate with the request */
1022 	spin_lock_irqsave(&ndp->lock, flags);
1023 	nr = &ndp->requests[hdr->id];
1024 	if (!nr->used) {
1025 		spin_unlock_irqrestore(&ndp->lock, flags);
1026 		return -ENODEV;
1027 	}
1028 
1029 	nr->rsp = skb;
1030 	if (!nr->enabled) {
1031 		spin_unlock_irqrestore(&ndp->lock, flags);
1032 		ret = -ENOENT;
1033 		goto out;
1034 	}
1035 
1036 	/* Validate the packet */
1037 	spin_unlock_irqrestore(&ndp->lock, flags);
1038 	payload = nrh->payload;
1039 	if (payload < 0)
1040 		payload = ntohs(hdr->length);
1041 	ret = ncsi_validate_rsp_pkt(nr, payload);
1042 	if (ret) {
1043 		netdev_warn(ndp->ndev.dev,
1044 			    "NCSI: 'bad' packet ignored for type 0x%x\n",
1045 			    hdr->type);
1046 		goto out;
1047 	}
1048 
1049 	/* Process the packet */
1050 	ret = nrh->handler(nr);
1051 	if (ret)
1052 		netdev_err(ndp->ndev.dev,
1053 			   "NCSI: Handler for packet type 0x%x returned %d\n",
1054 			   hdr->type, ret);
1055 out:
1056 	ncsi_free_request(nr);
1057 	return ret;
1058 }
1059