ipath_driver.c 54 KB

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