thunderbolt.c 35 KB

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