ipath_driver.c 59 KB

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