ipath_driver.c 72 KB

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