ipath_driver.c 71 KB

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