ipath_driver.c 58 KB

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