ipath_driver.c 81 KB

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