Net, libertas: Resolve memory leak in if_spi_host_to_card()
[linux-2.6.git] / drivers / net / wireless / libertas / if_spi.c
1 /*
2  *      linux/drivers/net/wireless/libertas/if_spi.c
3  *
4  *      Driver for Marvell SPI WLAN cards.
5  *
6  *      Copyright 2008 Analog Devices Inc.
7  *
8  *      Authors:
9  *      Andrey Yurovsky <andrey@cozybit.com>
10  *      Colin McCabe <colin@cozybit.com>
11  *
12  *      Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  */
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/hardirq.h>
23 #include <linux/interrupt.h>
24 #include <linux/moduleparam.h>
25 #include <linux/firmware.h>
26 #include <linux/jiffies.h>
27 #include <linux/list.h>
28 #include <linux/netdevice.h>
29 #include <linux/slab.h>
30 #include <linux/spi/libertas_spi.h>
31 #include <linux/spi/spi.h>
32
33 #include "host.h"
34 #include "decl.h"
35 #include "defs.h"
36 #include "dev.h"
37 #include "if_spi.h"
38
39 struct if_spi_packet {
40         struct list_head                list;
41         u16                             blen;
42         u8                              buffer[0] __attribute__((aligned(4)));
43 };
44
45 struct if_spi_card {
46         struct spi_device               *spi;
47         struct lbs_private              *priv;
48         struct libertas_spi_platform_data *pdata;
49
50         /* The card ID and card revision, as reported by the hardware. */
51         u16                             card_id;
52         u8                              card_rev;
53
54         /* The last time that we initiated an SPU operation */
55         unsigned long                   prev_xfer_time;
56
57         int                             use_dummy_writes;
58         unsigned long                   spu_port_delay;
59         unsigned long                   spu_reg_delay;
60
61         /* Handles all SPI communication (except for FW load) */
62         struct workqueue_struct         *workqueue;
63         struct work_struct              packet_work;
64         struct work_struct              resume_work;
65
66         u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
67
68         /* A buffer of incoming packets from libertas core.
69          * Since we can't sleep in hw_host_to_card, we have to buffer
70          * them. */
71         struct list_head                cmd_packet_list;
72         struct list_head                data_packet_list;
73
74         /* Protects cmd_packet_list and data_packet_list */
75         spinlock_t                      buffer_lock;
76
77         /* True is card suspended */
78         u8                              suspended;
79 };
80
81 static void free_if_spi_card(struct if_spi_card *card)
82 {
83         struct list_head *cursor, *next;
84         struct if_spi_packet *packet;
85
86         list_for_each_safe(cursor, next, &card->cmd_packet_list) {
87                 packet = container_of(cursor, struct if_spi_packet, list);
88                 list_del(&packet->list);
89                 kfree(packet);
90         }
91         list_for_each_safe(cursor, next, &card->data_packet_list) {
92                 packet = container_of(cursor, struct if_spi_packet, list);
93                 list_del(&packet->list);
94                 kfree(packet);
95         }
96         spi_set_drvdata(card->spi, NULL);
97         kfree(card);
98 }
99
100 #define MODEL_8385      0x04
101 #define MODEL_8686      0x0b
102 #define MODEL_8688      0x10
103
104 static const struct lbs_fw_table fw_table[] = {
105         { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
106         { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
107         { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
108         { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
109         { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
110         { 0, NULL, NULL }
111 };
112 MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
113 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
114 MODULE_FIRMWARE("libertas/gspi8385.bin");
115 MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
116 MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
117 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
118 MODULE_FIRMWARE("libertas/gspi8686.bin");
119 MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
120 MODULE_FIRMWARE("libertas/gspi8688.bin");
121
122
123 /*
124  * SPI Interface Unit Routines
125  *
126  * The SPU sits between the host and the WLAN module.
127  * All communication with the firmware is through SPU transactions.
128  *
129  * First we have to put a SPU register name on the bus. Then we can
130  * either read from or write to that register.
131  *
132  */
133
134 static void spu_transaction_init(struct if_spi_card *card)
135 {
136         if (!time_after(jiffies, card->prev_xfer_time + 1)) {
137                 /* Unfortunately, the SPU requires a delay between successive
138                  * transactions. If our last transaction was more than a jiffy
139                  * ago, we have obviously already delayed enough.
140                  * If not, we have to busy-wait to be on the safe side. */
141                 ndelay(400);
142         }
143 }
144
145 static void spu_transaction_finish(struct if_spi_card *card)
146 {
147         card->prev_xfer_time = jiffies;
148 }
149
150 /*
151  * Write out a byte buffer to an SPI register,
152  * using a series of 16-bit transfers.
153  */
154 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
155 {
156         int err = 0;
157         __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
158         struct spi_message m;
159         struct spi_transfer reg_trans;
160         struct spi_transfer data_trans;
161
162         spi_message_init(&m);
163         memset(&reg_trans, 0, sizeof(reg_trans));
164         memset(&data_trans, 0, sizeof(data_trans));
165
166         /* You must give an even number of bytes to the SPU, even if it
167          * doesn't care about the last one.  */
168         BUG_ON(len & 0x1);
169
170         spu_transaction_init(card);
171
172         /* write SPU register index */
173         reg_trans.tx_buf = &reg_out;
174         reg_trans.len = sizeof(reg_out);
175
176         data_trans.tx_buf = buf;
177         data_trans.len = len;
178
179         spi_message_add_tail(&reg_trans, &m);
180         spi_message_add_tail(&data_trans, &m);
181
182         err = spi_sync(card->spi, &m);
183         spu_transaction_finish(card);
184         return err;
185 }
186
187 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
188 {
189         __le16 buff;
190
191         buff = cpu_to_le16(val);
192         return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
193 }
194
195 static inline int spu_reg_is_port_reg(u16 reg)
196 {
197         switch (reg) {
198         case IF_SPI_IO_RDWRPORT_REG:
199         case IF_SPI_CMD_RDWRPORT_REG:
200         case IF_SPI_DATA_RDWRPORT_REG:
201                 return 1;
202         default:
203                 return 0;
204         }
205 }
206
207 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
208 {
209         unsigned int delay;
210         int err = 0;
211         __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
212         struct spi_message m;
213         struct spi_transfer reg_trans;
214         struct spi_transfer dummy_trans;
215         struct spi_transfer data_trans;
216
217         /*
218          * You must take an even number of bytes from the SPU, even if you
219          * don't care about the last one.
220          */
221         BUG_ON(len & 0x1);
222
223         spu_transaction_init(card);
224
225         spi_message_init(&m);
226         memset(&reg_trans, 0, sizeof(reg_trans));
227         memset(&dummy_trans, 0, sizeof(dummy_trans));
228         memset(&data_trans, 0, sizeof(data_trans));
229
230         /* write SPU register index */
231         reg_trans.tx_buf = &reg_out;
232         reg_trans.len = sizeof(reg_out);
233         spi_message_add_tail(&reg_trans, &m);
234
235         delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
236                                                 card->spu_reg_delay;
237         if (card->use_dummy_writes) {
238                 /* Clock in dummy cycles while the SPU fills the FIFO */
239                 dummy_trans.len = delay / 8;
240                 spi_message_add_tail(&dummy_trans, &m);
241         } else {
242                 /* Busy-wait while the SPU fills the FIFO */
243                 reg_trans.delay_usecs =
244                         DIV_ROUND_UP((100 + (delay * 10)), 1000);
245         }
246
247         /* read in data */
248         data_trans.rx_buf = buf;
249         data_trans.len = len;
250         spi_message_add_tail(&data_trans, &m);
251
252         err = spi_sync(card->spi, &m);
253         spu_transaction_finish(card);
254         return err;
255 }
256
257 /* Read 16 bits from an SPI register */
258 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
259 {
260         __le16 buf;
261         int ret;
262
263         ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
264         if (ret == 0)
265                 *val = le16_to_cpup(&buf);
266         return ret;
267 }
268
269 /*
270  * Read 32 bits from an SPI register.
271  * The low 16 bits are read first.
272  */
273 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
274 {
275         __le32 buf;
276         int err;
277
278         err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
279         if (!err)
280                 *val = le32_to_cpup(&buf);
281         return err;
282 }
283
284 /*
285  * Keep reading 16 bits from an SPI register until you get the correct result.
286  *
287  * If mask = 0, the correct result is any non-zero number.
288  * If mask != 0, the correct result is any number where
289  * number & target_mask == target
290  *
291  * Returns -ETIMEDOUT if a second passes without the correct result.
292  */
293 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
294                         u16 target_mask, u16 target)
295 {
296         int err;
297         unsigned long timeout = jiffies + 5*HZ;
298         while (1) {
299                 u16 val;
300                 err = spu_read_u16(card, reg, &val);
301                 if (err)
302                         return err;
303                 if (target_mask) {
304                         if ((val & target_mask) == target)
305                                 return 0;
306                 } else {
307                         if (val)
308                                 return 0;
309                 }
310                 udelay(100);
311                 if (time_after(jiffies, timeout)) {
312                         pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
313                                __func__, val, target_mask, target);
314                         return -ETIMEDOUT;
315                 }
316         }
317 }
318
319 /*
320  * Read 16 bits from an SPI register until you receive a specific value.
321  * Returns -ETIMEDOUT if a 4 tries pass without success.
322  */
323 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
324 {
325         int err, try;
326         for (try = 0; try < 4; ++try) {
327                 u32 val = 0;
328                 err = spu_read_u32(card, reg, &val);
329                 if (err)
330                         return err;
331                 if (val == target)
332                         return 0;
333                 mdelay(100);
334         }
335         return -ETIMEDOUT;
336 }
337
338 static int spu_set_interrupt_mode(struct if_spi_card *card,
339                            int suppress_host_int,
340                            int auto_int)
341 {
342         int err = 0;
343
344         /*
345          * We can suppress a host interrupt by clearing the appropriate
346          * bit in the "host interrupt status mask" register
347          */
348         if (suppress_host_int) {
349                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
350                 if (err)
351                         return err;
352         } else {
353                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
354                               IF_SPI_HISM_TX_DOWNLOAD_RDY |
355                               IF_SPI_HISM_RX_UPLOAD_RDY |
356                               IF_SPI_HISM_CMD_DOWNLOAD_RDY |
357                               IF_SPI_HISM_CARDEVENT |
358                               IF_SPI_HISM_CMD_UPLOAD_RDY);
359                 if (err)
360                         return err;
361         }
362
363         /*
364          * If auto-interrupts are on, the completion of certain transactions
365          * will trigger an interrupt automatically. If auto-interrupts
366          * are off, we need to set the "Card Interrupt Cause" register to
367          * trigger a card interrupt.
368          */
369         if (auto_int) {
370                 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
371                                 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
372                                 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
373                                 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
374                                 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
375                 if (err)
376                         return err;
377         } else {
378                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
379                 if (err)
380                         return err;
381         }
382         return err;
383 }
384
385 static int spu_get_chip_revision(struct if_spi_card *card,
386                                   u16 *card_id, u8 *card_rev)
387 {
388         int err = 0;
389         u32 dev_ctrl;
390         err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
391         if (err)
392                 return err;
393         *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
394         *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
395         return err;
396 }
397
398 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
399 {
400         int err = 0;
401         u16 rval;
402         /* set bus mode */
403         err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
404         if (err)
405                 return err;
406         /* Check that we were able to read back what we just wrote. */
407         err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
408         if (err)
409                 return err;
410         if ((rval & 0xF) != mode) {
411                 pr_err("Can't read bus mode register\n");
412                 return -EIO;
413         }
414         return 0;
415 }
416
417 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
418 {
419         int err = 0;
420         u32 delay;
421
422         /*
423          * We have to start up in timed delay mode so that we can safely
424          * read the Delay Read Register.
425          */
426         card->use_dummy_writes = 0;
427         err = spu_set_bus_mode(card,
428                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
429                                 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
430                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
431         if (err)
432                 return err;
433         card->spu_port_delay = 1000;
434         card->spu_reg_delay = 1000;
435         err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
436         if (err)
437                 return err;
438         card->spu_port_delay = delay & 0x0000ffff;
439         card->spu_reg_delay = (delay & 0xffff0000) >> 16;
440
441         /* If dummy clock delay mode has been requested, switch to it now */
442         if (use_dummy_writes) {
443                 card->use_dummy_writes = 1;
444                 err = spu_set_bus_mode(card,
445                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
446                                 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
447                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
448                 if (err)
449                         return err;
450         }
451
452         lbs_deb_spi("Initialized SPU unit. "
453                     "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
454                     card->spu_port_delay, card->spu_reg_delay);
455         return err;
456 }
457
458 /*
459  * Firmware Loading
460  */
461
462 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
463                                         const struct firmware *firmware)
464 {
465         int err = 0;
466         int bytes_remaining;
467         const u8 *fw;
468         u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
469
470         lbs_deb_enter(LBS_DEB_SPI);
471
472         err = spu_set_interrupt_mode(card, 1, 0);
473         if (err)
474                 goto out;
475
476         bytes_remaining = firmware->size;
477         fw = firmware->data;
478
479         /* Load helper firmware image */
480         while (bytes_remaining > 0) {
481                 /*
482                  * Scratch pad 1 should contain the number of bytes we
483                  * want to download to the firmware
484                  */
485                 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
486                                         HELPER_FW_LOAD_CHUNK_SZ);
487                 if (err)
488                         goto out;
489
490                 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
491                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY,
492                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY);
493                 if (err)
494                         goto out;
495
496                 /*
497                  * Feed the data into the command read/write port reg
498                  * in chunks of 64 bytes
499                  */
500                 memset(temp, 0, sizeof(temp));
501                 memcpy(temp, fw,
502                        min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
503                 mdelay(10);
504                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
505                                         temp, HELPER_FW_LOAD_CHUNK_SZ);
506                 if (err)
507                         goto out;
508
509                 /* Interrupt the boot code */
510                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
511                 if (err)
512                         goto out;
513                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
514                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
515                 if (err)
516                         goto out;
517                 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
518                 fw += HELPER_FW_LOAD_CHUNK_SZ;
519         }
520
521         /*
522          * Once the helper / single stage firmware download is complete,
523          * write 0 to scratch pad 1 and interrupt the
524          * bootloader. This completes the helper download.
525          */
526         err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
527         if (err)
528                 goto out;
529         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
530         if (err)
531                 goto out;
532         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
533                                 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
534                 goto out;
535
536         lbs_deb_spi("waiting for helper to boot...\n");
537
538 out:
539         if (err)
540                 pr_err("failed to load helper firmware (err=%d)\n", err);
541         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
542         return err;
543 }
544
545 /*
546  * Returns the length of the next packet the firmware expects us to send.
547  * Sets crc_err if the previous transfer had a CRC error.
548  */
549 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
550                                                 int *crc_err)
551 {
552         u16 len;
553         int err = 0;
554
555         /*
556          * wait until the host interrupt status register indicates
557          * that we are ready to download
558          */
559         err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
560                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
561                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
562         if (err) {
563                 pr_err("timed out waiting for host_int_status\n");
564                 return err;
565         }
566
567         /* Ask the device how many bytes of firmware it wants. */
568         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
569         if (err)
570                 return err;
571
572         if (len > IF_SPI_CMD_BUF_SIZE) {
573                 pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
574                        len);
575                 return -EIO;
576         }
577         if (len & 0x1) {
578                 lbs_deb_spi("%s: crc error\n", __func__);
579                 len &= ~0x1;
580                 *crc_err = 1;
581         } else
582                 *crc_err = 0;
583
584         return len;
585 }
586
587 static int if_spi_prog_main_firmware(struct if_spi_card *card,
588                                         const struct firmware *firmware)
589 {
590         struct lbs_private *priv = card->priv;
591         int len, prev_len;
592         int bytes, crc_err = 0, err = 0;
593         const u8 *fw;
594         u16 num_crc_errs;
595
596         lbs_deb_enter(LBS_DEB_SPI);
597
598         err = spu_set_interrupt_mode(card, 1, 0);
599         if (err)
600                 goto out;
601
602         err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
603         if (err) {
604                 netdev_err(priv->dev,
605                            "%s: timed out waiting for initial scratch reg = 0\n",
606                            __func__);
607                 goto out;
608         }
609
610         num_crc_errs = 0;
611         prev_len = 0;
612         bytes = firmware->size;
613         fw = firmware->data;
614         while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
615                 if (len < 0) {
616                         err = len;
617                         goto out;
618                 }
619                 if (bytes < 0) {
620                         /*
621                          * If there are no more bytes left, we would normally
622                          * expect to have terminated with len = 0
623                          */
624                         netdev_err(priv->dev,
625                                    "Firmware load wants more bytes than we have to offer.\n");
626                         break;
627                 }
628                 if (crc_err) {
629                         /* Previous transfer failed. */
630                         if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
631                                 pr_err("Too many CRC errors encountered in firmware load.\n");
632                                 err = -EIO;
633                                 goto out;
634                         }
635                 } else {
636                         /* Previous transfer succeeded. Advance counters. */
637                         bytes -= prev_len;
638                         fw += prev_len;
639                 }
640                 if (bytes < len) {
641                         memset(card->cmd_buffer, 0, len);
642                         memcpy(card->cmd_buffer, fw, bytes);
643                 } else
644                         memcpy(card->cmd_buffer, fw, len);
645
646                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
647                 if (err)
648                         goto out;
649                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
650                                 card->cmd_buffer, len);
651                 if (err)
652                         goto out;
653                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
654                                         IF_SPI_CIC_CMD_DOWNLOAD_OVER);
655                 if (err)
656                         goto out;
657                 prev_len = len;
658         }
659         if (bytes > prev_len) {
660                 pr_err("firmware load wants fewer bytes than we have to offer\n");
661         }
662
663         /* Confirm firmware download */
664         err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
665                                         SUCCESSFUL_FW_DOWNLOAD_MAGIC);
666         if (err) {
667                 pr_err("failed to confirm the firmware download\n");
668                 goto out;
669         }
670
671 out:
672         if (err)
673                 pr_err("failed to load firmware (err=%d)\n", err);
674         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
675         return err;
676 }
677
678 /*
679  * SPI Transfer Thread
680  *
681  * The SPI worker handles all SPI transfers, so there is no need for a lock.
682  */
683
684 /* Move a command from the card to the host */
685 static int if_spi_c2h_cmd(struct if_spi_card *card)
686 {
687         struct lbs_private *priv = card->priv;
688         unsigned long flags;
689         int err = 0;
690         u16 len;
691         u8 i;
692
693         /*
694          * We need a buffer big enough to handle whatever people send to
695          * hw_host_to_card
696          */
697         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
698         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
699
700         /*
701          * It's just annoying if the buffer size isn't a multiple of 4, because
702          * then we might have len < IF_SPI_CMD_BUF_SIZE but
703          * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
704          */
705         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
706
707         lbs_deb_enter(LBS_DEB_SPI);
708
709         /* How many bytes are there to read? */
710         err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
711         if (err)
712                 goto out;
713         if (!len) {
714                 netdev_err(priv->dev, "%s: error: card has no data for host\n",
715                            __func__);
716                 err = -EINVAL;
717                 goto out;
718         } else if (len > IF_SPI_CMD_BUF_SIZE) {
719                 netdev_err(priv->dev,
720                            "%s: error: response packet too large: %d bytes, but maximum is %d\n",
721                            __func__, len, IF_SPI_CMD_BUF_SIZE);
722                 err = -EINVAL;
723                 goto out;
724         }
725
726         /* Read the data from the WLAN module into our command buffer */
727         err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
728                                 card->cmd_buffer, ALIGN(len, 4));
729         if (err)
730                 goto out;
731
732         spin_lock_irqsave(&priv->driver_lock, flags);
733         i = (priv->resp_idx == 0) ? 1 : 0;
734         BUG_ON(priv->resp_len[i]);
735         priv->resp_len[i] = len;
736         memcpy(priv->resp_buf[i], card->cmd_buffer, len);
737         lbs_notify_command_response(priv, i);
738         spin_unlock_irqrestore(&priv->driver_lock, flags);
739
740 out:
741         if (err)
742                 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
743         lbs_deb_leave(LBS_DEB_SPI);
744         return err;
745 }
746
747 /* Move data from the card to the host */
748 static int if_spi_c2h_data(struct if_spi_card *card)
749 {
750         struct lbs_private *priv = card->priv;
751         struct sk_buff *skb;
752         char *data;
753         u16 len;
754         int err = 0;
755
756         lbs_deb_enter(LBS_DEB_SPI);
757
758         /* How many bytes are there to read? */
759         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
760         if (err)
761                 goto out;
762         if (!len) {
763                 netdev_err(priv->dev, "%s: error: card has no data for host\n",
764                            __func__);
765                 err = -EINVAL;
766                 goto out;
767         } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
768                 netdev_err(priv->dev,
769                            "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
770                            __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
771                 err = -EINVAL;
772                 goto out;
773         }
774
775         /* TODO: should we allocate a smaller skb if we have less data? */
776         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
777         if (!skb) {
778                 err = -ENOBUFS;
779                 goto out;
780         }
781         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
782         data = skb_put(skb, len);
783
784         /* Read the data from the WLAN module into our skb... */
785         err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
786         if (err)
787                 goto free_skb;
788
789         /* pass the SKB to libertas */
790         err = lbs_process_rxed_packet(card->priv, skb);
791         if (err)
792                 goto free_skb;
793
794         /* success */
795         goto out;
796
797 free_skb:
798         dev_kfree_skb(skb);
799 out:
800         if (err)
801                 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
802         lbs_deb_leave(LBS_DEB_SPI);
803         return err;
804 }
805
806 /* Move data or a command from the host to the card. */
807 static void if_spi_h2c(struct if_spi_card *card,
808                         struct if_spi_packet *packet, int type)
809 {
810         struct lbs_private *priv = card->priv;
811         int err = 0;
812         u16 int_type, port_reg;
813
814         switch (type) {
815         case MVMS_DAT:
816                 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
817                 port_reg = IF_SPI_DATA_RDWRPORT_REG;
818                 break;
819         case MVMS_CMD:
820                 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
821                 port_reg = IF_SPI_CMD_RDWRPORT_REG;
822                 break;
823         default:
824                 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
825                            type);
826                 err = -EINVAL;
827                 goto out;
828         }
829
830         /* Write the data to the card */
831         err = spu_write(card, port_reg, packet->buffer, packet->blen);
832         if (err)
833                 goto out;
834
835 out:
836         kfree(packet);
837
838         if (err)
839                 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
840 }
841
842 /* Inform the host about a card event */
843 static void if_spi_e2h(struct if_spi_card *card)
844 {
845         int err = 0;
846         u32 cause;
847         struct lbs_private *priv = card->priv;
848
849         err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
850         if (err)
851                 goto out;
852
853         /* re-enable the card event interrupt */
854         spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
855                         ~IF_SPI_HICU_CARD_EVENT);
856
857         /* generate a card interrupt */
858         spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
859
860         lbs_queue_event(priv, cause & 0xff);
861 out:
862         if (err)
863                 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
864 }
865
866 static void if_spi_host_to_card_worker(struct work_struct *work)
867 {
868         int err;
869         struct if_spi_card *card;
870         u16 hiStatus;
871         unsigned long flags;
872         struct if_spi_packet *packet;
873         struct lbs_private *priv;
874
875         card = container_of(work, struct if_spi_card, packet_work);
876         priv = card->priv;
877
878         lbs_deb_enter(LBS_DEB_SPI);
879
880         /*
881          * Read the host interrupt status register to see what we
882          * can do.
883          */
884         err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
885                                 &hiStatus);
886         if (err) {
887                 netdev_err(priv->dev, "I/O error\n");
888                 goto err;
889         }
890
891         if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
892                 err = if_spi_c2h_cmd(card);
893                 if (err)
894                         goto err;
895         }
896         if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
897                 err = if_spi_c2h_data(card);
898                 if (err)
899                         goto err;
900         }
901
902         /*
903          * workaround: in PS mode, the card does not set the Command
904          * Download Ready bit, but it sets TX Download Ready.
905          */
906         if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
907            (card->priv->psstate != PS_STATE_FULL_POWER &&
908             (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
909                 /*
910                  * This means two things. First of all,
911                  * if there was a previous command sent, the card has
912                  * successfully received it.
913                  * Secondly, it is now ready to download another
914                  * command.
915                  */
916                 lbs_host_to_card_done(card->priv);
917
918                 /* Do we have any command packets from the host to send? */
919                 packet = NULL;
920                 spin_lock_irqsave(&card->buffer_lock, flags);
921                 if (!list_empty(&card->cmd_packet_list)) {
922                         packet = (struct if_spi_packet *)(card->
923                                         cmd_packet_list.next);
924                         list_del(&packet->list);
925                 }
926                 spin_unlock_irqrestore(&card->buffer_lock, flags);
927
928                 if (packet)
929                         if_spi_h2c(card, packet, MVMS_CMD);
930         }
931         if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
932                 /* Do we have any data packets from the host to send? */
933                 packet = NULL;
934                 spin_lock_irqsave(&card->buffer_lock, flags);
935                 if (!list_empty(&card->data_packet_list)) {
936                         packet = (struct if_spi_packet *)(card->
937                                         data_packet_list.next);
938                         list_del(&packet->list);
939                 }
940                 spin_unlock_irqrestore(&card->buffer_lock, flags);
941
942                 if (packet)
943                         if_spi_h2c(card, packet, MVMS_DAT);
944         }
945         if (hiStatus & IF_SPI_HIST_CARD_EVENT)
946                 if_spi_e2h(card);
947
948 err:
949         if (err)
950                 netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
951
952         lbs_deb_leave(LBS_DEB_SPI);
953 }
954
955 /*
956  * Host to Card
957  *
958  * Called from Libertas to transfer some data to the WLAN device
959  * We can't sleep here.
960  */
961 static int if_spi_host_to_card(struct lbs_private *priv,
962                                 u8 type, u8 *buf, u16 nb)
963 {
964         int err = 0;
965         unsigned long flags;
966         struct if_spi_card *card = priv->card;
967         struct if_spi_packet *packet;
968         u16 blen;
969
970         lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
971
972         if (nb == 0) {
973                 netdev_err(priv->dev, "%s: invalid size requested: %d\n",
974                            __func__, nb);
975                 err = -EINVAL;
976                 goto out;
977         }
978         blen = ALIGN(nb, 4);
979         packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
980         if (!packet) {
981                 err = -ENOMEM;
982                 goto out;
983         }
984         packet->blen = blen;
985         memcpy(packet->buffer, buf, nb);
986         memset(packet->buffer + nb, 0, blen - nb);
987
988         switch (type) {
989         case MVMS_CMD:
990                 priv->dnld_sent = DNLD_CMD_SENT;
991                 spin_lock_irqsave(&card->buffer_lock, flags);
992                 list_add_tail(&packet->list, &card->cmd_packet_list);
993                 spin_unlock_irqrestore(&card->buffer_lock, flags);
994                 break;
995         case MVMS_DAT:
996                 priv->dnld_sent = DNLD_DATA_SENT;
997                 spin_lock_irqsave(&card->buffer_lock, flags);
998                 list_add_tail(&packet->list, &card->data_packet_list);
999                 spin_unlock_irqrestore(&card->buffer_lock, flags);
1000                 break;
1001         default:
1002                 kfree(packet);
1003                 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
1004                            type);
1005                 err = -EINVAL;
1006                 break;
1007         }
1008
1009         /* Queue spi xfer work */
1010         queue_work(card->workqueue, &card->packet_work);
1011 out:
1012         lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
1013         return err;
1014 }
1015
1016 /*
1017  * Host Interrupts
1018  *
1019  * Service incoming interrupts from the WLAN device. We can't sleep here, so
1020  * don't try to talk on the SPI bus, just queue the SPI xfer work.
1021  */
1022 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1023 {
1024         struct if_spi_card *card = dev_id;
1025
1026         queue_work(card->workqueue, &card->packet_work);
1027
1028         return IRQ_HANDLED;
1029 }
1030
1031 /*
1032  * SPI callbacks
1033  */
1034
1035 static int if_spi_init_card(struct if_spi_card *card)
1036 {
1037         struct lbs_private *priv = card->priv;
1038         int err, i;
1039         u32 scratch;
1040         const struct firmware *helper = NULL;
1041         const struct firmware *mainfw = NULL;
1042
1043         lbs_deb_enter(LBS_DEB_SPI);
1044
1045         err = spu_init(card, card->pdata->use_dummy_writes);
1046         if (err)
1047                 goto out;
1048         err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1049         if (err)
1050                 goto out;
1051
1052         err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1053         if (err)
1054                 goto out;
1055         if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1056                 lbs_deb_spi("Firmware is already loaded for "
1057                             "Marvell WLAN 802.11 adapter\n");
1058         else {
1059                 /* Check if we support this card */
1060                 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1061                         if (card->card_id == fw_table[i].model)
1062                                 break;
1063                 }
1064                 if (i == ARRAY_SIZE(fw_table)) {
1065                         netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1066                                    card->card_id);
1067                         err = -ENODEV;
1068                         goto out;
1069                 }
1070
1071                 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1072                                         card->card_id, &fw_table[0], &helper,
1073                                         &mainfw);
1074                 if (err) {
1075                         netdev_err(priv->dev, "failed to find firmware (%d)\n",
1076                                    err);
1077                         goto out;
1078                 }
1079
1080                 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1081                                 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1082                                 "attached to SPI bus_num %d, chip_select %d. "
1083                                 "spi->max_speed_hz=%d\n",
1084                                 card->card_id, card->card_rev,
1085                                 card->spi->master->bus_num,
1086                                 card->spi->chip_select,
1087                                 card->spi->max_speed_hz);
1088                 err = if_spi_prog_helper_firmware(card, helper);
1089                 if (err)
1090                         goto out;
1091                 err = if_spi_prog_main_firmware(card, mainfw);
1092                 if (err)
1093                         goto out;
1094                 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1095         }
1096
1097         err = spu_set_interrupt_mode(card, 0, 1);
1098         if (err)
1099                 goto out;
1100
1101 out:
1102         if (helper)
1103                 release_firmware(helper);
1104         if (mainfw)
1105                 release_firmware(mainfw);
1106
1107         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1108
1109         return err;
1110 }
1111
1112 static void if_spi_resume_worker(struct work_struct *work)
1113 {
1114         struct if_spi_card *card;
1115
1116         card = container_of(work, struct if_spi_card, resume_work);
1117
1118         if (card->suspended) {
1119                 if (card->pdata->setup)
1120                         card->pdata->setup(card->spi);
1121
1122                 /* Init card ... */
1123                 if_spi_init_card(card);
1124
1125                 enable_irq(card->spi->irq);
1126
1127                 /* And resume it ... */
1128                 lbs_resume(card->priv);
1129
1130                 card->suspended = 0;
1131         }
1132 }
1133
1134 static int __devinit if_spi_probe(struct spi_device *spi)
1135 {
1136         struct if_spi_card *card;
1137         struct lbs_private *priv = NULL;
1138         struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1139         int err = 0;
1140
1141         lbs_deb_enter(LBS_DEB_SPI);
1142
1143         if (!pdata) {
1144                 err = -EINVAL;
1145                 goto out;
1146         }
1147
1148         if (pdata->setup) {
1149                 err = pdata->setup(spi);
1150                 if (err)
1151                         goto out;
1152         }
1153
1154         /* Allocate card structure to represent this specific device */
1155         card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1156         if (!card) {
1157                 err = -ENOMEM;
1158                 goto teardown;
1159         }
1160         spi_set_drvdata(spi, card);
1161         card->pdata = pdata;
1162         card->spi = spi;
1163         card->prev_xfer_time = jiffies;
1164
1165         INIT_LIST_HEAD(&card->cmd_packet_list);
1166         INIT_LIST_HEAD(&card->data_packet_list);
1167         spin_lock_init(&card->buffer_lock);
1168
1169         /* Initialize the SPI Interface Unit */
1170
1171         /* Firmware load */
1172         err = if_spi_init_card(card);
1173         if (err)
1174                 goto free_card;
1175
1176         /*
1177          * Register our card with libertas.
1178          * This will call alloc_etherdev.
1179          */
1180         priv = lbs_add_card(card, &spi->dev);
1181         if (!priv) {
1182                 err = -ENOMEM;
1183                 goto free_card;
1184         }
1185         card->priv = priv;
1186         priv->setup_fw_on_resume = 1;
1187         priv->card = card;
1188         priv->hw_host_to_card = if_spi_host_to_card;
1189         priv->enter_deep_sleep = NULL;
1190         priv->exit_deep_sleep = NULL;
1191         priv->reset_deep_sleep_wakeup = NULL;
1192         priv->fw_ready = 1;
1193
1194         /* Initialize interrupt handling stuff. */
1195         card->workqueue = create_workqueue("libertas_spi");
1196         INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1197         INIT_WORK(&card->resume_work, if_spi_resume_worker);
1198
1199         err = request_irq(spi->irq, if_spi_host_interrupt,
1200                         IRQF_TRIGGER_FALLING, "libertas_spi", card);
1201         if (err) {
1202                 pr_err("can't get host irq line-- request_irq failed\n");
1203                 goto terminate_workqueue;
1204         }
1205
1206         /*
1207          * Start the card.
1208          * This will call register_netdev, and we'll start
1209          * getting interrupts...
1210          */
1211         err = lbs_start_card(priv);
1212         if (err)
1213                 goto release_irq;
1214
1215         lbs_deb_spi("Finished initializing WLAN module.\n");
1216
1217         /* successful exit */
1218         goto out;
1219
1220 release_irq:
1221         free_irq(spi->irq, card);
1222 terminate_workqueue:
1223         flush_workqueue(card->workqueue);
1224         destroy_workqueue(card->workqueue);
1225         lbs_remove_card(priv); /* will call free_netdev */
1226 free_card:
1227         free_if_spi_card(card);
1228 teardown:
1229         if (pdata->teardown)
1230                 pdata->teardown(spi);
1231 out:
1232         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1233         return err;
1234 }
1235
1236 static int __devexit libertas_spi_remove(struct spi_device *spi)
1237 {
1238         struct if_spi_card *card = spi_get_drvdata(spi);
1239         struct lbs_private *priv = card->priv;
1240
1241         lbs_deb_spi("libertas_spi_remove\n");
1242         lbs_deb_enter(LBS_DEB_SPI);
1243
1244         cancel_work_sync(&card->resume_work);
1245
1246         lbs_stop_card(priv);
1247         lbs_remove_card(priv); /* will call free_netdev */
1248
1249         free_irq(spi->irq, card);
1250         flush_workqueue(card->workqueue);
1251         destroy_workqueue(card->workqueue);
1252         if (card->pdata->teardown)
1253                 card->pdata->teardown(spi);
1254         free_if_spi_card(card);
1255         lbs_deb_leave(LBS_DEB_SPI);
1256         return 0;
1257 }
1258
1259 static int if_spi_suspend(struct device *dev)
1260 {
1261         struct spi_device *spi = to_spi_device(dev);
1262         struct if_spi_card *card = spi_get_drvdata(spi);
1263
1264         if (!card->suspended) {
1265                 lbs_suspend(card->priv);
1266                 flush_workqueue(card->workqueue);
1267                 disable_irq(spi->irq);
1268
1269                 if (card->pdata->teardown)
1270                         card->pdata->teardown(spi);
1271                 card->suspended = 1;
1272         }
1273
1274         return 0;
1275 }
1276
1277 static int if_spi_resume(struct device *dev)
1278 {
1279         struct spi_device *spi = to_spi_device(dev);
1280         struct if_spi_card *card = spi_get_drvdata(spi);
1281
1282         /* Schedule delayed work */
1283         schedule_work(&card->resume_work);
1284
1285         return 0;
1286 }
1287
1288 static const struct dev_pm_ops if_spi_pm_ops = {
1289         .suspend        = if_spi_suspend,
1290         .resume         = if_spi_resume,
1291 };
1292
1293 static struct spi_driver libertas_spi_driver = {
1294         .probe  = if_spi_probe,
1295         .remove = __devexit_p(libertas_spi_remove),
1296         .driver = {
1297                 .name   = "libertas_spi",
1298                 .bus    = &spi_bus_type,
1299                 .owner  = THIS_MODULE,
1300                 .pm     = &if_spi_pm_ops,
1301         },
1302 };
1303
1304 /*
1305  * Module functions
1306  */
1307
1308 static int __init if_spi_init_module(void)
1309 {
1310         int ret = 0;
1311         lbs_deb_enter(LBS_DEB_SPI);
1312         printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1313         ret = spi_register_driver(&libertas_spi_driver);
1314         lbs_deb_leave(LBS_DEB_SPI);
1315         return ret;
1316 }
1317
1318 static void __exit if_spi_exit_module(void)
1319 {
1320         lbs_deb_enter(LBS_DEB_SPI);
1321         spi_unregister_driver(&libertas_spi_driver);
1322         lbs_deb_leave(LBS_DEB_SPI);
1323 }
1324
1325 module_init(if_spi_init_module);
1326 module_exit(if_spi_exit_module);
1327
1328 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1329 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1330               "Colin McCabe <colin@cozybit.com>");
1331 MODULE_LICENSE("GPL");
1332 MODULE_ALIAS("spi:libertas_spi");