1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2017-2026 Morse Micro
 */
#include "yaps_hw.h"
#include "bus.h"
#include "hif.h"
#include "yaps.h"

#define YAPS_HW_WINDOW_SIZE_BYTES 32768
#define YAPS_MAX_PKT_SIZE_BYTES 16128
#define YAPS_METADATA_PAGE_COUNT 1

#define YAPS_PHANDLE_CORRUPTION_WAR_EXTRA_PAGE 1

#define YAPS_PAGE_SIZE 256

/* Calculate padding required for yaps transaction */
#define YAPS_CALC_PADDING(_bytes) ((_bytes) & 0x3 ? (4 - ((_bytes) & 0x3)) : 0)

#define YAPS_RESERVED_PAGE_SIZE 256

/*
 * Yaps data stream delimiter is a 32 bit word with the following fields:
 *
 * pkt_size (14 bits) - Packet size not including delimiter or padding
 * pool_id  (3  bits) - Pool that pages should be allocated from.
 * padding  (2  bits) - Padding required to bring packet to word (4 byte)
 * irq      (1  bit ) - Raise a PKT_IRQ on the YDS this is sent to
 * reserved (5  bits) - Reserved, must write as 0
 * crc      (7  bits) - YAPS CRC
 */

/* Packet size not including delimiter or padding */
#define YAPS_DELIM_GET_PKT_SIZE(_delim) \
	(((_delim) & 0x3FFF) - YAPS_RESERVED_PAGE_SIZE)
#define YAPS_DELIM_SET_PKT_SIZE(_pkt_size) \
	(((_pkt_size) & 0x3FFF) + YAPS_RESERVED_PAGE_SIZE)
#define YAPS_DELIM_GET_PHANDLE_SIZE(_delim) (((_delim) & 0x3FFF))

/* Pool that pages should be allocated from. */
#define YAPS_DELIM_SET_POOL_ID(_pool_id) (((_pool_id) & 0x7) << 14)

/* Padding required to bring packet to word (4 byte) boundary */
#define YAPS_DELIM_GET_PADDING(_delim) (((_delim) >> 17) & 0x3)
#define YAPS_DELIM_SET_PADDING(_padding) (((_padding) & 0x3) << 17)

/* Raise a PKT_IRQ on the YDS this is sent to */
#define YAPS_DELIM_SET_IRQ(_irq) (((_irq) & 0x1) << 19)

/* YAPS CRC */
#define YAPS_DELIM_GET_CRC(_delim) (((_delim) >> 25) & 0x7F)
#define YAPS_DELIM_SET_CRC(_crc) (((_crc) & 0x7F) << 25)

struct mm81x_yaps_status_regs {
	/* Allocation pools */
	u32 tc_tx_pool_num_pages;
	u32 tc_cmd_pool_num_pages;
	u32 tc_beacon_pool_num_pages;
	u32 tc_mgmt_pool_num_pages;
	u32 fc_rx_pool_num_pages;
	u32 fc_resp_pool_num_pages;
	u32 fc_tx_sts_pool_num_pages;
	u32 fc_aux_pool_num_pages;
	u32 tc_tx_num_pkts;
	u32 tc_cmd_num_pkts;
	u32 tc_beacon_num_pkts;
	u32 tc_mgmt_num_pkts;
	u32 fc_num_pkts;
	u32 fc_done_num_pkts;
	u32 fc_rx_bytes_in_queue;
	u32 tc_delim_crc_fail_detected;
	u32 fc_host_ysl_status;
	u32 lock;
} __packed __aligned(8);

