ipoib_main.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "ipoib.h"
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  41. #include <linux/ip.h>
  42. #include <linux/in.h>
  43. #include <net/dst.h>
  44. MODULE_AUTHOR("Roland Dreier");
  45. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  48. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  49. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  50. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  51. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  52. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  53. static int lro;
  54. module_param(lro, bool, 0444);
  55. MODULE_PARM_DESC(lro, "Enable LRO (Large Receive Offload)");
  56. static int lro_max_aggr = IPOIB_LRO_MAX_AGGR;
  57. module_param(lro_max_aggr, int, 0644);
  58. MODULE_PARM_DESC(lro_max_aggr, "LRO: Max packets to be aggregated "
  59. "(default = 64)");
  60. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  61. int ipoib_debug_level;
  62. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  63. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  64. #endif
  65. struct ipoib_path_iter {
  66. struct net_device *dev;
  67. struct ipoib_path path;
  68. };
  69. static const u8 ipv4_bcast_addr[] = {
  70. 0x00, 0xff, 0xff, 0xff,
  71. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  73. };
  74. struct workqueue_struct *ipoib_workqueue;
  75. struct ib_sa_client ipoib_sa_client;
  76. static void ipoib_add_one(struct ib_device *device);
  77. static void ipoib_remove_one(struct ib_device *device);
  78. static struct ib_client ipoib_client = {
  79. .name = "ipoib",
  80. .add = ipoib_add_one,
  81. .remove = ipoib_remove_one
  82. };
  83. int ipoib_open(struct net_device *dev)
  84. {
  85. struct ipoib_dev_priv *priv = netdev_priv(dev);
  86. ipoib_dbg(priv, "bringing up interface\n");
  87. napi_enable(&priv->napi);
  88. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  89. if (ipoib_pkey_dev_delay_open(dev))
  90. return 0;
  91. if (ipoib_ib_dev_open(dev)) {
  92. napi_disable(&priv->napi);
  93. return -EINVAL;
  94. }
  95. if (ipoib_ib_dev_up(dev)) {
  96. ipoib_ib_dev_stop(dev, 1);
  97. napi_disable(&priv->napi);
  98. return -EINVAL;
  99. }
  100. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  101. struct ipoib_dev_priv *cpriv;
  102. /* Bring up any child interfaces too */
  103. mutex_lock(&priv->vlan_mutex);
  104. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  105. int flags;
  106. flags = cpriv->dev->flags;
  107. if (flags & IFF_UP)
  108. continue;
  109. dev_change_flags(cpriv->dev, flags | IFF_UP);
  110. }
  111. mutex_unlock(&priv->vlan_mutex);
  112. }
  113. netif_start_queue(dev);
  114. return 0;
  115. }
  116. static int ipoib_stop(struct net_device *dev)
  117. {
  118. struct ipoib_dev_priv *priv = netdev_priv(dev);
  119. ipoib_dbg(priv, "stopping interface\n");
  120. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  121. napi_disable(&priv->napi);
  122. netif_stop_queue(dev);
  123. ipoib_ib_dev_down(dev, 0);
  124. ipoib_ib_dev_stop(dev, 0);
  125. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  126. struct ipoib_dev_priv *cpriv;
  127. /* Bring down any child interfaces too */
  128. mutex_lock(&priv->vlan_mutex);
  129. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  130. int flags;
  131. flags = cpriv->dev->flags;
  132. if (!(flags & IFF_UP))
  133. continue;
  134. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  135. }
  136. mutex_unlock(&priv->vlan_mutex);
  137. }
  138. return 0;
  139. }
  140. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  141. {
  142. struct ipoib_dev_priv *priv = netdev_priv(dev);
  143. /* dev->mtu > 2K ==> connected mode */
  144. if (ipoib_cm_admin_enabled(dev)) {
  145. if (new_mtu > ipoib_cm_max_mtu(dev))
  146. return -EINVAL;
  147. if (new_mtu > priv->mcast_mtu)
  148. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  149. priv->mcast_mtu);
  150. dev->mtu = new_mtu;
  151. return 0;
  152. }
  153. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  154. return -EINVAL;
  155. priv->admin_mtu = new_mtu;
  156. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  157. return 0;
  158. }
  159. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  160. {
  161. struct ipoib_dev_priv *priv = netdev_priv(dev);
  162. struct rb_node *n = priv->path_tree.rb_node;
  163. struct ipoib_path *path;
  164. int ret;
  165. while (n) {
  166. path = rb_entry(n, struct ipoib_path, rb_node);
  167. ret = memcmp(gid, path->pathrec.dgid.raw,
  168. sizeof (union ib_gid));
  169. if (ret < 0)
  170. n = n->rb_left;
  171. else if (ret > 0)
  172. n = n->rb_right;
  173. else
  174. return path;
  175. }
  176. return NULL;
  177. }
  178. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  179. {
  180. struct ipoib_dev_priv *priv = netdev_priv(dev);
  181. struct rb_node **n = &priv->path_tree.rb_node;
  182. struct rb_node *pn = NULL;
  183. struct ipoib_path *tpath;
  184. int ret;
  185. while (*n) {
  186. pn = *n;
  187. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  188. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  189. sizeof (union ib_gid));
  190. if (ret < 0)
  191. n = &pn->rb_left;
  192. else if (ret > 0)
  193. n = &pn->rb_right;
  194. else
  195. return -EEXIST;
  196. }
  197. rb_link_node(&path->rb_node, pn, n);
  198. rb_insert_color(&path->rb_node, &priv->path_tree);
  199. list_add_tail(&path->list, &priv->path_list);
  200. return 0;
  201. }
  202. static void path_free(struct net_device *dev, struct ipoib_path *path)
  203. {
  204. struct ipoib_dev_priv *priv = netdev_priv(dev);
  205. struct ipoib_neigh *neigh, *tn;
  206. struct sk_buff *skb;
  207. unsigned long flags;
  208. while ((skb = __skb_dequeue(&path->queue)))
  209. dev_kfree_skb_irq(skb);
  210. spin_lock_irqsave(&priv->lock, flags);
  211. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  212. /*
  213. * It's safe to call ipoib_put_ah() inside priv->lock
  214. * here, because we know that path->ah will always
  215. * hold one more reference, so ipoib_put_ah() will
  216. * never do more than decrement the ref count.
  217. */
  218. if (neigh->ah)
  219. ipoib_put_ah(neigh->ah);
  220. ipoib_neigh_free(dev, neigh);
  221. }
  222. spin_unlock_irqrestore(&priv->lock, flags);
  223. if (path->ah)
  224. ipoib_put_ah(path->ah);
  225. kfree(path);
  226. }
  227. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  228. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  229. {
  230. struct ipoib_path_iter *iter;
  231. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  232. if (!iter)
  233. return NULL;
  234. iter->dev = dev;
  235. memset(iter->path.pathrec.dgid.raw, 0, 16);
  236. if (ipoib_path_iter_next(iter)) {
  237. kfree(iter);
  238. return NULL;
  239. }
  240. return iter;
  241. }
  242. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  243. {
  244. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  245. struct rb_node *n;
  246. struct ipoib_path *path;
  247. int ret = 1;
  248. spin_lock_irq(&priv->lock);
  249. n = rb_first(&priv->path_tree);
  250. while (n) {
  251. path = rb_entry(n, struct ipoib_path, rb_node);
  252. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  253. sizeof (union ib_gid)) < 0) {
  254. iter->path = *path;
  255. ret = 0;
  256. break;
  257. }
  258. n = rb_next(n);
  259. }
  260. spin_unlock_irq(&priv->lock);
  261. return ret;
  262. }
  263. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  264. struct ipoib_path *path)
  265. {
  266. *path = iter->path;
  267. }
  268. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  269. void ipoib_mark_paths_invalid(struct net_device *dev)
  270. {
  271. struct ipoib_dev_priv *priv = netdev_priv(dev);
  272. struct ipoib_path *path, *tp;
  273. spin_lock_irq(&priv->lock);
  274. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  275. ipoib_dbg(priv, "mark path LID 0x%04x GID %p6 invalid\n",
  276. be16_to_cpu(path->pathrec.dlid),
  277. path->pathrec.dgid.raw);
  278. path->valid = 0;
  279. }
  280. spin_unlock_irq(&priv->lock);
  281. }
  282. void ipoib_flush_paths(struct net_device *dev)
  283. {
  284. struct ipoib_dev_priv *priv = netdev_priv(dev);
  285. struct ipoib_path *path, *tp;
  286. LIST_HEAD(remove_list);
  287. unsigned long flags;
  288. netif_tx_lock_bh(dev);
  289. spin_lock_irqsave(&priv->lock, flags);
  290. list_splice_init(&priv->path_list, &remove_list);
  291. list_for_each_entry(path, &remove_list, list)
  292. rb_erase(&path->rb_node, &priv->path_tree);
  293. list_for_each_entry_safe(path, tp, &remove_list, list) {
  294. if (path->query)
  295. ib_sa_cancel_query(path->query_id, path->query);
  296. spin_unlock_irqrestore(&priv->lock, flags);
  297. netif_tx_unlock_bh(dev);
  298. wait_for_completion(&path->done);
  299. path_free(dev, path);
  300. netif_tx_lock_bh(dev);
  301. spin_lock_irqsave(&priv->lock, flags);
  302. }
  303. spin_unlock_irqrestore(&priv->lock, flags);
  304. netif_tx_unlock_bh(dev);
  305. }
  306. static void path_rec_completion(int status,
  307. struct ib_sa_path_rec *pathrec,
  308. void *path_ptr)
  309. {
  310. struct ipoib_path *path = path_ptr;
  311. struct net_device *dev = path->dev;
  312. struct ipoib_dev_priv *priv = netdev_priv(dev);
  313. struct ipoib_ah *ah = NULL;
  314. struct ipoib_ah *old_ah = NULL;
  315. struct ipoib_neigh *neigh, *tn;
  316. struct sk_buff_head skqueue;
  317. struct sk_buff *skb;
  318. unsigned long flags;
  319. if (!status)
  320. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %p6\n",
  321. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  322. else
  323. ipoib_dbg(priv, "PathRec status %d for GID %p6\n",
  324. status, path->pathrec.dgid.raw);
  325. skb_queue_head_init(&skqueue);
  326. if (!status) {
  327. struct ib_ah_attr av;
  328. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  329. ah = ipoib_create_ah(dev, priv->pd, &av);
  330. }
  331. spin_lock_irqsave(&priv->lock, flags);
  332. if (ah) {
  333. path->pathrec = *pathrec;
  334. old_ah = path->ah;
  335. path->ah = ah;
  336. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  337. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  338. while ((skb = __skb_dequeue(&path->queue)))
  339. __skb_queue_tail(&skqueue, skb);
  340. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  341. if (neigh->ah) {
  342. WARN_ON(neigh->ah != old_ah);
  343. /*
  344. * Dropping the ah reference inside
  345. * priv->lock is safe here, because we
  346. * will hold one more reference from
  347. * the original value of path->ah (ie
  348. * old_ah).
  349. */
  350. ipoib_put_ah(neigh->ah);
  351. }
  352. kref_get(&path->ah->ref);
  353. neigh->ah = path->ah;
  354. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  355. sizeof(union ib_gid));
  356. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  357. if (!ipoib_cm_get(neigh))
  358. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  359. path,
  360. neigh));
  361. if (!ipoib_cm_get(neigh)) {
  362. list_del(&neigh->list);
  363. if (neigh->ah)
  364. ipoib_put_ah(neigh->ah);
  365. ipoib_neigh_free(dev, neigh);
  366. continue;
  367. }
  368. }
  369. while ((skb = __skb_dequeue(&neigh->queue)))
  370. __skb_queue_tail(&skqueue, skb);
  371. }
  372. path->valid = 1;
  373. }
  374. path->query = NULL;
  375. complete(&path->done);
  376. spin_unlock_irqrestore(&priv->lock, flags);
  377. if (old_ah)
  378. ipoib_put_ah(old_ah);
  379. while ((skb = __skb_dequeue(&skqueue))) {
  380. skb->dev = dev;
  381. if (dev_queue_xmit(skb))
  382. ipoib_warn(priv, "dev_queue_xmit failed "
  383. "to requeue packet\n");
  384. }
  385. }
  386. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  387. {
  388. struct ipoib_dev_priv *priv = netdev_priv(dev);
  389. struct ipoib_path *path;
  390. if (!priv->broadcast)
  391. return NULL;
  392. path = kzalloc(sizeof *path, GFP_ATOMIC);
  393. if (!path)
  394. return NULL;
  395. path->dev = dev;
  396. skb_queue_head_init(&path->queue);
  397. INIT_LIST_HEAD(&path->neigh_list);
  398. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  399. path->pathrec.sgid = priv->local_gid;
  400. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  401. path->pathrec.numb_path = 1;
  402. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  403. return path;
  404. }
  405. static int path_rec_start(struct net_device *dev,
  406. struct ipoib_path *path)
  407. {
  408. struct ipoib_dev_priv *priv = netdev_priv(dev);
  409. ipoib_dbg(priv, "Start path record lookup for %p6\n",
  410. path->pathrec.dgid.raw);
  411. init_completion(&path->done);
  412. path->query_id =
  413. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  414. &path->pathrec,
  415. IB_SA_PATH_REC_DGID |
  416. IB_SA_PATH_REC_SGID |
  417. IB_SA_PATH_REC_NUMB_PATH |
  418. IB_SA_PATH_REC_TRAFFIC_CLASS |
  419. IB_SA_PATH_REC_PKEY,
  420. 1000, GFP_ATOMIC,
  421. path_rec_completion,
  422. path, &path->query);
  423. if (path->query_id < 0) {
  424. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  425. path->query = NULL;
  426. return path->query_id;
  427. }
  428. return 0;
  429. }
  430. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  431. {
  432. struct ipoib_dev_priv *priv = netdev_priv(dev);
  433. struct ipoib_path *path;
  434. struct ipoib_neigh *neigh;
  435. unsigned long flags;
  436. neigh = ipoib_neigh_alloc(skb->dst->neighbour, skb->dev);
  437. if (!neigh) {
  438. ++dev->stats.tx_dropped;
  439. dev_kfree_skb_any(skb);
  440. return;
  441. }
  442. spin_lock_irqsave(&priv->lock, flags);
  443. path = __path_find(dev, skb->dst->neighbour->ha + 4);
  444. if (!path) {
  445. path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
  446. if (!path)
  447. goto err_path;
  448. __path_add(dev, path);
  449. }
  450. list_add_tail(&neigh->list, &path->neigh_list);
  451. if (path->ah) {
  452. kref_get(&path->ah->ref);
  453. neigh->ah = path->ah;
  454. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  455. sizeof(union ib_gid));
  456. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  457. if (!ipoib_cm_get(neigh))
  458. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  459. if (!ipoib_cm_get(neigh)) {
  460. list_del(&neigh->list);
  461. if (neigh->ah)
  462. ipoib_put_ah(neigh->ah);
  463. ipoib_neigh_free(dev, neigh);
  464. goto err_drop;
  465. }
  466. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  467. __skb_queue_tail(&neigh->queue, skb);
  468. else {
  469. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  470. skb_queue_len(&neigh->queue));
  471. goto err_drop;
  472. }
  473. } else
  474. ipoib_send(dev, skb, path->ah, IPOIB_QPN(skb->dst->neighbour->ha));
  475. } else {
  476. neigh->ah = NULL;
  477. if (!path->query && path_rec_start(dev, path))
  478. goto err_list;
  479. __skb_queue_tail(&neigh->queue, skb);
  480. }
  481. spin_unlock_irqrestore(&priv->lock, flags);
  482. return;
  483. err_list:
  484. list_del(&neigh->list);
  485. err_path:
  486. ipoib_neigh_free(dev, neigh);
  487. err_drop:
  488. ++dev->stats.tx_dropped;
  489. dev_kfree_skb_any(skb);
  490. spin_unlock_irqrestore(&priv->lock, flags);
  491. }
  492. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  493. {
  494. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  495. /* Look up path record for unicasts */
  496. if (skb->dst->neighbour->ha[4] != 0xff) {
  497. neigh_add_path(skb, dev);
  498. return;
  499. }
  500. /* Add in the P_Key for multicasts */
  501. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  502. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  503. ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
  504. }
  505. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  506. struct ipoib_pseudoheader *phdr)
  507. {
  508. struct ipoib_dev_priv *priv = netdev_priv(dev);
  509. struct ipoib_path *path;
  510. unsigned long flags;
  511. spin_lock_irqsave(&priv->lock, flags);
  512. path = __path_find(dev, phdr->hwaddr + 4);
  513. if (!path || !path->valid) {
  514. if (!path)
  515. path = path_rec_create(dev, phdr->hwaddr + 4);
  516. if (path) {
  517. /* put pseudoheader back on for next time */
  518. skb_push(skb, sizeof *phdr);
  519. __skb_queue_tail(&path->queue, skb);
  520. if (path_rec_start(dev, path)) {
  521. spin_unlock_irqrestore(&priv->lock, flags);
  522. path_free(dev, path);
  523. return;
  524. } else
  525. __path_add(dev, path);
  526. } else {
  527. ++dev->stats.tx_dropped;
  528. dev_kfree_skb_any(skb);
  529. }
  530. spin_unlock_irqrestore(&priv->lock, flags);
  531. return;
  532. }
  533. if (path->ah) {
  534. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  535. be16_to_cpu(path->pathrec.dlid));
  536. ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
  537. } else if ((path->query || !path_rec_start(dev, path)) &&
  538. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  539. /* put pseudoheader back on for next time */
  540. skb_push(skb, sizeof *phdr);
  541. __skb_queue_tail(&path->queue, skb);
  542. } else {
  543. ++dev->stats.tx_dropped;
  544. dev_kfree_skb_any(skb);
  545. }
  546. spin_unlock_irqrestore(&priv->lock, flags);
  547. }
  548. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  549. {
  550. struct ipoib_dev_priv *priv = netdev_priv(dev);
  551. struct ipoib_neigh *neigh;
  552. unsigned long flags;
  553. if (likely(skb->dst && skb->dst->neighbour)) {
  554. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  555. ipoib_path_lookup(skb, dev);
  556. return NETDEV_TX_OK;
  557. }
  558. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  559. if (neigh->ah)
  560. if (unlikely((memcmp(&neigh->dgid.raw,
  561. skb->dst->neighbour->ha + 4,
  562. sizeof(union ib_gid))) ||
  563. (neigh->dev != dev))) {
  564. spin_lock_irqsave(&priv->lock, flags);
  565. /*
  566. * It's safe to call ipoib_put_ah() inside
  567. * priv->lock here, because we know that
  568. * path->ah will always hold one more reference,
  569. * so ipoib_put_ah() will never do more than
  570. * decrement the ref count.
  571. */
  572. ipoib_put_ah(neigh->ah);
  573. list_del(&neigh->list);
  574. ipoib_neigh_free(dev, neigh);
  575. spin_unlock_irqrestore(&priv->lock, flags);
  576. ipoib_path_lookup(skb, dev);
  577. return NETDEV_TX_OK;
  578. }
  579. if (ipoib_cm_get(neigh)) {
  580. if (ipoib_cm_up(neigh)) {
  581. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  582. return NETDEV_TX_OK;
  583. }
  584. } else if (neigh->ah) {
  585. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(skb->dst->neighbour->ha));
  586. return NETDEV_TX_OK;
  587. }
  588. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  589. spin_lock_irqsave(&priv->lock, flags);
  590. __skb_queue_tail(&neigh->queue, skb);
  591. spin_unlock_irqrestore(&priv->lock, flags);
  592. } else {
  593. ++dev->stats.tx_dropped;
  594. dev_kfree_skb_any(skb);
  595. }
  596. } else {
  597. struct ipoib_pseudoheader *phdr =
  598. (struct ipoib_pseudoheader *) skb->data;
  599. skb_pull(skb, sizeof *phdr);
  600. if (phdr->hwaddr[4] == 0xff) {
  601. /* Add in the P_Key for multicast*/
  602. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  603. phdr->hwaddr[9] = priv->pkey & 0xff;
  604. ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
  605. } else {
  606. /* unicast GID -- should be ARP or RARP reply */
  607. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  608. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  609. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x %p6\n",
  610. skb->dst ? "neigh" : "dst",
  611. be16_to_cpup((__be16 *) skb->data),
  612. IPOIB_QPN(phdr->hwaddr),
  613. phdr->hwaddr + 4);
  614. dev_kfree_skb_any(skb);
  615. ++dev->stats.tx_dropped;
  616. return NETDEV_TX_OK;
  617. }
  618. unicast_arp_send(skb, dev, phdr);
  619. }
  620. }
  621. return NETDEV_TX_OK;
  622. }
  623. static void ipoib_timeout(struct net_device *dev)
  624. {
  625. struct ipoib_dev_priv *priv = netdev_priv(dev);
  626. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  627. jiffies_to_msecs(jiffies - dev->trans_start));
  628. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  629. netif_queue_stopped(dev),
  630. priv->tx_head, priv->tx_tail);
  631. /* XXX reset QP, etc. */
  632. }
  633. static int ipoib_hard_header(struct sk_buff *skb,
  634. struct net_device *dev,
  635. unsigned short type,
  636. const void *daddr, const void *saddr, unsigned len)
  637. {
  638. struct ipoib_header *header;
  639. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  640. header->proto = htons(type);
  641. header->reserved = 0;
  642. /*
  643. * If we don't have a neighbour structure, stuff the
  644. * destination address onto the front of the skb so we can
  645. * figure out where to send the packet later.
  646. */
  647. if ((!skb->dst || !skb->dst->neighbour) && daddr) {
  648. struct ipoib_pseudoheader *phdr =
  649. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  650. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  651. }
  652. return 0;
  653. }
  654. static void ipoib_set_mcast_list(struct net_device *dev)
  655. {
  656. struct ipoib_dev_priv *priv = netdev_priv(dev);
  657. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  658. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  659. return;
  660. }
  661. queue_work(ipoib_workqueue, &priv->restart_task);
  662. }
  663. static void ipoib_neigh_cleanup(struct neighbour *n)
  664. {
  665. struct ipoib_neigh *neigh;
  666. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  667. unsigned long flags;
  668. struct ipoib_ah *ah = NULL;
  669. neigh = *to_ipoib_neigh(n);
  670. if (neigh)
  671. priv = netdev_priv(neigh->dev);
  672. else
  673. return;
  674. ipoib_dbg(priv,
  675. "neigh_cleanup for %06x %p6\n",
  676. IPOIB_QPN(n->ha),
  677. n->ha + 4);
  678. spin_lock_irqsave(&priv->lock, flags);
  679. if (neigh->ah)
  680. ah = neigh->ah;
  681. list_del(&neigh->list);
  682. ipoib_neigh_free(n->dev, neigh);
  683. spin_unlock_irqrestore(&priv->lock, flags);
  684. if (ah)
  685. ipoib_put_ah(ah);
  686. }
  687. struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
  688. struct net_device *dev)
  689. {
  690. struct ipoib_neigh *neigh;
  691. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  692. if (!neigh)
  693. return NULL;
  694. neigh->neighbour = neighbour;
  695. neigh->dev = dev;
  696. *to_ipoib_neigh(neighbour) = neigh;
  697. skb_queue_head_init(&neigh->queue);
  698. ipoib_cm_set(neigh, NULL);
  699. return neigh;
  700. }
  701. void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
  702. {
  703. struct sk_buff *skb;
  704. *to_ipoib_neigh(neigh->neighbour) = NULL;
  705. while ((skb = __skb_dequeue(&neigh->queue))) {
  706. ++dev->stats.tx_dropped;
  707. dev_kfree_skb_any(skb);
  708. }
  709. if (ipoib_cm_get(neigh))
  710. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  711. kfree(neigh);
  712. }
  713. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  714. {
  715. parms->neigh_cleanup = ipoib_neigh_cleanup;
  716. return 0;
  717. }
  718. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  719. {
  720. struct ipoib_dev_priv *priv = netdev_priv(dev);
  721. /* Allocate RX/TX "rings" to hold queued skbs */
  722. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  723. GFP_KERNEL);
  724. if (!priv->rx_ring) {
  725. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  726. ca->name, ipoib_recvq_size);
  727. goto out;
  728. }
  729. priv->tx_ring = vmalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  730. if (!priv->tx_ring) {
  731. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  732. ca->name, ipoib_sendq_size);
  733. goto out_rx_ring_cleanup;
  734. }
  735. memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
  736. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  737. if (ipoib_ib_dev_init(dev, ca, port))
  738. goto out_tx_ring_cleanup;
  739. return 0;
  740. out_tx_ring_cleanup:
  741. vfree(priv->tx_ring);
  742. out_rx_ring_cleanup:
  743. kfree(priv->rx_ring);
  744. out:
  745. return -ENOMEM;
  746. }
  747. void ipoib_dev_cleanup(struct net_device *dev)
  748. {
  749. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  750. ipoib_delete_debug_files(dev);
  751. /* Delete any child interfaces first */
  752. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  753. unregister_netdev(cpriv->dev);
  754. ipoib_dev_cleanup(cpriv->dev);
  755. free_netdev(cpriv->dev);
  756. }
  757. ipoib_ib_dev_cleanup(dev);
  758. kfree(priv->rx_ring);
  759. vfree(priv->tx_ring);
  760. priv->rx_ring = NULL;
  761. priv->tx_ring = NULL;
  762. }
  763. static const struct header_ops ipoib_header_ops = {
  764. .create = ipoib_hard_header,
  765. };
  766. static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
  767. void **tcph, u64 *hdr_flags, void *priv)
  768. {
  769. unsigned int ip_len;
  770. struct iphdr *iph;
  771. if (unlikely(skb->protocol != htons(ETH_P_IP)))
  772. return -1;
  773. /*
  774. * In the future we may add an else clause that verifies the
  775. * checksum and allows devices which do not calculate checksum
  776. * to use LRO.
  777. */
  778. if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY))
  779. return -1;
  780. /* Check for non-TCP packet */
  781. skb_reset_network_header(skb);
  782. iph = ip_hdr(skb);
  783. if (iph->protocol != IPPROTO_TCP)
  784. return -1;
  785. ip_len = ip_hdrlen(skb);
  786. skb_set_transport_header(skb, ip_len);
  787. *tcph = tcp_hdr(skb);
  788. /* check if IP header and TCP header are complete */
  789. if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
  790. return -1;
  791. *hdr_flags = LRO_IPV4 | LRO_TCP;
  792. *iphdr = iph;
  793. return 0;
  794. }
  795. static void ipoib_lro_setup(struct ipoib_dev_priv *priv)
  796. {
  797. priv->lro.lro_mgr.max_aggr = lro_max_aggr;
  798. priv->lro.lro_mgr.max_desc = IPOIB_MAX_LRO_DESCRIPTORS;
  799. priv->lro.lro_mgr.lro_arr = priv->lro.lro_desc;
  800. priv->lro.lro_mgr.get_skb_header = get_skb_hdr;
  801. priv->lro.lro_mgr.features = LRO_F_NAPI;
  802. priv->lro.lro_mgr.dev = priv->dev;
  803. priv->lro.lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  804. }
  805. static void ipoib_setup(struct net_device *dev)
  806. {
  807. struct ipoib_dev_priv *priv = netdev_priv(dev);
  808. dev->open = ipoib_open;
  809. dev->stop = ipoib_stop;
  810. dev->change_mtu = ipoib_change_mtu;
  811. dev->hard_start_xmit = ipoib_start_xmit;
  812. dev->tx_timeout = ipoib_timeout;
  813. dev->header_ops = &ipoib_header_ops;
  814. dev->set_multicast_list = ipoib_set_mcast_list;
  815. dev->neigh_setup = ipoib_neigh_setup_dev;
  816. ipoib_set_ethtool_ops(dev);
  817. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  818. dev->watchdog_timeo = HZ;
  819. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  820. /*
  821. * We add in INFINIBAND_ALEN to allow for the destination
  822. * address "pseudoheader" for skbs without neighbour struct.
  823. */
  824. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  825. dev->addr_len = INFINIBAND_ALEN;
  826. dev->type = ARPHRD_INFINIBAND;
  827. dev->tx_queue_len = ipoib_sendq_size * 2;
  828. dev->features = (NETIF_F_VLAN_CHALLENGED |
  829. NETIF_F_HIGHDMA);
  830. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  831. netif_carrier_off(dev);
  832. priv->dev = dev;
  833. ipoib_lro_setup(priv);
  834. spin_lock_init(&priv->lock);
  835. mutex_init(&priv->vlan_mutex);
  836. INIT_LIST_HEAD(&priv->path_list);
  837. INIT_LIST_HEAD(&priv->child_intfs);
  838. INIT_LIST_HEAD(&priv->dead_ahs);
  839. INIT_LIST_HEAD(&priv->multicast_list);
  840. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  841. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  842. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  843. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  844. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  845. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  846. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  847. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  848. }
  849. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  850. {
  851. struct net_device *dev;
  852. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  853. ipoib_setup);
  854. if (!dev)
  855. return NULL;
  856. return netdev_priv(dev);
  857. }
  858. static ssize_t show_pkey(struct device *dev,
  859. struct device_attribute *attr, char *buf)
  860. {
  861. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  862. return sprintf(buf, "0x%04x\n", priv->pkey);
  863. }
  864. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  865. static ssize_t show_umcast(struct device *dev,
  866. struct device_attribute *attr, char *buf)
  867. {
  868. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  869. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  870. }
  871. static ssize_t set_umcast(struct device *dev,
  872. struct device_attribute *attr,
  873. const char *buf, size_t count)
  874. {
  875. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  876. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  877. if (umcast_val > 0) {
  878. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  879. ipoib_warn(priv, "ignoring multicast groups joined directly "
  880. "by userspace\n");
  881. } else
  882. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  883. return count;
  884. }
  885. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  886. int ipoib_add_umcast_attr(struct net_device *dev)
  887. {
  888. return device_create_file(&dev->dev, &dev_attr_umcast);
  889. }
  890. static ssize_t create_child(struct device *dev,
  891. struct device_attribute *attr,
  892. const char *buf, size_t count)
  893. {
  894. int pkey;
  895. int ret;
  896. if (sscanf(buf, "%i", &pkey) != 1)
  897. return -EINVAL;
  898. if (pkey < 0 || pkey > 0xffff)
  899. return -EINVAL;
  900. /*
  901. * Set the full membership bit, so that we join the right
  902. * broadcast group, etc.
  903. */
  904. pkey |= 0x8000;
  905. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  906. return ret ? ret : count;
  907. }
  908. static DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  909. static ssize_t delete_child(struct device *dev,
  910. struct device_attribute *attr,
  911. const char *buf, size_t count)
  912. {
  913. int pkey;
  914. int ret;
  915. if (sscanf(buf, "%i", &pkey) != 1)
  916. return -EINVAL;
  917. if (pkey < 0 || pkey > 0xffff)
  918. return -EINVAL;
  919. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  920. return ret ? ret : count;
  921. }
  922. static DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  923. int ipoib_add_pkey_attr(struct net_device *dev)
  924. {
  925. return device_create_file(&dev->dev, &dev_attr_pkey);
  926. }
  927. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  928. {
  929. struct ib_device_attr *device_attr;
  930. int result = -ENOMEM;
  931. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  932. if (!device_attr) {
  933. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  934. hca->name, sizeof *device_attr);
  935. return result;
  936. }
  937. result = ib_query_device(hca, device_attr);
  938. if (result) {
  939. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  940. hca->name, result);
  941. kfree(device_attr);
  942. return result;
  943. }
  944. priv->hca_caps = device_attr->device_cap_flags;
  945. kfree(device_attr);
  946. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  947. set_bit(IPOIB_FLAG_CSUM, &priv->flags);
  948. priv->dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
  949. }
  950. if (lro)
  951. priv->dev->features |= NETIF_F_LRO;
  952. if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO)
  953. priv->dev->features |= NETIF_F_TSO;
  954. return 0;
  955. }
  956. static struct net_device *ipoib_add_port(const char *format,
  957. struct ib_device *hca, u8 port)
  958. {
  959. struct ipoib_dev_priv *priv;
  960. struct ib_port_attr attr;
  961. int result = -ENOMEM;
  962. priv = ipoib_intf_alloc(format);
  963. if (!priv)
  964. goto alloc_mem_failed;
  965. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  966. if (!ib_query_port(hca, port, &attr))
  967. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  968. else {
  969. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  970. hca->name, port);
  971. goto device_init_failed;
  972. }
  973. /* MTU will be reset when mcast join happens */
  974. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  975. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  976. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  977. if (result) {
  978. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  979. hca->name, port, result);
  980. goto device_init_failed;
  981. }
  982. if (ipoib_set_dev_features(priv, hca))
  983. goto device_init_failed;
  984. /*
  985. * Set the full membership bit, so that we join the right
  986. * broadcast group, etc.
  987. */
  988. priv->pkey |= 0x8000;
  989. priv->dev->broadcast[8] = priv->pkey >> 8;
  990. priv->dev->broadcast[9] = priv->pkey & 0xff;
  991. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  992. if (result) {
  993. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  994. hca->name, port, result);
  995. goto device_init_failed;
  996. } else
  997. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  998. result = ipoib_dev_init(priv->dev, hca, port);
  999. if (result < 0) {
  1000. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1001. hca->name, port, result);
  1002. goto device_init_failed;
  1003. }
  1004. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1005. priv->ca, ipoib_event);
  1006. result = ib_register_event_handler(&priv->event_handler);
  1007. if (result < 0) {
  1008. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1009. "port %d (ret = %d)\n",
  1010. hca->name, port, result);
  1011. goto event_failed;
  1012. }
  1013. result = register_netdev(priv->dev);
  1014. if (result) {
  1015. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1016. hca->name, port, result);
  1017. goto register_failed;
  1018. }
  1019. ipoib_create_debug_files(priv->dev);
  1020. if (ipoib_cm_add_mode_attr(priv->dev))
  1021. goto sysfs_failed;
  1022. if (ipoib_add_pkey_attr(priv->dev))
  1023. goto sysfs_failed;
  1024. if (ipoib_add_umcast_attr(priv->dev))
  1025. goto sysfs_failed;
  1026. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1027. goto sysfs_failed;
  1028. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1029. goto sysfs_failed;
  1030. return priv->dev;
  1031. sysfs_failed:
  1032. ipoib_delete_debug_files(priv->dev);
  1033. unregister_netdev(priv->dev);
  1034. register_failed:
  1035. ib_unregister_event_handler(&priv->event_handler);
  1036. flush_workqueue(ipoib_workqueue);
  1037. event_failed:
  1038. ipoib_dev_cleanup(priv->dev);
  1039. device_init_failed:
  1040. free_netdev(priv->dev);
  1041. alloc_mem_failed:
  1042. return ERR_PTR(result);
  1043. }
  1044. static void ipoib_add_one(struct ib_device *device)
  1045. {
  1046. struct list_head *dev_list;
  1047. struct net_device *dev;
  1048. struct ipoib_dev_priv *priv;
  1049. int s, e, p;
  1050. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1051. return;
  1052. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1053. if (!dev_list)
  1054. return;
  1055. INIT_LIST_HEAD(dev_list);
  1056. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1057. s = 0;
  1058. e = 0;
  1059. } else {
  1060. s = 1;
  1061. e = device->phys_port_cnt;
  1062. }
  1063. for (p = s; p <= e; ++p) {
  1064. dev = ipoib_add_port("ib%d", device, p);
  1065. if (!IS_ERR(dev)) {
  1066. priv = netdev_priv(dev);
  1067. list_add_tail(&priv->list, dev_list);
  1068. }
  1069. }
  1070. ib_set_client_data(device, &ipoib_client, dev_list);
  1071. }
  1072. static void ipoib_remove_one(struct ib_device *device)
  1073. {
  1074. struct ipoib_dev_priv *priv, *tmp;
  1075. struct list_head *dev_list;
  1076. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1077. return;
  1078. dev_list = ib_get_client_data(device, &ipoib_client);
  1079. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1080. ib_unregister_event_handler(&priv->event_handler);
  1081. rtnl_lock();
  1082. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1083. rtnl_unlock();
  1084. flush_workqueue(ipoib_workqueue);
  1085. unregister_netdev(priv->dev);
  1086. ipoib_dev_cleanup(priv->dev);
  1087. free_netdev(priv->dev);
  1088. }
  1089. kfree(dev_list);
  1090. }
  1091. static int __init ipoib_init_module(void)
  1092. {
  1093. int ret;
  1094. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1095. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1096. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1097. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1098. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1099. ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
  1100. IPOIB_MIN_QUEUE_SIZE));
  1101. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1102. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1103. #endif
  1104. /*
  1105. * When copying small received packets, we only copy from the
  1106. * linear data part of the SKB, so we rely on this condition.
  1107. */
  1108. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1109. ret = ipoib_register_debugfs();
  1110. if (ret)
  1111. return ret;
  1112. /*
  1113. * We create our own workqueue mainly because we want to be
  1114. * able to flush it when devices are being removed. We can't
  1115. * use schedule_work()/flush_scheduled_work() because both
  1116. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1117. * so flush_scheduled_work() can deadlock during device
  1118. * removal.
  1119. */
  1120. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1121. if (!ipoib_workqueue) {
  1122. ret = -ENOMEM;
  1123. goto err_fs;
  1124. }
  1125. ib_sa_register_client(&ipoib_sa_client);
  1126. ret = ib_register_client(&ipoib_client);
  1127. if (ret)
  1128. goto err_sa;
  1129. return 0;
  1130. err_sa:
  1131. ib_sa_unregister_client(&ipoib_sa_client);
  1132. destroy_workqueue(ipoib_workqueue);
  1133. err_fs:
  1134. ipoib_unregister_debugfs();
  1135. return ret;
  1136. }
  1137. static void __exit ipoib_cleanup_module(void)
  1138. {
  1139. ib_unregister_client(&ipoib_client);
  1140. ib_sa_unregister_client(&ipoib_sa_client);
  1141. ipoib_unregister_debugfs();
  1142. destroy_workqueue(ipoib_workqueue);
  1143. }
  1144. module_init(ipoib_init_module);
  1145. module_exit(ipoib_cleanup_module);