ipath_driver.c 55 KB

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