ipath_driver.c 69 KB

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