struct mm81x_yaps_hw_status_regs {
	__le32 tc_tx_pool_num_pages;
	__le32 tc_cmd_pool_num_pages;
	__le32 tc_beacon_pool_num_pages;
	__le32 tc_mgmt_pool_num_pages;
	__le32 fc_rx_pool_num_pages;
	__le32 fc_resp_pool_num_pages;
	__le32 fc_tx_sts_pool_num_pages;
	__le32 fc_aux_pool_num_pages;
	__le32 tc_tx_num_pkts;
	__le32 tc_cmd_num_pkts;
	__le32 tc_beacon_num_pkts;
	__le32 tc_mgmt_num_pkts;
	__le32 fc_num_pkts;
	__le32 fc_done_num_pkts;
	__le32 fc_rx_bytes_in_queue;
	__le32 tc_delim_crc_fail_detected;
	__le32 fc_host_ysl_status;
	__le32 lock;
} __packed __aligned(8);

struct mm81x_yaps_hw_aux_data {
	unsigned long access_lock;

	u32 yds_addr;
	u32 ysl_addr;
	u32 status_regs_addr;

	/* Alloc pool sizes */
	u16 tc_tx_pool_size;
	u16 tc_cmd_pool_size;
	u8 tc_beacon_pool_size;
	u8 tc_mgmt_pool_size;
	u8 fc_rx_pool_size;
	u8 fc_resp_pool_size;
	u8 fc_tx_sts_pool_size;
	u8 fc_aux_pool_size;

	/* To chip/from chip queue sizes */
	u8 tc_tx_q_size;
	u8 tc_cmd_q_size;
	u8 tc_beacon_q_size;
	u8 tc_mgmt_q_size;
	u8 fc_q_size;
	u8 fc_done_q_size;

	u16 reserved_yaps_page_size;

	/* Buffers to/from chip to support large contiguous reads/writes */
	char *to_chip_buffer;
	char *from_chip_buffer;

	/* status registers in host endian */
	struct mm81x_yaps_status_regs status_regs;

	/* DMA target buffer in firmware endian */
	struct mm81x_yaps_hw_status_regs hw_status_regs;
};

static int mm81x_yaps_hw_lock(struct mm81x_yaps *yaps)
{
	if (test_and_set_bit_lock(0, &yaps->aux_data->access_lock))
		return -1;
	return 0;
}

static void mm81x_yaps_hw_unlock(struct mm81x_yaps *yaps)
{
	clear_bit_unlock(0, &yaps->aux_data->access_lock);
}

static void
mm81x_yaps_hw_fill_aux_data_from_hw_tbl(struct mm81x_yaps_hw_aux_data *a,
					struct mm81x_yaps_hw_table *t)
{
	a->ysl_addr = __le32_to_cpu(t->ysl_addr);
	a->yds_addr = __le32_to_cpu(t->yds_addr);
	a->status_regs_addr = __le32_to_cpu(t->status_regs_addr);
	a->tc_tx_pool_size = __le16_to_cpu(t->tc_tx_pool_size);
	a->fc_rx_pool_size = __le16_to_cpu(t->fc_rx_pool_size);
	a->tc_cmd_pool_size = t->tc_cmd_pool_size;
	a->tc_beacon_pool_size = t->tc_beacon_pool_size;
	a->tc_mgmt_pool_size = t->tc_mgmt_pool_size;
	a->fc_resp_pool_size = t->fc_resp_pool_size;
	a->fc_tx_sts_pool_size = t->fc_tx_sts_pool_size;
	a->fc_aux_pool_size = t->fc_aux_pool_size;
	a->tc_tx_q_size = t->tc_tx_q_size;
	a->tc_cmd_q_size = t->tc_cmd_q_size;
	a->tc_beacon_q_size = t->tc_beacon_q_size;
	a->tc_mgmt_q_size = t->tc_mgmt_q_size;
	a->fc_q_size = t->fc_q_size;
	a->fc_done_q_size = t->fc_done_q_size;
	a->reserved_yaps_page_size = le16_to_cpu(t->yaps_reserved_page_size);
}

static u8 mm81x_yaps_hw_crc(u32 word)
{
	u8 crc = 0;
	u8 byte;
	int i;

	/* Mask to look at only non-CRC bits */
	word &= 0x1ffffff;

	for (i = 0; i < 4; i++) {
		byte = (word >> 24) & 0xff;
		crc = crc7_be(crc, &byte, 1);
		word <<= 8;
	}

	return crc >> 1;
}

