mad.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033
  1. /*
  2. * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. */
  35. #include <linux/dma-mapping.h>
  36. #include <rdma/ib_cache.h>
  37. #include "mad_priv.h"
  38. #include "mad_rmpp.h"
  39. #include "smi.h"
  40. #include "agent.h"
  41. MODULE_LICENSE("Dual BSD/GPL");
  42. MODULE_DESCRIPTION("kernel IB MAD API");
  43. MODULE_AUTHOR("Hal Rosenstock");
  44. MODULE_AUTHOR("Sean Hefty");
  45. static struct kmem_cache *ib_mad_cache;
  46. static struct list_head ib_mad_port_list;
  47. static u32 ib_mad_client_id = 0;
  48. /* Port list lock */
  49. static spinlock_t ib_mad_port_list_lock;
  50. /* Forward declarations */
  51. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  52. struct ib_mad_reg_req *mad_reg_req);
  53. static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
  54. static struct ib_mad_agent_private *find_mad_agent(
  55. struct ib_mad_port_private *port_priv,
  56. struct ib_mad *mad);
  57. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  58. struct ib_mad_private *mad);
  59. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
  60. static void timeout_sends(struct work_struct *work);
  61. static void local_completions(struct work_struct *work);
  62. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  63. struct ib_mad_agent_private *agent_priv,
  64. u8 mgmt_class);
  65. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  66. struct ib_mad_agent_private *agent_priv);
  67. /*
  68. * Returns a ib_mad_port_private structure or NULL for a device/port
  69. * Assumes ib_mad_port_list_lock is being held
  70. */
  71. static inline struct ib_mad_port_private *
  72. __ib_get_mad_port(struct ib_device *device, int port_num)
  73. {
  74. struct ib_mad_port_private *entry;
  75. list_for_each_entry(entry, &ib_mad_port_list, port_list) {
  76. if (entry->device == device && entry->port_num == port_num)
  77. return entry;
  78. }
  79. return NULL;
  80. }
  81. /*
  82. * Wrapper function to return a ib_mad_port_private structure or NULL
  83. * for a device/port
  84. */
  85. static inline struct ib_mad_port_private *
  86. ib_get_mad_port(struct ib_device *device, int port_num)
  87. {
  88. struct ib_mad_port_private *entry;
  89. unsigned long flags;
  90. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  91. entry = __ib_get_mad_port(device, port_num);
  92. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  93. return entry;
  94. }
  95. static inline u8 convert_mgmt_class(u8 mgmt_class)
  96. {
  97. /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
  98. return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
  99. 0 : mgmt_class;
  100. }
  101. static int get_spl_qp_index(enum ib_qp_type qp_type)
  102. {
  103. switch (qp_type)
  104. {
  105. case IB_QPT_SMI:
  106. return 0;
  107. case IB_QPT_GSI:
  108. return 1;
  109. default:
  110. return -1;
  111. }
  112. }
  113. static int vendor_class_index(u8 mgmt_class)
  114. {
  115. return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
  116. }
  117. static int is_vendor_class(u8 mgmt_class)
  118. {
  119. if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
  120. (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
  121. return 0;
  122. return 1;
  123. }
  124. static int is_vendor_oui(char *oui)
  125. {
  126. if (oui[0] || oui[1] || oui[2])
  127. return 1;
  128. return 0;
  129. }
  130. static int is_vendor_method_in_use(
  131. struct ib_mad_mgmt_vendor_class *vendor_class,
  132. struct ib_mad_reg_req *mad_reg_req)
  133. {
  134. struct ib_mad_mgmt_method_table *method;
  135. int i;
  136. for (i = 0; i < MAX_MGMT_OUI; i++) {
  137. if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
  138. method = vendor_class->method_table[i];
  139. if (method) {
  140. if (method_in_use(&method, mad_reg_req))
  141. return 1;
  142. else
  143. break;
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. int ib_response_mad(struct ib_mad *mad)
  150. {
  151. return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
  152. (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
  153. ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
  154. (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
  155. }
  156. EXPORT_SYMBOL(ib_response_mad);
  157. /*
  158. * ib_register_mad_agent - Register to send/receive MADs
  159. */
  160. struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
  161. u8 port_num,
  162. enum ib_qp_type qp_type,
  163. struct ib_mad_reg_req *mad_reg_req,
  164. u8 rmpp_version,
  165. ib_mad_send_handler send_handler,
  166. ib_mad_recv_handler recv_handler,
  167. void *context)
  168. {
  169. struct ib_mad_port_private *port_priv;
  170. struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
  171. struct ib_mad_agent_private *mad_agent_priv;
  172. struct ib_mad_reg_req *reg_req = NULL;
  173. struct ib_mad_mgmt_class_table *class;
  174. struct ib_mad_mgmt_vendor_class_table *vendor;
  175. struct ib_mad_mgmt_vendor_class *vendor_class;
  176. struct ib_mad_mgmt_method_table *method;
  177. int ret2, qpn;
  178. unsigned long flags;
  179. u8 mgmt_class, vclass;
  180. /* Validate parameters */
  181. qpn = get_spl_qp_index(qp_type);
  182. if (qpn == -1)
  183. goto error1;
  184. if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
  185. goto error1;
  186. /* Validate MAD registration request if supplied */
  187. if (mad_reg_req) {
  188. if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
  189. goto error1;
  190. if (!recv_handler)
  191. goto error1;
  192. if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
  193. /*
  194. * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
  195. * one in this range currently allowed
  196. */
  197. if (mad_reg_req->mgmt_class !=
  198. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  199. goto error1;
  200. } else if (mad_reg_req->mgmt_class == 0) {
  201. /*
  202. * Class 0 is reserved in IBA and is used for
  203. * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  204. */
  205. goto error1;
  206. } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
  207. /*
  208. * If class is in "new" vendor range,
  209. * ensure supplied OUI is not zero
  210. */
  211. if (!is_vendor_oui(mad_reg_req->oui))
  212. goto error1;
  213. }
  214. /* Make sure class supplied is consistent with RMPP */
  215. if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
  216. if (rmpp_version)
  217. goto error1;
  218. }
  219. /* Make sure class supplied is consistent with QP type */
  220. if (qp_type == IB_QPT_SMI) {
  221. if ((mad_reg_req->mgmt_class !=
  222. IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
  223. (mad_reg_req->mgmt_class !=
  224. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  225. goto error1;
  226. } else {
  227. if ((mad_reg_req->mgmt_class ==
  228. IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  229. (mad_reg_req->mgmt_class ==
  230. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  231. goto error1;
  232. }
  233. } else {
  234. /* No registration request supplied */
  235. if (!send_handler)
  236. goto error1;
  237. }
  238. /* Validate device and port */
  239. port_priv = ib_get_mad_port(device, port_num);
  240. if (!port_priv) {
  241. ret = ERR_PTR(-ENODEV);
  242. goto error1;
  243. }
  244. /* Allocate structures */
  245. mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
  246. if (!mad_agent_priv) {
  247. ret = ERR_PTR(-ENOMEM);
  248. goto error1;
  249. }
  250. mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
  251. IB_ACCESS_LOCAL_WRITE);
  252. if (IS_ERR(mad_agent_priv->agent.mr)) {
  253. ret = ERR_PTR(-ENOMEM);
  254. goto error2;
  255. }
  256. if (mad_reg_req) {
  257. reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
  258. if (!reg_req) {
  259. ret = ERR_PTR(-ENOMEM);
  260. goto error3;
  261. }
  262. /* Make a copy of the MAD registration request */
  263. memcpy(reg_req, mad_reg_req, sizeof *reg_req);
  264. }
  265. /* Now, fill in the various structures */
  266. mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
  267. mad_agent_priv->reg_req = reg_req;
  268. mad_agent_priv->agent.rmpp_version = rmpp_version;
  269. mad_agent_priv->agent.device = device;
  270. mad_agent_priv->agent.recv_handler = recv_handler;
  271. mad_agent_priv->agent.send_handler = send_handler;
  272. mad_agent_priv->agent.context = context;
  273. mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
  274. mad_agent_priv->agent.port_num = port_num;
  275. spin_lock_irqsave(&port_priv->reg_lock, flags);
  276. mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
  277. /*
  278. * Make sure MAD registration (if supplied)
  279. * is non overlapping with any existing ones
  280. */
  281. if (mad_reg_req) {
  282. mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
  283. if (!is_vendor_class(mgmt_class)) {
  284. class = port_priv->version[mad_reg_req->
  285. mgmt_class_version].class;
  286. if (class) {
  287. method = class->method_table[mgmt_class];
  288. if (method) {
  289. if (method_in_use(&method,
  290. mad_reg_req))
  291. goto error4;
  292. }
  293. }
  294. ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
  295. mgmt_class);
  296. } else {
  297. /* "New" vendor class range */
  298. vendor = port_priv->version[mad_reg_req->
  299. mgmt_class_version].vendor;
  300. if (vendor) {
  301. vclass = vendor_class_index(mgmt_class);
  302. vendor_class = vendor->vendor_class[vclass];
  303. if (vendor_class) {
  304. if (is_vendor_method_in_use(
  305. vendor_class,
  306. mad_reg_req))
  307. goto error4;
  308. }
  309. }
  310. ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
  311. }
  312. if (ret2) {
  313. ret = ERR_PTR(ret2);
  314. goto error4;
  315. }
  316. }
  317. /* Add mad agent into port's agent list */
  318. list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
  319. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  320. spin_lock_init(&mad_agent_priv->lock);
  321. INIT_LIST_HEAD(&mad_agent_priv->send_list);
  322. INIT_LIST_HEAD(&mad_agent_priv->wait_list);
  323. INIT_LIST_HEAD(&mad_agent_priv->done_list);
  324. INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
  325. INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
  326. INIT_LIST_HEAD(&mad_agent_priv->local_list);
  327. INIT_WORK(&mad_agent_priv->local_work, local_completions);
  328. atomic_set(&mad_agent_priv->refcount, 1);
  329. init_completion(&mad_agent_priv->comp);
  330. return &mad_agent_priv->agent;
  331. error4:
  332. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  333. kfree(reg_req);
  334. error3:
  335. ib_dereg_mr(mad_agent_priv->agent.mr);
  336. error2:
  337. kfree(mad_agent_priv);
  338. error1:
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(ib_register_mad_agent);
  342. static inline int is_snooping_sends(int mad_snoop_flags)
  343. {
  344. return (mad_snoop_flags &
  345. (/*IB_MAD_SNOOP_POSTED_SENDS |
  346. IB_MAD_SNOOP_RMPP_SENDS |*/
  347. IB_MAD_SNOOP_SEND_COMPLETIONS /*|
  348. IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
  349. }
  350. static inline int is_snooping_recvs(int mad_snoop_flags)
  351. {
  352. return (mad_snoop_flags &
  353. (IB_MAD_SNOOP_RECVS /*|
  354. IB_MAD_SNOOP_RMPP_RECVS*/));
  355. }
  356. static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
  357. struct ib_mad_snoop_private *mad_snoop_priv)
  358. {
  359. struct ib_mad_snoop_private **new_snoop_table;
  360. unsigned long flags;
  361. int i;
  362. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  363. /* Check for empty slot in array. */
  364. for (i = 0; i < qp_info->snoop_table_size; i++)
  365. if (!qp_info->snoop_table[i])
  366. break;
  367. if (i == qp_info->snoop_table_size) {
  368. /* Grow table. */
  369. new_snoop_table = kmalloc(sizeof mad_snoop_priv *
  370. qp_info->snoop_table_size + 1,
  371. GFP_ATOMIC);
  372. if (!new_snoop_table) {
  373. i = -ENOMEM;
  374. goto out;
  375. }
  376. if (qp_info->snoop_table) {
  377. memcpy(new_snoop_table, qp_info->snoop_table,
  378. sizeof mad_snoop_priv *
  379. qp_info->snoop_table_size);
  380. kfree(qp_info->snoop_table);
  381. }
  382. qp_info->snoop_table = new_snoop_table;
  383. qp_info->snoop_table_size++;
  384. }
  385. qp_info->snoop_table[i] = mad_snoop_priv;
  386. atomic_inc(&qp_info->snoop_count);
  387. out:
  388. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  389. return i;
  390. }
  391. struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
  392. u8 port_num,
  393. enum ib_qp_type qp_type,
  394. int mad_snoop_flags,
  395. ib_mad_snoop_handler snoop_handler,
  396. ib_mad_recv_handler recv_handler,
  397. void *context)
  398. {
  399. struct ib_mad_port_private *port_priv;
  400. struct ib_mad_agent *ret;
  401. struct ib_mad_snoop_private *mad_snoop_priv;
  402. int qpn;
  403. /* Validate parameters */
  404. if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
  405. (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
  406. ret = ERR_PTR(-EINVAL);
  407. goto error1;
  408. }
  409. qpn = get_spl_qp_index(qp_type);
  410. if (qpn == -1) {
  411. ret = ERR_PTR(-EINVAL);
  412. goto error1;
  413. }
  414. port_priv = ib_get_mad_port(device, port_num);
  415. if (!port_priv) {
  416. ret = ERR_PTR(-ENODEV);
  417. goto error1;
  418. }
  419. /* Allocate structures */
  420. mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
  421. if (!mad_snoop_priv) {
  422. ret = ERR_PTR(-ENOMEM);
  423. goto error1;
  424. }
  425. /* Now, fill in the various structures */
  426. mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
  427. mad_snoop_priv->agent.device = device;
  428. mad_snoop_priv->agent.recv_handler = recv_handler;
  429. mad_snoop_priv->agent.snoop_handler = snoop_handler;
  430. mad_snoop_priv->agent.context = context;
  431. mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
  432. mad_snoop_priv->agent.port_num = port_num;
  433. mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
  434. init_completion(&mad_snoop_priv->comp);
  435. mad_snoop_priv->snoop_index = register_snoop_agent(
  436. &port_priv->qp_info[qpn],
  437. mad_snoop_priv);
  438. if (mad_snoop_priv->snoop_index < 0) {
  439. ret = ERR_PTR(mad_snoop_priv->snoop_index);
  440. goto error2;
  441. }
  442. atomic_set(&mad_snoop_priv->refcount, 1);
  443. return &mad_snoop_priv->agent;
  444. error2:
  445. kfree(mad_snoop_priv);
  446. error1:
  447. return ret;
  448. }
  449. EXPORT_SYMBOL(ib_register_mad_snoop);
  450. static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  451. {
  452. if (atomic_dec_and_test(&mad_agent_priv->refcount))
  453. complete(&mad_agent_priv->comp);
  454. }
  455. static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
  456. {
  457. if (atomic_dec_and_test(&mad_snoop_priv->refcount))
  458. complete(&mad_snoop_priv->comp);
  459. }
  460. static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  461. {
  462. struct ib_mad_port_private *port_priv;
  463. unsigned long flags;
  464. /* Note that we could still be handling received MADs */
  465. /*
  466. * Canceling all sends results in dropping received response
  467. * MADs, preventing us from queuing additional work
  468. */
  469. cancel_mads(mad_agent_priv);
  470. port_priv = mad_agent_priv->qp_info->port_priv;
  471. cancel_delayed_work(&mad_agent_priv->timed_work);
  472. spin_lock_irqsave(&port_priv->reg_lock, flags);
  473. remove_mad_reg_req(mad_agent_priv);
  474. list_del(&mad_agent_priv->agent_list);
  475. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  476. flush_workqueue(port_priv->wq);
  477. ib_cancel_rmpp_recvs(mad_agent_priv);
  478. deref_mad_agent(mad_agent_priv);
  479. wait_for_completion(&mad_agent_priv->comp);
  480. kfree(mad_agent_priv->reg_req);
  481. ib_dereg_mr(mad_agent_priv->agent.mr);
  482. kfree(mad_agent_priv);
  483. }
  484. static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
  485. {
  486. struct ib_mad_qp_info *qp_info;
  487. unsigned long flags;
  488. qp_info = mad_snoop_priv->qp_info;
  489. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  490. qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
  491. atomic_dec(&qp_info->snoop_count);
  492. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  493. deref_snoop_agent(mad_snoop_priv);
  494. wait_for_completion(&mad_snoop_priv->comp);
  495. kfree(mad_snoop_priv);
  496. }
  497. /*
  498. * ib_unregister_mad_agent - Unregisters a client from using MAD services
  499. */
  500. int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
  501. {
  502. struct ib_mad_agent_private *mad_agent_priv;
  503. struct ib_mad_snoop_private *mad_snoop_priv;
  504. /* If the TID is zero, the agent can only snoop. */
  505. if (mad_agent->hi_tid) {
  506. mad_agent_priv = container_of(mad_agent,
  507. struct ib_mad_agent_private,
  508. agent);
  509. unregister_mad_agent(mad_agent_priv);
  510. } else {
  511. mad_snoop_priv = container_of(mad_agent,
  512. struct ib_mad_snoop_private,
  513. agent);
  514. unregister_mad_snoop(mad_snoop_priv);
  515. }
  516. return 0;
  517. }
  518. EXPORT_SYMBOL(ib_unregister_mad_agent);
  519. static void dequeue_mad(struct ib_mad_list_head *mad_list)
  520. {
  521. struct ib_mad_queue *mad_queue;
  522. unsigned long flags;
  523. BUG_ON(!mad_list->mad_queue);
  524. mad_queue = mad_list->mad_queue;
  525. spin_lock_irqsave(&mad_queue->lock, flags);
  526. list_del(&mad_list->list);
  527. mad_queue->count--;
  528. spin_unlock_irqrestore(&mad_queue->lock, flags);
  529. }
  530. static void snoop_send(struct ib_mad_qp_info *qp_info,
  531. struct ib_mad_send_buf *send_buf,
  532. struct ib_mad_send_wc *mad_send_wc,
  533. int mad_snoop_flags)
  534. {
  535. struct ib_mad_snoop_private *mad_snoop_priv;
  536. unsigned long flags;
  537. int i;
  538. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  539. for (i = 0; i < qp_info->snoop_table_size; i++) {
  540. mad_snoop_priv = qp_info->snoop_table[i];
  541. if (!mad_snoop_priv ||
  542. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  543. continue;
  544. atomic_inc(&mad_snoop_priv->refcount);
  545. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  546. mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
  547. send_buf, mad_send_wc);
  548. deref_snoop_agent(mad_snoop_priv);
  549. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  550. }
  551. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  552. }
  553. static void snoop_recv(struct ib_mad_qp_info *qp_info,
  554. struct ib_mad_recv_wc *mad_recv_wc,
  555. int mad_snoop_flags)
  556. {
  557. struct ib_mad_snoop_private *mad_snoop_priv;
  558. unsigned long flags;
  559. int i;
  560. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  561. for (i = 0; i < qp_info->snoop_table_size; i++) {
  562. mad_snoop_priv = qp_info->snoop_table[i];
  563. if (!mad_snoop_priv ||
  564. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  565. continue;
  566. atomic_inc(&mad_snoop_priv->refcount);
  567. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  568. mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
  569. mad_recv_wc);
  570. deref_snoop_agent(mad_snoop_priv);
  571. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  572. }
  573. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  574. }
  575. static void build_smp_wc(struct ib_qp *qp,
  576. u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
  577. struct ib_wc *wc)
  578. {
  579. memset(wc, 0, sizeof *wc);
  580. wc->wr_id = wr_id;
  581. wc->status = IB_WC_SUCCESS;
  582. wc->opcode = IB_WC_RECV;
  583. wc->pkey_index = pkey_index;
  584. wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
  585. wc->src_qp = IB_QP0;
  586. wc->qp = qp;
  587. wc->slid = slid;
  588. wc->sl = 0;
  589. wc->dlid_path_bits = 0;
  590. wc->port_num = port_num;
  591. }
  592. /*
  593. * Return 0 if SMP is to be sent
  594. * Return 1 if SMP was consumed locally (whether or not solicited)
  595. * Return < 0 if error
  596. */
  597. static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
  598. struct ib_mad_send_wr_private *mad_send_wr)
  599. {
  600. int ret = 0;
  601. struct ib_smp *smp = mad_send_wr->send_buf.mad;
  602. unsigned long flags;
  603. struct ib_mad_local_private *local;
  604. struct ib_mad_private *mad_priv;
  605. struct ib_mad_port_private *port_priv;
  606. struct ib_mad_agent_private *recv_mad_agent = NULL;
  607. struct ib_device *device = mad_agent_priv->agent.device;
  608. u8 port_num;
  609. struct ib_wc mad_wc;
  610. struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
  611. if (device->node_type == RDMA_NODE_IB_SWITCH &&
  612. smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  613. port_num = send_wr->wr.ud.port_num;
  614. else
  615. port_num = mad_agent_priv->agent.port_num;
  616. /*
  617. * Directed route handling starts if the initial LID routed part of
  618. * a request or the ending LID routed part of a response is empty.
  619. * If we are at the start of the LID routed part, don't update the
  620. * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
  621. */
  622. if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
  623. IB_LID_PERMISSIVE &&
  624. smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
  625. IB_SMI_DISCARD) {
  626. ret = -EINVAL;
  627. printk(KERN_ERR PFX "Invalid directed route\n");
  628. goto out;
  629. }
  630. /* Check to post send on QP or process locally */
  631. if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD)
  632. goto out;
  633. local = kmalloc(sizeof *local, GFP_ATOMIC);
  634. if (!local) {
  635. ret = -ENOMEM;
  636. printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
  637. goto out;
  638. }
  639. local->mad_priv = NULL;
  640. local->recv_mad_agent = NULL;
  641. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
  642. if (!mad_priv) {
  643. ret = -ENOMEM;
  644. printk(KERN_ERR PFX "No memory for local response MAD\n");
  645. kfree(local);
  646. goto out;
  647. }
  648. build_smp_wc(mad_agent_priv->agent.qp,
  649. send_wr->wr_id, be16_to_cpu(smp->dr_slid),
  650. send_wr->wr.ud.pkey_index,
  651. send_wr->wr.ud.port_num, &mad_wc);
  652. /* No GRH for DR SMP */
  653. ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
  654. (struct ib_mad *)smp,
  655. (struct ib_mad *)&mad_priv->mad);
  656. switch (ret)
  657. {
  658. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
  659. if (ib_response_mad(&mad_priv->mad.mad) &&
  660. mad_agent_priv->agent.recv_handler) {
  661. local->mad_priv = mad_priv;
  662. local->recv_mad_agent = mad_agent_priv;
  663. /*
  664. * Reference MAD agent until receive
  665. * side of local completion handled
  666. */
  667. atomic_inc(&mad_agent_priv->refcount);
  668. } else
  669. kmem_cache_free(ib_mad_cache, mad_priv);
  670. break;
  671. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
  672. kmem_cache_free(ib_mad_cache, mad_priv);
  673. break;
  674. case IB_MAD_RESULT_SUCCESS:
  675. /* Treat like an incoming receive MAD */
  676. port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
  677. mad_agent_priv->agent.port_num);
  678. if (port_priv) {
  679. mad_priv->mad.mad.mad_hdr.tid =
  680. ((struct ib_mad *)smp)->mad_hdr.tid;
  681. recv_mad_agent = find_mad_agent(port_priv,
  682. &mad_priv->mad.mad);
  683. }
  684. if (!port_priv || !recv_mad_agent) {
  685. kmem_cache_free(ib_mad_cache, mad_priv);
  686. kfree(local);
  687. ret = 0;
  688. goto out;
  689. }
  690. local->mad_priv = mad_priv;
  691. local->recv_mad_agent = recv_mad_agent;
  692. break;
  693. default:
  694. kmem_cache_free(ib_mad_cache, mad_priv);
  695. kfree(local);
  696. ret = -EINVAL;
  697. goto out;
  698. }
  699. local->mad_send_wr = mad_send_wr;
  700. /* Reference MAD agent until send side of local completion handled */
  701. atomic_inc(&mad_agent_priv->refcount);
  702. /* Queue local completion to local list */
  703. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  704. list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
  705. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  706. queue_work(mad_agent_priv->qp_info->port_priv->wq,
  707. &mad_agent_priv->local_work);
  708. ret = 1;
  709. out:
  710. return ret;
  711. }
  712. static int get_pad_size(int hdr_len, int data_len)
  713. {
  714. int seg_size, pad;
  715. seg_size = sizeof(struct ib_mad) - hdr_len;
  716. if (data_len && seg_size) {
  717. pad = seg_size - data_len % seg_size;
  718. return pad == seg_size ? 0 : pad;
  719. } else
  720. return seg_size;
  721. }
  722. static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
  723. {
  724. struct ib_rmpp_segment *s, *t;
  725. list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
  726. list_del(&s->list);
  727. kfree(s);
  728. }
  729. }
  730. static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
  731. gfp_t gfp_mask)
  732. {
  733. struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
  734. struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
  735. struct ib_rmpp_segment *seg = NULL;
  736. int left, seg_size, pad;
  737. send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
  738. seg_size = send_buf->seg_size;
  739. pad = send_wr->pad;
  740. /* Allocate data segments. */
  741. for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
  742. seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
  743. if (!seg) {
  744. printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
  745. "alloc failed for len %zd, gfp %#x\n",
  746. sizeof (*seg) + seg_size, gfp_mask);
  747. free_send_rmpp_list(send_wr);
  748. return -ENOMEM;
  749. }
  750. seg->num = ++send_buf->seg_count;
  751. list_add_tail(&seg->list, &send_wr->rmpp_list);
  752. }
  753. /* Zero any padding */
  754. if (pad)
  755. memset(seg->data + seg_size - pad, 0, pad);
  756. rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
  757. agent.rmpp_version;
  758. rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
  759. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  760. send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
  761. struct ib_rmpp_segment, list);
  762. send_wr->last_ack_seg = send_wr->cur_seg;
  763. return 0;
  764. }
  765. struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
  766. u32 remote_qpn, u16 pkey_index,
  767. int rmpp_active,
  768. int hdr_len, int data_len,
  769. gfp_t gfp_mask)
  770. {
  771. struct ib_mad_agent_private *mad_agent_priv;
  772. struct ib_mad_send_wr_private *mad_send_wr;
  773. int pad, message_size, ret, size;
  774. void *buf;
  775. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  776. agent);
  777. pad = get_pad_size(hdr_len, data_len);
  778. message_size = hdr_len + data_len + pad;
  779. if ((!mad_agent->rmpp_version &&
  780. (rmpp_active || message_size > sizeof(struct ib_mad))) ||
  781. (!rmpp_active && message_size > sizeof(struct ib_mad)))
  782. return ERR_PTR(-EINVAL);
  783. size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
  784. buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
  785. if (!buf)
  786. return ERR_PTR(-ENOMEM);
  787. mad_send_wr = buf + size;
  788. INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
  789. mad_send_wr->send_buf.mad = buf;
  790. mad_send_wr->send_buf.hdr_len = hdr_len;
  791. mad_send_wr->send_buf.data_len = data_len;
  792. mad_send_wr->pad = pad;
  793. mad_send_wr->mad_agent_priv = mad_agent_priv;
  794. mad_send_wr->sg_list[0].length = hdr_len;
  795. mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
  796. mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
  797. mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
  798. mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
  799. mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
  800. mad_send_wr->send_wr.num_sge = 2;
  801. mad_send_wr->send_wr.opcode = IB_WR_SEND;
  802. mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
  803. mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
  804. mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
  805. mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
  806. if (rmpp_active) {
  807. ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
  808. if (ret) {
  809. kfree(buf);
  810. return ERR_PTR(ret);
  811. }
  812. }
  813. mad_send_wr->send_buf.mad_agent = mad_agent;
  814. atomic_inc(&mad_agent_priv->refcount);
  815. return &mad_send_wr->send_buf;
  816. }
  817. EXPORT_SYMBOL(ib_create_send_mad);
  818. int ib_get_mad_data_offset(u8 mgmt_class)
  819. {
  820. if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
  821. return IB_MGMT_SA_HDR;
  822. else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  823. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  824. (mgmt_class == IB_MGMT_CLASS_BIS))
  825. return IB_MGMT_DEVICE_HDR;
  826. else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  827. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
  828. return IB_MGMT_VENDOR_HDR;
  829. else
  830. return IB_MGMT_MAD_HDR;
  831. }
  832. EXPORT_SYMBOL(ib_get_mad_data_offset);
  833. int ib_is_mad_class_rmpp(u8 mgmt_class)
  834. {
  835. if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
  836. (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  837. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  838. (mgmt_class == IB_MGMT_CLASS_BIS) ||
  839. ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  840. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
  841. return 1;
  842. return 0;
  843. }
  844. EXPORT_SYMBOL(ib_is_mad_class_rmpp);
  845. void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
  846. {
  847. struct ib_mad_send_wr_private *mad_send_wr;
  848. struct list_head *list;
  849. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  850. send_buf);
  851. list = &mad_send_wr->cur_seg->list;
  852. if (mad_send_wr->cur_seg->num < seg_num) {
  853. list_for_each_entry(mad_send_wr->cur_seg, list, list)
  854. if (mad_send_wr->cur_seg->num == seg_num)
  855. break;
  856. } else if (mad_send_wr->cur_seg->num > seg_num) {
  857. list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
  858. if (mad_send_wr->cur_seg->num == seg_num)
  859. break;
  860. }
  861. return mad_send_wr->cur_seg->data;
  862. }
  863. EXPORT_SYMBOL(ib_get_rmpp_segment);
  864. static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
  865. {
  866. if (mad_send_wr->send_buf.seg_count)
  867. return ib_get_rmpp_segment(&mad_send_wr->send_buf,
  868. mad_send_wr->seg_num);
  869. else
  870. return mad_send_wr->send_buf.mad +
  871. mad_send_wr->send_buf.hdr_len;
  872. }
  873. void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
  874. {
  875. struct ib_mad_agent_private *mad_agent_priv;
  876. struct ib_mad_send_wr_private *mad_send_wr;
  877. mad_agent_priv = container_of(send_buf->mad_agent,
  878. struct ib_mad_agent_private, agent);
  879. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  880. send_buf);
  881. free_send_rmpp_list(mad_send_wr);
  882. kfree(send_buf->mad);
  883. deref_mad_agent(mad_agent_priv);
  884. }
  885. EXPORT_SYMBOL(ib_free_send_mad);
  886. int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
  887. {
  888. struct ib_mad_qp_info *qp_info;
  889. struct list_head *list;
  890. struct ib_send_wr *bad_send_wr;
  891. struct ib_mad_agent *mad_agent;
  892. struct ib_sge *sge;
  893. unsigned long flags;
  894. int ret;
  895. /* Set WR ID to find mad_send_wr upon completion */
  896. qp_info = mad_send_wr->mad_agent_priv->qp_info;
  897. mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
  898. mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
  899. mad_agent = mad_send_wr->send_buf.mad_agent;
  900. sge = mad_send_wr->sg_list;
  901. sge[0].addr = ib_dma_map_single(mad_agent->device,
  902. mad_send_wr->send_buf.mad,
  903. sge[0].length,
  904. DMA_TO_DEVICE);
  905. mad_send_wr->header_mapping = sge[0].addr;
  906. sge[1].addr = ib_dma_map_single(mad_agent->device,
  907. ib_get_payload(mad_send_wr),
  908. sge[1].length,
  909. DMA_TO_DEVICE);
  910. mad_send_wr->payload_mapping = sge[1].addr;
  911. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  912. if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
  913. ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
  914. &bad_send_wr);
  915. list = &qp_info->send_queue.list;
  916. } else {
  917. ret = 0;
  918. list = &qp_info->overflow_list;
  919. }
  920. if (!ret) {
  921. qp_info->send_queue.count++;
  922. list_add_tail(&mad_send_wr->mad_list.list, list);
  923. }
  924. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  925. if (ret) {
  926. ib_dma_unmap_single(mad_agent->device,
  927. mad_send_wr->header_mapping,
  928. sge[0].length, DMA_TO_DEVICE);
  929. ib_dma_unmap_single(mad_agent->device,
  930. mad_send_wr->payload_mapping,
  931. sge[1].length, DMA_TO_DEVICE);
  932. }
  933. return ret;
  934. }
  935. /*
  936. * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
  937. * with the registered client
  938. */
  939. int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
  940. struct ib_mad_send_buf **bad_send_buf)
  941. {
  942. struct ib_mad_agent_private *mad_agent_priv;
  943. struct ib_mad_send_buf *next_send_buf;
  944. struct ib_mad_send_wr_private *mad_send_wr;
  945. unsigned long flags;
  946. int ret = -EINVAL;
  947. /* Walk list of send WRs and post each on send list */
  948. for (; send_buf; send_buf = next_send_buf) {
  949. mad_send_wr = container_of(send_buf,
  950. struct ib_mad_send_wr_private,
  951. send_buf);
  952. mad_agent_priv = mad_send_wr->mad_agent_priv;
  953. if (!send_buf->mad_agent->send_handler ||
  954. (send_buf->timeout_ms &&
  955. !send_buf->mad_agent->recv_handler)) {
  956. ret = -EINVAL;
  957. goto error;
  958. }
  959. if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
  960. if (mad_agent_priv->agent.rmpp_version) {
  961. ret = -EINVAL;
  962. goto error;
  963. }
  964. }
  965. /*
  966. * Save pointer to next work request to post in case the
  967. * current one completes, and the user modifies the work
  968. * request associated with the completion
  969. */
  970. next_send_buf = send_buf->next;
  971. mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
  972. if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
  973. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  974. ret = handle_outgoing_dr_smp(mad_agent_priv,
  975. mad_send_wr);
  976. if (ret < 0) /* error */
  977. goto error;
  978. else if (ret == 1) /* locally consumed */
  979. continue;
  980. }
  981. mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
  982. /* Timeout will be updated after send completes */
  983. mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
  984. mad_send_wr->retries = send_buf->retries;
  985. /* Reference for work request to QP + response */
  986. mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
  987. mad_send_wr->status = IB_WC_SUCCESS;
  988. /* Reference MAD agent until send completes */
  989. atomic_inc(&mad_agent_priv->refcount);
  990. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  991. list_add_tail(&mad_send_wr->agent_list,
  992. &mad_agent_priv->send_list);
  993. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  994. if (mad_agent_priv->agent.rmpp_version) {
  995. ret = ib_send_rmpp_mad(mad_send_wr);
  996. if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
  997. ret = ib_send_mad(mad_send_wr);
  998. } else
  999. ret = ib_send_mad(mad_send_wr);
  1000. if (ret < 0) {
  1001. /* Fail send request */
  1002. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1003. list_del(&mad_send_wr->agent_list);
  1004. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1005. atomic_dec(&mad_agent_priv->refcount);
  1006. goto error;
  1007. }
  1008. }
  1009. return 0;
  1010. error:
  1011. if (bad_send_buf)
  1012. *bad_send_buf = send_buf;
  1013. return ret;
  1014. }
  1015. EXPORT_SYMBOL(ib_post_send_mad);
  1016. /*
  1017. * ib_free_recv_mad - Returns data buffers used to receive
  1018. * a MAD to the access layer
  1019. */
  1020. void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
  1021. {
  1022. struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
  1023. struct ib_mad_private_header *mad_priv_hdr;
  1024. struct ib_mad_private *priv;
  1025. struct list_head free_list;
  1026. INIT_LIST_HEAD(&free_list);
  1027. list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
  1028. list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
  1029. &free_list, list) {
  1030. mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
  1031. recv_buf);
  1032. mad_priv_hdr = container_of(mad_recv_wc,
  1033. struct ib_mad_private_header,
  1034. recv_wc);
  1035. priv = container_of(mad_priv_hdr, struct ib_mad_private,
  1036. header);
  1037. kmem_cache_free(ib_mad_cache, priv);
  1038. }
  1039. }
  1040. EXPORT_SYMBOL(ib_free_recv_mad);
  1041. struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
  1042. u8 rmpp_version,
  1043. ib_mad_send_handler send_handler,
  1044. ib_mad_recv_handler recv_handler,
  1045. void *context)
  1046. {
  1047. return ERR_PTR(-EINVAL); /* XXX: for now */
  1048. }
  1049. EXPORT_SYMBOL(ib_redirect_mad_qp);
  1050. int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
  1051. struct ib_wc *wc)
  1052. {
  1053. printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL(ib_process_mad_wc);
  1057. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  1058. struct ib_mad_reg_req *mad_reg_req)
  1059. {
  1060. int i;
  1061. for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
  1062. i < IB_MGMT_MAX_METHODS;
  1063. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1064. 1+i)) {
  1065. if ((*method)->agent[i]) {
  1066. printk(KERN_ERR PFX "Method %d already in use\n", i);
  1067. return -EINVAL;
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
  1073. {
  1074. /* Allocate management method table */
  1075. *method = kzalloc(sizeof **method, GFP_ATOMIC);
  1076. if (!*method) {
  1077. printk(KERN_ERR PFX "No memory for "
  1078. "ib_mad_mgmt_method_table\n");
  1079. return -ENOMEM;
  1080. }
  1081. return 0;
  1082. }
  1083. /*
  1084. * Check to see if there are any methods still in use
  1085. */
  1086. static int check_method_table(struct ib_mad_mgmt_method_table *method)
  1087. {
  1088. int i;
  1089. for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
  1090. if (method->agent[i])
  1091. return 1;
  1092. return 0;
  1093. }
  1094. /*
  1095. * Check to see if there are any method tables for this class still in use
  1096. */
  1097. static int check_class_table(struct ib_mad_mgmt_class_table *class)
  1098. {
  1099. int i;
  1100. for (i = 0; i < MAX_MGMT_CLASS; i++)
  1101. if (class->method_table[i])
  1102. return 1;
  1103. return 0;
  1104. }
  1105. static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
  1106. {
  1107. int i;
  1108. for (i = 0; i < MAX_MGMT_OUI; i++)
  1109. if (vendor_class->method_table[i])
  1110. return 1;
  1111. return 0;
  1112. }
  1113. static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
  1114. char *oui)
  1115. {
  1116. int i;
  1117. for (i = 0; i < MAX_MGMT_OUI; i++)
  1118. /* Is there matching OUI for this vendor class ? */
  1119. if (!memcmp(vendor_class->oui[i], oui, 3))
  1120. return i;
  1121. return -1;
  1122. }
  1123. static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
  1124. {
  1125. int i;
  1126. for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
  1127. if (vendor->vendor_class[i])
  1128. return 1;
  1129. return 0;
  1130. }
  1131. static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
  1132. struct ib_mad_agent_private *agent)
  1133. {
  1134. int i;
  1135. /* Remove any methods for this mad agent */
  1136. for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
  1137. if (method->agent[i] == agent) {
  1138. method->agent[i] = NULL;
  1139. }
  1140. }
  1141. }
  1142. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1143. struct ib_mad_agent_private *agent_priv,
  1144. u8 mgmt_class)
  1145. {
  1146. struct ib_mad_port_private *port_priv;
  1147. struct ib_mad_mgmt_class_table **class;
  1148. struct ib_mad_mgmt_method_table **method;
  1149. int i, ret;
  1150. port_priv = agent_priv->qp_info->port_priv;
  1151. class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
  1152. if (!*class) {
  1153. /* Allocate management class table for "new" class version */
  1154. *class = kzalloc(sizeof **class, GFP_ATOMIC);
  1155. if (!*class) {
  1156. printk(KERN_ERR PFX "No memory for "
  1157. "ib_mad_mgmt_class_table\n");
  1158. ret = -ENOMEM;
  1159. goto error1;
  1160. }
  1161. /* Allocate method table for this management class */
  1162. method = &(*class)->method_table[mgmt_class];
  1163. if ((ret = allocate_method_table(method)))
  1164. goto error2;
  1165. } else {
  1166. method = &(*class)->method_table[mgmt_class];
  1167. if (!*method) {
  1168. /* Allocate method table for this management class */
  1169. if ((ret = allocate_method_table(method)))
  1170. goto error1;
  1171. }
  1172. }
  1173. /* Now, make sure methods are not already in use */
  1174. if (method_in_use(method, mad_reg_req))
  1175. goto error3;
  1176. /* Finally, add in methods being registered */
  1177. for (i = find_first_bit(mad_reg_req->method_mask,
  1178. IB_MGMT_MAX_METHODS);
  1179. i < IB_MGMT_MAX_METHODS;
  1180. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1181. 1+i)) {
  1182. (*method)->agent[i] = agent_priv;
  1183. }
  1184. return 0;
  1185. error3:
  1186. /* Remove any methods for this mad agent */
  1187. remove_methods_mad_agent(*method, agent_priv);
  1188. /* Now, check to see if there are any methods in use */
  1189. if (!check_method_table(*method)) {
  1190. /* If not, release management method table */
  1191. kfree(*method);
  1192. *method = NULL;
  1193. }
  1194. ret = -EINVAL;
  1195. goto error1;
  1196. error2:
  1197. kfree(*class);
  1198. *class = NULL;
  1199. error1:
  1200. return ret;
  1201. }
  1202. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1203. struct ib_mad_agent_private *agent_priv)
  1204. {
  1205. struct ib_mad_port_private *port_priv;
  1206. struct ib_mad_mgmt_vendor_class_table **vendor_table;
  1207. struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
  1208. struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
  1209. struct ib_mad_mgmt_method_table **method;
  1210. int i, ret = -ENOMEM;
  1211. u8 vclass;
  1212. /* "New" vendor (with OUI) class */
  1213. vclass = vendor_class_index(mad_reg_req->mgmt_class);
  1214. port_priv = agent_priv->qp_info->port_priv;
  1215. vendor_table = &port_priv->version[
  1216. mad_reg_req->mgmt_class_version].vendor;
  1217. if (!*vendor_table) {
  1218. /* Allocate mgmt vendor class table for "new" class version */
  1219. vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
  1220. if (!vendor) {
  1221. printk(KERN_ERR PFX "No memory for "
  1222. "ib_mad_mgmt_vendor_class_table\n");
  1223. goto error1;
  1224. }
  1225. *vendor_table = vendor;
  1226. }
  1227. if (!(*vendor_table)->vendor_class[vclass]) {
  1228. /* Allocate table for this management vendor class */
  1229. vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
  1230. if (!vendor_class) {
  1231. printk(KERN_ERR PFX "No memory for "
  1232. "ib_mad_mgmt_vendor_class\n");
  1233. goto error2;
  1234. }
  1235. (*vendor_table)->vendor_class[vclass] = vendor_class;
  1236. }
  1237. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1238. /* Is there matching OUI for this vendor class ? */
  1239. if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
  1240. mad_reg_req->oui, 3)) {
  1241. method = &(*vendor_table)->vendor_class[
  1242. vclass]->method_table[i];
  1243. BUG_ON(!*method);
  1244. goto check_in_use;
  1245. }
  1246. }
  1247. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1248. /* OUI slot available ? */
  1249. if (!is_vendor_oui((*vendor_table)->vendor_class[
  1250. vclass]->oui[i])) {
  1251. method = &(*vendor_table)->vendor_class[
  1252. vclass]->method_table[i];
  1253. BUG_ON(*method);
  1254. /* Allocate method table for this OUI */
  1255. if ((ret = allocate_method_table(method)))
  1256. goto error3;
  1257. memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
  1258. mad_reg_req->oui, 3);
  1259. goto check_in_use;
  1260. }
  1261. }
  1262. printk(KERN_ERR PFX "All OUI slots in use\n");
  1263. goto error3;
  1264. check_in_use:
  1265. /* Now, make sure methods are not already in use */
  1266. if (method_in_use(method, mad_reg_req))
  1267. goto error4;
  1268. /* Finally, add in methods being registered */
  1269. for (i = find_first_bit(mad_reg_req->method_mask,
  1270. IB_MGMT_MAX_METHODS);
  1271. i < IB_MGMT_MAX_METHODS;
  1272. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1273. 1+i)) {
  1274. (*method)->agent[i] = agent_priv;
  1275. }
  1276. return 0;
  1277. error4:
  1278. /* Remove any methods for this mad agent */
  1279. remove_methods_mad_agent(*method, agent_priv);
  1280. /* Now, check to see if there are any methods in use */
  1281. if (!check_method_table(*method)) {
  1282. /* If not, release management method table */
  1283. kfree(*method);
  1284. *method = NULL;
  1285. }
  1286. ret = -EINVAL;
  1287. error3:
  1288. if (vendor_class) {
  1289. (*vendor_table)->vendor_class[vclass] = NULL;
  1290. kfree(vendor_class);
  1291. }
  1292. error2:
  1293. if (vendor) {
  1294. *vendor_table = NULL;
  1295. kfree(vendor);
  1296. }
  1297. error1:
  1298. return ret;
  1299. }
  1300. static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
  1301. {
  1302. struct ib_mad_port_private *port_priv;
  1303. struct ib_mad_mgmt_class_table *class;
  1304. struct ib_mad_mgmt_method_table *method;
  1305. struct ib_mad_mgmt_vendor_class_table *vendor;
  1306. struct ib_mad_mgmt_vendor_class *vendor_class;
  1307. int index;
  1308. u8 mgmt_class;
  1309. /*
  1310. * Was MAD registration request supplied
  1311. * with original registration ?
  1312. */
  1313. if (!agent_priv->reg_req) {
  1314. goto out;
  1315. }
  1316. port_priv = agent_priv->qp_info->port_priv;
  1317. mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
  1318. class = port_priv->version[
  1319. agent_priv->reg_req->mgmt_class_version].class;
  1320. if (!class)
  1321. goto vendor_check;
  1322. method = class->method_table[mgmt_class];
  1323. if (method) {
  1324. /* Remove any methods for this mad agent */
  1325. remove_methods_mad_agent(method, agent_priv);
  1326. /* Now, check to see if there are any methods still in use */
  1327. if (!check_method_table(method)) {
  1328. /* If not, release management method table */
  1329. kfree(method);
  1330. class->method_table[mgmt_class] = NULL;
  1331. /* Any management classes left ? */
  1332. if (!check_class_table(class)) {
  1333. /* If not, release management class table */
  1334. kfree(class);
  1335. port_priv->version[
  1336. agent_priv->reg_req->
  1337. mgmt_class_version].class = NULL;
  1338. }
  1339. }
  1340. }
  1341. vendor_check:
  1342. if (!is_vendor_class(mgmt_class))
  1343. goto out;
  1344. /* normalize mgmt_class to vendor range 2 */
  1345. mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
  1346. vendor = port_priv->version[
  1347. agent_priv->reg_req->mgmt_class_version].vendor;
  1348. if (!vendor)
  1349. goto out;
  1350. vendor_class = vendor->vendor_class[mgmt_class];
  1351. if (vendor_class) {
  1352. index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
  1353. if (index < 0)
  1354. goto out;
  1355. method = vendor_class->method_table[index];
  1356. if (method) {
  1357. /* Remove any methods for this mad agent */
  1358. remove_methods_mad_agent(method, agent_priv);
  1359. /*
  1360. * Now, check to see if there are
  1361. * any methods still in use
  1362. */
  1363. if (!check_method_table(method)) {
  1364. /* If not, release management method table */
  1365. kfree(method);
  1366. vendor_class->method_table[index] = NULL;
  1367. memset(vendor_class->oui[index], 0, 3);
  1368. /* Any OUIs left ? */
  1369. if (!check_vendor_class(vendor_class)) {
  1370. /* If not, release vendor class table */
  1371. kfree(vendor_class);
  1372. vendor->vendor_class[mgmt_class] = NULL;
  1373. /* Any other vendor classes left ? */
  1374. if (!check_vendor_table(vendor)) {
  1375. kfree(vendor);
  1376. port_priv->version[
  1377. agent_priv->reg_req->
  1378. mgmt_class_version].
  1379. vendor = NULL;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. out:
  1386. return;
  1387. }
  1388. static struct ib_mad_agent_private *
  1389. find_mad_agent(struct ib_mad_port_private *port_priv,
  1390. struct ib_mad *mad)
  1391. {
  1392. struct ib_mad_agent_private *mad_agent = NULL;
  1393. unsigned long flags;
  1394. spin_lock_irqsave(&port_priv->reg_lock, flags);
  1395. if (ib_response_mad(mad)) {
  1396. u32 hi_tid;
  1397. struct ib_mad_agent_private *entry;
  1398. /*
  1399. * Routing is based on high 32 bits of transaction ID
  1400. * of MAD.
  1401. */
  1402. hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
  1403. list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
  1404. if (entry->agent.hi_tid == hi_tid) {
  1405. mad_agent = entry;
  1406. break;
  1407. }
  1408. }
  1409. } else {
  1410. struct ib_mad_mgmt_class_table *class;
  1411. struct ib_mad_mgmt_method_table *method;
  1412. struct ib_mad_mgmt_vendor_class_table *vendor;
  1413. struct ib_mad_mgmt_vendor_class *vendor_class;
  1414. struct ib_vendor_mad *vendor_mad;
  1415. int index;
  1416. /*
  1417. * Routing is based on version, class, and method
  1418. * For "newer" vendor MADs, also based on OUI
  1419. */
  1420. if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
  1421. goto out;
  1422. if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
  1423. class = port_priv->version[
  1424. mad->mad_hdr.class_version].class;
  1425. if (!class)
  1426. goto out;
  1427. method = class->method_table[convert_mgmt_class(
  1428. mad->mad_hdr.mgmt_class)];
  1429. if (method)
  1430. mad_agent = method->agent[mad->mad_hdr.method &
  1431. ~IB_MGMT_METHOD_RESP];
  1432. } else {
  1433. vendor = port_priv->version[
  1434. mad->mad_hdr.class_version].vendor;
  1435. if (!vendor)
  1436. goto out;
  1437. vendor_class = vendor->vendor_class[vendor_class_index(
  1438. mad->mad_hdr.mgmt_class)];
  1439. if (!vendor_class)
  1440. goto out;
  1441. /* Find matching OUI */
  1442. vendor_mad = (struct ib_vendor_mad *)mad;
  1443. index = find_vendor_oui(vendor_class, vendor_mad->oui);
  1444. if (index == -1)
  1445. goto out;
  1446. method = vendor_class->method_table[index];
  1447. if (method) {
  1448. mad_agent = method->agent[mad->mad_hdr.method &
  1449. ~IB_MGMT_METHOD_RESP];
  1450. }
  1451. }
  1452. }
  1453. if (mad_agent) {
  1454. if (mad_agent->agent.recv_handler)
  1455. atomic_inc(&mad_agent->refcount);
  1456. else {
  1457. printk(KERN_NOTICE PFX "No receive handler for client "
  1458. "%p on port %d\n",
  1459. &mad_agent->agent, port_priv->port_num);
  1460. mad_agent = NULL;
  1461. }
  1462. }
  1463. out:
  1464. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  1465. return mad_agent;
  1466. }
  1467. static int validate_mad(struct ib_mad *mad, u32 qp_num)
  1468. {
  1469. int valid = 0;
  1470. /* Make sure MAD base version is understood */
  1471. if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
  1472. printk(KERN_ERR PFX "MAD received with unsupported base "
  1473. "version %d\n", mad->mad_hdr.base_version);
  1474. goto out;
  1475. }
  1476. /* Filter SMI packets sent to other than QP0 */
  1477. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  1478. (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
  1479. if (qp_num == 0)
  1480. valid = 1;
  1481. } else {
  1482. /* Filter GSI packets sent to QP0 */
  1483. if (qp_num != 0)
  1484. valid = 1;
  1485. }
  1486. out:
  1487. return valid;
  1488. }
  1489. static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
  1490. struct ib_mad_hdr *mad_hdr)
  1491. {
  1492. struct ib_rmpp_mad *rmpp_mad;
  1493. rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
  1494. return !mad_agent_priv->agent.rmpp_version ||
  1495. !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  1496. IB_MGMT_RMPP_FLAG_ACTIVE) ||
  1497. (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
  1498. }
  1499. static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
  1500. struct ib_mad_recv_wc *rwc)
  1501. {
  1502. return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
  1503. rwc->recv_buf.mad->mad_hdr.mgmt_class;
  1504. }
  1505. static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
  1506. struct ib_mad_send_wr_private *wr,
  1507. struct ib_mad_recv_wc *rwc )
  1508. {
  1509. struct ib_ah_attr attr;
  1510. u8 send_resp, rcv_resp;
  1511. union ib_gid sgid;
  1512. struct ib_device *device = mad_agent_priv->agent.device;
  1513. u8 port_num = mad_agent_priv->agent.port_num;
  1514. u8 lmc;
  1515. send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
  1516. mad_hdr.method & IB_MGMT_METHOD_RESP;
  1517. rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
  1518. if (send_resp == rcv_resp)
  1519. /* both requests, or both responses. GIDs different */
  1520. return 0;
  1521. if (ib_query_ah(wr->send_buf.ah, &attr))
  1522. /* Assume not equal, to avoid false positives. */
  1523. return 0;
  1524. if (!!(attr.ah_flags & IB_AH_GRH) !=
  1525. !!(rwc->wc->wc_flags & IB_WC_GRH))
  1526. /* one has GID, other does not. Assume different */
  1527. return 0;
  1528. if (!send_resp && rcv_resp) {
  1529. /* is request/response. */
  1530. if (!(attr.ah_flags & IB_AH_GRH)) {
  1531. if (ib_get_cached_lmc(device, port_num, &lmc))
  1532. return 0;
  1533. return (!lmc || !((attr.src_path_bits ^
  1534. rwc->wc->dlid_path_bits) &
  1535. ((1 << lmc) - 1)));
  1536. } else {
  1537. if (ib_get_cached_gid(device, port_num,
  1538. attr.grh.sgid_index, &sgid))
  1539. return 0;
  1540. return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
  1541. 16);
  1542. }
  1543. }
  1544. if (!(attr.ah_flags & IB_AH_GRH))
  1545. return attr.dlid == rwc->wc->slid;
  1546. else
  1547. return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
  1548. 16);
  1549. }
  1550. static inline int is_direct(u8 class)
  1551. {
  1552. return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
  1553. }
  1554. struct ib_mad_send_wr_private*
  1555. ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
  1556. struct ib_mad_recv_wc *wc)
  1557. {
  1558. struct ib_mad_send_wr_private *wr;
  1559. struct ib_mad *mad;
  1560. mad = (struct ib_mad *)wc->recv_buf.mad;
  1561. list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
  1562. if ((wr->tid == mad->mad_hdr.tid) &&
  1563. rcv_has_same_class(wr, wc) &&
  1564. /*
  1565. * Don't check GID for direct routed MADs.
  1566. * These might have permissive LIDs.
  1567. */
  1568. (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
  1569. rcv_has_same_gid(mad_agent_priv, wr, wc)))
  1570. return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
  1571. }
  1572. /*
  1573. * It's possible to receive the response before we've
  1574. * been notified that the send has completed
  1575. */
  1576. list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
  1577. if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
  1578. wr->tid == mad->mad_hdr.tid &&
  1579. wr->timeout &&
  1580. rcv_has_same_class(wr, wc) &&
  1581. /*
  1582. * Don't check GID for direct routed MADs.
  1583. * These might have permissive LIDs.
  1584. */
  1585. (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
  1586. rcv_has_same_gid(mad_agent_priv, wr, wc)))
  1587. /* Verify request has not been canceled */
  1588. return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
  1589. }
  1590. return NULL;
  1591. }
  1592. void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
  1593. {
  1594. mad_send_wr->timeout = 0;
  1595. if (mad_send_wr->refcount == 1)
  1596. list_move_tail(&mad_send_wr->agent_list,
  1597. &mad_send_wr->mad_agent_priv->done_list);
  1598. }
  1599. static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
  1600. struct ib_mad_recv_wc *mad_recv_wc)
  1601. {
  1602. struct ib_mad_send_wr_private *mad_send_wr;
  1603. struct ib_mad_send_wc mad_send_wc;
  1604. unsigned long flags;
  1605. INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
  1606. list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
  1607. if (mad_agent_priv->agent.rmpp_version) {
  1608. mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
  1609. mad_recv_wc);
  1610. if (!mad_recv_wc) {
  1611. deref_mad_agent(mad_agent_priv);
  1612. return;
  1613. }
  1614. }
  1615. /* Complete corresponding request */
  1616. if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
  1617. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1618. mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
  1619. if (!mad_send_wr) {
  1620. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1621. ib_free_recv_mad(mad_recv_wc);
  1622. deref_mad_agent(mad_agent_priv);
  1623. return;
  1624. }
  1625. ib_mark_mad_done(mad_send_wr);
  1626. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1627. /* Defined behavior is to complete response before request */
  1628. mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
  1629. mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
  1630. mad_recv_wc);
  1631. atomic_dec(&mad_agent_priv->refcount);
  1632. mad_send_wc.status = IB_WC_SUCCESS;
  1633. mad_send_wc.vendor_err = 0;
  1634. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  1635. ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
  1636. } else {
  1637. mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
  1638. mad_recv_wc);
  1639. deref_mad_agent(mad_agent_priv);
  1640. }
  1641. }
  1642. static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
  1643. struct ib_wc *wc)
  1644. {
  1645. struct ib_mad_qp_info *qp_info;
  1646. struct ib_mad_private_header *mad_priv_hdr;
  1647. struct ib_mad_private *recv, *response = NULL;
  1648. struct ib_mad_list_head *mad_list;
  1649. struct ib_mad_agent_private *mad_agent;
  1650. int port_num;
  1651. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1652. qp_info = mad_list->mad_queue->qp_info;
  1653. dequeue_mad(mad_list);
  1654. mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
  1655. mad_list);
  1656. recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
  1657. ib_dma_unmap_single(port_priv->device,
  1658. recv->header.mapping,
  1659. sizeof(struct ib_mad_private) -
  1660. sizeof(struct ib_mad_private_header),
  1661. DMA_FROM_DEVICE);
  1662. /* Setup MAD receive work completion from "normal" work completion */
  1663. recv->header.wc = *wc;
  1664. recv->header.recv_wc.wc = &recv->header.wc;
  1665. recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
  1666. recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
  1667. recv->header.recv_wc.recv_buf.grh = &recv->grh;
  1668. if (atomic_read(&qp_info->snoop_count))
  1669. snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
  1670. /* Validate MAD */
  1671. if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
  1672. goto out;
  1673. response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
  1674. if (!response) {
  1675. printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
  1676. "for response buffer\n");
  1677. goto out;
  1678. }
  1679. if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
  1680. port_num = wc->port_num;
  1681. else
  1682. port_num = port_priv->port_num;
  1683. if (recv->mad.mad.mad_hdr.mgmt_class ==
  1684. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  1685. enum smi_forward_action retsmi;
  1686. if (smi_handle_dr_smp_recv(&recv->mad.smp,
  1687. port_priv->device->node_type,
  1688. port_num,
  1689. port_priv->device->phys_port_cnt) ==
  1690. IB_SMI_DISCARD)
  1691. goto out;
  1692. retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
  1693. if (retsmi == IB_SMI_LOCAL)
  1694. goto local;
  1695. if (retsmi == IB_SMI_SEND) { /* don't forward */
  1696. if (smi_handle_dr_smp_send(&recv->mad.smp,
  1697. port_priv->device->node_type,
  1698. port_num) == IB_SMI_DISCARD)
  1699. goto out;
  1700. if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
  1701. goto out;
  1702. } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
  1703. /* forward case for switches */
  1704. memcpy(response, recv, sizeof(*response));
  1705. response->header.recv_wc.wc = &response->header.wc;
  1706. response->header.recv_wc.recv_buf.mad = &response->mad.mad;
  1707. response->header.recv_wc.recv_buf.grh = &response->grh;
  1708. agent_send_response(&response->mad.mad,
  1709. &response->grh, wc,
  1710. port_priv->device,
  1711. smi_get_fwd_port(&recv->mad.smp),
  1712. qp_info->qp->qp_num);
  1713. goto out;
  1714. }
  1715. }
  1716. local:
  1717. /* Give driver "right of first refusal" on incoming MAD */
  1718. if (port_priv->device->process_mad) {
  1719. int ret;
  1720. if (!response) {
  1721. printk(KERN_ERR PFX "No memory for response MAD\n");
  1722. /*
  1723. * Is it better to assume that
  1724. * it wouldn't be processed ?
  1725. */
  1726. goto out;
  1727. }
  1728. ret = port_priv->device->process_mad(port_priv->device, 0,
  1729. port_priv->port_num,
  1730. wc, &recv->grh,
  1731. &recv->mad.mad,
  1732. &response->mad.mad);
  1733. if (ret & IB_MAD_RESULT_SUCCESS) {
  1734. if (ret & IB_MAD_RESULT_CONSUMED)
  1735. goto out;
  1736. if (ret & IB_MAD_RESULT_REPLY) {
  1737. agent_send_response(&response->mad.mad,
  1738. &recv->grh, wc,
  1739. port_priv->device,
  1740. port_num,
  1741. qp_info->qp->qp_num);
  1742. goto out;
  1743. }
  1744. }
  1745. }
  1746. mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
  1747. if (mad_agent) {
  1748. ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
  1749. /*
  1750. * recv is freed up in error cases in ib_mad_complete_recv
  1751. * or via recv_handler in ib_mad_complete_recv()
  1752. */
  1753. recv = NULL;
  1754. }
  1755. out:
  1756. /* Post another receive request for this QP */
  1757. if (response) {
  1758. ib_mad_post_receive_mads(qp_info, response);
  1759. if (recv)
  1760. kmem_cache_free(ib_mad_cache, recv);
  1761. } else
  1762. ib_mad_post_receive_mads(qp_info, recv);
  1763. }
  1764. static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
  1765. {
  1766. struct ib_mad_send_wr_private *mad_send_wr;
  1767. unsigned long delay;
  1768. if (list_empty(&mad_agent_priv->wait_list)) {
  1769. cancel_delayed_work(&mad_agent_priv->timed_work);
  1770. } else {
  1771. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  1772. struct ib_mad_send_wr_private,
  1773. agent_list);
  1774. if (time_after(mad_agent_priv->timeout,
  1775. mad_send_wr->timeout)) {
  1776. mad_agent_priv->timeout = mad_send_wr->timeout;
  1777. cancel_delayed_work(&mad_agent_priv->timed_work);
  1778. delay = mad_send_wr->timeout - jiffies;
  1779. if ((long)delay <= 0)
  1780. delay = 1;
  1781. queue_delayed_work(mad_agent_priv->qp_info->
  1782. port_priv->wq,
  1783. &mad_agent_priv->timed_work, delay);
  1784. }
  1785. }
  1786. }
  1787. static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
  1788. {
  1789. struct ib_mad_agent_private *mad_agent_priv;
  1790. struct ib_mad_send_wr_private *temp_mad_send_wr;
  1791. struct list_head *list_item;
  1792. unsigned long delay;
  1793. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1794. list_del(&mad_send_wr->agent_list);
  1795. delay = mad_send_wr->timeout;
  1796. mad_send_wr->timeout += jiffies;
  1797. if (delay) {
  1798. list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
  1799. temp_mad_send_wr = list_entry(list_item,
  1800. struct ib_mad_send_wr_private,
  1801. agent_list);
  1802. if (time_after(mad_send_wr->timeout,
  1803. temp_mad_send_wr->timeout))
  1804. break;
  1805. }
  1806. }
  1807. else
  1808. list_item = &mad_agent_priv->wait_list;
  1809. list_add(&mad_send_wr->agent_list, list_item);
  1810. /* Reschedule a work item if we have a shorter timeout */
  1811. if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
  1812. cancel_delayed_work(&mad_agent_priv->timed_work);
  1813. queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
  1814. &mad_agent_priv->timed_work, delay);
  1815. }
  1816. }
  1817. void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
  1818. int timeout_ms)
  1819. {
  1820. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  1821. wait_for_response(mad_send_wr);
  1822. }
  1823. /*
  1824. * Process a send work completion
  1825. */
  1826. void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
  1827. struct ib_mad_send_wc *mad_send_wc)
  1828. {
  1829. struct ib_mad_agent_private *mad_agent_priv;
  1830. unsigned long flags;
  1831. int ret;
  1832. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1833. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1834. if (mad_agent_priv->agent.rmpp_version) {
  1835. ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
  1836. if (ret == IB_RMPP_RESULT_CONSUMED)
  1837. goto done;
  1838. } else
  1839. ret = IB_RMPP_RESULT_UNHANDLED;
  1840. if (mad_send_wc->status != IB_WC_SUCCESS &&
  1841. mad_send_wr->status == IB_WC_SUCCESS) {
  1842. mad_send_wr->status = mad_send_wc->status;
  1843. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  1844. }
  1845. if (--mad_send_wr->refcount > 0) {
  1846. if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
  1847. mad_send_wr->status == IB_WC_SUCCESS) {
  1848. wait_for_response(mad_send_wr);
  1849. }
  1850. goto done;
  1851. }
  1852. /* Remove send from MAD agent and notify client of completion */
  1853. list_del(&mad_send_wr->agent_list);
  1854. adjust_timeout(mad_agent_priv);
  1855. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1856. if (mad_send_wr->status != IB_WC_SUCCESS )
  1857. mad_send_wc->status = mad_send_wr->status;
  1858. if (ret == IB_RMPP_RESULT_INTERNAL)
  1859. ib_rmpp_send_handler(mad_send_wc);
  1860. else
  1861. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  1862. mad_send_wc);
  1863. /* Release reference on agent taken when sending */
  1864. deref_mad_agent(mad_agent_priv);
  1865. return;
  1866. done:
  1867. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1868. }
  1869. static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
  1870. struct ib_wc *wc)
  1871. {
  1872. struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
  1873. struct ib_mad_list_head *mad_list;
  1874. struct ib_mad_qp_info *qp_info;
  1875. struct ib_mad_queue *send_queue;
  1876. struct ib_send_wr *bad_send_wr;
  1877. struct ib_mad_send_wc mad_send_wc;
  1878. unsigned long flags;
  1879. int ret;
  1880. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1881. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1882. mad_list);
  1883. send_queue = mad_list->mad_queue;
  1884. qp_info = send_queue->qp_info;
  1885. retry:
  1886. ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
  1887. mad_send_wr->header_mapping,
  1888. mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
  1889. ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
  1890. mad_send_wr->payload_mapping,
  1891. mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
  1892. queued_send_wr = NULL;
  1893. spin_lock_irqsave(&send_queue->lock, flags);
  1894. list_del(&mad_list->list);
  1895. /* Move queued send to the send queue */
  1896. if (send_queue->count-- > send_queue->max_active) {
  1897. mad_list = container_of(qp_info->overflow_list.next,
  1898. struct ib_mad_list_head, list);
  1899. queued_send_wr = container_of(mad_list,
  1900. struct ib_mad_send_wr_private,
  1901. mad_list);
  1902. list_move_tail(&mad_list->list, &send_queue->list);
  1903. }
  1904. spin_unlock_irqrestore(&send_queue->lock, flags);
  1905. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  1906. mad_send_wc.status = wc->status;
  1907. mad_send_wc.vendor_err = wc->vendor_err;
  1908. if (atomic_read(&qp_info->snoop_count))
  1909. snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
  1910. IB_MAD_SNOOP_SEND_COMPLETIONS);
  1911. ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
  1912. if (queued_send_wr) {
  1913. ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
  1914. &bad_send_wr);
  1915. if (ret) {
  1916. printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
  1917. mad_send_wr = queued_send_wr;
  1918. wc->status = IB_WC_LOC_QP_OP_ERR;
  1919. goto retry;
  1920. }
  1921. }
  1922. }
  1923. static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
  1924. {
  1925. struct ib_mad_send_wr_private *mad_send_wr;
  1926. struct ib_mad_list_head *mad_list;
  1927. unsigned long flags;
  1928. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  1929. list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
  1930. mad_send_wr = container_of(mad_list,
  1931. struct ib_mad_send_wr_private,
  1932. mad_list);
  1933. mad_send_wr->retry = 1;
  1934. }
  1935. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  1936. }
  1937. static void mad_error_handler(struct ib_mad_port_private *port_priv,
  1938. struct ib_wc *wc)
  1939. {
  1940. struct ib_mad_list_head *mad_list;
  1941. struct ib_mad_qp_info *qp_info;
  1942. struct ib_mad_send_wr_private *mad_send_wr;
  1943. int ret;
  1944. /* Determine if failure was a send or receive */
  1945. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1946. qp_info = mad_list->mad_queue->qp_info;
  1947. if (mad_list->mad_queue == &qp_info->recv_queue)
  1948. /*
  1949. * Receive errors indicate that the QP has entered the error
  1950. * state - error handling/shutdown code will cleanup
  1951. */
  1952. return;
  1953. /*
  1954. * Send errors will transition the QP to SQE - move
  1955. * QP to RTS and repost flushed work requests
  1956. */
  1957. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1958. mad_list);
  1959. if (wc->status == IB_WC_WR_FLUSH_ERR) {
  1960. if (mad_send_wr->retry) {
  1961. /* Repost send */
  1962. struct ib_send_wr *bad_send_wr;
  1963. mad_send_wr->retry = 0;
  1964. ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
  1965. &bad_send_wr);
  1966. if (ret)
  1967. ib_mad_send_done_handler(port_priv, wc);
  1968. } else
  1969. ib_mad_send_done_handler(port_priv, wc);
  1970. } else {
  1971. struct ib_qp_attr *attr;
  1972. /* Transition QP to RTS and fail offending send */
  1973. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1974. if (attr) {
  1975. attr->qp_state = IB_QPS_RTS;
  1976. attr->cur_qp_state = IB_QPS_SQE;
  1977. ret = ib_modify_qp(qp_info->qp, attr,
  1978. IB_QP_STATE | IB_QP_CUR_STATE);
  1979. kfree(attr);
  1980. if (ret)
  1981. printk(KERN_ERR PFX "mad_error_handler - "
  1982. "ib_modify_qp to RTS : %d\n", ret);
  1983. else
  1984. mark_sends_for_retry(qp_info);
  1985. }
  1986. ib_mad_send_done_handler(port_priv, wc);
  1987. }
  1988. }
  1989. /*
  1990. * IB MAD completion callback
  1991. */
  1992. static void ib_mad_completion_handler(struct work_struct *work)
  1993. {
  1994. struct ib_mad_port_private *port_priv;
  1995. struct ib_wc wc;
  1996. port_priv = container_of(work, struct ib_mad_port_private, work);
  1997. ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  1998. while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
  1999. if (wc.status == IB_WC_SUCCESS) {
  2000. switch (wc.opcode) {
  2001. case IB_WC_SEND:
  2002. ib_mad_send_done_handler(port_priv, &wc);
  2003. break;
  2004. case IB_WC_RECV:
  2005. ib_mad_recv_done_handler(port_priv, &wc);
  2006. break;
  2007. default:
  2008. BUG_ON(1);
  2009. break;
  2010. }
  2011. } else
  2012. mad_error_handler(port_priv, &wc);
  2013. }
  2014. }
  2015. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
  2016. {
  2017. unsigned long flags;
  2018. struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
  2019. struct ib_mad_send_wc mad_send_wc;
  2020. struct list_head cancel_list;
  2021. INIT_LIST_HEAD(&cancel_list);
  2022. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2023. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  2024. &mad_agent_priv->send_list, agent_list) {
  2025. if (mad_send_wr->status == IB_WC_SUCCESS) {
  2026. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  2027. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  2028. }
  2029. }
  2030. /* Empty wait list to prevent receives from finding a request */
  2031. list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
  2032. /* Empty local completion list as well */
  2033. list_splice_init(&mad_agent_priv->local_list, &cancel_list);
  2034. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2035. /* Report all cancelled requests */
  2036. mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
  2037. mad_send_wc.vendor_err = 0;
  2038. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  2039. &cancel_list, agent_list) {
  2040. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2041. list_del(&mad_send_wr->agent_list);
  2042. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2043. &mad_send_wc);
  2044. atomic_dec(&mad_agent_priv->refcount);
  2045. }
  2046. }
  2047. static struct ib_mad_send_wr_private*
  2048. find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
  2049. struct ib_mad_send_buf *send_buf)
  2050. {
  2051. struct ib_mad_send_wr_private *mad_send_wr;
  2052. list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
  2053. agent_list) {
  2054. if (&mad_send_wr->send_buf == send_buf)
  2055. return mad_send_wr;
  2056. }
  2057. list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
  2058. agent_list) {
  2059. if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
  2060. &mad_send_wr->send_buf == send_buf)
  2061. return mad_send_wr;
  2062. }
  2063. return NULL;
  2064. }
  2065. int ib_modify_mad(struct ib_mad_agent *mad_agent,
  2066. struct ib_mad_send_buf *send_buf, u32 timeout_ms)
  2067. {
  2068. struct ib_mad_agent_private *mad_agent_priv;
  2069. struct ib_mad_send_wr_private *mad_send_wr;
  2070. unsigned long flags;
  2071. int active;
  2072. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  2073. agent);
  2074. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2075. mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
  2076. if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
  2077. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2078. return -EINVAL;
  2079. }
  2080. active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
  2081. if (!timeout_ms) {
  2082. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  2083. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  2084. }
  2085. mad_send_wr->send_buf.timeout_ms = timeout_ms;
  2086. if (active)
  2087. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  2088. else
  2089. ib_reset_mad_timeout(mad_send_wr, timeout_ms);
  2090. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2091. return 0;
  2092. }
  2093. EXPORT_SYMBOL(ib_modify_mad);
  2094. void ib_cancel_mad(struct ib_mad_agent *mad_agent,
  2095. struct ib_mad_send_buf *send_buf)
  2096. {
  2097. ib_modify_mad(mad_agent, send_buf, 0);
  2098. }
  2099. EXPORT_SYMBOL(ib_cancel_mad);
  2100. static void local_completions(struct work_struct *work)
  2101. {
  2102. struct ib_mad_agent_private *mad_agent_priv;
  2103. struct ib_mad_local_private *local;
  2104. struct ib_mad_agent_private *recv_mad_agent;
  2105. unsigned long flags;
  2106. int recv = 0;
  2107. struct ib_wc wc;
  2108. struct ib_mad_send_wc mad_send_wc;
  2109. mad_agent_priv =
  2110. container_of(work, struct ib_mad_agent_private, local_work);
  2111. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2112. while (!list_empty(&mad_agent_priv->local_list)) {
  2113. local = list_entry(mad_agent_priv->local_list.next,
  2114. struct ib_mad_local_private,
  2115. completion_list);
  2116. list_del(&local->completion_list);
  2117. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2118. if (local->mad_priv) {
  2119. recv_mad_agent = local->recv_mad_agent;
  2120. if (!recv_mad_agent) {
  2121. printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
  2122. goto local_send_completion;
  2123. }
  2124. recv = 1;
  2125. /*
  2126. * Defined behavior is to complete response
  2127. * before request
  2128. */
  2129. build_smp_wc(recv_mad_agent->agent.qp,
  2130. (unsigned long) local->mad_send_wr,
  2131. be16_to_cpu(IB_LID_PERMISSIVE),
  2132. 0, recv_mad_agent->agent.port_num, &wc);
  2133. local->mad_priv->header.recv_wc.wc = &wc;
  2134. local->mad_priv->header.recv_wc.mad_len =
  2135. sizeof(struct ib_mad);
  2136. INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
  2137. list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
  2138. &local->mad_priv->header.recv_wc.rmpp_list);
  2139. local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
  2140. local->mad_priv->header.recv_wc.recv_buf.mad =
  2141. &local->mad_priv->mad.mad;
  2142. if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
  2143. snoop_recv(recv_mad_agent->qp_info,
  2144. &local->mad_priv->header.recv_wc,
  2145. IB_MAD_SNOOP_RECVS);
  2146. recv_mad_agent->agent.recv_handler(
  2147. &recv_mad_agent->agent,
  2148. &local->mad_priv->header.recv_wc);
  2149. spin_lock_irqsave(&recv_mad_agent->lock, flags);
  2150. atomic_dec(&recv_mad_agent->refcount);
  2151. spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
  2152. }
  2153. local_send_completion:
  2154. /* Complete send */
  2155. mad_send_wc.status = IB_WC_SUCCESS;
  2156. mad_send_wc.vendor_err = 0;
  2157. mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
  2158. if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
  2159. snoop_send(mad_agent_priv->qp_info,
  2160. &local->mad_send_wr->send_buf,
  2161. &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
  2162. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2163. &mad_send_wc);
  2164. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2165. atomic_dec(&mad_agent_priv->refcount);
  2166. if (!recv)
  2167. kmem_cache_free(ib_mad_cache, local->mad_priv);
  2168. kfree(local);
  2169. }
  2170. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2171. }
  2172. static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
  2173. {
  2174. int ret;
  2175. if (!mad_send_wr->retries--)
  2176. return -ETIMEDOUT;
  2177. mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
  2178. if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
  2179. ret = ib_retry_rmpp(mad_send_wr);
  2180. switch (ret) {
  2181. case IB_RMPP_RESULT_UNHANDLED:
  2182. ret = ib_send_mad(mad_send_wr);
  2183. break;
  2184. case IB_RMPP_RESULT_CONSUMED:
  2185. ret = 0;
  2186. break;
  2187. default:
  2188. ret = -ECOMM;
  2189. break;
  2190. }
  2191. } else
  2192. ret = ib_send_mad(mad_send_wr);
  2193. if (!ret) {
  2194. mad_send_wr->refcount++;
  2195. list_add_tail(&mad_send_wr->agent_list,
  2196. &mad_send_wr->mad_agent_priv->send_list);
  2197. }
  2198. return ret;
  2199. }
  2200. static void timeout_sends(struct work_struct *work)
  2201. {
  2202. struct ib_mad_agent_private *mad_agent_priv;
  2203. struct ib_mad_send_wr_private *mad_send_wr;
  2204. struct ib_mad_send_wc mad_send_wc;
  2205. unsigned long flags, delay;
  2206. mad_agent_priv = container_of(work, struct ib_mad_agent_private,
  2207. timed_work.work);
  2208. mad_send_wc.vendor_err = 0;
  2209. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2210. while (!list_empty(&mad_agent_priv->wait_list)) {
  2211. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  2212. struct ib_mad_send_wr_private,
  2213. agent_list);
  2214. if (time_after(mad_send_wr->timeout, jiffies)) {
  2215. delay = mad_send_wr->timeout - jiffies;
  2216. if ((long)delay <= 0)
  2217. delay = 1;
  2218. queue_delayed_work(mad_agent_priv->qp_info->
  2219. port_priv->wq,
  2220. &mad_agent_priv->timed_work, delay);
  2221. break;
  2222. }
  2223. list_del(&mad_send_wr->agent_list);
  2224. if (mad_send_wr->status == IB_WC_SUCCESS &&
  2225. !retry_send(mad_send_wr))
  2226. continue;
  2227. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2228. if (mad_send_wr->status == IB_WC_SUCCESS)
  2229. mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
  2230. else
  2231. mad_send_wc.status = mad_send_wr->status;
  2232. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2233. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2234. &mad_send_wc);
  2235. atomic_dec(&mad_agent_priv->refcount);
  2236. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2237. }
  2238. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2239. }
  2240. static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
  2241. {
  2242. struct ib_mad_port_private *port_priv = cq->cq_context;
  2243. unsigned long flags;
  2244. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2245. if (!list_empty(&port_priv->port_list))
  2246. queue_work(port_priv->wq, &port_priv->work);
  2247. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2248. }
  2249. /*
  2250. * Allocate receive MADs and post receive WRs for them
  2251. */
  2252. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  2253. struct ib_mad_private *mad)
  2254. {
  2255. unsigned long flags;
  2256. int post, ret;
  2257. struct ib_mad_private *mad_priv;
  2258. struct ib_sge sg_list;
  2259. struct ib_recv_wr recv_wr, *bad_recv_wr;
  2260. struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
  2261. /* Initialize common scatter list fields */
  2262. sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
  2263. sg_list.lkey = (*qp_info->port_priv->mr).lkey;
  2264. /* Initialize common receive WR fields */
  2265. recv_wr.next = NULL;
  2266. recv_wr.sg_list = &sg_list;
  2267. recv_wr.num_sge = 1;
  2268. do {
  2269. /* Allocate and map receive buffer */
  2270. if (mad) {
  2271. mad_priv = mad;
  2272. mad = NULL;
  2273. } else {
  2274. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
  2275. if (!mad_priv) {
  2276. printk(KERN_ERR PFX "No memory for receive buffer\n");
  2277. ret = -ENOMEM;
  2278. break;
  2279. }
  2280. }
  2281. sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
  2282. &mad_priv->grh,
  2283. sizeof *mad_priv -
  2284. sizeof mad_priv->header,
  2285. DMA_FROM_DEVICE);
  2286. mad_priv->header.mapping = sg_list.addr;
  2287. recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
  2288. mad_priv->header.mad_list.mad_queue = recv_queue;
  2289. /* Post receive WR */
  2290. spin_lock_irqsave(&recv_queue->lock, flags);
  2291. post = (++recv_queue->count < recv_queue->max_active);
  2292. list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
  2293. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2294. ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
  2295. if (ret) {
  2296. spin_lock_irqsave(&recv_queue->lock, flags);
  2297. list_del(&mad_priv->header.mad_list.list);
  2298. recv_queue->count--;
  2299. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2300. ib_dma_unmap_single(qp_info->port_priv->device,
  2301. mad_priv->header.mapping,
  2302. sizeof *mad_priv -
  2303. sizeof mad_priv->header,
  2304. DMA_FROM_DEVICE);
  2305. kmem_cache_free(ib_mad_cache, mad_priv);
  2306. printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
  2307. break;
  2308. }
  2309. } while (post);
  2310. return ret;
  2311. }
  2312. /*
  2313. * Return all the posted receive MADs
  2314. */
  2315. static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
  2316. {
  2317. struct ib_mad_private_header *mad_priv_hdr;
  2318. struct ib_mad_private *recv;
  2319. struct ib_mad_list_head *mad_list;
  2320. while (!list_empty(&qp_info->recv_queue.list)) {
  2321. mad_list = list_entry(qp_info->recv_queue.list.next,
  2322. struct ib_mad_list_head, list);
  2323. mad_priv_hdr = container_of(mad_list,
  2324. struct ib_mad_private_header,
  2325. mad_list);
  2326. recv = container_of(mad_priv_hdr, struct ib_mad_private,
  2327. header);
  2328. /* Remove from posted receive MAD list */
  2329. list_del(&mad_list->list);
  2330. ib_dma_unmap_single(qp_info->port_priv->device,
  2331. recv->header.mapping,
  2332. sizeof(struct ib_mad_private) -
  2333. sizeof(struct ib_mad_private_header),
  2334. DMA_FROM_DEVICE);
  2335. kmem_cache_free(ib_mad_cache, recv);
  2336. }
  2337. qp_info->recv_queue.count = 0;
  2338. }
  2339. /*
  2340. * Start the port
  2341. */
  2342. static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
  2343. {
  2344. int ret, i;
  2345. struct ib_qp_attr *attr;
  2346. struct ib_qp *qp;
  2347. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  2348. if (!attr) {
  2349. printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
  2350. return -ENOMEM;
  2351. }
  2352. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2353. qp = port_priv->qp_info[i].qp;
  2354. /*
  2355. * PKey index for QP1 is irrelevant but
  2356. * one is needed for the Reset to Init transition
  2357. */
  2358. attr->qp_state = IB_QPS_INIT;
  2359. attr->pkey_index = 0;
  2360. attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
  2361. ret = ib_modify_qp(qp, attr, IB_QP_STATE |
  2362. IB_QP_PKEY_INDEX | IB_QP_QKEY);
  2363. if (ret) {
  2364. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2365. "INIT: %d\n", i, ret);
  2366. goto out;
  2367. }
  2368. attr->qp_state = IB_QPS_RTR;
  2369. ret = ib_modify_qp(qp, attr, IB_QP_STATE);
  2370. if (ret) {
  2371. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2372. "RTR: %d\n", i, ret);
  2373. goto out;
  2374. }
  2375. attr->qp_state = IB_QPS_RTS;
  2376. attr->sq_psn = IB_MAD_SEND_Q_PSN;
  2377. ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
  2378. if (ret) {
  2379. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2380. "RTS: %d\n", i, ret);
  2381. goto out;
  2382. }
  2383. }
  2384. ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  2385. if (ret) {
  2386. printk(KERN_ERR PFX "Failed to request completion "
  2387. "notification: %d\n", ret);
  2388. goto out;
  2389. }
  2390. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2391. ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
  2392. if (ret) {
  2393. printk(KERN_ERR PFX "Couldn't post receive WRs\n");
  2394. goto out;
  2395. }
  2396. }
  2397. out:
  2398. kfree(attr);
  2399. return ret;
  2400. }
  2401. static void qp_event_handler(struct ib_event *event, void *qp_context)
  2402. {
  2403. struct ib_mad_qp_info *qp_info = qp_context;
  2404. /* It's worse than that! He's dead, Jim! */
  2405. printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
  2406. event->event, qp_info->qp->qp_num);
  2407. }
  2408. static void init_mad_queue(struct ib_mad_qp_info *qp_info,
  2409. struct ib_mad_queue *mad_queue)
  2410. {
  2411. mad_queue->qp_info = qp_info;
  2412. mad_queue->count = 0;
  2413. spin_lock_init(&mad_queue->lock);
  2414. INIT_LIST_HEAD(&mad_queue->list);
  2415. }
  2416. static void init_mad_qp(struct ib_mad_port_private *port_priv,
  2417. struct ib_mad_qp_info *qp_info)
  2418. {
  2419. qp_info->port_priv = port_priv;
  2420. init_mad_queue(qp_info, &qp_info->send_queue);
  2421. init_mad_queue(qp_info, &qp_info->recv_queue);
  2422. INIT_LIST_HEAD(&qp_info->overflow_list);
  2423. spin_lock_init(&qp_info->snoop_lock);
  2424. qp_info->snoop_table = NULL;
  2425. qp_info->snoop_table_size = 0;
  2426. atomic_set(&qp_info->snoop_count, 0);
  2427. }
  2428. static int create_mad_qp(struct ib_mad_qp_info *qp_info,
  2429. enum ib_qp_type qp_type)
  2430. {
  2431. struct ib_qp_init_attr qp_init_attr;
  2432. int ret;
  2433. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  2434. qp_init_attr.send_cq = qp_info->port_priv->cq;
  2435. qp_init_attr.recv_cq = qp_info->port_priv->cq;
  2436. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  2437. qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
  2438. qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
  2439. qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
  2440. qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
  2441. qp_init_attr.qp_type = qp_type;
  2442. qp_init_attr.port_num = qp_info->port_priv->port_num;
  2443. qp_init_attr.qp_context = qp_info;
  2444. qp_init_attr.event_handler = qp_event_handler;
  2445. qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
  2446. if (IS_ERR(qp_info->qp)) {
  2447. printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
  2448. get_spl_qp_index(qp_type));
  2449. ret = PTR_ERR(qp_info->qp);
  2450. goto error;
  2451. }
  2452. /* Use minimum queue sizes unless the CQ is resized */
  2453. qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
  2454. qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
  2455. return 0;
  2456. error:
  2457. return ret;
  2458. }
  2459. static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
  2460. {
  2461. ib_destroy_qp(qp_info->qp);
  2462. kfree(qp_info->snoop_table);
  2463. }
  2464. /*
  2465. * Open the port
  2466. * Create the QP, PD, MR, and CQ if needed
  2467. */
  2468. static int ib_mad_port_open(struct ib_device *device,
  2469. int port_num)
  2470. {
  2471. int ret, cq_size;
  2472. struct ib_mad_port_private *port_priv;
  2473. unsigned long flags;
  2474. char name[sizeof "ib_mad123"];
  2475. /* Create new device info */
  2476. port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
  2477. if (!port_priv) {
  2478. printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
  2479. return -ENOMEM;
  2480. }
  2481. port_priv->device = device;
  2482. port_priv->port_num = port_num;
  2483. spin_lock_init(&port_priv->reg_lock);
  2484. INIT_LIST_HEAD(&port_priv->agent_list);
  2485. init_mad_qp(port_priv, &port_priv->qp_info[0]);
  2486. init_mad_qp(port_priv, &port_priv->qp_info[1]);
  2487. cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
  2488. port_priv->cq = ib_create_cq(port_priv->device,
  2489. ib_mad_thread_completion_handler,
  2490. NULL, port_priv, cq_size, 0);
  2491. if (IS_ERR(port_priv->cq)) {
  2492. printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
  2493. ret = PTR_ERR(port_priv->cq);
  2494. goto error3;
  2495. }
  2496. port_priv->pd = ib_alloc_pd(device);
  2497. if (IS_ERR(port_priv->pd)) {
  2498. printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
  2499. ret = PTR_ERR(port_priv->pd);
  2500. goto error4;
  2501. }
  2502. port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
  2503. if (IS_ERR(port_priv->mr)) {
  2504. printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
  2505. ret = PTR_ERR(port_priv->mr);
  2506. goto error5;
  2507. }
  2508. ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
  2509. if (ret)
  2510. goto error6;
  2511. ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
  2512. if (ret)
  2513. goto error7;
  2514. snprintf(name, sizeof name, "ib_mad%d", port_num);
  2515. port_priv->wq = create_singlethread_workqueue(name);
  2516. if (!port_priv->wq) {
  2517. ret = -ENOMEM;
  2518. goto error8;
  2519. }
  2520. INIT_WORK(&port_priv->work, ib_mad_completion_handler);
  2521. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2522. list_add_tail(&port_priv->port_list, &ib_mad_port_list);
  2523. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2524. ret = ib_mad_port_start(port_priv);
  2525. if (ret) {
  2526. printk(KERN_ERR PFX "Couldn't start port\n");
  2527. goto error9;
  2528. }
  2529. return 0;
  2530. error9:
  2531. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2532. list_del_init(&port_priv->port_list);
  2533. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2534. destroy_workqueue(port_priv->wq);
  2535. error8:
  2536. destroy_mad_qp(&port_priv->qp_info[1]);
  2537. error7:
  2538. destroy_mad_qp(&port_priv->qp_info[0]);
  2539. error6:
  2540. ib_dereg_mr(port_priv->mr);
  2541. error5:
  2542. ib_dealloc_pd(port_priv->pd);
  2543. error4:
  2544. ib_destroy_cq(port_priv->cq);
  2545. cleanup_recv_queue(&port_priv->qp_info[1]);
  2546. cleanup_recv_queue(&port_priv->qp_info[0]);
  2547. error3:
  2548. kfree(port_priv);
  2549. return ret;
  2550. }
  2551. /*
  2552. * Close the port
  2553. * If there are no classes using the port, free the port
  2554. * resources (CQ, MR, PD, QP) and remove the port's info structure
  2555. */
  2556. static int ib_mad_port_close(struct ib_device *device, int port_num)
  2557. {
  2558. struct ib_mad_port_private *port_priv;
  2559. unsigned long flags;
  2560. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2561. port_priv = __ib_get_mad_port(device, port_num);
  2562. if (port_priv == NULL) {
  2563. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2564. printk(KERN_ERR PFX "Port %d not found\n", port_num);
  2565. return -ENODEV;
  2566. }
  2567. list_del_init(&port_priv->port_list);
  2568. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2569. destroy_workqueue(port_priv->wq);
  2570. destroy_mad_qp(&port_priv->qp_info[1]);
  2571. destroy_mad_qp(&port_priv->qp_info[0]);
  2572. ib_dereg_mr(port_priv->mr);
  2573. ib_dealloc_pd(port_priv->pd);
  2574. ib_destroy_cq(port_priv->cq);
  2575. cleanup_recv_queue(&port_priv->qp_info[1]);
  2576. cleanup_recv_queue(&port_priv->qp_info[0]);
  2577. /* XXX: Handle deallocation of MAD registration tables */
  2578. kfree(port_priv);
  2579. return 0;
  2580. }
  2581. static void ib_mad_init_device(struct ib_device *device)
  2582. {
  2583. int start, end, i;
  2584. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  2585. return;
  2586. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2587. start = 0;
  2588. end = 0;
  2589. } else {
  2590. start = 1;
  2591. end = device->phys_port_cnt;
  2592. }
  2593. for (i = start; i <= end; i++) {
  2594. if (ib_mad_port_open(device, i)) {
  2595. printk(KERN_ERR PFX "Couldn't open %s port %d\n",
  2596. device->name, i);
  2597. goto error;
  2598. }
  2599. if (ib_agent_port_open(device, i)) {
  2600. printk(KERN_ERR PFX "Couldn't open %s port %d "
  2601. "for agents\n",
  2602. device->name, i);
  2603. goto error_agent;
  2604. }
  2605. }
  2606. return;
  2607. error_agent:
  2608. if (ib_mad_port_close(device, i))
  2609. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2610. device->name, i);
  2611. error:
  2612. i--;
  2613. while (i >= start) {
  2614. if (ib_agent_port_close(device, i))
  2615. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2616. "for agents\n",
  2617. device->name, i);
  2618. if (ib_mad_port_close(device, i))
  2619. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2620. device->name, i);
  2621. i--;
  2622. }
  2623. }
  2624. static void ib_mad_remove_device(struct ib_device *device)
  2625. {
  2626. int i, num_ports, cur_port;
  2627. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2628. num_ports = 1;
  2629. cur_port = 0;
  2630. } else {
  2631. num_ports = device->phys_port_cnt;
  2632. cur_port = 1;
  2633. }
  2634. for (i = 0; i < num_ports; i++, cur_port++) {
  2635. if (ib_agent_port_close(device, cur_port))
  2636. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2637. "for agents\n",
  2638. device->name, cur_port);
  2639. if (ib_mad_port_close(device, cur_port))
  2640. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2641. device->name, cur_port);
  2642. }
  2643. }
  2644. static struct ib_client mad_client = {
  2645. .name = "mad",
  2646. .add = ib_mad_init_device,
  2647. .remove = ib_mad_remove_device
  2648. };
  2649. static int __init ib_mad_init_module(void)
  2650. {
  2651. int ret;
  2652. spin_lock_init(&ib_mad_port_list_lock);
  2653. ib_mad_cache = kmem_cache_create("ib_mad",
  2654. sizeof(struct ib_mad_private),
  2655. 0,
  2656. SLAB_HWCACHE_ALIGN,
  2657. NULL);
  2658. if (!ib_mad_cache) {
  2659. printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
  2660. ret = -ENOMEM;
  2661. goto error1;
  2662. }
  2663. INIT_LIST_HEAD(&ib_mad_port_list);
  2664. if (ib_register_client(&mad_client)) {
  2665. printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
  2666. ret = -EINVAL;
  2667. goto error2;
  2668. }
  2669. return 0;
  2670. error2:
  2671. kmem_cache_destroy(ib_mad_cache);
  2672. error1:
  2673. return ret;
  2674. }
  2675. static void __exit ib_mad_cleanup_module(void)
  2676. {
  2677. ib_unregister_client(&mad_client);
  2678. kmem_cache_destroy(ib_mad_cache);
  2679. }
  2680. module_init(ib_mad_init_module);
  2681. module_exit(ib_mad_cleanup_module);