mad.c 74 KB

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