mad.c 75 KB

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