ipath_driver.c 82 KB

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