ipath_driver.c 56 KB

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