ipath_driver.c 69 KB

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