mad.c 76 KB

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