ipath_driver.c 82 KB

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