The Release Notes for Simulink Coder in release R2022b includes an entry that seems to be relevant to this question. The suggested workflow is to create a protected model instead.
If this workflow doesn't satisfy your needs like creating an S-function from a subsystem does, please contact Technical Support directly using this link. Describe your workflow and indicate what's lacking from the protected model functionality that prevents it from satisfying your needs. The Support staff may be able to offer an alternate suggestion or enter your use case into the enhancement database for development to consider for a future release.