mad.c 77 KB

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