mad.c 77 KB

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