ipath_driver.c 59 KB

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