ipath_driver.c 59 KB

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