mad.c 81 KB

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