mad.c 76 KB

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