ipath_driver.c 59 KB

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