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