mad.c 74 KB

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