xref: /openbmc/linux/net/ncsi/ncsi-rsp.c (revision 2c64e9cb)
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/etherdevice.h>
15 #include <linux/skbuff.h>
16 
17 #include <net/ncsi.h>
18 #include <net/net_namespace.h>
19 #include <net/sock.h>
20 #include <net/genetlink.h>
21 
22 #include "internal.h"
23 #include "ncsi-pkt.h"
24 #include "ncsi-netlink.h"
25 
26 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
27 				 unsigned short payload)
28 {
29 	struct ncsi_rsp_pkt_hdr *h;
30 	u32 checksum;
31 	__be32 *pchecksum;
32 
33 	/* Check NCSI packet header. We don't need validate
34 	 * the packet type, which should have been checked
35 	 * before calling this function.
36 	 */
37 	h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
38 
39 	if (h->common.revision != NCSI_PKT_REVISION) {
40 		netdev_dbg(nr->ndp->ndev.dev,
41 			   "NCSI: unsupported header revision\n");
42 		return -EINVAL;
43 	}
44 	if (ntohs(h->common.length) != payload) {
45 		netdev_dbg(nr->ndp->ndev.dev,
46 			   "NCSI: payload length mismatched\n");
47 		return -EINVAL;
48 	}
49 
50 	/* Check on code and reason */
51 	if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
52 	    ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR) {
53 		netdev_dbg(nr->ndp->ndev.dev,
54 			   "NCSI: non zero response/reason code\n");
55 		return -EPERM;
56 	}
57 
58 	/* Validate checksum, which might be zeroes if the
59 	 * sender doesn't support checksum according to NCSI
60 	 * specification.
61 	 */
62 	pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
63 	if (ntohl(*pchecksum) == 0)
64 		return 0;
65 
66 	checksum = ncsi_calculate_checksum((unsigned char *)h,
67 					   sizeof(*h) + payload - 4);
68 
69 	if (*pchecksum != htonl(checksum)) {
70 		netdev_dbg(nr->ndp->ndev.dev, "NCSI: checksum mismatched\n");
71 		return -EINVAL;
72 	}
73 
74 	return 0;
75 }
76 
77 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
78 {
79 	struct ncsi_rsp_pkt *rsp;
80 	struct ncsi_dev_priv *ndp = nr->ndp;
81 	struct ncsi_package *np;
82 	struct ncsi_channel *nc;
83 	unsigned char id;
84 
85 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
86 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
87 	if (!nc) {
88 		if (ndp->flags & NCSI_DEV_PROBED)
89 			return -ENXIO;
90 
91 		id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
92 		nc = ncsi_add_channel(np, id);
93 	}
94 
95 	return nc ? 0 : -ENODEV;
96 }
97 
98 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
99 {
100 	struct ncsi_rsp_pkt *rsp;
101 	struct ncsi_dev_priv *ndp = nr->ndp;
102 	struct ncsi_package *np;
103 	unsigned char id;
104 
105 	/* Add the package if it's not existing. Otherwise,
106 	 * to change the state of its child channels.
107 	 */
108 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
109 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
110 				      &np, NULL);
111 	if (!np) {
112 		if (ndp->flags & NCSI_DEV_PROBED)
113 			return -ENXIO;
114 
115 		id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
116 		np = ncsi_add_package(ndp, id);
117 		if (!np)
118 			return -ENODEV;
119 	}
120 
121 	return 0;
122 }
123 
124 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
125 {
126 	struct ncsi_rsp_pkt *rsp;
127 	struct ncsi_dev_priv *ndp = nr->ndp;
128 	struct ncsi_package *np;
129 	struct ncsi_channel *nc;
130 	unsigned long flags;
131 
132 	/* Find the package */
133 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
134 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
135 				      &np, NULL);
136 	if (!np)
137 		return -ENODEV;
138 
139 	/* Change state of all channels attached to the package */
140 	NCSI_FOR_EACH_CHANNEL(np, nc) {
141 		spin_lock_irqsave(&nc->lock, flags);
142 		nc->state = NCSI_CHANNEL_INACTIVE;
143 		spin_unlock_irqrestore(&nc->lock, flags);
144 	}
145 
146 	return 0;
147 }
148 
149 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
150 {
151 	struct ncsi_rsp_pkt *rsp;
152 	struct ncsi_dev_priv *ndp = nr->ndp;
153 	struct ncsi_channel *nc;
154 	struct ncsi_channel_mode *ncm;
155 
156 	/* Find the package and channel */
157 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
158 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
159 				      NULL, &nc);
160 	if (!nc)
161 		return -ENODEV;
162 
163 	ncm = &nc->modes[NCSI_MODE_ENABLE];
164 	if (ncm->enable)
165 		return 0;
166 
167 	ncm->enable = 1;
168 	return 0;
169 }
170 
171 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
172 {
173 	struct ncsi_rsp_pkt *rsp;
174 	struct ncsi_dev_priv *ndp = nr->ndp;
175 	struct ncsi_channel *nc;
176 	struct ncsi_channel_mode *ncm;
177 	int ret;
178 
179 	ret = ncsi_validate_rsp_pkt(nr, 4);
180 	if (ret)
181 		return ret;
182 
183 	/* Find the package and channel */
184 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
185 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
186 				      NULL, &nc);
187 	if (!nc)
188 		return -ENODEV;
189 
190 	ncm = &nc->modes[NCSI_MODE_ENABLE];
191 	if (!ncm->enable)
192 		return 0;
193 
194 	ncm->enable = 0;
195 	return 0;
196 }
197 
198 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
199 {
200 	struct ncsi_rsp_pkt *rsp;
201 	struct ncsi_dev_priv *ndp = nr->ndp;
202 	struct ncsi_channel *nc;
203 	unsigned long flags;
204 
205 	/* Find the package and channel */
206 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
207 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
208 				      NULL, &nc);
209 	if (!nc)
210 		return -ENODEV;
211 
212 	/* Update state for the specified channel */
213 	spin_lock_irqsave(&nc->lock, flags);
214 	nc->state = NCSI_CHANNEL_INACTIVE;
215 	spin_unlock_irqrestore(&nc->lock, flags);
216 
217 	return 0;
218 }
219 
220 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
221 {
222 	struct ncsi_rsp_pkt *rsp;
223 	struct ncsi_dev_priv *ndp = nr->ndp;
224 	struct ncsi_channel *nc;
225 	struct ncsi_channel_mode *ncm;
226 
227 	/* Find the package and channel */
228 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
229 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
230 				      NULL, &nc);
231 	if (!nc)
232 		return -ENODEV;
233 
234 	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
235 	if (ncm->enable)
236 		return 0;
237 
238 	ncm->enable = 1;
239 	return 0;
240 }
241 
242 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
243 {
244 	struct ncsi_rsp_pkt *rsp;
245 	struct ncsi_dev_priv *ndp = nr->ndp;
246 	struct ncsi_channel *nc;
247 	struct ncsi_channel_mode *ncm;
248 
249 	/* Find the package and channel */
250 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
251 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
252 				      NULL, &nc);
253 	if (!nc)
254 		return -ENODEV;
255 
256 	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
257 	if (!ncm->enable)
258 		return 0;
259 
260 	ncm->enable = 0;
261 	return 0;
262 }
263 
264 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
265 {
266 	struct ncsi_cmd_ae_pkt *cmd;
267 	struct ncsi_rsp_pkt *rsp;
268 	struct ncsi_dev_priv *ndp = nr->ndp;
269 	struct ncsi_channel *nc;
270 	struct ncsi_channel_mode *ncm;
271 
272 	/* Find the package and channel */
273 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
274 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
275 				      NULL, &nc);
276 	if (!nc)
277 		return -ENODEV;
278 
279 	/* Check if the AEN has been enabled */
280 	ncm = &nc->modes[NCSI_MODE_AEN];
281 	if (ncm->enable)
282 		return 0;
283 
284 	/* Update to AEN configuration */
285 	cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
286 	ncm->enable = 1;
287 	ncm->data[0] = cmd->mc_id;
288 	ncm->data[1] = ntohl(cmd->mode);
289 
290 	return 0;
291 }
292 
293 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
294 {
295 	struct ncsi_cmd_sl_pkt *cmd;
296 	struct ncsi_rsp_pkt *rsp;
297 	struct ncsi_dev_priv *ndp = nr->ndp;
298 	struct ncsi_channel *nc;
299 	struct ncsi_channel_mode *ncm;
300 
301 	/* Find the package and channel */
302 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
303 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
304 				      NULL, &nc);
305 	if (!nc)
306 		return -ENODEV;
307 
308 	cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
309 	ncm = &nc->modes[NCSI_MODE_LINK];
310 	ncm->data[0] = ntohl(cmd->mode);
311 	ncm->data[1] = ntohl(cmd->oem_mode);
312 
313 	return 0;
314 }
315 
316 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
317 {
318 	struct ncsi_rsp_gls_pkt *rsp;
319 	struct ncsi_dev_priv *ndp = nr->ndp;
320 	struct ncsi_channel *nc;
321 	struct ncsi_channel_mode *ncm;
322 	unsigned long flags;
323 
324 	/* Find the package and channel */
325 	rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
326 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
327 				      NULL, &nc);
328 	if (!nc)
329 		return -ENODEV;
330 
331 	ncm = &nc->modes[NCSI_MODE_LINK];
332 	ncm->data[2] = ntohl(rsp->status);
333 	ncm->data[3] = ntohl(rsp->other);
334 	ncm->data[4] = ntohl(rsp->oem_status);
335 
336 	if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
337 		return 0;
338 
339 	/* Reset the channel monitor if it has been enabled */
340 	spin_lock_irqsave(&nc->lock, flags);
341 	nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
342 	spin_unlock_irqrestore(&nc->lock, flags);
343 
344 	return 0;
345 }
346 
347 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
348 {
349 	struct ncsi_cmd_svf_pkt *cmd;
350 	struct ncsi_rsp_pkt *rsp;
351 	struct ncsi_dev_priv *ndp = nr->ndp;
352 	struct ncsi_channel *nc;
353 	struct ncsi_channel_vlan_filter *ncf;
354 	unsigned long flags;
355 	void *bitmap;
356 
357 	/* Find the package and channel */
358 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
359 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
360 				      NULL, &nc);
361 	if (!nc)
362 		return -ENODEV;
363 
364 	cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
365 	ncf = &nc->vlan_filter;
366 	if (cmd->index == 0 || cmd->index > ncf->n_vids)
367 		return -ERANGE;
368 
369 	/* Add or remove the VLAN filter. Remember HW indexes from 1 */
370 	spin_lock_irqsave(&nc->lock, flags);
371 	bitmap = &ncf->bitmap;
372 	if (!(cmd->enable & 0x1)) {
373 		if (test_and_clear_bit(cmd->index - 1, bitmap))
374 			ncf->vids[cmd->index - 1] = 0;
375 	} else {
376 		set_bit(cmd->index - 1, bitmap);
377 		ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
378 	}
379 	spin_unlock_irqrestore(&nc->lock, flags);
380 
381 	return 0;
382 }
383 
384 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
385 {
386 	struct ncsi_cmd_ev_pkt *cmd;
387 	struct ncsi_rsp_pkt *rsp;
388 	struct ncsi_dev_priv *ndp = nr->ndp;
389 	struct ncsi_channel *nc;
390 	struct ncsi_channel_mode *ncm;
391 
392 	/* Find the package and channel */
393 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
394 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
395 				      NULL, &nc);
396 	if (!nc)
397 		return -ENODEV;
398 
399 	/* Check if VLAN mode has been enabled */
400 	ncm = &nc->modes[NCSI_MODE_VLAN];
401 	if (ncm->enable)
402 		return 0;
403 
404 	/* Update to VLAN mode */
405 	cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
406 	ncm->enable = 1;
407 	ncm->data[0] = ntohl(cmd->mode);
408 
409 	return 0;
410 }
411 
412 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
413 {
414 	struct ncsi_rsp_pkt *rsp;
415 	struct ncsi_dev_priv *ndp = nr->ndp;
416 	struct ncsi_channel *nc;
417 	struct ncsi_channel_mode *ncm;
418 
419 	/* Find the package and channel */
420 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
421 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
422 				      NULL, &nc);
423 	if (!nc)
424 		return -ENODEV;
425 
426 	/* Check if VLAN mode has been enabled */
427 	ncm = &nc->modes[NCSI_MODE_VLAN];
428 	if (!ncm->enable)
429 		return 0;
430 
431 	/* Update to VLAN mode */
432 	ncm->enable = 0;
433 	return 0;
434 }
435 
436 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
437 {
438 	struct ncsi_cmd_sma_pkt *cmd;
439 	struct ncsi_rsp_pkt *rsp;
440 	struct ncsi_dev_priv *ndp = nr->ndp;
441 	struct ncsi_channel *nc;
442 	struct ncsi_channel_mac_filter *ncf;
443 	unsigned long flags;
444 	void *bitmap;
445 	bool enabled;
446 	int index;
447 
448 
449 	/* Find the package and channel */
450 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
451 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
452 				      NULL, &nc);
453 	if (!nc)
454 		return -ENODEV;
455 
456 	/* According to NCSI spec 1.01, the mixed filter table
457 	 * isn't supported yet.
458 	 */
459 	cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
460 	enabled = cmd->at_e & 0x1;
461 	ncf = &nc->mac_filter;
462 	bitmap = &ncf->bitmap;
463 
464 	if (cmd->index == 0 ||
465 	    cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
466 		return -ERANGE;
467 
468 	index = (cmd->index - 1) * ETH_ALEN;
469 	spin_lock_irqsave(&nc->lock, flags);
470 	if (enabled) {
471 		set_bit(cmd->index - 1, bitmap);
472 		memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
473 	} else {
474 		clear_bit(cmd->index - 1, bitmap);
475 		memset(&ncf->addrs[index], 0, ETH_ALEN);
476 	}
477 	spin_unlock_irqrestore(&nc->lock, flags);
478 
479 	return 0;
480 }
481 
482 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
483 {
484 	struct ncsi_cmd_ebf_pkt *cmd;
485 	struct ncsi_rsp_pkt *rsp;
486 	struct ncsi_dev_priv *ndp = nr->ndp;
487 	struct ncsi_channel *nc;
488 	struct ncsi_channel_mode *ncm;
489 
490 	/* Find the package and channel */
491 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
492 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
493 	if (!nc)
494 		return -ENODEV;
495 
496 	/* Check if broadcast filter has been enabled */
497 	ncm = &nc->modes[NCSI_MODE_BC];
498 	if (ncm->enable)
499 		return 0;
500 
501 	/* Update to broadcast filter mode */
502 	cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
503 	ncm->enable = 1;
504 	ncm->data[0] = ntohl(cmd->mode);
505 
506 	return 0;
507 }
508 
509 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
510 {
511 	struct ncsi_rsp_pkt *rsp;
512 	struct ncsi_dev_priv *ndp = nr->ndp;
513 	struct ncsi_channel *nc;
514 	struct ncsi_channel_mode *ncm;
515 
516 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
517 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
518 				      NULL, &nc);
519 	if (!nc)
520 		return -ENODEV;
521 
522 	/* Check if broadcast filter isn't enabled */
523 	ncm = &nc->modes[NCSI_MODE_BC];
524 	if (!ncm->enable)
525 		return 0;
526 
527 	/* Update to broadcast filter mode */
528 	ncm->enable = 0;
529 	ncm->data[0] = 0;
530 
531 	return 0;
532 }
533 
534 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
535 {
536 	struct ncsi_cmd_egmf_pkt *cmd;
537 	struct ncsi_rsp_pkt *rsp;
538 	struct ncsi_dev_priv *ndp = nr->ndp;
539 	struct ncsi_channel *nc;
540 	struct ncsi_channel_mode *ncm;
541 
542 	/* Find the channel */
543 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
544 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
545 				      NULL, &nc);
546 	if (!nc)
547 		return -ENODEV;
548 
549 	/* Check if multicast filter has been enabled */
550 	ncm = &nc->modes[NCSI_MODE_MC];
551 	if (ncm->enable)
552 		return 0;
553 
554 	/* Update to multicast filter mode */
555 	cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
556 	ncm->enable = 1;
557 	ncm->data[0] = ntohl(cmd->mode);
558 
559 	return 0;
560 }
561 
562 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
563 {
564 	struct ncsi_rsp_pkt *rsp;
565 	struct ncsi_dev_priv *ndp = nr->ndp;
566 	struct ncsi_channel *nc;
567 	struct ncsi_channel_mode *ncm;
568 
569 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
570 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
571 				      NULL, &nc);
572 	if (!nc)
573 		return -ENODEV;
574 
575 	/* Check if multicast filter has been enabled */
576 	ncm = &nc->modes[NCSI_MODE_MC];
577 	if (!ncm->enable)
578 		return 0;
579 
580 	/* Update to multicast filter mode */
581 	ncm->enable = 0;
582 	ncm->data[0] = 0;
583 
584 	return 0;
585 }
586 
587 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
588 {
589 	struct ncsi_cmd_snfc_pkt *cmd;
590 	struct ncsi_rsp_pkt *rsp;
591 	struct ncsi_dev_priv *ndp = nr->ndp;
592 	struct ncsi_channel *nc;
593 	struct ncsi_channel_mode *ncm;
594 
595 	/* Find the channel */
596 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
597 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
598 				      NULL, &nc);
599 	if (!nc)
600 		return -ENODEV;
601 
602 	/* Check if flow control has been enabled */
603 	ncm = &nc->modes[NCSI_MODE_FC];
604 	if (ncm->enable)
605 		return 0;
606 
607 	/* Update to flow control mode */
608 	cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
609 	ncm->enable = 1;
610 	ncm->data[0] = cmd->mode;
611 
612 	return 0;
613 }
614 
615 /* Response handler for Mellanox command Get Mac Address */
616 static int ncsi_rsp_handler_oem_mlx_gma(struct ncsi_request *nr)
617 {
618 	struct ncsi_dev_priv *ndp = nr->ndp;
619 	struct net_device *ndev = ndp->ndev.dev;
620 	const struct net_device_ops *ops = ndev->netdev_ops;
621 	struct ncsi_rsp_oem_pkt *rsp;
622 	struct sockaddr saddr;
623 	int ret = 0;
624 
625 	/* Get the response header */
626 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
627 
628 	saddr.sa_family = ndev->type;
629 	ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
630 	memcpy(saddr.sa_data, &rsp->data[MLX_MAC_ADDR_OFFSET], ETH_ALEN);
631 	ret = ops->ndo_set_mac_address(ndev, &saddr);
632 	if (ret < 0)
633 		netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
634 
635 	return ret;
636 }
637 
638 /* Response handler for Mellanox card */
639 static int ncsi_rsp_handler_oem_mlx(struct ncsi_request *nr)
640 {
641 	struct ncsi_rsp_oem_mlx_pkt *mlx;
642 	struct ncsi_rsp_oem_pkt *rsp;
643 
644 	/* Get the response header */
645 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
646 	mlx = (struct ncsi_rsp_oem_mlx_pkt *)(rsp->data);
647 
648 	if (mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
649 	    mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
650 		return ncsi_rsp_handler_oem_mlx_gma(nr);
651 	return 0;
652 }
653 
654 /* Response handler for Broadcom command Get Mac Address */
655 static int ncsi_rsp_handler_oem_bcm_gma(struct ncsi_request *nr)
656 {
657 	struct ncsi_dev_priv *ndp = nr->ndp;
658 	struct net_device *ndev = ndp->ndev.dev;
659 	const struct net_device_ops *ops = ndev->netdev_ops;
660 	struct ncsi_rsp_oem_pkt *rsp;
661 	struct sockaddr saddr;
662 	int ret = 0;
663 
664 	/* Get the response header */
665 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
666 
667 	saddr.sa_family = ndev->type;
668 	ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
669 	memcpy(saddr.sa_data, &rsp->data[BCM_MAC_ADDR_OFFSET], ETH_ALEN);
670 	/* Increase mac address by 1 for BMC's address */
671 	eth_addr_inc((u8 *)saddr.sa_data);
672 	if (!is_valid_ether_addr((const u8 *)saddr.sa_data))
673 		return -ENXIO;
674 
675 	ret = ops->ndo_set_mac_address(ndev, &saddr);
676 	if (ret < 0)
677 		netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
678 
679 	return ret;
680 }
681 
682 /* Response handler for Broadcom card */
683 static int ncsi_rsp_handler_oem_bcm(struct ncsi_request *nr)
684 {
685 	struct ncsi_rsp_oem_bcm_pkt *bcm;
686 	struct ncsi_rsp_oem_pkt *rsp;
687 
688 	/* Get the response header */
689 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
690 	bcm = (struct ncsi_rsp_oem_bcm_pkt *)(rsp->data);
691 
692 	if (bcm->type == NCSI_OEM_BCM_CMD_GMA)
693 		return ncsi_rsp_handler_oem_bcm_gma(nr);
694 	return 0;
695 }
696 
697 static struct ncsi_rsp_oem_handler {
698 	unsigned int	mfr_id;
699 	int		(*handler)(struct ncsi_request *nr);
700 } ncsi_rsp_oem_handlers[] = {
701 	{ NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
702 	{ NCSI_OEM_MFR_BCM_ID, ncsi_rsp_handler_oem_bcm }
703 };
704 
705 /* Response handler for OEM command */
706 static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
707 {
708 	struct ncsi_rsp_oem_handler *nrh = NULL;
709 	struct ncsi_rsp_oem_pkt *rsp;
710 	unsigned int mfr_id, i;
711 
712 	/* Get the response header */
713 	rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
714 	mfr_id = ntohl(rsp->mfr_id);
715 
716 	/* Check for manufacturer id and Find the handler */
717 	for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
718 		if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
719 			if (ncsi_rsp_oem_handlers[i].handler)
720 				nrh = &ncsi_rsp_oem_handlers[i];
721 			else
722 				nrh = NULL;
723 
724 			break;
725 		}
726 	}
727 
728 	if (!nrh) {
729 		netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
730 			   mfr_id);
731 		return -ENOENT;
732 	}
733 
734 	/* Process the packet */
735 	return nrh->handler(nr);
736 }
737 
738 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
739 {
740 	struct ncsi_rsp_gvi_pkt *rsp;
741 	struct ncsi_dev_priv *ndp = nr->ndp;
742 	struct ncsi_channel *nc;
743 	struct ncsi_channel_version *ncv;
744 	int i;
745 
746 	/* Find the channel */
747 	rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
748 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
749 				      NULL, &nc);
750 	if (!nc)
751 		return -ENODEV;
752 
753 	/* Update to channel's version info */
754 	ncv = &nc->version;
755 	ncv->version = ntohl(rsp->ncsi_version);
756 	ncv->alpha2 = rsp->alpha2;
757 	memcpy(ncv->fw_name, rsp->fw_name, 12);
758 	ncv->fw_version = ntohl(rsp->fw_version);
759 	for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
760 		ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
761 	ncv->mf_id = ntohl(rsp->mf_id);
762 
763 	return 0;
764 }
765 
766 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
767 {
768 	struct ncsi_rsp_gc_pkt *rsp;
769 	struct ncsi_dev_priv *ndp = nr->ndp;
770 	struct ncsi_channel *nc;
771 	size_t size;
772 
773 	/* Find the channel */
774 	rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
775 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
776 				      NULL, &nc);
777 	if (!nc)
778 		return -ENODEV;
779 
780 	/* Update channel's capabilities */
781 	nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
782 					 NCSI_CAP_GENERIC_MASK;
783 	nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
784 				    NCSI_CAP_BC_MASK;
785 	nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
786 				    NCSI_CAP_MC_MASK;
787 	nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
788 	nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
789 				     NCSI_CAP_AEN_MASK;
790 	nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
791 				      NCSI_CAP_VLAN_MASK;
792 
793 	size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
794 	nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
795 	if (!nc->mac_filter.addrs)
796 		return -ENOMEM;
797 	nc->mac_filter.n_uc = rsp->uc_cnt;
798 	nc->mac_filter.n_mc = rsp->mc_cnt;
799 	nc->mac_filter.n_mixed = rsp->mixed_cnt;
800 
801 	nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
802 				       sizeof(*nc->vlan_filter.vids),
803 				       GFP_ATOMIC);
804 	if (!nc->vlan_filter.vids)
805 		return -ENOMEM;
806 	/* Set VLAN filters active so they are cleared in the first
807 	 * configuration state
808 	 */
809 	nc->vlan_filter.bitmap = U64_MAX;
810 	nc->vlan_filter.n_vids = rsp->vlan_cnt;
811 
812 	return 0;
813 }
814 
815 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
816 {
817 	struct ncsi_channel_vlan_filter *ncvf;
818 	struct ncsi_channel_mac_filter *ncmf;
819 	struct ncsi_dev_priv *ndp = nr->ndp;
820 	struct ncsi_rsp_gp_pkt *rsp;
821 	struct ncsi_channel *nc;
822 	unsigned short enable;
823 	unsigned char *pdata;
824 	unsigned long flags;
825 	void *bitmap;
826 	int i;
827 
828 	/* Find the channel */
829 	rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
830 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
831 				      NULL, &nc);
832 	if (!nc)
833 		return -ENODEV;
834 
835 	/* Modes with explicit enabled indications */
836 	if (ntohl(rsp->valid_modes) & 0x1) {	/* BC filter mode */
837 		nc->modes[NCSI_MODE_BC].enable = 1;
838 		nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
839 	}
840 	if (ntohl(rsp->valid_modes) & 0x2)	/* Channel enabled */
841 		nc->modes[NCSI_MODE_ENABLE].enable = 1;
842 	if (ntohl(rsp->valid_modes) & 0x4)	/* Channel Tx enabled */
843 		nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
844 	if (ntohl(rsp->valid_modes) & 0x8)	/* MC filter mode */
845 		nc->modes[NCSI_MODE_MC].enable = 1;
846 
847 	/* Modes without explicit enabled indications */
848 	nc->modes[NCSI_MODE_LINK].enable = 1;
849 	nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
850 	nc->modes[NCSI_MODE_VLAN].enable = 1;
851 	nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
852 	nc->modes[NCSI_MODE_FC].enable = 1;
853 	nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
854 	nc->modes[NCSI_MODE_AEN].enable = 1;
855 	nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
856 
857 	/* MAC addresses filter table */
858 	pdata = (unsigned char *)rsp + 48;
859 	enable = rsp->mac_enable;
860 	ncmf = &nc->mac_filter;
861 	spin_lock_irqsave(&nc->lock, flags);
862 	bitmap = &ncmf->bitmap;
863 	for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
864 		if (!(enable & (0x1 << i)))
865 			clear_bit(i, bitmap);
866 		else
867 			set_bit(i, bitmap);
868 
869 		memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
870 	}
871 	spin_unlock_irqrestore(&nc->lock, flags);
872 
873 	/* VLAN filter table */
874 	enable = ntohs(rsp->vlan_enable);
875 	ncvf = &nc->vlan_filter;
876 	bitmap = &ncvf->bitmap;
877 	spin_lock_irqsave(&nc->lock, flags);
878 	for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
879 		if (!(enable & (0x1 << i)))
880 			clear_bit(i, bitmap);
881 		else
882 			set_bit(i, bitmap);
883 
884 		ncvf->vids[i] = ntohs(*(__be16 *)pdata);
885 	}
886 	spin_unlock_irqrestore(&nc->lock, flags);
887 
888 	return 0;
889 }
890 
891 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
892 {
893 	struct ncsi_rsp_gcps_pkt *rsp;
894 	struct ncsi_dev_priv *ndp = nr->ndp;
895 	struct ncsi_channel *nc;
896 	struct ncsi_channel_stats *ncs;
897 
898 	/* Find the channel */
899 	rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
900 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
901 				      NULL, &nc);
902 	if (!nc)
903 		return -ENODEV;
904 
905 	/* Update HNC's statistics */
906 	ncs = &nc->stats;
907 	ncs->hnc_cnt_hi         = ntohl(rsp->cnt_hi);
908 	ncs->hnc_cnt_lo         = ntohl(rsp->cnt_lo);
909 	ncs->hnc_rx_bytes       = ntohl(rsp->rx_bytes);
910 	ncs->hnc_tx_bytes       = ntohl(rsp->tx_bytes);
911 	ncs->hnc_rx_uc_pkts     = ntohl(rsp->rx_uc_pkts);
912 	ncs->hnc_rx_mc_pkts     = ntohl(rsp->rx_mc_pkts);
913 	ncs->hnc_rx_bc_pkts     = ntohl(rsp->rx_bc_pkts);
914 	ncs->hnc_tx_uc_pkts     = ntohl(rsp->tx_uc_pkts);
915 	ncs->hnc_tx_mc_pkts     = ntohl(rsp->tx_mc_pkts);
916 	ncs->hnc_tx_bc_pkts     = ntohl(rsp->tx_bc_pkts);
917 	ncs->hnc_fcs_err        = ntohl(rsp->fcs_err);
918 	ncs->hnc_align_err      = ntohl(rsp->align_err);
919 	ncs->hnc_false_carrier  = ntohl(rsp->false_carrier);
920 	ncs->hnc_runt_pkts      = ntohl(rsp->runt_pkts);
921 	ncs->hnc_jabber_pkts    = ntohl(rsp->jabber_pkts);
922 	ncs->hnc_rx_pause_xon   = ntohl(rsp->rx_pause_xon);
923 	ncs->hnc_rx_pause_xoff  = ntohl(rsp->rx_pause_xoff);
924 	ncs->hnc_tx_pause_xon   = ntohl(rsp->tx_pause_xon);
925 	ncs->hnc_tx_pause_xoff  = ntohl(rsp->tx_pause_xoff);
926 	ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
927 	ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
928 	ncs->hnc_l_collision    = ntohl(rsp->l_collision);
929 	ncs->hnc_e_collision    = ntohl(rsp->e_collision);
930 	ncs->hnc_rx_ctl_frames  = ntohl(rsp->rx_ctl_frames);
931 	ncs->hnc_rx_64_frames   = ntohl(rsp->rx_64_frames);
932 	ncs->hnc_rx_127_frames  = ntohl(rsp->rx_127_frames);
933 	ncs->hnc_rx_255_frames  = ntohl(rsp->rx_255_frames);
934 	ncs->hnc_rx_511_frames  = ntohl(rsp->rx_511_frames);
935 	ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
936 	ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
937 	ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
938 	ncs->hnc_tx_64_frames   = ntohl(rsp->tx_64_frames);
939 	ncs->hnc_tx_127_frames  = ntohl(rsp->tx_127_frames);
940 	ncs->hnc_tx_255_frames  = ntohl(rsp->tx_255_frames);
941 	ncs->hnc_tx_511_frames  = ntohl(rsp->tx_511_frames);
942 	ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
943 	ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
944 	ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
945 	ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
946 	ncs->hnc_rx_runt_pkts   = ntohl(rsp->rx_runt_pkts);
947 	ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
948 
949 	return 0;
950 }
951 
952 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
953 {
954 	struct ncsi_rsp_gns_pkt *rsp;
955 	struct ncsi_dev_priv *ndp = nr->ndp;
956 	struct ncsi_channel *nc;
957 	struct ncsi_channel_stats *ncs;
958 
959 	/* Find the channel */
960 	rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
961 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
962 				      NULL, &nc);
963 	if (!nc)
964 		return -ENODEV;
965 
966 	/* Update HNC's statistics */
967 	ncs = &nc->stats;
968 	ncs->ncsi_rx_cmds       = ntohl(rsp->rx_cmds);
969 	ncs->ncsi_dropped_cmds  = ntohl(rsp->dropped_cmds);
970 	ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
971 	ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
972 	ncs->ncsi_rx_pkts       = ntohl(rsp->rx_pkts);
973 	ncs->ncsi_tx_pkts       = ntohl(rsp->tx_pkts);
974 	ncs->ncsi_tx_aen_pkts   = ntohl(rsp->tx_aen_pkts);
975 
976 	return 0;
977 }
978 
979 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
980 {
981 	struct ncsi_rsp_gnpts_pkt *rsp;
982 	struct ncsi_dev_priv *ndp = nr->ndp;
983 	struct ncsi_channel *nc;
984 	struct ncsi_channel_stats *ncs;
985 
986 	/* Find the channel */
987 	rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
988 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
989 				      NULL, &nc);
990 	if (!nc)
991 		return -ENODEV;
992 
993 	/* Update HNC's statistics */
994 	ncs = &nc->stats;
995 	ncs->pt_tx_pkts        = ntohl(rsp->tx_pkts);
996 	ncs->pt_tx_dropped     = ntohl(rsp->tx_dropped);
997 	ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
998 	ncs->pt_tx_us_err      = ntohl(rsp->tx_us_err);
999 	ncs->pt_rx_pkts        = ntohl(rsp->rx_pkts);
1000 	ncs->pt_rx_dropped     = ntohl(rsp->rx_dropped);
1001 	ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
1002 	ncs->pt_rx_us_err      = ntohl(rsp->rx_us_err);
1003 	ncs->pt_rx_os_err      = ntohl(rsp->rx_os_err);
1004 
1005 	return 0;
1006 }
1007 
1008 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
1009 {
1010 	struct ncsi_rsp_gps_pkt *rsp;
1011 	struct ncsi_dev_priv *ndp = nr->ndp;
1012 	struct ncsi_package *np;
1013 
1014 	/* Find the package */
1015 	rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
1016 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1017 				      &np, NULL);
1018 	if (!np)
1019 		return -ENODEV;
1020 
1021 	return 0;
1022 }
1023 
1024 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
1025 {
1026 	struct ncsi_rsp_gpuuid_pkt *rsp;
1027 	struct ncsi_dev_priv *ndp = nr->ndp;
1028 	struct ncsi_package *np;
1029 
1030 	/* Find the package */
1031 	rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
1032 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1033 				      &np, NULL);
1034 	if (!np)
1035 		return -ENODEV;
1036 
1037 	memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
1038 
1039 	return 0;
1040 }
1041 
1042 static int ncsi_rsp_handler_netlink(struct ncsi_request *nr)
1043 {
1044 	struct ncsi_dev_priv *ndp = nr->ndp;
1045 	struct ncsi_rsp_pkt *rsp;
1046 	struct ncsi_package *np;
1047 	struct ncsi_channel *nc;
1048 	int ret;
1049 
1050 	/* Find the package */
1051 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
1052 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1053 				      &np, &nc);
1054 	if (!np)
1055 		return -ENODEV;
1056 
1057 	ret = ncsi_send_netlink_rsp(nr, np, nc);
1058 
1059 	return ret;
1060 }
1061 
1062 static struct ncsi_rsp_handler {
1063 	unsigned char	type;
1064 	int             payload;
1065 	int		(*handler)(struct ncsi_request *nr);
1066 } ncsi_rsp_handlers[] = {
1067 	{ NCSI_PKT_RSP_CIS,     4, ncsi_rsp_handler_cis     },
1068 	{ NCSI_PKT_RSP_SP,      4, ncsi_rsp_handler_sp      },
1069 	{ NCSI_PKT_RSP_DP,      4, ncsi_rsp_handler_dp      },
1070 	{ NCSI_PKT_RSP_EC,      4, ncsi_rsp_handler_ec      },
1071 	{ NCSI_PKT_RSP_DC,      4, ncsi_rsp_handler_dc      },
1072 	{ NCSI_PKT_RSP_RC,      4, ncsi_rsp_handler_rc      },
1073 	{ NCSI_PKT_RSP_ECNT,    4, ncsi_rsp_handler_ecnt    },
1074 	{ NCSI_PKT_RSP_DCNT,    4, ncsi_rsp_handler_dcnt    },
1075 	{ NCSI_PKT_RSP_AE,      4, ncsi_rsp_handler_ae      },
1076 	{ NCSI_PKT_RSP_SL,      4, ncsi_rsp_handler_sl      },
1077 	{ NCSI_PKT_RSP_GLS,    16, ncsi_rsp_handler_gls     },
1078 	{ NCSI_PKT_RSP_SVF,     4, ncsi_rsp_handler_svf     },
1079 	{ NCSI_PKT_RSP_EV,      4, ncsi_rsp_handler_ev      },
1080 	{ NCSI_PKT_RSP_DV,      4, ncsi_rsp_handler_dv      },
1081 	{ NCSI_PKT_RSP_SMA,     4, ncsi_rsp_handler_sma     },
1082 	{ NCSI_PKT_RSP_EBF,     4, ncsi_rsp_handler_ebf     },
1083 	{ NCSI_PKT_RSP_DBF,     4, ncsi_rsp_handler_dbf     },
1084 	{ NCSI_PKT_RSP_EGMF,    4, ncsi_rsp_handler_egmf    },
1085 	{ NCSI_PKT_RSP_DGMF,    4, ncsi_rsp_handler_dgmf    },
1086 	{ NCSI_PKT_RSP_SNFC,    4, ncsi_rsp_handler_snfc    },
1087 	{ NCSI_PKT_RSP_GVI,    40, ncsi_rsp_handler_gvi     },
1088 	{ NCSI_PKT_RSP_GC,     32, ncsi_rsp_handler_gc      },
1089 	{ NCSI_PKT_RSP_GP,     -1, ncsi_rsp_handler_gp      },
1090 	{ NCSI_PKT_RSP_GCPS,  172, ncsi_rsp_handler_gcps    },
1091 	{ NCSI_PKT_RSP_GNS,   172, ncsi_rsp_handler_gns     },
1092 	{ NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts   },
1093 	{ NCSI_PKT_RSP_GPS,     8, ncsi_rsp_handler_gps     },
1094 	{ NCSI_PKT_RSP_OEM,    -1, ncsi_rsp_handler_oem     },
1095 	{ NCSI_PKT_RSP_PLDM,    0, NULL                     },
1096 	{ NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid  }
1097 };
1098 
1099 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
1100 		 struct packet_type *pt, struct net_device *orig_dev)
1101 {
1102 	struct ncsi_rsp_handler *nrh = NULL;
1103 	struct ncsi_dev *nd;
1104 	struct ncsi_dev_priv *ndp;
1105 	struct ncsi_request *nr;
1106 	struct ncsi_pkt_hdr *hdr;
1107 	unsigned long flags;
1108 	int payload, i, ret;
1109 
1110 	/* Find the NCSI device */
1111 	nd = ncsi_find_dev(dev);
1112 	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1113 	if (!ndp)
1114 		return -ENODEV;
1115 
1116 	/* Check if it is AEN packet */
1117 	hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
1118 	if (hdr->type == NCSI_PKT_AEN)
1119 		return ncsi_aen_handler(ndp, skb);
1120 
1121 	/* Find the handler */
1122 	for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
1123 		if (ncsi_rsp_handlers[i].type == hdr->type) {
1124 			if (ncsi_rsp_handlers[i].handler)
1125 				nrh = &ncsi_rsp_handlers[i];
1126 			else
1127 				nrh = NULL;
1128 
1129 			break;
1130 		}
1131 	}
1132 
1133 	if (!nrh) {
1134 		netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1135 			   hdr->type);
1136 		return -ENOENT;
1137 	}
1138 
1139 	/* Associate with the request */
1140 	spin_lock_irqsave(&ndp->lock, flags);
1141 	nr = &ndp->requests[hdr->id];
1142 	if (!nr->used) {
1143 		spin_unlock_irqrestore(&ndp->lock, flags);
1144 		return -ENODEV;
1145 	}
1146 
1147 	nr->rsp = skb;
1148 	if (!nr->enabled) {
1149 		spin_unlock_irqrestore(&ndp->lock, flags);
1150 		ret = -ENOENT;
1151 		goto out;
1152 	}
1153 
1154 	/* Validate the packet */
1155 	spin_unlock_irqrestore(&ndp->lock, flags);
1156 	payload = nrh->payload;
1157 	if (payload < 0)
1158 		payload = ntohs(hdr->length);
1159 	ret = ncsi_validate_rsp_pkt(nr, payload);
1160 	if (ret) {
1161 		netdev_warn(ndp->ndev.dev,
1162 			    "NCSI: 'bad' packet ignored for type 0x%x\n",
1163 			    hdr->type);
1164 
1165 		if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1166 			if (ret == -EPERM)
1167 				goto out_netlink;
1168 			else
1169 				ncsi_send_netlink_err(ndp->ndev.dev,
1170 						      nr->snd_seq,
1171 						      nr->snd_portid,
1172 						      &nr->nlhdr,
1173 						      ret);
1174 		}
1175 		goto out;
1176 	}
1177 
1178 	/* Process the packet */
1179 	ret = nrh->handler(nr);
1180 	if (ret)
1181 		netdev_err(ndp->ndev.dev,
1182 			   "NCSI: Handler for packet type 0x%x returned %d\n",
1183 			   hdr->type, ret);
1184 
1185 out_netlink:
1186 	if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1187 		ret = ncsi_rsp_handler_netlink(nr);
1188 		if (ret) {
1189 			netdev_err(ndp->ndev.dev,
1190 				   "NCSI: Netlink handler for packet type 0x%x returned %d\n",
1191 				   hdr->type, ret);
1192 		}
1193 	}
1194 
1195 out:
1196 	ncsi_free_request(nr);
1197 	return ret;
1198 }
1199