ipath_driver.c 81 KB

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