]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ncsi/ncsi-aen.c
net/ncsi: Don't enable all channels when HWA available
[linux.git] / net / ncsi / ncsi-aen.c
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_aen_pkt(struct ncsi_aen_pkt_hdr *h,
24                                  const unsigned short payload)
25 {
26         u32 checksum;
27         __be32 *pchecksum;
28
29         if (h->common.revision != NCSI_PKT_REVISION)
30                 return -EINVAL;
31         if (ntohs(h->common.length) != payload)
32                 return -EINVAL;
33
34         /* Validate checksum, which might be zeroes if the
35          * sender doesn't support checksum according to NCSI
36          * specification.
37          */
38         pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
39         if (ntohl(*pchecksum) == 0)
40                 return 0;
41
42         checksum = ncsi_calculate_checksum((unsigned char *)h,
43                                            sizeof(*h) + payload - 4);
44         if (*pchecksum != htonl(checksum))
45                 return -EINVAL;
46
47         return 0;
48 }
49
50 static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
51                                 struct ncsi_aen_pkt_hdr *h)
52 {
53         struct ncsi_aen_lsc_pkt *lsc;
54         struct ncsi_channel *nc;
55         struct ncsi_channel_mode *ncm;
56         bool chained;
57         int state;
58         unsigned long old_data, data;
59         unsigned long flags;
60
61         /* Find the NCSI channel */
62         ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
63         if (!nc)
64                 return -ENODEV;
65
66         /* Update the link status */
67         lsc = (struct ncsi_aen_lsc_pkt *)h;
68
69         spin_lock_irqsave(&nc->lock, flags);
70         ncm = &nc->modes[NCSI_MODE_LINK];
71         old_data = ncm->data[2];
72         data = ntohl(lsc->status);
73         ncm->data[2] = data;
74         ncm->data[4] = ntohl(lsc->oem_status);
75
76         netdev_dbg(ndp->ndev.dev, "NCSI: LSC AEN - channel %u state %s\n",
77                    nc->id, data & 0x1 ? "up" : "down");
78
79         chained = !list_empty(&nc->link);
80         state = nc->state;
81         spin_unlock_irqrestore(&nc->lock, flags);
82
83         if (!((old_data ^ data) & 0x1) || chained)
84                 return 0;
85         if (!(state == NCSI_CHANNEL_INACTIVE && (data & 0x1)) &&
86             !(state == NCSI_CHANNEL_ACTIVE && !(data & 0x1)))
87                 return 0;
88
89         if (state == NCSI_CHANNEL_ACTIVE)
90                 ndp->flags |= NCSI_DEV_RESHUFFLE;
91
92         ncsi_stop_channel_monitor(nc);
93         spin_lock_irqsave(&ndp->lock, flags);
94         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
95         spin_unlock_irqrestore(&ndp->lock, flags);
96
97         return ncsi_process_next_channel(ndp);
98 }
99
100 static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
101                                struct ncsi_aen_pkt_hdr *h)
102 {
103         struct ncsi_channel *nc;
104         unsigned long flags;
105
106         /* Find the NCSI channel */
107         ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
108         if (!nc)
109                 return -ENODEV;
110
111         spin_lock_irqsave(&nc->lock, flags);
112         if (!list_empty(&nc->link) ||
113             nc->state != NCSI_CHANNEL_ACTIVE) {
114                 spin_unlock_irqrestore(&nc->lock, flags);
115                 return 0;
116         }
117         spin_unlock_irqrestore(&nc->lock, flags);
118
119         ncsi_stop_channel_monitor(nc);
120         spin_lock_irqsave(&nc->lock, flags);
121         nc->state = NCSI_CHANNEL_INVISIBLE;
122         spin_unlock_irqrestore(&nc->lock, flags);
123
124         spin_lock_irqsave(&ndp->lock, flags);
125         nc->state = NCSI_CHANNEL_INACTIVE;
126         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
127         spin_unlock_irqrestore(&ndp->lock, flags);
128
129         return ncsi_process_next_channel(ndp);
130 }
131
132 static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
133                                    struct ncsi_aen_pkt_hdr *h)
134 {
135         struct ncsi_channel *nc;
136         struct ncsi_channel_mode *ncm;
137         struct ncsi_aen_hncdsc_pkt *hncdsc;
138         unsigned long flags;
139
140         /* Find the NCSI channel */
141         ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
142         if (!nc)
143                 return -ENODEV;
144
145         spin_lock_irqsave(&nc->lock, flags);
146         ncm = &nc->modes[NCSI_MODE_LINK];
147         hncdsc = (struct ncsi_aen_hncdsc_pkt *)h;
148         ncm->data[3] = ntohl(hncdsc->status);
149         spin_unlock_irqrestore(&nc->lock, flags);
150         netdev_dbg(ndp->ndev.dev,
151                    "NCSI: host driver %srunning on channel %u\n",
152                    ncm->data[3] & 0x1 ? "" : "not ", nc->id);
153
154         return 0;
155 }
156
157 static struct ncsi_aen_handler {
158         unsigned char type;
159         int           payload;
160         int           (*handler)(struct ncsi_dev_priv *ndp,
161                                  struct ncsi_aen_pkt_hdr *h);
162 } ncsi_aen_handlers[] = {
163         { NCSI_PKT_AEN_LSC,    12, ncsi_aen_handler_lsc    },
164         { NCSI_PKT_AEN_CR,      4, ncsi_aen_handler_cr     },
165         { NCSI_PKT_AEN_HNCDSC,  8, ncsi_aen_handler_hncdsc }
166 };
167
168 int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb)
169 {
170         struct ncsi_aen_pkt_hdr *h;
171         struct ncsi_aen_handler *nah = NULL;
172         int i, ret;
173
174         /* Find the handler */
175         h = (struct ncsi_aen_pkt_hdr *)skb_network_header(skb);
176         for (i = 0; i < ARRAY_SIZE(ncsi_aen_handlers); i++) {
177                 if (ncsi_aen_handlers[i].type == h->type) {
178                         nah = &ncsi_aen_handlers[i];
179                         break;
180                 }
181         }
182
183         if (!nah) {
184                 netdev_warn(ndp->ndev.dev, "Invalid AEN (0x%x) received\n",
185                             h->type);
186                 return -ENOENT;
187         }
188
189         ret = ncsi_validate_aen_pkt(h, nah->payload);
190         if (ret) {
191                 netdev_warn(ndp->ndev.dev,
192                             "NCSI: 'bad' packet ignored for AEN type 0x%x\n",
193                             h->type);
194                 goto out;
195         }
196
197         ret = nah->handler(ndp, h);
198         if (ret)
199                 netdev_err(ndp->ndev.dev,
200                            "NCSI: Handler for AEN type 0x%x returned %d\n",
201                            h->type, ret);
202 out:
203         consume_skb(skb);
204         return ret;
205 }