ipath_driver.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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. static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
  754. u32 eflags,
  755. u32 l,
  756. u32 etail,
  757. u64 *rc)
  758. {
  759. char emsg[128];
  760. struct ipath_message_header *hdr;
  761. get_rhf_errstring(eflags, emsg, sizeof emsg);
  762. hdr = (struct ipath_message_header *)&rc[1];
  763. ipath_cdbg(PKT, "RHFerrs %x hdrqtail=%x typ=%u "
  764. "tlen=%x opcode=%x egridx=%x: %s\n",
  765. eflags, l,
  766. ipath_hdrget_rcv_type((__le32 *) rc),
  767. ipath_hdrget_length_in_bytes((__le32 *) rc),
  768. be32_to_cpu(hdr->bth[0]) >> 24,
  769. etail, emsg);
  770. /* Count local link integrity errors. */
  771. if (eflags & (INFINIPATH_RHF_H_ICRCERR | INFINIPATH_RHF_H_VCRCERR)) {
  772. u8 n = (dd->ipath_ibcctrl >>
  773. INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
  774. INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
  775. if (++dd->ipath_lli_counter > n) {
  776. dd->ipath_lli_counter = 0;
  777. dd->ipath_lli_errors++;
  778. }
  779. }
  780. }
  781. /*
  782. * ipath_kreceive - receive a packet
  783. * @dd: the infinipath device
  784. *
  785. * called from interrupt handler for errors or receive interrupt
  786. */
  787. void ipath_kreceive(struct ipath_devdata *dd)
  788. {
  789. u64 *rc;
  790. void *ebuf;
  791. const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
  792. const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
  793. u32 etail = -1, l, hdrqtail;
  794. struct ipath_message_header *hdr;
  795. u32 eflags, i, etype, tlen, pkttot = 0, updegr=0, reloop=0;
  796. static u64 totcalls; /* stats, may eventually remove */
  797. if (!dd->ipath_hdrqtailptr) {
  798. ipath_dev_err(dd,
  799. "hdrqtailptr not set, can't do receives\n");
  800. goto bail;
  801. }
  802. /* There is already a thread processing this queue. */
  803. if (test_and_set_bit(0, &dd->ipath_rcv_pending))
  804. goto bail;
  805. l = dd->ipath_port0head;
  806. hdrqtail = (u32) le64_to_cpu(*dd->ipath_hdrqtailptr);
  807. if (l == hdrqtail)
  808. goto done;
  809. reloop:
  810. for (i = 0; l != hdrqtail; i++) {
  811. u32 qp;
  812. u8 *bthbytes;
  813. rc = (u64 *) (dd->ipath_pd[0]->port_rcvhdrq + (l << 2));
  814. hdr = (struct ipath_message_header *)&rc[1];
  815. /*
  816. * could make a network order version of IPATH_KD_QP, and
  817. * do the obvious shift before masking to speed this up.
  818. */
  819. qp = ntohl(hdr->bth[1]) & 0xffffff;
  820. bthbytes = (u8 *) hdr->bth;
  821. eflags = ipath_hdrget_err_flags((__le32 *) rc);
  822. etype = ipath_hdrget_rcv_type((__le32 *) rc);
  823. /* total length */
  824. tlen = ipath_hdrget_length_in_bytes((__le32 *) rc);
  825. ebuf = NULL;
  826. if (etype != RCVHQ_RCV_TYPE_EXPECTED) {
  827. /*
  828. * it turns out that the chips uses an eager buffer
  829. * for all non-expected packets, whether it "needs"
  830. * one or not. So always get the index, but don't
  831. * set ebuf (so we try to copy data) unless the
  832. * length requires it.
  833. */
  834. etail = ipath_hdrget_index((__le32 *) rc);
  835. if (tlen > sizeof(*hdr) ||
  836. etype == RCVHQ_RCV_TYPE_NON_KD)
  837. ebuf = ipath_get_egrbuf(dd, etail, 0);
  838. }
  839. /*
  840. * both tiderr and ipathhdrerr are set for all plain IB
  841. * packets; only ipathhdrerr should be set.
  842. */
  843. if (etype != RCVHQ_RCV_TYPE_NON_KD && etype !=
  844. RCVHQ_RCV_TYPE_ERROR && ipath_hdrget_ipath_ver(
  845. hdr->iph.ver_port_tid_offset) !=
  846. IPS_PROTO_VERSION) {
  847. ipath_cdbg(PKT, "Bad InfiniPath protocol version "
  848. "%x\n", etype);
  849. }
  850. if (unlikely(eflags))
  851. ipath_rcv_hdrerr(dd, eflags, l, etail, rc);
  852. else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
  853. int ret = __ipath_verbs_rcv(dd, rc + 1,
  854. ebuf, tlen);
  855. if (ret == -ENODEV)
  856. ipath_cdbg(VERBOSE,
  857. "received IB packet, "
  858. "not SMA (QP=%x)\n", qp);
  859. if (dd->ipath_lli_counter)
  860. dd->ipath_lli_counter--;
  861. } else if (etype == RCVHQ_RCV_TYPE_EAGER) {
  862. if (qp == IPATH_KD_QP &&
  863. bthbytes[0] == ipath_layer_rcv_opcode &&
  864. ebuf)
  865. ipath_rcv_layer(dd, etail, tlen,
  866. (struct ether_header *)hdr);
  867. else
  868. ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
  869. "qp=%x), len %x; ignored\n",
  870. etype, bthbytes[0], qp, tlen);
  871. }
  872. else if (etype == RCVHQ_RCV_TYPE_EXPECTED)
  873. ipath_dbg("Bug: Expected TID, opcode %x; ignored\n",
  874. be32_to_cpu(hdr->bth[0]) & 0xff);
  875. else {
  876. /*
  877. * error packet, type of error unknown.
  878. * Probably type 3, but we don't know, so don't
  879. * even try to print the opcode, etc.
  880. */
  881. ipath_dbg("Error Pkt, but no eflags! egrbuf %x, "
  882. "len %x\nhdrq@%lx;hdrq+%x rhf: %llx; "
  883. "hdr %llx %llx %llx %llx %llx\n",
  884. etail, tlen, (unsigned long) rc, l,
  885. (unsigned long long) rc[0],
  886. (unsigned long long) rc[1],
  887. (unsigned long long) rc[2],
  888. (unsigned long long) rc[3],
  889. (unsigned long long) rc[4],
  890. (unsigned long long) rc[5]);
  891. }
  892. l += rsize;
  893. if (l >= maxcnt)
  894. l = 0;
  895. if (etype != RCVHQ_RCV_TYPE_EXPECTED)
  896. updegr = 1;
  897. /*
  898. * update head regs on last packet, and every 16 packets.
  899. * Reduce bus traffic, while still trying to prevent
  900. * rcvhdrq overflows, for when the queue is nearly full
  901. */
  902. if (l == hdrqtail || (i && !(i&0xf))) {
  903. u64 lval;
  904. if (l == hdrqtail) /* PE-800 interrupt only on last */
  905. lval = dd->ipath_rhdrhead_intr_off | l;
  906. else
  907. lval = l;
  908. (void)ipath_write_ureg(dd, ur_rcvhdrhead, lval, 0);
  909. if (updegr) {
  910. (void)ipath_write_ureg(dd, ur_rcvegrindexhead,
  911. etail, 0);
  912. updegr = 0;
  913. }
  914. }
  915. }
  916. if (!dd->ipath_rhdrhead_intr_off && !reloop) {
  917. /* HT-400 workaround; we can have a race clearing chip
  918. * interrupt with another interrupt about to be delivered,
  919. * and can clear it before it is delivered on the GPIO
  920. * workaround. By doing the extra check here for the
  921. * in-memory tail register updating while we were doing
  922. * earlier packets, we "almost" guarantee we have covered
  923. * that case.
  924. */
  925. u32 hqtail = (u32)le64_to_cpu(*dd->ipath_hdrqtailptr);
  926. if (hqtail != hdrqtail) {
  927. hdrqtail = hqtail;
  928. reloop = 1; /* loop 1 extra time at most */
  929. goto reloop;
  930. }
  931. }
  932. pkttot += i;
  933. dd->ipath_port0head = l;
  934. if (pkttot > ipath_stats.sps_maxpkts_call)
  935. ipath_stats.sps_maxpkts_call = pkttot;
  936. ipath_stats.sps_port0pkts += pkttot;
  937. ipath_stats.sps_avgpkts_call =
  938. ipath_stats.sps_port0pkts / ++totcalls;
  939. done:
  940. clear_bit(0, &dd->ipath_rcv_pending);
  941. smp_mb__after_clear_bit();
  942. bail:;
  943. }
  944. /**
  945. * ipath_update_pio_bufs - update shadow copy of the PIO availability map
  946. * @dd: the infinipath device
  947. *
  948. * called whenever our local copy indicates we have run out of send buffers
  949. * NOTE: This can be called from interrupt context by some code
  950. * and from non-interrupt context by ipath_getpiobuf().
  951. */
  952. static void ipath_update_pio_bufs(struct ipath_devdata *dd)
  953. {
  954. unsigned long flags;
  955. int i;
  956. const unsigned piobregs = (unsigned)dd->ipath_pioavregs;
  957. /* If the generation (check) bits have changed, then we update the
  958. * busy bit for the corresponding PIO buffer. This algorithm will
  959. * modify positions to the value they already have in some cases
  960. * (i.e., no change), but it's faster than changing only the bits
  961. * that have changed.
  962. *
  963. * We would like to do this atomicly, to avoid spinlocks in the
  964. * critical send path, but that's not really possible, given the
  965. * type of changes, and that this routine could be called on
  966. * multiple cpu's simultaneously, so we lock in this routine only,
  967. * to avoid conflicting updates; all we change is the shadow, and
  968. * it's a single 64 bit memory location, so by definition the update
  969. * is atomic in terms of what other cpu's can see in testing the
  970. * bits. The spin_lock overhead isn't too bad, since it only
  971. * happens when all buffers are in use, so only cpu overhead, not
  972. * latency or bandwidth is affected.
  973. */
  974. #define _IPATH_ALL_CHECKBITS 0x5555555555555555ULL
  975. if (!dd->ipath_pioavailregs_dma) {
  976. ipath_dbg("Update shadow pioavail, but regs_dma NULL!\n");
  977. return;
  978. }
  979. if (ipath_debug & __IPATH_VERBDBG) {
  980. /* only if packet debug and verbose */
  981. volatile __le64 *dma = dd->ipath_pioavailregs_dma;
  982. unsigned long *shadow = dd->ipath_pioavailshadow;
  983. ipath_cdbg(PKT, "Refill avail, dma0=%llx shad0=%lx, "
  984. "d1=%llx s1=%lx, d2=%llx s2=%lx, d3=%llx "
  985. "s3=%lx\n",
  986. (unsigned long long) le64_to_cpu(dma[0]),
  987. shadow[0],
  988. (unsigned long long) le64_to_cpu(dma[1]),
  989. shadow[1],
  990. (unsigned long long) le64_to_cpu(dma[2]),
  991. shadow[2],
  992. (unsigned long long) le64_to_cpu(dma[3]),
  993. shadow[3]);
  994. if (piobregs > 4)
  995. ipath_cdbg(
  996. PKT, "2nd group, dma4=%llx shad4=%lx, "
  997. "d5=%llx s5=%lx, d6=%llx s6=%lx, "
  998. "d7=%llx s7=%lx\n",
  999. (unsigned long long) le64_to_cpu(dma[4]),
  1000. shadow[4],
  1001. (unsigned long long) le64_to_cpu(dma[5]),
  1002. shadow[5],
  1003. (unsigned long long) le64_to_cpu(dma[6]),
  1004. shadow[6],
  1005. (unsigned long long) le64_to_cpu(dma[7]),
  1006. shadow[7]);
  1007. }
  1008. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1009. for (i = 0; i < piobregs; i++) {
  1010. u64 pchbusy, pchg, piov, pnew;
  1011. /*
  1012. * Chip Errata: bug 6641; even and odd qwords>3 are swapped
  1013. */
  1014. if (i > 3) {
  1015. if (i & 1)
  1016. piov = le64_to_cpu(
  1017. dd->ipath_pioavailregs_dma[i - 1]);
  1018. else
  1019. piov = le64_to_cpu(
  1020. dd->ipath_pioavailregs_dma[i + 1]);
  1021. } else
  1022. piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
  1023. pchg = _IPATH_ALL_CHECKBITS &
  1024. ~(dd->ipath_pioavailshadow[i] ^ piov);
  1025. pchbusy = pchg << INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT;
  1026. if (pchg && (pchbusy & dd->ipath_pioavailshadow[i])) {
  1027. pnew = dd->ipath_pioavailshadow[i] & ~pchbusy;
  1028. pnew |= piov & pchbusy;
  1029. dd->ipath_pioavailshadow[i] = pnew;
  1030. }
  1031. }
  1032. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1033. }
  1034. /**
  1035. * ipath_setrcvhdrsize - set the receive header size
  1036. * @dd: the infinipath device
  1037. * @rhdrsize: the receive header size
  1038. *
  1039. * called from user init code, and also layered driver init
  1040. */
  1041. int ipath_setrcvhdrsize(struct ipath_devdata *dd, unsigned rhdrsize)
  1042. {
  1043. int ret = 0;
  1044. if (dd->ipath_flags & IPATH_RCVHDRSZ_SET) {
  1045. if (dd->ipath_rcvhdrsize != rhdrsize) {
  1046. dev_info(&dd->pcidev->dev,
  1047. "Error: can't set protocol header "
  1048. "size %u, already %u\n",
  1049. rhdrsize, dd->ipath_rcvhdrsize);
  1050. ret = -EAGAIN;
  1051. } else
  1052. ipath_cdbg(VERBOSE, "Reuse same protocol header "
  1053. "size %u\n", dd->ipath_rcvhdrsize);
  1054. } else if (rhdrsize > (dd->ipath_rcvhdrentsize -
  1055. (sizeof(u64) / sizeof(u32)))) {
  1056. ipath_dbg("Error: can't set protocol header size %u "
  1057. "(> max %u)\n", rhdrsize,
  1058. dd->ipath_rcvhdrentsize -
  1059. (u32) (sizeof(u64) / sizeof(u32)));
  1060. ret = -EOVERFLOW;
  1061. } else {
  1062. dd->ipath_flags |= IPATH_RCVHDRSZ_SET;
  1063. dd->ipath_rcvhdrsize = rhdrsize;
  1064. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
  1065. dd->ipath_rcvhdrsize);
  1066. ipath_cdbg(VERBOSE, "Set protocol header size to %u\n",
  1067. dd->ipath_rcvhdrsize);
  1068. }
  1069. return ret;
  1070. }
  1071. /**
  1072. * ipath_getpiobuf - find an available pio buffer
  1073. * @dd: the infinipath device
  1074. * @pbufnum: the buffer number is placed here
  1075. *
  1076. * do appropriate marking as busy, etc.
  1077. * returns buffer number if one found (>=0), negative number is error.
  1078. * Used by ipath_sma_send_pkt and ipath_layer_send
  1079. */
  1080. u32 __iomem *ipath_getpiobuf(struct ipath_devdata *dd, u32 * pbufnum)
  1081. {
  1082. int i, j, starti, updated = 0;
  1083. unsigned piobcnt, iter;
  1084. unsigned long flags;
  1085. unsigned long *shadow = dd->ipath_pioavailshadow;
  1086. u32 __iomem *buf;
  1087. piobcnt = (unsigned)(dd->ipath_piobcnt2k
  1088. + dd->ipath_piobcnt4k);
  1089. starti = dd->ipath_lastport_piobuf;
  1090. iter = piobcnt - starti;
  1091. if (dd->ipath_upd_pio_shadow) {
  1092. /*
  1093. * Minor optimization. If we had no buffers on last call,
  1094. * start out by doing the update; continue and do scan even
  1095. * if no buffers were updated, to be paranoid
  1096. */
  1097. ipath_update_pio_bufs(dd);
  1098. /* we scanned here, don't do it at end of scan */
  1099. updated = 1;
  1100. i = starti;
  1101. } else
  1102. i = dd->ipath_lastpioindex;
  1103. rescan:
  1104. /*
  1105. * while test_and_set_bit() is atomic, we do that and then the
  1106. * change_bit(), and the pair is not. See if this is the cause
  1107. * of the remaining armlaunch errors.
  1108. */
  1109. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1110. for (j = 0; j < iter; j++, i++) {
  1111. if (i >= piobcnt)
  1112. i = starti;
  1113. /*
  1114. * To avoid bus lock overhead, we first find a candidate
  1115. * buffer, then do the test and set, and continue if that
  1116. * fails.
  1117. */
  1118. if (test_bit((2 * i) + 1, shadow) ||
  1119. test_and_set_bit((2 * i) + 1, shadow))
  1120. continue;
  1121. /* flip generation bit */
  1122. change_bit(2 * i, shadow);
  1123. break;
  1124. }
  1125. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1126. if (j == iter) {
  1127. volatile __le64 *dma = dd->ipath_pioavailregs_dma;
  1128. /*
  1129. * first time through; shadow exhausted, but may be real
  1130. * buffers available, so go see; if any updated, rescan
  1131. * (once)
  1132. */
  1133. if (!updated) {
  1134. ipath_update_pio_bufs(dd);
  1135. updated = 1;
  1136. i = starti;
  1137. goto rescan;
  1138. }
  1139. dd->ipath_upd_pio_shadow = 1;
  1140. /*
  1141. * not atomic, but if we lose one once in a while, that's OK
  1142. */
  1143. ipath_stats.sps_nopiobufs++;
  1144. if (!(++dd->ipath_consec_nopiobuf % 100000)) {
  1145. ipath_dbg(
  1146. "%u pio sends with no bufavail; dmacopy: "
  1147. "%llx %llx %llx %llx; shadow: "
  1148. "%lx %lx %lx %lx\n",
  1149. dd->ipath_consec_nopiobuf,
  1150. (unsigned long long) le64_to_cpu(dma[0]),
  1151. (unsigned long long) le64_to_cpu(dma[1]),
  1152. (unsigned long long) le64_to_cpu(dma[2]),
  1153. (unsigned long long) le64_to_cpu(dma[3]),
  1154. shadow[0], shadow[1], shadow[2],
  1155. shadow[3]);
  1156. /*
  1157. * 4 buffers per byte, 4 registers above, cover rest
  1158. * below
  1159. */
  1160. if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) >
  1161. (sizeof(shadow[0]) * 4 * 4))
  1162. ipath_dbg("2nd group: dmacopy: %llx %llx "
  1163. "%llx %llx; shadow: %lx %lx "
  1164. "%lx %lx\n",
  1165. (unsigned long long)
  1166. le64_to_cpu(dma[4]),
  1167. (unsigned long long)
  1168. le64_to_cpu(dma[5]),
  1169. (unsigned long long)
  1170. le64_to_cpu(dma[6]),
  1171. (unsigned long long)
  1172. le64_to_cpu(dma[7]),
  1173. shadow[4], shadow[5],
  1174. shadow[6], shadow[7]);
  1175. }
  1176. buf = NULL;
  1177. goto bail;
  1178. }
  1179. if (updated)
  1180. /*
  1181. * ran out of bufs, now some (at least this one we just
  1182. * got) are now available, so tell the layered driver.
  1183. */
  1184. __ipath_layer_intr(dd, IPATH_LAYER_INT_SEND_CONTINUE);
  1185. /*
  1186. * set next starting place. Since it's just an optimization,
  1187. * it doesn't matter who wins on this, so no locking
  1188. */
  1189. dd->ipath_lastpioindex = i + 1;
  1190. if (dd->ipath_upd_pio_shadow)
  1191. dd->ipath_upd_pio_shadow = 0;
  1192. if (dd->ipath_consec_nopiobuf)
  1193. dd->ipath_consec_nopiobuf = 0;
  1194. if (i < dd->ipath_piobcnt2k)
  1195. buf = (u32 __iomem *) (dd->ipath_pio2kbase +
  1196. i * dd->ipath_palign);
  1197. else
  1198. buf = (u32 __iomem *)
  1199. (dd->ipath_pio4kbase +
  1200. (i - dd->ipath_piobcnt2k) * dd->ipath_4kalign);
  1201. ipath_cdbg(VERBOSE, "Return piobuf%u %uk @ %p\n",
  1202. i, (i < dd->ipath_piobcnt2k) ? 2 : 4, buf);
  1203. if (pbufnum)
  1204. *pbufnum = i;
  1205. bail:
  1206. return buf;
  1207. }
  1208. /**
  1209. * ipath_create_rcvhdrq - create a receive header queue
  1210. * @dd: the infinipath device
  1211. * @pd: the port data
  1212. *
  1213. * this must be contiguous memory (from an i/o perspective), and must be
  1214. * DMA'able (which means for some systems, it will go through an IOMMU,
  1215. * or be forced into a low address range).
  1216. */
  1217. int ipath_create_rcvhdrq(struct ipath_devdata *dd,
  1218. struct ipath_portdata *pd)
  1219. {
  1220. int ret = 0;
  1221. if (!pd->port_rcvhdrq) {
  1222. dma_addr_t phys_hdrqtail;
  1223. gfp_t gfp_flags = GFP_USER | __GFP_COMP;
  1224. int amt = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1225. sizeof(u32), PAGE_SIZE);
  1226. pd->port_rcvhdrq = dma_alloc_coherent(
  1227. &dd->pcidev->dev, amt, &pd->port_rcvhdrq_phys,
  1228. gfp_flags);
  1229. if (!pd->port_rcvhdrq) {
  1230. ipath_dev_err(dd, "attempt to allocate %d bytes "
  1231. "for port %u rcvhdrq failed\n",
  1232. amt, pd->port_port);
  1233. ret = -ENOMEM;
  1234. goto bail;
  1235. }
  1236. pd->port_rcvhdrtail_kvaddr = dma_alloc_coherent(
  1237. &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail, GFP_KERNEL);
  1238. if (!pd->port_rcvhdrtail_kvaddr) {
  1239. ipath_dev_err(dd, "attempt to allocate 1 page "
  1240. "for port %u rcvhdrqtailaddr failed\n",
  1241. pd->port_port);
  1242. ret = -ENOMEM;
  1243. goto bail;
  1244. }
  1245. pd->port_rcvhdrqtailaddr_phys = phys_hdrqtail;
  1246. pd->port_rcvhdrq_size = amt;
  1247. ipath_cdbg(VERBOSE, "%d pages at %p (phys %lx) size=%lu "
  1248. "for port %u rcvhdr Q\n",
  1249. amt >> PAGE_SHIFT, pd->port_rcvhdrq,
  1250. (unsigned long) pd->port_rcvhdrq_phys,
  1251. (unsigned long) pd->port_rcvhdrq_size,
  1252. pd->port_port);
  1253. ipath_cdbg(VERBOSE, "port %d hdrtailaddr, %llx physical\n",
  1254. pd->port_port,
  1255. (unsigned long long) phys_hdrqtail);
  1256. }
  1257. else
  1258. ipath_cdbg(VERBOSE, "reuse port %d rcvhdrq @%p %llx phys; "
  1259. "hdrtailaddr@%p %llx physical\n",
  1260. pd->port_port, pd->port_rcvhdrq,
  1261. pd->port_rcvhdrq_phys, pd->port_rcvhdrtail_kvaddr,
  1262. (unsigned long long)pd->port_rcvhdrqtailaddr_phys);
  1263. /* clear for security and sanity on each use */
  1264. memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
  1265. memset((void *)pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
  1266. /*
  1267. * tell chip each time we init it, even if we are re-using previous
  1268. * memory (we zero the register at process close)
  1269. */
  1270. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  1271. pd->port_port, pd->port_rcvhdrqtailaddr_phys);
  1272. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1273. pd->port_port, pd->port_rcvhdrq_phys);
  1274. ret = 0;
  1275. bail:
  1276. return ret;
  1277. }
  1278. int ipath_waitfor_complete(struct ipath_devdata *dd, ipath_kreg reg_id,
  1279. u64 bits_to_wait_for, u64 * valp)
  1280. {
  1281. unsigned long timeout;
  1282. u64 lastval, val;
  1283. int ret;
  1284. lastval = ipath_read_kreg64(dd, reg_id);
  1285. /* wait a ridiculously long time */
  1286. timeout = jiffies + msecs_to_jiffies(5);
  1287. do {
  1288. val = ipath_read_kreg64(dd, reg_id);
  1289. /* set so they have something, even on failures. */
  1290. *valp = val;
  1291. if ((val & bits_to_wait_for) == bits_to_wait_for) {
  1292. ret = 0;
  1293. break;
  1294. }
  1295. if (val != lastval)
  1296. ipath_cdbg(VERBOSE, "Changed from %llx to %llx, "
  1297. "waiting for %llx bits\n",
  1298. (unsigned long long) lastval,
  1299. (unsigned long long) val,
  1300. (unsigned long long) bits_to_wait_for);
  1301. cond_resched();
  1302. if (time_after(jiffies, timeout)) {
  1303. ipath_dbg("Didn't get bits %llx in register 0x%x, "
  1304. "got %llx\n",
  1305. (unsigned long long) bits_to_wait_for,
  1306. reg_id, (unsigned long long) *valp);
  1307. ret = -ENODEV;
  1308. break;
  1309. }
  1310. } while (1);
  1311. return ret;
  1312. }
  1313. /**
  1314. * ipath_waitfor_mdio_cmdready - wait for last command to complete
  1315. * @dd: the infinipath device
  1316. *
  1317. * Like ipath_waitfor_complete(), but we wait for the CMDVALID bit to go
  1318. * away indicating the last command has completed. It doesn't return data
  1319. */
  1320. int ipath_waitfor_mdio_cmdready(struct ipath_devdata *dd)
  1321. {
  1322. unsigned long timeout;
  1323. u64 val;
  1324. int ret;
  1325. /* wait a ridiculously long time */
  1326. timeout = jiffies + msecs_to_jiffies(5);
  1327. do {
  1328. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_mdio);
  1329. if (!(val & IPATH_MDIO_CMDVALID)) {
  1330. ret = 0;
  1331. break;
  1332. }
  1333. cond_resched();
  1334. if (time_after(jiffies, timeout)) {
  1335. ipath_dbg("CMDVALID stuck in mdio reg? (%llx)\n",
  1336. (unsigned long long) val);
  1337. ret = -ENODEV;
  1338. break;
  1339. }
  1340. } while (1);
  1341. return ret;
  1342. }
  1343. void ipath_set_ib_lstate(struct ipath_devdata *dd, int which)
  1344. {
  1345. static const char *what[4] = {
  1346. [0] = "DOWN",
  1347. [INFINIPATH_IBCC_LINKCMD_INIT] = "INIT",
  1348. [INFINIPATH_IBCC_LINKCMD_ARMED] = "ARMED",
  1349. [INFINIPATH_IBCC_LINKCMD_ACTIVE] = "ACTIVE"
  1350. };
  1351. int linkcmd = (which >> INFINIPATH_IBCC_LINKCMD_SHIFT) &
  1352. INFINIPATH_IBCC_LINKCMD_MASK;
  1353. ipath_cdbg(SMA, "Trying to move unit %u to %s, current ltstate "
  1354. "is %s\n", dd->ipath_unit,
  1355. what[linkcmd],
  1356. ipath_ibcstatus_str[
  1357. (ipath_read_kreg64
  1358. (dd, dd->ipath_kregs->kr_ibcstatus) >>
  1359. INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
  1360. INFINIPATH_IBCS_LINKTRAININGSTATE_MASK]);
  1361. /* flush all queued sends when going to DOWN or INIT, to be sure that
  1362. * they don't block SMA and other MAD packets */
  1363. if (!linkcmd || linkcmd == INFINIPATH_IBCC_LINKCMD_INIT) {
  1364. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1365. INFINIPATH_S_ABORT);
  1366. ipath_disarm_piobufs(dd, dd->ipath_lastport_piobuf,
  1367. (unsigned)(dd->ipath_piobcnt2k +
  1368. dd->ipath_piobcnt4k) -
  1369. dd->ipath_lastport_piobuf);
  1370. }
  1371. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1372. dd->ipath_ibcctrl | which);
  1373. }
  1374. /**
  1375. * ipath_read_kreg64_port - read a device's per-port 64-bit kernel register
  1376. * @dd: the infinipath device
  1377. * @regno: the register number to read
  1378. * @port: the port containing the register
  1379. *
  1380. * Registers that vary with the chip implementation constants (port)
  1381. * use this routine.
  1382. */
  1383. u64 ipath_read_kreg64_port(const struct ipath_devdata *dd, ipath_kreg regno,
  1384. unsigned port)
  1385. {
  1386. u16 where;
  1387. if (port < dd->ipath_portcnt &&
  1388. (regno == dd->ipath_kregs->kr_rcvhdraddr ||
  1389. regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
  1390. where = regno + port;
  1391. else
  1392. where = -1;
  1393. return ipath_read_kreg64(dd, where);
  1394. }
  1395. /**
  1396. * ipath_write_kreg_port - write a device's per-port 64-bit kernel register
  1397. * @dd: the infinipath device
  1398. * @regno: the register number to write
  1399. * @port: the port containing the register
  1400. * @value: the value to write
  1401. *
  1402. * Registers that vary with the chip implementation constants (port)
  1403. * use this routine.
  1404. */
  1405. void ipath_write_kreg_port(const struct ipath_devdata *dd, ipath_kreg regno,
  1406. unsigned port, u64 value)
  1407. {
  1408. u16 where;
  1409. if (port < dd->ipath_portcnt &&
  1410. (regno == dd->ipath_kregs->kr_rcvhdraddr ||
  1411. regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
  1412. where = regno + port;
  1413. else
  1414. where = -1;
  1415. ipath_write_kreg(dd, where, value);
  1416. }
  1417. /**
  1418. * ipath_shutdown_device - shut down a device
  1419. * @dd: the infinipath device
  1420. *
  1421. * This is called to make the device quiet when we are about to
  1422. * unload the driver, and also when the device is administratively
  1423. * disabled. It does not free any data structures.
  1424. * Everything it does has to be setup again by ipath_init_chip(dd,1)
  1425. */
  1426. void ipath_shutdown_device(struct ipath_devdata *dd)
  1427. {
  1428. u64 val;
  1429. ipath_dbg("Shutting down the device\n");
  1430. dd->ipath_flags |= IPATH_LINKUNK;
  1431. dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
  1432. IPATH_LINKINIT | IPATH_LINKARMED |
  1433. IPATH_LINKACTIVE);
  1434. *dd->ipath_statusp &= ~(IPATH_STATUS_IB_CONF |
  1435. IPATH_STATUS_IB_READY);
  1436. /* mask interrupts, but not errors */
  1437. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
  1438. dd->ipath_rcvctrl = 0;
  1439. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1440. dd->ipath_rcvctrl);
  1441. /*
  1442. * gracefully stop all sends allowing any in progress to trickle out
  1443. * first.
  1444. */
  1445. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0ULL);
  1446. /* flush it */
  1447. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1448. /*
  1449. * enough for anything that's going to trickle out to have actually
  1450. * done so.
  1451. */
  1452. udelay(5);
  1453. /*
  1454. * abort any armed or launched PIO buffers that didn't go. (self
  1455. * clearing). Will cause any packet currently being transmitted to
  1456. * go out with an EBP, and may also cause a short packet error on
  1457. * the receiver.
  1458. */
  1459. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1460. INFINIPATH_S_ABORT);
  1461. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
  1462. INFINIPATH_IBCC_LINKINITCMD_SHIFT);
  1463. /*
  1464. * we are shutting down, so tell the layered driver. We don't do
  1465. * this on just a link state change, much like ethernet, a cable
  1466. * unplug, etc. doesn't change driver state
  1467. */
  1468. ipath_layer_intr(dd, IPATH_LAYER_INT_IF_DOWN);
  1469. /* disable IBC */
  1470. dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
  1471. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  1472. dd->ipath_control | INFINIPATH_C_FREEZEMODE);
  1473. /*
  1474. * clear SerdesEnable and turn the leds off; do this here because
  1475. * we are unloading, so don't count on interrupts to move along
  1476. * Turn the LEDs off explictly for the same reason.
  1477. */
  1478. dd->ipath_f_quiet_serdes(dd);
  1479. dd->ipath_f_setextled(dd, 0, 0);
  1480. if (dd->ipath_stats_timer_active) {
  1481. del_timer_sync(&dd->ipath_stats_timer);
  1482. dd->ipath_stats_timer_active = 0;
  1483. }
  1484. /*
  1485. * clear all interrupts and errors, so that the next time the driver
  1486. * is loaded or device is enabled, we know that whatever is set
  1487. * happened while we were unloaded
  1488. */
  1489. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
  1490. ~0ULL & ~INFINIPATH_HWE_MEMBISTFAILED);
  1491. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
  1492. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
  1493. }
  1494. /**
  1495. * ipath_free_pddata - free a port's allocated data
  1496. * @dd: the infinipath device
  1497. * @pd: the portdata structure
  1498. *
  1499. * free up any allocated data for a port
  1500. * This should not touch anything that would affect a simultaneous
  1501. * re-allocation of port data, because it is called after ipath_mutex
  1502. * is released (and can be called from reinit as well).
  1503. * It should never change any chip state, or global driver state.
  1504. * (The only exception to global state is freeing the port0 port0_skbs.)
  1505. */
  1506. void ipath_free_pddata(struct ipath_devdata *dd, struct ipath_portdata *pd)
  1507. {
  1508. if (!pd)
  1509. return;
  1510. if (pd->port_rcvhdrq) {
  1511. ipath_cdbg(VERBOSE, "free closed port %d rcvhdrq @ %p "
  1512. "(size=%lu)\n", pd->port_port, pd->port_rcvhdrq,
  1513. (unsigned long) pd->port_rcvhdrq_size);
  1514. dma_free_coherent(&dd->pcidev->dev, pd->port_rcvhdrq_size,
  1515. pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
  1516. pd->port_rcvhdrq = NULL;
  1517. if (pd->port_rcvhdrtail_kvaddr) {
  1518. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  1519. (void *)pd->port_rcvhdrtail_kvaddr,
  1520. pd->port_rcvhdrqtailaddr_phys);
  1521. pd->port_rcvhdrtail_kvaddr = NULL;
  1522. }
  1523. }
  1524. if (pd->port_port && pd->port_rcvegrbuf) {
  1525. unsigned e;
  1526. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  1527. void *base = pd->port_rcvegrbuf[e];
  1528. size_t size = pd->port_rcvegrbuf_size;
  1529. ipath_cdbg(VERBOSE, "egrbuf free(%p, %lu), "
  1530. "chunk %u/%u\n", base,
  1531. (unsigned long) size,
  1532. e, pd->port_rcvegrbuf_chunks);
  1533. dma_free_coherent(&dd->pcidev->dev, size,
  1534. base, pd->port_rcvegrbuf_phys[e]);
  1535. }
  1536. vfree(pd->port_rcvegrbuf);
  1537. pd->port_rcvegrbuf = NULL;
  1538. vfree(pd->port_rcvegrbuf_phys);
  1539. pd->port_rcvegrbuf_phys = NULL;
  1540. pd->port_rcvegrbuf_chunks = 0;
  1541. } else if (pd->port_port == 0 && dd->ipath_port0_skbs) {
  1542. unsigned e;
  1543. struct sk_buff **skbs = dd->ipath_port0_skbs;
  1544. dd->ipath_port0_skbs = NULL;
  1545. ipath_cdbg(VERBOSE, "free closed port %d ipath_port0_skbs "
  1546. "@ %p\n", pd->port_port, skbs);
  1547. for (e = 0; e < dd->ipath_rcvegrcnt; e++)
  1548. if (skbs[e])
  1549. dev_kfree_skb(skbs[e]);
  1550. vfree(skbs);
  1551. }
  1552. kfree(pd->port_tid_pg_list);
  1553. kfree(pd);
  1554. }
  1555. static int __init infinipath_init(void)
  1556. {
  1557. int ret;
  1558. ipath_dbg(KERN_INFO DRIVER_LOAD_MSG "%s", ipath_core_version);
  1559. /*
  1560. * These must be called before the driver is registered with
  1561. * the PCI subsystem.
  1562. */
  1563. idr_init(&unit_table);
  1564. if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
  1565. ret = -ENOMEM;
  1566. goto bail;
  1567. }
  1568. ret = pci_register_driver(&ipath_driver);
  1569. if (ret < 0) {
  1570. printk(KERN_ERR IPATH_DRV_NAME
  1571. ": Unable to register driver: error %d\n", -ret);
  1572. goto bail_unit;
  1573. }
  1574. ret = ipath_driver_create_group(&ipath_driver.driver);
  1575. if (ret < 0) {
  1576. printk(KERN_ERR IPATH_DRV_NAME ": Unable to create driver "
  1577. "sysfs entries: error %d\n", -ret);
  1578. goto bail_pci;
  1579. }
  1580. ret = ipath_init_ipathfs();
  1581. if (ret < 0) {
  1582. printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
  1583. "ipathfs: error %d\n", -ret);
  1584. goto bail_group;
  1585. }
  1586. goto bail;
  1587. bail_group:
  1588. ipath_driver_remove_group(&ipath_driver.driver);
  1589. bail_pci:
  1590. pci_unregister_driver(&ipath_driver);
  1591. bail_unit:
  1592. idr_destroy(&unit_table);
  1593. bail:
  1594. return ret;
  1595. }
  1596. static void cleanup_device(struct ipath_devdata *dd)
  1597. {
  1598. int port;
  1599. ipath_shutdown_device(dd);
  1600. if (*dd->ipath_statusp & IPATH_STATUS_CHIP_PRESENT) {
  1601. /* can't do anything more with chip; needs re-init */
  1602. *dd->ipath_statusp &= ~IPATH_STATUS_CHIP_PRESENT;
  1603. if (dd->ipath_kregbase) {
  1604. /*
  1605. * if we haven't already cleaned up before these are
  1606. * to ensure any register reads/writes "fail" until
  1607. * re-init
  1608. */
  1609. dd->ipath_kregbase = NULL;
  1610. dd->ipath_uregbase = 0;
  1611. dd->ipath_sregbase = 0;
  1612. dd->ipath_cregbase = 0;
  1613. dd->ipath_kregsize = 0;
  1614. }
  1615. ipath_disable_wc(dd);
  1616. }
  1617. if (dd->ipath_pioavailregs_dma) {
  1618. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  1619. (void *) dd->ipath_pioavailregs_dma,
  1620. dd->ipath_pioavailregs_phys);
  1621. dd->ipath_pioavailregs_dma = NULL;
  1622. }
  1623. if (dd->ipath_dummy_hdrq) {
  1624. dma_free_coherent(&dd->pcidev->dev,
  1625. dd->ipath_pd[0]->port_rcvhdrq_size,
  1626. dd->ipath_dummy_hdrq, dd->ipath_dummy_hdrq_phys);
  1627. dd->ipath_dummy_hdrq = NULL;
  1628. }
  1629. if (dd->ipath_pageshadow) {
  1630. struct page **tmpp = dd->ipath_pageshadow;
  1631. int i, cnt = 0;
  1632. ipath_cdbg(VERBOSE, "Unlocking any expTID pages still "
  1633. "locked\n");
  1634. for (port = 0; port < dd->ipath_cfgports; port++) {
  1635. int port_tidbase = port * dd->ipath_rcvtidcnt;
  1636. int maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1637. for (i = port_tidbase; i < maxtid; i++) {
  1638. if (!tmpp[i])
  1639. continue;
  1640. ipath_release_user_pages(&tmpp[i], 1);
  1641. tmpp[i] = NULL;
  1642. cnt++;
  1643. }
  1644. }
  1645. if (cnt) {
  1646. ipath_stats.sps_pageunlocks += cnt;
  1647. ipath_cdbg(VERBOSE, "There were still %u expTID "
  1648. "entries locked\n", cnt);
  1649. }
  1650. if (ipath_stats.sps_pagelocks ||
  1651. ipath_stats.sps_pageunlocks)
  1652. ipath_cdbg(VERBOSE, "%llu pages locked, %llu "
  1653. "unlocked via ipath_m{un}lock\n",
  1654. (unsigned long long)
  1655. ipath_stats.sps_pagelocks,
  1656. (unsigned long long)
  1657. ipath_stats.sps_pageunlocks);
  1658. ipath_cdbg(VERBOSE, "Free shadow page tid array at %p\n",
  1659. dd->ipath_pageshadow);
  1660. vfree(dd->ipath_pageshadow);
  1661. dd->ipath_pageshadow = NULL;
  1662. }
  1663. /*
  1664. * free any resources still in use (usually just kernel ports)
  1665. * at unload; we do for portcnt, not cfgports, because cfgports
  1666. * could have changed while we were loaded.
  1667. */
  1668. for (port = 0; port < dd->ipath_portcnt; port++) {
  1669. struct ipath_portdata *pd = dd->ipath_pd[port];
  1670. dd->ipath_pd[port] = NULL;
  1671. ipath_free_pddata(dd, pd);
  1672. }
  1673. kfree(dd->ipath_pd);
  1674. /*
  1675. * debuggability, in case some cleanup path tries to use it
  1676. * after this
  1677. */
  1678. dd->ipath_pd = NULL;
  1679. }
  1680. static void __exit infinipath_cleanup(void)
  1681. {
  1682. struct ipath_devdata *dd, *tmp;
  1683. unsigned long flags;
  1684. ipath_exit_ipathfs();
  1685. ipath_driver_remove_group(&ipath_driver.driver);
  1686. spin_lock_irqsave(&ipath_devs_lock, flags);
  1687. /*
  1688. * turn off rcv, send, and interrupts for all ports, all drivers
  1689. * should also hard reset the chip here?
  1690. * free up port 0 (kernel) rcvhdr, egr bufs, and eventually tid bufs
  1691. * for all versions of the driver, if they were allocated
  1692. */
  1693. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  1694. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  1695. if (dd->ipath_kregbase)
  1696. cleanup_device(dd);
  1697. if (dd->pcidev) {
  1698. if (dd->pcidev->irq) {
  1699. ipath_cdbg(VERBOSE,
  1700. "unit %u free_irq of irq %x\n",
  1701. dd->ipath_unit, dd->pcidev->irq);
  1702. free_irq(dd->pcidev->irq, dd);
  1703. } else
  1704. ipath_dbg("irq is 0, not doing free_irq "
  1705. "for unit %u\n", dd->ipath_unit);
  1706. /*
  1707. * we check for NULL here, because it's outside
  1708. * the kregbase check, and we need to call it
  1709. * after the free_irq. Thus it's possible that
  1710. * the function pointers were never initialized.
  1711. */
  1712. if (dd->ipath_f_cleanup)
  1713. /* clean up chip-specific stuff */
  1714. dd->ipath_f_cleanup(dd);
  1715. dd->pcidev = NULL;
  1716. }
  1717. spin_lock_irqsave(&ipath_devs_lock, flags);
  1718. }
  1719. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  1720. ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
  1721. pci_unregister_driver(&ipath_driver);
  1722. idr_destroy(&unit_table);
  1723. }
  1724. /**
  1725. * ipath_reset_device - reset the chip if possible
  1726. * @unit: the device to reset
  1727. *
  1728. * Whether or not reset is successful, we attempt to re-initialize the chip
  1729. * (that is, much like a driver unload/reload). We clear the INITTED flag
  1730. * so that the various entry points will fail until we reinitialize. For
  1731. * now, we only allow this if no user ports are open that use chip resources
  1732. */
  1733. int ipath_reset_device(int unit)
  1734. {
  1735. int ret, i;
  1736. struct ipath_devdata *dd = ipath_lookup(unit);
  1737. if (!dd) {
  1738. ret = -ENODEV;
  1739. goto bail;
  1740. }
  1741. dev_info(&dd->pcidev->dev, "Reset on unit %u requested\n", unit);
  1742. if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT)) {
  1743. dev_info(&dd->pcidev->dev, "Invalid unit number %u or "
  1744. "not initialized or not present\n", unit);
  1745. ret = -ENXIO;
  1746. goto bail;
  1747. }
  1748. if (dd->ipath_pd)
  1749. for (i = 1; i < dd->ipath_cfgports; i++) {
  1750. if (dd->ipath_pd[i] && dd->ipath_pd[i]->port_cnt) {
  1751. ipath_dbg("unit %u port %d is in use "
  1752. "(PID %u cmd %s), can't reset\n",
  1753. unit, i,
  1754. dd->ipath_pd[i]->port_pid,
  1755. dd->ipath_pd[i]->port_comm);
  1756. ret = -EBUSY;
  1757. goto bail;
  1758. }
  1759. }
  1760. dd->ipath_flags &= ~IPATH_INITTED;
  1761. ret = dd->ipath_f_reset(dd);
  1762. if (ret != 1)
  1763. ipath_dbg("reset was not successful\n");
  1764. ipath_dbg("Trying to reinitialize unit %u after reset attempt\n",
  1765. unit);
  1766. ret = ipath_init_chip(dd, 1);
  1767. if (ret)
  1768. ipath_dev_err(dd, "Reinitialize unit %u after "
  1769. "reset failed with %d\n", unit, ret);
  1770. else
  1771. dev_info(&dd->pcidev->dev, "Reinitialized unit %u after "
  1772. "resetting\n", unit);
  1773. bail:
  1774. return ret;
  1775. }
  1776. module_init(infinipath_init);
  1777. module_exit(infinipath_cleanup);