ipath_driver.c 59 KB

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