mad.c 74 KB

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