ipath_driver.c 59 KB

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