static u32 mm81x_write_pkts_h_build_delim(struct mm81x_yaps *yaps,
					  unsigned int size, u8 pool_id,
					  bool irq)
{
	u32 delim = 0;

	delim |= YAPS_DELIM_SET_PKT_SIZE(size);
	delim |= YAPS_DELIM_SET_PADDING(YAPS_CALC_PADDING(size));
	delim |= YAPS_DELIM_SET_POOL_ID(pool_id);
	delim |= YAPS_DELIM_SET_IRQ(irq);
	delim |= YAPS_DELIM_SET_CRC(mm81x_yaps_hw_crc(delim));
	return delim;
}

void mm81x_yaps_hw_enable_irqs(struct mm81x *mors, bool enable)
{
	mm81x_hw_irq_enable(mors, MM81X_INT_YAPS_FC_PKT_WAITING_IRQN, enable);
	mm81x_hw_irq_enable(mors, MM81X_INT_YAPS_FC_PACKET_FREED_UP_IRQN,
			    enable);
}

void mm81x_yaps_hw_read_table(struct mm81x *mors,
			      struct mm81x_yaps_hw_table *tbl_ptr)
{
	mm81x_yaps_hw_fill_aux_data_from_hw_tbl(mors->hif.u.yaps.aux_data,
						tbl_ptr);
	mm81x_yaps_hw_enable_irqs(mors, true);
}

static unsigned int mm81x_write_pkts_h_pages_required(struct mm81x_yaps *yaps,
						      unsigned int size_bytes)
{
	/* Always account for the first metadata page */
	return DIV_ROUND_UP(size_bytes +
				    yaps->aux_data->reserved_yaps_page_size,
			    YAPS_PAGE_SIZE) +
	       YAPS_METADATA_PAGE_COUNT +
	       YAPS_PHANDLE_CORRUPTION_WAR_EXTRA_PAGE;
}

/*
 * Checks if a single pkt will fit in the chip using the pool/alloc holding
 * information from the last status register read.
 */
static bool mm81x_write_pkts_h_will_fit(struct mm81x_yaps *yaps,
					struct mm81x_yaps_pkt *pkt, bool update)
{
	bool will_fit = true;
	const int pages_required =
		mm81x_write_pkts_h_pages_required(yaps, pkt->skb->len);
	int *pool_pages_avail = NULL;
	int *pkts_in_queue = NULL;
	int queue_pkts_avail = 0;

	switch (pkt->tc_queue) {
	case MM81X_YAPS_TX_Q:
		pool_pages_avail =
			&yaps->aux_data->status_regs.tc_tx_pool_num_pages;
		pkts_in_queue = &yaps->aux_data->status_regs.tc_tx_num_pkts;
		queue_pkts_avail =
			yaps->aux_data->tc_tx_q_size - *pkts_in_queue;
		break;
	case MM81X_YAPS_CMD_Q:
		pool_pages_avail =
			&yaps->aux_data->status_regs.tc_cmd_pool_num_pages;
		pkts_in_queue = &yaps->aux_data->status_regs.tc_cmd_num_pkts;
		queue_pkts_avail =
			yaps->aux_data->tc_cmd_q_size - *pkts_in_queue;
		break;
	case MM81X_YAPS_BEACON_Q:
		pool_pages_avail =
			&yaps->aux_data->status_regs.tc_beacon_pool_num_pages;
		pkts_in_queue = &yaps->aux_data->status_regs.tc_beacon_num_pkts;
		queue_pkts_avail =
			yaps->aux_data->tc_beacon_q_size - *pkts_in_queue;
		break;
	case MM81X_YAPS_MGMT_Q:
		pool_pages_avail =
			&yaps->aux_data->status_regs.tc_mgmt_pool_num_pages;
		pkts_in_queue = &yaps->aux_data->status_regs.tc_mgmt_num_pkts;
		queue_pkts_avail =
			yaps->aux_data->tc_mgmt_q_size - *pkts_in_queue;
		break;
	default:
		dev_err(yaps->mors->dev, "yaps invalid tc queue");
		return false;
	}

