ipath_driver.c 81 KB

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