ipath_driver.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/sched.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/idr.h>
  36. #include <linux/pci.h>
  37. #include <linux/io.h>
  38. #include <linux/delay.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/bitmap.h>
  42. #include <linux/slab.h>
  43. #include "ipath_kernel.h"
  44. #include "ipath_verbs.h"
  45. static void ipath_update_pio_bufs(struct ipath_devdata *);
  46. const char *ipath_get_unit_name(int unit)
  47. {
  48. static char iname[16];
  49. snprintf(iname, sizeof iname, "infinipath%u", unit);
  50. return iname;
  51. }
  52. #define DRIVER_LOAD_MSG "QLogic " IPATH_DRV_NAME " loaded: "
  53. #define PFX IPATH_DRV_NAME ": "
  54. /*
  55. * The size has to be longer than this string, so we can append
  56. * board/chip information to it in the init code.
  57. */
  58. const char ib_ipath_version[] = IPATH_IDSTR "\n";
  59. static struct idr unit_table;
  60. DEFINE_SPINLOCK(ipath_devs_lock);
  61. LIST_HEAD(ipath_dev_list);
  62. wait_queue_head_t ipath_state_wait;
  63. unsigned ipath_debug = __IPATH_INFO;
  64. module_param_named(debug, ipath_debug, uint, S_IWUSR | S_IRUGO);
  65. MODULE_PARM_DESC(debug, "mask for debug prints");
  66. EXPORT_SYMBOL_GPL(ipath_debug);
  67. unsigned ipath_mtu4096 = 1; /* max 4KB IB mtu by default, if supported */
  68. module_param_named(mtu4096, ipath_mtu4096, uint, S_IRUGO);
  69. MODULE_PARM_DESC(mtu4096, "enable MTU of 4096 bytes, if supported");
  70. static unsigned ipath_hol_timeout_ms = 13000;
  71. module_param_named(hol_timeout_ms, ipath_hol_timeout_ms, uint, S_IRUGO);
  72. MODULE_PARM_DESC(hol_timeout_ms,
  73. "duration of user app suspension after link failure");
  74. unsigned ipath_linkrecovery = 1;
  75. module_param_named(linkrecovery, ipath_linkrecovery, uint, S_IWUSR | S_IRUGO);
  76. MODULE_PARM_DESC(linkrecovery, "enable workaround for link recovery issue");
  77. MODULE_LICENSE("GPL");
  78. MODULE_AUTHOR("QLogic <support@qlogic.com>");
  79. MODULE_DESCRIPTION("QLogic InfiniPath driver");
  80. /*
  81. * Table to translate the LINKTRAININGSTATE portion of
  82. * IBCStatus to a human-readable form.
  83. */
  84. const char *ipath_ibcstatus_str[] = {
  85. "Disabled",
  86. "LinkUp",
  87. "PollActive",
  88. "PollQuiet",
  89. "SleepDelay",
  90. "SleepQuiet",
  91. "LState6", /* unused */
  92. "LState7", /* unused */
  93. "CfgDebounce",
  94. "CfgRcvfCfg",
  95. "CfgWaitRmt",
  96. "CfgIdle",
  97. "RecovRetrain",
  98. "CfgTxRevLane", /* unused before IBA7220 */
  99. "RecovWaitRmt",
  100. "RecovIdle",
  101. /* below were added for IBA7220 */
  102. "CfgEnhanced",
  103. "CfgTest",
  104. "CfgWaitRmtTest",
  105. "CfgWaitCfgEnhanced",
  106. "SendTS_T",
  107. "SendTstIdles",
  108. "RcvTS_T",
  109. "SendTst_TS1s",
  110. "LTState18", "LTState19", "LTState1A", "LTState1B",
  111. "LTState1C", "LTState1D", "LTState1E", "LTState1F"
  112. };
  113. static void __devexit ipath_remove_one(struct pci_dev *);
  114. static int __devinit ipath_init_one(struct pci_dev *,
  115. const struct pci_device_id *);
  116. /* Only needed for registration, nothing else needs this info */
  117. #define PCI_VENDOR_ID_PATHSCALE 0x1fc1
  118. #define PCI_DEVICE_ID_INFINIPATH_HT 0xd
  119. /* Number of seconds before our card status check... */
  120. #define STATUS_TIMEOUT 60
  121. static const struct pci_device_id ipath_pci_tbl[] = {
  122. { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_HT) },
  123. { 0, }
  124. };
  125. MODULE_DEVICE_TABLE(pci, ipath_pci_tbl);
  126. static struct pci_driver ipath_driver = {
  127. .name = IPATH_DRV_NAME,
  128. .probe = ipath_init_one,
  129. .remove = __devexit_p(ipath_remove_one),
  130. .id_table = ipath_pci_tbl,
  131. .driver = {
  132. .groups = ipath_driver_attr_groups,
  133. },
  134. };
  135. static inline void read_bars(struct ipath_devdata *dd, struct pci_dev *dev,
  136. u32 *bar0, u32 *bar1)
  137. {
  138. int ret;
  139. ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
  140. if (ret)
  141. ipath_dev_err(dd, "failed to read bar0 before enable: "
  142. "error %d\n", -ret);
  143. ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, bar1);
  144. if (ret)
  145. ipath_dev_err(dd, "failed to read bar1 before enable: "
  146. "error %d\n", -ret);
  147. ipath_dbg("Read bar0 %x bar1 %x\n", *bar0, *bar1);
  148. }
  149. static void ipath_free_devdata(struct pci_dev *pdev,
  150. struct ipath_devdata *dd)
  151. {
  152. unsigned long flags;
  153. pci_set_drvdata(pdev, NULL);
  154. if (dd->ipath_unit != -1) {
  155. spin_lock_irqsave(&ipath_devs_lock, flags);
  156. idr_remove(&unit_table, dd->ipath_unit);
  157. list_del(&dd->ipath_list);
  158. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  159. }
  160. vfree(dd);
  161. }
  162. static struct ipath_devdata *ipath_alloc_devdata(struct pci_dev *pdev)
  163. {
  164. unsigned long flags;
  165. struct ipath_devdata *dd;
  166. int ret;
  167. if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
  168. dd = ERR_PTR(-ENOMEM);
  169. goto bail;
  170. }
  171. dd = vzalloc(sizeof(*dd));
  172. if (!dd) {
  173. dd = ERR_PTR(-ENOMEM);
  174. goto bail;
  175. }
  176. dd->ipath_unit = -1;
  177. spin_lock_irqsave(&ipath_devs_lock, flags);
  178. ret = idr_get_new(&unit_table, dd, &dd->ipath_unit);
  179. if (ret < 0) {
  180. printk(KERN_ERR IPATH_DRV_NAME
  181. ": Could not allocate unit ID: error %d\n", -ret);
  182. ipath_free_devdata(pdev, dd);
  183. dd = ERR_PTR(ret);
  184. goto bail_unlock;
  185. }
  186. dd->pcidev = pdev;
  187. pci_set_drvdata(pdev, dd);
  188. list_add(&dd->ipath_list, &ipath_dev_list);
  189. bail_unlock:
  190. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  191. bail:
  192. return dd;
  193. }
  194. static inline struct ipath_devdata *__ipath_lookup(int unit)
  195. {
  196. return idr_find(&unit_table, unit);
  197. }
  198. struct ipath_devdata *ipath_lookup(int unit)
  199. {
  200. struct ipath_devdata *dd;
  201. unsigned long flags;
  202. spin_lock_irqsave(&ipath_devs_lock, flags);
  203. dd = __ipath_lookup(unit);
  204. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  205. return dd;
  206. }
  207. int ipath_count_units(int *npresentp, int *nupp, int *maxportsp)
  208. {
  209. int nunits, npresent, nup;
  210. struct ipath_devdata *dd;
  211. unsigned long flags;
  212. int maxports;
  213. nunits = npresent = nup = maxports = 0;
  214. spin_lock_irqsave(&ipath_devs_lock, flags);
  215. list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
  216. nunits++;
  217. if ((dd->ipath_flags & IPATH_PRESENT) && dd->ipath_kregbase)
  218. npresent++;
  219. if (dd->ipath_lid &&
  220. !(dd->ipath_flags & (IPATH_DISABLED | IPATH_LINKDOWN
  221. | IPATH_LINKUNK)))
  222. nup++;
  223. if (dd->ipath_cfgports > maxports)
  224. maxports = dd->ipath_cfgports;
  225. }
  226. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  227. if (npresentp)
  228. *npresentp = npresent;
  229. if (nupp)
  230. *nupp = nup;
  231. if (maxportsp)
  232. *maxportsp = maxports;
  233. return nunits;
  234. }
  235. /*
  236. * These next two routines are placeholders in case we don't have per-arch
  237. * code for controlling write combining. If explicit control of write
  238. * combining is not available, performance will probably be awful.
  239. */
  240. int __attribute__((weak)) ipath_enable_wc(struct ipath_devdata *dd)
  241. {
  242. return -EOPNOTSUPP;
  243. }
  244. void __attribute__((weak)) ipath_disable_wc(struct ipath_devdata *dd)
  245. {
  246. }
  247. /*
  248. * Perform a PIO buffer bandwidth write test, to verify proper system
  249. * configuration. Even when all the setup calls work, occasionally
  250. * BIOS or other issues can prevent write combining from working, or
  251. * can cause other bandwidth problems to the chip.
  252. *
  253. * This test simply writes the same buffer over and over again, and
  254. * measures close to the peak bandwidth to the chip (not testing
  255. * data bandwidth to the wire). On chips that use an address-based
  256. * trigger to send packets to the wire, this is easy. On chips that
  257. * use a count to trigger, we want to make sure that the packet doesn't
  258. * go out on the wire, or trigger flow control checks.
  259. */
  260. static void ipath_verify_pioperf(struct ipath_devdata *dd)
  261. {
  262. u32 pbnum, cnt, lcnt;
  263. u32 __iomem *piobuf;
  264. u32 *addr;
  265. u64 msecs, emsecs;
  266. piobuf = ipath_getpiobuf(dd, 0, &pbnum);
  267. if (!piobuf) {
  268. dev_info(&dd->pcidev->dev,
  269. "No PIObufs for checking perf, skipping\n");
  270. return;
  271. }
  272. /*
  273. * Enough to give us a reasonable test, less than piobuf size, and
  274. * likely multiple of store buffer length.
  275. */
  276. cnt = 1024;
  277. addr = vmalloc(cnt);
  278. if (!addr) {
  279. dev_info(&dd->pcidev->dev,
  280. "Couldn't get memory for checking PIO perf,"
  281. " skipping\n");
  282. goto done;
  283. }
  284. preempt_disable(); /* we want reasonably accurate elapsed time */
  285. msecs = 1 + jiffies_to_msecs(jiffies);
  286. for (lcnt = 0; lcnt < 10000U; lcnt++) {
  287. /* wait until we cross msec boundary */
  288. if (jiffies_to_msecs(jiffies) >= msecs)
  289. break;
  290. udelay(1);
  291. }
  292. ipath_disable_armlaunch(dd);
  293. /*
  294. * length 0, no dwords actually sent, and mark as VL15
  295. * on chips where that may matter (due to IB flowcontrol)
  296. */
  297. if ((dd->ipath_flags & IPATH_HAS_PBC_CNT))
  298. writeq(1UL << 63, piobuf);
  299. else
  300. writeq(0, piobuf);
  301. ipath_flush_wc();
  302. /*
  303. * this is only roughly accurate, since even with preempt we
  304. * still take interrupts that could take a while. Running for
  305. * >= 5 msec seems to get us "close enough" to accurate values
  306. */
  307. msecs = jiffies_to_msecs(jiffies);
  308. for (emsecs = lcnt = 0; emsecs <= 5UL; lcnt++) {
  309. __iowrite32_copy(piobuf + 64, addr, cnt >> 2);
  310. emsecs = jiffies_to_msecs(jiffies) - msecs;
  311. }
  312. /* 1 GiB/sec, slightly over IB SDR line rate */
  313. if (lcnt < (emsecs * 1024U))
  314. ipath_dev_err(dd,
  315. "Performance problem: bandwidth to PIO buffers is "
  316. "only %u MiB/sec\n",
  317. lcnt / (u32) emsecs);
  318. else
  319. ipath_dbg("PIO buffer bandwidth %u MiB/sec is OK\n",
  320. lcnt / (u32) emsecs);
  321. preempt_enable();
  322. vfree(addr);
  323. done:
  324. /* disarm piobuf, so it's available again */
  325. ipath_disarm_piobufs(dd, pbnum, 1);
  326. ipath_enable_armlaunch(dd);
  327. }
  328. static void cleanup_device(struct ipath_devdata *dd);
  329. static int __devinit ipath_init_one(struct pci_dev *pdev,
  330. const struct pci_device_id *ent)
  331. {
  332. int ret, len, j;
  333. struct ipath_devdata *dd;
  334. unsigned long long addr;
  335. u32 bar0 = 0, bar1 = 0;
  336. u8 rev;
  337. dd = ipath_alloc_devdata(pdev);
  338. if (IS_ERR(dd)) {
  339. ret = PTR_ERR(dd);
  340. printk(KERN_ERR IPATH_DRV_NAME
  341. ": Could not allocate devdata: error %d\n", -ret);
  342. goto bail;
  343. }
  344. ipath_cdbg(VERBOSE, "initializing unit #%u\n", dd->ipath_unit);
  345. ret = pci_enable_device(pdev);
  346. if (ret) {
  347. /* This can happen iff:
  348. *
  349. * We did a chip reset, and then failed to reprogram the
  350. * BAR, or the chip reset due to an internal error. We then
  351. * unloaded the driver and reloaded it.
  352. *
  353. * Both reset cases set the BAR back to initial state. For
  354. * the latter case, the AER sticky error bit at offset 0x718
  355. * should be set, but the Linux kernel doesn't yet know
  356. * about that, it appears. If the original BAR was retained
  357. * in the kernel data structures, this may be OK.
  358. */
  359. ipath_dev_err(dd, "enable unit %d failed: error %d\n",
  360. dd->ipath_unit, -ret);
  361. goto bail_devdata;
  362. }
  363. addr = pci_resource_start(pdev, 0);
  364. len = pci_resource_len(pdev, 0);
  365. ipath_cdbg(VERBOSE, "regbase (0) %llx len %d irq %d, vend %x/%x "
  366. "driver_data %lx\n", addr, len, pdev->irq, ent->vendor,
  367. ent->device, ent->driver_data);
  368. read_bars(dd, pdev, &bar0, &bar1);
  369. if (!bar1 && !(bar0 & ~0xf)) {
  370. if (addr) {
  371. dev_info(&pdev->dev, "BAR is 0 (probable RESET), "
  372. "rewriting as %llx\n", addr);
  373. ret = pci_write_config_dword(
  374. pdev, PCI_BASE_ADDRESS_0, addr);
  375. if (ret) {
  376. ipath_dev_err(dd, "rewrite of BAR0 "
  377. "failed: err %d\n", -ret);
  378. goto bail_disable;
  379. }
  380. ret = pci_write_config_dword(
  381. pdev, PCI_BASE_ADDRESS_1, addr >> 32);
  382. if (ret) {
  383. ipath_dev_err(dd, "rewrite of BAR1 "
  384. "failed: err %d\n", -ret);
  385. goto bail_disable;
  386. }
  387. } else {
  388. ipath_dev_err(dd, "BAR is 0 (probable RESET), "
  389. "not usable until reboot\n");
  390. ret = -ENODEV;
  391. goto bail_disable;
  392. }
  393. }
  394. ret = pci_request_regions(pdev, IPATH_DRV_NAME);
  395. if (ret) {
  396. dev_info(&pdev->dev, "pci_request_regions unit %u fails: "
  397. "err %d\n", dd->ipath_unit, -ret);
  398. goto bail_disable;
  399. }
  400. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  401. if (ret) {
  402. /*
  403. * if the 64 bit setup fails, try 32 bit. Some systems
  404. * do not setup 64 bit maps on systems with 2GB or less
  405. * memory installed.
  406. */
  407. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  408. if (ret) {
  409. dev_info(&pdev->dev,
  410. "Unable to set DMA mask for unit %u: %d\n",
  411. dd->ipath_unit, ret);
  412. goto bail_regions;
  413. }
  414. else {
  415. ipath_dbg("No 64bit DMA mask, used 32 bit mask\n");
  416. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  417. if (ret)
  418. dev_info(&pdev->dev,
  419. "Unable to set DMA consistent mask "
  420. "for unit %u: %d\n",
  421. dd->ipath_unit, ret);
  422. }
  423. }
  424. else {
  425. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  426. if (ret)
  427. dev_info(&pdev->dev,
  428. "Unable to set DMA consistent mask "
  429. "for unit %u: %d\n",
  430. dd->ipath_unit, ret);
  431. }
  432. pci_set_master(pdev);
  433. /*
  434. * Save BARs to rewrite after device reset. Save all 64 bits of
  435. * BAR, just in case.
  436. */
  437. dd->ipath_pcibar0 = addr;
  438. dd->ipath_pcibar1 = addr >> 32;
  439. dd->ipath_deviceid = ent->device; /* save for later use */
  440. dd->ipath_vendorid = ent->vendor;
  441. /* setup the chip-specific functions, as early as possible. */
  442. switch (ent->device) {
  443. case PCI_DEVICE_ID_INFINIPATH_HT:
  444. ipath_init_iba6110_funcs(dd);
  445. break;
  446. default:
  447. ipath_dev_err(dd, "Found unknown QLogic deviceid 0x%x, "
  448. "failing\n", ent->device);
  449. return -ENODEV;
  450. }
  451. for (j = 0; j < 6; j++) {
  452. if (!pdev->resource[j].start)
  453. continue;
  454. ipath_cdbg(VERBOSE, "BAR %d %pR, len %llx\n",
  455. j, &pdev->resource[j],
  456. (unsigned long long)pci_resource_len(pdev, j));
  457. }
  458. if (!addr) {
  459. ipath_dev_err(dd, "No valid address in BAR 0!\n");
  460. ret = -ENODEV;
  461. goto bail_regions;
  462. }
  463. ret = pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
  464. if (ret) {
  465. ipath_dev_err(dd, "Failed to read PCI revision ID unit "
  466. "%u: err %d\n", dd->ipath_unit, -ret);
  467. goto bail_regions; /* shouldn't ever happen */
  468. }
  469. dd->ipath_pcirev = rev;
  470. #if defined(__powerpc__)
  471. /* There isn't a generic way to specify writethrough mappings */
  472. dd->ipath_kregbase = __ioremap(addr, len,
  473. (_PAGE_NO_CACHE|_PAGE_WRITETHRU));
  474. #else
  475. dd->ipath_kregbase = ioremap_nocache(addr, len);
  476. #endif
  477. if (!dd->ipath_kregbase) {
  478. ipath_dbg("Unable to map io addr %llx to kvirt, failing\n",
  479. addr);
  480. ret = -ENOMEM;
  481. goto bail_iounmap;
  482. }
  483. dd->ipath_kregend = (u64 __iomem *)
  484. ((void __iomem *)dd->ipath_kregbase + len);
  485. dd->ipath_physaddr = addr; /* used for io_remap, etc. */
  486. /* for user mmap */
  487. ipath_cdbg(VERBOSE, "mapped io addr %llx to kregbase %p\n",
  488. addr, dd->ipath_kregbase);
  489. if (dd->ipath_f_bus(dd, pdev))
  490. ipath_dev_err(dd, "Failed to setup config space; "
  491. "continuing anyway\n");
  492. /*
  493. * set up our interrupt handler; IRQF_SHARED probably not needed,
  494. * since MSI interrupts shouldn't be shared but won't hurt for now.
  495. * check 0 irq after we return from chip-specific bus setup, since
  496. * that can affect this due to setup
  497. */
  498. if (!dd->ipath_irq)
  499. ipath_dev_err(dd, "irq is 0, BIOS error? Interrupts won't "
  500. "work\n");
  501. else {
  502. ret = request_irq(dd->ipath_irq, ipath_intr, IRQF_SHARED,
  503. IPATH_DRV_NAME, dd);
  504. if (ret) {
  505. ipath_dev_err(dd, "Couldn't setup irq handler, "
  506. "irq=%d: %d\n", dd->ipath_irq, ret);
  507. goto bail_iounmap;
  508. }
  509. }
  510. ret = ipath_init_chip(dd, 0); /* do the chip-specific init */
  511. if (ret)
  512. goto bail_irqsetup;
  513. ret = ipath_enable_wc(dd);
  514. if (ret) {
  515. ipath_dev_err(dd, "Write combining not enabled "
  516. "(err %d): performance may be poor\n",
  517. -ret);
  518. ret = 0;
  519. }
  520. ipath_verify_pioperf(dd);
  521. ipath_device_create_group(&pdev->dev, dd);
  522. ipathfs_add_device(dd);
  523. ipath_user_add(dd);
  524. ipath_diag_add(dd);
  525. ipath_register_ib_device(dd);
  526. goto bail;
  527. bail_irqsetup:
  528. cleanup_device(dd);
  529. if (dd->ipath_irq)
  530. dd->ipath_f_free_irq(dd);
  531. if (dd->ipath_f_cleanup)
  532. dd->ipath_f_cleanup(dd);
  533. bail_iounmap:
  534. iounmap((volatile void __iomem *) dd->ipath_kregbase);
  535. bail_regions:
  536. pci_release_regions(pdev);
  537. bail_disable:
  538. pci_disable_device(pdev);
  539. bail_devdata:
  540. ipath_free_devdata(pdev, dd);
  541. bail:
  542. return ret;
  543. }
  544. static void cleanup_device(struct ipath_devdata *dd)
  545. {
  546. int port;
  547. struct ipath_portdata **tmp;
  548. unsigned long flags;
  549. if (*dd->ipath_statusp & IPATH_STATUS_CHIP_PRESENT) {
  550. /* can't do anything more with chip; needs re-init */
  551. *dd->ipath_statusp &= ~IPATH_STATUS_CHIP_PRESENT;
  552. if (dd->ipath_kregbase) {
  553. /*
  554. * if we haven't already cleaned up before these are
  555. * to ensure any register reads/writes "fail" until
  556. * re-init
  557. */
  558. dd->ipath_kregbase = NULL;
  559. dd->ipath_uregbase = 0;
  560. dd->ipath_sregbase = 0;
  561. dd->ipath_cregbase = 0;
  562. dd->ipath_kregsize = 0;
  563. }
  564. ipath_disable_wc(dd);
  565. }
  566. if (dd->ipath_spectriggerhit)
  567. dev_info(&dd->pcidev->dev, "%lu special trigger hits\n",
  568. dd->ipath_spectriggerhit);
  569. if (dd->ipath_pioavailregs_dma) {
  570. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  571. (void *) dd->ipath_pioavailregs_dma,
  572. dd->ipath_pioavailregs_phys);
  573. dd->ipath_pioavailregs_dma = NULL;
  574. }
  575. if (dd->ipath_dummy_hdrq) {
  576. dma_free_coherent(&dd->pcidev->dev,
  577. dd->ipath_pd[0]->port_rcvhdrq_size,
  578. dd->ipath_dummy_hdrq, dd->ipath_dummy_hdrq_phys);
  579. dd->ipath_dummy_hdrq = NULL;
  580. }
  581. if (dd->ipath_pageshadow) {
  582. struct page **tmpp = dd->ipath_pageshadow;
  583. dma_addr_t *tmpd = dd->ipath_physshadow;
  584. int i, cnt = 0;
  585. ipath_cdbg(VERBOSE, "Unlocking any expTID pages still "
  586. "locked\n");
  587. for (port = 0; port < dd->ipath_cfgports; port++) {
  588. int port_tidbase = port * dd->ipath_rcvtidcnt;
  589. int maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  590. for (i = port_tidbase; i < maxtid; i++) {
  591. if (!tmpp[i])
  592. continue;
  593. pci_unmap_page(dd->pcidev, tmpd[i],
  594. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  595. ipath_release_user_pages(&tmpp[i], 1);
  596. tmpp[i] = NULL;
  597. cnt++;
  598. }
  599. }
  600. if (cnt) {
  601. ipath_stats.sps_pageunlocks += cnt;
  602. ipath_cdbg(VERBOSE, "There were still %u expTID "
  603. "entries locked\n", cnt);
  604. }
  605. if (ipath_stats.sps_pagelocks ||
  606. ipath_stats.sps_pageunlocks)
  607. ipath_cdbg(VERBOSE, "%llu pages locked, %llu "
  608. "unlocked via ipath_m{un}lock\n",
  609. (unsigned long long)
  610. ipath_stats.sps_pagelocks,
  611. (unsigned long long)
  612. ipath_stats.sps_pageunlocks);
  613. ipath_cdbg(VERBOSE, "Free shadow page tid array at %p\n",
  614. dd->ipath_pageshadow);
  615. tmpp = dd->ipath_pageshadow;
  616. dd->ipath_pageshadow = NULL;
  617. vfree(tmpp);
  618. dd->ipath_egrtidbase = NULL;
  619. }
  620. /*
  621. * free any resources still in use (usually just kernel ports)
  622. * at unload; we do for portcnt, because that's what we allocate.
  623. * We acquire lock to be really paranoid that ipath_pd isn't being
  624. * accessed from some interrupt-related code (that should not happen,
  625. * but best to be sure).
  626. */
  627. spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
  628. tmp = dd->ipath_pd;
  629. dd->ipath_pd = NULL;
  630. spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
  631. for (port = 0; port < dd->ipath_portcnt; port++) {
  632. struct ipath_portdata *pd = tmp[port];
  633. tmp[port] = NULL; /* debugging paranoia */
  634. ipath_free_pddata(dd, pd);
  635. }
  636. kfree(tmp);
  637. }
  638. static void __devexit ipath_remove_one(struct pci_dev *pdev)
  639. {
  640. struct ipath_devdata *dd = pci_get_drvdata(pdev);
  641. ipath_cdbg(VERBOSE, "removing, pdev=%p, dd=%p\n", pdev, dd);
  642. /*
  643. * disable the IB link early, to be sure no new packets arrive, which
  644. * complicates the shutdown process
  645. */
  646. ipath_shutdown_device(dd);
  647. flush_workqueue(ib_wq);
  648. if (dd->verbs_dev)
  649. ipath_unregister_ib_device(dd->verbs_dev);
  650. ipath_diag_remove(dd);
  651. ipath_user_remove(dd);
  652. ipathfs_remove_device(dd);
  653. ipath_device_remove_group(&pdev->dev, dd);
  654. ipath_cdbg(VERBOSE, "Releasing pci memory regions, dd %p, "
  655. "unit %u\n", dd, (u32) dd->ipath_unit);
  656. cleanup_device(dd);
  657. /*
  658. * turn off rcv, send, and interrupts for all ports, all drivers
  659. * should also hard reset the chip here?
  660. * free up port 0 (kernel) rcvhdr, egr bufs, and eventually tid bufs
  661. * for all versions of the driver, if they were allocated
  662. */
  663. if (dd->ipath_irq) {
  664. ipath_cdbg(VERBOSE, "unit %u free irq %d\n",
  665. dd->ipath_unit, dd->ipath_irq);
  666. dd->ipath_f_free_irq(dd);
  667. } else
  668. ipath_dbg("irq is 0, not doing free_irq "
  669. "for unit %u\n", dd->ipath_unit);
  670. /*
  671. * we check for NULL here, because it's outside
  672. * the kregbase check, and we need to call it
  673. * after the free_irq. Thus it's possible that
  674. * the function pointers were never initialized.
  675. */
  676. if (dd->ipath_f_cleanup)
  677. /* clean up chip-specific stuff */
  678. dd->ipath_f_cleanup(dd);
  679. ipath_cdbg(VERBOSE, "Unmapping kregbase %p\n", dd->ipath_kregbase);
  680. iounmap((volatile void __iomem *) dd->ipath_kregbase);
  681. pci_release_regions(pdev);
  682. ipath_cdbg(VERBOSE, "calling pci_disable_device\n");
  683. pci_disable_device(pdev);
  684. ipath_free_devdata(pdev, dd);
  685. }
  686. /* general driver use */
  687. DEFINE_MUTEX(ipath_mutex);
  688. static DEFINE_SPINLOCK(ipath_pioavail_lock);
  689. /**
  690. * ipath_disarm_piobufs - cancel a range of PIO buffers
  691. * @dd: the infinipath device
  692. * @first: the first PIO buffer to cancel
  693. * @cnt: the number of PIO buffers to cancel
  694. *
  695. * cancel a range of PIO buffers, used when they might be armed, but
  696. * not triggered. Used at init to ensure buffer state, and also user
  697. * process close, in case it died while writing to a PIO buffer
  698. * Also after errors.
  699. */
  700. void ipath_disarm_piobufs(struct ipath_devdata *dd, unsigned first,
  701. unsigned cnt)
  702. {
  703. unsigned i, last = first + cnt;
  704. unsigned long flags;
  705. ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first);
  706. for (i = first; i < last; i++) {
  707. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  708. /*
  709. * The disarm-related bits are write-only, so it
  710. * is ok to OR them in with our copy of sendctrl
  711. * while we hold the lock.
  712. */
  713. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  714. dd->ipath_sendctrl | INFINIPATH_S_DISARM |
  715. (i << INFINIPATH_S_DISARMPIOBUF_SHIFT));
  716. /* can't disarm bufs back-to-back per iba7220 spec */
  717. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  718. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  719. }
  720. /* on some older chips, update may not happen after cancel */
  721. ipath_force_pio_avail_update(dd);
  722. }
  723. /**
  724. * ipath_wait_linkstate - wait for an IB link state change to occur
  725. * @dd: the infinipath device
  726. * @state: the state to wait for
  727. * @msecs: the number of milliseconds to wait
  728. *
  729. * wait up to msecs milliseconds for IB link state change to occur for
  730. * now, take the easy polling route. Currently used only by
  731. * ipath_set_linkstate. Returns 0 if state reached, otherwise
  732. * -ETIMEDOUT state can have multiple states set, for any of several
  733. * transitions.
  734. */
  735. int ipath_wait_linkstate(struct ipath_devdata *dd, u32 state, int msecs)
  736. {
  737. dd->ipath_state_wanted = state;
  738. wait_event_interruptible_timeout(ipath_state_wait,
  739. (dd->ipath_flags & state),
  740. msecs_to_jiffies(msecs));
  741. dd->ipath_state_wanted = 0;
  742. if (!(dd->ipath_flags & state)) {
  743. u64 val;
  744. ipath_cdbg(VERBOSE, "Didn't reach linkstate %s within %u"
  745. " ms\n",
  746. /* test INIT ahead of DOWN, both can be set */
  747. (state & IPATH_LINKINIT) ? "INIT" :
  748. ((state & IPATH_LINKDOWN) ? "DOWN" :
  749. ((state & IPATH_LINKARMED) ? "ARM" : "ACTIVE")),
  750. msecs);
  751. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  752. ipath_cdbg(VERBOSE, "ibcc=%llx ibcstatus=%llx (%s)\n",
  753. (unsigned long long) ipath_read_kreg64(
  754. dd, dd->ipath_kregs->kr_ibcctrl),
  755. (unsigned long long) val,
  756. ipath_ibcstatus_str[val & dd->ibcs_lts_mask]);
  757. }
  758. return (dd->ipath_flags & state) ? 0 : -ETIMEDOUT;
  759. }
  760. static void decode_sdma_errs(struct ipath_devdata *dd, ipath_err_t err,
  761. char *buf, size_t blen)
  762. {
  763. static const struct {
  764. ipath_err_t err;
  765. const char *msg;
  766. } errs[] = {
  767. { INFINIPATH_E_SDMAGENMISMATCH, "SDmaGenMismatch" },
  768. { INFINIPATH_E_SDMAOUTOFBOUND, "SDmaOutOfBound" },
  769. { INFINIPATH_E_SDMATAILOUTOFBOUND, "SDmaTailOutOfBound" },
  770. { INFINIPATH_E_SDMABASE, "SDmaBase" },
  771. { INFINIPATH_E_SDMA1STDESC, "SDma1stDesc" },
  772. { INFINIPATH_E_SDMARPYTAG, "SDmaRpyTag" },
  773. { INFINIPATH_E_SDMADWEN, "SDmaDwEn" },
  774. { INFINIPATH_E_SDMAMISSINGDW, "SDmaMissingDw" },
  775. { INFINIPATH_E_SDMAUNEXPDATA, "SDmaUnexpData" },
  776. { INFINIPATH_E_SDMADESCADDRMISALIGN, "SDmaDescAddrMisalign" },
  777. { INFINIPATH_E_SENDBUFMISUSE, "SendBufMisuse" },
  778. { INFINIPATH_E_SDMADISABLED, "SDmaDisabled" },
  779. };
  780. int i;
  781. int expected;
  782. size_t bidx = 0;
  783. for (i = 0; i < ARRAY_SIZE(errs); i++) {
  784. expected = (errs[i].err != INFINIPATH_E_SDMADISABLED) ? 0 :
  785. test_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  786. if ((err & errs[i].err) && !expected)
  787. bidx += snprintf(buf + bidx, blen - bidx,
  788. "%s ", errs[i].msg);
  789. }
  790. }
  791. /*
  792. * Decode the error status into strings, deciding whether to always
  793. * print * it or not depending on "normal packet errors" vs everything
  794. * else. Return 1 if "real" errors, otherwise 0 if only packet
  795. * errors, so caller can decide what to print with the string.
  796. */
  797. int ipath_decode_err(struct ipath_devdata *dd, char *buf, size_t blen,
  798. ipath_err_t err)
  799. {
  800. int iserr = 1;
  801. *buf = '\0';
  802. if (err & INFINIPATH_E_PKTERRS) {
  803. if (!(err & ~INFINIPATH_E_PKTERRS))
  804. iserr = 0; // if only packet errors.
  805. if (ipath_debug & __IPATH_ERRPKTDBG) {
  806. if (err & INFINIPATH_E_REBP)
  807. strlcat(buf, "EBP ", blen);
  808. if (err & INFINIPATH_E_RVCRC)
  809. strlcat(buf, "VCRC ", blen);
  810. if (err & INFINIPATH_E_RICRC) {
  811. strlcat(buf, "CRC ", blen);
  812. // clear for check below, so only once
  813. err &= INFINIPATH_E_RICRC;
  814. }
  815. if (err & INFINIPATH_E_RSHORTPKTLEN)
  816. strlcat(buf, "rshortpktlen ", blen);
  817. if (err & INFINIPATH_E_SDROPPEDDATAPKT)
  818. strlcat(buf, "sdroppeddatapkt ", blen);
  819. if (err & INFINIPATH_E_SPKTLEN)
  820. strlcat(buf, "spktlen ", blen);
  821. }
  822. if ((err & INFINIPATH_E_RICRC) &&
  823. !(err&(INFINIPATH_E_RVCRC|INFINIPATH_E_REBP)))
  824. strlcat(buf, "CRC ", blen);
  825. if (!iserr)
  826. goto done;
  827. }
  828. if (err & INFINIPATH_E_RHDRLEN)
  829. strlcat(buf, "rhdrlen ", blen);
  830. if (err & INFINIPATH_E_RBADTID)
  831. strlcat(buf, "rbadtid ", blen);
  832. if (err & INFINIPATH_E_RBADVERSION)
  833. strlcat(buf, "rbadversion ", blen);
  834. if (err & INFINIPATH_E_RHDR)
  835. strlcat(buf, "rhdr ", blen);
  836. if (err & INFINIPATH_E_SENDSPECIALTRIGGER)
  837. strlcat(buf, "sendspecialtrigger ", blen);
  838. if (err & INFINIPATH_E_RLONGPKTLEN)
  839. strlcat(buf, "rlongpktlen ", blen);
  840. if (err & INFINIPATH_E_RMAXPKTLEN)
  841. strlcat(buf, "rmaxpktlen ", blen);
  842. if (err & INFINIPATH_E_RMINPKTLEN)
  843. strlcat(buf, "rminpktlen ", blen);
  844. if (err & INFINIPATH_E_SMINPKTLEN)
  845. strlcat(buf, "sminpktlen ", blen);
  846. if (err & INFINIPATH_E_RFORMATERR)
  847. strlcat(buf, "rformaterr ", blen);
  848. if (err & INFINIPATH_E_RUNSUPVL)
  849. strlcat(buf, "runsupvl ", blen);
  850. if (err & INFINIPATH_E_RUNEXPCHAR)
  851. strlcat(buf, "runexpchar ", blen);
  852. if (err & INFINIPATH_E_RIBFLOW)
  853. strlcat(buf, "ribflow ", blen);
  854. if (err & INFINIPATH_E_SUNDERRUN)
  855. strlcat(buf, "sunderrun ", blen);
  856. if (err & INFINIPATH_E_SPIOARMLAUNCH)
  857. strlcat(buf, "spioarmlaunch ", blen);
  858. if (err & INFINIPATH_E_SUNEXPERRPKTNUM)
  859. strlcat(buf, "sunexperrpktnum ", blen);
  860. if (err & INFINIPATH_E_SDROPPEDSMPPKT)
  861. strlcat(buf, "sdroppedsmppkt ", blen);
  862. if (err & INFINIPATH_E_SMAXPKTLEN)
  863. strlcat(buf, "smaxpktlen ", blen);
  864. if (err & INFINIPATH_E_SUNSUPVL)
  865. strlcat(buf, "sunsupVL ", blen);
  866. if (err & INFINIPATH_E_INVALIDADDR)
  867. strlcat(buf, "invalidaddr ", blen);
  868. if (err & INFINIPATH_E_RRCVEGRFULL)
  869. strlcat(buf, "rcvegrfull ", blen);
  870. if (err & INFINIPATH_E_RRCVHDRFULL)
  871. strlcat(buf, "rcvhdrfull ", blen);
  872. if (err & INFINIPATH_E_IBSTATUSCHANGED)
  873. strlcat(buf, "ibcstatuschg ", blen);
  874. if (err & INFINIPATH_E_RIBLOSTLINK)
  875. strlcat(buf, "riblostlink ", blen);
  876. if (err & INFINIPATH_E_HARDWARE)
  877. strlcat(buf, "hardware ", blen);
  878. if (err & INFINIPATH_E_RESET)
  879. strlcat(buf, "reset ", blen);
  880. if (err & INFINIPATH_E_SDMAERRS)
  881. decode_sdma_errs(dd, err, buf, blen);
  882. if (err & INFINIPATH_E_INVALIDEEPCMD)
  883. strlcat(buf, "invalideepromcmd ", blen);
  884. done:
  885. return iserr;
  886. }
  887. /**
  888. * get_rhf_errstring - decode RHF errors
  889. * @err: the err number
  890. * @msg: the output buffer
  891. * @len: the length of the output buffer
  892. *
  893. * only used one place now, may want more later
  894. */
  895. static void get_rhf_errstring(u32 err, char *msg, size_t len)
  896. {
  897. /* if no errors, and so don't need to check what's first */
  898. *msg = '\0';
  899. if (err & INFINIPATH_RHF_H_ICRCERR)
  900. strlcat(msg, "icrcerr ", len);
  901. if (err & INFINIPATH_RHF_H_VCRCERR)
  902. strlcat(msg, "vcrcerr ", len);
  903. if (err & INFINIPATH_RHF_H_PARITYERR)
  904. strlcat(msg, "parityerr ", len);
  905. if (err & INFINIPATH_RHF_H_LENERR)
  906. strlcat(msg, "lenerr ", len);
  907. if (err & INFINIPATH_RHF_H_MTUERR)
  908. strlcat(msg, "mtuerr ", len);
  909. if (err & INFINIPATH_RHF_H_IHDRERR)
  910. /* infinipath hdr checksum error */
  911. strlcat(msg, "ipathhdrerr ", len);
  912. if (err & INFINIPATH_RHF_H_TIDERR)
  913. strlcat(msg, "tiderr ", len);
  914. if (err & INFINIPATH_RHF_H_MKERR)
  915. /* bad port, offset, etc. */
  916. strlcat(msg, "invalid ipathhdr ", len);
  917. if (err & INFINIPATH_RHF_H_IBERR)
  918. strlcat(msg, "iberr ", len);
  919. if (err & INFINIPATH_RHF_L_SWA)
  920. strlcat(msg, "swA ", len);
  921. if (err & INFINIPATH_RHF_L_SWB)
  922. strlcat(msg, "swB ", len);
  923. }
  924. /**
  925. * ipath_get_egrbuf - get an eager buffer
  926. * @dd: the infinipath device
  927. * @bufnum: the eager buffer to get
  928. *
  929. * must only be called if ipath_pd[port] is known to be allocated
  930. */
  931. static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum)
  932. {
  933. return dd->ipath_port0_skbinfo ?
  934. (void *) dd->ipath_port0_skbinfo[bufnum].skb->data : NULL;
  935. }
  936. /**
  937. * ipath_alloc_skb - allocate an skb and buffer with possible constraints
  938. * @dd: the infinipath device
  939. * @gfp_mask: the sk_buff SFP mask
  940. */
  941. struct sk_buff *ipath_alloc_skb(struct ipath_devdata *dd,
  942. gfp_t gfp_mask)
  943. {
  944. struct sk_buff *skb;
  945. u32 len;
  946. /*
  947. * Only fully supported way to handle this is to allocate lots
  948. * extra, align as needed, and then do skb_reserve(). That wastes
  949. * a lot of memory... I'll have to hack this into infinipath_copy
  950. * also.
  951. */
  952. /*
  953. * We need 2 extra bytes for ipath_ether data sent in the
  954. * key header. In order to keep everything dword aligned,
  955. * we'll reserve 4 bytes.
  956. */
  957. len = dd->ipath_ibmaxlen + 4;
  958. if (dd->ipath_flags & IPATH_4BYTE_TID) {
  959. /* We need a 2KB multiple alignment, and there is no way
  960. * to do it except to allocate extra and then skb_reserve
  961. * enough to bring it up to the right alignment.
  962. */
  963. len += 2047;
  964. }
  965. skb = __dev_alloc_skb(len, gfp_mask);
  966. if (!skb) {
  967. ipath_dev_err(dd, "Failed to allocate skbuff, length %u\n",
  968. len);
  969. goto bail;
  970. }
  971. skb_reserve(skb, 4);
  972. if (dd->ipath_flags & IPATH_4BYTE_TID) {
  973. u32 una = (unsigned long)skb->data & 2047;
  974. if (una)
  975. skb_reserve(skb, 2048 - una);
  976. }
  977. bail:
  978. return skb;
  979. }
  980. static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
  981. u32 eflags,
  982. u32 l,
  983. u32 etail,
  984. __le32 *rhf_addr,
  985. struct ipath_message_header *hdr)
  986. {
  987. char emsg[128];
  988. get_rhf_errstring(eflags, emsg, sizeof emsg);
  989. ipath_cdbg(PKT, "RHFerrs %x hdrqtail=%x typ=%u "
  990. "tlen=%x opcode=%x egridx=%x: %s\n",
  991. eflags, l,
  992. ipath_hdrget_rcv_type(rhf_addr),
  993. ipath_hdrget_length_in_bytes(rhf_addr),
  994. be32_to_cpu(hdr->bth[0]) >> 24,
  995. etail, emsg);
  996. /* Count local link integrity errors. */
  997. if (eflags & (INFINIPATH_RHF_H_ICRCERR | INFINIPATH_RHF_H_VCRCERR)) {
  998. u8 n = (dd->ipath_ibcctrl >>
  999. INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
  1000. INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
  1001. if (++dd->ipath_lli_counter > n) {
  1002. dd->ipath_lli_counter = 0;
  1003. dd->ipath_lli_errors++;
  1004. }
  1005. }
  1006. }
  1007. /*
  1008. * ipath_kreceive - receive a packet
  1009. * @pd: the infinipath port
  1010. *
  1011. * called from interrupt handler for errors or receive interrupt
  1012. */
  1013. void ipath_kreceive(struct ipath_portdata *pd)
  1014. {
  1015. struct ipath_devdata *dd = pd->port_dd;
  1016. __le32 *rhf_addr;
  1017. void *ebuf;
  1018. const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
  1019. const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
  1020. u32 etail = -1, l, hdrqtail;
  1021. struct ipath_message_header *hdr;
  1022. u32 eflags, i, etype, tlen, pkttot = 0, updegr = 0, reloop = 0;
  1023. static u64 totcalls; /* stats, may eventually remove */
  1024. int last;
  1025. l = pd->port_head;
  1026. rhf_addr = (__le32 *) pd->port_rcvhdrq + l + dd->ipath_rhf_offset;
  1027. if (dd->ipath_flags & IPATH_NODMA_RTAIL) {
  1028. u32 seq = ipath_hdrget_seq(rhf_addr);
  1029. if (seq != pd->port_seq_cnt)
  1030. goto bail;
  1031. hdrqtail = 0;
  1032. } else {
  1033. hdrqtail = ipath_get_rcvhdrtail(pd);
  1034. if (l == hdrqtail)
  1035. goto bail;
  1036. smp_rmb();
  1037. }
  1038. reloop:
  1039. for (last = 0, i = 1; !last; i += !last) {
  1040. hdr = dd->ipath_f_get_msgheader(dd, rhf_addr);
  1041. eflags = ipath_hdrget_err_flags(rhf_addr);
  1042. etype = ipath_hdrget_rcv_type(rhf_addr);
  1043. /* total length */
  1044. tlen = ipath_hdrget_length_in_bytes(rhf_addr);
  1045. ebuf = NULL;
  1046. if ((dd->ipath_flags & IPATH_NODMA_RTAIL) ?
  1047. ipath_hdrget_use_egr_buf(rhf_addr) :
  1048. (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
  1049. /*
  1050. * It turns out that the chip uses an eager buffer
  1051. * for all non-expected packets, whether it "needs"
  1052. * one or not. So always get the index, but don't
  1053. * set ebuf (so we try to copy data) unless the
  1054. * length requires it.
  1055. */
  1056. etail = ipath_hdrget_index(rhf_addr);
  1057. updegr = 1;
  1058. if (tlen > sizeof(*hdr) ||
  1059. etype == RCVHQ_RCV_TYPE_NON_KD)
  1060. ebuf = ipath_get_egrbuf(dd, etail);
  1061. }
  1062. /*
  1063. * both tiderr and ipathhdrerr are set for all plain IB
  1064. * packets; only ipathhdrerr should be set.
  1065. */
  1066. if (etype != RCVHQ_RCV_TYPE_NON_KD &&
  1067. etype != RCVHQ_RCV_TYPE_ERROR &&
  1068. ipath_hdrget_ipath_ver(hdr->iph.ver_port_tid_offset) !=
  1069. IPS_PROTO_VERSION)
  1070. ipath_cdbg(PKT, "Bad InfiniPath protocol version "
  1071. "%x\n", etype);
  1072. if (unlikely(eflags))
  1073. ipath_rcv_hdrerr(dd, eflags, l, etail, rhf_addr, hdr);
  1074. else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
  1075. ipath_ib_rcv(dd->verbs_dev, (u32 *)hdr, ebuf, tlen);
  1076. if (dd->ipath_lli_counter)
  1077. dd->ipath_lli_counter--;
  1078. } else if (etype == RCVHQ_RCV_TYPE_EAGER) {
  1079. u8 opcode = be32_to_cpu(hdr->bth[0]) >> 24;
  1080. u32 qp = be32_to_cpu(hdr->bth[1]) & 0xffffff;
  1081. ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
  1082. "qp=%x), len %x; ignored\n",
  1083. etype, opcode, qp, tlen);
  1084. }
  1085. else if (etype == RCVHQ_RCV_TYPE_EXPECTED)
  1086. ipath_dbg("Bug: Expected TID, opcode %x; ignored\n",
  1087. be32_to_cpu(hdr->bth[0]) >> 24);
  1088. else {
  1089. /*
  1090. * error packet, type of error unknown.
  1091. * Probably type 3, but we don't know, so don't
  1092. * even try to print the opcode, etc.
  1093. * Usually caused by a "bad packet", that has no
  1094. * BTH, when the LRH says it should.
  1095. */
  1096. ipath_cdbg(ERRPKT, "Error Pkt, but no eflags! egrbuf"
  1097. " %x, len %x hdrq+%x rhf: %Lx\n",
  1098. etail, tlen, l, (unsigned long long)
  1099. le64_to_cpu(*(__le64 *) rhf_addr));
  1100. if (ipath_debug & __IPATH_ERRPKTDBG) {
  1101. u32 j, *d, dw = rsize-2;
  1102. if (rsize > (tlen>>2))
  1103. dw = tlen>>2;
  1104. d = (u32 *)hdr;
  1105. printk(KERN_DEBUG "EPkt rcvhdr(%x dw):\n",
  1106. dw);
  1107. for (j = 0; j < dw; j++)
  1108. printk(KERN_DEBUG "%8x%s", d[j],
  1109. (j%8) == 7 ? "\n" : " ");
  1110. printk(KERN_DEBUG ".\n");
  1111. }
  1112. }
  1113. l += rsize;
  1114. if (l >= maxcnt)
  1115. l = 0;
  1116. rhf_addr = (__le32 *) pd->port_rcvhdrq +
  1117. l + dd->ipath_rhf_offset;
  1118. if (dd->ipath_flags & IPATH_NODMA_RTAIL) {
  1119. u32 seq = ipath_hdrget_seq(rhf_addr);
  1120. if (++pd->port_seq_cnt > 13)
  1121. pd->port_seq_cnt = 1;
  1122. if (seq != pd->port_seq_cnt)
  1123. last = 1;
  1124. } else if (l == hdrqtail)
  1125. last = 1;
  1126. /*
  1127. * update head regs on last packet, and every 16 packets.
  1128. * Reduce bus traffic, while still trying to prevent
  1129. * rcvhdrq overflows, for when the queue is nearly full
  1130. */
  1131. if (last || !(i & 0xf)) {
  1132. u64 lval = l;
  1133. /* request IBA6120 and 7220 interrupt only on last */
  1134. if (last)
  1135. lval |= dd->ipath_rhdrhead_intr_off;
  1136. ipath_write_ureg(dd, ur_rcvhdrhead, lval,
  1137. pd->port_port);
  1138. if (updegr) {
  1139. ipath_write_ureg(dd, ur_rcvegrindexhead,
  1140. etail, pd->port_port);
  1141. updegr = 0;
  1142. }
  1143. }
  1144. }
  1145. if (!dd->ipath_rhdrhead_intr_off && !reloop &&
  1146. !(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
  1147. /* IBA6110 workaround; we can have a race clearing chip
  1148. * interrupt with another interrupt about to be delivered,
  1149. * and can clear it before it is delivered on the GPIO
  1150. * workaround. By doing the extra check here for the
  1151. * in-memory tail register updating while we were doing
  1152. * earlier packets, we "almost" guarantee we have covered
  1153. * that case.
  1154. */
  1155. u32 hqtail = ipath_get_rcvhdrtail(pd);
  1156. if (hqtail != hdrqtail) {
  1157. hdrqtail = hqtail;
  1158. reloop = 1; /* loop 1 extra time at most */
  1159. goto reloop;
  1160. }
  1161. }
  1162. pkttot += i;
  1163. pd->port_head = l;
  1164. if (pkttot > ipath_stats.sps_maxpkts_call)
  1165. ipath_stats.sps_maxpkts_call = pkttot;
  1166. ipath_stats.sps_port0pkts += pkttot;
  1167. ipath_stats.sps_avgpkts_call =
  1168. ipath_stats.sps_port0pkts / ++totcalls;
  1169. bail:;
  1170. }
  1171. /**
  1172. * ipath_update_pio_bufs - update shadow copy of the PIO availability map
  1173. * @dd: the infinipath device
  1174. *
  1175. * called whenever our local copy indicates we have run out of send buffers
  1176. * NOTE: This can be called from interrupt context by some code
  1177. * and from non-interrupt context by ipath_getpiobuf().
  1178. */
  1179. static void ipath_update_pio_bufs(struct ipath_devdata *dd)
  1180. {
  1181. unsigned long flags;
  1182. int i;
  1183. const unsigned piobregs = (unsigned)dd->ipath_pioavregs;
  1184. /* If the generation (check) bits have changed, then we update the
  1185. * busy bit for the corresponding PIO buffer. This algorithm will
  1186. * modify positions to the value they already have in some cases
  1187. * (i.e., no change), but it's faster than changing only the bits
  1188. * that have changed.
  1189. *
  1190. * We would like to do this atomicly, to avoid spinlocks in the
  1191. * critical send path, but that's not really possible, given the
  1192. * type of changes, and that this routine could be called on
  1193. * multiple cpu's simultaneously, so we lock in this routine only,
  1194. * to avoid conflicting updates; all we change is the shadow, and
  1195. * it's a single 64 bit memory location, so by definition the update
  1196. * is atomic in terms of what other cpu's can see in testing the
  1197. * bits. The spin_lock overhead isn't too bad, since it only
  1198. * happens when all buffers are in use, so only cpu overhead, not
  1199. * latency or bandwidth is affected.
  1200. */
  1201. if (!dd->ipath_pioavailregs_dma) {
  1202. ipath_dbg("Update shadow pioavail, but regs_dma NULL!\n");
  1203. return;
  1204. }
  1205. if (ipath_debug & __IPATH_VERBDBG) {
  1206. /* only if packet debug and verbose */
  1207. volatile __le64 *dma = dd->ipath_pioavailregs_dma;
  1208. unsigned long *shadow = dd->ipath_pioavailshadow;
  1209. ipath_cdbg(PKT, "Refill avail, dma0=%llx shad0=%lx, "
  1210. "d1=%llx s1=%lx, d2=%llx s2=%lx, d3=%llx "
  1211. "s3=%lx\n",
  1212. (unsigned long long) le64_to_cpu(dma[0]),
  1213. shadow[0],
  1214. (unsigned long long) le64_to_cpu(dma[1]),
  1215. shadow[1],
  1216. (unsigned long long) le64_to_cpu(dma[2]),
  1217. shadow[2],
  1218. (unsigned long long) le64_to_cpu(dma[3]),
  1219. shadow[3]);
  1220. if (piobregs > 4)
  1221. ipath_cdbg(
  1222. PKT, "2nd group, dma4=%llx shad4=%lx, "
  1223. "d5=%llx s5=%lx, d6=%llx s6=%lx, "
  1224. "d7=%llx s7=%lx\n",
  1225. (unsigned long long) le64_to_cpu(dma[4]),
  1226. shadow[4],
  1227. (unsigned long long) le64_to_cpu(dma[5]),
  1228. shadow[5],
  1229. (unsigned long long) le64_to_cpu(dma[6]),
  1230. shadow[6],
  1231. (unsigned long long) le64_to_cpu(dma[7]),
  1232. shadow[7]);
  1233. }
  1234. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1235. for (i = 0; i < piobregs; i++) {
  1236. u64 pchbusy, pchg, piov, pnew;
  1237. /*
  1238. * Chip Errata: bug 6641; even and odd qwords>3 are swapped
  1239. */
  1240. if (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS))
  1241. piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i ^ 1]);
  1242. else
  1243. piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
  1244. pchg = dd->ipath_pioavailkernel[i] &
  1245. ~(dd->ipath_pioavailshadow[i] ^ piov);
  1246. pchbusy = pchg << INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT;
  1247. if (pchg && (pchbusy & dd->ipath_pioavailshadow[i])) {
  1248. pnew = dd->ipath_pioavailshadow[i] & ~pchbusy;
  1249. pnew |= piov & pchbusy;
  1250. dd->ipath_pioavailshadow[i] = pnew;
  1251. }
  1252. }
  1253. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1254. }
  1255. /*
  1256. * used to force update of pioavailshadow if we can't get a pio buffer.
  1257. * Needed primarily due to exitting freeze mode after recovering
  1258. * from errors. Done lazily, because it's safer (known to not
  1259. * be writing pio buffers).
  1260. */
  1261. static void ipath_reset_availshadow(struct ipath_devdata *dd)
  1262. {
  1263. int i, im;
  1264. unsigned long flags;
  1265. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1266. for (i = 0; i < dd->ipath_pioavregs; i++) {
  1267. u64 val, oldval;
  1268. /* deal with 6110 chip bug on high register #s */
  1269. im = (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS)) ?
  1270. i ^ 1 : i;
  1271. val = le64_to_cpu(dd->ipath_pioavailregs_dma[im]);
  1272. /*
  1273. * busy out the buffers not in the kernel avail list,
  1274. * without changing the generation bits.
  1275. */
  1276. oldval = dd->ipath_pioavailshadow[i];
  1277. dd->ipath_pioavailshadow[i] = val |
  1278. ((~dd->ipath_pioavailkernel[i] <<
  1279. INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT) &
  1280. 0xaaaaaaaaaaaaaaaaULL); /* All BUSY bits in qword */
  1281. if (oldval != dd->ipath_pioavailshadow[i])
  1282. ipath_dbg("shadow[%d] was %Lx, now %lx\n",
  1283. i, (unsigned long long) oldval,
  1284. dd->ipath_pioavailshadow[i]);
  1285. }
  1286. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1287. }
  1288. /**
  1289. * ipath_setrcvhdrsize - set the receive header size
  1290. * @dd: the infinipath device
  1291. * @rhdrsize: the receive header size
  1292. *
  1293. * called from user init code, and also layered driver init
  1294. */
  1295. int ipath_setrcvhdrsize(struct ipath_devdata *dd, unsigned rhdrsize)
  1296. {
  1297. int ret = 0;
  1298. if (dd->ipath_flags & IPATH_RCVHDRSZ_SET) {
  1299. if (dd->ipath_rcvhdrsize != rhdrsize) {
  1300. dev_info(&dd->pcidev->dev,
  1301. "Error: can't set protocol header "
  1302. "size %u, already %u\n",
  1303. rhdrsize, dd->ipath_rcvhdrsize);
  1304. ret = -EAGAIN;
  1305. } else
  1306. ipath_cdbg(VERBOSE, "Reuse same protocol header "
  1307. "size %u\n", dd->ipath_rcvhdrsize);
  1308. } else if (rhdrsize > (dd->ipath_rcvhdrentsize -
  1309. (sizeof(u64) / sizeof(u32)))) {
  1310. ipath_dbg("Error: can't set protocol header size %u "
  1311. "(> max %u)\n", rhdrsize,
  1312. dd->ipath_rcvhdrentsize -
  1313. (u32) (sizeof(u64) / sizeof(u32)));
  1314. ret = -EOVERFLOW;
  1315. } else {
  1316. dd->ipath_flags |= IPATH_RCVHDRSZ_SET;
  1317. dd->ipath_rcvhdrsize = rhdrsize;
  1318. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
  1319. dd->ipath_rcvhdrsize);
  1320. ipath_cdbg(VERBOSE, "Set protocol header size to %u\n",
  1321. dd->ipath_rcvhdrsize);
  1322. }
  1323. return ret;
  1324. }
  1325. /*
  1326. * debugging code and stats updates if no pio buffers available.
  1327. */
  1328. static noinline void no_pio_bufs(struct ipath_devdata *dd)
  1329. {
  1330. unsigned long *shadow = dd->ipath_pioavailshadow;
  1331. __le64 *dma = (__le64 *)dd->ipath_pioavailregs_dma;
  1332. dd->ipath_upd_pio_shadow = 1;
  1333. /*
  1334. * not atomic, but if we lose a stat count in a while, that's OK
  1335. */
  1336. ipath_stats.sps_nopiobufs++;
  1337. if (!(++dd->ipath_consec_nopiobuf % 100000)) {
  1338. ipath_force_pio_avail_update(dd); /* at start */
  1339. ipath_dbg("%u tries no piobufavail ts%lx; dmacopy: "
  1340. "%llx %llx %llx %llx\n"
  1341. "ipath shadow: %lx %lx %lx %lx\n",
  1342. dd->ipath_consec_nopiobuf,
  1343. (unsigned long)get_cycles(),
  1344. (unsigned long long) le64_to_cpu(dma[0]),
  1345. (unsigned long long) le64_to_cpu(dma[1]),
  1346. (unsigned long long) le64_to_cpu(dma[2]),
  1347. (unsigned long long) le64_to_cpu(dma[3]),
  1348. shadow[0], shadow[1], shadow[2], shadow[3]);
  1349. /*
  1350. * 4 buffers per byte, 4 registers above, cover rest
  1351. * below
  1352. */
  1353. if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) >
  1354. (sizeof(shadow[0]) * 4 * 4))
  1355. ipath_dbg("2nd group: dmacopy: "
  1356. "%llx %llx %llx %llx\n"
  1357. "ipath shadow: %lx %lx %lx %lx\n",
  1358. (unsigned long long)le64_to_cpu(dma[4]),
  1359. (unsigned long long)le64_to_cpu(dma[5]),
  1360. (unsigned long long)le64_to_cpu(dma[6]),
  1361. (unsigned long long)le64_to_cpu(dma[7]),
  1362. shadow[4], shadow[5], shadow[6], shadow[7]);
  1363. /* at end, so update likely happened */
  1364. ipath_reset_availshadow(dd);
  1365. }
  1366. }
  1367. /*
  1368. * common code for normal driver pio buffer allocation, and reserved
  1369. * allocation.
  1370. *
  1371. * do appropriate marking as busy, etc.
  1372. * returns buffer number if one found (>=0), negative number is error.
  1373. */
  1374. static u32 __iomem *ipath_getpiobuf_range(struct ipath_devdata *dd,
  1375. u32 *pbufnum, u32 first, u32 last, u32 firsti)
  1376. {
  1377. int i, j, updated = 0;
  1378. unsigned piobcnt;
  1379. unsigned long flags;
  1380. unsigned long *shadow = dd->ipath_pioavailshadow;
  1381. u32 __iomem *buf;
  1382. piobcnt = last - first;
  1383. if (dd->ipath_upd_pio_shadow) {
  1384. /*
  1385. * Minor optimization. If we had no buffers on last call,
  1386. * start out by doing the update; continue and do scan even
  1387. * if no buffers were updated, to be paranoid
  1388. */
  1389. ipath_update_pio_bufs(dd);
  1390. updated++;
  1391. i = first;
  1392. } else
  1393. i = firsti;
  1394. rescan:
  1395. /*
  1396. * while test_and_set_bit() is atomic, we do that and then the
  1397. * change_bit(), and the pair is not. See if this is the cause
  1398. * of the remaining armlaunch errors.
  1399. */
  1400. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1401. for (j = 0; j < piobcnt; j++, i++) {
  1402. if (i >= last)
  1403. i = first;
  1404. if (__test_and_set_bit((2 * i) + 1, shadow))
  1405. continue;
  1406. /* flip generation bit */
  1407. __change_bit(2 * i, shadow);
  1408. break;
  1409. }
  1410. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1411. if (j == piobcnt) {
  1412. if (!updated) {
  1413. /*
  1414. * first time through; shadow exhausted, but may be
  1415. * buffers available, try an update and then rescan.
  1416. */
  1417. ipath_update_pio_bufs(dd);
  1418. updated++;
  1419. i = first;
  1420. goto rescan;
  1421. } else if (updated == 1 && piobcnt <=
  1422. ((dd->ipath_sendctrl
  1423. >> INFINIPATH_S_UPDTHRESH_SHIFT) &
  1424. INFINIPATH_S_UPDTHRESH_MASK)) {
  1425. /*
  1426. * for chips supporting and using the update
  1427. * threshold we need to force an update of the
  1428. * in-memory copy if the count is less than the
  1429. * thershold, then check one more time.
  1430. */
  1431. ipath_force_pio_avail_update(dd);
  1432. ipath_update_pio_bufs(dd);
  1433. updated++;
  1434. i = first;
  1435. goto rescan;
  1436. }
  1437. no_pio_bufs(dd);
  1438. buf = NULL;
  1439. } else {
  1440. if (i < dd->ipath_piobcnt2k)
  1441. buf = (u32 __iomem *) (dd->ipath_pio2kbase +
  1442. i * dd->ipath_palign);
  1443. else
  1444. buf = (u32 __iomem *)
  1445. (dd->ipath_pio4kbase +
  1446. (i - dd->ipath_piobcnt2k) * dd->ipath_4kalign);
  1447. if (pbufnum)
  1448. *pbufnum = i;
  1449. }
  1450. return buf;
  1451. }
  1452. /**
  1453. * ipath_getpiobuf - find an available pio buffer
  1454. * @dd: the infinipath device
  1455. * @plen: the size of the PIO buffer needed in 32-bit words
  1456. * @pbufnum: the buffer number is placed here
  1457. */
  1458. u32 __iomem *ipath_getpiobuf(struct ipath_devdata *dd, u32 plen, u32 *pbufnum)
  1459. {
  1460. u32 __iomem *buf;
  1461. u32 pnum, nbufs;
  1462. u32 first, lasti;
  1463. if (plen + 1 >= IPATH_SMALLBUF_DWORDS) {
  1464. first = dd->ipath_piobcnt2k;
  1465. lasti = dd->ipath_lastpioindexl;
  1466. } else {
  1467. first = 0;
  1468. lasti = dd->ipath_lastpioindex;
  1469. }
  1470. nbufs = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k;
  1471. buf = ipath_getpiobuf_range(dd, &pnum, first, nbufs, lasti);
  1472. if (buf) {
  1473. /*
  1474. * Set next starting place. It's just an optimization,
  1475. * it doesn't matter who wins on this, so no locking
  1476. */
  1477. if (plen + 1 >= IPATH_SMALLBUF_DWORDS)
  1478. dd->ipath_lastpioindexl = pnum + 1;
  1479. else
  1480. dd->ipath_lastpioindex = pnum + 1;
  1481. if (dd->ipath_upd_pio_shadow)
  1482. dd->ipath_upd_pio_shadow = 0;
  1483. if (dd->ipath_consec_nopiobuf)
  1484. dd->ipath_consec_nopiobuf = 0;
  1485. ipath_cdbg(VERBOSE, "Return piobuf%u %uk @ %p\n",
  1486. pnum, (pnum < dd->ipath_piobcnt2k) ? 2 : 4, buf);
  1487. if (pbufnum)
  1488. *pbufnum = pnum;
  1489. }
  1490. return buf;
  1491. }
  1492. /**
  1493. * ipath_chg_pioavailkernel - change which send buffers are available for kernel
  1494. * @dd: the infinipath device
  1495. * @start: the starting send buffer number
  1496. * @len: the number of send buffers
  1497. * @avail: true if the buffers are available for kernel use, false otherwise
  1498. */
  1499. void ipath_chg_pioavailkernel(struct ipath_devdata *dd, unsigned start,
  1500. unsigned len, int avail)
  1501. {
  1502. unsigned long flags;
  1503. unsigned end, cnt = 0;
  1504. /* There are two bits per send buffer (busy and generation) */
  1505. start *= 2;
  1506. end = start + len * 2;
  1507. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1508. /* Set or clear the busy bit in the shadow. */
  1509. while (start < end) {
  1510. if (avail) {
  1511. unsigned long dma;
  1512. int i, im;
  1513. /*
  1514. * the BUSY bit will never be set, because we disarm
  1515. * the user buffers before we hand them back to the
  1516. * kernel. We do have to make sure the generation
  1517. * bit is set correctly in shadow, since it could
  1518. * have changed many times while allocated to user.
  1519. * We can't use the bitmap functions on the full
  1520. * dma array because it is always little-endian, so
  1521. * we have to flip to host-order first.
  1522. * BITS_PER_LONG is slightly wrong, since it's
  1523. * always 64 bits per register in chip...
  1524. * We only work on 64 bit kernels, so that's OK.
  1525. */
  1526. /* deal with 6110 chip bug on high register #s */
  1527. i = start / BITS_PER_LONG;
  1528. im = (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS)) ?
  1529. i ^ 1 : i;
  1530. __clear_bit(INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT
  1531. + start, dd->ipath_pioavailshadow);
  1532. dma = (unsigned long) le64_to_cpu(
  1533. dd->ipath_pioavailregs_dma[im]);
  1534. if (test_bit((INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1535. + start) % BITS_PER_LONG, &dma))
  1536. __set_bit(INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1537. + start, dd->ipath_pioavailshadow);
  1538. else
  1539. __clear_bit(INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1540. + start, dd->ipath_pioavailshadow);
  1541. __set_bit(start, dd->ipath_pioavailkernel);
  1542. } else {
  1543. __set_bit(start + INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT,
  1544. dd->ipath_pioavailshadow);
  1545. __clear_bit(start, dd->ipath_pioavailkernel);
  1546. }
  1547. start += 2;
  1548. }
  1549. if (dd->ipath_pioupd_thresh) {
  1550. end = 2 * (dd->ipath_piobcnt2k + dd->ipath_piobcnt4k);
  1551. cnt = bitmap_weight(dd->ipath_pioavailkernel, end);
  1552. }
  1553. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1554. /*
  1555. * When moving buffers from kernel to user, if number assigned to
  1556. * the user is less than the pio update threshold, and threshold
  1557. * is supported (cnt was computed > 0), drop the update threshold
  1558. * so we update at least once per allocated number of buffers.
  1559. * In any case, if the kernel buffers are less than the threshold,
  1560. * drop the threshold. We don't bother increasing it, having once
  1561. * decreased it, since it would typically just cycle back and forth.
  1562. * If we don't decrease below buffers in use, we can wait a long
  1563. * time for an update, until some other context uses PIO buffers.
  1564. */
  1565. if (!avail && len < cnt)
  1566. cnt = len;
  1567. if (cnt < dd->ipath_pioupd_thresh) {
  1568. dd->ipath_pioupd_thresh = cnt;
  1569. ipath_dbg("Decreased pio update threshold to %u\n",
  1570. dd->ipath_pioupd_thresh);
  1571. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1572. dd->ipath_sendctrl &= ~(INFINIPATH_S_UPDTHRESH_MASK
  1573. << INFINIPATH_S_UPDTHRESH_SHIFT);
  1574. dd->ipath_sendctrl |= dd->ipath_pioupd_thresh
  1575. << INFINIPATH_S_UPDTHRESH_SHIFT;
  1576. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1577. dd->ipath_sendctrl);
  1578. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1579. }
  1580. }
  1581. /**
  1582. * ipath_create_rcvhdrq - create a receive header queue
  1583. * @dd: the infinipath device
  1584. * @pd: the port data
  1585. *
  1586. * this must be contiguous memory (from an i/o perspective), and must be
  1587. * DMA'able (which means for some systems, it will go through an IOMMU,
  1588. * or be forced into a low address range).
  1589. */
  1590. int ipath_create_rcvhdrq(struct ipath_devdata *dd,
  1591. struct ipath_portdata *pd)
  1592. {
  1593. int ret = 0;
  1594. if (!pd->port_rcvhdrq) {
  1595. dma_addr_t phys_hdrqtail;
  1596. gfp_t gfp_flags = GFP_USER | __GFP_COMP;
  1597. int amt = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1598. sizeof(u32), PAGE_SIZE);
  1599. pd->port_rcvhdrq = dma_alloc_coherent(
  1600. &dd->pcidev->dev, amt, &pd->port_rcvhdrq_phys,
  1601. gfp_flags);
  1602. if (!pd->port_rcvhdrq) {
  1603. ipath_dev_err(dd, "attempt to allocate %d bytes "
  1604. "for port %u rcvhdrq failed\n",
  1605. amt, pd->port_port);
  1606. ret = -ENOMEM;
  1607. goto bail;
  1608. }
  1609. if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
  1610. pd->port_rcvhdrtail_kvaddr = dma_alloc_coherent(
  1611. &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
  1612. GFP_KERNEL);
  1613. if (!pd->port_rcvhdrtail_kvaddr) {
  1614. ipath_dev_err(dd, "attempt to allocate 1 page "
  1615. "for port %u rcvhdrqtailaddr "
  1616. "failed\n", pd->port_port);
  1617. ret = -ENOMEM;
  1618. dma_free_coherent(&dd->pcidev->dev, amt,
  1619. pd->port_rcvhdrq,
  1620. pd->port_rcvhdrq_phys);
  1621. pd->port_rcvhdrq = NULL;
  1622. goto bail;
  1623. }
  1624. pd->port_rcvhdrqtailaddr_phys = phys_hdrqtail;
  1625. ipath_cdbg(VERBOSE, "port %d hdrtailaddr, %llx "
  1626. "physical\n", pd->port_port,
  1627. (unsigned long long) phys_hdrqtail);
  1628. }
  1629. pd->port_rcvhdrq_size = amt;
  1630. ipath_cdbg(VERBOSE, "%d pages at %p (phys %lx) size=%lu "
  1631. "for port %u rcvhdr Q\n",
  1632. amt >> PAGE_SHIFT, pd->port_rcvhdrq,
  1633. (unsigned long) pd->port_rcvhdrq_phys,
  1634. (unsigned long) pd->port_rcvhdrq_size,
  1635. pd->port_port);
  1636. }
  1637. else
  1638. ipath_cdbg(VERBOSE, "reuse port %d rcvhdrq @%p %llx phys; "
  1639. "hdrtailaddr@%p %llx physical\n",
  1640. pd->port_port, pd->port_rcvhdrq,
  1641. (unsigned long long) pd->port_rcvhdrq_phys,
  1642. pd->port_rcvhdrtail_kvaddr, (unsigned long long)
  1643. pd->port_rcvhdrqtailaddr_phys);
  1644. /* clear for security and sanity on each use */
  1645. memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
  1646. if (pd->port_rcvhdrtail_kvaddr)
  1647. memset(pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
  1648. /*
  1649. * tell chip each time we init it, even if we are re-using previous
  1650. * memory (we zero the register at process close)
  1651. */
  1652. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  1653. pd->port_port, pd->port_rcvhdrqtailaddr_phys);
  1654. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1655. pd->port_port, pd->port_rcvhdrq_phys);
  1656. bail:
  1657. return ret;
  1658. }
  1659. /*
  1660. * Flush all sends that might be in the ready to send state, as well as any
  1661. * that are in the process of being sent. Used whenever we need to be
  1662. * sure the send side is idle. Cleans up all buffer state by canceling
  1663. * all pio buffers, and issuing an abort, which cleans up anything in the
  1664. * launch fifo. The cancel is superfluous on some chip versions, but
  1665. * it's safer to always do it.
  1666. * PIOAvail bits are updated by the chip as if normal send had happened.
  1667. */
  1668. void ipath_cancel_sends(struct ipath_devdata *dd, int restore_sendctrl)
  1669. {
  1670. unsigned long flags;
  1671. if (dd->ipath_flags & IPATH_IB_AUTONEG_INPROG) {
  1672. ipath_cdbg(VERBOSE, "Ignore while in autonegotiation\n");
  1673. goto bail;
  1674. }
  1675. /*
  1676. * If we have SDMA, and it's not disabled, we have to kick off the
  1677. * abort state machine, provided we aren't already aborting.
  1678. * If we are in the process of aborting SDMA (!DISABLED, but ABORTING),
  1679. * we skip the rest of this routine. It is already "in progress"
  1680. */
  1681. if (dd->ipath_flags & IPATH_HAS_SEND_DMA) {
  1682. int skip_cancel;
  1683. unsigned long *statp = &dd->ipath_sdma_status;
  1684. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  1685. skip_cancel =
  1686. test_and_set_bit(IPATH_SDMA_ABORTING, statp)
  1687. && !test_bit(IPATH_SDMA_DISABLED, statp);
  1688. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  1689. if (skip_cancel)
  1690. goto bail;
  1691. }
  1692. ipath_dbg("Cancelling all in-progress send buffers\n");
  1693. /* skip armlaunch errs for a while */
  1694. dd->ipath_lastcancel = jiffies + HZ / 2;
  1695. /*
  1696. * The abort bit is auto-clearing. We also don't want pioavail
  1697. * update happening during this, and we don't want any other
  1698. * sends going out, so turn those off for the duration. We read
  1699. * the scratch register to be sure that cancels and the abort
  1700. * have taken effect in the chip. Otherwise two parts are same
  1701. * as ipath_force_pio_avail_update()
  1702. */
  1703. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1704. dd->ipath_sendctrl &= ~(INFINIPATH_S_PIOBUFAVAILUPD
  1705. | INFINIPATH_S_PIOENABLE);
  1706. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1707. dd->ipath_sendctrl | INFINIPATH_S_ABORT);
  1708. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1709. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1710. /* disarm all send buffers */
  1711. ipath_disarm_piobufs(dd, 0,
  1712. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k);
  1713. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  1714. set_bit(IPATH_SDMA_DISARMED, &dd->ipath_sdma_status);
  1715. if (restore_sendctrl) {
  1716. /* else done by caller later if needed */
  1717. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1718. dd->ipath_sendctrl |= INFINIPATH_S_PIOBUFAVAILUPD |
  1719. INFINIPATH_S_PIOENABLE;
  1720. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1721. dd->ipath_sendctrl);
  1722. /* and again, be sure all have hit the chip */
  1723. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1724. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1725. }
  1726. if ((dd->ipath_flags & IPATH_HAS_SEND_DMA) &&
  1727. !test_bit(IPATH_SDMA_DISABLED, &dd->ipath_sdma_status) &&
  1728. test_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status)) {
  1729. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  1730. /* only wait so long for intr */
  1731. dd->ipath_sdma_abort_intr_timeout = jiffies + HZ;
  1732. dd->ipath_sdma_reset_wait = 200;
  1733. if (!test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  1734. tasklet_hi_schedule(&dd->ipath_sdma_abort_task);
  1735. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  1736. }
  1737. bail:;
  1738. }
  1739. /*
  1740. * Force an update of in-memory copy of the pioavail registers, when
  1741. * needed for any of a variety of reasons. We read the scratch register
  1742. * to make it highly likely that the update will have happened by the
  1743. * time we return. If already off (as in cancel_sends above), this
  1744. * routine is a nop, on the assumption that the caller will "do the
  1745. * right thing".
  1746. */
  1747. void ipath_force_pio_avail_update(struct ipath_devdata *dd)
  1748. {
  1749. unsigned long flags;
  1750. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1751. if (dd->ipath_sendctrl & INFINIPATH_S_PIOBUFAVAILUPD) {
  1752. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1753. dd->ipath_sendctrl & ~INFINIPATH_S_PIOBUFAVAILUPD);
  1754. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1755. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1756. dd->ipath_sendctrl);
  1757. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1758. }
  1759. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1760. }
  1761. static void ipath_set_ib_lstate(struct ipath_devdata *dd, int linkcmd,
  1762. int linitcmd)
  1763. {
  1764. u64 mod_wd;
  1765. static const char *what[4] = {
  1766. [0] = "NOP",
  1767. [INFINIPATH_IBCC_LINKCMD_DOWN] = "DOWN",
  1768. [INFINIPATH_IBCC_LINKCMD_ARMED] = "ARMED",
  1769. [INFINIPATH_IBCC_LINKCMD_ACTIVE] = "ACTIVE"
  1770. };
  1771. if (linitcmd == INFINIPATH_IBCC_LINKINITCMD_DISABLE) {
  1772. /*
  1773. * If we are told to disable, note that so link-recovery
  1774. * code does not attempt to bring us back up.
  1775. */
  1776. preempt_disable();
  1777. dd->ipath_flags |= IPATH_IB_LINK_DISABLED;
  1778. preempt_enable();
  1779. } else if (linitcmd) {
  1780. /*
  1781. * Any other linkinitcmd will lead to LINKDOWN and then
  1782. * to INIT (if all is well), so clear flag to let
  1783. * link-recovery code attempt to bring us back up.
  1784. */
  1785. preempt_disable();
  1786. dd->ipath_flags &= ~IPATH_IB_LINK_DISABLED;
  1787. preempt_enable();
  1788. }
  1789. mod_wd = (linkcmd << dd->ibcc_lc_shift) |
  1790. (linitcmd << INFINIPATH_IBCC_LINKINITCMD_SHIFT);
  1791. ipath_cdbg(VERBOSE,
  1792. "Moving unit %u to %s (initcmd=0x%x), current ltstate is %s\n",
  1793. dd->ipath_unit, what[linkcmd], linitcmd,
  1794. ipath_ibcstatus_str[ipath_ib_linktrstate(dd,
  1795. ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus))]);
  1796. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1797. dd->ipath_ibcctrl | mod_wd);
  1798. /* read from chip so write is flushed */
  1799. (void) ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  1800. }
  1801. int ipath_set_linkstate(struct ipath_devdata *dd, u8 newstate)
  1802. {
  1803. u32 lstate;
  1804. int ret;
  1805. switch (newstate) {
  1806. case IPATH_IB_LINKDOWN_ONLY:
  1807. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN, 0);
  1808. /* don't wait */
  1809. ret = 0;
  1810. goto bail;
  1811. case IPATH_IB_LINKDOWN:
  1812. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1813. INFINIPATH_IBCC_LINKINITCMD_POLL);
  1814. /* don't wait */
  1815. ret = 0;
  1816. goto bail;
  1817. case IPATH_IB_LINKDOWN_SLEEP:
  1818. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1819. INFINIPATH_IBCC_LINKINITCMD_SLEEP);
  1820. /* don't wait */
  1821. ret = 0;
  1822. goto bail;
  1823. case IPATH_IB_LINKDOWN_DISABLE:
  1824. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1825. INFINIPATH_IBCC_LINKINITCMD_DISABLE);
  1826. /* don't wait */
  1827. ret = 0;
  1828. goto bail;
  1829. case IPATH_IB_LINKARM:
  1830. if (dd->ipath_flags & IPATH_LINKARMED) {
  1831. ret = 0;
  1832. goto bail;
  1833. }
  1834. if (!(dd->ipath_flags &
  1835. (IPATH_LINKINIT | IPATH_LINKACTIVE))) {
  1836. ret = -EINVAL;
  1837. goto bail;
  1838. }
  1839. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ARMED, 0);
  1840. /*
  1841. * Since the port can transition to ACTIVE by receiving
  1842. * a non VL 15 packet, wait for either state.
  1843. */
  1844. lstate = IPATH_LINKARMED | IPATH_LINKACTIVE;
  1845. break;
  1846. case IPATH_IB_LINKACTIVE:
  1847. if (dd->ipath_flags & IPATH_LINKACTIVE) {
  1848. ret = 0;
  1849. goto bail;
  1850. }
  1851. if (!(dd->ipath_flags & IPATH_LINKARMED)) {
  1852. ret = -EINVAL;
  1853. goto bail;
  1854. }
  1855. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ACTIVE, 0);
  1856. lstate = IPATH_LINKACTIVE;
  1857. break;
  1858. case IPATH_IB_LINK_LOOPBACK:
  1859. dev_info(&dd->pcidev->dev, "Enabling IB local loopback\n");
  1860. dd->ipath_ibcctrl |= INFINIPATH_IBCC_LOOPBACK;
  1861. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1862. dd->ipath_ibcctrl);
  1863. /* turn heartbeat off, as it causes loopback to fail */
  1864. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1865. IPATH_IB_HRTBT_OFF);
  1866. /* don't wait */
  1867. ret = 0;
  1868. goto bail;
  1869. case IPATH_IB_LINK_EXTERNAL:
  1870. dev_info(&dd->pcidev->dev,
  1871. "Disabling IB local loopback (normal)\n");
  1872. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1873. IPATH_IB_HRTBT_ON);
  1874. dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LOOPBACK;
  1875. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1876. dd->ipath_ibcctrl);
  1877. /* don't wait */
  1878. ret = 0;
  1879. goto bail;
  1880. /*
  1881. * Heartbeat can be explicitly enabled by the user via
  1882. * "hrtbt_enable" "file", and if disabled, trying to enable here
  1883. * will have no effect. Implicit changes (heartbeat off when
  1884. * loopback on, and vice versa) are included to ease testing.
  1885. */
  1886. case IPATH_IB_LINK_HRTBT:
  1887. ret = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1888. IPATH_IB_HRTBT_ON);
  1889. goto bail;
  1890. case IPATH_IB_LINK_NO_HRTBT:
  1891. ret = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1892. IPATH_IB_HRTBT_OFF);
  1893. goto bail;
  1894. default:
  1895. ipath_dbg("Invalid linkstate 0x%x requested\n", newstate);
  1896. ret = -EINVAL;
  1897. goto bail;
  1898. }
  1899. ret = ipath_wait_linkstate(dd, lstate, 2000);
  1900. bail:
  1901. return ret;
  1902. }
  1903. /**
  1904. * ipath_set_mtu - set the MTU
  1905. * @dd: the infinipath device
  1906. * @arg: the new MTU
  1907. *
  1908. * we can handle "any" incoming size, the issue here is whether we
  1909. * need to restrict our outgoing size. For now, we don't do any
  1910. * sanity checking on this, and we don't deal with what happens to
  1911. * programs that are already running when the size changes.
  1912. * NOTE: changing the MTU will usually cause the IBC to go back to
  1913. * link INIT state...
  1914. */
  1915. int ipath_set_mtu(struct ipath_devdata *dd, u16 arg)
  1916. {
  1917. u32 piosize;
  1918. int changed = 0;
  1919. int ret;
  1920. /*
  1921. * mtu is IB data payload max. It's the largest power of 2 less
  1922. * than piosize (or even larger, since it only really controls the
  1923. * largest we can receive; we can send the max of the mtu and
  1924. * piosize). We check that it's one of the valid IB sizes.
  1925. */
  1926. if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
  1927. (arg != 4096 || !ipath_mtu4096)) {
  1928. ipath_dbg("Trying to set invalid mtu %u, failing\n", arg);
  1929. ret = -EINVAL;
  1930. goto bail;
  1931. }
  1932. if (dd->ipath_ibmtu == arg) {
  1933. ret = 0; /* same as current */
  1934. goto bail;
  1935. }
  1936. piosize = dd->ipath_ibmaxlen;
  1937. dd->ipath_ibmtu = arg;
  1938. if (arg >= (piosize - IPATH_PIO_MAXIBHDR)) {
  1939. /* Only if it's not the initial value (or reset to it) */
  1940. if (piosize != dd->ipath_init_ibmaxlen) {
  1941. if (arg > piosize && arg <= dd->ipath_init_ibmaxlen)
  1942. piosize = dd->ipath_init_ibmaxlen;
  1943. dd->ipath_ibmaxlen = piosize;
  1944. changed = 1;
  1945. }
  1946. } else if ((arg + IPATH_PIO_MAXIBHDR) != dd->ipath_ibmaxlen) {
  1947. piosize = arg + IPATH_PIO_MAXIBHDR;
  1948. ipath_cdbg(VERBOSE, "ibmaxlen was 0x%x, setting to 0x%x "
  1949. "(mtu 0x%x)\n", dd->ipath_ibmaxlen, piosize,
  1950. arg);
  1951. dd->ipath_ibmaxlen = piosize;
  1952. changed = 1;
  1953. }
  1954. if (changed) {
  1955. u64 ibc = dd->ipath_ibcctrl, ibdw;
  1956. /*
  1957. * update our housekeeping variables, and set IBC max
  1958. * size, same as init code; max IBC is max we allow in
  1959. * buffer, less the qword pbc, plus 1 for ICRC, in dwords
  1960. */
  1961. dd->ipath_ibmaxlen = piosize - 2 * sizeof(u32);
  1962. ibdw = (dd->ipath_ibmaxlen >> 2) + 1;
  1963. ibc &= ~(INFINIPATH_IBCC_MAXPKTLEN_MASK <<
  1964. dd->ibcc_mpl_shift);
  1965. ibc |= ibdw << dd->ibcc_mpl_shift;
  1966. dd->ipath_ibcctrl = ibc;
  1967. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1968. dd->ipath_ibcctrl);
  1969. dd->ipath_f_tidtemplate(dd);
  1970. }
  1971. ret = 0;
  1972. bail:
  1973. return ret;
  1974. }
  1975. int ipath_set_lid(struct ipath_devdata *dd, u32 lid, u8 lmc)
  1976. {
  1977. dd->ipath_lid = lid;
  1978. dd->ipath_lmc = lmc;
  1979. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LIDLMC, lid |
  1980. (~((1U << lmc) - 1)) << 16);
  1981. dev_info(&dd->pcidev->dev, "We got a lid: 0x%x\n", lid);
  1982. return 0;
  1983. }
  1984. /**
  1985. * ipath_write_kreg_port - write a device's per-port 64-bit kernel register
  1986. * @dd: the infinipath device
  1987. * @regno: the register number to write
  1988. * @port: the port containing the register
  1989. * @value: the value to write
  1990. *
  1991. * Registers that vary with the chip implementation constants (port)
  1992. * use this routine.
  1993. */
  1994. void ipath_write_kreg_port(const struct ipath_devdata *dd, ipath_kreg regno,
  1995. unsigned port, u64 value)
  1996. {
  1997. u16 where;
  1998. if (port < dd->ipath_portcnt &&
  1999. (regno == dd->ipath_kregs->kr_rcvhdraddr ||
  2000. regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
  2001. where = regno + port;
  2002. else
  2003. where = -1;
  2004. ipath_write_kreg(dd, where, value);
  2005. }
  2006. /*
  2007. * Following deal with the "obviously simple" task of overriding the state
  2008. * of the LEDS, which normally indicate link physical and logical status.
  2009. * The complications arise in dealing with different hardware mappings
  2010. * and the board-dependent routine being called from interrupts.
  2011. * and then there's the requirement to _flash_ them.
  2012. */
  2013. #define LED_OVER_FREQ_SHIFT 8
  2014. #define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
  2015. /* Below is "non-zero" to force override, but both actual LEDs are off */
  2016. #define LED_OVER_BOTH_OFF (8)
  2017. static void ipath_run_led_override(unsigned long opaque)
  2018. {
  2019. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  2020. int timeoff;
  2021. int pidx;
  2022. u64 lstate, ltstate, val;
  2023. if (!(dd->ipath_flags & IPATH_INITTED))
  2024. return;
  2025. pidx = dd->ipath_led_override_phase++ & 1;
  2026. dd->ipath_led_override = dd->ipath_led_override_vals[pidx];
  2027. timeoff = dd->ipath_led_override_timeoff;
  2028. /*
  2029. * below potentially restores the LED values per current status,
  2030. * should also possibly setup the traffic-blink register,
  2031. * but leave that to per-chip functions.
  2032. */
  2033. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  2034. ltstate = ipath_ib_linktrstate(dd, val);
  2035. lstate = ipath_ib_linkstate(dd, val);
  2036. dd->ipath_f_setextled(dd, lstate, ltstate);
  2037. mod_timer(&dd->ipath_led_override_timer, jiffies + timeoff);
  2038. }
  2039. void ipath_set_led_override(struct ipath_devdata *dd, unsigned int val)
  2040. {
  2041. int timeoff, freq;
  2042. if (!(dd->ipath_flags & IPATH_INITTED))
  2043. return;
  2044. /* First check if we are blinking. If not, use 1HZ polling */
  2045. timeoff = HZ;
  2046. freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
  2047. if (freq) {
  2048. /* For blink, set each phase from one nybble of val */
  2049. dd->ipath_led_override_vals[0] = val & 0xF;
  2050. dd->ipath_led_override_vals[1] = (val >> 4) & 0xF;
  2051. timeoff = (HZ << 4)/freq;
  2052. } else {
  2053. /* Non-blink set both phases the same. */
  2054. dd->ipath_led_override_vals[0] = val & 0xF;
  2055. dd->ipath_led_override_vals[1] = val & 0xF;
  2056. }
  2057. dd->ipath_led_override_timeoff = timeoff;
  2058. /*
  2059. * If the timer has not already been started, do so. Use a "quick"
  2060. * timeout so the function will be called soon, to look at our request.
  2061. */
  2062. if (atomic_inc_return(&dd->ipath_led_override_timer_active) == 1) {
  2063. /* Need to start timer */
  2064. init_timer(&dd->ipath_led_override_timer);
  2065. dd->ipath_led_override_timer.function =
  2066. ipath_run_led_override;
  2067. dd->ipath_led_override_timer.data = (unsigned long) dd;
  2068. dd->ipath_led_override_timer.expires = jiffies + 1;
  2069. add_timer(&dd->ipath_led_override_timer);
  2070. } else
  2071. atomic_dec(&dd->ipath_led_override_timer_active);
  2072. }
  2073. /**
  2074. * ipath_shutdown_device - shut down a device
  2075. * @dd: the infinipath device
  2076. *
  2077. * This is called to make the device quiet when we are about to
  2078. * unload the driver, and also when the device is administratively
  2079. * disabled. It does not free any data structures.
  2080. * Everything it does has to be setup again by ipath_init_chip(dd,1)
  2081. */
  2082. void ipath_shutdown_device(struct ipath_devdata *dd)
  2083. {
  2084. unsigned long flags;
  2085. ipath_dbg("Shutting down the device\n");
  2086. ipath_hol_up(dd); /* make sure user processes aren't suspended */
  2087. dd->ipath_flags |= IPATH_LINKUNK;
  2088. dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
  2089. IPATH_LINKINIT | IPATH_LINKARMED |
  2090. IPATH_LINKACTIVE);
  2091. *dd->ipath_statusp &= ~(IPATH_STATUS_IB_CONF |
  2092. IPATH_STATUS_IB_READY);
  2093. /* mask interrupts, but not errors */
  2094. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
  2095. dd->ipath_rcvctrl = 0;
  2096. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  2097. dd->ipath_rcvctrl);
  2098. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  2099. teardown_sdma(dd);
  2100. /*
  2101. * gracefully stop all sends allowing any in progress to trickle out
  2102. * first.
  2103. */
  2104. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  2105. dd->ipath_sendctrl = 0;
  2106. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  2107. /* flush it */
  2108. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  2109. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  2110. /*
  2111. * enough for anything that's going to trickle out to have actually
  2112. * done so.
  2113. */
  2114. udelay(5);
  2115. dd->ipath_f_setextled(dd, 0, 0); /* make sure LEDs are off */
  2116. ipath_set_ib_lstate(dd, 0, INFINIPATH_IBCC_LINKINITCMD_DISABLE);
  2117. ipath_cancel_sends(dd, 0);
  2118. /*
  2119. * we are shutting down, so tell components that care. We don't do
  2120. * this on just a link state change, much like ethernet, a cable
  2121. * unplug, etc. doesn't change driver state
  2122. */
  2123. signal_ib_event(dd, IB_EVENT_PORT_ERR);
  2124. /* disable IBC */
  2125. dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
  2126. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  2127. dd->ipath_control | INFINIPATH_C_FREEZEMODE);
  2128. /*
  2129. * clear SerdesEnable and turn the leds off; do this here because
  2130. * we are unloading, so don't count on interrupts to move along
  2131. * Turn the LEDs off explictly for the same reason.
  2132. */
  2133. dd->ipath_f_quiet_serdes(dd);
  2134. /* stop all the timers that might still be running */
  2135. del_timer_sync(&dd->ipath_hol_timer);
  2136. if (dd->ipath_stats_timer_active) {
  2137. del_timer_sync(&dd->ipath_stats_timer);
  2138. dd->ipath_stats_timer_active = 0;
  2139. }
  2140. if (dd->ipath_intrchk_timer.data) {
  2141. del_timer_sync(&dd->ipath_intrchk_timer);
  2142. dd->ipath_intrchk_timer.data = 0;
  2143. }
  2144. if (atomic_read(&dd->ipath_led_override_timer_active)) {
  2145. del_timer_sync(&dd->ipath_led_override_timer);
  2146. atomic_set(&dd->ipath_led_override_timer_active, 0);
  2147. }
  2148. /*
  2149. * clear all interrupts and errors, so that the next time the driver
  2150. * is loaded or device is enabled, we know that whatever is set
  2151. * happened while we were unloaded
  2152. */
  2153. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
  2154. ~0ULL & ~INFINIPATH_HWE_MEMBISTFAILED);
  2155. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
  2156. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
  2157. ipath_cdbg(VERBOSE, "Flush time and errors to EEPROM\n");
  2158. ipath_update_eeprom_log(dd);
  2159. }
  2160. /**
  2161. * ipath_free_pddata - free a port's allocated data
  2162. * @dd: the infinipath device
  2163. * @pd: the portdata structure
  2164. *
  2165. * free up any allocated data for a port
  2166. * This should not touch anything that would affect a simultaneous
  2167. * re-allocation of port data, because it is called after ipath_mutex
  2168. * is released (and can be called from reinit as well).
  2169. * It should never change any chip state, or global driver state.
  2170. * (The only exception to global state is freeing the port0 port0_skbs.)
  2171. */
  2172. void ipath_free_pddata(struct ipath_devdata *dd, struct ipath_portdata *pd)
  2173. {
  2174. if (!pd)
  2175. return;
  2176. if (pd->port_rcvhdrq) {
  2177. ipath_cdbg(VERBOSE, "free closed port %d rcvhdrq @ %p "
  2178. "(size=%lu)\n", pd->port_port, pd->port_rcvhdrq,
  2179. (unsigned long) pd->port_rcvhdrq_size);
  2180. dma_free_coherent(&dd->pcidev->dev, pd->port_rcvhdrq_size,
  2181. pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
  2182. pd->port_rcvhdrq = NULL;
  2183. if (pd->port_rcvhdrtail_kvaddr) {
  2184. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  2185. pd->port_rcvhdrtail_kvaddr,
  2186. pd->port_rcvhdrqtailaddr_phys);
  2187. pd->port_rcvhdrtail_kvaddr = NULL;
  2188. }
  2189. }
  2190. if (pd->port_port && pd->port_rcvegrbuf) {
  2191. unsigned e;
  2192. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  2193. void *base = pd->port_rcvegrbuf[e];
  2194. size_t size = pd->port_rcvegrbuf_size;
  2195. ipath_cdbg(VERBOSE, "egrbuf free(%p, %lu), "
  2196. "chunk %u/%u\n", base,
  2197. (unsigned long) size,
  2198. e, pd->port_rcvegrbuf_chunks);
  2199. dma_free_coherent(&dd->pcidev->dev, size,
  2200. base, pd->port_rcvegrbuf_phys[e]);
  2201. }
  2202. kfree(pd->port_rcvegrbuf);
  2203. pd->port_rcvegrbuf = NULL;
  2204. kfree(pd->port_rcvegrbuf_phys);
  2205. pd->port_rcvegrbuf_phys = NULL;
  2206. pd->port_rcvegrbuf_chunks = 0;
  2207. } else if (pd->port_port == 0 && dd->ipath_port0_skbinfo) {
  2208. unsigned e;
  2209. struct ipath_skbinfo *skbinfo = dd->ipath_port0_skbinfo;
  2210. dd->ipath_port0_skbinfo = NULL;
  2211. ipath_cdbg(VERBOSE, "free closed port %d "
  2212. "ipath_port0_skbinfo @ %p\n", pd->port_port,
  2213. skbinfo);
  2214. for (e = 0; e < dd->ipath_p0_rcvegrcnt; e++)
  2215. if (skbinfo[e].skb) {
  2216. pci_unmap_single(dd->pcidev, skbinfo[e].phys,
  2217. dd->ipath_ibmaxlen,
  2218. PCI_DMA_FROMDEVICE);
  2219. dev_kfree_skb(skbinfo[e].skb);
  2220. }
  2221. vfree(skbinfo);
  2222. }
  2223. kfree(pd->port_tid_pg_list);
  2224. vfree(pd->subport_uregbase);
  2225. vfree(pd->subport_rcvegrbuf);
  2226. vfree(pd->subport_rcvhdr_base);
  2227. kfree(pd);
  2228. }
  2229. static int __init infinipath_init(void)
  2230. {
  2231. int ret;
  2232. if (ipath_debug & __IPATH_DBG)
  2233. printk(KERN_INFO DRIVER_LOAD_MSG "%s", ib_ipath_version);
  2234. /*
  2235. * These must be called before the driver is registered with
  2236. * the PCI subsystem.
  2237. */
  2238. idr_init(&unit_table);
  2239. if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
  2240. printk(KERN_ERR IPATH_DRV_NAME ": idr_pre_get() failed\n");
  2241. ret = -ENOMEM;
  2242. goto bail;
  2243. }
  2244. ret = pci_register_driver(&ipath_driver);
  2245. if (ret < 0) {
  2246. printk(KERN_ERR IPATH_DRV_NAME
  2247. ": Unable to register driver: error %d\n", -ret);
  2248. goto bail_unit;
  2249. }
  2250. ret = ipath_init_ipathfs();
  2251. if (ret < 0) {
  2252. printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
  2253. "ipathfs: error %d\n", -ret);
  2254. goto bail_pci;
  2255. }
  2256. goto bail;
  2257. bail_pci:
  2258. pci_unregister_driver(&ipath_driver);
  2259. bail_unit:
  2260. idr_destroy(&unit_table);
  2261. bail:
  2262. return ret;
  2263. }
  2264. static void __exit infinipath_cleanup(void)
  2265. {
  2266. ipath_exit_ipathfs();
  2267. ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
  2268. pci_unregister_driver(&ipath_driver);
  2269. idr_destroy(&unit_table);
  2270. }
  2271. /**
  2272. * ipath_reset_device - reset the chip if possible
  2273. * @unit: the device to reset
  2274. *
  2275. * Whether or not reset is successful, we attempt to re-initialize the chip
  2276. * (that is, much like a driver unload/reload). We clear the INITTED flag
  2277. * so that the various entry points will fail until we reinitialize. For
  2278. * now, we only allow this if no user ports are open that use chip resources
  2279. */
  2280. int ipath_reset_device(int unit)
  2281. {
  2282. int ret, i;
  2283. struct ipath_devdata *dd = ipath_lookup(unit);
  2284. unsigned long flags;
  2285. if (!dd) {
  2286. ret = -ENODEV;
  2287. goto bail;
  2288. }
  2289. if (atomic_read(&dd->ipath_led_override_timer_active)) {
  2290. /* Need to stop LED timer, _then_ shut off LEDs */
  2291. del_timer_sync(&dd->ipath_led_override_timer);
  2292. atomic_set(&dd->ipath_led_override_timer_active, 0);
  2293. }
  2294. /* Shut off LEDs after we are sure timer is not running */
  2295. dd->ipath_led_override = LED_OVER_BOTH_OFF;
  2296. dd->ipath_f_setextled(dd, 0, 0);
  2297. dev_info(&dd->pcidev->dev, "Reset on unit %u requested\n", unit);
  2298. if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT)) {
  2299. dev_info(&dd->pcidev->dev, "Invalid unit number %u or "
  2300. "not initialized or not present\n", unit);
  2301. ret = -ENXIO;
  2302. goto bail;
  2303. }
  2304. spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
  2305. if (dd->ipath_pd)
  2306. for (i = 1; i < dd->ipath_cfgports; i++) {
  2307. if (!dd->ipath_pd[i] || !dd->ipath_pd[i]->port_cnt)
  2308. continue;
  2309. spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
  2310. ipath_dbg("unit %u port %d is in use "
  2311. "(PID %u cmd %s), can't reset\n",
  2312. unit, i,
  2313. pid_nr(dd->ipath_pd[i]->port_pid),
  2314. dd->ipath_pd[i]->port_comm);
  2315. ret = -EBUSY;
  2316. goto bail;
  2317. }
  2318. spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
  2319. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  2320. teardown_sdma(dd);
  2321. dd->ipath_flags &= ~IPATH_INITTED;
  2322. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
  2323. ret = dd->ipath_f_reset(dd);
  2324. if (ret == 1) {
  2325. ipath_dbg("Reinitializing unit %u after reset attempt\n",
  2326. unit);
  2327. ret = ipath_init_chip(dd, 1);
  2328. } else
  2329. ret = -EAGAIN;
  2330. if (ret)
  2331. ipath_dev_err(dd, "Reinitialize unit %u after "
  2332. "reset failed with %d\n", unit, ret);
  2333. else
  2334. dev_info(&dd->pcidev->dev, "Reinitialized unit %u after "
  2335. "resetting\n", unit);
  2336. bail:
  2337. return ret;
  2338. }
  2339. /*
  2340. * send a signal to all the processes that have the driver open
  2341. * through the normal interfaces (i.e., everything other than diags
  2342. * interface). Returns number of signalled processes.
  2343. */
  2344. static int ipath_signal_procs(struct ipath_devdata *dd, int sig)
  2345. {
  2346. int i, sub, any = 0;
  2347. struct pid *pid;
  2348. unsigned long flags;
  2349. if (!dd->ipath_pd)
  2350. return 0;
  2351. spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
  2352. for (i = 1; i < dd->ipath_cfgports; i++) {
  2353. if (!dd->ipath_pd[i] || !dd->ipath_pd[i]->port_cnt)
  2354. continue;
  2355. pid = dd->ipath_pd[i]->port_pid;
  2356. if (!pid)
  2357. continue;
  2358. dev_info(&dd->pcidev->dev, "context %d in use "
  2359. "(PID %u), sending signal %d\n",
  2360. i, pid_nr(pid), sig);
  2361. kill_pid(pid, sig, 1);
  2362. any++;
  2363. for (sub = 0; sub < INFINIPATH_MAX_SUBPORT; sub++) {
  2364. pid = dd->ipath_pd[i]->port_subpid[sub];
  2365. if (!pid)
  2366. continue;
  2367. dev_info(&dd->pcidev->dev, "sub-context "
  2368. "%d:%d in use (PID %u), sending "
  2369. "signal %d\n", i, sub, pid_nr(pid), sig);
  2370. kill_pid(pid, sig, 1);
  2371. any++;
  2372. }
  2373. }
  2374. spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
  2375. return any;
  2376. }
  2377. static void ipath_hol_signal_down(struct ipath_devdata *dd)
  2378. {
  2379. if (ipath_signal_procs(dd, SIGSTOP))
  2380. ipath_dbg("Stopped some processes\n");
  2381. ipath_cancel_sends(dd, 1);
  2382. }
  2383. static void ipath_hol_signal_up(struct ipath_devdata *dd)
  2384. {
  2385. if (ipath_signal_procs(dd, SIGCONT))
  2386. ipath_dbg("Continued some processes\n");
  2387. }
  2388. /*
  2389. * link is down, stop any users processes, and flush pending sends
  2390. * to prevent HoL blocking, then start the HoL timer that
  2391. * periodically continues, then stop procs, so they can detect
  2392. * link down if they want, and do something about it.
  2393. * Timer may already be running, so use mod_timer, not add_timer.
  2394. */
  2395. void ipath_hol_down(struct ipath_devdata *dd)
  2396. {
  2397. dd->ipath_hol_state = IPATH_HOL_DOWN;
  2398. ipath_hol_signal_down(dd);
  2399. dd->ipath_hol_next = IPATH_HOL_DOWNCONT;
  2400. dd->ipath_hol_timer.expires = jiffies +
  2401. msecs_to_jiffies(ipath_hol_timeout_ms);
  2402. mod_timer(&dd->ipath_hol_timer, dd->ipath_hol_timer.expires);
  2403. }
  2404. /*
  2405. * link is up, continue any user processes, and ensure timer
  2406. * is a nop, if running. Let timer keep running, if set; it
  2407. * will nop when it sees the link is up
  2408. */
  2409. void ipath_hol_up(struct ipath_devdata *dd)
  2410. {
  2411. ipath_hol_signal_up(dd);
  2412. dd->ipath_hol_state = IPATH_HOL_UP;
  2413. }
  2414. /*
  2415. * toggle the running/not running state of user proceses
  2416. * to prevent HoL blocking on chip resources, but still allow
  2417. * user processes to do link down special case handling.
  2418. * Should only be called via the timer
  2419. */
  2420. void ipath_hol_event(unsigned long opaque)
  2421. {
  2422. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  2423. if (dd->ipath_hol_next == IPATH_HOL_DOWNSTOP
  2424. && dd->ipath_hol_state != IPATH_HOL_UP) {
  2425. dd->ipath_hol_next = IPATH_HOL_DOWNCONT;
  2426. ipath_dbg("Stopping processes\n");
  2427. ipath_hol_signal_down(dd);
  2428. } else { /* may do "extra" if also in ipath_hol_up() */
  2429. dd->ipath_hol_next = IPATH_HOL_DOWNSTOP;
  2430. ipath_dbg("Continuing processes\n");
  2431. ipath_hol_signal_up(dd);
  2432. }
  2433. if (dd->ipath_hol_state == IPATH_HOL_UP)
  2434. ipath_dbg("link's up, don't resched timer\n");
  2435. else {
  2436. dd->ipath_hol_timer.expires = jiffies +
  2437. msecs_to_jiffies(ipath_hol_timeout_ms);
  2438. mod_timer(&dd->ipath_hol_timer,
  2439. dd->ipath_hol_timer.expires);
  2440. }
  2441. }
  2442. int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv)
  2443. {
  2444. u64 val;
  2445. if (new_pol_inv > INFINIPATH_XGXS_RX_POL_MASK)
  2446. return -1;
  2447. if (dd->ipath_rx_pol_inv != new_pol_inv) {
  2448. dd->ipath_rx_pol_inv = new_pol_inv;
  2449. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
  2450. val &= ~(INFINIPATH_XGXS_RX_POL_MASK <<
  2451. INFINIPATH_XGXS_RX_POL_SHIFT);
  2452. val |= ((u64)dd->ipath_rx_pol_inv) <<
  2453. INFINIPATH_XGXS_RX_POL_SHIFT;
  2454. ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, val);
  2455. }
  2456. return 0;
  2457. }
  2458. /*
  2459. * Disable and enable the armlaunch error. Used for PIO bandwidth testing on
  2460. * the 7220, which is count-based, rather than trigger-based. Safe for the
  2461. * driver check, since it's at init. Not completely safe when used for
  2462. * user-mode checking, since some error checking can be lost, but not
  2463. * particularly risky, and only has problematic side-effects in the face of
  2464. * very buggy user code. There is no reference counting, but that's also
  2465. * fine, given the intended use.
  2466. */
  2467. void ipath_enable_armlaunch(struct ipath_devdata *dd)
  2468. {
  2469. dd->ipath_lasterror &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2470. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear,
  2471. INFINIPATH_E_SPIOARMLAUNCH);
  2472. dd->ipath_errormask |= INFINIPATH_E_SPIOARMLAUNCH;
  2473. ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
  2474. dd->ipath_errormask);
  2475. }
  2476. void ipath_disable_armlaunch(struct ipath_devdata *dd)
  2477. {
  2478. /* so don't re-enable if already set */
  2479. dd->ipath_maskederrs &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2480. dd->ipath_errormask &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2481. ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
  2482. dd->ipath_errormask);
  2483. }
  2484. module_init(infinipath_init);
  2485. module_exit(infinipath_cleanup);