mad.c 82 KB

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