ipath_driver.c 54 KB

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