ipath_driver.c 80 KB

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