mad.c 84 KB

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