mad.c 75 KB

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