thunderbolt.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. * Networking over Thunderbolt cable using Apple ThunderboltIP protocol
  3. *
  4. * Copyright (C) 2017, Intel Corporation
  5. * Authors: Amir Levy <amir.jer.levy@intel.com>
  6. * Michael Jamet <michael.jamet@intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/highmem.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/jhash.h>
  17. #include <linux/module.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/sizes.h>
  21. #include <linux/thunderbolt.h>
  22. #include <linux/uuid.h>
  23. #include <linux/workqueue.h>
  24. #include <net/ip6_checksum.h>
  25. /* Protocol timeouts in ms */
  26. #define TBNET_LOGIN_DELAY 4500
  27. #define TBNET_LOGIN_TIMEOUT 500
  28. #define TBNET_LOGOUT_TIMEOUT 100
  29. #define TBNET_RING_SIZE 256
  30. #define TBNET_LOCAL_PATH 0xf
  31. #define TBNET_LOGIN_RETRIES 60
  32. #define TBNET_LOGOUT_RETRIES 5
  33. #define TBNET_MATCH_FRAGS_ID BIT(1)
  34. #define TBNET_MAX_MTU SZ_64K
  35. #define TBNET_FRAME_SIZE SZ_4K
  36. #define TBNET_MAX_PAYLOAD_SIZE \
  37. (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
  38. /* Rx packets need to hold space for skb_shared_info */
  39. #define TBNET_RX_MAX_SIZE \
  40. (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  41. #define TBNET_RX_PAGE_ORDER get_order(TBNET_RX_MAX_SIZE)
  42. #define TBNET_RX_PAGE_SIZE (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
  43. #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
  44. /**
  45. * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
  46. * @frame_size: size of the data with the frame
  47. * @frame_index: running index on the frames
  48. * @frame_id: ID of the frame to match frames to specific packet
  49. * @frame_count: how many frames assembles a full packet
  50. *
  51. * Each data frame passed to the high-speed DMA ring has this header. If
  52. * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
  53. * supported then @frame_id is filled, otherwise it stays %0.
  54. */
  55. struct thunderbolt_ip_frame_header {
  56. u32 frame_size;
  57. u16 frame_index;
  58. u16 frame_id;
  59. u32 frame_count;
  60. };
  61. enum thunderbolt_ip_frame_pdf {
  62. TBIP_PDF_FRAME_START = 1,
  63. TBIP_PDF_FRAME_END,
  64. };
  65. enum thunderbolt_ip_type {
  66. TBIP_LOGIN,
  67. TBIP_LOGIN_RESPONSE,
  68. TBIP_LOGOUT,
  69. TBIP_STATUS,
  70. };
  71. struct thunderbolt_ip_header {
  72. u32 route_hi;
  73. u32 route_lo;
  74. u32 length_sn;
  75. uuid_t uuid;
  76. uuid_t initiator_uuid;
  77. uuid_t target_uuid;
  78. u32 type;
  79. u32 command_id;
  80. };
  81. #define TBIP_HDR_LENGTH_MASK GENMASK(5, 0)
  82. #define TBIP_HDR_SN_MASK GENMASK(28, 27)
  83. #define TBIP_HDR_SN_SHIFT 27
  84. struct thunderbolt_ip_login {
  85. struct thunderbolt_ip_header hdr;
  86. u32 proto_version;
  87. u32 transmit_path;
  88. u32 reserved[4];
  89. };
  90. #define TBIP_LOGIN_PROTO_VERSION 1
  91. struct thunderbolt_ip_login_response {
  92. struct thunderbolt_ip_header hdr;
  93. u32 status;
  94. u32 receiver_mac[2];
  95. u32 receiver_mac_len;
  96. u32 reserved[4];
  97. };
  98. struct thunderbolt_ip_logout {
  99. struct thunderbolt_ip_header hdr;
  100. };
  101. struct thunderbolt_ip_status {
  102. struct thunderbolt_ip_header hdr;
  103. u32 status;
  104. };
  105. struct tbnet_stats {
  106. u64 tx_packets;
  107. u64 rx_packets;
  108. u64 tx_bytes;
  109. u64 rx_bytes;
  110. u64 rx_errors;
  111. u64 tx_errors;
  112. u64 rx_length_errors;
  113. u64 rx_over_errors;
  114. u64 rx_crc_errors;
  115. u64 rx_missed_errors;
  116. };
  117. struct tbnet_frame {
  118. struct net_device *dev;
  119. struct page *page;
  120. struct ring_frame frame;
  121. };
  122. struct tbnet_ring {
  123. struct tbnet_frame frames[TBNET_RING_SIZE];
  124. unsigned int cons;
  125. unsigned int prod;
  126. struct tb_ring *ring;
  127. };
  128. /**
  129. * struct tbnet - ThunderboltIP network driver private data
  130. * @svc: XDomain service the driver is bound to
  131. * @xd: XDomain the service blongs to
  132. * @handler: ThunderboltIP configuration protocol handler
  133. * @dev: Networking device
  134. * @napi: NAPI structure for Rx polling
  135. * @stats: Network statistics
  136. * @skb: Network packet that is currently processed on Rx path
  137. * @command_id: ID used for next configuration protocol packet
  138. * @login_sent: ThunderboltIP login message successfully sent
  139. * @login_received: ThunderboltIP login message received from the remote
  140. * host
  141. * @transmit_path: HopID the other end needs to use building the
  142. * opposite side path.
  143. * @connection_lock: Lock serializing access to @login_sent,
  144. * @login_received and @transmit_path.
  145. * @login_retries: Number of login retries currently done
  146. * @login_work: Worker to send ThunderboltIP login packets
  147. * @connected_work: Worker that finalizes the ThunderboltIP connection
  148. * setup and enables DMA paths for high speed data
  149. * transfers
  150. * @disconnect_work: Worker that handles tearing down the ThunderboltIP
  151. * connection
  152. * @rx_hdr: Copy of the currently processed Rx frame. Used when a
  153. * network packet consists of multiple Thunderbolt frames.
  154. * In host byte order.
  155. * @rx_ring: Software ring holding Rx frames
  156. * @frame_id: Frame ID use for next Tx packet
  157. * (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
  158. * @tx_ring: Software ring holding Tx frames
  159. */
  160. struct tbnet {
  161. const struct tb_service *svc;
  162. struct tb_xdomain *xd;
  163. struct tb_protocol_handler handler;
  164. struct net_device *dev;
  165. struct napi_struct napi;
  166. struct tbnet_stats stats;
  167. struct sk_buff *skb;
  168. atomic_t command_id;
  169. bool login_sent;
  170. bool login_received;
  171. u32 transmit_path;
  172. struct mutex connection_lock;
  173. int login_retries;
  174. struct delayed_work login_work;
  175. struct work_struct connected_work;
  176. struct work_struct disconnect_work;
  177. struct thunderbolt_ip_frame_header rx_hdr;
  178. struct tbnet_ring rx_ring;
  179. atomic_t frame_id;
  180. struct tbnet_ring tx_ring;
  181. };
  182. /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
  183. static const uuid_t tbnet_dir_uuid =
  184. UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
  185. 0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
  186. /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
  187. static const uuid_t tbnet_svc_uuid =
  188. UUID_INIT(0x798f589e, 0x3616, 0x8a47,
  189. 0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
  190. static struct tb_property_dir *tbnet_dir;
  191. static void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
  192. u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
  193. enum thunderbolt_ip_type type, size_t size, u32 command_id)
  194. {
  195. u32 length_sn;
  196. /* Length does not include route_hi/lo and length_sn fields */
  197. length_sn = (size - 3 * 4) / 4;
  198. length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
  199. hdr->route_hi = upper_32_bits(route);
  200. hdr->route_lo = lower_32_bits(route);
  201. hdr->length_sn = length_sn;
  202. uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
  203. uuid_copy(&hdr->initiator_uuid, initiator_uuid);
  204. uuid_copy(&hdr->target_uuid, target_uuid);
  205. hdr->type = type;
  206. hdr->command_id = command_id;
  207. }
  208. static int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
  209. u32 command_id)
  210. {
  211. struct thunderbolt_ip_login_response reply;
  212. struct tb_xdomain *xd = net->xd;
  213. memset(&reply, 0, sizeof(reply));
  214. tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
  215. xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
  216. command_id);
  217. memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
  218. reply.receiver_mac_len = ETH_ALEN;
  219. return tb_xdomain_response(xd, &reply, sizeof(reply),
  220. TB_CFG_PKG_XDOMAIN_RESP);
  221. }
  222. static int tbnet_login_request(struct tbnet *net, u8 sequence)
  223. {
  224. struct thunderbolt_ip_login_response reply;
  225. struct thunderbolt_ip_login request;
  226. struct tb_xdomain *xd = net->xd;
  227. memset(&request, 0, sizeof(request));
  228. tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
  229. xd->remote_uuid, TBIP_LOGIN, sizeof(request),
  230. atomic_inc_return(&net->command_id));
  231. request.proto_version = TBIP_LOGIN_PROTO_VERSION;
  232. request.transmit_path = TBNET_LOCAL_PATH;
  233. return tb_xdomain_request(xd, &request, sizeof(request),
  234. TB_CFG_PKG_XDOMAIN_RESP, &reply,
  235. sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
  236. TBNET_LOGIN_TIMEOUT);
  237. }
  238. static int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
  239. u32 command_id)
  240. {
  241. struct thunderbolt_ip_status reply;
  242. struct tb_xdomain *xd = net->xd;
  243. memset(&reply, 0, sizeof(reply));
  244. tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
  245. xd->remote_uuid, TBIP_STATUS, sizeof(reply),
  246. atomic_inc_return(&net->command_id));
  247. return tb_xdomain_response(xd, &reply, sizeof(reply),
  248. TB_CFG_PKG_XDOMAIN_RESP);
  249. }
  250. static int tbnet_logout_request(struct tbnet *net)
  251. {
  252. struct thunderbolt_ip_logout request;
  253. struct thunderbolt_ip_status reply;
  254. struct tb_xdomain *xd = net->xd;
  255. memset(&request, 0, sizeof(request));
  256. tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
  257. xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
  258. atomic_inc_return(&net->command_id));
  259. return tb_xdomain_request(xd, &request, sizeof(request),
  260. TB_CFG_PKG_XDOMAIN_RESP, &reply,
  261. sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
  262. TBNET_LOGOUT_TIMEOUT);
  263. }
  264. static void start_login(struct tbnet *net)
  265. {
  266. mutex_lock(&net->connection_lock);
  267. net->login_sent = false;
  268. net->login_received = false;
  269. mutex_unlock(&net->connection_lock);
  270. queue_delayed_work(system_long_wq, &net->login_work,
  271. msecs_to_jiffies(1000));
  272. }
  273. static void stop_login(struct tbnet *net)
  274. {
  275. cancel_delayed_work_sync(&net->login_work);
  276. cancel_work_sync(&net->connected_work);
  277. }
  278. static inline unsigned int tbnet_frame_size(const struct tbnet_frame *tf)
  279. {
  280. return tf->frame.size ? : TBNET_FRAME_SIZE;
  281. }
  282. static void tbnet_free_buffers(struct tbnet_ring *ring)
  283. {
  284. unsigned int i;
  285. for (i = 0; i < TBNET_RING_SIZE; i++) {
  286. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  287. struct tbnet_frame *tf = &ring->frames[i];
  288. enum dma_data_direction dir;
  289. unsigned int order;
  290. size_t size;
  291. if (!tf->page)
  292. continue;
  293. if (ring->ring->is_tx) {
  294. dir = DMA_TO_DEVICE;
  295. order = 0;
  296. size = TBNET_FRAME_SIZE;
  297. } else {
  298. dir = DMA_FROM_DEVICE;
  299. order = TBNET_RX_PAGE_ORDER;
  300. size = TBNET_RX_PAGE_SIZE;
  301. }
  302. if (tf->frame.buffer_phy)
  303. dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
  304. dir);
  305. __free_pages(tf->page, order);
  306. tf->page = NULL;
  307. }
  308. ring->cons = 0;
  309. ring->prod = 0;
  310. }
  311. static void tbnet_tear_down(struct tbnet *net, bool send_logout)
  312. {
  313. netif_carrier_off(net->dev);
  314. netif_stop_queue(net->dev);
  315. stop_login(net);
  316. mutex_lock(&net->connection_lock);
  317. if (net->login_sent && net->login_received) {
  318. int retries = TBNET_LOGOUT_RETRIES;
  319. while (send_logout && retries-- > 0) {
  320. int ret = tbnet_logout_request(net);
  321. if (ret != -ETIMEDOUT)
  322. break;
  323. }
  324. tb_ring_stop(net->rx_ring.ring);
  325. tb_ring_stop(net->tx_ring.ring);
  326. tbnet_free_buffers(&net->rx_ring);
  327. tbnet_free_buffers(&net->tx_ring);
  328. if (tb_xdomain_disable_paths(net->xd))
  329. netdev_warn(net->dev, "failed to disable DMA paths\n");
  330. }
  331. net->login_retries = 0;
  332. net->login_sent = false;
  333. net->login_received = false;
  334. mutex_unlock(&net->connection_lock);
  335. }
  336. static int tbnet_handle_packet(const void *buf, size_t size, void *data)
  337. {
  338. const struct thunderbolt_ip_login *pkg = buf;
  339. struct tbnet *net = data;
  340. u32 command_id;
  341. int ret = 0;
  342. u32 sequence;
  343. u64 route;
  344. /* Make sure the packet is for us */
  345. if (size < sizeof(struct thunderbolt_ip_header))
  346. return 0;
  347. if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
  348. return 0;
  349. if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
  350. return 0;
  351. route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
  352. route &= ~BIT_ULL(63);
  353. if (route != net->xd->route)
  354. return 0;
  355. sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
  356. sequence >>= TBIP_HDR_SN_SHIFT;
  357. command_id = pkg->hdr.command_id;
  358. switch (pkg->hdr.type) {
  359. case TBIP_LOGIN:
  360. if (!netif_running(net->dev))
  361. break;
  362. ret = tbnet_login_response(net, route, sequence,
  363. pkg->hdr.command_id);
  364. if (!ret) {
  365. mutex_lock(&net->connection_lock);
  366. net->login_received = true;
  367. net->transmit_path = pkg->transmit_path;
  368. /* If we reached the number of max retries or
  369. * previous logout, schedule another round of
  370. * login retries
  371. */
  372. if (net->login_retries >= TBNET_LOGIN_RETRIES ||
  373. !net->login_sent) {
  374. net->login_retries = 0;
  375. queue_delayed_work(system_long_wq,
  376. &net->login_work, 0);
  377. }
  378. mutex_unlock(&net->connection_lock);
  379. queue_work(system_long_wq, &net->connected_work);
  380. }
  381. break;
  382. case TBIP_LOGOUT:
  383. ret = tbnet_logout_response(net, route, sequence, command_id);
  384. if (!ret)
  385. queue_work(system_long_wq, &net->disconnect_work);
  386. break;
  387. default:
  388. return 0;
  389. }
  390. if (ret)
  391. netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
  392. return 1;
  393. }
  394. static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
  395. {
  396. return ring->prod - ring->cons;
  397. }
  398. static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
  399. {
  400. struct tbnet_ring *ring = &net->rx_ring;
  401. int ret;
  402. while (nbuffers--) {
  403. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  404. unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
  405. struct tbnet_frame *tf = &ring->frames[index];
  406. dma_addr_t dma_addr;
  407. if (tf->page)
  408. break;
  409. /* Allocate page (order > 0) so that it can hold maximum
  410. * ThunderboltIP frame (4kB) and the additional room for
  411. * SKB shared info required by build_skb().
  412. */
  413. tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
  414. if (!tf->page) {
  415. ret = -ENOMEM;
  416. goto err_free;
  417. }
  418. dma_addr = dma_map_page(dma_dev, tf->page, 0,
  419. TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
  420. if (dma_mapping_error(dma_dev, dma_addr)) {
  421. ret = -ENOMEM;
  422. goto err_free;
  423. }
  424. tf->frame.buffer_phy = dma_addr;
  425. tf->dev = net->dev;
  426. tb_ring_rx(ring->ring, &tf->frame);
  427. ring->prod++;
  428. }
  429. return 0;
  430. err_free:
  431. tbnet_free_buffers(ring);
  432. return ret;
  433. }
  434. static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
  435. {
  436. struct tbnet_ring *ring = &net->tx_ring;
  437. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  438. struct tbnet_frame *tf;
  439. unsigned int index;
  440. if (!tbnet_available_buffers(ring))
  441. return NULL;
  442. index = ring->cons++ & (TBNET_RING_SIZE - 1);
  443. tf = &ring->frames[index];
  444. tf->frame.size = 0;
  445. dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
  446. tbnet_frame_size(tf), DMA_TO_DEVICE);
  447. return tf;
  448. }
  449. static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
  450. bool canceled)
  451. {
  452. struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
  453. struct tbnet *net = netdev_priv(tf->dev);
  454. /* Return buffer to the ring */
  455. net->tx_ring.prod++;
  456. if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
  457. netif_wake_queue(net->dev);
  458. }
  459. static int tbnet_alloc_tx_buffers(struct tbnet *net)
  460. {
  461. struct tbnet_ring *ring = &net->tx_ring;
  462. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  463. unsigned int i;
  464. for (i = 0; i < TBNET_RING_SIZE; i++) {
  465. struct tbnet_frame *tf = &ring->frames[i];
  466. dma_addr_t dma_addr;
  467. tf->page = alloc_page(GFP_KERNEL);
  468. if (!tf->page) {
  469. tbnet_free_buffers(ring);
  470. return -ENOMEM;
  471. }
  472. dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
  473. DMA_TO_DEVICE);
  474. if (dma_mapping_error(dma_dev, dma_addr)) {
  475. __free_page(tf->page);
  476. tf->page = NULL;
  477. tbnet_free_buffers(ring);
  478. return -ENOMEM;
  479. }
  480. tf->dev = net->dev;
  481. tf->frame.buffer_phy = dma_addr;
  482. tf->frame.callback = tbnet_tx_callback;
  483. tf->frame.sof = TBIP_PDF_FRAME_START;
  484. tf->frame.eof = TBIP_PDF_FRAME_END;
  485. }
  486. ring->cons = 0;
  487. ring->prod = TBNET_RING_SIZE - 1;
  488. return 0;
  489. }
  490. static void tbnet_connected_work(struct work_struct *work)
  491. {
  492. struct tbnet *net = container_of(work, typeof(*net), connected_work);
  493. bool connected;
  494. int ret;
  495. if (netif_carrier_ok(net->dev))
  496. return;
  497. mutex_lock(&net->connection_lock);
  498. connected = net->login_sent && net->login_received;
  499. mutex_unlock(&net->connection_lock);
  500. if (!connected)
  501. return;
  502. /* Both logins successful so enable the high-speed DMA paths and
  503. * start the network device queue.
  504. */
  505. ret = tb_xdomain_enable_paths(net->xd, TBNET_LOCAL_PATH,
  506. net->rx_ring.ring->hop,
  507. net->transmit_path,
  508. net->tx_ring.ring->hop);
  509. if (ret) {
  510. netdev_err(net->dev, "failed to enable DMA paths\n");
  511. return;
  512. }
  513. tb_ring_start(net->tx_ring.ring);
  514. tb_ring_start(net->rx_ring.ring);
  515. ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
  516. if (ret)
  517. goto err_stop_rings;
  518. ret = tbnet_alloc_tx_buffers(net);
  519. if (ret)
  520. goto err_free_rx_buffers;
  521. netif_carrier_on(net->dev);
  522. netif_start_queue(net->dev);
  523. return;
  524. err_free_rx_buffers:
  525. tbnet_free_buffers(&net->rx_ring);
  526. err_stop_rings:
  527. tb_ring_stop(net->rx_ring.ring);
  528. tb_ring_stop(net->tx_ring.ring);
  529. }
  530. static void tbnet_login_work(struct work_struct *work)
  531. {
  532. struct tbnet *net = container_of(work, typeof(*net), login_work.work);
  533. unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
  534. int ret;
  535. if (netif_carrier_ok(net->dev))
  536. return;
  537. ret = tbnet_login_request(net, net->login_retries % 4);
  538. if (ret) {
  539. if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
  540. queue_delayed_work(system_long_wq, &net->login_work,
  541. delay);
  542. } else {
  543. netdev_info(net->dev, "ThunderboltIP login timed out\n");
  544. }
  545. } else {
  546. net->login_retries = 0;
  547. mutex_lock(&net->connection_lock);
  548. net->login_sent = true;
  549. mutex_unlock(&net->connection_lock);
  550. queue_work(system_long_wq, &net->connected_work);
  551. }
  552. }
  553. static void tbnet_disconnect_work(struct work_struct *work)
  554. {
  555. struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
  556. tbnet_tear_down(net, false);
  557. }
  558. static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
  559. const struct thunderbolt_ip_frame_header *hdr)
  560. {
  561. u32 frame_id, frame_count, frame_size, frame_index;
  562. unsigned int size;
  563. if (tf->frame.flags & RING_DESC_CRC_ERROR) {
  564. net->stats.rx_crc_errors++;
  565. return false;
  566. } else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
  567. net->stats.rx_over_errors++;
  568. return false;
  569. }
  570. /* Should be greater than just header i.e. contains data */
  571. size = tbnet_frame_size(tf);
  572. if (size <= sizeof(*hdr)) {
  573. net->stats.rx_length_errors++;
  574. return false;
  575. }
  576. frame_count = le32_to_cpu(hdr->frame_count);
  577. frame_size = le32_to_cpu(hdr->frame_size);
  578. frame_index = le16_to_cpu(hdr->frame_index);
  579. frame_id = le16_to_cpu(hdr->frame_id);
  580. if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
  581. net->stats.rx_length_errors++;
  582. return false;
  583. }
  584. /* In case we're in the middle of packet, validate the frame
  585. * header based on first fragment of the packet.
  586. */
  587. if (net->skb && net->rx_hdr.frame_count) {
  588. /* Check the frame count fits the count field */
  589. if (frame_count != net->rx_hdr.frame_count) {
  590. net->stats.rx_length_errors++;
  591. return false;
  592. }
  593. /* Check the frame identifiers are incremented correctly,
  594. * and id is matching.
  595. */
  596. if (frame_index != net->rx_hdr.frame_index + 1 ||
  597. frame_id != net->rx_hdr.frame_id) {
  598. net->stats.rx_missed_errors++;
  599. return false;
  600. }
  601. if (net->skb->len + frame_size > TBNET_MAX_MTU) {
  602. net->stats.rx_length_errors++;
  603. return false;
  604. }
  605. return true;
  606. }
  607. /* Start of packet, validate the frame header */
  608. if (frame_count == 0 || frame_count > TBNET_RING_SIZE / 4) {
  609. net->stats.rx_length_errors++;
  610. return false;
  611. }
  612. if (frame_index != 0) {
  613. net->stats.rx_missed_errors++;
  614. return false;
  615. }
  616. return true;
  617. }
  618. static int tbnet_poll(struct napi_struct *napi, int budget)
  619. {
  620. struct tbnet *net = container_of(napi, struct tbnet, napi);
  621. unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
  622. struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
  623. unsigned int rx_packets = 0;
  624. while (rx_packets < budget) {
  625. const struct thunderbolt_ip_frame_header *hdr;
  626. unsigned int hdr_size = sizeof(*hdr);
  627. struct sk_buff *skb = NULL;
  628. struct ring_frame *frame;
  629. struct tbnet_frame *tf;
  630. struct page *page;
  631. bool last = true;
  632. u32 frame_size;
  633. /* Return some buffers to hardware, one at a time is too
  634. * slow so allocate MAX_SKB_FRAGS buffers at the same
  635. * time.
  636. */
  637. if (cleaned_count >= MAX_SKB_FRAGS) {
  638. tbnet_alloc_rx_buffers(net, cleaned_count);
  639. cleaned_count = 0;
  640. }
  641. frame = tb_ring_poll(net->rx_ring.ring);
  642. if (!frame)
  643. break;
  644. dma_unmap_page(dma_dev, frame->buffer_phy,
  645. TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
  646. tf = container_of(frame, typeof(*tf), frame);
  647. page = tf->page;
  648. tf->page = NULL;
  649. net->rx_ring.cons++;
  650. cleaned_count++;
  651. hdr = page_address(page);
  652. if (!tbnet_check_frame(net, tf, hdr)) {
  653. __free_pages(page, TBNET_RX_PAGE_ORDER);
  654. dev_kfree_skb_any(net->skb);
  655. net->skb = NULL;
  656. continue;
  657. }
  658. frame_size = le32_to_cpu(hdr->frame_size);
  659. skb = net->skb;
  660. if (!skb) {
  661. skb = build_skb(page_address(page),
  662. TBNET_RX_PAGE_SIZE);
  663. if (!skb) {
  664. __free_pages(page, TBNET_RX_PAGE_ORDER);
  665. net->stats.rx_errors++;
  666. break;
  667. }
  668. skb_reserve(skb, hdr_size);
  669. skb_put(skb, frame_size);
  670. net->skb = skb;
  671. } else {
  672. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  673. page, hdr_size, frame_size,
  674. TBNET_RX_PAGE_SIZE - hdr_size);
  675. }
  676. net->rx_hdr.frame_size = frame_size;
  677. net->rx_hdr.frame_count = le32_to_cpu(hdr->frame_count);
  678. net->rx_hdr.frame_index = le16_to_cpu(hdr->frame_index);
  679. net->rx_hdr.frame_id = le16_to_cpu(hdr->frame_id);
  680. last = net->rx_hdr.frame_index == net->rx_hdr.frame_count - 1;
  681. rx_packets++;
  682. net->stats.rx_bytes += frame_size;
  683. if (last) {
  684. skb->protocol = eth_type_trans(skb, net->dev);
  685. napi_gro_receive(&net->napi, skb);
  686. net->skb = NULL;
  687. }
  688. }
  689. net->stats.rx_packets += rx_packets;
  690. if (cleaned_count)
  691. tbnet_alloc_rx_buffers(net, cleaned_count);
  692. if (rx_packets >= budget)
  693. return budget;
  694. napi_complete_done(napi, rx_packets);
  695. /* Re-enable the ring interrupt */
  696. tb_ring_poll_complete(net->rx_ring.ring);
  697. return rx_packets;
  698. }
  699. static void tbnet_start_poll(void *data)
  700. {
  701. struct tbnet *net = data;
  702. napi_schedule(&net->napi);
  703. }
  704. static int tbnet_open(struct net_device *dev)
  705. {
  706. struct tbnet *net = netdev_priv(dev);
  707. struct tb_xdomain *xd = net->xd;
  708. u16 sof_mask, eof_mask;
  709. struct tb_ring *ring;
  710. netif_carrier_off(dev);
  711. ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE,
  712. RING_FLAG_FRAME);
  713. if (!ring) {
  714. netdev_err(dev, "failed to allocate Tx ring\n");
  715. return -ENOMEM;
  716. }
  717. net->tx_ring.ring = ring;
  718. sof_mask = BIT(TBIP_PDF_FRAME_START);
  719. eof_mask = BIT(TBIP_PDF_FRAME_END);
  720. ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE,
  721. RING_FLAG_FRAME | RING_FLAG_E2E, sof_mask,
  722. eof_mask, tbnet_start_poll, net);
  723. if (!ring) {
  724. netdev_err(dev, "failed to allocate Rx ring\n");
  725. tb_ring_free(net->tx_ring.ring);
  726. net->tx_ring.ring = NULL;
  727. return -ENOMEM;
  728. }
  729. net->rx_ring.ring = ring;
  730. napi_enable(&net->napi);
  731. start_login(net);
  732. return 0;
  733. }
  734. static int tbnet_stop(struct net_device *dev)
  735. {
  736. struct tbnet *net = netdev_priv(dev);
  737. napi_disable(&net->napi);
  738. cancel_work_sync(&net->disconnect_work);
  739. tbnet_tear_down(net, true);
  740. tb_ring_free(net->rx_ring.ring);
  741. net->rx_ring.ring = NULL;
  742. tb_ring_free(net->tx_ring.ring);
  743. net->tx_ring.ring = NULL;
  744. return 0;
  745. }
  746. static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
  747. struct tbnet_frame **frames, u32 frame_count)
  748. {
  749. struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
  750. struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
  751. __wsum wsum = htonl(skb->len - skb_transport_offset(skb));
  752. unsigned int i, len, offset = skb_transport_offset(skb);
  753. __be16 protocol = skb->protocol;
  754. void *data = skb->data;
  755. void *dest = hdr + 1;
  756. __sum16 *tucso;
  757. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  758. /* No need to calculate checksum so we just update the
  759. * total frame count and sync the frames for DMA.
  760. */
  761. for (i = 0; i < frame_count; i++) {
  762. hdr = page_address(frames[i]->page);
  763. hdr->frame_count = cpu_to_le32(frame_count);
  764. dma_sync_single_for_device(dma_dev,
  765. frames[i]->frame.buffer_phy,
  766. tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
  767. }
  768. return true;
  769. }
  770. if (protocol == htons(ETH_P_8021Q)) {
  771. struct vlan_hdr *vhdr, vh;
  772. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh);
  773. if (!vhdr)
  774. return false;
  775. protocol = vhdr->h_vlan_encapsulated_proto;
  776. }
  777. /* Data points on the beginning of packet.
  778. * Check is the checksum absolute place in the packet.
  779. * ipcso will update IP checksum.
  780. * tucso will update TCP/UPD checksum.
  781. */
  782. if (protocol == htons(ETH_P_IP)) {
  783. __sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data);
  784. *ipcso = 0;
  785. *ipcso = ip_fast_csum(dest + skb_network_offset(skb),
  786. ip_hdr(skb)->ihl);
  787. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  788. tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
  789. else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  790. tucso = dest + ((void *)&(udp_hdr(skb)->check) - data);
  791. else
  792. return false;
  793. *tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  794. ip_hdr(skb)->daddr, 0,
  795. ip_hdr(skb)->protocol, 0);
  796. } else if (skb_is_gso_v6(skb)) {
  797. tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
  798. *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  799. &ipv6_hdr(skb)->daddr, 0,
  800. IPPROTO_TCP, 0);
  801. return false;
  802. } else if (protocol == htons(ETH_P_IPV6)) {
  803. tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset;
  804. *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  805. &ipv6_hdr(skb)->daddr, 0,
  806. ipv6_hdr(skb)->nexthdr, 0);
  807. } else {
  808. return false;
  809. }
  810. /* First frame was headers, rest of the frames contain data.
  811. * Calculate checksum over each frame.
  812. */
  813. for (i = 0; i < frame_count; i++) {
  814. hdr = page_address(frames[i]->page);
  815. dest = (void *)(hdr + 1) + offset;
  816. len = le32_to_cpu(hdr->frame_size) - offset;
  817. wsum = csum_partial(dest, len, wsum);
  818. hdr->frame_count = cpu_to_le32(frame_count);
  819. offset = 0;
  820. }
  821. *tucso = csum_fold(wsum);
  822. /* Checksum is finally calculated and we don't touch the memory
  823. * anymore, so DMA sync the frames now.
  824. */
  825. for (i = 0; i < frame_count; i++) {
  826. dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy,
  827. tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
  828. }
  829. return true;
  830. }
  831. static void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num,
  832. unsigned int *len)
  833. {
  834. const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
  835. *len = skb_frag_size(frag);
  836. return kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
  837. }
  838. static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
  839. struct net_device *dev)
  840. {
  841. struct tbnet *net = netdev_priv(dev);
  842. struct tbnet_frame *frames[MAX_SKB_FRAGS];
  843. u16 frame_id = atomic_read(&net->frame_id);
  844. struct thunderbolt_ip_frame_header *hdr;
  845. unsigned int len = skb_headlen(skb);
  846. unsigned int data_len = skb->len;
  847. unsigned int nframes, i;
  848. unsigned int frag = 0;
  849. void *src = skb->data;
  850. u32 frame_index = 0;
  851. bool unmap = false;
  852. void *dest;
  853. nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE);
  854. if (tbnet_available_buffers(&net->tx_ring) < nframes) {
  855. netif_stop_queue(net->dev);
  856. return NETDEV_TX_BUSY;
  857. }
  858. frames[frame_index] = tbnet_get_tx_buffer(net);
  859. if (!frames[frame_index])
  860. goto err_drop;
  861. hdr = page_address(frames[frame_index]->page);
  862. dest = hdr + 1;
  863. /* If overall packet is bigger than the frame data size */
  864. while (data_len > TBNET_MAX_PAYLOAD_SIZE) {
  865. unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE;
  866. hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE);
  867. hdr->frame_index = cpu_to_le16(frame_index);
  868. hdr->frame_id = cpu_to_le16(frame_id);
  869. do {
  870. if (len > size_left) {
  871. /* Copy data onto Tx buffer data with
  872. * full frame size then break and go to
  873. * next frame
  874. */
  875. memcpy(dest, src, size_left);
  876. len -= size_left;
  877. dest += size_left;
  878. src += size_left;
  879. break;
  880. }
  881. memcpy(dest, src, len);
  882. size_left -= len;
  883. dest += len;
  884. if (unmap) {
  885. kunmap_atomic(src);
  886. unmap = false;
  887. }
  888. /* Ensure all fragments have been processed */
  889. if (frag < skb_shinfo(skb)->nr_frags) {
  890. /* Map and then unmap quickly */
  891. src = tbnet_kmap_frag(skb, frag++, &len);
  892. unmap = true;
  893. } else if (unlikely(size_left > 0)) {
  894. goto err_drop;
  895. }
  896. } while (size_left > 0);
  897. data_len -= TBNET_MAX_PAYLOAD_SIZE;
  898. frame_index++;
  899. frames[frame_index] = tbnet_get_tx_buffer(net);
  900. if (!frames[frame_index])
  901. goto err_drop;
  902. hdr = page_address(frames[frame_index]->page);
  903. dest = hdr + 1;
  904. }
  905. hdr->frame_size = cpu_to_le32(data_len);
  906. hdr->frame_index = cpu_to_le16(frame_index);
  907. hdr->frame_id = cpu_to_le16(frame_id);
  908. frames[frame_index]->frame.size = data_len + sizeof(*hdr);
  909. /* In case the remaining data_len is smaller than a frame */
  910. while (len < data_len) {
  911. memcpy(dest, src, len);
  912. data_len -= len;
  913. dest += len;
  914. if (unmap) {
  915. kunmap_atomic(src);
  916. unmap = false;
  917. }
  918. if (frag < skb_shinfo(skb)->nr_frags) {
  919. src = tbnet_kmap_frag(skb, frag++, &len);
  920. unmap = true;
  921. } else if (unlikely(data_len > 0)) {
  922. goto err_drop;
  923. }
  924. }
  925. memcpy(dest, src, data_len);
  926. if (unmap)
  927. kunmap_atomic(src);
  928. if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
  929. goto err_drop;
  930. for (i = 0; i < frame_index + 1; i++)
  931. tb_ring_tx(net->tx_ring.ring, &frames[i]->frame);
  932. if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID)
  933. atomic_inc(&net->frame_id);
  934. net->stats.tx_packets++;
  935. net->stats.tx_bytes += skb->len;
  936. dev_consume_skb_any(skb);
  937. return NETDEV_TX_OK;
  938. err_drop:
  939. /* We can re-use the buffers */
  940. net->tx_ring.cons -= frame_index;
  941. dev_kfree_skb_any(skb);
  942. net->stats.tx_errors++;
  943. return NETDEV_TX_OK;
  944. }
  945. static void tbnet_get_stats64(struct net_device *dev,
  946. struct rtnl_link_stats64 *stats)
  947. {
  948. struct tbnet *net = netdev_priv(dev);
  949. stats->tx_packets = net->stats.tx_packets;
  950. stats->rx_packets = net->stats.rx_packets;
  951. stats->tx_bytes = net->stats.tx_bytes;
  952. stats->rx_bytes = net->stats.rx_bytes;
  953. stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors +
  954. net->stats.rx_over_errors + net->stats.rx_crc_errors +
  955. net->stats.rx_missed_errors;
  956. stats->tx_errors = net->stats.tx_errors;
  957. stats->rx_length_errors = net->stats.rx_length_errors;
  958. stats->rx_over_errors = net->stats.rx_over_errors;
  959. stats->rx_crc_errors = net->stats.rx_crc_errors;
  960. stats->rx_missed_errors = net->stats.rx_missed_errors;
  961. }
  962. static const struct net_device_ops tbnet_netdev_ops = {
  963. .ndo_open = tbnet_open,
  964. .ndo_stop = tbnet_stop,
  965. .ndo_start_xmit = tbnet_start_xmit,
  966. .ndo_get_stats64 = tbnet_get_stats64,
  967. };
  968. static void tbnet_generate_mac(struct net_device *dev)
  969. {
  970. const struct tbnet *net = netdev_priv(dev);
  971. const struct tb_xdomain *xd = net->xd;
  972. u8 phy_port;
  973. u32 hash;
  974. phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route));
  975. /* Unicast and locally administered MAC */
  976. dev->dev_addr[0] = phy_port << 4 | 0x02;
  977. hash = jhash2((u32 *)xd->local_uuid, 4, 0);
  978. memcpy(dev->dev_addr + 1, &hash, sizeof(hash));
  979. hash = jhash2((u32 *)xd->local_uuid, 4, hash);
  980. dev->dev_addr[5] = hash & 0xff;
  981. }
  982. static int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id)
  983. {
  984. struct tb_xdomain *xd = tb_service_parent(svc);
  985. struct net_device *dev;
  986. struct tbnet *net;
  987. int ret;
  988. dev = alloc_etherdev(sizeof(*net));
  989. if (!dev)
  990. return -ENOMEM;
  991. SET_NETDEV_DEV(dev, &svc->dev);
  992. net = netdev_priv(dev);
  993. INIT_DELAYED_WORK(&net->login_work, tbnet_login_work);
  994. INIT_WORK(&net->connected_work, tbnet_connected_work);
  995. INIT_WORK(&net->disconnect_work, tbnet_disconnect_work);
  996. mutex_init(&net->connection_lock);
  997. atomic_set(&net->command_id, 0);
  998. atomic_set(&net->frame_id, 0);
  999. net->svc = svc;
  1000. net->dev = dev;
  1001. net->xd = xd;
  1002. tbnet_generate_mac(dev);
  1003. strcpy(dev->name, "thunderbolt%d");
  1004. dev->netdev_ops = &tbnet_netdev_ops;
  1005. /* ThunderboltIP takes advantage of TSO packets but instead of
  1006. * segmenting them we just split the packet into Thunderbolt
  1007. * frames (maximum payload size of each frame is 4084 bytes) and
  1008. * calculate checksum over the whole packet here.
  1009. *
  1010. * The receiving side does the opposite if the host OS supports
  1011. * LRO, otherwise it needs to split the large packet into MTU
  1012. * sized smaller packets.
  1013. *
  1014. * In order to receive large packets from the networking stack,
  1015. * we need to announce support for most of the offloading
  1016. * features here.
  1017. */
  1018. dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO |
  1019. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1020. dev->features = dev->hw_features | NETIF_F_HIGHDMA;
  1021. dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header);
  1022. netif_napi_add(dev, &net->napi, tbnet_poll, NAPI_POLL_WEIGHT);
  1023. /* MTU range: 68 - 65522 */
  1024. dev->min_mtu = ETH_MIN_MTU;
  1025. dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN;
  1026. net->handler.uuid = &tbnet_svc_uuid;
  1027. net->handler.callback = tbnet_handle_packet,
  1028. net->handler.data = net;
  1029. tb_register_protocol_handler(&net->handler);
  1030. tb_service_set_drvdata(svc, net);
  1031. ret = register_netdev(dev);
  1032. if (ret) {
  1033. tb_unregister_protocol_handler(&net->handler);
  1034. free_netdev(dev);
  1035. return ret;
  1036. }
  1037. return 0;
  1038. }
  1039. static void tbnet_remove(struct tb_service *svc)
  1040. {
  1041. struct tbnet *net = tb_service_get_drvdata(svc);
  1042. unregister_netdev(net->dev);
  1043. tb_unregister_protocol_handler(&net->handler);
  1044. free_netdev(net->dev);
  1045. }
  1046. static void tbnet_shutdown(struct tb_service *svc)
  1047. {
  1048. tbnet_tear_down(tb_service_get_drvdata(svc), true);
  1049. }
  1050. static int __maybe_unused tbnet_suspend(struct device *dev)
  1051. {
  1052. struct tb_service *svc = tb_to_service(dev);
  1053. struct tbnet *net = tb_service_get_drvdata(svc);
  1054. stop_login(net);
  1055. if (netif_running(net->dev)) {
  1056. netif_device_detach(net->dev);
  1057. tbnet_tear_down(net, true);
  1058. }
  1059. tb_unregister_protocol_handler(&net->handler);
  1060. return 0;
  1061. }
  1062. static int __maybe_unused tbnet_resume(struct device *dev)
  1063. {
  1064. struct tb_service *svc = tb_to_service(dev);
  1065. struct tbnet *net = tb_service_get_drvdata(svc);
  1066. tb_register_protocol_handler(&net->handler);
  1067. netif_carrier_off(net->dev);
  1068. if (netif_running(net->dev)) {
  1069. netif_device_attach(net->dev);
  1070. start_login(net);
  1071. }
  1072. return 0;
  1073. }
  1074. static const struct dev_pm_ops tbnet_pm_ops = {
  1075. SET_SYSTEM_SLEEP_PM_OPS(tbnet_suspend, tbnet_resume)
  1076. };
  1077. static const struct tb_service_id tbnet_ids[] = {
  1078. { TB_SERVICE("network", 1) },
  1079. { },
  1080. };
  1081. MODULE_DEVICE_TABLE(tbsvc, tbnet_ids);
  1082. static struct tb_service_driver tbnet_driver = {
  1083. .driver = {
  1084. .owner = THIS_MODULE,
  1085. .name = "thunderbolt-net",
  1086. .pm = &tbnet_pm_ops,
  1087. },
  1088. .probe = tbnet_probe,
  1089. .remove = tbnet_remove,
  1090. .shutdown = tbnet_shutdown,
  1091. .id_table = tbnet_ids,
  1092. };
  1093. static int __init tbnet_init(void)
  1094. {
  1095. int ret;
  1096. tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid);
  1097. if (!tbnet_dir)
  1098. return -ENOMEM;
  1099. tb_property_add_immediate(tbnet_dir, "prtcid", 1);
  1100. tb_property_add_immediate(tbnet_dir, "prtcvers", 1);
  1101. tb_property_add_immediate(tbnet_dir, "prtcrevs", 1);
  1102. tb_property_add_immediate(tbnet_dir, "prtcstns",
  1103. TBNET_MATCH_FRAGS_ID);
  1104. ret = tb_register_property_dir("network", tbnet_dir);
  1105. if (ret) {
  1106. tb_property_free_dir(tbnet_dir);
  1107. return ret;
  1108. }
  1109. return tb_register_service_driver(&tbnet_driver);
  1110. }
  1111. module_init(tbnet_init);
  1112. static void __exit tbnet_exit(void)
  1113. {
  1114. tb_unregister_service_driver(&tbnet_driver);
  1115. tb_unregister_property_dir("network", tbnet_dir);
  1116. tb_property_free_dir(tbnet_dir);
  1117. }
  1118. module_exit(tbnet_exit);
  1119. MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
  1120. MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
  1121. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1122. MODULE_DESCRIPTION("Thunderbolt network driver");
  1123. MODULE_LICENSE("GPL v2");