ncsi-aen.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright Gavin Shan, IBM Corporation 2016.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <net/ncsi.h>
  11. #include <net/net_namespace.h>
  12. #include <net/sock.h>
  13. #include "internal.h"
  14. #include "ncsi-pkt.h"
  15. static int ncsi_validate_aen_pkt(struct ncsi_aen_pkt_hdr *h,
  16. const unsigned short payload)
  17. {
  18. u32 checksum;
  19. __be32 *pchecksum;
  20. if (h->common.revision != NCSI_PKT_REVISION)
  21. return -EINVAL;
  22. if (ntohs(h->common.length) != payload)
  23. return -EINVAL;
  24. /* Validate checksum, which might be zeroes if the
  25. * sender doesn't support checksum according to NCSI
  26. * specification.
  27. */
  28. pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
  29. if (ntohl(*pchecksum) == 0)
  30. return 0;
  31. checksum = ncsi_calculate_checksum((unsigned char *)h,
  32. sizeof(*h) + payload - 4);
  33. if (*pchecksum != htonl(checksum))
  34. return -EINVAL;
  35. return 0;
  36. }
  37. static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
  38. struct ncsi_aen_pkt_hdr *h)
  39. {
  40. struct ncsi_channel *nc, *tmp;
  41. struct ncsi_channel_mode *ncm;
  42. unsigned long old_data, data;
  43. struct ncsi_aen_lsc_pkt *lsc;
  44. struct ncsi_package *np;
  45. bool had_link, has_link;
  46. unsigned long flags;
  47. bool chained;
  48. int state;
  49. /* Find the NCSI channel */
  50. ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
  51. if (!nc)
  52. return -ENODEV;
  53. /* Update the link status */
  54. lsc = (struct ncsi_aen_lsc_pkt *)h;
  55. spin_lock_irqsave(&nc->lock, flags);
  56. ncm = &nc->modes[NCSI_MODE_LINK];
  57. old_data = ncm->data[2];
  58. data = ntohl(lsc->status);
  59. ncm->data[2] = data;
  60. ncm->data[4] = ntohl(lsc->oem_status);
  61. had_link = !!(old_data & 0x1);
  62. has_link = !!(data & 0x1);
  63. netdev_dbg(ndp->ndev.dev, "NCSI: LSC AEN - channel %u state %s\n",
  64. nc->id, data & 0x1 ? "up" : "down");
  65. chained = !list_empty(&nc->link);
  66. state = nc->state;
  67. spin_unlock_irqrestore(&nc->lock, flags);
  68. if (state == NCSI_CHANNEL_INACTIVE)
  69. netdev_warn(ndp->ndev.dev,
  70. "NCSI: Inactive channel %u received AEN!\n",
  71. nc->id);
  72. if ((had_link == has_link) || chained)
  73. return 0;
  74. if (!ndp->multi_package && !nc->package->multi_channel) {
  75. if (had_link) {
  76. ndp->flags |= NCSI_DEV_RESHUFFLE;
  77. ncsi_stop_channel_monitor(nc);
  78. spin_lock_irqsave(&ndp->lock, flags);
  79. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  80. spin_unlock_irqrestore(&ndp->lock, flags);
  81. return ncsi_process_next_channel(ndp);
  82. }
  83. /* Configured channel came up */
  84. return 0;
  85. }
  86. if (had_link) {
  87. ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
  88. if (ncsi_channel_is_last(ndp, nc)) {
  89. /* No channels left, reconfigure */
  90. return ncsi_reset_dev(&ndp->ndev);
  91. } else if (ncm->enable) {
  92. /* Need to failover Tx channel */
  93. ncsi_update_tx_channel(ndp, nc->package, nc, NULL);
  94. }
  95. } else if (has_link && nc->package->preferred_channel == nc) {
  96. /* Return Tx to preferred channel */
  97. ncsi_update_tx_channel(ndp, nc->package, NULL, nc);
  98. } else if (has_link) {
  99. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  100. NCSI_FOR_EACH_CHANNEL(np, tmp) {
  101. /* Enable Tx on this channel if the current Tx
  102. * channel is down.
  103. */
  104. ncm = &tmp->modes[NCSI_MODE_TX_ENABLE];
  105. if (ncm->enable &&
  106. !ncsi_channel_has_link(tmp)) {
  107. ncsi_update_tx_channel(ndp, nc->package,
  108. tmp, nc);
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. /* Leave configured channels active in a multi-channel scenario so
  115. * AEN events are still received.
  116. */
  117. return 0;
  118. }
  119. static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
  120. struct ncsi_aen_pkt_hdr *h)
  121. {
  122. struct ncsi_channel *nc;
  123. unsigned long flags;
  124. /* Find the NCSI channel */
  125. ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
  126. if (!nc)
  127. return -ENODEV;
  128. spin_lock_irqsave(&nc->lock, flags);
  129. if (!list_empty(&nc->link) ||
  130. nc->state != NCSI_CHANNEL_ACTIVE) {
  131. spin_unlock_irqrestore(&nc->lock, flags);
  132. return 0;
  133. }
  134. spin_unlock_irqrestore(&nc->lock, flags);
  135. ncsi_stop_channel_monitor(nc);
  136. spin_lock_irqsave(&nc->lock, flags);
  137. nc->state = NCSI_CHANNEL_INVISIBLE;
  138. spin_unlock_irqrestore(&nc->lock, flags);
  139. spin_lock_irqsave(&ndp->lock, flags);
  140. nc->state = NCSI_CHANNEL_INACTIVE;
  141. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  142. spin_unlock_irqrestore(&ndp->lock, flags);
  143. nc->modes[NCSI_MODE_TX_ENABLE].enable = 0;
  144. return ncsi_process_next_channel(ndp);
  145. }
  146. static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
  147. struct ncsi_aen_pkt_hdr *h)
  148. {
  149. struct ncsi_channel *nc;
  150. struct ncsi_channel_mode *ncm;
  151. struct ncsi_aen_hncdsc_pkt *hncdsc;
  152. unsigned long flags;
  153. /* Find the NCSI channel */
  154. ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
  155. if (!nc)
  156. return -ENODEV;
  157. spin_lock_irqsave(&nc->lock, flags);
  158. ncm = &nc->modes[NCSI_MODE_LINK];
  159. hncdsc = (struct ncsi_aen_hncdsc_pkt *)h;
  160. ncm->data[3] = ntohl(hncdsc->status);
  161. spin_unlock_irqrestore(&nc->lock, flags);
  162. netdev_dbg(ndp->ndev.dev,
  163. "NCSI: host driver %srunning on channel %u\n",
  164. ncm->data[3] & 0x1 ? "" : "not ", nc->id);
  165. return 0;
  166. }
  167. static struct ncsi_aen_handler {
  168. unsigned char type;
  169. int payload;
  170. int (*handler)(struct ncsi_dev_priv *ndp,
  171. struct ncsi_aen_pkt_hdr *h);
  172. } ncsi_aen_handlers[] = {
  173. { NCSI_PKT_AEN_LSC, 12, ncsi_aen_handler_lsc },
  174. { NCSI_PKT_AEN_CR, 4, ncsi_aen_handler_cr },
  175. { NCSI_PKT_AEN_HNCDSC, 8, ncsi_aen_handler_hncdsc }
  176. };
  177. int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb)
  178. {
  179. struct ncsi_aen_pkt_hdr *h;
  180. struct ncsi_aen_handler *nah = NULL;
  181. int i, ret;
  182. /* Find the handler */
  183. h = (struct ncsi_aen_pkt_hdr *)skb_network_header(skb);
  184. for (i = 0; i < ARRAY_SIZE(ncsi_aen_handlers); i++) {
  185. if (ncsi_aen_handlers[i].type == h->type) {
  186. nah = &ncsi_aen_handlers[i];
  187. break;
  188. }
  189. }
  190. if (!nah) {
  191. netdev_warn(ndp->ndev.dev, "Invalid AEN (0x%x) received\n",
  192. h->type);
  193. return -ENOENT;
  194. }
  195. ret = ncsi_validate_aen_pkt(h, nah->payload);
  196. if (ret) {
  197. netdev_warn(ndp->ndev.dev,
  198. "NCSI: 'bad' packet ignored for AEN type 0x%x\n",
  199. h->type);
  200. goto out;
  201. }
  202. ret = nah->handler(ndp, h);
  203. if (ret)
  204. netdev_err(ndp->ndev.dev,
  205. "NCSI: Handler for AEN type 0x%x returned %d\n",
  206. h->type, ret);
  207. out:
  208. consume_skb(skb);
  209. return ret;
  210. }