	WARN_ON(queue_pkts_avail < 0);

	if (pages_required > *pool_pages_avail)
		will_fit = false;

	if (queue_pkts_avail == 0)
		will_fit = false;

	if (will_fit && update) {
		*pool_pages_avail -= pages_required;
		*pkts_in_queue += 1;
	}

	return will_fit;
}

static int mm81x_write_pkts_h_err_check(struct mm81x_yaps *yaps,
					struct mm81x_yaps_pkt *pkt)
{
	if (pkt->skb->len + yaps->aux_data->reserved_yaps_page_size >
	    YAPS_MAX_PKT_SIZE_BYTES)
		return -EMSGSIZE;
	if (pkt->tc_queue >= MM81X_YAPS_NUM_TC_Q)
		return -EINVAL;
	if (!mm81x_write_pkts_h_will_fit(yaps, pkt, true))
		return -EAGAIN;

	return 0;
}

static int mm81x_yaps_hw_write_pkts(struct mm81x_yaps *yaps,
				    struct mm81x_yaps_pkt *pkts, int num_pkts,
				    int *num_pkts_sent)
{
	int ret = 0;
	int i;
	u32 delim = 0;
	int tx_len;
	int batch_txn_len = 0;
	int pkts_pending = 0;
	bool delim_irq = false;
	char *to_chip_buffer_aligned =
		PTR_ALIGN(yaps->aux_data->to_chip_buffer,
			  mm81x_bus_get_alignment(yaps->mors));
	char *write_buf = to_chip_buffer_aligned;

	ret = mm81x_yaps_hw_lock(yaps);
	if (ret) {
		dev_dbg(yaps->mors->dev, "yaps lock failed %d", ret);
		return ret;
	}

	*num_pkts_sent = 0;

	/* Check packet conditions */
	ret = mm81x_write_pkts_h_err_check(yaps, &pkts[0]);
	if (ret)
		goto exit;

	/* Batch packets into larger transactions */
	for (i = 0; i < num_pkts; ++i) {
		u32 pkt_size =
			pkts[i].skb->len + YAPS_CALC_PADDING(pkts[i].skb->len);
		tx_len = pkt_size + sizeof(delim);

		/*
		 * Send when we have reached window size, don't split pkt over
		 * boundary
		 */
		if ((batch_txn_len + tx_len) > YAPS_HW_WINDOW_SIZE_BYTES) {
			ret = mm81x_dm_write(yaps->mors,
					     yaps->aux_data->yds_addr,
					     to_chip_buffer_aligned,
					     batch_txn_len);

			batch_txn_len = 0;
			if (ret)
				goto exit;
			write_buf = to_chip_buffer_aligned;
			*num_pkts_sent += pkts_pending;
			pkts_pending = 0;
		}

		if ((i + 1) == num_pkts) {
			/* The last packet in the queue has IRQ set */
			delim_irq = true;
		} else {
			/*
			 * Since this is not the last packet, we can check for
			 * the next one. In case of errors in the next packet
			 * set the IRQ
			 */
			ret = mm81x_write_pkts_h_err_check(yaps, &pkts[i + 1]);
			if (ret)
				delim_irq = true;
		}

		/* Build stream header*/
		delim = mm81x_write_pkts_h_build_delim(
			yaps, pkt_size, pkts[i].tc_queue, delim_irq);
		*((__le32 *)write_buf) = cpu_to_le32(delim);
		memcpy(write_buf + sizeof(delim), pkts[i].skb->data,
		       pkts[i].skb->len);

		write_buf += tx_len;
		batch_txn_len += tx_len;
		pkts_pending++;

		if (ret)
			goto exit;
	}

exit:
	if (batch_txn_len > 0) {
		ret = mm81x_dm_write(yaps->mors, yaps->aux_data->yds_addr,
				     to_chip_buffer_aligned, batch_txn_len);
		*num_pkts_sent += pkts_pending;
	}

	mm81x_yaps_hw_unlock(yaps);
	return ret;
}

