]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/qlxgbe/ql_isr.c
MFC r316309
[FreeBSD/stable/10.git] / sys / dev / qlxgbe / ql_isr.c
1 /*
2  * Copyright (c) 2013-2016 Qlogic Corporation
3  * All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  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  *
15  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /*
29  * File: ql_isr.c
30  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36
37 #include "ql_os.h"
38 #include "ql_hw.h"
39 #include "ql_def.h"
40 #include "ql_inline.h"
41 #include "ql_ver.h"
42 #include "ql_glbl.h"
43 #include "ql_dbg.h"
44
45 static void qla_replenish_normal_rx(qla_host_t *ha, qla_sds_t *sdsp,
46                 uint32_t r_idx);
47
48 static void
49 qla_rcv_error(qla_host_t *ha)
50 {
51         ha->flags.stop_rcv = 1;
52         ha->qla_initiate_recovery = 1;
53 }
54
55
56 /*
57  * Name: qla_rx_intr
58  * Function: Handles normal ethernet frames received
59  */
60 static void
61 qla_rx_intr(qla_host_t *ha, qla_sgl_rcv_t *sgc, uint32_t sds_idx)
62 {
63         qla_rx_buf_t            *rxb;
64         struct mbuf             *mp = NULL, *mpf = NULL, *mpl = NULL;
65         struct ifnet            *ifp = ha->ifp;
66         qla_sds_t               *sdsp;
67         struct ether_vlan_header *eh;
68         uint32_t                i, rem_len = 0;
69         uint32_t                r_idx = 0;
70         qla_rx_ring_t           *rx_ring;
71         struct lro_ctrl         *lro;
72
73         lro = &ha->hw.sds[sds_idx].lro;
74
75         if (ha->hw.num_rds_rings > 1)
76                 r_idx = sds_idx;
77         
78         ha->hw.rds[r_idx].count++;
79
80         sdsp = &ha->hw.sds[sds_idx];
81         rx_ring = &ha->rx_ring[r_idx];
82         
83         for (i = 0; i < sgc->num_handles; i++) {
84                 rxb = &rx_ring->rx_buf[sgc->handle[i] & 0x7FFF];
85
86                 QL_ASSERT(ha, (rxb != NULL),
87                         ("%s: [sds_idx]=[%d] rxb != NULL\n", __func__,\
88                         sds_idx));
89
90                 if ((rxb == NULL) || QL_ERR_INJECT(ha, INJCT_RX_RXB_INVAL)) {
91                         /* log the error */
92                         device_printf(ha->pci_dev,
93                                 "%s invalid rxb[%d, %d, 0x%04x]\n",
94                                 __func__, sds_idx, i, sgc->handle[i]);
95                         qla_rcv_error(ha);
96                         return;
97                 }
98
99                 mp = rxb->m_head;
100                 if (i == 0) 
101                         mpf = mp;
102
103                 QL_ASSERT(ha, (mp != NULL),
104                         ("%s: [sds_idx]=[%d] mp != NULL\n", __func__,\
105                         sds_idx));
106
107                 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_POSTREAD);
108
109                 rxb->m_head = NULL;
110                 rxb->next = sdsp->rxb_free;
111                 sdsp->rxb_free = rxb;
112                 sdsp->rx_free++;
113         
114                 if ((mp == NULL) || QL_ERR_INJECT(ha, INJCT_RX_MP_NULL)) {
115                         /* log the error */
116                         device_printf(ha->pci_dev,
117                                 "%s mp  == NULL [%d, %d, 0x%04x]\n",
118                                 __func__, sds_idx, i, sgc->handle[i]);
119                         qla_rcv_error(ha);
120                         return;
121                 }
122
123                 if (i == 0) {
124                         mpl = mpf = mp;
125                         mp->m_flags |= M_PKTHDR;
126                         mp->m_pkthdr.len = sgc->pkt_length;
127                         mp->m_pkthdr.rcvif = ifp;
128                         rem_len = mp->m_pkthdr.len;
129                 } else {
130                         mp->m_flags &= ~M_PKTHDR;
131                         mpl->m_next = mp;
132                         mpl = mp;
133                         rem_len = rem_len - mp->m_len;
134                 }
135         }
136
137         mpl->m_len = rem_len;
138
139         eh = mtod(mpf, struct ether_vlan_header *);
140
141         if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
142                 uint32_t *data = (uint32_t *)eh;
143
144                 mpf->m_pkthdr.ether_vtag = ntohs(eh->evl_tag);
145                 mpf->m_flags |= M_VLANTAG;
146
147                 *(data + 3) = *(data + 2);
148                 *(data + 2) = *(data + 1);
149                 *(data + 1) = *data;
150
151                 m_adj(mpf, ETHER_VLAN_ENCAP_LEN);
152         }
153
154         if (sgc->chksum_status == Q8_STAT_DESC_STATUS_CHKSUM_OK) {
155                 mpf->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
156                         CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
157                 mpf->m_pkthdr.csum_data = 0xFFFF;
158         } else {
159                 mpf->m_pkthdr.csum_flags = 0;
160         }
161
162         ifp->if_ipackets++;
163
164         mpf->m_pkthdr.flowid = sgc->rss_hash;
165
166 #if __FreeBSD_version >= 1100000
167         M_HASHTYPE_SET(mpf, M_HASHTYPE_OPAQUE_HASH);
168 #else
169         M_HASHTYPE_SET(mpf, M_HASHTYPE_NONE);
170 #endif /* #if __FreeBSD_version >= 1100000 */
171
172         if (ha->hw.enable_soft_lro) {
173
174 #if (__FreeBSD_version >= 1100101)
175
176                 tcp_lro_queue_mbuf(lro, mpf);
177
178 #else
179                 if (tcp_lro_rx(lro, mpf, 0))
180                         (*ifp->if_input)(ifp, mpf);
181
182 #endif /* #if (__FreeBSD_version >= 1100101) */
183
184
185         } else {
186                 (*ifp->if_input)(ifp, mpf);
187         }
188
189         if (sdsp->rx_free > ha->std_replenish)
190                 qla_replenish_normal_rx(ha, sdsp, r_idx);
191
192         return;
193 }
194
195 #define QLA_TCP_HDR_SIZE        20
196 #define QLA_TCP_TS_OPTION_SIZE  12
197
198 /*
199  * Name: qla_lro_intr
200  * Function: Handles normal ethernet frames received
201  */
202 static int
203 qla_lro_intr(qla_host_t *ha, qla_sgl_lro_t *sgc, uint32_t sds_idx)
204 {
205         qla_rx_buf_t *rxb;
206         struct mbuf *mp = NULL, *mpf = NULL, *mpl = NULL;
207         struct ifnet *ifp = ha->ifp;
208         qla_sds_t *sdsp;
209         struct ether_vlan_header *eh;
210         uint32_t i, rem_len = 0, pkt_length, iplen;
211         struct tcphdr *th;
212         struct ip *ip = NULL;
213         struct ip6_hdr *ip6 = NULL;
214         uint16_t etype;
215         uint32_t r_idx = 0;
216         qla_rx_ring_t *rx_ring;
217
218         if (ha->hw.num_rds_rings > 1)
219                 r_idx = sds_idx;
220
221         ha->hw.rds[r_idx].count++;
222
223         rx_ring = &ha->rx_ring[r_idx];
224         
225         ha->lro_pkt_count++;
226
227         sdsp = &ha->hw.sds[sds_idx];
228         
229         pkt_length = sgc->payload_length + sgc->l4_offset;
230
231         if (sgc->flags & Q8_LRO_COMP_TS) {
232                 pkt_length += QLA_TCP_HDR_SIZE + QLA_TCP_TS_OPTION_SIZE;
233         } else {
234                 pkt_length += QLA_TCP_HDR_SIZE;
235         }
236         ha->lro_bytes += pkt_length;
237
238         for (i = 0; i < sgc->num_handles; i++) {
239                 rxb = &rx_ring->rx_buf[sgc->handle[i] & 0x7FFF];
240
241                 QL_ASSERT(ha, (rxb != NULL),
242                         ("%s: [sds_idx]=[%d] rxb != NULL\n", __func__,\
243                         sds_idx));
244
245                 if ((rxb == NULL) || QL_ERR_INJECT(ha, INJCT_LRO_RXB_INVAL)) {
246                         /* log the error */
247                         device_printf(ha->pci_dev,
248                                 "%s invalid rxb[%d, %d, 0x%04x]\n",
249                                 __func__, sds_idx, i, sgc->handle[i]);
250                         qla_rcv_error(ha);
251                         return (0);
252                 }
253
254                 mp = rxb->m_head;
255                 if (i == 0) 
256                         mpf = mp;
257
258                 QL_ASSERT(ha, (mp != NULL),
259                         ("%s: [sds_idx]=[%d] mp != NULL\n", __func__,\
260                         sds_idx));
261
262                 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_POSTREAD);
263
264                 rxb->m_head = NULL;
265                 rxb->next = sdsp->rxb_free;
266                 sdsp->rxb_free = rxb;
267                 sdsp->rx_free++;
268         
269                 if ((mp == NULL) || QL_ERR_INJECT(ha, INJCT_LRO_MP_NULL)) {
270                         /* log the error */
271                         device_printf(ha->pci_dev,
272                                 "%s mp  == NULL [%d, %d, 0x%04x]\n",
273                                 __func__, sds_idx, i, sgc->handle[i]);
274                         qla_rcv_error(ha);
275                         return (0);
276                 }
277
278                 if (i == 0) {
279                         mpl = mpf = mp;
280                         mp->m_flags |= M_PKTHDR;
281                         mp->m_pkthdr.len = pkt_length;
282                         mp->m_pkthdr.rcvif = ifp;
283                         rem_len = mp->m_pkthdr.len;
284                 } else {
285                         mp->m_flags &= ~M_PKTHDR;
286                         mpl->m_next = mp;
287                         mpl = mp;
288                         rem_len = rem_len - mp->m_len;
289                 }
290         }
291
292         mpl->m_len = rem_len;
293
294         th = (struct tcphdr *)(mpf->m_data + sgc->l4_offset);
295
296         if (sgc->flags & Q8_LRO_COMP_PUSH_BIT)
297                 th->th_flags |= TH_PUSH;
298
299         m_adj(mpf, sgc->l2_offset);
300
301         eh = mtod(mpf, struct ether_vlan_header *);
302
303         if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
304                 uint32_t *data = (uint32_t *)eh;
305
306                 mpf->m_pkthdr.ether_vtag = ntohs(eh->evl_tag);
307                 mpf->m_flags |= M_VLANTAG;
308
309                 *(data + 3) = *(data + 2);
310                 *(data + 2) = *(data + 1);
311                 *(data + 1) = *data;
312
313                 m_adj(mpf, ETHER_VLAN_ENCAP_LEN);
314
315                 etype = ntohs(eh->evl_proto);
316         } else {
317                 etype = ntohs(eh->evl_encap_proto);
318         }
319
320         if (etype == ETHERTYPE_IP) {
321                 ip = (struct ip *)(mpf->m_data + ETHER_HDR_LEN);
322         
323                 iplen = (ip->ip_hl << 2) + (th->th_off << 2) +
324                                 sgc->payload_length;
325
326                 ip->ip_len = htons(iplen);
327
328                 ha->ipv4_lro++;
329
330                 M_HASHTYPE_SET(mpf, M_HASHTYPE_RSS_TCP_IPV4);
331
332         } else if (etype == ETHERTYPE_IPV6) {
333                 ip6 = (struct ip6_hdr *)(mpf->m_data + ETHER_HDR_LEN);
334
335                 iplen = (th->th_off << 2) + sgc->payload_length;
336
337                 ip6->ip6_plen = htons(iplen);
338
339                 ha->ipv6_lro++;
340
341                 M_HASHTYPE_SET(mpf, M_HASHTYPE_RSS_TCP_IPV6);
342
343         } else {
344                 m_freem(mpf);
345
346                 if (sdsp->rx_free > ha->std_replenish)
347                         qla_replenish_normal_rx(ha, sdsp, r_idx);
348                 return 0;
349         }
350
351         mpf->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
352                                         CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
353         mpf->m_pkthdr.csum_data = 0xFFFF;
354
355         mpf->m_pkthdr.flowid = sgc->rss_hash;
356         M_HASHTYPE_SET(mpf, M_HASHTYPE_OPAQUE);
357
358         ifp->if_ipackets++;
359
360         (*ifp->if_input)(ifp, mpf);
361
362         if (sdsp->rx_free > ha->std_replenish)
363                 qla_replenish_normal_rx(ha, sdsp, r_idx);
364
365         return (0);
366 }
367
368 static int
369 qla_rcv_cont_sds(qla_host_t *ha, uint32_t sds_idx, uint32_t comp_idx,
370         uint32_t dcount, uint16_t *handle, uint16_t *nhandles)
371 {
372         uint32_t i;
373         uint16_t num_handles;
374         q80_stat_desc_t *sdesc;
375         uint32_t opcode;
376
377         *nhandles = 0;
378         dcount--;
379
380         for (i = 0; i < dcount; i++) {
381                 comp_idx = (comp_idx + 1) & (NUM_STATUS_DESCRIPTORS-1);
382                 sdesc = (q80_stat_desc_t *)
383                                 &ha->hw.sds[sds_idx].sds_ring_base[comp_idx];
384
385                 opcode = Q8_STAT_DESC_OPCODE((sdesc->data[1]));
386
387                 if (!opcode) {
388                         device_printf(ha->pci_dev, "%s: opcode=0 %p %p\n",
389                                 __func__, (void *)sdesc->data[0],
390                                 (void *)sdesc->data[1]);
391                         return -1;
392                 }
393
394                 num_handles = Q8_SGL_STAT_DESC_NUM_HANDLES((sdesc->data[1]));
395                 if (!num_handles) {
396                         device_printf(ha->pci_dev, "%s: opcode=0 %p %p\n",
397                                 __func__, (void *)sdesc->data[0],
398                                 (void *)sdesc->data[1]);
399                         return -1;
400                 }
401
402                 if (QL_ERR_INJECT(ha, INJCT_NUM_HNDLE_INVALID))
403                         num_handles = -1;
404
405                 switch (num_handles) {
406
407                 case 1:
408                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
409                         break;
410
411                 case 2:
412                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
413                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
414                         break;
415
416                 case 3:
417                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
418                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
419                         *handle++ = Q8_SGL_STAT_DESC_HANDLE3((sdesc->data[0]));
420                         break;
421
422                 case 4:
423                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
424                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
425                         *handle++ = Q8_SGL_STAT_DESC_HANDLE3((sdesc->data[0]));
426                         *handle++ = Q8_SGL_STAT_DESC_HANDLE4((sdesc->data[0]));
427                         break;
428
429                 case 5:
430                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
431                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
432                         *handle++ = Q8_SGL_STAT_DESC_HANDLE3((sdesc->data[0]));
433                         *handle++ = Q8_SGL_STAT_DESC_HANDLE4((sdesc->data[0]));
434                         *handle++ = Q8_SGL_STAT_DESC_HANDLE5((sdesc->data[1]));
435                         break;
436
437                 case 6:
438                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
439                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
440                         *handle++ = Q8_SGL_STAT_DESC_HANDLE3((sdesc->data[0]));
441                         *handle++ = Q8_SGL_STAT_DESC_HANDLE4((sdesc->data[0]));
442                         *handle++ = Q8_SGL_STAT_DESC_HANDLE5((sdesc->data[1]));
443                         *handle++ = Q8_SGL_STAT_DESC_HANDLE6((sdesc->data[1]));
444                         break;
445
446                 case 7:
447                         *handle++ = Q8_SGL_STAT_DESC_HANDLE1((sdesc->data[0]));
448                         *handle++ = Q8_SGL_STAT_DESC_HANDLE2((sdesc->data[0]));
449                         *handle++ = Q8_SGL_STAT_DESC_HANDLE3((sdesc->data[0]));
450                         *handle++ = Q8_SGL_STAT_DESC_HANDLE4((sdesc->data[0]));
451                         *handle++ = Q8_SGL_STAT_DESC_HANDLE5((sdesc->data[1]));
452                         *handle++ = Q8_SGL_STAT_DESC_HANDLE6((sdesc->data[1]));
453                         *handle++ = Q8_SGL_STAT_DESC_HANDLE7((sdesc->data[1]));
454                         break;
455
456                 default:
457                         device_printf(ha->pci_dev,
458                                 "%s: invalid num handles %p %p\n",
459                                 __func__, (void *)sdesc->data[0],
460                                 (void *)sdesc->data[1]);
461
462                         QL_ASSERT(ha, (0),\
463                         ("%s: %s [nh, sds, d0, d1]=[%d, %d, %p, %p]\n",
464                         __func__, "invalid num handles", sds_idx, num_handles,
465                         (void *)sdesc->data[0],(void *)sdesc->data[1]));
466
467                         qla_rcv_error(ha);
468                         return 0;
469                 }
470                 *nhandles = *nhandles + num_handles;
471         }
472         return 0;
473 }
474
475 /*
476  * Name: ql_rcv_isr
477  * Function: Main Interrupt Service Routine
478  */
479 uint32_t
480 ql_rcv_isr(qla_host_t *ha, uint32_t sds_idx, uint32_t count)
481 {
482         device_t dev;
483         qla_hw_t *hw;
484         uint32_t comp_idx, c_idx = 0, desc_count = 0, opcode;
485         volatile q80_stat_desc_t *sdesc, *sdesc0 = NULL;
486         uint32_t ret = 0;
487         qla_sgl_comp_t sgc;
488         uint16_t nhandles;
489         uint32_t sds_replenish_threshold = 0;
490         uint32_t r_idx = 0;
491         qla_sds_t *sdsp;
492
493         dev = ha->pci_dev;
494         hw = &ha->hw;
495
496         hw->sds[sds_idx].rcv_active = 1;
497         if (ha->flags.stop_rcv) {
498                 hw->sds[sds_idx].rcv_active = 0;
499                 return 0;
500         }
501
502         QL_DPRINT2(ha, (dev, "%s: [%d]enter\n", __func__, sds_idx));
503
504         /*
505          * receive interrupts
506          */
507         comp_idx = hw->sds[sds_idx].sdsr_next;
508
509         while (count-- && !ha->flags.stop_rcv) {
510
511                 sdesc = (q80_stat_desc_t *)
512                                 &hw->sds[sds_idx].sds_ring_base[comp_idx];
513
514                 opcode = Q8_STAT_DESC_OPCODE((sdesc->data[1]));
515
516                 if (!opcode)
517                         break;
518
519                 hw->sds[sds_idx].intr_count++;
520                 switch (opcode) {
521
522                 case Q8_STAT_DESC_OPCODE_RCV_PKT:
523
524                         desc_count = 1;
525
526                         bzero(&sgc, sizeof(qla_sgl_comp_t));
527
528                         sgc.rcv.pkt_length =
529                                 Q8_STAT_DESC_TOTAL_LENGTH((sdesc->data[0]));
530                         sgc.rcv.num_handles = 1;
531                         sgc.rcv.handle[0] =
532                                 Q8_STAT_DESC_HANDLE((sdesc->data[0]));
533                         sgc.rcv.chksum_status =
534                                 Q8_STAT_DESC_STATUS((sdesc->data[1]));
535
536                         sgc.rcv.rss_hash =
537                                 Q8_STAT_DESC_RSS_HASH((sdesc->data[0]));
538
539                         if (Q8_STAT_DESC_VLAN((sdesc->data[1]))) {
540                                 sgc.rcv.vlan_tag =
541                                         Q8_STAT_DESC_VLAN_ID((sdesc->data[1]));
542                         }
543                         qla_rx_intr(ha, &sgc.rcv, sds_idx);
544                         break;
545
546                 case Q8_STAT_DESC_OPCODE_SGL_RCV:
547
548                         desc_count =
549                                 Q8_STAT_DESC_COUNT_SGL_RCV((sdesc->data[1]));
550
551                         if (desc_count > 1) {
552                                 c_idx = (comp_idx + desc_count -1) &
553                                                 (NUM_STATUS_DESCRIPTORS-1);
554                                 sdesc0 = (q80_stat_desc_t *)
555                                         &hw->sds[sds_idx].sds_ring_base[c_idx];
556
557                                 if (Q8_STAT_DESC_OPCODE((sdesc0->data[1])) !=
558                                                 Q8_STAT_DESC_OPCODE_CONT) {
559                                         desc_count = 0;
560                                         break;
561                                 }
562                         }
563
564                         bzero(&sgc, sizeof(qla_sgl_comp_t));
565
566                         sgc.rcv.pkt_length =
567                                 Q8_STAT_DESC_TOTAL_LENGTH_SGL_RCV(\
568                                         (sdesc->data[0]));
569                         sgc.rcv.chksum_status =
570                                 Q8_STAT_DESC_STATUS((sdesc->data[1]));
571
572                         sgc.rcv.rss_hash =
573                                 Q8_STAT_DESC_RSS_HASH((sdesc->data[0]));
574
575                         if (Q8_STAT_DESC_VLAN((sdesc->data[1]))) {
576                                 sgc.rcv.vlan_tag =
577                                         Q8_STAT_DESC_VLAN_ID((sdesc->data[1]));
578                         }
579
580                         QL_ASSERT(ha, (desc_count <= 2) ,\
581                                 ("%s: [sds_idx, data0, data1]="\
582                                 "%d, %p, %p]\n", __func__, sds_idx,\
583                                 (void *)sdesc->data[0],\
584                                 (void *)sdesc->data[1]));
585
586                         sgc.rcv.num_handles = 1;
587                         sgc.rcv.handle[0] = 
588                                 Q8_STAT_DESC_HANDLE((sdesc->data[0]));
589                         
590                         if (qla_rcv_cont_sds(ha, sds_idx, comp_idx, desc_count,
591                                 &sgc.rcv.handle[1], &nhandles)) {
592                                 device_printf(dev,
593                                         "%s: [sds_idx, dcount, data0, data1]="
594                                          "[%d, %d, 0x%llx, 0x%llx]\n",
595                                         __func__, sds_idx, desc_count,
596                                         (long long unsigned int)sdesc->data[0],
597                                         (long long unsigned int)sdesc->data[1]);
598                                 desc_count = 0;
599                                 break;  
600                         }
601
602                         sgc.rcv.num_handles += nhandles;
603
604                         qla_rx_intr(ha, &sgc.rcv, sds_idx);
605                         
606                         break;
607
608                 case Q8_STAT_DESC_OPCODE_SGL_LRO:
609
610                         desc_count =
611                                 Q8_STAT_DESC_COUNT_SGL_LRO((sdesc->data[1]));
612
613                         if (desc_count > 1) {
614                                 c_idx = (comp_idx + desc_count -1) &
615                                                 (NUM_STATUS_DESCRIPTORS-1);
616                                 sdesc0 = (q80_stat_desc_t *)
617                                         &hw->sds[sds_idx].sds_ring_base[c_idx];
618
619                                 if (Q8_STAT_DESC_OPCODE((sdesc0->data[1])) !=
620                                                 Q8_STAT_DESC_OPCODE_CONT) {
621                                         desc_count = 0;
622                                         break;
623                                 }
624                         }
625                         bzero(&sgc, sizeof(qla_sgl_comp_t));
626
627                         sgc.lro.payload_length =
628                         Q8_STAT_DESC_TOTAL_LENGTH_SGL_RCV((sdesc->data[0]));
629                                 
630                         sgc.lro.rss_hash =
631                                 Q8_STAT_DESC_RSS_HASH((sdesc->data[0]));
632                         
633                         sgc.lro.num_handles = 1;
634                         sgc.lro.handle[0] =
635                                 Q8_STAT_DESC_HANDLE((sdesc->data[0]));
636
637                         if (Q8_SGL_LRO_STAT_TS((sdesc->data[1])))
638                                 sgc.lro.flags |= Q8_LRO_COMP_TS;
639
640                         if (Q8_SGL_LRO_STAT_PUSH_BIT((sdesc->data[1])))
641                                 sgc.lro.flags |= Q8_LRO_COMP_PUSH_BIT;
642
643                         sgc.lro.l2_offset =
644                                 Q8_SGL_LRO_STAT_L2_OFFSET((sdesc->data[1]));
645                         sgc.lro.l4_offset =
646                                 Q8_SGL_LRO_STAT_L4_OFFSET((sdesc->data[1]));
647
648                         if (Q8_STAT_DESC_VLAN((sdesc->data[1]))) {
649                                 sgc.lro.vlan_tag =
650                                         Q8_STAT_DESC_VLAN_ID((sdesc->data[1]));
651                         }
652
653                         QL_ASSERT(ha, (desc_count <= 7) ,\
654                                 ("%s: [sds_idx, data0, data1]="\
655                                  "[%d, 0x%llx, 0x%llx]\n",\
656                                 __func__, sds_idx,\
657                                 (long long unsigned int)sdesc->data[0],\
658                                 (long long unsigned int)sdesc->data[1]));
659                                 
660                         if (qla_rcv_cont_sds(ha, sds_idx, comp_idx, 
661                                 desc_count, &sgc.lro.handle[1], &nhandles)) {
662                                 device_printf(dev,
663                                 "%s: [sds_idx, data0, data1]="\
664                                  "[%d, 0x%llx, 0x%llx]\n",\
665                                 __func__, sds_idx,\
666                                 (long long unsigned int)sdesc->data[0],\
667                                 (long long unsigned int)sdesc->data[1]);
668
669                                 desc_count = 0;
670                                 break;  
671                         }
672
673                         sgc.lro.num_handles += nhandles;
674
675                         if (qla_lro_intr(ha, &sgc.lro, sds_idx)) {
676                                 device_printf(dev,
677                                 "%s: [sds_idx, data0, data1]="\
678                                  "[%d, 0x%llx, 0x%llx]\n",\
679                                 __func__, sds_idx,\
680                                 (long long unsigned int)sdesc->data[0],\
681                                 (long long unsigned int)sdesc->data[1]);
682                                 device_printf(dev,
683                                 "%s: [comp_idx, c_idx, dcount, nhndls]="\
684                                  "[%d, %d, %d, %d]\n",\
685                                 __func__, comp_idx, c_idx, desc_count,
686                                 sgc.lro.num_handles);
687                                 if (desc_count > 1) {
688                                 device_printf(dev,
689                                 "%s: [sds_idx, data0, data1]="\
690                                  "[%d, 0x%llx, 0x%llx]\n",\
691                                 __func__, sds_idx,\
692                                 (long long unsigned int)sdesc0->data[0],\
693                                 (long long unsigned int)sdesc0->data[1]);
694                                 }
695                         }
696                         
697                         break;
698
699                 default:
700                         device_printf(dev, "%s: default 0x%llx!\n", __func__,
701                                         (long long unsigned int)sdesc->data[0]);
702                         break;
703                 }
704
705                 if (desc_count == 0)
706                         break;
707
708                 sds_replenish_threshold += desc_count;
709
710
711                 while (desc_count--) {
712                         sdesc->data[0] = 0ULL;
713                         sdesc->data[1] = 0ULL;
714                         comp_idx = (comp_idx + 1) & (NUM_STATUS_DESCRIPTORS-1);
715                         sdesc = (q80_stat_desc_t *)
716                                 &hw->sds[sds_idx].sds_ring_base[comp_idx];
717                 }
718
719                 if (sds_replenish_threshold > ha->hw.sds_cidx_thres) {
720                         sds_replenish_threshold = 0;
721                         if (hw->sds[sds_idx].sdsr_next != comp_idx) {
722                                 QL_UPDATE_SDS_CONSUMER_INDEX(ha, sds_idx,\
723                                         comp_idx);
724                         }
725                         hw->sds[sds_idx].sdsr_next = comp_idx;
726                 }
727         }
728
729         if (ha->hw.enable_soft_lro) {
730                 struct lro_ctrl         *lro;
731
732                 lro = &ha->hw.sds[sds_idx].lro;
733
734 #if (__FreeBSD_version >= 1100101)
735
736                 tcp_lro_flush_all(lro);
737
738 #else
739                 struct lro_entry *queued;
740
741                 while ((!SLIST_EMPTY(&lro->lro_active))) {
742                         queued = SLIST_FIRST(&lro->lro_active);
743                         SLIST_REMOVE_HEAD(&lro->lro_active, next);
744                         tcp_lro_flush(lro, queued);
745                 }
746
747 #endif /* #if (__FreeBSD_version >= 1100101) */
748
749         }
750
751         if (ha->flags.stop_rcv)
752                 goto ql_rcv_isr_exit;
753
754         if (hw->sds[sds_idx].sdsr_next != comp_idx) {
755                 QL_UPDATE_SDS_CONSUMER_INDEX(ha, sds_idx, comp_idx);
756                 hw->sds[sds_idx].sdsr_next = comp_idx;
757         } else {
758                 hw->sds[sds_idx].spurious_intr_count++;
759
760                 if (ha->hw.num_rds_rings > 1)
761                         r_idx = sds_idx;
762
763                 sdsp = &ha->hw.sds[sds_idx];
764
765                 if (sdsp->rx_free > ha->std_replenish)
766                         qla_replenish_normal_rx(ha, sdsp, r_idx);
767         }
768
769         sdesc = (q80_stat_desc_t *)&hw->sds[sds_idx].sds_ring_base[comp_idx];
770         opcode = Q8_STAT_DESC_OPCODE((sdesc->data[1]));
771
772         if (opcode)
773                 ret = -1;
774
775 ql_rcv_isr_exit:
776         hw->sds[sds_idx].rcv_active = 0;
777
778         return (ret);
779 }
780
781 void
782 ql_mbx_isr(void *arg)
783 {
784         qla_host_t *ha;
785         uint32_t data;
786         uint32_t prev_link_state;
787
788         ha = arg;
789
790         if (ha == NULL) {
791                 device_printf(ha->pci_dev, "%s: arg == NULL\n", __func__);
792                 return;
793         }
794
795         data = READ_REG32(ha, Q8_FW_MBOX_CNTRL);
796         if ((data & 0x3) != 0x1) {
797                 WRITE_REG32(ha, ha->hw.mbx_intr_mask_offset, 0);
798                 return;
799         }
800
801         data = READ_REG32(ha, Q8_FW_MBOX0);
802
803         if ((data & 0xF000) != 0x8000)
804                 return;
805
806         data = data & 0xFFFF;
807
808         switch (data) {
809
810         case 0x8001:  /* It's an AEN */
811                 
812                 ha->hw.cable_oui = READ_REG32(ha, (Q8_FW_MBOX0 + 4));
813
814                 data = READ_REG32(ha, (Q8_FW_MBOX0 + 8));
815                 ha->hw.cable_length = data & 0xFFFF;
816
817                 data = data >> 16;
818                 ha->hw.link_speed = data & 0xFFF;
819
820                 data = READ_REG32(ha, (Q8_FW_MBOX0 + 12));
821
822                 prev_link_state =  ha->hw.link_up;
823                 ha->hw.link_up = (((data & 0xFF) == 0) ? 0 : 1);
824
825                 if (prev_link_state !=  ha->hw.link_up) {
826                         if (ha->hw.link_up)
827                                 if_link_state_change(ha->ifp, LINK_STATE_UP);
828                         else
829                                 if_link_state_change(ha->ifp, LINK_STATE_DOWN);
830                 }
831
832
833                 ha->hw.module_type = ((data >> 8) & 0xFF);
834                 ha->hw.flags.fduplex = (((data & 0xFF0000) == 0) ? 0 : 1);
835                 ha->hw.flags.autoneg = (((data & 0xFF000000) == 0) ? 0 : 1);
836                 
837                 data = READ_REG32(ha, (Q8_FW_MBOX0 + 16));
838                 ha->hw.flags.loopback_mode = data & 0x03;
839
840                 ha->hw.link_faults = (data >> 3) & 0xFF;
841
842                 break;
843
844         case 0x8100:
845                 ha->hw.imd_compl=1;
846                 break;
847
848         case 0x8101:
849                 ha->async_event = 1;
850                 ha->hw.aen_mb0 = 0x8101;
851                 ha->hw.aen_mb1 = READ_REG32(ha, (Q8_FW_MBOX0 + 4));
852                 ha->hw.aen_mb2 = READ_REG32(ha, (Q8_FW_MBOX0 + 8));
853                 ha->hw.aen_mb3 = READ_REG32(ha, (Q8_FW_MBOX0 + 12));
854                 ha->hw.aen_mb4 = READ_REG32(ha, (Q8_FW_MBOX0 + 16));
855                 break;
856
857         case 0x8110:
858                 /* for now just dump the registers */
859                 {
860                         uint32_t ombx[5];
861
862                         ombx[0] = READ_REG32(ha, (Q8_FW_MBOX0 + 4));
863                         ombx[1] = READ_REG32(ha, (Q8_FW_MBOX0 + 8));
864                         ombx[2] = READ_REG32(ha, (Q8_FW_MBOX0 + 12));
865                         ombx[3] = READ_REG32(ha, (Q8_FW_MBOX0 + 16));
866                         ombx[4] = READ_REG32(ha, (Q8_FW_MBOX0 + 20));
867
868                         device_printf(ha->pci_dev, "%s: "
869                                 "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
870                                 __func__, data, ombx[0], ombx[1], ombx[2],
871                                 ombx[3], ombx[4]);
872                 }
873
874                 break;
875
876         case 0x8130:
877                 /* sfp insertion aen */
878                 device_printf(ha->pci_dev, "%s: sfp inserted [0x%08x]\n",
879                         __func__, READ_REG32(ha, (Q8_FW_MBOX0 + 4)));
880                 break;
881
882         case 0x8131:
883                 /* sfp removal aen */
884                 device_printf(ha->pci_dev, "%s: sfp removed]\n", __func__);
885                 break;
886
887         case 0x8140:
888                 {
889                         uint32_t ombx[3];
890
891                         ombx[0] = READ_REG32(ha, (Q8_FW_MBOX0 + 4));
892                         ombx[1] = READ_REG32(ha, (Q8_FW_MBOX0 + 8));
893                         ombx[2] = READ_REG32(ha, (Q8_FW_MBOX0 + 12));
894
895                         device_printf(ha->pci_dev, "%s: "
896                                 "0x%08x 0x%08x 0x%08x 0x%08x \n",
897                                 __func__, data, ombx[0], ombx[1], ombx[2]);
898                 }
899                 break;
900
901         default:
902                 device_printf(ha->pci_dev, "%s: AEN[0x%08x]\n", __func__, data);
903                 break;
904         }
905         WRITE_REG32(ha, Q8_FW_MBOX_CNTRL, 0x0);
906         WRITE_REG32(ha, ha->hw.mbx_intr_mask_offset, 0x0);
907         return;
908 }
909
910
911 static void
912 qla_replenish_normal_rx(qla_host_t *ha, qla_sds_t *sdsp, uint32_t r_idx)
913 {
914         qla_rx_buf_t *rxb;
915         int count = sdsp->rx_free;
916         uint32_t rx_next;
917         qla_rdesc_t *rdesc;
918
919         /* we can play with this value via a sysctl */
920         uint32_t replenish_thresh = ha->hw.rds_pidx_thres;
921
922         rdesc = &ha->hw.rds[r_idx];
923
924         rx_next = rdesc->rx_next;
925
926         while (count--) {
927                 rxb = sdsp->rxb_free;
928
929                 if (rxb == NULL)
930                         break;
931
932                 sdsp->rxb_free = rxb->next;
933                 sdsp->rx_free--;
934
935                 if (ql_get_mbuf(ha, rxb, NULL) == 0) {
936                         qla_set_hw_rcv_desc(ha, r_idx, rdesc->rx_in,
937                                 rxb->handle,
938                                 rxb->paddr, (rxb->m_head)->m_pkthdr.len);
939                         rdesc->rx_in++;
940                         if (rdesc->rx_in == NUM_RX_DESCRIPTORS)
941                                 rdesc->rx_in = 0;
942                         rdesc->rx_next++;
943                         if (rdesc->rx_next == NUM_RX_DESCRIPTORS)
944                                 rdesc->rx_next = 0;
945                 } else {
946                         device_printf(ha->pci_dev,
947                                 "%s: qla_get_mbuf [(%d),(%d),(%d)] failed\n",
948                                 __func__, r_idx, rdesc->rx_in, rxb->handle);
949
950                         rxb->m_head = NULL;
951                         rxb->next = sdsp->rxb_free;
952                         sdsp->rxb_free = rxb;
953                         sdsp->rx_free++;
954
955                         break;
956                 }
957                 if (replenish_thresh-- == 0) {
958                         QL_UPDATE_RDS_PRODUCER_INDEX(ha, rdesc->prod_std,
959                                 rdesc->rx_next);
960                         rx_next = rdesc->rx_next;
961                         replenish_thresh = ha->hw.rds_pidx_thres;
962                 }
963         }
964
965         if (rx_next != rdesc->rx_next) {
966                 QL_UPDATE_RDS_PRODUCER_INDEX(ha, rdesc->prod_std,
967                         rdesc->rx_next);
968         }
969 }
970
971 void
972 ql_isr(void *arg)
973 {
974         qla_ivec_t *ivec = arg;
975         qla_host_t *ha ;
976         int idx;
977         qla_hw_t *hw;
978         struct ifnet *ifp;
979         qla_tx_fp_t *fp;
980
981         ha = ivec->ha;
982         hw = &ha->hw;
983         ifp = ha->ifp;
984
985         if ((idx = ivec->sds_idx) >= ha->hw.num_sds_rings)
986                 return;
987
988
989         fp = &ha->tx_fp[idx];
990
991         if (fp->fp_taskqueue != NULL)
992                 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
993
994         return;
995 }
996