mad.c 77 KB

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