]> Pileus Git - ~andy/linux/blob - drivers/scsi/bfa/bfa_port.c
Merge branch 'x86-cpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / drivers / scsi / bfa / bfa_port.c
1 /*
2  * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17
18 #include <defs/bfa_defs_port.h>
19 #include <cs/bfa_trc.h>
20 #include <cs/bfa_log.h>
21 #include <cs/bfa_debug.h>
22 #include <port/bfa_port.h>
23 #include <bfi/bfi.h>
24 #include <bfi/bfi_port.h>
25 #include <bfa_ioc.h>
26 #include <cna/bfa_cna_trcmod.h>
27
28 BFA_TRC_FILE(CNA, PORT);
29
30 #define bfa_ioc_portid(__ioc) ((__ioc)->port_id)
31 #define bfa_lpuid(__arg) bfa_ioc_portid(&(__arg)->ioc)
32
33 static void
34 bfa_port_stats_swap(struct bfa_port_s *port, union bfa_pport_stats_u *stats)
35 {
36         u32       *dip = (u32 *) stats;
37         u32        t0, t1;
38         int             i;
39
40         for (i = 0; i < sizeof(union bfa_pport_stats_u) / sizeof(u32);
41              i += 2) {
42                 t0 = dip[i];
43                 t1 = dip[i + 1];
44 #ifdef __BIGENDIAN
45                 dip[i] = bfa_os_ntohl(t0);
46                 dip[i + 1] = bfa_os_ntohl(t1);
47 #else
48                 dip[i] = bfa_os_ntohl(t1);
49                 dip[i + 1] = bfa_os_ntohl(t0);
50 #endif
51         }
52
53     /** todo
54      * QoS stats r also swapped as 64bit; that structure also
55      * has to use 64 bit counters
56      */
57 }
58
59 /**
60  * bfa_port_enable_isr()
61  *
62  *
63  * @param[in] port - Pointer to the port module
64  *            status - Return status from the f/w
65  *
66  * @return void
67  */
68 static void
69 bfa_port_enable_isr(struct bfa_port_s *port, bfa_status_t status)
70 {
71         bfa_assert(0);
72 }
73
74 /**
75  * bfa_port_disable_isr()
76  *
77  *
78  * @param[in] port - Pointer to the port module
79  *            status - Return status from the f/w
80  *
81  * @return void
82  */
83 static void
84 bfa_port_disable_isr(struct bfa_port_s *port, bfa_status_t status)
85 {
86         bfa_assert(0);
87 }
88
89 /**
90  * bfa_port_get_stats_isr()
91  *
92  *
93  * @param[in] port - Pointer to the Port module
94  *            status - Return status from the f/w
95  *
96  * @return void
97  */
98 static void
99 bfa_port_get_stats_isr(struct bfa_port_s *port, bfa_status_t status)
100 {
101         port->stats_status = status;
102         port->stats_busy = BFA_FALSE;
103
104         if (status == BFA_STATUS_OK) {
105                 struct bfa_timeval_s tv;
106
107                 memcpy(port->stats, port->stats_dma.kva,
108                        sizeof(union bfa_pport_stats_u));
109                 bfa_port_stats_swap(port, port->stats);
110
111                 bfa_os_gettimeofday(&tv);
112                 port->stats->fc.secs_reset = tv.tv_sec - port->stats_reset_time;
113         }
114
115         if (port->stats_cbfn) {
116                 port->stats_cbfn(port->stats_cbarg, status);
117                 port->stats_cbfn = NULL;
118         }
119 }
120
121 /**
122  * bfa_port_clear_stats_isr()
123  *
124  *
125  * @param[in] port - Pointer to the Port module
126  *            status - Return status from the f/w
127  *
128  * @return void
129  */
130 static void
131 bfa_port_clear_stats_isr(struct bfa_port_s *port, bfa_status_t status)
132 {
133         struct bfa_timeval_s tv;
134
135         port->stats_status = status;
136         port->stats_busy = BFA_FALSE;
137
138         /**
139          * re-initialize time stamp for stats reset
140          */
141         bfa_os_gettimeofday(&tv);
142         port->stats_reset_time = tv.tv_sec;
143
144         if (port->stats_cbfn) {
145                 port->stats_cbfn(port->stats_cbarg, status);
146                 port->stats_cbfn = NULL;
147         }
148 }
149
150 /**
151  * bfa_port_isr()
152  *
153  *
154  * @param[in] Pointer to the Port module data structure.
155  *
156  * @return void
157  */
158 static void
159 bfa_port_isr(void *cbarg, struct bfi_mbmsg_s *m)
160 {
161         struct bfa_port_s *port = (struct bfa_port_s *)cbarg;
162         union bfi_port_i2h_msg_u *i2hmsg;
163
164         i2hmsg = (union bfi_port_i2h_msg_u *)m;
165         bfa_trc(port, m->mh.msg_id);
166
167         switch (m->mh.msg_id) {
168         case BFI_PORT_I2H_ENABLE_RSP:
169                 if (port->endis_pending == BFA_FALSE)
170                         break;
171                 bfa_port_enable_isr(port, i2hmsg->enable_rsp.status);
172                 break;
173
174         case BFI_PORT_I2H_DISABLE_RSP:
175                 if (port->endis_pending == BFA_FALSE)
176                         break;
177                 bfa_port_disable_isr(port, i2hmsg->disable_rsp.status);
178                 break;
179
180         case BFI_PORT_I2H_GET_STATS_RSP:
181                 /*
182                  * Stats busy flag is still set? (may be cmd timed out)
183                  */
184                 if (port->stats_busy == BFA_FALSE)
185                         break;
186                 bfa_port_get_stats_isr(port, i2hmsg->getstats_rsp.status);
187                 break;
188
189         case BFI_PORT_I2H_CLEAR_STATS_RSP:
190                 if (port->stats_busy == BFA_FALSE)
191                         break;
192                 bfa_port_clear_stats_isr(port, i2hmsg->clearstats_rsp.status);
193                 break;
194
195         default:
196                 bfa_assert(0);
197         }
198 }
199
200 /**
201  * bfa_port_meminfo()
202  *
203  *
204  * @param[in] void
205  *
206  * @return Size of DMA region
207  */
208 u32
209 bfa_port_meminfo(void)
210 {
211         return BFA_ROUNDUP(sizeof(union bfa_pport_stats_u), BFA_DMA_ALIGN_SZ);
212 }
213
214 /**
215  * bfa_port_mem_claim()
216  *
217  *
218  * @param[in] port Port module pointer
219  *            dma_kva Kernel Virtual Address of Port DMA Memory
220  *            dma_pa  Physical Address of Port DMA Memory
221  *
222  * @return void
223  */
224 void
225 bfa_port_mem_claim(struct bfa_port_s *port, u8 *dma_kva, u64 dma_pa)
226 {
227         port->stats_dma.kva = dma_kva;
228         port->stats_dma.pa = dma_pa;
229 }
230
231 /**
232  * bfa_port_enable()
233  *
234  *   Send the Port enable request to the f/w
235  *
236  * @param[in] Pointer to the Port module data structure.
237  *
238  * @return Status
239  */
240 bfa_status_t
241 bfa_port_enable(struct bfa_port_s *port, bfa_port_endis_cbfn_t cbfn,
242                 void *cbarg)
243 {
244         struct bfi_port_generic_req_s *m;
245
246         /** todo Not implemented */
247         bfa_assert(0);
248
249         if (!bfa_ioc_is_operational(port->ioc)) {
250                 bfa_trc(port, BFA_STATUS_IOC_FAILURE);
251                 return BFA_STATUS_IOC_FAILURE;
252         }
253
254         if (port->endis_pending) {
255                 bfa_trc(port, BFA_STATUS_DEVBUSY);
256                 return BFA_STATUS_DEVBUSY;
257         }
258
259         m = (struct bfi_port_generic_req_s *)port->endis_mb.msg;
260
261         port->msgtag++;
262         port->endis_cbfn = cbfn;
263         port->endis_cbarg = cbarg;
264         port->endis_pending = BFA_TRUE;
265
266         bfi_h2i_set(m->mh, BFI_MC_PORT, BFI_PORT_H2I_ENABLE_REQ,
267                     bfa_ioc_portid(port->ioc));
268         bfa_ioc_mbox_queue(port->ioc, &port->endis_mb);
269
270         return BFA_STATUS_OK;
271 }
272
273 /**
274  * bfa_port_disable()
275  *
276  *   Send the Port disable request to the f/w
277  *
278  * @param[in] Pointer to the Port module data structure.
279  *
280  * @return Status
281  */
282 bfa_status_t
283 bfa_port_disable(struct bfa_port_s *port, bfa_port_endis_cbfn_t cbfn,
284                  void *cbarg)
285 {
286         struct bfi_port_generic_req_s *m;
287
288         /** todo Not implemented */
289         bfa_assert(0);
290
291         if (!bfa_ioc_is_operational(port->ioc)) {
292                 bfa_trc(port, BFA_STATUS_IOC_FAILURE);
293                 return BFA_STATUS_IOC_FAILURE;
294         }
295
296         if (port->endis_pending) {
297                 bfa_trc(port, BFA_STATUS_DEVBUSY);
298                 return BFA_STATUS_DEVBUSY;
299         }
300
301         m = (struct bfi_port_generic_req_s *)port->endis_mb.msg;
302
303         port->msgtag++;
304         port->endis_cbfn = cbfn;
305         port->endis_cbarg = cbarg;
306         port->endis_pending = BFA_TRUE;
307
308         bfi_h2i_set(m->mh, BFI_MC_PORT, BFI_PORT_H2I_DISABLE_REQ,
309                     bfa_ioc_portid(port->ioc));
310         bfa_ioc_mbox_queue(port->ioc, &port->endis_mb);
311
312         return BFA_STATUS_OK;
313 }
314
315 /**
316  * bfa_port_get_stats()
317  *
318  *   Send the request to the f/w to fetch Port statistics.
319  *
320  * @param[in] Pointer to the Port module data structure.
321  *
322  * @return Status
323  */
324 bfa_status_t
325 bfa_port_get_stats(struct bfa_port_s *port, union bfa_pport_stats_u *stats,
326                    bfa_port_stats_cbfn_t cbfn, void *cbarg)
327 {
328         struct bfi_port_get_stats_req_s *m;
329
330         if (!bfa_ioc_is_operational(port->ioc)) {
331                 bfa_trc(port, BFA_STATUS_IOC_FAILURE);
332                 return BFA_STATUS_IOC_FAILURE;
333         }
334
335         if (port->stats_busy) {
336                 bfa_trc(port, BFA_STATUS_DEVBUSY);
337                 return BFA_STATUS_DEVBUSY;
338         }
339
340         m = (struct bfi_port_get_stats_req_s *)port->stats_mb.msg;
341
342         port->stats = stats;
343         port->stats_cbfn = cbfn;
344         port->stats_cbarg = cbarg;
345         port->stats_busy = BFA_TRUE;
346         bfa_dma_be_addr_set(m->dma_addr, port->stats_dma.pa);
347
348         bfi_h2i_set(m->mh, BFI_MC_PORT, BFI_PORT_H2I_GET_STATS_REQ,
349                     bfa_ioc_portid(port->ioc));
350         bfa_ioc_mbox_queue(port->ioc, &port->stats_mb);
351
352         return BFA_STATUS_OK;
353 }
354
355 /**
356  * bfa_port_clear_stats()
357  *
358  *
359  * @param[in] Pointer to the Port module data structure.
360  *
361  * @return Status
362  */
363 bfa_status_t
364 bfa_port_clear_stats(struct bfa_port_s *port, bfa_port_stats_cbfn_t cbfn,
365                      void *cbarg)
366 {
367         struct bfi_port_generic_req_s *m;
368
369         if (!bfa_ioc_is_operational(port->ioc)) {
370                 bfa_trc(port, BFA_STATUS_IOC_FAILURE);
371                 return BFA_STATUS_IOC_FAILURE;
372         }
373
374         if (port->stats_busy) {
375                 bfa_trc(port, BFA_STATUS_DEVBUSY);
376                 return BFA_STATUS_DEVBUSY;
377         }
378
379         m = (struct bfi_port_generic_req_s *)port->stats_mb.msg;
380
381         port->stats_cbfn = cbfn;
382         port->stats_cbarg = cbarg;
383         port->stats_busy = BFA_TRUE;
384
385         bfi_h2i_set(m->mh, BFI_MC_PORT, BFI_PORT_H2I_CLEAR_STATS_REQ,
386                     bfa_ioc_portid(port->ioc));
387         bfa_ioc_mbox_queue(port->ioc, &port->stats_mb);
388
389         return BFA_STATUS_OK;
390 }
391
392 /**
393  * bfa_port_hbfail()
394  *
395  *
396  * @param[in] Pointer to the Port module data structure.
397  *
398  * @return void
399  */
400 void
401 bfa_port_hbfail(void *arg)
402 {
403         struct bfa_port_s *port = (struct bfa_port_s *)arg;
404
405         /*
406          * Fail any pending get_stats/clear_stats requests
407          */
408         if (port->stats_busy) {
409                 if (port->stats_cbfn)
410                         port->stats_cbfn(port->stats_cbarg, BFA_STATUS_FAILED);
411                 port->stats_cbfn = NULL;
412                 port->stats_busy = BFA_FALSE;
413         }
414
415         /*
416          * Clear any enable/disable is pending
417          */
418         if (port->endis_pending) {
419                 if (port->endis_cbfn)
420                         port->endis_cbfn(port->endis_cbarg, BFA_STATUS_FAILED);
421                 port->endis_cbfn = NULL;
422                 port->endis_pending = BFA_FALSE;
423         }
424 }
425
426 /**
427  * bfa_port_attach()
428  *
429  *
430  * @param[in] port - Pointer to the Port module data structure
431  *            ioc  - Pointer to the ioc module data structure
432  *            dev  - Pointer to the device driver module data structure
433  *                   The device driver specific mbox ISR functions have
434  *                   this pointer as one of the parameters.
435  *            trcmod -
436  *            logmod -
437  *
438  * @return void
439  */
440 void
441 bfa_port_attach(struct bfa_port_s *port, struct bfa_ioc_s *ioc, void *dev,
442                 struct bfa_trc_mod_s *trcmod, struct bfa_log_mod_s *logmod)
443 {
444         struct bfa_timeval_s tv;
445
446         bfa_assert(port);
447
448         port->dev = dev;
449         port->ioc = ioc;
450         port->trcmod = trcmod;
451         port->logmod = logmod;
452
453         port->stats_busy = BFA_FALSE;
454         port->endis_pending = BFA_FALSE;
455         port->stats_cbfn = NULL;
456         port->endis_cbfn = NULL;
457
458         bfa_ioc_mbox_regisr(port->ioc, BFI_MC_PORT, bfa_port_isr, port);
459         bfa_ioc_hbfail_init(&port->hbfail, bfa_port_hbfail, port);
460         bfa_ioc_hbfail_register(port->ioc, &port->hbfail);
461
462         /**
463          * initialize time stamp for stats reset
464          */
465         bfa_os_gettimeofday(&tv);
466         port->stats_reset_time = tv.tv_sec;
467
468         bfa_trc(port, 0);
469 }
470
471 /**
472  * bfa_port_detach()
473  *
474  *
475  * @param[in] port - Pointer to the Port module data structure
476  *
477  * @return void
478  */
479 void
480 bfa_port_detach(struct bfa_port_s *port)
481 {
482         bfa_trc(port, 0);
483 }