ipath_driver.c 65 KB

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