mad.c 83 KB

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