rocker_tlv.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * drivers/net/ethernet/rocker/rocker_tlv.c - Rocker switch device driver
  3. * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
  4. * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/errno.h>
  14. #include "rocker_hw.h"
  15. #include "rocker_tlv.h"
  16. void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
  17. const char *buf, int buf_len)
  18. {
  19. const struct rocker_tlv *tlv;
  20. const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
  21. int rem;
  22. memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
  23. rocker_tlv_for_each(tlv, head, buf_len, rem) {
  24. u32 type = rocker_tlv_type(tlv);
  25. if (type > 0 && type <= maxtype)
  26. tb[type] = tlv;
  27. }
  28. }
  29. int rocker_tlv_put(struct rocker_desc_info *desc_info,
  30. int attrtype, int attrlen, const void *data)
  31. {
  32. int tail_room = desc_info->data_size - desc_info->tlv_size;
  33. int total_size = rocker_tlv_total_size(attrlen);
  34. struct rocker_tlv *tlv;
  35. if (unlikely(tail_room < total_size))
  36. return -EMSGSIZE;
  37. tlv = rocker_tlv_start(desc_info);
  38. desc_info->tlv_size += total_size;
  39. tlv->type = attrtype;
  40. tlv->len = rocker_tlv_attr_size(attrlen);
  41. memcpy(rocker_tlv_data(tlv), data, attrlen);
  42. memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
  43. return 0;
  44. }