mad.c 88 KB

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