Merge git://git.kernel.org/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6] / drivers / message / fusion / mptlan.c
1 /*
2  *  linux/drivers/message/fusion/mptlan.c
3  *      IP Over Fibre Channel device driver.
4  *      For use with LSI Logic Fibre Channel PCI chip/adapters
5  *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
6  *
7  *  Copyright (c) 2000-2007 LSI Logic Corporation
8  *  (mailto:DL-MPTFusionLinux@lsi.com)
9  *
10  */
11 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12 /*
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; version 2 of the License.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     NO WARRANTY
23     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
24     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
25     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
26     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
27     solely responsible for determining the appropriateness of using and
28     distributing the Program and assumes all risks associated with its
29     exercise of rights under this Agreement, including but not limited to
30     the risks and costs of program errors, damage to or loss of data,
31     programs or equipment, and unavailability or interruption of operations.
32
33     DISCLAIMER OF LIABILITY
34     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
35     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
37     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
40     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
41
42     You should have received a copy of the GNU General Public License
43     along with this program; if not, write to the Free Software
44     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45 */
46
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
48 /*
49  * Define statements used for debugging
50  */
51 //#define MPT_LAN_IO_DEBUG
52
53 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
54
55 #include "mptlan.h"
56 #include <linux/init.h>
57 #include <linux/module.h>
58 #include <linux/fs.h>
59
60 #define my_VERSION      MPT_LINUX_VERSION_COMMON
61 #define MYNAM           "mptlan"
62
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(my_VERSION);
65
66 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
67 /*
68  * MPT LAN message sizes without variable part.
69  */
70 #define MPT_LAN_RECEIVE_POST_REQUEST_SIZE \
71         (sizeof(LANReceivePostRequest_t) - sizeof(SGE_MPI_UNION))
72
73 #define MPT_LAN_TRANSACTION32_SIZE \
74         (sizeof(SGETransaction32_t) - sizeof(u32))
75
76 /*
77  *  Fusion MPT LAN private structures
78  */
79
80 struct NAA_Hosed {
81         u16 NAA;
82         u8 ieee[FC_ALEN];
83         struct NAA_Hosed *next;
84 };
85
86 struct BufferControl {
87         struct sk_buff  *skb;
88         dma_addr_t      dma;
89         unsigned int    len;
90 };
91
92 struct mpt_lan_priv {
93         MPT_ADAPTER *mpt_dev;
94         u8 pnum; /* Port number in the IOC. This is not a Unix network port! */
95
96         atomic_t buckets_out;           /* number of unused buckets on IOC */
97         int bucketthresh;               /* Send more when this many left */
98
99         int *mpt_txfidx; /* Free Tx Context list */
100         int mpt_txfidx_tail;
101         spinlock_t txfidx_lock;
102
103         int *mpt_rxfidx; /* Free Rx Context list */
104         int mpt_rxfidx_tail;
105         spinlock_t rxfidx_lock;
106
107         struct BufferControl *RcvCtl;   /* Receive BufferControl structs */
108         struct BufferControl *SendCtl;  /* Send BufferControl structs */
109
110         int max_buckets_out;            /* Max buckets to send to IOC */
111         int tx_max_out;                 /* IOC's Tx queue len */
112
113         u32 total_posted;
114         u32 total_received;
115         struct net_device_stats stats;  /* Per device statistics */
116
117         struct delayed_work post_buckets_task;
118         struct net_device *dev;
119         unsigned long post_buckets_active;
120 };
121
122 struct mpt_lan_ohdr {
123         u16     dtype;
124         u8      daddr[FC_ALEN];
125         u16     stype;
126         u8      saddr[FC_ALEN];
127 };
128
129 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
130
131 /*
132  *  Forward protos...
133  */
134 static int  lan_reply (MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf,
135                        MPT_FRAME_HDR *reply);
136 static int  mpt_lan_open(struct net_device *dev);
137 static int  mpt_lan_reset(struct net_device *dev);
138 static int  mpt_lan_close(struct net_device *dev);
139 static void mpt_lan_post_receive_buckets(struct mpt_lan_priv *priv);
140 static void mpt_lan_wake_post_buckets_task(struct net_device *dev,
141                                            int priority);
142 static int  mpt_lan_receive_post_turbo(struct net_device *dev, u32 tmsg);
143 static int  mpt_lan_receive_post_reply(struct net_device *dev,
144                                        LANReceivePostReply_t *pRecvRep);
145 static int  mpt_lan_send_turbo(struct net_device *dev, u32 tmsg);
146 static int  mpt_lan_send_reply(struct net_device *dev,
147                                LANSendReply_t *pSendRep);
148 static int  mpt_lan_ioc_reset(MPT_ADAPTER *ioc, int reset_phase);
149 static int  mpt_lan_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
150 static unsigned short mpt_lan_type_trans(struct sk_buff *skb,
151                                          struct net_device *dev);
152
153 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
154 /*
155  *  Fusion MPT LAN private data
156  */
157 static int LanCtx = -1;
158
159 static u32 max_buckets_out = 127;
160 static u32 tx_max_out_p = 127 - 16;
161
162 #ifdef QLOGIC_NAA_WORKAROUND
163 static struct NAA_Hosed *mpt_bad_naa = NULL;
164 DEFINE_RWLOCK(bad_naa_lock);
165 #endif
166
167 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
168 /*
169  * Fusion MPT LAN external data
170  */
171 extern int mpt_lan_index;
172
173 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
174 /**
175  *      lan_reply - Handle all data sent from the hardware.
176  *      @ioc: Pointer to MPT_ADAPTER structure
177  *      @mf: Pointer to original MPT request frame (NULL if TurboReply)
178  *      @reply: Pointer to MPT reply frame
179  *
180  *      Returns 1 indicating original alloc'd request frame ptr
181  *      should be freed, or 0 if it shouldn't.
182  */
183 static int
184 lan_reply (MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *reply)
185 {
186         struct net_device *dev = ioc->netdev;
187         int FreeReqFrame = 0;
188
189         dioprintk((KERN_INFO MYNAM ": %s/%s: Got reply.\n",
190                   IOC_AND_NETDEV_NAMES_s_s(dev)));
191
192 //      dioprintk((KERN_INFO MYNAM "@lan_reply: mf = %p, reply = %p\n",
193 //                      mf, reply));
194
195         if (mf == NULL) {
196                 u32 tmsg = CAST_PTR_TO_U32(reply);
197
198                 dioprintk((KERN_INFO MYNAM ": %s/%s: @lan_reply, tmsg %08x\n",
199                                 IOC_AND_NETDEV_NAMES_s_s(dev),
200                                 tmsg));
201
202                 switch (GET_LAN_FORM(tmsg)) {
203
204                 // NOTE!  (Optimization) First case here is now caught in
205                 //  mptbase.c::mpt_interrupt() routine and callcack here
206                 //  is now skipped for this case!
207 #if 0
208                 case LAN_REPLY_FORM_MESSAGE_CONTEXT:
209 //                      dioprintk((KERN_INFO MYNAM "/lan_reply: "
210 //                                "MessageContext turbo reply received\n"));
211                         FreeReqFrame = 1;
212                         break;
213 #endif
214
215                 case LAN_REPLY_FORM_SEND_SINGLE:
216 //                      dioprintk((MYNAM "/lan_reply: "
217 //                                "calling mpt_lan_send_reply (turbo)\n"));
218
219                         // Potential BUG here?
220                         //      FreeReqFrame = mpt_lan_send_turbo(dev, tmsg);
221                         //  If/when mpt_lan_send_turbo would return 1 here,
222                         //  calling routine (mptbase.c|mpt_interrupt)
223                         //  would Oops because mf has already been set
224                         //  to NULL.  So after return from this func,
225                         //  mpt_interrupt() will attempt to put (NULL) mf ptr
226                         //  item back onto its adapter FreeQ - Oops!:-(
227                         //  It's Ok, since mpt_lan_send_turbo() *currently*
228                         //  always returns 0, but..., just in case:
229
230                         (void) mpt_lan_send_turbo(dev, tmsg);
231                         FreeReqFrame = 0;
232
233                         break;
234
235                 case LAN_REPLY_FORM_RECEIVE_SINGLE:
236 //                      dioprintk((KERN_INFO MYNAM "@lan_reply: "
237 //                                "rcv-Turbo = %08x\n", tmsg));
238                         mpt_lan_receive_post_turbo(dev, tmsg);
239                         break;
240
241                 default:
242                         printk (KERN_ERR MYNAM "/lan_reply: Got a turbo reply "
243                                 "that I don't know what to do with\n");
244
245                         /* CHECKME!  Hmmm...  FreeReqFrame is 0 here; is that right? */
246
247                         break;
248                 }
249
250                 return FreeReqFrame;
251         }
252
253 //      msg = (u32 *) reply;
254 //      dioprintk((KERN_INFO MYNAM "@lan_reply: msg = %08x %08x %08x %08x\n",
255 //                le32_to_cpu(msg[0]), le32_to_cpu(msg[1]),
256 //                le32_to_cpu(msg[2]), le32_to_cpu(msg[3])));
257 //      dioprintk((KERN_INFO MYNAM "@lan_reply: Function = %02xh\n",
258 //                reply->u.hdr.Function));
259
260         switch (reply->u.hdr.Function) {
261
262         case MPI_FUNCTION_LAN_SEND:
263         {
264                 LANSendReply_t *pSendRep;
265
266                 pSendRep = (LANSendReply_t *) reply;
267                 FreeReqFrame = mpt_lan_send_reply(dev, pSendRep);
268                 break;
269         }
270
271         case MPI_FUNCTION_LAN_RECEIVE:
272         {
273                 LANReceivePostReply_t *pRecvRep;
274
275                 pRecvRep = (LANReceivePostReply_t *) reply;
276                 if (pRecvRep->NumberOfContexts) {
277                         mpt_lan_receive_post_reply(dev, pRecvRep);
278                         if (!(pRecvRep->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY))
279                                 FreeReqFrame = 1;
280                 } else
281                         dioprintk((KERN_INFO MYNAM "@lan_reply: zero context "
282                                   "ReceivePostReply received.\n"));
283                 break;
284         }
285
286         case MPI_FUNCTION_LAN_RESET:
287                 /* Just a default reply. Might want to check it to
288                  * make sure that everything went ok.
289                  */
290                 FreeReqFrame = 1;
291                 break;
292
293         case MPI_FUNCTION_EVENT_NOTIFICATION:
294         case MPI_FUNCTION_EVENT_ACK:
295                 /*  _EVENT_NOTIFICATION should NOT come down this path any more.
296                  *  Should be routed to mpt_lan_event_process(), but just in case...
297                  */
298                 FreeReqFrame = 1;
299                 break;
300
301         default:
302                 printk (KERN_ERR MYNAM "/lan_reply: Got a non-turbo "
303                         "reply that I don't know what to do with\n");
304
305                 /* CHECKME!  Hmmm...  FreeReqFrame is 0 here; is that right? */
306                 FreeReqFrame = 1;
307
308                 break;
309         }
310
311         return FreeReqFrame;
312 }
313
314 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
315 static int
316 mpt_lan_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
317 {
318         struct net_device *dev = ioc->netdev;
319         struct mpt_lan_priv *priv;
320
321         if (dev == NULL)
322                 return(1);
323         else
324                 priv = netdev_priv(dev);
325
326         dlprintk((KERN_INFO MYNAM ": IOC %s_reset routed to LAN driver!\n",
327                         reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
328                         reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
329
330         if (priv->mpt_rxfidx == NULL)
331                 return (1);
332
333         if (reset_phase == MPT_IOC_SETUP_RESET) {
334                 ;
335         } else if (reset_phase == MPT_IOC_PRE_RESET) {
336                 int i;
337                 unsigned long flags;
338
339                 netif_stop_queue(dev);
340
341                 dlprintk ((KERN_INFO "mptlan/ioc_reset: called netif_stop_queue for %s.\n", dev->name));
342
343                 atomic_set(&priv->buckets_out, 0);
344
345                 /* Reset Rx Free Tail index and re-populate the queue. */
346                 spin_lock_irqsave(&priv->rxfidx_lock, flags);
347                 priv->mpt_rxfidx_tail = -1;
348                 for (i = 0; i < priv->max_buckets_out; i++)
349                         priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = i;
350                 spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
351         } else {
352                 mpt_lan_post_receive_buckets(priv);
353                 netif_wake_queue(dev);
354         }
355
356         return 1;
357 }
358
359 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
360 static int
361 mpt_lan_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
362 {
363         dlprintk((KERN_INFO MYNAM ": MPT event routed to LAN driver!\n"));
364
365         switch (le32_to_cpu(pEvReply->Event)) {
366         case MPI_EVENT_NONE:                            /* 00 */
367         case MPI_EVENT_LOG_DATA:                        /* 01 */
368         case MPI_EVENT_STATE_CHANGE:                    /* 02 */
369         case MPI_EVENT_UNIT_ATTENTION:                  /* 03 */
370         case MPI_EVENT_IOC_BUS_RESET:                   /* 04 */
371         case MPI_EVENT_EXT_BUS_RESET:                   /* 05 */
372         case MPI_EVENT_RESCAN:                          /* 06 */
373                 /* Ok, do we need to do anything here? As far as
374                    I can tell, this is when a new device gets added
375                    to the loop. */
376         case MPI_EVENT_LINK_STATUS_CHANGE:              /* 07 */
377         case MPI_EVENT_LOOP_STATE_CHANGE:               /* 08 */
378         case MPI_EVENT_LOGOUT:                          /* 09 */
379         case MPI_EVENT_EVENT_CHANGE:                    /* 0A */
380         default:
381                 break;
382         }
383
384         /*
385          *  NOTE: pEvent->AckRequired handling now done in mptbase.c;
386          *  Do NOT do it here now!
387          */
388
389         return 1;
390 }
391
392 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
393 static int
394 mpt_lan_open(struct net_device *dev)
395 {
396         struct mpt_lan_priv *priv = netdev_priv(dev);
397         int i;
398
399         if (mpt_lan_reset(dev) != 0) {
400                 MPT_ADAPTER *mpt_dev = priv->mpt_dev;
401
402                 printk (KERN_WARNING MYNAM "/lan_open: lan_reset failed.");
403
404                 if (mpt_dev->active)
405                         printk ("The ioc is active. Perhaps it needs to be"
406                                 " reset?\n");
407                 else
408                         printk ("The ioc in inactive, most likely in the "
409                                 "process of being reset. Please try again in "
410                                 "a moment.\n");
411         }
412
413         priv->mpt_txfidx = kmalloc(priv->tx_max_out * sizeof(int), GFP_KERNEL);
414         if (priv->mpt_txfidx == NULL)
415                 goto out;
416         priv->mpt_txfidx_tail = -1;
417
418         priv->SendCtl = kcalloc(priv->tx_max_out, sizeof(struct BufferControl),
419                                 GFP_KERNEL);
420         if (priv->SendCtl == NULL)
421                 goto out_mpt_txfidx;
422         for (i = 0; i < priv->tx_max_out; i++)
423                 priv->mpt_txfidx[++priv->mpt_txfidx_tail] = i;
424
425         dlprintk((KERN_INFO MYNAM "@lo: Finished initializing SendCtl\n"));
426
427         priv->mpt_rxfidx = kmalloc(priv->max_buckets_out * sizeof(int),
428                                    GFP_KERNEL);
429         if (priv->mpt_rxfidx == NULL)
430                 goto out_SendCtl;
431         priv->mpt_rxfidx_tail = -1;
432
433         priv->RcvCtl = kcalloc(priv->max_buckets_out,
434                                sizeof(struct BufferControl),
435                                GFP_KERNEL);
436         if (priv->RcvCtl == NULL)
437                 goto out_mpt_rxfidx;
438         for (i = 0; i < priv->max_buckets_out; i++)
439                 priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = i;
440
441 /**/    dlprintk((KERN_INFO MYNAM "/lo: txfidx contains - "));
442 /**/    for (i = 0; i < priv->tx_max_out; i++)
443 /**/            dlprintk((" %xh", priv->mpt_txfidx[i]));
444 /**/    dlprintk(("\n"));
445
446         dlprintk((KERN_INFO MYNAM "/lo: Finished initializing RcvCtl\n"));
447
448         mpt_lan_post_receive_buckets(priv);
449         printk(KERN_INFO MYNAM ": %s/%s: interface up & active\n",
450                         IOC_AND_NETDEV_NAMES_s_s(dev));
451
452         if (mpt_event_register(LanCtx, mpt_lan_event_process) != 0) {
453                 printk (KERN_WARNING MYNAM "/lo: Unable to register for Event"
454                         " Notifications. This is a bad thing! We're not going "
455                         "to go ahead, but I'd be leery of system stability at "
456                         "this point.\n");
457         }
458
459         netif_start_queue(dev);
460         dlprintk((KERN_INFO MYNAM "/lo: Done.\n"));
461
462         return 0;
463 out_mpt_rxfidx:
464         kfree(priv->mpt_rxfidx);
465         priv->mpt_rxfidx = NULL;
466 out_SendCtl:
467         kfree(priv->SendCtl);
468         priv->SendCtl = NULL;
469 out_mpt_txfidx:
470         kfree(priv->mpt_txfidx);
471         priv->mpt_txfidx = NULL;
472 out:    return -ENOMEM;
473 }
474
475 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
476 /* Send a LanReset message to the FW. This should result in the FW returning
477    any buckets it still has. */
478 static int
479 mpt_lan_reset(struct net_device *dev)
480 {
481         MPT_FRAME_HDR *mf;
482         LANResetRequest_t *pResetReq;
483         struct mpt_lan_priv *priv = netdev_priv(dev);
484
485         mf = mpt_get_msg_frame(LanCtx, priv->mpt_dev);
486
487         if (mf == NULL) {
488 /*              dlprintk((KERN_ERR MYNAM "/reset: Evil funkiness abounds! "
489                 "Unable to allocate a request frame.\n"));
490 */
491                 return -1;
492         }
493
494         pResetReq = (LANResetRequest_t *) mf;
495
496         pResetReq->Function     = MPI_FUNCTION_LAN_RESET;
497         pResetReq->ChainOffset  = 0;
498         pResetReq->Reserved     = 0;
499         pResetReq->PortNumber   = priv->pnum;
500         pResetReq->MsgFlags     = 0;
501         pResetReq->Reserved2    = 0;
502
503         mpt_put_msg_frame(LanCtx, priv->mpt_dev, mf);
504
505         return 0;
506 }
507
508 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
509 static int
510 mpt_lan_close(struct net_device *dev)
511 {
512         struct mpt_lan_priv *priv = netdev_priv(dev);
513         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
514         unsigned long timeout;
515         int i;
516
517         dlprintk((KERN_INFO MYNAM ": mpt_lan_close called\n"));
518
519         mpt_event_deregister(LanCtx);
520
521         dlprintk((KERN_INFO MYNAM ":lan_close: Posted %d buckets "
522                   "since driver was loaded, %d still out\n",
523                   priv->total_posted,atomic_read(&priv->buckets_out)));
524
525         netif_stop_queue(dev);
526
527         mpt_lan_reset(dev);
528
529         timeout = jiffies + 2 * HZ;
530         while (atomic_read(&priv->buckets_out) && time_before(jiffies, timeout))
531                 schedule_timeout_interruptible(1);
532
533         for (i = 0; i < priv->max_buckets_out; i++) {
534                 if (priv->RcvCtl[i].skb != NULL) {
535 /**/                    dlprintk((KERN_INFO MYNAM "/lan_close: bucket %05x "
536 /**/                              "is still out\n", i));
537                         pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[i].dma,
538                                          priv->RcvCtl[i].len,
539                                          PCI_DMA_FROMDEVICE);
540                         dev_kfree_skb(priv->RcvCtl[i].skb);
541                 }
542         }
543
544         kfree(priv->RcvCtl);
545         kfree(priv->mpt_rxfidx);
546
547         for (i = 0; i < priv->tx_max_out; i++) {
548                 if (priv->SendCtl[i].skb != NULL) {
549                         pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[i].dma,
550                                          priv->SendCtl[i].len,
551                                          PCI_DMA_TODEVICE);
552                         dev_kfree_skb(priv->SendCtl[i].skb);
553                 }
554         }
555
556         kfree(priv->SendCtl);
557         kfree(priv->mpt_txfidx);
558
559         atomic_set(&priv->buckets_out, 0);
560
561         printk(KERN_INFO MYNAM ": %s/%s: interface down & inactive\n",
562                         IOC_AND_NETDEV_NAMES_s_s(dev));
563
564         return 0;
565 }
566
567 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
568 static struct net_device_stats *
569 mpt_lan_get_stats(struct net_device *dev)
570 {
571         struct mpt_lan_priv *priv = netdev_priv(dev);
572
573         return (struct net_device_stats *) &priv->stats;
574 }
575
576 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
577 static int
578 mpt_lan_change_mtu(struct net_device *dev, int new_mtu)
579 {
580         if ((new_mtu < MPT_LAN_MIN_MTU) || (new_mtu > MPT_LAN_MAX_MTU))
581                 return -EINVAL;
582         dev->mtu = new_mtu;
583         return 0;
584 }
585
586 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
587 /* Tx timeout handler. */
588 static void
589 mpt_lan_tx_timeout(struct net_device *dev)
590 {
591         struct mpt_lan_priv *priv = netdev_priv(dev);
592         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
593
594         if (mpt_dev->active) {
595                 dlprintk (("mptlan/tx_timeout: calling netif_wake_queue for %s.\n", dev->name));
596                 netif_wake_queue(dev);
597         }
598 }
599
600 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
601 //static inline int
602 static int
603 mpt_lan_send_turbo(struct net_device *dev, u32 tmsg)
604 {
605         struct mpt_lan_priv *priv = netdev_priv(dev);
606         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
607         struct sk_buff *sent;
608         unsigned long flags;
609         u32 ctx;
610
611         ctx = GET_LAN_BUFFER_CONTEXT(tmsg);
612         sent = priv->SendCtl[ctx].skb;
613
614         priv->stats.tx_packets++;
615         priv->stats.tx_bytes += sent->len;
616
617         dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, skb %p sent.\n",
618                         IOC_AND_NETDEV_NAMES_s_s(dev),
619                         __FUNCTION__, sent));
620
621         priv->SendCtl[ctx].skb = NULL;
622         pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[ctx].dma,
623                          priv->SendCtl[ctx].len, PCI_DMA_TODEVICE);
624         dev_kfree_skb_irq(sent);
625
626         spin_lock_irqsave(&priv->txfidx_lock, flags);
627         priv->mpt_txfidx[++priv->mpt_txfidx_tail] = ctx;
628         spin_unlock_irqrestore(&priv->txfidx_lock, flags);
629
630         netif_wake_queue(dev);
631         return 0;
632 }
633
634 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
635 static int
636 mpt_lan_send_reply(struct net_device *dev, LANSendReply_t *pSendRep)
637 {
638         struct mpt_lan_priv *priv = netdev_priv(dev);
639         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
640         struct sk_buff *sent;
641         unsigned long flags;
642         int FreeReqFrame = 0;
643         u32 *pContext;
644         u32 ctx;
645         u8 count;
646
647         count = pSendRep->NumberOfContexts;
648
649         dioprintk((KERN_INFO MYNAM ": send_reply: IOCStatus: %04x\n",
650                  le16_to_cpu(pSendRep->IOCStatus)));
651
652         /* Add check for Loginfo Flag in IOCStatus */
653
654         switch (le16_to_cpu(pSendRep->IOCStatus) & MPI_IOCSTATUS_MASK) {
655         case MPI_IOCSTATUS_SUCCESS:
656                 priv->stats.tx_packets += count;
657                 break;
658
659         case MPI_IOCSTATUS_LAN_CANCELED:
660         case MPI_IOCSTATUS_LAN_TRANSMIT_ABORTED:
661                 break;
662
663         case MPI_IOCSTATUS_INVALID_SGL:
664                 priv->stats.tx_errors += count;
665                 printk (KERN_ERR MYNAM ": %s/%s: ERROR - Invalid SGL sent to IOC!\n",
666                                 IOC_AND_NETDEV_NAMES_s_s(dev));
667                 goto out;
668
669         default:
670                 priv->stats.tx_errors += count;
671                 break;
672         }
673
674         pContext = &pSendRep->BufferContext;
675
676         spin_lock_irqsave(&priv->txfidx_lock, flags);
677         while (count > 0) {
678                 ctx = GET_LAN_BUFFER_CONTEXT(le32_to_cpu(*pContext));
679
680                 sent = priv->SendCtl[ctx].skb;
681                 priv->stats.tx_bytes += sent->len;
682
683                 dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, skb %p sent.\n",
684                                 IOC_AND_NETDEV_NAMES_s_s(dev),
685                                 __FUNCTION__, sent));
686
687                 priv->SendCtl[ctx].skb = NULL;
688                 pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[ctx].dma,
689                                  priv->SendCtl[ctx].len, PCI_DMA_TODEVICE);
690                 dev_kfree_skb_irq(sent);
691
692                 priv->mpt_txfidx[++priv->mpt_txfidx_tail] = ctx;
693
694                 pContext++;
695                 count--;
696         }
697         spin_unlock_irqrestore(&priv->txfidx_lock, flags);
698
699 out:
700         if (!(pSendRep->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY))
701                 FreeReqFrame = 1;
702
703         netif_wake_queue(dev);
704         return FreeReqFrame;
705 }
706
707 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
708 static int
709 mpt_lan_sdu_send (struct sk_buff *skb, struct net_device *dev)
710 {
711         struct mpt_lan_priv *priv = netdev_priv(dev);
712         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
713         MPT_FRAME_HDR *mf;
714         LANSendRequest_t *pSendReq;
715         SGETransaction32_t *pTrans;
716         SGESimple64_t *pSimple;
717         const unsigned char *mac;
718         dma_addr_t dma;
719         unsigned long flags;
720         int ctx;
721         u16 cur_naa = 0x1000;
722
723         dioprintk((KERN_INFO MYNAM ": %s called, skb_addr = %p\n",
724                         __FUNCTION__, skb));
725
726         spin_lock_irqsave(&priv->txfidx_lock, flags);
727         if (priv->mpt_txfidx_tail < 0) {
728                 netif_stop_queue(dev);
729                 spin_unlock_irqrestore(&priv->txfidx_lock, flags);
730
731                 printk (KERN_ERR "%s: no tx context available: %u\n",
732                         __FUNCTION__, priv->mpt_txfidx_tail);
733                 return 1;
734         }
735
736         mf = mpt_get_msg_frame(LanCtx, mpt_dev);
737         if (mf == NULL) {
738                 netif_stop_queue(dev);
739                 spin_unlock_irqrestore(&priv->txfidx_lock, flags);
740
741                 printk (KERN_ERR "%s: Unable to alloc request frame\n",
742                         __FUNCTION__);
743                 return 1;
744         }
745
746         ctx = priv->mpt_txfidx[priv->mpt_txfidx_tail--];
747         spin_unlock_irqrestore(&priv->txfidx_lock, flags);
748
749 //      dioprintk((KERN_INFO MYNAM ": %s/%s: Creating new msg frame (send).\n",
750 //                      IOC_AND_NETDEV_NAMES_s_s(dev)));
751
752         pSendReq = (LANSendRequest_t *) mf;
753
754         /* Set the mac.raw pointer, since this apparently isn't getting
755          * done before we get the skb. Pull the data pointer past the mac data.
756          */
757         skb_reset_mac_header(skb);
758         skb_pull(skb, 12);
759
760         dma = pci_map_single(mpt_dev->pcidev, skb->data, skb->len,
761                              PCI_DMA_TODEVICE);
762
763         priv->SendCtl[ctx].skb = skb;
764         priv->SendCtl[ctx].dma = dma;
765         priv->SendCtl[ctx].len = skb->len;
766
767         /* Message Header */
768         pSendReq->Reserved    = 0;
769         pSendReq->Function    = MPI_FUNCTION_LAN_SEND;
770         pSendReq->ChainOffset = 0;
771         pSendReq->Reserved2   = 0;
772         pSendReq->MsgFlags    = 0;
773         pSendReq->PortNumber  = priv->pnum;
774
775         /* Transaction Context Element */
776         pTrans = (SGETransaction32_t *) pSendReq->SG_List;
777
778         /* No Flags, 8 bytes of Details, 32bit Context (bloody turbo replies) */
779         pTrans->ContextSize   = sizeof(u32);
780         pTrans->DetailsLength = 2 * sizeof(u32);
781         pTrans->Flags         = 0;
782         pTrans->TransactionContext[0] = cpu_to_le32(ctx);
783
784 //      dioprintk((KERN_INFO MYNAM ": %s/%s: BC = %08x, skb = %p, buff = %p\n",
785 //                      IOC_AND_NETDEV_NAMES_s_s(dev),
786 //                      ctx, skb, skb->data));
787
788         mac = skb_mac_header(skb);
789 #ifdef QLOGIC_NAA_WORKAROUND
790 {
791         struct NAA_Hosed *nh;
792
793         /* Munge the NAA for Tx packets to QLogic boards, which don't follow
794            RFC 2625. The longer I look at this, the more my opinion of Qlogic
795            drops. */
796         read_lock_irq(&bad_naa_lock);
797         for (nh = mpt_bad_naa; nh != NULL; nh=nh->next) {
798                 if ((nh->ieee[0] == mac[0]) &&
799                     (nh->ieee[1] == mac[1]) &&
800                     (nh->ieee[2] == mac[2]) &&
801                     (nh->ieee[3] == mac[3]) &&
802                     (nh->ieee[4] == mac[4]) &&
803                     (nh->ieee[5] == mac[5])) {
804                         cur_naa = nh->NAA;
805                         dlprintk ((KERN_INFO "mptlan/sdu_send: using NAA value "
806                                   "= %04x.\n", cur_naa));
807                         break;
808                 }
809         }
810         read_unlock_irq(&bad_naa_lock);
811 }
812 #endif
813
814         pTrans->TransactionDetails[0] = cpu_to_le32((cur_naa         << 16) |
815                                                     (mac[0] <<  8) |
816                                                     (mac[1] <<  0));
817         pTrans->TransactionDetails[1] = cpu_to_le32((mac[2] << 24) |
818                                                     (mac[3] << 16) |
819                                                     (mac[4] <<  8) |
820                                                     (mac[5] <<  0));
821
822         pSimple = (SGESimple64_t *) &pTrans->TransactionDetails[2];
823
824         /* If we ever decide to send more than one Simple SGE per LANSend, then
825            we will need to make sure that LAST_ELEMENT only gets set on the
826            last one. Otherwise, bad voodoo and evil funkiness will commence. */
827         pSimple->FlagsLength = cpu_to_le32(
828                         ((MPI_SGE_FLAGS_LAST_ELEMENT |
829                           MPI_SGE_FLAGS_END_OF_BUFFER |
830                           MPI_SGE_FLAGS_SIMPLE_ELEMENT |
831                           MPI_SGE_FLAGS_SYSTEM_ADDRESS |
832                           MPI_SGE_FLAGS_HOST_TO_IOC |
833                           MPI_SGE_FLAGS_64_BIT_ADDRESSING |
834                           MPI_SGE_FLAGS_END_OF_LIST) << MPI_SGE_FLAGS_SHIFT) |
835                         skb->len);
836         pSimple->Address.Low = cpu_to_le32((u32) dma);
837         if (sizeof(dma_addr_t) > sizeof(u32))
838                 pSimple->Address.High = cpu_to_le32((u32) ((u64) dma >> 32));
839         else
840                 pSimple->Address.High = 0;
841
842         mpt_put_msg_frame (LanCtx, mpt_dev, mf);
843         dev->trans_start = jiffies;
844
845         dioprintk((KERN_INFO MYNAM ": %s/%s: Sending packet. FlagsLength = %08x.\n",
846                         IOC_AND_NETDEV_NAMES_s_s(dev),
847                         le32_to_cpu(pSimple->FlagsLength)));
848
849         return 0;
850 }
851
852 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
853 static void
854 mpt_lan_wake_post_buckets_task(struct net_device *dev, int priority)
855 /*
856  * @priority: 0 = put it on the timer queue, 1 = put it on the immediate queue
857  */
858 {
859         struct mpt_lan_priv *priv = dev->priv;
860         
861         if (test_and_set_bit(0, &priv->post_buckets_active) == 0) {
862                 if (priority) {
863                         schedule_delayed_work(&priv->post_buckets_task, 0);
864                 } else {
865                         schedule_delayed_work(&priv->post_buckets_task, 1);
866                         dioprintk((KERN_INFO MYNAM ": post_buckets queued on "
867                                    "timer.\n"));
868                 }
869                 dioprintk((KERN_INFO MYNAM ": %s/%s: Queued post_buckets task.\n",
870                            IOC_AND_NETDEV_NAMES_s_s(dev) ));
871         }
872 }
873
874 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
875 static int
876 mpt_lan_receive_skb(struct net_device *dev, struct sk_buff *skb)
877 {
878         struct mpt_lan_priv *priv = dev->priv;
879
880         skb->protocol = mpt_lan_type_trans(skb, dev);
881
882         dioprintk((KERN_INFO MYNAM ": %s/%s: Incoming packet (%d bytes) "
883                  "delivered to upper level.\n",
884                         IOC_AND_NETDEV_NAMES_s_s(dev), skb->len));
885
886         priv->stats.rx_bytes += skb->len;
887         priv->stats.rx_packets++;
888
889         skb->dev = dev;
890         netif_rx(skb);
891
892         dioprintk((MYNAM "/receive_skb: %d buckets remaining\n",
893                  atomic_read(&priv->buckets_out)));
894
895         if (atomic_read(&priv->buckets_out) < priv->bucketthresh)
896                 mpt_lan_wake_post_buckets_task(dev, 1);
897
898         dioprintk((KERN_INFO MYNAM "/receive_post_reply: %d buckets "
899                   "remaining, %d received back since sod\n",
900                   atomic_read(&priv->buckets_out), priv->total_received));
901
902         return 0;
903 }
904
905 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
906 //static inline int
907 static int
908 mpt_lan_receive_post_turbo(struct net_device *dev, u32 tmsg)
909 {
910         struct mpt_lan_priv *priv = dev->priv;
911         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
912         struct sk_buff *skb, *old_skb;
913         unsigned long flags;
914         u32 ctx, len;
915
916         ctx = GET_LAN_BUCKET_CONTEXT(tmsg);
917         skb = priv->RcvCtl[ctx].skb;
918
919         len = GET_LAN_PACKET_LENGTH(tmsg);
920
921         if (len < MPT_LAN_RX_COPYBREAK) {
922                 old_skb = skb;
923
924                 skb = (struct sk_buff *)dev_alloc_skb(len);
925                 if (!skb) {
926                         printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
927                                         IOC_AND_NETDEV_NAMES_s_s(dev),
928                                         __FILE__, __LINE__);
929                         return -ENOMEM;
930                 }
931
932                 pci_dma_sync_single_for_cpu(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
933                                             priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
934
935                 skb_copy_from_linear_data(old_skb, skb_put(skb, len), len);
936
937                 pci_dma_sync_single_for_device(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
938                                                priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
939                 goto out;
940         }
941
942         skb_put(skb, len);
943
944         priv->RcvCtl[ctx].skb = NULL;
945
946         pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
947                          priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
948
949 out:
950         spin_lock_irqsave(&priv->rxfidx_lock, flags);
951         priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
952         spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
953
954         atomic_dec(&priv->buckets_out);
955         priv->total_received++;
956
957         return mpt_lan_receive_skb(dev, skb);
958 }
959
960 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
961 static int
962 mpt_lan_receive_post_free(struct net_device *dev,
963                           LANReceivePostReply_t *pRecvRep)
964 {
965         struct mpt_lan_priv *priv = dev->priv;
966         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
967         unsigned long flags;
968         struct sk_buff *skb;
969         u32 ctx;
970         int count;
971         int i;
972
973         count = pRecvRep->NumberOfContexts;
974
975 /**/    dlprintk((KERN_INFO MYNAM "/receive_post_reply: "
976                   "IOC returned %d buckets, freeing them...\n", count));
977
978         spin_lock_irqsave(&priv->rxfidx_lock, flags);
979         for (i = 0; i < count; i++) {
980                 ctx = le32_to_cpu(pRecvRep->BucketContext[i]);
981
982                 skb = priv->RcvCtl[ctx].skb;
983
984 //              dlprintk((KERN_INFO MYNAM ": %s: dev_name = %s\n",
985 //                              IOC_AND_NETDEV_NAMES_s_s(dev)));
986 //              dlprintk((KERN_INFO MYNAM "@rpr[2], priv = %p, buckets_out addr = %p",
987 //                              priv, &(priv->buckets_out)));
988 //              dlprintk((KERN_INFO MYNAM "@rpr[2] TC + 3\n"));
989
990                 priv->RcvCtl[ctx].skb = NULL;
991                 pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
992                                  priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
993                 dev_kfree_skb_any(skb);
994
995                 priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
996         }
997         spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
998
999         atomic_sub(count, &priv->buckets_out);
1000
1001 //      for (i = 0; i < priv->max_buckets_out; i++)
1002 //              if (priv->RcvCtl[i].skb != NULL)
1003 //                      dlprintk((KERN_INFO MYNAM "@rpr: bucket %03x "
1004 //                                "is still out\n", i));
1005
1006 /*      dlprintk((KERN_INFO MYNAM "/receive_post_reply: freed %d buckets\n",
1007                   count));
1008 */
1009 /**/    dlprintk((KERN_INFO MYNAM "@receive_post_reply: %d buckets "
1010 /**/              "remaining, %d received back since sod.\n",
1011 /**/              atomic_read(&priv->buckets_out), priv->total_received));
1012         return 0;
1013 }
1014
1015 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1016 static int
1017 mpt_lan_receive_post_reply(struct net_device *dev,
1018                            LANReceivePostReply_t *pRecvRep)
1019 {
1020         struct mpt_lan_priv *priv = dev->priv;
1021         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
1022         struct sk_buff *skb, *old_skb;
1023         unsigned long flags;
1024         u32 len, ctx, offset;
1025         u32 remaining = le32_to_cpu(pRecvRep->BucketsRemaining);
1026         int count;
1027         int i, l;
1028
1029         dioprintk((KERN_INFO MYNAM ": mpt_lan_receive_post_reply called\n"));
1030         dioprintk((KERN_INFO MYNAM ": receive_post_reply: IOCStatus: %04x\n",
1031                  le16_to_cpu(pRecvRep->IOCStatus)));
1032
1033         if ((le16_to_cpu(pRecvRep->IOCStatus) & MPI_IOCSTATUS_MASK) ==
1034                                                 MPI_IOCSTATUS_LAN_CANCELED)
1035                 return mpt_lan_receive_post_free(dev, pRecvRep);
1036
1037         len = le32_to_cpu(pRecvRep->PacketLength);
1038         if (len == 0) {
1039                 printk (KERN_ERR MYNAM ": %s/%s: ERROR - Got a non-TURBO "
1040                         "ReceivePostReply w/ PacketLength zero!\n",
1041                                 IOC_AND_NETDEV_NAMES_s_s(dev));
1042                 printk (KERN_ERR MYNAM ": MsgFlags = %02x, IOCStatus = %04x\n",
1043                                 pRecvRep->MsgFlags, le16_to_cpu(pRecvRep->IOCStatus));
1044                 return -1;
1045         }
1046
1047         ctx    = le32_to_cpu(pRecvRep->BucketContext[0]);
1048         count  = pRecvRep->NumberOfContexts;
1049         skb    = priv->RcvCtl[ctx].skb;
1050
1051         offset = le32_to_cpu(pRecvRep->PacketOffset);
1052 //      if (offset != 0) {
1053 //              printk (KERN_INFO MYNAM ": %s/%s: Got a ReceivePostReply "
1054 //                      "w/ PacketOffset %u\n",
1055 //                              IOC_AND_NETDEV_NAMES_s_s(dev),
1056 //                              offset);
1057 //      }
1058
1059         dioprintk((KERN_INFO MYNAM ": %s/%s: @rpr, offset = %d, len = %d\n",
1060                         IOC_AND_NETDEV_NAMES_s_s(dev),
1061                         offset, len));
1062
1063         if (count > 1) {
1064                 int szrem = len;
1065
1066 //              dioprintk((KERN_INFO MYNAM ": %s/%s: Multiple buckets returned "
1067 //                      "for single packet, concatenating...\n",
1068 //                              IOC_AND_NETDEV_NAMES_s_s(dev)));
1069
1070                 skb = (struct sk_buff *)dev_alloc_skb(len);
1071                 if (!skb) {
1072                         printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
1073                                         IOC_AND_NETDEV_NAMES_s_s(dev),
1074                                         __FILE__, __LINE__);
1075                         return -ENOMEM;
1076                 }
1077
1078                 spin_lock_irqsave(&priv->rxfidx_lock, flags);
1079                 for (i = 0; i < count; i++) {
1080
1081                         ctx = le32_to_cpu(pRecvRep->BucketContext[i]);
1082                         old_skb = priv->RcvCtl[ctx].skb;
1083
1084                         l = priv->RcvCtl[ctx].len;
1085                         if (szrem < l)
1086                                 l = szrem;
1087
1088 //                      dioprintk((KERN_INFO MYNAM ": %s/%s: Buckets = %d, len = %u\n",
1089 //                                      IOC_AND_NETDEV_NAMES_s_s(dev),
1090 //                                      i, l));
1091
1092                         pci_dma_sync_single_for_cpu(mpt_dev->pcidev,
1093                                                     priv->RcvCtl[ctx].dma,
1094                                                     priv->RcvCtl[ctx].len,
1095                                                     PCI_DMA_FROMDEVICE);
1096                         skb_copy_from_linear_data(old_skb, skb_put(skb, l), l);
1097
1098                         pci_dma_sync_single_for_device(mpt_dev->pcidev,
1099                                                        priv->RcvCtl[ctx].dma,
1100                                                        priv->RcvCtl[ctx].len,
1101                                                        PCI_DMA_FROMDEVICE);
1102
1103                         priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
1104                         szrem -= l;
1105                 }
1106                 spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
1107
1108         } else if (len < MPT_LAN_RX_COPYBREAK) {
1109
1110                 old_skb = skb;
1111
1112                 skb = (struct sk_buff *)dev_alloc_skb(len);
1113                 if (!skb) {
1114                         printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
1115                                         IOC_AND_NETDEV_NAMES_s_s(dev),
1116                                         __FILE__, __LINE__);
1117                         return -ENOMEM;
1118                 }
1119
1120                 pci_dma_sync_single_for_cpu(mpt_dev->pcidev,
1121                                             priv->RcvCtl[ctx].dma,
1122                                             priv->RcvCtl[ctx].len,
1123                                             PCI_DMA_FROMDEVICE);
1124
1125                 skb_copy_from_linear_data(old_skb, skb_put(skb, len), len);
1126
1127                 pci_dma_sync_single_for_device(mpt_dev->pcidev,
1128                                                priv->RcvCtl[ctx].dma,
1129                                                priv->RcvCtl[ctx].len,
1130                                                PCI_DMA_FROMDEVICE);
1131
1132                 spin_lock_irqsave(&priv->rxfidx_lock, flags);
1133                 priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
1134                 spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
1135
1136         } else {
1137                 spin_lock_irqsave(&priv->rxfidx_lock, flags);
1138
1139                 priv->RcvCtl[ctx].skb = NULL;
1140
1141                 pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
1142                                  priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
1143                 priv->RcvCtl[ctx].dma = 0;
1144
1145                 priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
1146                 spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
1147
1148                 skb_put(skb,len);
1149         }
1150
1151         atomic_sub(count, &priv->buckets_out);
1152         priv->total_received += count;
1153
1154         if (priv->mpt_rxfidx_tail >= MPT_LAN_MAX_BUCKETS_OUT) {
1155                 printk (KERN_ERR MYNAM ": %s/%s: Yoohoo! mpt_rxfidx_tail = %d, "
1156                         "MPT_LAN_MAX_BUCKETS_OUT = %d\n",
1157                                 IOC_AND_NETDEV_NAMES_s_s(dev),
1158                                 priv->mpt_rxfidx_tail,
1159                                 MPT_LAN_MAX_BUCKETS_OUT);
1160
1161                 return -1;
1162         }
1163
1164         if (remaining == 0)
1165                 printk (KERN_WARNING MYNAM ": %s/%s: WARNING - IOC out of buckets! "
1166                         "(priv->buckets_out = %d)\n",
1167                         IOC_AND_NETDEV_NAMES_s_s(dev),
1168                         atomic_read(&priv->buckets_out));
1169         else if (remaining < 10)
1170                 printk (KERN_INFO MYNAM ": %s/%s: IOC says %d buckets left. "
1171                         "(priv->buckets_out = %d)\n",
1172                         IOC_AND_NETDEV_NAMES_s_s(dev),
1173                         remaining, atomic_read(&priv->buckets_out));
1174         
1175         if ((remaining < priv->bucketthresh) &&
1176             ((atomic_read(&priv->buckets_out) - remaining) >
1177              MPT_LAN_BUCKETS_REMAIN_MISMATCH_THRESH)) {
1178                 
1179                 printk (KERN_WARNING MYNAM " Mismatch between driver's "
1180                         "buckets_out count and fw's BucketsRemaining "
1181                         "count has crossed the threshold, issuing a "
1182                         "LanReset to clear the fw's hashtable. You may "
1183                         "want to check your /var/log/messages for \"CRC "
1184                         "error\" event notifications.\n");
1185                 
1186                 mpt_lan_reset(dev);
1187                 mpt_lan_wake_post_buckets_task(dev, 0);
1188         }
1189         
1190         return mpt_lan_receive_skb(dev, skb);
1191 }
1192
1193 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1194 /* Simple SGE's only at the moment */
1195
1196 static void
1197 mpt_lan_post_receive_buckets(struct mpt_lan_priv *priv)
1198 {
1199         struct net_device *dev = priv->dev;
1200         MPT_ADAPTER *mpt_dev = priv->mpt_dev;
1201         MPT_FRAME_HDR *mf;
1202         LANReceivePostRequest_t *pRecvReq;
1203         SGETransaction32_t *pTrans;
1204         SGESimple64_t *pSimple;
1205         struct sk_buff *skb;
1206         dma_addr_t dma;
1207         u32 curr, buckets, count, max;
1208         u32 len = (dev->mtu + dev->hard_header_len + 4);
1209         unsigned long flags;
1210         int i;
1211
1212         curr = atomic_read(&priv->buckets_out);
1213         buckets = (priv->max_buckets_out - curr);
1214
1215         dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, Start_buckets = %u, buckets_out = %u\n",
1216                         IOC_AND_NETDEV_NAMES_s_s(dev),
1217                         __FUNCTION__, buckets, curr));
1218
1219         max = (mpt_dev->req_sz - MPT_LAN_RECEIVE_POST_REQUEST_SIZE) /
1220                         (MPT_LAN_TRANSACTION32_SIZE + sizeof(SGESimple64_t));
1221
1222         while (buckets) {
1223                 mf = mpt_get_msg_frame(LanCtx, mpt_dev);
1224                 if (mf == NULL) {
1225                         printk (KERN_ERR "%s: Unable to alloc request frame\n",
1226                                 __FUNCTION__);
1227                         dioprintk((KERN_ERR "%s: %u buckets remaining\n",
1228                                  __FUNCTION__, buckets));
1229                         goto out;
1230                 }
1231                 pRecvReq = (LANReceivePostRequest_t *) mf;
1232
1233                 count = buckets;
1234                 if (count > max)
1235                         count = max;
1236
1237                 pRecvReq->Function    = MPI_FUNCTION_LAN_RECEIVE;
1238                 pRecvReq->ChainOffset = 0;
1239                 pRecvReq->MsgFlags    = 0;
1240                 pRecvReq->PortNumber  = priv->pnum;
1241
1242                 pTrans = (SGETransaction32_t *) pRecvReq->SG_List;
1243                 pSimple = NULL;
1244
1245                 for (i = 0; i < count; i++) {
1246                         int ctx;
1247
1248                         spin_lock_irqsave(&priv->rxfidx_lock, flags);
1249                         if (priv->mpt_rxfidx_tail < 0) {
1250                                 printk (KERN_ERR "%s: Can't alloc context\n",
1251                                         __FUNCTION__);
1252                                 spin_unlock_irqrestore(&priv->rxfidx_lock,
1253                                                        flags);
1254                                 break;
1255                         }
1256
1257                         ctx = priv->mpt_rxfidx[priv->mpt_rxfidx_tail--];
1258
1259                         skb = priv->RcvCtl[ctx].skb;
1260                         if (skb && (priv->RcvCtl[ctx].len != len)) {
1261                                 pci_unmap_single(mpt_dev->pcidev,
1262                                                  priv->RcvCtl[ctx].dma,
1263                                                  priv->RcvCtl[ctx].len,
1264                                                  PCI_DMA_FROMDEVICE);
1265                                 dev_kfree_skb(priv->RcvCtl[ctx].skb);
1266                                 skb = priv->RcvCtl[ctx].skb = NULL;
1267                         }
1268
1269                         if (skb == NULL) {
1270                                 skb = dev_alloc_skb(len);
1271                                 if (skb == NULL) {
1272                                         printk (KERN_WARNING
1273                                                 MYNAM "/%s: Can't alloc skb\n",
1274                                                 __FUNCTION__);
1275                                         priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
1276                                         spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
1277                                         break;
1278                                 }
1279
1280                                 dma = pci_map_single(mpt_dev->pcidev, skb->data,
1281                                                      len, PCI_DMA_FROMDEVICE);
1282
1283                                 priv->RcvCtl[ctx].skb = skb;
1284                                 priv->RcvCtl[ctx].dma = dma;
1285                                 priv->RcvCtl[ctx].len = len;
1286                         }
1287
1288                         spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
1289
1290                         pTrans->ContextSize   = sizeof(u32);
1291                         pTrans->DetailsLength = 0;
1292                         pTrans->Flags         = 0;
1293                         pTrans->TransactionContext[0] = cpu_to_le32(ctx);
1294
1295                         pSimple = (SGESimple64_t *) pTrans->TransactionDetails;
1296
1297                         pSimple->FlagsLength = cpu_to_le32(
1298                                 ((MPI_SGE_FLAGS_END_OF_BUFFER |
1299                                   MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1300                                   MPI_SGE_FLAGS_64_BIT_ADDRESSING) << MPI_SGE_FLAGS_SHIFT) | len);
1301                         pSimple->Address.Low = cpu_to_le32((u32) priv->RcvCtl[ctx].dma);
1302                         if (sizeof(dma_addr_t) > sizeof(u32))
1303                                 pSimple->Address.High = cpu_to_le32((u32) ((u64) priv->RcvCtl[ctx].dma >> 32));
1304                         else
1305                                 pSimple->Address.High = 0;
1306
1307                         pTrans = (SGETransaction32_t *) (pSimple + 1);
1308                 }
1309
1310                 if (pSimple == NULL) {
1311 /**/                    printk (KERN_WARNING MYNAM "/%s: No buckets posted\n",
1312 /**/                            __FUNCTION__);
1313                         mpt_free_msg_frame(mpt_dev, mf);
1314                         goto out;
1315                 }
1316
1317                 pSimple->FlagsLength |= cpu_to_le32(MPI_SGE_FLAGS_END_OF_LIST << MPI_SGE_FLAGS_SHIFT);
1318
1319                 pRecvReq->BucketCount = cpu_to_le32(i);
1320
1321 /*      printk(KERN_INFO MYNAM ": posting buckets\n   ");
1322  *      for (i = 0; i < j + 2; i ++)
1323  *          printk (" %08x", le32_to_cpu(msg[i]));
1324  *      printk ("\n");
1325  */
1326
1327                 mpt_put_msg_frame(LanCtx, mpt_dev, mf);
1328
1329                 priv->total_posted += i;
1330                 buckets -= i;
1331                 atomic_add(i, &priv->buckets_out);
1332         }
1333
1334 out:
1335         dioprintk((KERN_INFO MYNAM "/%s: End_buckets = %u, priv->buckets_out = %u\n",
1336                   __FUNCTION__, buckets, atomic_read(&priv->buckets_out)));
1337         dioprintk((KERN_INFO MYNAM "/%s: Posted %u buckets and received %u back\n",
1338         __FUNCTION__, priv->total_posted, priv->total_received));
1339
1340         clear_bit(0, &priv->post_buckets_active);
1341 }
1342
1343 static void
1344 mpt_lan_post_receive_buckets_work(struct work_struct *work)
1345 {
1346         mpt_lan_post_receive_buckets(container_of(work, struct mpt_lan_priv,
1347                                                   post_buckets_task.work));
1348 }
1349
1350 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1351 static struct net_device *
1352 mpt_register_lan_device (MPT_ADAPTER *mpt_dev, int pnum)
1353 {
1354         struct net_device *dev = alloc_fcdev(sizeof(struct mpt_lan_priv));
1355         struct mpt_lan_priv *priv = NULL;
1356         u8 HWaddr[FC_ALEN], *a;
1357
1358         if (!dev)
1359                 return NULL;
1360
1361         dev->mtu = MPT_LAN_MTU;
1362
1363         priv = netdev_priv(dev);
1364
1365         priv->dev = dev;
1366         priv->mpt_dev = mpt_dev;
1367         priv->pnum = pnum;
1368
1369         memset(&priv->post_buckets_task, 0, sizeof(priv->post_buckets_task));
1370         INIT_DELAYED_WORK(&priv->post_buckets_task,
1371                           mpt_lan_post_receive_buckets_work);
1372         priv->post_buckets_active = 0;
1373
1374         dlprintk((KERN_INFO MYNAM "@%d: bucketlen = %d\n",
1375                         __LINE__, dev->mtu + dev->hard_header_len + 4));
1376
1377         atomic_set(&priv->buckets_out, 0);
1378         priv->total_posted = 0;
1379         priv->total_received = 0;
1380         priv->max_buckets_out = max_buckets_out;
1381         if (mpt_dev->pfacts[0].MaxLanBuckets < max_buckets_out)
1382                 priv->max_buckets_out = mpt_dev->pfacts[0].MaxLanBuckets;
1383
1384         dlprintk((KERN_INFO MYNAM "@%d: MaxLanBuckets=%d, max_buckets_out/priv=%d/%d\n",
1385                         __LINE__,
1386                         mpt_dev->pfacts[0].MaxLanBuckets,
1387                         max_buckets_out,
1388                         priv->max_buckets_out));
1389
1390         priv->bucketthresh = priv->max_buckets_out * 2 / 3;
1391         spin_lock_init(&priv->txfidx_lock);
1392         spin_lock_init(&priv->rxfidx_lock);
1393
1394         memset(&priv->stats, 0, sizeof(priv->stats));
1395
1396         /*  Grab pre-fetched LANPage1 stuff. :-) */
1397         a = (u8 *) &mpt_dev->lan_cnfg_page1.HardwareAddressLow;
1398
1399         HWaddr[0] = a[5];
1400         HWaddr[1] = a[4];
1401         HWaddr[2] = a[3];
1402         HWaddr[3] = a[2];
1403         HWaddr[4] = a[1];
1404         HWaddr[5] = a[0];
1405
1406         dev->addr_len = FC_ALEN;
1407         memcpy(dev->dev_addr, HWaddr, FC_ALEN);
1408         memset(dev->broadcast, 0xff, FC_ALEN);
1409
1410         /* The Tx queue is 127 deep on the 909.
1411          * Give ourselves some breathing room.
1412          */
1413         priv->tx_max_out = (tx_max_out_p <= MPT_TX_MAX_OUT_LIM) ?
1414                             tx_max_out_p : MPT_TX_MAX_OUT_LIM;
1415
1416         dev->open = mpt_lan_open;
1417         dev->stop = mpt_lan_close;
1418         dev->get_stats = mpt_lan_get_stats;
1419         dev->set_multicast_list = NULL;
1420         dev->change_mtu = mpt_lan_change_mtu;
1421         dev->hard_start_xmit = mpt_lan_sdu_send;
1422
1423 /* Not in 2.3.42. Need 2.3.45+ */
1424         dev->tx_timeout = mpt_lan_tx_timeout;
1425         dev->watchdog_timeo = MPT_LAN_TX_TIMEOUT;
1426
1427         dlprintk((KERN_INFO MYNAM ": Finished registering dev "
1428                 "and setting initial values\n"));
1429
1430         SET_MODULE_OWNER(dev);
1431
1432         if (register_netdev(dev) != 0) {
1433                 free_netdev(dev);
1434                 dev = NULL;
1435         }
1436         return dev;
1437 }
1438
1439 static int
1440 mptlan_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1441 {
1442         MPT_ADAPTER             *ioc = pci_get_drvdata(pdev);
1443         struct net_device       *dev;
1444         int                     i;
1445
1446         for (i = 0; i < ioc->facts.NumberOfPorts; i++) {
1447                 printk(KERN_INFO MYNAM ": %s: PortNum=%x, "
1448                        "ProtocolFlags=%02Xh (%c%c%c%c)\n",
1449                        ioc->name, ioc->pfacts[i].PortNumber,
1450                        ioc->pfacts[i].ProtocolFlags,
1451                        MPT_PROTOCOL_FLAGS_c_c_c_c(
1452                                ioc->pfacts[i].ProtocolFlags));
1453
1454                 if (!(ioc->pfacts[i].ProtocolFlags &
1455                                         MPI_PORTFACTS_PROTOCOL_LAN)) {
1456                         printk(KERN_INFO MYNAM ": %s: Hmmm... LAN protocol "
1457                                "seems to be disabled on this adapter port!\n",
1458                                ioc->name);
1459                         continue;
1460                 }
1461
1462                 dev = mpt_register_lan_device(ioc, i);
1463                 if (!dev) {
1464                         printk(KERN_ERR MYNAM ": %s: Unable to register "
1465                                "port%d as a LAN device\n", ioc->name,
1466                                ioc->pfacts[i].PortNumber);
1467                         continue;
1468                 }
1469                 
1470                 printk(KERN_INFO MYNAM ": %s: Fusion MPT LAN device "
1471                        "registered as '%s'\n", ioc->name, dev->name);
1472                 printk(KERN_INFO MYNAM ": %s/%s: "
1473                        "LanAddr = %02X:%02X:%02X:%02X:%02X:%02X\n",
1474                        IOC_AND_NETDEV_NAMES_s_s(dev),
1475                        dev->dev_addr[0], dev->dev_addr[1],
1476                        dev->dev_addr[2], dev->dev_addr[3],
1477                        dev->dev_addr[4], dev->dev_addr[5]);
1478         
1479                 ioc->netdev = dev;
1480
1481                 return 0;
1482         }
1483
1484         return -ENODEV;
1485 }
1486
1487 static void
1488 mptlan_remove(struct pci_dev *pdev)
1489 {
1490         MPT_ADAPTER             *ioc = pci_get_drvdata(pdev);
1491         struct net_device       *dev = ioc->netdev;
1492
1493         if(dev != NULL) {
1494                 unregister_netdev(dev);
1495                 free_netdev(dev);
1496         }
1497 }
1498
1499 static struct mpt_pci_driver mptlan_driver = {
1500         .probe          = mptlan_probe,
1501         .remove         = mptlan_remove,
1502 };
1503
1504 static int __init mpt_lan_init (void)
1505 {
1506         show_mptmod_ver(LANAME, LANVER);
1507
1508         if ((LanCtx = mpt_register(lan_reply, MPTLAN_DRIVER)) <= 0) {
1509                 printk (KERN_ERR MYNAM ": Failed to register with MPT base driver\n");
1510                 return -EBUSY;
1511         }
1512
1513         /* Set the callback index to be used by driver core for turbo replies */
1514         mpt_lan_index = LanCtx;
1515
1516         dlprintk((KERN_INFO MYNAM ": assigned context of %d\n", LanCtx));
1517
1518         if (mpt_reset_register(LanCtx, mpt_lan_ioc_reset)) {
1519                 printk(KERN_ERR MYNAM ": Eieee! unable to register a reset "
1520                        "handler with mptbase! The world is at an end! "
1521                        "Everything is fading to black! Goodbye.\n");
1522                 return -EBUSY;
1523         }
1524
1525         dlprintk((KERN_INFO MYNAM ": Registered for IOC reset notifications\n"));
1526         
1527         mpt_device_driver_register(&mptlan_driver, MPTLAN_DRIVER);
1528         return 0;
1529 }
1530
1531 static void __exit mpt_lan_exit(void)
1532 {
1533         mpt_device_driver_deregister(MPTLAN_DRIVER);
1534         mpt_reset_deregister(LanCtx);
1535
1536         if (LanCtx >= 0) {
1537                 mpt_deregister(LanCtx);
1538                 LanCtx = -1;
1539                 mpt_lan_index = 0;
1540         }
1541 }
1542
1543 module_init(mpt_lan_init);
1544 module_exit(mpt_lan_exit);
1545
1546 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1547 static unsigned short
1548 mpt_lan_type_trans(struct sk_buff *skb, struct net_device *dev)
1549 {
1550         struct mpt_lan_ohdr *fch = (struct mpt_lan_ohdr *)skb->data;
1551         struct fcllc *fcllc;
1552
1553         skb_reset_mac_header(skb);
1554         skb_pull(skb, sizeof(struct mpt_lan_ohdr));
1555
1556         if (fch->dtype == htons(0xffff)) {
1557                 u32 *p = (u32 *) fch;
1558
1559                 swab32s(p + 0);
1560                 swab32s(p + 1);
1561                 swab32s(p + 2);
1562                 swab32s(p + 3);
1563
1564                 printk (KERN_WARNING MYNAM ": %s: WARNING - Broadcast swap F/W bug detected!\n",
1565                                 NETDEV_PTR_TO_IOC_NAME_s(dev));
1566                 printk (KERN_WARNING MYNAM ": Please update sender @ MAC_addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
1567                                 fch->saddr[0], fch->saddr[1], fch->saddr[2],
1568                                 fch->saddr[3], fch->saddr[4], fch->saddr[5]);
1569         }
1570
1571         if (*fch->daddr & 1) {
1572                 if (!memcmp(fch->daddr, dev->broadcast, FC_ALEN)) {
1573                         skb->pkt_type = PACKET_BROADCAST;
1574                 } else {
1575                         skb->pkt_type = PACKET_MULTICAST;
1576                 }
1577         } else {
1578                 if (memcmp(fch->daddr, dev->dev_addr, FC_ALEN)) {
1579                         skb->pkt_type = PACKET_OTHERHOST;
1580                 } else {
1581                         skb->pkt_type = PACKET_HOST;
1582                 }
1583         }
1584
1585         fcllc = (struct fcllc *)skb->data;
1586
1587 #ifdef QLOGIC_NAA_WORKAROUND
1588 {
1589         u16 source_naa = fch->stype, found = 0;
1590
1591         /* Workaround for QLogic not following RFC 2625 in regards to the NAA
1592            value. */
1593
1594         if ((source_naa & 0xF000) == 0)
1595                 source_naa = swab16(source_naa);
1596
1597         if (fcllc->ethertype == htons(ETH_P_ARP))
1598             dlprintk ((KERN_INFO "mptlan/type_trans: got arp req/rep w/ naa of "
1599                       "%04x.\n", source_naa));
1600
1601         if ((fcllc->ethertype == htons(ETH_P_ARP)) &&
1602            ((source_naa >> 12) !=  MPT_LAN_NAA_RFC2625)){
1603                 struct NAA_Hosed *nh, *prevnh;
1604                 int i;
1605
1606                 dlprintk ((KERN_INFO "mptlan/type_trans: ARP Req/Rep from "
1607                           "system with non-RFC 2625 NAA value (%04x).\n",
1608                           source_naa));
1609
1610                 write_lock_irq(&bad_naa_lock);
1611                 for (prevnh = nh = mpt_bad_naa; nh != NULL;
1612                      prevnh=nh, nh=nh->next) {
1613                         if ((nh->ieee[0] == fch->saddr[0]) &&
1614                             (nh->ieee[1] == fch->saddr[1]) &&
1615                             (nh->ieee[2] == fch->saddr[2]) &&
1616                             (nh->ieee[3] == fch->saddr[3]) &&
1617                             (nh->ieee[4] == fch->saddr[4]) &&
1618                             (nh->ieee[5] == fch->saddr[5])) {
1619                                 found = 1;
1620                                 dlprintk ((KERN_INFO "mptlan/type_trans: ARP Re"
1621                                          "q/Rep w/ bad NAA from system already"
1622                                          " in DB.\n"));
1623                                 break;
1624                         }
1625                 }
1626
1627                 if ((!found) && (nh == NULL)) {
1628
1629                         nh = kmalloc(sizeof(struct NAA_Hosed), GFP_KERNEL);
1630                         dlprintk ((KERN_INFO "mptlan/type_trans: ARP Req/Rep w/"
1631                                  " bad NAA from system not yet in DB.\n"));
1632
1633                         if (nh != NULL) {
1634                                 nh->next = NULL;
1635                                 if (!mpt_bad_naa)
1636                                         mpt_bad_naa = nh;
1637                                 if (prevnh)
1638                                         prevnh->next = nh;
1639
1640                                 nh->NAA = source_naa; /* Set the S_NAA value. */
1641                                 for (i = 0; i < FC_ALEN; i++)
1642                                         nh->ieee[i] = fch->saddr[i];
1643                                 dlprintk ((KERN_INFO "Got ARP from %02x:%02x:%02x:%02x:"
1644                                           "%02x:%02x with non-compliant S_NAA value.\n",
1645                                           fch->saddr[0], fch->saddr[1], fch->saddr[2],
1646                                           fch->saddr[3], fch->saddr[4],fch->saddr[5]));
1647                         } else {
1648                                 printk (KERN_ERR "mptlan/type_trans: Unable to"
1649                                         " kmalloc a NAA_Hosed struct.\n");
1650                         }
1651                 } else if (!found) {
1652                         printk (KERN_ERR "mptlan/type_trans: found not"
1653                                 " set, but nh isn't null. Evil "
1654                                 "funkiness abounds.\n");
1655                 }
1656                 write_unlock_irq(&bad_naa_lock);
1657         }
1658 }
1659 #endif
1660
1661         /* Strip the SNAP header from ARP packets since we don't
1662          * pass them through to the 802.2/SNAP layers.
1663          */
1664         if (fcllc->dsap == EXTENDED_SAP &&
1665                 (fcllc->ethertype == htons(ETH_P_IP) ||
1666                  fcllc->ethertype == htons(ETH_P_ARP))) {
1667                 skb_pull(skb, sizeof(struct fcllc));
1668                 return fcllc->ethertype;
1669         }
1670
1671         return htons(ETH_P_802_2);
1672 }
1673
1674 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/