mad.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995
  1. /*
  2. * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
  35. */
  36. #include <linux/dma-mapping.h>
  37. #include <rdma/ib_cache.h>
  38. #include "mad_priv.h"
  39. #include "mad_rmpp.h"
  40. #include "smi.h"
  41. #include "agent.h"
  42. MODULE_LICENSE("Dual BSD/GPL");
  43. MODULE_DESCRIPTION("kernel IB MAD API");
  44. MODULE_AUTHOR("Hal Rosenstock");
  45. MODULE_AUTHOR("Sean Hefty");
  46. static kmem_cache_t *ib_mad_cache;
  47. static struct list_head ib_mad_port_list;
  48. static u32 ib_mad_client_id = 0;
  49. /* Port list lock */
  50. static spinlock_t ib_mad_port_list_lock;
  51. /* Forward declarations */
  52. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  53. struct ib_mad_reg_req *mad_reg_req);
  54. static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
  55. static struct ib_mad_agent_private *find_mad_agent(
  56. struct ib_mad_port_private *port_priv,
  57. struct ib_mad *mad);
  58. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  59. struct ib_mad_private *mad);
  60. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
  61. static void timeout_sends(void *data);
  62. static void local_completions(void *data);
  63. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  64. struct ib_mad_agent_private *agent_priv,
  65. u8 mgmt_class);
  66. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  67. struct ib_mad_agent_private *agent_priv);
  68. /*
  69. * Returns a ib_mad_port_private structure or NULL for a device/port
  70. * Assumes ib_mad_port_list_lock is being held
  71. */
  72. static inline struct ib_mad_port_private *
  73. __ib_get_mad_port(struct ib_device *device, int port_num)
  74. {
  75. struct ib_mad_port_private *entry;
  76. list_for_each_entry(entry, &ib_mad_port_list, port_list) {
  77. if (entry->device == device && entry->port_num == port_num)
  78. return entry;
  79. }
  80. return NULL;
  81. }
  82. /*
  83. * Wrapper function to return a ib_mad_port_private structure or NULL
  84. * for a device/port
  85. */
  86. static inline struct ib_mad_port_private *
  87. ib_get_mad_port(struct ib_device *device, int port_num)
  88. {
  89. struct ib_mad_port_private *entry;
  90. unsigned long flags;
  91. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  92. entry = __ib_get_mad_port(device, port_num);
  93. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  94. return entry;
  95. }
  96. static inline u8 convert_mgmt_class(u8 mgmt_class)
  97. {
  98. /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
  99. return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
  100. 0 : mgmt_class;
  101. }
  102. static int get_spl_qp_index(enum ib_qp_type qp_type)
  103. {
  104. switch (qp_type)
  105. {
  106. case IB_QPT_SMI:
  107. return 0;
  108. case IB_QPT_GSI:
  109. return 1;
  110. default:
  111. return -1;
  112. }
  113. }
  114. static int vendor_class_index(u8 mgmt_class)
  115. {
  116. return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
  117. }
  118. static int is_vendor_class(u8 mgmt_class)
  119. {
  120. if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
  121. (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
  122. return 0;
  123. return 1;
  124. }
  125. static int is_vendor_oui(char *oui)
  126. {
  127. if (oui[0] || oui[1] || oui[2])
  128. return 1;
  129. return 0;
  130. }
  131. static int is_vendor_method_in_use(
  132. struct ib_mad_mgmt_vendor_class *vendor_class,
  133. struct ib_mad_reg_req *mad_reg_req)
  134. {
  135. struct ib_mad_mgmt_method_table *method;
  136. int i;
  137. for (i = 0; i < MAX_MGMT_OUI; i++) {
  138. if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
  139. method = vendor_class->method_table[i];
  140. if (method) {
  141. if (method_in_use(&method, mad_reg_req))
  142. return 1;
  143. else
  144. break;
  145. }
  146. }
  147. }
  148. return 0;
  149. }
  150. int ib_response_mad(struct ib_mad *mad)
  151. {
  152. return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
  153. (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
  154. ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
  155. (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
  156. }
  157. EXPORT_SYMBOL(ib_response_mad);
  158. /*
  159. * ib_register_mad_agent - Register to send/receive MADs
  160. */
  161. struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
  162. u8 port_num,
  163. enum ib_qp_type qp_type,
  164. struct ib_mad_reg_req *mad_reg_req,
  165. u8 rmpp_version,
  166. ib_mad_send_handler send_handler,
  167. ib_mad_recv_handler recv_handler,
  168. void *context)
  169. {
  170. struct ib_mad_port_private *port_priv;
  171. struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
  172. struct ib_mad_agent_private *mad_agent_priv;
  173. struct ib_mad_reg_req *reg_req = NULL;
  174. struct ib_mad_mgmt_class_table *class;
  175. struct ib_mad_mgmt_vendor_class_table *vendor;
  176. struct ib_mad_mgmt_vendor_class *vendor_class;
  177. struct ib_mad_mgmt_method_table *method;
  178. int ret2, qpn;
  179. unsigned long flags;
  180. u8 mgmt_class, vclass;
  181. /* Validate parameters */
  182. qpn = get_spl_qp_index(qp_type);
  183. if (qpn == -1)
  184. goto error1;
  185. if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
  186. goto error1;
  187. /* Validate MAD registration request if supplied */
  188. if (mad_reg_req) {
  189. if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
  190. goto error1;
  191. if (!recv_handler)
  192. goto error1;
  193. if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
  194. /*
  195. * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
  196. * one in this range currently allowed
  197. */
  198. if (mad_reg_req->mgmt_class !=
  199. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  200. goto error1;
  201. } else if (mad_reg_req->mgmt_class == 0) {
  202. /*
  203. * Class 0 is reserved in IBA and is used for
  204. * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  205. */
  206. goto error1;
  207. } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
  208. /*
  209. * If class is in "new" vendor range,
  210. * ensure supplied OUI is not zero
  211. */
  212. if (!is_vendor_oui(mad_reg_req->oui))
  213. goto error1;
  214. }
  215. /* Make sure class supplied is consistent with RMPP */
  216. if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
  217. if (rmpp_version)
  218. goto error1;
  219. }
  220. /* Make sure class supplied is consistent with QP type */
  221. if (qp_type == IB_QPT_SMI) {
  222. if ((mad_reg_req->mgmt_class !=
  223. IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
  224. (mad_reg_req->mgmt_class !=
  225. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  226. goto error1;
  227. } else {
  228. if ((mad_reg_req->mgmt_class ==
  229. IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  230. (mad_reg_req->mgmt_class ==
  231. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  232. goto error1;
  233. }
  234. } else {
  235. /* No registration request supplied */
  236. if (!send_handler)
  237. goto error1;
  238. }
  239. /* Validate device and port */
  240. port_priv = ib_get_mad_port(device, port_num);
  241. if (!port_priv) {
  242. ret = ERR_PTR(-ENODEV);
  243. goto error1;
  244. }
  245. /* Allocate structures */
  246. mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
  247. if (!mad_agent_priv) {
  248. ret = ERR_PTR(-ENOMEM);
  249. goto error1;
  250. }
  251. mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
  252. IB_ACCESS_LOCAL_WRITE);
  253. if (IS_ERR(mad_agent_priv->agent.mr)) {
  254. ret = ERR_PTR(-ENOMEM);
  255. goto error2;
  256. }
  257. if (mad_reg_req) {
  258. reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
  259. if (!reg_req) {
  260. ret = ERR_PTR(-ENOMEM);
  261. goto error3;
  262. }
  263. /* Make a copy of the MAD registration request */
  264. memcpy(reg_req, mad_reg_req, sizeof *reg_req);
  265. }
  266. /* Now, fill in the various structures */
  267. mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
  268. mad_agent_priv->reg_req = reg_req;
  269. mad_agent_priv->agent.rmpp_version = rmpp_version;
  270. mad_agent_priv->agent.device = device;
  271. mad_agent_priv->agent.recv_handler = recv_handler;
  272. mad_agent_priv->agent.send_handler = send_handler;
  273. mad_agent_priv->agent.context = context;
  274. mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
  275. mad_agent_priv->agent.port_num = port_num;
  276. spin_lock_irqsave(&port_priv->reg_lock, flags);
  277. mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
  278. /*
  279. * Make sure MAD registration (if supplied)
  280. * is non overlapping with any existing ones
  281. */
  282. if (mad_reg_req) {
  283. mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
  284. if (!is_vendor_class(mgmt_class)) {
  285. class = port_priv->version[mad_reg_req->
  286. mgmt_class_version].class;
  287. if (class) {
  288. method = class->method_table[mgmt_class];
  289. if (method) {
  290. if (method_in_use(&method,
  291. mad_reg_req))
  292. goto error4;
  293. }
  294. }
  295. ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
  296. mgmt_class);
  297. } else {
  298. /* "New" vendor class range */
  299. vendor = port_priv->version[mad_reg_req->
  300. mgmt_class_version].vendor;
  301. if (vendor) {
  302. vclass = vendor_class_index(mgmt_class);
  303. vendor_class = vendor->vendor_class[vclass];
  304. if (vendor_class) {
  305. if (is_vendor_method_in_use(
  306. vendor_class,
  307. mad_reg_req))
  308. goto error4;
  309. }
  310. }
  311. ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
  312. }
  313. if (ret2) {
  314. ret = ERR_PTR(ret2);
  315. goto error4;
  316. }
  317. }
  318. /* Add mad agent into port's agent list */
  319. list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
  320. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  321. spin_lock_init(&mad_agent_priv->lock);
  322. INIT_LIST_HEAD(&mad_agent_priv->send_list);
  323. INIT_LIST_HEAD(&mad_agent_priv->wait_list);
  324. INIT_LIST_HEAD(&mad_agent_priv->done_list);
  325. INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
  326. INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
  327. INIT_LIST_HEAD(&mad_agent_priv->local_list);
  328. INIT_WORK(&mad_agent_priv->local_work, local_completions,
  329. mad_agent_priv);
  330. atomic_set(&mad_agent_priv->refcount, 1);
  331. init_completion(&mad_agent_priv->comp);
  332. return &mad_agent_priv->agent;
  333. error4:
  334. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  335. kfree(reg_req);
  336. error3:
  337. ib_dereg_mr(mad_agent_priv->agent.mr);
  338. error2:
  339. kfree(mad_agent_priv);
  340. error1:
  341. return ret;
  342. }
  343. EXPORT_SYMBOL(ib_register_mad_agent);
  344. static inline int is_snooping_sends(int mad_snoop_flags)
  345. {
  346. return (mad_snoop_flags &
  347. (/*IB_MAD_SNOOP_POSTED_SENDS |
  348. IB_MAD_SNOOP_RMPP_SENDS |*/
  349. IB_MAD_SNOOP_SEND_COMPLETIONS /*|
  350. IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
  351. }
  352. static inline int is_snooping_recvs(int mad_snoop_flags)
  353. {
  354. return (mad_snoop_flags &
  355. (IB_MAD_SNOOP_RECVS /*|
  356. IB_MAD_SNOOP_RMPP_RECVS*/));
  357. }
  358. static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
  359. struct ib_mad_snoop_private *mad_snoop_priv)
  360. {
  361. struct ib_mad_snoop_private **new_snoop_table;
  362. unsigned long flags;
  363. int i;
  364. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  365. /* Check for empty slot in array. */
  366. for (i = 0; i < qp_info->snoop_table_size; i++)
  367. if (!qp_info->snoop_table[i])
  368. break;
  369. if (i == qp_info->snoop_table_size) {
  370. /* Grow table. */
  371. new_snoop_table = kmalloc(sizeof mad_snoop_priv *
  372. qp_info->snoop_table_size + 1,
  373. GFP_ATOMIC);
  374. if (!new_snoop_table) {
  375. i = -ENOMEM;
  376. goto out;
  377. }
  378. if (qp_info->snoop_table) {
  379. memcpy(new_snoop_table, qp_info->snoop_table,
  380. sizeof mad_snoop_priv *
  381. qp_info->snoop_table_size);
  382. kfree(qp_info->snoop_table);
  383. }
  384. qp_info->snoop_table = new_snoop_table;
  385. qp_info->snoop_table_size++;
  386. }
  387. qp_info->snoop_table[i] = mad_snoop_priv;
  388. atomic_inc(&qp_info->snoop_count);
  389. out:
  390. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  391. return i;
  392. }
  393. struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
  394. u8 port_num,
  395. enum ib_qp_type qp_type,
  396. int mad_snoop_flags,
  397. ib_mad_snoop_handler snoop_handler,
  398. ib_mad_recv_handler recv_handler,
  399. void *context)
  400. {
  401. struct ib_mad_port_private *port_priv;
  402. struct ib_mad_agent *ret;
  403. struct ib_mad_snoop_private *mad_snoop_priv;
  404. int qpn;
  405. /* Validate parameters */
  406. if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
  407. (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
  408. ret = ERR_PTR(-EINVAL);
  409. goto error1;
  410. }
  411. qpn = get_spl_qp_index(qp_type);
  412. if (qpn == -1) {
  413. ret = ERR_PTR(-EINVAL);
  414. goto error1;
  415. }
  416. port_priv = ib_get_mad_port(device, port_num);
  417. if (!port_priv) {
  418. ret = ERR_PTR(-ENODEV);
  419. goto error1;
  420. }
  421. /* Allocate structures */
  422. mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
  423. if (!mad_snoop_priv) {
  424. ret = ERR_PTR(-ENOMEM);
  425. goto error1;
  426. }
  427. /* Now, fill in the various structures */
  428. mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
  429. mad_snoop_priv->agent.device = device;
  430. mad_snoop_priv->agent.recv_handler = recv_handler;
  431. mad_snoop_priv->agent.snoop_handler = snoop_handler;
  432. mad_snoop_priv->agent.context = context;
  433. mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
  434. mad_snoop_priv->agent.port_num = port_num;
  435. mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
  436. init_completion(&mad_snoop_priv->comp);
  437. mad_snoop_priv->snoop_index = register_snoop_agent(
  438. &port_priv->qp_info[qpn],
  439. mad_snoop_priv);
  440. if (mad_snoop_priv->snoop_index < 0) {
  441. ret = ERR_PTR(mad_snoop_priv->snoop_index);
  442. goto error2;
  443. }
  444. atomic_set(&mad_snoop_priv->refcount, 1);
  445. return &mad_snoop_priv->agent;
  446. error2:
  447. kfree(mad_snoop_priv);
  448. error1:
  449. return ret;
  450. }
  451. EXPORT_SYMBOL(ib_register_mad_snoop);
  452. static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  453. {
  454. if (atomic_dec_and_test(&mad_agent_priv->refcount))
  455. complete(&mad_agent_priv->comp);
  456. }
  457. static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
  458. {
  459. if (atomic_dec_and_test(&mad_snoop_priv->refcount))
  460. complete(&mad_snoop_priv->comp);
  461. }
  462. static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  463. {
  464. struct ib_mad_port_private *port_priv;
  465. unsigned long flags;
  466. /* Note that we could still be handling received MADs */
  467. /*
  468. * Canceling all sends results in dropping received response
  469. * MADs, preventing us from queuing additional work
  470. */
  471. cancel_mads(mad_agent_priv);
  472. port_priv = mad_agent_priv->qp_info->port_priv;
  473. cancel_delayed_work(&mad_agent_priv->timed_work);
  474. spin_lock_irqsave(&port_priv->reg_lock, flags);
  475. remove_mad_reg_req(mad_agent_priv);
  476. list_del(&mad_agent_priv->agent_list);
  477. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  478. flush_workqueue(port_priv->wq);
  479. ib_cancel_rmpp_recvs(mad_agent_priv);
  480. deref_mad_agent(mad_agent_priv);
  481. wait_for_completion(&mad_agent_priv->comp);
  482. kfree(mad_agent_priv->reg_req);
  483. ib_dereg_mr(mad_agent_priv->agent.mr);
  484. kfree(mad_agent_priv);
  485. }
  486. static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
  487. {
  488. struct ib_mad_qp_info *qp_info;
  489. unsigned long flags;
  490. qp_info = mad_snoop_priv->qp_info;
  491. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  492. qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
  493. atomic_dec(&qp_info->snoop_count);
  494. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  495. deref_snoop_agent(mad_snoop_priv);
  496. wait_for_completion(&mad_snoop_priv->comp);
  497. kfree(mad_snoop_priv);
  498. }
  499. /*
  500. * ib_unregister_mad_agent - Unregisters a client from using MAD services
  501. */
  502. int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
  503. {
  504. struct ib_mad_agent_private *mad_agent_priv;
  505. struct ib_mad_snoop_private *mad_snoop_priv;
  506. /* If the TID is zero, the agent can only snoop. */
  507. if (mad_agent->hi_tid) {
  508. mad_agent_priv = container_of(mad_agent,
  509. struct ib_mad_agent_private,
  510. agent);
  511. unregister_mad_agent(mad_agent_priv);
  512. } else {
  513. mad_snoop_priv = container_of(mad_agent,
  514. struct ib_mad_snoop_private,
  515. agent);
  516. unregister_mad_snoop(mad_snoop_priv);
  517. }
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(ib_unregister_mad_agent);
  521. static void dequeue_mad(struct ib_mad_list_head *mad_list)
  522. {
  523. struct ib_mad_queue *mad_queue;
  524. unsigned long flags;
  525. BUG_ON(!mad_list->mad_queue);
  526. mad_queue = mad_list->mad_queue;
  527. spin_lock_irqsave(&mad_queue->lock, flags);
  528. list_del(&mad_list->list);
  529. mad_queue->count--;
  530. spin_unlock_irqrestore(&mad_queue->lock, flags);
  531. }
  532. static void snoop_send(struct ib_mad_qp_info *qp_info,
  533. struct ib_mad_send_buf *send_buf,
  534. struct ib_mad_send_wc *mad_send_wc,
  535. int mad_snoop_flags)
  536. {
  537. struct ib_mad_snoop_private *mad_snoop_priv;
  538. unsigned long flags;
  539. int i;
  540. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  541. for (i = 0; i < qp_info->snoop_table_size; i++) {
  542. mad_snoop_priv = qp_info->snoop_table[i];
  543. if (!mad_snoop_priv ||
  544. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  545. continue;
  546. atomic_inc(&mad_snoop_priv->refcount);
  547. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  548. mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
  549. send_buf, mad_send_wc);
  550. deref_snoop_agent(mad_snoop_priv);
  551. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  552. }
  553. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  554. }
  555. static void snoop_recv(struct ib_mad_qp_info *qp_info,
  556. struct ib_mad_recv_wc *mad_recv_wc,
  557. int mad_snoop_flags)
  558. {
  559. struct ib_mad_snoop_private *mad_snoop_priv;
  560. unsigned long flags;
  561. int i;
  562. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  563. for (i = 0; i < qp_info->snoop_table_size; i++) {
  564. mad_snoop_priv = qp_info->snoop_table[i];
  565. if (!mad_snoop_priv ||
  566. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  567. continue;
  568. atomic_inc(&mad_snoop_priv->refcount);
  569. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  570. mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
  571. mad_recv_wc);
  572. deref_snoop_agent(mad_snoop_priv);
  573. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  574. }
  575. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  576. }
  577. static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
  578. struct ib_wc *wc)
  579. {
  580. memset(wc, 0, sizeof *wc);
  581. wc->wr_id = wr_id;
  582. wc->status = IB_WC_SUCCESS;
  583. wc->opcode = IB_WC_RECV;
  584. wc->pkey_index = pkey_index;
  585. wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
  586. wc->src_qp = IB_QP0;
  587. wc->qp_num = IB_QP0;
  588. wc->slid = slid;
  589. wc->sl = 0;
  590. wc->dlid_path_bits = 0;
  591. wc->port_num = port_num;
  592. }
  593. /*
  594. * Return 0 if SMP is to be sent
  595. * Return 1 if SMP was consumed locally (whether or not solicited)
  596. * Return < 0 if error
  597. */
  598. static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
  599. struct ib_mad_send_wr_private *mad_send_wr)
  600. {
  601. int ret;
  602. struct ib_smp *smp = mad_send_wr->send_buf.mad;
  603. unsigned long flags;
  604. struct ib_mad_local_private *local;
  605. struct ib_mad_private *mad_priv;
  606. struct ib_mad_port_private *port_priv;
  607. struct ib_mad_agent_private *recv_mad_agent = NULL;
  608. struct ib_device *device = mad_agent_priv->agent.device;
  609. u8 port_num = mad_agent_priv->agent.port_num;
  610. struct ib_wc mad_wc;
  611. struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
  612. /*
  613. * Directed route handling starts if the initial LID routed part of
  614. * a request or the ending LID routed part of a response is empty.
  615. * If we are at the start of the LID routed part, don't update the
  616. * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
  617. */
  618. if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
  619. IB_LID_PERMISSIVE &&
  620. !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
  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. ret = smi_check_local_smp(smp, device);
  627. if (!ret)
  628. goto out;
  629. local = kmalloc(sizeof *local, GFP_ATOMIC);
  630. if (!local) {
  631. ret = -ENOMEM;
  632. printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
  633. goto out;
  634. }
  635. local->mad_priv = NULL;
  636. local->recv_mad_agent = NULL;
  637. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
  638. if (!mad_priv) {
  639. ret = -ENOMEM;
  640. printk(KERN_ERR PFX "No memory for local response MAD\n");
  641. kfree(local);
  642. goto out;
  643. }
  644. build_smp_wc(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 = dma_map_single(mad_agent->device->dma_device,
  897. mad_send_wr->send_buf.mad,
  898. sge[0].length,
  899. DMA_TO_DEVICE);
  900. pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
  901. sge[1].addr = dma_map_single(mad_agent->device->dma_device,
  902. ib_get_payload(mad_send_wr),
  903. sge[1].length,
  904. DMA_TO_DEVICE);
  905. pci_unmap_addr_set(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. dma_unmap_single(mad_agent->device->dma_device,
  922. pci_unmap_addr(mad_send_wr, header_mapping),
  923. sge[0].length, DMA_TO_DEVICE);
  924. dma_unmap_single(mad_agent->device->dma_device,
  925. pci_unmap_addr(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;
  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. dma_unmap_single(port_priv->device->dma_device,
  1656. pci_unmap_addr(&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. goto out;
  1678. if (!smi_check_forward_dr_smp(&recv->mad.smp))
  1679. goto local;
  1680. if (!smi_handle_dr_smp_send(&recv->mad.smp,
  1681. port_priv->device->node_type,
  1682. port_priv->port_num))
  1683. goto out;
  1684. if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
  1685. goto out;
  1686. }
  1687. local:
  1688. /* Give driver "right of first refusal" on incoming MAD */
  1689. if (port_priv->device->process_mad) {
  1690. int ret;
  1691. if (!response) {
  1692. printk(KERN_ERR PFX "No memory for response MAD\n");
  1693. /*
  1694. * Is it better to assume that
  1695. * it wouldn't be processed ?
  1696. */
  1697. goto out;
  1698. }
  1699. ret = port_priv->device->process_mad(port_priv->device, 0,
  1700. port_priv->port_num,
  1701. wc, &recv->grh,
  1702. &recv->mad.mad,
  1703. &response->mad.mad);
  1704. if (ret & IB_MAD_RESULT_SUCCESS) {
  1705. if (ret & IB_MAD_RESULT_CONSUMED)
  1706. goto out;
  1707. if (ret & IB_MAD_RESULT_REPLY) {
  1708. agent_send_response(&response->mad.mad,
  1709. &recv->grh, wc,
  1710. port_priv->device,
  1711. port_priv->port_num,
  1712. qp_info->qp->qp_num);
  1713. goto out;
  1714. }
  1715. }
  1716. }
  1717. mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
  1718. if (mad_agent) {
  1719. ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
  1720. /*
  1721. * recv is freed up in error cases in ib_mad_complete_recv
  1722. * or via recv_handler in ib_mad_complete_recv()
  1723. */
  1724. recv = NULL;
  1725. }
  1726. out:
  1727. /* Post another receive request for this QP */
  1728. if (response) {
  1729. ib_mad_post_receive_mads(qp_info, response);
  1730. if (recv)
  1731. kmem_cache_free(ib_mad_cache, recv);
  1732. } else
  1733. ib_mad_post_receive_mads(qp_info, recv);
  1734. }
  1735. static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
  1736. {
  1737. struct ib_mad_send_wr_private *mad_send_wr;
  1738. unsigned long delay;
  1739. if (list_empty(&mad_agent_priv->wait_list)) {
  1740. cancel_delayed_work(&mad_agent_priv->timed_work);
  1741. } else {
  1742. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  1743. struct ib_mad_send_wr_private,
  1744. agent_list);
  1745. if (time_after(mad_agent_priv->timeout,
  1746. mad_send_wr->timeout)) {
  1747. mad_agent_priv->timeout = mad_send_wr->timeout;
  1748. cancel_delayed_work(&mad_agent_priv->timed_work);
  1749. delay = mad_send_wr->timeout - jiffies;
  1750. if ((long)delay <= 0)
  1751. delay = 1;
  1752. queue_delayed_work(mad_agent_priv->qp_info->
  1753. port_priv->wq,
  1754. &mad_agent_priv->timed_work, delay);
  1755. }
  1756. }
  1757. }
  1758. static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
  1759. {
  1760. struct ib_mad_agent_private *mad_agent_priv;
  1761. struct ib_mad_send_wr_private *temp_mad_send_wr;
  1762. struct list_head *list_item;
  1763. unsigned long delay;
  1764. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1765. list_del(&mad_send_wr->agent_list);
  1766. delay = mad_send_wr->timeout;
  1767. mad_send_wr->timeout += jiffies;
  1768. if (delay) {
  1769. list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
  1770. temp_mad_send_wr = list_entry(list_item,
  1771. struct ib_mad_send_wr_private,
  1772. agent_list);
  1773. if (time_after(mad_send_wr->timeout,
  1774. temp_mad_send_wr->timeout))
  1775. break;
  1776. }
  1777. }
  1778. else
  1779. list_item = &mad_agent_priv->wait_list;
  1780. list_add(&mad_send_wr->agent_list, list_item);
  1781. /* Reschedule a work item if we have a shorter timeout */
  1782. if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
  1783. cancel_delayed_work(&mad_agent_priv->timed_work);
  1784. queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
  1785. &mad_agent_priv->timed_work, delay);
  1786. }
  1787. }
  1788. void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
  1789. int timeout_ms)
  1790. {
  1791. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  1792. wait_for_response(mad_send_wr);
  1793. }
  1794. /*
  1795. * Process a send work completion
  1796. */
  1797. void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
  1798. struct ib_mad_send_wc *mad_send_wc)
  1799. {
  1800. struct ib_mad_agent_private *mad_agent_priv;
  1801. unsigned long flags;
  1802. int ret;
  1803. mad_agent_priv = mad_send_wr->mad_agent_priv;
  1804. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1805. if (mad_agent_priv->agent.rmpp_version) {
  1806. ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
  1807. if (ret == IB_RMPP_RESULT_CONSUMED)
  1808. goto done;
  1809. } else
  1810. ret = IB_RMPP_RESULT_UNHANDLED;
  1811. if (mad_send_wc->status != IB_WC_SUCCESS &&
  1812. mad_send_wr->status == IB_WC_SUCCESS) {
  1813. mad_send_wr->status = mad_send_wc->status;
  1814. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  1815. }
  1816. if (--mad_send_wr->refcount > 0) {
  1817. if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
  1818. mad_send_wr->status == IB_WC_SUCCESS) {
  1819. wait_for_response(mad_send_wr);
  1820. }
  1821. goto done;
  1822. }
  1823. /* Remove send from MAD agent and notify client of completion */
  1824. list_del(&mad_send_wr->agent_list);
  1825. adjust_timeout(mad_agent_priv);
  1826. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1827. if (mad_send_wr->status != IB_WC_SUCCESS )
  1828. mad_send_wc->status = mad_send_wr->status;
  1829. if (ret == IB_RMPP_RESULT_INTERNAL)
  1830. ib_rmpp_send_handler(mad_send_wc);
  1831. else
  1832. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  1833. mad_send_wc);
  1834. /* Release reference on agent taken when sending */
  1835. deref_mad_agent(mad_agent_priv);
  1836. return;
  1837. done:
  1838. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1839. }
  1840. static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
  1841. struct ib_wc *wc)
  1842. {
  1843. struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
  1844. struct ib_mad_list_head *mad_list;
  1845. struct ib_mad_qp_info *qp_info;
  1846. struct ib_mad_queue *send_queue;
  1847. struct ib_send_wr *bad_send_wr;
  1848. struct ib_mad_send_wc mad_send_wc;
  1849. unsigned long flags;
  1850. int ret;
  1851. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1852. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1853. mad_list);
  1854. send_queue = mad_list->mad_queue;
  1855. qp_info = send_queue->qp_info;
  1856. retry:
  1857. dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
  1858. pci_unmap_addr(mad_send_wr, header_mapping),
  1859. mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
  1860. dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
  1861. pci_unmap_addr(mad_send_wr, payload_mapping),
  1862. mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
  1863. queued_send_wr = NULL;
  1864. spin_lock_irqsave(&send_queue->lock, flags);
  1865. list_del(&mad_list->list);
  1866. /* Move queued send to the send queue */
  1867. if (send_queue->count-- > send_queue->max_active) {
  1868. mad_list = container_of(qp_info->overflow_list.next,
  1869. struct ib_mad_list_head, list);
  1870. queued_send_wr = container_of(mad_list,
  1871. struct ib_mad_send_wr_private,
  1872. mad_list);
  1873. list_move_tail(&mad_list->list, &send_queue->list);
  1874. }
  1875. spin_unlock_irqrestore(&send_queue->lock, flags);
  1876. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  1877. mad_send_wc.status = wc->status;
  1878. mad_send_wc.vendor_err = wc->vendor_err;
  1879. if (atomic_read(&qp_info->snoop_count))
  1880. snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
  1881. IB_MAD_SNOOP_SEND_COMPLETIONS);
  1882. ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
  1883. if (queued_send_wr) {
  1884. ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
  1885. &bad_send_wr);
  1886. if (ret) {
  1887. printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
  1888. mad_send_wr = queued_send_wr;
  1889. wc->status = IB_WC_LOC_QP_OP_ERR;
  1890. goto retry;
  1891. }
  1892. }
  1893. }
  1894. static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
  1895. {
  1896. struct ib_mad_send_wr_private *mad_send_wr;
  1897. struct ib_mad_list_head *mad_list;
  1898. unsigned long flags;
  1899. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  1900. list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
  1901. mad_send_wr = container_of(mad_list,
  1902. struct ib_mad_send_wr_private,
  1903. mad_list);
  1904. mad_send_wr->retry = 1;
  1905. }
  1906. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  1907. }
  1908. static void mad_error_handler(struct ib_mad_port_private *port_priv,
  1909. struct ib_wc *wc)
  1910. {
  1911. struct ib_mad_list_head *mad_list;
  1912. struct ib_mad_qp_info *qp_info;
  1913. struct ib_mad_send_wr_private *mad_send_wr;
  1914. int ret;
  1915. /* Determine if failure was a send or receive */
  1916. mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
  1917. qp_info = mad_list->mad_queue->qp_info;
  1918. if (mad_list->mad_queue == &qp_info->recv_queue)
  1919. /*
  1920. * Receive errors indicate that the QP has entered the error
  1921. * state - error handling/shutdown code will cleanup
  1922. */
  1923. return;
  1924. /*
  1925. * Send errors will transition the QP to SQE - move
  1926. * QP to RTS and repost flushed work requests
  1927. */
  1928. mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
  1929. mad_list);
  1930. if (wc->status == IB_WC_WR_FLUSH_ERR) {
  1931. if (mad_send_wr->retry) {
  1932. /* Repost send */
  1933. struct ib_send_wr *bad_send_wr;
  1934. mad_send_wr->retry = 0;
  1935. ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
  1936. &bad_send_wr);
  1937. if (ret)
  1938. ib_mad_send_done_handler(port_priv, wc);
  1939. } else
  1940. ib_mad_send_done_handler(port_priv, wc);
  1941. } else {
  1942. struct ib_qp_attr *attr;
  1943. /* Transition QP to RTS and fail offending send */
  1944. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1945. if (attr) {
  1946. attr->qp_state = IB_QPS_RTS;
  1947. attr->cur_qp_state = IB_QPS_SQE;
  1948. ret = ib_modify_qp(qp_info->qp, attr,
  1949. IB_QP_STATE | IB_QP_CUR_STATE);
  1950. kfree(attr);
  1951. if (ret)
  1952. printk(KERN_ERR PFX "mad_error_handler - "
  1953. "ib_modify_qp to RTS : %d\n", ret);
  1954. else
  1955. mark_sends_for_retry(qp_info);
  1956. }
  1957. ib_mad_send_done_handler(port_priv, wc);
  1958. }
  1959. }
  1960. /*
  1961. * IB MAD completion callback
  1962. */
  1963. static void ib_mad_completion_handler(void *data)
  1964. {
  1965. struct ib_mad_port_private *port_priv;
  1966. struct ib_wc wc;
  1967. port_priv = (struct ib_mad_port_private *)data;
  1968. ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  1969. while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
  1970. if (wc.status == IB_WC_SUCCESS) {
  1971. switch (wc.opcode) {
  1972. case IB_WC_SEND:
  1973. ib_mad_send_done_handler(port_priv, &wc);
  1974. break;
  1975. case IB_WC_RECV:
  1976. ib_mad_recv_done_handler(port_priv, &wc);
  1977. break;
  1978. default:
  1979. BUG_ON(1);
  1980. break;
  1981. }
  1982. } else
  1983. mad_error_handler(port_priv, &wc);
  1984. }
  1985. }
  1986. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
  1987. {
  1988. unsigned long flags;
  1989. struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
  1990. struct ib_mad_send_wc mad_send_wc;
  1991. struct list_head cancel_list;
  1992. INIT_LIST_HEAD(&cancel_list);
  1993. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1994. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  1995. &mad_agent_priv->send_list, agent_list) {
  1996. if (mad_send_wr->status == IB_WC_SUCCESS) {
  1997. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  1998. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  1999. }
  2000. }
  2001. /* Empty wait list to prevent receives from finding a request */
  2002. list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
  2003. /* Empty local completion list as well */
  2004. list_splice_init(&mad_agent_priv->local_list, &cancel_list);
  2005. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2006. /* Report all cancelled requests */
  2007. mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
  2008. mad_send_wc.vendor_err = 0;
  2009. list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
  2010. &cancel_list, agent_list) {
  2011. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2012. list_del(&mad_send_wr->agent_list);
  2013. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2014. &mad_send_wc);
  2015. atomic_dec(&mad_agent_priv->refcount);
  2016. }
  2017. }
  2018. static struct ib_mad_send_wr_private*
  2019. find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
  2020. struct ib_mad_send_buf *send_buf)
  2021. {
  2022. struct ib_mad_send_wr_private *mad_send_wr;
  2023. list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
  2024. agent_list) {
  2025. if (&mad_send_wr->send_buf == send_buf)
  2026. return mad_send_wr;
  2027. }
  2028. list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
  2029. agent_list) {
  2030. if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
  2031. &mad_send_wr->send_buf == send_buf)
  2032. return mad_send_wr;
  2033. }
  2034. return NULL;
  2035. }
  2036. int ib_modify_mad(struct ib_mad_agent *mad_agent,
  2037. struct ib_mad_send_buf *send_buf, u32 timeout_ms)
  2038. {
  2039. struct ib_mad_agent_private *mad_agent_priv;
  2040. struct ib_mad_send_wr_private *mad_send_wr;
  2041. unsigned long flags;
  2042. int active;
  2043. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  2044. agent);
  2045. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2046. mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
  2047. if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
  2048. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2049. return -EINVAL;
  2050. }
  2051. active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
  2052. if (!timeout_ms) {
  2053. mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
  2054. mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
  2055. }
  2056. mad_send_wr->send_buf.timeout_ms = timeout_ms;
  2057. if (active)
  2058. mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
  2059. else
  2060. ib_reset_mad_timeout(mad_send_wr, timeout_ms);
  2061. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2062. return 0;
  2063. }
  2064. EXPORT_SYMBOL(ib_modify_mad);
  2065. void ib_cancel_mad(struct ib_mad_agent *mad_agent,
  2066. struct ib_mad_send_buf *send_buf)
  2067. {
  2068. ib_modify_mad(mad_agent, send_buf, 0);
  2069. }
  2070. EXPORT_SYMBOL(ib_cancel_mad);
  2071. static void local_completions(void *data)
  2072. {
  2073. struct ib_mad_agent_private *mad_agent_priv;
  2074. struct ib_mad_local_private *local;
  2075. struct ib_mad_agent_private *recv_mad_agent;
  2076. unsigned long flags;
  2077. int recv = 0;
  2078. struct ib_wc wc;
  2079. struct ib_mad_send_wc mad_send_wc;
  2080. mad_agent_priv = (struct ib_mad_agent_private *)data;
  2081. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2082. while (!list_empty(&mad_agent_priv->local_list)) {
  2083. local = list_entry(mad_agent_priv->local_list.next,
  2084. struct ib_mad_local_private,
  2085. completion_list);
  2086. list_del(&local->completion_list);
  2087. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2088. if (local->mad_priv) {
  2089. recv_mad_agent = local->recv_mad_agent;
  2090. if (!recv_mad_agent) {
  2091. printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
  2092. goto local_send_completion;
  2093. }
  2094. recv = 1;
  2095. /*
  2096. * Defined behavior is to complete response
  2097. * before request
  2098. */
  2099. build_smp_wc((unsigned long) local->mad_send_wr,
  2100. be16_to_cpu(IB_LID_PERMISSIVE),
  2101. 0, recv_mad_agent->agent.port_num, &wc);
  2102. local->mad_priv->header.recv_wc.wc = &wc;
  2103. local->mad_priv->header.recv_wc.mad_len =
  2104. sizeof(struct ib_mad);
  2105. INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
  2106. list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
  2107. &local->mad_priv->header.recv_wc.rmpp_list);
  2108. local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
  2109. local->mad_priv->header.recv_wc.recv_buf.mad =
  2110. &local->mad_priv->mad.mad;
  2111. if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
  2112. snoop_recv(recv_mad_agent->qp_info,
  2113. &local->mad_priv->header.recv_wc,
  2114. IB_MAD_SNOOP_RECVS);
  2115. recv_mad_agent->agent.recv_handler(
  2116. &recv_mad_agent->agent,
  2117. &local->mad_priv->header.recv_wc);
  2118. spin_lock_irqsave(&recv_mad_agent->lock, flags);
  2119. atomic_dec(&recv_mad_agent->refcount);
  2120. spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
  2121. }
  2122. local_send_completion:
  2123. /* Complete send */
  2124. mad_send_wc.status = IB_WC_SUCCESS;
  2125. mad_send_wc.vendor_err = 0;
  2126. mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
  2127. if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
  2128. snoop_send(mad_agent_priv->qp_info,
  2129. &local->mad_send_wr->send_buf,
  2130. &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
  2131. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2132. &mad_send_wc);
  2133. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2134. atomic_dec(&mad_agent_priv->refcount);
  2135. if (!recv)
  2136. kmem_cache_free(ib_mad_cache, local->mad_priv);
  2137. kfree(local);
  2138. }
  2139. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2140. }
  2141. static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
  2142. {
  2143. int ret;
  2144. if (!mad_send_wr->retries--)
  2145. return -ETIMEDOUT;
  2146. mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
  2147. if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
  2148. ret = ib_retry_rmpp(mad_send_wr);
  2149. switch (ret) {
  2150. case IB_RMPP_RESULT_UNHANDLED:
  2151. ret = ib_send_mad(mad_send_wr);
  2152. break;
  2153. case IB_RMPP_RESULT_CONSUMED:
  2154. ret = 0;
  2155. break;
  2156. default:
  2157. ret = -ECOMM;
  2158. break;
  2159. }
  2160. } else
  2161. ret = ib_send_mad(mad_send_wr);
  2162. if (!ret) {
  2163. mad_send_wr->refcount++;
  2164. list_add_tail(&mad_send_wr->agent_list,
  2165. &mad_send_wr->mad_agent_priv->send_list);
  2166. }
  2167. return ret;
  2168. }
  2169. static void timeout_sends(void *data)
  2170. {
  2171. struct ib_mad_agent_private *mad_agent_priv;
  2172. struct ib_mad_send_wr_private *mad_send_wr;
  2173. struct ib_mad_send_wc mad_send_wc;
  2174. unsigned long flags, delay;
  2175. mad_agent_priv = (struct ib_mad_agent_private *)data;
  2176. mad_send_wc.vendor_err = 0;
  2177. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2178. while (!list_empty(&mad_agent_priv->wait_list)) {
  2179. mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
  2180. struct ib_mad_send_wr_private,
  2181. agent_list);
  2182. if (time_after(mad_send_wr->timeout, jiffies)) {
  2183. delay = mad_send_wr->timeout - jiffies;
  2184. if ((long)delay <= 0)
  2185. delay = 1;
  2186. queue_delayed_work(mad_agent_priv->qp_info->
  2187. port_priv->wq,
  2188. &mad_agent_priv->timed_work, delay);
  2189. break;
  2190. }
  2191. list_del(&mad_send_wr->agent_list);
  2192. if (mad_send_wr->status == IB_WC_SUCCESS &&
  2193. !retry_send(mad_send_wr))
  2194. continue;
  2195. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2196. if (mad_send_wr->status == IB_WC_SUCCESS)
  2197. mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
  2198. else
  2199. mad_send_wc.status = mad_send_wr->status;
  2200. mad_send_wc.send_buf = &mad_send_wr->send_buf;
  2201. mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
  2202. &mad_send_wc);
  2203. atomic_dec(&mad_agent_priv->refcount);
  2204. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  2205. }
  2206. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  2207. }
  2208. static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
  2209. {
  2210. struct ib_mad_port_private *port_priv = cq->cq_context;
  2211. unsigned long flags;
  2212. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2213. if (!list_empty(&port_priv->port_list))
  2214. queue_work(port_priv->wq, &port_priv->work);
  2215. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2216. }
  2217. /*
  2218. * Allocate receive MADs and post receive WRs for them
  2219. */
  2220. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  2221. struct ib_mad_private *mad)
  2222. {
  2223. unsigned long flags;
  2224. int post, ret;
  2225. struct ib_mad_private *mad_priv;
  2226. struct ib_sge sg_list;
  2227. struct ib_recv_wr recv_wr, *bad_recv_wr;
  2228. struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
  2229. /* Initialize common scatter list fields */
  2230. sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
  2231. sg_list.lkey = (*qp_info->port_priv->mr).lkey;
  2232. /* Initialize common receive WR fields */
  2233. recv_wr.next = NULL;
  2234. recv_wr.sg_list = &sg_list;
  2235. recv_wr.num_sge = 1;
  2236. do {
  2237. /* Allocate and map receive buffer */
  2238. if (mad) {
  2239. mad_priv = mad;
  2240. mad = NULL;
  2241. } else {
  2242. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
  2243. if (!mad_priv) {
  2244. printk(KERN_ERR PFX "No memory for receive buffer\n");
  2245. ret = -ENOMEM;
  2246. break;
  2247. }
  2248. }
  2249. sg_list.addr = dma_map_single(qp_info->port_priv->
  2250. device->dma_device,
  2251. &mad_priv->grh,
  2252. sizeof *mad_priv -
  2253. sizeof mad_priv->header,
  2254. DMA_FROM_DEVICE);
  2255. pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
  2256. recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
  2257. mad_priv->header.mad_list.mad_queue = recv_queue;
  2258. /* Post receive WR */
  2259. spin_lock_irqsave(&recv_queue->lock, flags);
  2260. post = (++recv_queue->count < recv_queue->max_active);
  2261. list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
  2262. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2263. ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
  2264. if (ret) {
  2265. spin_lock_irqsave(&recv_queue->lock, flags);
  2266. list_del(&mad_priv->header.mad_list.list);
  2267. recv_queue->count--;
  2268. spin_unlock_irqrestore(&recv_queue->lock, flags);
  2269. dma_unmap_single(qp_info->port_priv->device->dma_device,
  2270. pci_unmap_addr(&mad_priv->header,
  2271. mapping),
  2272. sizeof *mad_priv -
  2273. sizeof mad_priv->header,
  2274. DMA_FROM_DEVICE);
  2275. kmem_cache_free(ib_mad_cache, mad_priv);
  2276. printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
  2277. break;
  2278. }
  2279. } while (post);
  2280. return ret;
  2281. }
  2282. /*
  2283. * Return all the posted receive MADs
  2284. */
  2285. static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
  2286. {
  2287. struct ib_mad_private_header *mad_priv_hdr;
  2288. struct ib_mad_private *recv;
  2289. struct ib_mad_list_head *mad_list;
  2290. while (!list_empty(&qp_info->recv_queue.list)) {
  2291. mad_list = list_entry(qp_info->recv_queue.list.next,
  2292. struct ib_mad_list_head, list);
  2293. mad_priv_hdr = container_of(mad_list,
  2294. struct ib_mad_private_header,
  2295. mad_list);
  2296. recv = container_of(mad_priv_hdr, struct ib_mad_private,
  2297. header);
  2298. /* Remove from posted receive MAD list */
  2299. list_del(&mad_list->list);
  2300. dma_unmap_single(qp_info->port_priv->device->dma_device,
  2301. pci_unmap_addr(&recv->header, mapping),
  2302. sizeof(struct ib_mad_private) -
  2303. sizeof(struct ib_mad_private_header),
  2304. DMA_FROM_DEVICE);
  2305. kmem_cache_free(ib_mad_cache, recv);
  2306. }
  2307. qp_info->recv_queue.count = 0;
  2308. }
  2309. /*
  2310. * Start the port
  2311. */
  2312. static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
  2313. {
  2314. int ret, i;
  2315. struct ib_qp_attr *attr;
  2316. struct ib_qp *qp;
  2317. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  2318. if (!attr) {
  2319. printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
  2320. return -ENOMEM;
  2321. }
  2322. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2323. qp = port_priv->qp_info[i].qp;
  2324. /*
  2325. * PKey index for QP1 is irrelevant but
  2326. * one is needed for the Reset to Init transition
  2327. */
  2328. attr->qp_state = IB_QPS_INIT;
  2329. attr->pkey_index = 0;
  2330. attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
  2331. ret = ib_modify_qp(qp, attr, IB_QP_STATE |
  2332. IB_QP_PKEY_INDEX | IB_QP_QKEY);
  2333. if (ret) {
  2334. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2335. "INIT: %d\n", i, ret);
  2336. goto out;
  2337. }
  2338. attr->qp_state = IB_QPS_RTR;
  2339. ret = ib_modify_qp(qp, attr, IB_QP_STATE);
  2340. if (ret) {
  2341. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2342. "RTR: %d\n", i, ret);
  2343. goto out;
  2344. }
  2345. attr->qp_state = IB_QPS_RTS;
  2346. attr->sq_psn = IB_MAD_SEND_Q_PSN;
  2347. ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
  2348. if (ret) {
  2349. printk(KERN_ERR PFX "Couldn't change QP%d state to "
  2350. "RTS: %d\n", i, ret);
  2351. goto out;
  2352. }
  2353. }
  2354. ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
  2355. if (ret) {
  2356. printk(KERN_ERR PFX "Failed to request completion "
  2357. "notification: %d\n", ret);
  2358. goto out;
  2359. }
  2360. for (i = 0; i < IB_MAD_QPS_CORE; i++) {
  2361. ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
  2362. if (ret) {
  2363. printk(KERN_ERR PFX "Couldn't post receive WRs\n");
  2364. goto out;
  2365. }
  2366. }
  2367. out:
  2368. kfree(attr);
  2369. return ret;
  2370. }
  2371. static void qp_event_handler(struct ib_event *event, void *qp_context)
  2372. {
  2373. struct ib_mad_qp_info *qp_info = qp_context;
  2374. /* It's worse than that! He's dead, Jim! */
  2375. printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
  2376. event->event, qp_info->qp->qp_num);
  2377. }
  2378. static void init_mad_queue(struct ib_mad_qp_info *qp_info,
  2379. struct ib_mad_queue *mad_queue)
  2380. {
  2381. mad_queue->qp_info = qp_info;
  2382. mad_queue->count = 0;
  2383. spin_lock_init(&mad_queue->lock);
  2384. INIT_LIST_HEAD(&mad_queue->list);
  2385. }
  2386. static void init_mad_qp(struct ib_mad_port_private *port_priv,
  2387. struct ib_mad_qp_info *qp_info)
  2388. {
  2389. qp_info->port_priv = port_priv;
  2390. init_mad_queue(qp_info, &qp_info->send_queue);
  2391. init_mad_queue(qp_info, &qp_info->recv_queue);
  2392. INIT_LIST_HEAD(&qp_info->overflow_list);
  2393. spin_lock_init(&qp_info->snoop_lock);
  2394. qp_info->snoop_table = NULL;
  2395. qp_info->snoop_table_size = 0;
  2396. atomic_set(&qp_info->snoop_count, 0);
  2397. }
  2398. static int create_mad_qp(struct ib_mad_qp_info *qp_info,
  2399. enum ib_qp_type qp_type)
  2400. {
  2401. struct ib_qp_init_attr qp_init_attr;
  2402. int ret;
  2403. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  2404. qp_init_attr.send_cq = qp_info->port_priv->cq;
  2405. qp_init_attr.recv_cq = qp_info->port_priv->cq;
  2406. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  2407. qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
  2408. qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
  2409. qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
  2410. qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
  2411. qp_init_attr.qp_type = qp_type;
  2412. qp_init_attr.port_num = qp_info->port_priv->port_num;
  2413. qp_init_attr.qp_context = qp_info;
  2414. qp_init_attr.event_handler = qp_event_handler;
  2415. qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
  2416. if (IS_ERR(qp_info->qp)) {
  2417. printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
  2418. get_spl_qp_index(qp_type));
  2419. ret = PTR_ERR(qp_info->qp);
  2420. goto error;
  2421. }
  2422. /* Use minimum queue sizes unless the CQ is resized */
  2423. qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
  2424. qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
  2425. return 0;
  2426. error:
  2427. return ret;
  2428. }
  2429. static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
  2430. {
  2431. ib_destroy_qp(qp_info->qp);
  2432. kfree(qp_info->snoop_table);
  2433. }
  2434. /*
  2435. * Open the port
  2436. * Create the QP, PD, MR, and CQ if needed
  2437. */
  2438. static int ib_mad_port_open(struct ib_device *device,
  2439. int port_num)
  2440. {
  2441. int ret, cq_size;
  2442. struct ib_mad_port_private *port_priv;
  2443. unsigned long flags;
  2444. char name[sizeof "ib_mad123"];
  2445. /* Create new device info */
  2446. port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
  2447. if (!port_priv) {
  2448. printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
  2449. return -ENOMEM;
  2450. }
  2451. port_priv->device = device;
  2452. port_priv->port_num = port_num;
  2453. spin_lock_init(&port_priv->reg_lock);
  2454. INIT_LIST_HEAD(&port_priv->agent_list);
  2455. init_mad_qp(port_priv, &port_priv->qp_info[0]);
  2456. init_mad_qp(port_priv, &port_priv->qp_info[1]);
  2457. cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
  2458. port_priv->cq = ib_create_cq(port_priv->device,
  2459. ib_mad_thread_completion_handler,
  2460. NULL, port_priv, cq_size);
  2461. if (IS_ERR(port_priv->cq)) {
  2462. printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
  2463. ret = PTR_ERR(port_priv->cq);
  2464. goto error3;
  2465. }
  2466. port_priv->pd = ib_alloc_pd(device);
  2467. if (IS_ERR(port_priv->pd)) {
  2468. printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
  2469. ret = PTR_ERR(port_priv->pd);
  2470. goto error4;
  2471. }
  2472. port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
  2473. if (IS_ERR(port_priv->mr)) {
  2474. printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
  2475. ret = PTR_ERR(port_priv->mr);
  2476. goto error5;
  2477. }
  2478. ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
  2479. if (ret)
  2480. goto error6;
  2481. ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
  2482. if (ret)
  2483. goto error7;
  2484. snprintf(name, sizeof name, "ib_mad%d", port_num);
  2485. port_priv->wq = create_singlethread_workqueue(name);
  2486. if (!port_priv->wq) {
  2487. ret = -ENOMEM;
  2488. goto error8;
  2489. }
  2490. INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
  2491. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2492. list_add_tail(&port_priv->port_list, &ib_mad_port_list);
  2493. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2494. ret = ib_mad_port_start(port_priv);
  2495. if (ret) {
  2496. printk(KERN_ERR PFX "Couldn't start port\n");
  2497. goto error9;
  2498. }
  2499. return 0;
  2500. error9:
  2501. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2502. list_del_init(&port_priv->port_list);
  2503. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2504. destroy_workqueue(port_priv->wq);
  2505. error8:
  2506. destroy_mad_qp(&port_priv->qp_info[1]);
  2507. error7:
  2508. destroy_mad_qp(&port_priv->qp_info[0]);
  2509. error6:
  2510. ib_dereg_mr(port_priv->mr);
  2511. error5:
  2512. ib_dealloc_pd(port_priv->pd);
  2513. error4:
  2514. ib_destroy_cq(port_priv->cq);
  2515. cleanup_recv_queue(&port_priv->qp_info[1]);
  2516. cleanup_recv_queue(&port_priv->qp_info[0]);
  2517. error3:
  2518. kfree(port_priv);
  2519. return ret;
  2520. }
  2521. /*
  2522. * Close the port
  2523. * If there are no classes using the port, free the port
  2524. * resources (CQ, MR, PD, QP) and remove the port's info structure
  2525. */
  2526. static int ib_mad_port_close(struct ib_device *device, int port_num)
  2527. {
  2528. struct ib_mad_port_private *port_priv;
  2529. unsigned long flags;
  2530. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  2531. port_priv = __ib_get_mad_port(device, port_num);
  2532. if (port_priv == NULL) {
  2533. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2534. printk(KERN_ERR PFX "Port %d not found\n", port_num);
  2535. return -ENODEV;
  2536. }
  2537. list_del_init(&port_priv->port_list);
  2538. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  2539. destroy_workqueue(port_priv->wq);
  2540. destroy_mad_qp(&port_priv->qp_info[1]);
  2541. destroy_mad_qp(&port_priv->qp_info[0]);
  2542. ib_dereg_mr(port_priv->mr);
  2543. ib_dealloc_pd(port_priv->pd);
  2544. ib_destroy_cq(port_priv->cq);
  2545. cleanup_recv_queue(&port_priv->qp_info[1]);
  2546. cleanup_recv_queue(&port_priv->qp_info[0]);
  2547. /* XXX: Handle deallocation of MAD registration tables */
  2548. kfree(port_priv);
  2549. return 0;
  2550. }
  2551. static void ib_mad_init_device(struct ib_device *device)
  2552. {
  2553. int start, end, i;
  2554. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  2555. return;
  2556. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2557. start = 0;
  2558. end = 0;
  2559. } else {
  2560. start = 1;
  2561. end = device->phys_port_cnt;
  2562. }
  2563. for (i = start; i <= end; i++) {
  2564. if (ib_mad_port_open(device, i)) {
  2565. printk(KERN_ERR PFX "Couldn't open %s port %d\n",
  2566. device->name, i);
  2567. goto error;
  2568. }
  2569. if (ib_agent_port_open(device, i)) {
  2570. printk(KERN_ERR PFX "Couldn't open %s port %d "
  2571. "for agents\n",
  2572. device->name, i);
  2573. goto error_agent;
  2574. }
  2575. }
  2576. return;
  2577. error_agent:
  2578. if (ib_mad_port_close(device, i))
  2579. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2580. device->name, i);
  2581. error:
  2582. i--;
  2583. while (i >= start) {
  2584. if (ib_agent_port_close(device, i))
  2585. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2586. "for agents\n",
  2587. device->name, i);
  2588. if (ib_mad_port_close(device, i))
  2589. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2590. device->name, i);
  2591. i--;
  2592. }
  2593. }
  2594. static void ib_mad_remove_device(struct ib_device *device)
  2595. {
  2596. int i, num_ports, cur_port;
  2597. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2598. num_ports = 1;
  2599. cur_port = 0;
  2600. } else {
  2601. num_ports = device->phys_port_cnt;
  2602. cur_port = 1;
  2603. }
  2604. for (i = 0; i < num_ports; i++, cur_port++) {
  2605. if (ib_agent_port_close(device, cur_port))
  2606. printk(KERN_ERR PFX "Couldn't close %s port %d "
  2607. "for agents\n",
  2608. device->name, cur_port);
  2609. if (ib_mad_port_close(device, cur_port))
  2610. printk(KERN_ERR PFX "Couldn't close %s port %d\n",
  2611. device->name, cur_port);
  2612. }
  2613. }
  2614. static struct ib_client mad_client = {
  2615. .name = "mad",
  2616. .add = ib_mad_init_device,
  2617. .remove = ib_mad_remove_device
  2618. };
  2619. static int __init ib_mad_init_module(void)
  2620. {
  2621. int ret;
  2622. spin_lock_init(&ib_mad_port_list_lock);
  2623. ib_mad_cache = kmem_cache_create("ib_mad",
  2624. sizeof(struct ib_mad_private),
  2625. 0,
  2626. SLAB_HWCACHE_ALIGN,
  2627. NULL,
  2628. NULL);
  2629. if (!ib_mad_cache) {
  2630. printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
  2631. ret = -ENOMEM;
  2632. goto error1;
  2633. }
  2634. INIT_LIST_HEAD(&ib_mad_port_list);
  2635. if (ib_register_client(&mad_client)) {
  2636. printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
  2637. ret = -EINVAL;
  2638. goto error2;
  2639. }
  2640. return 0;
  2641. error2:
  2642. kmem_cache_destroy(ib_mad_cache);
  2643. error1:
  2644. return ret;
  2645. }
  2646. static void __exit ib_mad_cleanup_module(void)
  2647. {
  2648. ib_unregister_client(&mad_client);
  2649. kmem_cache_destroy(ib_mad_cache);
  2650. }
  2651. module_init(ib_mad_init_module);
  2652. module_exit(ib_mad_cleanup_module);