mad.c 83 KB

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