cm.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  1. /*
  2. * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  4. * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
  5. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. *
  35. * $Id: cm.c 2821 2005-07-08 17:07:28Z sean.hefty $
  36. */
  37. #include <linux/completion.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/err.h>
  40. #include <linux/idr.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/pci.h>
  43. #include <linux/rbtree.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/workqueue.h>
  46. #include <rdma/ib_cache.h>
  47. #include <rdma/ib_cm.h>
  48. #include "cm_msgs.h"
  49. MODULE_AUTHOR("Sean Hefty");
  50. MODULE_DESCRIPTION("InfiniBand CM");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. static void cm_add_one(struct ib_device *device);
  53. static void cm_remove_one(struct ib_device *device);
  54. static struct ib_client cm_client = {
  55. .name = "cm",
  56. .add = cm_add_one,
  57. .remove = cm_remove_one
  58. };
  59. static struct ib_cm {
  60. spinlock_t lock;
  61. struct list_head device_list;
  62. rwlock_t device_lock;
  63. struct rb_root listen_service_table;
  64. u64 listen_service_id;
  65. /* struct rb_root peer_service_table; todo: fix peer to peer */
  66. struct rb_root remote_qp_table;
  67. struct rb_root remote_id_table;
  68. struct rb_root remote_sidr_table;
  69. struct idr local_id_table;
  70. struct workqueue_struct *wq;
  71. } cm;
  72. struct cm_port {
  73. struct cm_device *cm_dev;
  74. struct ib_mad_agent *mad_agent;
  75. u8 port_num;
  76. };
  77. struct cm_device {
  78. struct list_head list;
  79. struct ib_device *device;
  80. __be64 ca_guid;
  81. struct cm_port port[0];
  82. };
  83. struct cm_av {
  84. struct cm_port *port;
  85. union ib_gid dgid;
  86. struct ib_ah_attr ah_attr;
  87. u16 pkey_index;
  88. u8 packet_life_time;
  89. };
  90. struct cm_work {
  91. struct work_struct work;
  92. struct list_head list;
  93. struct cm_port *port;
  94. struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
  95. __be32 local_id; /* Established / timewait */
  96. __be32 remote_id;
  97. struct ib_cm_event cm_event;
  98. struct ib_sa_path_rec path[0];
  99. };
  100. struct cm_timewait_info {
  101. struct cm_work work; /* Must be first. */
  102. struct rb_node remote_qp_node;
  103. struct rb_node remote_id_node;
  104. __be64 remote_ca_guid;
  105. __be32 remote_qpn;
  106. u8 inserted_remote_qp;
  107. u8 inserted_remote_id;
  108. };
  109. struct cm_id_private {
  110. struct ib_cm_id id;
  111. struct rb_node service_node;
  112. struct rb_node sidr_id_node;
  113. spinlock_t lock; /* Do not acquire inside cm.lock */
  114. struct completion comp;
  115. atomic_t refcount;
  116. struct ib_mad_send_buf *msg;
  117. struct cm_timewait_info *timewait_info;
  118. /* todo: use alternate port on send failure */
  119. struct cm_av av;
  120. struct cm_av alt_av;
  121. void *private_data;
  122. __be64 tid;
  123. __be32 local_qpn;
  124. __be32 remote_qpn;
  125. enum ib_qp_type qp_type;
  126. __be32 sq_psn;
  127. __be32 rq_psn;
  128. int timeout_ms;
  129. enum ib_mtu path_mtu;
  130. u8 private_data_len;
  131. u8 max_cm_retries;
  132. u8 peer_to_peer;
  133. u8 responder_resources;
  134. u8 initiator_depth;
  135. u8 local_ack_timeout;
  136. u8 retry_count;
  137. u8 rnr_retry_count;
  138. u8 service_timeout;
  139. struct list_head work_list;
  140. atomic_t work_count;
  141. };
  142. static void cm_work_handler(void *data);
  143. static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
  144. {
  145. if (atomic_dec_and_test(&cm_id_priv->refcount))
  146. complete(&cm_id_priv->comp);
  147. }
  148. static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
  149. struct ib_mad_send_buf **msg)
  150. {
  151. struct ib_mad_agent *mad_agent;
  152. struct ib_mad_send_buf *m;
  153. struct ib_ah *ah;
  154. mad_agent = cm_id_priv->av.port->mad_agent;
  155. ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
  156. if (IS_ERR(ah))
  157. return PTR_ERR(ah);
  158. m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
  159. cm_id_priv->av.pkey_index,
  160. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  161. GFP_ATOMIC);
  162. if (IS_ERR(m)) {
  163. ib_destroy_ah(ah);
  164. return PTR_ERR(m);
  165. }
  166. /* Timeout set by caller if response is expected. */
  167. m->ah = ah;
  168. m->retries = cm_id_priv->max_cm_retries;
  169. atomic_inc(&cm_id_priv->refcount);
  170. m->context[0] = cm_id_priv;
  171. *msg = m;
  172. return 0;
  173. }
  174. static int cm_alloc_response_msg(struct cm_port *port,
  175. struct ib_mad_recv_wc *mad_recv_wc,
  176. struct ib_mad_send_buf **msg)
  177. {
  178. struct ib_mad_send_buf *m;
  179. struct ib_ah *ah;
  180. ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
  181. mad_recv_wc->recv_buf.grh, port->port_num);
  182. if (IS_ERR(ah))
  183. return PTR_ERR(ah);
  184. m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
  185. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  186. GFP_ATOMIC);
  187. if (IS_ERR(m)) {
  188. ib_destroy_ah(ah);
  189. return PTR_ERR(m);
  190. }
  191. m->ah = ah;
  192. *msg = m;
  193. return 0;
  194. }
  195. static void cm_free_msg(struct ib_mad_send_buf *msg)
  196. {
  197. ib_destroy_ah(msg->ah);
  198. if (msg->context[0])
  199. cm_deref_id(msg->context[0]);
  200. ib_free_send_mad(msg);
  201. }
  202. static void * cm_copy_private_data(const void *private_data,
  203. u8 private_data_len)
  204. {
  205. void *data;
  206. if (!private_data || !private_data_len)
  207. return NULL;
  208. data = kmalloc(private_data_len, GFP_KERNEL);
  209. if (!data)
  210. return ERR_PTR(-ENOMEM);
  211. memcpy(data, private_data, private_data_len);
  212. return data;
  213. }
  214. static void cm_set_private_data(struct cm_id_private *cm_id_priv,
  215. void *private_data, u8 private_data_len)
  216. {
  217. if (cm_id_priv->private_data && cm_id_priv->private_data_len)
  218. kfree(cm_id_priv->private_data);
  219. cm_id_priv->private_data = private_data;
  220. cm_id_priv->private_data_len = private_data_len;
  221. }
  222. static void cm_set_ah_attr(struct ib_ah_attr *ah_attr, u8 port_num,
  223. u16 dlid, u8 sl, u16 src_path_bits)
  224. {
  225. memset(ah_attr, 0, sizeof ah_attr);
  226. ah_attr->dlid = dlid;
  227. ah_attr->sl = sl;
  228. ah_attr->src_path_bits = src_path_bits;
  229. ah_attr->port_num = port_num;
  230. }
  231. static void cm_init_av_for_response(struct cm_port *port,
  232. struct ib_wc *wc, struct cm_av *av)
  233. {
  234. av->port = port;
  235. av->pkey_index = wc->pkey_index;
  236. cm_set_ah_attr(&av->ah_attr, port->port_num, wc->slid,
  237. wc->sl, wc->dlid_path_bits);
  238. }
  239. static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
  240. {
  241. struct cm_device *cm_dev;
  242. struct cm_port *port = NULL;
  243. unsigned long flags;
  244. int ret;
  245. u8 p;
  246. read_lock_irqsave(&cm.device_lock, flags);
  247. list_for_each_entry(cm_dev, &cm.device_list, list) {
  248. if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
  249. &p, NULL)) {
  250. port = &cm_dev->port[p-1];
  251. break;
  252. }
  253. }
  254. read_unlock_irqrestore(&cm.device_lock, flags);
  255. if (!port)
  256. return -EINVAL;
  257. ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
  258. be16_to_cpu(path->pkey), &av->pkey_index);
  259. if (ret)
  260. return ret;
  261. av->port = port;
  262. cm_set_ah_attr(&av->ah_attr, av->port->port_num,
  263. be16_to_cpu(path->dlid), path->sl,
  264. be16_to_cpu(path->slid) & 0x7F);
  265. av->packet_life_time = path->packet_life_time;
  266. return 0;
  267. }
  268. static int cm_alloc_id(struct cm_id_private *cm_id_priv)
  269. {
  270. unsigned long flags;
  271. int ret;
  272. static int next_id;
  273. do {
  274. spin_lock_irqsave(&cm.lock, flags);
  275. ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, next_id++,
  276. (__force int *) &cm_id_priv->id.local_id);
  277. spin_unlock_irqrestore(&cm.lock, flags);
  278. } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
  279. return ret;
  280. }
  281. static void cm_free_id(__be32 local_id)
  282. {
  283. unsigned long flags;
  284. spin_lock_irqsave(&cm.lock, flags);
  285. idr_remove(&cm.local_id_table, (__force int) local_id);
  286. spin_unlock_irqrestore(&cm.lock, flags);
  287. }
  288. static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
  289. {
  290. struct cm_id_private *cm_id_priv;
  291. cm_id_priv = idr_find(&cm.local_id_table, (__force int) local_id);
  292. if (cm_id_priv) {
  293. if (cm_id_priv->id.remote_id == remote_id)
  294. atomic_inc(&cm_id_priv->refcount);
  295. else
  296. cm_id_priv = NULL;
  297. }
  298. return cm_id_priv;
  299. }
  300. static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
  301. {
  302. struct cm_id_private *cm_id_priv;
  303. unsigned long flags;
  304. spin_lock_irqsave(&cm.lock, flags);
  305. cm_id_priv = cm_get_id(local_id, remote_id);
  306. spin_unlock_irqrestore(&cm.lock, flags);
  307. return cm_id_priv;
  308. }
  309. static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
  310. {
  311. struct rb_node **link = &cm.listen_service_table.rb_node;
  312. struct rb_node *parent = NULL;
  313. struct cm_id_private *cur_cm_id_priv;
  314. __be64 service_id = cm_id_priv->id.service_id;
  315. __be64 service_mask = cm_id_priv->id.service_mask;
  316. while (*link) {
  317. parent = *link;
  318. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  319. service_node);
  320. if ((cur_cm_id_priv->id.service_mask & service_id) ==
  321. (service_mask & cur_cm_id_priv->id.service_id) &&
  322. (cm_id_priv->id.device == cur_cm_id_priv->id.device))
  323. return cur_cm_id_priv;
  324. if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
  325. link = &(*link)->rb_left;
  326. else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
  327. link = &(*link)->rb_right;
  328. else if (service_id < cur_cm_id_priv->id.service_id)
  329. link = &(*link)->rb_left;
  330. else
  331. link = &(*link)->rb_right;
  332. }
  333. rb_link_node(&cm_id_priv->service_node, parent, link);
  334. rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
  335. return NULL;
  336. }
  337. static struct cm_id_private * cm_find_listen(struct ib_device *device,
  338. __be64 service_id)
  339. {
  340. struct rb_node *node = cm.listen_service_table.rb_node;
  341. struct cm_id_private *cm_id_priv;
  342. while (node) {
  343. cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
  344. if ((cm_id_priv->id.service_mask & service_id) ==
  345. cm_id_priv->id.service_id &&
  346. (cm_id_priv->id.device == device))
  347. return cm_id_priv;
  348. if (device < cm_id_priv->id.device)
  349. node = node->rb_left;
  350. else if (device > cm_id_priv->id.device)
  351. node = node->rb_right;
  352. else if (service_id < cm_id_priv->id.service_id)
  353. node = node->rb_left;
  354. else
  355. node = node->rb_right;
  356. }
  357. return NULL;
  358. }
  359. static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
  360. *timewait_info)
  361. {
  362. struct rb_node **link = &cm.remote_id_table.rb_node;
  363. struct rb_node *parent = NULL;
  364. struct cm_timewait_info *cur_timewait_info;
  365. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  366. __be32 remote_id = timewait_info->work.remote_id;
  367. while (*link) {
  368. parent = *link;
  369. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  370. remote_id_node);
  371. if (remote_id < cur_timewait_info->work.remote_id)
  372. link = &(*link)->rb_left;
  373. else if (remote_id > cur_timewait_info->work.remote_id)
  374. link = &(*link)->rb_right;
  375. else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
  376. link = &(*link)->rb_left;
  377. else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
  378. link = &(*link)->rb_right;
  379. else
  380. return cur_timewait_info;
  381. }
  382. timewait_info->inserted_remote_id = 1;
  383. rb_link_node(&timewait_info->remote_id_node, parent, link);
  384. rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
  385. return NULL;
  386. }
  387. static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
  388. __be32 remote_id)
  389. {
  390. struct rb_node *node = cm.remote_id_table.rb_node;
  391. struct cm_timewait_info *timewait_info;
  392. while (node) {
  393. timewait_info = rb_entry(node, struct cm_timewait_info,
  394. remote_id_node);
  395. if (remote_id < timewait_info->work.remote_id)
  396. node = node->rb_left;
  397. else if (remote_id > timewait_info->work.remote_id)
  398. node = node->rb_right;
  399. else if (remote_ca_guid < timewait_info->remote_ca_guid)
  400. node = node->rb_left;
  401. else if (remote_ca_guid > timewait_info->remote_ca_guid)
  402. node = node->rb_right;
  403. else
  404. return timewait_info;
  405. }
  406. return NULL;
  407. }
  408. static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
  409. *timewait_info)
  410. {
  411. struct rb_node **link = &cm.remote_qp_table.rb_node;
  412. struct rb_node *parent = NULL;
  413. struct cm_timewait_info *cur_timewait_info;
  414. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  415. __be32 remote_qpn = timewait_info->remote_qpn;
  416. while (*link) {
  417. parent = *link;
  418. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  419. remote_qp_node);
  420. if (remote_qpn < cur_timewait_info->remote_qpn)
  421. link = &(*link)->rb_left;
  422. else if (remote_qpn > cur_timewait_info->remote_qpn)
  423. link = &(*link)->rb_right;
  424. else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
  425. link = &(*link)->rb_left;
  426. else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
  427. link = &(*link)->rb_right;
  428. else
  429. return cur_timewait_info;
  430. }
  431. timewait_info->inserted_remote_qp = 1;
  432. rb_link_node(&timewait_info->remote_qp_node, parent, link);
  433. rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  434. return NULL;
  435. }
  436. static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
  437. *cm_id_priv)
  438. {
  439. struct rb_node **link = &cm.remote_sidr_table.rb_node;
  440. struct rb_node *parent = NULL;
  441. struct cm_id_private *cur_cm_id_priv;
  442. union ib_gid *port_gid = &cm_id_priv->av.dgid;
  443. __be32 remote_id = cm_id_priv->id.remote_id;
  444. while (*link) {
  445. parent = *link;
  446. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  447. sidr_id_node);
  448. if (remote_id < cur_cm_id_priv->id.remote_id)
  449. link = &(*link)->rb_left;
  450. else if (remote_id > cur_cm_id_priv->id.remote_id)
  451. link = &(*link)->rb_right;
  452. else {
  453. int cmp;
  454. cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
  455. sizeof *port_gid);
  456. if (cmp < 0)
  457. link = &(*link)->rb_left;
  458. else if (cmp > 0)
  459. link = &(*link)->rb_right;
  460. else
  461. return cur_cm_id_priv;
  462. }
  463. }
  464. rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
  465. rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  466. return NULL;
  467. }
  468. static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
  469. enum ib_cm_sidr_status status)
  470. {
  471. struct ib_cm_sidr_rep_param param;
  472. memset(&param, 0, sizeof param);
  473. param.status = status;
  474. ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
  475. }
  476. struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
  477. ib_cm_handler cm_handler,
  478. void *context)
  479. {
  480. struct cm_id_private *cm_id_priv;
  481. int ret;
  482. cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
  483. if (!cm_id_priv)
  484. return ERR_PTR(-ENOMEM);
  485. cm_id_priv->id.state = IB_CM_IDLE;
  486. cm_id_priv->id.device = device;
  487. cm_id_priv->id.cm_handler = cm_handler;
  488. cm_id_priv->id.context = context;
  489. cm_id_priv->id.remote_cm_qpn = 1;
  490. ret = cm_alloc_id(cm_id_priv);
  491. if (ret)
  492. goto error;
  493. spin_lock_init(&cm_id_priv->lock);
  494. init_completion(&cm_id_priv->comp);
  495. INIT_LIST_HEAD(&cm_id_priv->work_list);
  496. atomic_set(&cm_id_priv->work_count, -1);
  497. atomic_set(&cm_id_priv->refcount, 1);
  498. return &cm_id_priv->id;
  499. error:
  500. kfree(cm_id_priv);
  501. return ERR_PTR(-ENOMEM);
  502. }
  503. EXPORT_SYMBOL(ib_create_cm_id);
  504. static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
  505. {
  506. struct cm_work *work;
  507. if (list_empty(&cm_id_priv->work_list))
  508. return NULL;
  509. work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
  510. list_del(&work->list);
  511. return work;
  512. }
  513. static void cm_free_work(struct cm_work *work)
  514. {
  515. if (work->mad_recv_wc)
  516. ib_free_recv_mad(work->mad_recv_wc);
  517. kfree(work);
  518. }
  519. static inline int cm_convert_to_ms(int iba_time)
  520. {
  521. /* approximate conversion to ms from 4.096us x 2^iba_time */
  522. return 1 << max(iba_time - 8, 0);
  523. }
  524. static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
  525. {
  526. unsigned long flags;
  527. if (!timewait_info->inserted_remote_id &&
  528. !timewait_info->inserted_remote_qp)
  529. return;
  530. spin_lock_irqsave(&cm.lock, flags);
  531. if (timewait_info->inserted_remote_id) {
  532. rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
  533. timewait_info->inserted_remote_id = 0;
  534. }
  535. if (timewait_info->inserted_remote_qp) {
  536. rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  537. timewait_info->inserted_remote_qp = 0;
  538. }
  539. spin_unlock_irqrestore(&cm.lock, flags);
  540. }
  541. static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
  542. {
  543. struct cm_timewait_info *timewait_info;
  544. timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
  545. if (!timewait_info)
  546. return ERR_PTR(-ENOMEM);
  547. timewait_info->work.local_id = local_id;
  548. INIT_WORK(&timewait_info->work.work, cm_work_handler,
  549. &timewait_info->work);
  550. timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
  551. return timewait_info;
  552. }
  553. static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
  554. {
  555. int wait_time;
  556. /*
  557. * The cm_id could be destroyed by the user before we exit timewait.
  558. * To protect against this, we search for the cm_id after exiting
  559. * timewait before notifying the user that we've exited timewait.
  560. */
  561. cm_id_priv->id.state = IB_CM_TIMEWAIT;
  562. wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
  563. queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
  564. msecs_to_jiffies(wait_time));
  565. cm_id_priv->timewait_info = NULL;
  566. }
  567. static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
  568. {
  569. cm_id_priv->id.state = IB_CM_IDLE;
  570. if (cm_id_priv->timewait_info) {
  571. cm_cleanup_timewait(cm_id_priv->timewait_info);
  572. kfree(cm_id_priv->timewait_info);
  573. cm_id_priv->timewait_info = NULL;
  574. }
  575. }
  576. void ib_destroy_cm_id(struct ib_cm_id *cm_id)
  577. {
  578. struct cm_id_private *cm_id_priv;
  579. struct cm_work *work;
  580. unsigned long flags;
  581. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  582. retest:
  583. spin_lock_irqsave(&cm_id_priv->lock, flags);
  584. switch (cm_id->state) {
  585. case IB_CM_LISTEN:
  586. cm_id->state = IB_CM_IDLE;
  587. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  588. spin_lock_irqsave(&cm.lock, flags);
  589. rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
  590. spin_unlock_irqrestore(&cm.lock, flags);
  591. break;
  592. case IB_CM_SIDR_REQ_SENT:
  593. cm_id->state = IB_CM_IDLE;
  594. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  595. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  596. break;
  597. case IB_CM_SIDR_REQ_RCVD:
  598. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  599. cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
  600. break;
  601. case IB_CM_REQ_SENT:
  602. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  603. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  604. ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
  605. &cm_id_priv->av.port->cm_dev->ca_guid,
  606. sizeof cm_id_priv->av.port->cm_dev->ca_guid,
  607. NULL, 0);
  608. break;
  609. case IB_CM_MRA_REQ_RCVD:
  610. case IB_CM_REP_SENT:
  611. case IB_CM_MRA_REP_RCVD:
  612. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  613. /* Fall through */
  614. case IB_CM_REQ_RCVD:
  615. case IB_CM_MRA_REQ_SENT:
  616. case IB_CM_REP_RCVD:
  617. case IB_CM_MRA_REP_SENT:
  618. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  619. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
  620. NULL, 0, NULL, 0);
  621. break;
  622. case IB_CM_ESTABLISHED:
  623. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  624. ib_send_cm_dreq(cm_id, NULL, 0);
  625. goto retest;
  626. case IB_CM_DREQ_SENT:
  627. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  628. cm_enter_timewait(cm_id_priv);
  629. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  630. break;
  631. case IB_CM_DREQ_RCVD:
  632. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  633. ib_send_cm_drep(cm_id, NULL, 0);
  634. break;
  635. default:
  636. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  637. break;
  638. }
  639. cm_free_id(cm_id->local_id);
  640. cm_deref_id(cm_id_priv);
  641. wait_for_completion(&cm_id_priv->comp);
  642. while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
  643. cm_free_work(work);
  644. if (cm_id_priv->private_data && cm_id_priv->private_data_len)
  645. kfree(cm_id_priv->private_data);
  646. kfree(cm_id_priv);
  647. }
  648. EXPORT_SYMBOL(ib_destroy_cm_id);
  649. int ib_cm_listen(struct ib_cm_id *cm_id,
  650. __be64 service_id,
  651. __be64 service_mask)
  652. {
  653. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  654. unsigned long flags;
  655. int ret = 0;
  656. service_mask = service_mask ? service_mask :
  657. __constant_cpu_to_be64(~0ULL);
  658. service_id &= service_mask;
  659. if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
  660. (service_id != IB_CM_ASSIGN_SERVICE_ID))
  661. return -EINVAL;
  662. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  663. BUG_ON(cm_id->state != IB_CM_IDLE);
  664. cm_id->state = IB_CM_LISTEN;
  665. spin_lock_irqsave(&cm.lock, flags);
  666. if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
  667. cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
  668. cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
  669. } else {
  670. cm_id->service_id = service_id;
  671. cm_id->service_mask = service_mask;
  672. }
  673. cur_cm_id_priv = cm_insert_listen(cm_id_priv);
  674. spin_unlock_irqrestore(&cm.lock, flags);
  675. if (cur_cm_id_priv) {
  676. cm_id->state = IB_CM_IDLE;
  677. ret = -EBUSY;
  678. }
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(ib_cm_listen);
  682. static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
  683. enum cm_msg_sequence msg_seq)
  684. {
  685. u64 hi_tid, low_tid;
  686. hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
  687. low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
  688. (msg_seq << 30));
  689. return cpu_to_be64(hi_tid | low_tid);
  690. }
  691. static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
  692. __be16 attr_id, __be64 tid)
  693. {
  694. hdr->base_version = IB_MGMT_BASE_VERSION;
  695. hdr->mgmt_class = IB_MGMT_CLASS_CM;
  696. hdr->class_version = IB_CM_CLASS_VERSION;
  697. hdr->method = IB_MGMT_METHOD_SEND;
  698. hdr->attr_id = attr_id;
  699. hdr->tid = tid;
  700. }
  701. static void cm_format_req(struct cm_req_msg *req_msg,
  702. struct cm_id_private *cm_id_priv,
  703. struct ib_cm_req_param *param)
  704. {
  705. cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
  706. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
  707. req_msg->local_comm_id = cm_id_priv->id.local_id;
  708. req_msg->service_id = param->service_id;
  709. req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
  710. cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
  711. cm_req_set_resp_res(req_msg, param->responder_resources);
  712. cm_req_set_init_depth(req_msg, param->initiator_depth);
  713. cm_req_set_remote_resp_timeout(req_msg,
  714. param->remote_cm_response_timeout);
  715. cm_req_set_qp_type(req_msg, param->qp_type);
  716. cm_req_set_flow_ctrl(req_msg, param->flow_control);
  717. cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
  718. cm_req_set_local_resp_timeout(req_msg,
  719. param->local_cm_response_timeout);
  720. cm_req_set_retry_count(req_msg, param->retry_count);
  721. req_msg->pkey = param->primary_path->pkey;
  722. cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
  723. cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
  724. cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
  725. cm_req_set_srq(req_msg, param->srq);
  726. req_msg->primary_local_lid = param->primary_path->slid;
  727. req_msg->primary_remote_lid = param->primary_path->dlid;
  728. req_msg->primary_local_gid = param->primary_path->sgid;
  729. req_msg->primary_remote_gid = param->primary_path->dgid;
  730. cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
  731. cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
  732. req_msg->primary_traffic_class = param->primary_path->traffic_class;
  733. req_msg->primary_hop_limit = param->primary_path->hop_limit;
  734. cm_req_set_primary_sl(req_msg, param->primary_path->sl);
  735. cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
  736. cm_req_set_primary_local_ack_timeout(req_msg,
  737. min(31, param->primary_path->packet_life_time + 1));
  738. if (param->alternate_path) {
  739. req_msg->alt_local_lid = param->alternate_path->slid;
  740. req_msg->alt_remote_lid = param->alternate_path->dlid;
  741. req_msg->alt_local_gid = param->alternate_path->sgid;
  742. req_msg->alt_remote_gid = param->alternate_path->dgid;
  743. cm_req_set_alt_flow_label(req_msg,
  744. param->alternate_path->flow_label);
  745. cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
  746. req_msg->alt_traffic_class = param->alternate_path->traffic_class;
  747. req_msg->alt_hop_limit = param->alternate_path->hop_limit;
  748. cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
  749. cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
  750. cm_req_set_alt_local_ack_timeout(req_msg,
  751. min(31, param->alternate_path->packet_life_time + 1));
  752. }
  753. if (param->private_data && param->private_data_len)
  754. memcpy(req_msg->private_data, param->private_data,
  755. param->private_data_len);
  756. }
  757. static int cm_validate_req_param(struct ib_cm_req_param *param)
  758. {
  759. /* peer-to-peer not supported */
  760. if (param->peer_to_peer)
  761. return -EINVAL;
  762. if (!param->primary_path)
  763. return -EINVAL;
  764. if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
  765. return -EINVAL;
  766. if (param->private_data &&
  767. param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
  768. return -EINVAL;
  769. if (param->alternate_path &&
  770. (param->alternate_path->pkey != param->primary_path->pkey ||
  771. param->alternate_path->mtu != param->primary_path->mtu))
  772. return -EINVAL;
  773. return 0;
  774. }
  775. int ib_send_cm_req(struct ib_cm_id *cm_id,
  776. struct ib_cm_req_param *param)
  777. {
  778. struct cm_id_private *cm_id_priv;
  779. struct cm_req_msg *req_msg;
  780. unsigned long flags;
  781. int ret;
  782. ret = cm_validate_req_param(param);
  783. if (ret)
  784. return ret;
  785. /* Verify that we're not in timewait. */
  786. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  787. spin_lock_irqsave(&cm_id_priv->lock, flags);
  788. if (cm_id->state != IB_CM_IDLE) {
  789. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  790. ret = -EINVAL;
  791. goto out;
  792. }
  793. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  794. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  795. id.local_id);
  796. if (IS_ERR(cm_id_priv->timewait_info))
  797. goto out;
  798. ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
  799. if (ret)
  800. goto error1;
  801. if (param->alternate_path) {
  802. ret = cm_init_av_by_path(param->alternate_path,
  803. &cm_id_priv->alt_av);
  804. if (ret)
  805. goto error1;
  806. }
  807. cm_id->service_id = param->service_id;
  808. cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
  809. cm_id_priv->timeout_ms = cm_convert_to_ms(
  810. param->primary_path->packet_life_time) * 2 +
  811. cm_convert_to_ms(
  812. param->remote_cm_response_timeout);
  813. cm_id_priv->max_cm_retries = param->max_cm_retries;
  814. cm_id_priv->initiator_depth = param->initiator_depth;
  815. cm_id_priv->responder_resources = param->responder_resources;
  816. cm_id_priv->retry_count = param->retry_count;
  817. cm_id_priv->path_mtu = param->primary_path->mtu;
  818. cm_id_priv->qp_type = param->qp_type;
  819. ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
  820. if (ret)
  821. goto error1;
  822. req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
  823. cm_format_req(req_msg, cm_id_priv, param);
  824. cm_id_priv->tid = req_msg->hdr.tid;
  825. cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
  826. cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
  827. cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
  828. cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
  829. cm_id_priv->local_ack_timeout =
  830. cm_req_get_primary_local_ack_timeout(req_msg);
  831. spin_lock_irqsave(&cm_id_priv->lock, flags);
  832. ret = ib_post_send_mad(cm_id_priv->msg, NULL);
  833. if (ret) {
  834. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  835. goto error2;
  836. }
  837. BUG_ON(cm_id->state != IB_CM_IDLE);
  838. cm_id->state = IB_CM_REQ_SENT;
  839. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  840. return 0;
  841. error2: cm_free_msg(cm_id_priv->msg);
  842. error1: kfree(cm_id_priv->timewait_info);
  843. out: return ret;
  844. }
  845. EXPORT_SYMBOL(ib_send_cm_req);
  846. static int cm_issue_rej(struct cm_port *port,
  847. struct ib_mad_recv_wc *mad_recv_wc,
  848. enum ib_cm_rej_reason reason,
  849. enum cm_msg_response msg_rejected,
  850. void *ari, u8 ari_length)
  851. {
  852. struct ib_mad_send_buf *msg = NULL;
  853. struct cm_rej_msg *rej_msg, *rcv_msg;
  854. int ret;
  855. ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
  856. if (ret)
  857. return ret;
  858. /* We just need common CM header information. Cast to any message. */
  859. rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
  860. rej_msg = (struct cm_rej_msg *) msg->mad;
  861. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
  862. rej_msg->remote_comm_id = rcv_msg->local_comm_id;
  863. rej_msg->local_comm_id = rcv_msg->remote_comm_id;
  864. cm_rej_set_msg_rejected(rej_msg, msg_rejected);
  865. rej_msg->reason = cpu_to_be16(reason);
  866. if (ari && ari_length) {
  867. cm_rej_set_reject_info_len(rej_msg, ari_length);
  868. memcpy(rej_msg->ari, ari, ari_length);
  869. }
  870. ret = ib_post_send_mad(msg, NULL);
  871. if (ret)
  872. cm_free_msg(msg);
  873. return ret;
  874. }
  875. static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
  876. __be32 local_qpn, __be32 remote_qpn)
  877. {
  878. return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
  879. ((local_ca_guid == remote_ca_guid) &&
  880. (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
  881. }
  882. static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
  883. struct ib_sa_path_rec *primary_path,
  884. struct ib_sa_path_rec *alt_path)
  885. {
  886. memset(primary_path, 0, sizeof *primary_path);
  887. primary_path->dgid = req_msg->primary_local_gid;
  888. primary_path->sgid = req_msg->primary_remote_gid;
  889. primary_path->dlid = req_msg->primary_local_lid;
  890. primary_path->slid = req_msg->primary_remote_lid;
  891. primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
  892. primary_path->hop_limit = req_msg->primary_hop_limit;
  893. primary_path->traffic_class = req_msg->primary_traffic_class;
  894. primary_path->reversible = 1;
  895. primary_path->pkey = req_msg->pkey;
  896. primary_path->sl = cm_req_get_primary_sl(req_msg);
  897. primary_path->mtu_selector = IB_SA_EQ;
  898. primary_path->mtu = cm_req_get_path_mtu(req_msg);
  899. primary_path->rate_selector = IB_SA_EQ;
  900. primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
  901. primary_path->packet_life_time_selector = IB_SA_EQ;
  902. primary_path->packet_life_time =
  903. cm_req_get_primary_local_ack_timeout(req_msg);
  904. primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
  905. if (req_msg->alt_local_lid) {
  906. memset(alt_path, 0, sizeof *alt_path);
  907. alt_path->dgid = req_msg->alt_local_gid;
  908. alt_path->sgid = req_msg->alt_remote_gid;
  909. alt_path->dlid = req_msg->alt_local_lid;
  910. alt_path->slid = req_msg->alt_remote_lid;
  911. alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
  912. alt_path->hop_limit = req_msg->alt_hop_limit;
  913. alt_path->traffic_class = req_msg->alt_traffic_class;
  914. alt_path->reversible = 1;
  915. alt_path->pkey = req_msg->pkey;
  916. alt_path->sl = cm_req_get_alt_sl(req_msg);
  917. alt_path->mtu_selector = IB_SA_EQ;
  918. alt_path->mtu = cm_req_get_path_mtu(req_msg);
  919. alt_path->rate_selector = IB_SA_EQ;
  920. alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
  921. alt_path->packet_life_time_selector = IB_SA_EQ;
  922. alt_path->packet_life_time =
  923. cm_req_get_alt_local_ack_timeout(req_msg);
  924. alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
  925. }
  926. }
  927. static void cm_format_req_event(struct cm_work *work,
  928. struct cm_id_private *cm_id_priv,
  929. struct ib_cm_id *listen_id)
  930. {
  931. struct cm_req_msg *req_msg;
  932. struct ib_cm_req_event_param *param;
  933. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  934. param = &work->cm_event.param.req_rcvd;
  935. param->listen_id = listen_id;
  936. param->port = cm_id_priv->av.port->port_num;
  937. param->primary_path = &work->path[0];
  938. if (req_msg->alt_local_lid)
  939. param->alternate_path = &work->path[1];
  940. else
  941. param->alternate_path = NULL;
  942. param->remote_ca_guid = req_msg->local_ca_guid;
  943. param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
  944. param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
  945. param->qp_type = cm_req_get_qp_type(req_msg);
  946. param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
  947. param->responder_resources = cm_req_get_init_depth(req_msg);
  948. param->initiator_depth = cm_req_get_resp_res(req_msg);
  949. param->local_cm_response_timeout =
  950. cm_req_get_remote_resp_timeout(req_msg);
  951. param->flow_control = cm_req_get_flow_ctrl(req_msg);
  952. param->remote_cm_response_timeout =
  953. cm_req_get_local_resp_timeout(req_msg);
  954. param->retry_count = cm_req_get_retry_count(req_msg);
  955. param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  956. param->srq = cm_req_get_srq(req_msg);
  957. work->cm_event.private_data = &req_msg->private_data;
  958. }
  959. static void cm_process_work(struct cm_id_private *cm_id_priv,
  960. struct cm_work *work)
  961. {
  962. unsigned long flags;
  963. int ret;
  964. /* We will typically only have the current event to report. */
  965. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
  966. cm_free_work(work);
  967. while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
  968. spin_lock_irqsave(&cm_id_priv->lock, flags);
  969. work = cm_dequeue_work(cm_id_priv);
  970. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  971. BUG_ON(!work);
  972. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
  973. &work->cm_event);
  974. cm_free_work(work);
  975. }
  976. cm_deref_id(cm_id_priv);
  977. if (ret)
  978. ib_destroy_cm_id(&cm_id_priv->id);
  979. }
  980. static void cm_format_mra(struct cm_mra_msg *mra_msg,
  981. struct cm_id_private *cm_id_priv,
  982. enum cm_msg_response msg_mraed, u8 service_timeout,
  983. const void *private_data, u8 private_data_len)
  984. {
  985. cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
  986. cm_mra_set_msg_mraed(mra_msg, msg_mraed);
  987. mra_msg->local_comm_id = cm_id_priv->id.local_id;
  988. mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
  989. cm_mra_set_service_timeout(mra_msg, service_timeout);
  990. if (private_data && private_data_len)
  991. memcpy(mra_msg->private_data, private_data, private_data_len);
  992. }
  993. static void cm_format_rej(struct cm_rej_msg *rej_msg,
  994. struct cm_id_private *cm_id_priv,
  995. enum ib_cm_rej_reason reason,
  996. void *ari,
  997. u8 ari_length,
  998. const void *private_data,
  999. u8 private_data_len)
  1000. {
  1001. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
  1002. rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1003. switch(cm_id_priv->id.state) {
  1004. case IB_CM_REQ_RCVD:
  1005. rej_msg->local_comm_id = 0;
  1006. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1007. break;
  1008. case IB_CM_MRA_REQ_SENT:
  1009. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1010. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1011. break;
  1012. case IB_CM_REP_RCVD:
  1013. case IB_CM_MRA_REP_SENT:
  1014. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1015. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
  1016. break;
  1017. default:
  1018. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1019. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
  1020. break;
  1021. }
  1022. rej_msg->reason = cpu_to_be16(reason);
  1023. if (ari && ari_length) {
  1024. cm_rej_set_reject_info_len(rej_msg, ari_length);
  1025. memcpy(rej_msg->ari, ari, ari_length);
  1026. }
  1027. if (private_data && private_data_len)
  1028. memcpy(rej_msg->private_data, private_data, private_data_len);
  1029. }
  1030. static void cm_dup_req_handler(struct cm_work *work,
  1031. struct cm_id_private *cm_id_priv)
  1032. {
  1033. struct ib_mad_send_buf *msg = NULL;
  1034. unsigned long flags;
  1035. int ret;
  1036. /* Quick state check to discard duplicate REQs. */
  1037. if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
  1038. return;
  1039. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1040. if (ret)
  1041. return;
  1042. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1043. switch (cm_id_priv->id.state) {
  1044. case IB_CM_MRA_REQ_SENT:
  1045. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1046. CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
  1047. cm_id_priv->private_data,
  1048. cm_id_priv->private_data_len);
  1049. break;
  1050. case IB_CM_TIMEWAIT:
  1051. cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
  1052. IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
  1053. break;
  1054. default:
  1055. goto unlock;
  1056. }
  1057. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1058. ret = ib_post_send_mad(msg, NULL);
  1059. if (ret)
  1060. goto free;
  1061. return;
  1062. unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1063. free: cm_free_msg(msg);
  1064. }
  1065. static struct cm_id_private * cm_match_req(struct cm_work *work,
  1066. struct cm_id_private *cm_id_priv)
  1067. {
  1068. struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
  1069. struct cm_timewait_info *timewait_info;
  1070. struct cm_req_msg *req_msg;
  1071. unsigned long flags;
  1072. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1073. /* Check for duplicate REQ and stale connections. */
  1074. spin_lock_irqsave(&cm.lock, flags);
  1075. timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
  1076. if (!timewait_info)
  1077. timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
  1078. if (timewait_info) {
  1079. cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
  1080. timewait_info->work.remote_id);
  1081. spin_unlock_irqrestore(&cm.lock, flags);
  1082. if (cur_cm_id_priv) {
  1083. cm_dup_req_handler(work, cur_cm_id_priv);
  1084. cm_deref_id(cur_cm_id_priv);
  1085. } else
  1086. cm_issue_rej(work->port, work->mad_recv_wc,
  1087. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
  1088. NULL, 0);
  1089. goto error;
  1090. }
  1091. /* Find matching listen request. */
  1092. listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
  1093. req_msg->service_id);
  1094. if (!listen_cm_id_priv) {
  1095. spin_unlock_irqrestore(&cm.lock, flags);
  1096. cm_issue_rej(work->port, work->mad_recv_wc,
  1097. IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
  1098. NULL, 0);
  1099. goto error;
  1100. }
  1101. atomic_inc(&listen_cm_id_priv->refcount);
  1102. atomic_inc(&cm_id_priv->refcount);
  1103. cm_id_priv->id.state = IB_CM_REQ_RCVD;
  1104. atomic_inc(&cm_id_priv->work_count);
  1105. spin_unlock_irqrestore(&cm.lock, flags);
  1106. return listen_cm_id_priv;
  1107. error: cm_cleanup_timewait(cm_id_priv->timewait_info);
  1108. return NULL;
  1109. }
  1110. static int cm_req_handler(struct cm_work *work)
  1111. {
  1112. struct ib_cm_id *cm_id;
  1113. struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
  1114. struct cm_req_msg *req_msg;
  1115. int ret;
  1116. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1117. cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
  1118. if (IS_ERR(cm_id))
  1119. return PTR_ERR(cm_id);
  1120. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1121. cm_id_priv->id.remote_id = req_msg->local_comm_id;
  1122. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  1123. &cm_id_priv->av);
  1124. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  1125. id.local_id);
  1126. if (IS_ERR(cm_id_priv->timewait_info)) {
  1127. ret = PTR_ERR(cm_id_priv->timewait_info);
  1128. goto error1;
  1129. }
  1130. cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
  1131. cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
  1132. cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
  1133. listen_cm_id_priv = cm_match_req(work, cm_id_priv);
  1134. if (!listen_cm_id_priv) {
  1135. ret = -EINVAL;
  1136. goto error2;
  1137. }
  1138. cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
  1139. cm_id_priv->id.context = listen_cm_id_priv->id.context;
  1140. cm_id_priv->id.service_id = req_msg->service_id;
  1141. cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
  1142. cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
  1143. ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
  1144. if (ret)
  1145. goto error3;
  1146. if (req_msg->alt_local_lid) {
  1147. ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
  1148. if (ret)
  1149. goto error3;
  1150. }
  1151. cm_id_priv->tid = req_msg->hdr.tid;
  1152. cm_id_priv->timeout_ms = cm_convert_to_ms(
  1153. cm_req_get_local_resp_timeout(req_msg));
  1154. cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
  1155. cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
  1156. cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
  1157. cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
  1158. cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
  1159. cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
  1160. cm_id_priv->local_ack_timeout =
  1161. cm_req_get_primary_local_ack_timeout(req_msg);
  1162. cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
  1163. cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  1164. cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
  1165. cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
  1166. cm_process_work(cm_id_priv, work);
  1167. cm_deref_id(listen_cm_id_priv);
  1168. return 0;
  1169. error3: atomic_dec(&cm_id_priv->refcount);
  1170. cm_deref_id(listen_cm_id_priv);
  1171. cm_cleanup_timewait(cm_id_priv->timewait_info);
  1172. error2: kfree(cm_id_priv->timewait_info);
  1173. cm_id_priv->timewait_info = NULL;
  1174. error1: ib_destroy_cm_id(&cm_id_priv->id);
  1175. return ret;
  1176. }
  1177. static void cm_format_rep(struct cm_rep_msg *rep_msg,
  1178. struct cm_id_private *cm_id_priv,
  1179. struct ib_cm_rep_param *param)
  1180. {
  1181. cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
  1182. rep_msg->local_comm_id = cm_id_priv->id.local_id;
  1183. rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1184. cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
  1185. cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
  1186. rep_msg->resp_resources = param->responder_resources;
  1187. rep_msg->initiator_depth = param->initiator_depth;
  1188. cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
  1189. cm_rep_set_failover(rep_msg, param->failover_accepted);
  1190. cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
  1191. cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
  1192. cm_rep_set_srq(rep_msg, param->srq);
  1193. rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
  1194. if (param->private_data && param->private_data_len)
  1195. memcpy(rep_msg->private_data, param->private_data,
  1196. param->private_data_len);
  1197. }
  1198. int ib_send_cm_rep(struct ib_cm_id *cm_id,
  1199. struct ib_cm_rep_param *param)
  1200. {
  1201. struct cm_id_private *cm_id_priv;
  1202. struct ib_mad_send_buf *msg;
  1203. struct cm_rep_msg *rep_msg;
  1204. unsigned long flags;
  1205. int ret;
  1206. if (param->private_data &&
  1207. param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
  1208. return -EINVAL;
  1209. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1210. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1211. if (cm_id->state != IB_CM_REQ_RCVD &&
  1212. cm_id->state != IB_CM_MRA_REQ_SENT) {
  1213. ret = -EINVAL;
  1214. goto out;
  1215. }
  1216. ret = cm_alloc_msg(cm_id_priv, &msg);
  1217. if (ret)
  1218. goto out;
  1219. rep_msg = (struct cm_rep_msg *) msg->mad;
  1220. cm_format_rep(rep_msg, cm_id_priv, param);
  1221. msg->timeout_ms = cm_id_priv->timeout_ms;
  1222. msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
  1223. ret = ib_post_send_mad(msg, NULL);
  1224. if (ret) {
  1225. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1226. cm_free_msg(msg);
  1227. return ret;
  1228. }
  1229. cm_id->state = IB_CM_REP_SENT;
  1230. cm_id_priv->msg = msg;
  1231. cm_id_priv->initiator_depth = param->initiator_depth;
  1232. cm_id_priv->responder_resources = param->responder_resources;
  1233. cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
  1234. cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
  1235. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1236. return ret;
  1237. }
  1238. EXPORT_SYMBOL(ib_send_cm_rep);
  1239. static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
  1240. struct cm_id_private *cm_id_priv,
  1241. const void *private_data,
  1242. u8 private_data_len)
  1243. {
  1244. cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
  1245. rtu_msg->local_comm_id = cm_id_priv->id.local_id;
  1246. rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1247. if (private_data && private_data_len)
  1248. memcpy(rtu_msg->private_data, private_data, private_data_len);
  1249. }
  1250. int ib_send_cm_rtu(struct ib_cm_id *cm_id,
  1251. const void *private_data,
  1252. u8 private_data_len)
  1253. {
  1254. struct cm_id_private *cm_id_priv;
  1255. struct ib_mad_send_buf *msg;
  1256. unsigned long flags;
  1257. void *data;
  1258. int ret;
  1259. if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
  1260. return -EINVAL;
  1261. data = cm_copy_private_data(private_data, private_data_len);
  1262. if (IS_ERR(data))
  1263. return PTR_ERR(data);
  1264. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1265. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1266. if (cm_id->state != IB_CM_REP_RCVD &&
  1267. cm_id->state != IB_CM_MRA_REP_SENT) {
  1268. ret = -EINVAL;
  1269. goto error;
  1270. }
  1271. ret = cm_alloc_msg(cm_id_priv, &msg);
  1272. if (ret)
  1273. goto error;
  1274. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1275. private_data, private_data_len);
  1276. ret = ib_post_send_mad(msg, NULL);
  1277. if (ret) {
  1278. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1279. cm_free_msg(msg);
  1280. kfree(data);
  1281. return ret;
  1282. }
  1283. cm_id->state = IB_CM_ESTABLISHED;
  1284. cm_set_private_data(cm_id_priv, data, private_data_len);
  1285. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1286. return 0;
  1287. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1288. kfree(data);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL(ib_send_cm_rtu);
  1292. static void cm_format_rep_event(struct cm_work *work)
  1293. {
  1294. struct cm_rep_msg *rep_msg;
  1295. struct ib_cm_rep_event_param *param;
  1296. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1297. param = &work->cm_event.param.rep_rcvd;
  1298. param->remote_ca_guid = rep_msg->local_ca_guid;
  1299. param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
  1300. param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
  1301. param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
  1302. param->responder_resources = rep_msg->initiator_depth;
  1303. param->initiator_depth = rep_msg->resp_resources;
  1304. param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
  1305. param->failover_accepted = cm_rep_get_failover(rep_msg);
  1306. param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
  1307. param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1308. param->srq = cm_rep_get_srq(rep_msg);
  1309. work->cm_event.private_data = &rep_msg->private_data;
  1310. }
  1311. static void cm_dup_rep_handler(struct cm_work *work)
  1312. {
  1313. struct cm_id_private *cm_id_priv;
  1314. struct cm_rep_msg *rep_msg;
  1315. struct ib_mad_send_buf *msg = NULL;
  1316. unsigned long flags;
  1317. int ret;
  1318. rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
  1319. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
  1320. rep_msg->local_comm_id);
  1321. if (!cm_id_priv)
  1322. return;
  1323. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1324. if (ret)
  1325. goto deref;
  1326. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1327. if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
  1328. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1329. cm_id_priv->private_data,
  1330. cm_id_priv->private_data_len);
  1331. else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
  1332. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1333. CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
  1334. cm_id_priv->private_data,
  1335. cm_id_priv->private_data_len);
  1336. else
  1337. goto unlock;
  1338. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1339. ret = ib_post_send_mad(msg, NULL);
  1340. if (ret)
  1341. goto free;
  1342. goto deref;
  1343. unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1344. free: cm_free_msg(msg);
  1345. deref: cm_deref_id(cm_id_priv);
  1346. }
  1347. static int cm_rep_handler(struct cm_work *work)
  1348. {
  1349. struct cm_id_private *cm_id_priv;
  1350. struct cm_rep_msg *rep_msg;
  1351. unsigned long flags;
  1352. int ret;
  1353. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1354. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
  1355. if (!cm_id_priv) {
  1356. cm_dup_rep_handler(work);
  1357. return -EINVAL;
  1358. }
  1359. cm_format_rep_event(work);
  1360. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1361. switch (cm_id_priv->id.state) {
  1362. case IB_CM_REQ_SENT:
  1363. case IB_CM_MRA_REQ_RCVD:
  1364. break;
  1365. default:
  1366. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1367. ret = -EINVAL;
  1368. goto error;
  1369. }
  1370. cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
  1371. cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
  1372. cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
  1373. spin_lock(&cm.lock);
  1374. /* Check for duplicate REP. */
  1375. if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
  1376. spin_unlock(&cm.lock);
  1377. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1378. ret = -EINVAL;
  1379. goto error;
  1380. }
  1381. /* Check for a stale connection. */
  1382. if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
  1383. rb_erase(&cm_id_priv->timewait_info->remote_id_node,
  1384. &cm.remote_id_table);
  1385. cm_id_priv->timewait_info->inserted_remote_id = 0;
  1386. spin_unlock(&cm.lock);
  1387. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1388. cm_issue_rej(work->port, work->mad_recv_wc,
  1389. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
  1390. NULL, 0);
  1391. ret = -EINVAL;
  1392. goto error;
  1393. }
  1394. spin_unlock(&cm.lock);
  1395. cm_id_priv->id.state = IB_CM_REP_RCVD;
  1396. cm_id_priv->id.remote_id = rep_msg->local_comm_id;
  1397. cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
  1398. cm_id_priv->initiator_depth = rep_msg->resp_resources;
  1399. cm_id_priv->responder_resources = rep_msg->initiator_depth;
  1400. cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
  1401. cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1402. /* todo: handle peer_to_peer */
  1403. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1404. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1405. if (!ret)
  1406. list_add_tail(&work->list, &cm_id_priv->work_list);
  1407. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1408. if (ret)
  1409. cm_process_work(cm_id_priv, work);
  1410. else
  1411. cm_deref_id(cm_id_priv);
  1412. return 0;
  1413. error:
  1414. cm_deref_id(cm_id_priv);
  1415. return ret;
  1416. }
  1417. static int cm_establish_handler(struct cm_work *work)
  1418. {
  1419. struct cm_id_private *cm_id_priv;
  1420. unsigned long flags;
  1421. int ret;
  1422. /* See comment in ib_cm_establish about lookup. */
  1423. cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
  1424. if (!cm_id_priv)
  1425. return -EINVAL;
  1426. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1427. if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
  1428. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1429. goto out;
  1430. }
  1431. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1432. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1433. if (!ret)
  1434. list_add_tail(&work->list, &cm_id_priv->work_list);
  1435. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1436. if (ret)
  1437. cm_process_work(cm_id_priv, work);
  1438. else
  1439. cm_deref_id(cm_id_priv);
  1440. return 0;
  1441. out:
  1442. cm_deref_id(cm_id_priv);
  1443. return -EINVAL;
  1444. }
  1445. static int cm_rtu_handler(struct cm_work *work)
  1446. {
  1447. struct cm_id_private *cm_id_priv;
  1448. struct cm_rtu_msg *rtu_msg;
  1449. unsigned long flags;
  1450. int ret;
  1451. rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
  1452. cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
  1453. rtu_msg->local_comm_id);
  1454. if (!cm_id_priv)
  1455. return -EINVAL;
  1456. work->cm_event.private_data = &rtu_msg->private_data;
  1457. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1458. if (cm_id_priv->id.state != IB_CM_REP_SENT &&
  1459. cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
  1460. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1461. goto out;
  1462. }
  1463. cm_id_priv->id.state = IB_CM_ESTABLISHED;
  1464. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1465. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1466. if (!ret)
  1467. list_add_tail(&work->list, &cm_id_priv->work_list);
  1468. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1469. if (ret)
  1470. cm_process_work(cm_id_priv, work);
  1471. else
  1472. cm_deref_id(cm_id_priv);
  1473. return 0;
  1474. out:
  1475. cm_deref_id(cm_id_priv);
  1476. return -EINVAL;
  1477. }
  1478. static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
  1479. struct cm_id_private *cm_id_priv,
  1480. const void *private_data,
  1481. u8 private_data_len)
  1482. {
  1483. cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
  1484. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
  1485. dreq_msg->local_comm_id = cm_id_priv->id.local_id;
  1486. dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1487. cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
  1488. if (private_data && private_data_len)
  1489. memcpy(dreq_msg->private_data, private_data, private_data_len);
  1490. }
  1491. int ib_send_cm_dreq(struct ib_cm_id *cm_id,
  1492. const void *private_data,
  1493. u8 private_data_len)
  1494. {
  1495. struct cm_id_private *cm_id_priv;
  1496. struct ib_mad_send_buf *msg;
  1497. unsigned long flags;
  1498. int ret;
  1499. if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
  1500. return -EINVAL;
  1501. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1502. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1503. if (cm_id->state != IB_CM_ESTABLISHED) {
  1504. ret = -EINVAL;
  1505. goto out;
  1506. }
  1507. ret = cm_alloc_msg(cm_id_priv, &msg);
  1508. if (ret) {
  1509. cm_enter_timewait(cm_id_priv);
  1510. goto out;
  1511. }
  1512. cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
  1513. private_data, private_data_len);
  1514. msg->timeout_ms = cm_id_priv->timeout_ms;
  1515. msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
  1516. ret = ib_post_send_mad(msg, NULL);
  1517. if (ret) {
  1518. cm_enter_timewait(cm_id_priv);
  1519. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1520. cm_free_msg(msg);
  1521. return ret;
  1522. }
  1523. cm_id->state = IB_CM_DREQ_SENT;
  1524. cm_id_priv->msg = msg;
  1525. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1526. return ret;
  1527. }
  1528. EXPORT_SYMBOL(ib_send_cm_dreq);
  1529. static void cm_format_drep(struct cm_drep_msg *drep_msg,
  1530. struct cm_id_private *cm_id_priv,
  1531. const void *private_data,
  1532. u8 private_data_len)
  1533. {
  1534. cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
  1535. drep_msg->local_comm_id = cm_id_priv->id.local_id;
  1536. drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1537. if (private_data && private_data_len)
  1538. memcpy(drep_msg->private_data, private_data, private_data_len);
  1539. }
  1540. int ib_send_cm_drep(struct ib_cm_id *cm_id,
  1541. const void *private_data,
  1542. u8 private_data_len)
  1543. {
  1544. struct cm_id_private *cm_id_priv;
  1545. struct ib_mad_send_buf *msg;
  1546. unsigned long flags;
  1547. void *data;
  1548. int ret;
  1549. if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
  1550. return -EINVAL;
  1551. data = cm_copy_private_data(private_data, private_data_len);
  1552. if (IS_ERR(data))
  1553. return PTR_ERR(data);
  1554. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1555. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1556. if (cm_id->state != IB_CM_DREQ_RCVD) {
  1557. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1558. kfree(data);
  1559. return -EINVAL;
  1560. }
  1561. cm_set_private_data(cm_id_priv, data, private_data_len);
  1562. cm_enter_timewait(cm_id_priv);
  1563. ret = cm_alloc_msg(cm_id_priv, &msg);
  1564. if (ret)
  1565. goto out;
  1566. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  1567. private_data, private_data_len);
  1568. ret = ib_post_send_mad(msg, NULL);
  1569. if (ret) {
  1570. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1571. cm_free_msg(msg);
  1572. return ret;
  1573. }
  1574. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1575. return ret;
  1576. }
  1577. EXPORT_SYMBOL(ib_send_cm_drep);
  1578. static int cm_dreq_handler(struct cm_work *work)
  1579. {
  1580. struct cm_id_private *cm_id_priv;
  1581. struct cm_dreq_msg *dreq_msg;
  1582. struct ib_mad_send_buf *msg = NULL;
  1583. unsigned long flags;
  1584. int ret;
  1585. dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
  1586. cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
  1587. dreq_msg->local_comm_id);
  1588. if (!cm_id_priv)
  1589. return -EINVAL;
  1590. work->cm_event.private_data = &dreq_msg->private_data;
  1591. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1592. if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
  1593. goto unlock;
  1594. switch (cm_id_priv->id.state) {
  1595. case IB_CM_REP_SENT:
  1596. case IB_CM_DREQ_SENT:
  1597. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1598. break;
  1599. case IB_CM_ESTABLISHED:
  1600. case IB_CM_MRA_REP_RCVD:
  1601. break;
  1602. case IB_CM_TIMEWAIT:
  1603. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  1604. goto unlock;
  1605. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  1606. cm_id_priv->private_data,
  1607. cm_id_priv->private_data_len);
  1608. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1609. if (ib_post_send_mad(msg, NULL))
  1610. cm_free_msg(msg);
  1611. goto deref;
  1612. default:
  1613. goto unlock;
  1614. }
  1615. cm_id_priv->id.state = IB_CM_DREQ_RCVD;
  1616. cm_id_priv->tid = dreq_msg->hdr.tid;
  1617. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1618. if (!ret)
  1619. list_add_tail(&work->list, &cm_id_priv->work_list);
  1620. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1621. if (ret)
  1622. cm_process_work(cm_id_priv, work);
  1623. else
  1624. cm_deref_id(cm_id_priv);
  1625. return 0;
  1626. unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1627. deref: cm_deref_id(cm_id_priv);
  1628. return -EINVAL;
  1629. }
  1630. static int cm_drep_handler(struct cm_work *work)
  1631. {
  1632. struct cm_id_private *cm_id_priv;
  1633. struct cm_drep_msg *drep_msg;
  1634. unsigned long flags;
  1635. int ret;
  1636. drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
  1637. cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
  1638. drep_msg->local_comm_id);
  1639. if (!cm_id_priv)
  1640. return -EINVAL;
  1641. work->cm_event.private_data = &drep_msg->private_data;
  1642. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1643. if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
  1644. cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
  1645. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1646. goto out;
  1647. }
  1648. cm_enter_timewait(cm_id_priv);
  1649. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1650. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1651. if (!ret)
  1652. list_add_tail(&work->list, &cm_id_priv->work_list);
  1653. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1654. if (ret)
  1655. cm_process_work(cm_id_priv, work);
  1656. else
  1657. cm_deref_id(cm_id_priv);
  1658. return 0;
  1659. out:
  1660. cm_deref_id(cm_id_priv);
  1661. return -EINVAL;
  1662. }
  1663. int ib_send_cm_rej(struct ib_cm_id *cm_id,
  1664. enum ib_cm_rej_reason reason,
  1665. void *ari,
  1666. u8 ari_length,
  1667. const void *private_data,
  1668. u8 private_data_len)
  1669. {
  1670. struct cm_id_private *cm_id_priv;
  1671. struct ib_mad_send_buf *msg;
  1672. unsigned long flags;
  1673. int ret;
  1674. if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
  1675. (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
  1676. return -EINVAL;
  1677. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1678. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1679. switch (cm_id->state) {
  1680. case IB_CM_REQ_SENT:
  1681. case IB_CM_MRA_REQ_RCVD:
  1682. case IB_CM_REQ_RCVD:
  1683. case IB_CM_MRA_REQ_SENT:
  1684. case IB_CM_REP_RCVD:
  1685. case IB_CM_MRA_REP_SENT:
  1686. ret = cm_alloc_msg(cm_id_priv, &msg);
  1687. if (!ret)
  1688. cm_format_rej((struct cm_rej_msg *) msg->mad,
  1689. cm_id_priv, reason, ari, ari_length,
  1690. private_data, private_data_len);
  1691. cm_reset_to_idle(cm_id_priv);
  1692. break;
  1693. case IB_CM_REP_SENT:
  1694. case IB_CM_MRA_REP_RCVD:
  1695. ret = cm_alloc_msg(cm_id_priv, &msg);
  1696. if (!ret)
  1697. cm_format_rej((struct cm_rej_msg *) msg->mad,
  1698. cm_id_priv, reason, ari, ari_length,
  1699. private_data, private_data_len);
  1700. cm_enter_timewait(cm_id_priv);
  1701. break;
  1702. default:
  1703. ret = -EINVAL;
  1704. goto out;
  1705. }
  1706. if (ret)
  1707. goto out;
  1708. ret = ib_post_send_mad(msg, NULL);
  1709. if (ret)
  1710. cm_free_msg(msg);
  1711. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1712. return ret;
  1713. }
  1714. EXPORT_SYMBOL(ib_send_cm_rej);
  1715. static void cm_format_rej_event(struct cm_work *work)
  1716. {
  1717. struct cm_rej_msg *rej_msg;
  1718. struct ib_cm_rej_event_param *param;
  1719. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  1720. param = &work->cm_event.param.rej_rcvd;
  1721. param->ari = rej_msg->ari;
  1722. param->ari_length = cm_rej_get_reject_info_len(rej_msg);
  1723. param->reason = __be16_to_cpu(rej_msg->reason);
  1724. work->cm_event.private_data = &rej_msg->private_data;
  1725. }
  1726. static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
  1727. {
  1728. struct cm_timewait_info *timewait_info;
  1729. struct cm_id_private *cm_id_priv;
  1730. unsigned long flags;
  1731. __be32 remote_id;
  1732. remote_id = rej_msg->local_comm_id;
  1733. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
  1734. spin_lock_irqsave(&cm.lock, flags);
  1735. timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
  1736. remote_id);
  1737. if (!timewait_info) {
  1738. spin_unlock_irqrestore(&cm.lock, flags);
  1739. return NULL;
  1740. }
  1741. cm_id_priv = idr_find(&cm.local_id_table,
  1742. (__force int) timewait_info->work.local_id);
  1743. if (cm_id_priv) {
  1744. if (cm_id_priv->id.remote_id == remote_id)
  1745. atomic_inc(&cm_id_priv->refcount);
  1746. else
  1747. cm_id_priv = NULL;
  1748. }
  1749. spin_unlock_irqrestore(&cm.lock, flags);
  1750. } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
  1751. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
  1752. else
  1753. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
  1754. return cm_id_priv;
  1755. }
  1756. static int cm_rej_handler(struct cm_work *work)
  1757. {
  1758. struct cm_id_private *cm_id_priv;
  1759. struct cm_rej_msg *rej_msg;
  1760. unsigned long flags;
  1761. int ret;
  1762. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  1763. cm_id_priv = cm_acquire_rejected_id(rej_msg);
  1764. if (!cm_id_priv)
  1765. return -EINVAL;
  1766. cm_format_rej_event(work);
  1767. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1768. switch (cm_id_priv->id.state) {
  1769. case IB_CM_REQ_SENT:
  1770. case IB_CM_MRA_REQ_RCVD:
  1771. case IB_CM_REP_SENT:
  1772. case IB_CM_MRA_REP_RCVD:
  1773. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1774. /* fall through */
  1775. case IB_CM_REQ_RCVD:
  1776. case IB_CM_MRA_REQ_SENT:
  1777. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
  1778. cm_enter_timewait(cm_id_priv);
  1779. else
  1780. cm_reset_to_idle(cm_id_priv);
  1781. break;
  1782. case IB_CM_DREQ_SENT:
  1783. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1784. /* fall through */
  1785. case IB_CM_REP_RCVD:
  1786. case IB_CM_MRA_REP_SENT:
  1787. case IB_CM_ESTABLISHED:
  1788. cm_enter_timewait(cm_id_priv);
  1789. break;
  1790. default:
  1791. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1792. ret = -EINVAL;
  1793. goto out;
  1794. }
  1795. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1796. if (!ret)
  1797. list_add_tail(&work->list, &cm_id_priv->work_list);
  1798. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1799. if (ret)
  1800. cm_process_work(cm_id_priv, work);
  1801. else
  1802. cm_deref_id(cm_id_priv);
  1803. return 0;
  1804. out:
  1805. cm_deref_id(cm_id_priv);
  1806. return -EINVAL;
  1807. }
  1808. int ib_send_cm_mra(struct ib_cm_id *cm_id,
  1809. u8 service_timeout,
  1810. const void *private_data,
  1811. u8 private_data_len)
  1812. {
  1813. struct cm_id_private *cm_id_priv;
  1814. struct ib_mad_send_buf *msg;
  1815. void *data;
  1816. unsigned long flags;
  1817. int ret;
  1818. if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
  1819. return -EINVAL;
  1820. data = cm_copy_private_data(private_data, private_data_len);
  1821. if (IS_ERR(data))
  1822. return PTR_ERR(data);
  1823. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1824. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1825. switch(cm_id_priv->id.state) {
  1826. case IB_CM_REQ_RCVD:
  1827. ret = cm_alloc_msg(cm_id_priv, &msg);
  1828. if (ret)
  1829. goto error1;
  1830. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1831. CM_MSG_RESPONSE_REQ, service_timeout,
  1832. private_data, private_data_len);
  1833. ret = ib_post_send_mad(msg, NULL);
  1834. if (ret)
  1835. goto error2;
  1836. cm_id->state = IB_CM_MRA_REQ_SENT;
  1837. break;
  1838. case IB_CM_REP_RCVD:
  1839. ret = cm_alloc_msg(cm_id_priv, &msg);
  1840. if (ret)
  1841. goto error1;
  1842. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1843. CM_MSG_RESPONSE_REP, service_timeout,
  1844. private_data, private_data_len);
  1845. ret = ib_post_send_mad(msg, NULL);
  1846. if (ret)
  1847. goto error2;
  1848. cm_id->state = IB_CM_MRA_REP_SENT;
  1849. break;
  1850. case IB_CM_ESTABLISHED:
  1851. ret = cm_alloc_msg(cm_id_priv, &msg);
  1852. if (ret)
  1853. goto error1;
  1854. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1855. CM_MSG_RESPONSE_OTHER, service_timeout,
  1856. private_data, private_data_len);
  1857. ret = ib_post_send_mad(msg, NULL);
  1858. if (ret)
  1859. goto error2;
  1860. cm_id->lap_state = IB_CM_MRA_LAP_SENT;
  1861. break;
  1862. default:
  1863. ret = -EINVAL;
  1864. goto error1;
  1865. }
  1866. cm_id_priv->service_timeout = service_timeout;
  1867. cm_set_private_data(cm_id_priv, data, private_data_len);
  1868. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1869. return 0;
  1870. error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1871. kfree(data);
  1872. return ret;
  1873. error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1874. kfree(data);
  1875. cm_free_msg(msg);
  1876. return ret;
  1877. }
  1878. EXPORT_SYMBOL(ib_send_cm_mra);
  1879. static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
  1880. {
  1881. switch (cm_mra_get_msg_mraed(mra_msg)) {
  1882. case CM_MSG_RESPONSE_REQ:
  1883. return cm_acquire_id(mra_msg->remote_comm_id, 0);
  1884. case CM_MSG_RESPONSE_REP:
  1885. case CM_MSG_RESPONSE_OTHER:
  1886. return cm_acquire_id(mra_msg->remote_comm_id,
  1887. mra_msg->local_comm_id);
  1888. default:
  1889. return NULL;
  1890. }
  1891. }
  1892. static int cm_mra_handler(struct cm_work *work)
  1893. {
  1894. struct cm_id_private *cm_id_priv;
  1895. struct cm_mra_msg *mra_msg;
  1896. unsigned long flags;
  1897. int timeout, ret;
  1898. mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
  1899. cm_id_priv = cm_acquire_mraed_id(mra_msg);
  1900. if (!cm_id_priv)
  1901. return -EINVAL;
  1902. work->cm_event.private_data = &mra_msg->private_data;
  1903. work->cm_event.param.mra_rcvd.service_timeout =
  1904. cm_mra_get_service_timeout(mra_msg);
  1905. timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
  1906. cm_convert_to_ms(cm_id_priv->av.packet_life_time);
  1907. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1908. switch (cm_id_priv->id.state) {
  1909. case IB_CM_REQ_SENT:
  1910. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
  1911. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  1912. cm_id_priv->msg, timeout))
  1913. goto out;
  1914. cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
  1915. break;
  1916. case IB_CM_REP_SENT:
  1917. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
  1918. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  1919. cm_id_priv->msg, timeout))
  1920. goto out;
  1921. cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
  1922. break;
  1923. case IB_CM_ESTABLISHED:
  1924. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
  1925. cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
  1926. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  1927. cm_id_priv->msg, timeout))
  1928. goto out;
  1929. cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
  1930. break;
  1931. default:
  1932. goto out;
  1933. }
  1934. cm_id_priv->msg->context[1] = (void *) (unsigned long)
  1935. cm_id_priv->id.state;
  1936. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1937. if (!ret)
  1938. list_add_tail(&work->list, &cm_id_priv->work_list);
  1939. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1940. if (ret)
  1941. cm_process_work(cm_id_priv, work);
  1942. else
  1943. cm_deref_id(cm_id_priv);
  1944. return 0;
  1945. out:
  1946. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1947. cm_deref_id(cm_id_priv);
  1948. return -EINVAL;
  1949. }
  1950. static void cm_format_lap(struct cm_lap_msg *lap_msg,
  1951. struct cm_id_private *cm_id_priv,
  1952. struct ib_sa_path_rec *alternate_path,
  1953. const void *private_data,
  1954. u8 private_data_len)
  1955. {
  1956. cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
  1957. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
  1958. lap_msg->local_comm_id = cm_id_priv->id.local_id;
  1959. lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1960. cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
  1961. /* todo: need remote CM response timeout */
  1962. cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
  1963. lap_msg->alt_local_lid = alternate_path->slid;
  1964. lap_msg->alt_remote_lid = alternate_path->dlid;
  1965. lap_msg->alt_local_gid = alternate_path->sgid;
  1966. lap_msg->alt_remote_gid = alternate_path->dgid;
  1967. cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
  1968. cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
  1969. lap_msg->alt_hop_limit = alternate_path->hop_limit;
  1970. cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
  1971. cm_lap_set_sl(lap_msg, alternate_path->sl);
  1972. cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
  1973. cm_lap_set_local_ack_timeout(lap_msg,
  1974. min(31, alternate_path->packet_life_time + 1));
  1975. if (private_data && private_data_len)
  1976. memcpy(lap_msg->private_data, private_data, private_data_len);
  1977. }
  1978. int ib_send_cm_lap(struct ib_cm_id *cm_id,
  1979. struct ib_sa_path_rec *alternate_path,
  1980. const void *private_data,
  1981. u8 private_data_len)
  1982. {
  1983. struct cm_id_private *cm_id_priv;
  1984. struct ib_mad_send_buf *msg;
  1985. unsigned long flags;
  1986. int ret;
  1987. if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
  1988. return -EINVAL;
  1989. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1990. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1991. if (cm_id->state != IB_CM_ESTABLISHED ||
  1992. cm_id->lap_state != IB_CM_LAP_IDLE) {
  1993. ret = -EINVAL;
  1994. goto out;
  1995. }
  1996. ret = cm_alloc_msg(cm_id_priv, &msg);
  1997. if (ret)
  1998. goto out;
  1999. cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
  2000. alternate_path, private_data, private_data_len);
  2001. msg->timeout_ms = cm_id_priv->timeout_ms;
  2002. msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
  2003. ret = ib_post_send_mad(msg, NULL);
  2004. if (ret) {
  2005. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2006. cm_free_msg(msg);
  2007. return ret;
  2008. }
  2009. cm_id->lap_state = IB_CM_LAP_SENT;
  2010. cm_id_priv->msg = msg;
  2011. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2012. return ret;
  2013. }
  2014. EXPORT_SYMBOL(ib_send_cm_lap);
  2015. static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
  2016. struct cm_lap_msg *lap_msg)
  2017. {
  2018. memset(path, 0, sizeof *path);
  2019. path->dgid = lap_msg->alt_local_gid;
  2020. path->sgid = lap_msg->alt_remote_gid;
  2021. path->dlid = lap_msg->alt_local_lid;
  2022. path->slid = lap_msg->alt_remote_lid;
  2023. path->flow_label = cm_lap_get_flow_label(lap_msg);
  2024. path->hop_limit = lap_msg->alt_hop_limit;
  2025. path->traffic_class = cm_lap_get_traffic_class(lap_msg);
  2026. path->reversible = 1;
  2027. /* pkey is same as in REQ */
  2028. path->sl = cm_lap_get_sl(lap_msg);
  2029. path->mtu_selector = IB_SA_EQ;
  2030. /* mtu is same as in REQ */
  2031. path->rate_selector = IB_SA_EQ;
  2032. path->rate = cm_lap_get_packet_rate(lap_msg);
  2033. path->packet_life_time_selector = IB_SA_EQ;
  2034. path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
  2035. path->packet_life_time -= (path->packet_life_time > 0);
  2036. }
  2037. static int cm_lap_handler(struct cm_work *work)
  2038. {
  2039. struct cm_id_private *cm_id_priv;
  2040. struct cm_lap_msg *lap_msg;
  2041. struct ib_cm_lap_event_param *param;
  2042. struct ib_mad_send_buf *msg = NULL;
  2043. unsigned long flags;
  2044. int ret;
  2045. /* todo: verify LAP request and send reject APR if invalid. */
  2046. lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
  2047. cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
  2048. lap_msg->local_comm_id);
  2049. if (!cm_id_priv)
  2050. return -EINVAL;
  2051. param = &work->cm_event.param.lap_rcvd;
  2052. param->alternate_path = &work->path[0];
  2053. cm_format_path_from_lap(param->alternate_path, lap_msg);
  2054. work->cm_event.private_data = &lap_msg->private_data;
  2055. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2056. if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
  2057. goto unlock;
  2058. switch (cm_id_priv->id.lap_state) {
  2059. case IB_CM_LAP_IDLE:
  2060. break;
  2061. case IB_CM_MRA_LAP_SENT:
  2062. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  2063. goto unlock;
  2064. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  2065. CM_MSG_RESPONSE_OTHER,
  2066. cm_id_priv->service_timeout,
  2067. cm_id_priv->private_data,
  2068. cm_id_priv->private_data_len);
  2069. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2070. if (ib_post_send_mad(msg, NULL))
  2071. cm_free_msg(msg);
  2072. goto deref;
  2073. default:
  2074. goto unlock;
  2075. }
  2076. cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
  2077. cm_id_priv->tid = lap_msg->hdr.tid;
  2078. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2079. if (!ret)
  2080. list_add_tail(&work->list, &cm_id_priv->work_list);
  2081. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2082. if (ret)
  2083. cm_process_work(cm_id_priv, work);
  2084. else
  2085. cm_deref_id(cm_id_priv);
  2086. return 0;
  2087. unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2088. deref: cm_deref_id(cm_id_priv);
  2089. return -EINVAL;
  2090. }
  2091. static void cm_format_apr(struct cm_apr_msg *apr_msg,
  2092. struct cm_id_private *cm_id_priv,
  2093. enum ib_cm_apr_status status,
  2094. void *info,
  2095. u8 info_length,
  2096. const void *private_data,
  2097. u8 private_data_len)
  2098. {
  2099. cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
  2100. apr_msg->local_comm_id = cm_id_priv->id.local_id;
  2101. apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
  2102. apr_msg->ap_status = (u8) status;
  2103. if (info && info_length) {
  2104. apr_msg->info_length = info_length;
  2105. memcpy(apr_msg->info, info, info_length);
  2106. }
  2107. if (private_data && private_data_len)
  2108. memcpy(apr_msg->private_data, private_data, private_data_len);
  2109. }
  2110. int ib_send_cm_apr(struct ib_cm_id *cm_id,
  2111. enum ib_cm_apr_status status,
  2112. void *info,
  2113. u8 info_length,
  2114. const void *private_data,
  2115. u8 private_data_len)
  2116. {
  2117. struct cm_id_private *cm_id_priv;
  2118. struct ib_mad_send_buf *msg;
  2119. unsigned long flags;
  2120. int ret;
  2121. if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
  2122. (info && info_length > IB_CM_APR_INFO_LENGTH))
  2123. return -EINVAL;
  2124. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2125. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2126. if (cm_id->state != IB_CM_ESTABLISHED ||
  2127. (cm_id->lap_state != IB_CM_LAP_RCVD &&
  2128. cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
  2129. ret = -EINVAL;
  2130. goto out;
  2131. }
  2132. ret = cm_alloc_msg(cm_id_priv, &msg);
  2133. if (ret)
  2134. goto out;
  2135. cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
  2136. info, info_length, private_data, private_data_len);
  2137. ret = ib_post_send_mad(msg, NULL);
  2138. if (ret) {
  2139. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2140. cm_free_msg(msg);
  2141. return ret;
  2142. }
  2143. cm_id->lap_state = IB_CM_LAP_IDLE;
  2144. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2145. return ret;
  2146. }
  2147. EXPORT_SYMBOL(ib_send_cm_apr);
  2148. static int cm_apr_handler(struct cm_work *work)
  2149. {
  2150. struct cm_id_private *cm_id_priv;
  2151. struct cm_apr_msg *apr_msg;
  2152. unsigned long flags;
  2153. int ret;
  2154. apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
  2155. cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
  2156. apr_msg->local_comm_id);
  2157. if (!cm_id_priv)
  2158. return -EINVAL; /* Unmatched reply. */
  2159. work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
  2160. work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
  2161. work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
  2162. work->cm_event.private_data = &apr_msg->private_data;
  2163. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2164. if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
  2165. (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
  2166. cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
  2167. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2168. goto out;
  2169. }
  2170. cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
  2171. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2172. cm_id_priv->msg = NULL;
  2173. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2174. if (!ret)
  2175. list_add_tail(&work->list, &cm_id_priv->work_list);
  2176. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2177. if (ret)
  2178. cm_process_work(cm_id_priv, work);
  2179. else
  2180. cm_deref_id(cm_id_priv);
  2181. return 0;
  2182. out:
  2183. cm_deref_id(cm_id_priv);
  2184. return -EINVAL;
  2185. }
  2186. static int cm_timewait_handler(struct cm_work *work)
  2187. {
  2188. struct cm_timewait_info *timewait_info;
  2189. struct cm_id_private *cm_id_priv;
  2190. unsigned long flags;
  2191. int ret;
  2192. timewait_info = (struct cm_timewait_info *)work;
  2193. cm_cleanup_timewait(timewait_info);
  2194. cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
  2195. timewait_info->work.remote_id);
  2196. if (!cm_id_priv)
  2197. return -EINVAL;
  2198. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2199. if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
  2200. cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
  2201. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2202. goto out;
  2203. }
  2204. cm_id_priv->id.state = IB_CM_IDLE;
  2205. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2206. if (!ret)
  2207. list_add_tail(&work->list, &cm_id_priv->work_list);
  2208. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2209. if (ret)
  2210. cm_process_work(cm_id_priv, work);
  2211. else
  2212. cm_deref_id(cm_id_priv);
  2213. return 0;
  2214. out:
  2215. cm_deref_id(cm_id_priv);
  2216. return -EINVAL;
  2217. }
  2218. static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
  2219. struct cm_id_private *cm_id_priv,
  2220. struct ib_cm_sidr_req_param *param)
  2221. {
  2222. cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
  2223. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
  2224. sidr_req_msg->request_id = cm_id_priv->id.local_id;
  2225. sidr_req_msg->pkey = cpu_to_be16(param->pkey);
  2226. sidr_req_msg->service_id = param->service_id;
  2227. if (param->private_data && param->private_data_len)
  2228. memcpy(sidr_req_msg->private_data, param->private_data,
  2229. param->private_data_len);
  2230. }
  2231. int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
  2232. struct ib_cm_sidr_req_param *param)
  2233. {
  2234. struct cm_id_private *cm_id_priv;
  2235. struct ib_mad_send_buf *msg;
  2236. unsigned long flags;
  2237. int ret;
  2238. if (!param->path || (param->private_data &&
  2239. param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
  2240. return -EINVAL;
  2241. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2242. ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
  2243. if (ret)
  2244. goto out;
  2245. cm_id->service_id = param->service_id;
  2246. cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
  2247. cm_id_priv->timeout_ms = param->timeout_ms;
  2248. cm_id_priv->max_cm_retries = param->max_cm_retries;
  2249. ret = cm_alloc_msg(cm_id_priv, &msg);
  2250. if (ret)
  2251. goto out;
  2252. cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
  2253. param);
  2254. msg->timeout_ms = cm_id_priv->timeout_ms;
  2255. msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
  2256. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2257. if (cm_id->state == IB_CM_IDLE)
  2258. ret = ib_post_send_mad(msg, NULL);
  2259. else
  2260. ret = -EINVAL;
  2261. if (ret) {
  2262. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2263. cm_free_msg(msg);
  2264. goto out;
  2265. }
  2266. cm_id->state = IB_CM_SIDR_REQ_SENT;
  2267. cm_id_priv->msg = msg;
  2268. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2269. out:
  2270. return ret;
  2271. }
  2272. EXPORT_SYMBOL(ib_send_cm_sidr_req);
  2273. static void cm_format_sidr_req_event(struct cm_work *work,
  2274. struct ib_cm_id *listen_id)
  2275. {
  2276. struct cm_sidr_req_msg *sidr_req_msg;
  2277. struct ib_cm_sidr_req_event_param *param;
  2278. sidr_req_msg = (struct cm_sidr_req_msg *)
  2279. work->mad_recv_wc->recv_buf.mad;
  2280. param = &work->cm_event.param.sidr_req_rcvd;
  2281. param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
  2282. param->listen_id = listen_id;
  2283. param->port = work->port->port_num;
  2284. work->cm_event.private_data = &sidr_req_msg->private_data;
  2285. }
  2286. static int cm_sidr_req_handler(struct cm_work *work)
  2287. {
  2288. struct ib_cm_id *cm_id;
  2289. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  2290. struct cm_sidr_req_msg *sidr_req_msg;
  2291. struct ib_wc *wc;
  2292. unsigned long flags;
  2293. cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
  2294. if (IS_ERR(cm_id))
  2295. return PTR_ERR(cm_id);
  2296. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2297. /* Record SGID/SLID and request ID for lookup. */
  2298. sidr_req_msg = (struct cm_sidr_req_msg *)
  2299. work->mad_recv_wc->recv_buf.mad;
  2300. wc = work->mad_recv_wc->wc;
  2301. cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
  2302. cm_id_priv->av.dgid.global.interface_id = 0;
  2303. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  2304. &cm_id_priv->av);
  2305. cm_id_priv->id.remote_id = sidr_req_msg->request_id;
  2306. cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
  2307. cm_id_priv->tid = sidr_req_msg->hdr.tid;
  2308. atomic_inc(&cm_id_priv->work_count);
  2309. spin_lock_irqsave(&cm.lock, flags);
  2310. cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
  2311. if (cur_cm_id_priv) {
  2312. spin_unlock_irqrestore(&cm.lock, flags);
  2313. goto out; /* Duplicate message. */
  2314. }
  2315. cur_cm_id_priv = cm_find_listen(cm_id->device,
  2316. sidr_req_msg->service_id);
  2317. if (!cur_cm_id_priv) {
  2318. rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  2319. spin_unlock_irqrestore(&cm.lock, flags);
  2320. /* todo: reply with no match */
  2321. goto out; /* No match. */
  2322. }
  2323. atomic_inc(&cur_cm_id_priv->refcount);
  2324. spin_unlock_irqrestore(&cm.lock, flags);
  2325. cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
  2326. cm_id_priv->id.context = cur_cm_id_priv->id.context;
  2327. cm_id_priv->id.service_id = sidr_req_msg->service_id;
  2328. cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
  2329. cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
  2330. cm_process_work(cm_id_priv, work);
  2331. cm_deref_id(cur_cm_id_priv);
  2332. return 0;
  2333. out:
  2334. ib_destroy_cm_id(&cm_id_priv->id);
  2335. return -EINVAL;
  2336. }
  2337. static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
  2338. struct cm_id_private *cm_id_priv,
  2339. struct ib_cm_sidr_rep_param *param)
  2340. {
  2341. cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
  2342. cm_id_priv->tid);
  2343. sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
  2344. sidr_rep_msg->status = param->status;
  2345. cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
  2346. sidr_rep_msg->service_id = cm_id_priv->id.service_id;
  2347. sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
  2348. if (param->info && param->info_length)
  2349. memcpy(sidr_rep_msg->info, param->info, param->info_length);
  2350. if (param->private_data && param->private_data_len)
  2351. memcpy(sidr_rep_msg->private_data, param->private_data,
  2352. param->private_data_len);
  2353. }
  2354. int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
  2355. struct ib_cm_sidr_rep_param *param)
  2356. {
  2357. struct cm_id_private *cm_id_priv;
  2358. struct ib_mad_send_buf *msg;
  2359. unsigned long flags;
  2360. int ret;
  2361. if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
  2362. (param->private_data &&
  2363. param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
  2364. return -EINVAL;
  2365. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2366. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2367. if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
  2368. ret = -EINVAL;
  2369. goto error;
  2370. }
  2371. ret = cm_alloc_msg(cm_id_priv, &msg);
  2372. if (ret)
  2373. goto error;
  2374. cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
  2375. param);
  2376. ret = ib_post_send_mad(msg, NULL);
  2377. if (ret) {
  2378. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2379. cm_free_msg(msg);
  2380. return ret;
  2381. }
  2382. cm_id->state = IB_CM_IDLE;
  2383. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2384. spin_lock_irqsave(&cm.lock, flags);
  2385. rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  2386. spin_unlock_irqrestore(&cm.lock, flags);
  2387. return 0;
  2388. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2389. return ret;
  2390. }
  2391. EXPORT_SYMBOL(ib_send_cm_sidr_rep);
  2392. static void cm_format_sidr_rep_event(struct cm_work *work)
  2393. {
  2394. struct cm_sidr_rep_msg *sidr_rep_msg;
  2395. struct ib_cm_sidr_rep_event_param *param;
  2396. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2397. work->mad_recv_wc->recv_buf.mad;
  2398. param = &work->cm_event.param.sidr_rep_rcvd;
  2399. param->status = sidr_rep_msg->status;
  2400. param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
  2401. param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
  2402. param->info = &sidr_rep_msg->info;
  2403. param->info_len = sidr_rep_msg->info_length;
  2404. work->cm_event.private_data = &sidr_rep_msg->private_data;
  2405. }
  2406. static int cm_sidr_rep_handler(struct cm_work *work)
  2407. {
  2408. struct cm_sidr_rep_msg *sidr_rep_msg;
  2409. struct cm_id_private *cm_id_priv;
  2410. unsigned long flags;
  2411. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2412. work->mad_recv_wc->recv_buf.mad;
  2413. cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
  2414. if (!cm_id_priv)
  2415. return -EINVAL; /* Unmatched reply. */
  2416. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2417. if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
  2418. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2419. goto out;
  2420. }
  2421. cm_id_priv->id.state = IB_CM_IDLE;
  2422. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2423. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2424. cm_format_sidr_rep_event(work);
  2425. cm_process_work(cm_id_priv, work);
  2426. return 0;
  2427. out:
  2428. cm_deref_id(cm_id_priv);
  2429. return -EINVAL;
  2430. }
  2431. static void cm_process_send_error(struct ib_mad_send_buf *msg,
  2432. enum ib_wc_status wc_status)
  2433. {
  2434. struct cm_id_private *cm_id_priv;
  2435. struct ib_cm_event cm_event;
  2436. enum ib_cm_state state;
  2437. unsigned long flags;
  2438. int ret;
  2439. memset(&cm_event, 0, sizeof cm_event);
  2440. cm_id_priv = msg->context[0];
  2441. /* Discard old sends or ones without a response. */
  2442. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2443. state = (enum ib_cm_state) (unsigned long) msg->context[1];
  2444. if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
  2445. goto discard;
  2446. switch (state) {
  2447. case IB_CM_REQ_SENT:
  2448. case IB_CM_MRA_REQ_RCVD:
  2449. cm_reset_to_idle(cm_id_priv);
  2450. cm_event.event = IB_CM_REQ_ERROR;
  2451. break;
  2452. case IB_CM_REP_SENT:
  2453. case IB_CM_MRA_REP_RCVD:
  2454. cm_reset_to_idle(cm_id_priv);
  2455. cm_event.event = IB_CM_REP_ERROR;
  2456. break;
  2457. case IB_CM_DREQ_SENT:
  2458. cm_enter_timewait(cm_id_priv);
  2459. cm_event.event = IB_CM_DREQ_ERROR;
  2460. break;
  2461. case IB_CM_SIDR_REQ_SENT:
  2462. cm_id_priv->id.state = IB_CM_IDLE;
  2463. cm_event.event = IB_CM_SIDR_REQ_ERROR;
  2464. break;
  2465. default:
  2466. goto discard;
  2467. }
  2468. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2469. cm_event.param.send_status = wc_status;
  2470. /* No other events can occur on the cm_id at this point. */
  2471. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
  2472. cm_free_msg(msg);
  2473. if (ret)
  2474. ib_destroy_cm_id(&cm_id_priv->id);
  2475. return;
  2476. discard:
  2477. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2478. cm_free_msg(msg);
  2479. }
  2480. static void cm_send_handler(struct ib_mad_agent *mad_agent,
  2481. struct ib_mad_send_wc *mad_send_wc)
  2482. {
  2483. struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
  2484. switch (mad_send_wc->status) {
  2485. case IB_WC_SUCCESS:
  2486. case IB_WC_WR_FLUSH_ERR:
  2487. cm_free_msg(msg);
  2488. break;
  2489. default:
  2490. if (msg->context[0] && msg->context[1])
  2491. cm_process_send_error(msg, mad_send_wc->status);
  2492. else
  2493. cm_free_msg(msg);
  2494. break;
  2495. }
  2496. }
  2497. static void cm_work_handler(void *data)
  2498. {
  2499. struct cm_work *work = data;
  2500. int ret;
  2501. switch (work->cm_event.event) {
  2502. case IB_CM_REQ_RECEIVED:
  2503. ret = cm_req_handler(work);
  2504. break;
  2505. case IB_CM_MRA_RECEIVED:
  2506. ret = cm_mra_handler(work);
  2507. break;
  2508. case IB_CM_REJ_RECEIVED:
  2509. ret = cm_rej_handler(work);
  2510. break;
  2511. case IB_CM_REP_RECEIVED:
  2512. ret = cm_rep_handler(work);
  2513. break;
  2514. case IB_CM_RTU_RECEIVED:
  2515. ret = cm_rtu_handler(work);
  2516. break;
  2517. case IB_CM_USER_ESTABLISHED:
  2518. ret = cm_establish_handler(work);
  2519. break;
  2520. case IB_CM_DREQ_RECEIVED:
  2521. ret = cm_dreq_handler(work);
  2522. break;
  2523. case IB_CM_DREP_RECEIVED:
  2524. ret = cm_drep_handler(work);
  2525. break;
  2526. case IB_CM_SIDR_REQ_RECEIVED:
  2527. ret = cm_sidr_req_handler(work);
  2528. break;
  2529. case IB_CM_SIDR_REP_RECEIVED:
  2530. ret = cm_sidr_rep_handler(work);
  2531. break;
  2532. case IB_CM_LAP_RECEIVED:
  2533. ret = cm_lap_handler(work);
  2534. break;
  2535. case IB_CM_APR_RECEIVED:
  2536. ret = cm_apr_handler(work);
  2537. break;
  2538. case IB_CM_TIMEWAIT_EXIT:
  2539. ret = cm_timewait_handler(work);
  2540. break;
  2541. default:
  2542. ret = -EINVAL;
  2543. break;
  2544. }
  2545. if (ret)
  2546. cm_free_work(work);
  2547. }
  2548. int ib_cm_establish(struct ib_cm_id *cm_id)
  2549. {
  2550. struct cm_id_private *cm_id_priv;
  2551. struct cm_work *work;
  2552. unsigned long flags;
  2553. int ret = 0;
  2554. work = kmalloc(sizeof *work, GFP_ATOMIC);
  2555. if (!work)
  2556. return -ENOMEM;
  2557. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2558. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2559. switch (cm_id->state)
  2560. {
  2561. case IB_CM_REP_SENT:
  2562. case IB_CM_MRA_REP_RCVD:
  2563. cm_id->state = IB_CM_ESTABLISHED;
  2564. break;
  2565. case IB_CM_ESTABLISHED:
  2566. ret = -EISCONN;
  2567. break;
  2568. default:
  2569. ret = -EINVAL;
  2570. break;
  2571. }
  2572. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2573. if (ret) {
  2574. kfree(work);
  2575. goto out;
  2576. }
  2577. /*
  2578. * The CM worker thread may try to destroy the cm_id before it
  2579. * can execute this work item. To prevent potential deadlock,
  2580. * we need to find the cm_id once we're in the context of the
  2581. * worker thread, rather than holding a reference on it.
  2582. */
  2583. INIT_WORK(&work->work, cm_work_handler, work);
  2584. work->local_id = cm_id->local_id;
  2585. work->remote_id = cm_id->remote_id;
  2586. work->mad_recv_wc = NULL;
  2587. work->cm_event.event = IB_CM_USER_ESTABLISHED;
  2588. queue_work(cm.wq, &work->work);
  2589. out:
  2590. return ret;
  2591. }
  2592. EXPORT_SYMBOL(ib_cm_establish);
  2593. static void cm_recv_handler(struct ib_mad_agent *mad_agent,
  2594. struct ib_mad_recv_wc *mad_recv_wc)
  2595. {
  2596. struct cm_work *work;
  2597. enum ib_cm_event_type event;
  2598. int paths = 0;
  2599. switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
  2600. case CM_REQ_ATTR_ID:
  2601. paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
  2602. alt_local_lid != 0);
  2603. event = IB_CM_REQ_RECEIVED;
  2604. break;
  2605. case CM_MRA_ATTR_ID:
  2606. event = IB_CM_MRA_RECEIVED;
  2607. break;
  2608. case CM_REJ_ATTR_ID:
  2609. event = IB_CM_REJ_RECEIVED;
  2610. break;
  2611. case CM_REP_ATTR_ID:
  2612. event = IB_CM_REP_RECEIVED;
  2613. break;
  2614. case CM_RTU_ATTR_ID:
  2615. event = IB_CM_RTU_RECEIVED;
  2616. break;
  2617. case CM_DREQ_ATTR_ID:
  2618. event = IB_CM_DREQ_RECEIVED;
  2619. break;
  2620. case CM_DREP_ATTR_ID:
  2621. event = IB_CM_DREP_RECEIVED;
  2622. break;
  2623. case CM_SIDR_REQ_ATTR_ID:
  2624. event = IB_CM_SIDR_REQ_RECEIVED;
  2625. break;
  2626. case CM_SIDR_REP_ATTR_ID:
  2627. event = IB_CM_SIDR_REP_RECEIVED;
  2628. break;
  2629. case CM_LAP_ATTR_ID:
  2630. paths = 1;
  2631. event = IB_CM_LAP_RECEIVED;
  2632. break;
  2633. case CM_APR_ATTR_ID:
  2634. event = IB_CM_APR_RECEIVED;
  2635. break;
  2636. default:
  2637. ib_free_recv_mad(mad_recv_wc);
  2638. return;
  2639. }
  2640. work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
  2641. GFP_KERNEL);
  2642. if (!work) {
  2643. ib_free_recv_mad(mad_recv_wc);
  2644. return;
  2645. }
  2646. INIT_WORK(&work->work, cm_work_handler, work);
  2647. work->cm_event.event = event;
  2648. work->mad_recv_wc = mad_recv_wc;
  2649. work->port = (struct cm_port *)mad_agent->context;
  2650. queue_work(cm.wq, &work->work);
  2651. }
  2652. static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
  2653. struct ib_qp_attr *qp_attr,
  2654. int *qp_attr_mask)
  2655. {
  2656. unsigned long flags;
  2657. int ret;
  2658. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2659. switch (cm_id_priv->id.state) {
  2660. case IB_CM_REQ_SENT:
  2661. case IB_CM_MRA_REQ_RCVD:
  2662. case IB_CM_REQ_RCVD:
  2663. case IB_CM_MRA_REQ_SENT:
  2664. case IB_CM_REP_RCVD:
  2665. case IB_CM_MRA_REP_SENT:
  2666. case IB_CM_REP_SENT:
  2667. case IB_CM_MRA_REP_RCVD:
  2668. case IB_CM_ESTABLISHED:
  2669. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
  2670. IB_QP_PKEY_INDEX | IB_QP_PORT;
  2671. qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
  2672. IB_ACCESS_REMOTE_WRITE;
  2673. if (cm_id_priv->responder_resources)
  2674. qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
  2675. qp_attr->pkey_index = cm_id_priv->av.pkey_index;
  2676. qp_attr->port_num = cm_id_priv->av.port->port_num;
  2677. ret = 0;
  2678. break;
  2679. default:
  2680. ret = -EINVAL;
  2681. break;
  2682. }
  2683. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2684. return ret;
  2685. }
  2686. static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
  2687. struct ib_qp_attr *qp_attr,
  2688. int *qp_attr_mask)
  2689. {
  2690. unsigned long flags;
  2691. int ret;
  2692. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2693. switch (cm_id_priv->id.state) {
  2694. case IB_CM_REQ_RCVD:
  2695. case IB_CM_MRA_REQ_SENT:
  2696. case IB_CM_REP_RCVD:
  2697. case IB_CM_MRA_REP_SENT:
  2698. case IB_CM_REP_SENT:
  2699. case IB_CM_MRA_REP_RCVD:
  2700. case IB_CM_ESTABLISHED:
  2701. *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
  2702. IB_QP_DEST_QPN | IB_QP_RQ_PSN;
  2703. qp_attr->ah_attr = cm_id_priv->av.ah_attr;
  2704. qp_attr->path_mtu = cm_id_priv->path_mtu;
  2705. qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
  2706. qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
  2707. if (cm_id_priv->qp_type == IB_QPT_RC) {
  2708. *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
  2709. IB_QP_MIN_RNR_TIMER;
  2710. qp_attr->max_dest_rd_atomic =
  2711. cm_id_priv->responder_resources;
  2712. qp_attr->min_rnr_timer = 0;
  2713. }
  2714. if (cm_id_priv->alt_av.ah_attr.dlid) {
  2715. *qp_attr_mask |= IB_QP_ALT_PATH;
  2716. qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
  2717. }
  2718. ret = 0;
  2719. break;
  2720. default:
  2721. ret = -EINVAL;
  2722. break;
  2723. }
  2724. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2725. return ret;
  2726. }
  2727. static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
  2728. struct ib_qp_attr *qp_attr,
  2729. int *qp_attr_mask)
  2730. {
  2731. unsigned long flags;
  2732. int ret;
  2733. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2734. switch (cm_id_priv->id.state) {
  2735. case IB_CM_REP_RCVD:
  2736. case IB_CM_MRA_REP_SENT:
  2737. case IB_CM_REP_SENT:
  2738. case IB_CM_MRA_REP_RCVD:
  2739. case IB_CM_ESTABLISHED:
  2740. *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
  2741. qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
  2742. if (cm_id_priv->qp_type == IB_QPT_RC) {
  2743. *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
  2744. IB_QP_RNR_RETRY |
  2745. IB_QP_MAX_QP_RD_ATOMIC;
  2746. qp_attr->timeout = cm_id_priv->local_ack_timeout;
  2747. qp_attr->retry_cnt = cm_id_priv->retry_count;
  2748. qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
  2749. qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
  2750. }
  2751. if (cm_id_priv->alt_av.ah_attr.dlid) {
  2752. *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
  2753. qp_attr->path_mig_state = IB_MIG_REARM;
  2754. }
  2755. ret = 0;
  2756. break;
  2757. default:
  2758. ret = -EINVAL;
  2759. break;
  2760. }
  2761. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2762. return ret;
  2763. }
  2764. int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
  2765. struct ib_qp_attr *qp_attr,
  2766. int *qp_attr_mask)
  2767. {
  2768. struct cm_id_private *cm_id_priv;
  2769. int ret;
  2770. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2771. switch (qp_attr->qp_state) {
  2772. case IB_QPS_INIT:
  2773. ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
  2774. break;
  2775. case IB_QPS_RTR:
  2776. ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
  2777. break;
  2778. case IB_QPS_RTS:
  2779. ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
  2780. break;
  2781. default:
  2782. ret = -EINVAL;
  2783. break;
  2784. }
  2785. return ret;
  2786. }
  2787. EXPORT_SYMBOL(ib_cm_init_qp_attr);
  2788. static void cm_add_one(struct ib_device *device)
  2789. {
  2790. struct cm_device *cm_dev;
  2791. struct cm_port *port;
  2792. struct ib_mad_reg_req reg_req = {
  2793. .mgmt_class = IB_MGMT_CLASS_CM,
  2794. .mgmt_class_version = IB_CM_CLASS_VERSION
  2795. };
  2796. struct ib_port_modify port_modify = {
  2797. .set_port_cap_mask = IB_PORT_CM_SUP
  2798. };
  2799. unsigned long flags;
  2800. int ret;
  2801. u8 i;
  2802. cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
  2803. device->phys_port_cnt, GFP_KERNEL);
  2804. if (!cm_dev)
  2805. return;
  2806. cm_dev->device = device;
  2807. cm_dev->ca_guid = device->node_guid;
  2808. set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
  2809. for (i = 1; i <= device->phys_port_cnt; i++) {
  2810. port = &cm_dev->port[i-1];
  2811. port->cm_dev = cm_dev;
  2812. port->port_num = i;
  2813. port->mad_agent = ib_register_mad_agent(device, i,
  2814. IB_QPT_GSI,
  2815. &reg_req,
  2816. 0,
  2817. cm_send_handler,
  2818. cm_recv_handler,
  2819. port);
  2820. if (IS_ERR(port->mad_agent))
  2821. goto error1;
  2822. ret = ib_modify_port(device, i, 0, &port_modify);
  2823. if (ret)
  2824. goto error2;
  2825. }
  2826. ib_set_client_data(device, &cm_client, cm_dev);
  2827. write_lock_irqsave(&cm.device_lock, flags);
  2828. list_add_tail(&cm_dev->list, &cm.device_list);
  2829. write_unlock_irqrestore(&cm.device_lock, flags);
  2830. return;
  2831. error2:
  2832. ib_unregister_mad_agent(port->mad_agent);
  2833. error1:
  2834. port_modify.set_port_cap_mask = 0;
  2835. port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
  2836. while (--i) {
  2837. port = &cm_dev->port[i-1];
  2838. ib_modify_port(device, port->port_num, 0, &port_modify);
  2839. ib_unregister_mad_agent(port->mad_agent);
  2840. }
  2841. kfree(cm_dev);
  2842. }
  2843. static void cm_remove_one(struct ib_device *device)
  2844. {
  2845. struct cm_device *cm_dev;
  2846. struct cm_port *port;
  2847. struct ib_port_modify port_modify = {
  2848. .clr_port_cap_mask = IB_PORT_CM_SUP
  2849. };
  2850. unsigned long flags;
  2851. int i;
  2852. cm_dev = ib_get_client_data(device, &cm_client);
  2853. if (!cm_dev)
  2854. return;
  2855. write_lock_irqsave(&cm.device_lock, flags);
  2856. list_del(&cm_dev->list);
  2857. write_unlock_irqrestore(&cm.device_lock, flags);
  2858. for (i = 1; i <= device->phys_port_cnt; i++) {
  2859. port = &cm_dev->port[i-1];
  2860. ib_modify_port(device, port->port_num, 0, &port_modify);
  2861. ib_unregister_mad_agent(port->mad_agent);
  2862. }
  2863. kfree(cm_dev);
  2864. }
  2865. static int __init ib_cm_init(void)
  2866. {
  2867. int ret;
  2868. memset(&cm, 0, sizeof cm);
  2869. INIT_LIST_HEAD(&cm.device_list);
  2870. rwlock_init(&cm.device_lock);
  2871. spin_lock_init(&cm.lock);
  2872. cm.listen_service_table = RB_ROOT;
  2873. cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
  2874. cm.remote_id_table = RB_ROOT;
  2875. cm.remote_qp_table = RB_ROOT;
  2876. cm.remote_sidr_table = RB_ROOT;
  2877. idr_init(&cm.local_id_table);
  2878. idr_pre_get(&cm.local_id_table, GFP_KERNEL);
  2879. cm.wq = create_workqueue("ib_cm");
  2880. if (!cm.wq)
  2881. return -ENOMEM;
  2882. ret = ib_register_client(&cm_client);
  2883. if (ret)
  2884. goto error;
  2885. return 0;
  2886. error:
  2887. destroy_workqueue(cm.wq);
  2888. return ret;
  2889. }
  2890. static void __exit ib_cm_cleanup(void)
  2891. {
  2892. flush_workqueue(cm.wq);
  2893. destroy_workqueue(cm.wq);
  2894. ib_unregister_client(&cm_client);
  2895. idr_destroy(&cm.local_id_table);
  2896. }
  2897. module_init(ib_cm_init);
  2898. module_exit(ib_cm_cleanup);