mad.c 76 KB

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