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