ipath_driver.c 54 KB

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