ipath_driver.c 81 KB

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