switchtec.c 52 KB

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