static bool mm81x_read_pkts_h_is_valid_delim(u32 delim)
{
	u8 calc_crc = mm81x_yaps_hw_crc(delim);
	int pkt_size = YAPS_DELIM_GET_PHANDLE_SIZE(delim);
	int padding = YAPS_DELIM_GET_PADDING(delim);

	if (calc_crc != YAPS_DELIM_GET_CRC(delim))
		return false;

	if (pkt_size == 0)
		return false;

	if ((pkt_size + padding) > YAPS_MAX_PKT_SIZE_BYTES)
		return false;

	/* Pkt length + padding should not require more padding */
	if (YAPS_CALC_PADDING(pkt_size) != padding)
		return false;

	return true;
}

static int mm81x_read_pkts_h_bytes_remaining(struct mm81x_yaps *yaps)
{
	u32 bytes_in_queue = yaps->aux_data->status_regs.fc_rx_bytes_in_queue;
	u32 delim_overhead =
		yaps->aux_data->status_regs.fc_num_pkts * sizeof(u32);
	u32 reserved_bytes = yaps->aux_data->status_regs.fc_num_pkts *
			     yaps->aux_data->reserved_yaps_page_size;

	if (WARN_ON(bytes_in_queue > INT_MAX) ||
	    WARN_ON(delim_overhead > INT_MAX) ||
	    WARN_ON(reserved_bytes > INT_MAX))
		return -EIO;

	return (int)bytes_in_queue;
}

static int mm81x_yaps_hw_read_pkts(struct mm81x_yaps *yaps,
				   struct mm81x_yaps_pkt *pkts,
				   int num_pkts_max, int *num_pkts_received)
{
	int ret;
	int i = 0;
	char *from_chip_buffer_aligned =
		PTR_ALIGN(yaps->aux_data->from_chip_buffer,
			  mm81x_bus_get_alignment(yaps->mors));
	char *read_ptr = from_chip_buffer_aligned;
	int bytes_remaining = mm81x_read_pkts_h_bytes_remaining(yaps);
	bool again = false;

	*num_pkts_received = 0;

	if (num_pkts_max == 0 || bytes_remaining == 0)
		return 0;
	if (bytes_remaining < 0)
		return bytes_remaining;

	if (bytes_remaining > YAPS_HW_WINDOW_SIZE_BYTES) {
		bytes_remaining = YAPS_HW_WINDOW_SIZE_BYTES;
		again = true;
	}

	/*
	 * This is more coarse-grained than it needs to be - once the data
	 * is read into a local buffer the lock can be released, however
	 * access to from_chip_buffer will need to be protected with its
	 * own lock
	 */
	ret = mm81x_yaps_hw_lock(yaps);
	if (ret) {
		dev_dbg(yaps->mors->dev, "yaps lock failed %d", ret);
		return ret;
	}

	/* Read all available packets to the buffer */
	ret = mm81x_dm_read(yaps->mors, yaps->aux_data->ysl_addr,
			    from_chip_buffer_aligned, bytes_remaining);

	if (ret)
		goto exit;

	/* Split serialised packets from buffer */
	while (i < num_pkts_max && bytes_remaining > 0) {
		u32 delim;
		int total_len;
		int pkt_size;

		delim = le32_to_cpu(*((__le32 *)read_ptr));
		read_ptr += sizeof(delim);
		bytes_remaining -= sizeof(delim);

		/* End of stream */
		if (!delim)
			break;

		if (!mm81x_read_pkts_h_is_valid_delim(delim)) {
			/*
			 * This will start a hunt for a valid delimiter. Given
			 * the CRC is only 7 bit it's possible to find an
			 * invalid block with a valid delimiter, leading to
			 * desynchronisation.
			 */
			dev_warn(yaps->mors->dev, "yaps invalid delim");
			break;
		}

		/* Total length in chip */
		pkt_size = YAPS_DELIM_GET_PKT_SIZE(delim);
		total_len = pkt_size + YAPS_DELIM_GET_PADDING(delim);

		if (pkts[i].skb)
			dev_err(yaps->mors->dev, "yaps packet leak");

		/* SKB doesn't want padding */
		pkts[i].skb = dev_alloc_skb(pkt_size);
		if (!pkts[i].skb) {
			ret = -ENOMEM;
			dev_err(yaps->mors->dev, "yaps no mem for skb");
			goto exit;
		}
		skb_put(pkts[i].skb, pkt_size);

		if (total_len <= bytes_remaining) {
			memcpy(pkts[i].skb->data, read_ptr, pkt_size);
			read_ptr += total_len;
			bytes_remaining -= total_len;
		} else {
			const int read_overhang_len =
				total_len - bytes_remaining;
			const int pkt_overhang_len = pkt_size - bytes_remaining;

			memcpy(pkts[i].skb->data, read_ptr, bytes_remaining);
			read_ptr = from_chip_buffer_aligned;

			ret = mm81x_dm_read(
				yaps->mors,
				/* Offset by 4 to avoid retry logic */
				yaps->aux_data->ysl_addr + 4, read_ptr,
				read_overhang_len);

			if (ret)
				goto exit;

			memcpy(pkts[i].skb->data + bytes_remaining, read_ptr,
			       pkt_overhang_len);
			read_ptr += read_overhang_len;
			bytes_remaining = 0;
		}

		*num_pkts_received += 1;
		i++;
	}

	if (again)
		ret = -EAGAIN;

exit:
	mm81x_yaps_hw_unlock(yaps);
	return ret;
}

