1 /*******************************************************************************
2 *Copyright (c) 2014 PMC-Sierra, Inc. All rights reserved.
3 *
4 *Redistribution and use in source and binary forms, with or without modification, are permitted provided
5 *that the following conditions are met:
6 *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7 *following disclaimer.
8 *2. Redistributions in binary form must reproduce the above copyright notice,
9 *this list of conditions and the following disclaimer in the documentation and/or other materials provided
10 *with the distribution.
11 *
12 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16 *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20 *
21 *
22 *******************************************************************************/
23 /*******************************************************************************
24 **
25 ** Version Control Information:
26 **
27 ** $Revision: 113920 $
28 ** $Author: mcleanda $
29 ** $Date: 2012-05-08 11:30:44 -0700 (Tue, 08 May 2012) $
30 ** $Id: lxencrypt.c 113920 2012-05-08 18:30:44Z mcleanda $
31 **
32 *******************************************************************************/
33
34 #include <dev/pms/RefTisa/tisa/sassata/common/tdioctl.h>
35 #include <dev/pms/RefTisa/tisa/api/titypes.h>
36
37 #include <dev/pms/freebsd/driver/common/lxencrypt.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <vm/uma.h>
41
42
43 #ifdef ENCRYPT_ENHANCE
44 static atomic_t ioerr_queue_count;
45 /******************************************************************************
46 careful_write():
47
48 Purpose:
49 Parameters:
50 Return:
51 Note:
52 ******************************************************************************/
53 static int
careful_write(char * buf,int offset,int max,const char * fmt,...)54 careful_write(char *buf, int offset, int max, const char *fmt, ...)
55 {
56 static char s[PAGE_SIZE]; /* Assumes serialization */
57 va_list args;
58 int i;
59
60 if(offset > max)
61 return 0;
62 s[PAGE_SIZE - 1] = '\0';
63
64 va_start(args, fmt);
65 i = vsnprintf(s, PAGE_SIZE - 1, fmt, args);
66 if((offset + i) > max)
67 return 0;
68 memcpy(buf + offset, s, i);
69 va_end(args);
70
71 return i;
72 }
73
74 /******************************************************************************
75 set_dek_table_entry():
76
77 Purpose:
78 Parameters:
79 Return:
80 Note:
81 ******************************************************************************/
82 static inline int
set_dek_table_entry(struct device * dev,const char * buf,size_t len,dek_table_e table)83 set_dek_table_entry(struct device *dev, const char *buf, size_t len, dek_table_e table)
84 {
85 int index;
86 struct Scsi_Host *shost = class_to_shost(dev);
87 struct agtiapi_softc *pCard = (struct agtiapi_softc *) shost->hostdata;
88
89 /* Check permissions */
90 if(!capable(CAP_SYS_ADMIN))
91 return -EACCES;
92
93 if(!pCard->encrypt)
94 return -EINVAL;
95
96 if(table != DEK_TABLE_0 && table != DEK_TABLE_1)
97 return -EINVAL;
98
99 sscanf(buf, "%d", &index);
100 if(index >= 0 && index < DEK_MAX_TABLE_ITEMS) {
101 pCard->dek_index[table] = index;
102 return strlen(buf);
103 }
104 return -EINVAL;
105 }
106
107 /******************************************************************************
108 set_dek_table_entry0():
109
110 Purpose:
111 Parameters:
112 Return:
113 Note:
114 ******************************************************************************/
115 ssize_t
set_dek_table_entry0(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)116 set_dek_table_entry0(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
117 {
118 return set_dek_table_entry(dev, buf, len, DEK_TABLE_0);
119 }
120
121 /******************************************************************************
122 set_dek_table_entry1():
123
124 Purpose:
125 Parameters:
126 Return:
127 Note:
128 ******************************************************************************/
129 ssize_t
set_dek_table_entry1(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)130 set_dek_table_entry1(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
131 {
132 return set_dek_table_entry(dev, buf, len, DEK_TABLE_1);
133 }
134
135
136 /******************************************************************************
137 show_dek_table_entry():
138
139 Purpose:
140 Parameters:
141 Return:
142 Note:
143 ******************************************************************************/
144 static inline int
show_dek_table_entry(struct device * dev,char * buf,unsigned int table)145 show_dek_table_entry(struct device *dev, char *buf, unsigned int table)
146 {
147 int i = 0, j;
148 unsigned char *p;
149 struct Scsi_Host *sh = class_to_shost(dev);
150 ag_card_t *pCard = (ag_card_t *) sh->hostdata;
151 ag_card_info_t *pCardInfo = pCard->pCardInfo;
152 ag_resource_info_t *pRscInfo = &pCardInfo->tiRscInfo;
153 tiEncryptDekBlob_t *pDekTable = NULL;
154
155 if(!pCard->encrypt)
156 return -EINVAL;
157
158 if(table == DEK_TABLE_0)
159 pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_1].virtPtr;
160 else if(table == DEK_TABLE_1)
161 pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_2].virtPtr;
162 if(pDekTable == NULL)
163 return -EINVAL;
164
165 if(pCard->dek_index[table] >= 0 || pCard->dek_index[table] < DEK_MAX_TABLE_ITEMS) {
166 i += careful_write(buf, i, PAGE_SIZE, "%4d: ", pCard->dek_index[table]);
167 p = (unsigned char *) &pDekTable[pCard->dek_index[table]];
168 for(j = 0; j < sizeof(tiEncryptDekBlob_t); j++) {
169 i += careful_write(buf, i, PAGE_SIZE, "%02x", p[j]);
170 }
171 i += careful_write(buf, i, PAGE_SIZE, "\n");
172 } else {
173 i += careful_write(buf, i, PAGE_SIZE, "Bad DEK index %d; range: 0 - %d\n", pCard->dek_index[table], DEK_MAX_TABLE_ITEMS);
174 }
175
176 /* BUG if we return more than a single page of data */
177 //BUG_ON(i > PAGE_SIZE);
178 if (i > PAGE_SIZE)
179 i = PAGE_SIZE;
180
181 return i;
182 }
183
184 /******************************************************************************
185 show_dek_table_entry0():
186
187 Purpose:
188 Parameters:
189 Return:
190 Note:
191 ******************************************************************************/
192 ssize_t
show_dek_table_entry0(struct device * dev,struct device_attribute * attr,char * buf)193 show_dek_table_entry0(struct device *dev, struct device_attribute *attr, char *buf)
194 {
195 return show_dek_table_entry(dev, buf, DEK_TABLE_0);
196 }
197
198 /******************************************************************************
199 show_dek_table_entry1():
200
201 Purpose:
202 Parameters:
203 Return:
204 Note:
205 ******************************************************************************/
206 ssize_t
show_dek_table_entry1(struct device * dev,struct device_attribute * attr,char * buf)207 show_dek_table_entry1(struct device *dev, struct device_attribute *attr, char *buf)
208 {
209 return show_dek_table_entry(dev, buf, DEK_TABLE_1);
210 }
211
212 /******************************************************************************
213 show_kek_table():
214
215 Purpose:
216 Parameters:
217 Return:
218 Note:
219 ******************************************************************************/
220 ssize_t
show_kek_table(struct device * dev,struct device_attribute * attr,char * buf)221 show_kek_table(struct device *dev, struct device_attribute *attr, char *buf)
222 {
223 int i = 0, j, kek_index;
224 unsigned char *p;
225 struct Scsi_Host *sh = class_to_shost(dev);
226 ag_card_t *pCard = (ag_card_t *) sh->hostdata;
227
228 if(!pCard->encrypt)
229 return -EINVAL;
230
231 for(kek_index = 0; kek_index < KEK_TABLE_MAX_ENTRY; kek_index++) {
232 i += careful_write(buf, i, PAGE_SIZE, " %4d: %08x ", kek_index, pCard->kek_table[kek_index].wrapperIndex);
233 p = (unsigned char *) &pCard->kek_table[kek_index].kekBlob;
234 for(j = 0; j < sizeof(tiEncryptKekBlob_t); j++) {
235 i += careful_write(buf, i, PAGE_SIZE, "%02x", p[j]);
236 }
237 i += careful_write(buf, i, PAGE_SIZE, "\n");
238 }
239 i += careful_write(buf, i, PAGE_SIZE, "\n");
240
241 /* BUG if we return more than a single page of data */
242 //BUG_ON(i > PAGE_SIZE);
243 if (i > PAGE_SIZE)
244 i = PAGE_SIZE;
245
246 return i;
247 }
248
249 /******************************************************************************
250 show_dek_kek_map():
251
252 Purpose:
253 Parameters:
254 Return:
255 Note:
256 ******************************************************************************/
257 static inline int
show_dek_kek_map(struct device * dev,char * buf,unsigned int table)258 show_dek_kek_map(struct device *dev, char *buf, unsigned int table)
259 {
260 int i = 0, dek_index;
261 struct Scsi_Host *sh = class_to_shost(dev);
262 ag_card_t *pCard = (ag_card_t *) sh->hostdata;
263
264 if(!pCard->encrypt)
265 return -EINVAL;
266
267 if(table != DEK_TABLE_0 && table != DEK_TABLE_1)
268 return -EINVAL;
269
270 i += careful_write(buf, i, PAGE_SIZE, "Table %d\n", table);
271 i += careful_write(buf, i, PAGE_SIZE, "=======\n");
272 for(dek_index = 0; dek_index < DEK_MAX_TABLE_ITEMS; dek_index++) {
273 i += careful_write(buf, i, PAGE_SIZE, " %4d: %08x\n", dek_index, pCard->dek_kek_map[table][dek_index].kekIndex);
274 }
275 i += sprintf(buf + i, "\n");
276
277 /* BUG if we return more than a single page of data */
278 //BUG_ON(i > PAGE_SIZE);
279 if (i > PAGE_SIZE)
280 i = PAGE_SIZE;
281
282 return i;
283 }
284
285 /******************************************************************************
286 show_dek_kek_map0():
287
288 Purpose:
289 Parameters:
290 Return:
291 Note:
292 ******************************************************************************/
293 ssize_t
294
show_dek_kek_map0(struct device * dev,struct device_attribute * attr,char * buf)295 show_dek_kek_map0(struct device *dev, struct device_attribute *attr, char *buf)
296 {
297 return show_dek_kek_map(dev, buf, 0);
298 }
299
300 /******************************************************************************
301 show_dek_kek_map1():
302
303 Purpose:
304 Parameters:
305 Return:
306 Note:
307 ******************************************************************************/
308 ssize_t
show_dek_kek_map1(struct device * dev,struct device_attribute * attr,char * buf)309 show_dek_kek_map1(struct device *dev, struct device_attribute *attr, char *buf)
310 {
311 return show_dek_kek_map(dev, buf, 1);
312 }
313
314 /******************************************************************************
315 show_target_dek_map():
316
317 Purpose:
318 Parameters:
319 Return:
320 Note:
321 ******************************************************************************/
322 ssize_t
show_target_dek_map(struct device * dev,struct device_attribute * attr,char * buf)323 show_target_dek_map(struct device *dev, struct device_attribute *attr, char *buf)
324 {
325 int i = 0;
326 unsigned int chan, device, lun = 0;
327 ag_encrypt_map_t *p;
328 struct list_head *lh;
329 struct Scsi_Host *sh = class_to_shost(dev);
330 ag_card_t *pCard = (ag_card_t *) sh->hostdata;
331
332 if(!pCard->encrypt)
333 return -EINVAL;
334
335 for(chan = 0; chan <= AGTIAPI_MAX_CHANNEL_NUM; chan++) {
336 for(device = 0; device < pCard->devDiscover; device++) {
337 #ifdef REPORT_ALL_LUNS
338 for(lun = 0; lun < AGTIAPI_MAX_LUN; lun++) {
339 #endif
340 lh = MAP_TABLE_ENTRY(pCard, chan, device, lun);
341 if(lh) {
342 list_for_each_entry(p, lh, list) {
343 if(p->dekIndex != DEK_INDEX_INVALID)
344 i += careful_write(buf, i, PAGE_SIZE, " %u:%u:%u: %x %8x %8x %16lx %16lx %08x:%08x %1x\n", chan, device, lun, p->dekTable, p->dekIndex, p->kekIndex, p->lbaMin, p->lbaMax, p->keyTag[1], p->keyTag[0], p->keyTagCheck);
345 }
346 }
347 #ifdef REPORT_ALL_LUNS
348 }
349 #endif
350 }
351 }
352
353 if (i > PAGE_SIZE)
354 i = PAGE_SIZE;
355
356 return i;
357 }
358
359
360 /******************************************************************************
361 agtiapi_AddDek():
362
363 Purpose:
364 Parameters:
365 Return:
366 Note:
367 ******************************************************************************/
368 static int
agtiapi_AddDek(ag_card_t * pCard,bit32 dek_table,bit32 dek_index,bit32 blob_format,bit32 entry_sz,tiEncryptDekBlob_t * dek_blob,U32_64 * addr)369 agtiapi_AddDek(ag_card_t *pCard, bit32 dek_table, bit32 dek_index, bit32 blob_format, bit32 entry_sz, tiEncryptDekBlob_t *dek_blob, U32_64 *addr)
370 {
371 ag_resource_info_t *pRscInfo = &pCard->pCardInfo->tiRscInfo;
372 tiEncryptDekBlob_t *pDekTable;
373 char *p;
374
375 if (dek_index >= DEK_MAX_TABLE_ITEMS) {
376 printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS);
377 return -E_DEK_INDEX;
378 }
379
380 switch(dek_table) {
381 case DEK_TABLE_0:
382 pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_1].virtPtr;
383 break;
384 case DEK_TABLE_1:
385 pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_2].virtPtr;
386 break;
387 default:
388 printf("%s: Unknown dek table %d\n", __FUNCTION__, dek_table);
389 return -E_DEK_TABLE;
390 }
391
392 #ifdef __VMKLNX__
393 *addr = (U32_64) __pa(&pDekTable[0]);
394 #else
395 *addr = (U32_64) virt_to_phys(&pDekTable[0]);
396 #endif
397
398 p = (char *) &pDekTable[0] + (dek_index * pCard->dek_size);
399
400 printf("%s: Base: %p, Index: %08x, Virt: %p Size: %d\n", __FUNCTION__, pDekTable, dek_index, &pDekTable[dek_index], pCard->dek_size);
401 memcpy(p, dek_blob, pCard->dek_size);
402 wmb();
403
404 /* Flush entry */
405 ostiCacheFlush(&pCard->tiRoot, NULL, p, pCard->dek_size);
406
407 return 0;
408 }
409
410 /******************************************************************************
411 agtiapi_MapDekKek():
412
413 Purpose:
414 Parameters:
415 Return:
416 Note:
417 ******************************************************************************/
418 static int
agtiapi_MapDekKek(ag_card_t * pCard,bit32 dek_table,bit32 dek_index,bit32 kek_index)419 agtiapi_MapDekKek(ag_card_t *pCard, bit32 dek_table, bit32 dek_index, bit32 kek_index)
420 {
421 if (dek_index >= DEK_MAX_TABLE_ITEMS) {
422 printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS);
423 return -E_DEK_INDEX;
424 }
425
426 if (dek_table >= DEK_MAX_TABLES) {
427 printf("%s: Bad dek table.\n", __FUNCTION__);
428 return -E_DEK_TABLE;
429 }
430
431 if (kek_index >= KEK_TABLE_MAX_ENTRY) {
432 printf("%s: Bad kek index.\n", __FUNCTION__);
433 return -E_KEK_INDEX;
434 }
435
436 pCard->dek_kek_map[dek_table][dek_index].kekIndex = kek_index;
437 return 0;
438 }
439
440 /******************************************************************************
441 agtiapi_AddKek():
442
443 Purpose:
444 Parameters:
445 Return:
446 Note:
447 ******************************************************************************/
448 static int
agtiapi_AddKek(ag_card_t * pCard,bit32 kek_index,bit32 wrapper_kek_index,tiEncryptKekBlob_t * kek_blob)449 agtiapi_AddKek(ag_card_t *pCard, bit32 kek_index, bit32 wrapper_kek_index, tiEncryptKekBlob_t *kek_blob)
450 {
451 if (kek_index >= KEK_TABLE_MAX_ENTRY) {
452 printf("%s: Bad kek index.\n", __FUNCTION__);
453 return -E_KEK_INDEX;
454 }
455 if (wrapper_kek_index >= KEK_TABLE_MAX_ENTRY) {
456 printf("%s: Bad kek wrapper index.\n", __FUNCTION__);
457 return -E_KEK_INDEX;
458 }
459 pCard->kek_table[kek_index].wrapperIndex = wrapper_kek_index;
460 memcpy(&pCard->kek_table[kek_index].kekBlob, kek_blob, sizeof(tiEncryptKekBlob_t));
461 return 0;
462 }
463
464 /******************************************************************************
465 agtiapi_MapDek():
466
467 Purpose:
468 Parameters:
469 Return:
470 Note:
471 ******************************************************************************/
472 static int
agtiapi_MapDek(ag_card_t * pCard,EncryptDeviceDekMap_t * dek_map)473 agtiapi_MapDek(ag_card_t *pCard, EncryptDeviceDekMap_t *dek_map)
474 {
475 int found = 0;
476 bit32 chan, device, lun;
477 bit32 dek_table, dek_index, kek_index;
478 unsigned long long lba_min, lba_max;
479 ag_encrypt_map_t *p, *n;
480 struct list_head *lh;
481
482 chan = dek_map->channel;
483 device = dek_map->device;
484 lun = dek_map->lun;
485
486 lba_min = dek_map->dekMapEntry[0].startLBA;
487 lba_max = dek_map->dekMapEntry[0].endLBA;
488
489 dek_table = dek_map->dekMapEntry[0].dek.dekTable;
490 dek_index = dek_map->dekMapEntry[0].dek.dekIndex;
491
492 /* Sanity check channel, device, lun */
493 if (chan > AGTIAPI_MAX_CHANNEL_NUM) {
494 printf("%s: Bad channel %d.\n", __FUNCTION__, chan);
495 return -E_CHANNEL_INDEX;
496 }
497 if (device >= pCard->devDiscover) {
498 printf("%s: Bad device %d.\n", __FUNCTION__, device);
499 return -E_DEVICE_INDEX;
500 }
501 if (lun >= AGTIAPI_MAX_LUN) {
502 printf("%s: Bad lun %d.\n", __FUNCTION__, lun);
503 return -E_LUN_INDEX;
504 }
505
506 /* Sanity check dek index */
507 if (dek_index >= DEK_MAX_TABLE_ITEMS) {
508 printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS);
509 return -E_DEK_INDEX;
510 }
511
512 /* Sanity check dek table */
513 if (dek_table >= DEK_MAX_TABLES) {
514 printf("%s: Bad dek table %d.\n", __FUNCTION__, dek_table);
515 return -E_DEK_TABLE;
516 }
517
518 /* Check that lba min and lba max are sane */
519 if (lba_min >= lba_max) {
520 printf("%s: Bad lba min and lba max: %llx %llx.\n", __FUNCTION__, lba_min, lba_max);
521 return -E_LBA_RANGE;
522 }
523
524 /* dek_table and dek_index are valid, look up kek */
525 kek_index = pCard->dek_kek_map[dek_table][dek_index].kekIndex;
526
527 lh = MAP_TABLE_ENTRY(pCard, chan, device, lun);
528
529 if (dek_map->dekMapEntry[0].flags & ENCRYPT_DEK_MAP_ENTRY_CLEAR) {
530 /* Delete the entry */
531 found = 0;
532 list_for_each_entry_safe(p, n, lh, list) {
533 if (p->lbaMin == lba_min &&
534 p->lbaMax == lba_max &&
535 p->dekTable == dek_table &&
536 p->dekIndex == dek_index &&
537 p->kekIndex == kek_index) {
538 /* Entry found, unlink and reclaim it */
539 found = 1;
540 list_del(&p->list);
541 mempool_free(p, pCard->map_mempool);
542 }
543 }
544 if (!found) {
545 printf("%s: Entry %x %x %x %llx %llx not found.\n", __FUNCTION__, dek_table, dek_index, kek_index, lba_min, lba_max);
546 return -E_NOT_FOUND;
547 }
548 } else if (dek_map->dekMapEntry[0].flags & ENCRYPT_DEK_MAP_ENTRY_VALID) {
549 /* Add the entry */
550
551 p = (ag_encrypt_map_t *)uma_zalloc(pCard->map_cache, M_WAITOK); //Encryption
552 if (!p) {
553 printf("%s: Unable to allocate from memory pool.\n", __FUNCTION__);
554 return -E_MEMPOOL_ALLOC;
555 }
556
557 /* Populate it */
558 p->lbaMin = lba_min;
559 p->lbaMax = lba_max;
560 p->dekTable = dek_table;
561 p->dekIndex = dek_index;
562 p->kekIndex = kek_index;
563 p->keyTagCheck = dek_map->keytag_check;
564 memcpy(&p->keyTag, &dek_map->keytag, sizeof(p->keyTag));
565
566 /* Test to see if this new mapping overlaps an existing mapping */
567 list_for_each_entry(n, lh, list) {
568 /*
569 * Check if the start lba falls in existing range ||
570 * Check if the end lba falls in existing range ||
571 * Check if the start lba of the existing range falls in the new range
572 */
573 if (((p->lbaMin >= n->lbaMin) && (p->lbaMin <= n->lbaMax)) ||
574 ((p->lbaMax >= n->lbaMin) && (p->lbaMax <= n->lbaMax)) ||
575 ((n->lbaMin >= p->lbaMin) && (n->lbaMin <= p->lbaMax))) {
576 printf("%s: WARNING: New entry lba range overlap: %llx - %llx vs %llx - %llx.\n", __FUNCTION__, p->lbaMin, p->lbaMax, n->lbaMin, n->lbaMax);
577 }
578 }
579
580 /* Link it in to list at the head so it takes precedence */
581 list_add(&p->list, lh);
582
583 /* TODO: Decide if/how to refcount each dek/kek index used by the mapping */
584
585 } else {
586 printf("%s: Bad flags %08x\n", __FUNCTION__, dek_map->dekMapEntry[0].flags);
587 return -E_FLAGS;
588 }
589
590 return 0;
591 }
592 #endif
593 #ifdef HIALEAH_ENCRYPTION
594 /******************************************************************************
595 agtiapi_SetupEncryption():
596
597 Purpose:
598 Parameters:
599 Return:
600 Note:
601 ******************************************************************************/
602 int
agtiapi_SetupEncryption(struct agtiapi_softc * pCard)603 agtiapi_SetupEncryption(struct agtiapi_softc *pCard)
604 {
605 tiRoot_t *tiRoot = (tiRoot_t *) &pCard->tiRoot;
606 bit32 status = tiSuccess;
607 printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION\n");
608 if (pCard->encrypt == agTRUE)
609 {
610 status = tiCOMEncryptGetInfo(tiRoot);
611 printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION tiCOMEncryptGetInfo Status 0x%x\n",status);
612
613 if(status == 1 )
614 {
615 status = tiCOMEncryptHilSet(tiRoot );
616 if (status) {
617 pCard->encrypt = agFALSE;
618 printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION not set\n");
619 }
620 }
621 }
622 return 0;
623 }
624 #ifdef ENCRYPT_ENHANCE
625 /******************************************************************************
626 agtiapi_SetupEncryptionPools():
627
628 Purpose:
629 Parameters:
630 Return:
631 Note:
632 ******************************************************************************/
633 int
agtiapi_SetupEncryptionPools(struct agtiapi_softc * pCard)634 agtiapi_SetupEncryptionPools(struct agtiapi_softc *pCard)
635 {
636 /* Configure encryption memory pool */
637 memset(pCard->map_cache_name, 0, sizeof(pCard->map_cache_name));
638 snprintf(pCard->map_cache_name, sizeof(pCard->map_cache_name) - 1, "map_cache_%d", pCard->cardNo);
639
640 //zone allocation
641 pCard->map_cache = uma_zcreate(pCard->map_cache_name, sizeof(ag_encrypt_map_t),NULL, NULL, NULL, NULL, 0, 0);
642 if(!pCard->map_cache) {
643 /*
644 * This error may be due to an existing cache in the kernel
645 * from an earlier kmem_cache that wasn't properly freed
646 */
647 printf("Unable to create uma_zcreate cache for encryption map mempool.\n");
648 return -EFAULT;
649 }
650 uma_zone_set_max(pCard->map_cache, ENCRYPTION_MAP_MEMPOOL_SIZE);
651
652
653 /* Configure encryption IO error pool */
654 INIT_LIST_HEAD(&pCard->ioerr_queue);
655 /*#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)) // ####
656 pCard->ioerr_queue_lock = SPIN_LOCK_UNLOCKED;
657 #else */
658 pCard->ioerr_queue_lock = AG_SPIN_UNLOCK(pCard->ioerr_queue_lock);
659 //#endif
660
661
662 memset(pCard->ioerr_cache_name, 0, sizeof(pCard->ioerr_cache_name));
663 snprintf(pCard->ioerr_cache_name, sizeof(pCard->ioerr_cache_name) - 1, "ioerr_cache_%d", pCard->cardNo);
664
665 pCard->ioerr_cache = uma_zcreate(pCard->ioerr_cache_name, sizeof(ag_encrypt_ioerr_t), NULL, NULL, NULL, NULL, 0, 0);
666 if(!pCard->ioerr_cache) {
667 /*
668 * This error may be due to an existing cache in the kernel
669 * from an earlier kmem_cache that wasn't properly freed
670 */
671 printf("Unable to create kmem cache for encryption IO error mempool.\n");
672 return -EFAULT;
673 }
674 uma_zone_set_max(pCard->ioerr_cache, ENCRYPTION_IO_ERR_MEMPOOL_SIZE);
675
676 /* Set cipher mode to something invalid */
677 pCard->cipher_mode = CIPHER_MODE_INVALID;
678
679 return 0;
680 }
681 #endif
682 /******************************************************************************
683 agtiapi_CleanupEncryption():
684
685 Purpose:
686 Parameters:
687 Return:
688 Note:
689 ******************************************************************************/
690 void
agtiapi_CleanupEncryption(struct agtiapi_softc * pCard)691 agtiapi_CleanupEncryption(struct agtiapi_softc *pCard)
692 {
693 #ifdef ENCRYPT_ENHANCE
694 if(pCard->encrypt_map) {
695 int chan, device, lun;
696 struct list_head *lh;
697 ag_encrypt_map_t *p, *n;
698
699 for (chan = 0; chan < (AGTIAPI_MAX_CHANNEL_NUM + 1); chan++) {
700 for (device = 0; device < pCard->devDiscover; device++) {
701 for (lun = 0; lun < AGTIAPI_MAX_LUN; lun++) {
702 lh = MAP_TABLE_ENTRY(pCard, chan, device, lun);
703 list_for_each_entry_safe(p, n, lh, list) {
704 // mempool_free(p, pCard->map_mempool);
705 }
706 }
707 }
708 }
709 vfree(pCard->encrypt_map);
710 pCard->encrypt_map = NULL;
711 }
712 #endif
713 }
714
715 #ifdef ENCRYPT_ENHANCE
716 /******************************************************************************
717 agtiapi_CleanupEncryptionPools():
718
719 Purpose:
720 Parameters:
721 Return:
722 Note:
723 ******************************************************************************/
724 void
agtiapi_CleanupEncryptionPools(struct agtiapi_softc * pCard)725 agtiapi_CleanupEncryptionPools(struct agtiapi_softc *pCard)
726 {
727 ag_encrypt_ioerr_t *ioerr, *tmp;
728 atomic_set(&ioerr_queue_count);
729
730 /*
731 * TODO: check "outstanding_encrypted_io_count" for non-zero
732 * and free all mempool items prior to destroying pool
733 */
734
735 /* Clean up memory pools */
736 if (pCard->map_mempool) {
737 mempool_destroy(pCard->map_mempool);
738 printf("Encryption Map mempool released.\n");
739 pCard->map_mempool = NULL;
740 }
741
742 /* Clean up kmem cache */
743 if (pCard->map_cache) {
744 kmem_cache_destroy(pCard->map_cache);
745 printf("Kernel memory cache %s released.\n", pCard->map_cache_name);
746 pCard->map_cache = NULL;
747 }
748
749 /* Clean up memory pools */
750 list_for_each_entry_safe(ioerr, tmp, &pCard->ioerr_queue, list) {
751 list_del_init(&ioerr->list);
752 mempool_free(ioerr, pCard->ioerr_mempool);
753 atomic_dec(&ioerr_queue_count);
754 }
755
756 if (pCard->ioerr_mempool) {
757 mempool_destroy(pCard->ioerr_mempool);
758 printf("Encryption IO Error mempool released.\n");
759 pCard->ioerr_mempool = NULL;
760 }
761
762 /* Clean up kmem cache */
763 if (pCard->ioerr_cache) {
764 kmem_cache_destroy(pCard->ioerr_cache);
765 printf("Kernel memory cache %s released.\n", pCard->ioerr_cache_name);
766 pCard->ioerr_cache = NULL;
767 }
768 }
769
770 /******************************************************************************
771 agtiapi_EncryptionIoctl():
772
773 Purpose:
774 Parameters:
775 Return:
776 Note:
777 ******************************************************************************/
778 int
agtiapi_EncryptionIoctl(struct agtiapi_softc * pCard,IoctlEncrypt_t * pIoctlPayload)779 agtiapi_EncryptionIoctl(struct agtiapi_softc *pCard, IoctlEncrypt_t *pIoctlPayload)
780 {
781 int rv, rc = 0, skip_wait = 0;
782 tiRoot_t *tiRoot = (tiRoot_t *) &pCard->tiRoot;
783 IoctlTISAEncrypt_t *ioctl_data = &pIoctlPayload->body;
784 pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE;
785 pCard->ioctl_data = (void *) ioctl_data;
786 init_completion(&pCard->ioctl_completion);
787
788 /* Check that the system is quiesced */
789 if (atomic_read(&outstanding_encrypted_io_count) != 0)
790 printf("%s: WARNING: Attempting encryption management update with outstanding encrypted IOs!\n", __FUNCTION__);
791
792 printf("%s: Minor %d\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction);
793 switch(pIoctlPayload->hdr.MinorFunction) {
794 case IOCTL_MN_ENCRYPTION_GET_INFO:
795 {
796 //IoctlEncryptGetInfo_t *get_info = (IoctlEncryptGetInfo_t *) &ioctl_data->request;
797 rc = tiCOMEncryptGetInfo(tiRoot);
798 }
799 break;
800 case IOCTL_MN_ENCRYPTION_SET_MODE:
801 {
802 u32 reg_val = 0, new_cipher_mode = 0;
803 IoctlEncryptSetMode_t *set_mode = (IoctlEncryptSetMode_t *) &ioctl_data->request;
804
805 printf("%s: input %08x\n", __FUNCTION__, set_mode->securityCipherMode);
806
807 /* Set security mode */
808 if(TI_ENCRYPT_SEC_MODE_FACT_INIT)
809 if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_FACT_INIT) {
810 reg_val |= TI_ENCRYPT_SEC_MODE_FACT_INIT;
811 pCard->dek_size = DEK_SIZE_PLAIN;
812 }
813 if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_A) {
814 reg_val |= TI_ENCRYPT_SEC_MODE_A;
815 pCard->dek_size = DEK_SIZE_ENCRYPT;
816 } else if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_B) {
817 reg_val |= TI_ENCRYPT_SEC_MODE_B;
818 pCard->dek_size = DEK_SIZE_ENCRYPT;
819 }
820
821 /* Set cipher mode */
822 if(set_mode->securityCipherMode & TI_ENCRYPT_ATTRIB_CIPHER_XTS) {
823 reg_val |= TI_ENCRYPT_ATTRIB_CIPHER_XTS;
824 new_cipher_mode = TI_ENCRYPT_MODE_XTS_AES;
825 }
826
827 printf("%s: Setting security cipher mode to: 0x%08x\n", __FUNCTION__, reg_val);
828 pCard->cipher_mode = new_cipher_mode;
829
830 rc = tiCOMEncryptSetMode(tiRoot, reg_val);
831 }
832 break;
833 case IOCTL_MN_ENCRYPTION_KEK_ADD:
834 {
835 tiEncryptKekBlob_t kek_blob;
836 IoctlEncryptKekAdd_t *kek_add = (IoctlEncryptKekAdd_t *) &ioctl_data->request;
837 printf("%s: Add kek at index 0x%x wrapper 0x%x format 0x%x\n", __FUNCTION__, kek_add->kekIndex, kek_add->wrapperKekIndex, kek_add->blobFormat);
838
839 /* Copy kek_blob from user pointer to local buffer */
840 if(access_ok(kek_add->EncryptKekBlob, sizeof(kek_blob))) {
841 printf("%s: Starting copy from user %p to kernel %p\n", __FUNCTION__, kek_add->EncryptKekBlob, &kek_blob);
842 if((rv = copy_from_user(&kek_blob, kek_add->EncryptKekBlob, sizeof(kek_blob))) != 0) {
843 printf("%s: Copy error, %d left\n", __FUNCTION__, rv);
844 return IOCTL_CALL_FAIL;
845 }
846 rc = tiCOMEncryptKekAdd(tiRoot, kek_add->kekIndex, kek_add->wrapperKekIndex, kek_add->blobFormat, &kek_blob);
847
848 /* Add kek to local kek table (in case of chip reset) */
849 if(rc == tiSuccess) {
850 if(agtiapi_AddKek(pCard, kek_add->kekIndex, kek_add->wrapperKekIndex, &kek_blob) < 0) {
851 return IOCTL_CALL_FAIL;
852 }
853 }
854 } else {
855 return IOCTL_CALL_FAIL;
856 }
857 }
858 break;
859 case IOCTL_MN_ENCRYPTION_DEK_ADD:
860 {
861 tiEncryptDekBlob_t dek_blob; /* Copied in */
862 IoctlEncryptDekAdd_t *dek_add = (IoctlEncryptDekAdd_t *) &ioctl_data->request;
863 bit32 kek_index = dek_add->kekIndex;
864 bit32 dek_index = dek_add->dekIndex;
865 bit32 dek_table = dek_add->dekTable;
866 bit32 blob_format = dek_add->dekBlobFormat;
867 bit32 entry_sz = dek_add->dekTableKeyEntrySize;
868 U32_64 addr = 0;
869 bit32 addr_table[2];
870 memset(addr_table, 0, sizeof(addr_table));
871
872 printf("%s: Add dek at index 0x%x, table %x, kek index %x, blob format %x, entry size %x\n", __FUNCTION__, dek_index, dek_table, kek_index, blob_format, entry_sz);
873
874 /* Copy dek_blob from user pointer to local buffer */
875 if(access_ok(dek_add->dekBlob, sizeof(dek_blob))) {
876 printf("%s: Starting copy from user %p to kernel %p\n", __FUNCTION__, dek_add->dekBlob, &dek_blob);
877 if((rv = copy_from_user(&dek_blob, dek_add->dekBlob, sizeof(dek_blob))) != 0) {
878 printf("%s: Copy error, %d left\n", __FUNCTION__, rv);
879 return IOCTL_CALL_FAIL;
880 }
881
882 /* Add DEK to local table */
883 if (agtiapi_AddDek(pCard, dek_table, dek_index, blob_format, entry_sz, &dek_blob, &addr) < 0) {
884 return IOCTL_CALL_FAIL;
885 }
886 memcpy(addr_table, &addr, sizeof(addr));
887
888 /* Add DEK-KEK association in local table */
889 if (agtiapi_MapDekKek(pCard, dek_table, dek_index, kek_index) < 0) {
890 return IOCTL_CALL_FAIL;
891 }
892
893 /* Push DEK to chip */
894 rc = tiCOMEncryptDekAdd(tiRoot, kek_index, dek_table, addr_table[1], addr_table[0], dek_index, 1, blob_format, entry_sz);
895 } else {
896 return IOCTL_CALL_FAIL;
897 }
898 }
899 break;
900 case IOCTL_MN_ENCRYPTION_DEK_INVALID:
901 {
902 IoctlEncryptDekInvalidate_t *dek_to_invalidate = (IoctlEncryptDekInvalidate_t *) &ioctl_data->request;
903 printf("%s: Invalidating dek at index 0x%x, table %x\n", __FUNCTION__, dek_to_invalidate->dek.dekIndex, dek_to_invalidate->dek.dekTable);
904
905 rc = tiCOMEncryptDekInvalidate(tiRoot, dek_to_invalidate->dek.dekTable, dek_to_invalidate->dek.dekIndex);
906 /* TODO: What to do in local tables? Mark it? */
907 }
908 break;
909 case IOCTL_MN_ENCRYPTION_KEK_NVRAM:
910 {
911 rc = tiError;
912 }
913 break;
914 case IOCTL_MN_ENCRYPTION_DEK_ASSIGN:
915 {
916 IoctlEncryptDekMapTable_t *p_dek_map = (IoctlEncryptDekMapTable_t *) &ioctl_data->request;
917
918 /* Fill in host */
919 p_dek_map->dekMap[0].host = (bit32) pCard->pHost->host_no;
920
921 printf("%s: Host %u: Mapping %u:%u:%u (%llx to %llx) to dek at index 0x%x, table %x, keytag %08x:%08x\n", __FUNCTION__, p_dek_map->dekMap[0].host, p_dek_map->dekMap[0].channel, p_dek_map->dekMap[0].device, p_dek_map->dekMap[0].lun, p_dek_map->dekMap[0].dekMapEntry[0].startLBA, p_dek_map->dekMap[0].dekMapEntry[0].endLBA, p_dek_map->dekMap[0].dekMapEntry[0].dek.dekIndex, p_dek_map->dekMap[0].dekMapEntry[0].dek.dekTable, p_dek_map->dekMap[0].keytag[1], p_dek_map->dekMap[0].keytag[0]);
922
923 /* Create a mapping in local tables */
924 if (agtiapi_MapDek(pCard, &p_dek_map->dekMap[0]) < 0) {
925 pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE;
926 return IOCTL_CALL_FAIL;
927 }
928
929 rc = tiSuccess;
930 skip_wait = 1;
931 ioctl_data->encryptFunction = encryptSetDekMap;
932 ioctl_data->status = tiSuccess;
933 ioctl_data->subEvent = 0;
934 }
935 break;
936 case IOCTL_MN_ENCRYPTION_ERROR_QUERY:
937 {
938 unsigned long flags, i, query_flag;
939 ag_encrypt_ioerr_t *ioerr, *tmp;
940 IoctlEncryptErrorQuery_t *perr = (IoctlEncryptErrorQuery_t *) &ioctl_data->request;
941
942 printf("%s: query flag %x\n", __FUNCTION__, perr->query_flag);
943 query_flag = perr->query_flag;
944
945 /* initialize */
946 memset(perr, 0, sizeof(IoctlEncryptErrorQuery_t));
947
948 error_query_restart:
949 /* Take spinlock */
950 // spin_lock_irqsave(&pCard->ioerr_queue_lock, flags);
951 AG_SPIN_LOCK_IRQ(&pCard->ioerr_queue_lock, flags);
952
953 /* Walk list */
954 i = 0;
955 list_for_each_entry_safe(ioerr, tmp, &pCard->ioerr_queue, list) {
956 if (i >= 32)
957 break;
958
959 perr->valid_mask |= (1 << i);
960 memcpy(&perr->error[i], &ioerr->ioerr, sizeof(IoctlEncryptIOError_t));
961 list_del_init(&ioerr->list);
962 mempool_free(ioerr, pCard->ioerr_mempool);
963 i++;
964 atomic_dec(&ioerr_queue_count);
965 }
966
967 /* Release spinlock */
968 // spin_unlock_irqrestore(&pCard->ioerr_queue_lock, flags);
969 AG_SPIN_UNLOCK_IRQ(&pCard->ioerr_queue_lock, flags); //for test
970
971 if (!perr->valid_mask) {
972 /* No encryption IO error events, check flags to see if blocking wait OK */
973 if (query_flag == ERROR_QUERY_FLAG_BLOCK) {
974 if (wait_event_interruptible(ioerr_waitq, (atomic_read(&ioerr_queue_count)))) {
975 /* Awoken by signal */
976 return IOCTL_CALL_FAIL;
977 } else {
978 /* Awoken by IO error */
979 goto error_query_restart;
980 }
981 }
982 }
983 rc = tiSuccess;
984 skip_wait = 1;
985 ioctl_data->encryptFunction = encryptErrorQuery;
986 ioctl_data->status = tiSuccess;
987 ioctl_data->subEvent = 0;
988 }
989 break;
990 default:
991 printf("%s: Unrecognized Minor Function %d\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction);
992 pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE;
993 return IOCTL_CALL_FAIL;
994 break;
995 }
996
997 /* Demux rc */
998 switch(rc) {
999 case tiSuccess:
1000 if(!skip_wait)
1001 wait_for_completion(&pCard->ioctl_completion);
1002 /* Maybe: wait_for_completion_timeout() */
1003 pIoctlPayload->hdr.Status = ioctl_data->status;
1004 break;
1005 case tiNotSupported:
1006 pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_NOT_SUPPORTED;
1007 break;
1008 default:
1009 printf("%s: Status: %d\n", __FUNCTION__, rc);
1010 pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE;
1011 break;
1012 }
1013
1014 printf("%s: Encryption ioctl %d successful.\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction);
1015 return IOCTL_CALL_SUCCESS;
1016 }
1017 #endif
1018 /******************************************************************************
1019 agtiapi_SetupEncryptedIO():
1020
1021 Purpose:
1022 Parameters:
1023 Return:
1024 Note:
1025 ******************************************************************************/
1026 int
agtiapi_SetupEncryptedIO(struct agtiapi_softc * pCard,ccb_t * pccb,unsigned long long block)1027 agtiapi_SetupEncryptedIO(struct agtiapi_softc *pCard, ccb_t *pccb, unsigned long long block)
1028 {
1029
1030 pCard->cipher_mode = TI_ENCRYPT_ATTRIB_CIPHER_XTS;
1031 /* Check that cipher mode is set properly */
1032 if (pCard->cipher_mode == CIPHER_MODE_INVALID) {
1033 printf("%s: Cipher mode not yet set.\n", __FUNCTION__);
1034 return -E_BAD_CIPHER_MODE;
1035 }
1036
1037 memset(&(pccb->tiSuperScsiRequest.Encrypt), 0, sizeof(pccb->tiSuperScsiRequest.Encrypt));
1038 pccb->tiSuperScsiRequest.Encrypt.keyTagCheck = FALSE;
1039 pccb->tiSuperScsiRequest.Encrypt.encryptMode = pCard->cipher_mode;
1040 pccb->tiSuperScsiRequest.Encrypt.tweakVal_W0 = block;
1041 if(pccb->tiSuperScsiRequest.scsiCmnd.cdb[0] == READ_16 ||
1042 pccb->tiSuperScsiRequest.scsiCmnd.cdb[0] == WRITE_16)
1043 {
1044 pccb->tiSuperScsiRequest.Encrypt.tweakVal_W0 = ((pccb->tiSuperScsiRequest.scsiCmnd.cdb[6] << 24 ) |
1045 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[7] << 16 ) |
1046 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[8] << 8 ) |
1047 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[9]));
1048 pccb->tiSuperScsiRequest.Encrypt.tweakVal_W1 = ((pccb->tiSuperScsiRequest.scsiCmnd.cdb[2] << 24 ) |
1049 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[3] << 16 ) |
1050 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[4] << 8 ) |
1051 (pccb->tiSuperScsiRequest.scsiCmnd.cdb[5]));
1052 }
1053 /* Mark IO as valid encrypted IO */
1054 pccb->flags |= ENCRYPTED_IO;
1055 pccb->tiSuperScsiRequest.flags = TI_SCSI_INITIATOR_ENCRYPT;
1056
1057 /* Bump refcount (atomic) */
1058 atomic_inc(&outstanding_encrypted_io_count);
1059 return 0;
1060 }
1061
1062 /******************************************************************************
1063 agtiapi_CleanupEncryptedIO():
1064
1065 Purpose:
1066 Parameters:
1067 Return:
1068 Note:
1069 ******************************************************************************/
1070 void
agtiapi_CleanupEncryptedIO(struct agtiapi_softc * pCard,ccb_t * pccb)1071 agtiapi_CleanupEncryptedIO(struct agtiapi_softc *pCard, ccb_t *pccb)
1072 {
1073 if ((pccb->flags & ENCRYPTED_IO)) {
1074 /* Decrement refcount */
1075 atomic_dec(&outstanding_encrypted_io_count);
1076 }
1077 pccb->tiSuperScsiRequest.flags &= ~TI_SCSI_INITIATOR_ENCRYPT;
1078 pccb->flags &= ~ENCRYPTED_IO;
1079 }
1080 #ifdef ENCRYPT_ENHANCE
1081 /******************************************************************************
1082 agtiapi_HandleEncryptedIOFailure():
1083
1084 Purpose:
1085 Parameters:
1086 Return:
1087 Note:
1088 ******************************************************************************/
1089 void
agtiapi_HandleEncryptedIOFailure(ag_device_t * pDev,ccb_t * pccb)1090 agtiapi_HandleEncryptedIOFailure(ag_device_t *pDev, ccb_t *pccb)
1091 {
1092 unsigned long flags, qdepth;
1093 struct scsi_cmnd *cmd;
1094 ag_encrypt_ioerr_t *perr;
1095 ag_card_t *pCard;
1096
1097 cmd = pccb->cmd;
1098 if (!cmd) {
1099 printf("%s: Malformed pccb %p.\n", __FUNCTION__, pccb);
1100 return;
1101 }
1102
1103 pCard = pDev->pCard;
1104
1105 /* Sanity check */
1106 if (!(pccb->flags & ENCRYPTED_IO)) {
1107 printf("%s: Skipping IO %lx: Not Encrypted.\n", __FUNCTION__, cmd->serial_number);
1108 return;
1109 }
1110
1111 /* Check queue depth against max */
1112 qdepth = atomic_read(&ioerr_queue_count);
1113 if (qdepth >= IOERR_QUEUE_DEPTH_MAX) {
1114 printf("%s: Not queueing IO error due to queue full: %lu entries.\n", __FUNCTION__, qdepth);
1115 return;
1116 }
1117
1118 /* Get a container for the ag_encrypt_ioerr_t item from the mempool */
1119 // perr = mempool_alloc(pCard->ioerr_mempool, GFP_ATOMIC);
1120 p = (ag_encrypt_map_t *)uma_zalloc(pCard->map_cache, M_WAITOK); //Encryption
1121 if (!perr) {
1122 printf("%s: Mempool allocation failure.\n", __FUNCTION__);
1123 return;
1124 }
1125
1126 /* Populate ag_encrypt_ioerr_t container */
1127 perr->ioerr.error_id = cmd->serial_number;
1128 perr->ioerr.timestamp = cmd->jiffies_at_alloc;
1129 perr->ioerr.host = (unsigned int) cmd->device->host->host_no;
1130 perr->ioerr.channel = cmd->device->channel;
1131 perr->ioerr.device = cmd->device->id;
1132 perr->ioerr.lun = cmd->device->lun;
1133 perr->ioerr.scsi_cmd = (unsigned int) cmd->cmnd[0];
1134 perr->ioerr.dek_index = pccb->tiSuperScsiRequest.Encrypt.dekInfo.dekIndex;
1135 perr->ioerr.dek_table = pccb->tiSuperScsiRequest.Encrypt.dekInfo.dekTable;
1136 perr->ioerr.kek_index = pccb->tiSuperScsiRequest.Encrypt.kekIndex;
1137 perr->ioerr.keytag_check = pccb->tiSuperScsiRequest.Encrypt.keyTagCheck;
1138 perr->ioerr.encrypt_mode = pccb->tiSuperScsiRequest.Encrypt.encryptMode;
1139 perr->ioerr.keytag[0] = pccb->tiSuperScsiRequest.Encrypt.keyTag_W0;
1140 perr->ioerr.keytag[1] = pccb->tiSuperScsiRequest.Encrypt.keyTag_W1;
1141
1142 switch(pccb->scsiStatus) {
1143 case tiDetailDekKeyCacheMiss:
1144 case tiDetailDekIVMismatch:
1145 perr->ioerr.error_type = pccb->scsiStatus;
1146 break;
1147 default:
1148 printf("%s: Unrecognized encrypted IO completion error status: %d\n", __FUNCTION__, pccb->scsiStatus);
1149 perr->ioerr.error_type = 0xffffffff;
1150 break;
1151 }
1152
1153 /* Link IO err into queue */
1154 AG_SPIN_LOCK_IRQ(&pCard->ioerr_queue_lock, flags);
1155 list_add_tail(&perr->list, &pCard->ioerr_queue);
1156 AG_SPIN_UNLOCK_IRQ(&pCard->ioerr_queue_lock, flags);
1157
1158 /* Notify any wait queue waiters that an IO error has occurred */
1159 atomic_inc(&ioerr_queue_count);
1160 wake_up_interruptible(&ioerr_waitq);
1161
1162 }
1163 #endif
1164 #endif
1165