mad.c 82 KB

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