static int mm81x_yaps_hw_update_status(struct mm81x_yaps *yaps)
{
	int ret;
	int tc_total_pkt_count;
	unsigned long reg_read_timeout;
	struct mm81x_yaps_status_regs *r = &yaps->aux_data->status_regs;
	struct mm81x_yaps_hw_status_regs *hw_r = &yaps->aux_data->hw_status_regs;

	ret = mm81x_yaps_hw_lock(yaps);
	if (ret) {
		dev_dbg(yaps->mors->dev, "yaps lock failed %d", ret);
		return ret;
	}

	reg_read_timeout = jiffies + msecs_to_jiffies(100);
	do {
		if (time_after(jiffies, reg_read_timeout)) {
			dev_err(yaps->mors->dev,
				"timed out reading status registers: %d", ret);
			ret = -ETIMEDOUT;
			break;
		}

		ret = mm81x_dm_read(yaps->mors,
				    yaps->aux_data->status_regs_addr,
				    (u8 *)hw_r, sizeof(*hw_r));
	} while (!ret && le32_to_cpu(hw_r->lock));

	if (ret) {
		if (ret != -ENODEV) {
			dev_err(yaps->mors->dev,
				"error reading yaps status registers: %d", ret);
		}
		goto exit_unlock;
	}

	r->tc_tx_pool_num_pages = le32_to_cpu(hw_r->tc_tx_pool_num_pages);
	r->tc_cmd_pool_num_pages = le32_to_cpu(hw_r->tc_cmd_pool_num_pages);
	r->tc_beacon_pool_num_pages = le32_to_cpu(hw_r->tc_beacon_pool_num_pages);
	r->tc_mgmt_pool_num_pages = le32_to_cpu(hw_r->tc_mgmt_pool_num_pages);
	r->fc_rx_pool_num_pages = le32_to_cpu(hw_r->fc_rx_pool_num_pages);
	r->fc_resp_pool_num_pages = le32_to_cpu(hw_r->fc_resp_pool_num_pages);
	r->fc_tx_sts_pool_num_pages = le32_to_cpu(hw_r->fc_tx_sts_pool_num_pages);
	r->fc_aux_pool_num_pages = le32_to_cpu(hw_r->fc_aux_pool_num_pages);
	r->tc_tx_num_pkts = le32_to_cpu(hw_r->tc_tx_num_pkts);
	r->tc_cmd_num_pkts = le32_to_cpu(hw_r->tc_cmd_num_pkts);
	r->tc_beacon_num_pkts = le32_to_cpu(hw_r->tc_beacon_num_pkts);
	r->tc_mgmt_num_pkts = le32_to_cpu(hw_r->tc_mgmt_num_pkts);
	r->fc_num_pkts = le32_to_cpu(hw_r->fc_num_pkts);
	r->fc_done_num_pkts = le32_to_cpu(hw_r->fc_done_num_pkts);
	r->fc_rx_bytes_in_queue = le32_to_cpu(hw_r->fc_rx_bytes_in_queue);
	r->tc_delim_crc_fail_detected = le32_to_cpu(hw_r->tc_delim_crc_fail_detected);
	r->lock = le32_to_cpu(hw_r->lock);
	r->fc_host_ysl_status = le32_to_cpu(hw_r->fc_host_ysl_status);

	tc_total_pkt_count = r->tc_tx_num_pkts + r->tc_cmd_num_pkts +
			     r->tc_beacon_num_pkts + r->tc_mgmt_num_pkts;

	if (r->tc_delim_crc_fail_detected) {
		/*
		 * Host and chip have become desynchronised. This can happen if
		 * the chip crashes during a YAPS transaction. We cannot
		 * recover from this.
		 */
		dev_err(yaps->mors->dev,
			"to-chip yaps delimiter CRC fail, pkt_count=%d",
			tc_total_pkt_count);
		ret = -EIO;
	}

	if (mm81x_read_pkts_h_bytes_remaining(yaps))
		set_bit(MM81X_HIF_EVT_RX_PEND, &yaps->mors->hif.event_flags);

exit_unlock:
	mm81x_yaps_hw_unlock(yaps);
	return ret;
}

