* Select a driver for a new session that supports the specified * algorithms and, optionally, is constrained according to the flags. */
| 619 | * algorithms and, optionally, is constrained according to the flags. |
| 620 | */ |
| 621 | static struct cryptocap * |
| 622 | crypto_select_driver(const struct crypto_session_params *csp, int flags) |
| 623 | { |
| 624 | struct cryptocap *cap, *best; |
| 625 | int best_match, error, hid; |
| 626 | |
| 627 | CRYPTO_DRIVER_ASSERT(); |
| 628 | |
| 629 | best = NULL; |
| 630 | for (hid = 0; hid < crypto_drivers_size; hid++) { |
| 631 | /* |
| 632 | * If there is no driver for this slot, or the driver |
| 633 | * is not appropriate (hardware or software based on |
| 634 | * match), then skip. |
| 635 | */ |
| 636 | cap = crypto_drivers[hid]; |
| 637 | if (cap == NULL || |
| 638 | (cap->cc_flags & flags) == 0) |
| 639 | continue; |
| 640 | |
| 641 | error = CRYPTODEV_PROBESESSION(cap->cc_dev, csp); |
| 642 | if (error >= 0) |
| 643 | continue; |
| 644 | |
| 645 | /* |
| 646 | * Use the driver with the highest probe value. |
| 647 | * Hardware drivers use a higher probe value than |
| 648 | * software. In case of a tie, prefer the driver with |
| 649 | * the fewest active sessions. |
| 650 | */ |
| 651 | if (best == NULL || error > best_match || |
| 652 | (error == best_match && |
| 653 | cap->cc_sessions < best->cc_sessions)) { |
| 654 | best = cap; |
| 655 | best_match = error; |
| 656 | } |
| 657 | } |
| 658 | return best; |
| 659 | } |
| 660 | |
| 661 | static enum alg_type { |
| 662 | ALG_NONE = 0, |