ipath_driver.c 66 KB

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