ipath_driver.c 59 KB

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