ipath_driver.c 58 KB

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