mad.c 85 KB

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