card_base.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@de.ibm.com>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * Module initialization and PCIe setup. Card health monitoring and
  22. * recovery functionality. Character device creation and deletion are
  23. * controlled from here.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/pci.h>
  28. #include <linux/err.h>
  29. #include <linux/aer.h>
  30. #include <linux/string.h>
  31. #include <linux/sched.h>
  32. #include <linux/wait.h>
  33. #include <linux/delay.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/module.h>
  36. #include <linux/notifier.h>
  37. #include <linux/device.h>
  38. #include <linux/log2.h>
  39. #include <linux/genwqe/genwqe_card.h>
  40. #include "card_base.h"
  41. #include "card_ddcb.h"
  42. MODULE_AUTHOR("Frank Haverkamp <haver@linux.vnet.ibm.com>");
  43. MODULE_AUTHOR("Michael Ruettger <michael@ibmra.de>");
  44. MODULE_AUTHOR("Joerg-Stephan Vogt <jsvogt@de.ibm.com>");
  45. MODULE_AUTHOR("Michal Jung <mijung@de.ibm.com>");
  46. MODULE_DESCRIPTION("GenWQE Card");
  47. MODULE_VERSION(DRV_VERS_STRING);
  48. MODULE_LICENSE("GPL");
  49. static char genwqe_driver_name[] = GENWQE_DEVNAME;
  50. static struct class *class_genwqe;
  51. static struct dentry *debugfs_genwqe;
  52. static struct genwqe_dev *genwqe_devices[GENWQE_CARD_NO_MAX];
  53. /* PCI structure for identifying device by PCI vendor and device ID */
  54. static DEFINE_PCI_DEVICE_TABLE(genwqe_device_table) = {
  55. { .vendor = PCI_VENDOR_ID_IBM,
  56. .device = PCI_DEVICE_GENWQE,
  57. .subvendor = PCI_SUBVENDOR_ID_IBM,
  58. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
  59. .class = (PCI_CLASSCODE_GENWQE5 << 8),
  60. .class_mask = ~0,
  61. .driver_data = 0 },
  62. /* Initial SR-IOV bring-up image */
  63. { .vendor = PCI_VENDOR_ID_IBM,
  64. .device = PCI_DEVICE_GENWQE,
  65. .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
  66. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_SRIOV,
  67. .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
  68. .class_mask = ~0,
  69. .driver_data = 0 },
  70. { .vendor = PCI_VENDOR_ID_IBM, /* VF Vendor ID */
  71. .device = 0x0000, /* VF Device ID */
  72. .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
  73. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_SRIOV,
  74. .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
  75. .class_mask = ~0,
  76. .driver_data = 0 },
  77. /* Fixed up image */
  78. { .vendor = PCI_VENDOR_ID_IBM,
  79. .device = PCI_DEVICE_GENWQE,
  80. .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
  81. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
  82. .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
  83. .class_mask = ~0,
  84. .driver_data = 0 },
  85. { .vendor = PCI_VENDOR_ID_IBM, /* VF Vendor ID */
  86. .device = 0x0000, /* VF Device ID */
  87. .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
  88. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
  89. .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
  90. .class_mask = ~0,
  91. .driver_data = 0 },
  92. /* Even one more ... */
  93. { .vendor = PCI_VENDOR_ID_IBM,
  94. .device = PCI_DEVICE_GENWQE,
  95. .subvendor = PCI_SUBVENDOR_ID_IBM,
  96. .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_NEW,
  97. .class = (PCI_CLASSCODE_GENWQE5 << 8),
  98. .class_mask = ~0,
  99. .driver_data = 0 },
  100. { 0, } /* 0 terminated list. */
  101. };
  102. MODULE_DEVICE_TABLE(pci, genwqe_device_table);
  103. /**
  104. * genwqe_dev_alloc() - Create and prepare a new card descriptor
  105. *
  106. * Return: Pointer to card descriptor, or ERR_PTR(err) on error
  107. */
  108. static struct genwqe_dev *genwqe_dev_alloc(void)
  109. {
  110. unsigned int i = 0, j;
  111. struct genwqe_dev *cd;
  112. for (i = 0; i < GENWQE_CARD_NO_MAX; i++) {
  113. if (genwqe_devices[i] == NULL)
  114. break;
  115. }
  116. if (i >= GENWQE_CARD_NO_MAX)
  117. return ERR_PTR(-ENODEV);
  118. cd = kzalloc(sizeof(struct genwqe_dev), GFP_KERNEL);
  119. if (!cd)
  120. return ERR_PTR(-ENOMEM);
  121. cd->card_idx = i;
  122. cd->class_genwqe = class_genwqe;
  123. cd->debugfs_genwqe = debugfs_genwqe;
  124. init_waitqueue_head(&cd->queue_waitq);
  125. spin_lock_init(&cd->file_lock);
  126. INIT_LIST_HEAD(&cd->file_list);
  127. cd->card_state = GENWQE_CARD_UNUSED;
  128. spin_lock_init(&cd->print_lock);
  129. cd->ddcb_software_timeout = genwqe_ddcb_software_timeout;
  130. cd->kill_timeout = genwqe_kill_timeout;
  131. for (j = 0; j < GENWQE_MAX_VFS; j++)
  132. cd->vf_jobtimeout_msec[j] = genwqe_vf_jobtimeout_msec;
  133. genwqe_devices[i] = cd;
  134. return cd;
  135. }
  136. static void genwqe_dev_free(struct genwqe_dev *cd)
  137. {
  138. if (!cd)
  139. return;
  140. genwqe_devices[cd->card_idx] = NULL;
  141. kfree(cd);
  142. }
  143. /**
  144. * genwqe_bus_reset() - Card recovery
  145. *
  146. * pci_reset_function() will recover the device and ensure that the
  147. * registers are accessible again when it completes with success. If
  148. * not, the card will stay dead and registers will be unaccessible
  149. * still.
  150. */
  151. static int genwqe_bus_reset(struct genwqe_dev *cd)
  152. {
  153. int bars, rc = 0;
  154. struct pci_dev *pci_dev = cd->pci_dev;
  155. void __iomem *mmio;
  156. if (cd->err_inject & GENWQE_INJECT_BUS_RESET_FAILURE)
  157. return -EIO;
  158. mmio = cd->mmio;
  159. cd->mmio = NULL;
  160. pci_iounmap(pci_dev, mmio);
  161. bars = pci_select_bars(pci_dev, IORESOURCE_MEM);
  162. pci_release_selected_regions(pci_dev, bars);
  163. /*
  164. * Firmware/BIOS might change memory mapping during bus reset.
  165. * Settings like enable bus-mastering, ... are backuped and
  166. * restored by the pci_reset_function().
  167. */
  168. dev_dbg(&pci_dev->dev, "[%s] pci_reset function ...\n", __func__);
  169. rc = pci_reset_function(pci_dev);
  170. if (rc) {
  171. dev_err(&pci_dev->dev,
  172. "[%s] err: failed reset func (rc %d)\n", __func__, rc);
  173. return rc;
  174. }
  175. dev_dbg(&pci_dev->dev, "[%s] done with rc=%d\n", __func__, rc);
  176. /*
  177. * Here is the right spot to clear the register read
  178. * failure. pci_bus_reset() does this job in real systems.
  179. */
  180. cd->err_inject &= ~(GENWQE_INJECT_HARDWARE_FAILURE |
  181. GENWQE_INJECT_GFIR_FATAL |
  182. GENWQE_INJECT_GFIR_INFO);
  183. rc = pci_request_selected_regions(pci_dev, bars, genwqe_driver_name);
  184. if (rc) {
  185. dev_err(&pci_dev->dev,
  186. "[%s] err: request bars failed (%d)\n", __func__, rc);
  187. return -EIO;
  188. }
  189. cd->mmio = pci_iomap(pci_dev, 0, 0);
  190. if (cd->mmio == NULL) {
  191. dev_err(&pci_dev->dev,
  192. "[%s] err: mapping BAR0 failed\n", __func__);
  193. return -ENOMEM;
  194. }
  195. return 0;
  196. }
  197. /*
  198. * Hardware circumvention section. Certain bitstreams in our test-lab
  199. * had different kinds of problems. Here is where we adjust those
  200. * bitstreams to function will with this version of our device driver.
  201. *
  202. * Thise circumventions are applied to the physical function only.
  203. * The magical numbers below are identifying development/manufacturing
  204. * versions of the bitstream used on the card.
  205. *
  206. * Turn off error reporting for old/manufacturing images.
  207. */
  208. bool genwqe_need_err_masking(struct genwqe_dev *cd)
  209. {
  210. return (cd->slu_unitcfg & 0xFFFF0ull) < 0x32170ull;
  211. }
  212. static void genwqe_tweak_hardware(struct genwqe_dev *cd)
  213. {
  214. struct pci_dev *pci_dev = cd->pci_dev;
  215. /* Mask FIRs for development images */
  216. if (((cd->slu_unitcfg & 0xFFFF0ull) >= 0x32000ull) &&
  217. ((cd->slu_unitcfg & 0xFFFF0ull) <= 0x33250ull)) {
  218. dev_warn(&pci_dev->dev,
  219. "FIRs masked due to bitstream %016llx.%016llx\n",
  220. cd->slu_unitcfg, cd->app_unitcfg);
  221. __genwqe_writeq(cd, IO_APP_SEC_LEM_DEBUG_OVR,
  222. 0xFFFFFFFFFFFFFFFFull);
  223. __genwqe_writeq(cd, IO_APP_ERR_ACT_MASK,
  224. 0x0000000000000000ull);
  225. }
  226. }
  227. /**
  228. * genwqe_recovery_on_fatal_gfir_required() - Version depended actions
  229. *
  230. * Bitstreams older than 2013-02-17 have a bug where fatal GFIRs must
  231. * be ignored. This is e.g. true for the bitstream we gave to the card
  232. * manufacturer, but also for some old bitstreams we released to our
  233. * test-lab.
  234. */
  235. int genwqe_recovery_on_fatal_gfir_required(struct genwqe_dev *cd)
  236. {
  237. return (cd->slu_unitcfg & 0xFFFF0ull) >= 0x32170ull;
  238. }
  239. int genwqe_flash_readback_fails(struct genwqe_dev *cd)
  240. {
  241. return (cd->slu_unitcfg & 0xFFFF0ull) < 0x32170ull;
  242. }
  243. /**
  244. * genwqe_T_psec() - Calculate PF/VF timeout register content
  245. *
  246. * Note: From a design perspective it turned out to be a bad idea to
  247. * use codes here to specifiy the frequency/speed values. An old
  248. * driver cannot understand new codes and is therefore always a
  249. * problem. Better is to measure out the value or put the
  250. * speed/frequency directly into a register which is always a valid
  251. * value for old as well as for new software.
  252. */
  253. /* T = 1/f */
  254. static int genwqe_T_psec(struct genwqe_dev *cd)
  255. {
  256. u16 speed; /* 1/f -> 250, 200, 166, 175 */
  257. static const int T[] = { 4000, 5000, 6000, 5714 };
  258. speed = (u16)((cd->slu_unitcfg >> 28) & 0x0full);
  259. if (speed >= ARRAY_SIZE(T))
  260. return -1; /* illegal value */
  261. return T[speed];
  262. }
  263. /**
  264. * genwqe_setup_pf_jtimer() - Setup PF hardware timeouts for DDCB execution
  265. *
  266. * Do this _after_ card_reset() is called. Otherwise the values will
  267. * vanish. The settings need to be done when the queues are inactive.
  268. *
  269. * The max. timeout value is 2^(10+x) * T (6ns for 166MHz) * 15/16.
  270. * The min. timeout value is 2^(10+x) * T (6ns for 166MHz) * 14/16.
  271. */
  272. static bool genwqe_setup_pf_jtimer(struct genwqe_dev *cd)
  273. {
  274. u32 T = genwqe_T_psec(cd);
  275. u64 x;
  276. if (genwqe_pf_jobtimeout_msec == 0)
  277. return false;
  278. /* PF: large value needed, flash update 2sec per block */
  279. x = ilog2(genwqe_pf_jobtimeout_msec *
  280. 16000000000uL/(T * 15)) - 10;
  281. genwqe_write_vreg(cd, IO_SLC_VF_APPJOB_TIMEOUT,
  282. 0xff00 | (x & 0xff), 0);
  283. return true;
  284. }
  285. /**
  286. * genwqe_setup_vf_jtimer() - Setup VF hardware timeouts for DDCB execution
  287. */
  288. static bool genwqe_setup_vf_jtimer(struct genwqe_dev *cd)
  289. {
  290. struct pci_dev *pci_dev = cd->pci_dev;
  291. unsigned int vf;
  292. u32 T = genwqe_T_psec(cd);
  293. u64 x;
  294. for (vf = 0; vf < pci_sriov_get_totalvfs(pci_dev); vf++) {
  295. if (cd->vf_jobtimeout_msec[vf] == 0)
  296. continue;
  297. x = ilog2(cd->vf_jobtimeout_msec[vf] *
  298. 16000000000uL/(T * 15)) - 10;
  299. genwqe_write_vreg(cd, IO_SLC_VF_APPJOB_TIMEOUT,
  300. 0xff00 | (x & 0xff), vf + 1);
  301. }
  302. return true;
  303. }
  304. static int genwqe_ffdc_buffs_alloc(struct genwqe_dev *cd)
  305. {
  306. unsigned int type, e = 0;
  307. for (type = 0; type < GENWQE_DBG_UNITS; type++) {
  308. switch (type) {
  309. case GENWQE_DBG_UNIT0:
  310. e = genwqe_ffdc_buff_size(cd, 0);
  311. break;
  312. case GENWQE_DBG_UNIT1:
  313. e = genwqe_ffdc_buff_size(cd, 1);
  314. break;
  315. case GENWQE_DBG_UNIT2:
  316. e = genwqe_ffdc_buff_size(cd, 2);
  317. break;
  318. case GENWQE_DBG_REGS:
  319. e = GENWQE_FFDC_REGS;
  320. break;
  321. }
  322. /* currently support only the debug units mentioned here */
  323. cd->ffdc[type].entries = e;
  324. cd->ffdc[type].regs = kmalloc(e * sizeof(struct genwqe_reg),
  325. GFP_KERNEL);
  326. /*
  327. * regs == NULL is ok, the using code treats this as no regs,
  328. * Printing warning is ok in this case.
  329. */
  330. }
  331. return 0;
  332. }
  333. static void genwqe_ffdc_buffs_free(struct genwqe_dev *cd)
  334. {
  335. unsigned int type;
  336. for (type = 0; type < GENWQE_DBG_UNITS; type++) {
  337. kfree(cd->ffdc[type].regs);
  338. cd->ffdc[type].regs = NULL;
  339. }
  340. }
  341. static int genwqe_read_ids(struct genwqe_dev *cd)
  342. {
  343. int err = 0;
  344. int slu_id;
  345. struct pci_dev *pci_dev = cd->pci_dev;
  346. cd->slu_unitcfg = __genwqe_readq(cd, IO_SLU_UNITCFG);
  347. if (cd->slu_unitcfg == IO_ILLEGAL_VALUE) {
  348. dev_err(&pci_dev->dev,
  349. "err: SLUID=%016llx\n", cd->slu_unitcfg);
  350. err = -EIO;
  351. goto out_err;
  352. }
  353. slu_id = genwqe_get_slu_id(cd);
  354. if (slu_id < GENWQE_SLU_ARCH_REQ || slu_id == 0xff) {
  355. dev_err(&pci_dev->dev,
  356. "err: incompatible SLU Architecture %u\n", slu_id);
  357. err = -ENOENT;
  358. goto out_err;
  359. }
  360. cd->app_unitcfg = __genwqe_readq(cd, IO_APP_UNITCFG);
  361. if (cd->app_unitcfg == IO_ILLEGAL_VALUE) {
  362. dev_err(&pci_dev->dev,
  363. "err: APPID=%016llx\n", cd->app_unitcfg);
  364. err = -EIO;
  365. goto out_err;
  366. }
  367. genwqe_read_app_id(cd, cd->app_name, sizeof(cd->app_name));
  368. /*
  369. * Is access to all registers possible? If we are a VF the
  370. * answer is obvious. If we run fully virtualized, we need to
  371. * check if we can access all registers. If we do not have
  372. * full access we will cause an UR and some informational FIRs
  373. * in the PF, but that should not harm.
  374. */
  375. if (pci_dev->is_virtfn)
  376. cd->is_privileged = 0;
  377. else
  378. cd->is_privileged = (__genwqe_readq(cd, IO_SLU_BITSTREAM)
  379. != IO_ILLEGAL_VALUE);
  380. out_err:
  381. return err;
  382. }
  383. static int genwqe_start(struct genwqe_dev *cd)
  384. {
  385. int err;
  386. struct pci_dev *pci_dev = cd->pci_dev;
  387. err = genwqe_read_ids(cd);
  388. if (err)
  389. return err;
  390. if (genwqe_is_privileged(cd)) {
  391. /* do this after the tweaks. alloc fail is acceptable */
  392. genwqe_ffdc_buffs_alloc(cd);
  393. genwqe_stop_traps(cd);
  394. /* Collect registers e.g. FIRs, UNITIDs, traces ... */
  395. genwqe_read_ffdc_regs(cd, cd->ffdc[GENWQE_DBG_REGS].regs,
  396. cd->ffdc[GENWQE_DBG_REGS].entries, 0);
  397. genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT0,
  398. cd->ffdc[GENWQE_DBG_UNIT0].regs,
  399. cd->ffdc[GENWQE_DBG_UNIT0].entries);
  400. genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT1,
  401. cd->ffdc[GENWQE_DBG_UNIT1].regs,
  402. cd->ffdc[GENWQE_DBG_UNIT1].entries);
  403. genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT2,
  404. cd->ffdc[GENWQE_DBG_UNIT2].regs,
  405. cd->ffdc[GENWQE_DBG_UNIT2].entries);
  406. genwqe_start_traps(cd);
  407. if (cd->card_state == GENWQE_CARD_FATAL_ERROR) {
  408. dev_warn(&pci_dev->dev,
  409. "[%s] chip reload/recovery!\n", __func__);
  410. /*
  411. * Stealth Mode: Reload chip on either hot
  412. * reset or PERST.
  413. */
  414. cd->softreset = 0x7Cull;
  415. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET,
  416. cd->softreset);
  417. err = genwqe_bus_reset(cd);
  418. if (err != 0) {
  419. dev_err(&pci_dev->dev,
  420. "[%s] err: bus reset failed!\n",
  421. __func__);
  422. goto out;
  423. }
  424. /*
  425. * Re-read the IDs because
  426. * it could happen that the bitstream load
  427. * failed!
  428. */
  429. err = genwqe_read_ids(cd);
  430. if (err)
  431. goto out;
  432. }
  433. }
  434. err = genwqe_setup_service_layer(cd); /* does a reset to the card */
  435. if (err != 0) {
  436. dev_err(&pci_dev->dev,
  437. "[%s] err: could not setup servicelayer!\n", __func__);
  438. err = -ENODEV;
  439. goto out;
  440. }
  441. if (genwqe_is_privileged(cd)) { /* code is running _after_ reset */
  442. genwqe_tweak_hardware(cd);
  443. genwqe_setup_pf_jtimer(cd);
  444. genwqe_setup_vf_jtimer(cd);
  445. }
  446. err = genwqe_device_create(cd);
  447. if (err < 0) {
  448. dev_err(&pci_dev->dev,
  449. "err: chdev init failed! (err=%d)\n", err);
  450. goto out_release_service_layer;
  451. }
  452. return 0;
  453. out_release_service_layer:
  454. genwqe_release_service_layer(cd);
  455. out:
  456. if (genwqe_is_privileged(cd))
  457. genwqe_ffdc_buffs_free(cd);
  458. return -EIO;
  459. }
  460. /**
  461. * genwqe_stop() - Stop card operation
  462. *
  463. * Recovery notes:
  464. * As long as genwqe_thread runs we might access registers during
  465. * error data capture. Same is with the genwqe_health_thread.
  466. * When genwqe_bus_reset() fails this function might called two times:
  467. * first by the genwqe_health_thread() and later by genwqe_remove() to
  468. * unbind the device. We must be able to survive that.
  469. *
  470. * This function must be robust enough to be called twice.
  471. */
  472. static int genwqe_stop(struct genwqe_dev *cd)
  473. {
  474. genwqe_finish_queue(cd); /* no register access */
  475. genwqe_device_remove(cd); /* device removed, procs killed */
  476. genwqe_release_service_layer(cd); /* here genwqe_thread is stopped */
  477. if (genwqe_is_privileged(cd)) {
  478. pci_disable_sriov(cd->pci_dev); /* access pci config space */
  479. genwqe_ffdc_buffs_free(cd);
  480. }
  481. return 0;
  482. }
  483. /**
  484. * genwqe_recover_card() - Try to recover the card if it is possible
  485. *
  486. * If fatal_err is set no register access is possible anymore. It is
  487. * likely that genwqe_start fails in that situation. Proper error
  488. * handling is required in this case.
  489. *
  490. * genwqe_bus_reset() will cause the pci code to call genwqe_remove()
  491. * and later genwqe_probe() for all virtual functions.
  492. */
  493. static int genwqe_recover_card(struct genwqe_dev *cd, int fatal_err)
  494. {
  495. int rc;
  496. struct pci_dev *pci_dev = cd->pci_dev;
  497. genwqe_stop(cd);
  498. /*
  499. * Make sure chip is not reloaded to maintain FFDC. Write SLU
  500. * Reset Register, CPLDReset field to 0.
  501. */
  502. if (!fatal_err) {
  503. cd->softreset = 0x70ull;
  504. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, cd->softreset);
  505. }
  506. rc = genwqe_bus_reset(cd);
  507. if (rc != 0) {
  508. dev_err(&pci_dev->dev,
  509. "[%s] err: card recovery impossible!\n", __func__);
  510. return rc;
  511. }
  512. rc = genwqe_start(cd);
  513. if (rc < 0) {
  514. dev_err(&pci_dev->dev,
  515. "[%s] err: failed to launch device!\n", __func__);
  516. return rc;
  517. }
  518. return 0;
  519. }
  520. static int genwqe_health_check_cond(struct genwqe_dev *cd, u64 *gfir)
  521. {
  522. *gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  523. return (*gfir & GFIR_ERR_TRIGGER) &&
  524. genwqe_recovery_on_fatal_gfir_required(cd);
  525. }
  526. /**
  527. * genwqe_fir_checking() - Check the fault isolation registers of the card
  528. *
  529. * If this code works ok, can be tried out with help of the genwqe_poke tool:
  530. * sudo ./tools/genwqe_poke 0x8 0xfefefefefef
  531. *
  532. * Now the relevant FIRs/sFIRs should be printed out and the driver should
  533. * invoke recovery (devices are removed and readded).
  534. */
  535. static u64 genwqe_fir_checking(struct genwqe_dev *cd)
  536. {
  537. int j, iterations = 0;
  538. u64 mask, fir, fec, uid, gfir, gfir_masked, sfir, sfec;
  539. u32 fir_addr, fir_clr_addr, fec_addr, sfir_addr, sfec_addr;
  540. struct pci_dev *pci_dev = cd->pci_dev;
  541. healthMonitor:
  542. iterations++;
  543. if (iterations > 16) {
  544. dev_err(&pci_dev->dev, "* exit looping after %d times\n",
  545. iterations);
  546. goto fatal_error;
  547. }
  548. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  549. if (gfir != 0x0)
  550. dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n",
  551. IO_SLC_CFGREG_GFIR, gfir);
  552. if (gfir == IO_ILLEGAL_VALUE)
  553. goto fatal_error;
  554. /*
  555. * Avoid printing when to GFIR bit is on prevents contignous
  556. * printout e.g. for the following bug:
  557. * FIR set without a 2ndary FIR/FIR cannot be cleared
  558. * Comment out the following if to get the prints:
  559. */
  560. if (gfir == 0)
  561. return 0;
  562. gfir_masked = gfir & GFIR_ERR_TRIGGER; /* fatal errors */
  563. for (uid = 0; uid < GENWQE_MAX_UNITS; uid++) { /* 0..2 in zEDC */
  564. /* read the primary FIR (pfir) */
  565. fir_addr = (uid << 24) + 0x08;
  566. fir = __genwqe_readq(cd, fir_addr);
  567. if (fir == 0x0)
  568. continue; /* no error in this unit */
  569. dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n", fir_addr, fir);
  570. if (fir == IO_ILLEGAL_VALUE)
  571. goto fatal_error;
  572. /* read primary FEC */
  573. fec_addr = (uid << 24) + 0x18;
  574. fec = __genwqe_readq(cd, fec_addr);
  575. dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n", fec_addr, fec);
  576. if (fec == IO_ILLEGAL_VALUE)
  577. goto fatal_error;
  578. for (j = 0, mask = 1ULL; j < 64; j++, mask <<= 1) {
  579. /* secondary fir empty, skip it */
  580. if ((fir & mask) == 0x0)
  581. continue;
  582. sfir_addr = (uid << 24) + 0x100 + 0x08 * j;
  583. sfir = __genwqe_readq(cd, sfir_addr);
  584. if (sfir == IO_ILLEGAL_VALUE)
  585. goto fatal_error;
  586. dev_err(&pci_dev->dev,
  587. "* 0x%08x 0x%016llx\n", sfir_addr, sfir);
  588. sfec_addr = (uid << 24) + 0x300 + 0x08 * j;
  589. sfec = __genwqe_readq(cd, sfec_addr);
  590. if (sfec == IO_ILLEGAL_VALUE)
  591. goto fatal_error;
  592. dev_err(&pci_dev->dev,
  593. "* 0x%08x 0x%016llx\n", sfec_addr, sfec);
  594. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  595. if (gfir == IO_ILLEGAL_VALUE)
  596. goto fatal_error;
  597. /* gfir turned on during routine! get out and
  598. start over. */
  599. if ((gfir_masked == 0x0) &&
  600. (gfir & GFIR_ERR_TRIGGER)) {
  601. goto healthMonitor;
  602. }
  603. /* do not clear if we entered with a fatal gfir */
  604. if (gfir_masked == 0x0) {
  605. /* NEW clear by mask the logged bits */
  606. sfir_addr = (uid << 24) + 0x100 + 0x08 * j;
  607. __genwqe_writeq(cd, sfir_addr, sfir);
  608. dev_dbg(&pci_dev->dev,
  609. "[HM] Clearing 2ndary FIR 0x%08x "
  610. "with 0x%016llx\n", sfir_addr, sfir);
  611. /*
  612. * note, these cannot be error-Firs
  613. * since gfir_masked is 0 after sfir
  614. * was read. Also, it is safe to do
  615. * this write if sfir=0. Still need to
  616. * clear the primary. This just means
  617. * there is no secondary FIR.
  618. */
  619. /* clear by mask the logged bit. */
  620. fir_clr_addr = (uid << 24) + 0x10;
  621. __genwqe_writeq(cd, fir_clr_addr, mask);
  622. dev_dbg(&pci_dev->dev,
  623. "[HM] Clearing primary FIR 0x%08x "
  624. "with 0x%016llx\n", fir_clr_addr,
  625. mask);
  626. }
  627. }
  628. }
  629. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  630. if (gfir == IO_ILLEGAL_VALUE)
  631. goto fatal_error;
  632. if ((gfir_masked == 0x0) && (gfir & GFIR_ERR_TRIGGER)) {
  633. /*
  634. * Check once more that it didn't go on after all the
  635. * FIRS were cleared.
  636. */
  637. dev_dbg(&pci_dev->dev, "ACK! Another FIR! Recursing %d!\n",
  638. iterations);
  639. goto healthMonitor;
  640. }
  641. return gfir_masked;
  642. fatal_error:
  643. return IO_ILLEGAL_VALUE;
  644. }
  645. /**
  646. * genwqe_health_thread() - Health checking thread
  647. *
  648. * This thread is only started for the PF of the card.
  649. *
  650. * This thread monitors the health of the card. A critical situation
  651. * is when we read registers which contain -1 (IO_ILLEGAL_VALUE). In
  652. * this case we need to be recovered from outside. Writing to
  653. * registers will very likely not work either.
  654. *
  655. * This thread must only exit if kthread_should_stop() becomes true.
  656. *
  657. * Condition for the health-thread to trigger:
  658. * a) when a kthread_stop() request comes in or
  659. * b) a critical GFIR occured
  660. *
  661. * Informational GFIRs are checked and potentially printed in
  662. * health_check_interval seconds.
  663. */
  664. static int genwqe_health_thread(void *data)
  665. {
  666. int rc, should_stop = 0;
  667. struct genwqe_dev *cd = data;
  668. struct pci_dev *pci_dev = cd->pci_dev;
  669. u64 gfir, gfir_masked, slu_unitcfg, app_unitcfg;
  670. while (!kthread_should_stop()) {
  671. rc = wait_event_interruptible_timeout(cd->health_waitq,
  672. (genwqe_health_check_cond(cd, &gfir) ||
  673. (should_stop = kthread_should_stop())),
  674. genwqe_health_check_interval * HZ);
  675. if (should_stop)
  676. break;
  677. if (gfir == IO_ILLEGAL_VALUE) {
  678. dev_err(&pci_dev->dev,
  679. "[%s] GFIR=%016llx\n", __func__, gfir);
  680. goto fatal_error;
  681. }
  682. slu_unitcfg = __genwqe_readq(cd, IO_SLU_UNITCFG);
  683. if (slu_unitcfg == IO_ILLEGAL_VALUE) {
  684. dev_err(&pci_dev->dev,
  685. "[%s] SLU_UNITCFG=%016llx\n",
  686. __func__, slu_unitcfg);
  687. goto fatal_error;
  688. }
  689. app_unitcfg = __genwqe_readq(cd, IO_APP_UNITCFG);
  690. if (app_unitcfg == IO_ILLEGAL_VALUE) {
  691. dev_err(&pci_dev->dev,
  692. "[%s] APP_UNITCFG=%016llx\n",
  693. __func__, app_unitcfg);
  694. goto fatal_error;
  695. }
  696. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  697. if (gfir == IO_ILLEGAL_VALUE) {
  698. dev_err(&pci_dev->dev,
  699. "[%s] %s: GFIR=%016llx\n", __func__,
  700. (gfir & GFIR_ERR_TRIGGER) ? "err" : "info",
  701. gfir);
  702. goto fatal_error;
  703. }
  704. gfir_masked = genwqe_fir_checking(cd);
  705. if (gfir_masked == IO_ILLEGAL_VALUE)
  706. goto fatal_error;
  707. /*
  708. * GFIR ErrorTrigger bits set => reset the card!
  709. * Never do this for old/manufacturing images!
  710. */
  711. if ((gfir_masked) && !cd->skip_recovery &&
  712. genwqe_recovery_on_fatal_gfir_required(cd)) {
  713. cd->card_state = GENWQE_CARD_FATAL_ERROR;
  714. rc = genwqe_recover_card(cd, 0);
  715. if (rc < 0) {
  716. /* FIXME Card is unusable and needs unbind! */
  717. goto fatal_error;
  718. }
  719. }
  720. cd->last_gfir = gfir;
  721. cond_resched();
  722. }
  723. return 0;
  724. fatal_error:
  725. dev_err(&pci_dev->dev,
  726. "[%s] card unusable. Please trigger unbind!\n", __func__);
  727. /* Bring down logical devices to inform user space via udev remove. */
  728. cd->card_state = GENWQE_CARD_FATAL_ERROR;
  729. genwqe_stop(cd);
  730. /* genwqe_bus_reset failed(). Now wait for genwqe_remove(). */
  731. while (!kthread_should_stop())
  732. cond_resched();
  733. return -EIO;
  734. }
  735. static int genwqe_health_check_start(struct genwqe_dev *cd)
  736. {
  737. int rc;
  738. if (genwqe_health_check_interval <= 0)
  739. return 0; /* valid for disabling the service */
  740. /* moved before request_irq() */
  741. /* init_waitqueue_head(&cd->health_waitq); */
  742. cd->health_thread = kthread_run(genwqe_health_thread, cd,
  743. GENWQE_DEVNAME "%d_health",
  744. cd->card_idx);
  745. if (IS_ERR(cd->health_thread)) {
  746. rc = PTR_ERR(cd->health_thread);
  747. cd->health_thread = NULL;
  748. return rc;
  749. }
  750. return 0;
  751. }
  752. static int genwqe_health_thread_running(struct genwqe_dev *cd)
  753. {
  754. return cd->health_thread != NULL;
  755. }
  756. static int genwqe_health_check_stop(struct genwqe_dev *cd)
  757. {
  758. int rc;
  759. if (!genwqe_health_thread_running(cd))
  760. return -EIO;
  761. rc = kthread_stop(cd->health_thread);
  762. cd->health_thread = NULL;
  763. return 0;
  764. }
  765. /**
  766. * genwqe_pci_setup() - Allocate PCIe related resources for our card
  767. */
  768. static int genwqe_pci_setup(struct genwqe_dev *cd)
  769. {
  770. int err, bars;
  771. struct pci_dev *pci_dev = cd->pci_dev;
  772. bars = pci_select_bars(pci_dev, IORESOURCE_MEM);
  773. err = pci_enable_device_mem(pci_dev);
  774. if (err) {
  775. dev_err(&pci_dev->dev,
  776. "err: failed to enable pci memory (err=%d)\n", err);
  777. goto err_out;
  778. }
  779. /* Reserve PCI I/O and memory resources */
  780. err = pci_request_selected_regions(pci_dev, bars, genwqe_driver_name);
  781. if (err) {
  782. dev_err(&pci_dev->dev,
  783. "[%s] err: request bars failed (%d)\n", __func__, err);
  784. err = -EIO;
  785. goto err_disable_device;
  786. }
  787. /* check for 64-bit DMA address supported (DAC) */
  788. if (!pci_set_dma_mask(pci_dev, DMA_BIT_MASK(64))) {
  789. err = pci_set_consistent_dma_mask(pci_dev, DMA_BIT_MASK(64));
  790. if (err) {
  791. dev_err(&pci_dev->dev,
  792. "err: DMA64 consistent mask error\n");
  793. err = -EIO;
  794. goto out_release_resources;
  795. }
  796. /* check for 32-bit DMA address supported (SAC) */
  797. } else if (!pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
  798. err = pci_set_consistent_dma_mask(pci_dev, DMA_BIT_MASK(32));
  799. if (err) {
  800. dev_err(&pci_dev->dev,
  801. "err: DMA32 consistent mask error\n");
  802. err = -EIO;
  803. goto out_release_resources;
  804. }
  805. } else {
  806. dev_err(&pci_dev->dev,
  807. "err: neither DMA32 nor DMA64 supported\n");
  808. err = -EIO;
  809. goto out_release_resources;
  810. }
  811. pci_set_master(pci_dev);
  812. pci_enable_pcie_error_reporting(pci_dev);
  813. /* request complete BAR-0 space (length = 0) */
  814. cd->mmio_len = pci_resource_len(pci_dev, 0);
  815. cd->mmio = pci_iomap(pci_dev, 0, 0);
  816. if (cd->mmio == NULL) {
  817. dev_err(&pci_dev->dev,
  818. "[%s] err: mapping BAR0 failed\n", __func__);
  819. err = -ENOMEM;
  820. goto out_release_resources;
  821. }
  822. cd->num_vfs = pci_sriov_get_totalvfs(pci_dev);
  823. err = genwqe_read_ids(cd);
  824. if (err)
  825. goto out_iounmap;
  826. return 0;
  827. out_iounmap:
  828. pci_iounmap(pci_dev, cd->mmio);
  829. out_release_resources:
  830. pci_release_selected_regions(pci_dev, bars);
  831. err_disable_device:
  832. pci_disable_device(pci_dev);
  833. err_out:
  834. return err;
  835. }
  836. /**
  837. * genwqe_pci_remove() - Free PCIe related resources for our card
  838. */
  839. static void genwqe_pci_remove(struct genwqe_dev *cd)
  840. {
  841. int bars;
  842. struct pci_dev *pci_dev = cd->pci_dev;
  843. if (cd->mmio)
  844. pci_iounmap(pci_dev, cd->mmio);
  845. bars = pci_select_bars(pci_dev, IORESOURCE_MEM);
  846. pci_release_selected_regions(pci_dev, bars);
  847. pci_disable_device(pci_dev);
  848. }
  849. /**
  850. * genwqe_probe() - Device initialization
  851. * @pdev: PCI device information struct
  852. *
  853. * Callable for multiple cards. This function is called on bind.
  854. *
  855. * Return: 0 if succeeded, < 0 when failed
  856. */
  857. static int genwqe_probe(struct pci_dev *pci_dev,
  858. const struct pci_device_id *id)
  859. {
  860. int err;
  861. struct genwqe_dev *cd;
  862. genwqe_init_crc32();
  863. cd = genwqe_dev_alloc();
  864. if (IS_ERR(cd)) {
  865. dev_err(&pci_dev->dev, "err: could not alloc mem (err=%d)!\n",
  866. (int)PTR_ERR(cd));
  867. return PTR_ERR(cd);
  868. }
  869. dev_set_drvdata(&pci_dev->dev, cd);
  870. cd->pci_dev = pci_dev;
  871. err = genwqe_pci_setup(cd);
  872. if (err < 0) {
  873. dev_err(&pci_dev->dev,
  874. "err: problems with PCI setup (err=%d)\n", err);
  875. goto out_free_dev;
  876. }
  877. err = genwqe_start(cd);
  878. if (err < 0) {
  879. dev_err(&pci_dev->dev,
  880. "err: cannot start card services! (err=%d)\n", err);
  881. goto out_pci_remove;
  882. }
  883. if (genwqe_is_privileged(cd)) {
  884. err = genwqe_health_check_start(cd);
  885. if (err < 0) {
  886. dev_err(&pci_dev->dev,
  887. "err: cannot start health checking! "
  888. "(err=%d)\n", err);
  889. goto out_stop_services;
  890. }
  891. }
  892. return 0;
  893. out_stop_services:
  894. genwqe_stop(cd);
  895. out_pci_remove:
  896. genwqe_pci_remove(cd);
  897. out_free_dev:
  898. genwqe_dev_free(cd);
  899. return err;
  900. }
  901. /**
  902. * genwqe_remove() - Called when device is removed (hot-plugable)
  903. *
  904. * Or when driver is unloaded respecitively when unbind is done.
  905. */
  906. static void genwqe_remove(struct pci_dev *pci_dev)
  907. {
  908. struct genwqe_dev *cd = dev_get_drvdata(&pci_dev->dev);
  909. genwqe_health_check_stop(cd);
  910. /*
  911. * genwqe_stop() must survive if it is called twice
  912. * sequentially. This happens when the health thread calls it
  913. * and fails on genwqe_bus_reset().
  914. */
  915. genwqe_stop(cd);
  916. genwqe_pci_remove(cd);
  917. genwqe_dev_free(cd);
  918. }
  919. /*
  920. * genwqe_err_error_detected() - Error detection callback
  921. *
  922. * This callback is called by the PCI subsystem whenever a PCI bus
  923. * error is detected.
  924. */
  925. static pci_ers_result_t genwqe_err_error_detected(struct pci_dev *pci_dev,
  926. enum pci_channel_state state)
  927. {
  928. struct genwqe_dev *cd;
  929. dev_err(&pci_dev->dev, "[%s] state=%d\n", __func__, state);
  930. if (pci_dev == NULL)
  931. return PCI_ERS_RESULT_NEED_RESET;
  932. cd = dev_get_drvdata(&pci_dev->dev);
  933. if (cd == NULL)
  934. return PCI_ERS_RESULT_NEED_RESET;
  935. switch (state) {
  936. case pci_channel_io_normal:
  937. return PCI_ERS_RESULT_CAN_RECOVER;
  938. case pci_channel_io_frozen:
  939. return PCI_ERS_RESULT_NEED_RESET;
  940. case pci_channel_io_perm_failure:
  941. return PCI_ERS_RESULT_DISCONNECT;
  942. }
  943. return PCI_ERS_RESULT_NEED_RESET;
  944. }
  945. static pci_ers_result_t genwqe_err_result_none(struct pci_dev *dev)
  946. {
  947. return PCI_ERS_RESULT_NONE;
  948. }
  949. static void genwqe_err_resume(struct pci_dev *dev)
  950. {
  951. }
  952. static int genwqe_sriov_configure(struct pci_dev *dev, int numvfs)
  953. {
  954. struct genwqe_dev *cd = dev_get_drvdata(&dev->dev);
  955. if (numvfs > 0) {
  956. genwqe_setup_vf_jtimer(cd);
  957. pci_enable_sriov(dev, numvfs);
  958. return numvfs;
  959. }
  960. if (numvfs == 0) {
  961. pci_disable_sriov(dev);
  962. return 0;
  963. }
  964. return 0;
  965. }
  966. static struct pci_error_handlers genwqe_err_handler = {
  967. .error_detected = genwqe_err_error_detected,
  968. .mmio_enabled = genwqe_err_result_none,
  969. .link_reset = genwqe_err_result_none,
  970. .slot_reset = genwqe_err_result_none,
  971. .resume = genwqe_err_resume,
  972. };
  973. static struct pci_driver genwqe_driver = {
  974. .name = genwqe_driver_name,
  975. .id_table = genwqe_device_table,
  976. .probe = genwqe_probe,
  977. .remove = genwqe_remove,
  978. .sriov_configure = genwqe_sriov_configure,
  979. .err_handler = &genwqe_err_handler,
  980. };
  981. /**
  982. * genwqe_init_module() - Driver registration and initialization
  983. */
  984. static int __init genwqe_init_module(void)
  985. {
  986. int rc;
  987. class_genwqe = class_create(THIS_MODULE, GENWQE_DEVNAME);
  988. if (IS_ERR(class_genwqe)) {
  989. pr_err("[%s] create class failed\n", __func__);
  990. return -ENOMEM;
  991. }
  992. debugfs_genwqe = debugfs_create_dir(GENWQE_DEVNAME, NULL);
  993. if (!debugfs_genwqe) {
  994. rc = -ENOMEM;
  995. goto err_out;
  996. }
  997. rc = pci_register_driver(&genwqe_driver);
  998. if (rc != 0) {
  999. pr_err("[%s] pci_reg_driver (rc=%d)\n", __func__, rc);
  1000. goto err_out0;
  1001. }
  1002. return rc;
  1003. err_out0:
  1004. debugfs_remove(debugfs_genwqe);
  1005. err_out:
  1006. class_destroy(class_genwqe);
  1007. return rc;
  1008. }
  1009. /**
  1010. * genwqe_exit_module() - Driver exit
  1011. */
  1012. static void __exit genwqe_exit_module(void)
  1013. {
  1014. pci_unregister_driver(&genwqe_driver);
  1015. debugfs_remove(debugfs_genwqe);
  1016. class_destroy(class_genwqe);
  1017. }
  1018. module_init(genwqe_init_module);
  1019. module_exit(genwqe_exit_module);