switchtec.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microsemi Switchtec(tm) PCIe Management Driver
  4. * Copyright (c) 2017, Microsemi Corporation
  5. */
  6. #include <linux/switchtec.h>
  7. #include <linux/switchtec_ioctl.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/poll.h>
  13. #include <linux/wait.h>
  14. #include <linux/io-64-nonatomic-lo-hi.h>
  15. #include <linux/nospec.h>
  16. MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
  17. MODULE_VERSION("0.1");
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Microsemi Corporation");
  20. static int max_devices = 16;
  21. module_param(max_devices, int, 0644);
  22. MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
  23. static bool use_dma_mrpc = true;
  24. module_param(use_dma_mrpc, bool, 0644);
  25. MODULE_PARM_DESC(use_dma_mrpc,
  26. "Enable the use of the DMA MRPC feature");
  27. static int nirqs = 32;
  28. module_param(nirqs, int, 0644);
  29. MODULE_PARM_DESC(nirqs, "number of interrupts to allocate (more may be useful for NTB applications)");
  30. static dev_t switchtec_devt;
  31. static DEFINE_IDA(switchtec_minor_ida);
  32. const struct class switchtec_class = {
  33. .name = "switchtec",
  34. };
  35. EXPORT_SYMBOL_GPL(switchtec_class);
  36. enum mrpc_state {
  37. MRPC_IDLE = 0,
  38. MRPC_QUEUED,
  39. MRPC_RUNNING,
  40. MRPC_DONE,
  41. MRPC_IO_ERROR,
  42. };
  43. struct switchtec_user {
  44. struct switchtec_dev *stdev;
  45. enum mrpc_state state;
  46. wait_queue_head_t cmd_comp;
  47. struct kref kref;
  48. struct list_head list;
  49. bool cmd_done;
  50. u32 cmd;
  51. u32 status;
  52. u32 return_code;
  53. size_t data_len;
  54. size_t read_len;
  55. unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
  56. int event_cnt;
  57. };
  58. /*
  59. * The MMIO reads to the device_id register should always return the device ID
  60. * of the device, otherwise the firmware is probably stuck or unreachable
  61. * due to a firmware reset which clears PCI state including the BARs and Memory
  62. * Space Enable bits.
  63. */
  64. static int is_firmware_running(struct switchtec_dev *stdev)
  65. {
  66. u32 device = ioread32(&stdev->mmio_sys_info->device_id);
  67. return stdev->pdev->device == device;
  68. }
  69. static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
  70. {
  71. struct switchtec_user *stuser;
  72. stuser = kzalloc_obj(*stuser);
  73. if (!stuser)
  74. return ERR_PTR(-ENOMEM);
  75. get_device(&stdev->dev);
  76. stuser->stdev = stdev;
  77. kref_init(&stuser->kref);
  78. INIT_LIST_HEAD(&stuser->list);
  79. init_waitqueue_head(&stuser->cmd_comp);
  80. stuser->event_cnt = atomic_read(&stdev->event_cnt);
  81. dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
  82. return stuser;
  83. }
  84. static void stuser_free(struct kref *kref)
  85. {
  86. struct switchtec_user *stuser;
  87. stuser = container_of(kref, struct switchtec_user, kref);
  88. dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
  89. put_device(&stuser->stdev->dev);
  90. kfree(stuser);
  91. }
  92. static void stuser_put(struct switchtec_user *stuser)
  93. {
  94. kref_put(&stuser->kref, stuser_free);
  95. }
  96. static void stuser_set_state(struct switchtec_user *stuser,
  97. enum mrpc_state state)
  98. {
  99. /* requires the mrpc_mutex to already be held when called */
  100. static const char * const state_names[] = {
  101. [MRPC_IDLE] = "IDLE",
  102. [MRPC_QUEUED] = "QUEUED",
  103. [MRPC_RUNNING] = "RUNNING",
  104. [MRPC_DONE] = "DONE",
  105. [MRPC_IO_ERROR] = "IO_ERROR",
  106. };
  107. stuser->state = state;
  108. dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
  109. stuser, state_names[state]);
  110. }
  111. static void mrpc_complete_cmd(struct switchtec_dev *stdev);
  112. static void flush_wc_buf(struct switchtec_dev *stdev)
  113. {
  114. struct ntb_dbmsg_regs __iomem *mmio_dbmsg;
  115. /*
  116. * odb (outbound doorbell) register is processed by low latency
  117. * hardware and w/o side effect
  118. */
  119. mmio_dbmsg = (void __iomem *)stdev->mmio_ntb +
  120. SWITCHTEC_NTB_REG_DBMSG_OFFSET;
  121. ioread32(&mmio_dbmsg->odb);
  122. }
  123. static void mrpc_cmd_submit(struct switchtec_dev *stdev)
  124. {
  125. /* requires the mrpc_mutex to already be held when called */
  126. struct switchtec_user *stuser;
  127. if (stdev->mrpc_busy)
  128. return;
  129. if (list_empty(&stdev->mrpc_queue))
  130. return;
  131. stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
  132. list);
  133. if (stdev->dma_mrpc) {
  134. stdev->dma_mrpc->status = SWITCHTEC_MRPC_STATUS_INPROGRESS;
  135. memset(stdev->dma_mrpc->data, 0xFF, SWITCHTEC_MRPC_PAYLOAD_SIZE);
  136. }
  137. stuser_set_state(stuser, MRPC_RUNNING);
  138. stdev->mrpc_busy = 1;
  139. memcpy_toio(&stdev->mmio_mrpc->input_data,
  140. stuser->data, stuser->data_len);
  141. flush_wc_buf(stdev);
  142. iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
  143. schedule_delayed_work(&stdev->mrpc_timeout,
  144. msecs_to_jiffies(500));
  145. }
  146. static int mrpc_queue_cmd(struct switchtec_user *stuser)
  147. {
  148. /* requires the mrpc_mutex to already be held when called */
  149. struct switchtec_dev *stdev = stuser->stdev;
  150. kref_get(&stuser->kref);
  151. stuser->read_len = sizeof(stuser->data);
  152. stuser_set_state(stuser, MRPC_QUEUED);
  153. stuser->cmd_done = false;
  154. list_add_tail(&stuser->list, &stdev->mrpc_queue);
  155. mrpc_cmd_submit(stdev);
  156. return 0;
  157. }
  158. static void mrpc_cleanup_cmd(struct switchtec_dev *stdev)
  159. {
  160. /* requires the mrpc_mutex to already be held when called */
  161. struct switchtec_user *stuser = list_entry(stdev->mrpc_queue.next,
  162. struct switchtec_user, list);
  163. stuser->cmd_done = true;
  164. wake_up_interruptible(&stuser->cmd_comp);
  165. list_del_init(&stuser->list);
  166. stuser_put(stuser);
  167. stdev->mrpc_busy = 0;
  168. mrpc_cmd_submit(stdev);
  169. }
  170. static void mrpc_complete_cmd(struct switchtec_dev *stdev)
  171. {
  172. /* requires the mrpc_mutex to already be held when called */
  173. struct switchtec_user *stuser;
  174. if (list_empty(&stdev->mrpc_queue))
  175. return;
  176. stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
  177. list);
  178. if (stdev->dma_mrpc)
  179. stuser->status = stdev->dma_mrpc->status;
  180. else
  181. stuser->status = ioread32(&stdev->mmio_mrpc->status);
  182. if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
  183. return;
  184. stuser_set_state(stuser, MRPC_DONE);
  185. stuser->return_code = 0;
  186. if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE &&
  187. stuser->status != SWITCHTEC_MRPC_STATUS_ERROR)
  188. goto out;
  189. if (stdev->dma_mrpc)
  190. stuser->return_code = stdev->dma_mrpc->rtn_code;
  191. else
  192. stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
  193. if (stuser->return_code != 0)
  194. goto out;
  195. if (stdev->dma_mrpc)
  196. memcpy(stuser->data, &stdev->dma_mrpc->data,
  197. stuser->read_len);
  198. else
  199. memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
  200. stuser->read_len);
  201. out:
  202. mrpc_cleanup_cmd(stdev);
  203. }
  204. static void mrpc_event_work(struct work_struct *work)
  205. {
  206. struct switchtec_dev *stdev;
  207. stdev = container_of(work, struct switchtec_dev, mrpc_work);
  208. dev_dbg(&stdev->dev, "%s\n", __func__);
  209. guard(mutex)(&stdev->mrpc_mutex);
  210. cancel_delayed_work(&stdev->mrpc_timeout);
  211. mrpc_complete_cmd(stdev);
  212. }
  213. static void mrpc_error_complete_cmd(struct switchtec_dev *stdev)
  214. {
  215. /* requires the mrpc_mutex to already be held when called */
  216. struct switchtec_user *stuser;
  217. if (list_empty(&stdev->mrpc_queue))
  218. return;
  219. stuser = list_entry(stdev->mrpc_queue.next,
  220. struct switchtec_user, list);
  221. stuser_set_state(stuser, MRPC_IO_ERROR);
  222. mrpc_cleanup_cmd(stdev);
  223. }
  224. static void mrpc_timeout_work(struct work_struct *work)
  225. {
  226. struct switchtec_dev *stdev;
  227. u32 status;
  228. stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
  229. dev_dbg(&stdev->dev, "%s\n", __func__);
  230. mutex_lock(&stdev->mrpc_mutex);
  231. if (!is_firmware_running(stdev)) {
  232. mrpc_error_complete_cmd(stdev);
  233. goto out;
  234. }
  235. if (stdev->dma_mrpc)
  236. status = stdev->dma_mrpc->status;
  237. else
  238. status = ioread32(&stdev->mmio_mrpc->status);
  239. if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
  240. schedule_delayed_work(&stdev->mrpc_timeout,
  241. msecs_to_jiffies(500));
  242. goto out;
  243. }
  244. mrpc_complete_cmd(stdev);
  245. out:
  246. mutex_unlock(&stdev->mrpc_mutex);
  247. }
  248. static ssize_t device_version_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct switchtec_dev *stdev = to_stdev(dev);
  252. u32 ver;
  253. ver = ioread32(&stdev->mmio_sys_info->device_version);
  254. return sysfs_emit(buf, "%x\n", ver);
  255. }
  256. static DEVICE_ATTR_RO(device_version);
  257. static ssize_t fw_version_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct switchtec_dev *stdev = to_stdev(dev);
  261. u32 ver;
  262. ver = ioread32(&stdev->mmio_sys_info->firmware_version);
  263. return sysfs_emit(buf, "%08x\n", ver);
  264. }
  265. static DEVICE_ATTR_RO(fw_version);
  266. static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
  267. {
  268. int i;
  269. memcpy_fromio(buf, attr, len);
  270. buf[len] = '\n';
  271. buf[len + 1] = 0;
  272. for (i = len - 1; i > 0; i--) {
  273. if (buf[i] != ' ')
  274. break;
  275. buf[i] = '\n';
  276. buf[i + 1] = 0;
  277. }
  278. return strlen(buf);
  279. }
  280. #define DEVICE_ATTR_SYS_INFO_STR(field) \
  281. static ssize_t field ## _show(struct device *dev, \
  282. struct device_attribute *attr, char *buf) \
  283. { \
  284. struct switchtec_dev *stdev = to_stdev(dev); \
  285. struct sys_info_regs __iomem *si = stdev->mmio_sys_info; \
  286. if (stdev->gen == SWITCHTEC_GEN3) \
  287. return io_string_show(buf, &si->gen3.field, \
  288. sizeof(si->gen3.field)); \
  289. else if (stdev->gen >= SWITCHTEC_GEN4) \
  290. return io_string_show(buf, &si->gen4.field, \
  291. sizeof(si->gen4.field)); \
  292. else \
  293. return -EOPNOTSUPP; \
  294. } \
  295. \
  296. static DEVICE_ATTR_RO(field)
  297. DEVICE_ATTR_SYS_INFO_STR(vendor_id);
  298. DEVICE_ATTR_SYS_INFO_STR(product_id);
  299. DEVICE_ATTR_SYS_INFO_STR(product_revision);
  300. static ssize_t component_vendor_show(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. struct switchtec_dev *stdev = to_stdev(dev);
  304. struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
  305. /* component_vendor field not supported after gen3 */
  306. if (stdev->gen != SWITCHTEC_GEN3)
  307. return sysfs_emit(buf, "none\n");
  308. return io_string_show(buf, &si->gen3.component_vendor,
  309. sizeof(si->gen3.component_vendor));
  310. }
  311. static DEVICE_ATTR_RO(component_vendor);
  312. static ssize_t component_id_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct switchtec_dev *stdev = to_stdev(dev);
  316. int id = ioread16(&stdev->mmio_sys_info->gen3.component_id);
  317. /* component_id field not supported after gen3 */
  318. if (stdev->gen != SWITCHTEC_GEN3)
  319. return sysfs_emit(buf, "none\n");
  320. return sysfs_emit(buf, "PM%04X\n", id);
  321. }
  322. static DEVICE_ATTR_RO(component_id);
  323. static ssize_t component_revision_show(struct device *dev,
  324. struct device_attribute *attr, char *buf)
  325. {
  326. struct switchtec_dev *stdev = to_stdev(dev);
  327. int rev = ioread8(&stdev->mmio_sys_info->gen3.component_revision);
  328. /* component_revision field not supported after gen3 */
  329. if (stdev->gen != SWITCHTEC_GEN3)
  330. return sysfs_emit(buf, "255\n");
  331. return sysfs_emit(buf, "%d\n", rev);
  332. }
  333. static DEVICE_ATTR_RO(component_revision);
  334. static ssize_t partition_show(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct switchtec_dev *stdev = to_stdev(dev);
  338. return sysfs_emit(buf, "%d\n", stdev->partition);
  339. }
  340. static DEVICE_ATTR_RO(partition);
  341. static ssize_t partition_count_show(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. struct switchtec_dev *stdev = to_stdev(dev);
  345. return sysfs_emit(buf, "%d\n", stdev->partition_count);
  346. }
  347. static DEVICE_ATTR_RO(partition_count);
  348. static struct attribute *switchtec_device_attrs[] = {
  349. &dev_attr_device_version.attr,
  350. &dev_attr_fw_version.attr,
  351. &dev_attr_vendor_id.attr,
  352. &dev_attr_product_id.attr,
  353. &dev_attr_product_revision.attr,
  354. &dev_attr_component_vendor.attr,
  355. &dev_attr_component_id.attr,
  356. &dev_attr_component_revision.attr,
  357. &dev_attr_partition.attr,
  358. &dev_attr_partition_count.attr,
  359. NULL,
  360. };
  361. ATTRIBUTE_GROUPS(switchtec_device);
  362. static int switchtec_dev_open(struct inode *inode, struct file *filp)
  363. {
  364. struct switchtec_dev *stdev;
  365. struct switchtec_user *stuser;
  366. stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
  367. stuser = stuser_create(stdev);
  368. if (IS_ERR(stuser))
  369. return PTR_ERR(stuser);
  370. filp->private_data = stuser;
  371. stream_open(inode, filp);
  372. dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
  373. return 0;
  374. }
  375. static int switchtec_dev_release(struct inode *inode, struct file *filp)
  376. {
  377. struct switchtec_user *stuser = filp->private_data;
  378. stuser_put(stuser);
  379. return 0;
  380. }
  381. static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
  382. {
  383. if (mutex_lock_interruptible(&stdev->mrpc_mutex))
  384. return -EINTR;
  385. if (!stdev->alive) {
  386. mutex_unlock(&stdev->mrpc_mutex);
  387. return -ENODEV;
  388. }
  389. return 0;
  390. }
  391. static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
  392. size_t size, loff_t *off)
  393. {
  394. struct switchtec_user *stuser = filp->private_data;
  395. struct switchtec_dev *stdev = stuser->stdev;
  396. int rc;
  397. if (size < sizeof(stuser->cmd) ||
  398. size > sizeof(stuser->cmd) + sizeof(stuser->data))
  399. return -EINVAL;
  400. stuser->data_len = size - sizeof(stuser->cmd);
  401. rc = lock_mutex_and_test_alive(stdev);
  402. if (rc)
  403. return rc;
  404. if (stuser->state != MRPC_IDLE) {
  405. rc = -EBADE;
  406. goto out;
  407. }
  408. rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
  409. if (rc) {
  410. rc = -EFAULT;
  411. goto out;
  412. }
  413. if (((MRPC_CMD_ID(stuser->cmd) == MRPC_GAS_WRITE) ||
  414. (MRPC_CMD_ID(stuser->cmd) == MRPC_GAS_READ)) &&
  415. !capable(CAP_SYS_ADMIN)) {
  416. rc = -EPERM;
  417. goto out;
  418. }
  419. data += sizeof(stuser->cmd);
  420. rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
  421. if (rc) {
  422. rc = -EFAULT;
  423. goto out;
  424. }
  425. rc = mrpc_queue_cmd(stuser);
  426. out:
  427. mutex_unlock(&stdev->mrpc_mutex);
  428. if (rc)
  429. return rc;
  430. return size;
  431. }
  432. static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
  433. size_t size, loff_t *off)
  434. {
  435. struct switchtec_user *stuser = filp->private_data;
  436. struct switchtec_dev *stdev = stuser->stdev;
  437. int rc;
  438. if (size < sizeof(stuser->cmd) ||
  439. size > sizeof(stuser->cmd) + sizeof(stuser->data))
  440. return -EINVAL;
  441. rc = lock_mutex_and_test_alive(stdev);
  442. if (rc)
  443. return rc;
  444. if (stuser->state == MRPC_IDLE) {
  445. mutex_unlock(&stdev->mrpc_mutex);
  446. return -EBADE;
  447. }
  448. stuser->read_len = size - sizeof(stuser->return_code);
  449. mutex_unlock(&stdev->mrpc_mutex);
  450. if (filp->f_flags & O_NONBLOCK) {
  451. if (!stuser->cmd_done)
  452. return -EAGAIN;
  453. } else {
  454. rc = wait_event_interruptible(stuser->cmd_comp,
  455. stuser->cmd_done);
  456. if (rc < 0)
  457. return rc;
  458. }
  459. rc = lock_mutex_and_test_alive(stdev);
  460. if (rc)
  461. return rc;
  462. if (stuser->state == MRPC_IO_ERROR) {
  463. mutex_unlock(&stdev->mrpc_mutex);
  464. return -EIO;
  465. }
  466. if (stuser->state != MRPC_DONE) {
  467. mutex_unlock(&stdev->mrpc_mutex);
  468. return -EBADE;
  469. }
  470. rc = copy_to_user(data, &stuser->return_code,
  471. sizeof(stuser->return_code));
  472. if (rc) {
  473. mutex_unlock(&stdev->mrpc_mutex);
  474. return -EFAULT;
  475. }
  476. data += sizeof(stuser->return_code);
  477. rc = copy_to_user(data, &stuser->data,
  478. size - sizeof(stuser->return_code));
  479. if (rc) {
  480. mutex_unlock(&stdev->mrpc_mutex);
  481. return -EFAULT;
  482. }
  483. stuser_set_state(stuser, MRPC_IDLE);
  484. mutex_unlock(&stdev->mrpc_mutex);
  485. if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE ||
  486. stuser->status == SWITCHTEC_MRPC_STATUS_ERROR)
  487. return size;
  488. else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
  489. return -ENXIO;
  490. else
  491. return -EBADMSG;
  492. }
  493. static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
  494. {
  495. struct switchtec_user *stuser = filp->private_data;
  496. struct switchtec_dev *stdev = stuser->stdev;
  497. __poll_t ret = 0;
  498. poll_wait(filp, &stuser->cmd_comp, wait);
  499. poll_wait(filp, &stdev->event_wq, wait);
  500. if (lock_mutex_and_test_alive(stdev))
  501. return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
  502. mutex_unlock(&stdev->mrpc_mutex);
  503. if (stuser->cmd_done)
  504. ret |= EPOLLIN | EPOLLRDNORM;
  505. if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
  506. ret |= EPOLLPRI | EPOLLRDBAND;
  507. return ret;
  508. }
  509. static int ioctl_flash_info(struct switchtec_dev *stdev,
  510. struct switchtec_ioctl_flash_info __user *uinfo)
  511. {
  512. struct switchtec_ioctl_flash_info info = {0};
  513. struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
  514. if (stdev->gen == SWITCHTEC_GEN3) {
  515. info.flash_length = ioread32(&fi->gen3.flash_length);
  516. info.num_partitions = SWITCHTEC_NUM_PARTITIONS_GEN3;
  517. } else if (stdev->gen >= SWITCHTEC_GEN4) {
  518. info.flash_length = ioread32(&fi->gen4.flash_length);
  519. info.num_partitions = SWITCHTEC_NUM_PARTITIONS_GEN4;
  520. } else {
  521. return -EOPNOTSUPP;
  522. }
  523. if (copy_to_user(uinfo, &info, sizeof(info)))
  524. return -EFAULT;
  525. return 0;
  526. }
  527. static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
  528. struct partition_info __iomem *pi)
  529. {
  530. info->address = ioread32(&pi->address);
  531. info->length = ioread32(&pi->length);
  532. }
  533. static int flash_part_info_gen3(struct switchtec_dev *stdev,
  534. struct switchtec_ioctl_flash_part_info *info)
  535. {
  536. struct flash_info_regs_gen3 __iomem *fi =
  537. &stdev->mmio_flash_info->gen3;
  538. struct sys_info_regs_gen3 __iomem *si = &stdev->mmio_sys_info->gen3;
  539. u32 active_addr = -1;
  540. switch (info->flash_partition) {
  541. case SWITCHTEC_IOCTL_PART_CFG0:
  542. active_addr = ioread32(&fi->active_cfg);
  543. set_fw_info_part(info, &fi->cfg0);
  544. if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG0_RUNNING)
  545. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  546. break;
  547. case SWITCHTEC_IOCTL_PART_CFG1:
  548. active_addr = ioread32(&fi->active_cfg);
  549. set_fw_info_part(info, &fi->cfg1);
  550. if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG1_RUNNING)
  551. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  552. break;
  553. case SWITCHTEC_IOCTL_PART_IMG0:
  554. active_addr = ioread32(&fi->active_img);
  555. set_fw_info_part(info, &fi->img0);
  556. if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG0_RUNNING)
  557. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  558. break;
  559. case SWITCHTEC_IOCTL_PART_IMG1:
  560. active_addr = ioread32(&fi->active_img);
  561. set_fw_info_part(info, &fi->img1);
  562. if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG1_RUNNING)
  563. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  564. break;
  565. case SWITCHTEC_IOCTL_PART_NVLOG:
  566. set_fw_info_part(info, &fi->nvlog);
  567. break;
  568. case SWITCHTEC_IOCTL_PART_VENDOR0:
  569. set_fw_info_part(info, &fi->vendor[0]);
  570. break;
  571. case SWITCHTEC_IOCTL_PART_VENDOR1:
  572. set_fw_info_part(info, &fi->vendor[1]);
  573. break;
  574. case SWITCHTEC_IOCTL_PART_VENDOR2:
  575. set_fw_info_part(info, &fi->vendor[2]);
  576. break;
  577. case SWITCHTEC_IOCTL_PART_VENDOR3:
  578. set_fw_info_part(info, &fi->vendor[3]);
  579. break;
  580. case SWITCHTEC_IOCTL_PART_VENDOR4:
  581. set_fw_info_part(info, &fi->vendor[4]);
  582. break;
  583. case SWITCHTEC_IOCTL_PART_VENDOR5:
  584. set_fw_info_part(info, &fi->vendor[5]);
  585. break;
  586. case SWITCHTEC_IOCTL_PART_VENDOR6:
  587. set_fw_info_part(info, &fi->vendor[6]);
  588. break;
  589. case SWITCHTEC_IOCTL_PART_VENDOR7:
  590. set_fw_info_part(info, &fi->vendor[7]);
  591. break;
  592. default:
  593. return -EINVAL;
  594. }
  595. if (info->address == active_addr)
  596. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  597. return 0;
  598. }
  599. static int flash_part_info_gen4(struct switchtec_dev *stdev,
  600. struct switchtec_ioctl_flash_part_info *info)
  601. {
  602. struct flash_info_regs_gen4 __iomem *fi = &stdev->mmio_flash_info->gen4;
  603. struct sys_info_regs_gen4 __iomem *si = &stdev->mmio_sys_info->gen4;
  604. struct active_partition_info_gen4 __iomem *af = &fi->active_flag;
  605. switch (info->flash_partition) {
  606. case SWITCHTEC_IOCTL_PART_MAP_0:
  607. set_fw_info_part(info, &fi->map0);
  608. break;
  609. case SWITCHTEC_IOCTL_PART_MAP_1:
  610. set_fw_info_part(info, &fi->map1);
  611. break;
  612. case SWITCHTEC_IOCTL_PART_KEY_0:
  613. set_fw_info_part(info, &fi->key0);
  614. if (ioread8(&af->key) == SWITCHTEC_GEN4_KEY0_ACTIVE)
  615. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  616. if (ioread16(&si->key_running) == SWITCHTEC_GEN4_KEY0_RUNNING)
  617. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  618. break;
  619. case SWITCHTEC_IOCTL_PART_KEY_1:
  620. set_fw_info_part(info, &fi->key1);
  621. if (ioread8(&af->key) == SWITCHTEC_GEN4_KEY1_ACTIVE)
  622. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  623. if (ioread16(&si->key_running) == SWITCHTEC_GEN4_KEY1_RUNNING)
  624. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  625. break;
  626. case SWITCHTEC_IOCTL_PART_BL2_0:
  627. set_fw_info_part(info, &fi->bl2_0);
  628. if (ioread8(&af->bl2) == SWITCHTEC_GEN4_BL2_0_ACTIVE)
  629. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  630. if (ioread16(&si->bl2_running) == SWITCHTEC_GEN4_BL2_0_RUNNING)
  631. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  632. break;
  633. case SWITCHTEC_IOCTL_PART_BL2_1:
  634. set_fw_info_part(info, &fi->bl2_1);
  635. if (ioread8(&af->bl2) == SWITCHTEC_GEN4_BL2_1_ACTIVE)
  636. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  637. if (ioread16(&si->bl2_running) == SWITCHTEC_GEN4_BL2_1_RUNNING)
  638. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  639. break;
  640. case SWITCHTEC_IOCTL_PART_CFG0:
  641. set_fw_info_part(info, &fi->cfg0);
  642. if (ioread8(&af->cfg) == SWITCHTEC_GEN4_CFG0_ACTIVE)
  643. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  644. if (ioread16(&si->cfg_running) == SWITCHTEC_GEN4_CFG0_RUNNING)
  645. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  646. break;
  647. case SWITCHTEC_IOCTL_PART_CFG1:
  648. set_fw_info_part(info, &fi->cfg1);
  649. if (ioread8(&af->cfg) == SWITCHTEC_GEN4_CFG1_ACTIVE)
  650. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  651. if (ioread16(&si->cfg_running) == SWITCHTEC_GEN4_CFG1_RUNNING)
  652. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  653. break;
  654. case SWITCHTEC_IOCTL_PART_IMG0:
  655. set_fw_info_part(info, &fi->img0);
  656. if (ioread8(&af->img) == SWITCHTEC_GEN4_IMG0_ACTIVE)
  657. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  658. if (ioread16(&si->img_running) == SWITCHTEC_GEN4_IMG0_RUNNING)
  659. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  660. break;
  661. case SWITCHTEC_IOCTL_PART_IMG1:
  662. set_fw_info_part(info, &fi->img1);
  663. if (ioread8(&af->img) == SWITCHTEC_GEN4_IMG1_ACTIVE)
  664. info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
  665. if (ioread16(&si->img_running) == SWITCHTEC_GEN4_IMG1_RUNNING)
  666. info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
  667. break;
  668. case SWITCHTEC_IOCTL_PART_NVLOG:
  669. set_fw_info_part(info, &fi->nvlog);
  670. break;
  671. case SWITCHTEC_IOCTL_PART_VENDOR0:
  672. set_fw_info_part(info, &fi->vendor[0]);
  673. break;
  674. case SWITCHTEC_IOCTL_PART_VENDOR1:
  675. set_fw_info_part(info, &fi->vendor[1]);
  676. break;
  677. case SWITCHTEC_IOCTL_PART_VENDOR2:
  678. set_fw_info_part(info, &fi->vendor[2]);
  679. break;
  680. case SWITCHTEC_IOCTL_PART_VENDOR3:
  681. set_fw_info_part(info, &fi->vendor[3]);
  682. break;
  683. case SWITCHTEC_IOCTL_PART_VENDOR4:
  684. set_fw_info_part(info, &fi->vendor[4]);
  685. break;
  686. case SWITCHTEC_IOCTL_PART_VENDOR5:
  687. set_fw_info_part(info, &fi->vendor[5]);
  688. break;
  689. case SWITCHTEC_IOCTL_PART_VENDOR6:
  690. set_fw_info_part(info, &fi->vendor[6]);
  691. break;
  692. case SWITCHTEC_IOCTL_PART_VENDOR7:
  693. set_fw_info_part(info, &fi->vendor[7]);
  694. break;
  695. default:
  696. return -EINVAL;
  697. }
  698. return 0;
  699. }
  700. static int ioctl_flash_part_info(struct switchtec_dev *stdev,
  701. struct switchtec_ioctl_flash_part_info __user *uinfo)
  702. {
  703. int ret;
  704. struct switchtec_ioctl_flash_part_info info = {0};
  705. if (copy_from_user(&info, uinfo, sizeof(info)))
  706. return -EFAULT;
  707. if (stdev->gen == SWITCHTEC_GEN3) {
  708. ret = flash_part_info_gen3(stdev, &info);
  709. if (ret)
  710. return ret;
  711. } else if (stdev->gen >= SWITCHTEC_GEN4) {
  712. ret = flash_part_info_gen4(stdev, &info);
  713. if (ret)
  714. return ret;
  715. } else {
  716. return -EOPNOTSUPP;
  717. }
  718. if (copy_to_user(uinfo, &info, sizeof(info)))
  719. return -EFAULT;
  720. return 0;
  721. }
  722. static int ioctl_event_summary(struct switchtec_dev *stdev,
  723. struct switchtec_user *stuser,
  724. struct switchtec_ioctl_event_summary __user *usum,
  725. size_t size)
  726. {
  727. struct switchtec_ioctl_event_summary *s;
  728. int i;
  729. u32 reg;
  730. int ret = 0;
  731. s = kzalloc_obj(*s);
  732. if (!s)
  733. return -ENOMEM;
  734. s->global = ioread32(&stdev->mmio_sw_event->global_summary);
  735. s->part_bitmap = ioread64(&stdev->mmio_sw_event->part_event_bitmap);
  736. s->local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
  737. for (i = 0; i < stdev->partition_count; i++) {
  738. reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
  739. s->part[i] = reg;
  740. }
  741. for (i = 0; i < stdev->pff_csr_count; i++) {
  742. reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
  743. s->pff[i] = reg;
  744. }
  745. if (copy_to_user(usum, s, size)) {
  746. ret = -EFAULT;
  747. goto error_case;
  748. }
  749. stuser->event_cnt = atomic_read(&stdev->event_cnt);
  750. error_case:
  751. kfree(s);
  752. return ret;
  753. }
  754. static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
  755. size_t offset, int index)
  756. {
  757. return (void __iomem *)stdev->mmio_sw_event + offset;
  758. }
  759. static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
  760. size_t offset, int index)
  761. {
  762. return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
  763. }
  764. static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
  765. size_t offset, int index)
  766. {
  767. return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
  768. }
  769. #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
  770. #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
  771. #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
  772. static const struct event_reg {
  773. size_t offset;
  774. u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
  775. size_t offset, int index);
  776. } event_regs[] = {
  777. EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
  778. EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
  779. EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
  780. EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
  781. EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
  782. EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
  783. EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
  784. EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
  785. EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
  786. EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
  787. twi_mrpc_comp_async_hdr),
  788. EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
  789. EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
  790. cli_mrpc_comp_async_hdr),
  791. EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
  792. EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
  793. EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
  794. EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
  795. EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
  796. EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
  797. EV_PAR(SWITCHTEC_IOCTL_EVENT_INTERCOMM_REQ_NOTIFY,
  798. intercomm_notify_hdr),
  799. EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
  800. EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
  801. EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
  802. EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
  803. EV_PFF(SWITCHTEC_IOCTL_EVENT_UEC, uec_hdr),
  804. EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
  805. EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
  806. EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
  807. EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
  808. EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
  809. EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
  810. EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
  811. EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
  812. };
  813. static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
  814. int event_id, int index)
  815. {
  816. size_t off;
  817. if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
  818. return (u32 __iomem *)ERR_PTR(-EINVAL);
  819. off = event_regs[event_id].offset;
  820. if (event_regs[event_id].map_reg == part_ev_reg) {
  821. if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
  822. index = stdev->partition;
  823. else if (index < 0 || index >= stdev->partition_count)
  824. return (u32 __iomem *)ERR_PTR(-EINVAL);
  825. } else if (event_regs[event_id].map_reg == pff_ev_reg) {
  826. if (index < 0 || index >= stdev->pff_csr_count)
  827. return (u32 __iomem *)ERR_PTR(-EINVAL);
  828. }
  829. return event_regs[event_id].map_reg(stdev, off, index);
  830. }
  831. static int event_ctl(struct switchtec_dev *stdev,
  832. struct switchtec_ioctl_event_ctl *ctl)
  833. {
  834. int i;
  835. u32 __iomem *reg;
  836. u32 hdr;
  837. reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
  838. if (IS_ERR(reg))
  839. return PTR_ERR(reg);
  840. hdr = ioread32(reg);
  841. if (hdr & SWITCHTEC_EVENT_NOT_SUPP)
  842. return -EOPNOTSUPP;
  843. for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
  844. ctl->data[i] = ioread32(&reg[i + 1]);
  845. ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
  846. ctl->count = (hdr >> 5) & 0xFF;
  847. if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
  848. hdr &= ~SWITCHTEC_EVENT_CLEAR;
  849. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
  850. hdr |= SWITCHTEC_EVENT_EN_IRQ;
  851. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
  852. hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
  853. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
  854. hdr |= SWITCHTEC_EVENT_EN_LOG;
  855. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
  856. hdr &= ~SWITCHTEC_EVENT_EN_LOG;
  857. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
  858. hdr |= SWITCHTEC_EVENT_EN_CLI;
  859. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
  860. hdr &= ~SWITCHTEC_EVENT_EN_CLI;
  861. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
  862. hdr |= SWITCHTEC_EVENT_FATAL;
  863. if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
  864. hdr &= ~SWITCHTEC_EVENT_FATAL;
  865. if (ctl->flags)
  866. iowrite32(hdr, reg);
  867. ctl->flags = 0;
  868. if (hdr & SWITCHTEC_EVENT_EN_IRQ)
  869. ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
  870. if (hdr & SWITCHTEC_EVENT_EN_LOG)
  871. ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
  872. if (hdr & SWITCHTEC_EVENT_EN_CLI)
  873. ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
  874. if (hdr & SWITCHTEC_EVENT_FATAL)
  875. ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
  876. return 0;
  877. }
  878. static int ioctl_event_ctl(struct switchtec_dev *stdev,
  879. struct switchtec_ioctl_event_ctl __user *uctl)
  880. {
  881. int ret;
  882. int nr_idxs;
  883. unsigned int event_flags;
  884. struct switchtec_ioctl_event_ctl ctl;
  885. if (copy_from_user(&ctl, uctl, sizeof(ctl)))
  886. return -EFAULT;
  887. if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
  888. return -EINVAL;
  889. if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
  890. return -EINVAL;
  891. if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
  892. if (event_regs[ctl.event_id].map_reg == global_ev_reg)
  893. nr_idxs = 1;
  894. else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
  895. nr_idxs = stdev->partition_count;
  896. else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
  897. nr_idxs = stdev->pff_csr_count;
  898. else
  899. return -EINVAL;
  900. event_flags = ctl.flags;
  901. for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
  902. ctl.flags = event_flags;
  903. ret = event_ctl(stdev, &ctl);
  904. if (ret < 0 && ret != -EOPNOTSUPP)
  905. return ret;
  906. }
  907. } else {
  908. ret = event_ctl(stdev, &ctl);
  909. if (ret < 0)
  910. return ret;
  911. }
  912. if (copy_to_user(uctl, &ctl, sizeof(ctl)))
  913. return -EFAULT;
  914. return 0;
  915. }
  916. static int ioctl_pff_to_port(struct switchtec_dev *stdev,
  917. struct switchtec_ioctl_pff_port __user *up)
  918. {
  919. int i, part;
  920. u32 reg;
  921. struct part_cfg_regs __iomem *pcfg;
  922. struct switchtec_ioctl_pff_port p;
  923. if (copy_from_user(&p, up, sizeof(p)))
  924. return -EFAULT;
  925. p.port = -1;
  926. for (part = 0; part < stdev->partition_count; part++) {
  927. pcfg = &stdev->mmio_part_cfg_all[part];
  928. p.partition = part;
  929. reg = ioread32(&pcfg->usp_pff_inst_id);
  930. if (reg == p.pff) {
  931. p.port = 0;
  932. break;
  933. }
  934. reg = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
  935. if (reg == p.pff) {
  936. p.port = SWITCHTEC_IOCTL_PFF_VEP;
  937. break;
  938. }
  939. for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
  940. reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
  941. if (reg != p.pff)
  942. continue;
  943. p.port = i + 1;
  944. break;
  945. }
  946. if (p.port != -1)
  947. break;
  948. }
  949. if (copy_to_user(up, &p, sizeof(p)))
  950. return -EFAULT;
  951. return 0;
  952. }
  953. static int ioctl_port_to_pff(struct switchtec_dev *stdev,
  954. struct switchtec_ioctl_pff_port __user *up)
  955. {
  956. struct switchtec_ioctl_pff_port p;
  957. struct part_cfg_regs __iomem *pcfg;
  958. if (copy_from_user(&p, up, sizeof(p)))
  959. return -EFAULT;
  960. if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
  961. pcfg = stdev->mmio_part_cfg;
  962. else if (p.partition < stdev->partition_count)
  963. pcfg = &stdev->mmio_part_cfg_all[p.partition];
  964. else
  965. return -EINVAL;
  966. switch (p.port) {
  967. case 0:
  968. p.pff = ioread32(&pcfg->usp_pff_inst_id);
  969. break;
  970. case SWITCHTEC_IOCTL_PFF_VEP:
  971. p.pff = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
  972. break;
  973. default:
  974. if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
  975. return -EINVAL;
  976. p.port = array_index_nospec(p.port,
  977. ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
  978. p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
  979. break;
  980. }
  981. if (copy_to_user(up, &p, sizeof(p)))
  982. return -EFAULT;
  983. return 0;
  984. }
  985. static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
  986. unsigned long arg)
  987. {
  988. struct switchtec_user *stuser = filp->private_data;
  989. struct switchtec_dev *stdev = stuser->stdev;
  990. int rc;
  991. void __user *argp = (void __user *)arg;
  992. rc = lock_mutex_and_test_alive(stdev);
  993. if (rc)
  994. return rc;
  995. switch (cmd) {
  996. case SWITCHTEC_IOCTL_FLASH_INFO:
  997. rc = ioctl_flash_info(stdev, argp);
  998. break;
  999. case SWITCHTEC_IOCTL_FLASH_PART_INFO:
  1000. rc = ioctl_flash_part_info(stdev, argp);
  1001. break;
  1002. case SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY:
  1003. rc = ioctl_event_summary(stdev, stuser, argp,
  1004. sizeof(struct switchtec_ioctl_event_summary_legacy));
  1005. break;
  1006. case SWITCHTEC_IOCTL_EVENT_CTL:
  1007. rc = ioctl_event_ctl(stdev, argp);
  1008. break;
  1009. case SWITCHTEC_IOCTL_PFF_TO_PORT:
  1010. rc = ioctl_pff_to_port(stdev, argp);
  1011. break;
  1012. case SWITCHTEC_IOCTL_PORT_TO_PFF:
  1013. rc = ioctl_port_to_pff(stdev, argp);
  1014. break;
  1015. case SWITCHTEC_IOCTL_EVENT_SUMMARY:
  1016. rc = ioctl_event_summary(stdev, stuser, argp,
  1017. sizeof(struct switchtec_ioctl_event_summary));
  1018. break;
  1019. default:
  1020. rc = -ENOTTY;
  1021. break;
  1022. }
  1023. mutex_unlock(&stdev->mrpc_mutex);
  1024. return rc;
  1025. }
  1026. static const struct file_operations switchtec_fops = {
  1027. .owner = THIS_MODULE,
  1028. .open = switchtec_dev_open,
  1029. .release = switchtec_dev_release,
  1030. .write = switchtec_dev_write,
  1031. .read = switchtec_dev_read,
  1032. .poll = switchtec_dev_poll,
  1033. .unlocked_ioctl = switchtec_dev_ioctl,
  1034. .compat_ioctl = compat_ptr_ioctl,
  1035. };
  1036. static void link_event_work(struct work_struct *work)
  1037. {
  1038. struct switchtec_dev *stdev;
  1039. stdev = container_of(work, struct switchtec_dev, link_event_work);
  1040. if (stdev->link_notifier)
  1041. stdev->link_notifier(stdev);
  1042. }
  1043. static void check_link_state_events(struct switchtec_dev *stdev)
  1044. {
  1045. int idx;
  1046. u32 reg;
  1047. int count;
  1048. int occurred = 0;
  1049. for (idx = 0; idx < stdev->pff_csr_count; idx++) {
  1050. reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
  1051. dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
  1052. count = (reg >> 5) & 0xFF;
  1053. if (count != stdev->link_event_count[idx]) {
  1054. occurred = 1;
  1055. stdev->link_event_count[idx] = count;
  1056. }
  1057. }
  1058. if (occurred)
  1059. schedule_work(&stdev->link_event_work);
  1060. }
  1061. static void enable_link_state_events(struct switchtec_dev *stdev)
  1062. {
  1063. int idx;
  1064. for (idx = 0; idx < stdev->pff_csr_count; idx++) {
  1065. iowrite32(SWITCHTEC_EVENT_CLEAR |
  1066. SWITCHTEC_EVENT_EN_IRQ,
  1067. &stdev->mmio_pff_csr[idx].link_state_hdr);
  1068. }
  1069. }
  1070. static void enable_dma_mrpc(struct switchtec_dev *stdev)
  1071. {
  1072. writeq(stdev->dma_mrpc_dma_addr, &stdev->mmio_mrpc->dma_addr);
  1073. flush_wc_buf(stdev);
  1074. iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
  1075. }
  1076. static void stdev_release(struct device *dev)
  1077. {
  1078. struct switchtec_dev *stdev = to_stdev(dev);
  1079. kfree(stdev);
  1080. }
  1081. static void stdev_kill(struct switchtec_dev *stdev)
  1082. {
  1083. struct switchtec_user *stuser, *tmpuser;
  1084. pci_clear_master(stdev->pdev);
  1085. cancel_delayed_work_sync(&stdev->mrpc_timeout);
  1086. /* Mark the hardware as unavailable and complete all completions */
  1087. scoped_guard (mutex, &stdev->mrpc_mutex) {
  1088. stdev->alive = false;
  1089. /* Wake up and kill any users waiting on an MRPC request */
  1090. list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
  1091. stuser->cmd_done = true;
  1092. wake_up_interruptible(&stuser->cmd_comp);
  1093. list_del_init(&stuser->list);
  1094. stuser_put(stuser);
  1095. }
  1096. }
  1097. /* Wake up any users waiting on event_wq */
  1098. wake_up_interruptible(&stdev->event_wq);
  1099. }
  1100. static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
  1101. {
  1102. struct switchtec_dev *stdev;
  1103. int minor;
  1104. struct device *dev;
  1105. struct cdev *cdev;
  1106. int rc;
  1107. stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
  1108. dev_to_node(&pdev->dev));
  1109. if (!stdev)
  1110. return ERR_PTR(-ENOMEM);
  1111. stdev->alive = true;
  1112. stdev->pdev = pci_dev_get(pdev);
  1113. INIT_LIST_HEAD(&stdev->mrpc_queue);
  1114. mutex_init(&stdev->mrpc_mutex);
  1115. stdev->mrpc_busy = 0;
  1116. INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
  1117. INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
  1118. INIT_WORK(&stdev->link_event_work, link_event_work);
  1119. init_waitqueue_head(&stdev->event_wq);
  1120. atomic_set(&stdev->event_cnt, 0);
  1121. dev = &stdev->dev;
  1122. device_initialize(dev);
  1123. dev->class = &switchtec_class;
  1124. dev->parent = &pdev->dev;
  1125. dev->groups = switchtec_device_groups;
  1126. dev->release = stdev_release;
  1127. minor = ida_alloc(&switchtec_minor_ida, GFP_KERNEL);
  1128. if (minor < 0) {
  1129. rc = minor;
  1130. goto err_put;
  1131. }
  1132. dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
  1133. dev_set_name(dev, "switchtec%d", minor);
  1134. cdev = &stdev->cdev;
  1135. cdev_init(cdev, &switchtec_fops);
  1136. cdev->owner = THIS_MODULE;
  1137. return stdev;
  1138. err_put:
  1139. pci_dev_put(stdev->pdev);
  1140. put_device(&stdev->dev);
  1141. return ERR_PTR(rc);
  1142. }
  1143. static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
  1144. {
  1145. size_t off = event_regs[eid].offset;
  1146. u32 __iomem *hdr_reg;
  1147. u32 hdr;
  1148. hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
  1149. hdr = ioread32(hdr_reg);
  1150. if (hdr & SWITCHTEC_EVENT_NOT_SUPP)
  1151. return 0;
  1152. if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
  1153. return 0;
  1154. dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
  1155. hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
  1156. iowrite32(hdr, hdr_reg);
  1157. return 1;
  1158. }
  1159. static int mask_all_events(struct switchtec_dev *stdev, int eid)
  1160. {
  1161. int idx;
  1162. int count = 0;
  1163. if (event_regs[eid].map_reg == part_ev_reg) {
  1164. for (idx = 0; idx < stdev->partition_count; idx++)
  1165. count += mask_event(stdev, eid, idx);
  1166. } else if (event_regs[eid].map_reg == pff_ev_reg) {
  1167. for (idx = 0; idx < stdev->pff_csr_count; idx++) {
  1168. if (!stdev->pff_local[idx])
  1169. continue;
  1170. count += mask_event(stdev, eid, idx);
  1171. }
  1172. } else {
  1173. count += mask_event(stdev, eid, 0);
  1174. }
  1175. return count;
  1176. }
  1177. static irqreturn_t switchtec_event_isr(int irq, void *dev)
  1178. {
  1179. struct switchtec_dev *stdev = dev;
  1180. u32 reg;
  1181. irqreturn_t ret = IRQ_NONE;
  1182. int eid, event_count = 0;
  1183. reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
  1184. if (reg & SWITCHTEC_EVENT_OCCURRED) {
  1185. dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
  1186. ret = IRQ_HANDLED;
  1187. schedule_work(&stdev->mrpc_work);
  1188. iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
  1189. }
  1190. check_link_state_events(stdev);
  1191. for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++) {
  1192. if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE ||
  1193. eid == SWITCHTEC_IOCTL_EVENT_MRPC_COMP)
  1194. continue;
  1195. event_count += mask_all_events(stdev, eid);
  1196. }
  1197. if (event_count) {
  1198. atomic_inc(&stdev->event_cnt);
  1199. wake_up_interruptible(&stdev->event_wq);
  1200. dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
  1201. event_count);
  1202. return IRQ_HANDLED;
  1203. }
  1204. return ret;
  1205. }
  1206. static irqreturn_t switchtec_dma_mrpc_isr(int irq, void *dev)
  1207. {
  1208. struct switchtec_dev *stdev = dev;
  1209. iowrite32(SWITCHTEC_EVENT_CLEAR |
  1210. SWITCHTEC_EVENT_EN_IRQ,
  1211. &stdev->mmio_part_cfg->mrpc_comp_hdr);
  1212. schedule_work(&stdev->mrpc_work);
  1213. return IRQ_HANDLED;
  1214. }
  1215. static int switchtec_init_isr(struct switchtec_dev *stdev)
  1216. {
  1217. int nvecs;
  1218. int event_irq;
  1219. int dma_mrpc_irq;
  1220. int rc;
  1221. if (nirqs < 4)
  1222. nirqs = 4;
  1223. nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, nirqs,
  1224. PCI_IRQ_MSIX | PCI_IRQ_MSI |
  1225. PCI_IRQ_VIRTUAL);
  1226. if (nvecs < 0)
  1227. return nvecs;
  1228. event_irq = ioread16(&stdev->mmio_part_cfg->vep_vector_number);
  1229. if (event_irq < 0 || event_irq >= nvecs)
  1230. return -EFAULT;
  1231. event_irq = pci_irq_vector(stdev->pdev, event_irq);
  1232. if (event_irq < 0)
  1233. return event_irq;
  1234. rc = devm_request_irq(&stdev->pdev->dev, event_irq,
  1235. switchtec_event_isr, 0,
  1236. KBUILD_MODNAME, stdev);
  1237. if (rc)
  1238. return rc;
  1239. if (!stdev->dma_mrpc)
  1240. return rc;
  1241. dma_mrpc_irq = ioread32(&stdev->mmio_mrpc->dma_vector);
  1242. if (dma_mrpc_irq < 0 || dma_mrpc_irq >= nvecs)
  1243. return -EFAULT;
  1244. dma_mrpc_irq = pci_irq_vector(stdev->pdev, dma_mrpc_irq);
  1245. if (dma_mrpc_irq < 0)
  1246. return dma_mrpc_irq;
  1247. rc = devm_request_irq(&stdev->pdev->dev, dma_mrpc_irq,
  1248. switchtec_dma_mrpc_isr, 0,
  1249. KBUILD_MODNAME, stdev);
  1250. return rc;
  1251. }
  1252. static void init_pff(struct switchtec_dev *stdev)
  1253. {
  1254. int i;
  1255. u32 reg;
  1256. struct part_cfg_regs __iomem *pcfg = stdev->mmio_part_cfg;
  1257. for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
  1258. reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
  1259. if (reg != PCI_VENDOR_ID_MICROSEMI)
  1260. break;
  1261. }
  1262. stdev->pff_csr_count = i;
  1263. reg = ioread32(&pcfg->usp_pff_inst_id);
  1264. if (reg < stdev->pff_csr_count)
  1265. stdev->pff_local[reg] = 1;
  1266. reg = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
  1267. if (reg < stdev->pff_csr_count)
  1268. stdev->pff_local[reg] = 1;
  1269. for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
  1270. reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
  1271. if (reg < stdev->pff_csr_count)
  1272. stdev->pff_local[reg] = 1;
  1273. }
  1274. }
  1275. static int switchtec_init_pci(struct switchtec_dev *stdev,
  1276. struct pci_dev *pdev)
  1277. {
  1278. int rc;
  1279. void __iomem *map;
  1280. unsigned long res_start, res_len;
  1281. u32 __iomem *part_id;
  1282. rc = pcim_enable_device(pdev);
  1283. if (rc)
  1284. return rc;
  1285. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  1286. if (rc)
  1287. return rc;
  1288. pci_set_master(pdev);
  1289. res_start = pci_resource_start(pdev, 0);
  1290. res_len = pci_resource_len(pdev, 0);
  1291. if (!devm_request_mem_region(&pdev->dev, res_start,
  1292. res_len, KBUILD_MODNAME))
  1293. return -EBUSY;
  1294. stdev->mmio_mrpc = devm_ioremap_wc(&pdev->dev, res_start,
  1295. SWITCHTEC_GAS_TOP_CFG_OFFSET);
  1296. if (!stdev->mmio_mrpc)
  1297. return -ENOMEM;
  1298. map = devm_ioremap(&pdev->dev,
  1299. res_start + SWITCHTEC_GAS_TOP_CFG_OFFSET,
  1300. res_len - SWITCHTEC_GAS_TOP_CFG_OFFSET);
  1301. if (!map)
  1302. return -ENOMEM;
  1303. stdev->mmio = map - SWITCHTEC_GAS_TOP_CFG_OFFSET;
  1304. stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
  1305. stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
  1306. stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
  1307. stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
  1308. if (stdev->gen == SWITCHTEC_GEN3)
  1309. part_id = &stdev->mmio_sys_info->gen3.partition_id;
  1310. else if (stdev->gen >= SWITCHTEC_GEN4)
  1311. part_id = &stdev->mmio_sys_info->gen4.partition_id;
  1312. else
  1313. return -EOPNOTSUPP;
  1314. stdev->partition = ioread8(part_id);
  1315. stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
  1316. stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
  1317. stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
  1318. stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
  1319. if (stdev->partition_count < 1)
  1320. stdev->partition_count = 1;
  1321. init_pff(stdev);
  1322. pci_set_drvdata(pdev, stdev);
  1323. if (!use_dma_mrpc)
  1324. return 0;
  1325. if (ioread32(&stdev->mmio_mrpc->dma_ver) == 0)
  1326. return 0;
  1327. stdev->dma_mrpc = dma_alloc_coherent(&stdev->pdev->dev,
  1328. sizeof(*stdev->dma_mrpc),
  1329. &stdev->dma_mrpc_dma_addr,
  1330. GFP_KERNEL);
  1331. if (stdev->dma_mrpc == NULL)
  1332. return -ENOMEM;
  1333. return 0;
  1334. }
  1335. static void switchtec_exit_pci(struct switchtec_dev *stdev)
  1336. {
  1337. if (stdev->dma_mrpc) {
  1338. iowrite32(0, &stdev->mmio_mrpc->dma_en);
  1339. flush_wc_buf(stdev);
  1340. writeq(0, &stdev->mmio_mrpc->dma_addr);
  1341. dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
  1342. stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
  1343. stdev->dma_mrpc = NULL;
  1344. }
  1345. }
  1346. static int switchtec_pci_probe(struct pci_dev *pdev,
  1347. const struct pci_device_id *id)
  1348. {
  1349. struct switchtec_dev *stdev;
  1350. int rc;
  1351. if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
  1352. request_module_nowait("ntb_hw_switchtec");
  1353. stdev = stdev_create(pdev);
  1354. if (IS_ERR(stdev))
  1355. return PTR_ERR(stdev);
  1356. stdev->gen = id->driver_data;
  1357. rc = switchtec_init_pci(stdev, pdev);
  1358. if (rc)
  1359. goto err_put;
  1360. rc = switchtec_init_isr(stdev);
  1361. if (rc) {
  1362. dev_err(&stdev->dev, "failed to init isr.\n");
  1363. goto err_exit_pci;
  1364. }
  1365. iowrite32(SWITCHTEC_EVENT_CLEAR |
  1366. SWITCHTEC_EVENT_EN_IRQ,
  1367. &stdev->mmio_part_cfg->mrpc_comp_hdr);
  1368. enable_link_state_events(stdev);
  1369. if (stdev->dma_mrpc)
  1370. enable_dma_mrpc(stdev);
  1371. rc = cdev_device_add(&stdev->cdev, &stdev->dev);
  1372. if (rc)
  1373. goto err_devadd;
  1374. dev_info(&stdev->dev, "Management device registered.\n");
  1375. return 0;
  1376. err_devadd:
  1377. stdev_kill(stdev);
  1378. err_exit_pci:
  1379. switchtec_exit_pci(stdev);
  1380. err_put:
  1381. ida_free(&switchtec_minor_ida, MINOR(stdev->dev.devt));
  1382. put_device(&stdev->dev);
  1383. return rc;
  1384. }
  1385. static void switchtec_pci_remove(struct pci_dev *pdev)
  1386. {
  1387. struct switchtec_dev *stdev = pci_get_drvdata(pdev);
  1388. pci_set_drvdata(pdev, NULL);
  1389. cdev_device_del(&stdev->cdev, &stdev->dev);
  1390. ida_free(&switchtec_minor_ida, MINOR(stdev->dev.devt));
  1391. dev_info(&stdev->dev, "unregistered.\n");
  1392. stdev_kill(stdev);
  1393. switchtec_exit_pci(stdev);
  1394. pci_dev_put(stdev->pdev);
  1395. stdev->pdev = NULL;
  1396. put_device(&stdev->dev);
  1397. }
  1398. #define SWITCHTEC_PCI_DEVICE(device_id, gen) \
  1399. { \
  1400. .vendor = PCI_VENDOR_ID_MICROSEMI, \
  1401. .device = device_id, \
  1402. .subvendor = PCI_ANY_ID, \
  1403. .subdevice = PCI_ANY_ID, \
  1404. .class = (PCI_CLASS_MEMORY_OTHER << 8), \
  1405. .class_mask = 0xFFFFFFFF, \
  1406. .driver_data = gen, \
  1407. }, \
  1408. { \
  1409. .vendor = PCI_VENDOR_ID_MICROSEMI, \
  1410. .device = device_id, \
  1411. .subvendor = PCI_ANY_ID, \
  1412. .subdevice = PCI_ANY_ID, \
  1413. .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
  1414. .class_mask = 0xFFFFFFFF, \
  1415. .driver_data = gen, \
  1416. }
  1417. #define SWITCHTEC_PCI100X_DEVICE(device_id, gen) \
  1418. { \
  1419. .vendor = PCI_VENDOR_ID_EFAR, \
  1420. .device = device_id, \
  1421. .subvendor = PCI_ANY_ID, \
  1422. .subdevice = PCI_ANY_ID, \
  1423. .class = (PCI_CLASS_MEMORY_OTHER << 8), \
  1424. .class_mask = 0xFFFFFFFF, \
  1425. .driver_data = gen, \
  1426. }, \
  1427. { \
  1428. .vendor = PCI_VENDOR_ID_EFAR, \
  1429. .device = device_id, \
  1430. .subvendor = PCI_ANY_ID, \
  1431. .subdevice = PCI_ANY_ID, \
  1432. .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
  1433. .class_mask = 0xFFFFFFFF, \
  1434. .driver_data = gen, \
  1435. }
  1436. static const struct pci_device_id switchtec_pci_tbl[] = {
  1437. SWITCHTEC_PCI_DEVICE(0x8531, SWITCHTEC_GEN3), /* PFX 24xG3 */
  1438. SWITCHTEC_PCI_DEVICE(0x8532, SWITCHTEC_GEN3), /* PFX 32xG3 */
  1439. SWITCHTEC_PCI_DEVICE(0x8533, SWITCHTEC_GEN3), /* PFX 48xG3 */
  1440. SWITCHTEC_PCI_DEVICE(0x8534, SWITCHTEC_GEN3), /* PFX 64xG3 */
  1441. SWITCHTEC_PCI_DEVICE(0x8535, SWITCHTEC_GEN3), /* PFX 80xG3 */
  1442. SWITCHTEC_PCI_DEVICE(0x8536, SWITCHTEC_GEN3), /* PFX 96xG3 */
  1443. SWITCHTEC_PCI_DEVICE(0x8541, SWITCHTEC_GEN3), /* PSX 24xG3 */
  1444. SWITCHTEC_PCI_DEVICE(0x8542, SWITCHTEC_GEN3), /* PSX 32xG3 */
  1445. SWITCHTEC_PCI_DEVICE(0x8543, SWITCHTEC_GEN3), /* PSX 48xG3 */
  1446. SWITCHTEC_PCI_DEVICE(0x8544, SWITCHTEC_GEN3), /* PSX 64xG3 */
  1447. SWITCHTEC_PCI_DEVICE(0x8545, SWITCHTEC_GEN3), /* PSX 80xG3 */
  1448. SWITCHTEC_PCI_DEVICE(0x8546, SWITCHTEC_GEN3), /* PSX 96xG3 */
  1449. SWITCHTEC_PCI_DEVICE(0x8551, SWITCHTEC_GEN3), /* PAX 24XG3 */
  1450. SWITCHTEC_PCI_DEVICE(0x8552, SWITCHTEC_GEN3), /* PAX 32XG3 */
  1451. SWITCHTEC_PCI_DEVICE(0x8553, SWITCHTEC_GEN3), /* PAX 48XG3 */
  1452. SWITCHTEC_PCI_DEVICE(0x8554, SWITCHTEC_GEN3), /* PAX 64XG3 */
  1453. SWITCHTEC_PCI_DEVICE(0x8555, SWITCHTEC_GEN3), /* PAX 80XG3 */
  1454. SWITCHTEC_PCI_DEVICE(0x8556, SWITCHTEC_GEN3), /* PAX 96XG3 */
  1455. SWITCHTEC_PCI_DEVICE(0x8561, SWITCHTEC_GEN3), /* PFXL 24XG3 */
  1456. SWITCHTEC_PCI_DEVICE(0x8562, SWITCHTEC_GEN3), /* PFXL 32XG3 */
  1457. SWITCHTEC_PCI_DEVICE(0x8563, SWITCHTEC_GEN3), /* PFXL 48XG3 */
  1458. SWITCHTEC_PCI_DEVICE(0x8564, SWITCHTEC_GEN3), /* PFXL 64XG3 */
  1459. SWITCHTEC_PCI_DEVICE(0x8565, SWITCHTEC_GEN3), /* PFXL 80XG3 */
  1460. SWITCHTEC_PCI_DEVICE(0x8566, SWITCHTEC_GEN3), /* PFXL 96XG3 */
  1461. SWITCHTEC_PCI_DEVICE(0x8571, SWITCHTEC_GEN3), /* PFXI 24XG3 */
  1462. SWITCHTEC_PCI_DEVICE(0x8572, SWITCHTEC_GEN3), /* PFXI 32XG3 */
  1463. SWITCHTEC_PCI_DEVICE(0x8573, SWITCHTEC_GEN3), /* PFXI 48XG3 */
  1464. SWITCHTEC_PCI_DEVICE(0x8574, SWITCHTEC_GEN3), /* PFXI 64XG3 */
  1465. SWITCHTEC_PCI_DEVICE(0x8575, SWITCHTEC_GEN3), /* PFXI 80XG3 */
  1466. SWITCHTEC_PCI_DEVICE(0x8576, SWITCHTEC_GEN3), /* PFXI 96XG3 */
  1467. SWITCHTEC_PCI_DEVICE(0x4000, SWITCHTEC_GEN4), /* PFX 100XG4 */
  1468. SWITCHTEC_PCI_DEVICE(0x4084, SWITCHTEC_GEN4), /* PFX 84XG4 */
  1469. SWITCHTEC_PCI_DEVICE(0x4068, SWITCHTEC_GEN4), /* PFX 68XG4 */
  1470. SWITCHTEC_PCI_DEVICE(0x4052, SWITCHTEC_GEN4), /* PFX 52XG4 */
  1471. SWITCHTEC_PCI_DEVICE(0x4036, SWITCHTEC_GEN4), /* PFX 36XG4 */
  1472. SWITCHTEC_PCI_DEVICE(0x4028, SWITCHTEC_GEN4), /* PFX 28XG4 */
  1473. SWITCHTEC_PCI_DEVICE(0x4100, SWITCHTEC_GEN4), /* PSX 100XG4 */
  1474. SWITCHTEC_PCI_DEVICE(0x4184, SWITCHTEC_GEN4), /* PSX 84XG4 */
  1475. SWITCHTEC_PCI_DEVICE(0x4168, SWITCHTEC_GEN4), /* PSX 68XG4 */
  1476. SWITCHTEC_PCI_DEVICE(0x4152, SWITCHTEC_GEN4), /* PSX 52XG4 */
  1477. SWITCHTEC_PCI_DEVICE(0x4136, SWITCHTEC_GEN4), /* PSX 36XG4 */
  1478. SWITCHTEC_PCI_DEVICE(0x4128, SWITCHTEC_GEN4), /* PSX 28XG4 */
  1479. SWITCHTEC_PCI_DEVICE(0x4200, SWITCHTEC_GEN4), /* PAX 100XG4 */
  1480. SWITCHTEC_PCI_DEVICE(0x4284, SWITCHTEC_GEN4), /* PAX 84XG4 */
  1481. SWITCHTEC_PCI_DEVICE(0x4268, SWITCHTEC_GEN4), /* PAX 68XG4 */
  1482. SWITCHTEC_PCI_DEVICE(0x4252, SWITCHTEC_GEN4), /* PAX 52XG4 */
  1483. SWITCHTEC_PCI_DEVICE(0x4236, SWITCHTEC_GEN4), /* PAX 36XG4 */
  1484. SWITCHTEC_PCI_DEVICE(0x4228, SWITCHTEC_GEN4), /* PAX 28XG4 */
  1485. SWITCHTEC_PCI_DEVICE(0x4352, SWITCHTEC_GEN4), /* PFXA 52XG4 */
  1486. SWITCHTEC_PCI_DEVICE(0x4336, SWITCHTEC_GEN4), /* PFXA 36XG4 */
  1487. SWITCHTEC_PCI_DEVICE(0x4328, SWITCHTEC_GEN4), /* PFXA 28XG4 */
  1488. SWITCHTEC_PCI_DEVICE(0x4452, SWITCHTEC_GEN4), /* PSXA 52XG4 */
  1489. SWITCHTEC_PCI_DEVICE(0x4436, SWITCHTEC_GEN4), /* PSXA 36XG4 */
  1490. SWITCHTEC_PCI_DEVICE(0x4428, SWITCHTEC_GEN4), /* PSXA 28XG4 */
  1491. SWITCHTEC_PCI_DEVICE(0x4552, SWITCHTEC_GEN4), /* PAXA 52XG4 */
  1492. SWITCHTEC_PCI_DEVICE(0x4536, SWITCHTEC_GEN4), /* PAXA 36XG4 */
  1493. SWITCHTEC_PCI_DEVICE(0x4528, SWITCHTEC_GEN4), /* PAXA 28XG4 */
  1494. SWITCHTEC_PCI_DEVICE(0x5000, SWITCHTEC_GEN5), /* PFX 100XG5 */
  1495. SWITCHTEC_PCI_DEVICE(0x5084, SWITCHTEC_GEN5), /* PFX 84XG5 */
  1496. SWITCHTEC_PCI_DEVICE(0x5068, SWITCHTEC_GEN5), /* PFX 68XG5 */
  1497. SWITCHTEC_PCI_DEVICE(0x5052, SWITCHTEC_GEN5), /* PFX 52XG5 */
  1498. SWITCHTEC_PCI_DEVICE(0x5036, SWITCHTEC_GEN5), /* PFX 36XG5 */
  1499. SWITCHTEC_PCI_DEVICE(0x5028, SWITCHTEC_GEN5), /* PFX 28XG5 */
  1500. SWITCHTEC_PCI_DEVICE(0x5100, SWITCHTEC_GEN5), /* PSX 100XG5 */
  1501. SWITCHTEC_PCI_DEVICE(0x5184, SWITCHTEC_GEN5), /* PSX 84XG5 */
  1502. SWITCHTEC_PCI_DEVICE(0x5168, SWITCHTEC_GEN5), /* PSX 68XG5 */
  1503. SWITCHTEC_PCI_DEVICE(0x5152, SWITCHTEC_GEN5), /* PSX 52XG5 */
  1504. SWITCHTEC_PCI_DEVICE(0x5136, SWITCHTEC_GEN5), /* PSX 36XG5 */
  1505. SWITCHTEC_PCI_DEVICE(0x5128, SWITCHTEC_GEN5), /* PSX 28XG5 */
  1506. SWITCHTEC_PCI_DEVICE(0x5200, SWITCHTEC_GEN5), /* PAX 100XG5 */
  1507. SWITCHTEC_PCI_DEVICE(0x5284, SWITCHTEC_GEN5), /* PAX 84XG5 */
  1508. SWITCHTEC_PCI_DEVICE(0x5268, SWITCHTEC_GEN5), /* PAX 68XG5 */
  1509. SWITCHTEC_PCI_DEVICE(0x5252, SWITCHTEC_GEN5), /* PAX 52XG5 */
  1510. SWITCHTEC_PCI_DEVICE(0x5236, SWITCHTEC_GEN5), /* PAX 36XG5 */
  1511. SWITCHTEC_PCI_DEVICE(0x5228, SWITCHTEC_GEN5), /* PAX 28XG5 */
  1512. SWITCHTEC_PCI_DEVICE(0x5300, SWITCHTEC_GEN5), /* PFXA 100XG5 */
  1513. SWITCHTEC_PCI_DEVICE(0x5384, SWITCHTEC_GEN5), /* PFXA 84XG5 */
  1514. SWITCHTEC_PCI_DEVICE(0x5368, SWITCHTEC_GEN5), /* PFXA 68XG5 */
  1515. SWITCHTEC_PCI_DEVICE(0x5352, SWITCHTEC_GEN5), /* PFXA 52XG5 */
  1516. SWITCHTEC_PCI_DEVICE(0x5336, SWITCHTEC_GEN5), /* PFXA 36XG5 */
  1517. SWITCHTEC_PCI_DEVICE(0x5328, SWITCHTEC_GEN5), /* PFXA 28XG5 */
  1518. SWITCHTEC_PCI_DEVICE(0x5400, SWITCHTEC_GEN5), /* PSXA 100XG5 */
  1519. SWITCHTEC_PCI_DEVICE(0x5484, SWITCHTEC_GEN5), /* PSXA 84XG5 */
  1520. SWITCHTEC_PCI_DEVICE(0x5468, SWITCHTEC_GEN5), /* PSXA 68XG5 */
  1521. SWITCHTEC_PCI_DEVICE(0x5452, SWITCHTEC_GEN5), /* PSXA 52XG5 */
  1522. SWITCHTEC_PCI_DEVICE(0x5436, SWITCHTEC_GEN5), /* PSXA 36XG5 */
  1523. SWITCHTEC_PCI_DEVICE(0x5428, SWITCHTEC_GEN5), /* PSXA 28XG5 */
  1524. SWITCHTEC_PCI_DEVICE(0x5500, SWITCHTEC_GEN5), /* PAXA 100XG5 */
  1525. SWITCHTEC_PCI_DEVICE(0x5584, SWITCHTEC_GEN5), /* PAXA 84XG5 */
  1526. SWITCHTEC_PCI_DEVICE(0x5568, SWITCHTEC_GEN5), /* PAXA 68XG5 */
  1527. SWITCHTEC_PCI_DEVICE(0x5552, SWITCHTEC_GEN5), /* PAXA 52XG5 */
  1528. SWITCHTEC_PCI_DEVICE(0x5536, SWITCHTEC_GEN5), /* PAXA 36XG5 */
  1529. SWITCHTEC_PCI_DEVICE(0x5528, SWITCHTEC_GEN5), /* PAXA 28XG5 */
  1530. SWITCHTEC_PCI100X_DEVICE(0x1001, SWITCHTEC_GEN4), /* PCI1001 16XG4 */
  1531. SWITCHTEC_PCI100X_DEVICE(0x1002, SWITCHTEC_GEN4), /* PCI1002 12XG4 */
  1532. SWITCHTEC_PCI100X_DEVICE(0x1003, SWITCHTEC_GEN4), /* PCI1003 16XG4 */
  1533. SWITCHTEC_PCI100X_DEVICE(0x1004, SWITCHTEC_GEN4), /* PCI1004 16XG4 */
  1534. SWITCHTEC_PCI100X_DEVICE(0x1005, SWITCHTEC_GEN4), /* PCI1005 16XG4 */
  1535. SWITCHTEC_PCI100X_DEVICE(0x1006, SWITCHTEC_GEN4), /* PCI1006 16XG4 */
  1536. {0}
  1537. };
  1538. MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
  1539. static struct pci_driver switchtec_pci_driver = {
  1540. .name = KBUILD_MODNAME,
  1541. .id_table = switchtec_pci_tbl,
  1542. .probe = switchtec_pci_probe,
  1543. .remove = switchtec_pci_remove,
  1544. };
  1545. static int __init switchtec_init(void)
  1546. {
  1547. int rc;
  1548. rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
  1549. "switchtec");
  1550. if (rc)
  1551. return rc;
  1552. rc = class_register(&switchtec_class);
  1553. if (rc)
  1554. goto err_create_class;
  1555. rc = pci_register_driver(&switchtec_pci_driver);
  1556. if (rc)
  1557. goto err_pci_register;
  1558. pr_info(KBUILD_MODNAME ": loaded.\n");
  1559. return 0;
  1560. err_pci_register:
  1561. class_unregister(&switchtec_class);
  1562. err_create_class:
  1563. unregister_chrdev_region(switchtec_devt, max_devices);
  1564. return rc;
  1565. }
  1566. module_init(switchtec_init);
  1567. static void __exit switchtec_exit(void)
  1568. {
  1569. pci_unregister_driver(&switchtec_pci_driver);
  1570. class_unregister(&switchtec_class);
  1571. unregister_chrdev_region(switchtec_devt, max_devices);
  1572. ida_destroy(&switchtec_minor_ida);
  1573. pr_info(KBUILD_MODNAME ": unloaded.\n");
  1574. }
  1575. module_exit(switchtec_exit);