fjes_main.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FUJITSU Extended Socket Network Device driver
  4. * Copyright (c) 2015 FUJITSU LIMITED
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/nls.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/interrupt.h>
  12. #include "fjes.h"
  13. #include "fjes_trace.h"
  14. #define MAJ 1
  15. #define MIN 2
  16. #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
  17. #define DRV_NAME "fjes"
  18. char fjes_driver_name[] = DRV_NAME;
  19. char fjes_driver_version[] = DRV_VERSION;
  20. static const char fjes_driver_string[] =
  21. "FUJITSU Extended Socket Network Device Driver";
  22. static const char fjes_copyright[] =
  23. "Copyright (c) 2015 FUJITSU LIMITED";
  24. MODULE_AUTHOR("Taku Izumi <izumi.taku@jp.fujitsu.com>");
  25. MODULE_DESCRIPTION("FUJITSU Extended Socket Network Device Driver");
  26. MODULE_LICENSE("GPL");
  27. MODULE_VERSION(DRV_VERSION);
  28. #define ACPI_MOTHERBOARD_RESOURCE_HID "PNP0C02"
  29. static int fjes_request_irq(struct fjes_adapter *);
  30. static void fjes_free_irq(struct fjes_adapter *);
  31. static int fjes_open(struct net_device *);
  32. static int fjes_close(struct net_device *);
  33. static int fjes_setup_resources(struct fjes_adapter *);
  34. static void fjes_free_resources(struct fjes_adapter *);
  35. static netdev_tx_t fjes_xmit_frame(struct sk_buff *, struct net_device *);
  36. static void fjes_raise_intr_rxdata_task(struct work_struct *);
  37. static void fjes_tx_stall_task(struct work_struct *);
  38. static void fjes_force_close_task(struct work_struct *);
  39. static irqreturn_t fjes_intr(int, void*);
  40. static void fjes_get_stats64(struct net_device *, struct rtnl_link_stats64 *);
  41. static int fjes_change_mtu(struct net_device *, int);
  42. static int fjes_vlan_rx_add_vid(struct net_device *, __be16 proto, u16);
  43. static int fjes_vlan_rx_kill_vid(struct net_device *, __be16 proto, u16);
  44. static void fjes_tx_retry(struct net_device *);
  45. static int fjes_acpi_add(struct acpi_device *);
  46. static int fjes_acpi_remove(struct acpi_device *);
  47. static acpi_status fjes_get_acpi_resource(struct acpi_resource *, void*);
  48. static int fjes_probe(struct platform_device *);
  49. static int fjes_remove(struct platform_device *);
  50. static int fjes_sw_init(struct fjes_adapter *);
  51. static void fjes_netdev_setup(struct net_device *);
  52. static void fjes_irq_watch_task(struct work_struct *);
  53. static void fjes_watch_unshare_task(struct work_struct *);
  54. static void fjes_rx_irq(struct fjes_adapter *, int);
  55. static int fjes_poll(struct napi_struct *, int);
  56. static const struct acpi_device_id fjes_acpi_ids[] = {
  57. {ACPI_MOTHERBOARD_RESOURCE_HID, 0},
  58. {"", 0},
  59. };
  60. MODULE_DEVICE_TABLE(acpi, fjes_acpi_ids);
  61. static struct acpi_driver fjes_acpi_driver = {
  62. .name = DRV_NAME,
  63. .class = DRV_NAME,
  64. .owner = THIS_MODULE,
  65. .ids = fjes_acpi_ids,
  66. .ops = {
  67. .add = fjes_acpi_add,
  68. .remove = fjes_acpi_remove,
  69. },
  70. };
  71. static struct platform_driver fjes_driver = {
  72. .driver = {
  73. .name = DRV_NAME,
  74. },
  75. .probe = fjes_probe,
  76. .remove = fjes_remove,
  77. };
  78. static struct resource fjes_resource[] = {
  79. {
  80. .flags = IORESOURCE_MEM,
  81. .start = 0,
  82. .end = 0,
  83. },
  84. {
  85. .flags = IORESOURCE_IRQ,
  86. .start = 0,
  87. .end = 0,
  88. },
  89. };
  90. static bool is_extended_socket_device(struct acpi_device *device)
  91. {
  92. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  93. char str_buf[sizeof(FJES_ACPI_SYMBOL) + 1];
  94. union acpi_object *str;
  95. acpi_status status;
  96. int result;
  97. status = acpi_evaluate_object(device->handle, "_STR", NULL, &buffer);
  98. if (ACPI_FAILURE(status))
  99. return false;
  100. str = buffer.pointer;
  101. result = utf16s_to_utf8s((wchar_t *)str->string.pointer,
  102. str->string.length, UTF16_LITTLE_ENDIAN,
  103. str_buf, sizeof(str_buf) - 1);
  104. str_buf[result] = 0;
  105. if (strncmp(FJES_ACPI_SYMBOL, str_buf, strlen(FJES_ACPI_SYMBOL)) != 0) {
  106. kfree(buffer.pointer);
  107. return false;
  108. }
  109. kfree(buffer.pointer);
  110. return true;
  111. }
  112. static int acpi_check_extended_socket_status(struct acpi_device *device)
  113. {
  114. unsigned long long sta;
  115. acpi_status status;
  116. status = acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
  117. if (ACPI_FAILURE(status))
  118. return -ENODEV;
  119. if (!((sta & ACPI_STA_DEVICE_PRESENT) &&
  120. (sta & ACPI_STA_DEVICE_ENABLED) &&
  121. (sta & ACPI_STA_DEVICE_UI) &&
  122. (sta & ACPI_STA_DEVICE_FUNCTIONING)))
  123. return -ENODEV;
  124. return 0;
  125. }
  126. static int fjes_acpi_add(struct acpi_device *device)
  127. {
  128. struct platform_device *plat_dev;
  129. acpi_status status;
  130. if (!is_extended_socket_device(device))
  131. return -ENODEV;
  132. if (acpi_check_extended_socket_status(device))
  133. return -ENODEV;
  134. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  135. fjes_get_acpi_resource, fjes_resource);
  136. if (ACPI_FAILURE(status))
  137. return -ENODEV;
  138. /* create platform_device */
  139. plat_dev = platform_device_register_simple(DRV_NAME, 0, fjes_resource,
  140. ARRAY_SIZE(fjes_resource));
  141. if (IS_ERR(plat_dev))
  142. return PTR_ERR(plat_dev);
  143. device->driver_data = plat_dev;
  144. return 0;
  145. }
  146. static int fjes_acpi_remove(struct acpi_device *device)
  147. {
  148. struct platform_device *plat_dev;
  149. plat_dev = (struct platform_device *)acpi_driver_data(device);
  150. platform_device_unregister(plat_dev);
  151. return 0;
  152. }
  153. static acpi_status
  154. fjes_get_acpi_resource(struct acpi_resource *acpi_res, void *data)
  155. {
  156. struct acpi_resource_address32 *addr;
  157. struct acpi_resource_irq *irq;
  158. struct resource *res = data;
  159. switch (acpi_res->type) {
  160. case ACPI_RESOURCE_TYPE_ADDRESS32:
  161. addr = &acpi_res->data.address32;
  162. res[0].start = addr->address.minimum;
  163. res[0].end = addr->address.minimum +
  164. addr->address.address_length - 1;
  165. break;
  166. case ACPI_RESOURCE_TYPE_IRQ:
  167. irq = &acpi_res->data.irq;
  168. if (irq->interrupt_count != 1)
  169. return AE_ERROR;
  170. res[1].start = irq->interrupts[0];
  171. res[1].end = irq->interrupts[0];
  172. break;
  173. default:
  174. break;
  175. }
  176. return AE_OK;
  177. }
  178. static int fjes_request_irq(struct fjes_adapter *adapter)
  179. {
  180. struct net_device *netdev = adapter->netdev;
  181. int result = -1;
  182. adapter->interrupt_watch_enable = true;
  183. if (!delayed_work_pending(&adapter->interrupt_watch_task)) {
  184. queue_delayed_work(adapter->control_wq,
  185. &adapter->interrupt_watch_task,
  186. FJES_IRQ_WATCH_DELAY);
  187. }
  188. if (!adapter->irq_registered) {
  189. result = request_irq(adapter->hw.hw_res.irq, fjes_intr,
  190. IRQF_SHARED, netdev->name, adapter);
  191. if (result)
  192. adapter->irq_registered = false;
  193. else
  194. adapter->irq_registered = true;
  195. }
  196. return result;
  197. }
  198. static void fjes_free_irq(struct fjes_adapter *adapter)
  199. {
  200. struct fjes_hw *hw = &adapter->hw;
  201. adapter->interrupt_watch_enable = false;
  202. cancel_delayed_work_sync(&adapter->interrupt_watch_task);
  203. fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
  204. if (adapter->irq_registered) {
  205. free_irq(adapter->hw.hw_res.irq, adapter);
  206. adapter->irq_registered = false;
  207. }
  208. }
  209. static const struct net_device_ops fjes_netdev_ops = {
  210. .ndo_open = fjes_open,
  211. .ndo_stop = fjes_close,
  212. .ndo_start_xmit = fjes_xmit_frame,
  213. .ndo_get_stats64 = fjes_get_stats64,
  214. .ndo_change_mtu = fjes_change_mtu,
  215. .ndo_tx_timeout = fjes_tx_retry,
  216. .ndo_vlan_rx_add_vid = fjes_vlan_rx_add_vid,
  217. .ndo_vlan_rx_kill_vid = fjes_vlan_rx_kill_vid,
  218. };
  219. /* fjes_open - Called when a network interface is made active */
  220. static int fjes_open(struct net_device *netdev)
  221. {
  222. struct fjes_adapter *adapter = netdev_priv(netdev);
  223. struct fjes_hw *hw = &adapter->hw;
  224. int result;
  225. if (adapter->open_guard)
  226. return -ENXIO;
  227. result = fjes_setup_resources(adapter);
  228. if (result)
  229. goto err_setup_res;
  230. hw->txrx_stop_req_bit = 0;
  231. hw->epstop_req_bit = 0;
  232. napi_enable(&adapter->napi);
  233. fjes_hw_capture_interrupt_status(hw);
  234. result = fjes_request_irq(adapter);
  235. if (result)
  236. goto err_req_irq;
  237. fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, false);
  238. netif_tx_start_all_queues(netdev);
  239. netif_carrier_on(netdev);
  240. return 0;
  241. err_req_irq:
  242. fjes_free_irq(adapter);
  243. napi_disable(&adapter->napi);
  244. err_setup_res:
  245. fjes_free_resources(adapter);
  246. return result;
  247. }
  248. /* fjes_close - Disables a network interface */
  249. static int fjes_close(struct net_device *netdev)
  250. {
  251. struct fjes_adapter *adapter = netdev_priv(netdev);
  252. struct fjes_hw *hw = &adapter->hw;
  253. unsigned long flags;
  254. int epidx;
  255. netif_tx_stop_all_queues(netdev);
  256. netif_carrier_off(netdev);
  257. fjes_hw_raise_epstop(hw);
  258. napi_disable(&adapter->napi);
  259. spin_lock_irqsave(&hw->rx_status_lock, flags);
  260. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  261. if (epidx == hw->my_epid)
  262. continue;
  263. if (fjes_hw_get_partner_ep_status(hw, epidx) ==
  264. EP_PARTNER_SHARED)
  265. adapter->hw.ep_shm_info[epidx]
  266. .tx.info->v1i.rx_status &=
  267. ~FJES_RX_POLL_WORK;
  268. }
  269. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  270. fjes_free_irq(adapter);
  271. cancel_delayed_work_sync(&adapter->interrupt_watch_task);
  272. cancel_work_sync(&adapter->unshare_watch_task);
  273. adapter->unshare_watch_bitmask = 0;
  274. cancel_work_sync(&adapter->raise_intr_rxdata_task);
  275. cancel_work_sync(&adapter->tx_stall_task);
  276. cancel_work_sync(&hw->update_zone_task);
  277. cancel_work_sync(&hw->epstop_task);
  278. fjes_hw_wait_epstop(hw);
  279. fjes_free_resources(adapter);
  280. return 0;
  281. }
  282. static int fjes_setup_resources(struct fjes_adapter *adapter)
  283. {
  284. struct net_device *netdev = adapter->netdev;
  285. struct ep_share_mem_info *buf_pair;
  286. struct fjes_hw *hw = &adapter->hw;
  287. unsigned long flags;
  288. int result;
  289. int epidx;
  290. mutex_lock(&hw->hw_info.lock);
  291. result = fjes_hw_request_info(hw);
  292. switch (result) {
  293. case 0:
  294. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  295. hw->ep_shm_info[epidx].es_status =
  296. hw->hw_info.res_buf->info.info[epidx].es_status;
  297. hw->ep_shm_info[epidx].zone =
  298. hw->hw_info.res_buf->info.info[epidx].zone;
  299. }
  300. break;
  301. default:
  302. case -ENOMSG:
  303. case -EBUSY:
  304. adapter->force_reset = true;
  305. mutex_unlock(&hw->hw_info.lock);
  306. return result;
  307. }
  308. mutex_unlock(&hw->hw_info.lock);
  309. for (epidx = 0; epidx < (hw->max_epid); epidx++) {
  310. if ((epidx != hw->my_epid) &&
  311. (hw->ep_shm_info[epidx].es_status ==
  312. FJES_ZONING_STATUS_ENABLE)) {
  313. fjes_hw_raise_interrupt(hw, epidx,
  314. REG_ICTL_MASK_INFO_UPDATE);
  315. hw->ep_shm_info[epidx].ep_stats
  316. .send_intr_zoneupdate += 1;
  317. }
  318. }
  319. msleep(FJES_OPEN_ZONE_UPDATE_WAIT * hw->max_epid);
  320. for (epidx = 0; epidx < (hw->max_epid); epidx++) {
  321. if (epidx == hw->my_epid)
  322. continue;
  323. buf_pair = &hw->ep_shm_info[epidx];
  324. spin_lock_irqsave(&hw->rx_status_lock, flags);
  325. fjes_hw_setup_epbuf(&buf_pair->tx, netdev->dev_addr,
  326. netdev->mtu);
  327. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  328. if (fjes_hw_epid_is_same_zone(hw, epidx)) {
  329. mutex_lock(&hw->hw_info.lock);
  330. result =
  331. fjes_hw_register_buff_addr(hw, epidx, buf_pair);
  332. mutex_unlock(&hw->hw_info.lock);
  333. switch (result) {
  334. case 0:
  335. break;
  336. case -ENOMSG:
  337. case -EBUSY:
  338. default:
  339. adapter->force_reset = true;
  340. return result;
  341. }
  342. hw->ep_shm_info[epidx].ep_stats
  343. .com_regist_buf_exec += 1;
  344. }
  345. }
  346. return 0;
  347. }
  348. static void fjes_free_resources(struct fjes_adapter *adapter)
  349. {
  350. struct net_device *netdev = adapter->netdev;
  351. struct fjes_device_command_param param;
  352. struct ep_share_mem_info *buf_pair;
  353. struct fjes_hw *hw = &adapter->hw;
  354. bool reset_flag = false;
  355. unsigned long flags;
  356. int result;
  357. int epidx;
  358. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  359. if (epidx == hw->my_epid)
  360. continue;
  361. mutex_lock(&hw->hw_info.lock);
  362. result = fjes_hw_unregister_buff_addr(hw, epidx);
  363. mutex_unlock(&hw->hw_info.lock);
  364. hw->ep_shm_info[epidx].ep_stats.com_unregist_buf_exec += 1;
  365. if (result)
  366. reset_flag = true;
  367. buf_pair = &hw->ep_shm_info[epidx];
  368. spin_lock_irqsave(&hw->rx_status_lock, flags);
  369. fjes_hw_setup_epbuf(&buf_pair->tx,
  370. netdev->dev_addr, netdev->mtu);
  371. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  372. clear_bit(epidx, &hw->txrx_stop_req_bit);
  373. }
  374. if (reset_flag || adapter->force_reset) {
  375. result = fjes_hw_reset(hw);
  376. adapter->force_reset = false;
  377. if (result)
  378. adapter->open_guard = true;
  379. hw->hw_info.buffer_share_bit = 0;
  380. memset((void *)&param, 0, sizeof(param));
  381. param.req_len = hw->hw_info.req_buf_size;
  382. param.req_start = __pa(hw->hw_info.req_buf);
  383. param.res_len = hw->hw_info.res_buf_size;
  384. param.res_start = __pa(hw->hw_info.res_buf);
  385. param.share_start = __pa(hw->hw_info.share->ep_status);
  386. fjes_hw_init_command_registers(hw, &param);
  387. }
  388. }
  389. static void fjes_tx_stall_task(struct work_struct *work)
  390. {
  391. struct fjes_adapter *adapter = container_of(work,
  392. struct fjes_adapter, tx_stall_task);
  393. struct net_device *netdev = adapter->netdev;
  394. struct fjes_hw *hw = &adapter->hw;
  395. int all_queue_available, sendable;
  396. enum ep_partner_status pstatus;
  397. int max_epid, my_epid, epid;
  398. union ep_buffer_info *info;
  399. int i;
  400. if (((long)jiffies -
  401. dev_trans_start(netdev)) > FJES_TX_TX_STALL_TIMEOUT) {
  402. netif_wake_queue(netdev);
  403. return;
  404. }
  405. my_epid = hw->my_epid;
  406. max_epid = hw->max_epid;
  407. for (i = 0; i < 5; i++) {
  408. all_queue_available = 1;
  409. for (epid = 0; epid < max_epid; epid++) {
  410. if (my_epid == epid)
  411. continue;
  412. pstatus = fjes_hw_get_partner_ep_status(hw, epid);
  413. sendable = (pstatus == EP_PARTNER_SHARED);
  414. if (!sendable)
  415. continue;
  416. info = adapter->hw.ep_shm_info[epid].tx.info;
  417. if (!(info->v1i.rx_status & FJES_RX_MTU_CHANGING_DONE))
  418. return;
  419. if (EP_RING_FULL(info->v1i.head, info->v1i.tail,
  420. info->v1i.count_max)) {
  421. all_queue_available = 0;
  422. break;
  423. }
  424. }
  425. if (all_queue_available) {
  426. netif_wake_queue(netdev);
  427. return;
  428. }
  429. }
  430. usleep_range(50, 100);
  431. queue_work(adapter->txrx_wq, &adapter->tx_stall_task);
  432. }
  433. static void fjes_force_close_task(struct work_struct *work)
  434. {
  435. struct fjes_adapter *adapter = container_of(work,
  436. struct fjes_adapter, force_close_task);
  437. struct net_device *netdev = adapter->netdev;
  438. rtnl_lock();
  439. dev_close(netdev);
  440. rtnl_unlock();
  441. }
  442. static void fjes_raise_intr_rxdata_task(struct work_struct *work)
  443. {
  444. struct fjes_adapter *adapter = container_of(work,
  445. struct fjes_adapter, raise_intr_rxdata_task);
  446. struct fjes_hw *hw = &adapter->hw;
  447. enum ep_partner_status pstatus;
  448. int max_epid, my_epid, epid;
  449. my_epid = hw->my_epid;
  450. max_epid = hw->max_epid;
  451. for (epid = 0; epid < max_epid; epid++)
  452. hw->ep_shm_info[epid].tx_status_work = 0;
  453. for (epid = 0; epid < max_epid; epid++) {
  454. if (epid == my_epid)
  455. continue;
  456. pstatus = fjes_hw_get_partner_ep_status(hw, epid);
  457. if (pstatus == EP_PARTNER_SHARED) {
  458. hw->ep_shm_info[epid].tx_status_work =
  459. hw->ep_shm_info[epid].tx.info->v1i.tx_status;
  460. if (hw->ep_shm_info[epid].tx_status_work ==
  461. FJES_TX_DELAY_SEND_PENDING) {
  462. hw->ep_shm_info[epid].tx.info->v1i.tx_status =
  463. FJES_TX_DELAY_SEND_NONE;
  464. }
  465. }
  466. }
  467. for (epid = 0; epid < max_epid; epid++) {
  468. if (epid == my_epid)
  469. continue;
  470. pstatus = fjes_hw_get_partner_ep_status(hw, epid);
  471. if ((hw->ep_shm_info[epid].tx_status_work ==
  472. FJES_TX_DELAY_SEND_PENDING) &&
  473. (pstatus == EP_PARTNER_SHARED) &&
  474. !(hw->ep_shm_info[epid].rx.info->v1i.rx_status &
  475. FJES_RX_POLL_WORK)) {
  476. fjes_hw_raise_interrupt(hw, epid,
  477. REG_ICTL_MASK_RX_DATA);
  478. hw->ep_shm_info[epid].ep_stats.send_intr_rx += 1;
  479. }
  480. }
  481. usleep_range(500, 1000);
  482. }
  483. static int fjes_tx_send(struct fjes_adapter *adapter, int dest,
  484. void *data, size_t len)
  485. {
  486. int retval;
  487. retval = fjes_hw_epbuf_tx_pkt_send(&adapter->hw.ep_shm_info[dest].tx,
  488. data, len);
  489. if (retval)
  490. return retval;
  491. adapter->hw.ep_shm_info[dest].tx.info->v1i.tx_status =
  492. FJES_TX_DELAY_SEND_PENDING;
  493. if (!work_pending(&adapter->raise_intr_rxdata_task))
  494. queue_work(adapter->txrx_wq,
  495. &adapter->raise_intr_rxdata_task);
  496. retval = 0;
  497. return retval;
  498. }
  499. static netdev_tx_t
  500. fjes_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  501. {
  502. struct fjes_adapter *adapter = netdev_priv(netdev);
  503. struct fjes_hw *hw = &adapter->hw;
  504. int max_epid, my_epid, dest_epid;
  505. enum ep_partner_status pstatus;
  506. struct netdev_queue *cur_queue;
  507. char shortpkt[VLAN_ETH_HLEN];
  508. bool is_multi, vlan;
  509. struct ethhdr *eth;
  510. u16 queue_no = 0;
  511. u16 vlan_id = 0;
  512. netdev_tx_t ret;
  513. char *data;
  514. int len;
  515. ret = NETDEV_TX_OK;
  516. is_multi = false;
  517. cur_queue = netdev_get_tx_queue(netdev, queue_no);
  518. eth = (struct ethhdr *)skb->data;
  519. my_epid = hw->my_epid;
  520. vlan = (vlan_get_tag(skb, &vlan_id) == 0) ? true : false;
  521. data = skb->data;
  522. len = skb->len;
  523. if (is_multicast_ether_addr(eth->h_dest)) {
  524. dest_epid = 0;
  525. max_epid = hw->max_epid;
  526. is_multi = true;
  527. } else if (is_local_ether_addr(eth->h_dest)) {
  528. dest_epid = eth->h_dest[ETH_ALEN - 1];
  529. max_epid = dest_epid + 1;
  530. if ((eth->h_dest[0] == 0x02) &&
  531. (0x00 == (eth->h_dest[1] | eth->h_dest[2] |
  532. eth->h_dest[3] | eth->h_dest[4])) &&
  533. (dest_epid < hw->max_epid)) {
  534. ;
  535. } else {
  536. dest_epid = 0;
  537. max_epid = 0;
  538. ret = NETDEV_TX_OK;
  539. adapter->stats64.tx_packets += 1;
  540. hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
  541. adapter->stats64.tx_bytes += len;
  542. hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
  543. }
  544. } else {
  545. dest_epid = 0;
  546. max_epid = 0;
  547. ret = NETDEV_TX_OK;
  548. adapter->stats64.tx_packets += 1;
  549. hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
  550. adapter->stats64.tx_bytes += len;
  551. hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
  552. }
  553. for (; dest_epid < max_epid; dest_epid++) {
  554. if (my_epid == dest_epid)
  555. continue;
  556. pstatus = fjes_hw_get_partner_ep_status(hw, dest_epid);
  557. if (pstatus != EP_PARTNER_SHARED) {
  558. if (!is_multi)
  559. hw->ep_shm_info[dest_epid].ep_stats
  560. .tx_dropped_not_shared += 1;
  561. ret = NETDEV_TX_OK;
  562. } else if (!fjes_hw_check_epbuf_version(
  563. &adapter->hw.ep_shm_info[dest_epid].rx, 0)) {
  564. /* version is NOT 0 */
  565. adapter->stats64.tx_carrier_errors += 1;
  566. hw->ep_shm_info[dest_epid].net_stats
  567. .tx_carrier_errors += 1;
  568. hw->ep_shm_info[dest_epid].ep_stats
  569. .tx_dropped_ver_mismatch += 1;
  570. ret = NETDEV_TX_OK;
  571. } else if (!fjes_hw_check_mtu(
  572. &adapter->hw.ep_shm_info[dest_epid].rx,
  573. netdev->mtu)) {
  574. adapter->stats64.tx_dropped += 1;
  575. hw->ep_shm_info[dest_epid].net_stats.tx_dropped += 1;
  576. adapter->stats64.tx_errors += 1;
  577. hw->ep_shm_info[dest_epid].net_stats.tx_errors += 1;
  578. hw->ep_shm_info[dest_epid].ep_stats
  579. .tx_dropped_buf_size_mismatch += 1;
  580. ret = NETDEV_TX_OK;
  581. } else if (vlan &&
  582. !fjes_hw_check_vlan_id(
  583. &adapter->hw.ep_shm_info[dest_epid].rx,
  584. vlan_id)) {
  585. hw->ep_shm_info[dest_epid].ep_stats
  586. .tx_dropped_vlanid_mismatch += 1;
  587. ret = NETDEV_TX_OK;
  588. } else {
  589. if (len < VLAN_ETH_HLEN) {
  590. memset(shortpkt, 0, VLAN_ETH_HLEN);
  591. memcpy(shortpkt, skb->data, skb->len);
  592. len = VLAN_ETH_HLEN;
  593. data = shortpkt;
  594. }
  595. if (adapter->tx_retry_count == 0) {
  596. adapter->tx_start_jiffies = jiffies;
  597. adapter->tx_retry_count = 1;
  598. } else {
  599. adapter->tx_retry_count++;
  600. }
  601. if (fjes_tx_send(adapter, dest_epid, data, len)) {
  602. if (is_multi) {
  603. ret = NETDEV_TX_OK;
  604. } else if (
  605. ((long)jiffies -
  606. (long)adapter->tx_start_jiffies) >=
  607. FJES_TX_RETRY_TIMEOUT) {
  608. adapter->stats64.tx_fifo_errors += 1;
  609. hw->ep_shm_info[dest_epid].net_stats
  610. .tx_fifo_errors += 1;
  611. adapter->stats64.tx_errors += 1;
  612. hw->ep_shm_info[dest_epid].net_stats
  613. .tx_errors += 1;
  614. ret = NETDEV_TX_OK;
  615. } else {
  616. netif_trans_update(netdev);
  617. hw->ep_shm_info[dest_epid].ep_stats
  618. .tx_buffer_full += 1;
  619. netif_tx_stop_queue(cur_queue);
  620. if (!work_pending(&adapter->tx_stall_task))
  621. queue_work(adapter->txrx_wq,
  622. &adapter->tx_stall_task);
  623. ret = NETDEV_TX_BUSY;
  624. }
  625. } else {
  626. if (!is_multi) {
  627. adapter->stats64.tx_packets += 1;
  628. hw->ep_shm_info[dest_epid].net_stats
  629. .tx_packets += 1;
  630. adapter->stats64.tx_bytes += len;
  631. hw->ep_shm_info[dest_epid].net_stats
  632. .tx_bytes += len;
  633. }
  634. adapter->tx_retry_count = 0;
  635. ret = NETDEV_TX_OK;
  636. }
  637. }
  638. }
  639. if (ret == NETDEV_TX_OK) {
  640. dev_kfree_skb(skb);
  641. if (is_multi) {
  642. adapter->stats64.tx_packets += 1;
  643. hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
  644. adapter->stats64.tx_bytes += 1;
  645. hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
  646. }
  647. }
  648. return ret;
  649. }
  650. static void fjes_tx_retry(struct net_device *netdev)
  651. {
  652. struct netdev_queue *queue = netdev_get_tx_queue(netdev, 0);
  653. netif_tx_wake_queue(queue);
  654. }
  655. static void
  656. fjes_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  657. {
  658. struct fjes_adapter *adapter = netdev_priv(netdev);
  659. memcpy(stats, &adapter->stats64, sizeof(struct rtnl_link_stats64));
  660. }
  661. static int fjes_change_mtu(struct net_device *netdev, int new_mtu)
  662. {
  663. struct fjes_adapter *adapter = netdev_priv(netdev);
  664. bool running = netif_running(netdev);
  665. struct fjes_hw *hw = &adapter->hw;
  666. unsigned long flags;
  667. int ret = -EINVAL;
  668. int idx, epidx;
  669. for (idx = 0; fjes_support_mtu[idx] != 0; idx++) {
  670. if (new_mtu <= fjes_support_mtu[idx]) {
  671. new_mtu = fjes_support_mtu[idx];
  672. if (new_mtu == netdev->mtu)
  673. return 0;
  674. ret = 0;
  675. break;
  676. }
  677. }
  678. if (ret)
  679. return ret;
  680. if (running) {
  681. spin_lock_irqsave(&hw->rx_status_lock, flags);
  682. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  683. if (epidx == hw->my_epid)
  684. continue;
  685. hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
  686. ~FJES_RX_MTU_CHANGING_DONE;
  687. }
  688. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  689. netif_tx_stop_all_queues(netdev);
  690. netif_carrier_off(netdev);
  691. cancel_work_sync(&adapter->tx_stall_task);
  692. napi_disable(&adapter->napi);
  693. msleep(1000);
  694. netif_tx_stop_all_queues(netdev);
  695. }
  696. netdev->mtu = new_mtu;
  697. if (running) {
  698. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  699. if (epidx == hw->my_epid)
  700. continue;
  701. spin_lock_irqsave(&hw->rx_status_lock, flags);
  702. fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
  703. netdev->dev_addr,
  704. netdev->mtu);
  705. hw->ep_shm_info[epidx].tx.info->v1i.rx_status |=
  706. FJES_RX_MTU_CHANGING_DONE;
  707. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  708. }
  709. netif_tx_wake_all_queues(netdev);
  710. netif_carrier_on(netdev);
  711. napi_enable(&adapter->napi);
  712. napi_schedule(&adapter->napi);
  713. }
  714. return ret;
  715. }
  716. static int fjes_vlan_rx_add_vid(struct net_device *netdev,
  717. __be16 proto, u16 vid)
  718. {
  719. struct fjes_adapter *adapter = netdev_priv(netdev);
  720. bool ret = true;
  721. int epid;
  722. for (epid = 0; epid < adapter->hw.max_epid; epid++) {
  723. if (epid == adapter->hw.my_epid)
  724. continue;
  725. if (!fjes_hw_check_vlan_id(
  726. &adapter->hw.ep_shm_info[epid].tx, vid))
  727. ret = fjes_hw_set_vlan_id(
  728. &adapter->hw.ep_shm_info[epid].tx, vid);
  729. }
  730. return ret ? 0 : -ENOSPC;
  731. }
  732. static int fjes_vlan_rx_kill_vid(struct net_device *netdev,
  733. __be16 proto, u16 vid)
  734. {
  735. struct fjes_adapter *adapter = netdev_priv(netdev);
  736. int epid;
  737. for (epid = 0; epid < adapter->hw.max_epid; epid++) {
  738. if (epid == adapter->hw.my_epid)
  739. continue;
  740. fjes_hw_del_vlan_id(&adapter->hw.ep_shm_info[epid].tx, vid);
  741. }
  742. return 0;
  743. }
  744. static void fjes_txrx_stop_req_irq(struct fjes_adapter *adapter,
  745. int src_epid)
  746. {
  747. struct fjes_hw *hw = &adapter->hw;
  748. enum ep_partner_status status;
  749. unsigned long flags;
  750. status = fjes_hw_get_partner_ep_status(hw, src_epid);
  751. trace_fjes_txrx_stop_req_irq_pre(hw, src_epid, status);
  752. switch (status) {
  753. case EP_PARTNER_UNSHARE:
  754. case EP_PARTNER_COMPLETE:
  755. default:
  756. break;
  757. case EP_PARTNER_WAITING:
  758. if (src_epid < hw->my_epid) {
  759. spin_lock_irqsave(&hw->rx_status_lock, flags);
  760. hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
  761. FJES_RX_STOP_REQ_DONE;
  762. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  763. clear_bit(src_epid, &hw->txrx_stop_req_bit);
  764. set_bit(src_epid, &adapter->unshare_watch_bitmask);
  765. if (!work_pending(&adapter->unshare_watch_task))
  766. queue_work(adapter->control_wq,
  767. &adapter->unshare_watch_task);
  768. }
  769. break;
  770. case EP_PARTNER_SHARED:
  771. if (hw->ep_shm_info[src_epid].rx.info->v1i.rx_status &
  772. FJES_RX_STOP_REQ_REQUEST) {
  773. set_bit(src_epid, &hw->epstop_req_bit);
  774. if (!work_pending(&hw->epstop_task))
  775. queue_work(adapter->control_wq,
  776. &hw->epstop_task);
  777. }
  778. break;
  779. }
  780. trace_fjes_txrx_stop_req_irq_post(hw, src_epid);
  781. }
  782. static void fjes_stop_req_irq(struct fjes_adapter *adapter, int src_epid)
  783. {
  784. struct fjes_hw *hw = &adapter->hw;
  785. enum ep_partner_status status;
  786. unsigned long flags;
  787. set_bit(src_epid, &hw->hw_info.buffer_unshare_reserve_bit);
  788. status = fjes_hw_get_partner_ep_status(hw, src_epid);
  789. trace_fjes_stop_req_irq_pre(hw, src_epid, status);
  790. switch (status) {
  791. case EP_PARTNER_WAITING:
  792. spin_lock_irqsave(&hw->rx_status_lock, flags);
  793. hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
  794. FJES_RX_STOP_REQ_DONE;
  795. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  796. clear_bit(src_epid, &hw->txrx_stop_req_bit);
  797. /* fall through */
  798. case EP_PARTNER_UNSHARE:
  799. case EP_PARTNER_COMPLETE:
  800. default:
  801. set_bit(src_epid, &adapter->unshare_watch_bitmask);
  802. if (!work_pending(&adapter->unshare_watch_task))
  803. queue_work(adapter->control_wq,
  804. &adapter->unshare_watch_task);
  805. break;
  806. case EP_PARTNER_SHARED:
  807. set_bit(src_epid, &hw->epstop_req_bit);
  808. if (!work_pending(&hw->epstop_task))
  809. queue_work(adapter->control_wq, &hw->epstop_task);
  810. break;
  811. }
  812. trace_fjes_stop_req_irq_post(hw, src_epid);
  813. }
  814. static void fjes_update_zone_irq(struct fjes_adapter *adapter,
  815. int src_epid)
  816. {
  817. struct fjes_hw *hw = &adapter->hw;
  818. if (!work_pending(&hw->update_zone_task))
  819. queue_work(adapter->control_wq, &hw->update_zone_task);
  820. }
  821. static irqreturn_t fjes_intr(int irq, void *data)
  822. {
  823. struct fjes_adapter *adapter = data;
  824. struct fjes_hw *hw = &adapter->hw;
  825. irqreturn_t ret;
  826. u32 icr;
  827. icr = fjes_hw_capture_interrupt_status(hw);
  828. if (icr & REG_IS_MASK_IS_ASSERT) {
  829. if (icr & REG_ICTL_MASK_RX_DATA) {
  830. fjes_rx_irq(adapter, icr & REG_IS_MASK_EPID);
  831. hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
  832. .recv_intr_rx += 1;
  833. }
  834. if (icr & REG_ICTL_MASK_DEV_STOP_REQ) {
  835. fjes_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
  836. hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
  837. .recv_intr_stop += 1;
  838. }
  839. if (icr & REG_ICTL_MASK_TXRX_STOP_REQ) {
  840. fjes_txrx_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
  841. hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
  842. .recv_intr_unshare += 1;
  843. }
  844. if (icr & REG_ICTL_MASK_TXRX_STOP_DONE)
  845. fjes_hw_set_irqmask(hw,
  846. REG_ICTL_MASK_TXRX_STOP_DONE, true);
  847. if (icr & REG_ICTL_MASK_INFO_UPDATE) {
  848. fjes_update_zone_irq(adapter, icr & REG_IS_MASK_EPID);
  849. hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
  850. .recv_intr_zoneupdate += 1;
  851. }
  852. ret = IRQ_HANDLED;
  853. } else {
  854. ret = IRQ_NONE;
  855. }
  856. return ret;
  857. }
  858. static int fjes_rxframe_search_exist(struct fjes_adapter *adapter,
  859. int start_epid)
  860. {
  861. struct fjes_hw *hw = &adapter->hw;
  862. enum ep_partner_status pstatus;
  863. int max_epid, cur_epid;
  864. int i;
  865. max_epid = hw->max_epid;
  866. start_epid = (start_epid + 1 + max_epid) % max_epid;
  867. for (i = 0; i < max_epid; i++) {
  868. cur_epid = (start_epid + i) % max_epid;
  869. if (cur_epid == hw->my_epid)
  870. continue;
  871. pstatus = fjes_hw_get_partner_ep_status(hw, cur_epid);
  872. if (pstatus == EP_PARTNER_SHARED) {
  873. if (!fjes_hw_epbuf_rx_is_empty(
  874. &hw->ep_shm_info[cur_epid].rx))
  875. return cur_epid;
  876. }
  877. }
  878. return -1;
  879. }
  880. static void *fjes_rxframe_get(struct fjes_adapter *adapter, size_t *psize,
  881. int *cur_epid)
  882. {
  883. void *frame;
  884. *cur_epid = fjes_rxframe_search_exist(adapter, *cur_epid);
  885. if (*cur_epid < 0)
  886. return NULL;
  887. frame =
  888. fjes_hw_epbuf_rx_curpkt_get_addr(
  889. &adapter->hw.ep_shm_info[*cur_epid].rx, psize);
  890. return frame;
  891. }
  892. static void fjes_rxframe_release(struct fjes_adapter *adapter, int cur_epid)
  893. {
  894. fjes_hw_epbuf_rx_curpkt_drop(&adapter->hw.ep_shm_info[cur_epid].rx);
  895. }
  896. static void fjes_rx_irq(struct fjes_adapter *adapter, int src_epid)
  897. {
  898. struct fjes_hw *hw = &adapter->hw;
  899. fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, true);
  900. adapter->unset_rx_last = true;
  901. napi_schedule(&adapter->napi);
  902. }
  903. static int fjes_poll(struct napi_struct *napi, int budget)
  904. {
  905. struct fjes_adapter *adapter =
  906. container_of(napi, struct fjes_adapter, napi);
  907. struct net_device *netdev = napi->dev;
  908. struct fjes_hw *hw = &adapter->hw;
  909. struct sk_buff *skb;
  910. int work_done = 0;
  911. int cur_epid = 0;
  912. int epidx;
  913. size_t frame_len;
  914. void *frame;
  915. spin_lock(&hw->rx_status_lock);
  916. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  917. if (epidx == hw->my_epid)
  918. continue;
  919. if (fjes_hw_get_partner_ep_status(hw, epidx) ==
  920. EP_PARTNER_SHARED)
  921. adapter->hw.ep_shm_info[epidx]
  922. .tx.info->v1i.rx_status |= FJES_RX_POLL_WORK;
  923. }
  924. spin_unlock(&hw->rx_status_lock);
  925. while (work_done < budget) {
  926. prefetch(&adapter->hw);
  927. frame = fjes_rxframe_get(adapter, &frame_len, &cur_epid);
  928. if (frame) {
  929. skb = napi_alloc_skb(napi, frame_len);
  930. if (!skb) {
  931. adapter->stats64.rx_dropped += 1;
  932. hw->ep_shm_info[cur_epid].net_stats
  933. .rx_dropped += 1;
  934. adapter->stats64.rx_errors += 1;
  935. hw->ep_shm_info[cur_epid].net_stats
  936. .rx_errors += 1;
  937. } else {
  938. skb_put_data(skb, frame, frame_len);
  939. skb->protocol = eth_type_trans(skb, netdev);
  940. skb->ip_summed = CHECKSUM_UNNECESSARY;
  941. netif_receive_skb(skb);
  942. work_done++;
  943. adapter->stats64.rx_packets += 1;
  944. hw->ep_shm_info[cur_epid].net_stats
  945. .rx_packets += 1;
  946. adapter->stats64.rx_bytes += frame_len;
  947. hw->ep_shm_info[cur_epid].net_stats
  948. .rx_bytes += frame_len;
  949. if (is_multicast_ether_addr(
  950. ((struct ethhdr *)frame)->h_dest)) {
  951. adapter->stats64.multicast += 1;
  952. hw->ep_shm_info[cur_epid].net_stats
  953. .multicast += 1;
  954. }
  955. }
  956. fjes_rxframe_release(adapter, cur_epid);
  957. adapter->unset_rx_last = true;
  958. } else {
  959. break;
  960. }
  961. }
  962. if (work_done < budget) {
  963. napi_complete_done(napi, work_done);
  964. if (adapter->unset_rx_last) {
  965. adapter->rx_last_jiffies = jiffies;
  966. adapter->unset_rx_last = false;
  967. }
  968. if (((long)jiffies - (long)adapter->rx_last_jiffies) < 3) {
  969. napi_reschedule(napi);
  970. } else {
  971. spin_lock(&hw->rx_status_lock);
  972. for (epidx = 0; epidx < hw->max_epid; epidx++) {
  973. if (epidx == hw->my_epid)
  974. continue;
  975. if (fjes_hw_get_partner_ep_status(hw, epidx) ==
  976. EP_PARTNER_SHARED)
  977. adapter->hw.ep_shm_info[epidx].tx
  978. .info->v1i.rx_status &=
  979. ~FJES_RX_POLL_WORK;
  980. }
  981. spin_unlock(&hw->rx_status_lock);
  982. fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, false);
  983. }
  984. }
  985. return work_done;
  986. }
  987. /* fjes_probe - Device Initialization Routine */
  988. static int fjes_probe(struct platform_device *plat_dev)
  989. {
  990. struct fjes_adapter *adapter;
  991. struct net_device *netdev;
  992. struct resource *res;
  993. struct fjes_hw *hw;
  994. int err;
  995. err = -ENOMEM;
  996. netdev = alloc_netdev_mq(sizeof(struct fjes_adapter), "es%d",
  997. NET_NAME_UNKNOWN, fjes_netdev_setup,
  998. FJES_MAX_QUEUES);
  999. if (!netdev)
  1000. goto err_out;
  1001. SET_NETDEV_DEV(netdev, &plat_dev->dev);
  1002. dev_set_drvdata(&plat_dev->dev, netdev);
  1003. adapter = netdev_priv(netdev);
  1004. adapter->netdev = netdev;
  1005. adapter->plat_dev = plat_dev;
  1006. hw = &adapter->hw;
  1007. hw->back = adapter;
  1008. /* setup the private structure */
  1009. err = fjes_sw_init(adapter);
  1010. if (err)
  1011. goto err_free_netdev;
  1012. INIT_WORK(&adapter->force_close_task, fjes_force_close_task);
  1013. adapter->force_reset = false;
  1014. adapter->open_guard = false;
  1015. adapter->txrx_wq = alloc_workqueue(DRV_NAME "/txrx", WQ_MEM_RECLAIM, 0);
  1016. if (unlikely(!adapter->txrx_wq)) {
  1017. err = -ENOMEM;
  1018. goto err_free_netdev;
  1019. }
  1020. adapter->control_wq = alloc_workqueue(DRV_NAME "/control",
  1021. WQ_MEM_RECLAIM, 0);
  1022. if (unlikely(!adapter->control_wq)) {
  1023. err = -ENOMEM;
  1024. goto err_free_txrx_wq;
  1025. }
  1026. INIT_WORK(&adapter->tx_stall_task, fjes_tx_stall_task);
  1027. INIT_WORK(&adapter->raise_intr_rxdata_task,
  1028. fjes_raise_intr_rxdata_task);
  1029. INIT_WORK(&adapter->unshare_watch_task, fjes_watch_unshare_task);
  1030. adapter->unshare_watch_bitmask = 0;
  1031. INIT_DELAYED_WORK(&adapter->interrupt_watch_task, fjes_irq_watch_task);
  1032. adapter->interrupt_watch_enable = false;
  1033. res = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
  1034. if (!res) {
  1035. err = -EINVAL;
  1036. goto err_free_control_wq;
  1037. }
  1038. hw->hw_res.start = res->start;
  1039. hw->hw_res.size = resource_size(res);
  1040. hw->hw_res.irq = platform_get_irq(plat_dev, 0);
  1041. err = fjes_hw_init(&adapter->hw);
  1042. if (err)
  1043. goto err_free_control_wq;
  1044. /* setup MAC address (02:00:00:00:00:[epid])*/
  1045. netdev->dev_addr[0] = 2;
  1046. netdev->dev_addr[1] = 0;
  1047. netdev->dev_addr[2] = 0;
  1048. netdev->dev_addr[3] = 0;
  1049. netdev->dev_addr[4] = 0;
  1050. netdev->dev_addr[5] = hw->my_epid; /* EPID */
  1051. err = register_netdev(netdev);
  1052. if (err)
  1053. goto err_hw_exit;
  1054. netif_carrier_off(netdev);
  1055. fjes_dbg_adapter_init(adapter);
  1056. return 0;
  1057. err_hw_exit:
  1058. fjes_hw_exit(&adapter->hw);
  1059. err_free_control_wq:
  1060. destroy_workqueue(adapter->control_wq);
  1061. err_free_txrx_wq:
  1062. destroy_workqueue(adapter->txrx_wq);
  1063. err_free_netdev:
  1064. free_netdev(netdev);
  1065. err_out:
  1066. return err;
  1067. }
  1068. /* fjes_remove - Device Removal Routine */
  1069. static int fjes_remove(struct platform_device *plat_dev)
  1070. {
  1071. struct net_device *netdev = dev_get_drvdata(&plat_dev->dev);
  1072. struct fjes_adapter *adapter = netdev_priv(netdev);
  1073. struct fjes_hw *hw = &adapter->hw;
  1074. fjes_dbg_adapter_exit(adapter);
  1075. cancel_delayed_work_sync(&adapter->interrupt_watch_task);
  1076. cancel_work_sync(&adapter->unshare_watch_task);
  1077. cancel_work_sync(&adapter->raise_intr_rxdata_task);
  1078. cancel_work_sync(&adapter->tx_stall_task);
  1079. if (adapter->control_wq)
  1080. destroy_workqueue(adapter->control_wq);
  1081. if (adapter->txrx_wq)
  1082. destroy_workqueue(adapter->txrx_wq);
  1083. unregister_netdev(netdev);
  1084. fjes_hw_exit(hw);
  1085. netif_napi_del(&adapter->napi);
  1086. free_netdev(netdev);
  1087. return 0;
  1088. }
  1089. static int fjes_sw_init(struct fjes_adapter *adapter)
  1090. {
  1091. struct net_device *netdev = adapter->netdev;
  1092. netif_napi_add(netdev, &adapter->napi, fjes_poll, 64);
  1093. return 0;
  1094. }
  1095. /* fjes_netdev_setup - netdevice initialization routine */
  1096. static void fjes_netdev_setup(struct net_device *netdev)
  1097. {
  1098. ether_setup(netdev);
  1099. netdev->watchdog_timeo = FJES_TX_RETRY_INTERVAL;
  1100. netdev->netdev_ops = &fjes_netdev_ops;
  1101. fjes_set_ethtool_ops(netdev);
  1102. netdev->mtu = fjes_support_mtu[3];
  1103. netdev->min_mtu = fjes_support_mtu[0];
  1104. netdev->max_mtu = fjes_support_mtu[3];
  1105. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1106. }
  1107. static void fjes_irq_watch_task(struct work_struct *work)
  1108. {
  1109. struct fjes_adapter *adapter = container_of(to_delayed_work(work),
  1110. struct fjes_adapter, interrupt_watch_task);
  1111. local_irq_disable();
  1112. fjes_intr(adapter->hw.hw_res.irq, adapter);
  1113. local_irq_enable();
  1114. if (fjes_rxframe_search_exist(adapter, 0) >= 0)
  1115. napi_schedule(&adapter->napi);
  1116. if (adapter->interrupt_watch_enable) {
  1117. if (!delayed_work_pending(&adapter->interrupt_watch_task))
  1118. queue_delayed_work(adapter->control_wq,
  1119. &adapter->interrupt_watch_task,
  1120. FJES_IRQ_WATCH_DELAY);
  1121. }
  1122. }
  1123. static void fjes_watch_unshare_task(struct work_struct *work)
  1124. {
  1125. struct fjes_adapter *adapter =
  1126. container_of(work, struct fjes_adapter, unshare_watch_task);
  1127. struct net_device *netdev = adapter->netdev;
  1128. struct fjes_hw *hw = &adapter->hw;
  1129. int unshare_watch, unshare_reserve;
  1130. int max_epid, my_epid, epidx;
  1131. int stop_req, stop_req_done;
  1132. ulong unshare_watch_bitmask;
  1133. unsigned long flags;
  1134. int wait_time = 0;
  1135. int is_shared;
  1136. int ret;
  1137. my_epid = hw->my_epid;
  1138. max_epid = hw->max_epid;
  1139. unshare_watch_bitmask = adapter->unshare_watch_bitmask;
  1140. adapter->unshare_watch_bitmask = 0;
  1141. while ((unshare_watch_bitmask || hw->txrx_stop_req_bit) &&
  1142. (wait_time < 3000)) {
  1143. for (epidx = 0; epidx < max_epid; epidx++) {
  1144. if (epidx == my_epid)
  1145. continue;
  1146. is_shared = fjes_hw_epid_is_shared(hw->hw_info.share,
  1147. epidx);
  1148. stop_req = test_bit(epidx, &hw->txrx_stop_req_bit);
  1149. stop_req_done = hw->ep_shm_info[epidx].rx.info->v1i.rx_status &
  1150. FJES_RX_STOP_REQ_DONE;
  1151. unshare_watch = test_bit(epidx, &unshare_watch_bitmask);
  1152. unshare_reserve = test_bit(epidx,
  1153. &hw->hw_info.buffer_unshare_reserve_bit);
  1154. if ((!stop_req ||
  1155. (is_shared && (!is_shared || !stop_req_done))) &&
  1156. (is_shared || !unshare_watch || !unshare_reserve))
  1157. continue;
  1158. mutex_lock(&hw->hw_info.lock);
  1159. ret = fjes_hw_unregister_buff_addr(hw, epidx);
  1160. switch (ret) {
  1161. case 0:
  1162. break;
  1163. case -ENOMSG:
  1164. case -EBUSY:
  1165. default:
  1166. if (!work_pending(
  1167. &adapter->force_close_task)) {
  1168. adapter->force_reset = true;
  1169. schedule_work(
  1170. &adapter->force_close_task);
  1171. }
  1172. break;
  1173. }
  1174. mutex_unlock(&hw->hw_info.lock);
  1175. hw->ep_shm_info[epidx].ep_stats
  1176. .com_unregist_buf_exec += 1;
  1177. spin_lock_irqsave(&hw->rx_status_lock, flags);
  1178. fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
  1179. netdev->dev_addr, netdev->mtu);
  1180. spin_unlock_irqrestore(&hw->rx_status_lock, flags);
  1181. clear_bit(epidx, &hw->txrx_stop_req_bit);
  1182. clear_bit(epidx, &unshare_watch_bitmask);
  1183. clear_bit(epidx,
  1184. &hw->hw_info.buffer_unshare_reserve_bit);
  1185. }
  1186. msleep(100);
  1187. wait_time += 100;
  1188. }
  1189. if (hw->hw_info.buffer_unshare_reserve_bit) {
  1190. for (epidx = 0; epidx < max_epid; epidx++) {
  1191. if (epidx == my_epid)
  1192. continue;
  1193. if (test_bit(epidx,
  1194. &hw->hw_info.buffer_unshare_reserve_bit)) {
  1195. mutex_lock(&hw->hw_info.lock);
  1196. ret = fjes_hw_unregister_buff_addr(hw, epidx);
  1197. switch (ret) {
  1198. case 0:
  1199. break;
  1200. case -ENOMSG:
  1201. case -EBUSY:
  1202. default:
  1203. if (!work_pending(
  1204. &adapter->force_close_task)) {
  1205. adapter->force_reset = true;
  1206. schedule_work(
  1207. &adapter->force_close_task);
  1208. }
  1209. break;
  1210. }
  1211. mutex_unlock(&hw->hw_info.lock);
  1212. hw->ep_shm_info[epidx].ep_stats
  1213. .com_unregist_buf_exec += 1;
  1214. spin_lock_irqsave(&hw->rx_status_lock, flags);
  1215. fjes_hw_setup_epbuf(
  1216. &hw->ep_shm_info[epidx].tx,
  1217. netdev->dev_addr, netdev->mtu);
  1218. spin_unlock_irqrestore(&hw->rx_status_lock,
  1219. flags);
  1220. clear_bit(epidx, &hw->txrx_stop_req_bit);
  1221. clear_bit(epidx, &unshare_watch_bitmask);
  1222. clear_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
  1223. }
  1224. if (test_bit(epidx, &unshare_watch_bitmask)) {
  1225. spin_lock_irqsave(&hw->rx_status_lock, flags);
  1226. hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
  1227. ~FJES_RX_STOP_REQ_DONE;
  1228. spin_unlock_irqrestore(&hw->rx_status_lock,
  1229. flags);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. static acpi_status
  1235. acpi_find_extended_socket_device(acpi_handle obj_handle, u32 level,
  1236. void *context, void **return_value)
  1237. {
  1238. struct acpi_device *device;
  1239. bool *found = context;
  1240. int result;
  1241. result = acpi_bus_get_device(obj_handle, &device);
  1242. if (result)
  1243. return AE_OK;
  1244. if (strcmp(acpi_device_hid(device), ACPI_MOTHERBOARD_RESOURCE_HID))
  1245. return AE_OK;
  1246. if (!is_extended_socket_device(device))
  1247. return AE_OK;
  1248. if (acpi_check_extended_socket_status(device))
  1249. return AE_OK;
  1250. *found = true;
  1251. return AE_CTRL_TERMINATE;
  1252. }
  1253. /* fjes_init_module - Driver Registration Routine */
  1254. static int __init fjes_init_module(void)
  1255. {
  1256. bool found = false;
  1257. int result;
  1258. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
  1259. acpi_find_extended_socket_device, NULL, &found,
  1260. NULL);
  1261. if (!found)
  1262. return -ENODEV;
  1263. pr_info("%s - version %s - %s\n",
  1264. fjes_driver_string, fjes_driver_version, fjes_copyright);
  1265. fjes_dbg_init();
  1266. result = platform_driver_register(&fjes_driver);
  1267. if (result < 0) {
  1268. fjes_dbg_exit();
  1269. return result;
  1270. }
  1271. result = acpi_bus_register_driver(&fjes_acpi_driver);
  1272. if (result < 0)
  1273. goto fail_acpi_driver;
  1274. return 0;
  1275. fail_acpi_driver:
  1276. platform_driver_unregister(&fjes_driver);
  1277. fjes_dbg_exit();
  1278. return result;
  1279. }
  1280. module_init(fjes_init_module);
  1281. /* fjes_exit_module - Driver Exit Cleanup Routine */
  1282. static void __exit fjes_exit_module(void)
  1283. {
  1284. acpi_bus_unregister_driver(&fjes_acpi_driver);
  1285. platform_driver_unregister(&fjes_driver);
  1286. fjes_dbg_exit();
  1287. }
  1288. module_exit(fjes_exit_module);