ipath_layer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * These are the routines used by layered drivers, currently just the
  35. * layered ethernet driver and verbs layer.
  36. */
  37. #include <linux/io.h>
  38. #include <asm/byteorder.h>
  39. #include "ipath_kernel.h"
  40. #include "ipath_layer.h"
  41. #include "ipath_verbs.h"
  42. #include "ipath_common.h"
  43. /* Acquire before ipath_devs_lock. */
  44. static DEFINE_MUTEX(ipath_layer_mutex);
  45. u16 ipath_layer_rcv_opcode;
  46. static int (*layer_intr)(void *, u32);
  47. static int (*layer_rcv)(void *, void *, struct sk_buff *);
  48. static int (*layer_rcv_lid)(void *, void *);
  49. static void *(*layer_add_one)(int, struct ipath_devdata *);
  50. static void (*layer_remove_one)(void *);
  51. int __ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
  52. {
  53. int ret = -ENODEV;
  54. if (dd->ipath_layer.l_arg && layer_intr)
  55. ret = layer_intr(dd->ipath_layer.l_arg, arg);
  56. return ret;
  57. }
  58. int ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
  59. {
  60. int ret;
  61. mutex_lock(&ipath_layer_mutex);
  62. ret = __ipath_layer_intr(dd, arg);
  63. mutex_unlock(&ipath_layer_mutex);
  64. return ret;
  65. }
  66. int __ipath_layer_rcv(struct ipath_devdata *dd, void *hdr,
  67. struct sk_buff *skb)
  68. {
  69. int ret = -ENODEV;
  70. if (dd->ipath_layer.l_arg && layer_rcv)
  71. ret = layer_rcv(dd->ipath_layer.l_arg, hdr, skb);
  72. return ret;
  73. }
  74. int __ipath_layer_rcv_lid(struct ipath_devdata *dd, void *hdr)
  75. {
  76. int ret = -ENODEV;
  77. if (dd->ipath_layer.l_arg && layer_rcv_lid)
  78. ret = layer_rcv_lid(dd->ipath_layer.l_arg, hdr);
  79. return ret;
  80. }
  81. void ipath_layer_lid_changed(struct ipath_devdata *dd)
  82. {
  83. mutex_lock(&ipath_layer_mutex);
  84. if (dd->ipath_layer.l_arg && layer_intr)
  85. layer_intr(dd->ipath_layer.l_arg, IPATH_LAYER_INT_LID);
  86. mutex_unlock(&ipath_layer_mutex);
  87. }
  88. void ipath_layer_add(struct ipath_devdata *dd)
  89. {
  90. mutex_lock(&ipath_layer_mutex);
  91. if (layer_add_one)
  92. dd->ipath_layer.l_arg =
  93. layer_add_one(dd->ipath_unit, dd);
  94. mutex_unlock(&ipath_layer_mutex);
  95. }
  96. void ipath_layer_remove(struct ipath_devdata *dd)
  97. {
  98. mutex_lock(&ipath_layer_mutex);
  99. if (dd->ipath_layer.l_arg && layer_remove_one) {
  100. layer_remove_one(dd->ipath_layer.l_arg);
  101. dd->ipath_layer.l_arg = NULL;
  102. }
  103. mutex_unlock(&ipath_layer_mutex);
  104. }
  105. int ipath_layer_register(void *(*l_add)(int, struct ipath_devdata *),
  106. void (*l_remove)(void *),
  107. int (*l_intr)(void *, u32),
  108. int (*l_rcv)(void *, void *, struct sk_buff *),
  109. u16 l_rcv_opcode,
  110. int (*l_rcv_lid)(void *, void *))
  111. {
  112. struct ipath_devdata *dd, *tmp;
  113. unsigned long flags;
  114. mutex_lock(&ipath_layer_mutex);
  115. layer_add_one = l_add;
  116. layer_remove_one = l_remove;
  117. layer_intr = l_intr;
  118. layer_rcv = l_rcv;
  119. layer_rcv_lid = l_rcv_lid;
  120. ipath_layer_rcv_opcode = l_rcv_opcode;
  121. spin_lock_irqsave(&ipath_devs_lock, flags);
  122. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  123. if (!(dd->ipath_flags & IPATH_INITTED))
  124. continue;
  125. if (dd->ipath_layer.l_arg)
  126. continue;
  127. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  128. dd->ipath_layer.l_arg = l_add(dd->ipath_unit, dd);
  129. spin_lock_irqsave(&ipath_devs_lock, flags);
  130. }
  131. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  132. mutex_unlock(&ipath_layer_mutex);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(ipath_layer_register);
  136. void ipath_layer_unregister(void)
  137. {
  138. struct ipath_devdata *dd, *tmp;
  139. unsigned long flags;
  140. mutex_lock(&ipath_layer_mutex);
  141. spin_lock_irqsave(&ipath_devs_lock, flags);
  142. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  143. if (dd->ipath_layer.l_arg && layer_remove_one) {
  144. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  145. layer_remove_one(dd->ipath_layer.l_arg);
  146. spin_lock_irqsave(&ipath_devs_lock, flags);
  147. dd->ipath_layer.l_arg = NULL;
  148. }
  149. }
  150. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  151. layer_add_one = NULL;
  152. layer_remove_one = NULL;
  153. layer_intr = NULL;
  154. layer_rcv = NULL;
  155. layer_rcv_lid = NULL;
  156. mutex_unlock(&ipath_layer_mutex);
  157. }
  158. EXPORT_SYMBOL_GPL(ipath_layer_unregister);
  159. int ipath_layer_open(struct ipath_devdata *dd, u32 * pktmax)
  160. {
  161. int ret;
  162. u32 intval = 0;
  163. mutex_lock(&ipath_layer_mutex);
  164. if (!dd->ipath_layer.l_arg) {
  165. ret = -EINVAL;
  166. goto bail;
  167. }
  168. ret = ipath_setrcvhdrsize(dd, IPATH_HEADER_QUEUE_WORDS);
  169. if (ret < 0)
  170. goto bail;
  171. *pktmax = dd->ipath_ibmaxlen;
  172. if (*dd->ipath_statusp & IPATH_STATUS_IB_READY)
  173. intval |= IPATH_LAYER_INT_IF_UP;
  174. if (dd->ipath_lid)
  175. intval |= IPATH_LAYER_INT_LID;
  176. if (dd->ipath_mlid)
  177. intval |= IPATH_LAYER_INT_BCAST;
  178. /*
  179. * do this on open, in case low level is already up and
  180. * just layered driver was reloaded, etc.
  181. */
  182. if (intval)
  183. layer_intr(dd->ipath_layer.l_arg, intval);
  184. ret = 0;
  185. bail:
  186. mutex_unlock(&ipath_layer_mutex);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL_GPL(ipath_layer_open);
  190. u16 ipath_layer_get_lid(struct ipath_devdata *dd)
  191. {
  192. return dd->ipath_lid;
  193. }
  194. EXPORT_SYMBOL_GPL(ipath_layer_get_lid);
  195. /**
  196. * ipath_layer_get_mac - get the MAC address
  197. * @dd: the infinipath device
  198. * @mac: the MAC is put here
  199. *
  200. * This is the EUID-64 OUI octets (top 3), then
  201. * skip the next 2 (which should both be zero or 0xff).
  202. * The returned MAC is in network order
  203. * mac points to at least 6 bytes of buffer
  204. * We assume that by the time the LID is set, that the GUID is as valid
  205. * as it's ever going to be, rather than adding yet another status bit.
  206. */
  207. int ipath_layer_get_mac(struct ipath_devdata *dd, u8 * mac)
  208. {
  209. u8 *guid;
  210. guid = (u8 *) &dd->ipath_guid;
  211. mac[0] = guid[0];
  212. mac[1] = guid[1];
  213. mac[2] = guid[2];
  214. mac[3] = guid[5];
  215. mac[4] = guid[6];
  216. mac[5] = guid[7];
  217. if ((guid[3] || guid[4]) && !(guid[3] == 0xff && guid[4] == 0xff))
  218. ipath_dbg("Warning, guid bytes 3 and 4 not 0 or 0xffff: "
  219. "%x %x\n", guid[3], guid[4]);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(ipath_layer_get_mac);
  223. u16 ipath_layer_get_bcast(struct ipath_devdata *dd)
  224. {
  225. return dd->ipath_mlid;
  226. }
  227. EXPORT_SYMBOL_GPL(ipath_layer_get_bcast);
  228. int ipath_layer_send_hdr(struct ipath_devdata *dd, struct ether_header *hdr)
  229. {
  230. int ret = 0;
  231. u32 __iomem *piobuf;
  232. u32 plen, *uhdr;
  233. size_t count;
  234. __be16 vlsllnh;
  235. if (!(dd->ipath_flags & IPATH_RCVHDRSZ_SET)) {
  236. ipath_dbg("send while not open\n");
  237. ret = -EINVAL;
  238. } else
  239. if ((dd->ipath_flags & (IPATH_LINKUNK | IPATH_LINKDOWN)) ||
  240. dd->ipath_lid == 0) {
  241. /*
  242. * lid check is for when sma hasn't yet configured
  243. */
  244. ret = -ENETDOWN;
  245. ipath_cdbg(VERBOSE, "send while not ready, "
  246. "mylid=%u, flags=0x%x\n",
  247. dd->ipath_lid, dd->ipath_flags);
  248. }
  249. vlsllnh = *((__be16 *) hdr);
  250. if (vlsllnh != htons(IPATH_LRH_BTH)) {
  251. ipath_dbg("Warning: lrh[0] wrong (%x, not %x); "
  252. "not sending\n", be16_to_cpu(vlsllnh),
  253. IPATH_LRH_BTH);
  254. ret = -EINVAL;
  255. }
  256. if (ret)
  257. goto done;
  258. /* Get a PIO buffer to use. */
  259. piobuf = ipath_getpiobuf(dd, NULL);
  260. if (piobuf == NULL) {
  261. ret = -EBUSY;
  262. goto done;
  263. }
  264. plen = (sizeof(*hdr) >> 2); /* actual length */
  265. ipath_cdbg(EPKT, "0x%x+1w pio %p\n", plen, piobuf);
  266. writeq(plen+1, piobuf); /* len (+1 for pad) to pbc, no flags */
  267. ipath_flush_wc();
  268. piobuf += 2;
  269. uhdr = (u32 *)hdr;
  270. count = plen-1; /* amount we can copy before trigger word */
  271. __iowrite32_copy(piobuf, uhdr, count);
  272. ipath_flush_wc();
  273. __raw_writel(uhdr[count], piobuf + count);
  274. ipath_flush_wc(); /* ensure it's sent, now */
  275. ipath_stats.sps_ether_spkts++; /* ether packet sent */
  276. done:
  277. return ret;
  278. }
  279. EXPORT_SYMBOL_GPL(ipath_layer_send_hdr);
  280. int ipath_layer_set_piointbufavail_int(struct ipath_devdata *dd)
  281. {
  282. set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
  283. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  284. dd->ipath_sendctrl);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL_GPL(ipath_layer_set_piointbufavail_int);