mad.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  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(struct ib_qp *qp,
  577. u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
  578. struct ib_wc *wc)
  579. {
  580. memset(wc, 0, sizeof *wc);
  581. wc->wr_id = wr_id;
  582. wc->status = IB_WC_SUCCESS;
  583. wc->opcode = IB_WC_RECV;
  584. wc->pkey_index = pkey_index;
  585. wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
  586. wc->src_qp = IB_QP0;
  587. wc->qp = qp;
  588. wc->slid = slid;
  589. wc->sl = 0;
  590. wc->dlid_path_bits = 0;
  591. wc->port_num = port_num;
  592. }
  593. /*
  594. * Return 0 if SMP is to be sent
  595. * Return 1 if SMP was consumed locally (whether or not solicited)
  596. * Return < 0 if error
  597. */
  598. static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
  599. struct ib_mad_send_wr_private *mad_send_wr)
  600. {
  601. int ret;
  602. struct ib_smp *smp = mad_send_wr->send_buf.mad;
  603. unsigned long flags;
  604. struct ib_mad_local_private *local;
  605. struct ib_mad_private *mad_priv;
  606. struct ib_mad_port_private *port_priv;
  607. struct ib_mad_agent_private *recv_mad_agent = NULL;
  608. struct ib_device *device = mad_agent_priv->agent.device;
  609. u8 port_num = mad_agent_priv->agent.port_num;
  610. struct ib_wc mad_wc;
  611. struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
  612. /*
  613. * Directed route handling starts if the initial LID routed part of
  614. * a request or the ending LID routed part of a response is empty.
  615. * If we are at the start of the LID routed part, don't update the
  616. * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
  617. */
  618. if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
  619. IB_LID_PERMISSIVE &&
  620. !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
  621. ret = -EINVAL;
  622. printk(KERN_ERR PFX "Invalid directed route\n");
  623. goto out;
  624. }
  625. /* Check to post send on QP or process locally */
  626. ret = smi_check_local_smp(smp, device);
  627. if (!ret)
  628. goto out;
  629. local = kmalloc(sizeof *local, GFP_ATOMIC);
  630. if (!local) {
  631. ret = -ENOMEM;
  632. printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
  633. goto out;
  634. }
  635. local->mad_priv = NULL;
  636. local->recv_mad_agent = NULL;
  637. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
  638. if (!mad_priv) {
  639. ret = -ENOMEM;
  640. printk(KERN_ERR PFX "No memory for local response MAD\n");
  641. kfree(local);
  642. goto out;
  643. }
  644. build_smp_wc(mad_agent_priv->agent.qp,
  645. send_wr->wr_id, be16_to_cpu(smp->dr_slid),
  646. send_wr->wr.ud.pkey_index,
  647. send_wr->wr.ud.port_num, &mad_wc);
  648. /* No GRH for DR SMP */
  649. ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
  650. (struct ib_mad *)smp,
  651. (struct ib_mad *)&mad_priv->mad);
  652. switch (ret)
  653. {
  654. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
  655. if (ib_response_mad(&mad_priv->mad.mad) &&
  656. mad_agent_priv->agent.recv_handler) {
  657. local->mad_priv = mad_priv;
  658. local->recv_mad_agent = mad_agent_priv;
  659. /*
  660. * Reference MAD agent until receive
  661. * side of local completion handled
  662. */
  663. atomic_inc(&mad_agent_priv->refcount);
  664. } else
  665. kmem_cache_free(ib_mad_cache, mad_priv);
  666. break;
  667. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
  668. kmem_cache_free(ib_mad_cache, mad_priv);
  669. break;
  670. case IB_MAD_RESULT_SUCCESS:
  671. /* Treat like an incoming receive MAD */
  672. port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
  673. mad_agent_priv->agent.port_num);
  674. if (port_priv) {
  675. mad_priv->mad.mad.mad_hdr.tid =
  676. ((struct ib_mad *)smp)->mad_hdr.tid;
  677. recv_mad_agent = find_mad_agent(port_priv,
  678. &mad_priv->mad.mad);
  679. }
  680. if (!port_priv || !recv_mad_agent) {
  681. kmem_cache_free(ib_mad_cache, mad_priv);
  682. kfree(local);
  683. ret = 0;
  684. goto out;
  685. }
  686. local->mad_priv = mad_priv;
  687. local->recv_mad_agent = recv_mad_agent;
  688. break;
  689. default:
  690. kmem_cache_free(ib_mad_cache, mad_priv);
  691. kfree(local);
  692. ret = -EINVAL;
  693. goto out;
  694. }
  695. local->mad_send_wr = mad_send_wr;
  696. /* Reference MAD agent until send side of local completion handled */
  697. atomic_inc(&mad_agent_priv->refcount);
  698. /* Queue local completion to local list */
  699. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  700. list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
  701. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  702. queue_work(mad_agent_priv->qp_info->port_priv->wq,
  703. &mad_agent_priv->local_work);
  704. ret = 1;
  705. out:
  706. return ret;
  707. }
  708. static int get_pad_size(int hdr_len, int data_len)
  709. {
  710. int seg_size, pad;
  711. seg_size = sizeof(struct ib_mad) - hdr_len;
  712. if (data_len && seg_size) {
  713. pad = seg_size - data_len % seg_size;
  714. return pad == seg_size ? 0 : pad;
  715. } else
  716. return seg_size;
  717. }
  718. static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
  719. {
  720. struct ib_rmpp_segment *s, *t;
  721. list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
  722. list_del(&s->list);
  723. kfree(s);
  724. }
  725. }
  726. static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
  727. gfp_t gfp_mask)
  728. {
  729. struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
  730. struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
  731. struct ib_rmpp_segment *seg = NULL;
  732. int left, seg_size, pad;
  733. send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
  734. seg_size = send_buf->seg_size;
  735. pad = send_wr->pad;
  736. /* Allocate data segments. */
  737. for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
  738. seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
  739. if (!seg) {
  740. printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
  741. "alloc failed for len %zd, gfp %#x\n",
  742. sizeof (*seg) + seg_size, gfp_mask);
  743. free_send_rmpp_list(send_wr);
  744. return -ENOMEM;
  745. }
  746. seg->num = ++send_buf->seg_count;
  747. list_add_tail(&seg->list, &send_wr->rmpp_list);
  748. }
  749. /* Zero any padding */
  750. if (pad)
  751. memset(seg->data + seg_size - pad, 0, pad);
  752. rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
  753. agent.rmpp_version;
  754. rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
  755. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  756. send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
  757. struct ib_rmpp_segment, list);
  758. send_wr->last_ack_seg = send_wr->cur_seg;
  759. return 0;
  760. }
  761. struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
  762. u32 remote_qpn, u16 pkey_index,
  763. int rmpp_active,
  764. int hdr_len, int data_len,
  765. gfp_t gfp_mask)
  766. {
  767. struct ib_mad_agent_private *mad_agent_priv;
  768. struct ib_mad_send_wr_private *mad_send_wr;
  769. int pad, message_size, ret, size;
  770. void *buf;
  771. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  772. agent);
  773. pad = get_pad_size(hdr_len, data_len);
  774. message_size = hdr_len + data_len + pad;
  775. if ((!mad_agent->rmpp_version &&
  776. (rmpp_active || message_size > sizeof(struct ib_mad))) ||
  777. (!rmpp_active && message_size > sizeof(struct ib_mad)))
  778. return ERR_PTR(-EINVAL);
  779. size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
  780. buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
  781. if (!buf)
  782. return ERR_PTR(-ENOMEM);
  783. mad_send_wr = buf + size;
  784. INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
  785. mad_send_wr->send_buf.mad = buf;
  786. mad_send_wr->send_buf.hdr_len = hdr_len;
  787. mad_send_wr->send_buf.data_len = data_len;
  788. mad_send_wr->pad = pad;
  789. mad_send_wr->mad_agent_priv = mad_agent_priv;
  790. mad_send_wr->sg_list[0].length = hdr_len;
  791. mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
  792. mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
  793. mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
  794. mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
  795. mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
  796. mad_send_wr->send_wr.num_sge = 2;
  797. mad_send_wr->send_wr.opcode = IB_WR_SEND;
  798. mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
  799. mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
  800. mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
  801. mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
  802. if (rmpp_active) {
  803. ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
  804. if (ret) {
  805. kfree(buf);
  806. return ERR_PTR(ret);
  807. }
  808. }
  809. mad_send_wr->send_buf.mad_agent = mad_agent;
  810. atomic_inc(&mad_agent_priv->refcount);
  811. return &mad_send_wr->send_buf;
  812. }
  813. EXPORT_SYMBOL(ib_create_send_mad);
  814. int ib_get_mad_data_offset(u8 mgmt_class)
  815. {
  816. if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
  817. return IB_MGMT_SA_HDR;
  818. else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  819. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  820. (mgmt_class == IB_MGMT_CLASS_BIS))
  821. return IB_MGMT_DEVICE_HDR;
  822. else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  823. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
  824. return IB_MGMT_VENDOR_HDR;
  825. else
  826. return IB_MGMT_MAD_HDR;
  827. }
  828. EXPORT_SYMBOL(ib_get_mad_data_offset);
  829. int ib_is_mad_class_rmpp(u8 mgmt_class)
  830. {
  831. if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
  832. (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  833. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  834. (mgmt_class == IB_MGMT_CLASS_BIS) ||
  835. ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  836. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
  837. return 1;
  838. return 0;
  839. }
  840. EXPORT_SYMBOL(ib_is_mad_class_rmpp);
  841. void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
  842. {
  843. struct ib_mad_send_wr_private *mad_send_wr;
  844. struct list_head *list;
  845. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  846. send_buf);
  847. list = &mad_send_wr->cur_seg->list;
  848. if (mad_send_wr->cur_seg->num < seg_num) {
  849. list_for_each_entry(mad_send_wr->cur_seg, list, list)
  850. if (mad_send_wr->cur_seg->num == seg_num)
  851. break;
  852. } else if (mad_send_wr->cur_seg->num > seg_num) {
  853. list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
  854. if (mad_send_wr->cur_seg->num == seg_num)
  855. break;
  856. }
  857. return mad_send_wr->cur_seg->data;
  858. }
  859. EXPORT_SYMBOL(ib_get_rmpp_segment);
  860. static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
  861. {
  862. if (mad_send_wr->send_buf.seg_count)
  863. return ib_get_rmpp_segment(&mad_send_wr->send_buf,
  864. mad_send_wr->seg_num);
  865. else
  866. return mad_send_wr->send_buf.mad +
  867. mad_send_wr->send_buf.hdr_len;
  868. }
  869. void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
  870. {
  871. struct ib_mad_agent_private *mad_agent_priv;
  872. struct ib_mad_send_wr_private *mad_send_wr;
  873. mad_agent_priv = container_of(send_buf->mad_agent,
  874. struct ib_mad_agent_private, agent);
  875. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  876. send_buf);
  877. free_send_rmpp_list(mad_send_wr);
  878. kfree(send_buf->mad);
  879. deref_mad_agent(mad_agent_priv);
  880. }
  881. EXPORT_SYMBOL(ib_free_send_mad);
  882. int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
  883. {
  884. struct ib_mad_qp_info *qp_info;
  885. struct list_head *list;
  886. struct ib_send_wr *bad_send_wr;
  887. struct ib_mad_agent *mad_agent;
  888. struct ib_sge *sge;
  889. unsigned long flags;
  890. int ret;
  891. /* Set WR ID to find mad_send_wr upon completion */
  892. qp_info = mad_send_wr->mad_agent_priv->qp_info;
  893. mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
  894. mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
  895. mad_agent = mad_send_wr->send_buf.mad_agent;
  896. sge = mad_send_wr->sg_list;
  897. sge[0].addr = ib_dma_map_single(mad_agent->device,
  898. mad_send_wr->send_buf.mad,
  899. sge[0].length,
  900. DMA_TO_DEVICE);
  901. mad_send_wr->header_mapping = sge[0].addr;
  902. sge[1].addr = ib_dma_map_single(mad_agent->device,
  903. ib_get_payload(mad_send_wr),
  904. sge[1].length,
  905. DMA_TO_DEVICE);
  906. mad_send_wr->payload_mapping = sge[1].addr;
  907. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  908. if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
  909. ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
  910. &bad_send_wr);
  911. list = &qp_info->send_queue.list;
  912. } else {
  913. ret = 0;
  914. list = &qp_info->overflow_list;
  915. }
  916. if (!ret) {
  917. qp_info->send_queue.count++;
  918. list_add_tail(&mad_send_wr->mad_list.list, list);
  919. }
  920. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  921. if (ret) {
  922. ib_dma_unmap_single(mad_agent->device,
  923. mad_send_wr->header_mapping,
  924. sge[0].length, DMA_TO_DEVICE);
  925. ib_dma_unmap_single(mad_agent->device,
  926. mad_send_wr->payload_mapping,
  927. sge[1].length, DMA_TO_DEVICE);
  928. }
  929. return ret;
  930. }
  931. /*
  932. * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
  933. * with the registered client
  934. */
  935. int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
  936. struct ib_mad_send_buf **bad_send_buf)
  937. {
  938. struct ib_mad_agent_private *mad_agent_priv;
  939. struct ib_mad_send_buf *next_send_buf;
  940. struct ib_mad_send_wr_private *mad_send_wr;
  941. unsigned long flags;
  942. int ret = -EINVAL;
  943. /* Walk list of send WRs and post each on send list */
  944. for (; send_buf; send_buf = next_send_buf) {
  945. mad_send_wr = container_of(send_buf,
  946. struct ib_mad_send_wr_private,
  947. send_buf);
  948. mad_agent_priv = mad_send_wr->mad_agent_priv;
  949. if (!send_buf->mad_agent->send_handler ||
  950. (send_buf->timeout_ms &&
  951. !send_buf->mad_agent->recv_handler)) {
  952. ret = -EINVAL;
  953. goto error;
  954. }
  955. if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
  956. if (mad_agent_priv->agent.rmpp_version) {
  957. ret = -EINVAL;
  958. goto error;
  959. }
  960. }
  961. /*
  962. * Save pointer to next work request to post in case the
  963. * current one completes, and the user modifies the work
  964. * request associated with the completion
  965. */
  966. next_send_buf = send_buf->next;
  967. mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
  968. if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
  969. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  970. ret = handle_outgoing_dr_smp(mad_agent_priv,
  971. mad_send_wr);
  972. if (ret < 0) /* error */
  973. goto error;
  974. else if (ret == 1) /* locally consumed */
  975. continue;
  976. }
  977. mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
  978. /* Timeout will be updated after send completes */
  979. mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
  980. mad_send_wr->retries = send_buf->retries;
  981. /* Reference for work request to QP + response */
  982. mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
  983. mad_send_wr->status = IB_WC_SUCCESS;
  984. /* Reference MAD agent until send completes */
  985. atomic_inc(&mad_agent_priv->refcount);
  986. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  987. list_add_tail(&mad_send_wr->agent_list,
  988. &mad_agent_priv->send_list);
  989. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  990. if (mad_agent_priv->agent.rmpp_version) {
  991. ret = ib_send_rmpp_mad(mad_send_wr);
  992. if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
  993. ret = ib_send_mad(mad_send_wr);
  994. } else
  995. ret = ib_send_mad(mad_send_wr);
  996. if (ret < 0) {
  997. /* Fail send request */
  998. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  999. list_del(&mad_send_wr->agent_list);
  1000. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1001. atomic_dec(&mad_agent_priv->refcount);
  1002. goto error;
  1003. }
  1004. }
  1005. return 0;
  1006. error:
  1007. if (bad_send_buf)
  1008. *bad_send_buf = send_buf;
  1009. return ret;
  1010. }
  1011. EXPORT_SYMBOL(ib_post_send_mad);
  1012. /*
  1013. * ib_free_recv_mad - Returns data buffers used to receive
  1014. * a MAD to the access layer
  1015. */
  1016. void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
  1017. {
  1018. struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
  1019. struct ib_mad_private_header *mad_priv_hdr;
  1020. struct ib_mad_private *priv;
  1021. struct list_head free_list;
  1022. INIT_LIST_HEAD(&free_list);
  1023. list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
  1024. list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
  1025. &free_list, list) {
  1026. mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
  1027. recv_buf);
  1028. mad_priv_hdr = container_of(mad_recv_wc,
  1029. struct ib_mad_private_header,
  1030. recv_wc);
  1031. priv = container_of(mad_priv_hdr, struct ib_mad_private,
  1032. header);
  1033. kmem_cache_free(ib_mad_cache, priv);
  1034. }
  1035. }
  1036. EXPORT_SYMBOL(ib_free_recv_mad);
  1037. struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
  1038. u8 rmpp_version,
  1039. ib_mad_send_handler send_handler,
  1040. ib_mad_recv_handler recv_handler,
  1041. void *context)
  1042. {
  1043. return ERR_PTR(-EINVAL); /* XXX: for now */
  1044. }
  1045. EXPORT_SYMBOL(ib_redirect_mad_qp);
  1046. int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
  1047. struct ib_wc *wc)
  1048. {
  1049. printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
  1050. return 0;
  1051. }
  1052. EXPORT_SYMBOL(ib_process_mad_wc);
  1053. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  1054. struct ib_mad_reg_req *mad_reg_req)
  1055. {
  1056. int i;
  1057. for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
  1058. i < IB_MGMT_MAX_METHODS;
  1059. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1060. 1+i)) {
  1061. if ((*method)->agent[i]) {
  1062. printk(KERN_ERR PFX "Method %d already in use\n", i);
  1063. return -EINVAL;
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
  1069. {
  1070. /* Allocate management method table */
  1071. *method = kzalloc(sizeof **method, GFP_ATOMIC);
  1072. if (!*method) {
  1073. printk(KERN_ERR PFX "No memory for "
  1074. "ib_mad_mgmt_method_table\n");
  1075. return -ENOMEM;
  1076. }
  1077. return 0;
  1078. }
  1079. /*
  1080. * Check to see if there are any methods still in use
  1081. */
  1082. static int check_method_table(struct ib_mad_mgmt_method_table *method)
  1083. {
  1084. int i;
  1085. for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
  1086. if (method->agent[i])
  1087. return 1;
  1088. return 0;
  1089. }
  1090. /*
  1091. * Check to see if there are any method tables for this class still in use
  1092. */
  1093. static int check_class_table(struct ib_mad_mgmt_class_table *class)
  1094. {
  1095. int i;
  1096. for (i = 0; i < MAX_MGMT_CLASS; i++)
  1097. if (class->method_table[i])
  1098. return 1;
  1099. return 0;
  1100. }
  1101. static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
  1102. {
  1103. int i;
  1104. for (i = 0; i < MAX_MGMT_OUI; i++)
  1105. if (vendor_class->method_table[i])
  1106. return 1;
  1107. return 0;
  1108. }
  1109. static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
  1110. char *oui)
  1111. {
  1112. int i;
  1113. for (i = 0; i < MAX_MGMT_OUI; i++)
  1114. /* Is there matching OUI for this vendor class ? */
  1115. if (!memcmp(vendor_class->oui[i], oui, 3))
  1116. return i;
  1117. return -1;
  1118. }
  1119. static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
  1120. {
  1121. int i;
  1122. for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
  1123. if (vendor->vendor_class[i])
  1124. return 1;
  1125. return 0;
  1126. }
  1127. static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
  1128. struct ib_mad_agent_private *agent)
  1129. {
  1130. int i;
  1131. /* Remove any methods for this mad agent */
  1132. for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
  1133. if (method->agent[i] == agent) {
  1134. method->agent[i] = NULL;
  1135. }
  1136. }
  1137. }
  1138. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1139. struct ib_mad_agent_private *agent_priv,
  1140. u8 mgmt_class)
  1141. {
  1142. struct ib_mad_port_private *port_priv;
  1143. struct ib_mad_mgmt_class_table **class;
  1144. struct ib_mad_mgmt_method_table **method;
  1145. int i, ret;
  1146. port_priv = agent_priv->qp_info->port_priv;
  1147. class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
  1148. if (!*class) {
  1149. /* Allocate management class table for "new" class version */
  1150. *class = kzalloc(sizeof **class, GFP_ATOMIC);
  1151. if (!*class) {
  1152. printk(KERN_ERR PFX "No memory for "
  1153. "ib_mad_mgmt_class_table\n");
  1154. ret = -ENOMEM;
  1155. goto error1;
  1156. }
  1157. /* Allocate method table for this management class */
  1158. method = &(*class)->method_table[mgmt_class];
  1159. if ((ret = allocate_method_table(method)))
  1160. goto error2;
  1161. } else {
  1162. method = &(*class)->method_table[mgmt_class];
  1163. if (!*method) {
  1164. /* Allocate method table for this management class */
  1165. if ((ret = allocate_method_table(method)))
  1166. goto error1;
  1167. }
  1168. }
  1169. /* Now, make sure methods are not already in use */
  1170. if (method_in_use(method, mad_reg_req))
  1171. goto error3;
  1172. /* Finally, add in methods being registered */
  1173. for (i = find_first_bit(mad_reg_req->method_mask,
  1174. IB_MGMT_MAX_METHODS);
  1175. i < IB_MGMT_MAX_METHODS;
  1176. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1177. 1+i)) {
  1178. (*method)->agent[i] = agent_priv;
  1179. }
  1180. return 0;
  1181. error3:
  1182. /* Remove any methods for this mad agent */
  1183. remove_methods_mad_agent(*method, agent_priv);
  1184. /* Now, check to see if there are any methods in use */
  1185. if (!check_method_table(*method)) {
  1186. /* If not, release management method table */
  1187. kfree(*method);
  1188. *method = NULL;
  1189. }
  1190. ret = -EINVAL;
  1191. goto error1;
  1192. error2:
  1193. kfree(*class);
  1194. *class = NULL;
  1195. error1:
  1196. return ret;
  1197. }
  1198. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1199. struct ib_mad_agent_private *agent_priv)
  1200. {
  1201. struct ib_mad_port_private *port_priv;
  1202. struct ib_mad_mgmt_vendor_class_table **vendor_table;
  1203. struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
  1204. struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
  1205. struct ib_mad_mgmt_method_table **method;
  1206. int i, ret = -ENOMEM;
  1207. u8 vclass;
  1208. /* "New" vendor (with OUI) class */
  1209. vclass = vendor_class_index(mad_reg_req->mgmt_class);
  1210. port_priv = agent_priv->qp_info->port_priv;
  1211. vendor_table = &port_priv->version[
  1212. mad_reg_req->mgmt_class_version].vendor;
  1213. if (!*vendor_table) {
  1214. /* Allocate mgmt vendor class table for "new" class version */
  1215. vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
  1216. if (!vendor) {
  1217. printk(KERN_ERR PFX "No memory for "
  1218. "ib_mad_mgmt_vendor_class_table\n");
  1219. goto error1;
  1220. }
  1221. *vendor_table = vendor;
  1222. }
  1223. if (!(*vendor_table)->vendor_class[vclass]) {
  1224. /* Allocate table for this management vendor class */
  1225. vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
  1226. if (!vendor_class) {
  1227. printk(KERN_ERR PFX "No memory for "
  1228. "ib_mad_mgmt_vendor_class\n");
  1229. goto error2;
  1230. }
  1231. (*vendor_table)->vendor_class[vclass] = vendor_class;
  1232. }
  1233. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1234. /* Is there matching OUI for this vendor class ? */
  1235. if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
  1236. mad_reg_req->oui, 3)) {
  1237. method = &(*vendor_table)->vendor_class[
  1238. vclass]->method_table[i];
  1239. BUG_ON(!*method);
  1240. goto check_in_use;
  1241. }
  1242. }
  1243. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1244. /* OUI slot available ? */
  1245. if (!is_vendor_oui((*vendor_table)->vendor_class[
  1246. vclass]->oui[i])) {
  1247. method = &(*vendor_table)->vendor_class[
  1248. vclass]->method_table[i];
  1249. BUG_ON(*method);
  1250. /* Allocate method table for this OUI */
  1251. if ((ret = allocate_method_table(method)))
  1252. goto error3;
  1253. memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
  1254. mad_reg_req->oui, 3);
  1255. goto check_in_use;
  1256. }
  1257. }
  1258. printk(KERN_ERR PFX "All OUI slots in use\n");
  1259. goto error3;
  1260. check_in_use:
  1261. /* Now, make sure methods are not already in use */
  1262. if (method_in_use(method, mad_reg_req))
  1263. goto error4;
  1264. /* Finally, add in methods being registered */
  1265. for (i = find_first_bit(mad_reg_req->method_mask,
  1266. IB_MGMT_MAX_METHODS);
  1267. i < IB_MGMT_MAX_METHODS;
  1268. i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
  1269. 1+i)) {
  1270. (*method)->agent[i] = agent_priv;
  1271. }
  1272. return 0;
  1273. error4:
  1274. /* Remove any methods for this mad agent */
  1275. remove_methods_mad_agent(*method, agent_priv);
  1276. /* Now, check to see if there are any methods in use */
  1277. if (!check_method_table(*method)) {
  1278. /* If not, release management method table */
  1279. kfree(*method);
  1280. *method = NULL;
  1281. }
  1282. ret = -EINVAL;
  1283. error3:
  1284. if (vendor_class) {
  1285. (*vendor_table)->vendor_class[vclass] = NULL;
  1286. kfree(vendor_class);
  1287. }
  1288. error2:
  1289. if (vendor) {
  1290. *vendor_table = NULL;
  1291. kfree(vendor);
  1292. }
  1293. error1:
  1294. return ret;
  1295. }
  1296. static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
  1297. {
  1298. struct ib_mad_port_private *port_priv;
  1299. struct ib_mad_mgmt_class_table *class;
  1300. struct ib_mad_mgmt_method_table *method;
  1301. struct ib_mad_mgmt_vendor_class_table *vendor;
  1302. struct ib_mad_mgmt_vendor_class *vendor_class;
  1303. int index;
  1304. u8 mgmt_class;
  1305. /*
  1306. * Was MAD registration request supplied
  1307. * with original registration ?
  1308. */
  1309. if (!agent_priv->reg_req) {
  1310. goto out;
  1311. }
  1312. port_priv = agent_priv->qp_info->port_priv;
  1313. mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
  1314. class = port_priv->version[
  1315. agent_priv->reg_req->mgmt_class_version].class;
  1316. if (!class)
  1317. goto vendor_check;
  1318. method = class->method_table[mgmt_class];
  1319. if (method) {
  1320. /* Remove any methods for this mad agent */
  1321. remove_methods_mad_agent(method, agent_priv);
  1322. /* Now, check to see if there are any methods still in use */
  1323. if (!check_method_table(method)) {
  1324. /* If not, release management method table */
  1325. kfree(method);
  1326. class->method_table[mgmt_class] = NULL;
  1327. /* Any management classes left ? */
  1328. if (!check_class_table(class)) {
  1329. /* If not, release management class table */
  1330. kfree(class);
  1331. port_priv->version[
  1332. agent_priv->reg_req->
  1333. mgmt_class_version].class = NULL;
  1334. }
  1335. }
  1336. }
  1337. vendor_check:
  1338. if (!is_vendor_class(mgmt_class))
  1339. goto out;
  1340. /* normalize mgmt_class to vendor range 2 */
  1341. mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
  1342. vendor = port_priv->version[
  1343. agent_priv->reg_req->mgmt_class_version].vendor;
  1344. if (!vendor)
  1345. goto out;
  1346. vendor_class = vendor->vendor_class[mgmt_class];
  1347. if (vendor_class) {
  1348. index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
  1349. if (index < 0)
  1350. goto out;
  1351. method = vendor_class->method_table[index];
  1352. if (method) {
  1353. /* Remove any methods for this mad agent */
  1354. remove_methods_mad_agent(method, agent_priv);
  1355. /*
  1356. * Now, check to see if there are
  1357. * any methods still in use
  1358. */
  1359. if (!check_method_table(method)) {
  1360. /* If not, release management method table */
  1361. kfree(method);
  1362. vendor_class->method_table[index] = NULL;
  1363. memset(vendor_class->oui[index], 0, 3);
  1364. /* Any OUIs left ? */
  1365. if (!check_vendor_class(vendor_class)) {
  1366. /* If not, release vendor class table */
  1367. kfree(vendor_class);
  1368. vendor->vendor_class[mgmt_class] = NULL;
  1369. /* Any other vendor classes left ? */
  1370. if (!check_vendor_table(vendor)) {
  1371. kfree(vendor);
  1372. port_priv->version[
  1373. agent_priv->reg_req->
  1374. mgmt_class_version].
  1375. vendor = NULL;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. out:
  1382. return;
  1383. }
  1384. static struct ib_mad_agent_private *
  1385. find_mad_agent(struct ib_mad_port_private *port_priv,
  1386. struct ib_mad *mad)
  1387. {
  1388. struct ib_mad_agent_private *mad_agent = NULL;
  1389. unsigned long flags;
  1390. spin_lock_irqsave(&port_priv->reg_lock, flags);
  1391. if (ib_response_mad(mad)) {
  1392. u32 hi_tid;
  1393. struct ib_mad_agent_private *entry;
  1394. /*
  1395. * Routing is based on high 32 bits of transaction ID
  1396. * of MAD.
  1397. */
  1398. hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
  1399. list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
  1400. if (entry->agent.hi_tid == hi_tid) {
  1401. mad_agent = entry;
  1402. break;
  1403. }
  1404. }
  1405. } else {
  1406. struct ib_mad_mgmt_class_table *class;
  1407. struct ib_mad_mgmt_method_table *method;
  1408. struct ib_mad_mgmt_vendor_class_table *vendor;
  1409. struct ib_mad_mgmt_vendor_class *vendor_class;
  1410. struct ib_vendor_mad *vendor_mad;
  1411. int index;
  1412. /*
  1413. * Routing is based on version, class, and method
  1414. * For "newer" vendor MADs, also based on OUI
  1415. */
  1416. if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
  1417. goto out;
  1418. if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
  1419. class = port_priv->version[
  1420. mad->mad_hdr.class_version].class;
  1421. if (!class)
  1422. goto out;
  1423. method = class->method_table[convert_mgmt_class(
  1424. mad->mad_hdr.mgmt_class)];
  1425. if (method)
  1426. mad_agent = method->agent[mad->mad_hdr.method &
  1427. ~IB_MGMT_METHOD_RESP];
  1428. } else {
  1429. vendor = port_priv->version[
  1430. mad->mad_hdr.class_version].vendor;
  1431. if (!vendor)
  1432. goto out;
  1433. vendor_class = vendor->vendor_class[vendor_class_index(
  1434. mad->mad_hdr.mgmt_class)];
  1435. if (!vendor_class)
  1436. goto out;
  1437. /* Find matching OUI */
  1438. vendor_mad = (struct ib_vendor_mad *)mad;
  1439. index = find_vendor_oui(vendor_class, vendor_mad->oui);
  1440. if (index == -1)
  1441. goto out;
  1442. method = vendor_class->method_table[index];
  1443. if (method) {
  1444. mad_agent = method->agent[mad->mad_hdr.method &
  1445. ~IB_MGMT_METHOD_RESP];
  1446. }
  1447. }
  1448. }
  1449. if (mad_agent) {
  1450. if (mad_agent->agent.recv_handler)
  1451. atomic_inc(&mad_agent->refcount);
  1452. else {
  1453. printk(KERN_NOTICE PFX "No receive handler for client "
  1454. "%p on port %d\n",
  1455. &mad_agent->agent, port_priv->port_num);
  1456. mad_agent = NULL;
  1457. }
  1458. }
  1459. out:
  1460. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  1461. return mad_agent;
  1462. }
  1463. static int validate_mad(struct ib_mad *mad, u32 qp_num)
  1464. {
  1465. int valid = 0;
  1466. /* Make sure MAD base version is understood */
  1467. if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
  1468. printk(KERN_ERR PFX "MAD received with unsupported base "
  1469. "version %d\n", mad->mad_hdr.base_version);
  1470. goto out;
  1471. }
  1472. /* Filter SMI packets sent to other than QP0 */
  1473. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  1474. (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
  1475. if (qp_num == 0)
  1476. valid = 1;
  1477. } else {
  1478. /* Filter GSI packets sent to QP0 */
  1479. if (qp_num != 0)
  1480. valid = 1;
  1481. }
  1482. out:
  1483. return valid;
  1484. }
  1485. static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
  1486. struct ib_mad_hdr *mad_hdr)
  1487. {
  1488. struct ib_rmpp_mad *rmpp_mad;
  1489. rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
  1490. return !mad_agent_priv->agent.rmpp_version ||
  1491. !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  1492. IB_MGMT_RMPP_FLAG_ACTIVE) ||
  1493. (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
  1494. }
  1495. static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
  1496. struct ib_mad_recv_wc *rwc)
  1497. {
  1498. return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
  1499. rwc->recv_buf.mad->mad_hdr.mgmt_class;
  1500. }
  1501. static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
  1502. struct ib_mad_send_wr_private *wr,
  1503. struct ib_mad_recv_wc *rwc )
  1504. {
  1505. struct ib_ah_attr attr;
  1506. u8 send_resp, rcv_resp;
  1507. union ib_gid sgid;
  1508. struct ib_device *device = mad_agent_priv->agent.device;
  1509. u8 port_num = mad_agent_priv->agent.port_num;
  1510. u8 lmc;
  1511. send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
  1512. mad_hdr.method & IB_MGMT_METHOD_RESP;
  1513. rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
  1514. if (send_resp == rcv_resp)
  1515. /* both requests, or both responses. GIDs different */
  1516. return 0;
  1517. if (ib_query_ah(wr->send_buf.ah, &attr))
  1518. /* Assume not equal, to avoid false positives. */
  1519. return 0;
  1520. if (!!(attr.ah_flags & IB_AH_GRH) !=
  1521. !!(rwc->wc->wc_flags & IB_WC_GRH))
  1522. /* one has GID, other does not. Assume different */
  1523. return 0;
  1524. if (!send_resp && rcv_resp) {
  1525. /* is request/response. */
  1526. if (!(attr.ah_flags & IB_AH_GRH)) {
  1527. if (ib_get_cached_lmc(device, port_num, &lmc))
  1528. return 0;
  1529. return (!lmc || !((attr.src_path_bits ^
  1530. rwc->wc->dlid_path_bits) &
  1531. ((1 << lmc) - 1)));
  1532. } else {
  1533. if (ib_get_cached_gid(device, port_num,
  1534. attr.grh.sgid_index, &sgid))
  1535. return 0;
  1536. return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
  1537. 16);
  1538. }
  1539. }
  1540. if (!(attr.ah_flags & IB_AH_GRH))
  1541. return attr.dlid == rwc->wc->slid;
  1542. else
  1543. return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
  1544. 16);
  1545. }
  1546. static inline int is_direct(u8 class)
  1547. {
  1548. return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
  1549. }
  1550. struct ib_mad_send_wr_private*
  1551. ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
  1552. struct ib_mad_recv_wc *wc)
  1553. {
  1554. struct ib_mad_send_wr_private *wr;
  1555. struct ib_mad *mad;
  1556. mad = (struct ib_mad *)wc->recv_buf.mad;
  1557. list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
  1558. if ((wr->tid == mad->mad_hdr.tid) &&
  1559. rcv_has_same_class(wr, wc) &&
  1560. /*
  1561. * Don't check GID for direct routed MADs.
  1562. * These might have permissive LIDs.
  1563. */
  1564. (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
  1565. rcv_has_same_gid(mad_agent_priv, wr, wc)))
  1566. return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
  1567. }
  1568. /*
  1569. * It's possible to receive the response before we've
  1570. * been notified that the send has completed
  1571. */
  1572. list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
  1573. if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
  1574. wr->tid == mad->mad_hdr.tid &&
  1575. wr->timeout &&
  1576. rcv_has_same_class(wr, wc) &&
  1577. /*
  1578. * Don't check GID for direct routed MADs.
  1579. * These might have permissive LIDs.
  1580. */
  1581. (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
  1582. rcv_has_same_gid(mad_agent_priv, wr, wc)))
  1583. /* Verify request has not been canceled */
  1584. return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
  1585. }
  1586. return NULL;
  1587. }
  1588. void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
  1589. {
  1590. mad_send_wr->timeout = 0;
  1591. if (mad_send_wr->refcount == 1)
  1592. list_move_tail(&mad_send_wr->agent_list,
  1593. &mad_send_wr->mad_agent_priv->done_list);
  1594. }
  1595. static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
  1596. struct ib_mad_recv_wc *mad_recv_wc)
  1597. {
  1598. struct ib_mad_send_wr_private *mad_send_wr;
  1599. struct ib_mad_send_wc mad_send_wc;
  1600. unsigned long flags;
  1601. INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
  1602. list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
  1603. if (mad_agent_priv->agent.rmpp_version) {
  1604. mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
  1605. mad_recv_wc);
  1606. if (!mad_recv_wc) {
  1607. deref_mad_agent(mad_agent_priv);
  1608. return;
  1609. }
  1610. }
  1611. /* Complete corresponding request */
  1612. if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
  1613. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1614. mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
  1615. if (!mad_send_wr) {
  1616. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1617. ib_free_recv_mad(mad_recv_wc);
  1618. deref_mad_agent(mad_agent_priv);
  1619. return;
  1620. }
  1621. ib_mark_mad_done(mad_send_wr);
  1622. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1623. /* Defined behavior is to complete response before request */
  1624. mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
  1625. mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
  1626. mad_recv_wc);
  1627. atomic_dec(&mad_agent_priv->refcount);
  1628. mad_send_wc.status = IB_WC_SUCCESS;
  1629. mad_send_wc.vendor_err = 0;
  1630. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  1631. ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
  1632. } else {
  1633. mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
  1634. mad_recv_wc);
  1635. deref_mad_agent(mad_agent_priv);
  1636. }
  1637. }
  1638. static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
  1639. struct ib_wc *wc)
  1640. {
  1641. struct ib_mad_qp_info *qp_info;
  1642. struct ib_mad_private_header *mad_priv_hdr;
  1643. struct ib_mad_private *recv, *response;
  1644. struct ib_mad_list_head *mad_list;
  1645. struct ib_mad_agent_private *mad_agent;
  1646. response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
  1647. if (!response)
  1648. printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
  1649. "for response buffer\n");
  1650. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1651. qp_info = mad_list->mad_queue->qp_info;
  1652. dequeue_mad(mad_list);
  1653. mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
  1654. mad_list);
  1655. recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
  1656. ib_dma_unmap_single(port_priv->device,
  1657. recv->header.mapping,
  1658. sizeof(struct ib_mad_private) -
  1659. sizeof(struct ib_mad_private_header),
  1660. DMA_FROM_DEVICE);
  1661. /* Setup MAD receive work completion from "normal" work completion */
  1662. recv->header.wc = *wc;
  1663. recv->header.recv_wc.wc = &recv->header.wc;
  1664. recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
  1665. recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
  1666. recv->header.recv_wc.recv_buf.grh = &recv->grh;
  1667. if (atomic_read(&qp_info->snoop_count))
  1668. snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
  1669. /* Validate MAD */
  1670. if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
  1671. goto out;
  1672. if (recv->mad.mad.mad_hdr.mgmt_class ==
  1673. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  1674. if (!smi_handle_dr_smp_recv(&recv->mad.smp,
  1675. port_priv->device->node_type,
  1676. port_priv->port_num,
  1677. port_priv->device->phys_port_cnt))
  1678. goto out;
  1679. if (!smi_check_forward_dr_smp(&recv->mad.smp))
  1680. goto local;
  1681. if (!smi_handle_dr_smp_send(&recv->mad.smp,
  1682. port_priv->device->node_type,
  1683. port_priv->port_num))
  1684. goto out;
  1685. if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
  1686. goto out;
  1687. }
  1688. local:
  1689. /* Give driver "right of first refusal" on incoming MAD */
  1690. if (port_priv->device->process_mad) {
  1691. int ret;
  1692. if (!response) {
  1693. printk(KERN_ERR PFX "No memory for response MAD\n");
  1694. /*
  1695. * Is it better to assume that
  1696. * it wouldn't be processed ?
  1697. */
  1698. goto out;
  1699. }
  1700. ret = port_priv->device->process_mad(port_priv->device, 0,
  1701. port_priv->port_num,
  1702. wc, &recv->grh,
  1703. &recv->mad.mad,
  1704. &response->mad.mad);
  1705. if (ret & IB_MAD_RESULT_SUCCESS) {
  1706. if (ret & IB_MAD_RESULT_CONSUMED)
  1707. goto out;
  1708. if (ret & IB_MAD_RESULT_REPLY) {
  1709. agent_send_response(&response->mad.mad,
  1710. &recv->grh, wc,
  1711. port_priv->device,
  1712. port_priv->port_num,
  1713. qp_info->qp->qp_num);
  1714. goto out;
  1715. }
  1716. }
  1717. }
  1718. mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
  1719. if (mad_agent) {
  1720. ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
  1721. /*
  1722. * recv is freed up in error cases in ib_mad_complete_recv
  1723. * or via recv_handler in ib_mad_complete_recv()
  1724. */
  1725. recv = NULL;
  1726. }
  1727. out:
  1728. /* Post another receive request for this QP */
  1729. if (response) {
  1730. ib_mad_post_receive_mads(qp_info, response);
  1731. if (recv)
  1732. kmem_cache_free(ib_mad_cache, recv);
  1733. } else
  1734. ib_mad_post_receive_mads(qp_info, recv);
  1735. }
  1736. static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
  1737. {
  1738. struct ib_mad_send_wr_private *mad_send_wr;
  1739. unsigned long delay;
  1740. if (list_empty(&mad_agent_priv->wait_list)) {
  1741. cancel_delayed_work(&mad_agent_priv->timed_work);
  1742. } else {
  1743. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  1744. struct ib_mad_send_wr_private,
  1745. agent_list);
  1746. if (time_after(mad_agent_priv->timeout,
  1747. mad_send_wr->timeout)) {
  1748. mad_agent_priv->timeout = mad_send_wr->timeout;
  1749. cancel_delayed_work(&mad_agent_priv->timed_work);
  1750. delay = mad_send_wr->timeout - jiffies;
  1751. if ((long)delay <= 0)
  1752. delay = 1;
  1753. queue_delayed_work(mad_agent_priv->qp_info->
  1754. port_priv->wq,
  1755. &mad_agent_priv->timed_work, delay);
  1756. }
  1757. }
  1758. }
  1759. static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
  1760. {
  1761. struct ib_mad_agent_private *mad_agent_priv;
  1762. struct ib_mad_send_wr_private *temp_mad_send_wr;
  1763. struct list_head *list_item;
  1764. unsigned long delay;
  1765. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1766. list_del(&mad_send_wr->agent_list);
  1767. delay = mad_send_wr->timeout;
  1768. mad_send_wr->timeout += jiffies;
  1769. if (delay) {
  1770. list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
  1771. temp_mad_send_wr = list_entry(list_item,
  1772. struct ib_mad_send_wr_private,
  1773. agent_list);
  1774. if (time_after(mad_send_wr->timeout,
  1775. temp_mad_send_wr->timeout))
  1776. break;
  1777. }
  1778. }
  1779. else
  1780. list_item = &mad_agent_priv->wait_list;
  1781. list_add(&mad_send_wr->agent_list, list_item);
  1782. /* Reschedule a work item if we have a shorter timeout */
  1783. if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
  1784. cancel_delayed_work(&mad_agent_priv->timed_work);
  1785. queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
  1786. &mad_agent_priv->timed_work, delay);
  1787. }
  1788. }
  1789. void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
  1790. int timeout_ms)
  1791. {
  1792. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  1793. wait_for_response(mad_send_wr);
  1794. }
  1795. /*
  1796. * Process a send work completion
  1797. */
  1798. void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
  1799. struct ib_mad_send_wc *mad_send_wc)
  1800. {
  1801. struct ib_mad_agent_private *mad_agent_priv;
  1802. unsigned long flags;
  1803. int ret;
  1804. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1805. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1806. if (mad_agent_priv->agent.rmpp_version) {
  1807. ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
  1808. if (ret == IB_RMPP_RESULT_CONSUMED)
  1809. goto done;
  1810. } else
  1811. ret = IB_RMPP_RESULT_UNHANDLED;
  1812. if (mad_send_wc->status != IB_WC_SUCCESS &&
  1813. mad_send_wr->status == IB_WC_SUCCESS) {
  1814. mad_send_wr->status = mad_send_wc->status;
  1815. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  1816. }
  1817. if (--mad_send_wr->refcount > 0) {
  1818. if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
  1819. mad_send_wr->status == IB_WC_SUCCESS) {
  1820. wait_for_response(mad_send_wr);
  1821. }
  1822. goto done;
  1823. }
  1824. /* Remove send from MAD agent and notify client of completion */
  1825. list_del(&mad_send_wr->agent_list);
  1826. adjust_timeout(mad_agent_priv);
  1827. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1828. if (mad_send_wr->status != IB_WC_SUCCESS )
  1829. mad_send_wc->status = mad_send_wr->status;
  1830. if (ret == IB_RMPP_RESULT_INTERNAL)
  1831. ib_rmpp_send_handler(mad_send_wc);
  1832. else
  1833. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  1834. mad_send_wc);
  1835. /* Release reference on agent taken when sending */
  1836. deref_mad_agent(mad_agent_priv);
  1837. return;
  1838. done:
  1839. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1840. }
  1841. static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
  1842. struct ib_wc *wc)
  1843. {
  1844. struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
  1845. struct ib_mad_list_head *mad_list;
  1846. struct ib_mad_qp_info *qp_info;
  1847. struct ib_mad_queue *send_queue;
  1848. struct ib_send_wr *bad_send_wr;
  1849. struct ib_mad_send_wc mad_send_wc;
  1850. unsigned long flags;
  1851. int ret;
  1852. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1853. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1854. mad_list);
  1855. send_queue = mad_list->mad_queue;
  1856. qp_info = send_queue->qp_info;
  1857. retry:
  1858. ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
  1859. mad_send_wr->header_mapping,
  1860. mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
  1861. ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
  1862. mad_send_wr->payload_mapping,
  1863. mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
  1864. queued_send_wr = NULL;
  1865. spin_lock_irqsave(&send_queue->lock, flags);
  1866. list_del(&mad_list->list);
  1867. /* Move queued send to the send queue */
  1868. if (send_queue->count-- > send_queue->max_active) {
  1869. mad_list = container_of(qp_info->overflow_list.next,
  1870. struct ib_mad_list_head, list);
  1871. queued_send_wr = container_of(mad_list,
  1872. struct ib_mad_send_wr_private,
  1873. mad_list);
  1874. list_move_tail(&mad_list->list, &send_queue->list);
  1875. }
  1876. spin_unlock_irqrestore(&send_queue->lock, flags);
  1877. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  1878. mad_send_wc.status = wc->status;
  1879. mad_send_wc.vendor_err = wc->vendor_err;
  1880. if (atomic_read(&qp_info->snoop_count))
  1881. snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
  1882. IB_MAD_SNOOP_SEND_COMPLETIONS);
  1883. ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
  1884. if (queued_send_wr) {
  1885. ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
  1886. &bad_send_wr);
  1887. if (ret) {
  1888. printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
  1889. mad_send_wr = queued_send_wr;
  1890. wc->status = IB_WC_LOC_QP_OP_ERR;
  1891. goto retry;
  1892. }
  1893. }
  1894. }
  1895. static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
  1896. {
  1897. struct ib_mad_send_wr_private *mad_send_wr;
  1898. struct ib_mad_list_head *mad_list;
  1899. unsigned long flags;
  1900. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  1901. list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
  1902. mad_send_wr = container_of(mad_list,
  1903. struct ib_mad_send_wr_private,
  1904. mad_list);
  1905. mad_send_wr->retry = 1;
  1906. }
  1907. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  1908. }
  1909. static void mad_error_handler(struct ib_mad_port_private *port_priv,
  1910. struct ib_wc *wc)
  1911. {
  1912. struct ib_mad_list_head *mad_list;
  1913. struct ib_mad_qp_info *qp_info;
  1914. struct ib_mad_send_wr_private *mad_send_wr;
  1915. int ret;
  1916. /* Determine if failure was a send or receive */
  1917. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1918. qp_info = mad_list->mad_queue->qp_info;
  1919. if (mad_list->mad_queue == &qp_info->recv_queue)
  1920. /*
  1921. * Receive errors indicate that the QP has entered the error
  1922. * state - error handling/shutdown code will cleanup
  1923. */
  1924. return;
  1925. /*
  1926. * Send errors will transition the QP to SQE - move
  1927. * QP to RTS and repost flushed work requests
  1928. */
  1929. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1930. mad_list);
  1931. if (wc->status == IB_WC_WR_FLUSH_ERR) {
  1932. if (mad_send_wr->retry) {
  1933. /* Repost send */
  1934. struct ib_send_wr *bad_send_wr;
  1935. mad_send_wr->retry = 0;
  1936. ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
  1937. &bad_send_wr);
  1938. if (ret)
  1939. ib_mad_send_done_handler(port_priv, wc);
  1940. } else
  1941. ib_mad_send_done_handler(port_priv, wc);
  1942. } else {
  1943. struct ib_qp_attr *attr;
  1944. /* Transition QP to RTS and fail offending send */
  1945. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1946. if (attr) {
  1947. attr->qp_state = IB_QPS_RTS;
  1948. attr->cur_qp_state = IB_QPS_SQE;
  1949. ret = ib_modify_qp(qp_info->qp, attr,
  1950. IB_QP_STATE | IB_QP_CUR_STATE);
  1951. kfree(attr);
  1952. if (ret)
  1953. printk(KERN_ERR PFX "mad_error_handler - "
  1954. "ib_modify_qp to RTS : %d\n", ret);
  1955. else
  1956. mark_sends_for_retry(qp_info);
  1957. }
  1958. ib_mad_send_done_handler(port_priv, wc);
  1959. }
  1960. }
  1961. /*
  1962. * IB MAD completion callback
  1963. */
  1964. static void ib_mad_completion_handler(struct work_struct *work)
  1965. {
  1966. struct ib_mad_port_private *port_priv;
  1967. struct ib_wc wc;
  1968. port_priv = container_of(work, struct ib_mad_port_private, work);
  1969. ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  1970. while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
  1971. if (wc.status == IB_WC_SUCCESS) {
  1972. switch (wc.opcode) {
  1973. case IB_WC_SEND:
  1974. ib_mad_send_done_handler(port_priv, &wc);
  1975. break;
  1976. case IB_WC_RECV:
  1977. ib_mad_recv_done_handler(port_priv, &wc);
  1978. break;
  1979. default:
  1980. BUG_ON(1);
  1981. break;
  1982. }
  1983. } else
  1984. mad_error_handler(port_priv, &wc);
  1985. }
  1986. }
  1987. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
  1988. {
  1989. unsigned long flags;
  1990. struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
  1991. struct ib_mad_send_wc mad_send_wc;
  1992. struct list_head cancel_list;
  1993. INIT_LIST_HEAD(&cancel_list);
  1994. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1995. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  1996. &mad_agent_priv->send_list, agent_list) {
  1997. if (mad_send_wr->status == IB_WC_SUCCESS) {
  1998. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  1999. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  2000. }
  2001. }
  2002. /* Empty wait list to prevent receives from finding a request */
  2003. list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
  2004. /* Empty local completion list as well */
  2005. list_splice_init(&mad_agent_priv->local_list, &cancel_list);
  2006. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2007. /* Report all cancelled requests */
  2008. mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
  2009. mad_send_wc.vendor_err = 0;
  2010. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  2011. &cancel_list, agent_list) {
  2012. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2013. list_del(&mad_send_wr->agent_list);
  2014. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2015. &mad_send_wc);
  2016. atomic_dec(&mad_agent_priv->refcount);
  2017. }
  2018. }
  2019. static struct ib_mad_send_wr_private*
  2020. find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
  2021. struct ib_mad_send_buf *send_buf)
  2022. {
  2023. struct ib_mad_send_wr_private *mad_send_wr;
  2024. list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
  2025. agent_list) {
  2026. if (&mad_send_wr->send_buf == send_buf)
  2027. return mad_send_wr;
  2028. }
  2029. list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
  2030. agent_list) {
  2031. if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
  2032. &mad_send_wr->send_buf == send_buf)
  2033. return mad_send_wr;
  2034. }
  2035. return NULL;
  2036. }
  2037. int ib_modify_mad(struct ib_mad_agent *mad_agent,
  2038. struct ib_mad_send_buf *send_buf, u32 timeout_ms)
  2039. {
  2040. struct ib_mad_agent_private *mad_agent_priv;
  2041. struct ib_mad_send_wr_private *mad_send_wr;
  2042. unsigned long flags;
  2043. int active;
  2044. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  2045. agent);
  2046. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2047. mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
  2048. if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
  2049. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2050. return -EINVAL;
  2051. }
  2052. active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
  2053. if (!timeout_ms) {
  2054. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  2055. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  2056. }
  2057. mad_send_wr->send_buf.timeout_ms = timeout_ms;
  2058. if (active)
  2059. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  2060. else
  2061. ib_reset_mad_timeout(mad_send_wr, timeout_ms);
  2062. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2063. return 0;
  2064. }
  2065. EXPORT_SYMBOL(ib_modify_mad);
  2066. void ib_cancel_mad(struct ib_mad_agent *mad_agent,
  2067. struct ib_mad_send_buf *send_buf)
  2068. {
  2069. ib_modify_mad(mad_agent, send_buf, 0);
  2070. }
  2071. EXPORT_SYMBOL(ib_cancel_mad);
  2072. static void local_completions(struct work_struct *work)
  2073. {
  2074. struct ib_mad_agent_private *mad_agent_priv;
  2075. struct ib_mad_local_private *local;
  2076. struct ib_mad_agent_private *recv_mad_agent;
  2077. unsigned long flags;
  2078. int recv = 0;
  2079. struct ib_wc wc;
  2080. struct ib_mad_send_wc mad_send_wc;
  2081. mad_agent_priv =
  2082. container_of(work, struct ib_mad_agent_private, local_work);
  2083. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2084. while (!list_empty(&mad_agent_priv->local_list)) {
  2085. local = list_entry(mad_agent_priv->local_list.next,
  2086. struct ib_mad_local_private,
  2087. completion_list);
  2088. list_del(&local->completion_list);
  2089. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2090. if (local->mad_priv) {
  2091. recv_mad_agent = local->recv_mad_agent;
  2092. if (!recv_mad_agent) {
  2093. printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
  2094. goto local_send_completion;
  2095. }
  2096. recv = 1;
  2097. /*
  2098. * Defined behavior is to complete response
  2099. * before request
  2100. */
  2101. build_smp_wc(recv_mad_agent->agent.qp,
  2102. (unsigned long) local->mad_send_wr,
  2103. be16_to_cpu(IB_LID_PERMISSIVE),
  2104. 0, recv_mad_agent->agent.port_num, &wc);
  2105. local->mad_priv->header.recv_wc.wc = &wc;
  2106. local->mad_priv->header.recv_wc.mad_len =
  2107. sizeof(struct ib_mad);
  2108. INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
  2109. list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
  2110. &local->mad_priv->header.recv_wc.rmpp_list);
  2111. local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
  2112. local->mad_priv->header.recv_wc.recv_buf.mad =
  2113. &local->mad_priv->mad.mad;
  2114. if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
  2115. snoop_recv(recv_mad_agent->qp_info,
  2116. &local->mad_priv->header.recv_wc,
  2117. IB_MAD_SNOOP_RECVS);
  2118. recv_mad_agent->agent.recv_handler(
  2119. &recv_mad_agent->agent,
  2120. &local->mad_priv->header.recv_wc);
  2121. spin_lock_irqsave(&recv_mad_agent->lock, flags);
  2122. atomic_dec(&recv_mad_agent->refcount);
  2123. spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
  2124. }
  2125. local_send_completion:
  2126. /* Complete send */
  2127. mad_send_wc.status = IB_WC_SUCCESS;
  2128. mad_send_wc.vendor_err = 0;
  2129. mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
  2130. if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
  2131. snoop_send(mad_agent_priv->qp_info,
  2132. &local->mad_send_wr->send_buf,
  2133. &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
  2134. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2135. &mad_send_wc);
  2136. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2137. atomic_dec(&mad_agent_priv->refcount);
  2138. if (!recv)
  2139. kmem_cache_free(ib_mad_cache, local->mad_priv);
  2140. kfree(local);
  2141. }
  2142. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2143. }
  2144. static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
  2145. {
  2146. int ret;
  2147. if (!mad_send_wr->retries--)
  2148. return -ETIMEDOUT;
  2149. mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
  2150. if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
  2151. ret = ib_retry_rmpp(mad_send_wr);
  2152. switch (ret) {
  2153. case IB_RMPP_RESULT_UNHANDLED:
  2154. ret = ib_send_mad(mad_send_wr);
  2155. break;
  2156. case IB_RMPP_RESULT_CONSUMED:
  2157. ret = 0;
  2158. break;
  2159. default:
  2160. ret = -ECOMM;
  2161. break;
  2162. }
  2163. } else
  2164. ret = ib_send_mad(mad_send_wr);
  2165. if (!ret) {
  2166. mad_send_wr->refcount++;
  2167. list_add_tail(&mad_send_wr->agent_list,
  2168. &mad_send_wr->mad_agent_priv->send_list);
  2169. }
  2170. return ret;
  2171. }
  2172. static void timeout_sends(struct work_struct *work)
  2173. {
  2174. struct ib_mad_agent_private *mad_agent_priv;
  2175. struct ib_mad_send_wr_private *mad_send_wr;
  2176. struct ib_mad_send_wc mad_send_wc;
  2177. unsigned long flags, delay;
  2178. mad_agent_priv = container_of(work, struct ib_mad_agent_private,
  2179. timed_work.work);
  2180. mad_send_wc.vendor_err = 0;
  2181. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2182. while (!list_empty(&mad_agent_priv->wait_list)) {
  2183. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  2184. struct ib_mad_send_wr_private,
  2185. agent_list);
  2186. if (time_after(mad_send_wr->timeout, jiffies)) {
  2187. delay = mad_send_wr->timeout - jiffies;
  2188. if ((long)delay <= 0)
  2189. delay = 1;
  2190. queue_delayed_work(mad_agent_priv->qp_info->
  2191. port_priv->wq,
  2192. &mad_agent_priv->timed_work, delay);
  2193. break;
  2194. }
  2195. list_del(&mad_send_wr->agent_list);
  2196. if (mad_send_wr->status == IB_WC_SUCCESS &&
  2197. !retry_send(mad_send_wr))
  2198. continue;
  2199. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2200. if (mad_send_wr->status == IB_WC_SUCCESS)
  2201. mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
  2202. else
  2203. mad_send_wc.status = mad_send_wr->status;
  2204. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2205. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2206. &mad_send_wc);
  2207. atomic_dec(&mad_agent_priv->refcount);
  2208. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2209. }
  2210. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2211. }
  2212. static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
  2213. {
  2214. struct ib_mad_port_private *port_priv = cq->cq_context;
  2215. unsigned long flags;
  2216. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2217. if (!list_empty(&port_priv->port_list))
  2218. queue_work(port_priv->wq, &port_priv->work);
  2219. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2220. }
  2221. /*
  2222. * Allocate receive MADs and post receive WRs for them
  2223. */
  2224. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  2225. struct ib_mad_private *mad)
  2226. {
  2227. unsigned long flags;
  2228. int post, ret;
  2229. struct ib_mad_private *mad_priv;
  2230. struct ib_sge sg_list;
  2231. struct ib_recv_wr recv_wr, *bad_recv_wr;
  2232. struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
  2233. /* Initialize common scatter list fields */
  2234. sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
  2235. sg_list.lkey = (*qp_info->port_priv->mr).lkey;
  2236. /* Initialize common receive WR fields */
  2237. recv_wr.next = NULL;
  2238. recv_wr.sg_list = &sg_list;
  2239. recv_wr.num_sge = 1;
  2240. do {
  2241. /* Allocate and map receive buffer */
  2242. if (mad) {
  2243. mad_priv = mad;
  2244. mad = NULL;
  2245. } else {
  2246. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
  2247. if (!mad_priv) {
  2248. printk(KERN_ERR PFX "No memory for receive buffer\n");
  2249. ret = -ENOMEM;
  2250. break;
  2251. }
  2252. }
  2253. sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
  2254. &mad_priv->grh,
  2255. sizeof *mad_priv -
  2256. sizeof mad_priv->header,
  2257. DMA_FROM_DEVICE);
  2258. mad_priv->header.mapping = sg_list.addr;
  2259. recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
  2260. mad_priv->header.mad_list.mad_queue = recv_queue;
  2261. /* Post receive WR */
  2262. spin_lock_irqsave(&recv_queue->lock, flags);
  2263. post = (++recv_queue->count < recv_queue->max_active);
  2264. list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
  2265. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2266. ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
  2267. if (ret) {
  2268. spin_lock_irqsave(&recv_queue->lock, flags);
  2269. list_del(&mad_priv->header.mad_list.list);
  2270. recv_queue->count--;
  2271. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2272. ib_dma_unmap_single(qp_info->port_priv->device,
  2273. mad_priv->header.mapping,
  2274. sizeof *mad_priv -
  2275. sizeof mad_priv->header,
  2276. DMA_FROM_DEVICE);
  2277. kmem_cache_free(ib_mad_cache, mad_priv);
  2278. printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
  2279. break;
  2280. }
  2281. } while (post);
  2282. return ret;
  2283. }
  2284. /*
  2285. * Return all the posted receive MADs
  2286. */
  2287. static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
  2288. {
  2289. struct ib_mad_private_header *mad_priv_hdr;
  2290. struct ib_mad_private *recv;
  2291. struct ib_mad_list_head *mad_list;
  2292. while (!list_empty(&qp_info->recv_queue.list)) {
  2293. mad_list = list_entry(qp_info->recv_queue.list.next,
  2294. struct ib_mad_list_head, list);
  2295. mad_priv_hdr = container_of(mad_list,
  2296. struct ib_mad_private_header,
  2297. mad_list);
  2298. recv = container_of(mad_priv_hdr, struct ib_mad_private,
  2299. header);
  2300. /* Remove from posted receive MAD list */
  2301. list_del(&mad_list->list);
  2302. ib_dma_unmap_single(qp_info->port_priv->device,
  2303. recv->header.mapping,
  2304. sizeof(struct ib_mad_private) -
  2305. sizeof(struct ib_mad_private_header),
  2306. DMA_FROM_DEVICE);
  2307. kmem_cache_free(ib_mad_cache, recv);
  2308. }
  2309. qp_info->recv_queue.count = 0;
  2310. }
  2311. /*
  2312. * Start the port
  2313. */
  2314. static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
  2315. {
  2316. int ret, i;
  2317. struct ib_qp_attr *attr;
  2318. struct ib_qp *qp;
  2319. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  2320. if (!attr) {
  2321. printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
  2322. return -ENOMEM;
  2323. }
  2324. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2325. qp = port_priv->qp_info[i].qp;
  2326. /*
  2327. * PKey index for QP1 is irrelevant but
  2328. * one is needed for the Reset to Init transition
  2329. */
  2330. attr->qp_state = IB_QPS_INIT;
  2331. attr->pkey_index = 0;
  2332. attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
  2333. ret = ib_modify_qp(qp, attr, IB_QP_STATE |
  2334. IB_QP_PKEY_INDEX | IB_QP_QKEY);
  2335. if (ret) {
  2336. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2337. "INIT: %d\n", i, ret);
  2338. goto out;
  2339. }
  2340. attr->qp_state = IB_QPS_RTR;
  2341. ret = ib_modify_qp(qp, attr, IB_QP_STATE);
  2342. if (ret) {
  2343. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2344. "RTR: %d\n", i, ret);
  2345. goto out;
  2346. }
  2347. attr->qp_state = IB_QPS_RTS;
  2348. attr->sq_psn = IB_MAD_SEND_Q_PSN;
  2349. ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
  2350. if (ret) {
  2351. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2352. "RTS: %d\n", i, ret);
  2353. goto out;
  2354. }
  2355. }
  2356. ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  2357. if (ret) {
  2358. printk(KERN_ERR PFX "Failed to request completion "
  2359. "notification: %d\n", ret);
  2360. goto out;
  2361. }
  2362. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2363. ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
  2364. if (ret) {
  2365. printk(KERN_ERR PFX "Couldn't post receive WRs\n");
  2366. goto out;
  2367. }
  2368. }
  2369. out:
  2370. kfree(attr);
  2371. return ret;
  2372. }
  2373. static void qp_event_handler(struct ib_event *event, void *qp_context)
  2374. {
  2375. struct ib_mad_qp_info *qp_info = qp_context;
  2376. /* It's worse than that! He's dead, Jim! */
  2377. printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
  2378. event->event, qp_info->qp->qp_num);
  2379. }
  2380. static void init_mad_queue(struct ib_mad_qp_info *qp_info,
  2381. struct ib_mad_queue *mad_queue)
  2382. {
  2383. mad_queue->qp_info = qp_info;
  2384. mad_queue->count = 0;
  2385. spin_lock_init(&mad_queue->lock);
  2386. INIT_LIST_HEAD(&mad_queue->list);
  2387. }
  2388. static void init_mad_qp(struct ib_mad_port_private *port_priv,
  2389. struct ib_mad_qp_info *qp_info)
  2390. {
  2391. qp_info->port_priv = port_priv;
  2392. init_mad_queue(qp_info, &qp_info->send_queue);
  2393. init_mad_queue(qp_info, &qp_info->recv_queue);
  2394. INIT_LIST_HEAD(&qp_info->overflow_list);
  2395. spin_lock_init(&qp_info->snoop_lock);
  2396. qp_info->snoop_table = NULL;
  2397. qp_info->snoop_table_size = 0;
  2398. atomic_set(&qp_info->snoop_count, 0);
  2399. }
  2400. static int create_mad_qp(struct ib_mad_qp_info *qp_info,
  2401. enum ib_qp_type qp_type)
  2402. {
  2403. struct ib_qp_init_attr qp_init_attr;
  2404. int ret;
  2405. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  2406. qp_init_attr.send_cq = qp_info->port_priv->cq;
  2407. qp_init_attr.recv_cq = qp_info->port_priv->cq;
  2408. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  2409. qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
  2410. qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
  2411. qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
  2412. qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
  2413. qp_init_attr.qp_type = qp_type;
  2414. qp_init_attr.port_num = qp_info->port_priv->port_num;
  2415. qp_init_attr.qp_context = qp_info;
  2416. qp_init_attr.event_handler = qp_event_handler;
  2417. qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
  2418. if (IS_ERR(qp_info->qp)) {
  2419. printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
  2420. get_spl_qp_index(qp_type));
  2421. ret = PTR_ERR(qp_info->qp);
  2422. goto error;
  2423. }
  2424. /* Use minimum queue sizes unless the CQ is resized */
  2425. qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
  2426. qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
  2427. return 0;
  2428. error:
  2429. return ret;
  2430. }
  2431. static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
  2432. {
  2433. ib_destroy_qp(qp_info->qp);
  2434. kfree(qp_info->snoop_table);
  2435. }
  2436. /*
  2437. * Open the port
  2438. * Create the QP, PD, MR, and CQ if needed
  2439. */
  2440. static int ib_mad_port_open(struct ib_device *device,
  2441. int port_num)
  2442. {
  2443. int ret, cq_size;
  2444. struct ib_mad_port_private *port_priv;
  2445. unsigned long flags;
  2446. char name[sizeof "ib_mad123"];
  2447. /* Create new device info */
  2448. port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
  2449. if (!port_priv) {
  2450. printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
  2451. return -ENOMEM;
  2452. }
  2453. port_priv->device = device;
  2454. port_priv->port_num = port_num;
  2455. spin_lock_init(&port_priv->reg_lock);
  2456. INIT_LIST_HEAD(&port_priv->agent_list);
  2457. init_mad_qp(port_priv, &port_priv->qp_info[0]);
  2458. init_mad_qp(port_priv, &port_priv->qp_info[1]);
  2459. cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
  2460. port_priv->cq = ib_create_cq(port_priv->device,
  2461. ib_mad_thread_completion_handler,
  2462. NULL, port_priv, cq_size);
  2463. if (IS_ERR(port_priv->cq)) {
  2464. printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
  2465. ret = PTR_ERR(port_priv->cq);
  2466. goto error3;
  2467. }
  2468. port_priv->pd = ib_alloc_pd(device);
  2469. if (IS_ERR(port_priv->pd)) {
  2470. printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
  2471. ret = PTR_ERR(port_priv->pd);
  2472. goto error4;
  2473. }
  2474. port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
  2475. if (IS_ERR(port_priv->mr)) {
  2476. printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
  2477. ret = PTR_ERR(port_priv->mr);
  2478. goto error5;
  2479. }
  2480. ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
  2481. if (ret)
  2482. goto error6;
  2483. ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
  2484. if (ret)
  2485. goto error7;
  2486. snprintf(name, sizeof name, "ib_mad%d", port_num);
  2487. port_priv->wq = create_singlethread_workqueue(name);
  2488. if (!port_priv->wq) {
  2489. ret = -ENOMEM;
  2490. goto error8;
  2491. }
  2492. INIT_WORK(&port_priv->work, ib_mad_completion_handler);
  2493. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2494. list_add_tail(&port_priv->port_list, &ib_mad_port_list);
  2495. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2496. ret = ib_mad_port_start(port_priv);
  2497. if (ret) {
  2498. printk(KERN_ERR PFX "Couldn't start port\n");
  2499. goto error9;
  2500. }
  2501. return 0;
  2502. error9:
  2503. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2504. list_del_init(&port_priv->port_list);
  2505. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2506. destroy_workqueue(port_priv->wq);
  2507. error8:
  2508. destroy_mad_qp(&port_priv->qp_info[1]);
  2509. error7:
  2510. destroy_mad_qp(&port_priv->qp_info[0]);
  2511. error6:
  2512. ib_dereg_mr(port_priv->mr);
  2513. error5:
  2514. ib_dealloc_pd(port_priv->pd);
  2515. error4:
  2516. ib_destroy_cq(port_priv->cq);
  2517. cleanup_recv_queue(&port_priv->qp_info[1]);
  2518. cleanup_recv_queue(&port_priv->qp_info[0]);
  2519. error3:
  2520. kfree(port_priv);
  2521. return ret;
  2522. }
  2523. /*
  2524. * Close the port
  2525. * If there are no classes using the port, free the port
  2526. * resources (CQ, MR, PD, QP) and remove the port's info structure
  2527. */
  2528. static int ib_mad_port_close(struct ib_device *device, int port_num)
  2529. {
  2530. struct ib_mad_port_private *port_priv;
  2531. unsigned long flags;
  2532. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2533. port_priv = __ib_get_mad_port(device, port_num);
  2534. if (port_priv == NULL) {
  2535. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2536. printk(KERN_ERR PFX "Port %d not found\n", port_num);
  2537. return -ENODEV;
  2538. }
  2539. list_del_init(&port_priv->port_list);
  2540. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2541. destroy_workqueue(port_priv->wq);
  2542. destroy_mad_qp(&port_priv->qp_info[1]);
  2543. destroy_mad_qp(&port_priv->qp_info[0]);
  2544. ib_dereg_mr(port_priv->mr);
  2545. ib_dealloc_pd(port_priv->pd);
  2546. ib_destroy_cq(port_priv->cq);
  2547. cleanup_recv_queue(&port_priv->qp_info[1]);
  2548. cleanup_recv_queue(&port_priv->qp_info[0]);
  2549. /* XXX: Handle deallocation of MAD registration tables */
  2550. kfree(port_priv);
  2551. return 0;
  2552. }
  2553. static void ib_mad_init_device(struct ib_device *device)
  2554. {
  2555. int start, end, i;
  2556. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  2557. return;
  2558. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2559. start = 0;
  2560. end = 0;
  2561. } else {
  2562. start = 1;
  2563. end = device->phys_port_cnt;
  2564. }
  2565. for (i = start; i <= end; i++) {
  2566. if (ib_mad_port_open(device, i)) {
  2567. printk(KERN_ERR PFX "Couldn't open %s port %d\n",
  2568. device->name, i);
  2569. goto error;
  2570. }
  2571. if (ib_agent_port_open(device, i)) {
  2572. printk(KERN_ERR PFX "Couldn't open %s port %d "
  2573. "for agents\n",
  2574. device->name, i);
  2575. goto error_agent;
  2576. }
  2577. }
  2578. return;
  2579. error_agent:
  2580. if (ib_mad_port_close(device, i))
  2581. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2582. device->name, i);
  2583. error:
  2584. i--;
  2585. while (i >= start) {
  2586. if (ib_agent_port_close(device, i))
  2587. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2588. "for agents\n",
  2589. device->name, i);
  2590. if (ib_mad_port_close(device, i))
  2591. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2592. device->name, i);
  2593. i--;
  2594. }
  2595. }
  2596. static void ib_mad_remove_device(struct ib_device *device)
  2597. {
  2598. int i, num_ports, cur_port;
  2599. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2600. num_ports = 1;
  2601. cur_port = 0;
  2602. } else {
  2603. num_ports = device->phys_port_cnt;
  2604. cur_port = 1;
  2605. }
  2606. for (i = 0; i < num_ports; i++, cur_port++) {
  2607. if (ib_agent_port_close(device, cur_port))
  2608. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2609. "for agents\n",
  2610. device->name, cur_port);
  2611. if (ib_mad_port_close(device, cur_port))
  2612. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2613. device->name, cur_port);
  2614. }
  2615. }
  2616. static struct ib_client mad_client = {
  2617. .name = "mad",
  2618. .add = ib_mad_init_device,
  2619. .remove = ib_mad_remove_device
  2620. };
  2621. static int __init ib_mad_init_module(void)
  2622. {
  2623. int ret;
  2624. spin_lock_init(&ib_mad_port_list_lock);
  2625. ib_mad_cache = kmem_cache_create("ib_mad",
  2626. sizeof(struct ib_mad_private),
  2627. 0,
  2628. SLAB_HWCACHE_ALIGN,
  2629. NULL,
  2630. NULL);
  2631. if (!ib_mad_cache) {
  2632. printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
  2633. ret = -ENOMEM;
  2634. goto error1;
  2635. }
  2636. INIT_LIST_HEAD(&ib_mad_port_list);
  2637. if (ib_register_client(&mad_client)) {
  2638. printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
  2639. ret = -EINVAL;
  2640. goto error2;
  2641. }
  2642. return 0;
  2643. error2:
  2644. kmem_cache_destroy(ib_mad_cache);
  2645. error1:
  2646. return ret;
  2647. }
  2648. static void __exit ib_mad_cleanup_module(void)
  2649. {
  2650. ib_unregister_client(&mad_client);
  2651. kmem_cache_destroy(ib_mad_cache);
  2652. }
  2653. module_init(ib_mad_init_module);
  2654. module_exit(ib_mad_cleanup_module);