ipath_driver.c 66 KB

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