ipath_driver.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 QLogic Corporation. 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/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/vmalloc.h>
  40. #include "ipath_kernel.h"
  41. #include "ipath_verbs.h"
  42. static void ipath_update_pio_bufs(struct ipath_devdata *);
  43. const char *ipath_get_unit_name(int unit)
  44. {
  45. static char iname[16];
  46. snprintf(iname, sizeof iname, "infinipath%u", unit);
  47. return iname;
  48. }
  49. #define DRIVER_LOAD_MSG "QLogic " IPATH_DRV_NAME " loaded: "
  50. #define PFX IPATH_DRV_NAME ": "
  51. /*
  52. * The size has to be longer than this string, so we can append
  53. * board/chip information to it in the init code.
  54. */
  55. const char ib_ipath_version[] = IPATH_IDSTR "\n";
  56. static struct idr unit_table;
  57. DEFINE_SPINLOCK(ipath_devs_lock);
  58. LIST_HEAD(ipath_dev_list);
  59. wait_queue_head_t ipath_state_wait;
  60. unsigned ipath_debug = __IPATH_INFO;
  61. module_param_named(debug, ipath_debug, uint, S_IWUSR | S_IRUGO);
  62. MODULE_PARM_DESC(debug, "mask for debug prints");
  63. EXPORT_SYMBOL_GPL(ipath_debug);
  64. unsigned ipath_mtu4096 = 1; /* max 4KB IB mtu by default, if supported */
  65. module_param_named(mtu4096, ipath_mtu4096, uint, S_IRUGO);
  66. MODULE_PARM_DESC(mtu4096, "enable MTU of 4096 bytes, if supported");
  67. static unsigned ipath_hol_timeout_ms = 13000;
  68. module_param_named(hol_timeout_ms, ipath_hol_timeout_ms, uint, S_IRUGO);
  69. MODULE_PARM_DESC(hol_timeout_ms,
  70. "duration of user app suspension after link failure");
  71. unsigned ipath_linkrecovery = 1;
  72. module_param_named(linkrecovery, ipath_linkrecovery, uint, S_IWUSR | S_IRUGO);
  73. MODULE_PARM_DESC(linkrecovery, "enable workaround for link recovery issue");
  74. MODULE_LICENSE("GPL");
  75. MODULE_AUTHOR("QLogic <support@qlogic.com>");
  76. MODULE_DESCRIPTION("QLogic InfiniPath driver");
  77. /*
  78. * Table to translate the LINKTRAININGSTATE portion of
  79. * IBCStatus to a human-readable form.
  80. */
  81. const char *ipath_ibcstatus_str[] = {
  82. "Disabled",
  83. "LinkUp",
  84. "PollActive",
  85. "PollQuiet",
  86. "SleepDelay",
  87. "SleepQuiet",
  88. "LState6", /* unused */
  89. "LState7", /* unused */
  90. "CfgDebounce",
  91. "CfgRcvfCfg",
  92. "CfgWaitRmt",
  93. "CfgIdle",
  94. "RecovRetrain",
  95. "CfgTxRevLane", /* unused before IBA7220 */
  96. "RecovWaitRmt",
  97. "RecovIdle",
  98. /* below were added for IBA7220 */
  99. "CfgEnhanced",
  100. "CfgTest",
  101. "CfgWaitRmtTest",
  102. "CfgWaitCfgEnhanced",
  103. "SendTS_T",
  104. "SendTstIdles",
  105. "RcvTS_T",
  106. "SendTst_TS1s",
  107. "LTState18", "LTState19", "LTState1A", "LTState1B",
  108. "LTState1C", "LTState1D", "LTState1E", "LTState1F"
  109. };
  110. static void __devexit ipath_remove_one(struct pci_dev *);
  111. static int __devinit ipath_init_one(struct pci_dev *,
  112. const struct pci_device_id *);
  113. /* Only needed for registration, nothing else needs this info */
  114. #define PCI_VENDOR_ID_PATHSCALE 0x1fc1
  115. #define PCI_VENDOR_ID_QLOGIC 0x1077
  116. #define PCI_DEVICE_ID_INFINIPATH_HT 0xd
  117. #define PCI_DEVICE_ID_INFINIPATH_PE800 0x10
  118. #define PCI_DEVICE_ID_INFINIPATH_7220 0x7220
  119. /* Number of seconds before our card status check... */
  120. #define STATUS_TIMEOUT 60
  121. static const struct pci_device_id ipath_pci_tbl[] = {
  122. { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_HT) },
  123. { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_PE800) },
  124. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_INFINIPATH_7220) },
  125. { 0, }
  126. };
  127. MODULE_DEVICE_TABLE(pci, ipath_pci_tbl);
  128. static struct pci_driver ipath_driver = {
  129. .name = IPATH_DRV_NAME,
  130. .probe = ipath_init_one,
  131. .remove = __devexit_p(ipath_remove_one),
  132. .id_table = ipath_pci_tbl,
  133. .driver = {
  134. .groups = ipath_driver_attr_groups,
  135. },
  136. };
  137. static inline void read_bars(struct ipath_devdata *dd, struct pci_dev *dev,
  138. u32 *bar0, u32 *bar1)
  139. {
  140. int ret;
  141. ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
  142. if (ret)
  143. ipath_dev_err(dd, "failed to read bar0 before enable: "
  144. "error %d\n", -ret);
  145. ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, bar1);
  146. if (ret)
  147. ipath_dev_err(dd, "failed to read bar1 before enable: "
  148. "error %d\n", -ret);
  149. ipath_dbg("Read bar0 %x bar1 %x\n", *bar0, *bar1);
  150. }
  151. static void ipath_free_devdata(struct pci_dev *pdev,
  152. struct ipath_devdata *dd)
  153. {
  154. unsigned long flags;
  155. pci_set_drvdata(pdev, NULL);
  156. if (dd->ipath_unit != -1) {
  157. spin_lock_irqsave(&ipath_devs_lock, flags);
  158. idr_remove(&unit_table, dd->ipath_unit);
  159. list_del(&dd->ipath_list);
  160. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  161. }
  162. vfree(dd);
  163. }
  164. static struct ipath_devdata *ipath_alloc_devdata(struct pci_dev *pdev)
  165. {
  166. unsigned long flags;
  167. struct ipath_devdata *dd;
  168. int ret;
  169. if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
  170. dd = ERR_PTR(-ENOMEM);
  171. goto bail;
  172. }
  173. dd = vmalloc(sizeof(*dd));
  174. if (!dd) {
  175. dd = ERR_PTR(-ENOMEM);
  176. goto bail;
  177. }
  178. memset(dd, 0, sizeof(*dd));
  179. dd->ipath_unit = -1;
  180. spin_lock_irqsave(&ipath_devs_lock, flags);
  181. ret = idr_get_new(&unit_table, dd, &dd->ipath_unit);
  182. if (ret < 0) {
  183. printk(KERN_ERR IPATH_DRV_NAME
  184. ": Could not allocate unit ID: error %d\n", -ret);
  185. ipath_free_devdata(pdev, dd);
  186. dd = ERR_PTR(ret);
  187. goto bail_unlock;
  188. }
  189. dd->pcidev = pdev;
  190. pci_set_drvdata(pdev, dd);
  191. list_add(&dd->ipath_list, &ipath_dev_list);
  192. bail_unlock:
  193. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  194. bail:
  195. return dd;
  196. }
  197. static inline struct ipath_devdata *__ipath_lookup(int unit)
  198. {
  199. return idr_find(&unit_table, unit);
  200. }
  201. struct ipath_devdata *ipath_lookup(int unit)
  202. {
  203. struct ipath_devdata *dd;
  204. unsigned long flags;
  205. spin_lock_irqsave(&ipath_devs_lock, flags);
  206. dd = __ipath_lookup(unit);
  207. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  208. return dd;
  209. }
  210. int ipath_count_units(int *npresentp, int *nupp, int *maxportsp)
  211. {
  212. int nunits, npresent, nup;
  213. struct ipath_devdata *dd;
  214. unsigned long flags;
  215. int maxports;
  216. nunits = npresent = nup = maxports = 0;
  217. spin_lock_irqsave(&ipath_devs_lock, flags);
  218. list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
  219. nunits++;
  220. if ((dd->ipath_flags & IPATH_PRESENT) && dd->ipath_kregbase)
  221. npresent++;
  222. if (dd->ipath_lid &&
  223. !(dd->ipath_flags & (IPATH_DISABLED | IPATH_LINKDOWN
  224. | IPATH_LINKUNK)))
  225. nup++;
  226. if (dd->ipath_cfgports > maxports)
  227. maxports = dd->ipath_cfgports;
  228. }
  229. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  230. if (npresentp)
  231. *npresentp = npresent;
  232. if (nupp)
  233. *nupp = nup;
  234. if (maxportsp)
  235. *maxportsp = maxports;
  236. return nunits;
  237. }
  238. /*
  239. * These next two routines are placeholders in case we don't have per-arch
  240. * code for controlling write combining. If explicit control of write
  241. * combining is not available, performance will probably be awful.
  242. */
  243. int __attribute__((weak)) ipath_enable_wc(struct ipath_devdata *dd)
  244. {
  245. return -EOPNOTSUPP;
  246. }
  247. void __attribute__((weak)) ipath_disable_wc(struct ipath_devdata *dd)
  248. {
  249. }
  250. /*
  251. * Perform a PIO buffer bandwidth write test, to verify proper system
  252. * configuration. Even when all the setup calls work, occasionally
  253. * BIOS or other issues can prevent write combining from working, or
  254. * can cause other bandwidth problems to the chip.
  255. *
  256. * This test simply writes the same buffer over and over again, and
  257. * measures close to the peak bandwidth to the chip (not testing
  258. * data bandwidth to the wire). On chips that use an address-based
  259. * trigger to send packets to the wire, this is easy. On chips that
  260. * use a count to trigger, we want to make sure that the packet doesn't
  261. * go out on the wire, or trigger flow control checks.
  262. */
  263. static void ipath_verify_pioperf(struct ipath_devdata *dd)
  264. {
  265. u32 pbnum, cnt, lcnt;
  266. u32 __iomem *piobuf;
  267. u32 *addr;
  268. u64 msecs, emsecs;
  269. piobuf = ipath_getpiobuf(dd, 0, &pbnum);
  270. if (!piobuf) {
  271. dev_info(&dd->pcidev->dev,
  272. "No PIObufs for checking perf, skipping\n");
  273. return;
  274. }
  275. /*
  276. * Enough to give us a reasonable test, less than piobuf size, and
  277. * likely multiple of store buffer length.
  278. */
  279. cnt = 1024;
  280. addr = vmalloc(cnt);
  281. if (!addr) {
  282. dev_info(&dd->pcidev->dev,
  283. "Couldn't get memory for checking PIO perf,"
  284. " skipping\n");
  285. goto done;
  286. }
  287. preempt_disable(); /* we want reasonably accurate elapsed time */
  288. msecs = 1 + jiffies_to_msecs(jiffies);
  289. for (lcnt = 0; lcnt < 10000U; lcnt++) {
  290. /* wait until we cross msec boundary */
  291. if (jiffies_to_msecs(jiffies) >= msecs)
  292. break;
  293. udelay(1);
  294. }
  295. ipath_disable_armlaunch(dd);
  296. /*
  297. * length 0, no dwords actually sent, and mark as VL15
  298. * on chips where that may matter (due to IB flowcontrol)
  299. */
  300. if ((dd->ipath_flags & IPATH_HAS_PBC_CNT))
  301. writeq(1UL << 63, piobuf);
  302. else
  303. writeq(0, piobuf);
  304. ipath_flush_wc();
  305. /*
  306. * this is only roughly accurate, since even with preempt we
  307. * still take interrupts that could take a while. Running for
  308. * >= 5 msec seems to get us "close enough" to accurate values
  309. */
  310. msecs = jiffies_to_msecs(jiffies);
  311. for (emsecs = lcnt = 0; emsecs <= 5UL; lcnt++) {
  312. __iowrite32_copy(piobuf + 64, addr, cnt >> 2);
  313. emsecs = jiffies_to_msecs(jiffies) - msecs;
  314. }
  315. /* 1 GiB/sec, slightly over IB SDR line rate */
  316. if (lcnt < (emsecs * 1024U))
  317. ipath_dev_err(dd,
  318. "Performance problem: bandwidth to PIO buffers is "
  319. "only %u MiB/sec\n",
  320. lcnt / (u32) emsecs);
  321. else
  322. ipath_dbg("PIO buffer bandwidth %u MiB/sec is OK\n",
  323. lcnt / (u32) emsecs);
  324. preempt_enable();
  325. vfree(addr);
  326. done:
  327. /* disarm piobuf, so it's available again */
  328. ipath_disarm_piobufs(dd, pbnum, 1);
  329. ipath_enable_armlaunch(dd);
  330. }
  331. static int __devinit ipath_init_one(struct pci_dev *pdev,
  332. const struct pci_device_id *ent)
  333. {
  334. int ret, len, j;
  335. struct ipath_devdata *dd;
  336. unsigned long long addr;
  337. u32 bar0 = 0, bar1 = 0;
  338. u8 rev;
  339. dd = ipath_alloc_devdata(pdev);
  340. if (IS_ERR(dd)) {
  341. ret = PTR_ERR(dd);
  342. printk(KERN_ERR IPATH_DRV_NAME
  343. ": Could not allocate devdata: error %d\n", -ret);
  344. goto bail;
  345. }
  346. ipath_cdbg(VERBOSE, "initializing unit #%u\n", dd->ipath_unit);
  347. ret = pci_enable_device(pdev);
  348. if (ret) {
  349. /* This can happen iff:
  350. *
  351. * We did a chip reset, and then failed to reprogram the
  352. * BAR, or the chip reset due to an internal error. We then
  353. * unloaded the driver and reloaded it.
  354. *
  355. * Both reset cases set the BAR back to initial state. For
  356. * the latter case, the AER sticky error bit at offset 0x718
  357. * should be set, but the Linux kernel doesn't yet know
  358. * about that, it appears. If the original BAR was retained
  359. * in the kernel data structures, this may be OK.
  360. */
  361. ipath_dev_err(dd, "enable unit %d failed: error %d\n",
  362. dd->ipath_unit, -ret);
  363. goto bail_devdata;
  364. }
  365. addr = pci_resource_start(pdev, 0);
  366. len = pci_resource_len(pdev, 0);
  367. ipath_cdbg(VERBOSE, "regbase (0) %llx len %d irq %d, vend %x/%x "
  368. "driver_data %lx\n", addr, len, pdev->irq, ent->vendor,
  369. ent->device, ent->driver_data);
  370. read_bars(dd, pdev, &bar0, &bar1);
  371. if (!bar1 && !(bar0 & ~0xf)) {
  372. if (addr) {
  373. dev_info(&pdev->dev, "BAR is 0 (probable RESET), "
  374. "rewriting as %llx\n", addr);
  375. ret = pci_write_config_dword(
  376. pdev, PCI_BASE_ADDRESS_0, addr);
  377. if (ret) {
  378. ipath_dev_err(dd, "rewrite of BAR0 "
  379. "failed: err %d\n", -ret);
  380. goto bail_disable;
  381. }
  382. ret = pci_write_config_dword(
  383. pdev, PCI_BASE_ADDRESS_1, addr >> 32);
  384. if (ret) {
  385. ipath_dev_err(dd, "rewrite of BAR1 "
  386. "failed: err %d\n", -ret);
  387. goto bail_disable;
  388. }
  389. } else {
  390. ipath_dev_err(dd, "BAR is 0 (probable RESET), "
  391. "not usable until reboot\n");
  392. ret = -ENODEV;
  393. goto bail_disable;
  394. }
  395. }
  396. ret = pci_request_regions(pdev, IPATH_DRV_NAME);
  397. if (ret) {
  398. dev_info(&pdev->dev, "pci_request_regions unit %u fails: "
  399. "err %d\n", dd->ipath_unit, -ret);
  400. goto bail_disable;
  401. }
  402. ret = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  403. if (ret) {
  404. /*
  405. * if the 64 bit setup fails, try 32 bit. Some systems
  406. * do not setup 64 bit maps on systems with 2GB or less
  407. * memory installed.
  408. */
  409. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  410. if (ret) {
  411. dev_info(&pdev->dev,
  412. "Unable to set DMA mask for unit %u: %d\n",
  413. dd->ipath_unit, ret);
  414. goto bail_regions;
  415. }
  416. else {
  417. ipath_dbg("No 64bit DMA mask, used 32 bit mask\n");
  418. ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  419. if (ret)
  420. dev_info(&pdev->dev,
  421. "Unable to set DMA consistent mask "
  422. "for unit %u: %d\n",
  423. dd->ipath_unit, ret);
  424. }
  425. }
  426. else {
  427. ret = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  428. if (ret)
  429. dev_info(&pdev->dev,
  430. "Unable to set DMA consistent mask "
  431. "for unit %u: %d\n",
  432. dd->ipath_unit, ret);
  433. }
  434. pci_set_master(pdev);
  435. /*
  436. * Save BARs to rewrite after device reset. Save all 64 bits of
  437. * BAR, just in case.
  438. */
  439. dd->ipath_pcibar0 = addr;
  440. dd->ipath_pcibar1 = addr >> 32;
  441. dd->ipath_deviceid = ent->device; /* save for later use */
  442. dd->ipath_vendorid = ent->vendor;
  443. /* setup the chip-specific functions, as early as possible. */
  444. switch (ent->device) {
  445. case PCI_DEVICE_ID_INFINIPATH_HT:
  446. #ifdef CONFIG_HT_IRQ
  447. ipath_init_iba6110_funcs(dd);
  448. break;
  449. #else
  450. ipath_dev_err(dd, "QLogic HT device 0x%x cannot work if "
  451. "CONFIG_HT_IRQ is not enabled\n", ent->device);
  452. return -ENODEV;
  453. #endif
  454. case PCI_DEVICE_ID_INFINIPATH_PE800:
  455. #ifdef CONFIG_PCI_MSI
  456. ipath_init_iba6120_funcs(dd);
  457. break;
  458. #else
  459. ipath_dev_err(dd, "QLogic PCIE device 0x%x cannot work if "
  460. "CONFIG_PCI_MSI is not enabled\n", ent->device);
  461. return -ENODEV;
  462. #endif
  463. case PCI_DEVICE_ID_INFINIPATH_7220:
  464. #ifndef CONFIG_PCI_MSI
  465. ipath_dbg("CONFIG_PCI_MSI is not enabled, "
  466. "using INTx for unit %u\n", dd->ipath_unit);
  467. #endif
  468. ipath_init_iba7220_funcs(dd);
  469. break;
  470. default:
  471. ipath_dev_err(dd, "Found unknown QLogic deviceid 0x%x, "
  472. "failing\n", ent->device);
  473. return -ENODEV;
  474. }
  475. for (j = 0; j < 6; j++) {
  476. if (!pdev->resource[j].start)
  477. continue;
  478. ipath_cdbg(VERBOSE, "BAR %d start %llx, end %llx, len %llx\n",
  479. j, (unsigned long long)pdev->resource[j].start,
  480. (unsigned long long)pdev->resource[j].end,
  481. (unsigned long long)pci_resource_len(pdev, j));
  482. }
  483. if (!addr) {
  484. ipath_dev_err(dd, "No valid address in BAR 0!\n");
  485. ret = -ENODEV;
  486. goto bail_regions;
  487. }
  488. ret = pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
  489. if (ret) {
  490. ipath_dev_err(dd, "Failed to read PCI revision ID unit "
  491. "%u: err %d\n", dd->ipath_unit, -ret);
  492. goto bail_regions; /* shouldn't ever happen */
  493. }
  494. dd->ipath_pcirev = rev;
  495. #if defined(__powerpc__)
  496. /* There isn't a generic way to specify writethrough mappings */
  497. dd->ipath_kregbase = __ioremap(addr, len,
  498. (_PAGE_NO_CACHE|_PAGE_WRITETHRU));
  499. #else
  500. dd->ipath_kregbase = ioremap_nocache(addr, len);
  501. #endif
  502. if (!dd->ipath_kregbase) {
  503. ipath_dbg("Unable to map io addr %llx to kvirt, failing\n",
  504. addr);
  505. ret = -ENOMEM;
  506. goto bail_iounmap;
  507. }
  508. dd->ipath_kregend = (u64 __iomem *)
  509. ((void __iomem *)dd->ipath_kregbase + len);
  510. dd->ipath_physaddr = addr; /* used for io_remap, etc. */
  511. /* for user mmap */
  512. ipath_cdbg(VERBOSE, "mapped io addr %llx to kregbase %p\n",
  513. addr, dd->ipath_kregbase);
  514. if (dd->ipath_f_bus(dd, pdev))
  515. ipath_dev_err(dd, "Failed to setup config space; "
  516. "continuing anyway\n");
  517. /*
  518. * set up our interrupt handler; IRQF_SHARED probably not needed,
  519. * since MSI interrupts shouldn't be shared but won't hurt for now.
  520. * check 0 irq after we return from chip-specific bus setup, since
  521. * that can affect this due to setup
  522. */
  523. if (!dd->ipath_irq)
  524. ipath_dev_err(dd, "irq is 0, BIOS error? Interrupts won't "
  525. "work\n");
  526. else {
  527. ret = request_irq(dd->ipath_irq, ipath_intr, IRQF_SHARED,
  528. IPATH_DRV_NAME, dd);
  529. if (ret) {
  530. ipath_dev_err(dd, "Couldn't setup irq handler, "
  531. "irq=%d: %d\n", dd->ipath_irq, ret);
  532. goto bail_iounmap;
  533. }
  534. }
  535. ret = ipath_init_chip(dd, 0); /* do the chip-specific init */
  536. if (ret)
  537. goto bail_irqsetup;
  538. ret = ipath_enable_wc(dd);
  539. if (ret) {
  540. ipath_dev_err(dd, "Write combining not enabled "
  541. "(err %d): performance may be poor\n",
  542. -ret);
  543. ret = 0;
  544. }
  545. ipath_verify_pioperf(dd);
  546. ipath_device_create_group(&pdev->dev, dd);
  547. ipathfs_add_device(dd);
  548. ipath_user_add(dd);
  549. ipath_diag_add(dd);
  550. ipath_register_ib_device(dd);
  551. goto bail;
  552. bail_irqsetup:
  553. if (pdev->irq)
  554. free_irq(pdev->irq, dd);
  555. bail_iounmap:
  556. iounmap((volatile void __iomem *) dd->ipath_kregbase);
  557. bail_regions:
  558. pci_release_regions(pdev);
  559. bail_disable:
  560. pci_disable_device(pdev);
  561. bail_devdata:
  562. ipath_free_devdata(pdev, dd);
  563. bail:
  564. return ret;
  565. }
  566. static void __devexit cleanup_device(struct ipath_devdata *dd)
  567. {
  568. int port;
  569. if (*dd->ipath_statusp & IPATH_STATUS_CHIP_PRESENT) {
  570. /* can't do anything more with chip; needs re-init */
  571. *dd->ipath_statusp &= ~IPATH_STATUS_CHIP_PRESENT;
  572. if (dd->ipath_kregbase) {
  573. /*
  574. * if we haven't already cleaned up before these are
  575. * to ensure any register reads/writes "fail" until
  576. * re-init
  577. */
  578. dd->ipath_kregbase = NULL;
  579. dd->ipath_uregbase = 0;
  580. dd->ipath_sregbase = 0;
  581. dd->ipath_cregbase = 0;
  582. dd->ipath_kregsize = 0;
  583. }
  584. ipath_disable_wc(dd);
  585. }
  586. if (dd->ipath_spectriggerhit)
  587. dev_info(&dd->pcidev->dev, "%lu special trigger hits\n",
  588. dd->ipath_spectriggerhit);
  589. if (dd->ipath_pioavailregs_dma) {
  590. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  591. (void *) dd->ipath_pioavailregs_dma,
  592. dd->ipath_pioavailregs_phys);
  593. dd->ipath_pioavailregs_dma = NULL;
  594. }
  595. if (dd->ipath_dummy_hdrq) {
  596. dma_free_coherent(&dd->pcidev->dev,
  597. dd->ipath_pd[0]->port_rcvhdrq_size,
  598. dd->ipath_dummy_hdrq, dd->ipath_dummy_hdrq_phys);
  599. dd->ipath_dummy_hdrq = NULL;
  600. }
  601. if (dd->ipath_pageshadow) {
  602. struct page **tmpp = dd->ipath_pageshadow;
  603. dma_addr_t *tmpd = dd->ipath_physshadow;
  604. int i, cnt = 0;
  605. ipath_cdbg(VERBOSE, "Unlocking any expTID pages still "
  606. "locked\n");
  607. for (port = 0; port < dd->ipath_cfgports; port++) {
  608. int port_tidbase = port * dd->ipath_rcvtidcnt;
  609. int maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  610. for (i = port_tidbase; i < maxtid; i++) {
  611. if (!tmpp[i])
  612. continue;
  613. pci_unmap_page(dd->pcidev, tmpd[i],
  614. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  615. ipath_release_user_pages(&tmpp[i], 1);
  616. tmpp[i] = NULL;
  617. cnt++;
  618. }
  619. }
  620. if (cnt) {
  621. ipath_stats.sps_pageunlocks += cnt;
  622. ipath_cdbg(VERBOSE, "There were still %u expTID "
  623. "entries locked\n", cnt);
  624. }
  625. if (ipath_stats.sps_pagelocks ||
  626. ipath_stats.sps_pageunlocks)
  627. ipath_cdbg(VERBOSE, "%llu pages locked, %llu "
  628. "unlocked via ipath_m{un}lock\n",
  629. (unsigned long long)
  630. ipath_stats.sps_pagelocks,
  631. (unsigned long long)
  632. ipath_stats.sps_pageunlocks);
  633. ipath_cdbg(VERBOSE, "Free shadow page tid array at %p\n",
  634. dd->ipath_pageshadow);
  635. tmpp = dd->ipath_pageshadow;
  636. dd->ipath_pageshadow = NULL;
  637. vfree(tmpp);
  638. dd->ipath_egrtidbase = NULL;
  639. }
  640. /*
  641. * free any resources still in use (usually just kernel ports)
  642. * at unload; we do for portcnt, not cfgports, because cfgports
  643. * could have changed while we were loaded.
  644. */
  645. for (port = 0; port < dd->ipath_portcnt; port++) {
  646. struct ipath_portdata *pd = dd->ipath_pd[port];
  647. dd->ipath_pd[port] = NULL;
  648. ipath_free_pddata(dd, pd);
  649. }
  650. kfree(dd->ipath_pd);
  651. /*
  652. * debuggability, in case some cleanup path tries to use it
  653. * after this
  654. */
  655. dd->ipath_pd = NULL;
  656. }
  657. static void __devexit ipath_remove_one(struct pci_dev *pdev)
  658. {
  659. struct ipath_devdata *dd = pci_get_drvdata(pdev);
  660. ipath_cdbg(VERBOSE, "removing, pdev=%p, dd=%p\n", pdev, dd);
  661. /*
  662. * disable the IB link early, to be sure no new packets arrive, which
  663. * complicates the shutdown process
  664. */
  665. ipath_shutdown_device(dd);
  666. flush_scheduled_work();
  667. if (dd->verbs_dev)
  668. ipath_unregister_ib_device(dd->verbs_dev);
  669. ipath_diag_remove(dd);
  670. ipath_user_remove(dd);
  671. ipathfs_remove_device(dd);
  672. ipath_device_remove_group(&pdev->dev, dd);
  673. ipath_cdbg(VERBOSE, "Releasing pci memory regions, dd %p, "
  674. "unit %u\n", dd, (u32) dd->ipath_unit);
  675. cleanup_device(dd);
  676. /*
  677. * turn off rcv, send, and interrupts for all ports, all drivers
  678. * should also hard reset the chip here?
  679. * free up port 0 (kernel) rcvhdr, egr bufs, and eventually tid bufs
  680. * for all versions of the driver, if they were allocated
  681. */
  682. if (dd->ipath_irq) {
  683. ipath_cdbg(VERBOSE, "unit %u free irq %d\n",
  684. dd->ipath_unit, dd->ipath_irq);
  685. dd->ipath_f_free_irq(dd);
  686. } else
  687. ipath_dbg("irq is 0, not doing free_irq "
  688. "for unit %u\n", dd->ipath_unit);
  689. /*
  690. * we check for NULL here, because it's outside
  691. * the kregbase check, and we need to call it
  692. * after the free_irq. Thus it's possible that
  693. * the function pointers were never initialized.
  694. */
  695. if (dd->ipath_f_cleanup)
  696. /* clean up chip-specific stuff */
  697. dd->ipath_f_cleanup(dd);
  698. ipath_cdbg(VERBOSE, "Unmapping kregbase %p\n", dd->ipath_kregbase);
  699. iounmap((volatile void __iomem *) dd->ipath_kregbase);
  700. pci_release_regions(pdev);
  701. ipath_cdbg(VERBOSE, "calling pci_disable_device\n");
  702. pci_disable_device(pdev);
  703. ipath_free_devdata(pdev, dd);
  704. }
  705. /* general driver use */
  706. DEFINE_MUTEX(ipath_mutex);
  707. static DEFINE_SPINLOCK(ipath_pioavail_lock);
  708. /**
  709. * ipath_disarm_piobufs - cancel a range of PIO buffers
  710. * @dd: the infinipath device
  711. * @first: the first PIO buffer to cancel
  712. * @cnt: the number of PIO buffers to cancel
  713. *
  714. * cancel a range of PIO buffers, used when they might be armed, but
  715. * not triggered. Used at init to ensure buffer state, and also user
  716. * process close, in case it died while writing to a PIO buffer
  717. * Also after errors.
  718. */
  719. void ipath_disarm_piobufs(struct ipath_devdata *dd, unsigned first,
  720. unsigned cnt)
  721. {
  722. unsigned i, last = first + cnt;
  723. unsigned long flags;
  724. ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first);
  725. for (i = first; i < last; i++) {
  726. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  727. /*
  728. * The disarm-related bits are write-only, so it
  729. * is ok to OR them in with our copy of sendctrl
  730. * while we hold the lock.
  731. */
  732. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  733. dd->ipath_sendctrl | INFINIPATH_S_DISARM |
  734. (i << INFINIPATH_S_DISARMPIOBUF_SHIFT));
  735. /* can't disarm bufs back-to-back per iba7220 spec */
  736. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  737. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  738. }
  739. /* on some older chips, update may not happen after cancel */
  740. ipath_force_pio_avail_update(dd);
  741. }
  742. /**
  743. * ipath_wait_linkstate - wait for an IB link state change to occur
  744. * @dd: the infinipath device
  745. * @state: the state to wait for
  746. * @msecs: the number of milliseconds to wait
  747. *
  748. * wait up to msecs milliseconds for IB link state change to occur for
  749. * now, take the easy polling route. Currently used only by
  750. * ipath_set_linkstate. Returns 0 if state reached, otherwise
  751. * -ETIMEDOUT state can have multiple states set, for any of several
  752. * transitions.
  753. */
  754. int ipath_wait_linkstate(struct ipath_devdata *dd, u32 state, int msecs)
  755. {
  756. dd->ipath_state_wanted = state;
  757. wait_event_interruptible_timeout(ipath_state_wait,
  758. (dd->ipath_flags & state),
  759. msecs_to_jiffies(msecs));
  760. dd->ipath_state_wanted = 0;
  761. if (!(dd->ipath_flags & state)) {
  762. u64 val;
  763. ipath_cdbg(VERBOSE, "Didn't reach linkstate %s within %u"
  764. " ms\n",
  765. /* test INIT ahead of DOWN, both can be set */
  766. (state & IPATH_LINKINIT) ? "INIT" :
  767. ((state & IPATH_LINKDOWN) ? "DOWN" :
  768. ((state & IPATH_LINKARMED) ? "ARM" : "ACTIVE")),
  769. msecs);
  770. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  771. ipath_cdbg(VERBOSE, "ibcc=%llx ibcstatus=%llx (%s)\n",
  772. (unsigned long long) ipath_read_kreg64(
  773. dd, dd->ipath_kregs->kr_ibcctrl),
  774. (unsigned long long) val,
  775. ipath_ibcstatus_str[val & dd->ibcs_lts_mask]);
  776. }
  777. return (dd->ipath_flags & state) ? 0 : -ETIMEDOUT;
  778. }
  779. static void decode_sdma_errs(struct ipath_devdata *dd, ipath_err_t err,
  780. char *buf, size_t blen)
  781. {
  782. static const struct {
  783. ipath_err_t err;
  784. const char *msg;
  785. } errs[] = {
  786. { INFINIPATH_E_SDMAGENMISMATCH, "SDmaGenMismatch" },
  787. { INFINIPATH_E_SDMAOUTOFBOUND, "SDmaOutOfBound" },
  788. { INFINIPATH_E_SDMATAILOUTOFBOUND, "SDmaTailOutOfBound" },
  789. { INFINIPATH_E_SDMABASE, "SDmaBase" },
  790. { INFINIPATH_E_SDMA1STDESC, "SDma1stDesc" },
  791. { INFINIPATH_E_SDMARPYTAG, "SDmaRpyTag" },
  792. { INFINIPATH_E_SDMADWEN, "SDmaDwEn" },
  793. { INFINIPATH_E_SDMAMISSINGDW, "SDmaMissingDw" },
  794. { INFINIPATH_E_SDMAUNEXPDATA, "SDmaUnexpData" },
  795. { INFINIPATH_E_SDMADESCADDRMISALIGN, "SDmaDescAddrMisalign" },
  796. { INFINIPATH_E_SENDBUFMISUSE, "SendBufMisuse" },
  797. { INFINIPATH_E_SDMADISABLED, "SDmaDisabled" },
  798. };
  799. int i;
  800. int expected;
  801. size_t bidx = 0;
  802. for (i = 0; i < ARRAY_SIZE(errs); i++) {
  803. expected = (errs[i].err != INFINIPATH_E_SDMADISABLED) ? 0 :
  804. test_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  805. if ((err & errs[i].err) && !expected)
  806. bidx += snprintf(buf + bidx, blen - bidx,
  807. "%s ", errs[i].msg);
  808. }
  809. }
  810. /*
  811. * Decode the error status into strings, deciding whether to always
  812. * print * it or not depending on "normal packet errors" vs everything
  813. * else. Return 1 if "real" errors, otherwise 0 if only packet
  814. * errors, so caller can decide what to print with the string.
  815. */
  816. int ipath_decode_err(struct ipath_devdata *dd, char *buf, size_t blen,
  817. ipath_err_t err)
  818. {
  819. int iserr = 1;
  820. *buf = '\0';
  821. if (err & INFINIPATH_E_PKTERRS) {
  822. if (!(err & ~INFINIPATH_E_PKTERRS))
  823. iserr = 0; // if only packet errors.
  824. if (ipath_debug & __IPATH_ERRPKTDBG) {
  825. if (err & INFINIPATH_E_REBP)
  826. strlcat(buf, "EBP ", blen);
  827. if (err & INFINIPATH_E_RVCRC)
  828. strlcat(buf, "VCRC ", blen);
  829. if (err & INFINIPATH_E_RICRC) {
  830. strlcat(buf, "CRC ", blen);
  831. // clear for check below, so only once
  832. err &= INFINIPATH_E_RICRC;
  833. }
  834. if (err & INFINIPATH_E_RSHORTPKTLEN)
  835. strlcat(buf, "rshortpktlen ", blen);
  836. if (err & INFINIPATH_E_SDROPPEDDATAPKT)
  837. strlcat(buf, "sdroppeddatapkt ", blen);
  838. if (err & INFINIPATH_E_SPKTLEN)
  839. strlcat(buf, "spktlen ", blen);
  840. }
  841. if ((err & INFINIPATH_E_RICRC) &&
  842. !(err&(INFINIPATH_E_RVCRC|INFINIPATH_E_REBP)))
  843. strlcat(buf, "CRC ", blen);
  844. if (!iserr)
  845. goto done;
  846. }
  847. if (err & INFINIPATH_E_RHDRLEN)
  848. strlcat(buf, "rhdrlen ", blen);
  849. if (err & INFINIPATH_E_RBADTID)
  850. strlcat(buf, "rbadtid ", blen);
  851. if (err & INFINIPATH_E_RBADVERSION)
  852. strlcat(buf, "rbadversion ", blen);
  853. if (err & INFINIPATH_E_RHDR)
  854. strlcat(buf, "rhdr ", blen);
  855. if (err & INFINIPATH_E_SENDSPECIALTRIGGER)
  856. strlcat(buf, "sendspecialtrigger ", blen);
  857. if (err & INFINIPATH_E_RLONGPKTLEN)
  858. strlcat(buf, "rlongpktlen ", blen);
  859. if (err & INFINIPATH_E_RMAXPKTLEN)
  860. strlcat(buf, "rmaxpktlen ", blen);
  861. if (err & INFINIPATH_E_RMINPKTLEN)
  862. strlcat(buf, "rminpktlen ", blen);
  863. if (err & INFINIPATH_E_SMINPKTLEN)
  864. strlcat(buf, "sminpktlen ", blen);
  865. if (err & INFINIPATH_E_RFORMATERR)
  866. strlcat(buf, "rformaterr ", blen);
  867. if (err & INFINIPATH_E_RUNSUPVL)
  868. strlcat(buf, "runsupvl ", blen);
  869. if (err & INFINIPATH_E_RUNEXPCHAR)
  870. strlcat(buf, "runexpchar ", blen);
  871. if (err & INFINIPATH_E_RIBFLOW)
  872. strlcat(buf, "ribflow ", blen);
  873. if (err & INFINIPATH_E_SUNDERRUN)
  874. strlcat(buf, "sunderrun ", blen);
  875. if (err & INFINIPATH_E_SPIOARMLAUNCH)
  876. strlcat(buf, "spioarmlaunch ", blen);
  877. if (err & INFINIPATH_E_SUNEXPERRPKTNUM)
  878. strlcat(buf, "sunexperrpktnum ", blen);
  879. if (err & INFINIPATH_E_SDROPPEDSMPPKT)
  880. strlcat(buf, "sdroppedsmppkt ", blen);
  881. if (err & INFINIPATH_E_SMAXPKTLEN)
  882. strlcat(buf, "smaxpktlen ", blen);
  883. if (err & INFINIPATH_E_SUNSUPVL)
  884. strlcat(buf, "sunsupVL ", blen);
  885. if (err & INFINIPATH_E_INVALIDADDR)
  886. strlcat(buf, "invalidaddr ", blen);
  887. if (err & INFINIPATH_E_RRCVEGRFULL)
  888. strlcat(buf, "rcvegrfull ", blen);
  889. if (err & INFINIPATH_E_RRCVHDRFULL)
  890. strlcat(buf, "rcvhdrfull ", blen);
  891. if (err & INFINIPATH_E_IBSTATUSCHANGED)
  892. strlcat(buf, "ibcstatuschg ", blen);
  893. if (err & INFINIPATH_E_RIBLOSTLINK)
  894. strlcat(buf, "riblostlink ", blen);
  895. if (err & INFINIPATH_E_HARDWARE)
  896. strlcat(buf, "hardware ", blen);
  897. if (err & INFINIPATH_E_RESET)
  898. strlcat(buf, "reset ", blen);
  899. if (err & INFINIPATH_E_SDMAERRS)
  900. decode_sdma_errs(dd, err, buf, blen);
  901. if (err & INFINIPATH_E_INVALIDEEPCMD)
  902. strlcat(buf, "invalideepromcmd ", blen);
  903. done:
  904. return iserr;
  905. }
  906. /**
  907. * get_rhf_errstring - decode RHF errors
  908. * @err: the err number
  909. * @msg: the output buffer
  910. * @len: the length of the output buffer
  911. *
  912. * only used one place now, may want more later
  913. */
  914. static void get_rhf_errstring(u32 err, char *msg, size_t len)
  915. {
  916. /* if no errors, and so don't need to check what's first */
  917. *msg = '\0';
  918. if (err & INFINIPATH_RHF_H_ICRCERR)
  919. strlcat(msg, "icrcerr ", len);
  920. if (err & INFINIPATH_RHF_H_VCRCERR)
  921. strlcat(msg, "vcrcerr ", len);
  922. if (err & INFINIPATH_RHF_H_PARITYERR)
  923. strlcat(msg, "parityerr ", len);
  924. if (err & INFINIPATH_RHF_H_LENERR)
  925. strlcat(msg, "lenerr ", len);
  926. if (err & INFINIPATH_RHF_H_MTUERR)
  927. strlcat(msg, "mtuerr ", len);
  928. if (err & INFINIPATH_RHF_H_IHDRERR)
  929. /* infinipath hdr checksum error */
  930. strlcat(msg, "ipathhdrerr ", len);
  931. if (err & INFINIPATH_RHF_H_TIDERR)
  932. strlcat(msg, "tiderr ", len);
  933. if (err & INFINIPATH_RHF_H_MKERR)
  934. /* bad port, offset, etc. */
  935. strlcat(msg, "invalid ipathhdr ", len);
  936. if (err & INFINIPATH_RHF_H_IBERR)
  937. strlcat(msg, "iberr ", len);
  938. if (err & INFINIPATH_RHF_L_SWA)
  939. strlcat(msg, "swA ", len);
  940. if (err & INFINIPATH_RHF_L_SWB)
  941. strlcat(msg, "swB ", len);
  942. }
  943. /**
  944. * ipath_get_egrbuf - get an eager buffer
  945. * @dd: the infinipath device
  946. * @bufnum: the eager buffer to get
  947. *
  948. * must only be called if ipath_pd[port] is known to be allocated
  949. */
  950. static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum)
  951. {
  952. return dd->ipath_port0_skbinfo ?
  953. (void *) dd->ipath_port0_skbinfo[bufnum].skb->data : NULL;
  954. }
  955. /**
  956. * ipath_alloc_skb - allocate an skb and buffer with possible constraints
  957. * @dd: the infinipath device
  958. * @gfp_mask: the sk_buff SFP mask
  959. */
  960. struct sk_buff *ipath_alloc_skb(struct ipath_devdata *dd,
  961. gfp_t gfp_mask)
  962. {
  963. struct sk_buff *skb;
  964. u32 len;
  965. /*
  966. * Only fully supported way to handle this is to allocate lots
  967. * extra, align as needed, and then do skb_reserve(). That wastes
  968. * a lot of memory... I'll have to hack this into infinipath_copy
  969. * also.
  970. */
  971. /*
  972. * We need 2 extra bytes for ipath_ether data sent in the
  973. * key header. In order to keep everything dword aligned,
  974. * we'll reserve 4 bytes.
  975. */
  976. len = dd->ipath_ibmaxlen + 4;
  977. if (dd->ipath_flags & IPATH_4BYTE_TID) {
  978. /* We need a 2KB multiple alignment, and there is no way
  979. * to do it except to allocate extra and then skb_reserve
  980. * enough to bring it up to the right alignment.
  981. */
  982. len += 2047;
  983. }
  984. skb = __dev_alloc_skb(len, gfp_mask);
  985. if (!skb) {
  986. ipath_dev_err(dd, "Failed to allocate skbuff, length %u\n",
  987. len);
  988. goto bail;
  989. }
  990. skb_reserve(skb, 4);
  991. if (dd->ipath_flags & IPATH_4BYTE_TID) {
  992. u32 una = (unsigned long)skb->data & 2047;
  993. if (una)
  994. skb_reserve(skb, 2048 - una);
  995. }
  996. bail:
  997. return skb;
  998. }
  999. static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
  1000. u32 eflags,
  1001. u32 l,
  1002. u32 etail,
  1003. __le32 *rhf_addr,
  1004. struct ipath_message_header *hdr)
  1005. {
  1006. char emsg[128];
  1007. get_rhf_errstring(eflags, emsg, sizeof emsg);
  1008. ipath_cdbg(PKT, "RHFerrs %x hdrqtail=%x typ=%u "
  1009. "tlen=%x opcode=%x egridx=%x: %s\n",
  1010. eflags, l,
  1011. ipath_hdrget_rcv_type(rhf_addr),
  1012. ipath_hdrget_length_in_bytes(rhf_addr),
  1013. be32_to_cpu(hdr->bth[0]) >> 24,
  1014. etail, emsg);
  1015. /* Count local link integrity errors. */
  1016. if (eflags & (INFINIPATH_RHF_H_ICRCERR | INFINIPATH_RHF_H_VCRCERR)) {
  1017. u8 n = (dd->ipath_ibcctrl >>
  1018. INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
  1019. INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
  1020. if (++dd->ipath_lli_counter > n) {
  1021. dd->ipath_lli_counter = 0;
  1022. dd->ipath_lli_errors++;
  1023. }
  1024. }
  1025. }
  1026. /*
  1027. * ipath_kreceive - receive a packet
  1028. * @pd: the infinipath port
  1029. *
  1030. * called from interrupt handler for errors or receive interrupt
  1031. */
  1032. void ipath_kreceive(struct ipath_portdata *pd)
  1033. {
  1034. struct ipath_devdata *dd = pd->port_dd;
  1035. __le32 *rhf_addr;
  1036. void *ebuf;
  1037. const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
  1038. const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
  1039. u32 etail = -1, l, hdrqtail;
  1040. struct ipath_message_header *hdr;
  1041. u32 eflags, i, etype, tlen, pkttot = 0, updegr = 0, reloop = 0;
  1042. static u64 totcalls; /* stats, may eventually remove */
  1043. int last;
  1044. l = pd->port_head;
  1045. rhf_addr = (__le32 *) pd->port_rcvhdrq + l + dd->ipath_rhf_offset;
  1046. if (dd->ipath_flags & IPATH_NODMA_RTAIL) {
  1047. u32 seq = ipath_hdrget_seq(rhf_addr);
  1048. if (seq != pd->port_seq_cnt)
  1049. goto bail;
  1050. hdrqtail = 0;
  1051. } else {
  1052. hdrqtail = ipath_get_rcvhdrtail(pd);
  1053. if (l == hdrqtail)
  1054. goto bail;
  1055. smp_rmb();
  1056. }
  1057. reloop:
  1058. for (last = 0, i = 1; !last; i += !last) {
  1059. hdr = dd->ipath_f_get_msgheader(dd, rhf_addr);
  1060. eflags = ipath_hdrget_err_flags(rhf_addr);
  1061. etype = ipath_hdrget_rcv_type(rhf_addr);
  1062. /* total length */
  1063. tlen = ipath_hdrget_length_in_bytes(rhf_addr);
  1064. ebuf = NULL;
  1065. if ((dd->ipath_flags & IPATH_NODMA_RTAIL) ?
  1066. ipath_hdrget_use_egr_buf(rhf_addr) :
  1067. (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
  1068. /*
  1069. * It turns out that the chip uses an eager buffer
  1070. * for all non-expected packets, whether it "needs"
  1071. * one or not. So always get the index, but don't
  1072. * set ebuf (so we try to copy data) unless the
  1073. * length requires it.
  1074. */
  1075. etail = ipath_hdrget_index(rhf_addr);
  1076. updegr = 1;
  1077. if (tlen > sizeof(*hdr) ||
  1078. etype == RCVHQ_RCV_TYPE_NON_KD)
  1079. ebuf = ipath_get_egrbuf(dd, etail);
  1080. }
  1081. /*
  1082. * both tiderr and ipathhdrerr are set for all plain IB
  1083. * packets; only ipathhdrerr should be set.
  1084. */
  1085. if (etype != RCVHQ_RCV_TYPE_NON_KD &&
  1086. etype != RCVHQ_RCV_TYPE_ERROR &&
  1087. ipath_hdrget_ipath_ver(hdr->iph.ver_port_tid_offset) !=
  1088. IPS_PROTO_VERSION)
  1089. ipath_cdbg(PKT, "Bad InfiniPath protocol version "
  1090. "%x\n", etype);
  1091. if (unlikely(eflags))
  1092. ipath_rcv_hdrerr(dd, eflags, l, etail, rhf_addr, hdr);
  1093. else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
  1094. ipath_ib_rcv(dd->verbs_dev, (u32 *)hdr, ebuf, tlen);
  1095. if (dd->ipath_lli_counter)
  1096. dd->ipath_lli_counter--;
  1097. } else if (etype == RCVHQ_RCV_TYPE_EAGER) {
  1098. u8 opcode = be32_to_cpu(hdr->bth[0]) >> 24;
  1099. u32 qp = be32_to_cpu(hdr->bth[1]) & 0xffffff;
  1100. ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
  1101. "qp=%x), len %x; ignored\n",
  1102. etype, opcode, qp, tlen);
  1103. }
  1104. else if (etype == RCVHQ_RCV_TYPE_EXPECTED)
  1105. ipath_dbg("Bug: Expected TID, opcode %x; ignored\n",
  1106. be32_to_cpu(hdr->bth[0]) >> 24);
  1107. else {
  1108. /*
  1109. * error packet, type of error unknown.
  1110. * Probably type 3, but we don't know, so don't
  1111. * even try to print the opcode, etc.
  1112. * Usually caused by a "bad packet", that has no
  1113. * BTH, when the LRH says it should.
  1114. */
  1115. ipath_cdbg(ERRPKT, "Error Pkt, but no eflags! egrbuf"
  1116. " %x, len %x hdrq+%x rhf: %Lx\n",
  1117. etail, tlen, l, (unsigned long long)
  1118. le64_to_cpu(*(__le64 *) rhf_addr));
  1119. if (ipath_debug & __IPATH_ERRPKTDBG) {
  1120. u32 j, *d, dw = rsize-2;
  1121. if (rsize > (tlen>>2))
  1122. dw = tlen>>2;
  1123. d = (u32 *)hdr;
  1124. printk(KERN_DEBUG "EPkt rcvhdr(%x dw):\n",
  1125. dw);
  1126. for (j = 0; j < dw; j++)
  1127. printk(KERN_DEBUG "%8x%s", d[j],
  1128. (j%8) == 7 ? "\n" : " ");
  1129. printk(KERN_DEBUG ".\n");
  1130. }
  1131. }
  1132. l += rsize;
  1133. if (l >= maxcnt)
  1134. l = 0;
  1135. rhf_addr = (__le32 *) pd->port_rcvhdrq +
  1136. l + dd->ipath_rhf_offset;
  1137. if (dd->ipath_flags & IPATH_NODMA_RTAIL) {
  1138. u32 seq = ipath_hdrget_seq(rhf_addr);
  1139. if (++pd->port_seq_cnt > 13)
  1140. pd->port_seq_cnt = 1;
  1141. if (seq != pd->port_seq_cnt)
  1142. last = 1;
  1143. } else if (l == hdrqtail)
  1144. last = 1;
  1145. /*
  1146. * update head regs on last packet, and every 16 packets.
  1147. * Reduce bus traffic, while still trying to prevent
  1148. * rcvhdrq overflows, for when the queue is nearly full
  1149. */
  1150. if (last || !(i & 0xf)) {
  1151. u64 lval = l;
  1152. /* request IBA6120 and 7220 interrupt only on last */
  1153. if (last)
  1154. lval |= dd->ipath_rhdrhead_intr_off;
  1155. ipath_write_ureg(dd, ur_rcvhdrhead, lval,
  1156. pd->port_port);
  1157. if (updegr) {
  1158. ipath_write_ureg(dd, ur_rcvegrindexhead,
  1159. etail, pd->port_port);
  1160. updegr = 0;
  1161. }
  1162. }
  1163. }
  1164. if (!dd->ipath_rhdrhead_intr_off && !reloop &&
  1165. !(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
  1166. /* IBA6110 workaround; we can have a race clearing chip
  1167. * interrupt with another interrupt about to be delivered,
  1168. * and can clear it before it is delivered on the GPIO
  1169. * workaround. By doing the extra check here for the
  1170. * in-memory tail register updating while we were doing
  1171. * earlier packets, we "almost" guarantee we have covered
  1172. * that case.
  1173. */
  1174. u32 hqtail = ipath_get_rcvhdrtail(pd);
  1175. if (hqtail != hdrqtail) {
  1176. hdrqtail = hqtail;
  1177. reloop = 1; /* loop 1 extra time at most */
  1178. goto reloop;
  1179. }
  1180. }
  1181. pkttot += i;
  1182. pd->port_head = l;
  1183. if (pkttot > ipath_stats.sps_maxpkts_call)
  1184. ipath_stats.sps_maxpkts_call = pkttot;
  1185. ipath_stats.sps_port0pkts += pkttot;
  1186. ipath_stats.sps_avgpkts_call =
  1187. ipath_stats.sps_port0pkts / ++totcalls;
  1188. bail:;
  1189. }
  1190. /**
  1191. * ipath_update_pio_bufs - update shadow copy of the PIO availability map
  1192. * @dd: the infinipath device
  1193. *
  1194. * called whenever our local copy indicates we have run out of send buffers
  1195. * NOTE: This can be called from interrupt context by some code
  1196. * and from non-interrupt context by ipath_getpiobuf().
  1197. */
  1198. static void ipath_update_pio_bufs(struct ipath_devdata *dd)
  1199. {
  1200. unsigned long flags;
  1201. int i;
  1202. const unsigned piobregs = (unsigned)dd->ipath_pioavregs;
  1203. /* If the generation (check) bits have changed, then we update the
  1204. * busy bit for the corresponding PIO buffer. This algorithm will
  1205. * modify positions to the value they already have in some cases
  1206. * (i.e., no change), but it's faster than changing only the bits
  1207. * that have changed.
  1208. *
  1209. * We would like to do this atomicly, to avoid spinlocks in the
  1210. * critical send path, but that's not really possible, given the
  1211. * type of changes, and that this routine could be called on
  1212. * multiple cpu's simultaneously, so we lock in this routine only,
  1213. * to avoid conflicting updates; all we change is the shadow, and
  1214. * it's a single 64 bit memory location, so by definition the update
  1215. * is atomic in terms of what other cpu's can see in testing the
  1216. * bits. The spin_lock overhead isn't too bad, since it only
  1217. * happens when all buffers are in use, so only cpu overhead, not
  1218. * latency or bandwidth is affected.
  1219. */
  1220. if (!dd->ipath_pioavailregs_dma) {
  1221. ipath_dbg("Update shadow pioavail, but regs_dma NULL!\n");
  1222. return;
  1223. }
  1224. if (ipath_debug & __IPATH_VERBDBG) {
  1225. /* only if packet debug and verbose */
  1226. volatile __le64 *dma = dd->ipath_pioavailregs_dma;
  1227. unsigned long *shadow = dd->ipath_pioavailshadow;
  1228. ipath_cdbg(PKT, "Refill avail, dma0=%llx shad0=%lx, "
  1229. "d1=%llx s1=%lx, d2=%llx s2=%lx, d3=%llx "
  1230. "s3=%lx\n",
  1231. (unsigned long long) le64_to_cpu(dma[0]),
  1232. shadow[0],
  1233. (unsigned long long) le64_to_cpu(dma[1]),
  1234. shadow[1],
  1235. (unsigned long long) le64_to_cpu(dma[2]),
  1236. shadow[2],
  1237. (unsigned long long) le64_to_cpu(dma[3]),
  1238. shadow[3]);
  1239. if (piobregs > 4)
  1240. ipath_cdbg(
  1241. PKT, "2nd group, dma4=%llx shad4=%lx, "
  1242. "d5=%llx s5=%lx, d6=%llx s6=%lx, "
  1243. "d7=%llx s7=%lx\n",
  1244. (unsigned long long) le64_to_cpu(dma[4]),
  1245. shadow[4],
  1246. (unsigned long long) le64_to_cpu(dma[5]),
  1247. shadow[5],
  1248. (unsigned long long) le64_to_cpu(dma[6]),
  1249. shadow[6],
  1250. (unsigned long long) le64_to_cpu(dma[7]),
  1251. shadow[7]);
  1252. }
  1253. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1254. for (i = 0; i < piobregs; i++) {
  1255. u64 pchbusy, pchg, piov, pnew;
  1256. /*
  1257. * Chip Errata: bug 6641; even and odd qwords>3 are swapped
  1258. */
  1259. if (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS))
  1260. piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i ^ 1]);
  1261. else
  1262. piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
  1263. pchg = dd->ipath_pioavailkernel[i] &
  1264. ~(dd->ipath_pioavailshadow[i] ^ piov);
  1265. pchbusy = pchg << INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT;
  1266. if (pchg && (pchbusy & dd->ipath_pioavailshadow[i])) {
  1267. pnew = dd->ipath_pioavailshadow[i] & ~pchbusy;
  1268. pnew |= piov & pchbusy;
  1269. dd->ipath_pioavailshadow[i] = pnew;
  1270. }
  1271. }
  1272. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1273. }
  1274. /*
  1275. * used to force update of pioavailshadow if we can't get a pio buffer.
  1276. * Needed primarily due to exitting freeze mode after recovering
  1277. * from errors. Done lazily, because it's safer (known to not
  1278. * be writing pio buffers).
  1279. */
  1280. static void ipath_reset_availshadow(struct ipath_devdata *dd)
  1281. {
  1282. int i, im;
  1283. unsigned long flags;
  1284. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1285. for (i = 0; i < dd->ipath_pioavregs; i++) {
  1286. u64 val, oldval;
  1287. /* deal with 6110 chip bug on high register #s */
  1288. im = (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS)) ?
  1289. i ^ 1 : i;
  1290. val = le64_to_cpu(dd->ipath_pioavailregs_dma[im]);
  1291. /*
  1292. * busy out the buffers not in the kernel avail list,
  1293. * without changing the generation bits.
  1294. */
  1295. oldval = dd->ipath_pioavailshadow[i];
  1296. dd->ipath_pioavailshadow[i] = val |
  1297. ((~dd->ipath_pioavailkernel[i] <<
  1298. INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT) &
  1299. 0xaaaaaaaaaaaaaaaaULL); /* All BUSY bits in qword */
  1300. if (oldval != dd->ipath_pioavailshadow[i])
  1301. ipath_dbg("shadow[%d] was %Lx, now %lx\n",
  1302. i, (unsigned long long) oldval,
  1303. dd->ipath_pioavailshadow[i]);
  1304. }
  1305. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1306. }
  1307. /**
  1308. * ipath_setrcvhdrsize - set the receive header size
  1309. * @dd: the infinipath device
  1310. * @rhdrsize: the receive header size
  1311. *
  1312. * called from user init code, and also layered driver init
  1313. */
  1314. int ipath_setrcvhdrsize(struct ipath_devdata *dd, unsigned rhdrsize)
  1315. {
  1316. int ret = 0;
  1317. if (dd->ipath_flags & IPATH_RCVHDRSZ_SET) {
  1318. if (dd->ipath_rcvhdrsize != rhdrsize) {
  1319. dev_info(&dd->pcidev->dev,
  1320. "Error: can't set protocol header "
  1321. "size %u, already %u\n",
  1322. rhdrsize, dd->ipath_rcvhdrsize);
  1323. ret = -EAGAIN;
  1324. } else
  1325. ipath_cdbg(VERBOSE, "Reuse same protocol header "
  1326. "size %u\n", dd->ipath_rcvhdrsize);
  1327. } else if (rhdrsize > (dd->ipath_rcvhdrentsize -
  1328. (sizeof(u64) / sizeof(u32)))) {
  1329. ipath_dbg("Error: can't set protocol header size %u "
  1330. "(> max %u)\n", rhdrsize,
  1331. dd->ipath_rcvhdrentsize -
  1332. (u32) (sizeof(u64) / sizeof(u32)));
  1333. ret = -EOVERFLOW;
  1334. } else {
  1335. dd->ipath_flags |= IPATH_RCVHDRSZ_SET;
  1336. dd->ipath_rcvhdrsize = rhdrsize;
  1337. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
  1338. dd->ipath_rcvhdrsize);
  1339. ipath_cdbg(VERBOSE, "Set protocol header size to %u\n",
  1340. dd->ipath_rcvhdrsize);
  1341. }
  1342. return ret;
  1343. }
  1344. /*
  1345. * debugging code and stats updates if no pio buffers available.
  1346. */
  1347. static noinline void no_pio_bufs(struct ipath_devdata *dd)
  1348. {
  1349. unsigned long *shadow = dd->ipath_pioavailshadow;
  1350. __le64 *dma = (__le64 *)dd->ipath_pioavailregs_dma;
  1351. dd->ipath_upd_pio_shadow = 1;
  1352. /*
  1353. * not atomic, but if we lose a stat count in a while, that's OK
  1354. */
  1355. ipath_stats.sps_nopiobufs++;
  1356. if (!(++dd->ipath_consec_nopiobuf % 100000)) {
  1357. ipath_force_pio_avail_update(dd); /* at start */
  1358. ipath_dbg("%u tries no piobufavail ts%lx; dmacopy: "
  1359. "%llx %llx %llx %llx\n"
  1360. "ipath shadow: %lx %lx %lx %lx\n",
  1361. dd->ipath_consec_nopiobuf,
  1362. (unsigned long)get_cycles(),
  1363. (unsigned long long) le64_to_cpu(dma[0]),
  1364. (unsigned long long) le64_to_cpu(dma[1]),
  1365. (unsigned long long) le64_to_cpu(dma[2]),
  1366. (unsigned long long) le64_to_cpu(dma[3]),
  1367. shadow[0], shadow[1], shadow[2], shadow[3]);
  1368. /*
  1369. * 4 buffers per byte, 4 registers above, cover rest
  1370. * below
  1371. */
  1372. if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) >
  1373. (sizeof(shadow[0]) * 4 * 4))
  1374. ipath_dbg("2nd group: dmacopy: "
  1375. "%llx %llx %llx %llx\n"
  1376. "ipath shadow: %lx %lx %lx %lx\n",
  1377. (unsigned long long)le64_to_cpu(dma[4]),
  1378. (unsigned long long)le64_to_cpu(dma[5]),
  1379. (unsigned long long)le64_to_cpu(dma[6]),
  1380. (unsigned long long)le64_to_cpu(dma[7]),
  1381. shadow[4], shadow[5], shadow[6], shadow[7]);
  1382. /* at end, so update likely happened */
  1383. ipath_reset_availshadow(dd);
  1384. }
  1385. }
  1386. /*
  1387. * common code for normal driver pio buffer allocation, and reserved
  1388. * allocation.
  1389. *
  1390. * do appropriate marking as busy, etc.
  1391. * returns buffer number if one found (>=0), negative number is error.
  1392. */
  1393. static u32 __iomem *ipath_getpiobuf_range(struct ipath_devdata *dd,
  1394. u32 *pbufnum, u32 first, u32 last, u32 firsti)
  1395. {
  1396. int i, j, updated = 0;
  1397. unsigned piobcnt;
  1398. unsigned long flags;
  1399. unsigned long *shadow = dd->ipath_pioavailshadow;
  1400. u32 __iomem *buf;
  1401. piobcnt = last - first;
  1402. if (dd->ipath_upd_pio_shadow) {
  1403. /*
  1404. * Minor optimization. If we had no buffers on last call,
  1405. * start out by doing the update; continue and do scan even
  1406. * if no buffers were updated, to be paranoid
  1407. */
  1408. ipath_update_pio_bufs(dd);
  1409. updated++;
  1410. i = first;
  1411. } else
  1412. i = firsti;
  1413. rescan:
  1414. /*
  1415. * while test_and_set_bit() is atomic, we do that and then the
  1416. * change_bit(), and the pair is not. See if this is the cause
  1417. * of the remaining armlaunch errors.
  1418. */
  1419. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1420. for (j = 0; j < piobcnt; j++, i++) {
  1421. if (i >= last)
  1422. i = first;
  1423. if (__test_and_set_bit((2 * i) + 1, shadow))
  1424. continue;
  1425. /* flip generation bit */
  1426. __change_bit(2 * i, shadow);
  1427. break;
  1428. }
  1429. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1430. if (j == piobcnt) {
  1431. if (!updated) {
  1432. /*
  1433. * first time through; shadow exhausted, but may be
  1434. * buffers available, try an update and then rescan.
  1435. */
  1436. ipath_update_pio_bufs(dd);
  1437. updated++;
  1438. i = first;
  1439. goto rescan;
  1440. } else if (updated == 1 && piobcnt <=
  1441. ((dd->ipath_sendctrl
  1442. >> INFINIPATH_S_UPDTHRESH_SHIFT) &
  1443. INFINIPATH_S_UPDTHRESH_MASK)) {
  1444. /*
  1445. * for chips supporting and using the update
  1446. * threshold we need to force an update of the
  1447. * in-memory copy if the count is less than the
  1448. * thershold, then check one more time.
  1449. */
  1450. ipath_force_pio_avail_update(dd);
  1451. ipath_update_pio_bufs(dd);
  1452. updated++;
  1453. i = first;
  1454. goto rescan;
  1455. }
  1456. no_pio_bufs(dd);
  1457. buf = NULL;
  1458. } else {
  1459. if (i < dd->ipath_piobcnt2k)
  1460. buf = (u32 __iomem *) (dd->ipath_pio2kbase +
  1461. i * dd->ipath_palign);
  1462. else
  1463. buf = (u32 __iomem *)
  1464. (dd->ipath_pio4kbase +
  1465. (i - dd->ipath_piobcnt2k) * dd->ipath_4kalign);
  1466. if (pbufnum)
  1467. *pbufnum = i;
  1468. }
  1469. return buf;
  1470. }
  1471. /**
  1472. * ipath_getpiobuf - find an available pio buffer
  1473. * @dd: the infinipath device
  1474. * @plen: the size of the PIO buffer needed in 32-bit words
  1475. * @pbufnum: the buffer number is placed here
  1476. */
  1477. u32 __iomem *ipath_getpiobuf(struct ipath_devdata *dd, u32 plen, u32 *pbufnum)
  1478. {
  1479. u32 __iomem *buf;
  1480. u32 pnum, nbufs;
  1481. u32 first, lasti;
  1482. if (plen + 1 >= IPATH_SMALLBUF_DWORDS) {
  1483. first = dd->ipath_piobcnt2k;
  1484. lasti = dd->ipath_lastpioindexl;
  1485. } else {
  1486. first = 0;
  1487. lasti = dd->ipath_lastpioindex;
  1488. }
  1489. nbufs = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k;
  1490. buf = ipath_getpiobuf_range(dd, &pnum, first, nbufs, lasti);
  1491. if (buf) {
  1492. /*
  1493. * Set next starting place. It's just an optimization,
  1494. * it doesn't matter who wins on this, so no locking
  1495. */
  1496. if (plen + 1 >= IPATH_SMALLBUF_DWORDS)
  1497. dd->ipath_lastpioindexl = pnum + 1;
  1498. else
  1499. dd->ipath_lastpioindex = pnum + 1;
  1500. if (dd->ipath_upd_pio_shadow)
  1501. dd->ipath_upd_pio_shadow = 0;
  1502. if (dd->ipath_consec_nopiobuf)
  1503. dd->ipath_consec_nopiobuf = 0;
  1504. ipath_cdbg(VERBOSE, "Return piobuf%u %uk @ %p\n",
  1505. pnum, (pnum < dd->ipath_piobcnt2k) ? 2 : 4, buf);
  1506. if (pbufnum)
  1507. *pbufnum = pnum;
  1508. }
  1509. return buf;
  1510. }
  1511. /**
  1512. * ipath_chg_pioavailkernel - change which send buffers are available for kernel
  1513. * @dd: the infinipath device
  1514. * @start: the starting send buffer number
  1515. * @len: the number of send buffers
  1516. * @avail: true if the buffers are available for kernel use, false otherwise
  1517. */
  1518. void ipath_chg_pioavailkernel(struct ipath_devdata *dd, unsigned start,
  1519. unsigned len, int avail)
  1520. {
  1521. unsigned long flags;
  1522. unsigned end, cnt = 0, next;
  1523. /* There are two bits per send buffer (busy and generation) */
  1524. start *= 2;
  1525. end = start + len * 2;
  1526. spin_lock_irqsave(&ipath_pioavail_lock, flags);
  1527. /* Set or clear the busy bit in the shadow. */
  1528. while (start < end) {
  1529. if (avail) {
  1530. unsigned long dma;
  1531. int i, im;
  1532. /*
  1533. * the BUSY bit will never be set, because we disarm
  1534. * the user buffers before we hand them back to the
  1535. * kernel. We do have to make sure the generation
  1536. * bit is set correctly in shadow, since it could
  1537. * have changed many times while allocated to user.
  1538. * We can't use the bitmap functions on the full
  1539. * dma array because it is always little-endian, so
  1540. * we have to flip to host-order first.
  1541. * BITS_PER_LONG is slightly wrong, since it's
  1542. * always 64 bits per register in chip...
  1543. * We only work on 64 bit kernels, so that's OK.
  1544. */
  1545. /* deal with 6110 chip bug on high register #s */
  1546. i = start / BITS_PER_LONG;
  1547. im = (i > 3 && (dd->ipath_flags & IPATH_SWAP_PIOBUFS)) ?
  1548. i ^ 1 : i;
  1549. __clear_bit(INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT
  1550. + start, dd->ipath_pioavailshadow);
  1551. dma = (unsigned long) le64_to_cpu(
  1552. dd->ipath_pioavailregs_dma[im]);
  1553. if (test_bit((INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1554. + start) % BITS_PER_LONG, &dma))
  1555. __set_bit(INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1556. + start, dd->ipath_pioavailshadow);
  1557. else
  1558. __clear_bit(INFINIPATH_SENDPIOAVAIL_CHECK_SHIFT
  1559. + start, dd->ipath_pioavailshadow);
  1560. __set_bit(start, dd->ipath_pioavailkernel);
  1561. } else {
  1562. __set_bit(start + INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT,
  1563. dd->ipath_pioavailshadow);
  1564. __clear_bit(start, dd->ipath_pioavailkernel);
  1565. }
  1566. start += 2;
  1567. }
  1568. if (dd->ipath_pioupd_thresh) {
  1569. end = 2 * (dd->ipath_piobcnt2k + dd->ipath_piobcnt4k);
  1570. next = find_first_bit(dd->ipath_pioavailkernel, end);
  1571. while (next < end) {
  1572. cnt++;
  1573. next = find_next_bit(dd->ipath_pioavailkernel, end,
  1574. next + 1);
  1575. }
  1576. }
  1577. spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
  1578. /*
  1579. * When moving buffers from kernel to user, if number assigned to
  1580. * the user is less than the pio update threshold, and threshold
  1581. * is supported (cnt was computed > 0), drop the update threshold
  1582. * so we update at least once per allocated number of buffers.
  1583. * In any case, if the kernel buffers are less than the threshold,
  1584. * drop the threshold. We don't bother increasing it, having once
  1585. * decreased it, since it would typically just cycle back and forth.
  1586. * If we don't decrease below buffers in use, we can wait a long
  1587. * time for an update, until some other context uses PIO buffers.
  1588. */
  1589. if (!avail && len < cnt)
  1590. cnt = len;
  1591. if (cnt < dd->ipath_pioupd_thresh) {
  1592. dd->ipath_pioupd_thresh = cnt;
  1593. ipath_dbg("Decreased pio update threshold to %u\n",
  1594. dd->ipath_pioupd_thresh);
  1595. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1596. dd->ipath_sendctrl &= ~(INFINIPATH_S_UPDTHRESH_MASK
  1597. << INFINIPATH_S_UPDTHRESH_SHIFT);
  1598. dd->ipath_sendctrl |= dd->ipath_pioupd_thresh
  1599. << INFINIPATH_S_UPDTHRESH_SHIFT;
  1600. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1601. dd->ipath_sendctrl);
  1602. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1603. }
  1604. }
  1605. /**
  1606. * ipath_create_rcvhdrq - create a receive header queue
  1607. * @dd: the infinipath device
  1608. * @pd: the port data
  1609. *
  1610. * this must be contiguous memory (from an i/o perspective), and must be
  1611. * DMA'able (which means for some systems, it will go through an IOMMU,
  1612. * or be forced into a low address range).
  1613. */
  1614. int ipath_create_rcvhdrq(struct ipath_devdata *dd,
  1615. struct ipath_portdata *pd)
  1616. {
  1617. int ret = 0;
  1618. if (!pd->port_rcvhdrq) {
  1619. dma_addr_t phys_hdrqtail;
  1620. gfp_t gfp_flags = GFP_USER | __GFP_COMP;
  1621. int amt = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1622. sizeof(u32), PAGE_SIZE);
  1623. pd->port_rcvhdrq = dma_alloc_coherent(
  1624. &dd->pcidev->dev, amt, &pd->port_rcvhdrq_phys,
  1625. gfp_flags);
  1626. if (!pd->port_rcvhdrq) {
  1627. ipath_dev_err(dd, "attempt to allocate %d bytes "
  1628. "for port %u rcvhdrq failed\n",
  1629. amt, pd->port_port);
  1630. ret = -ENOMEM;
  1631. goto bail;
  1632. }
  1633. if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
  1634. pd->port_rcvhdrtail_kvaddr = dma_alloc_coherent(
  1635. &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
  1636. GFP_KERNEL);
  1637. if (!pd->port_rcvhdrtail_kvaddr) {
  1638. ipath_dev_err(dd, "attempt to allocate 1 page "
  1639. "for port %u rcvhdrqtailaddr "
  1640. "failed\n", pd->port_port);
  1641. ret = -ENOMEM;
  1642. dma_free_coherent(&dd->pcidev->dev, amt,
  1643. pd->port_rcvhdrq,
  1644. pd->port_rcvhdrq_phys);
  1645. pd->port_rcvhdrq = NULL;
  1646. goto bail;
  1647. }
  1648. pd->port_rcvhdrqtailaddr_phys = phys_hdrqtail;
  1649. ipath_cdbg(VERBOSE, "port %d hdrtailaddr, %llx "
  1650. "physical\n", pd->port_port,
  1651. (unsigned long long) phys_hdrqtail);
  1652. }
  1653. pd->port_rcvhdrq_size = amt;
  1654. ipath_cdbg(VERBOSE, "%d pages at %p (phys %lx) size=%lu "
  1655. "for port %u rcvhdr Q\n",
  1656. amt >> PAGE_SHIFT, pd->port_rcvhdrq,
  1657. (unsigned long) pd->port_rcvhdrq_phys,
  1658. (unsigned long) pd->port_rcvhdrq_size,
  1659. pd->port_port);
  1660. }
  1661. else
  1662. ipath_cdbg(VERBOSE, "reuse port %d rcvhdrq @%p %llx phys; "
  1663. "hdrtailaddr@%p %llx physical\n",
  1664. pd->port_port, pd->port_rcvhdrq,
  1665. (unsigned long long) pd->port_rcvhdrq_phys,
  1666. pd->port_rcvhdrtail_kvaddr, (unsigned long long)
  1667. pd->port_rcvhdrqtailaddr_phys);
  1668. /* clear for security and sanity on each use */
  1669. memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
  1670. if (pd->port_rcvhdrtail_kvaddr)
  1671. memset(pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
  1672. /*
  1673. * tell chip each time we init it, even if we are re-using previous
  1674. * memory (we zero the register at process close)
  1675. */
  1676. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  1677. pd->port_port, pd->port_rcvhdrqtailaddr_phys);
  1678. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1679. pd->port_port, pd->port_rcvhdrq_phys);
  1680. bail:
  1681. return ret;
  1682. }
  1683. /*
  1684. * Flush all sends that might be in the ready to send state, as well as any
  1685. * that are in the process of being sent. Used whenever we need to be
  1686. * sure the send side is idle. Cleans up all buffer state by canceling
  1687. * all pio buffers, and issuing an abort, which cleans up anything in the
  1688. * launch fifo. The cancel is superfluous on some chip versions, but
  1689. * it's safer to always do it.
  1690. * PIOAvail bits are updated by the chip as if normal send had happened.
  1691. */
  1692. void ipath_cancel_sends(struct ipath_devdata *dd, int restore_sendctrl)
  1693. {
  1694. unsigned long flags;
  1695. if (dd->ipath_flags & IPATH_IB_AUTONEG_INPROG) {
  1696. ipath_cdbg(VERBOSE, "Ignore while in autonegotiation\n");
  1697. goto bail;
  1698. }
  1699. /*
  1700. * If we have SDMA, and it's not disabled, we have to kick off the
  1701. * abort state machine, provided we aren't already aborting.
  1702. * If we are in the process of aborting SDMA (!DISABLED, but ABORTING),
  1703. * we skip the rest of this routine. It is already "in progress"
  1704. */
  1705. if (dd->ipath_flags & IPATH_HAS_SEND_DMA) {
  1706. int skip_cancel;
  1707. unsigned long *statp = &dd->ipath_sdma_status;
  1708. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  1709. skip_cancel =
  1710. test_and_set_bit(IPATH_SDMA_ABORTING, statp)
  1711. && !test_bit(IPATH_SDMA_DISABLED, statp);
  1712. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  1713. if (skip_cancel)
  1714. goto bail;
  1715. }
  1716. ipath_dbg("Cancelling all in-progress send buffers\n");
  1717. /* skip armlaunch errs for a while */
  1718. dd->ipath_lastcancel = jiffies + HZ / 2;
  1719. /*
  1720. * The abort bit is auto-clearing. We also don't want pioavail
  1721. * update happening during this, and we don't want any other
  1722. * sends going out, so turn those off for the duration. We read
  1723. * the scratch register to be sure that cancels and the abort
  1724. * have taken effect in the chip. Otherwise two parts are same
  1725. * as ipath_force_pio_avail_update()
  1726. */
  1727. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1728. dd->ipath_sendctrl &= ~(INFINIPATH_S_PIOBUFAVAILUPD
  1729. | INFINIPATH_S_PIOENABLE);
  1730. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1731. dd->ipath_sendctrl | INFINIPATH_S_ABORT);
  1732. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1733. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1734. /* disarm all send buffers */
  1735. ipath_disarm_piobufs(dd, 0,
  1736. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k);
  1737. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  1738. set_bit(IPATH_SDMA_DISARMED, &dd->ipath_sdma_status);
  1739. if (restore_sendctrl) {
  1740. /* else done by caller later if needed */
  1741. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1742. dd->ipath_sendctrl |= INFINIPATH_S_PIOBUFAVAILUPD |
  1743. INFINIPATH_S_PIOENABLE;
  1744. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1745. dd->ipath_sendctrl);
  1746. /* and again, be sure all have hit the chip */
  1747. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1748. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1749. }
  1750. if ((dd->ipath_flags & IPATH_HAS_SEND_DMA) &&
  1751. !test_bit(IPATH_SDMA_DISABLED, &dd->ipath_sdma_status) &&
  1752. test_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status)) {
  1753. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  1754. /* only wait so long for intr */
  1755. dd->ipath_sdma_abort_intr_timeout = jiffies + HZ;
  1756. dd->ipath_sdma_reset_wait = 200;
  1757. if (!test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  1758. tasklet_hi_schedule(&dd->ipath_sdma_abort_task);
  1759. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  1760. }
  1761. bail:;
  1762. }
  1763. /*
  1764. * Force an update of in-memory copy of the pioavail registers, when
  1765. * needed for any of a variety of reasons. We read the scratch register
  1766. * to make it highly likely that the update will have happened by the
  1767. * time we return. If already off (as in cancel_sends above), this
  1768. * routine is a nop, on the assumption that the caller will "do the
  1769. * right thing".
  1770. */
  1771. void ipath_force_pio_avail_update(struct ipath_devdata *dd)
  1772. {
  1773. unsigned long flags;
  1774. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  1775. if (dd->ipath_sendctrl & INFINIPATH_S_PIOBUFAVAILUPD) {
  1776. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1777. dd->ipath_sendctrl & ~INFINIPATH_S_PIOBUFAVAILUPD);
  1778. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1779. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  1780. dd->ipath_sendctrl);
  1781. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1782. }
  1783. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  1784. }
  1785. static void ipath_set_ib_lstate(struct ipath_devdata *dd, int linkcmd,
  1786. int linitcmd)
  1787. {
  1788. u64 mod_wd;
  1789. static const char *what[4] = {
  1790. [0] = "NOP",
  1791. [INFINIPATH_IBCC_LINKCMD_DOWN] = "DOWN",
  1792. [INFINIPATH_IBCC_LINKCMD_ARMED] = "ARMED",
  1793. [INFINIPATH_IBCC_LINKCMD_ACTIVE] = "ACTIVE"
  1794. };
  1795. if (linitcmd == INFINIPATH_IBCC_LINKINITCMD_DISABLE) {
  1796. /*
  1797. * If we are told to disable, note that so link-recovery
  1798. * code does not attempt to bring us back up.
  1799. */
  1800. preempt_disable();
  1801. dd->ipath_flags |= IPATH_IB_LINK_DISABLED;
  1802. preempt_enable();
  1803. } else if (linitcmd) {
  1804. /*
  1805. * Any other linkinitcmd will lead to LINKDOWN and then
  1806. * to INIT (if all is well), so clear flag to let
  1807. * link-recovery code attempt to bring us back up.
  1808. */
  1809. preempt_disable();
  1810. dd->ipath_flags &= ~IPATH_IB_LINK_DISABLED;
  1811. preempt_enable();
  1812. }
  1813. mod_wd = (linkcmd << dd->ibcc_lc_shift) |
  1814. (linitcmd << INFINIPATH_IBCC_LINKINITCMD_SHIFT);
  1815. ipath_cdbg(VERBOSE,
  1816. "Moving unit %u to %s (initcmd=0x%x), current ltstate is %s\n",
  1817. dd->ipath_unit, what[linkcmd], linitcmd,
  1818. ipath_ibcstatus_str[ipath_ib_linktrstate(dd,
  1819. ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus))]);
  1820. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1821. dd->ipath_ibcctrl | mod_wd);
  1822. /* read from chip so write is flushed */
  1823. (void) ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  1824. }
  1825. int ipath_set_linkstate(struct ipath_devdata *dd, u8 newstate)
  1826. {
  1827. u32 lstate;
  1828. int ret;
  1829. switch (newstate) {
  1830. case IPATH_IB_LINKDOWN_ONLY:
  1831. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN, 0);
  1832. /* don't wait */
  1833. ret = 0;
  1834. goto bail;
  1835. case IPATH_IB_LINKDOWN:
  1836. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1837. INFINIPATH_IBCC_LINKINITCMD_POLL);
  1838. /* don't wait */
  1839. ret = 0;
  1840. goto bail;
  1841. case IPATH_IB_LINKDOWN_SLEEP:
  1842. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1843. INFINIPATH_IBCC_LINKINITCMD_SLEEP);
  1844. /* don't wait */
  1845. ret = 0;
  1846. goto bail;
  1847. case IPATH_IB_LINKDOWN_DISABLE:
  1848. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_DOWN,
  1849. INFINIPATH_IBCC_LINKINITCMD_DISABLE);
  1850. /* don't wait */
  1851. ret = 0;
  1852. goto bail;
  1853. case IPATH_IB_LINKARM:
  1854. if (dd->ipath_flags & IPATH_LINKARMED) {
  1855. ret = 0;
  1856. goto bail;
  1857. }
  1858. if (!(dd->ipath_flags &
  1859. (IPATH_LINKINIT | IPATH_LINKACTIVE))) {
  1860. ret = -EINVAL;
  1861. goto bail;
  1862. }
  1863. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ARMED, 0);
  1864. /*
  1865. * Since the port can transition to ACTIVE by receiving
  1866. * a non VL 15 packet, wait for either state.
  1867. */
  1868. lstate = IPATH_LINKARMED | IPATH_LINKACTIVE;
  1869. break;
  1870. case IPATH_IB_LINKACTIVE:
  1871. if (dd->ipath_flags & IPATH_LINKACTIVE) {
  1872. ret = 0;
  1873. goto bail;
  1874. }
  1875. if (!(dd->ipath_flags & IPATH_LINKARMED)) {
  1876. ret = -EINVAL;
  1877. goto bail;
  1878. }
  1879. ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ACTIVE, 0);
  1880. lstate = IPATH_LINKACTIVE;
  1881. break;
  1882. case IPATH_IB_LINK_LOOPBACK:
  1883. dev_info(&dd->pcidev->dev, "Enabling IB local loopback\n");
  1884. dd->ipath_ibcctrl |= INFINIPATH_IBCC_LOOPBACK;
  1885. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1886. dd->ipath_ibcctrl);
  1887. /* turn heartbeat off, as it causes loopback to fail */
  1888. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1889. IPATH_IB_HRTBT_OFF);
  1890. /* don't wait */
  1891. ret = 0;
  1892. goto bail;
  1893. case IPATH_IB_LINK_EXTERNAL:
  1894. dev_info(&dd->pcidev->dev,
  1895. "Disabling IB local loopback (normal)\n");
  1896. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1897. IPATH_IB_HRTBT_ON);
  1898. dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LOOPBACK;
  1899. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1900. dd->ipath_ibcctrl);
  1901. /* don't wait */
  1902. ret = 0;
  1903. goto bail;
  1904. /*
  1905. * Heartbeat can be explicitly enabled by the user via
  1906. * "hrtbt_enable" "file", and if disabled, trying to enable here
  1907. * will have no effect. Implicit changes (heartbeat off when
  1908. * loopback on, and vice versa) are included to ease testing.
  1909. */
  1910. case IPATH_IB_LINK_HRTBT:
  1911. ret = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1912. IPATH_IB_HRTBT_ON);
  1913. goto bail;
  1914. case IPATH_IB_LINK_NO_HRTBT:
  1915. ret = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT,
  1916. IPATH_IB_HRTBT_OFF);
  1917. goto bail;
  1918. default:
  1919. ipath_dbg("Invalid linkstate 0x%x requested\n", newstate);
  1920. ret = -EINVAL;
  1921. goto bail;
  1922. }
  1923. ret = ipath_wait_linkstate(dd, lstate, 2000);
  1924. bail:
  1925. return ret;
  1926. }
  1927. /**
  1928. * ipath_set_mtu - set the MTU
  1929. * @dd: the infinipath device
  1930. * @arg: the new MTU
  1931. *
  1932. * we can handle "any" incoming size, the issue here is whether we
  1933. * need to restrict our outgoing size. For now, we don't do any
  1934. * sanity checking on this, and we don't deal with what happens to
  1935. * programs that are already running when the size changes.
  1936. * NOTE: changing the MTU will usually cause the IBC to go back to
  1937. * link INIT state...
  1938. */
  1939. int ipath_set_mtu(struct ipath_devdata *dd, u16 arg)
  1940. {
  1941. u32 piosize;
  1942. int changed = 0;
  1943. int ret;
  1944. /*
  1945. * mtu is IB data payload max. It's the largest power of 2 less
  1946. * than piosize (or even larger, since it only really controls the
  1947. * largest we can receive; we can send the max of the mtu and
  1948. * piosize). We check that it's one of the valid IB sizes.
  1949. */
  1950. if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
  1951. (arg != 4096 || !ipath_mtu4096)) {
  1952. ipath_dbg("Trying to set invalid mtu %u, failing\n", arg);
  1953. ret = -EINVAL;
  1954. goto bail;
  1955. }
  1956. if (dd->ipath_ibmtu == arg) {
  1957. ret = 0; /* same as current */
  1958. goto bail;
  1959. }
  1960. piosize = dd->ipath_ibmaxlen;
  1961. dd->ipath_ibmtu = arg;
  1962. if (arg >= (piosize - IPATH_PIO_MAXIBHDR)) {
  1963. /* Only if it's not the initial value (or reset to it) */
  1964. if (piosize != dd->ipath_init_ibmaxlen) {
  1965. if (arg > piosize && arg <= dd->ipath_init_ibmaxlen)
  1966. piosize = dd->ipath_init_ibmaxlen;
  1967. dd->ipath_ibmaxlen = piosize;
  1968. changed = 1;
  1969. }
  1970. } else if ((arg + IPATH_PIO_MAXIBHDR) != dd->ipath_ibmaxlen) {
  1971. piosize = arg + IPATH_PIO_MAXIBHDR;
  1972. ipath_cdbg(VERBOSE, "ibmaxlen was 0x%x, setting to 0x%x "
  1973. "(mtu 0x%x)\n", dd->ipath_ibmaxlen, piosize,
  1974. arg);
  1975. dd->ipath_ibmaxlen = piosize;
  1976. changed = 1;
  1977. }
  1978. if (changed) {
  1979. u64 ibc = dd->ipath_ibcctrl, ibdw;
  1980. /*
  1981. * update our housekeeping variables, and set IBC max
  1982. * size, same as init code; max IBC is max we allow in
  1983. * buffer, less the qword pbc, plus 1 for ICRC, in dwords
  1984. */
  1985. dd->ipath_ibmaxlen = piosize - 2 * sizeof(u32);
  1986. ibdw = (dd->ipath_ibmaxlen >> 2) + 1;
  1987. ibc &= ~(INFINIPATH_IBCC_MAXPKTLEN_MASK <<
  1988. dd->ibcc_mpl_shift);
  1989. ibc |= ibdw << dd->ibcc_mpl_shift;
  1990. dd->ipath_ibcctrl = ibc;
  1991. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  1992. dd->ipath_ibcctrl);
  1993. dd->ipath_f_tidtemplate(dd);
  1994. }
  1995. ret = 0;
  1996. bail:
  1997. return ret;
  1998. }
  1999. int ipath_set_lid(struct ipath_devdata *dd, u32 lid, u8 lmc)
  2000. {
  2001. dd->ipath_lid = lid;
  2002. dd->ipath_lmc = lmc;
  2003. dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LIDLMC, lid |
  2004. (~((1U << lmc) - 1)) << 16);
  2005. dev_info(&dd->pcidev->dev, "We got a lid: 0x%x\n", lid);
  2006. return 0;
  2007. }
  2008. /**
  2009. * ipath_write_kreg_port - write a device's per-port 64-bit kernel register
  2010. * @dd: the infinipath device
  2011. * @regno: the register number to write
  2012. * @port: the port containing the register
  2013. * @value: the value to write
  2014. *
  2015. * Registers that vary with the chip implementation constants (port)
  2016. * use this routine.
  2017. */
  2018. void ipath_write_kreg_port(const struct ipath_devdata *dd, ipath_kreg regno,
  2019. unsigned port, u64 value)
  2020. {
  2021. u16 where;
  2022. if (port < dd->ipath_portcnt &&
  2023. (regno == dd->ipath_kregs->kr_rcvhdraddr ||
  2024. regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
  2025. where = regno + port;
  2026. else
  2027. where = -1;
  2028. ipath_write_kreg(dd, where, value);
  2029. }
  2030. /*
  2031. * Following deal with the "obviously simple" task of overriding the state
  2032. * of the LEDS, which normally indicate link physical and logical status.
  2033. * The complications arise in dealing with different hardware mappings
  2034. * and the board-dependent routine being called from interrupts.
  2035. * and then there's the requirement to _flash_ them.
  2036. */
  2037. #define LED_OVER_FREQ_SHIFT 8
  2038. #define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
  2039. /* Below is "non-zero" to force override, but both actual LEDs are off */
  2040. #define LED_OVER_BOTH_OFF (8)
  2041. static void ipath_run_led_override(unsigned long opaque)
  2042. {
  2043. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  2044. int timeoff;
  2045. int pidx;
  2046. u64 lstate, ltstate, val;
  2047. if (!(dd->ipath_flags & IPATH_INITTED))
  2048. return;
  2049. pidx = dd->ipath_led_override_phase++ & 1;
  2050. dd->ipath_led_override = dd->ipath_led_override_vals[pidx];
  2051. timeoff = dd->ipath_led_override_timeoff;
  2052. /*
  2053. * below potentially restores the LED values per current status,
  2054. * should also possibly setup the traffic-blink register,
  2055. * but leave that to per-chip functions.
  2056. */
  2057. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
  2058. ltstate = ipath_ib_linktrstate(dd, val);
  2059. lstate = ipath_ib_linkstate(dd, val);
  2060. dd->ipath_f_setextled(dd, lstate, ltstate);
  2061. mod_timer(&dd->ipath_led_override_timer, jiffies + timeoff);
  2062. }
  2063. void ipath_set_led_override(struct ipath_devdata *dd, unsigned int val)
  2064. {
  2065. int timeoff, freq;
  2066. if (!(dd->ipath_flags & IPATH_INITTED))
  2067. return;
  2068. /* First check if we are blinking. If not, use 1HZ polling */
  2069. timeoff = HZ;
  2070. freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
  2071. if (freq) {
  2072. /* For blink, set each phase from one nybble of val */
  2073. dd->ipath_led_override_vals[0] = val & 0xF;
  2074. dd->ipath_led_override_vals[1] = (val >> 4) & 0xF;
  2075. timeoff = (HZ << 4)/freq;
  2076. } else {
  2077. /* Non-blink set both phases the same. */
  2078. dd->ipath_led_override_vals[0] = val & 0xF;
  2079. dd->ipath_led_override_vals[1] = val & 0xF;
  2080. }
  2081. dd->ipath_led_override_timeoff = timeoff;
  2082. /*
  2083. * If the timer has not already been started, do so. Use a "quick"
  2084. * timeout so the function will be called soon, to look at our request.
  2085. */
  2086. if (atomic_inc_return(&dd->ipath_led_override_timer_active) == 1) {
  2087. /* Need to start timer */
  2088. init_timer(&dd->ipath_led_override_timer);
  2089. dd->ipath_led_override_timer.function =
  2090. ipath_run_led_override;
  2091. dd->ipath_led_override_timer.data = (unsigned long) dd;
  2092. dd->ipath_led_override_timer.expires = jiffies + 1;
  2093. add_timer(&dd->ipath_led_override_timer);
  2094. } else
  2095. atomic_dec(&dd->ipath_led_override_timer_active);
  2096. }
  2097. /**
  2098. * ipath_shutdown_device - shut down a device
  2099. * @dd: the infinipath device
  2100. *
  2101. * This is called to make the device quiet when we are about to
  2102. * unload the driver, and also when the device is administratively
  2103. * disabled. It does not free any data structures.
  2104. * Everything it does has to be setup again by ipath_init_chip(dd,1)
  2105. */
  2106. void ipath_shutdown_device(struct ipath_devdata *dd)
  2107. {
  2108. unsigned long flags;
  2109. ipath_dbg("Shutting down the device\n");
  2110. ipath_hol_up(dd); /* make sure user processes aren't suspended */
  2111. dd->ipath_flags |= IPATH_LINKUNK;
  2112. dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
  2113. IPATH_LINKINIT | IPATH_LINKARMED |
  2114. IPATH_LINKACTIVE);
  2115. *dd->ipath_statusp &= ~(IPATH_STATUS_IB_CONF |
  2116. IPATH_STATUS_IB_READY);
  2117. /* mask interrupts, but not errors */
  2118. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
  2119. dd->ipath_rcvctrl = 0;
  2120. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  2121. dd->ipath_rcvctrl);
  2122. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  2123. teardown_sdma(dd);
  2124. /*
  2125. * gracefully stop all sends allowing any in progress to trickle out
  2126. * first.
  2127. */
  2128. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  2129. dd->ipath_sendctrl = 0;
  2130. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  2131. /* flush it */
  2132. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  2133. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  2134. /*
  2135. * enough for anything that's going to trickle out to have actually
  2136. * done so.
  2137. */
  2138. udelay(5);
  2139. dd->ipath_f_setextled(dd, 0, 0); /* make sure LEDs are off */
  2140. ipath_set_ib_lstate(dd, 0, INFINIPATH_IBCC_LINKINITCMD_DISABLE);
  2141. ipath_cancel_sends(dd, 0);
  2142. /*
  2143. * we are shutting down, so tell components that care. We don't do
  2144. * this on just a link state change, much like ethernet, a cable
  2145. * unplug, etc. doesn't change driver state
  2146. */
  2147. signal_ib_event(dd, IB_EVENT_PORT_ERR);
  2148. /* disable IBC */
  2149. dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
  2150. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  2151. dd->ipath_control | INFINIPATH_C_FREEZEMODE);
  2152. /*
  2153. * clear SerdesEnable and turn the leds off; do this here because
  2154. * we are unloading, so don't count on interrupts to move along
  2155. * Turn the LEDs off explictly for the same reason.
  2156. */
  2157. dd->ipath_f_quiet_serdes(dd);
  2158. /* stop all the timers that might still be running */
  2159. del_timer_sync(&dd->ipath_hol_timer);
  2160. if (dd->ipath_stats_timer_active) {
  2161. del_timer_sync(&dd->ipath_stats_timer);
  2162. dd->ipath_stats_timer_active = 0;
  2163. }
  2164. if (dd->ipath_intrchk_timer.data) {
  2165. del_timer_sync(&dd->ipath_intrchk_timer);
  2166. dd->ipath_intrchk_timer.data = 0;
  2167. }
  2168. if (atomic_read(&dd->ipath_led_override_timer_active)) {
  2169. del_timer_sync(&dd->ipath_led_override_timer);
  2170. atomic_set(&dd->ipath_led_override_timer_active, 0);
  2171. }
  2172. /*
  2173. * clear all interrupts and errors, so that the next time the driver
  2174. * is loaded or device is enabled, we know that whatever is set
  2175. * happened while we were unloaded
  2176. */
  2177. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
  2178. ~0ULL & ~INFINIPATH_HWE_MEMBISTFAILED);
  2179. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
  2180. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
  2181. ipath_cdbg(VERBOSE, "Flush time and errors to EEPROM\n");
  2182. ipath_update_eeprom_log(dd);
  2183. }
  2184. /**
  2185. * ipath_free_pddata - free a port's allocated data
  2186. * @dd: the infinipath device
  2187. * @pd: the portdata structure
  2188. *
  2189. * free up any allocated data for a port
  2190. * This should not touch anything that would affect a simultaneous
  2191. * re-allocation of port data, because it is called after ipath_mutex
  2192. * is released (and can be called from reinit as well).
  2193. * It should never change any chip state, or global driver state.
  2194. * (The only exception to global state is freeing the port0 port0_skbs.)
  2195. */
  2196. void ipath_free_pddata(struct ipath_devdata *dd, struct ipath_portdata *pd)
  2197. {
  2198. if (!pd)
  2199. return;
  2200. if (pd->port_rcvhdrq) {
  2201. ipath_cdbg(VERBOSE, "free closed port %d rcvhdrq @ %p "
  2202. "(size=%lu)\n", pd->port_port, pd->port_rcvhdrq,
  2203. (unsigned long) pd->port_rcvhdrq_size);
  2204. dma_free_coherent(&dd->pcidev->dev, pd->port_rcvhdrq_size,
  2205. pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
  2206. pd->port_rcvhdrq = NULL;
  2207. if (pd->port_rcvhdrtail_kvaddr) {
  2208. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  2209. pd->port_rcvhdrtail_kvaddr,
  2210. pd->port_rcvhdrqtailaddr_phys);
  2211. pd->port_rcvhdrtail_kvaddr = NULL;
  2212. }
  2213. }
  2214. if (pd->port_port && pd->port_rcvegrbuf) {
  2215. unsigned e;
  2216. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  2217. void *base = pd->port_rcvegrbuf[e];
  2218. size_t size = pd->port_rcvegrbuf_size;
  2219. ipath_cdbg(VERBOSE, "egrbuf free(%p, %lu), "
  2220. "chunk %u/%u\n", base,
  2221. (unsigned long) size,
  2222. e, pd->port_rcvegrbuf_chunks);
  2223. dma_free_coherent(&dd->pcidev->dev, size,
  2224. base, pd->port_rcvegrbuf_phys[e]);
  2225. }
  2226. kfree(pd->port_rcvegrbuf);
  2227. pd->port_rcvegrbuf = NULL;
  2228. kfree(pd->port_rcvegrbuf_phys);
  2229. pd->port_rcvegrbuf_phys = NULL;
  2230. pd->port_rcvegrbuf_chunks = 0;
  2231. } else if (pd->port_port == 0 && dd->ipath_port0_skbinfo) {
  2232. unsigned e;
  2233. struct ipath_skbinfo *skbinfo = dd->ipath_port0_skbinfo;
  2234. dd->ipath_port0_skbinfo = NULL;
  2235. ipath_cdbg(VERBOSE, "free closed port %d "
  2236. "ipath_port0_skbinfo @ %p\n", pd->port_port,
  2237. skbinfo);
  2238. for (e = 0; e < dd->ipath_p0_rcvegrcnt; e++)
  2239. if (skbinfo[e].skb) {
  2240. pci_unmap_single(dd->pcidev, skbinfo[e].phys,
  2241. dd->ipath_ibmaxlen,
  2242. PCI_DMA_FROMDEVICE);
  2243. dev_kfree_skb(skbinfo[e].skb);
  2244. }
  2245. vfree(skbinfo);
  2246. }
  2247. kfree(pd->port_tid_pg_list);
  2248. vfree(pd->subport_uregbase);
  2249. vfree(pd->subport_rcvegrbuf);
  2250. vfree(pd->subport_rcvhdr_base);
  2251. kfree(pd);
  2252. }
  2253. static int __init infinipath_init(void)
  2254. {
  2255. int ret;
  2256. if (ipath_debug & __IPATH_DBG)
  2257. printk(KERN_INFO DRIVER_LOAD_MSG "%s", ib_ipath_version);
  2258. /*
  2259. * These must be called before the driver is registered with
  2260. * the PCI subsystem.
  2261. */
  2262. idr_init(&unit_table);
  2263. if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
  2264. printk(KERN_ERR IPATH_DRV_NAME ": idr_pre_get() failed\n");
  2265. ret = -ENOMEM;
  2266. goto bail;
  2267. }
  2268. ret = pci_register_driver(&ipath_driver);
  2269. if (ret < 0) {
  2270. printk(KERN_ERR IPATH_DRV_NAME
  2271. ": Unable to register driver: error %d\n", -ret);
  2272. goto bail_unit;
  2273. }
  2274. ret = ipath_init_ipathfs();
  2275. if (ret < 0) {
  2276. printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
  2277. "ipathfs: error %d\n", -ret);
  2278. goto bail_pci;
  2279. }
  2280. goto bail;
  2281. bail_pci:
  2282. pci_unregister_driver(&ipath_driver);
  2283. bail_unit:
  2284. idr_destroy(&unit_table);
  2285. bail:
  2286. return ret;
  2287. }
  2288. static void __exit infinipath_cleanup(void)
  2289. {
  2290. ipath_exit_ipathfs();
  2291. ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
  2292. pci_unregister_driver(&ipath_driver);
  2293. idr_destroy(&unit_table);
  2294. }
  2295. /**
  2296. * ipath_reset_device - reset the chip if possible
  2297. * @unit: the device to reset
  2298. *
  2299. * Whether or not reset is successful, we attempt to re-initialize the chip
  2300. * (that is, much like a driver unload/reload). We clear the INITTED flag
  2301. * so that the various entry points will fail until we reinitialize. For
  2302. * now, we only allow this if no user ports are open that use chip resources
  2303. */
  2304. int ipath_reset_device(int unit)
  2305. {
  2306. int ret, i;
  2307. struct ipath_devdata *dd = ipath_lookup(unit);
  2308. if (!dd) {
  2309. ret = -ENODEV;
  2310. goto bail;
  2311. }
  2312. if (atomic_read(&dd->ipath_led_override_timer_active)) {
  2313. /* Need to stop LED timer, _then_ shut off LEDs */
  2314. del_timer_sync(&dd->ipath_led_override_timer);
  2315. atomic_set(&dd->ipath_led_override_timer_active, 0);
  2316. }
  2317. /* Shut off LEDs after we are sure timer is not running */
  2318. dd->ipath_led_override = LED_OVER_BOTH_OFF;
  2319. dd->ipath_f_setextled(dd, 0, 0);
  2320. dev_info(&dd->pcidev->dev, "Reset on unit %u requested\n", unit);
  2321. if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT)) {
  2322. dev_info(&dd->pcidev->dev, "Invalid unit number %u or "
  2323. "not initialized or not present\n", unit);
  2324. ret = -ENXIO;
  2325. goto bail;
  2326. }
  2327. if (dd->ipath_pd)
  2328. for (i = 1; i < dd->ipath_cfgports; i++) {
  2329. if (dd->ipath_pd[i] && dd->ipath_pd[i]->port_cnt) {
  2330. ipath_dbg("unit %u port %d is in use "
  2331. "(PID %u cmd %s), can't reset\n",
  2332. unit, i,
  2333. pid_nr(dd->ipath_pd[i]->port_pid),
  2334. dd->ipath_pd[i]->port_comm);
  2335. ret = -EBUSY;
  2336. goto bail;
  2337. }
  2338. }
  2339. if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  2340. teardown_sdma(dd);
  2341. dd->ipath_flags &= ~IPATH_INITTED;
  2342. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
  2343. ret = dd->ipath_f_reset(dd);
  2344. if (ret == 1) {
  2345. ipath_dbg("Reinitializing unit %u after reset attempt\n",
  2346. unit);
  2347. ret = ipath_init_chip(dd, 1);
  2348. } else
  2349. ret = -EAGAIN;
  2350. if (ret)
  2351. ipath_dev_err(dd, "Reinitialize unit %u after "
  2352. "reset failed with %d\n", unit, ret);
  2353. else
  2354. dev_info(&dd->pcidev->dev, "Reinitialized unit %u after "
  2355. "resetting\n", unit);
  2356. bail:
  2357. return ret;
  2358. }
  2359. /*
  2360. * send a signal to all the processes that have the driver open
  2361. * through the normal interfaces (i.e., everything other than diags
  2362. * interface). Returns number of signalled processes.
  2363. */
  2364. static int ipath_signal_procs(struct ipath_devdata *dd, int sig)
  2365. {
  2366. int i, sub, any = 0;
  2367. struct pid *pid;
  2368. if (!dd->ipath_pd)
  2369. return 0;
  2370. for (i = 1; i < dd->ipath_cfgports; i++) {
  2371. if (!dd->ipath_pd[i] || !dd->ipath_pd[i]->port_cnt)
  2372. continue;
  2373. pid = dd->ipath_pd[i]->port_pid;
  2374. if (!pid)
  2375. continue;
  2376. dev_info(&dd->pcidev->dev, "context %d in use "
  2377. "(PID %u), sending signal %d\n",
  2378. i, pid_nr(pid), sig);
  2379. kill_pid(pid, sig, 1);
  2380. any++;
  2381. for (sub = 0; sub < INFINIPATH_MAX_SUBPORT; sub++) {
  2382. pid = dd->ipath_pd[i]->port_subpid[sub];
  2383. if (!pid)
  2384. continue;
  2385. dev_info(&dd->pcidev->dev, "sub-context "
  2386. "%d:%d in use (PID %u), sending "
  2387. "signal %d\n", i, sub, pid_nr(pid), sig);
  2388. kill_pid(pid, sig, 1);
  2389. any++;
  2390. }
  2391. }
  2392. return any;
  2393. }
  2394. static void ipath_hol_signal_down(struct ipath_devdata *dd)
  2395. {
  2396. if (ipath_signal_procs(dd, SIGSTOP))
  2397. ipath_dbg("Stopped some processes\n");
  2398. ipath_cancel_sends(dd, 1);
  2399. }
  2400. static void ipath_hol_signal_up(struct ipath_devdata *dd)
  2401. {
  2402. if (ipath_signal_procs(dd, SIGCONT))
  2403. ipath_dbg("Continued some processes\n");
  2404. }
  2405. /*
  2406. * link is down, stop any users processes, and flush pending sends
  2407. * to prevent HoL blocking, then start the HoL timer that
  2408. * periodically continues, then stop procs, so they can detect
  2409. * link down if they want, and do something about it.
  2410. * Timer may already be running, so use __mod_timer, not add_timer.
  2411. */
  2412. void ipath_hol_down(struct ipath_devdata *dd)
  2413. {
  2414. dd->ipath_hol_state = IPATH_HOL_DOWN;
  2415. ipath_hol_signal_down(dd);
  2416. dd->ipath_hol_next = IPATH_HOL_DOWNCONT;
  2417. dd->ipath_hol_timer.expires = jiffies +
  2418. msecs_to_jiffies(ipath_hol_timeout_ms);
  2419. __mod_timer(&dd->ipath_hol_timer, dd->ipath_hol_timer.expires);
  2420. }
  2421. /*
  2422. * link is up, continue any user processes, and ensure timer
  2423. * is a nop, if running. Let timer keep running, if set; it
  2424. * will nop when it sees the link is up
  2425. */
  2426. void ipath_hol_up(struct ipath_devdata *dd)
  2427. {
  2428. ipath_hol_signal_up(dd);
  2429. dd->ipath_hol_state = IPATH_HOL_UP;
  2430. }
  2431. /*
  2432. * toggle the running/not running state of user proceses
  2433. * to prevent HoL blocking on chip resources, but still allow
  2434. * user processes to do link down special case handling.
  2435. * Should only be called via the timer
  2436. */
  2437. void ipath_hol_event(unsigned long opaque)
  2438. {
  2439. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  2440. if (dd->ipath_hol_next == IPATH_HOL_DOWNSTOP
  2441. && dd->ipath_hol_state != IPATH_HOL_UP) {
  2442. dd->ipath_hol_next = IPATH_HOL_DOWNCONT;
  2443. ipath_dbg("Stopping processes\n");
  2444. ipath_hol_signal_down(dd);
  2445. } else { /* may do "extra" if also in ipath_hol_up() */
  2446. dd->ipath_hol_next = IPATH_HOL_DOWNSTOP;
  2447. ipath_dbg("Continuing processes\n");
  2448. ipath_hol_signal_up(dd);
  2449. }
  2450. if (dd->ipath_hol_state == IPATH_HOL_UP)
  2451. ipath_dbg("link's up, don't resched timer\n");
  2452. else {
  2453. dd->ipath_hol_timer.expires = jiffies +
  2454. msecs_to_jiffies(ipath_hol_timeout_ms);
  2455. __mod_timer(&dd->ipath_hol_timer,
  2456. dd->ipath_hol_timer.expires);
  2457. }
  2458. }
  2459. int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv)
  2460. {
  2461. u64 val;
  2462. if (new_pol_inv > INFINIPATH_XGXS_RX_POL_MASK)
  2463. return -1;
  2464. if (dd->ipath_rx_pol_inv != new_pol_inv) {
  2465. dd->ipath_rx_pol_inv = new_pol_inv;
  2466. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
  2467. val &= ~(INFINIPATH_XGXS_RX_POL_MASK <<
  2468. INFINIPATH_XGXS_RX_POL_SHIFT);
  2469. val |= ((u64)dd->ipath_rx_pol_inv) <<
  2470. INFINIPATH_XGXS_RX_POL_SHIFT;
  2471. ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, val);
  2472. }
  2473. return 0;
  2474. }
  2475. /*
  2476. * Disable and enable the armlaunch error. Used for PIO bandwidth testing on
  2477. * the 7220, which is count-based, rather than trigger-based. Safe for the
  2478. * driver check, since it's at init. Not completely safe when used for
  2479. * user-mode checking, since some error checking can be lost, but not
  2480. * particularly risky, and only has problematic side-effects in the face of
  2481. * very buggy user code. There is no reference counting, but that's also
  2482. * fine, given the intended use.
  2483. */
  2484. void ipath_enable_armlaunch(struct ipath_devdata *dd)
  2485. {
  2486. dd->ipath_lasterror &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2487. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear,
  2488. INFINIPATH_E_SPIOARMLAUNCH);
  2489. dd->ipath_errormask |= INFINIPATH_E_SPIOARMLAUNCH;
  2490. ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
  2491. dd->ipath_errormask);
  2492. }
  2493. void ipath_disable_armlaunch(struct ipath_devdata *dd)
  2494. {
  2495. /* so don't re-enable if already set */
  2496. dd->ipath_maskederrs &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2497. dd->ipath_errormask &= ~INFINIPATH_E_SPIOARMLAUNCH;
  2498. ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
  2499. dd->ipath_errormask);
  2500. }
  2501. module_init(infinipath_init);
  2502. module_exit(infinipath_cleanup);