ipath_driver.c 54 KB

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