GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / net / fjes / fjes_hw.c
1 /*
2  *  FUJITSU Extended Socket Network Device driver
3  *  Copyright (c) 2015 FUJITSU LIMITED
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * The full GNU General Public License is included in this distribution in
18  * the file called "COPYING".
19  *
20  */
21
22 #include "fjes_hw.h"
23 #include "fjes.h"
24 #include "fjes_trace.h"
25
26 static void fjes_hw_update_zone_task(struct work_struct *);
27 static void fjes_hw_epstop_task(struct work_struct *);
28
29 /* supported MTU list */
30 const u32 fjes_support_mtu[] = {
31         FJES_MTU_DEFINE(8 * 1024),
32         FJES_MTU_DEFINE(16 * 1024),
33         FJES_MTU_DEFINE(32 * 1024),
34         FJES_MTU_DEFINE(64 * 1024),
35         0
36 };
37
38 u32 fjes_hw_rd32(struct fjes_hw *hw, u32 reg)
39 {
40         u8 *base = hw->base;
41         u32 value = 0;
42
43         value = readl(&base[reg]);
44
45         return value;
46 }
47
48 static u8 *fjes_hw_iomap(struct fjes_hw *hw)
49 {
50         u8 *base;
51
52         if (!request_mem_region(hw->hw_res.start, hw->hw_res.size,
53                                 fjes_driver_name)) {
54                 pr_err("request_mem_region failed\n");
55                 return NULL;
56         }
57
58         base = (u8 *)ioremap_nocache(hw->hw_res.start, hw->hw_res.size);
59
60         return base;
61 }
62
63 static void fjes_hw_iounmap(struct fjes_hw *hw)
64 {
65         iounmap(hw->base);
66         release_mem_region(hw->hw_res.start, hw->hw_res.size);
67 }
68
69 int fjes_hw_reset(struct fjes_hw *hw)
70 {
71         union REG_DCTL dctl;
72         int timeout;
73
74         dctl.reg = 0;
75         dctl.bits.reset = 1;
76         wr32(XSCT_DCTL, dctl.reg);
77
78         timeout = FJES_DEVICE_RESET_TIMEOUT * 1000;
79         dctl.reg = rd32(XSCT_DCTL);
80         while ((dctl.bits.reset == 1) && (timeout > 0)) {
81                 msleep(1000);
82                 dctl.reg = rd32(XSCT_DCTL);
83                 timeout -= 1000;
84         }
85
86         return timeout > 0 ? 0 : -EIO;
87 }
88
89 static int fjes_hw_get_max_epid(struct fjes_hw *hw)
90 {
91         union REG_MAX_EP info;
92
93         info.reg = rd32(XSCT_MAX_EP);
94
95         return info.bits.maxep;
96 }
97
98 static int fjes_hw_get_my_epid(struct fjes_hw *hw)
99 {
100         union REG_OWNER_EPID info;
101
102         info.reg = rd32(XSCT_OWNER_EPID);
103
104         return info.bits.epid;
105 }
106
107 static int fjes_hw_alloc_shared_status_region(struct fjes_hw *hw)
108 {
109         size_t size;
110
111         size = sizeof(struct fjes_device_shared_info) +
112             (sizeof(u8) * hw->max_epid);
113         hw->hw_info.share = kzalloc(size, GFP_KERNEL);
114         if (!hw->hw_info.share)
115                 return -ENOMEM;
116
117         hw->hw_info.share->epnum = hw->max_epid;
118
119         return 0;
120 }
121
122 static void fjes_hw_free_shared_status_region(struct fjes_hw *hw)
123 {
124         kfree(hw->hw_info.share);
125         hw->hw_info.share = NULL;
126 }
127
128 static int fjes_hw_alloc_epbuf(struct epbuf_handler *epbh)
129 {
130         void *mem;
131
132         mem = vzalloc(EP_BUFFER_SIZE);
133         if (!mem)
134                 return -ENOMEM;
135
136         epbh->buffer = mem;
137         epbh->size = EP_BUFFER_SIZE;
138
139         epbh->info = (union ep_buffer_info *)mem;
140         epbh->ring = (u8 *)(mem + sizeof(union ep_buffer_info));
141
142         return 0;
143 }
144
145 static void fjes_hw_free_epbuf(struct epbuf_handler *epbh)
146 {
147         vfree(epbh->buffer);
148         epbh->buffer = NULL;
149         epbh->size = 0;
150
151         epbh->info = NULL;
152         epbh->ring = NULL;
153 }
154
155 void fjes_hw_setup_epbuf(struct epbuf_handler *epbh, u8 *mac_addr, u32 mtu)
156 {
157         union ep_buffer_info *info = epbh->info;
158         u16 vlan_id[EP_BUFFER_SUPPORT_VLAN_MAX];
159         int i;
160
161         for (i = 0; i < EP_BUFFER_SUPPORT_VLAN_MAX; i++)
162                 vlan_id[i] = info->v1i.vlan_id[i];
163
164         memset(info, 0, sizeof(union ep_buffer_info));
165
166         info->v1i.version = 0;  /* version 0 */
167
168         for (i = 0; i < ETH_ALEN; i++)
169                 info->v1i.mac_addr[i] = mac_addr[i];
170
171         info->v1i.head = 0;
172         info->v1i.tail = 1;
173
174         info->v1i.info_size = sizeof(union ep_buffer_info);
175         info->v1i.buffer_size = epbh->size - info->v1i.info_size;
176
177         info->v1i.frame_max = FJES_MTU_TO_FRAME_SIZE(mtu);
178         info->v1i.count_max =
179             EP_RING_NUM(info->v1i.buffer_size, info->v1i.frame_max);
180
181         for (i = 0; i < EP_BUFFER_SUPPORT_VLAN_MAX; i++)
182                 info->v1i.vlan_id[i] = vlan_id[i];
183
184         info->v1i.rx_status |= FJES_RX_MTU_CHANGING_DONE;
185 }
186
187 void
188 fjes_hw_init_command_registers(struct fjes_hw *hw,
189                                struct fjes_device_command_param *param)
190 {
191         /* Request Buffer length */
192         wr32(XSCT_REQBL, (__le32)(param->req_len));
193         /* Response Buffer Length */
194         wr32(XSCT_RESPBL, (__le32)(param->res_len));
195
196         /* Request Buffer Address */
197         wr32(XSCT_REQBAL,
198              (__le32)(param->req_start & GENMASK_ULL(31, 0)));
199         wr32(XSCT_REQBAH,
200              (__le32)((param->req_start & GENMASK_ULL(63, 32)) >> 32));
201
202         /* Response Buffer Address */
203         wr32(XSCT_RESPBAL,
204              (__le32)(param->res_start & GENMASK_ULL(31, 0)));
205         wr32(XSCT_RESPBAH,
206              (__le32)((param->res_start & GENMASK_ULL(63, 32)) >> 32));
207
208         /* Share status address */
209         wr32(XSCT_SHSTSAL,
210              (__le32)(param->share_start & GENMASK_ULL(31, 0)));
211         wr32(XSCT_SHSTSAH,
212              (__le32)((param->share_start & GENMASK_ULL(63, 32)) >> 32));
213 }
214
215 static int fjes_hw_setup(struct fjes_hw *hw)
216 {
217         u8 mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
218         struct fjes_device_command_param param;
219         struct ep_share_mem_info *buf_pair;
220         unsigned long flags;
221         size_t mem_size;
222         int result;
223         int epidx;
224         void *buf;
225
226         hw->hw_info.max_epid = &hw->max_epid;
227         hw->hw_info.my_epid = &hw->my_epid;
228
229         buf = kcalloc(hw->max_epid, sizeof(struct ep_share_mem_info),
230                       GFP_KERNEL);
231         if (!buf)
232                 return -ENOMEM;
233
234         hw->ep_shm_info = (struct ep_share_mem_info *)buf;
235
236         mem_size = FJES_DEV_REQ_BUF_SIZE(hw->max_epid);
237         hw->hw_info.req_buf = kzalloc(mem_size, GFP_KERNEL);
238         if (!(hw->hw_info.req_buf)) {
239                 result = -ENOMEM;
240                 goto free_ep_info;
241         }
242
243         hw->hw_info.req_buf_size = mem_size;
244
245         mem_size = FJES_DEV_RES_BUF_SIZE(hw->max_epid);
246         hw->hw_info.res_buf = kzalloc(mem_size, GFP_KERNEL);
247         if (!(hw->hw_info.res_buf)) {
248                 result = -ENOMEM;
249                 goto free_req_buf;
250         }
251
252         hw->hw_info.res_buf_size = mem_size;
253
254         result = fjes_hw_alloc_shared_status_region(hw);
255         if (result)
256                 goto free_res_buf;
257
258         hw->hw_info.buffer_share_bit = 0;
259         hw->hw_info.buffer_unshare_reserve_bit = 0;
260
261         for (epidx = 0; epidx < hw->max_epid; epidx++) {
262                 if (epidx != hw->my_epid) {
263                         buf_pair = &hw->ep_shm_info[epidx];
264
265                         result = fjes_hw_alloc_epbuf(&buf_pair->tx);
266                         if (result)
267                                 goto free_epbuf;
268
269                         result = fjes_hw_alloc_epbuf(&buf_pair->rx);
270                         if (result)
271                                 goto free_epbuf;
272
273                         spin_lock_irqsave(&hw->rx_status_lock, flags);
274                         fjes_hw_setup_epbuf(&buf_pair->tx, mac,
275                                             fjes_support_mtu[0]);
276                         fjes_hw_setup_epbuf(&buf_pair->rx, mac,
277                                             fjes_support_mtu[0]);
278                         spin_unlock_irqrestore(&hw->rx_status_lock, flags);
279                 }
280         }
281
282         memset(&param, 0, sizeof(param));
283
284         param.req_len = hw->hw_info.req_buf_size;
285         param.req_start = __pa(hw->hw_info.req_buf);
286         param.res_len = hw->hw_info.res_buf_size;
287         param.res_start = __pa(hw->hw_info.res_buf);
288
289         param.share_start = __pa(hw->hw_info.share->ep_status);
290
291         fjes_hw_init_command_registers(hw, &param);
292
293         return 0;
294
295 free_epbuf:
296         for (epidx = 0; epidx < hw->max_epid ; epidx++) {
297                 if (epidx == hw->my_epid)
298                         continue;
299                 fjes_hw_free_epbuf(&hw->ep_shm_info[epidx].tx);
300                 fjes_hw_free_epbuf(&hw->ep_shm_info[epidx].rx);
301         }
302         fjes_hw_free_shared_status_region(hw);
303 free_res_buf:
304         kfree(hw->hw_info.res_buf);
305         hw->hw_info.res_buf = NULL;
306 free_req_buf:
307         kfree(hw->hw_info.req_buf);
308         hw->hw_info.req_buf = NULL;
309 free_ep_info:
310         kfree(hw->ep_shm_info);
311         hw->ep_shm_info = NULL;
312         return result;
313 }
314
315 static void fjes_hw_cleanup(struct fjes_hw *hw)
316 {
317         int epidx;
318
319         if (!hw->ep_shm_info)
320                 return;
321
322         fjes_hw_free_shared_status_region(hw);
323
324         kfree(hw->hw_info.req_buf);
325         hw->hw_info.req_buf = NULL;
326
327         kfree(hw->hw_info.res_buf);
328         hw->hw_info.res_buf = NULL;
329
330         for (epidx = 0; epidx < hw->max_epid ; epidx++) {
331                 if (epidx == hw->my_epid)
332                         continue;
333                 fjes_hw_free_epbuf(&hw->ep_shm_info[epidx].tx);
334                 fjes_hw_free_epbuf(&hw->ep_shm_info[epidx].rx);
335         }
336
337         kfree(hw->ep_shm_info);
338         hw->ep_shm_info = NULL;
339 }
340
341 int fjes_hw_init(struct fjes_hw *hw)
342 {
343         int ret;
344
345         hw->base = fjes_hw_iomap(hw);
346         if (!hw->base)
347                 return -EIO;
348
349         ret = fjes_hw_reset(hw);
350         if (ret)
351                 return ret;
352
353         fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
354
355         INIT_WORK(&hw->update_zone_task, fjes_hw_update_zone_task);
356         INIT_WORK(&hw->epstop_task, fjes_hw_epstop_task);
357
358         mutex_init(&hw->hw_info.lock);
359         spin_lock_init(&hw->rx_status_lock);
360
361         hw->max_epid = fjes_hw_get_max_epid(hw);
362         hw->my_epid = fjes_hw_get_my_epid(hw);
363
364         if ((hw->max_epid == 0) || (hw->my_epid >= hw->max_epid))
365                 return -ENXIO;
366
367         ret = fjes_hw_setup(hw);
368
369         hw->hw_info.trace = vzalloc(FJES_DEBUG_BUFFER_SIZE);
370         hw->hw_info.trace_size = FJES_DEBUG_BUFFER_SIZE;
371
372         return ret;
373 }
374
375 void fjes_hw_exit(struct fjes_hw *hw)
376 {
377         int ret;
378
379         if (hw->base) {
380
381                 if (hw->debug_mode) {
382                         /* disable debug mode */
383                         mutex_lock(&hw->hw_info.lock);
384                         fjes_hw_stop_debug(hw);
385                         mutex_unlock(&hw->hw_info.lock);
386                 }
387                 vfree(hw->hw_info.trace);
388                 hw->hw_info.trace = NULL;
389                 hw->hw_info.trace_size = 0;
390                 hw->debug_mode = 0;
391
392                 ret = fjes_hw_reset(hw);
393                 if (ret)
394                         pr_err("%s: reset error", __func__);
395
396                 fjes_hw_iounmap(hw);
397                 hw->base = NULL;
398         }
399
400         fjes_hw_cleanup(hw);
401
402         cancel_work_sync(&hw->update_zone_task);
403         cancel_work_sync(&hw->epstop_task);
404 }
405
406 static enum fjes_dev_command_response_e
407 fjes_hw_issue_request_command(struct fjes_hw *hw,
408                               enum fjes_dev_command_request_type type)
409 {
410         enum fjes_dev_command_response_e ret = FJES_CMD_STATUS_UNKNOWN;
411         union REG_CR cr;
412         union REG_CS cs;
413         int timeout = FJES_COMMAND_REQ_TIMEOUT * 1000;
414
415         cr.reg = 0;
416         cr.bits.req_start = 1;
417         cr.bits.req_code = type;
418         wr32(XSCT_CR, cr.reg);
419         cr.reg = rd32(XSCT_CR);
420
421         if (cr.bits.error == 0) {
422                 timeout = FJES_COMMAND_REQ_TIMEOUT * 1000;
423                 cs.reg = rd32(XSCT_CS);
424
425                 while ((cs.bits.complete != 1) && timeout > 0) {
426                         msleep(1000);
427                         cs.reg = rd32(XSCT_CS);
428                         timeout -= 1000;
429                 }
430
431                 if (cs.bits.complete == 1)
432                         ret = FJES_CMD_STATUS_NORMAL;
433                 else if (timeout <= 0)
434                         ret = FJES_CMD_STATUS_TIMEOUT;
435
436         } else {
437                 switch (cr.bits.err_info) {
438                 case FJES_CMD_REQ_ERR_INFO_PARAM:
439                         ret = FJES_CMD_STATUS_ERROR_PARAM;
440                         break;
441                 case FJES_CMD_REQ_ERR_INFO_STATUS:
442                         ret = FJES_CMD_STATUS_ERROR_STATUS;
443                         break;
444                 default:
445                         ret = FJES_CMD_STATUS_UNKNOWN;
446                         break;
447                 }
448         }
449
450         trace_fjes_hw_issue_request_command(&cr, &cs, timeout, ret);
451
452         return ret;
453 }
454
455 int fjes_hw_request_info(struct fjes_hw *hw)
456 {
457         union fjes_device_command_req *req_buf = hw->hw_info.req_buf;
458         union fjes_device_command_res *res_buf = hw->hw_info.res_buf;
459         enum fjes_dev_command_response_e ret;
460         int result;
461
462         memset(req_buf, 0, hw->hw_info.req_buf_size);
463         memset(res_buf, 0, hw->hw_info.res_buf_size);
464
465         req_buf->info.length = FJES_DEV_COMMAND_INFO_REQ_LEN;
466
467         res_buf->info.length = 0;
468         res_buf->info.code = 0;
469
470         ret = fjes_hw_issue_request_command(hw, FJES_CMD_REQ_INFO);
471         trace_fjes_hw_request_info(hw, res_buf);
472
473         result = 0;
474
475         if (FJES_DEV_COMMAND_INFO_RES_LEN((*hw->hw_info.max_epid)) !=
476                 res_buf->info.length) {
477                 trace_fjes_hw_request_info_err("Invalid res_buf");
478                 result = -ENOMSG;
479         } else if (ret == FJES_CMD_STATUS_NORMAL) {
480                 switch (res_buf->info.code) {
481                 case FJES_CMD_REQ_RES_CODE_NORMAL:
482                         result = 0;
483                         break;
484                 default:
485                         result = -EPERM;
486                         break;
487                 }
488         } else {
489                 switch (ret) {
490                 case FJES_CMD_STATUS_UNKNOWN:
491                         result = -EPERM;
492                         break;
493                 case FJES_CMD_STATUS_TIMEOUT:
494                         trace_fjes_hw_request_info_err("Timeout");
495                         result = -EBUSY;
496                         break;
497                 case FJES_CMD_STATUS_ERROR_PARAM:
498                         result = -EPERM;
499                         break;
500                 case FJES_CMD_STATUS_ERROR_STATUS:
501                         result = -EPERM;
502                         break;
503                 default:
504                         result = -EPERM;
505                         break;
506                 }
507         }
508
509         return result;
510 }
511
512 int fjes_hw_register_buff_addr(struct fjes_hw *hw, int dest_epid,
513                                struct ep_share_mem_info *buf_pair)
514 {
515         union fjes_device_command_req *req_buf = hw->hw_info.req_buf;
516         union fjes_device_command_res *res_buf = hw->hw_info.res_buf;
517         enum fjes_dev_command_response_e ret;
518         int page_count;
519         int timeout;
520         int i, idx;
521         void *addr;
522         int result;
523
524         if (test_bit(dest_epid, &hw->hw_info.buffer_share_bit))
525                 return 0;
526
527         memset(req_buf, 0, hw->hw_info.req_buf_size);
528         memset(res_buf, 0, hw->hw_info.res_buf_size);
529
530         req_buf->share_buffer.length = FJES_DEV_COMMAND_SHARE_BUFFER_REQ_LEN(
531                                                 buf_pair->tx.size,
532                                                 buf_pair->rx.size);
533         req_buf->share_buffer.epid = dest_epid;
534
535         idx = 0;
536         req_buf->share_buffer.buffer[idx++] = buf_pair->tx.size;
537         page_count = buf_pair->tx.size / EP_BUFFER_INFO_SIZE;
538         for (i = 0; i < page_count; i++) {
539                 addr = ((u8 *)(buf_pair->tx.buffer)) +
540                                 (i * EP_BUFFER_INFO_SIZE);
541                 req_buf->share_buffer.buffer[idx++] =
542                                 (__le64)(page_to_phys(vmalloc_to_page(addr)) +
543                                                 offset_in_page(addr));
544         }
545
546         req_buf->share_buffer.buffer[idx++] = buf_pair->rx.size;
547         page_count = buf_pair->rx.size / EP_BUFFER_INFO_SIZE;
548         for (i = 0; i < page_count; i++) {
549                 addr = ((u8 *)(buf_pair->rx.buffer)) +
550                                 (i * EP_BUFFER_INFO_SIZE);
551                 req_buf->share_buffer.buffer[idx++] =
552                                 (__le64)(page_to_phys(vmalloc_to_page(addr)) +
553                                                 offset_in_page(addr));
554         }
555
556         res_buf->share_buffer.length = 0;
557         res_buf->share_buffer.code = 0;
558
559         trace_fjes_hw_register_buff_addr_req(req_buf, buf_pair);
560
561         ret = fjes_hw_issue_request_command(hw, FJES_CMD_REQ_SHARE_BUFFER);
562
563         timeout = FJES_COMMAND_REQ_BUFF_TIMEOUT * 1000;
564         while ((ret == FJES_CMD_STATUS_NORMAL) &&
565                (res_buf->share_buffer.length ==
566                 FJES_DEV_COMMAND_SHARE_BUFFER_RES_LEN) &&
567                (res_buf->share_buffer.code == FJES_CMD_REQ_RES_CODE_BUSY) &&
568                (timeout > 0)) {
569                         msleep(200 + hw->my_epid * 20);
570                         timeout -= (200 + hw->my_epid * 20);
571
572                         res_buf->share_buffer.length = 0;
573                         res_buf->share_buffer.code = 0;
574
575                         ret = fjes_hw_issue_request_command(
576                                         hw, FJES_CMD_REQ_SHARE_BUFFER);
577         }
578
579         result = 0;
580
581         trace_fjes_hw_register_buff_addr(res_buf, timeout);
582
583         if (res_buf->share_buffer.length !=
584                         FJES_DEV_COMMAND_SHARE_BUFFER_RES_LEN) {
585                 trace_fjes_hw_register_buff_addr_err("Invalid res_buf");
586                 result = -ENOMSG;
587         } else if (ret == FJES_CMD_STATUS_NORMAL) {
588                 switch (res_buf->share_buffer.code) {
589                 case FJES_CMD_REQ_RES_CODE_NORMAL:
590                         result = 0;
591                         set_bit(dest_epid, &hw->hw_info.buffer_share_bit);
592                         break;
593                 case FJES_CMD_REQ_RES_CODE_BUSY:
594                         trace_fjes_hw_register_buff_addr_err("Busy Timeout");
595                         result = -EBUSY;
596                         break;
597                 default:
598                         result = -EPERM;
599                         break;
600                 }
601         } else {
602                 switch (ret) {
603                 case FJES_CMD_STATUS_UNKNOWN:
604                         result = -EPERM;
605                         break;
606                 case FJES_CMD_STATUS_TIMEOUT:
607                         trace_fjes_hw_register_buff_addr_err("Timeout");
608                         result = -EBUSY;
609                         break;
610                 case FJES_CMD_STATUS_ERROR_PARAM:
611                 case FJES_CMD_STATUS_ERROR_STATUS:
612                 default:
613                         result = -EPERM;
614                         break;
615                 }
616         }
617
618         return result;
619 }
620
621 int fjes_hw_unregister_buff_addr(struct fjes_hw *hw, int dest_epid)
622 {
623         union fjes_device_command_req *req_buf = hw->hw_info.req_buf;
624         union fjes_device_command_res *res_buf = hw->hw_info.res_buf;
625         struct fjes_device_shared_info *share = hw->hw_info.share;
626         enum fjes_dev_command_response_e ret;
627         int timeout;
628         int result;
629
630         if (!hw->base)
631                 return -EPERM;
632
633         if (!req_buf || !res_buf || !share)
634                 return -EPERM;
635
636         if (!test_bit(dest_epid, &hw->hw_info.buffer_share_bit))
637                 return 0;
638
639         memset(req_buf, 0, hw->hw_info.req_buf_size);
640         memset(res_buf, 0, hw->hw_info.res_buf_size);
641
642         req_buf->unshare_buffer.length =
643                         FJES_DEV_COMMAND_UNSHARE_BUFFER_REQ_LEN;
644         req_buf->unshare_buffer.epid = dest_epid;
645
646         res_buf->unshare_buffer.length = 0;
647         res_buf->unshare_buffer.code = 0;
648
649         trace_fjes_hw_unregister_buff_addr_req(req_buf);
650         ret = fjes_hw_issue_request_command(hw, FJES_CMD_REQ_UNSHARE_BUFFER);
651
652         timeout = FJES_COMMAND_REQ_BUFF_TIMEOUT * 1000;
653         while ((ret == FJES_CMD_STATUS_NORMAL) &&
654                (res_buf->unshare_buffer.length ==
655                 FJES_DEV_COMMAND_UNSHARE_BUFFER_RES_LEN) &&
656                (res_buf->unshare_buffer.code ==
657                 FJES_CMD_REQ_RES_CODE_BUSY) &&
658                (timeout > 0)) {
659                 msleep(200 + hw->my_epid * 20);
660                 timeout -= (200 + hw->my_epid * 20);
661
662                 res_buf->unshare_buffer.length = 0;
663                 res_buf->unshare_buffer.code = 0;
664
665                 ret =
666                 fjes_hw_issue_request_command(hw, FJES_CMD_REQ_UNSHARE_BUFFER);
667         }
668
669         result = 0;
670
671         trace_fjes_hw_unregister_buff_addr(res_buf, timeout);
672
673         if (res_buf->unshare_buffer.length !=
674                         FJES_DEV_COMMAND_UNSHARE_BUFFER_RES_LEN) {
675                 trace_fjes_hw_unregister_buff_addr_err("Invalid res_buf");
676                 result = -ENOMSG;
677         } else if (ret == FJES_CMD_STATUS_NORMAL) {
678                 switch (res_buf->unshare_buffer.code) {
679                 case FJES_CMD_REQ_RES_CODE_NORMAL:
680                         result = 0;
681                         clear_bit(dest_epid, &hw->hw_info.buffer_share_bit);
682                         break;
683                 case FJES_CMD_REQ_RES_CODE_BUSY:
684                         trace_fjes_hw_unregister_buff_addr_err("Busy Timeout");
685                         result = -EBUSY;
686                         break;
687                 default:
688                         result = -EPERM;
689                         break;
690                 }
691         } else {
692                 switch (ret) {
693                 case FJES_CMD_STATUS_UNKNOWN:
694                         result = -EPERM;
695                         break;
696                 case FJES_CMD_STATUS_TIMEOUT:
697                         trace_fjes_hw_unregister_buff_addr_err("Timeout");
698                         result = -EBUSY;
699                         break;
700                 case FJES_CMD_STATUS_ERROR_PARAM:
701                 case FJES_CMD_STATUS_ERROR_STATUS:
702                 default:
703                         result = -EPERM;
704                         break;
705                 }
706         }
707
708         return result;
709 }
710
711 int fjes_hw_raise_interrupt(struct fjes_hw *hw, int dest_epid,
712                             enum REG_ICTL_MASK  mask)
713 {
714         u32 ig = mask | dest_epid;
715
716         wr32(XSCT_IG, cpu_to_le32(ig));
717
718         return 0;
719 }
720
721 u32 fjes_hw_capture_interrupt_status(struct fjes_hw *hw)
722 {
723         u32 cur_is;
724
725         cur_is = rd32(XSCT_IS);
726
727         return cur_is;
728 }
729
730 void fjes_hw_set_irqmask(struct fjes_hw *hw,
731                          enum REG_ICTL_MASK intr_mask, bool mask)
732 {
733         if (mask)
734                 wr32(XSCT_IMS, intr_mask);
735         else
736                 wr32(XSCT_IMC, intr_mask);
737 }
738
739 bool fjes_hw_epid_is_same_zone(struct fjes_hw *hw, int epid)
740 {
741         if (epid >= hw->max_epid)
742                 return false;
743
744         if ((hw->ep_shm_info[epid].es_status !=
745                         FJES_ZONING_STATUS_ENABLE) ||
746                 (hw->ep_shm_info[hw->my_epid].zone ==
747                         FJES_ZONING_ZONE_TYPE_NONE))
748                 return false;
749         else
750                 return (hw->ep_shm_info[epid].zone ==
751                                 hw->ep_shm_info[hw->my_epid].zone);
752 }
753
754 int fjes_hw_epid_is_shared(struct fjes_device_shared_info *share,
755                            int dest_epid)
756 {
757         int value = false;
758
759         if (dest_epid < share->epnum)
760                 value = share->ep_status[dest_epid];
761
762         return value;
763 }
764
765 static bool fjes_hw_epid_is_stop_requested(struct fjes_hw *hw, int src_epid)
766 {
767         return test_bit(src_epid, &hw->txrx_stop_req_bit);
768 }
769
770 static bool fjes_hw_epid_is_stop_process_done(struct fjes_hw *hw, int src_epid)
771 {
772         return (hw->ep_shm_info[src_epid].tx.info->v1i.rx_status &
773                         FJES_RX_STOP_REQ_DONE);
774 }
775
776 enum ep_partner_status
777 fjes_hw_get_partner_ep_status(struct fjes_hw *hw, int epid)
778 {
779         enum ep_partner_status status;
780
781         if (fjes_hw_epid_is_shared(hw->hw_info.share, epid)) {
782                 if (fjes_hw_epid_is_stop_requested(hw, epid)) {
783                         status = EP_PARTNER_WAITING;
784                 } else {
785                         if (fjes_hw_epid_is_stop_process_done(hw, epid))
786                                 status = EP_PARTNER_COMPLETE;
787                         else
788                                 status = EP_PARTNER_SHARED;
789                 }
790         } else {
791                 status = EP_PARTNER_UNSHARE;
792         }
793
794         return status;
795 }
796
797 void fjes_hw_raise_epstop(struct fjes_hw *hw)
798 {
799         enum ep_partner_status status;
800         unsigned long flags;
801         int epidx;
802
803         for (epidx = 0; epidx < hw->max_epid; epidx++) {
804                 if (epidx == hw->my_epid)
805                         continue;
806
807                 status = fjes_hw_get_partner_ep_status(hw, epidx);
808                 switch (status) {
809                 case EP_PARTNER_SHARED:
810                         fjes_hw_raise_interrupt(hw, epidx,
811                                                 REG_ICTL_MASK_TXRX_STOP_REQ);
812                         hw->ep_shm_info[epidx].ep_stats.send_intr_unshare += 1;
813                         break;
814                 default:
815                         break;
816                 }
817
818                 set_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
819                 set_bit(epidx, &hw->txrx_stop_req_bit);
820
821                 spin_lock_irqsave(&hw->rx_status_lock, flags);
822                 hw->ep_shm_info[epidx].tx.info->v1i.rx_status |=
823                                 FJES_RX_STOP_REQ_REQUEST;
824                 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
825         }
826 }
827
828 int fjes_hw_wait_epstop(struct fjes_hw *hw)
829 {
830         enum ep_partner_status status;
831         union ep_buffer_info *info;
832         int wait_time = 0;
833         int epidx;
834
835         while (hw->hw_info.buffer_unshare_reserve_bit &&
836                (wait_time < FJES_COMMAND_EPSTOP_WAIT_TIMEOUT * 1000)) {
837                 for (epidx = 0; epidx < hw->max_epid; epidx++) {
838                         if (epidx == hw->my_epid)
839                                 continue;
840                         status = fjes_hw_epid_is_shared(hw->hw_info.share,
841                                                         epidx);
842                         info = hw->ep_shm_info[epidx].rx.info;
843                         if ((!status ||
844                              (info->v1i.rx_status &
845                               FJES_RX_STOP_REQ_DONE)) &&
846                             test_bit(epidx,
847                                      &hw->hw_info.buffer_unshare_reserve_bit)) {
848                                 clear_bit(epidx,
849                                           &hw->hw_info.buffer_unshare_reserve_bit);
850                         }
851                 }
852
853                 msleep(100);
854                 wait_time += 100;
855         }
856
857         for (epidx = 0; epidx < hw->max_epid; epidx++) {
858                 if (epidx == hw->my_epid)
859                         continue;
860                 if (test_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit))
861                         clear_bit(epidx,
862                                   &hw->hw_info.buffer_unshare_reserve_bit);
863         }
864
865         return (wait_time < FJES_COMMAND_EPSTOP_WAIT_TIMEOUT * 1000)
866                         ? 0 : -EBUSY;
867 }
868
869 bool fjes_hw_check_epbuf_version(struct epbuf_handler *epbh, u32 version)
870 {
871         union ep_buffer_info *info = epbh->info;
872
873         return (info->common.version == version);
874 }
875
876 bool fjes_hw_check_mtu(struct epbuf_handler *epbh, u32 mtu)
877 {
878         union ep_buffer_info *info = epbh->info;
879
880         return ((info->v1i.frame_max == FJES_MTU_TO_FRAME_SIZE(mtu)) &&
881                 info->v1i.rx_status & FJES_RX_MTU_CHANGING_DONE);
882 }
883
884 bool fjes_hw_check_vlan_id(struct epbuf_handler *epbh, u16 vlan_id)
885 {
886         union ep_buffer_info *info = epbh->info;
887         bool ret = false;
888         int i;
889
890         if (vlan_id == 0) {
891                 ret = true;
892         } else {
893                 for (i = 0; i < EP_BUFFER_SUPPORT_VLAN_MAX; i++) {
894                         if (vlan_id == info->v1i.vlan_id[i]) {
895                                 ret = true;
896                                 break;
897                         }
898                 }
899         }
900         return ret;
901 }
902
903 bool fjes_hw_set_vlan_id(struct epbuf_handler *epbh, u16 vlan_id)
904 {
905         union ep_buffer_info *info = epbh->info;
906         int i;
907
908         for (i = 0; i < EP_BUFFER_SUPPORT_VLAN_MAX; i++) {
909                 if (info->v1i.vlan_id[i] == 0) {
910                         info->v1i.vlan_id[i] = vlan_id;
911                         return true;
912                 }
913         }
914         return false;
915 }
916
917 void fjes_hw_del_vlan_id(struct epbuf_handler *epbh, u16 vlan_id)
918 {
919         union ep_buffer_info *info = epbh->info;
920         int i;
921
922         if (0 != vlan_id) {
923                 for (i = 0; i < EP_BUFFER_SUPPORT_VLAN_MAX; i++) {
924                         if (vlan_id == info->v1i.vlan_id[i])
925                                 info->v1i.vlan_id[i] = 0;
926                 }
927         }
928 }
929
930 bool fjes_hw_epbuf_rx_is_empty(struct epbuf_handler *epbh)
931 {
932         union ep_buffer_info *info = epbh->info;
933
934         if (!(info->v1i.rx_status & FJES_RX_MTU_CHANGING_DONE))
935                 return true;
936
937         if (info->v1i.count_max == 0)
938                 return true;
939
940         return EP_RING_EMPTY(info->v1i.head, info->v1i.tail,
941                              info->v1i.count_max);
942 }
943
944 void *fjes_hw_epbuf_rx_curpkt_get_addr(struct epbuf_handler *epbh,
945                                        size_t *psize)
946 {
947         union ep_buffer_info *info = epbh->info;
948         struct esmem_frame *ring_frame;
949         void *frame;
950
951         ring_frame = (struct esmem_frame *)&(epbh->ring[EP_RING_INDEX
952                                              (info->v1i.head,
953                                               info->v1i.count_max) *
954                                              info->v1i.frame_max]);
955
956         *psize = (size_t)ring_frame->frame_size;
957
958         frame = ring_frame->frame_data;
959
960         return frame;
961 }
962
963 void fjes_hw_epbuf_rx_curpkt_drop(struct epbuf_handler *epbh)
964 {
965         union ep_buffer_info *info = epbh->info;
966
967         if (fjes_hw_epbuf_rx_is_empty(epbh))
968                 return;
969
970         EP_RING_INDEX_INC(epbh->info->v1i.head, info->v1i.count_max);
971 }
972
973 int fjes_hw_epbuf_tx_pkt_send(struct epbuf_handler *epbh,
974                               void *frame, size_t size)
975 {
976         union ep_buffer_info *info = epbh->info;
977         struct esmem_frame *ring_frame;
978
979         if (EP_RING_FULL(info->v1i.head, info->v1i.tail, info->v1i.count_max))
980                 return -ENOBUFS;
981
982         ring_frame = (struct esmem_frame *)&(epbh->ring[EP_RING_INDEX
983                                              (info->v1i.tail - 1,
984                                               info->v1i.count_max) *
985                                              info->v1i.frame_max]);
986
987         ring_frame->frame_size = size;
988         memcpy((void *)(ring_frame->frame_data), (void *)frame, size);
989
990         EP_RING_INDEX_INC(epbh->info->v1i.tail, info->v1i.count_max);
991
992         return 0;
993 }
994
995 static void fjes_hw_update_zone_task(struct work_struct *work)
996 {
997         struct fjes_hw *hw = container_of(work,
998                         struct fjes_hw, update_zone_task);
999
1000         struct my_s {u8 es_status; u8 zone; } *info;
1001         union fjes_device_command_res *res_buf;
1002         enum ep_partner_status pstatus;
1003
1004         struct fjes_adapter *adapter;
1005         struct net_device *netdev;
1006         unsigned long flags;
1007
1008         ulong unshare_bit = 0;
1009         ulong share_bit = 0;
1010         ulong irq_bit = 0;
1011
1012         int epidx;
1013         int ret;
1014
1015         adapter = (struct fjes_adapter *)hw->back;
1016         netdev = adapter->netdev;
1017         res_buf = hw->hw_info.res_buf;
1018         info = (struct my_s *)&res_buf->info.info;
1019
1020         mutex_lock(&hw->hw_info.lock);
1021
1022         ret = fjes_hw_request_info(hw);
1023         switch (ret) {
1024         case -ENOMSG:
1025         case -EBUSY:
1026         default:
1027                 if (!work_pending(&adapter->force_close_task)) {
1028                         adapter->force_reset = true;
1029                         schedule_work(&adapter->force_close_task);
1030                 }
1031                 break;
1032
1033         case 0:
1034
1035                 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1036                         if (epidx == hw->my_epid) {
1037                                 hw->ep_shm_info[epidx].es_status =
1038                                         info[epidx].es_status;
1039                                 hw->ep_shm_info[epidx].zone =
1040                                         info[epidx].zone;
1041                                 continue;
1042                         }
1043
1044                         pstatus = fjes_hw_get_partner_ep_status(hw, epidx);
1045                         switch (pstatus) {
1046                         case EP_PARTNER_UNSHARE:
1047                         default:
1048                                 if ((info[epidx].zone !=
1049                                         FJES_ZONING_ZONE_TYPE_NONE) &&
1050                                     (info[epidx].es_status ==
1051                                         FJES_ZONING_STATUS_ENABLE) &&
1052                                     (info[epidx].zone ==
1053                                         info[hw->my_epid].zone))
1054                                         set_bit(epidx, &share_bit);
1055                                 else
1056                                         set_bit(epidx, &unshare_bit);
1057                                 break;
1058
1059                         case EP_PARTNER_COMPLETE:
1060                         case EP_PARTNER_WAITING:
1061                                 if ((info[epidx].zone ==
1062                                         FJES_ZONING_ZONE_TYPE_NONE) ||
1063                                     (info[epidx].es_status !=
1064                                         FJES_ZONING_STATUS_ENABLE) ||
1065                                     (info[epidx].zone !=
1066                                         info[hw->my_epid].zone)) {
1067                                         set_bit(epidx,
1068                                                 &adapter->unshare_watch_bitmask);
1069                                         set_bit(epidx,
1070                                                 &hw->hw_info.buffer_unshare_reserve_bit);
1071                                 }
1072                                 break;
1073
1074                         case EP_PARTNER_SHARED:
1075                                 if ((info[epidx].zone ==
1076                                         FJES_ZONING_ZONE_TYPE_NONE) ||
1077                                     (info[epidx].es_status !=
1078                                         FJES_ZONING_STATUS_ENABLE) ||
1079                                     (info[epidx].zone !=
1080                                         info[hw->my_epid].zone))
1081                                         set_bit(epidx, &irq_bit);
1082                                 break;
1083                         }
1084
1085                         hw->ep_shm_info[epidx].es_status =
1086                                 info[epidx].es_status;
1087                         hw->ep_shm_info[epidx].zone = info[epidx].zone;
1088                 }
1089                 break;
1090         }
1091
1092         mutex_unlock(&hw->hw_info.lock);
1093
1094         for (epidx = 0; epidx < hw->max_epid; epidx++) {
1095                 if (epidx == hw->my_epid)
1096                         continue;
1097
1098                 if (test_bit(epidx, &share_bit)) {
1099                         spin_lock_irqsave(&hw->rx_status_lock, flags);
1100                         fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
1101                                             netdev->dev_addr, netdev->mtu);
1102                         spin_unlock_irqrestore(&hw->rx_status_lock, flags);
1103
1104                         mutex_lock(&hw->hw_info.lock);
1105
1106                         ret = fjes_hw_register_buff_addr(
1107                                 hw, epidx, &hw->ep_shm_info[epidx]);
1108
1109                         switch (ret) {
1110                         case 0:
1111                                 break;
1112                         case -ENOMSG:
1113                         case -EBUSY:
1114                         default:
1115                                 if (!work_pending(&adapter->force_close_task)) {
1116                                         adapter->force_reset = true;
1117                                         schedule_work(
1118                                           &adapter->force_close_task);
1119                                 }
1120                                 break;
1121                         }
1122                         mutex_unlock(&hw->hw_info.lock);
1123
1124                         hw->ep_shm_info[epidx].ep_stats
1125                                               .com_regist_buf_exec += 1;
1126                 }
1127
1128                 if (test_bit(epidx, &unshare_bit)) {
1129                         mutex_lock(&hw->hw_info.lock);
1130
1131                         ret = fjes_hw_unregister_buff_addr(hw, epidx);
1132
1133                         switch (ret) {
1134                         case 0:
1135                                 break;
1136                         case -ENOMSG:
1137                         case -EBUSY:
1138                         default:
1139                                 if (!work_pending(&adapter->force_close_task)) {
1140                                         adapter->force_reset = true;
1141                                         schedule_work(
1142                                           &adapter->force_close_task);
1143                                 }
1144                                 break;
1145                         }
1146
1147                         mutex_unlock(&hw->hw_info.lock);
1148
1149                         hw->ep_shm_info[epidx].ep_stats
1150                                               .com_unregist_buf_exec += 1;
1151
1152                         if (ret == 0) {
1153                                 spin_lock_irqsave(&hw->rx_status_lock, flags);
1154                                 fjes_hw_setup_epbuf(
1155                                         &hw->ep_shm_info[epidx].tx,
1156                                         netdev->dev_addr, netdev->mtu);
1157                                 spin_unlock_irqrestore(&hw->rx_status_lock,
1158                                                        flags);
1159                         }
1160                 }
1161
1162                 if (test_bit(epidx, &irq_bit)) {
1163                         fjes_hw_raise_interrupt(hw, epidx,
1164                                                 REG_ICTL_MASK_TXRX_STOP_REQ);
1165
1166                         hw->ep_shm_info[epidx].ep_stats.send_intr_unshare += 1;
1167
1168                         set_bit(epidx, &hw->txrx_stop_req_bit);
1169                         spin_lock_irqsave(&hw->rx_status_lock, flags);
1170                         hw->ep_shm_info[epidx].tx.
1171                                 info->v1i.rx_status |=
1172                                         FJES_RX_STOP_REQ_REQUEST;
1173                         spin_unlock_irqrestore(&hw->rx_status_lock, flags);
1174                         set_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
1175                 }
1176         }
1177
1178         if (irq_bit || adapter->unshare_watch_bitmask) {
1179                 if (!work_pending(&adapter->unshare_watch_task))
1180                         queue_work(adapter->control_wq,
1181                                    &adapter->unshare_watch_task);
1182         }
1183 }
1184
1185 static void fjes_hw_epstop_task(struct work_struct *work)
1186 {
1187         struct fjes_hw *hw = container_of(work, struct fjes_hw, epstop_task);
1188         struct fjes_adapter *adapter = (struct fjes_adapter *)hw->back;
1189         unsigned long flags;
1190
1191         ulong remain_bit;
1192         int epid_bit;
1193
1194         while ((remain_bit = hw->epstop_req_bit)) {
1195                 for (epid_bit = 0; remain_bit; remain_bit >>= 1, epid_bit++) {
1196                         if (remain_bit & 1) {
1197                                 spin_lock_irqsave(&hw->rx_status_lock, flags);
1198                                 hw->ep_shm_info[epid_bit].
1199                                         tx.info->v1i.rx_status |=
1200                                                 FJES_RX_STOP_REQ_DONE;
1201                                 spin_unlock_irqrestore(&hw->rx_status_lock,
1202                                                        flags);
1203
1204                                 clear_bit(epid_bit, &hw->epstop_req_bit);
1205                                 set_bit(epid_bit,
1206                                         &adapter->unshare_watch_bitmask);
1207
1208                                 if (!work_pending(&adapter->unshare_watch_task))
1209                                         queue_work(
1210                                                 adapter->control_wq,
1211                                                 &adapter->unshare_watch_task);
1212                         }
1213                 }
1214         }
1215 }
1216
1217 int fjes_hw_start_debug(struct fjes_hw *hw)
1218 {
1219         union fjes_device_command_req *req_buf = hw->hw_info.req_buf;
1220         union fjes_device_command_res *res_buf = hw->hw_info.res_buf;
1221         enum fjes_dev_command_response_e ret;
1222         int page_count;
1223         int result = 0;
1224         void *addr;
1225         int i;
1226
1227         if (!hw->hw_info.trace)
1228                 return -EPERM;
1229         memset(hw->hw_info.trace, 0, FJES_DEBUG_BUFFER_SIZE);
1230
1231         memset(req_buf, 0, hw->hw_info.req_buf_size);
1232         memset(res_buf, 0, hw->hw_info.res_buf_size);
1233
1234         req_buf->start_trace.length =
1235                 FJES_DEV_COMMAND_START_DBG_REQ_LEN(hw->hw_info.trace_size);
1236         req_buf->start_trace.mode = hw->debug_mode;
1237         req_buf->start_trace.buffer_len = hw->hw_info.trace_size;
1238         page_count = hw->hw_info.trace_size / FJES_DEBUG_PAGE_SIZE;
1239         for (i = 0; i < page_count; i++) {
1240                 addr = ((u8 *)hw->hw_info.trace) + i * FJES_DEBUG_PAGE_SIZE;
1241                 req_buf->start_trace.buffer[i] =
1242                         (__le64)(page_to_phys(vmalloc_to_page(addr)) +
1243                         offset_in_page(addr));
1244         }
1245
1246         res_buf->start_trace.length = 0;
1247         res_buf->start_trace.code = 0;
1248
1249         trace_fjes_hw_start_debug_req(req_buf);
1250         ret = fjes_hw_issue_request_command(hw, FJES_CMD_REQ_START_DEBUG);
1251         trace_fjes_hw_start_debug(res_buf);
1252
1253         if (res_buf->start_trace.length !=
1254                 FJES_DEV_COMMAND_START_DBG_RES_LEN) {
1255                 result = -ENOMSG;
1256                 trace_fjes_hw_start_debug_err("Invalid res_buf");
1257         } else if (ret == FJES_CMD_STATUS_NORMAL) {
1258                 switch (res_buf->start_trace.code) {
1259                 case FJES_CMD_REQ_RES_CODE_NORMAL:
1260                         result = 0;
1261                         break;
1262                 default:
1263                         result = -EPERM;
1264                         break;
1265                 }
1266         } else {
1267                 switch (ret) {
1268                 case FJES_CMD_STATUS_UNKNOWN:
1269                         result = -EPERM;
1270                         break;
1271                 case FJES_CMD_STATUS_TIMEOUT:
1272                         trace_fjes_hw_start_debug_err("Busy Timeout");
1273                         result = -EBUSY;
1274                         break;
1275                 case FJES_CMD_STATUS_ERROR_PARAM:
1276                 case FJES_CMD_STATUS_ERROR_STATUS:
1277                 default:
1278                         result = -EPERM;
1279                         break;
1280                 }
1281         }
1282
1283         return result;
1284 }
1285
1286 int fjes_hw_stop_debug(struct fjes_hw *hw)
1287 {
1288         union fjes_device_command_req *req_buf = hw->hw_info.req_buf;
1289         union fjes_device_command_res *res_buf = hw->hw_info.res_buf;
1290         enum fjes_dev_command_response_e ret;
1291         int result = 0;
1292
1293         if (!hw->hw_info.trace)
1294                 return -EPERM;
1295
1296         memset(req_buf, 0, hw->hw_info.req_buf_size);
1297         memset(res_buf, 0, hw->hw_info.res_buf_size);
1298         req_buf->stop_trace.length = FJES_DEV_COMMAND_STOP_DBG_REQ_LEN;
1299
1300         res_buf->stop_trace.length = 0;
1301         res_buf->stop_trace.code = 0;
1302
1303         ret = fjes_hw_issue_request_command(hw, FJES_CMD_REQ_STOP_DEBUG);
1304         trace_fjes_hw_stop_debug(res_buf);
1305
1306         if (res_buf->stop_trace.length != FJES_DEV_COMMAND_STOP_DBG_RES_LEN) {
1307                 trace_fjes_hw_stop_debug_err("Invalid res_buf");
1308                 result = -ENOMSG;
1309         } else if (ret == FJES_CMD_STATUS_NORMAL) {
1310                 switch (res_buf->stop_trace.code) {
1311                 case FJES_CMD_REQ_RES_CODE_NORMAL:
1312                         result = 0;
1313                         hw->debug_mode = 0;
1314                         break;
1315                 default:
1316                         result = -EPERM;
1317                         break;
1318                 }
1319         } else {
1320                 switch (ret) {
1321                 case FJES_CMD_STATUS_UNKNOWN:
1322                         result = -EPERM;
1323                         break;
1324                 case FJES_CMD_STATUS_TIMEOUT:
1325                         result = -EBUSY;
1326                         trace_fjes_hw_stop_debug_err("Busy Timeout");
1327                         break;
1328                 case FJES_CMD_STATUS_ERROR_PARAM:
1329                 case FJES_CMD_STATUS_ERROR_STATUS:
1330                 default:
1331                         result = -EPERM;
1332                         break;
1333                 }
1334         }
1335
1336         return result;
1337 }