cm.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145
  1. /*
  2. * Copyright (c) 2004-2007 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. #include <linux/completion.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/device.h>
  38. #include <linux/module.h>
  39. #include <linux/err.h>
  40. #include <linux/idr.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/random.h>
  43. #include <linux/rbtree.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/slab.h>
  46. #include <linux/sysfs.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/kdev_t.h>
  49. #include <linux/etherdevice.h>
  50. #include <rdma/ib_cache.h>
  51. #include <rdma/ib_cm.h>
  52. #include "cm_msgs.h"
  53. MODULE_AUTHOR("Sean Hefty");
  54. MODULE_DESCRIPTION("InfiniBand CM");
  55. MODULE_LICENSE("Dual BSD/GPL");
  56. static void cm_add_one(struct ib_device *device);
  57. static void cm_remove_one(struct ib_device *device, void *client_data);
  58. static struct ib_client cm_client = {
  59. .name = "cm",
  60. .add = cm_add_one,
  61. .remove = cm_remove_one
  62. };
  63. static struct ib_cm {
  64. spinlock_t lock;
  65. struct list_head device_list;
  66. rwlock_t device_lock;
  67. struct rb_root listen_service_table;
  68. u64 listen_service_id;
  69. /* struct rb_root peer_service_table; todo: fix peer to peer */
  70. struct rb_root remote_qp_table;
  71. struct rb_root remote_id_table;
  72. struct rb_root remote_sidr_table;
  73. struct idr local_id_table;
  74. __be32 random_id_operand;
  75. struct list_head timewait_list;
  76. struct workqueue_struct *wq;
  77. /* Sync on cm change port state */
  78. spinlock_t state_lock;
  79. } cm;
  80. /* Counter indexes ordered by attribute ID */
  81. enum {
  82. CM_REQ_COUNTER,
  83. CM_MRA_COUNTER,
  84. CM_REJ_COUNTER,
  85. CM_REP_COUNTER,
  86. CM_RTU_COUNTER,
  87. CM_DREQ_COUNTER,
  88. CM_DREP_COUNTER,
  89. CM_SIDR_REQ_COUNTER,
  90. CM_SIDR_REP_COUNTER,
  91. CM_LAP_COUNTER,
  92. CM_APR_COUNTER,
  93. CM_ATTR_COUNT,
  94. CM_ATTR_ID_OFFSET = 0x0010,
  95. };
  96. enum {
  97. CM_XMIT,
  98. CM_XMIT_RETRIES,
  99. CM_RECV,
  100. CM_RECV_DUPLICATES,
  101. CM_COUNTER_GROUPS
  102. };
  103. static char const counter_group_names[CM_COUNTER_GROUPS]
  104. [sizeof("cm_rx_duplicates")] = {
  105. "cm_tx_msgs", "cm_tx_retries",
  106. "cm_rx_msgs", "cm_rx_duplicates"
  107. };
  108. struct cm_counter_group {
  109. struct kobject obj;
  110. atomic_long_t counter[CM_ATTR_COUNT];
  111. };
  112. struct cm_counter_attribute {
  113. struct attribute attr;
  114. int index;
  115. };
  116. #define CM_COUNTER_ATTR(_name, _index) \
  117. struct cm_counter_attribute cm_##_name##_counter_attr = { \
  118. .attr = { .name = __stringify(_name), .mode = 0444 }, \
  119. .index = _index \
  120. }
  121. static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
  122. static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
  123. static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
  124. static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
  125. static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
  126. static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
  127. static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
  128. static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
  129. static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
  130. static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
  131. static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
  132. static struct attribute *cm_counter_default_attrs[] = {
  133. &cm_req_counter_attr.attr,
  134. &cm_mra_counter_attr.attr,
  135. &cm_rej_counter_attr.attr,
  136. &cm_rep_counter_attr.attr,
  137. &cm_rtu_counter_attr.attr,
  138. &cm_dreq_counter_attr.attr,
  139. &cm_drep_counter_attr.attr,
  140. &cm_sidr_req_counter_attr.attr,
  141. &cm_sidr_rep_counter_attr.attr,
  142. &cm_lap_counter_attr.attr,
  143. &cm_apr_counter_attr.attr,
  144. NULL
  145. };
  146. struct cm_port {
  147. struct cm_device *cm_dev;
  148. struct ib_mad_agent *mad_agent;
  149. struct kobject port_obj;
  150. u8 port_num;
  151. struct list_head cm_priv_prim_list;
  152. struct list_head cm_priv_altr_list;
  153. struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
  154. };
  155. struct cm_device {
  156. struct list_head list;
  157. struct ib_device *ib_device;
  158. struct device *device;
  159. u8 ack_delay;
  160. int going_down;
  161. struct cm_port *port[0];
  162. };
  163. struct cm_av {
  164. struct cm_port *port;
  165. union ib_gid dgid;
  166. struct ib_ah_attr ah_attr;
  167. u16 pkey_index;
  168. u8 timeout;
  169. };
  170. struct cm_work {
  171. struct delayed_work work;
  172. struct list_head list;
  173. struct cm_port *port;
  174. struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
  175. __be32 local_id; /* Established / timewait */
  176. __be32 remote_id;
  177. struct ib_cm_event cm_event;
  178. struct ib_sa_path_rec path[0];
  179. };
  180. struct cm_timewait_info {
  181. struct cm_work work; /* Must be first. */
  182. struct list_head list;
  183. struct rb_node remote_qp_node;
  184. struct rb_node remote_id_node;
  185. __be64 remote_ca_guid;
  186. __be32 remote_qpn;
  187. u8 inserted_remote_qp;
  188. u8 inserted_remote_id;
  189. };
  190. struct cm_id_private {
  191. struct ib_cm_id id;
  192. struct rb_node service_node;
  193. struct rb_node sidr_id_node;
  194. spinlock_t lock; /* Do not acquire inside cm.lock */
  195. struct completion comp;
  196. atomic_t refcount;
  197. /* Number of clients sharing this ib_cm_id. Only valid for listeners.
  198. * Protected by the cm.lock spinlock. */
  199. int listen_sharecount;
  200. struct ib_mad_send_buf *msg;
  201. struct cm_timewait_info *timewait_info;
  202. /* todo: use alternate port on send failure */
  203. struct cm_av av;
  204. struct cm_av alt_av;
  205. void *private_data;
  206. __be64 tid;
  207. __be32 local_qpn;
  208. __be32 remote_qpn;
  209. enum ib_qp_type qp_type;
  210. __be32 sq_psn;
  211. __be32 rq_psn;
  212. int timeout_ms;
  213. enum ib_mtu path_mtu;
  214. __be16 pkey;
  215. u8 private_data_len;
  216. u8 max_cm_retries;
  217. u8 peer_to_peer;
  218. u8 responder_resources;
  219. u8 initiator_depth;
  220. u8 retry_count;
  221. u8 rnr_retry_count;
  222. u8 service_timeout;
  223. u8 target_ack_delay;
  224. struct list_head prim_list;
  225. struct list_head altr_list;
  226. /* Indicates that the send port mad is registered and av is set */
  227. int prim_send_port_not_ready;
  228. int altr_send_port_not_ready;
  229. struct list_head work_list;
  230. atomic_t work_count;
  231. };
  232. static void cm_work_handler(struct work_struct *work);
  233. static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
  234. {
  235. if (atomic_dec_and_test(&cm_id_priv->refcount))
  236. complete(&cm_id_priv->comp);
  237. }
  238. static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
  239. struct ib_mad_send_buf **msg)
  240. {
  241. struct ib_mad_agent *mad_agent;
  242. struct ib_mad_send_buf *m;
  243. struct ib_ah *ah;
  244. struct cm_av *av;
  245. unsigned long flags, flags2;
  246. int ret = 0;
  247. /* don't let the port to be released till the agent is down */
  248. spin_lock_irqsave(&cm.state_lock, flags2);
  249. spin_lock_irqsave(&cm.lock, flags);
  250. if (!cm_id_priv->prim_send_port_not_ready)
  251. av = &cm_id_priv->av;
  252. else if (!cm_id_priv->altr_send_port_not_ready &&
  253. (cm_id_priv->alt_av.port))
  254. av = &cm_id_priv->alt_av;
  255. else {
  256. pr_info("%s: not valid CM id\n", __func__);
  257. ret = -ENODEV;
  258. spin_unlock_irqrestore(&cm.lock, flags);
  259. goto out;
  260. }
  261. spin_unlock_irqrestore(&cm.lock, flags);
  262. /* Make sure the port haven't released the mad yet */
  263. mad_agent = cm_id_priv->av.port->mad_agent;
  264. if (!mad_agent) {
  265. pr_info("%s: not a valid MAD agent\n", __func__);
  266. ret = -ENODEV;
  267. goto out;
  268. }
  269. ah = ib_create_ah(mad_agent->qp->pd, &av->ah_attr);
  270. if (IS_ERR(ah)) {
  271. ret = PTR_ERR(ah);
  272. goto out;
  273. }
  274. m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
  275. av->pkey_index,
  276. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  277. GFP_ATOMIC,
  278. IB_MGMT_BASE_VERSION);
  279. if (IS_ERR(m)) {
  280. ib_destroy_ah(ah);
  281. ret = PTR_ERR(m);
  282. goto out;
  283. }
  284. /* Timeout set by caller if response is expected. */
  285. m->ah = ah;
  286. m->retries = cm_id_priv->max_cm_retries;
  287. atomic_inc(&cm_id_priv->refcount);
  288. m->context[0] = cm_id_priv;
  289. *msg = m;
  290. out:
  291. spin_unlock_irqrestore(&cm.state_lock, flags2);
  292. return ret;
  293. }
  294. static int cm_alloc_response_msg(struct cm_port *port,
  295. struct ib_mad_recv_wc *mad_recv_wc,
  296. struct ib_mad_send_buf **msg)
  297. {
  298. struct ib_mad_send_buf *m;
  299. struct ib_ah *ah;
  300. ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
  301. mad_recv_wc->recv_buf.grh, port->port_num);
  302. if (IS_ERR(ah))
  303. return PTR_ERR(ah);
  304. m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
  305. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  306. GFP_ATOMIC,
  307. IB_MGMT_BASE_VERSION);
  308. if (IS_ERR(m)) {
  309. ib_destroy_ah(ah);
  310. return PTR_ERR(m);
  311. }
  312. m->ah = ah;
  313. *msg = m;
  314. return 0;
  315. }
  316. static void cm_free_msg(struct ib_mad_send_buf *msg)
  317. {
  318. ib_destroy_ah(msg->ah);
  319. if (msg->context[0])
  320. cm_deref_id(msg->context[0]);
  321. ib_free_send_mad(msg);
  322. }
  323. static void * cm_copy_private_data(const void *private_data,
  324. u8 private_data_len)
  325. {
  326. void *data;
  327. if (!private_data || !private_data_len)
  328. return NULL;
  329. data = kmemdup(private_data, private_data_len, GFP_KERNEL);
  330. if (!data)
  331. return ERR_PTR(-ENOMEM);
  332. return data;
  333. }
  334. static void cm_set_private_data(struct cm_id_private *cm_id_priv,
  335. void *private_data, u8 private_data_len)
  336. {
  337. if (cm_id_priv->private_data && cm_id_priv->private_data_len)
  338. kfree(cm_id_priv->private_data);
  339. cm_id_priv->private_data = private_data;
  340. cm_id_priv->private_data_len = private_data_len;
  341. }
  342. static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
  343. struct ib_grh *grh, struct cm_av *av)
  344. {
  345. av->port = port;
  346. av->pkey_index = wc->pkey_index;
  347. ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
  348. grh, &av->ah_attr);
  349. }
  350. static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av,
  351. struct cm_id_private *cm_id_priv)
  352. {
  353. struct cm_device *cm_dev;
  354. struct cm_port *port = NULL;
  355. unsigned long flags;
  356. int ret;
  357. u8 p;
  358. struct net_device *ndev = ib_get_ndev_from_path(path);
  359. read_lock_irqsave(&cm.device_lock, flags);
  360. list_for_each_entry(cm_dev, &cm.device_list, list) {
  361. if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
  362. path->gid_type, ndev, &p, NULL)) {
  363. port = cm_dev->port[p-1];
  364. break;
  365. }
  366. }
  367. read_unlock_irqrestore(&cm.device_lock, flags);
  368. if (ndev)
  369. dev_put(ndev);
  370. if (!port)
  371. return -EINVAL;
  372. ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
  373. be16_to_cpu(path->pkey), &av->pkey_index);
  374. if (ret)
  375. return ret;
  376. av->port = port;
  377. ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
  378. &av->ah_attr);
  379. av->timeout = path->packet_life_time + 1;
  380. spin_lock_irqsave(&cm.lock, flags);
  381. if (&cm_id_priv->av == av)
  382. list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list);
  383. else if (&cm_id_priv->alt_av == av)
  384. list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list);
  385. else
  386. ret = -EINVAL;
  387. spin_unlock_irqrestore(&cm.lock, flags);
  388. return ret;
  389. }
  390. static int cm_alloc_id(struct cm_id_private *cm_id_priv)
  391. {
  392. unsigned long flags;
  393. int id;
  394. idr_preload(GFP_KERNEL);
  395. spin_lock_irqsave(&cm.lock, flags);
  396. id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
  397. spin_unlock_irqrestore(&cm.lock, flags);
  398. idr_preload_end();
  399. cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
  400. return id < 0 ? id : 0;
  401. }
  402. static void cm_free_id(__be32 local_id)
  403. {
  404. spin_lock_irq(&cm.lock);
  405. idr_remove(&cm.local_id_table,
  406. (__force int) (local_id ^ cm.random_id_operand));
  407. spin_unlock_irq(&cm.lock);
  408. }
  409. static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
  410. {
  411. struct cm_id_private *cm_id_priv;
  412. cm_id_priv = idr_find(&cm.local_id_table,
  413. (__force int) (local_id ^ cm.random_id_operand));
  414. if (cm_id_priv) {
  415. if (cm_id_priv->id.remote_id == remote_id)
  416. atomic_inc(&cm_id_priv->refcount);
  417. else
  418. cm_id_priv = NULL;
  419. }
  420. return cm_id_priv;
  421. }
  422. static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
  423. {
  424. struct cm_id_private *cm_id_priv;
  425. spin_lock_irq(&cm.lock);
  426. cm_id_priv = cm_get_id(local_id, remote_id);
  427. spin_unlock_irq(&cm.lock);
  428. return cm_id_priv;
  429. }
  430. /*
  431. * Trivial helpers to strip endian annotation and compare; the
  432. * endianness doesn't actually matter since we just need a stable
  433. * order for the RB tree.
  434. */
  435. static int be32_lt(__be32 a, __be32 b)
  436. {
  437. return (__force u32) a < (__force u32) b;
  438. }
  439. static int be32_gt(__be32 a, __be32 b)
  440. {
  441. return (__force u32) a > (__force u32) b;
  442. }
  443. static int be64_lt(__be64 a, __be64 b)
  444. {
  445. return (__force u64) a < (__force u64) b;
  446. }
  447. static int be64_gt(__be64 a, __be64 b)
  448. {
  449. return (__force u64) a > (__force u64) b;
  450. }
  451. static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
  452. {
  453. struct rb_node **link = &cm.listen_service_table.rb_node;
  454. struct rb_node *parent = NULL;
  455. struct cm_id_private *cur_cm_id_priv;
  456. __be64 service_id = cm_id_priv->id.service_id;
  457. __be64 service_mask = cm_id_priv->id.service_mask;
  458. while (*link) {
  459. parent = *link;
  460. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  461. service_node);
  462. if ((cur_cm_id_priv->id.service_mask & service_id) ==
  463. (service_mask & cur_cm_id_priv->id.service_id) &&
  464. (cm_id_priv->id.device == cur_cm_id_priv->id.device))
  465. return cur_cm_id_priv;
  466. if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
  467. link = &(*link)->rb_left;
  468. else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
  469. link = &(*link)->rb_right;
  470. else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
  471. link = &(*link)->rb_left;
  472. else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
  473. link = &(*link)->rb_right;
  474. else
  475. link = &(*link)->rb_right;
  476. }
  477. rb_link_node(&cm_id_priv->service_node, parent, link);
  478. rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
  479. return NULL;
  480. }
  481. static struct cm_id_private * cm_find_listen(struct ib_device *device,
  482. __be64 service_id)
  483. {
  484. struct rb_node *node = cm.listen_service_table.rb_node;
  485. struct cm_id_private *cm_id_priv;
  486. while (node) {
  487. cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
  488. if ((cm_id_priv->id.service_mask & service_id) ==
  489. cm_id_priv->id.service_id &&
  490. (cm_id_priv->id.device == device))
  491. return cm_id_priv;
  492. if (device < cm_id_priv->id.device)
  493. node = node->rb_left;
  494. else if (device > cm_id_priv->id.device)
  495. node = node->rb_right;
  496. else if (be64_lt(service_id, cm_id_priv->id.service_id))
  497. node = node->rb_left;
  498. else if (be64_gt(service_id, cm_id_priv->id.service_id))
  499. node = node->rb_right;
  500. else
  501. node = node->rb_right;
  502. }
  503. return NULL;
  504. }
  505. static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
  506. *timewait_info)
  507. {
  508. struct rb_node **link = &cm.remote_id_table.rb_node;
  509. struct rb_node *parent = NULL;
  510. struct cm_timewait_info *cur_timewait_info;
  511. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  512. __be32 remote_id = timewait_info->work.remote_id;
  513. while (*link) {
  514. parent = *link;
  515. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  516. remote_id_node);
  517. if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
  518. link = &(*link)->rb_left;
  519. else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
  520. link = &(*link)->rb_right;
  521. else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  522. link = &(*link)->rb_left;
  523. else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  524. link = &(*link)->rb_right;
  525. else
  526. return cur_timewait_info;
  527. }
  528. timewait_info->inserted_remote_id = 1;
  529. rb_link_node(&timewait_info->remote_id_node, parent, link);
  530. rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
  531. return NULL;
  532. }
  533. static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
  534. __be32 remote_id)
  535. {
  536. struct rb_node *node = cm.remote_id_table.rb_node;
  537. struct cm_timewait_info *timewait_info;
  538. while (node) {
  539. timewait_info = rb_entry(node, struct cm_timewait_info,
  540. remote_id_node);
  541. if (be32_lt(remote_id, timewait_info->work.remote_id))
  542. node = node->rb_left;
  543. else if (be32_gt(remote_id, timewait_info->work.remote_id))
  544. node = node->rb_right;
  545. else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
  546. node = node->rb_left;
  547. else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
  548. node = node->rb_right;
  549. else
  550. return timewait_info;
  551. }
  552. return NULL;
  553. }
  554. static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
  555. *timewait_info)
  556. {
  557. struct rb_node **link = &cm.remote_qp_table.rb_node;
  558. struct rb_node *parent = NULL;
  559. struct cm_timewait_info *cur_timewait_info;
  560. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  561. __be32 remote_qpn = timewait_info->remote_qpn;
  562. while (*link) {
  563. parent = *link;
  564. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  565. remote_qp_node);
  566. if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
  567. link = &(*link)->rb_left;
  568. else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
  569. link = &(*link)->rb_right;
  570. else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  571. link = &(*link)->rb_left;
  572. else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  573. link = &(*link)->rb_right;
  574. else
  575. return cur_timewait_info;
  576. }
  577. timewait_info->inserted_remote_qp = 1;
  578. rb_link_node(&timewait_info->remote_qp_node, parent, link);
  579. rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  580. return NULL;
  581. }
  582. static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
  583. *cm_id_priv)
  584. {
  585. struct rb_node **link = &cm.remote_sidr_table.rb_node;
  586. struct rb_node *parent = NULL;
  587. struct cm_id_private *cur_cm_id_priv;
  588. union ib_gid *port_gid = &cm_id_priv->av.dgid;
  589. __be32 remote_id = cm_id_priv->id.remote_id;
  590. while (*link) {
  591. parent = *link;
  592. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  593. sidr_id_node);
  594. if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
  595. link = &(*link)->rb_left;
  596. else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
  597. link = &(*link)->rb_right;
  598. else {
  599. int cmp;
  600. cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
  601. sizeof *port_gid);
  602. if (cmp < 0)
  603. link = &(*link)->rb_left;
  604. else if (cmp > 0)
  605. link = &(*link)->rb_right;
  606. else
  607. return cur_cm_id_priv;
  608. }
  609. }
  610. rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
  611. rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  612. return NULL;
  613. }
  614. static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
  615. enum ib_cm_sidr_status status)
  616. {
  617. struct ib_cm_sidr_rep_param param;
  618. memset(&param, 0, sizeof param);
  619. param.status = status;
  620. ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
  621. }
  622. struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
  623. ib_cm_handler cm_handler,
  624. void *context)
  625. {
  626. struct cm_id_private *cm_id_priv;
  627. int ret;
  628. cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
  629. if (!cm_id_priv)
  630. return ERR_PTR(-ENOMEM);
  631. cm_id_priv->id.state = IB_CM_IDLE;
  632. cm_id_priv->id.device = device;
  633. cm_id_priv->id.cm_handler = cm_handler;
  634. cm_id_priv->id.context = context;
  635. cm_id_priv->id.remote_cm_qpn = 1;
  636. ret = cm_alloc_id(cm_id_priv);
  637. if (ret)
  638. goto error;
  639. spin_lock_init(&cm_id_priv->lock);
  640. init_completion(&cm_id_priv->comp);
  641. INIT_LIST_HEAD(&cm_id_priv->work_list);
  642. INIT_LIST_HEAD(&cm_id_priv->prim_list);
  643. INIT_LIST_HEAD(&cm_id_priv->altr_list);
  644. atomic_set(&cm_id_priv->work_count, -1);
  645. atomic_set(&cm_id_priv->refcount, 1);
  646. return &cm_id_priv->id;
  647. error:
  648. kfree(cm_id_priv);
  649. return ERR_PTR(-ENOMEM);
  650. }
  651. EXPORT_SYMBOL(ib_create_cm_id);
  652. static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
  653. {
  654. struct cm_work *work;
  655. if (list_empty(&cm_id_priv->work_list))
  656. return NULL;
  657. work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
  658. list_del(&work->list);
  659. return work;
  660. }
  661. static void cm_free_work(struct cm_work *work)
  662. {
  663. if (work->mad_recv_wc)
  664. ib_free_recv_mad(work->mad_recv_wc);
  665. kfree(work);
  666. }
  667. static inline int cm_convert_to_ms(int iba_time)
  668. {
  669. /* approximate conversion to ms from 4.096us x 2^iba_time */
  670. return 1 << max(iba_time - 8, 0);
  671. }
  672. /*
  673. * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
  674. * Because of how ack_timeout is stored, adding one doubles the timeout.
  675. * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
  676. * increment it (round up) only if the other is within 50%.
  677. */
  678. static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
  679. {
  680. int ack_timeout = packet_life_time + 1;
  681. if (ack_timeout >= ca_ack_delay)
  682. ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
  683. else
  684. ack_timeout = ca_ack_delay +
  685. (ack_timeout >= (ca_ack_delay - 1));
  686. return min(31, ack_timeout);
  687. }
  688. static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
  689. {
  690. if (timewait_info->inserted_remote_id) {
  691. rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
  692. timewait_info->inserted_remote_id = 0;
  693. }
  694. if (timewait_info->inserted_remote_qp) {
  695. rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  696. timewait_info->inserted_remote_qp = 0;
  697. }
  698. }
  699. static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
  700. {
  701. struct cm_timewait_info *timewait_info;
  702. timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
  703. if (!timewait_info)
  704. return ERR_PTR(-ENOMEM);
  705. timewait_info->work.local_id = local_id;
  706. INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
  707. timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
  708. return timewait_info;
  709. }
  710. static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
  711. {
  712. int wait_time;
  713. unsigned long flags;
  714. struct cm_device *cm_dev;
  715. cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
  716. if (!cm_dev)
  717. return;
  718. spin_lock_irqsave(&cm.lock, flags);
  719. cm_cleanup_timewait(cm_id_priv->timewait_info);
  720. list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
  721. spin_unlock_irqrestore(&cm.lock, flags);
  722. /*
  723. * The cm_id could be destroyed by the user before we exit timewait.
  724. * To protect against this, we search for the cm_id after exiting
  725. * timewait before notifying the user that we've exited timewait.
  726. */
  727. cm_id_priv->id.state = IB_CM_TIMEWAIT;
  728. wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
  729. /* Check if the device started its remove_one */
  730. spin_lock_irqsave(&cm.lock, flags);
  731. if (!cm_dev->going_down)
  732. queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
  733. msecs_to_jiffies(wait_time));
  734. spin_unlock_irqrestore(&cm.lock, flags);
  735. cm_id_priv->timewait_info = NULL;
  736. }
  737. static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
  738. {
  739. unsigned long flags;
  740. cm_id_priv->id.state = IB_CM_IDLE;
  741. if (cm_id_priv->timewait_info) {
  742. spin_lock_irqsave(&cm.lock, flags);
  743. cm_cleanup_timewait(cm_id_priv->timewait_info);
  744. spin_unlock_irqrestore(&cm.lock, flags);
  745. kfree(cm_id_priv->timewait_info);
  746. cm_id_priv->timewait_info = NULL;
  747. }
  748. }
  749. static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
  750. {
  751. struct cm_id_private *cm_id_priv;
  752. struct cm_work *work;
  753. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  754. retest:
  755. spin_lock_irq(&cm_id_priv->lock);
  756. switch (cm_id->state) {
  757. case IB_CM_LISTEN:
  758. spin_unlock_irq(&cm_id_priv->lock);
  759. spin_lock_irq(&cm.lock);
  760. if (--cm_id_priv->listen_sharecount > 0) {
  761. /* The id is still shared. */
  762. cm_deref_id(cm_id_priv);
  763. spin_unlock_irq(&cm.lock);
  764. return;
  765. }
  766. rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
  767. spin_unlock_irq(&cm.lock);
  768. break;
  769. case IB_CM_SIDR_REQ_SENT:
  770. cm_id->state = IB_CM_IDLE;
  771. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  772. spin_unlock_irq(&cm_id_priv->lock);
  773. break;
  774. case IB_CM_SIDR_REQ_RCVD:
  775. spin_unlock_irq(&cm_id_priv->lock);
  776. cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
  777. spin_lock_irq(&cm.lock);
  778. if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
  779. rb_erase(&cm_id_priv->sidr_id_node,
  780. &cm.remote_sidr_table);
  781. spin_unlock_irq(&cm.lock);
  782. break;
  783. case IB_CM_REQ_SENT:
  784. case IB_CM_MRA_REQ_RCVD:
  785. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  786. spin_unlock_irq(&cm_id_priv->lock);
  787. ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
  788. &cm_id_priv->id.device->node_guid,
  789. sizeof cm_id_priv->id.device->node_guid,
  790. NULL, 0);
  791. break;
  792. case IB_CM_REQ_RCVD:
  793. if (err == -ENOMEM) {
  794. /* Do not reject to allow future retries. */
  795. cm_reset_to_idle(cm_id_priv);
  796. spin_unlock_irq(&cm_id_priv->lock);
  797. } else {
  798. spin_unlock_irq(&cm_id_priv->lock);
  799. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
  800. NULL, 0, NULL, 0);
  801. }
  802. break;
  803. case IB_CM_REP_SENT:
  804. case IB_CM_MRA_REP_RCVD:
  805. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  806. /* Fall through */
  807. case IB_CM_MRA_REQ_SENT:
  808. case IB_CM_REP_RCVD:
  809. case IB_CM_MRA_REP_SENT:
  810. spin_unlock_irq(&cm_id_priv->lock);
  811. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
  812. NULL, 0, NULL, 0);
  813. break;
  814. case IB_CM_ESTABLISHED:
  815. spin_unlock_irq(&cm_id_priv->lock);
  816. if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
  817. break;
  818. ib_send_cm_dreq(cm_id, NULL, 0);
  819. goto retest;
  820. case IB_CM_DREQ_SENT:
  821. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  822. cm_enter_timewait(cm_id_priv);
  823. spin_unlock_irq(&cm_id_priv->lock);
  824. break;
  825. case IB_CM_DREQ_RCVD:
  826. spin_unlock_irq(&cm_id_priv->lock);
  827. ib_send_cm_drep(cm_id, NULL, 0);
  828. break;
  829. default:
  830. spin_unlock_irq(&cm_id_priv->lock);
  831. break;
  832. }
  833. spin_lock_irq(&cm.lock);
  834. if (!list_empty(&cm_id_priv->altr_list) &&
  835. (!cm_id_priv->altr_send_port_not_ready))
  836. list_del(&cm_id_priv->altr_list);
  837. if (!list_empty(&cm_id_priv->prim_list) &&
  838. (!cm_id_priv->prim_send_port_not_ready))
  839. list_del(&cm_id_priv->prim_list);
  840. spin_unlock_irq(&cm.lock);
  841. cm_free_id(cm_id->local_id);
  842. cm_deref_id(cm_id_priv);
  843. wait_for_completion(&cm_id_priv->comp);
  844. while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
  845. cm_free_work(work);
  846. kfree(cm_id_priv->private_data);
  847. kfree(cm_id_priv);
  848. }
  849. void ib_destroy_cm_id(struct ib_cm_id *cm_id)
  850. {
  851. cm_destroy_id(cm_id, 0);
  852. }
  853. EXPORT_SYMBOL(ib_destroy_cm_id);
  854. /**
  855. * __ib_cm_listen - Initiates listening on the specified service ID for
  856. * connection and service ID resolution requests.
  857. * @cm_id: Connection identifier associated with the listen request.
  858. * @service_id: Service identifier matched against incoming connection
  859. * and service ID resolution requests. The service ID should be specified
  860. * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
  861. * assign a service ID to the caller.
  862. * @service_mask: Mask applied to service ID used to listen across a
  863. * range of service IDs. If set to 0, the service ID is matched
  864. * exactly. This parameter is ignored if %service_id is set to
  865. * IB_CM_ASSIGN_SERVICE_ID.
  866. */
  867. static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
  868. __be64 service_mask)
  869. {
  870. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  871. int ret = 0;
  872. service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
  873. service_id &= service_mask;
  874. if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
  875. (service_id != IB_CM_ASSIGN_SERVICE_ID))
  876. return -EINVAL;
  877. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  878. if (cm_id->state != IB_CM_IDLE)
  879. return -EINVAL;
  880. cm_id->state = IB_CM_LISTEN;
  881. ++cm_id_priv->listen_sharecount;
  882. if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
  883. cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
  884. cm_id->service_mask = ~cpu_to_be64(0);
  885. } else {
  886. cm_id->service_id = service_id;
  887. cm_id->service_mask = service_mask;
  888. }
  889. cur_cm_id_priv = cm_insert_listen(cm_id_priv);
  890. if (cur_cm_id_priv) {
  891. cm_id->state = IB_CM_IDLE;
  892. --cm_id_priv->listen_sharecount;
  893. ret = -EBUSY;
  894. }
  895. return ret;
  896. }
  897. int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
  898. {
  899. unsigned long flags;
  900. int ret;
  901. spin_lock_irqsave(&cm.lock, flags);
  902. ret = __ib_cm_listen(cm_id, service_id, service_mask);
  903. spin_unlock_irqrestore(&cm.lock, flags);
  904. return ret;
  905. }
  906. EXPORT_SYMBOL(ib_cm_listen);
  907. /**
  908. * Create a new listening ib_cm_id and listen on the given service ID.
  909. *
  910. * If there's an existing ID listening on that same device and service ID,
  911. * return it.
  912. *
  913. * @device: Device associated with the cm_id. All related communication will
  914. * be associated with the specified device.
  915. * @cm_handler: Callback invoked to notify the user of CM events.
  916. * @service_id: Service identifier matched against incoming connection
  917. * and service ID resolution requests. The service ID should be specified
  918. * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
  919. * assign a service ID to the caller.
  920. *
  921. * Callers should call ib_destroy_cm_id when done with the listener ID.
  922. */
  923. struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
  924. ib_cm_handler cm_handler,
  925. __be64 service_id)
  926. {
  927. struct cm_id_private *cm_id_priv;
  928. struct ib_cm_id *cm_id;
  929. unsigned long flags;
  930. int err = 0;
  931. /* Create an ID in advance, since the creation may sleep */
  932. cm_id = ib_create_cm_id(device, cm_handler, NULL);
  933. if (IS_ERR(cm_id))
  934. return cm_id;
  935. spin_lock_irqsave(&cm.lock, flags);
  936. if (service_id == IB_CM_ASSIGN_SERVICE_ID)
  937. goto new_id;
  938. /* Find an existing ID */
  939. cm_id_priv = cm_find_listen(device, service_id);
  940. if (cm_id_priv) {
  941. if (cm_id->cm_handler != cm_handler || cm_id->context) {
  942. /* Sharing an ib_cm_id with different handlers is not
  943. * supported */
  944. spin_unlock_irqrestore(&cm.lock, flags);
  945. return ERR_PTR(-EINVAL);
  946. }
  947. atomic_inc(&cm_id_priv->refcount);
  948. ++cm_id_priv->listen_sharecount;
  949. spin_unlock_irqrestore(&cm.lock, flags);
  950. ib_destroy_cm_id(cm_id);
  951. cm_id = &cm_id_priv->id;
  952. return cm_id;
  953. }
  954. new_id:
  955. /* Use newly created ID */
  956. err = __ib_cm_listen(cm_id, service_id, 0);
  957. spin_unlock_irqrestore(&cm.lock, flags);
  958. if (err) {
  959. ib_destroy_cm_id(cm_id);
  960. return ERR_PTR(err);
  961. }
  962. return cm_id;
  963. }
  964. EXPORT_SYMBOL(ib_cm_insert_listen);
  965. static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
  966. enum cm_msg_sequence msg_seq)
  967. {
  968. u64 hi_tid, low_tid;
  969. hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
  970. low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
  971. (msg_seq << 30));
  972. return cpu_to_be64(hi_tid | low_tid);
  973. }
  974. static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
  975. __be16 attr_id, __be64 tid)
  976. {
  977. hdr->base_version = IB_MGMT_BASE_VERSION;
  978. hdr->mgmt_class = IB_MGMT_CLASS_CM;
  979. hdr->class_version = IB_CM_CLASS_VERSION;
  980. hdr->method = IB_MGMT_METHOD_SEND;
  981. hdr->attr_id = attr_id;
  982. hdr->tid = tid;
  983. }
  984. static void cm_format_req(struct cm_req_msg *req_msg,
  985. struct cm_id_private *cm_id_priv,
  986. struct ib_cm_req_param *param)
  987. {
  988. struct ib_sa_path_rec *pri_path = param->primary_path;
  989. struct ib_sa_path_rec *alt_path = param->alternate_path;
  990. cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
  991. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
  992. req_msg->local_comm_id = cm_id_priv->id.local_id;
  993. req_msg->service_id = param->service_id;
  994. req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
  995. cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
  996. cm_req_set_init_depth(req_msg, param->initiator_depth);
  997. cm_req_set_remote_resp_timeout(req_msg,
  998. param->remote_cm_response_timeout);
  999. cm_req_set_qp_type(req_msg, param->qp_type);
  1000. cm_req_set_flow_ctrl(req_msg, param->flow_control);
  1001. cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
  1002. cm_req_set_local_resp_timeout(req_msg,
  1003. param->local_cm_response_timeout);
  1004. req_msg->pkey = param->primary_path->pkey;
  1005. cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
  1006. cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
  1007. if (param->qp_type != IB_QPT_XRC_INI) {
  1008. cm_req_set_resp_res(req_msg, param->responder_resources);
  1009. cm_req_set_retry_count(req_msg, param->retry_count);
  1010. cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
  1011. cm_req_set_srq(req_msg, param->srq);
  1012. }
  1013. if (pri_path->hop_limit <= 1) {
  1014. req_msg->primary_local_lid = pri_path->slid;
  1015. req_msg->primary_remote_lid = pri_path->dlid;
  1016. } else {
  1017. /* Work-around until there's a way to obtain remote LID info */
  1018. req_msg->primary_local_lid = IB_LID_PERMISSIVE;
  1019. req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
  1020. }
  1021. req_msg->primary_local_gid = pri_path->sgid;
  1022. req_msg->primary_remote_gid = pri_path->dgid;
  1023. cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
  1024. cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
  1025. req_msg->primary_traffic_class = pri_path->traffic_class;
  1026. req_msg->primary_hop_limit = pri_path->hop_limit;
  1027. cm_req_set_primary_sl(req_msg, pri_path->sl);
  1028. cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
  1029. cm_req_set_primary_local_ack_timeout(req_msg,
  1030. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  1031. pri_path->packet_life_time));
  1032. if (alt_path) {
  1033. if (alt_path->hop_limit <= 1) {
  1034. req_msg->alt_local_lid = alt_path->slid;
  1035. req_msg->alt_remote_lid = alt_path->dlid;
  1036. } else {
  1037. req_msg->alt_local_lid = IB_LID_PERMISSIVE;
  1038. req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
  1039. }
  1040. req_msg->alt_local_gid = alt_path->sgid;
  1041. req_msg->alt_remote_gid = alt_path->dgid;
  1042. cm_req_set_alt_flow_label(req_msg,
  1043. alt_path->flow_label);
  1044. cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
  1045. req_msg->alt_traffic_class = alt_path->traffic_class;
  1046. req_msg->alt_hop_limit = alt_path->hop_limit;
  1047. cm_req_set_alt_sl(req_msg, alt_path->sl);
  1048. cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
  1049. cm_req_set_alt_local_ack_timeout(req_msg,
  1050. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  1051. alt_path->packet_life_time));
  1052. }
  1053. if (param->private_data && param->private_data_len)
  1054. memcpy(req_msg->private_data, param->private_data,
  1055. param->private_data_len);
  1056. }
  1057. static int cm_validate_req_param(struct ib_cm_req_param *param)
  1058. {
  1059. /* peer-to-peer not supported */
  1060. if (param->peer_to_peer)
  1061. return -EINVAL;
  1062. if (!param->primary_path)
  1063. return -EINVAL;
  1064. if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
  1065. param->qp_type != IB_QPT_XRC_INI)
  1066. return -EINVAL;
  1067. if (param->private_data &&
  1068. param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
  1069. return -EINVAL;
  1070. if (param->alternate_path &&
  1071. (param->alternate_path->pkey != param->primary_path->pkey ||
  1072. param->alternate_path->mtu != param->primary_path->mtu))
  1073. return -EINVAL;
  1074. return 0;
  1075. }
  1076. int ib_send_cm_req(struct ib_cm_id *cm_id,
  1077. struct ib_cm_req_param *param)
  1078. {
  1079. struct cm_id_private *cm_id_priv;
  1080. struct cm_req_msg *req_msg;
  1081. unsigned long flags;
  1082. int ret;
  1083. ret = cm_validate_req_param(param);
  1084. if (ret)
  1085. return ret;
  1086. /* Verify that we're not in timewait. */
  1087. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1088. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1089. if (cm_id->state != IB_CM_IDLE) {
  1090. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1091. ret = -EINVAL;
  1092. goto out;
  1093. }
  1094. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1095. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  1096. id.local_id);
  1097. if (IS_ERR(cm_id_priv->timewait_info)) {
  1098. ret = PTR_ERR(cm_id_priv->timewait_info);
  1099. goto out;
  1100. }
  1101. ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av,
  1102. cm_id_priv);
  1103. if (ret)
  1104. goto error1;
  1105. if (param->alternate_path) {
  1106. ret = cm_init_av_by_path(param->alternate_path,
  1107. &cm_id_priv->alt_av, cm_id_priv);
  1108. if (ret)
  1109. goto error1;
  1110. }
  1111. cm_id->service_id = param->service_id;
  1112. cm_id->service_mask = ~cpu_to_be64(0);
  1113. cm_id_priv->timeout_ms = cm_convert_to_ms(
  1114. param->primary_path->packet_life_time) * 2 +
  1115. cm_convert_to_ms(
  1116. param->remote_cm_response_timeout);
  1117. cm_id_priv->max_cm_retries = param->max_cm_retries;
  1118. cm_id_priv->initiator_depth = param->initiator_depth;
  1119. cm_id_priv->responder_resources = param->responder_resources;
  1120. cm_id_priv->retry_count = param->retry_count;
  1121. cm_id_priv->path_mtu = param->primary_path->mtu;
  1122. cm_id_priv->pkey = param->primary_path->pkey;
  1123. cm_id_priv->qp_type = param->qp_type;
  1124. ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
  1125. if (ret)
  1126. goto error1;
  1127. req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
  1128. cm_format_req(req_msg, cm_id_priv, param);
  1129. cm_id_priv->tid = req_msg->hdr.tid;
  1130. cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
  1131. cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
  1132. cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
  1133. cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
  1134. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1135. ret = ib_post_send_mad(cm_id_priv->msg, NULL);
  1136. if (ret) {
  1137. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1138. goto error2;
  1139. }
  1140. BUG_ON(cm_id->state != IB_CM_IDLE);
  1141. cm_id->state = IB_CM_REQ_SENT;
  1142. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1143. return 0;
  1144. error2: cm_free_msg(cm_id_priv->msg);
  1145. error1: kfree(cm_id_priv->timewait_info);
  1146. out: return ret;
  1147. }
  1148. EXPORT_SYMBOL(ib_send_cm_req);
  1149. static int cm_issue_rej(struct cm_port *port,
  1150. struct ib_mad_recv_wc *mad_recv_wc,
  1151. enum ib_cm_rej_reason reason,
  1152. enum cm_msg_response msg_rejected,
  1153. void *ari, u8 ari_length)
  1154. {
  1155. struct ib_mad_send_buf *msg = NULL;
  1156. struct cm_rej_msg *rej_msg, *rcv_msg;
  1157. int ret;
  1158. ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
  1159. if (ret)
  1160. return ret;
  1161. /* We just need common CM header information. Cast to any message. */
  1162. rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
  1163. rej_msg = (struct cm_rej_msg *) msg->mad;
  1164. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
  1165. rej_msg->remote_comm_id = rcv_msg->local_comm_id;
  1166. rej_msg->local_comm_id = rcv_msg->remote_comm_id;
  1167. cm_rej_set_msg_rejected(rej_msg, msg_rejected);
  1168. rej_msg->reason = cpu_to_be16(reason);
  1169. if (ari && ari_length) {
  1170. cm_rej_set_reject_info_len(rej_msg, ari_length);
  1171. memcpy(rej_msg->ari, ari, ari_length);
  1172. }
  1173. ret = ib_post_send_mad(msg, NULL);
  1174. if (ret)
  1175. cm_free_msg(msg);
  1176. return ret;
  1177. }
  1178. static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
  1179. __be32 local_qpn, __be32 remote_qpn)
  1180. {
  1181. return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
  1182. ((local_ca_guid == remote_ca_guid) &&
  1183. (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
  1184. }
  1185. static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
  1186. struct ib_sa_path_rec *primary_path,
  1187. struct ib_sa_path_rec *alt_path)
  1188. {
  1189. memset(primary_path, 0, sizeof *primary_path);
  1190. primary_path->dgid = req_msg->primary_local_gid;
  1191. primary_path->sgid = req_msg->primary_remote_gid;
  1192. primary_path->dlid = req_msg->primary_local_lid;
  1193. primary_path->slid = req_msg->primary_remote_lid;
  1194. primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
  1195. primary_path->hop_limit = req_msg->primary_hop_limit;
  1196. primary_path->traffic_class = req_msg->primary_traffic_class;
  1197. primary_path->reversible = 1;
  1198. primary_path->pkey = req_msg->pkey;
  1199. primary_path->sl = cm_req_get_primary_sl(req_msg);
  1200. primary_path->mtu_selector = IB_SA_EQ;
  1201. primary_path->mtu = cm_req_get_path_mtu(req_msg);
  1202. primary_path->rate_selector = IB_SA_EQ;
  1203. primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
  1204. primary_path->packet_life_time_selector = IB_SA_EQ;
  1205. primary_path->packet_life_time =
  1206. cm_req_get_primary_local_ack_timeout(req_msg);
  1207. primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
  1208. primary_path->service_id = req_msg->service_id;
  1209. if (req_msg->alt_local_lid) {
  1210. memset(alt_path, 0, sizeof *alt_path);
  1211. alt_path->dgid = req_msg->alt_local_gid;
  1212. alt_path->sgid = req_msg->alt_remote_gid;
  1213. alt_path->dlid = req_msg->alt_local_lid;
  1214. alt_path->slid = req_msg->alt_remote_lid;
  1215. alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
  1216. alt_path->hop_limit = req_msg->alt_hop_limit;
  1217. alt_path->traffic_class = req_msg->alt_traffic_class;
  1218. alt_path->reversible = 1;
  1219. alt_path->pkey = req_msg->pkey;
  1220. alt_path->sl = cm_req_get_alt_sl(req_msg);
  1221. alt_path->mtu_selector = IB_SA_EQ;
  1222. alt_path->mtu = cm_req_get_path_mtu(req_msg);
  1223. alt_path->rate_selector = IB_SA_EQ;
  1224. alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
  1225. alt_path->packet_life_time_selector = IB_SA_EQ;
  1226. alt_path->packet_life_time =
  1227. cm_req_get_alt_local_ack_timeout(req_msg);
  1228. alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
  1229. alt_path->service_id = req_msg->service_id;
  1230. }
  1231. }
  1232. static u16 cm_get_bth_pkey(struct cm_work *work)
  1233. {
  1234. struct ib_device *ib_dev = work->port->cm_dev->ib_device;
  1235. u8 port_num = work->port->port_num;
  1236. u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
  1237. u16 pkey;
  1238. int ret;
  1239. ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
  1240. if (ret) {
  1241. dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
  1242. port_num, pkey_index, ret);
  1243. return 0;
  1244. }
  1245. return pkey;
  1246. }
  1247. static void cm_format_req_event(struct cm_work *work,
  1248. struct cm_id_private *cm_id_priv,
  1249. struct ib_cm_id *listen_id)
  1250. {
  1251. struct cm_req_msg *req_msg;
  1252. struct ib_cm_req_event_param *param;
  1253. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1254. param = &work->cm_event.param.req_rcvd;
  1255. param->listen_id = listen_id;
  1256. param->bth_pkey = cm_get_bth_pkey(work);
  1257. param->port = cm_id_priv->av.port->port_num;
  1258. param->primary_path = &work->path[0];
  1259. if (req_msg->alt_local_lid)
  1260. param->alternate_path = &work->path[1];
  1261. else
  1262. param->alternate_path = NULL;
  1263. param->remote_ca_guid = req_msg->local_ca_guid;
  1264. param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
  1265. param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
  1266. param->qp_type = cm_req_get_qp_type(req_msg);
  1267. param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
  1268. param->responder_resources = cm_req_get_init_depth(req_msg);
  1269. param->initiator_depth = cm_req_get_resp_res(req_msg);
  1270. param->local_cm_response_timeout =
  1271. cm_req_get_remote_resp_timeout(req_msg);
  1272. param->flow_control = cm_req_get_flow_ctrl(req_msg);
  1273. param->remote_cm_response_timeout =
  1274. cm_req_get_local_resp_timeout(req_msg);
  1275. param->retry_count = cm_req_get_retry_count(req_msg);
  1276. param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  1277. param->srq = cm_req_get_srq(req_msg);
  1278. work->cm_event.private_data = &req_msg->private_data;
  1279. }
  1280. static void cm_process_work(struct cm_id_private *cm_id_priv,
  1281. struct cm_work *work)
  1282. {
  1283. int ret;
  1284. /* We will typically only have the current event to report. */
  1285. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
  1286. cm_free_work(work);
  1287. while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
  1288. spin_lock_irq(&cm_id_priv->lock);
  1289. work = cm_dequeue_work(cm_id_priv);
  1290. spin_unlock_irq(&cm_id_priv->lock);
  1291. BUG_ON(!work);
  1292. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
  1293. &work->cm_event);
  1294. cm_free_work(work);
  1295. }
  1296. cm_deref_id(cm_id_priv);
  1297. if (ret)
  1298. cm_destroy_id(&cm_id_priv->id, ret);
  1299. }
  1300. static void cm_format_mra(struct cm_mra_msg *mra_msg,
  1301. struct cm_id_private *cm_id_priv,
  1302. enum cm_msg_response msg_mraed, u8 service_timeout,
  1303. const void *private_data, u8 private_data_len)
  1304. {
  1305. cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
  1306. cm_mra_set_msg_mraed(mra_msg, msg_mraed);
  1307. mra_msg->local_comm_id = cm_id_priv->id.local_id;
  1308. mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1309. cm_mra_set_service_timeout(mra_msg, service_timeout);
  1310. if (private_data && private_data_len)
  1311. memcpy(mra_msg->private_data, private_data, private_data_len);
  1312. }
  1313. static void cm_format_rej(struct cm_rej_msg *rej_msg,
  1314. struct cm_id_private *cm_id_priv,
  1315. enum ib_cm_rej_reason reason,
  1316. void *ari,
  1317. u8 ari_length,
  1318. const void *private_data,
  1319. u8 private_data_len)
  1320. {
  1321. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
  1322. rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1323. switch(cm_id_priv->id.state) {
  1324. case IB_CM_REQ_RCVD:
  1325. rej_msg->local_comm_id = 0;
  1326. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1327. break;
  1328. case IB_CM_MRA_REQ_SENT:
  1329. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1330. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1331. break;
  1332. case IB_CM_REP_RCVD:
  1333. case IB_CM_MRA_REP_SENT:
  1334. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1335. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
  1336. break;
  1337. default:
  1338. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1339. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
  1340. break;
  1341. }
  1342. rej_msg->reason = cpu_to_be16(reason);
  1343. if (ari && ari_length) {
  1344. cm_rej_set_reject_info_len(rej_msg, ari_length);
  1345. memcpy(rej_msg->ari, ari, ari_length);
  1346. }
  1347. if (private_data && private_data_len)
  1348. memcpy(rej_msg->private_data, private_data, private_data_len);
  1349. }
  1350. static void cm_dup_req_handler(struct cm_work *work,
  1351. struct cm_id_private *cm_id_priv)
  1352. {
  1353. struct ib_mad_send_buf *msg = NULL;
  1354. int ret;
  1355. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1356. counter[CM_REQ_COUNTER]);
  1357. /* Quick state check to discard duplicate REQs. */
  1358. if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
  1359. return;
  1360. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1361. if (ret)
  1362. return;
  1363. spin_lock_irq(&cm_id_priv->lock);
  1364. switch (cm_id_priv->id.state) {
  1365. case IB_CM_MRA_REQ_SENT:
  1366. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1367. CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
  1368. cm_id_priv->private_data,
  1369. cm_id_priv->private_data_len);
  1370. break;
  1371. case IB_CM_TIMEWAIT:
  1372. cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
  1373. IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
  1374. break;
  1375. default:
  1376. goto unlock;
  1377. }
  1378. spin_unlock_irq(&cm_id_priv->lock);
  1379. ret = ib_post_send_mad(msg, NULL);
  1380. if (ret)
  1381. goto free;
  1382. return;
  1383. unlock: spin_unlock_irq(&cm_id_priv->lock);
  1384. free: cm_free_msg(msg);
  1385. }
  1386. static struct cm_id_private * cm_match_req(struct cm_work *work,
  1387. struct cm_id_private *cm_id_priv)
  1388. {
  1389. struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
  1390. struct cm_timewait_info *timewait_info;
  1391. struct cm_req_msg *req_msg;
  1392. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1393. /* Check for possible duplicate REQ. */
  1394. spin_lock_irq(&cm.lock);
  1395. timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
  1396. if (timewait_info) {
  1397. cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
  1398. timewait_info->work.remote_id);
  1399. spin_unlock_irq(&cm.lock);
  1400. if (cur_cm_id_priv) {
  1401. cm_dup_req_handler(work, cur_cm_id_priv);
  1402. cm_deref_id(cur_cm_id_priv);
  1403. }
  1404. return NULL;
  1405. }
  1406. /* Check for stale connections. */
  1407. timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
  1408. if (timewait_info) {
  1409. cm_cleanup_timewait(cm_id_priv->timewait_info);
  1410. spin_unlock_irq(&cm.lock);
  1411. cm_issue_rej(work->port, work->mad_recv_wc,
  1412. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
  1413. NULL, 0);
  1414. return NULL;
  1415. }
  1416. /* Find matching listen request. */
  1417. listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
  1418. req_msg->service_id);
  1419. if (!listen_cm_id_priv) {
  1420. cm_cleanup_timewait(cm_id_priv->timewait_info);
  1421. spin_unlock_irq(&cm.lock);
  1422. cm_issue_rej(work->port, work->mad_recv_wc,
  1423. IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
  1424. NULL, 0);
  1425. goto out;
  1426. }
  1427. atomic_inc(&listen_cm_id_priv->refcount);
  1428. atomic_inc(&cm_id_priv->refcount);
  1429. cm_id_priv->id.state = IB_CM_REQ_RCVD;
  1430. atomic_inc(&cm_id_priv->work_count);
  1431. spin_unlock_irq(&cm.lock);
  1432. out:
  1433. return listen_cm_id_priv;
  1434. }
  1435. /*
  1436. * Work-around for inter-subnet connections. If the LIDs are permissive,
  1437. * we need to override the LID/SL data in the REQ with the LID information
  1438. * in the work completion.
  1439. */
  1440. static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
  1441. {
  1442. if (!cm_req_get_primary_subnet_local(req_msg)) {
  1443. if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
  1444. req_msg->primary_local_lid = cpu_to_be16(wc->slid);
  1445. cm_req_set_primary_sl(req_msg, wc->sl);
  1446. }
  1447. if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
  1448. req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
  1449. }
  1450. if (!cm_req_get_alt_subnet_local(req_msg)) {
  1451. if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
  1452. req_msg->alt_local_lid = cpu_to_be16(wc->slid);
  1453. cm_req_set_alt_sl(req_msg, wc->sl);
  1454. }
  1455. if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
  1456. req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
  1457. }
  1458. }
  1459. static int cm_req_handler(struct cm_work *work)
  1460. {
  1461. struct ib_cm_id *cm_id;
  1462. struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
  1463. struct cm_req_msg *req_msg;
  1464. union ib_gid gid;
  1465. struct ib_gid_attr gid_attr;
  1466. int ret;
  1467. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1468. cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
  1469. if (IS_ERR(cm_id))
  1470. return PTR_ERR(cm_id);
  1471. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1472. cm_id_priv->id.remote_id = req_msg->local_comm_id;
  1473. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  1474. work->mad_recv_wc->recv_buf.grh,
  1475. &cm_id_priv->av);
  1476. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  1477. id.local_id);
  1478. if (IS_ERR(cm_id_priv->timewait_info)) {
  1479. ret = PTR_ERR(cm_id_priv->timewait_info);
  1480. goto destroy;
  1481. }
  1482. cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
  1483. cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
  1484. cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
  1485. listen_cm_id_priv = cm_match_req(work, cm_id_priv);
  1486. if (!listen_cm_id_priv) {
  1487. ret = -EINVAL;
  1488. kfree(cm_id_priv->timewait_info);
  1489. goto destroy;
  1490. }
  1491. cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
  1492. cm_id_priv->id.context = listen_cm_id_priv->id.context;
  1493. cm_id_priv->id.service_id = req_msg->service_id;
  1494. cm_id_priv->id.service_mask = ~cpu_to_be64(0);
  1495. cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
  1496. cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
  1497. memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
  1498. work->path[0].hop_limit = cm_id_priv->av.ah_attr.grh.hop_limit;
  1499. ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
  1500. work->port->port_num,
  1501. cm_id_priv->av.ah_attr.grh.sgid_index,
  1502. &gid, &gid_attr);
  1503. if (!ret) {
  1504. if (gid_attr.ndev) {
  1505. work->path[0].ifindex = gid_attr.ndev->ifindex;
  1506. work->path[0].net = dev_net(gid_attr.ndev);
  1507. dev_put(gid_attr.ndev);
  1508. }
  1509. work->path[0].gid_type = gid_attr.gid_type;
  1510. ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av,
  1511. cm_id_priv);
  1512. }
  1513. if (ret) {
  1514. int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
  1515. work->port->port_num, 0,
  1516. &work->path[0].sgid,
  1517. &gid_attr);
  1518. if (!err && gid_attr.ndev) {
  1519. work->path[0].ifindex = gid_attr.ndev->ifindex;
  1520. work->path[0].net = dev_net(gid_attr.ndev);
  1521. dev_put(gid_attr.ndev);
  1522. }
  1523. work->path[0].gid_type = gid_attr.gid_type;
  1524. ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
  1525. &work->path[0].sgid, sizeof work->path[0].sgid,
  1526. NULL, 0);
  1527. goto rejected;
  1528. }
  1529. if (req_msg->alt_local_lid) {
  1530. ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av,
  1531. cm_id_priv);
  1532. if (ret) {
  1533. ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
  1534. &work->path[0].sgid,
  1535. sizeof work->path[0].sgid, NULL, 0);
  1536. goto rejected;
  1537. }
  1538. }
  1539. cm_id_priv->tid = req_msg->hdr.tid;
  1540. cm_id_priv->timeout_ms = cm_convert_to_ms(
  1541. cm_req_get_local_resp_timeout(req_msg));
  1542. cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
  1543. cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
  1544. cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
  1545. cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
  1546. cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
  1547. cm_id_priv->pkey = req_msg->pkey;
  1548. cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
  1549. cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
  1550. cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  1551. cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
  1552. cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
  1553. cm_process_work(cm_id_priv, work);
  1554. cm_deref_id(listen_cm_id_priv);
  1555. return 0;
  1556. rejected:
  1557. atomic_dec(&cm_id_priv->refcount);
  1558. cm_deref_id(listen_cm_id_priv);
  1559. destroy:
  1560. ib_destroy_cm_id(cm_id);
  1561. return ret;
  1562. }
  1563. static void cm_format_rep(struct cm_rep_msg *rep_msg,
  1564. struct cm_id_private *cm_id_priv,
  1565. struct ib_cm_rep_param *param)
  1566. {
  1567. cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
  1568. rep_msg->local_comm_id = cm_id_priv->id.local_id;
  1569. rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1570. cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
  1571. rep_msg->resp_resources = param->responder_resources;
  1572. cm_rep_set_target_ack_delay(rep_msg,
  1573. cm_id_priv->av.port->cm_dev->ack_delay);
  1574. cm_rep_set_failover(rep_msg, param->failover_accepted);
  1575. cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
  1576. rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
  1577. if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
  1578. rep_msg->initiator_depth = param->initiator_depth;
  1579. cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
  1580. cm_rep_set_srq(rep_msg, param->srq);
  1581. cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
  1582. } else {
  1583. cm_rep_set_srq(rep_msg, 1);
  1584. cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
  1585. }
  1586. if (param->private_data && param->private_data_len)
  1587. memcpy(rep_msg->private_data, param->private_data,
  1588. param->private_data_len);
  1589. }
  1590. int ib_send_cm_rep(struct ib_cm_id *cm_id,
  1591. struct ib_cm_rep_param *param)
  1592. {
  1593. struct cm_id_private *cm_id_priv;
  1594. struct ib_mad_send_buf *msg;
  1595. struct cm_rep_msg *rep_msg;
  1596. unsigned long flags;
  1597. int ret;
  1598. if (param->private_data &&
  1599. param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
  1600. return -EINVAL;
  1601. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1602. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1603. if (cm_id->state != IB_CM_REQ_RCVD &&
  1604. cm_id->state != IB_CM_MRA_REQ_SENT) {
  1605. ret = -EINVAL;
  1606. goto out;
  1607. }
  1608. ret = cm_alloc_msg(cm_id_priv, &msg);
  1609. if (ret)
  1610. goto out;
  1611. rep_msg = (struct cm_rep_msg *) msg->mad;
  1612. cm_format_rep(rep_msg, cm_id_priv, param);
  1613. msg->timeout_ms = cm_id_priv->timeout_ms;
  1614. msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
  1615. ret = ib_post_send_mad(msg, NULL);
  1616. if (ret) {
  1617. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1618. cm_free_msg(msg);
  1619. return ret;
  1620. }
  1621. cm_id->state = IB_CM_REP_SENT;
  1622. cm_id_priv->msg = msg;
  1623. cm_id_priv->initiator_depth = param->initiator_depth;
  1624. cm_id_priv->responder_resources = param->responder_resources;
  1625. cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
  1626. cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
  1627. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1628. return ret;
  1629. }
  1630. EXPORT_SYMBOL(ib_send_cm_rep);
  1631. static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
  1632. struct cm_id_private *cm_id_priv,
  1633. const void *private_data,
  1634. u8 private_data_len)
  1635. {
  1636. cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
  1637. rtu_msg->local_comm_id = cm_id_priv->id.local_id;
  1638. rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1639. if (private_data && private_data_len)
  1640. memcpy(rtu_msg->private_data, private_data, private_data_len);
  1641. }
  1642. int ib_send_cm_rtu(struct ib_cm_id *cm_id,
  1643. const void *private_data,
  1644. u8 private_data_len)
  1645. {
  1646. struct cm_id_private *cm_id_priv;
  1647. struct ib_mad_send_buf *msg;
  1648. unsigned long flags;
  1649. void *data;
  1650. int ret;
  1651. if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
  1652. return -EINVAL;
  1653. data = cm_copy_private_data(private_data, private_data_len);
  1654. if (IS_ERR(data))
  1655. return PTR_ERR(data);
  1656. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1657. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1658. if (cm_id->state != IB_CM_REP_RCVD &&
  1659. cm_id->state != IB_CM_MRA_REP_SENT) {
  1660. ret = -EINVAL;
  1661. goto error;
  1662. }
  1663. ret = cm_alloc_msg(cm_id_priv, &msg);
  1664. if (ret)
  1665. goto error;
  1666. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1667. private_data, private_data_len);
  1668. ret = ib_post_send_mad(msg, NULL);
  1669. if (ret) {
  1670. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1671. cm_free_msg(msg);
  1672. kfree(data);
  1673. return ret;
  1674. }
  1675. cm_id->state = IB_CM_ESTABLISHED;
  1676. cm_set_private_data(cm_id_priv, data, private_data_len);
  1677. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1678. return 0;
  1679. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1680. kfree(data);
  1681. return ret;
  1682. }
  1683. EXPORT_SYMBOL(ib_send_cm_rtu);
  1684. static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
  1685. {
  1686. struct cm_rep_msg *rep_msg;
  1687. struct ib_cm_rep_event_param *param;
  1688. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1689. param = &work->cm_event.param.rep_rcvd;
  1690. param->remote_ca_guid = rep_msg->local_ca_guid;
  1691. param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
  1692. param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
  1693. param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
  1694. param->responder_resources = rep_msg->initiator_depth;
  1695. param->initiator_depth = rep_msg->resp_resources;
  1696. param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
  1697. param->failover_accepted = cm_rep_get_failover(rep_msg);
  1698. param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
  1699. param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1700. param->srq = cm_rep_get_srq(rep_msg);
  1701. work->cm_event.private_data = &rep_msg->private_data;
  1702. }
  1703. static void cm_dup_rep_handler(struct cm_work *work)
  1704. {
  1705. struct cm_id_private *cm_id_priv;
  1706. struct cm_rep_msg *rep_msg;
  1707. struct ib_mad_send_buf *msg = NULL;
  1708. int ret;
  1709. rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
  1710. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
  1711. rep_msg->local_comm_id);
  1712. if (!cm_id_priv)
  1713. return;
  1714. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1715. counter[CM_REP_COUNTER]);
  1716. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1717. if (ret)
  1718. goto deref;
  1719. spin_lock_irq(&cm_id_priv->lock);
  1720. if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
  1721. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1722. cm_id_priv->private_data,
  1723. cm_id_priv->private_data_len);
  1724. else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
  1725. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1726. CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
  1727. cm_id_priv->private_data,
  1728. cm_id_priv->private_data_len);
  1729. else
  1730. goto unlock;
  1731. spin_unlock_irq(&cm_id_priv->lock);
  1732. ret = ib_post_send_mad(msg, NULL);
  1733. if (ret)
  1734. goto free;
  1735. goto deref;
  1736. unlock: spin_unlock_irq(&cm_id_priv->lock);
  1737. free: cm_free_msg(msg);
  1738. deref: cm_deref_id(cm_id_priv);
  1739. }
  1740. static int cm_rep_handler(struct cm_work *work)
  1741. {
  1742. struct cm_id_private *cm_id_priv;
  1743. struct cm_rep_msg *rep_msg;
  1744. int ret;
  1745. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1746. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
  1747. if (!cm_id_priv) {
  1748. cm_dup_rep_handler(work);
  1749. return -EINVAL;
  1750. }
  1751. cm_format_rep_event(work, cm_id_priv->qp_type);
  1752. spin_lock_irq(&cm_id_priv->lock);
  1753. switch (cm_id_priv->id.state) {
  1754. case IB_CM_REQ_SENT:
  1755. case IB_CM_MRA_REQ_RCVD:
  1756. break;
  1757. default:
  1758. spin_unlock_irq(&cm_id_priv->lock);
  1759. ret = -EINVAL;
  1760. goto error;
  1761. }
  1762. cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
  1763. cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
  1764. cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
  1765. spin_lock(&cm.lock);
  1766. /* Check for duplicate REP. */
  1767. if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
  1768. spin_unlock(&cm.lock);
  1769. spin_unlock_irq(&cm_id_priv->lock);
  1770. ret = -EINVAL;
  1771. goto error;
  1772. }
  1773. /* Check for a stale connection. */
  1774. if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
  1775. rb_erase(&cm_id_priv->timewait_info->remote_id_node,
  1776. &cm.remote_id_table);
  1777. cm_id_priv->timewait_info->inserted_remote_id = 0;
  1778. spin_unlock(&cm.lock);
  1779. spin_unlock_irq(&cm_id_priv->lock);
  1780. cm_issue_rej(work->port, work->mad_recv_wc,
  1781. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
  1782. NULL, 0);
  1783. ret = -EINVAL;
  1784. goto error;
  1785. }
  1786. spin_unlock(&cm.lock);
  1787. cm_id_priv->id.state = IB_CM_REP_RCVD;
  1788. cm_id_priv->id.remote_id = rep_msg->local_comm_id;
  1789. cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
  1790. cm_id_priv->initiator_depth = rep_msg->resp_resources;
  1791. cm_id_priv->responder_resources = rep_msg->initiator_depth;
  1792. cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
  1793. cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1794. cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
  1795. cm_id_priv->av.timeout =
  1796. cm_ack_timeout(cm_id_priv->target_ack_delay,
  1797. cm_id_priv->av.timeout - 1);
  1798. cm_id_priv->alt_av.timeout =
  1799. cm_ack_timeout(cm_id_priv->target_ack_delay,
  1800. cm_id_priv->alt_av.timeout - 1);
  1801. /* todo: handle peer_to_peer */
  1802. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1803. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1804. if (!ret)
  1805. list_add_tail(&work->list, &cm_id_priv->work_list);
  1806. spin_unlock_irq(&cm_id_priv->lock);
  1807. if (ret)
  1808. cm_process_work(cm_id_priv, work);
  1809. else
  1810. cm_deref_id(cm_id_priv);
  1811. return 0;
  1812. error:
  1813. cm_deref_id(cm_id_priv);
  1814. return ret;
  1815. }
  1816. static int cm_establish_handler(struct cm_work *work)
  1817. {
  1818. struct cm_id_private *cm_id_priv;
  1819. int ret;
  1820. /* See comment in cm_establish about lookup. */
  1821. cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
  1822. if (!cm_id_priv)
  1823. return -EINVAL;
  1824. spin_lock_irq(&cm_id_priv->lock);
  1825. if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
  1826. spin_unlock_irq(&cm_id_priv->lock);
  1827. goto out;
  1828. }
  1829. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1830. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1831. if (!ret)
  1832. list_add_tail(&work->list, &cm_id_priv->work_list);
  1833. spin_unlock_irq(&cm_id_priv->lock);
  1834. if (ret)
  1835. cm_process_work(cm_id_priv, work);
  1836. else
  1837. cm_deref_id(cm_id_priv);
  1838. return 0;
  1839. out:
  1840. cm_deref_id(cm_id_priv);
  1841. return -EINVAL;
  1842. }
  1843. static int cm_rtu_handler(struct cm_work *work)
  1844. {
  1845. struct cm_id_private *cm_id_priv;
  1846. struct cm_rtu_msg *rtu_msg;
  1847. int ret;
  1848. rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
  1849. cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
  1850. rtu_msg->local_comm_id);
  1851. if (!cm_id_priv)
  1852. return -EINVAL;
  1853. work->cm_event.private_data = &rtu_msg->private_data;
  1854. spin_lock_irq(&cm_id_priv->lock);
  1855. if (cm_id_priv->id.state != IB_CM_REP_SENT &&
  1856. cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
  1857. spin_unlock_irq(&cm_id_priv->lock);
  1858. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1859. counter[CM_RTU_COUNTER]);
  1860. goto out;
  1861. }
  1862. cm_id_priv->id.state = IB_CM_ESTABLISHED;
  1863. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1864. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1865. if (!ret)
  1866. list_add_tail(&work->list, &cm_id_priv->work_list);
  1867. spin_unlock_irq(&cm_id_priv->lock);
  1868. if (ret)
  1869. cm_process_work(cm_id_priv, work);
  1870. else
  1871. cm_deref_id(cm_id_priv);
  1872. return 0;
  1873. out:
  1874. cm_deref_id(cm_id_priv);
  1875. return -EINVAL;
  1876. }
  1877. static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
  1878. struct cm_id_private *cm_id_priv,
  1879. const void *private_data,
  1880. u8 private_data_len)
  1881. {
  1882. cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
  1883. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
  1884. dreq_msg->local_comm_id = cm_id_priv->id.local_id;
  1885. dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1886. cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
  1887. if (private_data && private_data_len)
  1888. memcpy(dreq_msg->private_data, private_data, private_data_len);
  1889. }
  1890. int ib_send_cm_dreq(struct ib_cm_id *cm_id,
  1891. const void *private_data,
  1892. u8 private_data_len)
  1893. {
  1894. struct cm_id_private *cm_id_priv;
  1895. struct ib_mad_send_buf *msg;
  1896. unsigned long flags;
  1897. int ret;
  1898. if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
  1899. return -EINVAL;
  1900. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1901. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1902. if (cm_id->state != IB_CM_ESTABLISHED) {
  1903. ret = -EINVAL;
  1904. goto out;
  1905. }
  1906. if (cm_id->lap_state == IB_CM_LAP_SENT ||
  1907. cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
  1908. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1909. ret = cm_alloc_msg(cm_id_priv, &msg);
  1910. if (ret) {
  1911. cm_enter_timewait(cm_id_priv);
  1912. goto out;
  1913. }
  1914. cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
  1915. private_data, private_data_len);
  1916. msg->timeout_ms = cm_id_priv->timeout_ms;
  1917. msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
  1918. ret = ib_post_send_mad(msg, NULL);
  1919. if (ret) {
  1920. cm_enter_timewait(cm_id_priv);
  1921. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1922. cm_free_msg(msg);
  1923. return ret;
  1924. }
  1925. cm_id->state = IB_CM_DREQ_SENT;
  1926. cm_id_priv->msg = msg;
  1927. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1928. return ret;
  1929. }
  1930. EXPORT_SYMBOL(ib_send_cm_dreq);
  1931. static void cm_format_drep(struct cm_drep_msg *drep_msg,
  1932. struct cm_id_private *cm_id_priv,
  1933. const void *private_data,
  1934. u8 private_data_len)
  1935. {
  1936. cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
  1937. drep_msg->local_comm_id = cm_id_priv->id.local_id;
  1938. drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1939. if (private_data && private_data_len)
  1940. memcpy(drep_msg->private_data, private_data, private_data_len);
  1941. }
  1942. int ib_send_cm_drep(struct ib_cm_id *cm_id,
  1943. const void *private_data,
  1944. u8 private_data_len)
  1945. {
  1946. struct cm_id_private *cm_id_priv;
  1947. struct ib_mad_send_buf *msg;
  1948. unsigned long flags;
  1949. void *data;
  1950. int ret;
  1951. if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
  1952. return -EINVAL;
  1953. data = cm_copy_private_data(private_data, private_data_len);
  1954. if (IS_ERR(data))
  1955. return PTR_ERR(data);
  1956. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1957. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1958. if (cm_id->state != IB_CM_DREQ_RCVD) {
  1959. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1960. kfree(data);
  1961. return -EINVAL;
  1962. }
  1963. cm_set_private_data(cm_id_priv, data, private_data_len);
  1964. cm_enter_timewait(cm_id_priv);
  1965. ret = cm_alloc_msg(cm_id_priv, &msg);
  1966. if (ret)
  1967. goto out;
  1968. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  1969. private_data, private_data_len);
  1970. ret = ib_post_send_mad(msg, NULL);
  1971. if (ret) {
  1972. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1973. cm_free_msg(msg);
  1974. return ret;
  1975. }
  1976. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1977. return ret;
  1978. }
  1979. EXPORT_SYMBOL(ib_send_cm_drep);
  1980. static int cm_issue_drep(struct cm_port *port,
  1981. struct ib_mad_recv_wc *mad_recv_wc)
  1982. {
  1983. struct ib_mad_send_buf *msg = NULL;
  1984. struct cm_dreq_msg *dreq_msg;
  1985. struct cm_drep_msg *drep_msg;
  1986. int ret;
  1987. ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
  1988. if (ret)
  1989. return ret;
  1990. dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
  1991. drep_msg = (struct cm_drep_msg *) msg->mad;
  1992. cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
  1993. drep_msg->remote_comm_id = dreq_msg->local_comm_id;
  1994. drep_msg->local_comm_id = dreq_msg->remote_comm_id;
  1995. ret = ib_post_send_mad(msg, NULL);
  1996. if (ret)
  1997. cm_free_msg(msg);
  1998. return ret;
  1999. }
  2000. static int cm_dreq_handler(struct cm_work *work)
  2001. {
  2002. struct cm_id_private *cm_id_priv;
  2003. struct cm_dreq_msg *dreq_msg;
  2004. struct ib_mad_send_buf *msg = NULL;
  2005. int ret;
  2006. dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
  2007. cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
  2008. dreq_msg->local_comm_id);
  2009. if (!cm_id_priv) {
  2010. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2011. counter[CM_DREQ_COUNTER]);
  2012. cm_issue_drep(work->port, work->mad_recv_wc);
  2013. return -EINVAL;
  2014. }
  2015. work->cm_event.private_data = &dreq_msg->private_data;
  2016. spin_lock_irq(&cm_id_priv->lock);
  2017. if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
  2018. goto unlock;
  2019. switch (cm_id_priv->id.state) {
  2020. case IB_CM_REP_SENT:
  2021. case IB_CM_DREQ_SENT:
  2022. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2023. break;
  2024. case IB_CM_ESTABLISHED:
  2025. if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
  2026. cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
  2027. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2028. break;
  2029. case IB_CM_MRA_REP_RCVD:
  2030. break;
  2031. case IB_CM_TIMEWAIT:
  2032. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2033. counter[CM_DREQ_COUNTER]);
  2034. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  2035. goto unlock;
  2036. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  2037. cm_id_priv->private_data,
  2038. cm_id_priv->private_data_len);
  2039. spin_unlock_irq(&cm_id_priv->lock);
  2040. if (ib_post_send_mad(msg, NULL))
  2041. cm_free_msg(msg);
  2042. goto deref;
  2043. case IB_CM_DREQ_RCVD:
  2044. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2045. counter[CM_DREQ_COUNTER]);
  2046. goto unlock;
  2047. default:
  2048. goto unlock;
  2049. }
  2050. cm_id_priv->id.state = IB_CM_DREQ_RCVD;
  2051. cm_id_priv->tid = dreq_msg->hdr.tid;
  2052. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2053. if (!ret)
  2054. list_add_tail(&work->list, &cm_id_priv->work_list);
  2055. spin_unlock_irq(&cm_id_priv->lock);
  2056. if (ret)
  2057. cm_process_work(cm_id_priv, work);
  2058. else
  2059. cm_deref_id(cm_id_priv);
  2060. return 0;
  2061. unlock: spin_unlock_irq(&cm_id_priv->lock);
  2062. deref: cm_deref_id(cm_id_priv);
  2063. return -EINVAL;
  2064. }
  2065. static int cm_drep_handler(struct cm_work *work)
  2066. {
  2067. struct cm_id_private *cm_id_priv;
  2068. struct cm_drep_msg *drep_msg;
  2069. int ret;
  2070. drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
  2071. cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
  2072. drep_msg->local_comm_id);
  2073. if (!cm_id_priv)
  2074. return -EINVAL;
  2075. work->cm_event.private_data = &drep_msg->private_data;
  2076. spin_lock_irq(&cm_id_priv->lock);
  2077. if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
  2078. cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
  2079. spin_unlock_irq(&cm_id_priv->lock);
  2080. goto out;
  2081. }
  2082. cm_enter_timewait(cm_id_priv);
  2083. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2084. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2085. if (!ret)
  2086. list_add_tail(&work->list, &cm_id_priv->work_list);
  2087. spin_unlock_irq(&cm_id_priv->lock);
  2088. if (ret)
  2089. cm_process_work(cm_id_priv, work);
  2090. else
  2091. cm_deref_id(cm_id_priv);
  2092. return 0;
  2093. out:
  2094. cm_deref_id(cm_id_priv);
  2095. return -EINVAL;
  2096. }
  2097. int ib_send_cm_rej(struct ib_cm_id *cm_id,
  2098. enum ib_cm_rej_reason reason,
  2099. void *ari,
  2100. u8 ari_length,
  2101. const void *private_data,
  2102. u8 private_data_len)
  2103. {
  2104. struct cm_id_private *cm_id_priv;
  2105. struct ib_mad_send_buf *msg;
  2106. unsigned long flags;
  2107. int ret;
  2108. if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
  2109. (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
  2110. return -EINVAL;
  2111. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2112. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2113. switch (cm_id->state) {
  2114. case IB_CM_REQ_SENT:
  2115. case IB_CM_MRA_REQ_RCVD:
  2116. case IB_CM_REQ_RCVD:
  2117. case IB_CM_MRA_REQ_SENT:
  2118. case IB_CM_REP_RCVD:
  2119. case IB_CM_MRA_REP_SENT:
  2120. ret = cm_alloc_msg(cm_id_priv, &msg);
  2121. if (!ret)
  2122. cm_format_rej((struct cm_rej_msg *) msg->mad,
  2123. cm_id_priv, reason, ari, ari_length,
  2124. private_data, private_data_len);
  2125. cm_reset_to_idle(cm_id_priv);
  2126. break;
  2127. case IB_CM_REP_SENT:
  2128. case IB_CM_MRA_REP_RCVD:
  2129. ret = cm_alloc_msg(cm_id_priv, &msg);
  2130. if (!ret)
  2131. cm_format_rej((struct cm_rej_msg *) msg->mad,
  2132. cm_id_priv, reason, ari, ari_length,
  2133. private_data, private_data_len);
  2134. cm_enter_timewait(cm_id_priv);
  2135. break;
  2136. default:
  2137. ret = -EINVAL;
  2138. goto out;
  2139. }
  2140. if (ret)
  2141. goto out;
  2142. ret = ib_post_send_mad(msg, NULL);
  2143. if (ret)
  2144. cm_free_msg(msg);
  2145. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2146. return ret;
  2147. }
  2148. EXPORT_SYMBOL(ib_send_cm_rej);
  2149. static void cm_format_rej_event(struct cm_work *work)
  2150. {
  2151. struct cm_rej_msg *rej_msg;
  2152. struct ib_cm_rej_event_param *param;
  2153. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  2154. param = &work->cm_event.param.rej_rcvd;
  2155. param->ari = rej_msg->ari;
  2156. param->ari_length = cm_rej_get_reject_info_len(rej_msg);
  2157. param->reason = __be16_to_cpu(rej_msg->reason);
  2158. work->cm_event.private_data = &rej_msg->private_data;
  2159. }
  2160. static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
  2161. {
  2162. struct cm_timewait_info *timewait_info;
  2163. struct cm_id_private *cm_id_priv;
  2164. __be32 remote_id;
  2165. remote_id = rej_msg->local_comm_id;
  2166. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
  2167. spin_lock_irq(&cm.lock);
  2168. timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
  2169. remote_id);
  2170. if (!timewait_info) {
  2171. spin_unlock_irq(&cm.lock);
  2172. return NULL;
  2173. }
  2174. cm_id_priv = idr_find(&cm.local_id_table, (__force int)
  2175. (timewait_info->work.local_id ^
  2176. cm.random_id_operand));
  2177. if (cm_id_priv) {
  2178. if (cm_id_priv->id.remote_id == remote_id)
  2179. atomic_inc(&cm_id_priv->refcount);
  2180. else
  2181. cm_id_priv = NULL;
  2182. }
  2183. spin_unlock_irq(&cm.lock);
  2184. } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
  2185. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
  2186. else
  2187. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
  2188. return cm_id_priv;
  2189. }
  2190. static int cm_rej_handler(struct cm_work *work)
  2191. {
  2192. struct cm_id_private *cm_id_priv;
  2193. struct cm_rej_msg *rej_msg;
  2194. int ret;
  2195. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  2196. cm_id_priv = cm_acquire_rejected_id(rej_msg);
  2197. if (!cm_id_priv)
  2198. return -EINVAL;
  2199. cm_format_rej_event(work);
  2200. spin_lock_irq(&cm_id_priv->lock);
  2201. switch (cm_id_priv->id.state) {
  2202. case IB_CM_REQ_SENT:
  2203. case IB_CM_MRA_REQ_RCVD:
  2204. case IB_CM_REP_SENT:
  2205. case IB_CM_MRA_REP_RCVD:
  2206. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2207. /* fall through */
  2208. case IB_CM_REQ_RCVD:
  2209. case IB_CM_MRA_REQ_SENT:
  2210. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
  2211. cm_enter_timewait(cm_id_priv);
  2212. else
  2213. cm_reset_to_idle(cm_id_priv);
  2214. break;
  2215. case IB_CM_DREQ_SENT:
  2216. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2217. /* fall through */
  2218. case IB_CM_REP_RCVD:
  2219. case IB_CM_MRA_REP_SENT:
  2220. cm_enter_timewait(cm_id_priv);
  2221. break;
  2222. case IB_CM_ESTABLISHED:
  2223. if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
  2224. cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
  2225. if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
  2226. ib_cancel_mad(cm_id_priv->av.port->mad_agent,
  2227. cm_id_priv->msg);
  2228. cm_enter_timewait(cm_id_priv);
  2229. break;
  2230. }
  2231. /* fall through */
  2232. default:
  2233. spin_unlock_irq(&cm_id_priv->lock);
  2234. ret = -EINVAL;
  2235. goto out;
  2236. }
  2237. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2238. if (!ret)
  2239. list_add_tail(&work->list, &cm_id_priv->work_list);
  2240. spin_unlock_irq(&cm_id_priv->lock);
  2241. if (ret)
  2242. cm_process_work(cm_id_priv, work);
  2243. else
  2244. cm_deref_id(cm_id_priv);
  2245. return 0;
  2246. out:
  2247. cm_deref_id(cm_id_priv);
  2248. return -EINVAL;
  2249. }
  2250. int ib_send_cm_mra(struct ib_cm_id *cm_id,
  2251. u8 service_timeout,
  2252. const void *private_data,
  2253. u8 private_data_len)
  2254. {
  2255. struct cm_id_private *cm_id_priv;
  2256. struct ib_mad_send_buf *msg;
  2257. enum ib_cm_state cm_state;
  2258. enum ib_cm_lap_state lap_state;
  2259. enum cm_msg_response msg_response;
  2260. void *data;
  2261. unsigned long flags;
  2262. int ret;
  2263. if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
  2264. return -EINVAL;
  2265. data = cm_copy_private_data(private_data, private_data_len);
  2266. if (IS_ERR(data))
  2267. return PTR_ERR(data);
  2268. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2269. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2270. switch(cm_id_priv->id.state) {
  2271. case IB_CM_REQ_RCVD:
  2272. cm_state = IB_CM_MRA_REQ_SENT;
  2273. lap_state = cm_id->lap_state;
  2274. msg_response = CM_MSG_RESPONSE_REQ;
  2275. break;
  2276. case IB_CM_REP_RCVD:
  2277. cm_state = IB_CM_MRA_REP_SENT;
  2278. lap_state = cm_id->lap_state;
  2279. msg_response = CM_MSG_RESPONSE_REP;
  2280. break;
  2281. case IB_CM_ESTABLISHED:
  2282. if (cm_id->lap_state == IB_CM_LAP_RCVD) {
  2283. cm_state = cm_id->state;
  2284. lap_state = IB_CM_MRA_LAP_SENT;
  2285. msg_response = CM_MSG_RESPONSE_OTHER;
  2286. break;
  2287. }
  2288. default:
  2289. ret = -EINVAL;
  2290. goto error1;
  2291. }
  2292. if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
  2293. ret = cm_alloc_msg(cm_id_priv, &msg);
  2294. if (ret)
  2295. goto error1;
  2296. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  2297. msg_response, service_timeout,
  2298. private_data, private_data_len);
  2299. ret = ib_post_send_mad(msg, NULL);
  2300. if (ret)
  2301. goto error2;
  2302. }
  2303. cm_id->state = cm_state;
  2304. cm_id->lap_state = lap_state;
  2305. cm_id_priv->service_timeout = service_timeout;
  2306. cm_set_private_data(cm_id_priv, data, private_data_len);
  2307. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2308. return 0;
  2309. error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2310. kfree(data);
  2311. return ret;
  2312. error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2313. kfree(data);
  2314. cm_free_msg(msg);
  2315. return ret;
  2316. }
  2317. EXPORT_SYMBOL(ib_send_cm_mra);
  2318. static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
  2319. {
  2320. switch (cm_mra_get_msg_mraed(mra_msg)) {
  2321. case CM_MSG_RESPONSE_REQ:
  2322. return cm_acquire_id(mra_msg->remote_comm_id, 0);
  2323. case CM_MSG_RESPONSE_REP:
  2324. case CM_MSG_RESPONSE_OTHER:
  2325. return cm_acquire_id(mra_msg->remote_comm_id,
  2326. mra_msg->local_comm_id);
  2327. default:
  2328. return NULL;
  2329. }
  2330. }
  2331. static int cm_mra_handler(struct cm_work *work)
  2332. {
  2333. struct cm_id_private *cm_id_priv;
  2334. struct cm_mra_msg *mra_msg;
  2335. int timeout, ret;
  2336. mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
  2337. cm_id_priv = cm_acquire_mraed_id(mra_msg);
  2338. if (!cm_id_priv)
  2339. return -EINVAL;
  2340. work->cm_event.private_data = &mra_msg->private_data;
  2341. work->cm_event.param.mra_rcvd.service_timeout =
  2342. cm_mra_get_service_timeout(mra_msg);
  2343. timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
  2344. cm_convert_to_ms(cm_id_priv->av.timeout);
  2345. spin_lock_irq(&cm_id_priv->lock);
  2346. switch (cm_id_priv->id.state) {
  2347. case IB_CM_REQ_SENT:
  2348. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
  2349. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2350. cm_id_priv->msg, timeout))
  2351. goto out;
  2352. cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
  2353. break;
  2354. case IB_CM_REP_SENT:
  2355. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
  2356. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2357. cm_id_priv->msg, timeout))
  2358. goto out;
  2359. cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
  2360. break;
  2361. case IB_CM_ESTABLISHED:
  2362. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
  2363. cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
  2364. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2365. cm_id_priv->msg, timeout)) {
  2366. if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
  2367. atomic_long_inc(&work->port->
  2368. counter_group[CM_RECV_DUPLICATES].
  2369. counter[CM_MRA_COUNTER]);
  2370. goto out;
  2371. }
  2372. cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
  2373. break;
  2374. case IB_CM_MRA_REQ_RCVD:
  2375. case IB_CM_MRA_REP_RCVD:
  2376. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2377. counter[CM_MRA_COUNTER]);
  2378. /* fall through */
  2379. default:
  2380. goto out;
  2381. }
  2382. cm_id_priv->msg->context[1] = (void *) (unsigned long)
  2383. cm_id_priv->id.state;
  2384. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2385. if (!ret)
  2386. list_add_tail(&work->list, &cm_id_priv->work_list);
  2387. spin_unlock_irq(&cm_id_priv->lock);
  2388. if (ret)
  2389. cm_process_work(cm_id_priv, work);
  2390. else
  2391. cm_deref_id(cm_id_priv);
  2392. return 0;
  2393. out:
  2394. spin_unlock_irq(&cm_id_priv->lock);
  2395. cm_deref_id(cm_id_priv);
  2396. return -EINVAL;
  2397. }
  2398. static void cm_format_lap(struct cm_lap_msg *lap_msg,
  2399. struct cm_id_private *cm_id_priv,
  2400. struct ib_sa_path_rec *alternate_path,
  2401. const void *private_data,
  2402. u8 private_data_len)
  2403. {
  2404. cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
  2405. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
  2406. lap_msg->local_comm_id = cm_id_priv->id.local_id;
  2407. lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
  2408. cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
  2409. /* todo: need remote CM response timeout */
  2410. cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
  2411. lap_msg->alt_local_lid = alternate_path->slid;
  2412. lap_msg->alt_remote_lid = alternate_path->dlid;
  2413. lap_msg->alt_local_gid = alternate_path->sgid;
  2414. lap_msg->alt_remote_gid = alternate_path->dgid;
  2415. cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
  2416. cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
  2417. lap_msg->alt_hop_limit = alternate_path->hop_limit;
  2418. cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
  2419. cm_lap_set_sl(lap_msg, alternate_path->sl);
  2420. cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
  2421. cm_lap_set_local_ack_timeout(lap_msg,
  2422. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  2423. alternate_path->packet_life_time));
  2424. if (private_data && private_data_len)
  2425. memcpy(lap_msg->private_data, private_data, private_data_len);
  2426. }
  2427. int ib_send_cm_lap(struct ib_cm_id *cm_id,
  2428. struct ib_sa_path_rec *alternate_path,
  2429. const void *private_data,
  2430. u8 private_data_len)
  2431. {
  2432. struct cm_id_private *cm_id_priv;
  2433. struct ib_mad_send_buf *msg;
  2434. unsigned long flags;
  2435. int ret;
  2436. if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
  2437. return -EINVAL;
  2438. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2439. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2440. if (cm_id->state != IB_CM_ESTABLISHED ||
  2441. (cm_id->lap_state != IB_CM_LAP_UNINIT &&
  2442. cm_id->lap_state != IB_CM_LAP_IDLE)) {
  2443. ret = -EINVAL;
  2444. goto out;
  2445. }
  2446. ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av,
  2447. cm_id_priv);
  2448. if (ret)
  2449. goto out;
  2450. cm_id_priv->alt_av.timeout =
  2451. cm_ack_timeout(cm_id_priv->target_ack_delay,
  2452. cm_id_priv->alt_av.timeout - 1);
  2453. ret = cm_alloc_msg(cm_id_priv, &msg);
  2454. if (ret)
  2455. goto out;
  2456. cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
  2457. alternate_path, private_data, private_data_len);
  2458. msg->timeout_ms = cm_id_priv->timeout_ms;
  2459. msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
  2460. ret = ib_post_send_mad(msg, NULL);
  2461. if (ret) {
  2462. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2463. cm_free_msg(msg);
  2464. return ret;
  2465. }
  2466. cm_id->lap_state = IB_CM_LAP_SENT;
  2467. cm_id_priv->msg = msg;
  2468. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2469. return ret;
  2470. }
  2471. EXPORT_SYMBOL(ib_send_cm_lap);
  2472. static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
  2473. struct ib_sa_path_rec *path,
  2474. struct cm_lap_msg *lap_msg)
  2475. {
  2476. memset(path, 0, sizeof *path);
  2477. path->dgid = lap_msg->alt_local_gid;
  2478. path->sgid = lap_msg->alt_remote_gid;
  2479. path->dlid = lap_msg->alt_local_lid;
  2480. path->slid = lap_msg->alt_remote_lid;
  2481. path->flow_label = cm_lap_get_flow_label(lap_msg);
  2482. path->hop_limit = lap_msg->alt_hop_limit;
  2483. path->traffic_class = cm_lap_get_traffic_class(lap_msg);
  2484. path->reversible = 1;
  2485. path->pkey = cm_id_priv->pkey;
  2486. path->sl = cm_lap_get_sl(lap_msg);
  2487. path->mtu_selector = IB_SA_EQ;
  2488. path->mtu = cm_id_priv->path_mtu;
  2489. path->rate_selector = IB_SA_EQ;
  2490. path->rate = cm_lap_get_packet_rate(lap_msg);
  2491. path->packet_life_time_selector = IB_SA_EQ;
  2492. path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
  2493. path->packet_life_time -= (path->packet_life_time > 0);
  2494. }
  2495. static int cm_lap_handler(struct cm_work *work)
  2496. {
  2497. struct cm_id_private *cm_id_priv;
  2498. struct cm_lap_msg *lap_msg;
  2499. struct ib_cm_lap_event_param *param;
  2500. struct ib_mad_send_buf *msg = NULL;
  2501. int ret;
  2502. /* todo: verify LAP request and send reject APR if invalid. */
  2503. lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
  2504. cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
  2505. lap_msg->local_comm_id);
  2506. if (!cm_id_priv)
  2507. return -EINVAL;
  2508. param = &work->cm_event.param.lap_rcvd;
  2509. param->alternate_path = &work->path[0];
  2510. cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
  2511. work->cm_event.private_data = &lap_msg->private_data;
  2512. spin_lock_irq(&cm_id_priv->lock);
  2513. if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
  2514. goto unlock;
  2515. switch (cm_id_priv->id.lap_state) {
  2516. case IB_CM_LAP_UNINIT:
  2517. case IB_CM_LAP_IDLE:
  2518. break;
  2519. case IB_CM_MRA_LAP_SENT:
  2520. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2521. counter[CM_LAP_COUNTER]);
  2522. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  2523. goto unlock;
  2524. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  2525. CM_MSG_RESPONSE_OTHER,
  2526. cm_id_priv->service_timeout,
  2527. cm_id_priv->private_data,
  2528. cm_id_priv->private_data_len);
  2529. spin_unlock_irq(&cm_id_priv->lock);
  2530. if (ib_post_send_mad(msg, NULL))
  2531. cm_free_msg(msg);
  2532. goto deref;
  2533. case IB_CM_LAP_RCVD:
  2534. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2535. counter[CM_LAP_COUNTER]);
  2536. goto unlock;
  2537. default:
  2538. goto unlock;
  2539. }
  2540. cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
  2541. cm_id_priv->tid = lap_msg->hdr.tid;
  2542. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  2543. work->mad_recv_wc->recv_buf.grh,
  2544. &cm_id_priv->av);
  2545. cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av,
  2546. cm_id_priv);
  2547. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2548. if (!ret)
  2549. list_add_tail(&work->list, &cm_id_priv->work_list);
  2550. spin_unlock_irq(&cm_id_priv->lock);
  2551. if (ret)
  2552. cm_process_work(cm_id_priv, work);
  2553. else
  2554. cm_deref_id(cm_id_priv);
  2555. return 0;
  2556. unlock: spin_unlock_irq(&cm_id_priv->lock);
  2557. deref: cm_deref_id(cm_id_priv);
  2558. return -EINVAL;
  2559. }
  2560. static void cm_format_apr(struct cm_apr_msg *apr_msg,
  2561. struct cm_id_private *cm_id_priv,
  2562. enum ib_cm_apr_status status,
  2563. void *info,
  2564. u8 info_length,
  2565. const void *private_data,
  2566. u8 private_data_len)
  2567. {
  2568. cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
  2569. apr_msg->local_comm_id = cm_id_priv->id.local_id;
  2570. apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
  2571. apr_msg->ap_status = (u8) status;
  2572. if (info && info_length) {
  2573. apr_msg->info_length = info_length;
  2574. memcpy(apr_msg->info, info, info_length);
  2575. }
  2576. if (private_data && private_data_len)
  2577. memcpy(apr_msg->private_data, private_data, private_data_len);
  2578. }
  2579. int ib_send_cm_apr(struct ib_cm_id *cm_id,
  2580. enum ib_cm_apr_status status,
  2581. void *info,
  2582. u8 info_length,
  2583. const void *private_data,
  2584. u8 private_data_len)
  2585. {
  2586. struct cm_id_private *cm_id_priv;
  2587. struct ib_mad_send_buf *msg;
  2588. unsigned long flags;
  2589. int ret;
  2590. if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
  2591. (info && info_length > IB_CM_APR_INFO_LENGTH))
  2592. return -EINVAL;
  2593. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2594. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2595. if (cm_id->state != IB_CM_ESTABLISHED ||
  2596. (cm_id->lap_state != IB_CM_LAP_RCVD &&
  2597. cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
  2598. ret = -EINVAL;
  2599. goto out;
  2600. }
  2601. ret = cm_alloc_msg(cm_id_priv, &msg);
  2602. if (ret)
  2603. goto out;
  2604. cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
  2605. info, info_length, private_data, private_data_len);
  2606. ret = ib_post_send_mad(msg, NULL);
  2607. if (ret) {
  2608. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2609. cm_free_msg(msg);
  2610. return ret;
  2611. }
  2612. cm_id->lap_state = IB_CM_LAP_IDLE;
  2613. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2614. return ret;
  2615. }
  2616. EXPORT_SYMBOL(ib_send_cm_apr);
  2617. static int cm_apr_handler(struct cm_work *work)
  2618. {
  2619. struct cm_id_private *cm_id_priv;
  2620. struct cm_apr_msg *apr_msg;
  2621. int ret;
  2622. apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
  2623. cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
  2624. apr_msg->local_comm_id);
  2625. if (!cm_id_priv)
  2626. return -EINVAL; /* Unmatched reply. */
  2627. work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
  2628. work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
  2629. work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
  2630. work->cm_event.private_data = &apr_msg->private_data;
  2631. spin_lock_irq(&cm_id_priv->lock);
  2632. if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
  2633. (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
  2634. cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
  2635. spin_unlock_irq(&cm_id_priv->lock);
  2636. goto out;
  2637. }
  2638. cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
  2639. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2640. cm_id_priv->msg = NULL;
  2641. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2642. if (!ret)
  2643. list_add_tail(&work->list, &cm_id_priv->work_list);
  2644. spin_unlock_irq(&cm_id_priv->lock);
  2645. if (ret)
  2646. cm_process_work(cm_id_priv, work);
  2647. else
  2648. cm_deref_id(cm_id_priv);
  2649. return 0;
  2650. out:
  2651. cm_deref_id(cm_id_priv);
  2652. return -EINVAL;
  2653. }
  2654. static int cm_timewait_handler(struct cm_work *work)
  2655. {
  2656. struct cm_timewait_info *timewait_info;
  2657. struct cm_id_private *cm_id_priv;
  2658. int ret;
  2659. timewait_info = (struct cm_timewait_info *)work;
  2660. spin_lock_irq(&cm.lock);
  2661. list_del(&timewait_info->list);
  2662. spin_unlock_irq(&cm.lock);
  2663. cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
  2664. timewait_info->work.remote_id);
  2665. if (!cm_id_priv)
  2666. return -EINVAL;
  2667. spin_lock_irq(&cm_id_priv->lock);
  2668. if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
  2669. cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
  2670. spin_unlock_irq(&cm_id_priv->lock);
  2671. goto out;
  2672. }
  2673. cm_id_priv->id.state = IB_CM_IDLE;
  2674. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2675. if (!ret)
  2676. list_add_tail(&work->list, &cm_id_priv->work_list);
  2677. spin_unlock_irq(&cm_id_priv->lock);
  2678. if (ret)
  2679. cm_process_work(cm_id_priv, work);
  2680. else
  2681. cm_deref_id(cm_id_priv);
  2682. return 0;
  2683. out:
  2684. cm_deref_id(cm_id_priv);
  2685. return -EINVAL;
  2686. }
  2687. static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
  2688. struct cm_id_private *cm_id_priv,
  2689. struct ib_cm_sidr_req_param *param)
  2690. {
  2691. cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
  2692. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
  2693. sidr_req_msg->request_id = cm_id_priv->id.local_id;
  2694. sidr_req_msg->pkey = param->path->pkey;
  2695. sidr_req_msg->service_id = param->service_id;
  2696. if (param->private_data && param->private_data_len)
  2697. memcpy(sidr_req_msg->private_data, param->private_data,
  2698. param->private_data_len);
  2699. }
  2700. int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
  2701. struct ib_cm_sidr_req_param *param)
  2702. {
  2703. struct cm_id_private *cm_id_priv;
  2704. struct ib_mad_send_buf *msg;
  2705. unsigned long flags;
  2706. int ret;
  2707. if (!param->path || (param->private_data &&
  2708. param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
  2709. return -EINVAL;
  2710. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2711. ret = cm_init_av_by_path(param->path, &cm_id_priv->av, cm_id_priv);
  2712. if (ret)
  2713. goto out;
  2714. cm_id->service_id = param->service_id;
  2715. cm_id->service_mask = ~cpu_to_be64(0);
  2716. cm_id_priv->timeout_ms = param->timeout_ms;
  2717. cm_id_priv->max_cm_retries = param->max_cm_retries;
  2718. ret = cm_alloc_msg(cm_id_priv, &msg);
  2719. if (ret)
  2720. goto out;
  2721. cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
  2722. param);
  2723. msg->timeout_ms = cm_id_priv->timeout_ms;
  2724. msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
  2725. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2726. if (cm_id->state == IB_CM_IDLE)
  2727. ret = ib_post_send_mad(msg, NULL);
  2728. else
  2729. ret = -EINVAL;
  2730. if (ret) {
  2731. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2732. cm_free_msg(msg);
  2733. goto out;
  2734. }
  2735. cm_id->state = IB_CM_SIDR_REQ_SENT;
  2736. cm_id_priv->msg = msg;
  2737. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2738. out:
  2739. return ret;
  2740. }
  2741. EXPORT_SYMBOL(ib_send_cm_sidr_req);
  2742. static void cm_format_sidr_req_event(struct cm_work *work,
  2743. struct ib_cm_id *listen_id)
  2744. {
  2745. struct cm_sidr_req_msg *sidr_req_msg;
  2746. struct ib_cm_sidr_req_event_param *param;
  2747. sidr_req_msg = (struct cm_sidr_req_msg *)
  2748. work->mad_recv_wc->recv_buf.mad;
  2749. param = &work->cm_event.param.sidr_req_rcvd;
  2750. param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
  2751. param->listen_id = listen_id;
  2752. param->service_id = sidr_req_msg->service_id;
  2753. param->bth_pkey = cm_get_bth_pkey(work);
  2754. param->port = work->port->port_num;
  2755. work->cm_event.private_data = &sidr_req_msg->private_data;
  2756. }
  2757. static int cm_sidr_req_handler(struct cm_work *work)
  2758. {
  2759. struct ib_cm_id *cm_id;
  2760. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  2761. struct cm_sidr_req_msg *sidr_req_msg;
  2762. struct ib_wc *wc;
  2763. cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
  2764. if (IS_ERR(cm_id))
  2765. return PTR_ERR(cm_id);
  2766. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2767. /* Record SGID/SLID and request ID for lookup. */
  2768. sidr_req_msg = (struct cm_sidr_req_msg *)
  2769. work->mad_recv_wc->recv_buf.mad;
  2770. wc = work->mad_recv_wc->wc;
  2771. cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
  2772. cm_id_priv->av.dgid.global.interface_id = 0;
  2773. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  2774. work->mad_recv_wc->recv_buf.grh,
  2775. &cm_id_priv->av);
  2776. cm_id_priv->id.remote_id = sidr_req_msg->request_id;
  2777. cm_id_priv->tid = sidr_req_msg->hdr.tid;
  2778. atomic_inc(&cm_id_priv->work_count);
  2779. spin_lock_irq(&cm.lock);
  2780. cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
  2781. if (cur_cm_id_priv) {
  2782. spin_unlock_irq(&cm.lock);
  2783. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2784. counter[CM_SIDR_REQ_COUNTER]);
  2785. goto out; /* Duplicate message. */
  2786. }
  2787. cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
  2788. cur_cm_id_priv = cm_find_listen(cm_id->device,
  2789. sidr_req_msg->service_id);
  2790. if (!cur_cm_id_priv) {
  2791. spin_unlock_irq(&cm.lock);
  2792. cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
  2793. goto out; /* No match. */
  2794. }
  2795. atomic_inc(&cur_cm_id_priv->refcount);
  2796. atomic_inc(&cm_id_priv->refcount);
  2797. spin_unlock_irq(&cm.lock);
  2798. cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
  2799. cm_id_priv->id.context = cur_cm_id_priv->id.context;
  2800. cm_id_priv->id.service_id = sidr_req_msg->service_id;
  2801. cm_id_priv->id.service_mask = ~cpu_to_be64(0);
  2802. cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
  2803. cm_process_work(cm_id_priv, work);
  2804. cm_deref_id(cur_cm_id_priv);
  2805. return 0;
  2806. out:
  2807. ib_destroy_cm_id(&cm_id_priv->id);
  2808. return -EINVAL;
  2809. }
  2810. static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
  2811. struct cm_id_private *cm_id_priv,
  2812. struct ib_cm_sidr_rep_param *param)
  2813. {
  2814. cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
  2815. cm_id_priv->tid);
  2816. sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
  2817. sidr_rep_msg->status = param->status;
  2818. cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
  2819. sidr_rep_msg->service_id = cm_id_priv->id.service_id;
  2820. sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
  2821. if (param->info && param->info_length)
  2822. memcpy(sidr_rep_msg->info, param->info, param->info_length);
  2823. if (param->private_data && param->private_data_len)
  2824. memcpy(sidr_rep_msg->private_data, param->private_data,
  2825. param->private_data_len);
  2826. }
  2827. int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
  2828. struct ib_cm_sidr_rep_param *param)
  2829. {
  2830. struct cm_id_private *cm_id_priv;
  2831. struct ib_mad_send_buf *msg;
  2832. unsigned long flags;
  2833. int ret;
  2834. if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
  2835. (param->private_data &&
  2836. param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
  2837. return -EINVAL;
  2838. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2839. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2840. if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
  2841. ret = -EINVAL;
  2842. goto error;
  2843. }
  2844. ret = cm_alloc_msg(cm_id_priv, &msg);
  2845. if (ret)
  2846. goto error;
  2847. cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
  2848. param);
  2849. ret = ib_post_send_mad(msg, NULL);
  2850. if (ret) {
  2851. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2852. cm_free_msg(msg);
  2853. return ret;
  2854. }
  2855. cm_id->state = IB_CM_IDLE;
  2856. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2857. spin_lock_irqsave(&cm.lock, flags);
  2858. if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
  2859. rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  2860. RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
  2861. }
  2862. spin_unlock_irqrestore(&cm.lock, flags);
  2863. return 0;
  2864. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2865. return ret;
  2866. }
  2867. EXPORT_SYMBOL(ib_send_cm_sidr_rep);
  2868. static void cm_format_sidr_rep_event(struct cm_work *work)
  2869. {
  2870. struct cm_sidr_rep_msg *sidr_rep_msg;
  2871. struct ib_cm_sidr_rep_event_param *param;
  2872. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2873. work->mad_recv_wc->recv_buf.mad;
  2874. param = &work->cm_event.param.sidr_rep_rcvd;
  2875. param->status = sidr_rep_msg->status;
  2876. param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
  2877. param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
  2878. param->info = &sidr_rep_msg->info;
  2879. param->info_len = sidr_rep_msg->info_length;
  2880. work->cm_event.private_data = &sidr_rep_msg->private_data;
  2881. }
  2882. static int cm_sidr_rep_handler(struct cm_work *work)
  2883. {
  2884. struct cm_sidr_rep_msg *sidr_rep_msg;
  2885. struct cm_id_private *cm_id_priv;
  2886. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2887. work->mad_recv_wc->recv_buf.mad;
  2888. cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
  2889. if (!cm_id_priv)
  2890. return -EINVAL; /* Unmatched reply. */
  2891. spin_lock_irq(&cm_id_priv->lock);
  2892. if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
  2893. spin_unlock_irq(&cm_id_priv->lock);
  2894. goto out;
  2895. }
  2896. cm_id_priv->id.state = IB_CM_IDLE;
  2897. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2898. spin_unlock_irq(&cm_id_priv->lock);
  2899. cm_format_sidr_rep_event(work);
  2900. cm_process_work(cm_id_priv, work);
  2901. return 0;
  2902. out:
  2903. cm_deref_id(cm_id_priv);
  2904. return -EINVAL;
  2905. }
  2906. static void cm_process_send_error(struct ib_mad_send_buf *msg,
  2907. enum ib_wc_status wc_status)
  2908. {
  2909. struct cm_id_private *cm_id_priv;
  2910. struct ib_cm_event cm_event;
  2911. enum ib_cm_state state;
  2912. int ret;
  2913. memset(&cm_event, 0, sizeof cm_event);
  2914. cm_id_priv = msg->context[0];
  2915. /* Discard old sends or ones without a response. */
  2916. spin_lock_irq(&cm_id_priv->lock);
  2917. state = (enum ib_cm_state) (unsigned long) msg->context[1];
  2918. if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
  2919. goto discard;
  2920. switch (state) {
  2921. case IB_CM_REQ_SENT:
  2922. case IB_CM_MRA_REQ_RCVD:
  2923. cm_reset_to_idle(cm_id_priv);
  2924. cm_event.event = IB_CM_REQ_ERROR;
  2925. break;
  2926. case IB_CM_REP_SENT:
  2927. case IB_CM_MRA_REP_RCVD:
  2928. cm_reset_to_idle(cm_id_priv);
  2929. cm_event.event = IB_CM_REP_ERROR;
  2930. break;
  2931. case IB_CM_DREQ_SENT:
  2932. cm_enter_timewait(cm_id_priv);
  2933. cm_event.event = IB_CM_DREQ_ERROR;
  2934. break;
  2935. case IB_CM_SIDR_REQ_SENT:
  2936. cm_id_priv->id.state = IB_CM_IDLE;
  2937. cm_event.event = IB_CM_SIDR_REQ_ERROR;
  2938. break;
  2939. default:
  2940. goto discard;
  2941. }
  2942. spin_unlock_irq(&cm_id_priv->lock);
  2943. cm_event.param.send_status = wc_status;
  2944. /* No other events can occur on the cm_id at this point. */
  2945. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
  2946. cm_free_msg(msg);
  2947. if (ret)
  2948. ib_destroy_cm_id(&cm_id_priv->id);
  2949. return;
  2950. discard:
  2951. spin_unlock_irq(&cm_id_priv->lock);
  2952. cm_free_msg(msg);
  2953. }
  2954. static void cm_send_handler(struct ib_mad_agent *mad_agent,
  2955. struct ib_mad_send_wc *mad_send_wc)
  2956. {
  2957. struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
  2958. struct cm_port *port;
  2959. u16 attr_index;
  2960. port = mad_agent->context;
  2961. attr_index = be16_to_cpu(((struct ib_mad_hdr *)
  2962. msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
  2963. /*
  2964. * If the send was in response to a received message (context[0] is not
  2965. * set to a cm_id), and is not a REJ, then it is a send that was
  2966. * manually retried.
  2967. */
  2968. if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
  2969. msg->retries = 1;
  2970. atomic_long_add(1 + msg->retries,
  2971. &port->counter_group[CM_XMIT].counter[attr_index]);
  2972. if (msg->retries)
  2973. atomic_long_add(msg->retries,
  2974. &port->counter_group[CM_XMIT_RETRIES].
  2975. counter[attr_index]);
  2976. switch (mad_send_wc->status) {
  2977. case IB_WC_SUCCESS:
  2978. case IB_WC_WR_FLUSH_ERR:
  2979. cm_free_msg(msg);
  2980. break;
  2981. default:
  2982. if (msg->context[0] && msg->context[1])
  2983. cm_process_send_error(msg, mad_send_wc->status);
  2984. else
  2985. cm_free_msg(msg);
  2986. break;
  2987. }
  2988. }
  2989. static void cm_work_handler(struct work_struct *_work)
  2990. {
  2991. struct cm_work *work = container_of(_work, struct cm_work, work.work);
  2992. int ret;
  2993. switch (work->cm_event.event) {
  2994. case IB_CM_REQ_RECEIVED:
  2995. ret = cm_req_handler(work);
  2996. break;
  2997. case IB_CM_MRA_RECEIVED:
  2998. ret = cm_mra_handler(work);
  2999. break;
  3000. case IB_CM_REJ_RECEIVED:
  3001. ret = cm_rej_handler(work);
  3002. break;
  3003. case IB_CM_REP_RECEIVED:
  3004. ret = cm_rep_handler(work);
  3005. break;
  3006. case IB_CM_RTU_RECEIVED:
  3007. ret = cm_rtu_handler(work);
  3008. break;
  3009. case IB_CM_USER_ESTABLISHED:
  3010. ret = cm_establish_handler(work);
  3011. break;
  3012. case IB_CM_DREQ_RECEIVED:
  3013. ret = cm_dreq_handler(work);
  3014. break;
  3015. case IB_CM_DREP_RECEIVED:
  3016. ret = cm_drep_handler(work);
  3017. break;
  3018. case IB_CM_SIDR_REQ_RECEIVED:
  3019. ret = cm_sidr_req_handler(work);
  3020. break;
  3021. case IB_CM_SIDR_REP_RECEIVED:
  3022. ret = cm_sidr_rep_handler(work);
  3023. break;
  3024. case IB_CM_LAP_RECEIVED:
  3025. ret = cm_lap_handler(work);
  3026. break;
  3027. case IB_CM_APR_RECEIVED:
  3028. ret = cm_apr_handler(work);
  3029. break;
  3030. case IB_CM_TIMEWAIT_EXIT:
  3031. ret = cm_timewait_handler(work);
  3032. break;
  3033. default:
  3034. ret = -EINVAL;
  3035. break;
  3036. }
  3037. if (ret)
  3038. cm_free_work(work);
  3039. }
  3040. static int cm_establish(struct ib_cm_id *cm_id)
  3041. {
  3042. struct cm_id_private *cm_id_priv;
  3043. struct cm_work *work;
  3044. unsigned long flags;
  3045. int ret = 0;
  3046. struct cm_device *cm_dev;
  3047. cm_dev = ib_get_client_data(cm_id->device, &cm_client);
  3048. if (!cm_dev)
  3049. return -ENODEV;
  3050. work = kmalloc(sizeof *work, GFP_ATOMIC);
  3051. if (!work)
  3052. return -ENOMEM;
  3053. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  3054. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3055. switch (cm_id->state)
  3056. {
  3057. case IB_CM_REP_SENT:
  3058. case IB_CM_MRA_REP_RCVD:
  3059. cm_id->state = IB_CM_ESTABLISHED;
  3060. break;
  3061. case IB_CM_ESTABLISHED:
  3062. ret = -EISCONN;
  3063. break;
  3064. default:
  3065. ret = -EINVAL;
  3066. break;
  3067. }
  3068. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3069. if (ret) {
  3070. kfree(work);
  3071. goto out;
  3072. }
  3073. /*
  3074. * The CM worker thread may try to destroy the cm_id before it
  3075. * can execute this work item. To prevent potential deadlock,
  3076. * we need to find the cm_id once we're in the context of the
  3077. * worker thread, rather than holding a reference on it.
  3078. */
  3079. INIT_DELAYED_WORK(&work->work, cm_work_handler);
  3080. work->local_id = cm_id->local_id;
  3081. work->remote_id = cm_id->remote_id;
  3082. work->mad_recv_wc = NULL;
  3083. work->cm_event.event = IB_CM_USER_ESTABLISHED;
  3084. /* Check if the device started its remove_one */
  3085. spin_lock_irqsave(&cm.lock, flags);
  3086. if (!cm_dev->going_down) {
  3087. queue_delayed_work(cm.wq, &work->work, 0);
  3088. } else {
  3089. kfree(work);
  3090. ret = -ENODEV;
  3091. }
  3092. spin_unlock_irqrestore(&cm.lock, flags);
  3093. out:
  3094. return ret;
  3095. }
  3096. static int cm_migrate(struct ib_cm_id *cm_id)
  3097. {
  3098. struct cm_id_private *cm_id_priv;
  3099. struct cm_av tmp_av;
  3100. unsigned long flags;
  3101. int tmp_send_port_not_ready;
  3102. int ret = 0;
  3103. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  3104. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3105. if (cm_id->state == IB_CM_ESTABLISHED &&
  3106. (cm_id->lap_state == IB_CM_LAP_UNINIT ||
  3107. cm_id->lap_state == IB_CM_LAP_IDLE)) {
  3108. cm_id->lap_state = IB_CM_LAP_IDLE;
  3109. /* Swap address vector */
  3110. tmp_av = cm_id_priv->av;
  3111. cm_id_priv->av = cm_id_priv->alt_av;
  3112. cm_id_priv->alt_av = tmp_av;
  3113. /* Swap port send ready state */
  3114. tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
  3115. cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
  3116. cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
  3117. } else
  3118. ret = -EINVAL;
  3119. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3120. return ret;
  3121. }
  3122. int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
  3123. {
  3124. int ret;
  3125. switch (event) {
  3126. case IB_EVENT_COMM_EST:
  3127. ret = cm_establish(cm_id);
  3128. break;
  3129. case IB_EVENT_PATH_MIG:
  3130. ret = cm_migrate(cm_id);
  3131. break;
  3132. default:
  3133. ret = -EINVAL;
  3134. }
  3135. return ret;
  3136. }
  3137. EXPORT_SYMBOL(ib_cm_notify);
  3138. static void cm_recv_handler(struct ib_mad_agent *mad_agent,
  3139. struct ib_mad_send_buf *send_buf,
  3140. struct ib_mad_recv_wc *mad_recv_wc)
  3141. {
  3142. struct cm_port *port = mad_agent->context;
  3143. struct cm_work *work;
  3144. enum ib_cm_event_type event;
  3145. u16 attr_id;
  3146. int paths = 0;
  3147. int going_down = 0;
  3148. switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
  3149. case CM_REQ_ATTR_ID:
  3150. paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
  3151. alt_local_lid != 0);
  3152. event = IB_CM_REQ_RECEIVED;
  3153. break;
  3154. case CM_MRA_ATTR_ID:
  3155. event = IB_CM_MRA_RECEIVED;
  3156. break;
  3157. case CM_REJ_ATTR_ID:
  3158. event = IB_CM_REJ_RECEIVED;
  3159. break;
  3160. case CM_REP_ATTR_ID:
  3161. event = IB_CM_REP_RECEIVED;
  3162. break;
  3163. case CM_RTU_ATTR_ID:
  3164. event = IB_CM_RTU_RECEIVED;
  3165. break;
  3166. case CM_DREQ_ATTR_ID:
  3167. event = IB_CM_DREQ_RECEIVED;
  3168. break;
  3169. case CM_DREP_ATTR_ID:
  3170. event = IB_CM_DREP_RECEIVED;
  3171. break;
  3172. case CM_SIDR_REQ_ATTR_ID:
  3173. event = IB_CM_SIDR_REQ_RECEIVED;
  3174. break;
  3175. case CM_SIDR_REP_ATTR_ID:
  3176. event = IB_CM_SIDR_REP_RECEIVED;
  3177. break;
  3178. case CM_LAP_ATTR_ID:
  3179. paths = 1;
  3180. event = IB_CM_LAP_RECEIVED;
  3181. break;
  3182. case CM_APR_ATTR_ID:
  3183. event = IB_CM_APR_RECEIVED;
  3184. break;
  3185. default:
  3186. ib_free_recv_mad(mad_recv_wc);
  3187. return;
  3188. }
  3189. attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
  3190. atomic_long_inc(&port->counter_group[CM_RECV].
  3191. counter[attr_id - CM_ATTR_ID_OFFSET]);
  3192. work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
  3193. GFP_KERNEL);
  3194. if (!work) {
  3195. ib_free_recv_mad(mad_recv_wc);
  3196. return;
  3197. }
  3198. INIT_DELAYED_WORK(&work->work, cm_work_handler);
  3199. work->cm_event.event = event;
  3200. work->mad_recv_wc = mad_recv_wc;
  3201. work->port = port;
  3202. /* Check if the device started its remove_one */
  3203. spin_lock_irq(&cm.lock);
  3204. if (!port->cm_dev->going_down)
  3205. queue_delayed_work(cm.wq, &work->work, 0);
  3206. else
  3207. going_down = 1;
  3208. spin_unlock_irq(&cm.lock);
  3209. if (going_down) {
  3210. kfree(work);
  3211. ib_free_recv_mad(mad_recv_wc);
  3212. }
  3213. }
  3214. static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
  3215. struct ib_qp_attr *qp_attr,
  3216. int *qp_attr_mask)
  3217. {
  3218. unsigned long flags;
  3219. int ret;
  3220. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3221. switch (cm_id_priv->id.state) {
  3222. case IB_CM_REQ_SENT:
  3223. case IB_CM_MRA_REQ_RCVD:
  3224. case IB_CM_REQ_RCVD:
  3225. case IB_CM_MRA_REQ_SENT:
  3226. case IB_CM_REP_RCVD:
  3227. case IB_CM_MRA_REP_SENT:
  3228. case IB_CM_REP_SENT:
  3229. case IB_CM_MRA_REP_RCVD:
  3230. case IB_CM_ESTABLISHED:
  3231. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
  3232. IB_QP_PKEY_INDEX | IB_QP_PORT;
  3233. qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
  3234. if (cm_id_priv->responder_resources)
  3235. qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
  3236. IB_ACCESS_REMOTE_ATOMIC;
  3237. qp_attr->pkey_index = cm_id_priv->av.pkey_index;
  3238. qp_attr->port_num = cm_id_priv->av.port->port_num;
  3239. ret = 0;
  3240. break;
  3241. default:
  3242. ret = -EINVAL;
  3243. break;
  3244. }
  3245. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3246. return ret;
  3247. }
  3248. static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
  3249. struct ib_qp_attr *qp_attr,
  3250. int *qp_attr_mask)
  3251. {
  3252. unsigned long flags;
  3253. int ret;
  3254. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3255. switch (cm_id_priv->id.state) {
  3256. case IB_CM_REQ_RCVD:
  3257. case IB_CM_MRA_REQ_SENT:
  3258. case IB_CM_REP_RCVD:
  3259. case IB_CM_MRA_REP_SENT:
  3260. case IB_CM_REP_SENT:
  3261. case IB_CM_MRA_REP_RCVD:
  3262. case IB_CM_ESTABLISHED:
  3263. *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
  3264. IB_QP_DEST_QPN | IB_QP_RQ_PSN;
  3265. qp_attr->ah_attr = cm_id_priv->av.ah_attr;
  3266. qp_attr->path_mtu = cm_id_priv->path_mtu;
  3267. qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
  3268. qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
  3269. if (cm_id_priv->qp_type == IB_QPT_RC ||
  3270. cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
  3271. *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
  3272. IB_QP_MIN_RNR_TIMER;
  3273. qp_attr->max_dest_rd_atomic =
  3274. cm_id_priv->responder_resources;
  3275. qp_attr->min_rnr_timer = 0;
  3276. }
  3277. if (cm_id_priv->alt_av.ah_attr.dlid) {
  3278. *qp_attr_mask |= IB_QP_ALT_PATH;
  3279. qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
  3280. qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
  3281. qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
  3282. qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
  3283. }
  3284. ret = 0;
  3285. break;
  3286. default:
  3287. ret = -EINVAL;
  3288. break;
  3289. }
  3290. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3291. return ret;
  3292. }
  3293. static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
  3294. struct ib_qp_attr *qp_attr,
  3295. int *qp_attr_mask)
  3296. {
  3297. unsigned long flags;
  3298. int ret;
  3299. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3300. switch (cm_id_priv->id.state) {
  3301. /* Allow transition to RTS before sending REP */
  3302. case IB_CM_REQ_RCVD:
  3303. case IB_CM_MRA_REQ_SENT:
  3304. case IB_CM_REP_RCVD:
  3305. case IB_CM_MRA_REP_SENT:
  3306. case IB_CM_REP_SENT:
  3307. case IB_CM_MRA_REP_RCVD:
  3308. case IB_CM_ESTABLISHED:
  3309. if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
  3310. *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
  3311. qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
  3312. switch (cm_id_priv->qp_type) {
  3313. case IB_QPT_RC:
  3314. case IB_QPT_XRC_INI:
  3315. *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
  3316. IB_QP_MAX_QP_RD_ATOMIC;
  3317. qp_attr->retry_cnt = cm_id_priv->retry_count;
  3318. qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
  3319. qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
  3320. /* fall through */
  3321. case IB_QPT_XRC_TGT:
  3322. *qp_attr_mask |= IB_QP_TIMEOUT;
  3323. qp_attr->timeout = cm_id_priv->av.timeout;
  3324. break;
  3325. default:
  3326. break;
  3327. }
  3328. if (cm_id_priv->alt_av.ah_attr.dlid) {
  3329. *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
  3330. qp_attr->path_mig_state = IB_MIG_REARM;
  3331. }
  3332. } else {
  3333. *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
  3334. qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
  3335. qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
  3336. qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
  3337. qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
  3338. qp_attr->path_mig_state = IB_MIG_REARM;
  3339. }
  3340. ret = 0;
  3341. break;
  3342. default:
  3343. ret = -EINVAL;
  3344. break;
  3345. }
  3346. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3347. return ret;
  3348. }
  3349. int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
  3350. struct ib_qp_attr *qp_attr,
  3351. int *qp_attr_mask)
  3352. {
  3353. struct cm_id_private *cm_id_priv;
  3354. int ret;
  3355. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  3356. switch (qp_attr->qp_state) {
  3357. case IB_QPS_INIT:
  3358. ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3359. break;
  3360. case IB_QPS_RTR:
  3361. ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3362. break;
  3363. case IB_QPS_RTS:
  3364. ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3365. break;
  3366. default:
  3367. ret = -EINVAL;
  3368. break;
  3369. }
  3370. return ret;
  3371. }
  3372. EXPORT_SYMBOL(ib_cm_init_qp_attr);
  3373. static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
  3374. char *buf)
  3375. {
  3376. struct cm_counter_group *group;
  3377. struct cm_counter_attribute *cm_attr;
  3378. group = container_of(obj, struct cm_counter_group, obj);
  3379. cm_attr = container_of(attr, struct cm_counter_attribute, attr);
  3380. return sprintf(buf, "%ld\n",
  3381. atomic_long_read(&group->counter[cm_attr->index]));
  3382. }
  3383. static const struct sysfs_ops cm_counter_ops = {
  3384. .show = cm_show_counter
  3385. };
  3386. static struct kobj_type cm_counter_obj_type = {
  3387. .sysfs_ops = &cm_counter_ops,
  3388. .default_attrs = cm_counter_default_attrs
  3389. };
  3390. static void cm_release_port_obj(struct kobject *obj)
  3391. {
  3392. struct cm_port *cm_port;
  3393. cm_port = container_of(obj, struct cm_port, port_obj);
  3394. kfree(cm_port);
  3395. }
  3396. static struct kobj_type cm_port_obj_type = {
  3397. .release = cm_release_port_obj
  3398. };
  3399. static char *cm_devnode(struct device *dev, umode_t *mode)
  3400. {
  3401. if (mode)
  3402. *mode = 0666;
  3403. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  3404. }
  3405. struct class cm_class = {
  3406. .owner = THIS_MODULE,
  3407. .name = "infiniband_cm",
  3408. .devnode = cm_devnode,
  3409. };
  3410. EXPORT_SYMBOL(cm_class);
  3411. static int cm_create_port_fs(struct cm_port *port)
  3412. {
  3413. int i, ret;
  3414. ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
  3415. &port->cm_dev->device->kobj,
  3416. "%d", port->port_num);
  3417. if (ret) {
  3418. kfree(port);
  3419. return ret;
  3420. }
  3421. for (i = 0; i < CM_COUNTER_GROUPS; i++) {
  3422. ret = kobject_init_and_add(&port->counter_group[i].obj,
  3423. &cm_counter_obj_type,
  3424. &port->port_obj,
  3425. "%s", counter_group_names[i]);
  3426. if (ret)
  3427. goto error;
  3428. }
  3429. return 0;
  3430. error:
  3431. while (i--)
  3432. kobject_put(&port->counter_group[i].obj);
  3433. kobject_put(&port->port_obj);
  3434. return ret;
  3435. }
  3436. static void cm_remove_port_fs(struct cm_port *port)
  3437. {
  3438. int i;
  3439. for (i = 0; i < CM_COUNTER_GROUPS; i++)
  3440. kobject_put(&port->counter_group[i].obj);
  3441. kobject_put(&port->port_obj);
  3442. }
  3443. static void cm_add_one(struct ib_device *ib_device)
  3444. {
  3445. struct cm_device *cm_dev;
  3446. struct cm_port *port;
  3447. struct ib_mad_reg_req reg_req = {
  3448. .mgmt_class = IB_MGMT_CLASS_CM,
  3449. .mgmt_class_version = IB_CM_CLASS_VERSION,
  3450. };
  3451. struct ib_port_modify port_modify = {
  3452. .set_port_cap_mask = IB_PORT_CM_SUP
  3453. };
  3454. unsigned long flags;
  3455. int ret;
  3456. int count = 0;
  3457. u8 i;
  3458. cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
  3459. ib_device->phys_port_cnt, GFP_KERNEL);
  3460. if (!cm_dev)
  3461. return;
  3462. cm_dev->ib_device = ib_device;
  3463. cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
  3464. cm_dev->going_down = 0;
  3465. cm_dev->device = device_create(&cm_class, &ib_device->dev,
  3466. MKDEV(0, 0), NULL,
  3467. "%s", ib_device->name);
  3468. if (IS_ERR(cm_dev->device)) {
  3469. kfree(cm_dev);
  3470. return;
  3471. }
  3472. set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
  3473. for (i = 1; i <= ib_device->phys_port_cnt; i++) {
  3474. if (!rdma_cap_ib_cm(ib_device, i))
  3475. continue;
  3476. port = kzalloc(sizeof *port, GFP_KERNEL);
  3477. if (!port)
  3478. goto error1;
  3479. cm_dev->port[i-1] = port;
  3480. port->cm_dev = cm_dev;
  3481. port->port_num = i;
  3482. INIT_LIST_HEAD(&port->cm_priv_prim_list);
  3483. INIT_LIST_HEAD(&port->cm_priv_altr_list);
  3484. ret = cm_create_port_fs(port);
  3485. if (ret)
  3486. goto error1;
  3487. port->mad_agent = ib_register_mad_agent(ib_device, i,
  3488. IB_QPT_GSI,
  3489. &reg_req,
  3490. 0,
  3491. cm_send_handler,
  3492. cm_recv_handler,
  3493. port,
  3494. 0);
  3495. if (IS_ERR(port->mad_agent))
  3496. goto error2;
  3497. ret = ib_modify_port(ib_device, i, 0, &port_modify);
  3498. if (ret)
  3499. goto error3;
  3500. count++;
  3501. }
  3502. if (!count)
  3503. goto free;
  3504. ib_set_client_data(ib_device, &cm_client, cm_dev);
  3505. write_lock_irqsave(&cm.device_lock, flags);
  3506. list_add_tail(&cm_dev->list, &cm.device_list);
  3507. write_unlock_irqrestore(&cm.device_lock, flags);
  3508. return;
  3509. error3:
  3510. ib_unregister_mad_agent(port->mad_agent);
  3511. error2:
  3512. cm_remove_port_fs(port);
  3513. error1:
  3514. port_modify.set_port_cap_mask = 0;
  3515. port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
  3516. while (--i) {
  3517. if (!rdma_cap_ib_cm(ib_device, i))
  3518. continue;
  3519. port = cm_dev->port[i-1];
  3520. ib_modify_port(ib_device, port->port_num, 0, &port_modify);
  3521. ib_unregister_mad_agent(port->mad_agent);
  3522. cm_remove_port_fs(port);
  3523. }
  3524. free:
  3525. device_unregister(cm_dev->device);
  3526. kfree(cm_dev);
  3527. }
  3528. static void cm_remove_one(struct ib_device *ib_device, void *client_data)
  3529. {
  3530. struct cm_device *cm_dev = client_data;
  3531. struct cm_port *port;
  3532. struct cm_id_private *cm_id_priv;
  3533. struct ib_mad_agent *cur_mad_agent;
  3534. struct ib_port_modify port_modify = {
  3535. .clr_port_cap_mask = IB_PORT_CM_SUP
  3536. };
  3537. unsigned long flags;
  3538. int i;
  3539. if (!cm_dev)
  3540. return;
  3541. write_lock_irqsave(&cm.device_lock, flags);
  3542. list_del(&cm_dev->list);
  3543. write_unlock_irqrestore(&cm.device_lock, flags);
  3544. spin_lock_irq(&cm.lock);
  3545. cm_dev->going_down = 1;
  3546. spin_unlock_irq(&cm.lock);
  3547. for (i = 1; i <= ib_device->phys_port_cnt; i++) {
  3548. if (!rdma_cap_ib_cm(ib_device, i))
  3549. continue;
  3550. port = cm_dev->port[i-1];
  3551. ib_modify_port(ib_device, port->port_num, 0, &port_modify);
  3552. /* Mark all the cm_id's as not valid */
  3553. spin_lock_irq(&cm.lock);
  3554. list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
  3555. cm_id_priv->altr_send_port_not_ready = 1;
  3556. list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
  3557. cm_id_priv->prim_send_port_not_ready = 1;
  3558. spin_unlock_irq(&cm.lock);
  3559. /*
  3560. * We flush the queue here after the going_down set, this
  3561. * verify that no new works will be queued in the recv handler,
  3562. * after that we can call the unregister_mad_agent
  3563. */
  3564. flush_workqueue(cm.wq);
  3565. spin_lock_irq(&cm.state_lock);
  3566. cur_mad_agent = port->mad_agent;
  3567. port->mad_agent = NULL;
  3568. spin_unlock_irq(&cm.state_lock);
  3569. ib_unregister_mad_agent(cur_mad_agent);
  3570. cm_remove_port_fs(port);
  3571. }
  3572. device_unregister(cm_dev->device);
  3573. kfree(cm_dev);
  3574. }
  3575. static int __init ib_cm_init(void)
  3576. {
  3577. int ret;
  3578. memset(&cm, 0, sizeof cm);
  3579. INIT_LIST_HEAD(&cm.device_list);
  3580. rwlock_init(&cm.device_lock);
  3581. spin_lock_init(&cm.lock);
  3582. spin_lock_init(&cm.state_lock);
  3583. cm.listen_service_table = RB_ROOT;
  3584. cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
  3585. cm.remote_id_table = RB_ROOT;
  3586. cm.remote_qp_table = RB_ROOT;
  3587. cm.remote_sidr_table = RB_ROOT;
  3588. idr_init(&cm.local_id_table);
  3589. get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
  3590. INIT_LIST_HEAD(&cm.timewait_list);
  3591. ret = class_register(&cm_class);
  3592. if (ret) {
  3593. ret = -ENOMEM;
  3594. goto error1;
  3595. }
  3596. cm.wq = create_workqueue("ib_cm");
  3597. if (!cm.wq) {
  3598. ret = -ENOMEM;
  3599. goto error2;
  3600. }
  3601. ret = ib_register_client(&cm_client);
  3602. if (ret)
  3603. goto error3;
  3604. return 0;
  3605. error3:
  3606. destroy_workqueue(cm.wq);
  3607. error2:
  3608. class_unregister(&cm_class);
  3609. error1:
  3610. idr_destroy(&cm.local_id_table);
  3611. return ret;
  3612. }
  3613. static void __exit ib_cm_cleanup(void)
  3614. {
  3615. struct cm_timewait_info *timewait_info, *tmp;
  3616. spin_lock_irq(&cm.lock);
  3617. list_for_each_entry(timewait_info, &cm.timewait_list, list)
  3618. cancel_delayed_work(&timewait_info->work.work);
  3619. spin_unlock_irq(&cm.lock);
  3620. ib_unregister_client(&cm_client);
  3621. destroy_workqueue(cm.wq);
  3622. list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
  3623. list_del(&timewait_info->list);
  3624. kfree(timewait_info);
  3625. }
  3626. class_unregister(&cm_class);
  3627. idr_destroy(&cm.local_id_table);
  3628. }
  3629. module_init(ib_cm_init);
  3630. module_exit(ib_cm_cleanup);