mad.c 83 KB

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