ipath_driver.c 82 KB

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