pci.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/pci.c
  3. * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/export.h>
  37. #include <linux/err.h>
  38. #include <linux/device.h>
  39. #include <linux/pci.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/wait.h>
  42. #include <linux/types.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/if_vlan.h>
  45. #include <linux/log2.h>
  46. #include <linux/string.h>
  47. #include "pci_hw.h"
  48. #include "pci.h"
  49. #include "core.h"
  50. #include "cmd.h"
  51. #include "port.h"
  52. #include "resources.h"
  53. static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
  54. #define mlxsw_pci_write32(mlxsw_pci, reg, val) \
  55. iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
  56. #define mlxsw_pci_read32(mlxsw_pci, reg) \
  57. ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
  58. enum mlxsw_pci_queue_type {
  59. MLXSW_PCI_QUEUE_TYPE_SDQ,
  60. MLXSW_PCI_QUEUE_TYPE_RDQ,
  61. MLXSW_PCI_QUEUE_TYPE_CQ,
  62. MLXSW_PCI_QUEUE_TYPE_EQ,
  63. };
  64. #define MLXSW_PCI_QUEUE_TYPE_COUNT 4
  65. static const u16 mlxsw_pci_doorbell_type_offset[] = {
  66. MLXSW_PCI_DOORBELL_SDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_SDQ */
  67. MLXSW_PCI_DOORBELL_RDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_RDQ */
  68. MLXSW_PCI_DOORBELL_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
  69. MLXSW_PCI_DOORBELL_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
  70. };
  71. static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
  72. 0, /* unused */
  73. 0, /* unused */
  74. MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
  75. MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
  76. };
  77. struct mlxsw_pci_mem_item {
  78. char *buf;
  79. dma_addr_t mapaddr;
  80. size_t size;
  81. };
  82. struct mlxsw_pci_queue_elem_info {
  83. char *elem; /* pointer to actual dma mapped element mem chunk */
  84. union {
  85. struct {
  86. struct sk_buff *skb;
  87. } sdq;
  88. struct {
  89. struct sk_buff *skb;
  90. } rdq;
  91. } u;
  92. };
  93. struct mlxsw_pci_queue {
  94. spinlock_t lock; /* for queue accesses */
  95. struct mlxsw_pci_mem_item mem_item;
  96. struct mlxsw_pci_queue_elem_info *elem_info;
  97. u16 producer_counter;
  98. u16 consumer_counter;
  99. u16 count; /* number of elements in queue */
  100. u8 num; /* queue number */
  101. u8 elem_size; /* size of one element */
  102. enum mlxsw_pci_queue_type type;
  103. struct tasklet_struct tasklet; /* queue processing tasklet */
  104. struct mlxsw_pci *pci;
  105. union {
  106. struct {
  107. u32 comp_sdq_count;
  108. u32 comp_rdq_count;
  109. } cq;
  110. struct {
  111. u32 ev_cmd_count;
  112. u32 ev_comp_count;
  113. u32 ev_other_count;
  114. } eq;
  115. } u;
  116. };
  117. struct mlxsw_pci_queue_type_group {
  118. struct mlxsw_pci_queue *q;
  119. u8 count; /* number of queues in group */
  120. };
  121. struct mlxsw_pci {
  122. struct pci_dev *pdev;
  123. u8 __iomem *hw_addr;
  124. struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
  125. u32 doorbell_offset;
  126. struct mlxsw_core *core;
  127. struct {
  128. struct mlxsw_pci_mem_item *items;
  129. unsigned int count;
  130. } fw_area;
  131. struct {
  132. struct mlxsw_pci_mem_item out_mbox;
  133. struct mlxsw_pci_mem_item in_mbox;
  134. struct mutex lock; /* Lock access to command registers */
  135. bool nopoll;
  136. wait_queue_head_t wait;
  137. bool wait_done;
  138. struct {
  139. u8 status;
  140. u64 out_param;
  141. } comp;
  142. } cmd;
  143. struct mlxsw_bus_info bus_info;
  144. };
  145. static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q)
  146. {
  147. tasklet_schedule(&q->tasklet);
  148. }
  149. static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
  150. size_t elem_size, int elem_index)
  151. {
  152. return q->mem_item.buf + (elem_size * elem_index);
  153. }
  154. static struct mlxsw_pci_queue_elem_info *
  155. mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
  156. {
  157. return &q->elem_info[elem_index];
  158. }
  159. static struct mlxsw_pci_queue_elem_info *
  160. mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
  161. {
  162. int index = q->producer_counter & (q->count - 1);
  163. if ((u16) (q->producer_counter - q->consumer_counter) == q->count)
  164. return NULL;
  165. return mlxsw_pci_queue_elem_info_get(q, index);
  166. }
  167. static struct mlxsw_pci_queue_elem_info *
  168. mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
  169. {
  170. int index = q->consumer_counter & (q->count - 1);
  171. return mlxsw_pci_queue_elem_info_get(q, index);
  172. }
  173. static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
  174. {
  175. return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
  176. }
  177. static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
  178. {
  179. return owner_bit != !!(q->consumer_counter & q->count);
  180. }
  181. static char *
  182. mlxsw_pci_queue_sw_elem_get(struct mlxsw_pci_queue *q,
  183. u32 (*get_elem_owner_func)(const char *))
  184. {
  185. struct mlxsw_pci_queue_elem_info *elem_info;
  186. char *elem;
  187. bool owner_bit;
  188. elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
  189. elem = elem_info->elem;
  190. owner_bit = get_elem_owner_func(elem);
  191. if (mlxsw_pci_elem_hw_owned(q, owner_bit))
  192. return NULL;
  193. q->consumer_counter++;
  194. rmb(); /* make sure we read owned bit before the rest of elem */
  195. return elem;
  196. }
  197. static struct mlxsw_pci_queue_type_group *
  198. mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
  199. enum mlxsw_pci_queue_type q_type)
  200. {
  201. return &mlxsw_pci->queues[q_type];
  202. }
  203. static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci,
  204. enum mlxsw_pci_queue_type q_type)
  205. {
  206. struct mlxsw_pci_queue_type_group *queue_group;
  207. queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type);
  208. return queue_group->count;
  209. }
  210. static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci)
  211. {
  212. return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ);
  213. }
  214. static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci)
  215. {
  216. return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ);
  217. }
  218. static struct mlxsw_pci_queue *
  219. __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci,
  220. enum mlxsw_pci_queue_type q_type, u8 q_num)
  221. {
  222. return &mlxsw_pci->queues[q_type].q[q_num];
  223. }
  224. static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
  225. u8 q_num)
  226. {
  227. return __mlxsw_pci_queue_get(mlxsw_pci,
  228. MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
  229. }
  230. static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci,
  231. u8 q_num)
  232. {
  233. return __mlxsw_pci_queue_get(mlxsw_pci,
  234. MLXSW_PCI_QUEUE_TYPE_RDQ, q_num);
  235. }
  236. static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
  237. u8 q_num)
  238. {
  239. return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
  240. }
  241. static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci,
  242. u8 q_num)
  243. {
  244. return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num);
  245. }
  246. static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
  247. struct mlxsw_pci_queue *q,
  248. u16 val)
  249. {
  250. mlxsw_pci_write32(mlxsw_pci,
  251. DOORBELL(mlxsw_pci->doorbell_offset,
  252. mlxsw_pci_doorbell_type_offset[q->type],
  253. q->num), val);
  254. }
  255. static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
  256. struct mlxsw_pci_queue *q,
  257. u16 val)
  258. {
  259. mlxsw_pci_write32(mlxsw_pci,
  260. DOORBELL(mlxsw_pci->doorbell_offset,
  261. mlxsw_pci_doorbell_arm_type_offset[q->type],
  262. q->num), val);
  263. }
  264. static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
  265. struct mlxsw_pci_queue *q)
  266. {
  267. wmb(); /* ensure all writes are done before we ring a bell */
  268. __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
  269. }
  270. static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
  271. struct mlxsw_pci_queue *q)
  272. {
  273. wmb(); /* ensure all writes are done before we ring a bell */
  274. __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q,
  275. q->consumer_counter + q->count);
  276. }
  277. static void
  278. mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
  279. struct mlxsw_pci_queue *q)
  280. {
  281. wmb(); /* ensure all writes are done before we ring a bell */
  282. __mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter);
  283. }
  284. static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
  285. int page_index)
  286. {
  287. return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
  288. }
  289. static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  290. struct mlxsw_pci_queue *q)
  291. {
  292. int i;
  293. int err;
  294. q->producer_counter = 0;
  295. q->consumer_counter = 0;
  296. /* Set CQ of same number of this SDQ. */
  297. mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num);
  298. mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 3);
  299. mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
  300. for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
  301. dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
  302. mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
  303. }
  304. err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
  305. if (err)
  306. return err;
  307. mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
  308. return 0;
  309. }
  310. static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
  311. struct mlxsw_pci_queue *q)
  312. {
  313. mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
  314. }
  315. static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe,
  316. int index, char *frag_data, size_t frag_len,
  317. int direction)
  318. {
  319. struct pci_dev *pdev = mlxsw_pci->pdev;
  320. dma_addr_t mapaddr;
  321. mapaddr = pci_map_single(pdev, frag_data, frag_len, direction);
  322. if (unlikely(pci_dma_mapping_error(pdev, mapaddr))) {
  323. dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n");
  324. return -EIO;
  325. }
  326. mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
  327. mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
  328. return 0;
  329. }
  330. static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe,
  331. int index, int direction)
  332. {
  333. struct pci_dev *pdev = mlxsw_pci->pdev;
  334. size_t frag_len = mlxsw_pci_wqe_byte_count_get(wqe, index);
  335. dma_addr_t mapaddr = mlxsw_pci_wqe_address_get(wqe, index);
  336. if (!frag_len)
  337. return;
  338. pci_unmap_single(pdev, mapaddr, frag_len, direction);
  339. }
  340. static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci,
  341. struct mlxsw_pci_queue_elem_info *elem_info)
  342. {
  343. size_t buf_len = MLXSW_PORT_MAX_MTU;
  344. char *wqe = elem_info->elem;
  345. struct sk_buff *skb;
  346. int err;
  347. elem_info->u.rdq.skb = NULL;
  348. skb = netdev_alloc_skb_ip_align(NULL, buf_len);
  349. if (!skb)
  350. return -ENOMEM;
  351. /* Assume that wqe was previously zeroed. */
  352. err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
  353. buf_len, DMA_FROM_DEVICE);
  354. if (err)
  355. goto err_frag_map;
  356. elem_info->u.rdq.skb = skb;
  357. return 0;
  358. err_frag_map:
  359. dev_kfree_skb_any(skb);
  360. return err;
  361. }
  362. static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci,
  363. struct mlxsw_pci_queue_elem_info *elem_info)
  364. {
  365. struct sk_buff *skb;
  366. char *wqe;
  367. skb = elem_info->u.rdq.skb;
  368. wqe = elem_info->elem;
  369. mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
  370. dev_kfree_skb_any(skb);
  371. }
  372. static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  373. struct mlxsw_pci_queue *q)
  374. {
  375. struct mlxsw_pci_queue_elem_info *elem_info;
  376. u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci);
  377. int i;
  378. int err;
  379. q->producer_counter = 0;
  380. q->consumer_counter = 0;
  381. /* Set CQ of same number of this RDQ with base
  382. * above SDQ count as the lower ones are assigned to SDQs.
  383. */
  384. mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num);
  385. mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
  386. for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
  387. dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
  388. mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
  389. }
  390. err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
  391. if (err)
  392. return err;
  393. mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
  394. for (i = 0; i < q->count; i++) {
  395. elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
  396. BUG_ON(!elem_info);
  397. err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
  398. if (err)
  399. goto rollback;
  400. /* Everything is set up, ring doorbell to pass elem to HW */
  401. q->producer_counter++;
  402. mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
  403. }
  404. return 0;
  405. rollback:
  406. for (i--; i >= 0; i--) {
  407. elem_info = mlxsw_pci_queue_elem_info_get(q, i);
  408. mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
  409. }
  410. mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
  411. return err;
  412. }
  413. static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
  414. struct mlxsw_pci_queue *q)
  415. {
  416. struct mlxsw_pci_queue_elem_info *elem_info;
  417. int i;
  418. mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
  419. for (i = 0; i < q->count; i++) {
  420. elem_info = mlxsw_pci_queue_elem_info_get(q, i);
  421. mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
  422. }
  423. }
  424. static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  425. struct mlxsw_pci_queue *q)
  426. {
  427. int i;
  428. int err;
  429. q->consumer_counter = 0;
  430. for (i = 0; i < q->count; i++) {
  431. char *elem = mlxsw_pci_queue_elem_get(q, i);
  432. mlxsw_pci_cqe_owner_set(elem, 1);
  433. }
  434. mlxsw_cmd_mbox_sw2hw_cq_cv_set(mbox, 0); /* CQE ver 0 */
  435. mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM);
  436. mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0);
  437. mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count));
  438. for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
  439. dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
  440. mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
  441. }
  442. err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
  443. if (err)
  444. return err;
  445. mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
  446. mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
  447. return 0;
  448. }
  449. static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
  450. struct mlxsw_pci_queue *q)
  451. {
  452. mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
  453. }
  454. static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
  455. struct mlxsw_pci_queue *q,
  456. u16 consumer_counter_limit,
  457. char *cqe)
  458. {
  459. struct pci_dev *pdev = mlxsw_pci->pdev;
  460. struct mlxsw_pci_queue_elem_info *elem_info;
  461. char *wqe;
  462. struct sk_buff *skb;
  463. int i;
  464. spin_lock(&q->lock);
  465. elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
  466. skb = elem_info->u.sdq.skb;
  467. wqe = elem_info->elem;
  468. for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
  469. mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
  470. dev_kfree_skb_any(skb);
  471. elem_info->u.sdq.skb = NULL;
  472. if (q->consumer_counter++ != consumer_counter_limit)
  473. dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n");
  474. spin_unlock(&q->lock);
  475. }
  476. static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
  477. struct mlxsw_pci_queue *q,
  478. u16 consumer_counter_limit,
  479. char *cqe)
  480. {
  481. struct pci_dev *pdev = mlxsw_pci->pdev;
  482. struct mlxsw_pci_queue_elem_info *elem_info;
  483. char *wqe;
  484. struct sk_buff *skb;
  485. struct mlxsw_rx_info rx_info;
  486. u16 byte_count;
  487. int err;
  488. elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
  489. skb = elem_info->u.sdq.skb;
  490. if (!skb)
  491. return;
  492. wqe = elem_info->elem;
  493. mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
  494. if (q->consumer_counter++ != consumer_counter_limit)
  495. dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
  496. if (mlxsw_pci_cqe_lag_get(cqe)) {
  497. rx_info.is_lag = true;
  498. rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe);
  499. rx_info.lag_port_index = mlxsw_pci_cqe_lag_port_index_get(cqe);
  500. } else {
  501. rx_info.is_lag = false;
  502. rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
  503. }
  504. rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
  505. byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
  506. if (mlxsw_pci_cqe_crc_get(cqe))
  507. byte_count -= ETH_FCS_LEN;
  508. skb_put(skb, byte_count);
  509. mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
  510. memset(wqe, 0, q->elem_size);
  511. err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
  512. if (err)
  513. dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n");
  514. /* Everything is set up, ring doorbell to pass elem to HW */
  515. q->producer_counter++;
  516. mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
  517. return;
  518. }
  519. static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
  520. {
  521. return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_cqe_owner_get);
  522. }
  523. static void mlxsw_pci_cq_tasklet(unsigned long data)
  524. {
  525. struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
  526. struct mlxsw_pci *mlxsw_pci = q->pci;
  527. char *cqe;
  528. int items = 0;
  529. int credits = q->count >> 1;
  530. while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
  531. u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
  532. u8 sendq = mlxsw_pci_cqe_sr_get(cqe);
  533. u8 dqn = mlxsw_pci_cqe_dqn_get(cqe);
  534. if (sendq) {
  535. struct mlxsw_pci_queue *sdq;
  536. sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn);
  537. mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
  538. wqe_counter, cqe);
  539. q->u.cq.comp_sdq_count++;
  540. } else {
  541. struct mlxsw_pci_queue *rdq;
  542. rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn);
  543. mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
  544. wqe_counter, cqe);
  545. q->u.cq.comp_rdq_count++;
  546. }
  547. if (++items == credits)
  548. break;
  549. }
  550. if (items) {
  551. mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
  552. mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
  553. }
  554. }
  555. static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  556. struct mlxsw_pci_queue *q)
  557. {
  558. int i;
  559. int err;
  560. q->consumer_counter = 0;
  561. for (i = 0; i < q->count; i++) {
  562. char *elem = mlxsw_pci_queue_elem_get(q, i);
  563. mlxsw_pci_eqe_owner_set(elem, 1);
  564. }
  565. mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
  566. mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
  567. mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
  568. for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
  569. dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
  570. mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
  571. }
  572. err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
  573. if (err)
  574. return err;
  575. mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
  576. mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
  577. return 0;
  578. }
  579. static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
  580. struct mlxsw_pci_queue *q)
  581. {
  582. mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
  583. }
  584. static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe)
  585. {
  586. mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe);
  587. mlxsw_pci->cmd.comp.out_param =
  588. ((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 |
  589. mlxsw_pci_eqe_cmd_out_param_l_get(eqe);
  590. mlxsw_pci->cmd.wait_done = true;
  591. wake_up(&mlxsw_pci->cmd.wait);
  592. }
  593. static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
  594. {
  595. return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_eqe_owner_get);
  596. }
  597. static void mlxsw_pci_eq_tasklet(unsigned long data)
  598. {
  599. struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
  600. struct mlxsw_pci *mlxsw_pci = q->pci;
  601. u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci);
  602. unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
  603. char *eqe;
  604. u8 cqn;
  605. bool cq_handle = false;
  606. int items = 0;
  607. int credits = q->count >> 1;
  608. memset(&active_cqns, 0, sizeof(active_cqns));
  609. while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
  610. u8 event_type = mlxsw_pci_eqe_event_type_get(eqe);
  611. switch (event_type) {
  612. case MLXSW_PCI_EQE_EVENT_TYPE_CMD:
  613. mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe);
  614. q->u.eq.ev_cmd_count++;
  615. break;
  616. case MLXSW_PCI_EQE_EVENT_TYPE_COMP:
  617. cqn = mlxsw_pci_eqe_cqn_get(eqe);
  618. set_bit(cqn, active_cqns);
  619. cq_handle = true;
  620. q->u.eq.ev_comp_count++;
  621. break;
  622. default:
  623. q->u.eq.ev_other_count++;
  624. }
  625. if (++items == credits)
  626. break;
  627. }
  628. if (items) {
  629. mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
  630. mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
  631. }
  632. if (!cq_handle)
  633. return;
  634. for_each_set_bit(cqn, active_cqns, cq_count) {
  635. q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
  636. mlxsw_pci_queue_tasklet_schedule(q);
  637. }
  638. }
  639. struct mlxsw_pci_queue_ops {
  640. const char *name;
  641. enum mlxsw_pci_queue_type type;
  642. int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
  643. struct mlxsw_pci_queue *q);
  644. void (*fini)(struct mlxsw_pci *mlxsw_pci,
  645. struct mlxsw_pci_queue *q);
  646. void (*tasklet)(unsigned long data);
  647. u16 elem_count;
  648. u8 elem_size;
  649. };
  650. static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
  651. .type = MLXSW_PCI_QUEUE_TYPE_SDQ,
  652. .init = mlxsw_pci_sdq_init,
  653. .fini = mlxsw_pci_sdq_fini,
  654. .elem_count = MLXSW_PCI_WQE_COUNT,
  655. .elem_size = MLXSW_PCI_WQE_SIZE,
  656. };
  657. static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
  658. .type = MLXSW_PCI_QUEUE_TYPE_RDQ,
  659. .init = mlxsw_pci_rdq_init,
  660. .fini = mlxsw_pci_rdq_fini,
  661. .elem_count = MLXSW_PCI_WQE_COUNT,
  662. .elem_size = MLXSW_PCI_WQE_SIZE
  663. };
  664. static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
  665. .type = MLXSW_PCI_QUEUE_TYPE_CQ,
  666. .init = mlxsw_pci_cq_init,
  667. .fini = mlxsw_pci_cq_fini,
  668. .tasklet = mlxsw_pci_cq_tasklet,
  669. .elem_count = MLXSW_PCI_CQE_COUNT,
  670. .elem_size = MLXSW_PCI_CQE_SIZE
  671. };
  672. static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
  673. .type = MLXSW_PCI_QUEUE_TYPE_EQ,
  674. .init = mlxsw_pci_eq_init,
  675. .fini = mlxsw_pci_eq_fini,
  676. .tasklet = mlxsw_pci_eq_tasklet,
  677. .elem_count = MLXSW_PCI_EQE_COUNT,
  678. .elem_size = MLXSW_PCI_EQE_SIZE
  679. };
  680. static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  681. const struct mlxsw_pci_queue_ops *q_ops,
  682. struct mlxsw_pci_queue *q, u8 q_num)
  683. {
  684. struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
  685. int i;
  686. int err;
  687. spin_lock_init(&q->lock);
  688. q->num = q_num;
  689. q->count = q_ops->elem_count;
  690. q->elem_size = q_ops->elem_size;
  691. q->type = q_ops->type;
  692. q->pci = mlxsw_pci;
  693. if (q_ops->tasklet)
  694. tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q);
  695. mem_item->size = MLXSW_PCI_AQ_SIZE;
  696. mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
  697. mem_item->size,
  698. &mem_item->mapaddr);
  699. if (!mem_item->buf)
  700. return -ENOMEM;
  701. memset(mem_item->buf, 0, mem_item->size);
  702. q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
  703. if (!q->elem_info) {
  704. err = -ENOMEM;
  705. goto err_elem_info_alloc;
  706. }
  707. /* Initialize dma mapped elements info elem_info for
  708. * future easy access.
  709. */
  710. for (i = 0; i < q->count; i++) {
  711. struct mlxsw_pci_queue_elem_info *elem_info;
  712. elem_info = mlxsw_pci_queue_elem_info_get(q, i);
  713. elem_info->elem =
  714. __mlxsw_pci_queue_elem_get(q, q_ops->elem_size, i);
  715. }
  716. mlxsw_cmd_mbox_zero(mbox);
  717. err = q_ops->init(mlxsw_pci, mbox, q);
  718. if (err)
  719. goto err_q_ops_init;
  720. return 0;
  721. err_q_ops_init:
  722. kfree(q->elem_info);
  723. err_elem_info_alloc:
  724. pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
  725. mem_item->buf, mem_item->mapaddr);
  726. return err;
  727. }
  728. static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
  729. const struct mlxsw_pci_queue_ops *q_ops,
  730. struct mlxsw_pci_queue *q)
  731. {
  732. struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
  733. q_ops->fini(mlxsw_pci, q);
  734. kfree(q->elem_info);
  735. pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
  736. mem_item->buf, mem_item->mapaddr);
  737. }
  738. static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  739. const struct mlxsw_pci_queue_ops *q_ops,
  740. u8 num_qs)
  741. {
  742. struct mlxsw_pci_queue_type_group *queue_group;
  743. int i;
  744. int err;
  745. queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
  746. queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
  747. if (!queue_group->q)
  748. return -ENOMEM;
  749. for (i = 0; i < num_qs; i++) {
  750. err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
  751. &queue_group->q[i], i);
  752. if (err)
  753. goto err_queue_init;
  754. }
  755. queue_group->count = num_qs;
  756. return 0;
  757. err_queue_init:
  758. for (i--; i >= 0; i--)
  759. mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
  760. kfree(queue_group->q);
  761. return err;
  762. }
  763. static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
  764. const struct mlxsw_pci_queue_ops *q_ops)
  765. {
  766. struct mlxsw_pci_queue_type_group *queue_group;
  767. int i;
  768. queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
  769. for (i = 0; i < queue_group->count; i++)
  770. mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
  771. kfree(queue_group->q);
  772. }
  773. static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
  774. {
  775. struct pci_dev *pdev = mlxsw_pci->pdev;
  776. u8 num_sdqs;
  777. u8 sdq_log2sz;
  778. u8 num_rdqs;
  779. u8 rdq_log2sz;
  780. u8 num_cqs;
  781. u8 cq_log2sz;
  782. u8 num_eqs;
  783. u8 eq_log2sz;
  784. int err;
  785. mlxsw_cmd_mbox_zero(mbox);
  786. err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
  787. if (err)
  788. return err;
  789. num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
  790. sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
  791. num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
  792. rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
  793. num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
  794. cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
  795. num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
  796. eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
  797. if (num_sdqs + num_rdqs > num_cqs ||
  798. num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) {
  799. dev_err(&pdev->dev, "Unsupported number of queues\n");
  800. return -EINVAL;
  801. }
  802. if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
  803. (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
  804. (1 << cq_log2sz != MLXSW_PCI_CQE_COUNT) ||
  805. (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
  806. dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
  807. return -EINVAL;
  808. }
  809. err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
  810. num_eqs);
  811. if (err) {
  812. dev_err(&pdev->dev, "Failed to initialize event queues\n");
  813. return err;
  814. }
  815. err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
  816. num_cqs);
  817. if (err) {
  818. dev_err(&pdev->dev, "Failed to initialize completion queues\n");
  819. goto err_cqs_init;
  820. }
  821. err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
  822. num_sdqs);
  823. if (err) {
  824. dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
  825. goto err_sdqs_init;
  826. }
  827. err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
  828. num_rdqs);
  829. if (err) {
  830. dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
  831. goto err_rdqs_init;
  832. }
  833. /* We have to poll in command interface until queues are initialized */
  834. mlxsw_pci->cmd.nopoll = true;
  835. return 0;
  836. err_rdqs_init:
  837. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
  838. err_sdqs_init:
  839. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
  840. err_cqs_init:
  841. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
  842. return err;
  843. }
  844. static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
  845. {
  846. mlxsw_pci->cmd.nopoll = false;
  847. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
  848. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
  849. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
  850. mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
  851. }
  852. static void
  853. mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
  854. char *mbox, int index,
  855. const struct mlxsw_swid_config *swid)
  856. {
  857. u8 mask = 0;
  858. if (swid->used_type) {
  859. mlxsw_cmd_mbox_config_profile_swid_config_type_set(
  860. mbox, index, swid->type);
  861. mask |= 1;
  862. }
  863. if (swid->used_properties) {
  864. mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
  865. mbox, index, swid->properties);
  866. mask |= 2;
  867. }
  868. mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
  869. }
  870. static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
  871. struct mlxsw_res *res,
  872. u8 query_enabled)
  873. {
  874. int index, i;
  875. u64 data;
  876. u16 id;
  877. int err;
  878. /* Not all the versions support resources query */
  879. if (!query_enabled)
  880. return 0;
  881. mlxsw_cmd_mbox_zero(mbox);
  882. for (index = 0; index < MLXSW_CMD_QUERY_RESOURCES_MAX_QUERIES;
  883. index++) {
  884. err = mlxsw_cmd_query_resources(mlxsw_pci->core, mbox, index);
  885. if (err)
  886. return err;
  887. for (i = 0; i < MLXSW_CMD_QUERY_RESOURCES_PER_QUERY; i++) {
  888. id = mlxsw_cmd_mbox_query_resource_id_get(mbox, i);
  889. data = mlxsw_cmd_mbox_query_resource_data_get(mbox, i);
  890. if (id == MLXSW_CMD_QUERY_RESOURCES_TABLE_END_ID)
  891. return 0;
  892. mlxsw_res_parse(res, id, data);
  893. }
  894. }
  895. /* If after MLXSW_RESOURCES_QUERY_MAX_QUERIES we still didn't get
  896. * MLXSW_RESOURCES_TABLE_END_ID, something went bad in the FW.
  897. */
  898. return -EIO;
  899. }
  900. static int
  901. mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *profile,
  902. struct mlxsw_res *res)
  903. {
  904. u32 single_size, double_size, linear_size;
  905. if (!MLXSW_RES_VALID(res, KVD_SINGLE_MIN_SIZE) ||
  906. !MLXSW_RES_VALID(res, KVD_DOUBLE_MIN_SIZE) ||
  907. !profile->used_kvd_split_data)
  908. return -EIO;
  909. linear_size = profile->kvd_linear_size;
  910. /* The hash part is what left of the kvd without the
  911. * linear part. It is split to the single size and
  912. * double size by the parts ratio from the profile.
  913. * Both sizes must be a multiplications of the
  914. * granularity from the profile.
  915. */
  916. double_size = MLXSW_RES_GET(res, KVD_SIZE) - linear_size;
  917. double_size *= profile->kvd_hash_double_parts;
  918. double_size /= profile->kvd_hash_double_parts +
  919. profile->kvd_hash_single_parts;
  920. double_size /= profile->kvd_hash_granularity;
  921. double_size *= profile->kvd_hash_granularity;
  922. single_size = MLXSW_RES_GET(res, KVD_SIZE) - double_size -
  923. linear_size;
  924. /* Check results are legal. */
  925. if (single_size < MLXSW_RES_GET(res, KVD_SINGLE_MIN_SIZE) ||
  926. double_size < MLXSW_RES_GET(res, KVD_DOUBLE_MIN_SIZE) ||
  927. MLXSW_RES_GET(res, KVD_SIZE) < linear_size)
  928. return -EIO;
  929. MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size);
  930. MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size);
  931. MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size);
  932. return 0;
  933. }
  934. static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
  935. const struct mlxsw_config_profile *profile,
  936. struct mlxsw_res *res)
  937. {
  938. int i;
  939. int err;
  940. mlxsw_cmd_mbox_zero(mbox);
  941. if (profile->used_max_vepa_channels) {
  942. mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
  943. mbox, 1);
  944. mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
  945. mbox, profile->max_vepa_channels);
  946. }
  947. if (profile->used_max_mid) {
  948. mlxsw_cmd_mbox_config_profile_set_max_mid_set(
  949. mbox, 1);
  950. mlxsw_cmd_mbox_config_profile_max_mid_set(
  951. mbox, profile->max_mid);
  952. }
  953. if (profile->used_max_pgt) {
  954. mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
  955. mbox, 1);
  956. mlxsw_cmd_mbox_config_profile_max_pgt_set(
  957. mbox, profile->max_pgt);
  958. }
  959. if (profile->used_max_system_port) {
  960. mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
  961. mbox, 1);
  962. mlxsw_cmd_mbox_config_profile_max_system_port_set(
  963. mbox, profile->max_system_port);
  964. }
  965. if (profile->used_max_vlan_groups) {
  966. mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
  967. mbox, 1);
  968. mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
  969. mbox, profile->max_vlan_groups);
  970. }
  971. if (profile->used_max_regions) {
  972. mlxsw_cmd_mbox_config_profile_set_max_regions_set(
  973. mbox, 1);
  974. mlxsw_cmd_mbox_config_profile_max_regions_set(
  975. mbox, profile->max_regions);
  976. }
  977. if (profile->used_flood_tables) {
  978. mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
  979. mbox, 1);
  980. mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
  981. mbox, profile->max_flood_tables);
  982. mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
  983. mbox, profile->max_vid_flood_tables);
  984. mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
  985. mbox, profile->max_fid_offset_flood_tables);
  986. mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
  987. mbox, profile->fid_offset_flood_table_size);
  988. mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
  989. mbox, profile->max_fid_flood_tables);
  990. mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
  991. mbox, profile->fid_flood_table_size);
  992. }
  993. if (profile->used_flood_mode) {
  994. mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
  995. mbox, 1);
  996. mlxsw_cmd_mbox_config_profile_flood_mode_set(
  997. mbox, profile->flood_mode);
  998. }
  999. if (profile->used_max_ib_mc) {
  1000. mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
  1001. mbox, 1);
  1002. mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
  1003. mbox, profile->max_ib_mc);
  1004. }
  1005. if (profile->used_max_pkey) {
  1006. mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
  1007. mbox, 1);
  1008. mlxsw_cmd_mbox_config_profile_max_pkey_set(
  1009. mbox, profile->max_pkey);
  1010. }
  1011. if (profile->used_ar_sec) {
  1012. mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
  1013. mbox, 1);
  1014. mlxsw_cmd_mbox_config_profile_ar_sec_set(
  1015. mbox, profile->ar_sec);
  1016. }
  1017. if (profile->used_adaptive_routing_group_cap) {
  1018. mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
  1019. mbox, 1);
  1020. mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
  1021. mbox, profile->adaptive_routing_group_cap);
  1022. }
  1023. if (MLXSW_RES_VALID(res, KVD_SIZE)) {
  1024. err = mlxsw_pci_profile_get_kvd_sizes(profile, res);
  1025. if (err)
  1026. return err;
  1027. mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1);
  1028. mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox,
  1029. MLXSW_RES_GET(res, KVD_LINEAR_SIZE));
  1030. mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox,
  1031. 1);
  1032. mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox,
  1033. MLXSW_RES_GET(res, KVD_SINGLE_SIZE));
  1034. mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
  1035. mbox, 1);
  1036. mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox,
  1037. MLXSW_RES_GET(res, KVD_DOUBLE_SIZE));
  1038. }
  1039. for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
  1040. mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
  1041. &profile->swid_config[i]);
  1042. return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
  1043. }
  1044. static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
  1045. {
  1046. struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
  1047. int err;
  1048. mlxsw_cmd_mbox_zero(mbox);
  1049. err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
  1050. if (err)
  1051. return err;
  1052. mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
  1053. mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
  1054. return 0;
  1055. }
  1056. static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
  1057. u16 num_pages)
  1058. {
  1059. struct mlxsw_pci_mem_item *mem_item;
  1060. int nent = 0;
  1061. int i;
  1062. int err;
  1063. mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
  1064. GFP_KERNEL);
  1065. if (!mlxsw_pci->fw_area.items)
  1066. return -ENOMEM;
  1067. mlxsw_pci->fw_area.count = num_pages;
  1068. mlxsw_cmd_mbox_zero(mbox);
  1069. for (i = 0; i < num_pages; i++) {
  1070. mem_item = &mlxsw_pci->fw_area.items[i];
  1071. mem_item->size = MLXSW_PCI_PAGE_SIZE;
  1072. mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
  1073. mem_item->size,
  1074. &mem_item->mapaddr);
  1075. if (!mem_item->buf) {
  1076. err = -ENOMEM;
  1077. goto err_alloc;
  1078. }
  1079. mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
  1080. mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
  1081. if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
  1082. err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
  1083. if (err)
  1084. goto err_cmd_map_fa;
  1085. nent = 0;
  1086. mlxsw_cmd_mbox_zero(mbox);
  1087. }
  1088. }
  1089. if (nent) {
  1090. err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
  1091. if (err)
  1092. goto err_cmd_map_fa;
  1093. }
  1094. return 0;
  1095. err_cmd_map_fa:
  1096. err_alloc:
  1097. for (i--; i >= 0; i--) {
  1098. mem_item = &mlxsw_pci->fw_area.items[i];
  1099. pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
  1100. mem_item->buf, mem_item->mapaddr);
  1101. }
  1102. kfree(mlxsw_pci->fw_area.items);
  1103. return err;
  1104. }
  1105. static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
  1106. {
  1107. struct mlxsw_pci_mem_item *mem_item;
  1108. int i;
  1109. mlxsw_cmd_unmap_fa(mlxsw_pci->core);
  1110. for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
  1111. mem_item = &mlxsw_pci->fw_area.items[i];
  1112. pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
  1113. mem_item->buf, mem_item->mapaddr);
  1114. }
  1115. kfree(mlxsw_pci->fw_area.items);
  1116. }
  1117. static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
  1118. {
  1119. struct mlxsw_pci *mlxsw_pci = dev_id;
  1120. struct mlxsw_pci_queue *q;
  1121. int i;
  1122. for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) {
  1123. q = mlxsw_pci_eq_get(mlxsw_pci, i);
  1124. mlxsw_pci_queue_tasklet_schedule(q);
  1125. }
  1126. return IRQ_HANDLED;
  1127. }
  1128. static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
  1129. struct mlxsw_pci_mem_item *mbox)
  1130. {
  1131. struct pci_dev *pdev = mlxsw_pci->pdev;
  1132. int err = 0;
  1133. mbox->size = MLXSW_CMD_MBOX_SIZE;
  1134. mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE,
  1135. &mbox->mapaddr);
  1136. if (!mbox->buf) {
  1137. dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
  1138. err = -ENOMEM;
  1139. }
  1140. return err;
  1141. }
  1142. static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
  1143. struct mlxsw_pci_mem_item *mbox)
  1144. {
  1145. struct pci_dev *pdev = mlxsw_pci->pdev;
  1146. pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
  1147. mbox->mapaddr);
  1148. }
  1149. static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
  1150. const struct mlxsw_config_profile *profile,
  1151. struct mlxsw_res *res)
  1152. {
  1153. struct mlxsw_pci *mlxsw_pci = bus_priv;
  1154. struct pci_dev *pdev = mlxsw_pci->pdev;
  1155. char *mbox;
  1156. u16 num_pages;
  1157. int err;
  1158. mutex_init(&mlxsw_pci->cmd.lock);
  1159. init_waitqueue_head(&mlxsw_pci->cmd.wait);
  1160. mlxsw_pci->core = mlxsw_core;
  1161. mbox = mlxsw_cmd_mbox_alloc();
  1162. if (!mbox)
  1163. return -ENOMEM;
  1164. err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
  1165. if (err)
  1166. goto mbox_put;
  1167. err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
  1168. if (err)
  1169. goto err_out_mbox_alloc;
  1170. err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
  1171. if (err)
  1172. goto err_query_fw;
  1173. mlxsw_pci->bus_info.fw_rev.major =
  1174. mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
  1175. mlxsw_pci->bus_info.fw_rev.minor =
  1176. mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
  1177. mlxsw_pci->bus_info.fw_rev.subminor =
  1178. mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
  1179. if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
  1180. dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
  1181. err = -EINVAL;
  1182. goto err_iface_rev;
  1183. }
  1184. if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
  1185. dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
  1186. err = -EINVAL;
  1187. goto err_doorbell_page_bar;
  1188. }
  1189. mlxsw_pci->doorbell_offset =
  1190. mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
  1191. num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
  1192. err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
  1193. if (err)
  1194. goto err_fw_area_init;
  1195. err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
  1196. if (err)
  1197. goto err_boardinfo;
  1198. err = mlxsw_pci_resources_query(mlxsw_pci, mbox, res,
  1199. profile->resource_query_enable);
  1200. if (err)
  1201. goto err_query_resources;
  1202. err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res);
  1203. if (err)
  1204. goto err_config_profile;
  1205. err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
  1206. if (err)
  1207. goto err_aqs_init;
  1208. err = request_irq(pci_irq_vector(pdev, 0),
  1209. mlxsw_pci_eq_irq_handler, 0,
  1210. mlxsw_pci->bus_info.device_kind, mlxsw_pci);
  1211. if (err) {
  1212. dev_err(&pdev->dev, "IRQ request failed\n");
  1213. goto err_request_eq_irq;
  1214. }
  1215. goto mbox_put;
  1216. err_request_eq_irq:
  1217. mlxsw_pci_aqs_fini(mlxsw_pci);
  1218. err_aqs_init:
  1219. err_config_profile:
  1220. err_query_resources:
  1221. err_boardinfo:
  1222. mlxsw_pci_fw_area_fini(mlxsw_pci);
  1223. err_fw_area_init:
  1224. err_doorbell_page_bar:
  1225. err_iface_rev:
  1226. err_query_fw:
  1227. mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
  1228. err_out_mbox_alloc:
  1229. mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
  1230. mbox_put:
  1231. mlxsw_cmd_mbox_free(mbox);
  1232. return err;
  1233. }
  1234. static void mlxsw_pci_fini(void *bus_priv)
  1235. {
  1236. struct mlxsw_pci *mlxsw_pci = bus_priv;
  1237. free_irq(pci_irq_vector(mlxsw_pci->pdev, 0), mlxsw_pci);
  1238. mlxsw_pci_aqs_fini(mlxsw_pci);
  1239. mlxsw_pci_fw_area_fini(mlxsw_pci);
  1240. mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
  1241. mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
  1242. }
  1243. static struct mlxsw_pci_queue *
  1244. mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
  1245. const struct mlxsw_tx_info *tx_info)
  1246. {
  1247. u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci);
  1248. return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
  1249. }
  1250. static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
  1251. const struct mlxsw_tx_info *tx_info)
  1252. {
  1253. struct mlxsw_pci *mlxsw_pci = bus_priv;
  1254. struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
  1255. return !mlxsw_pci_queue_elem_info_producer_get(q);
  1256. }
  1257. static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
  1258. const struct mlxsw_tx_info *tx_info)
  1259. {
  1260. struct mlxsw_pci *mlxsw_pci = bus_priv;
  1261. struct mlxsw_pci_queue *q;
  1262. struct mlxsw_pci_queue_elem_info *elem_info;
  1263. char *wqe;
  1264. int i;
  1265. int err;
  1266. if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
  1267. err = skb_linearize(skb);
  1268. if (err)
  1269. return err;
  1270. }
  1271. q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
  1272. spin_lock_bh(&q->lock);
  1273. elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
  1274. if (!elem_info) {
  1275. /* queue is full */
  1276. err = -EAGAIN;
  1277. goto unlock;
  1278. }
  1279. elem_info->u.sdq.skb = skb;
  1280. wqe = elem_info->elem;
  1281. mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
  1282. mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad);
  1283. mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
  1284. err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
  1285. skb_headlen(skb), DMA_TO_DEVICE);
  1286. if (err)
  1287. goto unlock;
  1288. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1289. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1290. err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
  1291. skb_frag_address(frag),
  1292. skb_frag_size(frag),
  1293. DMA_TO_DEVICE);
  1294. if (err)
  1295. goto unmap_frags;
  1296. }
  1297. /* Set unused sq entries byte count to zero. */
  1298. for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
  1299. mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
  1300. /* Everything is set up, ring producer doorbell to get HW going */
  1301. q->producer_counter++;
  1302. mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
  1303. goto unlock;
  1304. unmap_frags:
  1305. for (; i >= 0; i--)
  1306. mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
  1307. unlock:
  1308. spin_unlock_bh(&q->lock);
  1309. return err;
  1310. }
  1311. static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
  1312. u32 in_mod, bool out_mbox_direct,
  1313. char *in_mbox, size_t in_mbox_size,
  1314. char *out_mbox, size_t out_mbox_size,
  1315. u8 *p_status)
  1316. {
  1317. struct mlxsw_pci *mlxsw_pci = bus_priv;
  1318. dma_addr_t in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
  1319. dma_addr_t out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
  1320. bool evreq = mlxsw_pci->cmd.nopoll;
  1321. unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
  1322. bool *p_wait_done = &mlxsw_pci->cmd.wait_done;
  1323. int err;
  1324. *p_status = MLXSW_CMD_STATUS_OK;
  1325. err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
  1326. if (err)
  1327. return err;
  1328. if (in_mbox)
  1329. memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
  1330. mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
  1331. mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
  1332. mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
  1333. mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
  1334. mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
  1335. mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
  1336. *p_wait_done = false;
  1337. wmb(); /* all needs to be written before we write control register */
  1338. mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
  1339. MLXSW_PCI_CIR_CTRL_GO_BIT |
  1340. (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) |
  1341. (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
  1342. opcode);
  1343. if (!evreq) {
  1344. unsigned long end;
  1345. end = jiffies + timeout;
  1346. do {
  1347. u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
  1348. if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
  1349. *p_wait_done = true;
  1350. *p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
  1351. break;
  1352. }
  1353. cond_resched();
  1354. } while (time_before(jiffies, end));
  1355. } else {
  1356. wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout);
  1357. *p_status = mlxsw_pci->cmd.comp.status;
  1358. }
  1359. err = 0;
  1360. if (*p_wait_done) {
  1361. if (*p_status)
  1362. err = -EIO;
  1363. } else {
  1364. err = -ETIMEDOUT;
  1365. }
  1366. if (!err && out_mbox && out_mbox_direct) {
  1367. /* Some commands don't use output param as address to mailbox
  1368. * but they store output directly into registers. In that case,
  1369. * copy registers into mbox buffer.
  1370. */
  1371. __be32 tmp;
  1372. if (!evreq) {
  1373. tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
  1374. CIR_OUT_PARAM_HI));
  1375. memcpy(out_mbox, &tmp, sizeof(tmp));
  1376. tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
  1377. CIR_OUT_PARAM_LO));
  1378. memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
  1379. }
  1380. } else if (!err && out_mbox) {
  1381. memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
  1382. }
  1383. mutex_unlock(&mlxsw_pci->cmd.lock);
  1384. return err;
  1385. }
  1386. static const struct mlxsw_bus mlxsw_pci_bus = {
  1387. .kind = "pci",
  1388. .init = mlxsw_pci_init,
  1389. .fini = mlxsw_pci_fini,
  1390. .skb_transmit_busy = mlxsw_pci_skb_transmit_busy,
  1391. .skb_transmit = mlxsw_pci_skb_transmit,
  1392. .cmd_exec = mlxsw_pci_cmd_exec,
  1393. .features = MLXSW_BUS_F_TXRX,
  1394. };
  1395. static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci,
  1396. const struct pci_device_id *id)
  1397. {
  1398. unsigned long end;
  1399. mlxsw_pci_write32(mlxsw_pci, SW_RESET, MLXSW_PCI_SW_RESET_RST_BIT);
  1400. if (id->device == PCI_DEVICE_ID_MELLANOX_SWITCHX2) {
  1401. msleep(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
  1402. return 0;
  1403. }
  1404. wmb(); /* reset needs to be written before we read control register */
  1405. end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
  1406. do {
  1407. u32 val = mlxsw_pci_read32(mlxsw_pci, FW_READY);
  1408. if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC)
  1409. break;
  1410. cond_resched();
  1411. } while (time_before(jiffies, end));
  1412. return 0;
  1413. }
  1414. static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1415. {
  1416. const char *driver_name = pdev->driver->name;
  1417. struct mlxsw_pci *mlxsw_pci;
  1418. int err;
  1419. mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
  1420. if (!mlxsw_pci)
  1421. return -ENOMEM;
  1422. err = pci_enable_device(pdev);
  1423. if (err) {
  1424. dev_err(&pdev->dev, "pci_enable_device failed\n");
  1425. goto err_pci_enable_device;
  1426. }
  1427. err = pci_request_regions(pdev, driver_name);
  1428. if (err) {
  1429. dev_err(&pdev->dev, "pci_request_regions failed\n");
  1430. goto err_pci_request_regions;
  1431. }
  1432. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1433. if (!err) {
  1434. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1435. if (err) {
  1436. dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
  1437. goto err_pci_set_dma_mask;
  1438. }
  1439. } else {
  1440. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1441. if (err) {
  1442. dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
  1443. goto err_pci_set_dma_mask;
  1444. }
  1445. }
  1446. if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
  1447. dev_err(&pdev->dev, "invalid PCI region size\n");
  1448. err = -EINVAL;
  1449. goto err_pci_resource_len_check;
  1450. }
  1451. mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
  1452. pci_resource_len(pdev, 0));
  1453. if (!mlxsw_pci->hw_addr) {
  1454. dev_err(&pdev->dev, "ioremap failed\n");
  1455. err = -EIO;
  1456. goto err_ioremap;
  1457. }
  1458. pci_set_master(pdev);
  1459. mlxsw_pci->pdev = pdev;
  1460. pci_set_drvdata(pdev, mlxsw_pci);
  1461. err = mlxsw_pci_sw_reset(mlxsw_pci, id);
  1462. if (err) {
  1463. dev_err(&pdev->dev, "Software reset failed\n");
  1464. goto err_sw_reset;
  1465. }
  1466. err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX);
  1467. if (err < 0) {
  1468. dev_err(&pdev->dev, "MSI-X init failed\n");
  1469. goto err_msix_init;
  1470. }
  1471. mlxsw_pci->bus_info.device_kind = driver_name;
  1472. mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
  1473. mlxsw_pci->bus_info.dev = &pdev->dev;
  1474. err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
  1475. &mlxsw_pci_bus, mlxsw_pci);
  1476. if (err) {
  1477. dev_err(&pdev->dev, "cannot register bus device\n");
  1478. goto err_bus_device_register;
  1479. }
  1480. return 0;
  1481. err_bus_device_register:
  1482. pci_free_irq_vectors(mlxsw_pci->pdev);
  1483. err_msix_init:
  1484. err_sw_reset:
  1485. iounmap(mlxsw_pci->hw_addr);
  1486. err_ioremap:
  1487. err_pci_resource_len_check:
  1488. err_pci_set_dma_mask:
  1489. pci_release_regions(pdev);
  1490. err_pci_request_regions:
  1491. pci_disable_device(pdev);
  1492. err_pci_enable_device:
  1493. kfree(mlxsw_pci);
  1494. return err;
  1495. }
  1496. static void mlxsw_pci_remove(struct pci_dev *pdev)
  1497. {
  1498. struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
  1499. mlxsw_core_bus_device_unregister(mlxsw_pci->core);
  1500. pci_free_irq_vectors(mlxsw_pci->pdev);
  1501. iounmap(mlxsw_pci->hw_addr);
  1502. pci_release_regions(mlxsw_pci->pdev);
  1503. pci_disable_device(mlxsw_pci->pdev);
  1504. kfree(mlxsw_pci);
  1505. }
  1506. int mlxsw_pci_driver_register(struct pci_driver *pci_driver)
  1507. {
  1508. pci_driver->probe = mlxsw_pci_probe;
  1509. pci_driver->remove = mlxsw_pci_remove;
  1510. return pci_register_driver(pci_driver);
  1511. }
  1512. EXPORT_SYMBOL(mlxsw_pci_driver_register);
  1513. void mlxsw_pci_driver_unregister(struct pci_driver *pci_driver)
  1514. {
  1515. pci_unregister_driver(pci_driver);
  1516. }
  1517. EXPORT_SYMBOL(mlxsw_pci_driver_unregister);
  1518. static int __init mlxsw_pci_module_init(void)
  1519. {
  1520. return 0;
  1521. }
  1522. static void __exit mlxsw_pci_module_exit(void)
  1523. {
  1524. }
  1525. module_init(mlxsw_pci_module_init);
  1526. module_exit(mlxsw_pci_module_exit);
  1527. MODULE_LICENSE("Dual BSD/GPL");
  1528. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  1529. MODULE_DESCRIPTION("Mellanox switch PCI interface driver");