ipath_driver.c 81 KB

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