mad.c 76 KB

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