ipath_driver.c 58 KB

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