ipath_layer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <linux/pci.h>
  39. #include <asm/byteorder.h>
  40. #include "ipath_kernel.h"
  41. #include "ipath_layer.h"
  42. #include "ipath_verbs.h"
  43. #include "ipath_common.h"
  44. /* Acquire before ipath_devs_lock. */
  45. static DEFINE_MUTEX(ipath_layer_mutex);
  46. u16 ipath_layer_rcv_opcode;
  47. static int (*layer_intr)(void *, u32);
  48. static int (*layer_rcv)(void *, void *, struct sk_buff *);
  49. static int (*layer_rcv_lid)(void *, void *);
  50. static void *(*layer_add_one)(int, struct ipath_devdata *);
  51. static void (*layer_remove_one)(void *);
  52. int __ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
  53. {
  54. int ret = -ENODEV;
  55. if (dd->ipath_layer.l_arg && layer_intr)
  56. ret = layer_intr(dd->ipath_layer.l_arg, arg);
  57. return ret;
  58. }
  59. int ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
  60. {
  61. int ret;
  62. mutex_lock(&ipath_layer_mutex);
  63. ret = __ipath_layer_intr(dd, arg);
  64. mutex_unlock(&ipath_layer_mutex);
  65. return ret;
  66. }
  67. int __ipath_layer_rcv(struct ipath_devdata *dd, void *hdr,
  68. struct sk_buff *skb)
  69. {
  70. int ret = -ENODEV;
  71. if (dd->ipath_layer.l_arg && layer_rcv)
  72. ret = layer_rcv(dd->ipath_layer.l_arg, hdr, skb);
  73. return ret;
  74. }
  75. int __ipath_layer_rcv_lid(struct ipath_devdata *dd, void *hdr)
  76. {
  77. int ret = -ENODEV;
  78. if (dd->ipath_layer.l_arg && layer_rcv_lid)
  79. ret = layer_rcv_lid(dd->ipath_layer.l_arg, hdr);
  80. return ret;
  81. }
  82. void ipath_layer_lid_changed(struct ipath_devdata *dd)
  83. {
  84. mutex_lock(&ipath_layer_mutex);
  85. if (dd->ipath_layer.l_arg && layer_intr)
  86. layer_intr(dd->ipath_layer.l_arg, IPATH_LAYER_INT_LID);
  87. mutex_unlock(&ipath_layer_mutex);
  88. }
  89. void ipath_layer_add(struct ipath_devdata *dd)
  90. {
  91. mutex_lock(&ipath_layer_mutex);
  92. if (layer_add_one)
  93. dd->ipath_layer.l_arg =
  94. layer_add_one(dd->ipath_unit, dd);
  95. mutex_unlock(&ipath_layer_mutex);
  96. }
  97. void ipath_layer_remove(struct ipath_devdata *dd)
  98. {
  99. mutex_lock(&ipath_layer_mutex);
  100. if (dd->ipath_layer.l_arg && layer_remove_one) {
  101. layer_remove_one(dd->ipath_layer.l_arg);
  102. dd->ipath_layer.l_arg = NULL;
  103. }
  104. mutex_unlock(&ipath_layer_mutex);
  105. }
  106. int ipath_layer_register(void *(*l_add)(int, struct ipath_devdata *),
  107. void (*l_remove)(void *),
  108. int (*l_intr)(void *, u32),
  109. int (*l_rcv)(void *, void *, struct sk_buff *),
  110. u16 l_rcv_opcode,
  111. int (*l_rcv_lid)(void *, void *))
  112. {
  113. struct ipath_devdata *dd, *tmp;
  114. unsigned long flags;
  115. mutex_lock(&ipath_layer_mutex);
  116. layer_add_one = l_add;
  117. layer_remove_one = l_remove;
  118. layer_intr = l_intr;
  119. layer_rcv = l_rcv;
  120. layer_rcv_lid = l_rcv_lid;
  121. ipath_layer_rcv_opcode = l_rcv_opcode;
  122. spin_lock_irqsave(&ipath_devs_lock, flags);
  123. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  124. if (!(dd->ipath_flags & IPATH_INITTED))
  125. continue;
  126. if (dd->ipath_layer.l_arg)
  127. continue;
  128. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  129. dd->ipath_layer.l_arg = l_add(dd->ipath_unit, dd);
  130. spin_lock_irqsave(&ipath_devs_lock, flags);
  131. }
  132. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  133. mutex_unlock(&ipath_layer_mutex);
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(ipath_layer_register);
  137. void ipath_layer_unregister(void)
  138. {
  139. struct ipath_devdata *dd, *tmp;
  140. unsigned long flags;
  141. mutex_lock(&ipath_layer_mutex);
  142. spin_lock_irqsave(&ipath_devs_lock, flags);
  143. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  144. if (dd->ipath_layer.l_arg && layer_remove_one) {
  145. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  146. layer_remove_one(dd->ipath_layer.l_arg);
  147. spin_lock_irqsave(&ipath_devs_lock, flags);
  148. dd->ipath_layer.l_arg = NULL;
  149. }
  150. }
  151. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  152. layer_add_one = NULL;
  153. layer_remove_one = NULL;
  154. layer_intr = NULL;
  155. layer_rcv = NULL;
  156. layer_rcv_lid = NULL;
  157. mutex_unlock(&ipath_layer_mutex);
  158. }
  159. EXPORT_SYMBOL_GPL(ipath_layer_unregister);
  160. int ipath_layer_open(struct ipath_devdata *dd, u32 * pktmax)
  161. {
  162. int ret;
  163. u32 intval = 0;
  164. mutex_lock(&ipath_layer_mutex);
  165. if (!dd->ipath_layer.l_arg) {
  166. ret = -EINVAL;
  167. goto bail;
  168. }
  169. ret = ipath_setrcvhdrsize(dd, IPATH_HEADER_QUEUE_WORDS);
  170. if (ret < 0)
  171. goto bail;
  172. *pktmax = dd->ipath_ibmaxlen;
  173. if (*dd->ipath_statusp & IPATH_STATUS_IB_READY)
  174. intval |= IPATH_LAYER_INT_IF_UP;
  175. if (dd->ipath_lid)
  176. intval |= IPATH_LAYER_INT_LID;
  177. if (dd->ipath_mlid)
  178. intval |= IPATH_LAYER_INT_BCAST;
  179. /*
  180. * do this on open, in case low level is already up and
  181. * just layered driver was reloaded, etc.
  182. */
  183. if (intval)
  184. layer_intr(dd->ipath_layer.l_arg, intval);
  185. ret = 0;
  186. bail:
  187. mutex_unlock(&ipath_layer_mutex);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL_GPL(ipath_layer_open);
  191. u16 ipath_layer_get_lid(struct ipath_devdata *dd)
  192. {
  193. return dd->ipath_lid;
  194. }
  195. EXPORT_SYMBOL_GPL(ipath_layer_get_lid);
  196. /**
  197. * ipath_layer_get_mac - get the MAC address
  198. * @dd: the infinipath device
  199. * @mac: the MAC is put here
  200. *
  201. * This is the EUID-64 OUI octets (top 3), then
  202. * skip the next 2 (which should both be zero or 0xff).
  203. * The returned MAC is in network order
  204. * mac points to at least 6 bytes of buffer
  205. * We assume that by the time the LID is set, that the GUID is as valid
  206. * as it's ever going to be, rather than adding yet another status bit.
  207. */
  208. int ipath_layer_get_mac(struct ipath_devdata *dd, u8 * mac)
  209. {
  210. u8 *guid;
  211. guid = (u8 *) &dd->ipath_guid;
  212. mac[0] = guid[0];
  213. mac[1] = guid[1];
  214. mac[2] = guid[2];
  215. mac[3] = guid[5];
  216. mac[4] = guid[6];
  217. mac[5] = guid[7];
  218. if ((guid[3] || guid[4]) && !(guid[3] == 0xff && guid[4] == 0xff))
  219. ipath_dbg("Warning, guid bytes 3 and 4 not 0 or 0xffff: "
  220. "%x %x\n", guid[3], guid[4]);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL_GPL(ipath_layer_get_mac);
  224. u16 ipath_layer_get_bcast(struct ipath_devdata *dd)
  225. {
  226. return dd->ipath_mlid;
  227. }
  228. EXPORT_SYMBOL_GPL(ipath_layer_get_bcast);
  229. int ipath_layer_send_hdr(struct ipath_devdata *dd, struct ether_header *hdr)
  230. {
  231. int ret = 0;
  232. u32 __iomem *piobuf;
  233. u32 plen, *uhdr;
  234. size_t count;
  235. __be16 vlsllnh;
  236. if (!(dd->ipath_flags & IPATH_RCVHDRSZ_SET)) {
  237. ipath_dbg("send while not open\n");
  238. ret = -EINVAL;
  239. } else
  240. if ((dd->ipath_flags & (IPATH_LINKUNK | IPATH_LINKDOWN)) ||
  241. dd->ipath_lid == 0) {
  242. /*
  243. * lid check is for when sma hasn't yet configured
  244. */
  245. ret = -ENETDOWN;
  246. ipath_cdbg(VERBOSE, "send while not ready, "
  247. "mylid=%u, flags=0x%x\n",
  248. dd->ipath_lid, dd->ipath_flags);
  249. }
  250. vlsllnh = *((__be16 *) hdr);
  251. if (vlsllnh != htons(IPATH_LRH_BTH)) {
  252. ipath_dbg("Warning: lrh[0] wrong (%x, not %x); "
  253. "not sending\n", be16_to_cpu(vlsllnh),
  254. IPATH_LRH_BTH);
  255. ret = -EINVAL;
  256. }
  257. if (ret)
  258. goto done;
  259. /* Get a PIO buffer to use. */
  260. piobuf = ipath_getpiobuf(dd, NULL);
  261. if (piobuf == NULL) {
  262. ret = -EBUSY;
  263. goto done;
  264. }
  265. plen = (sizeof(*hdr) >> 2); /* actual length */
  266. ipath_cdbg(EPKT, "0x%x+1w pio %p\n", plen, piobuf);
  267. writeq(plen+1, piobuf); /* len (+1 for pad) to pbc, no flags */
  268. ipath_flush_wc();
  269. piobuf += 2;
  270. uhdr = (u32 *)hdr;
  271. count = plen-1; /* amount we can copy before trigger word */
  272. __iowrite32_copy(piobuf, uhdr, count);
  273. ipath_flush_wc();
  274. __raw_writel(uhdr[count], piobuf + count);
  275. ipath_flush_wc(); /* ensure it's sent, now */
  276. ipath_stats.sps_ether_spkts++; /* ether packet sent */
  277. done:
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(ipath_layer_send_hdr);
  281. int ipath_layer_set_piointbufavail_int(struct ipath_devdata *dd)
  282. {
  283. set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
  284. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  285. dd->ipath_sendctrl);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(ipath_layer_set_piointbufavail_int);