qed_ll2.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <asm/byteorder.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/kernel.h>
  37. #include <linux/pci.h>
  38. #include <linux/slab.h>
  39. #include <linux/stddef.h>
  40. #include <linux/version.h>
  41. #include <linux/workqueue.h>
  42. #include <net/ipv6.h>
  43. #include <linux/bitops.h>
  44. #include <linux/delay.h>
  45. #include <linux/errno.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/io.h>
  48. #include <linux/list.h>
  49. #include <linux/mutex.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/string.h>
  52. #include <linux/qed/qed_ll2_if.h>
  53. #include "qed.h"
  54. #include "qed_cxt.h"
  55. #include "qed_dev_api.h"
  56. #include "qed_hsi.h"
  57. #include "qed_hw.h"
  58. #include "qed_int.h"
  59. #include "qed_ll2.h"
  60. #include "qed_mcp.h"
  61. #include "qed_ooo.h"
  62. #include "qed_reg_addr.h"
  63. #include "qed_sp.h"
  64. #include "qed_roce.h"
  65. #define QED_LL2_RX_REGISTERED(ll2) ((ll2)->rx_queue.b_cb_registred)
  66. #define QED_LL2_TX_REGISTERED(ll2) ((ll2)->tx_queue.b_cb_registred)
  67. #define QED_LL2_TX_SIZE (256)
  68. #define QED_LL2_RX_SIZE (4096)
  69. struct qed_cb_ll2_info {
  70. int rx_cnt;
  71. u32 rx_size;
  72. u8 handle;
  73. bool frags_mapped;
  74. /* Lock protecting LL2 buffer lists in sleepless context */
  75. spinlock_t lock;
  76. struct list_head list;
  77. const struct qed_ll2_cb_ops *cbs;
  78. void *cb_cookie;
  79. };
  80. struct qed_ll2_buffer {
  81. struct list_head list;
  82. void *data;
  83. dma_addr_t phys_addr;
  84. };
  85. static void qed_ll2b_complete_tx_packet(struct qed_hwfn *p_hwfn,
  86. u8 connection_handle,
  87. void *cookie,
  88. dma_addr_t first_frag_addr,
  89. bool b_last_fragment,
  90. bool b_last_packet)
  91. {
  92. struct qed_dev *cdev = p_hwfn->cdev;
  93. struct sk_buff *skb = cookie;
  94. /* All we need to do is release the mapping */
  95. dma_unmap_single(&p_hwfn->cdev->pdev->dev, first_frag_addr,
  96. skb_headlen(skb), DMA_TO_DEVICE);
  97. if (cdev->ll2->cbs && cdev->ll2->cbs->tx_cb)
  98. cdev->ll2->cbs->tx_cb(cdev->ll2->cb_cookie, skb,
  99. b_last_fragment);
  100. if (cdev->ll2->frags_mapped)
  101. /* Case where mapped frags were received, need to
  102. * free skb with nr_frags marked as 0
  103. */
  104. skb_shinfo(skb)->nr_frags = 0;
  105. dev_kfree_skb_any(skb);
  106. }
  107. static int qed_ll2_alloc_buffer(struct qed_dev *cdev,
  108. u8 **data, dma_addr_t *phys_addr)
  109. {
  110. *data = kmalloc(cdev->ll2->rx_size, GFP_ATOMIC);
  111. if (!(*data)) {
  112. DP_INFO(cdev, "Failed to allocate LL2 buffer data\n");
  113. return -ENOMEM;
  114. }
  115. *phys_addr = dma_map_single(&cdev->pdev->dev,
  116. ((*data) + NET_SKB_PAD),
  117. cdev->ll2->rx_size, DMA_FROM_DEVICE);
  118. if (dma_mapping_error(&cdev->pdev->dev, *phys_addr)) {
  119. DP_INFO(cdev, "Failed to map LL2 buffer data\n");
  120. kfree((*data));
  121. return -ENOMEM;
  122. }
  123. return 0;
  124. }
  125. static int qed_ll2_dealloc_buffer(struct qed_dev *cdev,
  126. struct qed_ll2_buffer *buffer)
  127. {
  128. spin_lock_bh(&cdev->ll2->lock);
  129. dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
  130. cdev->ll2->rx_size, DMA_FROM_DEVICE);
  131. kfree(buffer->data);
  132. list_del(&buffer->list);
  133. cdev->ll2->rx_cnt--;
  134. if (!cdev->ll2->rx_cnt)
  135. DP_INFO(cdev, "All LL2 entries were removed\n");
  136. spin_unlock_bh(&cdev->ll2->lock);
  137. return 0;
  138. }
  139. static void qed_ll2_kill_buffers(struct qed_dev *cdev)
  140. {
  141. struct qed_ll2_buffer *buffer, *tmp_buffer;
  142. list_for_each_entry_safe(buffer, tmp_buffer, &cdev->ll2->list, list)
  143. qed_ll2_dealloc_buffer(cdev, buffer);
  144. }
  145. static void qed_ll2b_complete_rx_packet(struct qed_hwfn *p_hwfn,
  146. u8 connection_handle,
  147. struct qed_ll2_rx_packet *p_pkt,
  148. struct core_rx_fast_path_cqe *p_cqe,
  149. bool b_last_packet)
  150. {
  151. u16 packet_length = le16_to_cpu(p_cqe->packet_length);
  152. struct qed_ll2_buffer *buffer = p_pkt->cookie;
  153. struct qed_dev *cdev = p_hwfn->cdev;
  154. u16 vlan = le16_to_cpu(p_cqe->vlan);
  155. u32 opaque_data_0, opaque_data_1;
  156. u8 pad = p_cqe->placement_offset;
  157. dma_addr_t new_phys_addr;
  158. struct sk_buff *skb;
  159. bool reuse = false;
  160. int rc = -EINVAL;
  161. u8 *new_data;
  162. opaque_data_0 = le32_to_cpu(p_cqe->opaque_data.data[0]);
  163. opaque_data_1 = le32_to_cpu(p_cqe->opaque_data.data[1]);
  164. DP_VERBOSE(p_hwfn,
  165. (NETIF_MSG_RX_STATUS | QED_MSG_STORAGE | NETIF_MSG_PKTDATA),
  166. "Got an LL2 Rx completion: [Buffer at phys 0x%llx, offset 0x%02x] Length 0x%04x Parse_flags 0x%04x vlan 0x%04x Opaque data [0x%08x:0x%08x]\n",
  167. (u64)p_pkt->rx_buf_addr, pad, packet_length,
  168. le16_to_cpu(p_cqe->parse_flags.flags), vlan,
  169. opaque_data_0, opaque_data_1);
  170. if ((cdev->dp_module & NETIF_MSG_PKTDATA) && buffer->data) {
  171. print_hex_dump(KERN_INFO, "",
  172. DUMP_PREFIX_OFFSET, 16, 1,
  173. buffer->data, packet_length, false);
  174. }
  175. /* Determine if data is valid */
  176. if (packet_length < ETH_HLEN)
  177. reuse = true;
  178. /* Allocate a replacement for buffer; Reuse upon failure */
  179. if (!reuse)
  180. rc = qed_ll2_alloc_buffer(p_hwfn->cdev, &new_data,
  181. &new_phys_addr);
  182. /* If need to reuse or there's no replacement buffer, repost this */
  183. if (rc)
  184. goto out_post;
  185. dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
  186. cdev->ll2->rx_size, DMA_FROM_DEVICE);
  187. skb = build_skb(buffer->data, 0);
  188. if (!skb) {
  189. rc = -ENOMEM;
  190. goto out_post;
  191. }
  192. pad += NET_SKB_PAD;
  193. skb_reserve(skb, pad);
  194. skb_put(skb, packet_length);
  195. skb_checksum_none_assert(skb);
  196. /* Get parital ethernet information instead of eth_type_trans(),
  197. * Since we don't have an associated net_device.
  198. */
  199. skb_reset_mac_header(skb);
  200. skb->protocol = eth_hdr(skb)->h_proto;
  201. /* Pass SKB onward */
  202. if (cdev->ll2->cbs && cdev->ll2->cbs->rx_cb) {
  203. if (vlan)
  204. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
  205. cdev->ll2->cbs->rx_cb(cdev->ll2->cb_cookie, skb,
  206. opaque_data_0, opaque_data_1);
  207. }
  208. /* Update Buffer information and update FW producer */
  209. buffer->data = new_data;
  210. buffer->phys_addr = new_phys_addr;
  211. out_post:
  212. rc = qed_ll2_post_rx_buffer(QED_LEADING_HWFN(cdev), cdev->ll2->handle,
  213. buffer->phys_addr, 0, buffer, 1);
  214. if (rc)
  215. qed_ll2_dealloc_buffer(cdev, buffer);
  216. }
  217. static struct qed_ll2_info *__qed_ll2_handle_sanity(struct qed_hwfn *p_hwfn,
  218. u8 connection_handle,
  219. bool b_lock,
  220. bool b_only_active)
  221. {
  222. struct qed_ll2_info *p_ll2_conn, *p_ret = NULL;
  223. if (connection_handle >= QED_MAX_NUM_OF_LL2_CONNECTIONS)
  224. return NULL;
  225. if (!p_hwfn->p_ll2_info)
  226. return NULL;
  227. p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
  228. if (b_only_active) {
  229. if (b_lock)
  230. mutex_lock(&p_ll2_conn->mutex);
  231. if (p_ll2_conn->b_active)
  232. p_ret = p_ll2_conn;
  233. if (b_lock)
  234. mutex_unlock(&p_ll2_conn->mutex);
  235. } else {
  236. p_ret = p_ll2_conn;
  237. }
  238. return p_ret;
  239. }
  240. static struct qed_ll2_info *qed_ll2_handle_sanity(struct qed_hwfn *p_hwfn,
  241. u8 connection_handle)
  242. {
  243. return __qed_ll2_handle_sanity(p_hwfn, connection_handle, false, true);
  244. }
  245. static struct qed_ll2_info *qed_ll2_handle_sanity_lock(struct qed_hwfn *p_hwfn,
  246. u8 connection_handle)
  247. {
  248. return __qed_ll2_handle_sanity(p_hwfn, connection_handle, true, true);
  249. }
  250. static struct qed_ll2_info *qed_ll2_handle_sanity_inactive(struct qed_hwfn
  251. *p_hwfn,
  252. u8 connection_handle)
  253. {
  254. return __qed_ll2_handle_sanity(p_hwfn, connection_handle, false, false);
  255. }
  256. static void qed_ll2_txq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
  257. {
  258. bool b_last_packet = false, b_last_frag = false;
  259. struct qed_ll2_tx_packet *p_pkt = NULL;
  260. struct qed_ll2_info *p_ll2_conn;
  261. struct qed_ll2_tx_queue *p_tx;
  262. dma_addr_t tx_frag;
  263. p_ll2_conn = qed_ll2_handle_sanity_inactive(p_hwfn, connection_handle);
  264. if (!p_ll2_conn)
  265. return;
  266. p_tx = &p_ll2_conn->tx_queue;
  267. while (!list_empty(&p_tx->active_descq)) {
  268. p_pkt = list_first_entry(&p_tx->active_descq,
  269. struct qed_ll2_tx_packet, list_entry);
  270. if (!p_pkt)
  271. break;
  272. list_del(&p_pkt->list_entry);
  273. b_last_packet = list_empty(&p_tx->active_descq);
  274. list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
  275. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_ISCSI_OOO) {
  276. struct qed_ooo_buffer *p_buffer;
  277. p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
  278. qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
  279. p_buffer);
  280. } else {
  281. p_tx->cur_completing_packet = *p_pkt;
  282. p_tx->cur_completing_bd_idx = 1;
  283. b_last_frag =
  284. p_tx->cur_completing_bd_idx == p_pkt->bd_used;
  285. tx_frag = p_pkt->bds_set[0].tx_frag;
  286. if (p_ll2_conn->conn.gsi_enable)
  287. qed_ll2b_release_tx_gsi_packet(p_hwfn,
  288. p_ll2_conn->
  289. my_id,
  290. p_pkt->cookie,
  291. tx_frag,
  292. b_last_frag,
  293. b_last_packet);
  294. else
  295. qed_ll2b_complete_tx_packet(p_hwfn,
  296. p_ll2_conn->my_id,
  297. p_pkt->cookie,
  298. tx_frag,
  299. b_last_frag,
  300. b_last_packet);
  301. }
  302. }
  303. }
  304. static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
  305. {
  306. struct qed_ll2_info *p_ll2_conn = p_cookie;
  307. struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
  308. u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
  309. struct qed_ll2_tx_packet *p_pkt;
  310. bool b_last_frag = false;
  311. unsigned long flags;
  312. dma_addr_t tx_frag;
  313. int rc = -EINVAL;
  314. spin_lock_irqsave(&p_tx->lock, flags);
  315. if (p_tx->b_completing_packet) {
  316. rc = -EBUSY;
  317. goto out;
  318. }
  319. new_idx = le16_to_cpu(*p_tx->p_fw_cons);
  320. num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
  321. while (num_bds) {
  322. if (list_empty(&p_tx->active_descq))
  323. goto out;
  324. p_pkt = list_first_entry(&p_tx->active_descq,
  325. struct qed_ll2_tx_packet, list_entry);
  326. if (!p_pkt)
  327. goto out;
  328. p_tx->b_completing_packet = true;
  329. p_tx->cur_completing_packet = *p_pkt;
  330. num_bds_in_packet = p_pkt->bd_used;
  331. list_del(&p_pkt->list_entry);
  332. if (num_bds < num_bds_in_packet) {
  333. DP_NOTICE(p_hwfn,
  334. "Rest of BDs does not cover whole packet\n");
  335. goto out;
  336. }
  337. num_bds -= num_bds_in_packet;
  338. p_tx->bds_idx += num_bds_in_packet;
  339. while (num_bds_in_packet--)
  340. qed_chain_consume(&p_tx->txq_chain);
  341. p_tx->cur_completing_bd_idx = 1;
  342. b_last_frag = p_tx->cur_completing_bd_idx == p_pkt->bd_used;
  343. list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
  344. spin_unlock_irqrestore(&p_tx->lock, flags);
  345. tx_frag = p_pkt->bds_set[0].tx_frag;
  346. if (p_ll2_conn->conn.gsi_enable)
  347. qed_ll2b_complete_tx_gsi_packet(p_hwfn,
  348. p_ll2_conn->my_id,
  349. p_pkt->cookie,
  350. tx_frag,
  351. b_last_frag, !num_bds);
  352. else
  353. qed_ll2b_complete_tx_packet(p_hwfn,
  354. p_ll2_conn->my_id,
  355. p_pkt->cookie,
  356. tx_frag,
  357. b_last_frag, !num_bds);
  358. spin_lock_irqsave(&p_tx->lock, flags);
  359. }
  360. p_tx->b_completing_packet = false;
  361. rc = 0;
  362. out:
  363. spin_unlock_irqrestore(&p_tx->lock, flags);
  364. return rc;
  365. }
  366. static int
  367. qed_ll2_rxq_completion_gsi(struct qed_hwfn *p_hwfn,
  368. struct qed_ll2_info *p_ll2_info,
  369. union core_rx_cqe_union *p_cqe,
  370. unsigned long lock_flags, bool b_last_cqe)
  371. {
  372. struct qed_ll2_rx_queue *p_rx = &p_ll2_info->rx_queue;
  373. struct qed_ll2_rx_packet *p_pkt = NULL;
  374. u16 packet_length, parse_flags, vlan;
  375. u32 src_mac_addrhi;
  376. u16 src_mac_addrlo;
  377. if (!list_empty(&p_rx->active_descq))
  378. p_pkt = list_first_entry(&p_rx->active_descq,
  379. struct qed_ll2_rx_packet, list_entry);
  380. if (!p_pkt) {
  381. DP_NOTICE(p_hwfn,
  382. "GSI Rx completion but active_descq is empty\n");
  383. return -EIO;
  384. }
  385. list_del(&p_pkt->list_entry);
  386. parse_flags = le16_to_cpu(p_cqe->rx_cqe_gsi.parse_flags.flags);
  387. packet_length = le16_to_cpu(p_cqe->rx_cqe_gsi.data_length);
  388. vlan = le16_to_cpu(p_cqe->rx_cqe_gsi.vlan);
  389. src_mac_addrhi = le32_to_cpu(p_cqe->rx_cqe_gsi.src_mac_addrhi);
  390. src_mac_addrlo = le16_to_cpu(p_cqe->rx_cqe_gsi.src_mac_addrlo);
  391. if (qed_chain_consume(&p_rx->rxq_chain) != p_pkt->rxq_bd)
  392. DP_NOTICE(p_hwfn,
  393. "Mismatch between active_descq and the LL2 Rx chain\n");
  394. list_add_tail(&p_pkt->list_entry, &p_rx->free_descq);
  395. spin_unlock_irqrestore(&p_rx->lock, lock_flags);
  396. qed_ll2b_complete_rx_gsi_packet(p_hwfn,
  397. p_ll2_info->my_id,
  398. p_pkt->cookie,
  399. p_pkt->rx_buf_addr,
  400. packet_length,
  401. p_cqe->rx_cqe_gsi.data_length_error,
  402. parse_flags,
  403. vlan,
  404. src_mac_addrhi,
  405. src_mac_addrlo, b_last_cqe);
  406. spin_lock_irqsave(&p_rx->lock, lock_flags);
  407. return 0;
  408. }
  409. static int qed_ll2_rxq_completion_reg(struct qed_hwfn *p_hwfn,
  410. struct qed_ll2_info *p_ll2_conn,
  411. union core_rx_cqe_union *p_cqe,
  412. unsigned long *p_lock_flags,
  413. bool b_last_cqe)
  414. {
  415. struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
  416. struct qed_ll2_rx_packet *p_pkt = NULL;
  417. if (!list_empty(&p_rx->active_descq))
  418. p_pkt = list_first_entry(&p_rx->active_descq,
  419. struct qed_ll2_rx_packet, list_entry);
  420. if (!p_pkt) {
  421. DP_NOTICE(p_hwfn,
  422. "LL2 Rx completion but active_descq is empty\n");
  423. return -EIO;
  424. }
  425. list_del(&p_pkt->list_entry);
  426. if (qed_chain_consume(&p_rx->rxq_chain) != p_pkt->rxq_bd)
  427. DP_NOTICE(p_hwfn,
  428. "Mismatch between active_descq and the LL2 Rx chain\n");
  429. list_add_tail(&p_pkt->list_entry, &p_rx->free_descq);
  430. spin_unlock_irqrestore(&p_rx->lock, *p_lock_flags);
  431. qed_ll2b_complete_rx_packet(p_hwfn, p_ll2_conn->my_id,
  432. p_pkt, &p_cqe->rx_cqe_fp, b_last_cqe);
  433. spin_lock_irqsave(&p_rx->lock, *p_lock_flags);
  434. return 0;
  435. }
  436. static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
  437. {
  438. struct qed_ll2_info *p_ll2_conn = cookie;
  439. struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
  440. union core_rx_cqe_union *cqe = NULL;
  441. u16 cq_new_idx = 0, cq_old_idx = 0;
  442. unsigned long flags = 0;
  443. int rc = 0;
  444. spin_lock_irqsave(&p_rx->lock, flags);
  445. cq_new_idx = le16_to_cpu(*p_rx->p_fw_cons);
  446. cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
  447. while (cq_new_idx != cq_old_idx) {
  448. bool b_last_cqe = (cq_new_idx == cq_old_idx);
  449. cqe = qed_chain_consume(&p_rx->rcq_chain);
  450. cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
  451. DP_VERBOSE(p_hwfn,
  452. QED_MSG_LL2,
  453. "LL2 [sw. cons %04x, fw. at %04x] - Got Packet of type %02x\n",
  454. cq_old_idx, cq_new_idx, cqe->rx_cqe_sp.type);
  455. switch (cqe->rx_cqe_sp.type) {
  456. case CORE_RX_CQE_TYPE_SLOW_PATH:
  457. DP_NOTICE(p_hwfn, "LL2 - unexpected Rx CQE slowpath\n");
  458. rc = -EINVAL;
  459. break;
  460. case CORE_RX_CQE_TYPE_GSI_OFFLOAD:
  461. rc = qed_ll2_rxq_completion_gsi(p_hwfn, p_ll2_conn,
  462. cqe, flags, b_last_cqe);
  463. break;
  464. case CORE_RX_CQE_TYPE_REGULAR:
  465. rc = qed_ll2_rxq_completion_reg(p_hwfn, p_ll2_conn,
  466. cqe, &flags,
  467. b_last_cqe);
  468. break;
  469. default:
  470. rc = -EIO;
  471. }
  472. }
  473. spin_unlock_irqrestore(&p_rx->lock, flags);
  474. return rc;
  475. }
  476. static void qed_ll2_rxq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
  477. {
  478. struct qed_ll2_info *p_ll2_conn = NULL;
  479. struct qed_ll2_rx_packet *p_pkt = NULL;
  480. struct qed_ll2_rx_queue *p_rx;
  481. p_ll2_conn = qed_ll2_handle_sanity_inactive(p_hwfn, connection_handle);
  482. if (!p_ll2_conn)
  483. return;
  484. p_rx = &p_ll2_conn->rx_queue;
  485. while (!list_empty(&p_rx->active_descq)) {
  486. dma_addr_t rx_buf_addr;
  487. void *cookie;
  488. bool b_last;
  489. p_pkt = list_first_entry(&p_rx->active_descq,
  490. struct qed_ll2_rx_packet, list_entry);
  491. if (!p_pkt)
  492. break;
  493. list_move_tail(&p_pkt->list_entry, &p_rx->free_descq);
  494. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_ISCSI_OOO) {
  495. struct qed_ooo_buffer *p_buffer;
  496. p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
  497. qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
  498. p_buffer);
  499. } else {
  500. rx_buf_addr = p_pkt->rx_buf_addr;
  501. cookie = p_pkt->cookie;
  502. b_last = list_empty(&p_rx->active_descq);
  503. }
  504. }
  505. }
  506. #if IS_ENABLED(CONFIG_QED_ISCSI)
  507. static u8 qed_ll2_convert_rx_parse_to_tx_flags(u16 parse_flags)
  508. {
  509. u8 bd_flags = 0;
  510. if (GET_FIELD(parse_flags, PARSING_AND_ERR_FLAGS_TAG8021QEXIST))
  511. SET_FIELD(bd_flags, CORE_TX_BD_FLAGS_VLAN_INSERTION, 1);
  512. return bd_flags;
  513. }
  514. static int qed_ll2_lb_rxq_handler(struct qed_hwfn *p_hwfn,
  515. struct qed_ll2_info *p_ll2_conn)
  516. {
  517. struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
  518. u16 packet_length = 0, parse_flags = 0, vlan = 0;
  519. struct qed_ll2_rx_packet *p_pkt = NULL;
  520. u32 num_ooo_add_to_peninsula = 0, cid;
  521. union core_rx_cqe_union *cqe = NULL;
  522. u16 cq_new_idx = 0, cq_old_idx = 0;
  523. struct qed_ooo_buffer *p_buffer;
  524. struct ooo_opaque *iscsi_ooo;
  525. u8 placement_offset = 0;
  526. u8 cqe_type;
  527. cq_new_idx = le16_to_cpu(*p_rx->p_fw_cons);
  528. cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
  529. if (cq_new_idx == cq_old_idx)
  530. return 0;
  531. while (cq_new_idx != cq_old_idx) {
  532. struct core_rx_fast_path_cqe *p_cqe_fp;
  533. cqe = qed_chain_consume(&p_rx->rcq_chain);
  534. cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
  535. cqe_type = cqe->rx_cqe_sp.type;
  536. if (cqe_type != CORE_RX_CQE_TYPE_REGULAR) {
  537. DP_NOTICE(p_hwfn,
  538. "Got a non-regular LB LL2 completion [type 0x%02x]\n",
  539. cqe_type);
  540. return -EINVAL;
  541. }
  542. p_cqe_fp = &cqe->rx_cqe_fp;
  543. placement_offset = p_cqe_fp->placement_offset;
  544. parse_flags = le16_to_cpu(p_cqe_fp->parse_flags.flags);
  545. packet_length = le16_to_cpu(p_cqe_fp->packet_length);
  546. vlan = le16_to_cpu(p_cqe_fp->vlan);
  547. iscsi_ooo = (struct ooo_opaque *)&p_cqe_fp->opaque_data;
  548. qed_ooo_save_history_entry(p_hwfn, p_hwfn->p_ooo_info,
  549. iscsi_ooo);
  550. cid = le32_to_cpu(iscsi_ooo->cid);
  551. /* Process delete isle first */
  552. if (iscsi_ooo->drop_size)
  553. qed_ooo_delete_isles(p_hwfn, p_hwfn->p_ooo_info, cid,
  554. iscsi_ooo->drop_isle,
  555. iscsi_ooo->drop_size);
  556. if (iscsi_ooo->ooo_opcode == TCP_EVENT_NOP)
  557. continue;
  558. /* Now process create/add/join isles */
  559. if (list_empty(&p_rx->active_descq)) {
  560. DP_NOTICE(p_hwfn,
  561. "LL2 OOO RX chain has no submitted buffers\n"
  562. );
  563. return -EIO;
  564. }
  565. p_pkt = list_first_entry(&p_rx->active_descq,
  566. struct qed_ll2_rx_packet, list_entry);
  567. if ((iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_NEW_ISLE) ||
  568. (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_RIGHT) ||
  569. (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_LEFT) ||
  570. (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_PEN) ||
  571. (iscsi_ooo->ooo_opcode == TCP_EVENT_JOIN)) {
  572. if (!p_pkt) {
  573. DP_NOTICE(p_hwfn,
  574. "LL2 OOO RX packet is not valid\n");
  575. return -EIO;
  576. }
  577. list_del(&p_pkt->list_entry);
  578. p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
  579. p_buffer->packet_length = packet_length;
  580. p_buffer->parse_flags = parse_flags;
  581. p_buffer->vlan = vlan;
  582. p_buffer->placement_offset = placement_offset;
  583. qed_chain_consume(&p_rx->rxq_chain);
  584. list_add_tail(&p_pkt->list_entry, &p_rx->free_descq);
  585. switch (iscsi_ooo->ooo_opcode) {
  586. case TCP_EVENT_ADD_NEW_ISLE:
  587. qed_ooo_add_new_isle(p_hwfn,
  588. p_hwfn->p_ooo_info,
  589. cid,
  590. iscsi_ooo->ooo_isle,
  591. p_buffer);
  592. break;
  593. case TCP_EVENT_ADD_ISLE_RIGHT:
  594. qed_ooo_add_new_buffer(p_hwfn,
  595. p_hwfn->p_ooo_info,
  596. cid,
  597. iscsi_ooo->ooo_isle,
  598. p_buffer,
  599. QED_OOO_RIGHT_BUF);
  600. break;
  601. case TCP_EVENT_ADD_ISLE_LEFT:
  602. qed_ooo_add_new_buffer(p_hwfn,
  603. p_hwfn->p_ooo_info,
  604. cid,
  605. iscsi_ooo->ooo_isle,
  606. p_buffer,
  607. QED_OOO_LEFT_BUF);
  608. break;
  609. case TCP_EVENT_JOIN:
  610. qed_ooo_add_new_buffer(p_hwfn,
  611. p_hwfn->p_ooo_info,
  612. cid,
  613. iscsi_ooo->ooo_isle +
  614. 1,
  615. p_buffer,
  616. QED_OOO_LEFT_BUF);
  617. qed_ooo_join_isles(p_hwfn,
  618. p_hwfn->p_ooo_info,
  619. cid, iscsi_ooo->ooo_isle);
  620. break;
  621. case TCP_EVENT_ADD_PEN:
  622. num_ooo_add_to_peninsula++;
  623. qed_ooo_put_ready_buffer(p_hwfn,
  624. p_hwfn->p_ooo_info,
  625. p_buffer, true);
  626. break;
  627. }
  628. } else {
  629. DP_NOTICE(p_hwfn,
  630. "Unexpected event (%d) TX OOO completion\n",
  631. iscsi_ooo->ooo_opcode);
  632. }
  633. }
  634. return 0;
  635. }
  636. static void
  637. qed_ooo_submit_tx_buffers(struct qed_hwfn *p_hwfn,
  638. struct qed_ll2_info *p_ll2_conn)
  639. {
  640. struct qed_ooo_buffer *p_buffer;
  641. int rc;
  642. u16 l4_hdr_offset_w;
  643. dma_addr_t first_frag;
  644. u16 parse_flags;
  645. u8 bd_flags;
  646. /* Submit Tx buffers here */
  647. while ((p_buffer = qed_ooo_get_ready_buffer(p_hwfn,
  648. p_hwfn->p_ooo_info))) {
  649. l4_hdr_offset_w = 0;
  650. bd_flags = 0;
  651. first_frag = p_buffer->rx_buffer_phys_addr +
  652. p_buffer->placement_offset;
  653. parse_flags = p_buffer->parse_flags;
  654. bd_flags = qed_ll2_convert_rx_parse_to_tx_flags(parse_flags);
  655. SET_FIELD(bd_flags, CORE_TX_BD_FLAGS_FORCE_VLAN_MODE, 1);
  656. SET_FIELD(bd_flags, CORE_TX_BD_FLAGS_L4_PROTOCOL, 1);
  657. rc = qed_ll2_prepare_tx_packet(p_hwfn, p_ll2_conn->my_id, 1,
  658. p_buffer->vlan, bd_flags,
  659. l4_hdr_offset_w,
  660. p_ll2_conn->conn.tx_dest, 0,
  661. first_frag,
  662. p_buffer->packet_length,
  663. p_buffer, true);
  664. if (rc) {
  665. qed_ooo_put_ready_buffer(p_hwfn, p_hwfn->p_ooo_info,
  666. p_buffer, false);
  667. break;
  668. }
  669. }
  670. }
  671. static void
  672. qed_ooo_submit_rx_buffers(struct qed_hwfn *p_hwfn,
  673. struct qed_ll2_info *p_ll2_conn)
  674. {
  675. struct qed_ooo_buffer *p_buffer;
  676. int rc;
  677. while ((p_buffer = qed_ooo_get_free_buffer(p_hwfn,
  678. p_hwfn->p_ooo_info))) {
  679. rc = qed_ll2_post_rx_buffer(p_hwfn,
  680. p_ll2_conn->my_id,
  681. p_buffer->rx_buffer_phys_addr,
  682. 0, p_buffer, true);
  683. if (rc) {
  684. qed_ooo_put_free_buffer(p_hwfn,
  685. p_hwfn->p_ooo_info, p_buffer);
  686. break;
  687. }
  688. }
  689. }
  690. static int qed_ll2_lb_rxq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
  691. {
  692. struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)p_cookie;
  693. int rc;
  694. rc = qed_ll2_lb_rxq_handler(p_hwfn, p_ll2_conn);
  695. if (rc)
  696. return rc;
  697. qed_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
  698. qed_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
  699. return 0;
  700. }
  701. static int qed_ll2_lb_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
  702. {
  703. struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)p_cookie;
  704. struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
  705. struct qed_ll2_tx_packet *p_pkt = NULL;
  706. struct qed_ooo_buffer *p_buffer;
  707. bool b_dont_submit_rx = false;
  708. u16 new_idx = 0, num_bds = 0;
  709. int rc;
  710. new_idx = le16_to_cpu(*p_tx->p_fw_cons);
  711. num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
  712. if (!num_bds)
  713. return 0;
  714. while (num_bds) {
  715. if (list_empty(&p_tx->active_descq))
  716. return -EINVAL;
  717. p_pkt = list_first_entry(&p_tx->active_descq,
  718. struct qed_ll2_tx_packet, list_entry);
  719. if (!p_pkt)
  720. return -EINVAL;
  721. if (p_pkt->bd_used != 1) {
  722. DP_NOTICE(p_hwfn,
  723. "Unexpectedly many BDs(%d) in TX OOO completion\n",
  724. p_pkt->bd_used);
  725. return -EINVAL;
  726. }
  727. list_del(&p_pkt->list_entry);
  728. num_bds--;
  729. p_tx->bds_idx++;
  730. qed_chain_consume(&p_tx->txq_chain);
  731. p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
  732. list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
  733. if (b_dont_submit_rx) {
  734. qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
  735. p_buffer);
  736. continue;
  737. }
  738. rc = qed_ll2_post_rx_buffer(p_hwfn, p_ll2_conn->my_id,
  739. p_buffer->rx_buffer_phys_addr, 0,
  740. p_buffer, true);
  741. if (rc != 0) {
  742. qed_ooo_put_free_buffer(p_hwfn,
  743. p_hwfn->p_ooo_info, p_buffer);
  744. b_dont_submit_rx = true;
  745. }
  746. }
  747. qed_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
  748. return 0;
  749. }
  750. static int
  751. qed_ll2_acquire_connection_ooo(struct qed_hwfn *p_hwfn,
  752. struct qed_ll2_info *p_ll2_info,
  753. u16 rx_num_ooo_buffers, u16 mtu)
  754. {
  755. struct qed_ooo_buffer *p_buf = NULL;
  756. void *p_virt;
  757. u16 buf_idx;
  758. int rc = 0;
  759. if (p_ll2_info->conn.conn_type != QED_LL2_TYPE_ISCSI_OOO)
  760. return rc;
  761. if (!rx_num_ooo_buffers)
  762. return -EINVAL;
  763. for (buf_idx = 0; buf_idx < rx_num_ooo_buffers; buf_idx++) {
  764. p_buf = kzalloc(sizeof(*p_buf), GFP_KERNEL);
  765. if (!p_buf) {
  766. rc = -ENOMEM;
  767. goto out;
  768. }
  769. p_buf->rx_buffer_size = mtu + 26 + ETH_CACHE_LINE_SIZE;
  770. p_buf->rx_buffer_size = (p_buf->rx_buffer_size +
  771. ETH_CACHE_LINE_SIZE - 1) &
  772. ~(ETH_CACHE_LINE_SIZE - 1);
  773. p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  774. p_buf->rx_buffer_size,
  775. &p_buf->rx_buffer_phys_addr,
  776. GFP_KERNEL);
  777. if (!p_virt) {
  778. kfree(p_buf);
  779. rc = -ENOMEM;
  780. goto out;
  781. }
  782. p_buf->rx_buffer_virt_addr = p_virt;
  783. qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info, p_buf);
  784. }
  785. DP_VERBOSE(p_hwfn, QED_MSG_LL2,
  786. "Allocated [%04x] LL2 OOO buffers [each of size 0x%08x]\n",
  787. rx_num_ooo_buffers, p_buf->rx_buffer_size);
  788. out:
  789. return rc;
  790. }
  791. static void
  792. qed_ll2_establish_connection_ooo(struct qed_hwfn *p_hwfn,
  793. struct qed_ll2_info *p_ll2_conn)
  794. {
  795. if (p_ll2_conn->conn.conn_type != QED_LL2_TYPE_ISCSI_OOO)
  796. return;
  797. qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
  798. qed_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
  799. }
  800. static void qed_ll2_release_connection_ooo(struct qed_hwfn *p_hwfn,
  801. struct qed_ll2_info *p_ll2_conn)
  802. {
  803. struct qed_ooo_buffer *p_buffer;
  804. if (p_ll2_conn->conn.conn_type != QED_LL2_TYPE_ISCSI_OOO)
  805. return;
  806. qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
  807. while ((p_buffer = qed_ooo_get_free_buffer(p_hwfn,
  808. p_hwfn->p_ooo_info))) {
  809. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  810. p_buffer->rx_buffer_size,
  811. p_buffer->rx_buffer_virt_addr,
  812. p_buffer->rx_buffer_phys_addr);
  813. kfree(p_buffer);
  814. }
  815. }
  816. static void qed_ll2_stop_ooo(struct qed_dev *cdev)
  817. {
  818. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  819. u8 *handle = &hwfn->pf_params.iscsi_pf_params.ll2_ooo_queue_id;
  820. DP_VERBOSE(cdev, QED_MSG_STORAGE, "Stopping LL2 OOO queue [%02x]\n",
  821. *handle);
  822. qed_ll2_terminate_connection(hwfn, *handle);
  823. qed_ll2_release_connection(hwfn, *handle);
  824. *handle = QED_LL2_UNUSED_HANDLE;
  825. }
  826. static int qed_ll2_start_ooo(struct qed_dev *cdev,
  827. struct qed_ll2_params *params)
  828. {
  829. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  830. u8 *handle = &hwfn->pf_params.iscsi_pf_params.ll2_ooo_queue_id;
  831. struct qed_ll2_conn ll2_info = { 0 };
  832. int rc;
  833. ll2_info.conn_type = QED_LL2_TYPE_ISCSI_OOO;
  834. ll2_info.mtu = params->mtu;
  835. ll2_info.rx_drop_ttl0_flg = params->drop_ttl0_packets;
  836. ll2_info.rx_vlan_removal_en = params->rx_vlan_stripping;
  837. ll2_info.tx_tc = OOO_LB_TC;
  838. ll2_info.tx_dest = CORE_TX_DEST_LB;
  839. rc = qed_ll2_acquire_connection(hwfn, &ll2_info,
  840. QED_LL2_RX_SIZE, QED_LL2_TX_SIZE,
  841. handle);
  842. if (rc) {
  843. DP_INFO(cdev, "Failed to acquire LL2 OOO connection\n");
  844. goto out;
  845. }
  846. rc = qed_ll2_establish_connection(hwfn, *handle);
  847. if (rc) {
  848. DP_INFO(cdev, "Failed to establist LL2 OOO connection\n");
  849. goto fail;
  850. }
  851. return 0;
  852. fail:
  853. qed_ll2_release_connection(hwfn, *handle);
  854. out:
  855. *handle = QED_LL2_UNUSED_HANDLE;
  856. return rc;
  857. }
  858. #else /* IS_ENABLED(CONFIG_QED_ISCSI) */
  859. static int qed_ll2_lb_rxq_completion(struct qed_hwfn *p_hwfn,
  860. void *p_cookie) { return -EINVAL; }
  861. static int qed_ll2_lb_txq_completion(struct qed_hwfn *p_hwfn,
  862. void *p_cookie) { return -EINVAL; }
  863. static inline int
  864. qed_ll2_acquire_connection_ooo(struct qed_hwfn *p_hwfn,
  865. struct qed_ll2_info *p_ll2_info,
  866. u16 rx_num_ooo_buffers, u16 mtu) { return 0; }
  867. static inline void
  868. qed_ll2_establish_connection_ooo(struct qed_hwfn *p_hwfn,
  869. struct qed_ll2_info *p_ll2_conn) { return; }
  870. static inline void
  871. qed_ll2_release_connection_ooo(struct qed_hwfn *p_hwfn,
  872. struct qed_ll2_info *p_ll2_conn) { return; }
  873. static inline void qed_ll2_stop_ooo(struct qed_dev *cdev) { return; }
  874. static inline int qed_ll2_start_ooo(struct qed_dev *cdev,
  875. struct qed_ll2_params *params)
  876. { return -EINVAL; }
  877. #endif /* IS_ENABLED(CONFIG_QED_ISCSI) */
  878. static int qed_sp_ll2_rx_queue_start(struct qed_hwfn *p_hwfn,
  879. struct qed_ll2_info *p_ll2_conn,
  880. u8 action_on_error)
  881. {
  882. enum qed_ll2_conn_type conn_type = p_ll2_conn->conn.conn_type;
  883. struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
  884. struct core_rx_start_ramrod_data *p_ramrod = NULL;
  885. struct qed_spq_entry *p_ent = NULL;
  886. struct qed_sp_init_data init_data;
  887. u16 cqe_pbl_size;
  888. int rc = 0;
  889. /* Get SPQ entry */
  890. memset(&init_data, 0, sizeof(init_data));
  891. init_data.cid = p_ll2_conn->cid;
  892. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  893. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  894. rc = qed_sp_init_request(p_hwfn, &p_ent,
  895. CORE_RAMROD_RX_QUEUE_START,
  896. PROTOCOLID_CORE, &init_data);
  897. if (rc)
  898. return rc;
  899. p_ramrod = &p_ent->ramrod.core_rx_queue_start;
  900. p_ramrod->sb_id = cpu_to_le16(qed_int_get_sp_sb_id(p_hwfn));
  901. p_ramrod->sb_index = p_rx->rx_sb_index;
  902. p_ramrod->complete_event_flg = 1;
  903. p_ramrod->mtu = cpu_to_le16(p_ll2_conn->conn.mtu);
  904. DMA_REGPAIR_LE(p_ramrod->bd_base,
  905. p_rx->rxq_chain.p_phys_addr);
  906. cqe_pbl_size = (u16)qed_chain_get_page_cnt(&p_rx->rcq_chain);
  907. p_ramrod->num_of_pbl_pages = cpu_to_le16(cqe_pbl_size);
  908. DMA_REGPAIR_LE(p_ramrod->cqe_pbl_addr,
  909. qed_chain_get_pbl_phys(&p_rx->rcq_chain));
  910. p_ramrod->drop_ttl0_flg = p_ll2_conn->conn.rx_drop_ttl0_flg;
  911. p_ramrod->inner_vlan_removal_en = p_ll2_conn->conn.rx_vlan_removal_en;
  912. p_ramrod->queue_id = p_ll2_conn->queue_id;
  913. p_ramrod->main_func_queue = (conn_type == QED_LL2_TYPE_ISCSI_OOO) ? 0
  914. : 1;
  915. if ((IS_MF_DEFAULT(p_hwfn) || IS_MF_SI(p_hwfn)) &&
  916. p_ramrod->main_func_queue && (conn_type != QED_LL2_TYPE_ROCE)) {
  917. p_ramrod->mf_si_bcast_accept_all = 1;
  918. p_ramrod->mf_si_mcast_accept_all = 1;
  919. } else {
  920. p_ramrod->mf_si_bcast_accept_all = 0;
  921. p_ramrod->mf_si_mcast_accept_all = 0;
  922. }
  923. p_ramrod->action_on_error.error_type = action_on_error;
  924. p_ramrod->gsi_offload_flag = p_ll2_conn->conn.gsi_enable;
  925. return qed_spq_post(p_hwfn, p_ent, NULL);
  926. }
  927. static int qed_sp_ll2_tx_queue_start(struct qed_hwfn *p_hwfn,
  928. struct qed_ll2_info *p_ll2_conn)
  929. {
  930. enum qed_ll2_conn_type conn_type = p_ll2_conn->conn.conn_type;
  931. struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
  932. struct core_tx_start_ramrod_data *p_ramrod = NULL;
  933. struct qed_spq_entry *p_ent = NULL;
  934. struct qed_sp_init_data init_data;
  935. union qed_qm_pq_params pq_params;
  936. u16 pq_id = 0, pbl_size;
  937. int rc = -EINVAL;
  938. if (!QED_LL2_TX_REGISTERED(p_ll2_conn))
  939. return 0;
  940. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_ISCSI_OOO)
  941. p_ll2_conn->tx_stats_en = 0;
  942. else
  943. p_ll2_conn->tx_stats_en = 1;
  944. /* Get SPQ entry */
  945. memset(&init_data, 0, sizeof(init_data));
  946. init_data.cid = p_ll2_conn->cid;
  947. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  948. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  949. rc = qed_sp_init_request(p_hwfn, &p_ent,
  950. CORE_RAMROD_TX_QUEUE_START,
  951. PROTOCOLID_CORE, &init_data);
  952. if (rc)
  953. return rc;
  954. p_ramrod = &p_ent->ramrod.core_tx_queue_start;
  955. p_ramrod->sb_id = cpu_to_le16(qed_int_get_sp_sb_id(p_hwfn));
  956. p_ramrod->sb_index = p_tx->tx_sb_index;
  957. p_ramrod->mtu = cpu_to_le16(p_ll2_conn->conn.mtu);
  958. p_ramrod->stats_en = p_ll2_conn->tx_stats_en;
  959. p_ramrod->stats_id = p_ll2_conn->tx_stats_id;
  960. DMA_REGPAIR_LE(p_ramrod->pbl_base_addr,
  961. qed_chain_get_pbl_phys(&p_tx->txq_chain));
  962. pbl_size = qed_chain_get_page_cnt(&p_tx->txq_chain);
  963. p_ramrod->pbl_size = cpu_to_le16(pbl_size);
  964. memset(&pq_params, 0, sizeof(pq_params));
  965. pq_params.core.tc = p_ll2_conn->conn.tx_tc;
  966. pq_id = qed_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
  967. p_ramrod->qm_pq_id = cpu_to_le16(pq_id);
  968. switch (conn_type) {
  969. case QED_LL2_TYPE_FCOE:
  970. p_ramrod->conn_type = PROTOCOLID_FCOE;
  971. break;
  972. case QED_LL2_TYPE_ISCSI:
  973. case QED_LL2_TYPE_ISCSI_OOO:
  974. p_ramrod->conn_type = PROTOCOLID_ISCSI;
  975. break;
  976. case QED_LL2_TYPE_ROCE:
  977. p_ramrod->conn_type = PROTOCOLID_ROCE;
  978. break;
  979. default:
  980. p_ramrod->conn_type = PROTOCOLID_ETH;
  981. DP_NOTICE(p_hwfn, "Unknown connection type: %d\n", conn_type);
  982. }
  983. p_ramrod->gsi_offload_flag = p_ll2_conn->conn.gsi_enable;
  984. return qed_spq_post(p_hwfn, p_ent, NULL);
  985. }
  986. static int qed_sp_ll2_rx_queue_stop(struct qed_hwfn *p_hwfn,
  987. struct qed_ll2_info *p_ll2_conn)
  988. {
  989. struct core_rx_stop_ramrod_data *p_ramrod = NULL;
  990. struct qed_spq_entry *p_ent = NULL;
  991. struct qed_sp_init_data init_data;
  992. int rc = -EINVAL;
  993. /* Get SPQ entry */
  994. memset(&init_data, 0, sizeof(init_data));
  995. init_data.cid = p_ll2_conn->cid;
  996. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  997. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  998. rc = qed_sp_init_request(p_hwfn, &p_ent,
  999. CORE_RAMROD_RX_QUEUE_STOP,
  1000. PROTOCOLID_CORE, &init_data);
  1001. if (rc)
  1002. return rc;
  1003. p_ramrod = &p_ent->ramrod.core_rx_queue_stop;
  1004. p_ramrod->complete_event_flg = 1;
  1005. p_ramrod->queue_id = p_ll2_conn->queue_id;
  1006. return qed_spq_post(p_hwfn, p_ent, NULL);
  1007. }
  1008. static int qed_sp_ll2_tx_queue_stop(struct qed_hwfn *p_hwfn,
  1009. struct qed_ll2_info *p_ll2_conn)
  1010. {
  1011. struct qed_spq_entry *p_ent = NULL;
  1012. struct qed_sp_init_data init_data;
  1013. int rc = -EINVAL;
  1014. /* Get SPQ entry */
  1015. memset(&init_data, 0, sizeof(init_data));
  1016. init_data.cid = p_ll2_conn->cid;
  1017. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  1018. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  1019. rc = qed_sp_init_request(p_hwfn, &p_ent,
  1020. CORE_RAMROD_TX_QUEUE_STOP,
  1021. PROTOCOLID_CORE, &init_data);
  1022. if (rc)
  1023. return rc;
  1024. return qed_spq_post(p_hwfn, p_ent, NULL);
  1025. }
  1026. static int
  1027. qed_ll2_acquire_connection_rx(struct qed_hwfn *p_hwfn,
  1028. struct qed_ll2_info *p_ll2_info, u16 rx_num_desc)
  1029. {
  1030. struct qed_ll2_rx_packet *p_descq;
  1031. u32 capacity;
  1032. int rc = 0;
  1033. if (!rx_num_desc)
  1034. goto out;
  1035. rc = qed_chain_alloc(p_hwfn->cdev,
  1036. QED_CHAIN_USE_TO_CONSUME_PRODUCE,
  1037. QED_CHAIN_MODE_NEXT_PTR,
  1038. QED_CHAIN_CNT_TYPE_U16,
  1039. rx_num_desc,
  1040. sizeof(struct core_rx_bd),
  1041. &p_ll2_info->rx_queue.rxq_chain);
  1042. if (rc) {
  1043. DP_NOTICE(p_hwfn, "Failed to allocate ll2 rxq chain\n");
  1044. goto out;
  1045. }
  1046. capacity = qed_chain_get_capacity(&p_ll2_info->rx_queue.rxq_chain);
  1047. p_descq = kcalloc(capacity, sizeof(struct qed_ll2_rx_packet),
  1048. GFP_KERNEL);
  1049. if (!p_descq) {
  1050. rc = -ENOMEM;
  1051. DP_NOTICE(p_hwfn, "Failed to allocate ll2 Rx desc\n");
  1052. goto out;
  1053. }
  1054. p_ll2_info->rx_queue.descq_array = p_descq;
  1055. rc = qed_chain_alloc(p_hwfn->cdev,
  1056. QED_CHAIN_USE_TO_CONSUME_PRODUCE,
  1057. QED_CHAIN_MODE_PBL,
  1058. QED_CHAIN_CNT_TYPE_U16,
  1059. rx_num_desc,
  1060. sizeof(struct core_rx_fast_path_cqe),
  1061. &p_ll2_info->rx_queue.rcq_chain);
  1062. if (rc) {
  1063. DP_NOTICE(p_hwfn, "Failed to allocate ll2 rcq chain\n");
  1064. goto out;
  1065. }
  1066. DP_VERBOSE(p_hwfn, QED_MSG_LL2,
  1067. "Allocated LL2 Rxq [Type %08x] with 0x%08x buffers\n",
  1068. p_ll2_info->conn.conn_type, rx_num_desc);
  1069. out:
  1070. return rc;
  1071. }
  1072. static int qed_ll2_acquire_connection_tx(struct qed_hwfn *p_hwfn,
  1073. struct qed_ll2_info *p_ll2_info,
  1074. u16 tx_num_desc)
  1075. {
  1076. struct qed_ll2_tx_packet *p_descq;
  1077. u32 capacity;
  1078. int rc = 0;
  1079. if (!tx_num_desc)
  1080. goto out;
  1081. rc = qed_chain_alloc(p_hwfn->cdev,
  1082. QED_CHAIN_USE_TO_CONSUME_PRODUCE,
  1083. QED_CHAIN_MODE_PBL,
  1084. QED_CHAIN_CNT_TYPE_U16,
  1085. tx_num_desc,
  1086. sizeof(struct core_tx_bd),
  1087. &p_ll2_info->tx_queue.txq_chain);
  1088. if (rc)
  1089. goto out;
  1090. capacity = qed_chain_get_capacity(&p_ll2_info->tx_queue.txq_chain);
  1091. p_descq = kcalloc(capacity, sizeof(struct qed_ll2_tx_packet),
  1092. GFP_KERNEL);
  1093. if (!p_descq) {
  1094. rc = -ENOMEM;
  1095. goto out;
  1096. }
  1097. p_ll2_info->tx_queue.descq_array = p_descq;
  1098. DP_VERBOSE(p_hwfn, QED_MSG_LL2,
  1099. "Allocated LL2 Txq [Type %08x] with 0x%08x buffers\n",
  1100. p_ll2_info->conn.conn_type, tx_num_desc);
  1101. out:
  1102. if (rc)
  1103. DP_NOTICE(p_hwfn,
  1104. "Can't allocate memory for Tx LL2 with 0x%08x buffers\n",
  1105. tx_num_desc);
  1106. return rc;
  1107. }
  1108. int qed_ll2_acquire_connection(struct qed_hwfn *p_hwfn,
  1109. struct qed_ll2_conn *p_params,
  1110. u16 rx_num_desc,
  1111. u16 tx_num_desc,
  1112. u8 *p_connection_handle)
  1113. {
  1114. qed_int_comp_cb_t comp_rx_cb, comp_tx_cb;
  1115. struct qed_ll2_info *p_ll2_info = NULL;
  1116. int rc;
  1117. u8 i;
  1118. if (!p_connection_handle || !p_hwfn->p_ll2_info)
  1119. return -EINVAL;
  1120. /* Find a free connection to be used */
  1121. for (i = 0; (i < QED_MAX_NUM_OF_LL2_CONNECTIONS); i++) {
  1122. mutex_lock(&p_hwfn->p_ll2_info[i].mutex);
  1123. if (p_hwfn->p_ll2_info[i].b_active) {
  1124. mutex_unlock(&p_hwfn->p_ll2_info[i].mutex);
  1125. continue;
  1126. }
  1127. p_hwfn->p_ll2_info[i].b_active = true;
  1128. p_ll2_info = &p_hwfn->p_ll2_info[i];
  1129. mutex_unlock(&p_hwfn->p_ll2_info[i].mutex);
  1130. break;
  1131. }
  1132. if (!p_ll2_info)
  1133. return -EBUSY;
  1134. p_ll2_info->conn = *p_params;
  1135. rc = qed_ll2_acquire_connection_rx(p_hwfn, p_ll2_info, rx_num_desc);
  1136. if (rc)
  1137. goto q_allocate_fail;
  1138. rc = qed_ll2_acquire_connection_tx(p_hwfn, p_ll2_info, tx_num_desc);
  1139. if (rc)
  1140. goto q_allocate_fail;
  1141. rc = qed_ll2_acquire_connection_ooo(p_hwfn, p_ll2_info,
  1142. rx_num_desc * 2, p_params->mtu);
  1143. if (rc)
  1144. goto q_allocate_fail;
  1145. /* Register callbacks for the Rx/Tx queues */
  1146. if (p_params->conn_type == QED_LL2_TYPE_ISCSI_OOO) {
  1147. comp_rx_cb = qed_ll2_lb_rxq_completion;
  1148. comp_tx_cb = qed_ll2_lb_txq_completion;
  1149. } else {
  1150. comp_rx_cb = qed_ll2_rxq_completion;
  1151. comp_tx_cb = qed_ll2_txq_completion;
  1152. }
  1153. if (rx_num_desc) {
  1154. qed_int_register_cb(p_hwfn, comp_rx_cb,
  1155. &p_hwfn->p_ll2_info[i],
  1156. &p_ll2_info->rx_queue.rx_sb_index,
  1157. &p_ll2_info->rx_queue.p_fw_cons);
  1158. p_ll2_info->rx_queue.b_cb_registred = true;
  1159. }
  1160. if (tx_num_desc) {
  1161. qed_int_register_cb(p_hwfn,
  1162. comp_tx_cb,
  1163. &p_hwfn->p_ll2_info[i],
  1164. &p_ll2_info->tx_queue.tx_sb_index,
  1165. &p_ll2_info->tx_queue.p_fw_cons);
  1166. p_ll2_info->tx_queue.b_cb_registred = true;
  1167. }
  1168. *p_connection_handle = i;
  1169. return rc;
  1170. q_allocate_fail:
  1171. qed_ll2_release_connection(p_hwfn, i);
  1172. return -ENOMEM;
  1173. }
  1174. static int qed_ll2_establish_connection_rx(struct qed_hwfn *p_hwfn,
  1175. struct qed_ll2_info *p_ll2_conn)
  1176. {
  1177. u8 action_on_error = 0;
  1178. if (!QED_LL2_RX_REGISTERED(p_ll2_conn))
  1179. return 0;
  1180. DIRECT_REG_WR(p_ll2_conn->rx_queue.set_prod_addr, 0x0);
  1181. SET_FIELD(action_on_error,
  1182. CORE_RX_ACTION_ON_ERROR_PACKET_TOO_BIG,
  1183. p_ll2_conn->conn.ai_err_packet_too_big);
  1184. SET_FIELD(action_on_error,
  1185. CORE_RX_ACTION_ON_ERROR_NO_BUFF, p_ll2_conn->conn.ai_err_no_buf);
  1186. return qed_sp_ll2_rx_queue_start(p_hwfn, p_ll2_conn, action_on_error);
  1187. }
  1188. int qed_ll2_establish_connection(struct qed_hwfn *p_hwfn, u8 connection_handle)
  1189. {
  1190. struct qed_ll2_info *p_ll2_conn;
  1191. struct qed_ll2_rx_queue *p_rx;
  1192. struct qed_ll2_tx_queue *p_tx;
  1193. int rc = -EINVAL;
  1194. u32 i, capacity;
  1195. u8 qid;
  1196. p_ll2_conn = qed_ll2_handle_sanity_lock(p_hwfn, connection_handle);
  1197. if (!p_ll2_conn)
  1198. return -EINVAL;
  1199. p_rx = &p_ll2_conn->rx_queue;
  1200. p_tx = &p_ll2_conn->tx_queue;
  1201. qed_chain_reset(&p_rx->rxq_chain);
  1202. qed_chain_reset(&p_rx->rcq_chain);
  1203. INIT_LIST_HEAD(&p_rx->active_descq);
  1204. INIT_LIST_HEAD(&p_rx->free_descq);
  1205. INIT_LIST_HEAD(&p_rx->posting_descq);
  1206. spin_lock_init(&p_rx->lock);
  1207. capacity = qed_chain_get_capacity(&p_rx->rxq_chain);
  1208. for (i = 0; i < capacity; i++)
  1209. list_add_tail(&p_rx->descq_array[i].list_entry,
  1210. &p_rx->free_descq);
  1211. *p_rx->p_fw_cons = 0;
  1212. qed_chain_reset(&p_tx->txq_chain);
  1213. INIT_LIST_HEAD(&p_tx->active_descq);
  1214. INIT_LIST_HEAD(&p_tx->free_descq);
  1215. INIT_LIST_HEAD(&p_tx->sending_descq);
  1216. spin_lock_init(&p_tx->lock);
  1217. capacity = qed_chain_get_capacity(&p_tx->txq_chain);
  1218. for (i = 0; i < capacity; i++)
  1219. list_add_tail(&p_tx->descq_array[i].list_entry,
  1220. &p_tx->free_descq);
  1221. p_tx->cur_completing_bd_idx = 0;
  1222. p_tx->bds_idx = 0;
  1223. p_tx->b_completing_packet = false;
  1224. p_tx->cur_send_packet = NULL;
  1225. p_tx->cur_send_frag_num = 0;
  1226. p_tx->cur_completing_frag_num = 0;
  1227. *p_tx->p_fw_cons = 0;
  1228. qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_ll2_conn->cid);
  1229. qid = p_hwfn->hw_info.resc_start[QED_LL2_QUEUE] + connection_handle;
  1230. p_ll2_conn->queue_id = qid;
  1231. p_ll2_conn->tx_stats_id = qid;
  1232. p_rx->set_prod_addr = (u8 __iomem *)p_hwfn->regview +
  1233. GTT_BAR0_MAP_REG_TSDM_RAM +
  1234. TSTORM_LL2_RX_PRODS_OFFSET(qid);
  1235. p_tx->doorbell_addr = (u8 __iomem *)p_hwfn->doorbells +
  1236. qed_db_addr(p_ll2_conn->cid,
  1237. DQ_DEMS_LEGACY);
  1238. rc = qed_ll2_establish_connection_rx(p_hwfn, p_ll2_conn);
  1239. if (rc)
  1240. return rc;
  1241. rc = qed_sp_ll2_tx_queue_start(p_hwfn, p_ll2_conn);
  1242. if (rc)
  1243. return rc;
  1244. if (p_hwfn->hw_info.personality != QED_PCI_ETH_ROCE)
  1245. qed_wr(p_hwfn, p_hwfn->p_main_ptt, PRS_REG_USE_LIGHT_L2, 1);
  1246. qed_ll2_establish_connection_ooo(p_hwfn, p_ll2_conn);
  1247. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_FCOE) {
  1248. qed_llh_add_protocol_filter(p_hwfn, p_hwfn->p_main_ptt,
  1249. 0x8906, 0,
  1250. QED_LLH_FILTER_ETHERTYPE);
  1251. qed_llh_add_protocol_filter(p_hwfn, p_hwfn->p_main_ptt,
  1252. 0x8914, 0,
  1253. QED_LLH_FILTER_ETHERTYPE);
  1254. }
  1255. return rc;
  1256. }
  1257. static void qed_ll2_post_rx_buffer_notify_fw(struct qed_hwfn *p_hwfn,
  1258. struct qed_ll2_rx_queue *p_rx,
  1259. struct qed_ll2_rx_packet *p_curp)
  1260. {
  1261. struct qed_ll2_rx_packet *p_posting_packet = NULL;
  1262. struct core_ll2_rx_prod rx_prod = { 0, 0, 0 };
  1263. bool b_notify_fw = false;
  1264. u16 bd_prod, cq_prod;
  1265. /* This handles the flushing of already posted buffers */
  1266. while (!list_empty(&p_rx->posting_descq)) {
  1267. p_posting_packet = list_first_entry(&p_rx->posting_descq,
  1268. struct qed_ll2_rx_packet,
  1269. list_entry);
  1270. list_move_tail(&p_posting_packet->list_entry,
  1271. &p_rx->active_descq);
  1272. b_notify_fw = true;
  1273. }
  1274. /* This handles the supplied packet [if there is one] */
  1275. if (p_curp) {
  1276. list_add_tail(&p_curp->list_entry, &p_rx->active_descq);
  1277. b_notify_fw = true;
  1278. }
  1279. if (!b_notify_fw)
  1280. return;
  1281. bd_prod = qed_chain_get_prod_idx(&p_rx->rxq_chain);
  1282. cq_prod = qed_chain_get_prod_idx(&p_rx->rcq_chain);
  1283. rx_prod.bd_prod = cpu_to_le16(bd_prod);
  1284. rx_prod.cqe_prod = cpu_to_le16(cq_prod);
  1285. DIRECT_REG_WR(p_rx->set_prod_addr, *((u32 *)&rx_prod));
  1286. }
  1287. int qed_ll2_post_rx_buffer(struct qed_hwfn *p_hwfn,
  1288. u8 connection_handle,
  1289. dma_addr_t addr,
  1290. u16 buf_len, void *cookie, u8 notify_fw)
  1291. {
  1292. struct core_rx_bd_with_buff_len *p_curb = NULL;
  1293. struct qed_ll2_rx_packet *p_curp = NULL;
  1294. struct qed_ll2_info *p_ll2_conn;
  1295. struct qed_ll2_rx_queue *p_rx;
  1296. unsigned long flags;
  1297. void *p_data;
  1298. int rc = 0;
  1299. p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
  1300. if (!p_ll2_conn)
  1301. return -EINVAL;
  1302. p_rx = &p_ll2_conn->rx_queue;
  1303. spin_lock_irqsave(&p_rx->lock, flags);
  1304. if (!list_empty(&p_rx->free_descq))
  1305. p_curp = list_first_entry(&p_rx->free_descq,
  1306. struct qed_ll2_rx_packet, list_entry);
  1307. if (p_curp) {
  1308. if (qed_chain_get_elem_left(&p_rx->rxq_chain) &&
  1309. qed_chain_get_elem_left(&p_rx->rcq_chain)) {
  1310. p_data = qed_chain_produce(&p_rx->rxq_chain);
  1311. p_curb = (struct core_rx_bd_with_buff_len *)p_data;
  1312. qed_chain_produce(&p_rx->rcq_chain);
  1313. }
  1314. }
  1315. /* If we're lacking entires, let's try to flush buffers to FW */
  1316. if (!p_curp || !p_curb) {
  1317. rc = -EBUSY;
  1318. p_curp = NULL;
  1319. goto out_notify;
  1320. }
  1321. /* We have an Rx packet we can fill */
  1322. DMA_REGPAIR_LE(p_curb->addr, addr);
  1323. p_curb->buff_length = cpu_to_le16(buf_len);
  1324. p_curp->rx_buf_addr = addr;
  1325. p_curp->cookie = cookie;
  1326. p_curp->rxq_bd = p_curb;
  1327. p_curp->buf_length = buf_len;
  1328. list_del(&p_curp->list_entry);
  1329. /* Check if we only want to enqueue this packet without informing FW */
  1330. if (!notify_fw) {
  1331. list_add_tail(&p_curp->list_entry, &p_rx->posting_descq);
  1332. goto out;
  1333. }
  1334. out_notify:
  1335. qed_ll2_post_rx_buffer_notify_fw(p_hwfn, p_rx, p_curp);
  1336. out:
  1337. spin_unlock_irqrestore(&p_rx->lock, flags);
  1338. return rc;
  1339. }
  1340. static void qed_ll2_prepare_tx_packet_set(struct qed_hwfn *p_hwfn,
  1341. struct qed_ll2_tx_queue *p_tx,
  1342. struct qed_ll2_tx_packet *p_curp,
  1343. u8 num_of_bds,
  1344. dma_addr_t first_frag,
  1345. u16 first_frag_len, void *p_cookie,
  1346. u8 notify_fw)
  1347. {
  1348. list_del(&p_curp->list_entry);
  1349. p_curp->cookie = p_cookie;
  1350. p_curp->bd_used = num_of_bds;
  1351. p_curp->notify_fw = notify_fw;
  1352. p_tx->cur_send_packet = p_curp;
  1353. p_tx->cur_send_frag_num = 0;
  1354. p_curp->bds_set[p_tx->cur_send_frag_num].tx_frag = first_frag;
  1355. p_curp->bds_set[p_tx->cur_send_frag_num].frag_len = first_frag_len;
  1356. p_tx->cur_send_frag_num++;
  1357. }
  1358. static void qed_ll2_prepare_tx_packet_set_bd(struct qed_hwfn *p_hwfn,
  1359. struct qed_ll2_info *p_ll2,
  1360. struct qed_ll2_tx_packet *p_curp,
  1361. u8 num_of_bds,
  1362. enum core_tx_dest tx_dest,
  1363. u16 vlan,
  1364. u8 bd_flags,
  1365. u16 l4_hdr_offset_w,
  1366. enum core_roce_flavor_type type,
  1367. dma_addr_t first_frag,
  1368. u16 first_frag_len)
  1369. {
  1370. struct qed_chain *p_tx_chain = &p_ll2->tx_queue.txq_chain;
  1371. u16 prod_idx = qed_chain_get_prod_idx(p_tx_chain);
  1372. struct core_tx_bd *start_bd = NULL;
  1373. u16 frag_idx;
  1374. start_bd = (struct core_tx_bd *)qed_chain_produce(p_tx_chain);
  1375. start_bd->nw_vlan_or_lb_echo = cpu_to_le16(vlan);
  1376. SET_FIELD(start_bd->bitfield1, CORE_TX_BD_L4_HDR_OFFSET_W,
  1377. cpu_to_le16(l4_hdr_offset_w));
  1378. SET_FIELD(start_bd->bitfield1, CORE_TX_BD_TX_DST, tx_dest);
  1379. start_bd->bd_flags.as_bitfield = bd_flags;
  1380. start_bd->bd_flags.as_bitfield |= CORE_TX_BD_FLAGS_START_BD_MASK <<
  1381. CORE_TX_BD_FLAGS_START_BD_SHIFT;
  1382. SET_FIELD(start_bd->bitfield0, CORE_TX_BD_NBDS, num_of_bds);
  1383. SET_FIELD(start_bd->bitfield0, CORE_TX_BD_ROCE_FLAV, type);
  1384. DMA_REGPAIR_LE(start_bd->addr, first_frag);
  1385. start_bd->nbytes = cpu_to_le16(first_frag_len);
  1386. DP_VERBOSE(p_hwfn,
  1387. (NETIF_MSG_TX_QUEUED | QED_MSG_LL2),
  1388. "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Tx Producer at [0x%04x] - set with a %04x bytes %02x BDs buffer at %08x:%08x\n",
  1389. p_ll2->queue_id,
  1390. p_ll2->cid,
  1391. p_ll2->conn.conn_type,
  1392. prod_idx,
  1393. first_frag_len,
  1394. num_of_bds,
  1395. le32_to_cpu(start_bd->addr.hi),
  1396. le32_to_cpu(start_bd->addr.lo));
  1397. if (p_ll2->tx_queue.cur_send_frag_num == num_of_bds)
  1398. return;
  1399. /* Need to provide the packet with additional BDs for frags */
  1400. for (frag_idx = p_ll2->tx_queue.cur_send_frag_num;
  1401. frag_idx < num_of_bds; frag_idx++) {
  1402. struct core_tx_bd **p_bd = &p_curp->bds_set[frag_idx].txq_bd;
  1403. *p_bd = (struct core_tx_bd *)qed_chain_produce(p_tx_chain);
  1404. (*p_bd)->bd_flags.as_bitfield = 0;
  1405. (*p_bd)->bitfield1 = 0;
  1406. (*p_bd)->bitfield0 = 0;
  1407. p_curp->bds_set[frag_idx].tx_frag = 0;
  1408. p_curp->bds_set[frag_idx].frag_len = 0;
  1409. }
  1410. }
  1411. /* This should be called while the Txq spinlock is being held */
  1412. static void qed_ll2_tx_packet_notify(struct qed_hwfn *p_hwfn,
  1413. struct qed_ll2_info *p_ll2_conn)
  1414. {
  1415. bool b_notify = p_ll2_conn->tx_queue.cur_send_packet->notify_fw;
  1416. struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
  1417. struct qed_ll2_tx_packet *p_pkt = NULL;
  1418. struct core_db_data db_msg = { 0, 0, 0 };
  1419. u16 bd_prod;
  1420. /* If there are missing BDs, don't do anything now */
  1421. if (p_ll2_conn->tx_queue.cur_send_frag_num !=
  1422. p_ll2_conn->tx_queue.cur_send_packet->bd_used)
  1423. return;
  1424. /* Push the current packet to the list and clean after it */
  1425. list_add_tail(&p_ll2_conn->tx_queue.cur_send_packet->list_entry,
  1426. &p_ll2_conn->tx_queue.sending_descq);
  1427. p_ll2_conn->tx_queue.cur_send_packet = NULL;
  1428. p_ll2_conn->tx_queue.cur_send_frag_num = 0;
  1429. /* Notify FW of packet only if requested to */
  1430. if (!b_notify)
  1431. return;
  1432. bd_prod = qed_chain_get_prod_idx(&p_ll2_conn->tx_queue.txq_chain);
  1433. while (!list_empty(&p_tx->sending_descq)) {
  1434. p_pkt = list_first_entry(&p_tx->sending_descq,
  1435. struct qed_ll2_tx_packet, list_entry);
  1436. if (!p_pkt)
  1437. break;
  1438. list_move_tail(&p_pkt->list_entry, &p_tx->active_descq);
  1439. }
  1440. SET_FIELD(db_msg.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
  1441. SET_FIELD(db_msg.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
  1442. SET_FIELD(db_msg.params, CORE_DB_DATA_AGG_VAL_SEL,
  1443. DQ_XCM_CORE_TX_BD_PROD_CMD);
  1444. db_msg.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
  1445. db_msg.spq_prod = cpu_to_le16(bd_prod);
  1446. /* Make sure the BDs data is updated before ringing the doorbell */
  1447. wmb();
  1448. DIRECT_REG_WR(p_tx->doorbell_addr, *((u32 *)&db_msg));
  1449. DP_VERBOSE(p_hwfn,
  1450. (NETIF_MSG_TX_QUEUED | QED_MSG_LL2),
  1451. "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Doorbelled [producer 0x%04x]\n",
  1452. p_ll2_conn->queue_id,
  1453. p_ll2_conn->cid, p_ll2_conn->conn.conn_type, db_msg.spq_prod);
  1454. }
  1455. int qed_ll2_prepare_tx_packet(struct qed_hwfn *p_hwfn,
  1456. u8 connection_handle,
  1457. u8 num_of_bds,
  1458. u16 vlan,
  1459. u8 bd_flags,
  1460. u16 l4_hdr_offset_w,
  1461. enum qed_ll2_tx_dest e_tx_dest,
  1462. enum qed_ll2_roce_flavor_type qed_roce_flavor,
  1463. dma_addr_t first_frag,
  1464. u16 first_frag_len, void *cookie, u8 notify_fw)
  1465. {
  1466. struct qed_ll2_tx_packet *p_curp = NULL;
  1467. struct qed_ll2_info *p_ll2_conn = NULL;
  1468. enum core_roce_flavor_type roce_flavor;
  1469. struct qed_ll2_tx_queue *p_tx;
  1470. struct qed_chain *p_tx_chain;
  1471. enum core_tx_dest tx_dest;
  1472. unsigned long flags;
  1473. int rc = 0;
  1474. p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
  1475. if (!p_ll2_conn)
  1476. return -EINVAL;
  1477. p_tx = &p_ll2_conn->tx_queue;
  1478. p_tx_chain = &p_tx->txq_chain;
  1479. if (num_of_bds > CORE_LL2_TX_MAX_BDS_PER_PACKET)
  1480. return -EIO;
  1481. spin_lock_irqsave(&p_tx->lock, flags);
  1482. if (p_tx->cur_send_packet) {
  1483. rc = -EEXIST;
  1484. goto out;
  1485. }
  1486. /* Get entry, but only if we have tx elements for it */
  1487. if (!list_empty(&p_tx->free_descq))
  1488. p_curp = list_first_entry(&p_tx->free_descq,
  1489. struct qed_ll2_tx_packet, list_entry);
  1490. if (p_curp && qed_chain_get_elem_left(p_tx_chain) < num_of_bds)
  1491. p_curp = NULL;
  1492. if (!p_curp) {
  1493. rc = -EBUSY;
  1494. goto out;
  1495. }
  1496. tx_dest = e_tx_dest == QED_LL2_TX_DEST_NW ? CORE_TX_DEST_NW :
  1497. CORE_TX_DEST_LB;
  1498. if (qed_roce_flavor == QED_LL2_ROCE) {
  1499. roce_flavor = CORE_ROCE;
  1500. } else if (qed_roce_flavor == QED_LL2_RROCE) {
  1501. roce_flavor = CORE_RROCE;
  1502. } else {
  1503. rc = -EINVAL;
  1504. goto out;
  1505. }
  1506. /* Prepare packet and BD, and perhaps send a doorbell to FW */
  1507. qed_ll2_prepare_tx_packet_set(p_hwfn, p_tx, p_curp,
  1508. num_of_bds, first_frag,
  1509. first_frag_len, cookie, notify_fw);
  1510. qed_ll2_prepare_tx_packet_set_bd(p_hwfn, p_ll2_conn, p_curp,
  1511. num_of_bds, tx_dest,
  1512. vlan, bd_flags, l4_hdr_offset_w,
  1513. roce_flavor,
  1514. first_frag, first_frag_len);
  1515. qed_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
  1516. out:
  1517. spin_unlock_irqrestore(&p_tx->lock, flags);
  1518. return rc;
  1519. }
  1520. int qed_ll2_set_fragment_of_tx_packet(struct qed_hwfn *p_hwfn,
  1521. u8 connection_handle,
  1522. dma_addr_t addr, u16 nbytes)
  1523. {
  1524. struct qed_ll2_tx_packet *p_cur_send_packet = NULL;
  1525. struct qed_ll2_info *p_ll2_conn = NULL;
  1526. u16 cur_send_frag_num = 0;
  1527. struct core_tx_bd *p_bd;
  1528. unsigned long flags;
  1529. p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
  1530. if (!p_ll2_conn)
  1531. return -EINVAL;
  1532. if (!p_ll2_conn->tx_queue.cur_send_packet)
  1533. return -EINVAL;
  1534. p_cur_send_packet = p_ll2_conn->tx_queue.cur_send_packet;
  1535. cur_send_frag_num = p_ll2_conn->tx_queue.cur_send_frag_num;
  1536. if (cur_send_frag_num >= p_cur_send_packet->bd_used)
  1537. return -EINVAL;
  1538. /* Fill the BD information, and possibly notify FW */
  1539. p_bd = p_cur_send_packet->bds_set[cur_send_frag_num].txq_bd;
  1540. DMA_REGPAIR_LE(p_bd->addr, addr);
  1541. p_bd->nbytes = cpu_to_le16(nbytes);
  1542. p_cur_send_packet->bds_set[cur_send_frag_num].tx_frag = addr;
  1543. p_cur_send_packet->bds_set[cur_send_frag_num].frag_len = nbytes;
  1544. p_ll2_conn->tx_queue.cur_send_frag_num++;
  1545. spin_lock_irqsave(&p_ll2_conn->tx_queue.lock, flags);
  1546. qed_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
  1547. spin_unlock_irqrestore(&p_ll2_conn->tx_queue.lock, flags);
  1548. return 0;
  1549. }
  1550. int qed_ll2_terminate_connection(struct qed_hwfn *p_hwfn, u8 connection_handle)
  1551. {
  1552. struct qed_ll2_info *p_ll2_conn = NULL;
  1553. int rc = -EINVAL;
  1554. p_ll2_conn = qed_ll2_handle_sanity_lock(p_hwfn, connection_handle);
  1555. if (!p_ll2_conn)
  1556. return -EINVAL;
  1557. /* Stop Tx & Rx of connection, if needed */
  1558. if (QED_LL2_TX_REGISTERED(p_ll2_conn)) {
  1559. rc = qed_sp_ll2_tx_queue_stop(p_hwfn, p_ll2_conn);
  1560. if (rc)
  1561. return rc;
  1562. qed_ll2_txq_flush(p_hwfn, connection_handle);
  1563. }
  1564. if (QED_LL2_RX_REGISTERED(p_ll2_conn)) {
  1565. rc = qed_sp_ll2_rx_queue_stop(p_hwfn, p_ll2_conn);
  1566. if (rc)
  1567. return rc;
  1568. qed_ll2_rxq_flush(p_hwfn, connection_handle);
  1569. }
  1570. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_ISCSI_OOO)
  1571. qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
  1572. if (p_ll2_conn->conn.conn_type == QED_LL2_TYPE_FCOE) {
  1573. qed_llh_remove_protocol_filter(p_hwfn, p_hwfn->p_main_ptt,
  1574. 0x8906, 0,
  1575. QED_LLH_FILTER_ETHERTYPE);
  1576. qed_llh_remove_protocol_filter(p_hwfn, p_hwfn->p_main_ptt,
  1577. 0x8914, 0,
  1578. QED_LLH_FILTER_ETHERTYPE);
  1579. }
  1580. return rc;
  1581. }
  1582. void qed_ll2_release_connection(struct qed_hwfn *p_hwfn, u8 connection_handle)
  1583. {
  1584. struct qed_ll2_info *p_ll2_conn = NULL;
  1585. p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
  1586. if (!p_ll2_conn)
  1587. return;
  1588. if (QED_LL2_RX_REGISTERED(p_ll2_conn)) {
  1589. p_ll2_conn->rx_queue.b_cb_registred = false;
  1590. qed_int_unregister_cb(p_hwfn, p_ll2_conn->rx_queue.rx_sb_index);
  1591. }
  1592. if (QED_LL2_TX_REGISTERED(p_ll2_conn)) {
  1593. p_ll2_conn->tx_queue.b_cb_registred = false;
  1594. qed_int_unregister_cb(p_hwfn, p_ll2_conn->tx_queue.tx_sb_index);
  1595. }
  1596. kfree(p_ll2_conn->tx_queue.descq_array);
  1597. qed_chain_free(p_hwfn->cdev, &p_ll2_conn->tx_queue.txq_chain);
  1598. kfree(p_ll2_conn->rx_queue.descq_array);
  1599. qed_chain_free(p_hwfn->cdev, &p_ll2_conn->rx_queue.rxq_chain);
  1600. qed_chain_free(p_hwfn->cdev, &p_ll2_conn->rx_queue.rcq_chain);
  1601. qed_cxt_release_cid(p_hwfn, p_ll2_conn->cid);
  1602. qed_ll2_release_connection_ooo(p_hwfn, p_ll2_conn);
  1603. mutex_lock(&p_ll2_conn->mutex);
  1604. p_ll2_conn->b_active = false;
  1605. mutex_unlock(&p_ll2_conn->mutex);
  1606. }
  1607. struct qed_ll2_info *qed_ll2_alloc(struct qed_hwfn *p_hwfn)
  1608. {
  1609. struct qed_ll2_info *p_ll2_connections;
  1610. u8 i;
  1611. /* Allocate LL2's set struct */
  1612. p_ll2_connections = kcalloc(QED_MAX_NUM_OF_LL2_CONNECTIONS,
  1613. sizeof(struct qed_ll2_info), GFP_KERNEL);
  1614. if (!p_ll2_connections) {
  1615. DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_ll2'\n");
  1616. return NULL;
  1617. }
  1618. for (i = 0; i < QED_MAX_NUM_OF_LL2_CONNECTIONS; i++)
  1619. p_ll2_connections[i].my_id = i;
  1620. return p_ll2_connections;
  1621. }
  1622. void qed_ll2_setup(struct qed_hwfn *p_hwfn,
  1623. struct qed_ll2_info *p_ll2_connections)
  1624. {
  1625. int i;
  1626. for (i = 0; i < QED_MAX_NUM_OF_LL2_CONNECTIONS; i++)
  1627. mutex_init(&p_ll2_connections[i].mutex);
  1628. }
  1629. void qed_ll2_free(struct qed_hwfn *p_hwfn,
  1630. struct qed_ll2_info *p_ll2_connections)
  1631. {
  1632. kfree(p_ll2_connections);
  1633. }
  1634. static void _qed_ll2_get_tstats(struct qed_hwfn *p_hwfn,
  1635. struct qed_ptt *p_ptt,
  1636. struct qed_ll2_info *p_ll2_conn,
  1637. struct qed_ll2_stats *p_stats)
  1638. {
  1639. struct core_ll2_tstorm_per_queue_stat tstats;
  1640. u8 qid = p_ll2_conn->queue_id;
  1641. u32 tstats_addr;
  1642. memset(&tstats, 0, sizeof(tstats));
  1643. tstats_addr = BAR0_MAP_REG_TSDM_RAM +
  1644. CORE_LL2_TSTORM_PER_QUEUE_STAT_OFFSET(qid);
  1645. qed_memcpy_from(p_hwfn, p_ptt, &tstats, tstats_addr, sizeof(tstats));
  1646. p_stats->packet_too_big_discard =
  1647. HILO_64_REGPAIR(tstats.packet_too_big_discard);
  1648. p_stats->no_buff_discard = HILO_64_REGPAIR(tstats.no_buff_discard);
  1649. }
  1650. static void _qed_ll2_get_ustats(struct qed_hwfn *p_hwfn,
  1651. struct qed_ptt *p_ptt,
  1652. struct qed_ll2_info *p_ll2_conn,
  1653. struct qed_ll2_stats *p_stats)
  1654. {
  1655. struct core_ll2_ustorm_per_queue_stat ustats;
  1656. u8 qid = p_ll2_conn->queue_id;
  1657. u32 ustats_addr;
  1658. memset(&ustats, 0, sizeof(ustats));
  1659. ustats_addr = BAR0_MAP_REG_USDM_RAM +
  1660. CORE_LL2_USTORM_PER_QUEUE_STAT_OFFSET(qid);
  1661. qed_memcpy_from(p_hwfn, p_ptt, &ustats, ustats_addr, sizeof(ustats));
  1662. p_stats->rcv_ucast_bytes = HILO_64_REGPAIR(ustats.rcv_ucast_bytes);
  1663. p_stats->rcv_mcast_bytes = HILO_64_REGPAIR(ustats.rcv_mcast_bytes);
  1664. p_stats->rcv_bcast_bytes = HILO_64_REGPAIR(ustats.rcv_bcast_bytes);
  1665. p_stats->rcv_ucast_pkts = HILO_64_REGPAIR(ustats.rcv_ucast_pkts);
  1666. p_stats->rcv_mcast_pkts = HILO_64_REGPAIR(ustats.rcv_mcast_pkts);
  1667. p_stats->rcv_bcast_pkts = HILO_64_REGPAIR(ustats.rcv_bcast_pkts);
  1668. }
  1669. static void _qed_ll2_get_pstats(struct qed_hwfn *p_hwfn,
  1670. struct qed_ptt *p_ptt,
  1671. struct qed_ll2_info *p_ll2_conn,
  1672. struct qed_ll2_stats *p_stats)
  1673. {
  1674. struct core_ll2_pstorm_per_queue_stat pstats;
  1675. u8 stats_id = p_ll2_conn->tx_stats_id;
  1676. u32 pstats_addr;
  1677. memset(&pstats, 0, sizeof(pstats));
  1678. pstats_addr = BAR0_MAP_REG_PSDM_RAM +
  1679. CORE_LL2_PSTORM_PER_QUEUE_STAT_OFFSET(stats_id);
  1680. qed_memcpy_from(p_hwfn, p_ptt, &pstats, pstats_addr, sizeof(pstats));
  1681. p_stats->sent_ucast_bytes = HILO_64_REGPAIR(pstats.sent_ucast_bytes);
  1682. p_stats->sent_mcast_bytes = HILO_64_REGPAIR(pstats.sent_mcast_bytes);
  1683. p_stats->sent_bcast_bytes = HILO_64_REGPAIR(pstats.sent_bcast_bytes);
  1684. p_stats->sent_ucast_pkts = HILO_64_REGPAIR(pstats.sent_ucast_pkts);
  1685. p_stats->sent_mcast_pkts = HILO_64_REGPAIR(pstats.sent_mcast_pkts);
  1686. p_stats->sent_bcast_pkts = HILO_64_REGPAIR(pstats.sent_bcast_pkts);
  1687. }
  1688. int qed_ll2_get_stats(struct qed_hwfn *p_hwfn,
  1689. u8 connection_handle, struct qed_ll2_stats *p_stats)
  1690. {
  1691. struct qed_ll2_info *p_ll2_conn = NULL;
  1692. struct qed_ptt *p_ptt;
  1693. memset(p_stats, 0, sizeof(*p_stats));
  1694. if ((connection_handle >= QED_MAX_NUM_OF_LL2_CONNECTIONS) ||
  1695. !p_hwfn->p_ll2_info)
  1696. return -EINVAL;
  1697. p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
  1698. p_ptt = qed_ptt_acquire(p_hwfn);
  1699. if (!p_ptt) {
  1700. DP_ERR(p_hwfn, "Failed to acquire ptt\n");
  1701. return -EINVAL;
  1702. }
  1703. _qed_ll2_get_tstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
  1704. _qed_ll2_get_ustats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
  1705. if (p_ll2_conn->tx_stats_en)
  1706. _qed_ll2_get_pstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
  1707. qed_ptt_release(p_hwfn, p_ptt);
  1708. return 0;
  1709. }
  1710. static void qed_ll2_register_cb_ops(struct qed_dev *cdev,
  1711. const struct qed_ll2_cb_ops *ops,
  1712. void *cookie)
  1713. {
  1714. cdev->ll2->cbs = ops;
  1715. cdev->ll2->cb_cookie = cookie;
  1716. }
  1717. static int qed_ll2_start(struct qed_dev *cdev, struct qed_ll2_params *params)
  1718. {
  1719. struct qed_ll2_conn ll2_info;
  1720. struct qed_ll2_buffer *buffer, *tmp_buffer;
  1721. enum qed_ll2_conn_type conn_type;
  1722. struct qed_ptt *p_ptt;
  1723. int rc, i;
  1724. u8 gsi_enable = 1;
  1725. /* Initialize LL2 locks & lists */
  1726. INIT_LIST_HEAD(&cdev->ll2->list);
  1727. spin_lock_init(&cdev->ll2->lock);
  1728. cdev->ll2->rx_size = NET_SKB_PAD + ETH_HLEN +
  1729. L1_CACHE_BYTES + params->mtu;
  1730. cdev->ll2->frags_mapped = params->frags_mapped;
  1731. /*Allocate memory for LL2 */
  1732. DP_INFO(cdev, "Allocating LL2 buffers of size %08x bytes\n",
  1733. cdev->ll2->rx_size);
  1734. for (i = 0; i < QED_LL2_RX_SIZE; i++) {
  1735. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  1736. if (!buffer) {
  1737. DP_INFO(cdev, "Failed to allocate LL2 buffers\n");
  1738. goto fail;
  1739. }
  1740. rc = qed_ll2_alloc_buffer(cdev, (u8 **)&buffer->data,
  1741. &buffer->phys_addr);
  1742. if (rc) {
  1743. kfree(buffer);
  1744. goto fail;
  1745. }
  1746. list_add_tail(&buffer->list, &cdev->ll2->list);
  1747. }
  1748. switch (QED_LEADING_HWFN(cdev)->hw_info.personality) {
  1749. case QED_PCI_FCOE:
  1750. conn_type = QED_LL2_TYPE_FCOE;
  1751. gsi_enable = 0;
  1752. break;
  1753. case QED_PCI_ISCSI:
  1754. conn_type = QED_LL2_TYPE_ISCSI;
  1755. gsi_enable = 0;
  1756. break;
  1757. case QED_PCI_ETH_ROCE:
  1758. conn_type = QED_LL2_TYPE_ROCE;
  1759. break;
  1760. default:
  1761. conn_type = QED_LL2_TYPE_TEST;
  1762. }
  1763. /* Prepare the temporary ll2 information */
  1764. memset(&ll2_info, 0, sizeof(ll2_info));
  1765. ll2_info.conn_type = conn_type;
  1766. ll2_info.mtu = params->mtu;
  1767. ll2_info.rx_drop_ttl0_flg = params->drop_ttl0_packets;
  1768. ll2_info.rx_vlan_removal_en = params->rx_vlan_stripping;
  1769. ll2_info.tx_tc = 0;
  1770. ll2_info.tx_dest = CORE_TX_DEST_NW;
  1771. ll2_info.gsi_enable = gsi_enable;
  1772. rc = qed_ll2_acquire_connection(QED_LEADING_HWFN(cdev), &ll2_info,
  1773. QED_LL2_RX_SIZE, QED_LL2_TX_SIZE,
  1774. &cdev->ll2->handle);
  1775. if (rc) {
  1776. DP_INFO(cdev, "Failed to acquire LL2 connection\n");
  1777. goto fail;
  1778. }
  1779. rc = qed_ll2_establish_connection(QED_LEADING_HWFN(cdev),
  1780. cdev->ll2->handle);
  1781. if (rc) {
  1782. DP_INFO(cdev, "Failed to establish LL2 connection\n");
  1783. goto release_fail;
  1784. }
  1785. /* Post all Rx buffers to FW */
  1786. spin_lock_bh(&cdev->ll2->lock);
  1787. list_for_each_entry_safe(buffer, tmp_buffer, &cdev->ll2->list, list) {
  1788. rc = qed_ll2_post_rx_buffer(QED_LEADING_HWFN(cdev),
  1789. cdev->ll2->handle,
  1790. buffer->phys_addr, 0, buffer, 1);
  1791. if (rc) {
  1792. DP_INFO(cdev,
  1793. "Failed to post an Rx buffer; Deleting it\n");
  1794. dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
  1795. cdev->ll2->rx_size, DMA_FROM_DEVICE);
  1796. kfree(buffer->data);
  1797. list_del(&buffer->list);
  1798. kfree(buffer);
  1799. } else {
  1800. cdev->ll2->rx_cnt++;
  1801. }
  1802. }
  1803. spin_unlock_bh(&cdev->ll2->lock);
  1804. if (!cdev->ll2->rx_cnt) {
  1805. DP_INFO(cdev, "Failed passing even a single Rx buffer\n");
  1806. goto release_terminate;
  1807. }
  1808. if (!is_valid_ether_addr(params->ll2_mac_address)) {
  1809. DP_INFO(cdev, "Invalid Ethernet address\n");
  1810. goto release_terminate;
  1811. }
  1812. if (cdev->hwfns[0].hw_info.personality == QED_PCI_ISCSI &&
  1813. cdev->hwfns[0].pf_params.iscsi_pf_params.ooo_enable) {
  1814. DP_VERBOSE(cdev, QED_MSG_STORAGE, "Starting OOO LL2 queue\n");
  1815. rc = qed_ll2_start_ooo(cdev, params);
  1816. if (rc) {
  1817. DP_INFO(cdev,
  1818. "Failed to initialize the OOO LL2 queue\n");
  1819. goto release_terminate;
  1820. }
  1821. }
  1822. p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  1823. if (!p_ptt) {
  1824. DP_INFO(cdev, "Failed to acquire PTT\n");
  1825. goto release_terminate;
  1826. }
  1827. rc = qed_llh_add_mac_filter(QED_LEADING_HWFN(cdev), p_ptt,
  1828. params->ll2_mac_address);
  1829. qed_ptt_release(QED_LEADING_HWFN(cdev), p_ptt);
  1830. if (rc) {
  1831. DP_ERR(cdev, "Failed to allocate LLH filter\n");
  1832. goto release_terminate_all;
  1833. }
  1834. ether_addr_copy(cdev->ll2_mac_address, params->ll2_mac_address);
  1835. return 0;
  1836. release_terminate_all:
  1837. release_terminate:
  1838. qed_ll2_terminate_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
  1839. release_fail:
  1840. qed_ll2_release_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
  1841. fail:
  1842. qed_ll2_kill_buffers(cdev);
  1843. cdev->ll2->handle = QED_LL2_UNUSED_HANDLE;
  1844. return -EINVAL;
  1845. }
  1846. static int qed_ll2_stop(struct qed_dev *cdev)
  1847. {
  1848. struct qed_ptt *p_ptt;
  1849. int rc;
  1850. if (cdev->ll2->handle == QED_LL2_UNUSED_HANDLE)
  1851. return 0;
  1852. p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  1853. if (!p_ptt) {
  1854. DP_INFO(cdev, "Failed to acquire PTT\n");
  1855. goto fail;
  1856. }
  1857. qed_llh_remove_mac_filter(QED_LEADING_HWFN(cdev), p_ptt,
  1858. cdev->ll2_mac_address);
  1859. qed_ptt_release(QED_LEADING_HWFN(cdev), p_ptt);
  1860. eth_zero_addr(cdev->ll2_mac_address);
  1861. if (cdev->hwfns[0].hw_info.personality == QED_PCI_ISCSI &&
  1862. cdev->hwfns[0].pf_params.iscsi_pf_params.ooo_enable)
  1863. qed_ll2_stop_ooo(cdev);
  1864. rc = qed_ll2_terminate_connection(QED_LEADING_HWFN(cdev),
  1865. cdev->ll2->handle);
  1866. if (rc)
  1867. DP_INFO(cdev, "Failed to terminate LL2 connection\n");
  1868. qed_ll2_kill_buffers(cdev);
  1869. qed_ll2_release_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
  1870. cdev->ll2->handle = QED_LL2_UNUSED_HANDLE;
  1871. return rc;
  1872. fail:
  1873. return -EINVAL;
  1874. }
  1875. static int qed_ll2_start_xmit(struct qed_dev *cdev, struct sk_buff *skb)
  1876. {
  1877. const skb_frag_t *frag;
  1878. int rc = -EINVAL, i;
  1879. dma_addr_t mapping;
  1880. u16 vlan = 0;
  1881. u8 flags = 0;
  1882. if (unlikely(skb->ip_summed != CHECKSUM_NONE)) {
  1883. DP_INFO(cdev, "Cannot transmit a checksumed packet\n");
  1884. return -EINVAL;
  1885. }
  1886. if (1 + skb_shinfo(skb)->nr_frags > CORE_LL2_TX_MAX_BDS_PER_PACKET) {
  1887. DP_ERR(cdev, "Cannot transmit a packet with %d fragments\n",
  1888. 1 + skb_shinfo(skb)->nr_frags);
  1889. return -EINVAL;
  1890. }
  1891. mapping = dma_map_single(&cdev->pdev->dev, skb->data,
  1892. skb->len, DMA_TO_DEVICE);
  1893. if (unlikely(dma_mapping_error(&cdev->pdev->dev, mapping))) {
  1894. DP_NOTICE(cdev, "SKB mapping failed\n");
  1895. return -EINVAL;
  1896. }
  1897. /* Request HW to calculate IP csum */
  1898. if (!((vlan_get_protocol(skb) == htons(ETH_P_IPV6)) &&
  1899. ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
  1900. flags |= BIT(CORE_TX_BD_FLAGS_IP_CSUM_SHIFT);
  1901. if (skb_vlan_tag_present(skb)) {
  1902. vlan = skb_vlan_tag_get(skb);
  1903. flags |= BIT(CORE_TX_BD_FLAGS_VLAN_INSERTION_SHIFT);
  1904. }
  1905. rc = qed_ll2_prepare_tx_packet(QED_LEADING_HWFN(cdev),
  1906. cdev->ll2->handle,
  1907. 1 + skb_shinfo(skb)->nr_frags,
  1908. vlan, flags, 0, QED_LL2_TX_DEST_NW,
  1909. 0 /* RoCE FLAVOR */,
  1910. mapping, skb->len, skb, 1);
  1911. if (rc)
  1912. goto err;
  1913. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1914. frag = &skb_shinfo(skb)->frags[i];
  1915. if (!cdev->ll2->frags_mapped) {
  1916. mapping = skb_frag_dma_map(&cdev->pdev->dev, frag, 0,
  1917. skb_frag_size(frag),
  1918. DMA_TO_DEVICE);
  1919. if (unlikely(dma_mapping_error(&cdev->pdev->dev,
  1920. mapping))) {
  1921. DP_NOTICE(cdev,
  1922. "Unable to map frag - dropping packet\n");
  1923. rc = -ENOMEM;
  1924. goto err;
  1925. }
  1926. } else {
  1927. mapping = page_to_phys(skb_frag_page(frag)) |
  1928. frag->page_offset;
  1929. }
  1930. rc = qed_ll2_set_fragment_of_tx_packet(QED_LEADING_HWFN(cdev),
  1931. cdev->ll2->handle,
  1932. mapping,
  1933. skb_frag_size(frag));
  1934. /* if failed not much to do here, partial packet has been posted
  1935. * we can't free memory, will need to wait for completion.
  1936. */
  1937. if (rc)
  1938. goto err2;
  1939. }
  1940. return 0;
  1941. err:
  1942. dma_unmap_single(&cdev->pdev->dev, mapping, skb->len, DMA_TO_DEVICE);
  1943. err2:
  1944. return rc;
  1945. }
  1946. static int qed_ll2_stats(struct qed_dev *cdev, struct qed_ll2_stats *stats)
  1947. {
  1948. if (!cdev->ll2)
  1949. return -EINVAL;
  1950. return qed_ll2_get_stats(QED_LEADING_HWFN(cdev),
  1951. cdev->ll2->handle, stats);
  1952. }
  1953. const struct qed_ll2_ops qed_ll2_ops_pass = {
  1954. .start = &qed_ll2_start,
  1955. .stop = &qed_ll2_stop,
  1956. .start_xmit = &qed_ll2_start_xmit,
  1957. .register_cb_ops = &qed_ll2_register_cb_ops,
  1958. .get_stats = &qed_ll2_stats,
  1959. };
  1960. int qed_ll2_alloc_if(struct qed_dev *cdev)
  1961. {
  1962. cdev->ll2 = kzalloc(sizeof(*cdev->ll2), GFP_KERNEL);
  1963. return cdev->ll2 ? 0 : -ENOMEM;
  1964. }
  1965. void qed_ll2_dealloc_if(struct qed_dev *cdev)
  1966. {
  1967. kfree(cdev->ll2);
  1968. cdev->ll2 = NULL;
  1969. }