static const struct mm81x_yaps_ops mm81x_yaps_hw_ops = {
	.write_pkts = mm81x_yaps_hw_write_pkts,
	.read_pkts = mm81x_yaps_hw_read_pkts,
	.update_status = mm81x_yaps_hw_update_status,
};

int mm81x_yaps_hw_init(struct mm81x *mors)
{
	int ret = 0;
	struct mm81x_yaps *yaps = NULL;
	int aux_data_len = sizeof(struct mm81x_yaps_hw_aux_data);
	int alignment = mm81x_bus_get_alignment(mors);

	yaps = &mors->hif.u.yaps;
	yaps->aux_data = kzalloc(aux_data_len, GFP_KERNEL);
	if (!yaps->aux_data) {
		ret = -ENOMEM;
		goto err_exit;
	}

	yaps->aux_data->to_chip_buffer =
		kzalloc(YAPS_HW_WINDOW_SIZE_BYTES + alignment - 1, GFP_KERNEL);
	if (!yaps->aux_data->to_chip_buffer) {
		ret = -ENOMEM;
		goto err_exit;
	}

	yaps->aux_data->from_chip_buffer =
		kzalloc(YAPS_HW_WINDOW_SIZE_BYTES + alignment - 1, GFP_KERNEL);
	if (!yaps->aux_data->from_chip_buffer) {
		ret = -ENOMEM;
		goto err_exit;
	}

	if (!IS_ALIGNED((uintptr_t)&yaps->aux_data->status_regs, alignment)) {
		dev_warn(mors->dev,
			 "Status registers are not aligned to %d bytes",
			 alignment);
	}

	yaps->ops = &mm81x_yaps_hw_ops;
	return ret;

err_exit:
	mm81x_yaps_hw_finish(mors);
	return ret;
}

void mm81x_yaps_hw_finish(struct mm81x *mors)
{
	struct mm81x_yaps *yaps;

	yaps = &mors->hif.u.yaps;
	if (yaps->aux_data) {
		kfree(yaps->aux_data->from_chip_buffer);
		yaps->aux_data->from_chip_buffer = NULL;
		kfree(yaps->aux_data->to_chip_buffer);
		yaps->aux_data->to_chip_buffer = NULL;
		kfree(yaps->aux_data);
		yaps->aux_data = NULL;
	}
}