1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/slab.h>
4 #include <linux/ctype.h>
5 #include <linux/kernel.h>
6 #include <linux/delay.h>
7 #include <linux/pci_ids.h>
8 #include "adf_accel_devices.h"
9 #include "adf_common_drv.h"
10 #include "icp_qat_uclo.h"
11 #include "icp_qat_hal.h"
12 #include "icp_qat_fw_loader_handle.h"
13
14 #define UWORD_CPYBUF_SIZE 1024
15 #define INVLD_UWORD 0xffffffffffull
16 #define PID_MINOR_REV 0xf
17 #define PID_MAJOR_REV (0xf << 4)
18
qat_uclo_init_ae_data(struct icp_qat_uclo_objhandle * obj_handle,unsigned int ae,unsigned int image_num)19 static int qat_uclo_init_ae_data(struct icp_qat_uclo_objhandle *obj_handle,
20 unsigned int ae, unsigned int image_num)
21 {
22 struct icp_qat_uclo_aedata *ae_data;
23 struct icp_qat_uclo_encapme *encap_image;
24 struct icp_qat_uclo_page *page = NULL;
25 struct icp_qat_uclo_aeslice *ae_slice = NULL;
26
27 ae_data = &obj_handle->ae_data[ae];
28 encap_image = &obj_handle->ae_uimage[image_num];
29 ae_slice = &ae_data->ae_slices[ae_data->slice_num];
30 ae_slice->encap_image = encap_image;
31
32 if (encap_image->img_ptr) {
33 ae_slice->ctx_mask_assigned =
34 encap_image->img_ptr->ctx_assigned;
35 ae_data->eff_ustore_size = obj_handle->ustore_phy_size;
36 } else {
37 ae_slice->ctx_mask_assigned = 0;
38 }
39 ae_slice->region = kzalloc(sizeof(*ae_slice->region), GFP_KERNEL);
40 if (!ae_slice->region)
41 return -ENOMEM;
42 ae_slice->page = kzalloc(sizeof(*ae_slice->page), GFP_KERNEL);
43 if (!ae_slice->page)
44 goto out_err;
45 page = ae_slice->page;
46 page->encap_page = encap_image->page;
47 ae_slice->page->region = ae_slice->region;
48 ae_data->slice_num++;
49 return 0;
50 out_err:
51 kfree(ae_slice->region);
52 ae_slice->region = NULL;
53 return -ENOMEM;
54 }
55
qat_uclo_free_ae_data(struct icp_qat_uclo_aedata * ae_data)56 static int qat_uclo_free_ae_data(struct icp_qat_uclo_aedata *ae_data)
57 {
58 unsigned int i;
59
60 if (!ae_data) {
61 pr_err("QAT: bad argument, ae_data is NULL\n ");
62 return -EINVAL;
63 }
64
65 for (i = 0; i < ae_data->slice_num; i++) {
66 kfree(ae_data->ae_slices[i].region);
67 ae_data->ae_slices[i].region = NULL;
68 kfree(ae_data->ae_slices[i].page);
69 ae_data->ae_slices[i].page = NULL;
70 }
71 return 0;
72 }
73
qat_uclo_get_string(struct icp_qat_uof_strtable * str_table,unsigned int str_offset)74 static char *qat_uclo_get_string(struct icp_qat_uof_strtable *str_table,
75 unsigned int str_offset)
76 {
77 if ((!str_table->table_len) || (str_offset > str_table->table_len))
78 return NULL;
79 return (char *)(((uintptr_t)(str_table->strings)) + str_offset);
80 }
81
qat_uclo_check_uof_format(struct icp_qat_uof_filehdr * hdr)82 static int qat_uclo_check_uof_format(struct icp_qat_uof_filehdr *hdr)
83 {
84 int maj = hdr->maj_ver & 0xff;
85 int min = hdr->min_ver & 0xff;
86
87 if (hdr->file_id != ICP_QAT_UOF_FID) {
88 pr_err("QAT: Invalid header 0x%x\n", hdr->file_id);
89 return -EINVAL;
90 }
91 if (min != ICP_QAT_UOF_MINVER || maj != ICP_QAT_UOF_MAJVER) {
92 pr_err("QAT: bad UOF version, major 0x%x, minor 0x%x\n",
93 maj, min);
94 return -EINVAL;
95 }
96 return 0;
97 }
98
qat_uclo_check_suof_format(struct icp_qat_suof_filehdr * suof_hdr)99 static int qat_uclo_check_suof_format(struct icp_qat_suof_filehdr *suof_hdr)
100 {
101 int maj = suof_hdr->maj_ver & 0xff;
102 int min = suof_hdr->min_ver & 0xff;
103
104 if (suof_hdr->file_id != ICP_QAT_SUOF_FID) {
105 pr_err("QAT: invalid header 0x%x\n", suof_hdr->file_id);
106 return -EINVAL;
107 }
108 if (suof_hdr->fw_type != 0) {
109 pr_err("QAT: unsupported firmware type\n");
110 return -EINVAL;
111 }
112 if (suof_hdr->num_chunks <= 0x1) {
113 pr_err("QAT: SUOF chunk amount is incorrect\n");
114 return -EINVAL;
115 }
116 if (maj != ICP_QAT_SUOF_MAJVER || min != ICP_QAT_SUOF_MINVER) {
117 pr_err("QAT: bad SUOF version, major 0x%x, minor 0x%x\n",
118 maj, min);
119 return -EINVAL;
120 }
121 return 0;
122 }
123
qat_uclo_wr_sram_by_words(struct icp_qat_fw_loader_handle * handle,unsigned int addr,unsigned int * val,unsigned int num_in_bytes)124 static void qat_uclo_wr_sram_by_words(struct icp_qat_fw_loader_handle *handle,
125 unsigned int addr, unsigned int *val,
126 unsigned int num_in_bytes)
127 {
128 unsigned int outval;
129 unsigned char *ptr = (unsigned char *)val;
130
131 while (num_in_bytes) {
132 memcpy(&outval, ptr, 4);
133 SRAM_WRITE(handle, addr, outval);
134 num_in_bytes -= 4;
135 ptr += 4;
136 addr += 4;
137 }
138 }
139
qat_uclo_wr_umem_by_words(struct icp_qat_fw_loader_handle * handle,unsigned char ae,unsigned int addr,unsigned int * val,unsigned int num_in_bytes)140 static void qat_uclo_wr_umem_by_words(struct icp_qat_fw_loader_handle *handle,
141 unsigned char ae, unsigned int addr,
142 unsigned int *val,
143 unsigned int num_in_bytes)
144 {
145 unsigned int outval;
146 unsigned char *ptr = (unsigned char *)val;
147
148 addr >>= 0x2; /* convert to uword address */
149
150 while (num_in_bytes) {
151 memcpy(&outval, ptr, 4);
152 qat_hal_wr_umem(handle, ae, addr++, 1, &outval);
153 num_in_bytes -= 4;
154 ptr += 4;
155 }
156 }
157
qat_uclo_batch_wr_umem(struct icp_qat_fw_loader_handle * handle,unsigned char ae,struct icp_qat_uof_batch_init * umem_init_header)158 static void qat_uclo_batch_wr_umem(struct icp_qat_fw_loader_handle *handle,
159 unsigned char ae,
160 struct icp_qat_uof_batch_init
161 *umem_init_header)
162 {
163 struct icp_qat_uof_batch_init *umem_init;
164
165 if (!umem_init_header)
166 return;
167 umem_init = umem_init_header->next;
168 while (umem_init) {
169 unsigned int addr, *value, size;
170
171 ae = umem_init->ae;
172 addr = umem_init->addr;
173 value = umem_init->value;
174 size = umem_init->size;
175 qat_uclo_wr_umem_by_words(handle, ae, addr, value, size);
176 umem_init = umem_init->next;
177 }
178 }
179
180 static void
qat_uclo_cleanup_batch_init_list(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_batch_init ** base)181 qat_uclo_cleanup_batch_init_list(struct icp_qat_fw_loader_handle *handle,
182 struct icp_qat_uof_batch_init **base)
183 {
184 struct icp_qat_uof_batch_init *umem_init;
185
186 umem_init = *base;
187 while (umem_init) {
188 struct icp_qat_uof_batch_init *pre;
189
190 pre = umem_init;
191 umem_init = umem_init->next;
192 kfree(pre);
193 }
194 *base = NULL;
195 }
196
qat_uclo_parse_num(char * str,unsigned int * num)197 static int qat_uclo_parse_num(char *str, unsigned int *num)
198 {
199 char buf[16] = {0};
200 unsigned long ae = 0;
201 int i;
202
203 strncpy(buf, str, 15);
204 for (i = 0; i < 16; i++) {
205 if (!isdigit(buf[i])) {
206 buf[i] = '\0';
207 break;
208 }
209 }
210 if ((kstrtoul(buf, 10, &ae)))
211 return -EFAULT;
212
213 *num = (unsigned int)ae;
214 return 0;
215 }
216
qat_uclo_fetch_initmem_ae(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_initmem * init_mem,unsigned int size_range,unsigned int * ae)217 static int qat_uclo_fetch_initmem_ae(struct icp_qat_fw_loader_handle *handle,
218 struct icp_qat_uof_initmem *init_mem,
219 unsigned int size_range, unsigned int *ae)
220 {
221 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
222 char *str;
223
224 if ((init_mem->addr + init_mem->num_in_bytes) > (size_range << 0x2)) {
225 pr_err("QAT: initmem is out of range");
226 return -EINVAL;
227 }
228 if (init_mem->scope != ICP_QAT_UOF_LOCAL_SCOPE) {
229 pr_err("QAT: Memory scope for init_mem error\n");
230 return -EINVAL;
231 }
232 str = qat_uclo_get_string(&obj_handle->str_table, init_mem->sym_name);
233 if (!str) {
234 pr_err("QAT: AE name assigned in UOF init table is NULL\n");
235 return -EINVAL;
236 }
237 if (qat_uclo_parse_num(str, ae)) {
238 pr_err("QAT: Parse num for AE number failed\n");
239 return -EINVAL;
240 }
241 if (*ae >= ICP_QAT_UCLO_MAX_AE) {
242 pr_err("QAT: ae %d out of range\n", *ae);
243 return -EINVAL;
244 }
245 return 0;
246 }
247
qat_uclo_create_batch_init_list(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_initmem * init_mem,unsigned int ae,struct icp_qat_uof_batch_init ** init_tab_base)248 static int qat_uclo_create_batch_init_list(struct icp_qat_fw_loader_handle
249 *handle, struct icp_qat_uof_initmem
250 *init_mem, unsigned int ae,
251 struct icp_qat_uof_batch_init
252 **init_tab_base)
253 {
254 struct icp_qat_uof_batch_init *init_header, *tail;
255 struct icp_qat_uof_batch_init *mem_init, *tail_old;
256 struct icp_qat_uof_memvar_attr *mem_val_attr;
257 unsigned int i, flag = 0;
258
259 mem_val_attr =
260 (struct icp_qat_uof_memvar_attr *)((uintptr_t)init_mem +
261 sizeof(struct icp_qat_uof_initmem));
262
263 init_header = *init_tab_base;
264 if (!init_header) {
265 init_header = kzalloc(sizeof(*init_header), GFP_KERNEL);
266 if (!init_header)
267 return -ENOMEM;
268 init_header->size = 1;
269 *init_tab_base = init_header;
270 flag = 1;
271 }
272 tail_old = init_header;
273 while (tail_old->next)
274 tail_old = tail_old->next;
275 tail = tail_old;
276 for (i = 0; i < init_mem->val_attr_num; i++) {
277 mem_init = kzalloc(sizeof(*mem_init), GFP_KERNEL);
278 if (!mem_init)
279 goto out_err;
280 mem_init->ae = ae;
281 mem_init->addr = init_mem->addr + mem_val_attr->offset_in_byte;
282 mem_init->value = &mem_val_attr->value;
283 mem_init->size = 4;
284 mem_init->next = NULL;
285 tail->next = mem_init;
286 tail = mem_init;
287 init_header->size += qat_hal_get_ins_num();
288 mem_val_attr++;
289 }
290 return 0;
291 out_err:
292 /* Do not free the list head unless we allocated it. */
293 tail_old = tail_old->next;
294 if (flag) {
295 kfree(*init_tab_base);
296 *init_tab_base = NULL;
297 }
298
299 while (tail_old) {
300 mem_init = tail_old->next;
301 kfree(tail_old);
302 tail_old = mem_init;
303 }
304 return -ENOMEM;
305 }
306
qat_uclo_init_lmem_seg(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_initmem * init_mem)307 static int qat_uclo_init_lmem_seg(struct icp_qat_fw_loader_handle *handle,
308 struct icp_qat_uof_initmem *init_mem)
309 {
310 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
311 unsigned int ae;
312
313 if (qat_uclo_fetch_initmem_ae(handle, init_mem,
314 ICP_QAT_UCLO_MAX_LMEM_REG, &ae))
315 return -EINVAL;
316 if (qat_uclo_create_batch_init_list(handle, init_mem, ae,
317 &obj_handle->lm_init_tab[ae]))
318 return -EINVAL;
319 return 0;
320 }
321
qat_uclo_init_umem_seg(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_initmem * init_mem)322 static int qat_uclo_init_umem_seg(struct icp_qat_fw_loader_handle *handle,
323 struct icp_qat_uof_initmem *init_mem)
324 {
325 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
326 unsigned int ae, ustore_size, uaddr, i;
327
328 ustore_size = obj_handle->ustore_phy_size;
329 if (qat_uclo_fetch_initmem_ae(handle, init_mem, ustore_size, &ae))
330 return -EINVAL;
331 if (qat_uclo_create_batch_init_list(handle, init_mem, ae,
332 &obj_handle->umem_init_tab[ae]))
333 return -EINVAL;
334 /* set the highest ustore address referenced */
335 uaddr = (init_mem->addr + init_mem->num_in_bytes) >> 0x2;
336 for (i = 0; i < obj_handle->ae_data[ae].slice_num; i++) {
337 if (obj_handle->ae_data[ae].ae_slices[i].
338 encap_image->uwords_num < uaddr)
339 obj_handle->ae_data[ae].ae_slices[i].
340 encap_image->uwords_num = uaddr;
341 }
342 return 0;
343 }
344
345 #define ICP_DH895XCC_PESRAM_BAR_SIZE 0x80000
qat_uclo_init_ae_memory(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_initmem * init_mem)346 static int qat_uclo_init_ae_memory(struct icp_qat_fw_loader_handle *handle,
347 struct icp_qat_uof_initmem *init_mem)
348 {
349 switch (init_mem->region) {
350 case ICP_QAT_UOF_LMEM_REGION:
351 if (qat_uclo_init_lmem_seg(handle, init_mem))
352 return -EINVAL;
353 break;
354 case ICP_QAT_UOF_UMEM_REGION:
355 if (qat_uclo_init_umem_seg(handle, init_mem))
356 return -EINVAL;
357 break;
358 default:
359 pr_err("QAT: initmem region error. region type=0x%x\n",
360 init_mem->region);
361 return -EINVAL;
362 }
363 return 0;
364 }
365
qat_uclo_init_ustore(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uclo_encapme * image)366 static int qat_uclo_init_ustore(struct icp_qat_fw_loader_handle *handle,
367 struct icp_qat_uclo_encapme *image)
368 {
369 unsigned int i;
370 struct icp_qat_uclo_encap_page *page;
371 struct icp_qat_uof_image *uof_image;
372 unsigned char ae;
373 unsigned int ustore_size;
374 unsigned int patt_pos;
375 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
376 u64 *fill_data;
377
378 uof_image = image->img_ptr;
379 fill_data = kcalloc(ICP_QAT_UCLO_MAX_USTORE, sizeof(u64),
380 GFP_KERNEL);
381 if (!fill_data)
382 return -ENOMEM;
383 for (i = 0; i < ICP_QAT_UCLO_MAX_USTORE; i++)
384 memcpy(&fill_data[i], &uof_image->fill_pattern,
385 sizeof(u64));
386 page = image->page;
387
388 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
389 if (!test_bit(ae, (unsigned long *)&uof_image->ae_assigned))
390 continue;
391 ustore_size = obj_handle->ae_data[ae].eff_ustore_size;
392 patt_pos = page->beg_addr_p + page->micro_words_num;
393
394 qat_hal_wr_uwords(handle, (unsigned char)ae, 0,
395 page->beg_addr_p, &fill_data[0]);
396 qat_hal_wr_uwords(handle, (unsigned char)ae, patt_pos,
397 ustore_size - patt_pos + 1,
398 &fill_data[page->beg_addr_p]);
399 }
400 kfree(fill_data);
401 return 0;
402 }
403
qat_uclo_init_memory(struct icp_qat_fw_loader_handle * handle)404 static int qat_uclo_init_memory(struct icp_qat_fw_loader_handle *handle)
405 {
406 int i, ae;
407 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
408 struct icp_qat_uof_initmem *initmem = obj_handle->init_mem_tab.init_mem;
409
410 for (i = 0; i < obj_handle->init_mem_tab.entry_num; i++) {
411 if (initmem->num_in_bytes) {
412 if (qat_uclo_init_ae_memory(handle, initmem))
413 return -EINVAL;
414 }
415 initmem = (struct icp_qat_uof_initmem *)((uintptr_t)(
416 (uintptr_t)initmem +
417 sizeof(struct icp_qat_uof_initmem)) +
418 (sizeof(struct icp_qat_uof_memvar_attr) *
419 initmem->val_attr_num));
420 }
421 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
422 if (qat_hal_batch_wr_lm(handle, ae,
423 obj_handle->lm_init_tab[ae])) {
424 pr_err("QAT: fail to batch init lmem for AE %d\n", ae);
425 return -EINVAL;
426 }
427 qat_uclo_cleanup_batch_init_list(handle,
428 &obj_handle->lm_init_tab[ae]);
429 qat_uclo_batch_wr_umem(handle, ae,
430 obj_handle->umem_init_tab[ae]);
431 qat_uclo_cleanup_batch_init_list(handle,
432 &obj_handle->
433 umem_init_tab[ae]);
434 }
435 return 0;
436 }
437
qat_uclo_find_chunk(struct icp_qat_uof_objhdr * obj_hdr,char * chunk_id,void * cur)438 static void *qat_uclo_find_chunk(struct icp_qat_uof_objhdr *obj_hdr,
439 char *chunk_id, void *cur)
440 {
441 int i;
442 struct icp_qat_uof_chunkhdr *chunk_hdr =
443 (struct icp_qat_uof_chunkhdr *)
444 ((uintptr_t)obj_hdr + sizeof(struct icp_qat_uof_objhdr));
445
446 for (i = 0; i < obj_hdr->num_chunks; i++) {
447 if ((cur < (void *)&chunk_hdr[i]) &&
448 !strncmp(chunk_hdr[i].chunk_id, chunk_id,
449 ICP_QAT_UOF_OBJID_LEN)) {
450 return &chunk_hdr[i];
451 }
452 }
453 return NULL;
454 }
455
qat_uclo_calc_checksum(unsigned int reg,int ch)456 static unsigned int qat_uclo_calc_checksum(unsigned int reg, int ch)
457 {
458 int i;
459 unsigned int topbit = 1 << 0xF;
460 unsigned int inbyte = (unsigned int)((reg >> 0x18) ^ ch);
461
462 reg ^= inbyte << 0x8;
463 for (i = 0; i < 0x8; i++) {
464 if (reg & topbit)
465 reg = (reg << 1) ^ 0x1021;
466 else
467 reg <<= 1;
468 }
469 return reg & 0xFFFF;
470 }
471
qat_uclo_calc_str_checksum(char * ptr,int num)472 static unsigned int qat_uclo_calc_str_checksum(char *ptr, int num)
473 {
474 unsigned int chksum = 0;
475
476 if (ptr)
477 while (num--)
478 chksum = qat_uclo_calc_checksum(chksum, *ptr++);
479 return chksum;
480 }
481
482 static struct icp_qat_uclo_objhdr *
qat_uclo_map_chunk(char * buf,struct icp_qat_uof_filehdr * file_hdr,char * chunk_id)483 qat_uclo_map_chunk(char *buf, struct icp_qat_uof_filehdr *file_hdr,
484 char *chunk_id)
485 {
486 struct icp_qat_uof_filechunkhdr *file_chunk;
487 struct icp_qat_uclo_objhdr *obj_hdr;
488 char *chunk;
489 int i;
490
491 file_chunk = (struct icp_qat_uof_filechunkhdr *)
492 (buf + sizeof(struct icp_qat_uof_filehdr));
493 for (i = 0; i < file_hdr->num_chunks; i++) {
494 if (!strncmp(file_chunk->chunk_id, chunk_id,
495 ICP_QAT_UOF_OBJID_LEN)) {
496 chunk = buf + file_chunk->offset;
497 if (file_chunk->checksum != qat_uclo_calc_str_checksum(
498 chunk, file_chunk->size))
499 break;
500 obj_hdr = kzalloc(sizeof(*obj_hdr), GFP_KERNEL);
501 if (!obj_hdr)
502 break;
503 obj_hdr->file_buff = chunk;
504 obj_hdr->checksum = file_chunk->checksum;
505 obj_hdr->size = file_chunk->size;
506 return obj_hdr;
507 }
508 file_chunk++;
509 }
510 return NULL;
511 }
512
513 static unsigned int
qat_uclo_check_image_compat(struct icp_qat_uof_encap_obj * encap_uof_obj,struct icp_qat_uof_image * image)514 qat_uclo_check_image_compat(struct icp_qat_uof_encap_obj *encap_uof_obj,
515 struct icp_qat_uof_image *image)
516 {
517 struct icp_qat_uof_objtable *uc_var_tab, *imp_var_tab, *imp_expr_tab;
518 struct icp_qat_uof_objtable *neigh_reg_tab;
519 struct icp_qat_uof_code_page *code_page;
520
521 code_page = (struct icp_qat_uof_code_page *)
522 ((char *)image + sizeof(struct icp_qat_uof_image));
523 uc_var_tab = (struct icp_qat_uof_objtable *)(encap_uof_obj->beg_uof +
524 code_page->uc_var_tab_offset);
525 imp_var_tab = (struct icp_qat_uof_objtable *)(encap_uof_obj->beg_uof +
526 code_page->imp_var_tab_offset);
527 imp_expr_tab = (struct icp_qat_uof_objtable *)
528 (encap_uof_obj->beg_uof +
529 code_page->imp_expr_tab_offset);
530 if (uc_var_tab->entry_num || imp_var_tab->entry_num ||
531 imp_expr_tab->entry_num) {
532 pr_err("QAT: UOF can't contain imported variable to be parsed\n");
533 return -EINVAL;
534 }
535 neigh_reg_tab = (struct icp_qat_uof_objtable *)
536 (encap_uof_obj->beg_uof +
537 code_page->neigh_reg_tab_offset);
538 if (neigh_reg_tab->entry_num) {
539 pr_err("QAT: UOF can't contain shared control store feature\n");
540 return -EINVAL;
541 }
542 if (image->numpages > 1) {
543 pr_err("QAT: UOF can't contain multiple pages\n");
544 return -EINVAL;
545 }
546 if (ICP_QAT_SHARED_USTORE_MODE(image->ae_mode)) {
547 pr_err("QAT: UOF can't use shared control store feature\n");
548 return -EFAULT;
549 }
550 if (RELOADABLE_CTX_SHARED_MODE(image->ae_mode)) {
551 pr_err("QAT: UOF can't use reloadable feature\n");
552 return -EFAULT;
553 }
554 return 0;
555 }
556
qat_uclo_map_image_page(struct icp_qat_uof_encap_obj * encap_uof_obj,struct icp_qat_uof_image * img,struct icp_qat_uclo_encap_page * page)557 static void qat_uclo_map_image_page(struct icp_qat_uof_encap_obj
558 *encap_uof_obj,
559 struct icp_qat_uof_image *img,
560 struct icp_qat_uclo_encap_page *page)
561 {
562 struct icp_qat_uof_code_page *code_page;
563 struct icp_qat_uof_code_area *code_area;
564 struct icp_qat_uof_objtable *uword_block_tab;
565 struct icp_qat_uof_uword_block *uwblock;
566 int i;
567
568 code_page = (struct icp_qat_uof_code_page *)
569 ((char *)img + sizeof(struct icp_qat_uof_image));
570 page->def_page = code_page->def_page;
571 page->page_region = code_page->page_region;
572 page->beg_addr_v = code_page->beg_addr_v;
573 page->beg_addr_p = code_page->beg_addr_p;
574 code_area = (struct icp_qat_uof_code_area *)(encap_uof_obj->beg_uof +
575 code_page->code_area_offset);
576 page->micro_words_num = code_area->micro_words_num;
577 uword_block_tab = (struct icp_qat_uof_objtable *)
578 (encap_uof_obj->beg_uof +
579 code_area->uword_block_tab);
580 page->uwblock_num = uword_block_tab->entry_num;
581 uwblock = (struct icp_qat_uof_uword_block *)((char *)uword_block_tab +
582 sizeof(struct icp_qat_uof_objtable));
583 page->uwblock = (struct icp_qat_uclo_encap_uwblock *)uwblock;
584 for (i = 0; i < uword_block_tab->entry_num; i++)
585 page->uwblock[i].micro_words =
586 (uintptr_t)encap_uof_obj->beg_uof + uwblock[i].uword_offset;
587 }
588
qat_uclo_map_uimage(struct icp_qat_uclo_objhandle * obj_handle,struct icp_qat_uclo_encapme * ae_uimage,int max_image)589 static int qat_uclo_map_uimage(struct icp_qat_uclo_objhandle *obj_handle,
590 struct icp_qat_uclo_encapme *ae_uimage,
591 int max_image)
592 {
593 int i, j;
594 struct icp_qat_uof_chunkhdr *chunk_hdr = NULL;
595 struct icp_qat_uof_image *image;
596 struct icp_qat_uof_objtable *ae_regtab;
597 struct icp_qat_uof_objtable *init_reg_sym_tab;
598 struct icp_qat_uof_objtable *sbreak_tab;
599 struct icp_qat_uof_encap_obj *encap_uof_obj =
600 &obj_handle->encap_uof_obj;
601
602 for (j = 0; j < max_image; j++) {
603 chunk_hdr = qat_uclo_find_chunk(encap_uof_obj->obj_hdr,
604 ICP_QAT_UOF_IMAG, chunk_hdr);
605 if (!chunk_hdr)
606 break;
607 image = (struct icp_qat_uof_image *)(encap_uof_obj->beg_uof +
608 chunk_hdr->offset);
609 ae_regtab = (struct icp_qat_uof_objtable *)
610 (image->reg_tab_offset +
611 obj_handle->obj_hdr->file_buff);
612 ae_uimage[j].ae_reg_num = ae_regtab->entry_num;
613 ae_uimage[j].ae_reg = (struct icp_qat_uof_ae_reg *)
614 (((char *)ae_regtab) +
615 sizeof(struct icp_qat_uof_objtable));
616 init_reg_sym_tab = (struct icp_qat_uof_objtable *)
617 (image->init_reg_sym_tab +
618 obj_handle->obj_hdr->file_buff);
619 ae_uimage[j].init_regsym_num = init_reg_sym_tab->entry_num;
620 ae_uimage[j].init_regsym = (struct icp_qat_uof_init_regsym *)
621 (((char *)init_reg_sym_tab) +
622 sizeof(struct icp_qat_uof_objtable));
623 sbreak_tab = (struct icp_qat_uof_objtable *)
624 (image->sbreak_tab + obj_handle->obj_hdr->file_buff);
625 ae_uimage[j].sbreak_num = sbreak_tab->entry_num;
626 ae_uimage[j].sbreak = (struct icp_qat_uof_sbreak *)
627 (((char *)sbreak_tab) +
628 sizeof(struct icp_qat_uof_objtable));
629 ae_uimage[j].img_ptr = image;
630 if (qat_uclo_check_image_compat(encap_uof_obj, image))
631 goto out_err;
632 ae_uimage[j].page =
633 kzalloc(sizeof(struct icp_qat_uclo_encap_page),
634 GFP_KERNEL);
635 if (!ae_uimage[j].page)
636 goto out_err;
637 qat_uclo_map_image_page(encap_uof_obj, image,
638 ae_uimage[j].page);
639 }
640 return j;
641 out_err:
642 for (i = 0; i < j; i++)
643 kfree(ae_uimage[i].page);
644 return 0;
645 }
646
qat_uclo_map_ae(struct icp_qat_fw_loader_handle * handle,int max_ae)647 static int qat_uclo_map_ae(struct icp_qat_fw_loader_handle *handle, int max_ae)
648 {
649 int i, ae;
650 int mflag = 0;
651 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
652
653 for (ae = 0; ae < max_ae; ae++) {
654 if (!test_bit(ae,
655 (unsigned long *)&handle->hal_handle->ae_mask))
656 continue;
657 for (i = 0; i < obj_handle->uimage_num; i++) {
658 if (!test_bit(ae, (unsigned long *)
659 &obj_handle->ae_uimage[i].img_ptr->ae_assigned))
660 continue;
661 mflag = 1;
662 if (qat_uclo_init_ae_data(obj_handle, ae, i))
663 return -EINVAL;
664 }
665 }
666 if (!mflag) {
667 pr_err("QAT: uimage uses AE not set\n");
668 return -EINVAL;
669 }
670 return 0;
671 }
672
673 static struct icp_qat_uof_strtable *
qat_uclo_map_str_table(struct icp_qat_uclo_objhdr * obj_hdr,char * tab_name,struct icp_qat_uof_strtable * str_table)674 qat_uclo_map_str_table(struct icp_qat_uclo_objhdr *obj_hdr,
675 char *tab_name, struct icp_qat_uof_strtable *str_table)
676 {
677 struct icp_qat_uof_chunkhdr *chunk_hdr;
678
679 chunk_hdr = qat_uclo_find_chunk((struct icp_qat_uof_objhdr *)
680 obj_hdr->file_buff, tab_name, NULL);
681 if (chunk_hdr) {
682 int hdr_size;
683
684 memcpy(&str_table->table_len, obj_hdr->file_buff +
685 chunk_hdr->offset, sizeof(str_table->table_len));
686 hdr_size = (char *)&str_table->strings - (char *)str_table;
687 str_table->strings = (uintptr_t)obj_hdr->file_buff +
688 chunk_hdr->offset + hdr_size;
689 return str_table;
690 }
691 return NULL;
692 }
693
694 static void
qat_uclo_map_initmem_table(struct icp_qat_uof_encap_obj * encap_uof_obj,struct icp_qat_uclo_init_mem_table * init_mem_tab)695 qat_uclo_map_initmem_table(struct icp_qat_uof_encap_obj *encap_uof_obj,
696 struct icp_qat_uclo_init_mem_table *init_mem_tab)
697 {
698 struct icp_qat_uof_chunkhdr *chunk_hdr;
699
700 chunk_hdr = qat_uclo_find_chunk(encap_uof_obj->obj_hdr,
701 ICP_QAT_UOF_IMEM, NULL);
702 if (chunk_hdr) {
703 memmove(&init_mem_tab->entry_num, encap_uof_obj->beg_uof +
704 chunk_hdr->offset, sizeof(unsigned int));
705 init_mem_tab->init_mem = (struct icp_qat_uof_initmem *)
706 (encap_uof_obj->beg_uof + chunk_hdr->offset +
707 sizeof(unsigned int));
708 }
709 }
710
711 static unsigned int
qat_uclo_get_dev_type(struct icp_qat_fw_loader_handle * handle)712 qat_uclo_get_dev_type(struct icp_qat_fw_loader_handle *handle)
713 {
714 switch (handle->pci_dev->device) {
715 case PCI_DEVICE_ID_INTEL_QAT_DH895XCC:
716 return ICP_QAT_AC_895XCC_DEV_TYPE;
717 case PCI_DEVICE_ID_INTEL_QAT_C62X:
718 return ICP_QAT_AC_C62X_DEV_TYPE;
719 case PCI_DEVICE_ID_INTEL_QAT_C3XXX:
720 return ICP_QAT_AC_C3XXX_DEV_TYPE;
721 default:
722 pr_err("QAT: unsupported device 0x%x\n",
723 handle->pci_dev->device);
724 return 0;
725 }
726 }
727
qat_uclo_check_uof_compat(struct icp_qat_uclo_objhandle * obj_handle)728 static int qat_uclo_check_uof_compat(struct icp_qat_uclo_objhandle *obj_handle)
729 {
730 unsigned int maj_ver, prod_type = obj_handle->prod_type;
731
732 if (!(prod_type & obj_handle->encap_uof_obj.obj_hdr->ac_dev_type)) {
733 pr_err("QAT: UOF type 0x%x doesn't match with platform 0x%x\n",
734 obj_handle->encap_uof_obj.obj_hdr->ac_dev_type,
735 prod_type);
736 return -EINVAL;
737 }
738 maj_ver = obj_handle->prod_rev & 0xff;
739 if ((obj_handle->encap_uof_obj.obj_hdr->max_cpu_ver < maj_ver) ||
740 (obj_handle->encap_uof_obj.obj_hdr->min_cpu_ver > maj_ver)) {
741 pr_err("QAT: UOF majVer 0x%x out of range\n", maj_ver);
742 return -EINVAL;
743 }
744 return 0;
745 }
746
qat_uclo_init_reg(struct icp_qat_fw_loader_handle * handle,unsigned char ae,unsigned char ctx_mask,enum icp_qat_uof_regtype reg_type,unsigned short reg_addr,unsigned int value)747 static int qat_uclo_init_reg(struct icp_qat_fw_loader_handle *handle,
748 unsigned char ae, unsigned char ctx_mask,
749 enum icp_qat_uof_regtype reg_type,
750 unsigned short reg_addr, unsigned int value)
751 {
752 switch (reg_type) {
753 case ICP_GPA_ABS:
754 case ICP_GPB_ABS:
755 ctx_mask = 0;
756 fallthrough;
757 case ICP_GPA_REL:
758 case ICP_GPB_REL:
759 return qat_hal_init_gpr(handle, ae, ctx_mask, reg_type,
760 reg_addr, value);
761 case ICP_SR_ABS:
762 case ICP_DR_ABS:
763 case ICP_SR_RD_ABS:
764 case ICP_DR_RD_ABS:
765 ctx_mask = 0;
766 fallthrough;
767 case ICP_SR_REL:
768 case ICP_DR_REL:
769 case ICP_SR_RD_REL:
770 case ICP_DR_RD_REL:
771 return qat_hal_init_rd_xfer(handle, ae, ctx_mask, reg_type,
772 reg_addr, value);
773 case ICP_SR_WR_ABS:
774 case ICP_DR_WR_ABS:
775 ctx_mask = 0;
776 fallthrough;
777 case ICP_SR_WR_REL:
778 case ICP_DR_WR_REL:
779 return qat_hal_init_wr_xfer(handle, ae, ctx_mask, reg_type,
780 reg_addr, value);
781 case ICP_NEIGH_REL:
782 return qat_hal_init_nn(handle, ae, ctx_mask, reg_addr, value);
783 default:
784 pr_err("QAT: UOF uses not supported reg type 0x%x\n", reg_type);
785 return -EFAULT;
786 }
787 return 0;
788 }
789
qat_uclo_init_reg_sym(struct icp_qat_fw_loader_handle * handle,unsigned int ae,struct icp_qat_uclo_encapme * encap_ae)790 static int qat_uclo_init_reg_sym(struct icp_qat_fw_loader_handle *handle,
791 unsigned int ae,
792 struct icp_qat_uclo_encapme *encap_ae)
793 {
794 unsigned int i;
795 unsigned char ctx_mask;
796 struct icp_qat_uof_init_regsym *init_regsym;
797
798 if (ICP_QAT_CTX_MODE(encap_ae->img_ptr->ae_mode) ==
799 ICP_QAT_UCLO_MAX_CTX)
800 ctx_mask = 0xff;
801 else
802 ctx_mask = 0x55;
803
804 for (i = 0; i < encap_ae->init_regsym_num; i++) {
805 unsigned int exp_res;
806
807 init_regsym = &encap_ae->init_regsym[i];
808 exp_res = init_regsym->value;
809 switch (init_regsym->init_type) {
810 case ICP_QAT_UOF_INIT_REG:
811 qat_uclo_init_reg(handle, ae, ctx_mask,
812 (enum icp_qat_uof_regtype)
813 init_regsym->reg_type,
814 (unsigned short)init_regsym->reg_addr,
815 exp_res);
816 break;
817 case ICP_QAT_UOF_INIT_REG_CTX:
818 /* check if ctx is appropriate for the ctxMode */
819 if (!((1 << init_regsym->ctx) & ctx_mask)) {
820 pr_err("QAT: invalid ctx num = 0x%x\n",
821 init_regsym->ctx);
822 return -EINVAL;
823 }
824 qat_uclo_init_reg(handle, ae,
825 (unsigned char)
826 (1 << init_regsym->ctx),
827 (enum icp_qat_uof_regtype)
828 init_regsym->reg_type,
829 (unsigned short)init_regsym->reg_addr,
830 exp_res);
831 break;
832 case ICP_QAT_UOF_INIT_EXPR:
833 pr_err("QAT: INIT_EXPR feature not supported\n");
834 return -EINVAL;
835 case ICP_QAT_UOF_INIT_EXPR_ENDIAN_SWAP:
836 pr_err("QAT: INIT_EXPR_ENDIAN_SWAP feature not supported\n");
837 return -EINVAL;
838 default:
839 break;
840 }
841 }
842 return 0;
843 }
844
qat_uclo_init_globals(struct icp_qat_fw_loader_handle * handle)845 static int qat_uclo_init_globals(struct icp_qat_fw_loader_handle *handle)
846 {
847 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
848 unsigned int s, ae;
849
850 if (obj_handle->global_inited)
851 return 0;
852 if (obj_handle->init_mem_tab.entry_num) {
853 if (qat_uclo_init_memory(handle)) {
854 pr_err("QAT: initialize memory failed\n");
855 return -EINVAL;
856 }
857 }
858 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
859 for (s = 0; s < obj_handle->ae_data[ae].slice_num; s++) {
860 if (!obj_handle->ae_data[ae].ae_slices[s].encap_image)
861 continue;
862 if (qat_uclo_init_reg_sym(handle, ae,
863 obj_handle->ae_data[ae].
864 ae_slices[s].encap_image))
865 return -EINVAL;
866 }
867 }
868 obj_handle->global_inited = 1;
869 return 0;
870 }
871
qat_uclo_set_ae_mode(struct icp_qat_fw_loader_handle * handle)872 static int qat_uclo_set_ae_mode(struct icp_qat_fw_loader_handle *handle)
873 {
874 unsigned char ae, nn_mode, s;
875 struct icp_qat_uof_image *uof_image;
876 struct icp_qat_uclo_aedata *ae_data;
877 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
878
879 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
880 if (!test_bit(ae,
881 (unsigned long *)&handle->hal_handle->ae_mask))
882 continue;
883 ae_data = &obj_handle->ae_data[ae];
884 for (s = 0; s < min_t(unsigned int, ae_data->slice_num,
885 ICP_QAT_UCLO_MAX_CTX); s++) {
886 if (!obj_handle->ae_data[ae].ae_slices[s].encap_image)
887 continue;
888 uof_image = ae_data->ae_slices[s].encap_image->img_ptr;
889 if (qat_hal_set_ae_ctx_mode(handle, ae,
890 (char)ICP_QAT_CTX_MODE
891 (uof_image->ae_mode))) {
892 pr_err("QAT: qat_hal_set_ae_ctx_mode error\n");
893 return -EFAULT;
894 }
895 nn_mode = ICP_QAT_NN_MODE(uof_image->ae_mode);
896 if (qat_hal_set_ae_nn_mode(handle, ae, nn_mode)) {
897 pr_err("QAT: qat_hal_set_ae_nn_mode error\n");
898 return -EFAULT;
899 }
900 if (qat_hal_set_ae_lm_mode(handle, ae, ICP_LMEM0,
901 (char)ICP_QAT_LOC_MEM0_MODE
902 (uof_image->ae_mode))) {
903 pr_err("QAT: qat_hal_set_ae_lm_mode LMEM0 error\n");
904 return -EFAULT;
905 }
906 if (qat_hal_set_ae_lm_mode(handle, ae, ICP_LMEM1,
907 (char)ICP_QAT_LOC_MEM1_MODE
908 (uof_image->ae_mode))) {
909 pr_err("QAT: qat_hal_set_ae_lm_mode LMEM1 error\n");
910 return -EFAULT;
911 }
912 }
913 }
914 return 0;
915 }
916
qat_uclo_init_uword_num(struct icp_qat_fw_loader_handle * handle)917 static void qat_uclo_init_uword_num(struct icp_qat_fw_loader_handle *handle)
918 {
919 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
920 struct icp_qat_uclo_encapme *image;
921 int a;
922
923 for (a = 0; a < obj_handle->uimage_num; a++) {
924 image = &obj_handle->ae_uimage[a];
925 image->uwords_num = image->page->beg_addr_p +
926 image->page->micro_words_num;
927 }
928 }
929
qat_uclo_parse_uof_obj(struct icp_qat_fw_loader_handle * handle)930 static int qat_uclo_parse_uof_obj(struct icp_qat_fw_loader_handle *handle)
931 {
932 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
933 unsigned int ae;
934
935 obj_handle->encap_uof_obj.beg_uof = obj_handle->obj_hdr->file_buff;
936 obj_handle->encap_uof_obj.obj_hdr = (struct icp_qat_uof_objhdr *)
937 obj_handle->obj_hdr->file_buff;
938 obj_handle->uword_in_bytes = 6;
939 obj_handle->prod_type = qat_uclo_get_dev_type(handle);
940 obj_handle->prod_rev = PID_MAJOR_REV |
941 (PID_MINOR_REV & handle->hal_handle->revision_id);
942 if (qat_uclo_check_uof_compat(obj_handle)) {
943 pr_err("QAT: UOF incompatible\n");
944 return -EINVAL;
945 }
946 obj_handle->uword_buf = kcalloc(UWORD_CPYBUF_SIZE, sizeof(u64),
947 GFP_KERNEL);
948 if (!obj_handle->uword_buf)
949 return -ENOMEM;
950 obj_handle->ustore_phy_size = ICP_QAT_UCLO_MAX_USTORE;
951 if (!obj_handle->obj_hdr->file_buff ||
952 !qat_uclo_map_str_table(obj_handle->obj_hdr, ICP_QAT_UOF_STRT,
953 &obj_handle->str_table)) {
954 pr_err("QAT: UOF doesn't have effective images\n");
955 goto out_err;
956 }
957 obj_handle->uimage_num =
958 qat_uclo_map_uimage(obj_handle, obj_handle->ae_uimage,
959 ICP_QAT_UCLO_MAX_AE * ICP_QAT_UCLO_MAX_CTX);
960 if (!obj_handle->uimage_num)
961 goto out_err;
962 if (qat_uclo_map_ae(handle, handle->hal_handle->ae_max_num)) {
963 pr_err("QAT: Bad object\n");
964 goto out_check_uof_aemask_err;
965 }
966 qat_uclo_init_uword_num(handle);
967 qat_uclo_map_initmem_table(&obj_handle->encap_uof_obj,
968 &obj_handle->init_mem_tab);
969 if (qat_uclo_set_ae_mode(handle))
970 goto out_check_uof_aemask_err;
971 return 0;
972 out_check_uof_aemask_err:
973 for (ae = 0; ae < obj_handle->uimage_num; ae++)
974 kfree(obj_handle->ae_uimage[ae].page);
975 out_err:
976 kfree(obj_handle->uword_buf);
977 return -EFAULT;
978 }
979
qat_uclo_map_suof_file_hdr(struct icp_qat_fw_loader_handle * handle,struct icp_qat_suof_filehdr * suof_ptr,int suof_size)980 static int qat_uclo_map_suof_file_hdr(struct icp_qat_fw_loader_handle *handle,
981 struct icp_qat_suof_filehdr *suof_ptr,
982 int suof_size)
983 {
984 unsigned int check_sum = 0;
985 unsigned int min_ver_offset = 0;
986 struct icp_qat_suof_handle *suof_handle = handle->sobj_handle;
987
988 suof_handle->file_id = ICP_QAT_SUOF_FID;
989 suof_handle->suof_buf = (char *)suof_ptr;
990 suof_handle->suof_size = suof_size;
991 min_ver_offset = suof_size - offsetof(struct icp_qat_suof_filehdr,
992 min_ver);
993 check_sum = qat_uclo_calc_str_checksum((char *)&suof_ptr->min_ver,
994 min_ver_offset);
995 if (check_sum != suof_ptr->check_sum) {
996 pr_err("QAT: incorrect SUOF checksum\n");
997 return -EINVAL;
998 }
999 suof_handle->check_sum = suof_ptr->check_sum;
1000 suof_handle->min_ver = suof_ptr->min_ver;
1001 suof_handle->maj_ver = suof_ptr->maj_ver;
1002 suof_handle->fw_type = suof_ptr->fw_type;
1003 return 0;
1004 }
1005
qat_uclo_map_simg(struct icp_qat_suof_handle * suof_handle,struct icp_qat_suof_img_hdr * suof_img_hdr,struct icp_qat_suof_chunk_hdr * suof_chunk_hdr)1006 static void qat_uclo_map_simg(struct icp_qat_suof_handle *suof_handle,
1007 struct icp_qat_suof_img_hdr *suof_img_hdr,
1008 struct icp_qat_suof_chunk_hdr *suof_chunk_hdr)
1009 {
1010 struct icp_qat_simg_ae_mode *ae_mode;
1011 struct icp_qat_suof_objhdr *suof_objhdr;
1012
1013 suof_img_hdr->simg_buf = (suof_handle->suof_buf +
1014 suof_chunk_hdr->offset +
1015 sizeof(*suof_objhdr));
1016 suof_img_hdr->simg_len = ((struct icp_qat_suof_objhdr *)(uintptr_t)
1017 (suof_handle->suof_buf +
1018 suof_chunk_hdr->offset))->img_length;
1019
1020 suof_img_hdr->css_header = suof_img_hdr->simg_buf;
1021 suof_img_hdr->css_key = (suof_img_hdr->css_header +
1022 sizeof(struct icp_qat_css_hdr));
1023 suof_img_hdr->css_signature = suof_img_hdr->css_key +
1024 ICP_QAT_CSS_FWSK_MODULUS_LEN +
1025 ICP_QAT_CSS_FWSK_EXPONENT_LEN;
1026 suof_img_hdr->css_simg = suof_img_hdr->css_signature +
1027 ICP_QAT_CSS_SIGNATURE_LEN;
1028
1029 ae_mode = (struct icp_qat_simg_ae_mode *)(suof_img_hdr->css_simg);
1030 suof_img_hdr->ae_mask = ae_mode->ae_mask;
1031 suof_img_hdr->simg_name = (unsigned long)&ae_mode->simg_name;
1032 suof_img_hdr->appmeta_data = (unsigned long)&ae_mode->appmeta_data;
1033 suof_img_hdr->fw_type = ae_mode->fw_type;
1034 }
1035
1036 static void
qat_uclo_map_suof_symobjs(struct icp_qat_suof_handle * suof_handle,struct icp_qat_suof_chunk_hdr * suof_chunk_hdr)1037 qat_uclo_map_suof_symobjs(struct icp_qat_suof_handle *suof_handle,
1038 struct icp_qat_suof_chunk_hdr *suof_chunk_hdr)
1039 {
1040 char **sym_str = (char **)&suof_handle->sym_str;
1041 unsigned int *sym_size = &suof_handle->sym_size;
1042 struct icp_qat_suof_strtable *str_table_obj;
1043
1044 *sym_size = *(unsigned int *)(uintptr_t)
1045 (suof_chunk_hdr->offset + suof_handle->suof_buf);
1046 *sym_str = (char *)(uintptr_t)
1047 (suof_handle->suof_buf + suof_chunk_hdr->offset +
1048 sizeof(str_table_obj->tab_length));
1049 }
1050
qat_uclo_check_simg_compat(struct icp_qat_fw_loader_handle * handle,struct icp_qat_suof_img_hdr * img_hdr)1051 static int qat_uclo_check_simg_compat(struct icp_qat_fw_loader_handle *handle,
1052 struct icp_qat_suof_img_hdr *img_hdr)
1053 {
1054 struct icp_qat_simg_ae_mode *img_ae_mode = NULL;
1055 unsigned int prod_rev, maj_ver, prod_type;
1056
1057 prod_type = qat_uclo_get_dev_type(handle);
1058 img_ae_mode = (struct icp_qat_simg_ae_mode *)img_hdr->css_simg;
1059 prod_rev = PID_MAJOR_REV |
1060 (PID_MINOR_REV & handle->hal_handle->revision_id);
1061 if (img_ae_mode->dev_type != prod_type) {
1062 pr_err("QAT: incompatible product type %x\n",
1063 img_ae_mode->dev_type);
1064 return -EINVAL;
1065 }
1066 maj_ver = prod_rev & 0xff;
1067 if ((maj_ver > img_ae_mode->devmax_ver) ||
1068 (maj_ver < img_ae_mode->devmin_ver)) {
1069 pr_err("QAT: incompatible device majver 0x%x\n", maj_ver);
1070 return -EINVAL;
1071 }
1072 return 0;
1073 }
1074
qat_uclo_del_suof(struct icp_qat_fw_loader_handle * handle)1075 static void qat_uclo_del_suof(struct icp_qat_fw_loader_handle *handle)
1076 {
1077 struct icp_qat_suof_handle *sobj_handle = handle->sobj_handle;
1078
1079 kfree(sobj_handle->img_table.simg_hdr);
1080 sobj_handle->img_table.simg_hdr = NULL;
1081 kfree(handle->sobj_handle);
1082 handle->sobj_handle = NULL;
1083 }
1084
qat_uclo_tail_img(struct icp_qat_suof_img_hdr * suof_img_hdr,unsigned int img_id,unsigned int num_simgs)1085 static void qat_uclo_tail_img(struct icp_qat_suof_img_hdr *suof_img_hdr,
1086 unsigned int img_id, unsigned int num_simgs)
1087 {
1088 struct icp_qat_suof_img_hdr img_header;
1089
1090 if (img_id != num_simgs - 1) {
1091 memcpy(&img_header, &suof_img_hdr[num_simgs - 1],
1092 sizeof(*suof_img_hdr));
1093 memcpy(&suof_img_hdr[num_simgs - 1], &suof_img_hdr[img_id],
1094 sizeof(*suof_img_hdr));
1095 memcpy(&suof_img_hdr[img_id], &img_header,
1096 sizeof(*suof_img_hdr));
1097 }
1098 }
1099
qat_uclo_map_suof(struct icp_qat_fw_loader_handle * handle,struct icp_qat_suof_filehdr * suof_ptr,int suof_size)1100 static int qat_uclo_map_suof(struct icp_qat_fw_loader_handle *handle,
1101 struct icp_qat_suof_filehdr *suof_ptr,
1102 int suof_size)
1103 {
1104 struct icp_qat_suof_handle *suof_handle = handle->sobj_handle;
1105 struct icp_qat_suof_chunk_hdr *suof_chunk_hdr = NULL;
1106 struct icp_qat_suof_img_hdr *suof_img_hdr = NULL;
1107 int ret = 0, ae0_img = ICP_QAT_UCLO_MAX_AE;
1108 unsigned int i = 0;
1109 struct icp_qat_suof_img_hdr img_header;
1110
1111 if (!suof_ptr || (suof_size == 0)) {
1112 pr_err("QAT: input parameter SUOF pointer/size is NULL\n");
1113 return -EINVAL;
1114 }
1115 if (qat_uclo_check_suof_format(suof_ptr))
1116 return -EINVAL;
1117 ret = qat_uclo_map_suof_file_hdr(handle, suof_ptr, suof_size);
1118 if (ret)
1119 return ret;
1120 suof_chunk_hdr = (struct icp_qat_suof_chunk_hdr *)
1121 ((uintptr_t)suof_ptr + sizeof(*suof_ptr));
1122
1123 qat_uclo_map_suof_symobjs(suof_handle, suof_chunk_hdr);
1124 suof_handle->img_table.num_simgs = suof_ptr->num_chunks - 1;
1125
1126 if (suof_handle->img_table.num_simgs != 0) {
1127 suof_img_hdr = kcalloc(suof_handle->img_table.num_simgs,
1128 sizeof(img_header),
1129 GFP_KERNEL);
1130 if (!suof_img_hdr)
1131 return -ENOMEM;
1132 suof_handle->img_table.simg_hdr = suof_img_hdr;
1133 }
1134
1135 for (i = 0; i < suof_handle->img_table.num_simgs; i++) {
1136 qat_uclo_map_simg(handle->sobj_handle, &suof_img_hdr[i],
1137 &suof_chunk_hdr[1 + i]);
1138 ret = qat_uclo_check_simg_compat(handle,
1139 &suof_img_hdr[i]);
1140 if (ret)
1141 return ret;
1142 if ((suof_img_hdr[i].ae_mask & 0x1) != 0)
1143 ae0_img = i;
1144 }
1145 qat_uclo_tail_img(suof_img_hdr, ae0_img,
1146 suof_handle->img_table.num_simgs);
1147 return 0;
1148 }
1149
1150 #define ADD_ADDR(high, low) ((((u64)high) << 32) + low)
1151 #define BITS_IN_DWORD 32
1152
qat_uclo_auth_fw(struct icp_qat_fw_loader_handle * handle,struct icp_qat_fw_auth_desc * desc)1153 static int qat_uclo_auth_fw(struct icp_qat_fw_loader_handle *handle,
1154 struct icp_qat_fw_auth_desc *desc)
1155 {
1156 unsigned int fcu_sts, retry = 0;
1157 u64 bus_addr;
1158
1159 bus_addr = ADD_ADDR(desc->css_hdr_high, desc->css_hdr_low)
1160 - sizeof(struct icp_qat_auth_chunk);
1161 SET_CAP_CSR(handle, FCU_DRAM_ADDR_HI, (bus_addr >> BITS_IN_DWORD));
1162 SET_CAP_CSR(handle, FCU_DRAM_ADDR_LO, bus_addr);
1163 SET_CAP_CSR(handle, FCU_CONTROL, FCU_CTRL_CMD_AUTH);
1164
1165 do {
1166 msleep(FW_AUTH_WAIT_PERIOD);
1167 fcu_sts = GET_CAP_CSR(handle, FCU_STATUS);
1168 if ((fcu_sts & FCU_AUTH_STS_MASK) == FCU_STS_VERI_FAIL)
1169 goto auth_fail;
1170 if (((fcu_sts >> FCU_STS_AUTHFWLD_POS) & 0x1))
1171 if ((fcu_sts & FCU_AUTH_STS_MASK) == FCU_STS_VERI_DONE)
1172 return 0;
1173 } while (retry++ < FW_AUTH_MAX_RETRY);
1174 auth_fail:
1175 pr_err("QAT: authentication error (FCU_STATUS = 0x%x),retry = %d\n",
1176 fcu_sts & FCU_AUTH_STS_MASK, retry);
1177 return -EINVAL;
1178 }
1179
qat_uclo_simg_alloc(struct icp_qat_fw_loader_handle * handle,struct icp_firml_dram_desc * dram_desc,unsigned int size)1180 static int qat_uclo_simg_alloc(struct icp_qat_fw_loader_handle *handle,
1181 struct icp_firml_dram_desc *dram_desc,
1182 unsigned int size)
1183 {
1184 void *vptr;
1185 dma_addr_t ptr;
1186
1187 vptr = dma_alloc_coherent(&handle->pci_dev->dev,
1188 size, &ptr, GFP_KERNEL);
1189 if (!vptr)
1190 return -ENOMEM;
1191 dram_desc->dram_base_addr_v = vptr;
1192 dram_desc->dram_bus_addr = ptr;
1193 dram_desc->dram_size = size;
1194 return 0;
1195 }
1196
qat_uclo_simg_free(struct icp_qat_fw_loader_handle * handle,struct icp_firml_dram_desc * dram_desc)1197 static void qat_uclo_simg_free(struct icp_qat_fw_loader_handle *handle,
1198 struct icp_firml_dram_desc *dram_desc)
1199 {
1200 dma_free_coherent(&handle->pci_dev->dev,
1201 (size_t)(dram_desc->dram_size),
1202 (dram_desc->dram_base_addr_v),
1203 dram_desc->dram_bus_addr);
1204 memset(dram_desc, 0, sizeof(*dram_desc));
1205 }
1206
qat_uclo_ummap_auth_fw(struct icp_qat_fw_loader_handle * handle,struct icp_qat_fw_auth_desc ** desc)1207 static void qat_uclo_ummap_auth_fw(struct icp_qat_fw_loader_handle *handle,
1208 struct icp_qat_fw_auth_desc **desc)
1209 {
1210 struct icp_firml_dram_desc dram_desc;
1211
1212 dram_desc.dram_base_addr_v = *desc;
1213 dram_desc.dram_bus_addr = ((struct icp_qat_auth_chunk *)
1214 (*desc))->chunk_bus_addr;
1215 dram_desc.dram_size = ((struct icp_qat_auth_chunk *)
1216 (*desc))->chunk_size;
1217 qat_uclo_simg_free(handle, &dram_desc);
1218 }
1219
qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle * handle,char * image,unsigned int size,struct icp_qat_fw_auth_desc ** desc)1220 static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
1221 char *image, unsigned int size,
1222 struct icp_qat_fw_auth_desc **desc)
1223 {
1224 struct icp_qat_css_hdr *css_hdr = (struct icp_qat_css_hdr *)image;
1225 struct icp_qat_fw_auth_desc *auth_desc;
1226 struct icp_qat_auth_chunk *auth_chunk;
1227 u64 virt_addr, bus_addr, virt_base;
1228 unsigned int length, simg_offset = sizeof(*auth_chunk);
1229 struct icp_firml_dram_desc img_desc;
1230
1231 if (size > (ICP_QAT_AE_IMG_OFFSET + ICP_QAT_CSS_MAX_IMAGE_LEN)) {
1232 pr_err("QAT: error, input image size overflow %d\n", size);
1233 return -EINVAL;
1234 }
1235 length = (css_hdr->fw_type == CSS_AE_FIRMWARE) ?
1236 ICP_QAT_CSS_AE_SIMG_LEN + simg_offset :
1237 size + ICP_QAT_CSS_FWSK_PAD_LEN + simg_offset;
1238 if (qat_uclo_simg_alloc(handle, &img_desc, length)) {
1239 pr_err("QAT: error, allocate continuous dram fail\n");
1240 return -ENOMEM;
1241 }
1242
1243 auth_chunk = img_desc.dram_base_addr_v;
1244 auth_chunk->chunk_size = img_desc.dram_size;
1245 auth_chunk->chunk_bus_addr = img_desc.dram_bus_addr;
1246 virt_base = (uintptr_t)img_desc.dram_base_addr_v + simg_offset;
1247 bus_addr = img_desc.dram_bus_addr + simg_offset;
1248 auth_desc = img_desc.dram_base_addr_v;
1249 auth_desc->css_hdr_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
1250 auth_desc->css_hdr_low = (unsigned int)bus_addr;
1251 virt_addr = virt_base;
1252
1253 memcpy((void *)(uintptr_t)virt_addr, image, sizeof(*css_hdr));
1254 /* pub key */
1255 bus_addr = ADD_ADDR(auth_desc->css_hdr_high, auth_desc->css_hdr_low) +
1256 sizeof(*css_hdr);
1257 virt_addr = virt_addr + sizeof(*css_hdr);
1258
1259 auth_desc->fwsk_pub_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
1260 auth_desc->fwsk_pub_low = (unsigned int)bus_addr;
1261
1262 memcpy((void *)(uintptr_t)virt_addr,
1263 (void *)(image + sizeof(*css_hdr)),
1264 ICP_QAT_CSS_FWSK_MODULUS_LEN);
1265 /* padding */
1266 memset((void *)(uintptr_t)(virt_addr + ICP_QAT_CSS_FWSK_MODULUS_LEN),
1267 0, ICP_QAT_CSS_FWSK_PAD_LEN);
1268
1269 /* exponent */
1270 memcpy((void *)(uintptr_t)(virt_addr + ICP_QAT_CSS_FWSK_MODULUS_LEN +
1271 ICP_QAT_CSS_FWSK_PAD_LEN),
1272 (void *)(image + sizeof(*css_hdr) +
1273 ICP_QAT_CSS_FWSK_MODULUS_LEN),
1274 sizeof(unsigned int));
1275
1276 /* signature */
1277 bus_addr = ADD_ADDR(auth_desc->fwsk_pub_high,
1278 auth_desc->fwsk_pub_low) +
1279 ICP_QAT_CSS_FWSK_PUB_LEN;
1280 virt_addr = virt_addr + ICP_QAT_CSS_FWSK_PUB_LEN;
1281 auth_desc->signature_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
1282 auth_desc->signature_low = (unsigned int)bus_addr;
1283
1284 memcpy((void *)(uintptr_t)virt_addr,
1285 (void *)(image + sizeof(*css_hdr) +
1286 ICP_QAT_CSS_FWSK_MODULUS_LEN +
1287 ICP_QAT_CSS_FWSK_EXPONENT_LEN),
1288 ICP_QAT_CSS_SIGNATURE_LEN);
1289
1290 bus_addr = ADD_ADDR(auth_desc->signature_high,
1291 auth_desc->signature_low) +
1292 ICP_QAT_CSS_SIGNATURE_LEN;
1293 virt_addr += ICP_QAT_CSS_SIGNATURE_LEN;
1294
1295 auth_desc->img_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
1296 auth_desc->img_low = (unsigned int)bus_addr;
1297 auth_desc->img_len = size - ICP_QAT_AE_IMG_OFFSET;
1298 memcpy((void *)(uintptr_t)virt_addr,
1299 (void *)(image + ICP_QAT_AE_IMG_OFFSET),
1300 auth_desc->img_len);
1301 virt_addr = virt_base;
1302 /* AE firmware */
1303 if (((struct icp_qat_css_hdr *)(uintptr_t)virt_addr)->fw_type ==
1304 CSS_AE_FIRMWARE) {
1305 auth_desc->img_ae_mode_data_high = auth_desc->img_high;
1306 auth_desc->img_ae_mode_data_low = auth_desc->img_low;
1307 bus_addr = ADD_ADDR(auth_desc->img_ae_mode_data_high,
1308 auth_desc->img_ae_mode_data_low) +
1309 sizeof(struct icp_qat_simg_ae_mode);
1310
1311 auth_desc->img_ae_init_data_high = (unsigned int)
1312 (bus_addr >> BITS_IN_DWORD);
1313 auth_desc->img_ae_init_data_low = (unsigned int)bus_addr;
1314 bus_addr += ICP_QAT_SIMG_AE_INIT_SEQ_LEN;
1315 auth_desc->img_ae_insts_high = (unsigned int)
1316 (bus_addr >> BITS_IN_DWORD);
1317 auth_desc->img_ae_insts_low = (unsigned int)bus_addr;
1318 } else {
1319 auth_desc->img_ae_insts_high = auth_desc->img_high;
1320 auth_desc->img_ae_insts_low = auth_desc->img_low;
1321 }
1322 *desc = auth_desc;
1323 return 0;
1324 }
1325
qat_uclo_load_fw(struct icp_qat_fw_loader_handle * handle,struct icp_qat_fw_auth_desc * desc)1326 static int qat_uclo_load_fw(struct icp_qat_fw_loader_handle *handle,
1327 struct icp_qat_fw_auth_desc *desc)
1328 {
1329 unsigned int i;
1330 unsigned int fcu_sts;
1331 struct icp_qat_simg_ae_mode *virt_addr;
1332 unsigned int fcu_loaded_ae_pos = FCU_LOADED_AE_POS;
1333
1334 virt_addr = (void *)((uintptr_t)desc +
1335 sizeof(struct icp_qat_auth_chunk) +
1336 sizeof(struct icp_qat_css_hdr) +
1337 ICP_QAT_CSS_FWSK_PUB_LEN +
1338 ICP_QAT_CSS_SIGNATURE_LEN);
1339 for (i = 0; i < handle->hal_handle->ae_max_num; i++) {
1340 int retry = 0;
1341
1342 if (!((virt_addr->ae_mask >> i) & 0x1))
1343 continue;
1344 if (qat_hal_check_ae_active(handle, i)) {
1345 pr_err("QAT: AE %d is active\n", i);
1346 return -EINVAL;
1347 }
1348 SET_CAP_CSR(handle, FCU_CONTROL,
1349 (FCU_CTRL_CMD_LOAD | (i << FCU_CTRL_AE_POS)));
1350
1351 do {
1352 msleep(FW_AUTH_WAIT_PERIOD);
1353 fcu_sts = GET_CAP_CSR(handle, FCU_STATUS);
1354 if (((fcu_sts & FCU_AUTH_STS_MASK) ==
1355 FCU_STS_LOAD_DONE) &&
1356 ((fcu_sts >> fcu_loaded_ae_pos) & (1 << i)))
1357 break;
1358 } while (retry++ < FW_AUTH_MAX_RETRY);
1359 if (retry > FW_AUTH_MAX_RETRY) {
1360 pr_err("QAT: firmware load failed timeout %x\n", retry);
1361 return -EINVAL;
1362 }
1363 }
1364 return 0;
1365 }
1366
qat_uclo_map_suof_obj(struct icp_qat_fw_loader_handle * handle,void * addr_ptr,int mem_size)1367 static int qat_uclo_map_suof_obj(struct icp_qat_fw_loader_handle *handle,
1368 void *addr_ptr, int mem_size)
1369 {
1370 struct icp_qat_suof_handle *suof_handle;
1371
1372 suof_handle = kzalloc(sizeof(*suof_handle), GFP_KERNEL);
1373 if (!suof_handle)
1374 return -ENOMEM;
1375 handle->sobj_handle = suof_handle;
1376 if (qat_uclo_map_suof(handle, addr_ptr, mem_size)) {
1377 qat_uclo_del_suof(handle);
1378 pr_err("QAT: map SUOF failed\n");
1379 return -EINVAL;
1380 }
1381 return 0;
1382 }
1383
qat_uclo_wr_mimage(struct icp_qat_fw_loader_handle * handle,void * addr_ptr,int mem_size)1384 int qat_uclo_wr_mimage(struct icp_qat_fw_loader_handle *handle,
1385 void *addr_ptr, int mem_size)
1386 {
1387 struct icp_qat_fw_auth_desc *desc = NULL;
1388 int status = 0;
1389
1390 if (handle->fw_auth) {
1391 if (!qat_uclo_map_auth_fw(handle, addr_ptr, mem_size, &desc))
1392 status = qat_uclo_auth_fw(handle, desc);
1393 qat_uclo_ummap_auth_fw(handle, &desc);
1394 } else {
1395 if (handle->pci_dev->device == PCI_DEVICE_ID_INTEL_QAT_C3XXX) {
1396 pr_err("QAT: C3XXX doesn't support unsigned MMP\n");
1397 return -EINVAL;
1398 }
1399 qat_uclo_wr_sram_by_words(handle, 0, addr_ptr, mem_size);
1400 }
1401 return status;
1402 }
1403
qat_uclo_map_uof_obj(struct icp_qat_fw_loader_handle * handle,void * addr_ptr,int mem_size)1404 static int qat_uclo_map_uof_obj(struct icp_qat_fw_loader_handle *handle,
1405 void *addr_ptr, int mem_size)
1406 {
1407 struct icp_qat_uof_filehdr *filehdr;
1408 struct icp_qat_uclo_objhandle *objhdl;
1409
1410 objhdl = kzalloc(sizeof(*objhdl), GFP_KERNEL);
1411 if (!objhdl)
1412 return -ENOMEM;
1413 objhdl->obj_buf = kmemdup(addr_ptr, mem_size, GFP_KERNEL);
1414 if (!objhdl->obj_buf)
1415 goto out_objbuf_err;
1416 filehdr = (struct icp_qat_uof_filehdr *)objhdl->obj_buf;
1417 if (qat_uclo_check_uof_format(filehdr))
1418 goto out_objhdr_err;
1419 objhdl->obj_hdr = qat_uclo_map_chunk((char *)objhdl->obj_buf, filehdr,
1420 ICP_QAT_UOF_OBJS);
1421 if (!objhdl->obj_hdr) {
1422 pr_err("QAT: object file chunk is null\n");
1423 goto out_objhdr_err;
1424 }
1425 handle->obj_handle = objhdl;
1426 if (qat_uclo_parse_uof_obj(handle))
1427 goto out_overlay_obj_err;
1428 return 0;
1429
1430 out_overlay_obj_err:
1431 handle->obj_handle = NULL;
1432 kfree(objhdl->obj_hdr);
1433 out_objhdr_err:
1434 kfree(objhdl->obj_buf);
1435 out_objbuf_err:
1436 kfree(objhdl);
1437 return -ENOMEM;
1438 }
1439
qat_uclo_map_obj(struct icp_qat_fw_loader_handle * handle,void * addr_ptr,int mem_size)1440 int qat_uclo_map_obj(struct icp_qat_fw_loader_handle *handle,
1441 void *addr_ptr, int mem_size)
1442 {
1443 BUILD_BUG_ON(ICP_QAT_UCLO_MAX_AE >=
1444 (sizeof(handle->hal_handle->ae_mask) * 8));
1445
1446 if (!handle || !addr_ptr || mem_size < 24)
1447 return -EINVAL;
1448
1449 return (handle->fw_auth) ?
1450 qat_uclo_map_suof_obj(handle, addr_ptr, mem_size) :
1451 qat_uclo_map_uof_obj(handle, addr_ptr, mem_size);
1452 }
1453
qat_uclo_del_uof_obj(struct icp_qat_fw_loader_handle * handle)1454 void qat_uclo_del_uof_obj(struct icp_qat_fw_loader_handle *handle)
1455 {
1456 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
1457 unsigned int a;
1458
1459 if (handle->sobj_handle)
1460 qat_uclo_del_suof(handle);
1461 if (!obj_handle)
1462 return;
1463
1464 kfree(obj_handle->uword_buf);
1465 for (a = 0; a < obj_handle->uimage_num; a++)
1466 kfree(obj_handle->ae_uimage[a].page);
1467
1468 for (a = 0; a < handle->hal_handle->ae_max_num; a++)
1469 qat_uclo_free_ae_data(&obj_handle->ae_data[a]);
1470
1471 kfree(obj_handle->obj_hdr);
1472 kfree(obj_handle->obj_buf);
1473 kfree(obj_handle);
1474 handle->obj_handle = NULL;
1475 }
1476
qat_uclo_fill_uwords(struct icp_qat_uclo_objhandle * obj_handle,struct icp_qat_uclo_encap_page * encap_page,u64 * uword,unsigned int addr_p,unsigned int raddr,u64 fill)1477 static void qat_uclo_fill_uwords(struct icp_qat_uclo_objhandle *obj_handle,
1478 struct icp_qat_uclo_encap_page *encap_page,
1479 u64 *uword, unsigned int addr_p,
1480 unsigned int raddr, u64 fill)
1481 {
1482 u64 uwrd = 0;
1483 unsigned int i;
1484
1485 if (!encap_page) {
1486 *uword = fill;
1487 return;
1488 }
1489 for (i = 0; i < encap_page->uwblock_num; i++) {
1490 if (raddr >= encap_page->uwblock[i].start_addr &&
1491 raddr <= encap_page->uwblock[i].start_addr +
1492 encap_page->uwblock[i].words_num - 1) {
1493 raddr -= encap_page->uwblock[i].start_addr;
1494 raddr *= obj_handle->uword_in_bytes;
1495 memcpy(&uwrd, (void *)(((uintptr_t)
1496 encap_page->uwblock[i].micro_words) + raddr),
1497 obj_handle->uword_in_bytes);
1498 uwrd = uwrd & 0xbffffffffffull;
1499 }
1500 }
1501 *uword = uwrd;
1502 if (*uword == INVLD_UWORD)
1503 *uword = fill;
1504 }
1505
qat_uclo_wr_uimage_raw_page(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uclo_encap_page * encap_page,unsigned int ae)1506 static void qat_uclo_wr_uimage_raw_page(struct icp_qat_fw_loader_handle *handle,
1507 struct icp_qat_uclo_encap_page
1508 *encap_page, unsigned int ae)
1509 {
1510 unsigned int uw_physical_addr, uw_relative_addr, i, words_num, cpylen;
1511 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
1512 u64 fill_pat;
1513
1514 /* load the page starting at appropriate ustore address */
1515 /* get fill-pattern from an image -- they are all the same */
1516 memcpy(&fill_pat, obj_handle->ae_uimage[0].img_ptr->fill_pattern,
1517 sizeof(u64));
1518 uw_physical_addr = encap_page->beg_addr_p;
1519 uw_relative_addr = 0;
1520 words_num = encap_page->micro_words_num;
1521 while (words_num) {
1522 if (words_num < UWORD_CPYBUF_SIZE)
1523 cpylen = words_num;
1524 else
1525 cpylen = UWORD_CPYBUF_SIZE;
1526
1527 /* load the buffer */
1528 for (i = 0; i < cpylen; i++)
1529 qat_uclo_fill_uwords(obj_handle, encap_page,
1530 &obj_handle->uword_buf[i],
1531 uw_physical_addr + i,
1532 uw_relative_addr + i, fill_pat);
1533
1534 /* copy the buffer to ustore */
1535 qat_hal_wr_uwords(handle, (unsigned char)ae,
1536 uw_physical_addr, cpylen,
1537 obj_handle->uword_buf);
1538
1539 uw_physical_addr += cpylen;
1540 uw_relative_addr += cpylen;
1541 words_num -= cpylen;
1542 }
1543 }
1544
qat_uclo_wr_uimage_page(struct icp_qat_fw_loader_handle * handle,struct icp_qat_uof_image * image)1545 static void qat_uclo_wr_uimage_page(struct icp_qat_fw_loader_handle *handle,
1546 struct icp_qat_uof_image *image)
1547 {
1548 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
1549 unsigned int ctx_mask, s;
1550 struct icp_qat_uclo_page *page;
1551 unsigned char ae;
1552 int ctx;
1553
1554 if (ICP_QAT_CTX_MODE(image->ae_mode) == ICP_QAT_UCLO_MAX_CTX)
1555 ctx_mask = 0xff;
1556 else
1557 ctx_mask = 0x55;
1558 /* load the default page and set assigned CTX PC
1559 * to the entrypoint address */
1560 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
1561 if (!test_bit(ae, (unsigned long *)&image->ae_assigned))
1562 continue;
1563 /* find the slice to which this image is assigned */
1564 for (s = 0; s < obj_handle->ae_data[ae].slice_num; s++) {
1565 if (image->ctx_assigned & obj_handle->ae_data[ae].
1566 ae_slices[s].ctx_mask_assigned)
1567 break;
1568 }
1569 if (s >= obj_handle->ae_data[ae].slice_num)
1570 continue;
1571 page = obj_handle->ae_data[ae].ae_slices[s].page;
1572 if (!page->encap_page->def_page)
1573 continue;
1574 qat_uclo_wr_uimage_raw_page(handle, page->encap_page, ae);
1575
1576 page = obj_handle->ae_data[ae].ae_slices[s].page;
1577 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++)
1578 obj_handle->ae_data[ae].ae_slices[s].cur_page[ctx] =
1579 (ctx_mask & (1 << ctx)) ? page : NULL;
1580 qat_hal_set_live_ctx(handle, (unsigned char)ae,
1581 image->ctx_assigned);
1582 qat_hal_set_pc(handle, (unsigned char)ae, image->ctx_assigned,
1583 image->entry_address);
1584 }
1585 }
1586
qat_uclo_wr_suof_img(struct icp_qat_fw_loader_handle * handle)1587 static int qat_uclo_wr_suof_img(struct icp_qat_fw_loader_handle *handle)
1588 {
1589 unsigned int i;
1590 struct icp_qat_fw_auth_desc *desc = NULL;
1591 struct icp_qat_suof_handle *sobj_handle = handle->sobj_handle;
1592 struct icp_qat_suof_img_hdr *simg_hdr = sobj_handle->img_table.simg_hdr;
1593
1594 for (i = 0; i < sobj_handle->img_table.num_simgs; i++) {
1595 if (qat_uclo_map_auth_fw(handle,
1596 (char *)simg_hdr[i].simg_buf,
1597 (unsigned int)
1598 (simg_hdr[i].simg_len),
1599 &desc))
1600 goto wr_err;
1601 if (qat_uclo_auth_fw(handle, desc))
1602 goto wr_err;
1603 if (qat_uclo_load_fw(handle, desc))
1604 goto wr_err;
1605 qat_uclo_ummap_auth_fw(handle, &desc);
1606 }
1607 return 0;
1608 wr_err:
1609 qat_uclo_ummap_auth_fw(handle, &desc);
1610 return -EINVAL;
1611 }
1612
qat_uclo_wr_uof_img(struct icp_qat_fw_loader_handle * handle)1613 static int qat_uclo_wr_uof_img(struct icp_qat_fw_loader_handle *handle)
1614 {
1615 struct icp_qat_uclo_objhandle *obj_handle = handle->obj_handle;
1616 unsigned int i;
1617
1618 if (qat_uclo_init_globals(handle))
1619 return -EINVAL;
1620 for (i = 0; i < obj_handle->uimage_num; i++) {
1621 if (!obj_handle->ae_uimage[i].img_ptr)
1622 return -EINVAL;
1623 if (qat_uclo_init_ustore(handle, &obj_handle->ae_uimage[i]))
1624 return -EINVAL;
1625 qat_uclo_wr_uimage_page(handle,
1626 obj_handle->ae_uimage[i].img_ptr);
1627 }
1628 return 0;
1629 }
1630
qat_uclo_wr_all_uimage(struct icp_qat_fw_loader_handle * handle)1631 int qat_uclo_wr_all_uimage(struct icp_qat_fw_loader_handle *handle)
1632 {
1633 return (handle->fw_auth) ? qat_uclo_wr_suof_img(handle) :
1634 qat_uclo_wr_uof_img(handle);
1635 }
1636