ipath_layer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. if (!(*dd->ipath_statusp & IPATH_STATUS_SMA))
  129. *dd->ipath_statusp |= IPATH_STATUS_OIB_SMA;
  130. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  131. dd->ipath_layer.l_arg = l_add(dd->ipath_unit, dd);
  132. spin_lock_irqsave(&ipath_devs_lock, flags);
  133. }
  134. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  135. mutex_unlock(&ipath_layer_mutex);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(ipath_layer_register);
  139. void ipath_layer_unregister(void)
  140. {
  141. struct ipath_devdata *dd, *tmp;
  142. unsigned long flags;
  143. mutex_lock(&ipath_layer_mutex);
  144. spin_lock_irqsave(&ipath_devs_lock, flags);
  145. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  146. if (dd->ipath_layer.l_arg && layer_remove_one) {
  147. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  148. layer_remove_one(dd->ipath_layer.l_arg);
  149. spin_lock_irqsave(&ipath_devs_lock, flags);
  150. dd->ipath_layer.l_arg = NULL;
  151. }
  152. }
  153. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  154. layer_add_one = NULL;
  155. layer_remove_one = NULL;
  156. layer_intr = NULL;
  157. layer_rcv = NULL;
  158. layer_rcv_lid = NULL;
  159. mutex_unlock(&ipath_layer_mutex);
  160. }
  161. EXPORT_SYMBOL_GPL(ipath_layer_unregister);
  162. int ipath_layer_open(struct ipath_devdata *dd, u32 * pktmax)
  163. {
  164. int ret;
  165. u32 intval = 0;
  166. mutex_lock(&ipath_layer_mutex);
  167. if (!dd->ipath_layer.l_arg) {
  168. ret = -EINVAL;
  169. goto bail;
  170. }
  171. ret = ipath_setrcvhdrsize(dd, IPATH_HEADER_QUEUE_WORDS);
  172. if (ret < 0)
  173. goto bail;
  174. *pktmax = dd->ipath_ibmaxlen;
  175. if (*dd->ipath_statusp & IPATH_STATUS_IB_READY)
  176. intval |= IPATH_LAYER_INT_IF_UP;
  177. if (dd->ipath_lid)
  178. intval |= IPATH_LAYER_INT_LID;
  179. if (dd->ipath_mlid)
  180. intval |= IPATH_LAYER_INT_BCAST;
  181. /*
  182. * do this on open, in case low level is already up and
  183. * just layered driver was reloaded, etc.
  184. */
  185. if (intval)
  186. layer_intr(dd->ipath_layer.l_arg, intval);
  187. ret = 0;
  188. bail:
  189. mutex_unlock(&ipath_layer_mutex);
  190. return ret;
  191. }
  192. EXPORT_SYMBOL_GPL(ipath_layer_open);
  193. u16 ipath_layer_get_lid(struct ipath_devdata *dd)
  194. {
  195. return dd->ipath_lid;
  196. }
  197. EXPORT_SYMBOL_GPL(ipath_layer_get_lid);
  198. /**
  199. * ipath_layer_get_mac - get the MAC address
  200. * @dd: the infinipath device
  201. * @mac: the MAC is put here
  202. *
  203. * This is the EUID-64 OUI octets (top 3), then
  204. * skip the next 2 (which should both be zero or 0xff).
  205. * The returned MAC is in network order
  206. * mac points to at least 6 bytes of buffer
  207. * We assume that by the time the LID is set, that the GUID is as valid
  208. * as it's ever going to be, rather than adding yet another status bit.
  209. */
  210. int ipath_layer_get_mac(struct ipath_devdata *dd, u8 * mac)
  211. {
  212. u8 *guid;
  213. guid = (u8 *) &dd->ipath_guid;
  214. mac[0] = guid[0];
  215. mac[1] = guid[1];
  216. mac[2] = guid[2];
  217. mac[3] = guid[5];
  218. mac[4] = guid[6];
  219. mac[5] = guid[7];
  220. if ((guid[3] || guid[4]) && !(guid[3] == 0xff && guid[4] == 0xff))
  221. ipath_dbg("Warning, guid bytes 3 and 4 not 0 or 0xffff: "
  222. "%x %x\n", guid[3], guid[4]);
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(ipath_layer_get_mac);
  226. u16 ipath_layer_get_bcast(struct ipath_devdata *dd)
  227. {
  228. return dd->ipath_mlid;
  229. }
  230. EXPORT_SYMBOL_GPL(ipath_layer_get_bcast);
  231. int ipath_layer_send_hdr(struct ipath_devdata *dd, struct ether_header *hdr)
  232. {
  233. int ret = 0;
  234. u32 __iomem *piobuf;
  235. u32 plen, *uhdr;
  236. size_t count;
  237. __be16 vlsllnh;
  238. if (!(dd->ipath_flags & IPATH_RCVHDRSZ_SET)) {
  239. ipath_dbg("send while not open\n");
  240. ret = -EINVAL;
  241. } else
  242. if ((dd->ipath_flags & (IPATH_LINKUNK | IPATH_LINKDOWN)) ||
  243. dd->ipath_lid == 0) {
  244. /*
  245. * lid check is for when sma hasn't yet configured
  246. */
  247. ret = -ENETDOWN;
  248. ipath_cdbg(VERBOSE, "send while not ready, "
  249. "mylid=%u, flags=0x%x\n",
  250. dd->ipath_lid, dd->ipath_flags);
  251. }
  252. vlsllnh = *((__be16 *) hdr);
  253. if (vlsllnh != htons(IPATH_LRH_BTH)) {
  254. ipath_dbg("Warning: lrh[0] wrong (%x, not %x); "
  255. "not sending\n", be16_to_cpu(vlsllnh),
  256. IPATH_LRH_BTH);
  257. ret = -EINVAL;
  258. }
  259. if (ret)
  260. goto done;
  261. /* Get a PIO buffer to use. */
  262. piobuf = ipath_getpiobuf(dd, NULL);
  263. if (piobuf == NULL) {
  264. ret = -EBUSY;
  265. goto done;
  266. }
  267. plen = (sizeof(*hdr) >> 2); /* actual length */
  268. ipath_cdbg(EPKT, "0x%x+1w pio %p\n", plen, piobuf);
  269. writeq(plen+1, piobuf); /* len (+1 for pad) to pbc, no flags */
  270. ipath_flush_wc();
  271. piobuf += 2;
  272. uhdr = (u32 *)hdr;
  273. count = plen-1; /* amount we can copy before trigger word */
  274. __iowrite32_copy(piobuf, uhdr, count);
  275. ipath_flush_wc();
  276. __raw_writel(uhdr[count], piobuf + count);
  277. ipath_flush_wc(); /* ensure it's sent, now */
  278. ipath_stats.sps_ether_spkts++; /* ether packet sent */
  279. done:
  280. return ret;
  281. }
  282. EXPORT_SYMBOL_GPL(ipath_layer_send_hdr);
  283. int ipath_layer_set_piointbufavail_int(struct ipath_devdata *dd)
  284. {
  285. set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
  286. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  287. dd->ipath_sendctrl);
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(ipath_layer_set_piointbufavail_int);