mad.c 83 KB

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