ipath_driver.c 65 KB

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