ocf.patch 722 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653
  1. diff -Nur linux-2.6.30.orig/crypto/Kconfig linux-2.6.30/crypto/Kconfig
  2. --- linux-2.6.30.orig/crypto/Kconfig 2009-06-10 05:05:27.000000000 +0200
  3. +++ linux-2.6.30/crypto/Kconfig 2009-06-11 10:55:27.000000000 +0200
  4. @@ -781,3 +781,5 @@
  5. source "drivers/crypto/Kconfig"
  6. endif # if CRYPTO
  7. +
  8. +source "crypto/ocf/Kconfig"
  9. diff -Nur linux-2.6.30.orig/crypto/Makefile linux-2.6.30/crypto/Makefile
  10. --- linux-2.6.30.orig/crypto/Makefile 2009-06-10 05:05:27.000000000 +0200
  11. +++ linux-2.6.30/crypto/Makefile 2009-06-11 10:55:27.000000000 +0200
  12. @@ -84,6 +84,8 @@
  13. obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o
  14. obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
  15. +obj-$(CONFIG_OCF_OCF) += ocf/
  16. +
  17. #
  18. # generic algorithms and the async_tx api
  19. #
  20. diff -Nur linux-2.6.30.orig/crypto/ocf/Config.in linux-2.6.30/crypto/ocf/Config.in
  21. --- linux-2.6.30.orig/crypto/ocf/Config.in 1970-01-01 01:00:00.000000000 +0100
  22. +++ linux-2.6.30/crypto/ocf/Config.in 2009-06-11 10:55:27.000000000 +0200
  23. @@ -0,0 +1,34 @@
  24. +#############################################################################
  25. +
  26. +mainmenu_option next_comment
  27. +comment 'OCF Configuration'
  28. +tristate 'OCF (Open Cryptograhic Framework)' CONFIG_OCF_OCF
  29. +dep_mbool ' enable fips RNG checks (fips check on RNG data before use)' \
  30. + CONFIG_OCF_FIPS $CONFIG_OCF_OCF
  31. +dep_mbool ' enable harvesting entropy for /dev/random' \
  32. + CONFIG_OCF_RANDOMHARVEST $CONFIG_OCF_OCF
  33. +dep_tristate ' cryptodev (user space support)' \
  34. + CONFIG_OCF_CRYPTODEV $CONFIG_OCF_OCF
  35. +dep_tristate ' cryptosoft (software crypto engine)' \
  36. + CONFIG_OCF_CRYPTOSOFT $CONFIG_OCF_OCF
  37. +dep_tristate ' safenet (HW crypto engine)' \
  38. + CONFIG_OCF_SAFE $CONFIG_OCF_OCF
  39. +dep_tristate ' IXP4xx (HW crypto engine)' \
  40. + CONFIG_OCF_IXP4XX $CONFIG_OCF_OCF
  41. +dep_mbool ' Enable IXP4xx HW to perform SHA1 and MD5 hashing (very slow)' \
  42. + CONFIG_OCF_IXP4XX_SHA1_MD5 $CONFIG_OCF_IXP4XX
  43. +dep_tristate ' hifn (HW crypto engine)' \
  44. + CONFIG_OCF_HIFN $CONFIG_OCF_OCF
  45. +dep_tristate ' talitos (HW crypto engine)' \
  46. + CONFIG_OCF_TALITOS $CONFIG_OCF_OCF
  47. +dep_tristate ' pasemi (HW crypto engine)' \
  48. + CONFIG_OCF_PASEMI $CONFIG_OCF_OCF
  49. +dep_tristate ' ep80579 (HW crypto engine)' \
  50. + CONFIG_OCF_EP80579 $CONFIG_OCF_OCF
  51. +dep_tristate ' ocfnull (does no crypto)' \
  52. + CONFIG_OCF_OCFNULL $CONFIG_OCF_OCF
  53. +dep_tristate ' ocf-bench (HW crypto in-kernel benchmark)' \
  54. + CONFIG_OCF_BENCH $CONFIG_OCF_OCF
  55. +endmenu
  56. +
  57. +#############################################################################
  58. diff -Nur linux-2.6.30.orig/crypto/ocf/criov.c linux-2.6.30/crypto/ocf/criov.c
  59. --- linux-2.6.30.orig/crypto/ocf/criov.c 1970-01-01 01:00:00.000000000 +0100
  60. +++ linux-2.6.30/crypto/ocf/criov.c 2009-06-11 10:55:27.000000000 +0200
  61. @@ -0,0 +1,215 @@
  62. +/* $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $ */
  63. +
  64. +/*
  65. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  66. + * Copyright (C) 2006-2007 David McCullough
  67. + * Copyright (C) 2004-2005 Intel Corporation.
  68. + * The license and original author are listed below.
  69. + *
  70. + * Copyright (c) 1999 Theo de Raadt
  71. + *
  72. + * Redistribution and use in source and binary forms, with or without
  73. + * modification, are permitted provided that the following conditions
  74. + * are met:
  75. + *
  76. + * 1. Redistributions of source code must retain the above copyright
  77. + * notice, this list of conditions and the following disclaimer.
  78. + * 2. Redistributions in binary form must reproduce the above copyright
  79. + * notice, this list of conditions and the following disclaimer in the
  80. + * documentation and/or other materials provided with the distribution.
  81. + * 3. The name of the author may not be used to endorse or promote products
  82. + * derived from this software without specific prior written permission.
  83. + *
  84. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  85. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  86. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  87. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  88. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  89. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  90. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  91. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  92. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  93. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  94. + *
  95. +__FBSDID("$FreeBSD: src/sys/opencrypto/criov.c,v 1.5 2006/06/04 22:15:13 pjd Exp $");
  96. + */
  97. +
  98. +#ifndef AUTOCONF_INCLUDED
  99. +#include <linux/config.h>
  100. +#endif
  101. +#include <linux/module.h>
  102. +#include <linux/init.h>
  103. +#include <linux/slab.h>
  104. +#include <linux/uio.h>
  105. +#include <linux/skbuff.h>
  106. +#include <linux/kernel.h>
  107. +#include <linux/mm.h>
  108. +#include <asm/io.h>
  109. +
  110. +#include <uio.h>
  111. +#include <cryptodev.h>
  112. +
  113. +/*
  114. + * This macro is only for avoiding code duplication, as we need to skip
  115. + * given number of bytes in the same way in three functions below.
  116. + */
  117. +#define CUIO_SKIP() do { \
  118. + KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
  119. + KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
  120. + while (off > 0) { \
  121. + KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
  122. + if (off < iov->iov_len) \
  123. + break; \
  124. + off -= iov->iov_len; \
  125. + iol--; \
  126. + iov++; \
  127. + } \
  128. +} while (0)
  129. +
  130. +void
  131. +cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
  132. +{
  133. + struct iovec *iov = uio->uio_iov;
  134. + int iol = uio->uio_iovcnt;
  135. + unsigned count;
  136. +
  137. + CUIO_SKIP();
  138. + while (len > 0) {
  139. + KASSERT(iol >= 0, ("%s: empty", __func__));
  140. + count = min((int)(iov->iov_len - off), len);
  141. + memcpy(cp, ((caddr_t)iov->iov_base) + off, count);
  142. + len -= count;
  143. + cp += count;
  144. + off = 0;
  145. + iol--;
  146. + iov++;
  147. + }
  148. +}
  149. +
  150. +void
  151. +cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
  152. +{
  153. + struct iovec *iov = uio->uio_iov;
  154. + int iol = uio->uio_iovcnt;
  155. + unsigned count;
  156. +
  157. + CUIO_SKIP();
  158. + while (len > 0) {
  159. + KASSERT(iol >= 0, ("%s: empty", __func__));
  160. + count = min((int)(iov->iov_len - off), len);
  161. + memcpy(((caddr_t)iov->iov_base) + off, cp, count);
  162. + len -= count;
  163. + cp += count;
  164. + off = 0;
  165. + iol--;
  166. + iov++;
  167. + }
  168. +}
  169. +
  170. +/*
  171. + * Return a pointer to iov/offset of location in iovec list.
  172. + */
  173. +struct iovec *
  174. +cuio_getptr(struct uio *uio, int loc, int *off)
  175. +{
  176. + struct iovec *iov = uio->uio_iov;
  177. + int iol = uio->uio_iovcnt;
  178. +
  179. + while (loc >= 0) {
  180. + /* Normal end of search */
  181. + if (loc < iov->iov_len) {
  182. + *off = loc;
  183. + return (iov);
  184. + }
  185. +
  186. + loc -= iov->iov_len;
  187. + if (iol == 0) {
  188. + if (loc == 0) {
  189. + /* Point at the end of valid data */
  190. + *off = iov->iov_len;
  191. + return (iov);
  192. + } else
  193. + return (NULL);
  194. + } else {
  195. + iov++, iol--;
  196. + }
  197. + }
  198. +
  199. + return (NULL);
  200. +}
  201. +
  202. +EXPORT_SYMBOL(cuio_copyback);
  203. +EXPORT_SYMBOL(cuio_copydata);
  204. +EXPORT_SYMBOL(cuio_getptr);
  205. +
  206. +
  207. +static void
  208. +skb_copy_bits_back(struct sk_buff *skb, int offset, caddr_t cp, int len)
  209. +{
  210. + int i;
  211. + if (offset < skb_headlen(skb)) {
  212. + memcpy(skb->data + offset, cp, min_t(int, skb_headlen(skb), len));
  213. + len -= skb_headlen(skb);
  214. + cp += skb_headlen(skb);
  215. + }
  216. + offset -= skb_headlen(skb);
  217. + for (i = 0; len > 0 && i < skb_shinfo(skb)->nr_frags; i++) {
  218. + if (offset < skb_shinfo(skb)->frags[i].size) {
  219. + memcpy(page_address(skb_shinfo(skb)->frags[i].page) +
  220. + skb_shinfo(skb)->frags[i].page_offset,
  221. + cp, min_t(int, skb_shinfo(skb)->frags[i].size, len));
  222. + len -= skb_shinfo(skb)->frags[i].size;
  223. + cp += skb_shinfo(skb)->frags[i].size;
  224. + }
  225. + offset -= skb_shinfo(skb)->frags[i].size;
  226. + }
  227. +}
  228. +
  229. +void
  230. +crypto_copyback(int flags, caddr_t buf, int off, int size, caddr_t in)
  231. +{
  232. +
  233. + if ((flags & CRYPTO_F_SKBUF) != 0)
  234. + skb_copy_bits_back((struct sk_buff *)buf, off, in, size);
  235. + else if ((flags & CRYPTO_F_IOV) != 0)
  236. + cuio_copyback((struct uio *)buf, off, size, in);
  237. + else
  238. + bcopy(in, buf + off, size);
  239. +}
  240. +
  241. +void
  242. +crypto_copydata(int flags, caddr_t buf, int off, int size, caddr_t out)
  243. +{
  244. +
  245. + if ((flags & CRYPTO_F_SKBUF) != 0)
  246. + skb_copy_bits((struct sk_buff *)buf, off, out, size);
  247. + else if ((flags & CRYPTO_F_IOV) != 0)
  248. + cuio_copydata((struct uio *)buf, off, size, out);
  249. + else
  250. + bcopy(buf + off, out, size);
  251. +}
  252. +
  253. +int
  254. +crypto_apply(int flags, caddr_t buf, int off, int len,
  255. + int (*f)(void *, void *, u_int), void *arg)
  256. +{
  257. +#if 0
  258. + int error;
  259. +
  260. + if ((flags & CRYPTO_F_SKBUF) != 0)
  261. + error = XXXXXX((struct mbuf *)buf, off, len, f, arg);
  262. + else if ((flags & CRYPTO_F_IOV) != 0)
  263. + error = cuio_apply((struct uio *)buf, off, len, f, arg);
  264. + else
  265. + error = (*f)(arg, buf + off, len);
  266. + return (error);
  267. +#else
  268. + KASSERT(0, ("crypto_apply not implemented!\n"));
  269. +#endif
  270. + return 0;
  271. +}
  272. +
  273. +EXPORT_SYMBOL(crypto_copyback);
  274. +EXPORT_SYMBOL(crypto_copydata);
  275. +EXPORT_SYMBOL(crypto_apply);
  276. +
  277. diff -Nur linux-2.6.30.orig/crypto/ocf/crypto.c linux-2.6.30/crypto/ocf/crypto.c
  278. --- linux-2.6.30.orig/crypto/ocf/crypto.c 1970-01-01 01:00:00.000000000 +0100
  279. +++ linux-2.6.30/crypto/ocf/crypto.c 2009-06-11 10:55:27.000000000 +0200
  280. @@ -0,0 +1,1741 @@
  281. +/*-
  282. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  283. + * Copyright (C) 2006-2007 David McCullough
  284. + * Copyright (C) 2004-2005 Intel Corporation.
  285. + * The license and original author are listed below.
  286. + *
  287. + * Redistribution and use in source and binary forms, with or without
  288. + * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
  289. + *
  290. + * modification, are permitted provided that the following conditions
  291. + * are met:
  292. + * 1. Redistributions of source code must retain the above copyright
  293. + * notice, this list of conditions and the following disclaimer.
  294. + * 2. Redistributions in binary form must reproduce the above copyright
  295. + * notice, this list of conditions and the following disclaimer in the
  296. + * documentation and/or other materials provided with the distribution.
  297. + *
  298. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  299. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  300. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  301. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  302. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  303. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  304. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  305. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  306. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  307. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  308. + */
  309. +
  310. +#if 0
  311. +#include <sys/cdefs.h>
  312. +__FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.27 2007/03/21 03:42:51 sam Exp $");
  313. +#endif
  314. +
  315. +/*
  316. + * Cryptographic Subsystem.
  317. + *
  318. + * This code is derived from the Openbsd Cryptographic Framework (OCF)
  319. + * that has the copyright shown below. Very little of the original
  320. + * code remains.
  321. + */
  322. +/*-
  323. + * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
  324. + *
  325. + * This code was written by Angelos D. Keromytis in Athens, Greece, in
  326. + * February 2000. Network Security Technologies Inc. (NSTI) kindly
  327. + * supported the development of this code.
  328. + *
  329. + * Copyright (c) 2000, 2001 Angelos D. Keromytis
  330. + *
  331. + * Permission to use, copy, and modify this software with or without fee
  332. + * is hereby granted, provided that this entire notice is included in
  333. + * all source code copies of any software which is or includes a copy or
  334. + * modification of this software.
  335. + *
  336. + * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  337. + * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  338. + * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  339. + * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  340. + * PURPOSE.
  341. + *
  342. +__FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.16 2005/01/07 02:29:16 imp Exp $");
  343. + */
  344. +
  345. +
  346. +#ifndef AUTOCONF_INCLUDED
  347. +#include <linux/config.h>
  348. +#endif
  349. +#include <linux/module.h>
  350. +#include <linux/init.h>
  351. +#include <linux/list.h>
  352. +#include <linux/slab.h>
  353. +#include <linux/wait.h>
  354. +#include <linux/sched.h>
  355. +#include <linux/spinlock.h>
  356. +#include <linux/version.h>
  357. +#include <cryptodev.h>
  358. +
  359. +/*
  360. + * keep track of whether or not we have been initialised, a big
  361. + * issue if we are linked into the kernel and a driver gets started before
  362. + * us
  363. + */
  364. +static int crypto_initted = 0;
  365. +
  366. +/*
  367. + * Crypto drivers register themselves by allocating a slot in the
  368. + * crypto_drivers table with crypto_get_driverid() and then registering
  369. + * each algorithm they support with crypto_register() and crypto_kregister().
  370. + */
  371. +
  372. +/*
  373. + * lock on driver table
  374. + * we track its state as spin_is_locked does not do anything on non-SMP boxes
  375. + */
  376. +static spinlock_t crypto_drivers_lock;
  377. +static int crypto_drivers_locked; /* for non-SMP boxes */
  378. +
  379. +#define CRYPTO_DRIVER_LOCK() \
  380. + ({ \
  381. + spin_lock_irqsave(&crypto_drivers_lock, d_flags); \
  382. + crypto_drivers_locked = 1; \
  383. + dprintk("%s,%d: DRIVER_LOCK()\n", __FILE__, __LINE__); \
  384. + })
  385. +#define CRYPTO_DRIVER_UNLOCK() \
  386. + ({ \
  387. + dprintk("%s,%d: DRIVER_UNLOCK()\n", __FILE__, __LINE__); \
  388. + crypto_drivers_locked = 0; \
  389. + spin_unlock_irqrestore(&crypto_drivers_lock, d_flags); \
  390. + })
  391. +#define CRYPTO_DRIVER_ASSERT() \
  392. + ({ \
  393. + if (!crypto_drivers_locked) { \
  394. + dprintk("%s,%d: DRIVER_ASSERT!\n", __FILE__, __LINE__); \
  395. + } \
  396. + })
  397. +
  398. +/*
  399. + * Crypto device/driver capabilities structure.
  400. + *
  401. + * Synchronization:
  402. + * (d) - protected by CRYPTO_DRIVER_LOCK()
  403. + * (q) - protected by CRYPTO_Q_LOCK()
  404. + * Not tagged fields are read-only.
  405. + */
  406. +struct cryptocap {
  407. + device_t cc_dev; /* (d) device/driver */
  408. + u_int32_t cc_sessions; /* (d) # of sessions */
  409. + u_int32_t cc_koperations; /* (d) # os asym operations */
  410. + /*
  411. + * Largest possible operator length (in bits) for each type of
  412. + * encryption algorithm. XXX not used
  413. + */
  414. + u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
  415. + u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
  416. + u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
  417. +
  418. + int cc_flags; /* (d) flags */
  419. +#define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
  420. + int cc_qblocked; /* (q) symmetric q blocked */
  421. + int cc_kqblocked; /* (q) asymmetric q blocked */
  422. +};
  423. +static struct cryptocap *crypto_drivers = NULL;
  424. +static int crypto_drivers_num = 0;
  425. +
  426. +/*
  427. + * There are two queues for crypto requests; one for symmetric (e.g.
  428. + * cipher) operations and one for asymmetric (e.g. MOD)operations.
  429. + * A single mutex is used to lock access to both queues. We could
  430. + * have one per-queue but having one simplifies handling of block/unblock
  431. + * operations.
  432. + */
  433. +static int crp_sleep = 0;
  434. +static LIST_HEAD(crp_q); /* request queues */
  435. +static LIST_HEAD(crp_kq);
  436. +
  437. +static spinlock_t crypto_q_lock;
  438. +
  439. +int crypto_all_qblocked = 0; /* protect with Q_LOCK */
  440. +module_param(crypto_all_qblocked, int, 0444);
  441. +MODULE_PARM_DESC(crypto_all_qblocked, "Are all crypto queues blocked");
  442. +
  443. +int crypto_all_kqblocked = 0; /* protect with Q_LOCK */
  444. +module_param(crypto_all_kqblocked, int, 0444);
  445. +MODULE_PARM_DESC(crypto_all_kqblocked, "Are all asym crypto queues blocked");
  446. +
  447. +#define CRYPTO_Q_LOCK() \
  448. + ({ \
  449. + spin_lock_irqsave(&crypto_q_lock, q_flags); \
  450. + dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
  451. + })
  452. +#define CRYPTO_Q_UNLOCK() \
  453. + ({ \
  454. + dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
  455. + spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
  456. + })
  457. +
  458. +/*
  459. + * There are two queues for processing completed crypto requests; one
  460. + * for the symmetric and one for the asymmetric ops. We only need one
  461. + * but have two to avoid type futzing (cryptop vs. cryptkop). A single
  462. + * mutex is used to lock access to both queues. Note that this lock
  463. + * must be separate from the lock on request queues to insure driver
  464. + * callbacks don't generate lock order reversals.
  465. + */
  466. +static LIST_HEAD(crp_ret_q); /* callback queues */
  467. +static LIST_HEAD(crp_ret_kq);
  468. +
  469. +static spinlock_t crypto_ret_q_lock;
  470. +#define CRYPTO_RETQ_LOCK() \
  471. + ({ \
  472. + spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
  473. + dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
  474. + })
  475. +#define CRYPTO_RETQ_UNLOCK() \
  476. + ({ \
  477. + dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
  478. + spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
  479. + })
  480. +#define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
  481. +
  482. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  483. +static kmem_cache_t *cryptop_zone;
  484. +static kmem_cache_t *cryptodesc_zone;
  485. +#else
  486. +static struct kmem_cache *cryptop_zone;
  487. +static struct kmem_cache *cryptodesc_zone;
  488. +#endif
  489. +
  490. +#define debug crypto_debug
  491. +int crypto_debug = 0;
  492. +module_param(crypto_debug, int, 0644);
  493. +MODULE_PARM_DESC(crypto_debug, "Enable debug");
  494. +EXPORT_SYMBOL(crypto_debug);
  495. +
  496. +/*
  497. + * Maximum number of outstanding crypto requests before we start
  498. + * failing requests. We need this to prevent DOS when too many
  499. + * requests are arriving for us to keep up. Otherwise we will
  500. + * run the system out of memory. Since crypto is slow, we are
  501. + * usually the bottleneck that needs to say, enough is enough.
  502. + *
  503. + * We cannot print errors when this condition occurs, we are already too
  504. + * slow, printing anything will just kill us
  505. + */
  506. +
  507. +static int crypto_q_cnt = 0;
  508. +module_param(crypto_q_cnt, int, 0444);
  509. +MODULE_PARM_DESC(crypto_q_cnt,
  510. + "Current number of outstanding crypto requests");
  511. +
  512. +static int crypto_q_max = 1000;
  513. +module_param(crypto_q_max, int, 0644);
  514. +MODULE_PARM_DESC(crypto_q_max,
  515. + "Maximum number of outstanding crypto requests");
  516. +
  517. +#define bootverbose crypto_verbose
  518. +static int crypto_verbose = 0;
  519. +module_param(crypto_verbose, int, 0644);
  520. +MODULE_PARM_DESC(crypto_verbose,
  521. + "Enable verbose crypto startup");
  522. +
  523. +int crypto_usercrypto = 1; /* userland may do crypto reqs */
  524. +module_param(crypto_usercrypto, int, 0644);
  525. +MODULE_PARM_DESC(crypto_usercrypto,
  526. + "Enable/disable user-mode access to crypto support");
  527. +
  528. +int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
  529. +module_param(crypto_userasymcrypto, int, 0644);
  530. +MODULE_PARM_DESC(crypto_userasymcrypto,
  531. + "Enable/disable user-mode access to asymmetric crypto support");
  532. +
  533. +int crypto_devallowsoft = 0; /* only use hardware crypto */
  534. +module_param(crypto_devallowsoft, int, 0644);
  535. +MODULE_PARM_DESC(crypto_devallowsoft,
  536. + "Enable/disable use of software crypto support");
  537. +
  538. +static pid_t cryptoproc = (pid_t) -1;
  539. +static struct completion cryptoproc_exited;
  540. +static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait);
  541. +static pid_t cryptoretproc = (pid_t) -1;
  542. +static struct completion cryptoretproc_exited;
  543. +static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait);
  544. +
  545. +static int crypto_proc(void *arg);
  546. +static int crypto_ret_proc(void *arg);
  547. +static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
  548. +static int crypto_kinvoke(struct cryptkop *krp, int flags);
  549. +static void crypto_exit(void);
  550. +static int crypto_init(void);
  551. +
  552. +static struct cryptostats cryptostats;
  553. +
  554. +static struct cryptocap *
  555. +crypto_checkdriver(u_int32_t hid)
  556. +{
  557. + if (crypto_drivers == NULL)
  558. + return NULL;
  559. + return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
  560. +}
  561. +
  562. +/*
  563. + * Compare a driver's list of supported algorithms against another
  564. + * list; return non-zero if all algorithms are supported.
  565. + */
  566. +static int
  567. +driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
  568. +{
  569. + const struct cryptoini *cr;
  570. +
  571. + /* See if all the algorithms are supported. */
  572. + for (cr = cri; cr; cr = cr->cri_next)
  573. + if (cap->cc_alg[cr->cri_alg] == 0)
  574. + return 0;
  575. + return 1;
  576. +}
  577. +
  578. +/*
  579. + * Select a driver for a new session that supports the specified
  580. + * algorithms and, optionally, is constrained according to the flags.
  581. + * The algorithm we use here is pretty stupid; just use the
  582. + * first driver that supports all the algorithms we need. If there
  583. + * are multiple drivers we choose the driver with the fewest active
  584. + * sessions. We prefer hardware-backed drivers to software ones.
  585. + *
  586. + * XXX We need more smarts here (in real life too, but that's
  587. + * XXX another story altogether).
  588. + */
  589. +static struct cryptocap *
  590. +crypto_select_driver(const struct cryptoini *cri, int flags)
  591. +{
  592. + struct cryptocap *cap, *best;
  593. + int match, hid;
  594. +
  595. + CRYPTO_DRIVER_ASSERT();
  596. +
  597. + /*
  598. + * Look first for hardware crypto devices if permitted.
  599. + */
  600. + if (flags & CRYPTOCAP_F_HARDWARE)
  601. + match = CRYPTOCAP_F_HARDWARE;
  602. + else
  603. + match = CRYPTOCAP_F_SOFTWARE;
  604. + best = NULL;
  605. +again:
  606. + for (hid = 0; hid < crypto_drivers_num; hid++) {
  607. + cap = &crypto_drivers[hid];
  608. + /*
  609. + * If it's not initialized, is in the process of
  610. + * going away, or is not appropriate (hardware
  611. + * or software based on match), then skip.
  612. + */
  613. + if (cap->cc_dev == NULL ||
  614. + (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
  615. + (cap->cc_flags & match) == 0)
  616. + continue;
  617. +
  618. + /* verify all the algorithms are supported. */
  619. + if (driver_suitable(cap, cri)) {
  620. + if (best == NULL ||
  621. + cap->cc_sessions < best->cc_sessions)
  622. + best = cap;
  623. + }
  624. + }
  625. + if (best != NULL)
  626. + return best;
  627. + if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
  628. + /* sort of an Algol 68-style for loop */
  629. + match = CRYPTOCAP_F_SOFTWARE;
  630. + goto again;
  631. + }
  632. + return best;
  633. +}
  634. +
  635. +/*
  636. + * Create a new session. The crid argument specifies a crypto
  637. + * driver to use or constraints on a driver to select (hardware
  638. + * only, software only, either). Whatever driver is selected
  639. + * must be capable of the requested crypto algorithms.
  640. + */
  641. +int
  642. +crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
  643. +{
  644. + struct cryptocap *cap;
  645. + u_int32_t hid, lid;
  646. + int err;
  647. + unsigned long d_flags;
  648. +
  649. + CRYPTO_DRIVER_LOCK();
  650. + if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  651. + /*
  652. + * Use specified driver; verify it is capable.
  653. + */
  654. + cap = crypto_checkdriver(crid);
  655. + if (cap != NULL && !driver_suitable(cap, cri))
  656. + cap = NULL;
  657. + } else {
  658. + /*
  659. + * No requested driver; select based on crid flags.
  660. + */
  661. + cap = crypto_select_driver(cri, crid);
  662. + /*
  663. + * if NULL then can't do everything in one session.
  664. + * XXX Fix this. We need to inject a "virtual" session
  665. + * XXX layer right about here.
  666. + */
  667. + }
  668. + if (cap != NULL) {
  669. + /* Call the driver initialization routine. */
  670. + hid = cap - crypto_drivers;
  671. + lid = hid; /* Pass the driver ID. */
  672. + cap->cc_sessions++;
  673. + CRYPTO_DRIVER_UNLOCK();
  674. + err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
  675. + CRYPTO_DRIVER_LOCK();
  676. + if (err == 0) {
  677. + (*sid) = (cap->cc_flags & 0xff000000)
  678. + | (hid & 0x00ffffff);
  679. + (*sid) <<= 32;
  680. + (*sid) |= (lid & 0xffffffff);
  681. + } else
  682. + cap->cc_sessions--;
  683. + } else
  684. + err = EINVAL;
  685. + CRYPTO_DRIVER_UNLOCK();
  686. + return err;
  687. +}
  688. +
  689. +static void
  690. +crypto_remove(struct cryptocap *cap)
  691. +{
  692. + CRYPTO_DRIVER_ASSERT();
  693. + if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
  694. + bzero(cap, sizeof(*cap));
  695. +}
  696. +
  697. +/*
  698. + * Delete an existing session (or a reserved session on an unregistered
  699. + * driver).
  700. + */
  701. +int
  702. +crypto_freesession(u_int64_t sid)
  703. +{
  704. + struct cryptocap *cap;
  705. + u_int32_t hid;
  706. + int err = 0;
  707. + unsigned long d_flags;
  708. +
  709. + dprintk("%s()\n", __FUNCTION__);
  710. + CRYPTO_DRIVER_LOCK();
  711. +
  712. + if (crypto_drivers == NULL) {
  713. + err = EINVAL;
  714. + goto done;
  715. + }
  716. +
  717. + /* Determine two IDs. */
  718. + hid = CRYPTO_SESID2HID(sid);
  719. +
  720. + if (hid >= crypto_drivers_num) {
  721. + dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__, hid);
  722. + err = ENOENT;
  723. + goto done;
  724. + }
  725. + cap = &crypto_drivers[hid];
  726. +
  727. + if (cap->cc_dev) {
  728. + CRYPTO_DRIVER_UNLOCK();
  729. + /* Call the driver cleanup routine, if available, unlocked. */
  730. + err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
  731. + CRYPTO_DRIVER_LOCK();
  732. + }
  733. +
  734. + if (cap->cc_sessions)
  735. + cap->cc_sessions--;
  736. +
  737. + if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
  738. + crypto_remove(cap);
  739. +
  740. +done:
  741. + CRYPTO_DRIVER_UNLOCK();
  742. + return err;
  743. +}
  744. +
  745. +/*
  746. + * Return an unused driver id. Used by drivers prior to registering
  747. + * support for the algorithms they handle.
  748. + */
  749. +int32_t
  750. +crypto_get_driverid(device_t dev, int flags)
  751. +{
  752. + struct cryptocap *newdrv;
  753. + int i;
  754. + unsigned long d_flags;
  755. +
  756. + if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  757. + printf("%s: no flags specified when registering driver\n",
  758. + device_get_nameunit(dev));
  759. + return -1;
  760. + }
  761. +
  762. + CRYPTO_DRIVER_LOCK();
  763. +
  764. + for (i = 0; i < crypto_drivers_num; i++) {
  765. + if (crypto_drivers[i].cc_dev == NULL &&
  766. + (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
  767. + break;
  768. + }
  769. + }
  770. +
  771. + /* Out of entries, allocate some more. */
  772. + if (i == crypto_drivers_num) {
  773. + /* Be careful about wrap-around. */
  774. + if (2 * crypto_drivers_num <= crypto_drivers_num) {
  775. + CRYPTO_DRIVER_UNLOCK();
  776. + printk("crypto: driver count wraparound!\n");
  777. + return -1;
  778. + }
  779. +
  780. + newdrv = kmalloc(2 * crypto_drivers_num * sizeof(struct cryptocap),
  781. + GFP_KERNEL);
  782. + if (newdrv == NULL) {
  783. + CRYPTO_DRIVER_UNLOCK();
  784. + printk("crypto: no space to expand driver table!\n");
  785. + return -1;
  786. + }
  787. +
  788. + memcpy(newdrv, crypto_drivers,
  789. + crypto_drivers_num * sizeof(struct cryptocap));
  790. + memset(&newdrv[crypto_drivers_num], 0,
  791. + crypto_drivers_num * sizeof(struct cryptocap));
  792. +
  793. + crypto_drivers_num *= 2;
  794. +
  795. + kfree(crypto_drivers);
  796. + crypto_drivers = newdrv;
  797. + }
  798. +
  799. + /* NB: state is zero'd on free */
  800. + crypto_drivers[i].cc_sessions = 1; /* Mark */
  801. + crypto_drivers[i].cc_dev = dev;
  802. + crypto_drivers[i].cc_flags = flags;
  803. + if (bootverbose)
  804. + printf("crypto: assign %s driver id %u, flags %u\n",
  805. + device_get_nameunit(dev), i, flags);
  806. +
  807. + CRYPTO_DRIVER_UNLOCK();
  808. +
  809. + return i;
  810. +}
  811. +
  812. +/*
  813. + * Lookup a driver by name. We match against the full device
  814. + * name and unit, and against just the name. The latter gives
  815. + * us a simple widlcarding by device name. On success return the
  816. + * driver/hardware identifier; otherwise return -1.
  817. + */
  818. +int
  819. +crypto_find_driver(const char *match)
  820. +{
  821. + int i, len = strlen(match);
  822. + unsigned long d_flags;
  823. +
  824. + CRYPTO_DRIVER_LOCK();
  825. + for (i = 0; i < crypto_drivers_num; i++) {
  826. + device_t dev = crypto_drivers[i].cc_dev;
  827. + if (dev == NULL ||
  828. + (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
  829. + continue;
  830. + if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
  831. + strncmp(match, device_get_name(dev), len) == 0)
  832. + break;
  833. + }
  834. + CRYPTO_DRIVER_UNLOCK();
  835. + return i < crypto_drivers_num ? i : -1;
  836. +}
  837. +
  838. +/*
  839. + * Return the device_t for the specified driver or NULL
  840. + * if the driver identifier is invalid.
  841. + */
  842. +device_t
  843. +crypto_find_device_byhid(int hid)
  844. +{
  845. + struct cryptocap *cap = crypto_checkdriver(hid);
  846. + return cap != NULL ? cap->cc_dev : NULL;
  847. +}
  848. +
  849. +/*
  850. + * Return the device/driver capabilities.
  851. + */
  852. +int
  853. +crypto_getcaps(int hid)
  854. +{
  855. + struct cryptocap *cap = crypto_checkdriver(hid);
  856. + return cap != NULL ? cap->cc_flags : 0;
  857. +}
  858. +
  859. +/*
  860. + * Register support for a key-related algorithm. This routine
  861. + * is called once for each algorithm supported a driver.
  862. + */
  863. +int
  864. +crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
  865. +{
  866. + struct cryptocap *cap;
  867. + int err;
  868. + unsigned long d_flags;
  869. +
  870. + dprintk("%s()\n", __FUNCTION__);
  871. + CRYPTO_DRIVER_LOCK();
  872. +
  873. + cap = crypto_checkdriver(driverid);
  874. + if (cap != NULL &&
  875. + (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
  876. + /*
  877. + * XXX Do some performance testing to determine placing.
  878. + * XXX We probably need an auxiliary data structure that
  879. + * XXX describes relative performances.
  880. + */
  881. +
  882. + cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
  883. + if (bootverbose)
  884. + printf("crypto: %s registers key alg %u flags %u\n"
  885. + , device_get_nameunit(cap->cc_dev)
  886. + , kalg
  887. + , flags
  888. + );
  889. + err = 0;
  890. + } else
  891. + err = EINVAL;
  892. +
  893. + CRYPTO_DRIVER_UNLOCK();
  894. + return err;
  895. +}
  896. +
  897. +/*
  898. + * Register support for a non-key-related algorithm. This routine
  899. + * is called once for each such algorithm supported by a driver.
  900. + */
  901. +int
  902. +crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
  903. + u_int32_t flags)
  904. +{
  905. + struct cryptocap *cap;
  906. + int err;
  907. + unsigned long d_flags;
  908. +
  909. + dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__,
  910. + driverid, alg, maxoplen, flags);
  911. +
  912. + CRYPTO_DRIVER_LOCK();
  913. +
  914. + cap = crypto_checkdriver(driverid);
  915. + /* NB: algorithms are in the range [1..max] */
  916. + if (cap != NULL &&
  917. + (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
  918. + /*
  919. + * XXX Do some performance testing to determine placing.
  920. + * XXX We probably need an auxiliary data structure that
  921. + * XXX describes relative performances.
  922. + */
  923. +
  924. + cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
  925. + cap->cc_max_op_len[alg] = maxoplen;
  926. + if (bootverbose)
  927. + printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
  928. + , device_get_nameunit(cap->cc_dev)
  929. + , alg
  930. + , flags
  931. + , maxoplen
  932. + );
  933. + cap->cc_sessions = 0; /* Unmark */
  934. + err = 0;
  935. + } else
  936. + err = EINVAL;
  937. +
  938. + CRYPTO_DRIVER_UNLOCK();
  939. + return err;
  940. +}
  941. +
  942. +static void
  943. +driver_finis(struct cryptocap *cap)
  944. +{
  945. + u_int32_t ses, kops;
  946. +
  947. + CRYPTO_DRIVER_ASSERT();
  948. +
  949. + ses = cap->cc_sessions;
  950. + kops = cap->cc_koperations;
  951. + bzero(cap, sizeof(*cap));
  952. + if (ses != 0 || kops != 0) {
  953. + /*
  954. + * If there are pending sessions,
  955. + * just mark as invalid.
  956. + */
  957. + cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
  958. + cap->cc_sessions = ses;
  959. + cap->cc_koperations = kops;
  960. + }
  961. +}
  962. +
  963. +/*
  964. + * Unregister a crypto driver. If there are pending sessions using it,
  965. + * leave enough information around so that subsequent calls using those
  966. + * sessions will correctly detect the driver has been unregistered and
  967. + * reroute requests.
  968. + */
  969. +int
  970. +crypto_unregister(u_int32_t driverid, int alg)
  971. +{
  972. + struct cryptocap *cap;
  973. + int i, err;
  974. + unsigned long d_flags;
  975. +
  976. + dprintk("%s()\n", __FUNCTION__);
  977. + CRYPTO_DRIVER_LOCK();
  978. +
  979. + cap = crypto_checkdriver(driverid);
  980. + if (cap != NULL &&
  981. + (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
  982. + cap->cc_alg[alg] != 0) {
  983. + cap->cc_alg[alg] = 0;
  984. + cap->cc_max_op_len[alg] = 0;
  985. +
  986. + /* Was this the last algorithm ? */
  987. + for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
  988. + if (cap->cc_alg[i] != 0)
  989. + break;
  990. +
  991. + if (i == CRYPTO_ALGORITHM_MAX + 1)
  992. + driver_finis(cap);
  993. + err = 0;
  994. + } else
  995. + err = EINVAL;
  996. + CRYPTO_DRIVER_UNLOCK();
  997. + return err;
  998. +}
  999. +
  1000. +/*
  1001. + * Unregister all algorithms associated with a crypto driver.
  1002. + * If there are pending sessions using it, leave enough information
  1003. + * around so that subsequent calls using those sessions will
  1004. + * correctly detect the driver has been unregistered and reroute
  1005. + * requests.
  1006. + */
  1007. +int
  1008. +crypto_unregister_all(u_int32_t driverid)
  1009. +{
  1010. + struct cryptocap *cap;
  1011. + int err;
  1012. + unsigned long d_flags;
  1013. +
  1014. + dprintk("%s()\n", __FUNCTION__);
  1015. + CRYPTO_DRIVER_LOCK();
  1016. + cap = crypto_checkdriver(driverid);
  1017. + if (cap != NULL) {
  1018. + driver_finis(cap);
  1019. + err = 0;
  1020. + } else
  1021. + err = EINVAL;
  1022. + CRYPTO_DRIVER_UNLOCK();
  1023. +
  1024. + return err;
  1025. +}
  1026. +
  1027. +/*
  1028. + * Clear blockage on a driver. The what parameter indicates whether
  1029. + * the driver is now ready for cryptop's and/or cryptokop's.
  1030. + */
  1031. +int
  1032. +crypto_unblock(u_int32_t driverid, int what)
  1033. +{
  1034. + struct cryptocap *cap;
  1035. + int err;
  1036. + unsigned long q_flags;
  1037. +
  1038. + CRYPTO_Q_LOCK();
  1039. + cap = crypto_checkdriver(driverid);
  1040. + if (cap != NULL) {
  1041. + if (what & CRYPTO_SYMQ) {
  1042. + cap->cc_qblocked = 0;
  1043. + crypto_all_qblocked = 0;
  1044. + }
  1045. + if (what & CRYPTO_ASYMQ) {
  1046. + cap->cc_kqblocked = 0;
  1047. + crypto_all_kqblocked = 0;
  1048. + }
  1049. + if (crp_sleep)
  1050. + wake_up_interruptible(&cryptoproc_wait);
  1051. + err = 0;
  1052. + } else
  1053. + err = EINVAL;
  1054. + CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
  1055. +
  1056. + return err;
  1057. +}
  1058. +
  1059. +/*
  1060. + * Add a crypto request to a queue, to be processed by the kernel thread.
  1061. + */
  1062. +int
  1063. +crypto_dispatch(struct cryptop *crp)
  1064. +{
  1065. + struct cryptocap *cap;
  1066. + int result = -1;
  1067. + unsigned long q_flags;
  1068. +
  1069. + dprintk("%s()\n", __FUNCTION__);
  1070. +
  1071. + cryptostats.cs_ops++;
  1072. +
  1073. + CRYPTO_Q_LOCK();
  1074. + if (crypto_q_cnt >= crypto_q_max) {
  1075. + CRYPTO_Q_UNLOCK();
  1076. + cryptostats.cs_drops++;
  1077. + return ENOMEM;
  1078. + }
  1079. + crypto_q_cnt++;
  1080. +
  1081. + /*
  1082. + * Caller marked the request to be processed immediately; dispatch
  1083. + * it directly to the driver unless the driver is currently blocked.
  1084. + */
  1085. + if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
  1086. + int hid = CRYPTO_SESID2HID(crp->crp_sid);
  1087. + cap = crypto_checkdriver(hid);
  1088. + /* Driver cannot disappear when there is an active session. */
  1089. + KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
  1090. + if (!cap->cc_qblocked) {
  1091. + crypto_all_qblocked = 0;
  1092. + crypto_drivers[hid].cc_qblocked = 1;
  1093. + CRYPTO_Q_UNLOCK();
  1094. + result = crypto_invoke(cap, crp, 0);
  1095. + CRYPTO_Q_LOCK();
  1096. + if (result != ERESTART)
  1097. + crypto_drivers[hid].cc_qblocked = 0;
  1098. + }
  1099. + }
  1100. + if (result == ERESTART) {
  1101. + /*
  1102. + * The driver ran out of resources, mark the
  1103. + * driver ``blocked'' for cryptop's and put
  1104. + * the request back in the queue. It would
  1105. + * best to put the request back where we got
  1106. + * it but that's hard so for now we put it
  1107. + * at the front. This should be ok; putting
  1108. + * it at the end does not work.
  1109. + */
  1110. + list_add(&crp->crp_next, &crp_q);
  1111. + cryptostats.cs_blocks++;
  1112. + } else if (result == -1) {
  1113. + TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
  1114. + }
  1115. + if (crp_sleep)
  1116. + wake_up_interruptible(&cryptoproc_wait);
  1117. + CRYPTO_Q_UNLOCK();
  1118. + return 0;
  1119. +}
  1120. +
  1121. +/*
  1122. + * Add an asymetric crypto request to a queue,
  1123. + * to be processed by the kernel thread.
  1124. + */
  1125. +int
  1126. +crypto_kdispatch(struct cryptkop *krp)
  1127. +{
  1128. + int error;
  1129. + unsigned long q_flags;
  1130. +
  1131. + cryptostats.cs_kops++;
  1132. +
  1133. + error = crypto_kinvoke(krp, krp->krp_crid);
  1134. + if (error == ERESTART) {
  1135. + CRYPTO_Q_LOCK();
  1136. + TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
  1137. + if (crp_sleep)
  1138. + wake_up_interruptible(&cryptoproc_wait);
  1139. + CRYPTO_Q_UNLOCK();
  1140. + error = 0;
  1141. + }
  1142. + return error;
  1143. +}
  1144. +
  1145. +/*
  1146. + * Verify a driver is suitable for the specified operation.
  1147. + */
  1148. +static __inline int
  1149. +kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
  1150. +{
  1151. + return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
  1152. +}
  1153. +
  1154. +/*
  1155. + * Select a driver for an asym operation. The driver must
  1156. + * support the necessary algorithm. The caller can constrain
  1157. + * which device is selected with the flags parameter. The
  1158. + * algorithm we use here is pretty stupid; just use the first
  1159. + * driver that supports the algorithms we need. If there are
  1160. + * multiple suitable drivers we choose the driver with the
  1161. + * fewest active operations. We prefer hardware-backed
  1162. + * drivers to software ones when either may be used.
  1163. + */
  1164. +static struct cryptocap *
  1165. +crypto_select_kdriver(const struct cryptkop *krp, int flags)
  1166. +{
  1167. + struct cryptocap *cap, *best, *blocked;
  1168. + int match, hid;
  1169. +
  1170. + CRYPTO_DRIVER_ASSERT();
  1171. +
  1172. + /*
  1173. + * Look first for hardware crypto devices if permitted.
  1174. + */
  1175. + if (flags & CRYPTOCAP_F_HARDWARE)
  1176. + match = CRYPTOCAP_F_HARDWARE;
  1177. + else
  1178. + match = CRYPTOCAP_F_SOFTWARE;
  1179. + best = NULL;
  1180. + blocked = NULL;
  1181. +again:
  1182. + for (hid = 0; hid < crypto_drivers_num; hid++) {
  1183. + cap = &crypto_drivers[hid];
  1184. + /*
  1185. + * If it's not initialized, is in the process of
  1186. + * going away, or is not appropriate (hardware
  1187. + * or software based on match), then skip.
  1188. + */
  1189. + if (cap->cc_dev == NULL ||
  1190. + (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
  1191. + (cap->cc_flags & match) == 0)
  1192. + continue;
  1193. +
  1194. + /* verify all the algorithms are supported. */
  1195. + if (kdriver_suitable(cap, krp)) {
  1196. + if (best == NULL ||
  1197. + cap->cc_koperations < best->cc_koperations)
  1198. + best = cap;
  1199. + }
  1200. + }
  1201. + if (best != NULL)
  1202. + return best;
  1203. + if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
  1204. + /* sort of an Algol 68-style for loop */
  1205. + match = CRYPTOCAP_F_SOFTWARE;
  1206. + goto again;
  1207. + }
  1208. + return best;
  1209. +}
  1210. +
  1211. +/*
  1212. + * Dispatch an assymetric crypto request.
  1213. + */
  1214. +static int
  1215. +crypto_kinvoke(struct cryptkop *krp, int crid)
  1216. +{
  1217. + struct cryptocap *cap = NULL;
  1218. + int error;
  1219. + unsigned long d_flags;
  1220. +
  1221. + KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
  1222. + KASSERT(krp->krp_callback != NULL,
  1223. + ("%s: krp->crp_callback == NULL", __func__));
  1224. +
  1225. + CRYPTO_DRIVER_LOCK();
  1226. + if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  1227. + cap = crypto_checkdriver(crid);
  1228. + if (cap != NULL) {
  1229. + /*
  1230. + * Driver present, it must support the necessary
  1231. + * algorithm and, if s/w drivers are excluded,
  1232. + * it must be registered as hardware-backed.
  1233. + */
  1234. + if (!kdriver_suitable(cap, krp) ||
  1235. + (!crypto_devallowsoft &&
  1236. + (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
  1237. + cap = NULL;
  1238. + }
  1239. + } else {
  1240. + /*
  1241. + * No requested driver; select based on crid flags.
  1242. + */
  1243. + if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
  1244. + crid &= ~CRYPTOCAP_F_SOFTWARE;
  1245. + cap = crypto_select_kdriver(krp, crid);
  1246. + }
  1247. + if (cap != NULL && !cap->cc_kqblocked) {
  1248. + krp->krp_hid = cap - crypto_drivers;
  1249. + cap->cc_koperations++;
  1250. + CRYPTO_DRIVER_UNLOCK();
  1251. + error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
  1252. + CRYPTO_DRIVER_LOCK();
  1253. + if (error == ERESTART) {
  1254. + cap->cc_koperations--;
  1255. + CRYPTO_DRIVER_UNLOCK();
  1256. + return (error);
  1257. + }
  1258. + /* return the actual device used */
  1259. + krp->krp_crid = krp->krp_hid;
  1260. + } else {
  1261. + /*
  1262. + * NB: cap is !NULL if device is blocked; in
  1263. + * that case return ERESTART so the operation
  1264. + * is resubmitted if possible.
  1265. + */
  1266. + error = (cap == NULL) ? ENODEV : ERESTART;
  1267. + }
  1268. + CRYPTO_DRIVER_UNLOCK();
  1269. +
  1270. + if (error) {
  1271. + krp->krp_status = error;
  1272. + crypto_kdone(krp);
  1273. + }
  1274. + return 0;
  1275. +}
  1276. +
  1277. +
  1278. +/*
  1279. + * Dispatch a crypto request to the appropriate crypto devices.
  1280. + */
  1281. +static int
  1282. +crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
  1283. +{
  1284. + KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
  1285. + KASSERT(crp->crp_callback != NULL,
  1286. + ("%s: crp->crp_callback == NULL", __func__));
  1287. + KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
  1288. +
  1289. + dprintk("%s()\n", __FUNCTION__);
  1290. +
  1291. +#ifdef CRYPTO_TIMING
  1292. + if (crypto_timing)
  1293. + crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
  1294. +#endif
  1295. + if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
  1296. + struct cryptodesc *crd;
  1297. + u_int64_t nid;
  1298. +
  1299. + /*
  1300. + * Driver has unregistered; migrate the session and return
  1301. + * an error to the caller so they'll resubmit the op.
  1302. + *
  1303. + * XXX: What if there are more already queued requests for this
  1304. + * session?
  1305. + */
  1306. + crypto_freesession(crp->crp_sid);
  1307. +
  1308. + for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
  1309. + crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
  1310. +
  1311. + /* XXX propagate flags from initial session? */
  1312. + if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
  1313. + CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
  1314. + crp->crp_sid = nid;
  1315. +
  1316. + crp->crp_etype = EAGAIN;
  1317. + crypto_done(crp);
  1318. + return 0;
  1319. + } else {
  1320. + /*
  1321. + * Invoke the driver to process the request.
  1322. + */
  1323. + return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
  1324. + }
  1325. +}
  1326. +
  1327. +/*
  1328. + * Release a set of crypto descriptors.
  1329. + */
  1330. +void
  1331. +crypto_freereq(struct cryptop *crp)
  1332. +{
  1333. + struct cryptodesc *crd;
  1334. +
  1335. + if (crp == NULL)
  1336. + return;
  1337. +
  1338. +#ifdef DIAGNOSTIC
  1339. + {
  1340. + struct cryptop *crp2;
  1341. + unsigned long q_flags;
  1342. +
  1343. + CRYPTO_Q_LOCK();
  1344. + TAILQ_FOREACH(crp2, &crp_q, crp_next) {
  1345. + KASSERT(crp2 != crp,
  1346. + ("Freeing cryptop from the crypto queue (%p).",
  1347. + crp));
  1348. + }
  1349. + CRYPTO_Q_UNLOCK();
  1350. + CRYPTO_RETQ_LOCK();
  1351. + TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
  1352. + KASSERT(crp2 != crp,
  1353. + ("Freeing cryptop from the return queue (%p).",
  1354. + crp));
  1355. + }
  1356. + CRYPTO_RETQ_UNLOCK();
  1357. + }
  1358. +#endif
  1359. +
  1360. + while ((crd = crp->crp_desc) != NULL) {
  1361. + crp->crp_desc = crd->crd_next;
  1362. + kmem_cache_free(cryptodesc_zone, crd);
  1363. + }
  1364. + kmem_cache_free(cryptop_zone, crp);
  1365. +}
  1366. +
  1367. +/*
  1368. + * Acquire a set of crypto descriptors.
  1369. + */
  1370. +struct cryptop *
  1371. +crypto_getreq(int num)
  1372. +{
  1373. + struct cryptodesc *crd;
  1374. + struct cryptop *crp;
  1375. +
  1376. + crp = kmem_cache_alloc(cryptop_zone, SLAB_ATOMIC);
  1377. + if (crp != NULL) {
  1378. + memset(crp, 0, sizeof(*crp));
  1379. + INIT_LIST_HEAD(&crp->crp_next);
  1380. + init_waitqueue_head(&crp->crp_waitq);
  1381. + while (num--) {
  1382. + crd = kmem_cache_alloc(cryptodesc_zone, SLAB_ATOMIC);
  1383. + if (crd == NULL) {
  1384. + crypto_freereq(crp);
  1385. + return NULL;
  1386. + }
  1387. + memset(crd, 0, sizeof(*crd));
  1388. + crd->crd_next = crp->crp_desc;
  1389. + crp->crp_desc = crd;
  1390. + }
  1391. + }
  1392. + return crp;
  1393. +}
  1394. +
  1395. +/*
  1396. + * Invoke the callback on behalf of the driver.
  1397. + */
  1398. +void
  1399. +crypto_done(struct cryptop *crp)
  1400. +{
  1401. + unsigned long q_flags;
  1402. +
  1403. + dprintk("%s()\n", __FUNCTION__);
  1404. + if ((crp->crp_flags & CRYPTO_F_DONE) == 0) {
  1405. + crp->crp_flags |= CRYPTO_F_DONE;
  1406. + CRYPTO_Q_LOCK();
  1407. + crypto_q_cnt--;
  1408. + CRYPTO_Q_UNLOCK();
  1409. + } else
  1410. + printk("crypto: crypto_done op already done, flags 0x%x",
  1411. + crp->crp_flags);
  1412. + if (crp->crp_etype != 0)
  1413. + cryptostats.cs_errs++;
  1414. + /*
  1415. + * CBIMM means unconditionally do the callback immediately;
  1416. + * CBIFSYNC means do the callback immediately only if the
  1417. + * operation was done synchronously. Both are used to avoid
  1418. + * doing extraneous context switches; the latter is mostly
  1419. + * used with the software crypto driver.
  1420. + */
  1421. + if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
  1422. + ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
  1423. + (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
  1424. + /*
  1425. + * Do the callback directly. This is ok when the
  1426. + * callback routine does very little (e.g. the
  1427. + * /dev/crypto callback method just does a wakeup).
  1428. + */
  1429. + crp->crp_callback(crp);
  1430. + } else {
  1431. + unsigned long r_flags;
  1432. + /*
  1433. + * Normal case; queue the callback for the thread.
  1434. + */
  1435. + CRYPTO_RETQ_LOCK();
  1436. + if (CRYPTO_RETQ_EMPTY())
  1437. + wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
  1438. + TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
  1439. + CRYPTO_RETQ_UNLOCK();
  1440. + }
  1441. +}
  1442. +
  1443. +/*
  1444. + * Invoke the callback on behalf of the driver.
  1445. + */
  1446. +void
  1447. +crypto_kdone(struct cryptkop *krp)
  1448. +{
  1449. + struct cryptocap *cap;
  1450. + unsigned long d_flags;
  1451. +
  1452. + if ((krp->krp_flags & CRYPTO_KF_DONE) != 0)
  1453. + printk("crypto: crypto_kdone op already done, flags 0x%x",
  1454. + krp->krp_flags);
  1455. + krp->krp_flags |= CRYPTO_KF_DONE;
  1456. + if (krp->krp_status != 0)
  1457. + cryptostats.cs_kerrs++;
  1458. +
  1459. + CRYPTO_DRIVER_LOCK();
  1460. + /* XXX: What if driver is loaded in the meantime? */
  1461. + if (krp->krp_hid < crypto_drivers_num) {
  1462. + cap = &crypto_drivers[krp->krp_hid];
  1463. + cap->cc_koperations--;
  1464. + KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
  1465. + if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
  1466. + crypto_remove(cap);
  1467. + }
  1468. + CRYPTO_DRIVER_UNLOCK();
  1469. +
  1470. + /*
  1471. + * CBIMM means unconditionally do the callback immediately;
  1472. + * This is used to avoid doing extraneous context switches
  1473. + */
  1474. + if ((krp->krp_flags & CRYPTO_KF_CBIMM)) {
  1475. + /*
  1476. + * Do the callback directly. This is ok when the
  1477. + * callback routine does very little (e.g. the
  1478. + * /dev/crypto callback method just does a wakeup).
  1479. + */
  1480. + krp->krp_callback(krp);
  1481. + } else {
  1482. + unsigned long r_flags;
  1483. + /*
  1484. + * Normal case; queue the callback for the thread.
  1485. + */
  1486. + CRYPTO_RETQ_LOCK();
  1487. + if (CRYPTO_RETQ_EMPTY())
  1488. + wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
  1489. + TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
  1490. + CRYPTO_RETQ_UNLOCK();
  1491. + }
  1492. +}
  1493. +
  1494. +int
  1495. +crypto_getfeat(int *featp)
  1496. +{
  1497. + int hid, kalg, feat = 0;
  1498. + unsigned long d_flags;
  1499. +
  1500. + CRYPTO_DRIVER_LOCK();
  1501. + for (hid = 0; hid < crypto_drivers_num; hid++) {
  1502. + const struct cryptocap *cap = &crypto_drivers[hid];
  1503. +
  1504. + if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
  1505. + !crypto_devallowsoft) {
  1506. + continue;
  1507. + }
  1508. + for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
  1509. + if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
  1510. + feat |= 1 << kalg;
  1511. + }
  1512. + CRYPTO_DRIVER_UNLOCK();
  1513. + *featp = feat;
  1514. + return (0);
  1515. +}
  1516. +
  1517. +/*
  1518. + * Crypto thread, dispatches crypto requests.
  1519. + */
  1520. +static int
  1521. +crypto_proc(void *arg)
  1522. +{
  1523. + struct cryptop *crp, *submit;
  1524. + struct cryptkop *krp, *krpp;
  1525. + struct cryptocap *cap;
  1526. + u_int32_t hid;
  1527. + int result, hint;
  1528. + unsigned long q_flags;
  1529. +
  1530. + ocf_daemonize("crypto");
  1531. +
  1532. + CRYPTO_Q_LOCK();
  1533. + for (;;) {
  1534. + /*
  1535. + * we need to make sure we don't get into a busy loop with nothing
  1536. + * to do, the two crypto_all_*blocked vars help us find out when
  1537. + * we are all full and can do nothing on any driver or Q. If so we
  1538. + * wait for an unblock.
  1539. + */
  1540. + crypto_all_qblocked = !list_empty(&crp_q);
  1541. +
  1542. + /*
  1543. + * Find the first element in the queue that can be
  1544. + * processed and look-ahead to see if multiple ops
  1545. + * are ready for the same driver.
  1546. + */
  1547. + submit = NULL;
  1548. + hint = 0;
  1549. + list_for_each_entry(crp, &crp_q, crp_next) {
  1550. + hid = CRYPTO_SESID2HID(crp->crp_sid);
  1551. + cap = crypto_checkdriver(hid);
  1552. + /*
  1553. + * Driver cannot disappear when there is an active
  1554. + * session.
  1555. + */
  1556. + KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
  1557. + __func__, __LINE__));
  1558. + if (cap == NULL || cap->cc_dev == NULL) {
  1559. + /* Op needs to be migrated, process it. */
  1560. + if (submit == NULL)
  1561. + submit = crp;
  1562. + break;
  1563. + }
  1564. + if (!cap->cc_qblocked) {
  1565. + if (submit != NULL) {
  1566. + /*
  1567. + * We stop on finding another op,
  1568. + * regardless whether its for the same
  1569. + * driver or not. We could keep
  1570. + * searching the queue but it might be
  1571. + * better to just use a per-driver
  1572. + * queue instead.
  1573. + */
  1574. + if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
  1575. + hint = CRYPTO_HINT_MORE;
  1576. + break;
  1577. + } else {
  1578. + submit = crp;
  1579. + if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
  1580. + break;
  1581. + /* keep scanning for more are q'd */
  1582. + }
  1583. + }
  1584. + }
  1585. + if (submit != NULL) {
  1586. + hid = CRYPTO_SESID2HID(submit->crp_sid);
  1587. + crypto_all_qblocked = 0;
  1588. + list_del(&submit->crp_next);
  1589. + crypto_drivers[hid].cc_qblocked = 1;
  1590. + cap = crypto_checkdriver(hid);
  1591. + CRYPTO_Q_UNLOCK();
  1592. + KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
  1593. + __func__, __LINE__));
  1594. + result = crypto_invoke(cap, submit, hint);
  1595. + CRYPTO_Q_LOCK();
  1596. + if (result == ERESTART) {
  1597. + /*
  1598. + * The driver ran out of resources, mark the
  1599. + * driver ``blocked'' for cryptop's and put
  1600. + * the request back in the queue. It would
  1601. + * best to put the request back where we got
  1602. + * it but that's hard so for now we put it
  1603. + * at the front. This should be ok; putting
  1604. + * it at the end does not work.
  1605. + */
  1606. + /* XXX validate sid again? */
  1607. + list_add(&submit->crp_next, &crp_q);
  1608. + cryptostats.cs_blocks++;
  1609. + } else
  1610. + crypto_drivers[hid].cc_qblocked=0;
  1611. + }
  1612. +
  1613. + crypto_all_kqblocked = !list_empty(&crp_kq);
  1614. +
  1615. + /* As above, but for key ops */
  1616. + krp = NULL;
  1617. + list_for_each_entry(krpp, &crp_kq, krp_next) {
  1618. + cap = crypto_checkdriver(krpp->krp_hid);
  1619. + if (cap == NULL || cap->cc_dev == NULL) {
  1620. + /*
  1621. + * Operation needs to be migrated, invalidate
  1622. + * the assigned device so it will reselect a
  1623. + * new one below. Propagate the original
  1624. + * crid selection flags if supplied.
  1625. + */
  1626. + krp->krp_hid = krp->krp_crid &
  1627. + (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
  1628. + if (krp->krp_hid == 0)
  1629. + krp->krp_hid =
  1630. + CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
  1631. + break;
  1632. + }
  1633. + if (!cap->cc_kqblocked) {
  1634. + krp = krpp;
  1635. + break;
  1636. + }
  1637. + }
  1638. + if (krp != NULL) {
  1639. + crypto_all_kqblocked = 0;
  1640. + list_del(&krp->krp_next);
  1641. + crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
  1642. + CRYPTO_Q_UNLOCK();
  1643. + result = crypto_kinvoke(krp, krp->krp_hid);
  1644. + CRYPTO_Q_LOCK();
  1645. + if (result == ERESTART) {
  1646. + /*
  1647. + * The driver ran out of resources, mark the
  1648. + * driver ``blocked'' for cryptkop's and put
  1649. + * the request back in the queue. It would
  1650. + * best to put the request back where we got
  1651. + * it but that's hard so for now we put it
  1652. + * at the front. This should be ok; putting
  1653. + * it at the end does not work.
  1654. + */
  1655. + /* XXX validate sid again? */
  1656. + list_add(&krp->krp_next, &crp_kq);
  1657. + cryptostats.cs_kblocks++;
  1658. + } else
  1659. + crypto_drivers[krp->krp_hid].cc_kqblocked = 0;
  1660. + }
  1661. +
  1662. + if (submit == NULL && krp == NULL) {
  1663. + /*
  1664. + * Nothing more to be processed. Sleep until we're
  1665. + * woken because there are more ops to process.
  1666. + * This happens either by submission or by a driver
  1667. + * becoming unblocked and notifying us through
  1668. + * crypto_unblock. Note that when we wakeup we
  1669. + * start processing each queue again from the
  1670. + * front. It's not clear that it's important to
  1671. + * preserve this ordering since ops may finish
  1672. + * out of order if dispatched to different devices
  1673. + * and some become blocked while others do not.
  1674. + */
  1675. + dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
  1676. + __FUNCTION__,
  1677. + list_empty(&crp_q), crypto_all_qblocked,
  1678. + list_empty(&crp_kq), crypto_all_kqblocked);
  1679. + CRYPTO_Q_UNLOCK();
  1680. + crp_sleep = 1;
  1681. + wait_event_interruptible(cryptoproc_wait,
  1682. + !(list_empty(&crp_q) || crypto_all_qblocked) ||
  1683. + !(list_empty(&crp_kq) || crypto_all_kqblocked) ||
  1684. + cryptoproc == (pid_t) -1);
  1685. + crp_sleep = 0;
  1686. + if (signal_pending (current)) {
  1687. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1688. + spin_lock_irq(&current->sigmask_lock);
  1689. +#endif
  1690. + flush_signals(current);
  1691. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1692. + spin_unlock_irq(&current->sigmask_lock);
  1693. +#endif
  1694. + }
  1695. + CRYPTO_Q_LOCK();
  1696. + dprintk("%s - awake\n", __FUNCTION__);
  1697. + if (cryptoproc == (pid_t) -1)
  1698. + break;
  1699. + cryptostats.cs_intrs++;
  1700. + }
  1701. + }
  1702. + CRYPTO_Q_UNLOCK();
  1703. + complete_and_exit(&cryptoproc_exited, 0);
  1704. +}
  1705. +
  1706. +/*
  1707. + * Crypto returns thread, does callbacks for processed crypto requests.
  1708. + * Callbacks are done here, rather than in the crypto drivers, because
  1709. + * callbacks typically are expensive and would slow interrupt handling.
  1710. + */
  1711. +static int
  1712. +crypto_ret_proc(void *arg)
  1713. +{
  1714. + struct cryptop *crpt;
  1715. + struct cryptkop *krpt;
  1716. + unsigned long r_flags;
  1717. +
  1718. + ocf_daemonize("crypto_ret");
  1719. +
  1720. + CRYPTO_RETQ_LOCK();
  1721. + for (;;) {
  1722. + /* Harvest return q's for completed ops */
  1723. + crpt = NULL;
  1724. + if (!list_empty(&crp_ret_q))
  1725. + crpt = list_entry(crp_ret_q.next, typeof(*crpt), crp_next);
  1726. + if (crpt != NULL)
  1727. + list_del(&crpt->crp_next);
  1728. +
  1729. + krpt = NULL;
  1730. + if (!list_empty(&crp_ret_kq))
  1731. + krpt = list_entry(crp_ret_kq.next, typeof(*krpt), krp_next);
  1732. + if (krpt != NULL)
  1733. + list_del(&krpt->krp_next);
  1734. +
  1735. + if (crpt != NULL || krpt != NULL) {
  1736. + CRYPTO_RETQ_UNLOCK();
  1737. + /*
  1738. + * Run callbacks unlocked.
  1739. + */
  1740. + if (crpt != NULL)
  1741. + crpt->crp_callback(crpt);
  1742. + if (krpt != NULL)
  1743. + krpt->krp_callback(krpt);
  1744. + CRYPTO_RETQ_LOCK();
  1745. + } else {
  1746. + /*
  1747. + * Nothing more to be processed. Sleep until we're
  1748. + * woken because there are more returns to process.
  1749. + */
  1750. + dprintk("%s - sleeping\n", __FUNCTION__);
  1751. + CRYPTO_RETQ_UNLOCK();
  1752. + wait_event_interruptible(cryptoretproc_wait,
  1753. + cryptoretproc == (pid_t) -1 ||
  1754. + !list_empty(&crp_ret_q) ||
  1755. + !list_empty(&crp_ret_kq));
  1756. + if (signal_pending (current)) {
  1757. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1758. + spin_lock_irq(&current->sigmask_lock);
  1759. +#endif
  1760. + flush_signals(current);
  1761. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1762. + spin_unlock_irq(&current->sigmask_lock);
  1763. +#endif
  1764. + }
  1765. + CRYPTO_RETQ_LOCK();
  1766. + dprintk("%s - awake\n", __FUNCTION__);
  1767. + if (cryptoretproc == (pid_t) -1) {
  1768. + dprintk("%s - EXITING!\n", __FUNCTION__);
  1769. + break;
  1770. + }
  1771. + cryptostats.cs_rets++;
  1772. + }
  1773. + }
  1774. + CRYPTO_RETQ_UNLOCK();
  1775. + complete_and_exit(&cryptoretproc_exited, 0);
  1776. +}
  1777. +
  1778. +
  1779. +#if 0 /* should put this into /proc or something */
  1780. +static void
  1781. +db_show_drivers(void)
  1782. +{
  1783. + int hid;
  1784. +
  1785. + db_printf("%12s %4s %4s %8s %2s %2s\n"
  1786. + , "Device"
  1787. + , "Ses"
  1788. + , "Kops"
  1789. + , "Flags"
  1790. + , "QB"
  1791. + , "KB"
  1792. + );
  1793. + for (hid = 0; hid < crypto_drivers_num; hid++) {
  1794. + const struct cryptocap *cap = &crypto_drivers[hid];
  1795. + if (cap->cc_dev == NULL)
  1796. + continue;
  1797. + db_printf("%-12s %4u %4u %08x %2u %2u\n"
  1798. + , device_get_nameunit(cap->cc_dev)
  1799. + , cap->cc_sessions
  1800. + , cap->cc_koperations
  1801. + , cap->cc_flags
  1802. + , cap->cc_qblocked
  1803. + , cap->cc_kqblocked
  1804. + );
  1805. + }
  1806. +}
  1807. +
  1808. +DB_SHOW_COMMAND(crypto, db_show_crypto)
  1809. +{
  1810. + struct cryptop *crp;
  1811. +
  1812. + db_show_drivers();
  1813. + db_printf("\n");
  1814. +
  1815. + db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
  1816. + "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
  1817. + "Desc", "Callback");
  1818. + TAILQ_FOREACH(crp, &crp_q, crp_next) {
  1819. + db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
  1820. + , (int) CRYPTO_SESID2HID(crp->crp_sid)
  1821. + , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
  1822. + , crp->crp_ilen, crp->crp_olen
  1823. + , crp->crp_etype
  1824. + , crp->crp_flags
  1825. + , crp->crp_desc
  1826. + , crp->crp_callback
  1827. + );
  1828. + }
  1829. + if (!TAILQ_EMPTY(&crp_ret_q)) {
  1830. + db_printf("\n%4s %4s %4s %8s\n",
  1831. + "HID", "Etype", "Flags", "Callback");
  1832. + TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
  1833. + db_printf("%4u %4u %04x %8p\n"
  1834. + , (int) CRYPTO_SESID2HID(crp->crp_sid)
  1835. + , crp->crp_etype
  1836. + , crp->crp_flags
  1837. + , crp->crp_callback
  1838. + );
  1839. + }
  1840. + }
  1841. +}
  1842. +
  1843. +DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
  1844. +{
  1845. + struct cryptkop *krp;
  1846. +
  1847. + db_show_drivers();
  1848. + db_printf("\n");
  1849. +
  1850. + db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
  1851. + "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
  1852. + TAILQ_FOREACH(krp, &crp_kq, krp_next) {
  1853. + db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
  1854. + , krp->krp_op
  1855. + , krp->krp_status
  1856. + , krp->krp_iparams, krp->krp_oparams
  1857. + , krp->krp_crid, krp->krp_hid
  1858. + , krp->krp_callback
  1859. + );
  1860. + }
  1861. + if (!TAILQ_EMPTY(&crp_ret_q)) {
  1862. + db_printf("%4s %5s %8s %4s %8s\n",
  1863. + "Op", "Status", "CRID", "HID", "Callback");
  1864. + TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
  1865. + db_printf("%4u %5u %08x %4u %8p\n"
  1866. + , krp->krp_op
  1867. + , krp->krp_status
  1868. + , krp->krp_crid, krp->krp_hid
  1869. + , krp->krp_callback
  1870. + );
  1871. + }
  1872. + }
  1873. +}
  1874. +#endif
  1875. +
  1876. +
  1877. +static int
  1878. +crypto_init(void)
  1879. +{
  1880. + int error;
  1881. +
  1882. + dprintk("%s(0x%x)\n", __FUNCTION__, (int) crypto_init);
  1883. +
  1884. + if (crypto_initted)
  1885. + return 0;
  1886. + crypto_initted = 1;
  1887. +
  1888. + spin_lock_init(&crypto_drivers_lock);
  1889. + spin_lock_init(&crypto_q_lock);
  1890. + spin_lock_init(&crypto_ret_q_lock);
  1891. +
  1892. + cryptop_zone = kmem_cache_create("cryptop", sizeof(struct cryptop),
  1893. + 0, SLAB_HWCACHE_ALIGN, NULL
  1894. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  1895. + , NULL
  1896. +#endif
  1897. + );
  1898. +
  1899. + cryptodesc_zone = kmem_cache_create("cryptodesc", sizeof(struct cryptodesc),
  1900. + 0, SLAB_HWCACHE_ALIGN, NULL
  1901. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  1902. + , NULL
  1903. +#endif
  1904. + );
  1905. +
  1906. + if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
  1907. + printk("crypto: crypto_init cannot setup crypto zones\n");
  1908. + error = ENOMEM;
  1909. + goto bad;
  1910. + }
  1911. +
  1912. + crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
  1913. + crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
  1914. + GFP_KERNEL);
  1915. + if (crypto_drivers == NULL) {
  1916. + printk("crypto: crypto_init cannot setup crypto drivers\n");
  1917. + error = ENOMEM;
  1918. + goto bad;
  1919. + }
  1920. +
  1921. + memset(crypto_drivers, 0, crypto_drivers_num * sizeof(struct cryptocap));
  1922. +
  1923. + init_completion(&cryptoproc_exited);
  1924. + init_completion(&cryptoretproc_exited);
  1925. +
  1926. + cryptoproc = 0; /* to avoid race condition where proc runs first */
  1927. + cryptoproc = kernel_thread(crypto_proc, NULL, CLONE_FS|CLONE_FILES);
  1928. + if (cryptoproc < 0) {
  1929. + error = cryptoproc;
  1930. + printk("crypto: crypto_init cannot start crypto thread; error %d",
  1931. + error);
  1932. + goto bad;
  1933. + }
  1934. +
  1935. + cryptoretproc = 0; /* to avoid race condition where proc runs first */
  1936. + cryptoretproc = kernel_thread(crypto_ret_proc, NULL, CLONE_FS|CLONE_FILES);
  1937. + if (cryptoretproc < 0) {
  1938. + error = cryptoretproc;
  1939. + printk("crypto: crypto_init cannot start cryptoret thread; error %d",
  1940. + error);
  1941. + goto bad;
  1942. + }
  1943. +
  1944. + return 0;
  1945. +bad:
  1946. + crypto_exit();
  1947. + return error;
  1948. +}
  1949. +
  1950. +
  1951. +static void
  1952. +crypto_exit(void)
  1953. +{
  1954. + pid_t p;
  1955. + unsigned long d_flags;
  1956. +
  1957. + dprintk("%s()\n", __FUNCTION__);
  1958. +
  1959. + /*
  1960. + * Terminate any crypto threads.
  1961. + */
  1962. +
  1963. + CRYPTO_DRIVER_LOCK();
  1964. + p = cryptoproc;
  1965. + cryptoproc = (pid_t) -1;
  1966. + kill_pid(p, SIGTERM, 1);
  1967. + wake_up_interruptible(&cryptoproc_wait);
  1968. + CRYPTO_DRIVER_UNLOCK();
  1969. +
  1970. + wait_for_completion(&cryptoproc_exited);
  1971. +
  1972. + CRYPTO_DRIVER_LOCK();
  1973. + p = cryptoretproc;
  1974. + cryptoretproc = (pid_t) -1;
  1975. + kill_pid(p, SIGTERM, 1);
  1976. + wake_up_interruptible(&cryptoretproc_wait);
  1977. + CRYPTO_DRIVER_UNLOCK();
  1978. +
  1979. + wait_for_completion(&cryptoretproc_exited);
  1980. +
  1981. + /* XXX flush queues??? */
  1982. +
  1983. + /*
  1984. + * Reclaim dynamically allocated resources.
  1985. + */
  1986. + if (crypto_drivers != NULL)
  1987. + kfree(crypto_drivers);
  1988. +
  1989. + if (cryptodesc_zone != NULL)
  1990. + kmem_cache_destroy(cryptodesc_zone);
  1991. + if (cryptop_zone != NULL)
  1992. + kmem_cache_destroy(cryptop_zone);
  1993. +}
  1994. +
  1995. +
  1996. +EXPORT_SYMBOL(crypto_newsession);
  1997. +EXPORT_SYMBOL(crypto_freesession);
  1998. +EXPORT_SYMBOL(crypto_get_driverid);
  1999. +EXPORT_SYMBOL(crypto_kregister);
  2000. +EXPORT_SYMBOL(crypto_register);
  2001. +EXPORT_SYMBOL(crypto_unregister);
  2002. +EXPORT_SYMBOL(crypto_unregister_all);
  2003. +EXPORT_SYMBOL(crypto_unblock);
  2004. +EXPORT_SYMBOL(crypto_dispatch);
  2005. +EXPORT_SYMBOL(crypto_kdispatch);
  2006. +EXPORT_SYMBOL(crypto_freereq);
  2007. +EXPORT_SYMBOL(crypto_getreq);
  2008. +EXPORT_SYMBOL(crypto_done);
  2009. +EXPORT_SYMBOL(crypto_kdone);
  2010. +EXPORT_SYMBOL(crypto_getfeat);
  2011. +EXPORT_SYMBOL(crypto_userasymcrypto);
  2012. +EXPORT_SYMBOL(crypto_getcaps);
  2013. +EXPORT_SYMBOL(crypto_find_driver);
  2014. +EXPORT_SYMBOL(crypto_find_device_byhid);
  2015. +
  2016. +module_init(crypto_init);
  2017. +module_exit(crypto_exit);
  2018. +
  2019. +MODULE_LICENSE("BSD");
  2020. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  2021. +MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");
  2022. diff -Nur linux-2.6.30.orig/crypto/ocf/cryptodev.c linux-2.6.30/crypto/ocf/cryptodev.c
  2023. --- linux-2.6.30.orig/crypto/ocf/cryptodev.c 1970-01-01 01:00:00.000000000 +0100
  2024. +++ linux-2.6.30/crypto/ocf/cryptodev.c 2009-06-11 10:55:27.000000000 +0200
  2025. @@ -0,0 +1,1048 @@
  2026. +/* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
  2027. +
  2028. +/*-
  2029. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  2030. + * Copyright (C) 2006-2007 David McCullough
  2031. + * Copyright (C) 2004-2005 Intel Corporation.
  2032. + * The license and original author are listed below.
  2033. + *
  2034. + * Copyright (c) 2001 Theo de Raadt
  2035. + * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
  2036. + *
  2037. + * Redistribution and use in source and binary forms, with or without
  2038. + * modification, are permitted provided that the following conditions
  2039. + * are met:
  2040. + *
  2041. + * 1. Redistributions of source code must retain the above copyright
  2042. + * notice, this list of conditions and the following disclaimer.
  2043. + * 2. Redistributions in binary form must reproduce the above copyright
  2044. + * notice, this list of conditions and the following disclaimer in the
  2045. + * documentation and/or other materials provided with the distribution.
  2046. + * 3. The name of the author may not be used to endorse or promote products
  2047. + * derived from this software without specific prior written permission.
  2048. + *
  2049. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  2050. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  2051. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  2052. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  2053. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  2054. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2055. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2056. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2057. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  2058. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2059. + *
  2060. + * Effort sponsored in part by the Defense Advanced Research Projects
  2061. + * Agency (DARPA) and Air Force Research Laboratory, Air Force
  2062. + * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  2063. + *
  2064. +__FBSDID("$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.34 2007/05/09 19:37:02 gnn Exp $");
  2065. + */
  2066. +
  2067. +#ifndef AUTOCONF_INCLUDED
  2068. +#include <linux/config.h>
  2069. +#endif
  2070. +#include <linux/types.h>
  2071. +#include <linux/time.h>
  2072. +#include <linux/delay.h>
  2073. +#include <linux/list.h>
  2074. +#include <linux/init.h>
  2075. +#include <linux/sched.h>
  2076. +#include <linux/unistd.h>
  2077. +#include <linux/module.h>
  2078. +#include <linux/wait.h>
  2079. +#include <linux/slab.h>
  2080. +#include <linux/fs.h>
  2081. +#include <linux/dcache.h>
  2082. +#include <linux/file.h>
  2083. +#include <linux/mount.h>
  2084. +#include <linux/miscdevice.h>
  2085. +#include <linux/version.h>
  2086. +#include <asm/uaccess.h>
  2087. +
  2088. +#include <cryptodev.h>
  2089. +#include <uio.h>
  2090. +
  2091. +extern asmlinkage long sys_dup(unsigned int fildes);
  2092. +
  2093. +#define debug cryptodev_debug
  2094. +int cryptodev_debug = 0;
  2095. +module_param(cryptodev_debug, int, 0644);
  2096. +MODULE_PARM_DESC(cryptodev_debug, "Enable cryptodev debug");
  2097. +
  2098. +struct csession_info {
  2099. + u_int16_t blocksize;
  2100. + u_int16_t minkey, maxkey;
  2101. +
  2102. + u_int16_t keysize;
  2103. + /* u_int16_t hashsize; */
  2104. + u_int16_t authsize;
  2105. + /* u_int16_t ctxsize; */
  2106. +};
  2107. +
  2108. +struct csession {
  2109. + struct list_head list;
  2110. + u_int64_t sid;
  2111. + u_int32_t ses;
  2112. +
  2113. + wait_queue_head_t waitq;
  2114. +
  2115. + u_int32_t cipher;
  2116. +
  2117. + u_int32_t mac;
  2118. +
  2119. + caddr_t key;
  2120. + int keylen;
  2121. + u_char tmp_iv[EALG_MAX_BLOCK_LEN];
  2122. +
  2123. + caddr_t mackey;
  2124. + int mackeylen;
  2125. +
  2126. + struct csession_info info;
  2127. +
  2128. + struct iovec iovec;
  2129. + struct uio uio;
  2130. + int error;
  2131. +};
  2132. +
  2133. +struct fcrypt {
  2134. + struct list_head csessions;
  2135. + int sesn;
  2136. +};
  2137. +
  2138. +static struct csession *csefind(struct fcrypt *, u_int);
  2139. +static int csedelete(struct fcrypt *, struct csession *);
  2140. +static struct csession *cseadd(struct fcrypt *, struct csession *);
  2141. +static struct csession *csecreate(struct fcrypt *, u_int64_t,
  2142. + struct cryptoini *crie, struct cryptoini *cria, struct csession_info *);
  2143. +static int csefree(struct csession *);
  2144. +
  2145. +static int cryptodev_op(struct csession *, struct crypt_op *);
  2146. +static int cryptodev_key(struct crypt_kop *);
  2147. +static int cryptodev_find(struct crypt_find_op *);
  2148. +
  2149. +static int cryptodev_cb(void *);
  2150. +static int cryptodev_open(struct inode *inode, struct file *filp);
  2151. +
  2152. +/*
  2153. + * Check a crypto identifier to see if it requested
  2154. + * a valid crid and it's capabilities match.
  2155. + */
  2156. +static int
  2157. +checkcrid(int crid)
  2158. +{
  2159. + int hid = crid & ~(CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
  2160. + int typ = crid & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
  2161. + int caps = 0;
  2162. +
  2163. + /* if the user hasn't selected a driver, then just call newsession */
  2164. + if (hid == 0 && typ != 0)
  2165. + return 0;
  2166. +
  2167. + caps = crypto_getcaps(hid);
  2168. +
  2169. + /* didn't find anything with capabilities */
  2170. + if (caps == 0) {
  2171. + dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__, hid, typ);
  2172. + return EINVAL;
  2173. + }
  2174. +
  2175. + /* the user didn't specify SW or HW, so the driver is ok */
  2176. + if (typ == 0)
  2177. + return 0;
  2178. +
  2179. + /* if the type specified didn't match */
  2180. + if (typ != (caps & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE))) {
  2181. + dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__,
  2182. + hid, typ, caps);
  2183. + return EINVAL;
  2184. + }
  2185. +
  2186. + return 0;
  2187. +}
  2188. +
  2189. +static int
  2190. +cryptodev_op(struct csession *cse, struct crypt_op *cop)
  2191. +{
  2192. + struct cryptop *crp = NULL;
  2193. + struct cryptodesc *crde = NULL, *crda = NULL;
  2194. + int error = 0;
  2195. +
  2196. + dprintk("%s()\n", __FUNCTION__);
  2197. + if (cop->len > CRYPTO_MAX_DATA_LEN) {
  2198. + dprintk("%s: %d > %d\n", __FUNCTION__, cop->len, CRYPTO_MAX_DATA_LEN);
  2199. + return (E2BIG);
  2200. + }
  2201. +
  2202. + if (cse->info.blocksize && (cop->len % cse->info.blocksize) != 0) {
  2203. + dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__, cse->info.blocksize,
  2204. + cop->len);
  2205. + return (EINVAL);
  2206. + }
  2207. +
  2208. + cse->uio.uio_iov = &cse->iovec;
  2209. + cse->uio.uio_iovcnt = 1;
  2210. + cse->uio.uio_offset = 0;
  2211. +#if 0
  2212. + cse->uio.uio_resid = cop->len;
  2213. + cse->uio.uio_segflg = UIO_SYSSPACE;
  2214. + cse->uio.uio_rw = UIO_WRITE;
  2215. + cse->uio.uio_td = td;
  2216. +#endif
  2217. + cse->uio.uio_iov[0].iov_len = cop->len;
  2218. + if (cse->info.authsize)
  2219. + cse->uio.uio_iov[0].iov_len += cse->info.authsize;
  2220. + cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
  2221. + GFP_KERNEL);
  2222. +
  2223. + if (cse->uio.uio_iov[0].iov_base == NULL) {
  2224. + dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__,
  2225. + cse->uio.uio_iov[0].iov_len);
  2226. + return (ENOMEM);
  2227. + }
  2228. +
  2229. + crp = crypto_getreq((cse->info.blocksize != 0) + (cse->info.authsize != 0));
  2230. + if (crp == NULL) {
  2231. + dprintk("%s: ENOMEM\n", __FUNCTION__);
  2232. + error = ENOMEM;
  2233. + goto bail;
  2234. + }
  2235. +
  2236. + if (cse->info.authsize) {
  2237. + crda = crp->crp_desc;
  2238. + if (cse->info.blocksize)
  2239. + crde = crda->crd_next;
  2240. + } else {
  2241. + if (cse->info.blocksize)
  2242. + crde = crp->crp_desc;
  2243. + else {
  2244. + dprintk("%s: bad request\n", __FUNCTION__);
  2245. + error = EINVAL;
  2246. + goto bail;
  2247. + }
  2248. + }
  2249. +
  2250. + if ((error = copy_from_user(cse->uio.uio_iov[0].iov_base, cop->src,
  2251. + cop->len))) {
  2252. + dprintk("%s: bad copy\n", __FUNCTION__);
  2253. + goto bail;
  2254. + }
  2255. +
  2256. + if (crda) {
  2257. + crda->crd_skip = 0;
  2258. + crda->crd_len = cop->len;
  2259. + crda->crd_inject = cop->len;
  2260. +
  2261. + crda->crd_alg = cse->mac;
  2262. + crda->crd_key = cse->mackey;
  2263. + crda->crd_klen = cse->mackeylen * 8;
  2264. + }
  2265. +
  2266. + if (crde) {
  2267. + if (cop->op == COP_ENCRYPT)
  2268. + crde->crd_flags |= CRD_F_ENCRYPT;
  2269. + else
  2270. + crde->crd_flags &= ~CRD_F_ENCRYPT;
  2271. + crde->crd_len = cop->len;
  2272. + crde->crd_inject = 0;
  2273. +
  2274. + crde->crd_alg = cse->cipher;
  2275. + crde->crd_key = cse->key;
  2276. + crde->crd_klen = cse->keylen * 8;
  2277. + }
  2278. +
  2279. + crp->crp_ilen = cse->uio.uio_iov[0].iov_len;
  2280. + crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
  2281. + | (cop->flags & COP_F_BATCH);
  2282. + crp->crp_buf = (caddr_t)&cse->uio;
  2283. + crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
  2284. + crp->crp_sid = cse->sid;
  2285. + crp->crp_opaque = (void *)cse;
  2286. +
  2287. + if (cop->iv) {
  2288. + if (crde == NULL) {
  2289. + error = EINVAL;
  2290. + dprintk("%s no crde\n", __FUNCTION__);
  2291. + goto bail;
  2292. + }
  2293. + if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  2294. + error = EINVAL;
  2295. + dprintk("%s arc4 with IV\n", __FUNCTION__);
  2296. + goto bail;
  2297. + }
  2298. + if ((error = copy_from_user(cse->tmp_iv, cop->iv,
  2299. + cse->info.blocksize))) {
  2300. + dprintk("%s bad iv copy\n", __FUNCTION__);
  2301. + goto bail;
  2302. + }
  2303. + memcpy(crde->crd_iv, cse->tmp_iv, cse->info.blocksize);
  2304. + crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
  2305. + crde->crd_skip = 0;
  2306. + } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  2307. + crde->crd_skip = 0;
  2308. + } else if (crde) {
  2309. + crde->crd_flags |= CRD_F_IV_PRESENT;
  2310. + crde->crd_skip = cse->info.blocksize;
  2311. + crde->crd_len -= cse->info.blocksize;
  2312. + }
  2313. +
  2314. + if (cop->mac && crda == NULL) {
  2315. + error = EINVAL;
  2316. + dprintk("%s no crda\n", __FUNCTION__);
  2317. + goto bail;
  2318. + }
  2319. +
  2320. + /*
  2321. + * Let the dispatch run unlocked, then, interlock against the
  2322. + * callback before checking if the operation completed and going
  2323. + * to sleep. This insures drivers don't inherit our lock which
  2324. + * results in a lock order reversal between crypto_dispatch forced
  2325. + * entry and the crypto_done callback into us.
  2326. + */
  2327. + error = crypto_dispatch(crp);
  2328. + if (error == 0) {
  2329. + dprintk("%s about to WAIT\n", __FUNCTION__);
  2330. + /*
  2331. + * we really need to wait for driver to complete to maintain
  2332. + * state, luckily interrupts will be remembered
  2333. + */
  2334. + do {
  2335. + error = wait_event_interruptible(crp->crp_waitq,
  2336. + ((crp->crp_flags & CRYPTO_F_DONE) != 0));
  2337. + /*
  2338. + * we can't break out of this loop or we will leave behind
  2339. + * a huge mess, however, staying here means if your driver
  2340. + * is broken user applications can hang and not be killed.
  2341. + * The solution, fix your driver :-)
  2342. + */
  2343. + if (error) {
  2344. + schedule();
  2345. + error = 0;
  2346. + }
  2347. + } while ((crp->crp_flags & CRYPTO_F_DONE) == 0);
  2348. + dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
  2349. + }
  2350. +
  2351. + if (crp->crp_etype != 0) {
  2352. + error = crp->crp_etype;
  2353. + dprintk("%s error in crp processing\n", __FUNCTION__);
  2354. + goto bail;
  2355. + }
  2356. +
  2357. + if (cse->error) {
  2358. + error = cse->error;
  2359. + dprintk("%s error in cse processing\n", __FUNCTION__);
  2360. + goto bail;
  2361. + }
  2362. +
  2363. + if (cop->dst && (error = copy_to_user(cop->dst,
  2364. + cse->uio.uio_iov[0].iov_base, cop->len))) {
  2365. + dprintk("%s bad dst copy\n", __FUNCTION__);
  2366. + goto bail;
  2367. + }
  2368. +
  2369. + if (cop->mac &&
  2370. + (error=copy_to_user(cop->mac,
  2371. + (caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
  2372. + cse->info.authsize))) {
  2373. + dprintk("%s bad mac copy\n", __FUNCTION__);
  2374. + goto bail;
  2375. + }
  2376. +
  2377. +bail:
  2378. + if (crp)
  2379. + crypto_freereq(crp);
  2380. + if (cse->uio.uio_iov[0].iov_base)
  2381. + kfree(cse->uio.uio_iov[0].iov_base);
  2382. +
  2383. + return (error);
  2384. +}
  2385. +
  2386. +static int
  2387. +cryptodev_cb(void *op)
  2388. +{
  2389. + struct cryptop *crp = (struct cryptop *) op;
  2390. + struct csession *cse = (struct csession *)crp->crp_opaque;
  2391. + int error;
  2392. +
  2393. + dprintk("%s()\n", __FUNCTION__);
  2394. + error = crp->crp_etype;
  2395. + if (error == EAGAIN) {
  2396. + crp->crp_flags &= ~CRYPTO_F_DONE;
  2397. +#ifdef NOTYET
  2398. + /*
  2399. + * DAVIDM I am fairly sure that we should turn this into a batch
  2400. + * request to stop bad karma/lockup, revisit
  2401. + */
  2402. + crp->crp_flags |= CRYPTO_F_BATCH;
  2403. +#endif
  2404. + return crypto_dispatch(crp);
  2405. + }
  2406. + if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
  2407. + cse->error = error;
  2408. + wake_up_interruptible(&crp->crp_waitq);
  2409. + }
  2410. + return (0);
  2411. +}
  2412. +
  2413. +static int
  2414. +cryptodevkey_cb(void *op)
  2415. +{
  2416. + struct cryptkop *krp = (struct cryptkop *) op;
  2417. + dprintk("%s()\n", __FUNCTION__);
  2418. + wake_up_interruptible(&krp->krp_waitq);
  2419. + return (0);
  2420. +}
  2421. +
  2422. +static int
  2423. +cryptodev_key(struct crypt_kop *kop)
  2424. +{
  2425. + struct cryptkop *krp = NULL;
  2426. + int error = EINVAL;
  2427. + int in, out, size, i;
  2428. +
  2429. + dprintk("%s()\n", __FUNCTION__);
  2430. + if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
  2431. + dprintk("%s params too big\n", __FUNCTION__);
  2432. + return (EFBIG);
  2433. + }
  2434. +
  2435. + in = kop->crk_iparams;
  2436. + out = kop->crk_oparams;
  2437. + switch (kop->crk_op) {
  2438. + case CRK_MOD_EXP:
  2439. + if (in == 3 && out == 1)
  2440. + break;
  2441. + return (EINVAL);
  2442. + case CRK_MOD_EXP_CRT:
  2443. + if (in == 6 && out == 1)
  2444. + break;
  2445. + return (EINVAL);
  2446. + case CRK_DSA_SIGN:
  2447. + if (in == 5 && out == 2)
  2448. + break;
  2449. + return (EINVAL);
  2450. + case CRK_DSA_VERIFY:
  2451. + if (in == 7 && out == 0)
  2452. + break;
  2453. + return (EINVAL);
  2454. + case CRK_DH_COMPUTE_KEY:
  2455. + if (in == 3 && out == 1)
  2456. + break;
  2457. + return (EINVAL);
  2458. + default:
  2459. + return (EINVAL);
  2460. + }
  2461. +
  2462. + krp = (struct cryptkop *)kmalloc(sizeof *krp, GFP_KERNEL);
  2463. + if (!krp)
  2464. + return (ENOMEM);
  2465. + bzero(krp, sizeof *krp);
  2466. + krp->krp_op = kop->crk_op;
  2467. + krp->krp_status = kop->crk_status;
  2468. + krp->krp_iparams = kop->crk_iparams;
  2469. + krp->krp_oparams = kop->crk_oparams;
  2470. + krp->krp_crid = kop->crk_crid;
  2471. + krp->krp_status = 0;
  2472. + krp->krp_flags = CRYPTO_KF_CBIMM;
  2473. + krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
  2474. + init_waitqueue_head(&krp->krp_waitq);
  2475. +
  2476. + for (i = 0; i < CRK_MAXPARAM; i++)
  2477. + krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
  2478. + for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
  2479. + size = (krp->krp_param[i].crp_nbits + 7) / 8;
  2480. + if (size == 0)
  2481. + continue;
  2482. + krp->krp_param[i].crp_p = (caddr_t) kmalloc(size, GFP_KERNEL);
  2483. + if (i >= krp->krp_iparams)
  2484. + continue;
  2485. + error = copy_from_user(krp->krp_param[i].crp_p,
  2486. + kop->crk_param[i].crp_p, size);
  2487. + if (error)
  2488. + goto fail;
  2489. + }
  2490. +
  2491. + error = crypto_kdispatch(krp);
  2492. + if (error)
  2493. + goto fail;
  2494. +
  2495. + do {
  2496. + error = wait_event_interruptible(krp->krp_waitq,
  2497. + ((krp->krp_flags & CRYPTO_KF_DONE) != 0));
  2498. + /*
  2499. + * we can't break out of this loop or we will leave behind
  2500. + * a huge mess, however, staying here means if your driver
  2501. + * is broken user applications can hang and not be killed.
  2502. + * The solution, fix your driver :-)
  2503. + */
  2504. + if (error) {
  2505. + schedule();
  2506. + error = 0;
  2507. + }
  2508. + } while ((krp->krp_flags & CRYPTO_KF_DONE) == 0);
  2509. +
  2510. + dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
  2511. +
  2512. + kop->crk_crid = krp->krp_crid; /* device that did the work */
  2513. + if (krp->krp_status != 0) {
  2514. + error = krp->krp_status;
  2515. + goto fail;
  2516. + }
  2517. +
  2518. + for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
  2519. + size = (krp->krp_param[i].crp_nbits + 7) / 8;
  2520. + if (size == 0)
  2521. + continue;
  2522. + error = copy_to_user(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p,
  2523. + size);
  2524. + if (error)
  2525. + goto fail;
  2526. + }
  2527. +
  2528. +fail:
  2529. + if (krp) {
  2530. + kop->crk_status = krp->krp_status;
  2531. + for (i = 0; i < CRK_MAXPARAM; i++) {
  2532. + if (krp->krp_param[i].crp_p)
  2533. + kfree(krp->krp_param[i].crp_p);
  2534. + }
  2535. + kfree(krp);
  2536. + }
  2537. + return (error);
  2538. +}
  2539. +
  2540. +static int
  2541. +cryptodev_find(struct crypt_find_op *find)
  2542. +{
  2543. + device_t dev;
  2544. +
  2545. + if (find->crid != -1) {
  2546. + dev = crypto_find_device_byhid(find->crid);
  2547. + if (dev == NULL)
  2548. + return (ENOENT);
  2549. + strlcpy(find->name, device_get_nameunit(dev),
  2550. + sizeof(find->name));
  2551. + } else {
  2552. + find->crid = crypto_find_driver(find->name);
  2553. + if (find->crid == -1)
  2554. + return (ENOENT);
  2555. + }
  2556. + return (0);
  2557. +}
  2558. +
  2559. +static struct csession *
  2560. +csefind(struct fcrypt *fcr, u_int ses)
  2561. +{
  2562. + struct csession *cse;
  2563. +
  2564. + dprintk("%s()\n", __FUNCTION__);
  2565. + list_for_each_entry(cse, &fcr->csessions, list)
  2566. + if (cse->ses == ses)
  2567. + return (cse);
  2568. + return (NULL);
  2569. +}
  2570. +
  2571. +static int
  2572. +csedelete(struct fcrypt *fcr, struct csession *cse_del)
  2573. +{
  2574. + struct csession *cse;
  2575. +
  2576. + dprintk("%s()\n", __FUNCTION__);
  2577. + list_for_each_entry(cse, &fcr->csessions, list) {
  2578. + if (cse == cse_del) {
  2579. + list_del(&cse->list);
  2580. + return (1);
  2581. + }
  2582. + }
  2583. + return (0);
  2584. +}
  2585. +
  2586. +static struct csession *
  2587. +cseadd(struct fcrypt *fcr, struct csession *cse)
  2588. +{
  2589. + dprintk("%s()\n", __FUNCTION__);
  2590. + list_add_tail(&cse->list, &fcr->csessions);
  2591. + cse->ses = fcr->sesn++;
  2592. + return (cse);
  2593. +}
  2594. +
  2595. +static struct csession *
  2596. +csecreate(struct fcrypt *fcr, u_int64_t sid, struct cryptoini *crie,
  2597. + struct cryptoini *cria, struct csession_info *info)
  2598. +{
  2599. + struct csession *cse;
  2600. +
  2601. + dprintk("%s()\n", __FUNCTION__);
  2602. + cse = (struct csession *) kmalloc(sizeof(struct csession), GFP_KERNEL);
  2603. + if (cse == NULL)
  2604. + return NULL;
  2605. + memset(cse, 0, sizeof(struct csession));
  2606. +
  2607. + INIT_LIST_HEAD(&cse->list);
  2608. + init_waitqueue_head(&cse->waitq);
  2609. +
  2610. + cse->key = crie->cri_key;
  2611. + cse->keylen = crie->cri_klen/8;
  2612. + cse->mackey = cria->cri_key;
  2613. + cse->mackeylen = cria->cri_klen/8;
  2614. + cse->sid = sid;
  2615. + cse->cipher = crie->cri_alg;
  2616. + cse->mac = cria->cri_alg;
  2617. + cse->info = *info;
  2618. + cseadd(fcr, cse);
  2619. + return (cse);
  2620. +}
  2621. +
  2622. +static int
  2623. +csefree(struct csession *cse)
  2624. +{
  2625. + int error;
  2626. +
  2627. + dprintk("%s()\n", __FUNCTION__);
  2628. + error = crypto_freesession(cse->sid);
  2629. + if (cse->key)
  2630. + kfree(cse->key);
  2631. + if (cse->mackey)
  2632. + kfree(cse->mackey);
  2633. + kfree(cse);
  2634. + return(error);
  2635. +}
  2636. +
  2637. +static int
  2638. +cryptodev_ioctl(
  2639. + struct inode *inode,
  2640. + struct file *filp,
  2641. + unsigned int cmd,
  2642. + unsigned long arg)
  2643. +{
  2644. + struct cryptoini cria, crie;
  2645. + struct fcrypt *fcr = filp->private_data;
  2646. + struct csession *cse;
  2647. + struct csession_info info;
  2648. + struct session2_op sop;
  2649. + struct crypt_op cop;
  2650. + struct crypt_kop kop;
  2651. + struct crypt_find_op fop;
  2652. + u_int64_t sid;
  2653. + u_int32_t ses;
  2654. + int feat, fd, error = 0, crid;
  2655. + mm_segment_t fs;
  2656. +
  2657. + dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__, cmd, arg);
  2658. +
  2659. + switch (cmd) {
  2660. +
  2661. + case CRIOGET: {
  2662. + dprintk("%s(CRIOGET)\n", __FUNCTION__);
  2663. + fs = get_fs();
  2664. + set_fs(get_ds());
  2665. + for (fd = 0; fd < files_fdtable(current->files)->max_fds; fd++)
  2666. + if (files_fdtable(current->files)->fd[fd] == filp)
  2667. + break;
  2668. + fd = sys_dup(fd);
  2669. + set_fs(fs);
  2670. + put_user(fd, (int *) arg);
  2671. + return IS_ERR_VALUE(fd) ? fd : 0;
  2672. + }
  2673. +
  2674. +#define CIOCGSESSSTR (cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
  2675. + case CIOCGSESSION:
  2676. + case CIOCGSESSION2:
  2677. + dprintk("%s(%s)\n", __FUNCTION__, CIOCGSESSSTR);
  2678. + memset(&crie, 0, sizeof(crie));
  2679. + memset(&cria, 0, sizeof(cria));
  2680. + memset(&info, 0, sizeof(info));
  2681. + memset(&sop, 0, sizeof(sop));
  2682. +
  2683. + if (copy_from_user(&sop, (void*)arg, (cmd == CIOCGSESSION) ?
  2684. + sizeof(struct session_op) : sizeof(sop))) {
  2685. + dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  2686. + error = EFAULT;
  2687. + goto bail;
  2688. + }
  2689. +
  2690. + switch (sop.cipher) {
  2691. + case 0:
  2692. + dprintk("%s(%s) - no cipher\n", __FUNCTION__, CIOCGSESSSTR);
  2693. + break;
  2694. + case CRYPTO_NULL_CBC:
  2695. + info.blocksize = NULL_BLOCK_LEN;
  2696. + info.minkey = NULL_MIN_KEY_LEN;
  2697. + info.maxkey = NULL_MAX_KEY_LEN;
  2698. + break;
  2699. + case CRYPTO_DES_CBC:
  2700. + info.blocksize = DES_BLOCK_LEN;
  2701. + info.minkey = DES_MIN_KEY_LEN;
  2702. + info.maxkey = DES_MAX_KEY_LEN;
  2703. + break;
  2704. + case CRYPTO_3DES_CBC:
  2705. + info.blocksize = DES3_BLOCK_LEN;
  2706. + info.minkey = DES3_MIN_KEY_LEN;
  2707. + info.maxkey = DES3_MAX_KEY_LEN;
  2708. + break;
  2709. + case CRYPTO_BLF_CBC:
  2710. + info.blocksize = BLOWFISH_BLOCK_LEN;
  2711. + info.minkey = BLOWFISH_MIN_KEY_LEN;
  2712. + info.maxkey = BLOWFISH_MAX_KEY_LEN;
  2713. + break;
  2714. + case CRYPTO_CAST_CBC:
  2715. + info.blocksize = CAST128_BLOCK_LEN;
  2716. + info.minkey = CAST128_MIN_KEY_LEN;
  2717. + info.maxkey = CAST128_MAX_KEY_LEN;
  2718. + break;
  2719. + case CRYPTO_SKIPJACK_CBC:
  2720. + info.blocksize = SKIPJACK_BLOCK_LEN;
  2721. + info.minkey = SKIPJACK_MIN_KEY_LEN;
  2722. + info.maxkey = SKIPJACK_MAX_KEY_LEN;
  2723. + break;
  2724. + case CRYPTO_AES_CBC:
  2725. + info.blocksize = AES_BLOCK_LEN;
  2726. + info.minkey = AES_MIN_KEY_LEN;
  2727. + info.maxkey = AES_MAX_KEY_LEN;
  2728. + break;
  2729. + case CRYPTO_ARC4:
  2730. + info.blocksize = ARC4_BLOCK_LEN;
  2731. + info.minkey = ARC4_MIN_KEY_LEN;
  2732. + info.maxkey = ARC4_MAX_KEY_LEN;
  2733. + break;
  2734. + case CRYPTO_CAMELLIA_CBC:
  2735. + info.blocksize = CAMELLIA_BLOCK_LEN;
  2736. + info.minkey = CAMELLIA_MIN_KEY_LEN;
  2737. + info.maxkey = CAMELLIA_MAX_KEY_LEN;
  2738. + break;
  2739. + default:
  2740. + dprintk("%s(%s) - bad cipher\n", __FUNCTION__, CIOCGSESSSTR);
  2741. + error = EINVAL;
  2742. + goto bail;
  2743. + }
  2744. +
  2745. + switch (sop.mac) {
  2746. + case 0:
  2747. + dprintk("%s(%s) - no mac\n", __FUNCTION__, CIOCGSESSSTR);
  2748. + break;
  2749. + case CRYPTO_NULL_HMAC:
  2750. + info.authsize = NULL_HASH_LEN;
  2751. + break;
  2752. + case CRYPTO_MD5:
  2753. + info.authsize = MD5_HASH_LEN;
  2754. + break;
  2755. + case CRYPTO_SHA1:
  2756. + info.authsize = SHA1_HASH_LEN;
  2757. + break;
  2758. + case CRYPTO_SHA2_256:
  2759. + info.authsize = SHA2_256_HASH_LEN;
  2760. + break;
  2761. + case CRYPTO_SHA2_384:
  2762. + info.authsize = SHA2_384_HASH_LEN;
  2763. + break;
  2764. + case CRYPTO_SHA2_512:
  2765. + info.authsize = SHA2_512_HASH_LEN;
  2766. + break;
  2767. + case CRYPTO_RIPEMD160:
  2768. + info.authsize = RIPEMD160_HASH_LEN;
  2769. + break;
  2770. + case CRYPTO_MD5_HMAC:
  2771. + info.authsize = MD5_HASH_LEN;
  2772. + break;
  2773. + case CRYPTO_SHA1_HMAC:
  2774. + info.authsize = SHA1_HASH_LEN;
  2775. + break;
  2776. + case CRYPTO_SHA2_256_HMAC:
  2777. + info.authsize = SHA2_256_HASH_LEN;
  2778. + break;
  2779. + case CRYPTO_SHA2_384_HMAC:
  2780. + info.authsize = SHA2_384_HASH_LEN;
  2781. + break;
  2782. + case CRYPTO_SHA2_512_HMAC:
  2783. + info.authsize = SHA2_512_HASH_LEN;
  2784. + break;
  2785. + case CRYPTO_RIPEMD160_HMAC:
  2786. + info.authsize = RIPEMD160_HASH_LEN;
  2787. + break;
  2788. + default:
  2789. + dprintk("%s(%s) - bad mac\n", __FUNCTION__, CIOCGSESSSTR);
  2790. + error = EINVAL;
  2791. + goto bail;
  2792. + }
  2793. +
  2794. + if (info.blocksize) {
  2795. + crie.cri_alg = sop.cipher;
  2796. + crie.cri_klen = sop.keylen * 8;
  2797. + if ((info.maxkey && sop.keylen > info.maxkey) ||
  2798. + sop.keylen < info.minkey) {
  2799. + dprintk("%s(%s) - bad key\n", __FUNCTION__, CIOCGSESSSTR);
  2800. + error = EINVAL;
  2801. + goto bail;
  2802. + }
  2803. +
  2804. + crie.cri_key = (u_int8_t *) kmalloc(crie.cri_klen/8+1, GFP_KERNEL);
  2805. + if (copy_from_user(crie.cri_key, sop.key,
  2806. + crie.cri_klen/8)) {
  2807. + dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  2808. + error = EFAULT;
  2809. + goto bail;
  2810. + }
  2811. + if (info.authsize)
  2812. + crie.cri_next = &cria;
  2813. + }
  2814. +
  2815. + if (info.authsize) {
  2816. + cria.cri_alg = sop.mac;
  2817. + cria.cri_klen = sop.mackeylen * 8;
  2818. + if ((info.maxkey && sop.mackeylen > info.maxkey) ||
  2819. + sop.keylen < info.minkey) {
  2820. + dprintk("%s(%s) - mackeylen %d\n", __FUNCTION__, CIOCGSESSSTR,
  2821. + sop.mackeylen);
  2822. + error = EINVAL;
  2823. + goto bail;
  2824. + }
  2825. +
  2826. + if (cria.cri_klen) {
  2827. + cria.cri_key = (u_int8_t *) kmalloc(cria.cri_klen/8,GFP_KERNEL);
  2828. + if (copy_from_user(cria.cri_key, sop.mackey,
  2829. + cria.cri_klen / 8)) {
  2830. + dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  2831. + error = EFAULT;
  2832. + goto bail;
  2833. + }
  2834. + }
  2835. + }
  2836. +
  2837. + /* NB: CIOGSESSION2 has the crid */
  2838. + if (cmd == CIOCGSESSION2) {
  2839. + crid = sop.crid;
  2840. + error = checkcrid(crid);
  2841. + if (error) {
  2842. + dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__,
  2843. + CIOCGSESSSTR, error);
  2844. + goto bail;
  2845. + }
  2846. + } else {
  2847. + /* allow either HW or SW to be used */
  2848. + crid = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  2849. + }
  2850. + error = crypto_newsession(&sid, (info.blocksize ? &crie : &cria), crid);
  2851. + if (error) {
  2852. + dprintk("%s(%s) - newsession %d\n",__FUNCTION__,CIOCGSESSSTR,error);
  2853. + goto bail;
  2854. + }
  2855. +
  2856. + cse = csecreate(fcr, sid, &crie, &cria, &info);
  2857. + if (cse == NULL) {
  2858. + crypto_freesession(sid);
  2859. + error = EINVAL;
  2860. + dprintk("%s(%s) - csecreate failed\n", __FUNCTION__, CIOCGSESSSTR);
  2861. + goto bail;
  2862. + }
  2863. + sop.ses = cse->ses;
  2864. +
  2865. + if (cmd == CIOCGSESSION2) {
  2866. + /* return hardware/driver id */
  2867. + sop.crid = CRYPTO_SESID2HID(cse->sid);
  2868. + }
  2869. +
  2870. + if (copy_to_user((void*)arg, &sop, (cmd == CIOCGSESSION) ?
  2871. + sizeof(struct session_op) : sizeof(sop))) {
  2872. + dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  2873. + error = EFAULT;
  2874. + }
  2875. +bail:
  2876. + if (error) {
  2877. + dprintk("%s(%s) - bail %d\n", __FUNCTION__, CIOCGSESSSTR, error);
  2878. + if (crie.cri_key)
  2879. + kfree(crie.cri_key);
  2880. + if (cria.cri_key)
  2881. + kfree(cria.cri_key);
  2882. + }
  2883. + break;
  2884. + case CIOCFSESSION:
  2885. + dprintk("%s(CIOCFSESSION)\n", __FUNCTION__);
  2886. + get_user(ses, (uint32_t*)arg);
  2887. + cse = csefind(fcr, ses);
  2888. + if (cse == NULL) {
  2889. + error = EINVAL;
  2890. + dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__, error);
  2891. + break;
  2892. + }
  2893. + csedelete(fcr, cse);
  2894. + error = csefree(cse);
  2895. + break;
  2896. + case CIOCCRYPT:
  2897. + dprintk("%s(CIOCCRYPT)\n", __FUNCTION__);
  2898. + if(copy_from_user(&cop, (void*)arg, sizeof(cop))) {
  2899. + dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__);
  2900. + error = EFAULT;
  2901. + goto bail;
  2902. + }
  2903. + cse = csefind(fcr, cop.ses);
  2904. + if (cse == NULL) {
  2905. + error = EINVAL;
  2906. + dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__, error);
  2907. + break;
  2908. + }
  2909. + error = cryptodev_op(cse, &cop);
  2910. + if(copy_to_user((void*)arg, &cop, sizeof(cop))) {
  2911. + dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__);
  2912. + error = EFAULT;
  2913. + goto bail;
  2914. + }
  2915. + break;
  2916. + case CIOCKEY:
  2917. + case CIOCKEY2:
  2918. + dprintk("%s(CIOCKEY)\n", __FUNCTION__);
  2919. + if (!crypto_userasymcrypto)
  2920. + return (EPERM); /* XXX compat? */
  2921. + if(copy_from_user(&kop, (void*)arg, sizeof(kop))) {
  2922. + dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__);
  2923. + error = EFAULT;
  2924. + goto bail;
  2925. + }
  2926. + if (cmd == CIOCKEY) {
  2927. + /* NB: crypto core enforces s/w driver use */
  2928. + kop.crk_crid =
  2929. + CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  2930. + }
  2931. + error = cryptodev_key(&kop);
  2932. + if(copy_to_user((void*)arg, &kop, sizeof(kop))) {
  2933. + dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__);
  2934. + error = EFAULT;
  2935. + goto bail;
  2936. + }
  2937. + break;
  2938. + case CIOCASYMFEAT:
  2939. + dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__);
  2940. + if (!crypto_userasymcrypto) {
  2941. + /*
  2942. + * NB: if user asym crypto operations are
  2943. + * not permitted return "no algorithms"
  2944. + * so well-behaved applications will just
  2945. + * fallback to doing them in software.
  2946. + */
  2947. + feat = 0;
  2948. + } else
  2949. + error = crypto_getfeat(&feat);
  2950. + if (!error) {
  2951. + error = copy_to_user((void*)arg, &feat, sizeof(feat));
  2952. + }
  2953. + break;
  2954. + case CIOCFINDDEV:
  2955. + if (copy_from_user(&fop, (void*)arg, sizeof(fop))) {
  2956. + dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__);
  2957. + error = EFAULT;
  2958. + goto bail;
  2959. + }
  2960. + error = cryptodev_find(&fop);
  2961. + if (copy_to_user((void*)arg, &fop, sizeof(fop))) {
  2962. + dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__);
  2963. + error = EFAULT;
  2964. + goto bail;
  2965. + }
  2966. + break;
  2967. + default:
  2968. + dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__, cmd);
  2969. + error = EINVAL;
  2970. + break;
  2971. + }
  2972. + return(-error);
  2973. +}
  2974. +
  2975. +#ifdef HAVE_UNLOCKED_IOCTL
  2976. +static long
  2977. +cryptodev_unlocked_ioctl(
  2978. + struct file *filp,
  2979. + unsigned int cmd,
  2980. + unsigned long arg)
  2981. +{
  2982. + return cryptodev_ioctl(NULL, filp, cmd, arg);
  2983. +}
  2984. +#endif
  2985. +
  2986. +static int
  2987. +cryptodev_open(struct inode *inode, struct file *filp)
  2988. +{
  2989. + struct fcrypt *fcr;
  2990. +
  2991. + dprintk("%s()\n", __FUNCTION__);
  2992. + if (filp->private_data) {
  2993. + printk("cryptodev: Private data already exists !\n");
  2994. + return(0);
  2995. + }
  2996. +
  2997. + fcr = kmalloc(sizeof(*fcr), GFP_KERNEL);
  2998. + if (!fcr) {
  2999. + dprintk("%s() - malloc failed\n", __FUNCTION__);
  3000. + return(-ENOMEM);
  3001. + }
  3002. + memset(fcr, 0, sizeof(*fcr));
  3003. +
  3004. + INIT_LIST_HEAD(&fcr->csessions);
  3005. + filp->private_data = fcr;
  3006. + return(0);
  3007. +}
  3008. +
  3009. +static int
  3010. +cryptodev_release(struct inode *inode, struct file *filp)
  3011. +{
  3012. + struct fcrypt *fcr = filp->private_data;
  3013. + struct csession *cse, *tmp;
  3014. +
  3015. + dprintk("%s()\n", __FUNCTION__);
  3016. + if (!filp) {
  3017. + printk("cryptodev: No private data on release\n");
  3018. + return(0);
  3019. + }
  3020. +
  3021. + list_for_each_entry_safe(cse, tmp, &fcr->csessions, list) {
  3022. + list_del(&cse->list);
  3023. + (void)csefree(cse);
  3024. + }
  3025. + filp->private_data = NULL;
  3026. + kfree(fcr);
  3027. + return(0);
  3028. +}
  3029. +
  3030. +static struct file_operations cryptodev_fops = {
  3031. + .owner = THIS_MODULE,
  3032. + .open = cryptodev_open,
  3033. + .release = cryptodev_release,
  3034. + .ioctl = cryptodev_ioctl,
  3035. +#ifdef HAVE_UNLOCKED_IOCTL
  3036. + .unlocked_ioctl = cryptodev_unlocked_ioctl,
  3037. +#endif
  3038. +};
  3039. +
  3040. +static struct miscdevice cryptodev = {
  3041. + .minor = CRYPTODEV_MINOR,
  3042. + .name = "crypto",
  3043. + .fops = &cryptodev_fops,
  3044. +};
  3045. +
  3046. +static int __init
  3047. +cryptodev_init(void)
  3048. +{
  3049. + int rc;
  3050. +
  3051. + dprintk("%s(%p)\n", __FUNCTION__, cryptodev_init);
  3052. + rc = misc_register(&cryptodev);
  3053. + if (rc) {
  3054. + printk(KERN_ERR "cryptodev: registration of /dev/crypto failed\n");
  3055. + return(rc);
  3056. + }
  3057. +
  3058. + return(0);
  3059. +}
  3060. +
  3061. +static void __exit
  3062. +cryptodev_exit(void)
  3063. +{
  3064. + dprintk("%s()\n", __FUNCTION__);
  3065. + misc_deregister(&cryptodev);
  3066. +}
  3067. +
  3068. +module_init(cryptodev_init);
  3069. +module_exit(cryptodev_exit);
  3070. +
  3071. +MODULE_LICENSE("BSD");
  3072. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  3073. +MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");
  3074. diff -Nur linux-2.6.30.orig/crypto/ocf/cryptodev.h linux-2.6.30/crypto/ocf/cryptodev.h
  3075. --- linux-2.6.30.orig/crypto/ocf/cryptodev.h 1970-01-01 01:00:00.000000000 +0100
  3076. +++ linux-2.6.30/crypto/ocf/cryptodev.h 2009-06-11 10:55:27.000000000 +0200
  3077. @@ -0,0 +1,478 @@
  3078. +/* $FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.25 2007/05/09 19:37:02 gnn Exp $ */
  3079. +/* $OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $ */
  3080. +
  3081. +/*-
  3082. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  3083. + * Copyright (C) 2006-2007 David McCullough
  3084. + * Copyright (C) 2004-2005 Intel Corporation.
  3085. + * The license and original author are listed below.
  3086. + *
  3087. + * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
  3088. + * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
  3089. + *
  3090. + * This code was written by Angelos D. Keromytis in Athens, Greece, in
  3091. + * February 2000. Network Security Technologies Inc. (NSTI) kindly
  3092. + * supported the development of this code.
  3093. + *
  3094. + * Copyright (c) 2000 Angelos D. Keromytis
  3095. + *
  3096. + * Permission to use, copy, and modify this software with or without fee
  3097. + * is hereby granted, provided that this entire notice is included in
  3098. + * all source code copies of any software which is or includes a copy or
  3099. + * modification of this software.
  3100. + *
  3101. + * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  3102. + * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  3103. + * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  3104. + * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  3105. + * PURPOSE.
  3106. + *
  3107. + * Copyright (c) 2001 Theo de Raadt
  3108. + *
  3109. + * Redistribution and use in source and binary forms, with or without
  3110. + * modification, are permitted provided that the following conditions
  3111. + * are met:
  3112. + *
  3113. + * 1. Redistributions of source code must retain the above copyright
  3114. + * notice, this list of conditions and the following disclaimer.
  3115. + * 2. Redistributions in binary form must reproduce the above copyright
  3116. + * notice, this list of conditions and the following disclaimer in the
  3117. + * documentation and/or other materials provided with the distribution.
  3118. + * 3. The name of the author may not be used to endorse or promote products
  3119. + * derived from this software without specific prior written permission.
  3120. + *
  3121. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  3122. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3123. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3124. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  3125. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  3126. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3127. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3128. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3129. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  3130. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3131. + *
  3132. + * Effort sponsored in part by the Defense Advanced Research Projects
  3133. + * Agency (DARPA) and Air Force Research Laboratory, Air Force
  3134. + * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  3135. + *
  3136. + */
  3137. +
  3138. +#ifndef _CRYPTO_CRYPTO_H_
  3139. +#define _CRYPTO_CRYPTO_H_
  3140. +
  3141. +/* Some initial values */
  3142. +#define CRYPTO_DRIVERS_INITIAL 4
  3143. +#define CRYPTO_SW_SESSIONS 32
  3144. +
  3145. +/* Hash values */
  3146. +#define NULL_HASH_LEN 0
  3147. +#define MD5_HASH_LEN 16
  3148. +#define SHA1_HASH_LEN 20
  3149. +#define RIPEMD160_HASH_LEN 20
  3150. +#define SHA2_256_HASH_LEN 32
  3151. +#define SHA2_384_HASH_LEN 48
  3152. +#define SHA2_512_HASH_LEN 64
  3153. +#define MD5_KPDK_HASH_LEN 16
  3154. +#define SHA1_KPDK_HASH_LEN 20
  3155. +/* Maximum hash algorithm result length */
  3156. +#define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */
  3157. +
  3158. +/* HMAC values */
  3159. +#define NULL_HMAC_BLOCK_LEN 1
  3160. +#define MD5_HMAC_BLOCK_LEN 64
  3161. +#define SHA1_HMAC_BLOCK_LEN 64
  3162. +#define RIPEMD160_HMAC_BLOCK_LEN 64
  3163. +#define SHA2_256_HMAC_BLOCK_LEN 64
  3164. +#define SHA2_384_HMAC_BLOCK_LEN 128
  3165. +#define SHA2_512_HMAC_BLOCK_LEN 128
  3166. +/* Maximum HMAC block length */
  3167. +#define HMAC_MAX_BLOCK_LEN SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */
  3168. +#define HMAC_IPAD_VAL 0x36
  3169. +#define HMAC_OPAD_VAL 0x5C
  3170. +
  3171. +/* Encryption algorithm block sizes */
  3172. +#define NULL_BLOCK_LEN 1
  3173. +#define DES_BLOCK_LEN 8
  3174. +#define DES3_BLOCK_LEN 8
  3175. +#define BLOWFISH_BLOCK_LEN 8
  3176. +#define SKIPJACK_BLOCK_LEN 8
  3177. +#define CAST128_BLOCK_LEN 8
  3178. +#define RIJNDAEL128_BLOCK_LEN 16
  3179. +#define AES_BLOCK_LEN RIJNDAEL128_BLOCK_LEN
  3180. +#define CAMELLIA_BLOCK_LEN 16
  3181. +#define ARC4_BLOCK_LEN 1
  3182. +#define EALG_MAX_BLOCK_LEN AES_BLOCK_LEN /* Keep this updated */
  3183. +
  3184. +/* Encryption algorithm min and max key sizes */
  3185. +#define NULL_MIN_KEY_LEN 0
  3186. +#define NULL_MAX_KEY_LEN 0
  3187. +#define DES_MIN_KEY_LEN 8
  3188. +#define DES_MAX_KEY_LEN 8
  3189. +#define DES3_MIN_KEY_LEN 24
  3190. +#define DES3_MAX_KEY_LEN 24
  3191. +#define BLOWFISH_MIN_KEY_LEN 4
  3192. +#define BLOWFISH_MAX_KEY_LEN 56
  3193. +#define SKIPJACK_MIN_KEY_LEN 10
  3194. +#define SKIPJACK_MAX_KEY_LEN 10
  3195. +#define CAST128_MIN_KEY_LEN 5
  3196. +#define CAST128_MAX_KEY_LEN 16
  3197. +#define RIJNDAEL128_MIN_KEY_LEN 16
  3198. +#define RIJNDAEL128_MAX_KEY_LEN 32
  3199. +#define AES_MIN_KEY_LEN RIJNDAEL128_MIN_KEY_LEN
  3200. +#define AES_MAX_KEY_LEN RIJNDAEL128_MAX_KEY_LEN
  3201. +#define CAMELLIA_MIN_KEY_LEN 16
  3202. +#define CAMELLIA_MAX_KEY_LEN 32
  3203. +#define ARC4_MIN_KEY_LEN 1
  3204. +#define ARC4_MAX_KEY_LEN 256
  3205. +
  3206. +/* Max size of data that can be processed */
  3207. +#define CRYPTO_MAX_DATA_LEN 64*1024 - 1
  3208. +
  3209. +#define CRYPTO_ALGORITHM_MIN 1
  3210. +#define CRYPTO_DES_CBC 1
  3211. +#define CRYPTO_3DES_CBC 2
  3212. +#define CRYPTO_BLF_CBC 3
  3213. +#define CRYPTO_CAST_CBC 4
  3214. +#define CRYPTO_SKIPJACK_CBC 5
  3215. +#define CRYPTO_MD5_HMAC 6
  3216. +#define CRYPTO_SHA1_HMAC 7
  3217. +#define CRYPTO_RIPEMD160_HMAC 8
  3218. +#define CRYPTO_MD5_KPDK 9
  3219. +#define CRYPTO_SHA1_KPDK 10
  3220. +#define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */
  3221. +#define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */
  3222. +#define CRYPTO_ARC4 12
  3223. +#define CRYPTO_MD5 13
  3224. +#define CRYPTO_SHA1 14
  3225. +#define CRYPTO_NULL_HMAC 15
  3226. +#define CRYPTO_NULL_CBC 16
  3227. +#define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */
  3228. +#define CRYPTO_SHA2_256_HMAC 18
  3229. +#define CRYPTO_SHA2_384_HMAC 19
  3230. +#define CRYPTO_SHA2_512_HMAC 20
  3231. +#define CRYPTO_CAMELLIA_CBC 21
  3232. +#define CRYPTO_SHA2_256 22
  3233. +#define CRYPTO_SHA2_384 23
  3234. +#define CRYPTO_SHA2_512 24
  3235. +#define CRYPTO_RIPEMD160 25
  3236. +#define CRYPTO_ALGORITHM_MAX 25 /* Keep updated - see below */
  3237. +
  3238. +/* Algorithm flags */
  3239. +#define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
  3240. +#define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
  3241. +#define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
  3242. +
  3243. +/*
  3244. + * Crypto driver/device flags. They can set in the crid
  3245. + * parameter when creating a session or submitting a key
  3246. + * op to affect the device/driver assigned. If neither
  3247. + * of these are specified then the crid is assumed to hold
  3248. + * the driver id of an existing (and suitable) device that
  3249. + * must be used to satisfy the request.
  3250. + */
  3251. +#define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */
  3252. +#define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */
  3253. +
  3254. +/* NB: deprecated */
  3255. +struct session_op {
  3256. + u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
  3257. + u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
  3258. +
  3259. + u_int32_t keylen; /* cipher key */
  3260. + caddr_t key;
  3261. + int mackeylen; /* mac key */
  3262. + caddr_t mackey;
  3263. +
  3264. + u_int32_t ses; /* returns: session # */
  3265. +};
  3266. +
  3267. +struct session2_op {
  3268. + u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
  3269. + u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
  3270. +
  3271. + u_int32_t keylen; /* cipher key */
  3272. + caddr_t key;
  3273. + int mackeylen; /* mac key */
  3274. + caddr_t mackey;
  3275. +
  3276. + u_int32_t ses; /* returns: session # */
  3277. + int crid; /* driver id + flags (rw) */
  3278. + int pad[4]; /* for future expansion */
  3279. +};
  3280. +
  3281. +struct crypt_op {
  3282. + u_int32_t ses;
  3283. + u_int16_t op; /* i.e. COP_ENCRYPT */
  3284. +#define COP_NONE 0
  3285. +#define COP_ENCRYPT 1
  3286. +#define COP_DECRYPT 2
  3287. + u_int16_t flags;
  3288. +#define COP_F_BATCH 0x0008 /* Batch op if possible */
  3289. + u_int len;
  3290. + caddr_t src, dst; /* become iov[] inside kernel */
  3291. + caddr_t mac; /* must be big enough for chosen MAC */
  3292. + caddr_t iv;
  3293. +};
  3294. +
  3295. +/*
  3296. + * Parameters for looking up a crypto driver/device by
  3297. + * device name or by id. The latter are returned for
  3298. + * created sessions (crid) and completed key operations.
  3299. + */
  3300. +struct crypt_find_op {
  3301. + int crid; /* driver id + flags */
  3302. + char name[32]; /* device/driver name */
  3303. +};
  3304. +
  3305. +/* bignum parameter, in packed bytes, ... */
  3306. +struct crparam {
  3307. + caddr_t crp_p;
  3308. + u_int crp_nbits;
  3309. +};
  3310. +
  3311. +#define CRK_MAXPARAM 8
  3312. +
  3313. +struct crypt_kop {
  3314. + u_int crk_op; /* ie. CRK_MOD_EXP or other */
  3315. + u_int crk_status; /* return status */
  3316. + u_short crk_iparams; /* # of input parameters */
  3317. + u_short crk_oparams; /* # of output parameters */
  3318. + u_int crk_crid; /* NB: only used by CIOCKEY2 (rw) */
  3319. + struct crparam crk_param[CRK_MAXPARAM];
  3320. +};
  3321. +#define CRK_ALGORITM_MIN 0
  3322. +#define CRK_MOD_EXP 0
  3323. +#define CRK_MOD_EXP_CRT 1
  3324. +#define CRK_DSA_SIGN 2
  3325. +#define CRK_DSA_VERIFY 3
  3326. +#define CRK_DH_COMPUTE_KEY 4
  3327. +#define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */
  3328. +
  3329. +#define CRF_MOD_EXP (1 << CRK_MOD_EXP)
  3330. +#define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
  3331. +#define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
  3332. +#define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
  3333. +#define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
  3334. +
  3335. +/*
  3336. + * done against open of /dev/crypto, to get a cloned descriptor.
  3337. + * Please use F_SETFD against the cloned descriptor.
  3338. + */
  3339. +#define CRIOGET _IOWR('c', 100, u_int32_t)
  3340. +#define CRIOASYMFEAT CIOCASYMFEAT
  3341. +#define CRIOFINDDEV CIOCFINDDEV
  3342. +
  3343. +/* the following are done against the cloned descriptor */
  3344. +#define CIOCGSESSION _IOWR('c', 101, struct session_op)
  3345. +#define CIOCFSESSION _IOW('c', 102, u_int32_t)
  3346. +#define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
  3347. +#define CIOCKEY _IOWR('c', 104, struct crypt_kop)
  3348. +#define CIOCASYMFEAT _IOR('c', 105, u_int32_t)
  3349. +#define CIOCGSESSION2 _IOWR('c', 106, struct session2_op)
  3350. +#define CIOCKEY2 _IOWR('c', 107, struct crypt_kop)
  3351. +#define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op)
  3352. +
  3353. +struct cryptotstat {
  3354. + struct timespec acc; /* total accumulated time */
  3355. + struct timespec min; /* min time */
  3356. + struct timespec max; /* max time */
  3357. + u_int32_t count; /* number of observations */
  3358. +};
  3359. +
  3360. +struct cryptostats {
  3361. + u_int32_t cs_ops; /* symmetric crypto ops submitted */
  3362. + u_int32_t cs_errs; /* symmetric crypto ops that failed */
  3363. + u_int32_t cs_kops; /* asymetric/key ops submitted */
  3364. + u_int32_t cs_kerrs; /* asymetric/key ops that failed */
  3365. + u_int32_t cs_intrs; /* crypto swi thread activations */
  3366. + u_int32_t cs_rets; /* crypto return thread activations */
  3367. + u_int32_t cs_blocks; /* symmetric op driver block */
  3368. + u_int32_t cs_kblocks; /* symmetric op driver block */
  3369. + /*
  3370. + * When CRYPTO_TIMING is defined at compile time and the
  3371. + * sysctl debug.crypto is set to 1, the crypto system will
  3372. + * accumulate statistics about how long it takes to process
  3373. + * crypto requests at various points during processing.
  3374. + */
  3375. + struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */
  3376. + struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */
  3377. + struct cryptotstat cs_cb; /* crypto_done -> callback */
  3378. + struct cryptotstat cs_finis; /* callback -> callback return */
  3379. +
  3380. + u_int32_t cs_drops; /* crypto ops dropped due to congestion */
  3381. +};
  3382. +
  3383. +#ifdef __KERNEL__
  3384. +
  3385. +/* Standard initialization structure beginning */
  3386. +struct cryptoini {
  3387. + int cri_alg; /* Algorithm to use */
  3388. + int cri_klen; /* Key length, in bits */
  3389. + int cri_mlen; /* Number of bytes we want from the
  3390. + entire hash. 0 means all. */
  3391. + caddr_t cri_key; /* key to use */
  3392. + u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */
  3393. + struct cryptoini *cri_next;
  3394. +};
  3395. +
  3396. +/* Describe boundaries of a single crypto operation */
  3397. +struct cryptodesc {
  3398. + int crd_skip; /* How many bytes to ignore from start */
  3399. + int crd_len; /* How many bytes to process */
  3400. + int crd_inject; /* Where to inject results, if applicable */
  3401. + int crd_flags;
  3402. +
  3403. +#define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */
  3404. +#define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in
  3405. + place, so don't copy. */
  3406. +#define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */
  3407. +#define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */
  3408. +#define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */
  3409. +#define CRD_F_COMP 0x0f /* Set when doing compression */
  3410. +
  3411. + struct cryptoini CRD_INI; /* Initialization/context data */
  3412. +#define crd_iv CRD_INI.cri_iv
  3413. +#define crd_key CRD_INI.cri_key
  3414. +#define crd_alg CRD_INI.cri_alg
  3415. +#define crd_klen CRD_INI.cri_klen
  3416. +
  3417. + struct cryptodesc *crd_next;
  3418. +};
  3419. +
  3420. +/* Structure describing complete operation */
  3421. +struct cryptop {
  3422. + struct list_head crp_next;
  3423. + wait_queue_head_t crp_waitq;
  3424. +
  3425. + u_int64_t crp_sid; /* Session ID */
  3426. + int crp_ilen; /* Input data total length */
  3427. + int crp_olen; /* Result total length */
  3428. +
  3429. + int crp_etype; /*
  3430. + * Error type (zero means no error).
  3431. + * All error codes except EAGAIN
  3432. + * indicate possible data corruption (as in,
  3433. + * the data have been touched). On all
  3434. + * errors, the crp_sid may have changed
  3435. + * (reset to a new one), so the caller
  3436. + * should always check and use the new
  3437. + * value on future requests.
  3438. + */
  3439. + int crp_flags;
  3440. +
  3441. +#define CRYPTO_F_SKBUF 0x0001 /* Input/output are skbuf chains */
  3442. +#define CRYPTO_F_IOV 0x0002 /* Input/output are uio */
  3443. +#define CRYPTO_F_REL 0x0004 /* Must return data in same place */
  3444. +#define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */
  3445. +#define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
  3446. +#define CRYPTO_F_DONE 0x0020 /* Operation completed */
  3447. +#define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
  3448. +
  3449. + caddr_t crp_buf; /* Data to be processed */
  3450. + caddr_t crp_opaque; /* Opaque pointer, passed along */
  3451. + struct cryptodesc *crp_desc; /* Linked list of processing descriptors */
  3452. +
  3453. + int (*crp_callback)(struct cryptop *); /* Callback function */
  3454. +};
  3455. +
  3456. +#define CRYPTO_BUF_CONTIG 0x0
  3457. +#define CRYPTO_BUF_IOV 0x1
  3458. +#define CRYPTO_BUF_SKBUF 0x2
  3459. +
  3460. +#define CRYPTO_OP_DECRYPT 0x0
  3461. +#define CRYPTO_OP_ENCRYPT 0x1
  3462. +
  3463. +/*
  3464. + * Hints passed to process methods.
  3465. + */
  3466. +#define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
  3467. +
  3468. +struct cryptkop {
  3469. + struct list_head krp_next;
  3470. + wait_queue_head_t krp_waitq;
  3471. +
  3472. + int krp_flags;
  3473. +#define CRYPTO_KF_DONE 0x0001 /* Operation completed */
  3474. +#define CRYPTO_KF_CBIMM 0x0002 /* Do callback immediately */
  3475. +
  3476. + u_int krp_op; /* ie. CRK_MOD_EXP or other */
  3477. + u_int krp_status; /* return status */
  3478. + u_short krp_iparams; /* # of input parameters */
  3479. + u_short krp_oparams; /* # of output parameters */
  3480. + u_int krp_crid; /* desired device, etc. */
  3481. + u_int32_t krp_hid;
  3482. + struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
  3483. + int (*krp_callback)(struct cryptkop *);
  3484. +};
  3485. +
  3486. +#include <ocf-compat.h>
  3487. +
  3488. +/*
  3489. + * Session ids are 64 bits. The lower 32 bits contain a "local id" which
  3490. + * is a driver-private session identifier. The upper 32 bits contain a
  3491. + * "hardware id" used by the core crypto code to identify the driver and
  3492. + * a copy of the driver's capabilities that can be used by client code to
  3493. + * optimize operation.
  3494. + */
  3495. +#define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0x00ffffff)
  3496. +#define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 32) & 0xff000000)
  3497. +#define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff)
  3498. +
  3499. +extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
  3500. +extern int crypto_freesession(u_int64_t sid);
  3501. +#define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE
  3502. +#define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE
  3503. +#define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */
  3504. +extern int32_t crypto_get_driverid(device_t dev, int flags);
  3505. +extern int crypto_find_driver(const char *);
  3506. +extern device_t crypto_find_device_byhid(int hid);
  3507. +extern int crypto_getcaps(int hid);
  3508. +extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
  3509. + u_int32_t flags);
  3510. +extern int crypto_kregister(u_int32_t, int, u_int32_t);
  3511. +extern int crypto_unregister(u_int32_t driverid, int alg);
  3512. +extern int crypto_unregister_all(u_int32_t driverid);
  3513. +extern int crypto_dispatch(struct cryptop *crp);
  3514. +extern int crypto_kdispatch(struct cryptkop *);
  3515. +#define CRYPTO_SYMQ 0x1
  3516. +#define CRYPTO_ASYMQ 0x2
  3517. +extern int crypto_unblock(u_int32_t, int);
  3518. +extern void crypto_done(struct cryptop *crp);
  3519. +extern void crypto_kdone(struct cryptkop *);
  3520. +extern int crypto_getfeat(int *);
  3521. +
  3522. +extern void crypto_freereq(struct cryptop *crp);
  3523. +extern struct cryptop *crypto_getreq(int num);
  3524. +
  3525. +extern int crypto_usercrypto; /* userland may do crypto requests */
  3526. +extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
  3527. +extern int crypto_devallowsoft; /* only use hardware crypto */
  3528. +
  3529. +/*
  3530. + * random number support, crypto_unregister_all will unregister
  3531. + */
  3532. +extern int crypto_rregister(u_int32_t driverid,
  3533. + int (*read_random)(void *arg, u_int32_t *buf, int len), void *arg);
  3534. +extern int crypto_runregister_all(u_int32_t driverid);
  3535. +
  3536. +/*
  3537. + * Crypto-related utility routines used mainly by drivers.
  3538. + *
  3539. + * XXX these don't really belong here; but for now they're
  3540. + * kept apart from the rest of the system.
  3541. + */
  3542. +struct uio;
  3543. +extern void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
  3544. +extern void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp);
  3545. +extern struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
  3546. +
  3547. +extern void crypto_copyback(int flags, caddr_t buf, int off, int size,
  3548. + caddr_t in);
  3549. +extern void crypto_copydata(int flags, caddr_t buf, int off, int size,
  3550. + caddr_t out);
  3551. +extern int crypto_apply(int flags, caddr_t buf, int off, int len,
  3552. + int (*f)(void *, void *, u_int), void *arg);
  3553. +
  3554. +#endif /* __KERNEL__ */
  3555. +#endif /* _CRYPTO_CRYPTO_H_ */
  3556. diff -Nur linux-2.6.30.orig/crypto/ocf/cryptosoft.c linux-2.6.30/crypto/ocf/cryptosoft.c
  3557. --- linux-2.6.30.orig/crypto/ocf/cryptosoft.c 1970-01-01 01:00:00.000000000 +0100
  3558. +++ linux-2.6.30/crypto/ocf/cryptosoft.c 2009-06-11 10:55:27.000000000 +0200
  3559. @@ -0,0 +1,898 @@
  3560. +/*
  3561. + * An OCF module that uses the linux kernel cryptoapi, based on the
  3562. + * original cryptosoft for BSD by Angelos D. Keromytis (angelos@cis.upenn.edu)
  3563. + * but is mostly unrecognisable,
  3564. + *
  3565. + * Written by David McCullough <david_mccullough@securecomputing.com>
  3566. + * Copyright (C) 2004-2007 David McCullough
  3567. + * Copyright (C) 2004-2005 Intel Corporation.
  3568. + *
  3569. + * LICENSE TERMS
  3570. + *
  3571. + * The free distribution and use of this software in both source and binary
  3572. + * form is allowed (with or without changes) provided that:
  3573. + *
  3574. + * 1. distributions of this source code include the above copyright
  3575. + * notice, this list of conditions and the following disclaimer;
  3576. + *
  3577. + * 2. distributions in binary form include the above copyright
  3578. + * notice, this list of conditions and the following disclaimer
  3579. + * in the documentation and/or other associated materials;
  3580. + *
  3581. + * 3. the copyright holder's name is not used to endorse products
  3582. + * built using this software without specific written permission.
  3583. + *
  3584. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  3585. + * may be distributed under the terms of the GNU General Public License (GPL),
  3586. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  3587. + *
  3588. + * DISCLAIMER
  3589. + *
  3590. + * This software is provided 'as is' with no explicit or implied warranties
  3591. + * in respect of its properties, including, but not limited to, correctness
  3592. + * and/or fitness for purpose.
  3593. + * ---------------------------------------------------------------------------
  3594. + */
  3595. +
  3596. +#ifndef AUTOCONF_INCLUDED
  3597. +#include <linux/config.h>
  3598. +#endif
  3599. +#include <linux/module.h>
  3600. +#include <linux/init.h>
  3601. +#include <linux/list.h>
  3602. +#include <linux/slab.h>
  3603. +#include <linux/sched.h>
  3604. +#include <linux/wait.h>
  3605. +#include <linux/crypto.h>
  3606. +#include <linux/mm.h>
  3607. +#include <linux/skbuff.h>
  3608. +#include <linux/random.h>
  3609. +#include <linux/scatterlist.h>
  3610. +
  3611. +#include <cryptodev.h>
  3612. +#include <uio.h>
  3613. +
  3614. +struct {
  3615. + softc_device_decl sc_dev;
  3616. +} swcr_softc;
  3617. +
  3618. +#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  3619. +
  3620. +/* Software session entry */
  3621. +
  3622. +#define SW_TYPE_CIPHER 0
  3623. +#define SW_TYPE_HMAC 1
  3624. +#define SW_TYPE_AUTH2 2
  3625. +#define SW_TYPE_HASH 3
  3626. +#define SW_TYPE_COMP 4
  3627. +#define SW_TYPE_BLKCIPHER 5
  3628. +
  3629. +struct swcr_data {
  3630. + int sw_type;
  3631. + int sw_alg;
  3632. + struct crypto_tfm *sw_tfm;
  3633. + union {
  3634. + struct {
  3635. + char *sw_key;
  3636. + int sw_klen;
  3637. + int sw_mlen;
  3638. + } hmac;
  3639. + void *sw_comp_buf;
  3640. + } u;
  3641. + struct swcr_data *sw_next;
  3642. +};
  3643. +
  3644. +#ifndef CRYPTO_TFM_MODE_CBC
  3645. +/*
  3646. + * As of linux-2.6.21 this is no longer defined, and presumably no longer
  3647. + * needed to be passed into the crypto core code.
  3648. + */
  3649. +#define CRYPTO_TFM_MODE_CBC 0
  3650. +#define CRYPTO_TFM_MODE_ECB 0
  3651. +#endif
  3652. +
  3653. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
  3654. + /*
  3655. + * Linux 2.6.19 introduced a new Crypto API, setup macro's to convert new
  3656. + * API into old API.
  3657. + */
  3658. +
  3659. + /* Symmetric/Block Cipher */
  3660. + struct blkcipher_desc
  3661. + {
  3662. + struct crypto_tfm *tfm;
  3663. + void *info;
  3664. + };
  3665. + #define ecb(X) #X
  3666. + #define cbc(X) #X
  3667. + #define crypto_has_blkcipher(X, Y, Z) crypto_alg_available(X, 0)
  3668. + #define crypto_blkcipher_cast(X) X
  3669. + #define crypto_blkcipher_tfm(X) X
  3670. + #define crypto_alloc_blkcipher(X, Y, Z) crypto_alloc_tfm(X, mode)
  3671. + #define crypto_blkcipher_ivsize(X) crypto_tfm_alg_ivsize(X)
  3672. + #define crypto_blkcipher_blocksize(X) crypto_tfm_alg_blocksize(X)
  3673. + #define crypto_blkcipher_setkey(X, Y, Z) crypto_cipher_setkey(X, Y, Z)
  3674. + #define crypto_blkcipher_encrypt_iv(W, X, Y, Z) \
  3675. + crypto_cipher_encrypt_iv((W)->tfm, X, Y, Z, (u8 *)((W)->info))
  3676. + #define crypto_blkcipher_decrypt_iv(W, X, Y, Z) \
  3677. + crypto_cipher_decrypt_iv((W)->tfm, X, Y, Z, (u8 *)((W)->info))
  3678. +
  3679. + /* Hash/HMAC/Digest */
  3680. + struct hash_desc
  3681. + {
  3682. + struct crypto_tfm *tfm;
  3683. + };
  3684. + #define hmac(X) #X
  3685. + #define crypto_has_hash(X, Y, Z) crypto_alg_available(X, 0)
  3686. + #define crypto_hash_cast(X) X
  3687. + #define crypto_hash_tfm(X) X
  3688. + #define crypto_alloc_hash(X, Y, Z) crypto_alloc_tfm(X, mode)
  3689. + #define crypto_hash_digestsize(X) crypto_tfm_alg_digestsize(X)
  3690. + #define crypto_hash_digest(W, X, Y, Z) \
  3691. + crypto_digest_digest((W)->tfm, X, sg_num, Z)
  3692. +
  3693. + /* Asymmetric Cipher */
  3694. + #define crypto_has_cipher(X, Y, Z) crypto_alg_available(X, 0)
  3695. +
  3696. + /* Compression */
  3697. + #define crypto_has_comp(X, Y, Z) crypto_alg_available(X, 0)
  3698. + #define crypto_comp_tfm(X) X
  3699. + #define crypto_comp_cast(X) X
  3700. + #define crypto_alloc_comp(X, Y, Z) crypto_alloc_tfm(X, mode)
  3701. +#else
  3702. + #define ecb(X) "ecb(" #X ")"
  3703. + #define cbc(X) "cbc(" #X ")"
  3704. + #define hmac(X) "hmac(" #X ")"
  3705. +#endif /* if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) */
  3706. +
  3707. +struct crypto_details
  3708. +{
  3709. + char *alg_name;
  3710. + int mode;
  3711. + int sw_type;
  3712. +};
  3713. +
  3714. +/*
  3715. + * This needs to be kept updated with CRYPTO_xxx list (cryptodev.h).
  3716. + * If the Algorithm is not supported, then insert a {NULL, 0, 0} entry.
  3717. + *
  3718. + * IMPORTANT: The index to the array IS CRYPTO_xxx.
  3719. + */
  3720. +static struct crypto_details crypto_details[CRYPTO_ALGORITHM_MAX + 1] = {
  3721. + { NULL, 0, 0 },
  3722. + /* CRYPTO_xxx index starts at 1 */
  3723. + { cbc(des), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3724. + { cbc(des3_ede), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3725. + { cbc(blowfish), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3726. + { cbc(cast5), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3727. + { cbc(skipjack), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3728. + { hmac(md5), 0, SW_TYPE_HMAC },
  3729. + { hmac(sha1), 0, SW_TYPE_HMAC },
  3730. + { hmac(ripemd160), 0, SW_TYPE_HMAC },
  3731. + { "md5-kpdk??", 0, SW_TYPE_HASH },
  3732. + { "sha1-kpdk??", 0, SW_TYPE_HASH },
  3733. + { cbc(aes), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3734. + { ecb(arc4), CRYPTO_TFM_MODE_ECB, SW_TYPE_BLKCIPHER },
  3735. + { "md5", 0, SW_TYPE_HASH },
  3736. + { "sha1", 0, SW_TYPE_HASH },
  3737. + { hmac(digest_null), 0, SW_TYPE_HMAC },
  3738. + { cbc(cipher_null), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3739. + { "deflate", 0, SW_TYPE_COMP },
  3740. + { hmac(sha256), 0, SW_TYPE_HMAC },
  3741. + { hmac(sha384), 0, SW_TYPE_HMAC },
  3742. + { hmac(sha512), 0, SW_TYPE_HMAC },
  3743. + { cbc(camellia), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  3744. + { "sha256", 0, SW_TYPE_HASH },
  3745. + { "sha384", 0, SW_TYPE_HASH },
  3746. + { "sha512", 0, SW_TYPE_HASH },
  3747. + { "ripemd160", 0, SW_TYPE_HASH },
  3748. +};
  3749. +
  3750. +int32_t swcr_id = -1;
  3751. +module_param(swcr_id, int, 0444);
  3752. +MODULE_PARM_DESC(swcr_id, "Read-Only OCF ID for cryptosoft driver");
  3753. +
  3754. +int swcr_fail_if_compression_grows = 1;
  3755. +module_param(swcr_fail_if_compression_grows, int, 0644);
  3756. +MODULE_PARM_DESC(swcr_fail_if_compression_grows,
  3757. + "Treat compression that results in more data as a failure");
  3758. +
  3759. +static struct swcr_data **swcr_sessions = NULL;
  3760. +static u_int32_t swcr_sesnum = 0;
  3761. +
  3762. +static int swcr_process(device_t, struct cryptop *, int);
  3763. +static int swcr_newsession(device_t, u_int32_t *, struct cryptoini *);
  3764. +static int swcr_freesession(device_t, u_int64_t);
  3765. +
  3766. +static device_method_t swcr_methods = {
  3767. + /* crypto device methods */
  3768. + DEVMETHOD(cryptodev_newsession, swcr_newsession),
  3769. + DEVMETHOD(cryptodev_freesession,swcr_freesession),
  3770. + DEVMETHOD(cryptodev_process, swcr_process),
  3771. +};
  3772. +
  3773. +#define debug swcr_debug
  3774. +int swcr_debug = 0;
  3775. +module_param(swcr_debug, int, 0644);
  3776. +MODULE_PARM_DESC(swcr_debug, "Enable debug");
  3777. +
  3778. +/*
  3779. + * Generate a new software session.
  3780. + */
  3781. +static int
  3782. +swcr_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
  3783. +{
  3784. + struct swcr_data **swd;
  3785. + u_int32_t i;
  3786. + int error;
  3787. + char *algo;
  3788. + int mode, sw_type;
  3789. +
  3790. + dprintk("%s()\n", __FUNCTION__);
  3791. + if (sid == NULL || cri == NULL) {
  3792. + dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  3793. + return EINVAL;
  3794. + }
  3795. +
  3796. + if (swcr_sessions) {
  3797. + for (i = 1; i < swcr_sesnum; i++)
  3798. + if (swcr_sessions[i] == NULL)
  3799. + break;
  3800. + } else
  3801. + i = 1; /* NB: to silence compiler warning */
  3802. +
  3803. + if (swcr_sessions == NULL || i == swcr_sesnum) {
  3804. + if (swcr_sessions == NULL) {
  3805. + i = 1; /* We leave swcr_sessions[0] empty */
  3806. + swcr_sesnum = CRYPTO_SW_SESSIONS;
  3807. + } else
  3808. + swcr_sesnum *= 2;
  3809. +
  3810. + swd = kmalloc(swcr_sesnum * sizeof(struct swcr_data *), SLAB_ATOMIC);
  3811. + if (swd == NULL) {
  3812. + /* Reset session number */
  3813. + if (swcr_sesnum == CRYPTO_SW_SESSIONS)
  3814. + swcr_sesnum = 0;
  3815. + else
  3816. + swcr_sesnum /= 2;
  3817. + dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  3818. + return ENOBUFS;
  3819. + }
  3820. + memset(swd, 0, swcr_sesnum * sizeof(struct swcr_data *));
  3821. +
  3822. + /* Copy existing sessions */
  3823. + if (swcr_sessions) {
  3824. + memcpy(swd, swcr_sessions,
  3825. + (swcr_sesnum / 2) * sizeof(struct swcr_data *));
  3826. + kfree(swcr_sessions);
  3827. + }
  3828. +
  3829. + swcr_sessions = swd;
  3830. + }
  3831. +
  3832. + swd = &swcr_sessions[i];
  3833. + *sid = i;
  3834. +
  3835. + while (cri) {
  3836. + *swd = (struct swcr_data *) kmalloc(sizeof(struct swcr_data),
  3837. + SLAB_ATOMIC);
  3838. + if (*swd == NULL) {
  3839. + swcr_freesession(NULL, i);
  3840. + dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  3841. + return ENOBUFS;
  3842. + }
  3843. + memset(*swd, 0, sizeof(struct swcr_data));
  3844. +
  3845. + if (cri->cri_alg > CRYPTO_ALGORITHM_MAX) {
  3846. + printk("cryptosoft: Unknown algorithm 0x%x\n", cri->cri_alg);
  3847. + swcr_freesession(NULL, i);
  3848. + return EINVAL;
  3849. + }
  3850. +
  3851. + algo = crypto_details[cri->cri_alg].alg_name;
  3852. + if (!algo || !*algo) {
  3853. + printk("cryptosoft: Unsupported algorithm 0x%x\n", cri->cri_alg);
  3854. + swcr_freesession(NULL, i);
  3855. + return EINVAL;
  3856. + }
  3857. +
  3858. + mode = crypto_details[cri->cri_alg].mode;
  3859. + sw_type = crypto_details[cri->cri_alg].sw_type;
  3860. +
  3861. + /* Algorithm specific configuration */
  3862. + switch (cri->cri_alg) {
  3863. + case CRYPTO_NULL_CBC:
  3864. + cri->cri_klen = 0; /* make it work with crypto API */
  3865. + break;
  3866. + default:
  3867. + break;
  3868. + }
  3869. +
  3870. + if (sw_type == SW_TYPE_BLKCIPHER) {
  3871. + dprintk("%s crypto_alloc_blkcipher(%s, 0x%x)\n", __FUNCTION__,
  3872. + algo, mode);
  3873. +
  3874. + (*swd)->sw_tfm = crypto_blkcipher_tfm(
  3875. + crypto_alloc_blkcipher(algo, 0,
  3876. + CRYPTO_ALG_ASYNC));
  3877. + if (!(*swd)->sw_tfm) {
  3878. + dprintk("cryptosoft: crypto_alloc_blkcipher failed(%s,0x%x)\n",
  3879. + algo,mode);
  3880. + swcr_freesession(NULL, i);
  3881. + return EINVAL;
  3882. + }
  3883. +
  3884. + if (debug) {
  3885. + dprintk("%s key:cri->cri_klen=%d,(cri->cri_klen + 7)/8=%d",
  3886. + __FUNCTION__,cri->cri_klen,(cri->cri_klen + 7)/8);
  3887. + for (i = 0; i < (cri->cri_klen + 7) / 8; i++)
  3888. + {
  3889. + dprintk("%s0x%x", (i % 8) ? " " : "\n ",cri->cri_key[i]);
  3890. + }
  3891. + dprintk("\n");
  3892. + }
  3893. + error = crypto_blkcipher_setkey(
  3894. + crypto_blkcipher_cast((*swd)->sw_tfm), cri->cri_key,
  3895. + (cri->cri_klen + 7) / 8);
  3896. + if (error) {
  3897. + printk("cryptosoft: setkey failed %d (crt_flags=0x%x)\n", error,
  3898. + (*swd)->sw_tfm->crt_flags);
  3899. + swcr_freesession(NULL, i);
  3900. + return error;
  3901. + }
  3902. + } else if (sw_type == SW_TYPE_HMAC || sw_type == SW_TYPE_HASH) {
  3903. + dprintk("%s crypto_alloc_hash(%s, 0x%x)\n", __FUNCTION__,
  3904. + algo, mode);
  3905. +
  3906. + (*swd)->sw_tfm = crypto_hash_tfm(
  3907. + crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC));
  3908. +
  3909. + if (!(*swd)->sw_tfm) {
  3910. + dprintk("cryptosoft: crypto_alloc_hash failed(%s,0x%x)\n",
  3911. + algo, mode);
  3912. + swcr_freesession(NULL, i);
  3913. + return EINVAL;
  3914. + }
  3915. +
  3916. + (*swd)->u.hmac.sw_klen = (cri->cri_klen + 7) / 8;
  3917. + (*swd)->u.hmac.sw_key = (char *)kmalloc((*swd)->u.hmac.sw_klen,
  3918. + SLAB_ATOMIC);
  3919. + if ((*swd)->u.hmac.sw_key == NULL) {
  3920. + swcr_freesession(NULL, i);
  3921. + dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  3922. + return ENOBUFS;
  3923. + }
  3924. + memcpy((*swd)->u.hmac.sw_key, cri->cri_key, (*swd)->u.hmac.sw_klen);
  3925. + if (cri->cri_mlen) {
  3926. + (*swd)->u.hmac.sw_mlen = cri->cri_mlen;
  3927. + } else {
  3928. + (*swd)->u.hmac.sw_mlen =
  3929. + crypto_hash_digestsize(
  3930. + crypto_hash_cast((*swd)->sw_tfm));
  3931. + }
  3932. + } else if (sw_type == SW_TYPE_COMP) {
  3933. + (*swd)->sw_tfm = crypto_comp_tfm(
  3934. + crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC));
  3935. + if (!(*swd)->sw_tfm) {
  3936. + dprintk("cryptosoft: crypto_alloc_comp failed(%s,0x%x)\n",
  3937. + algo, mode);
  3938. + swcr_freesession(NULL, i);
  3939. + return EINVAL;
  3940. + }
  3941. + (*swd)->u.sw_comp_buf = kmalloc(CRYPTO_MAX_DATA_LEN, SLAB_ATOMIC);
  3942. + if ((*swd)->u.sw_comp_buf == NULL) {
  3943. + swcr_freesession(NULL, i);
  3944. + dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  3945. + return ENOBUFS;
  3946. + }
  3947. + } else {
  3948. + printk("cryptosoft: Unhandled sw_type %d\n", sw_type);
  3949. + swcr_freesession(NULL, i);
  3950. + return EINVAL;
  3951. + }
  3952. +
  3953. + (*swd)->sw_alg = cri->cri_alg;
  3954. + (*swd)->sw_type = sw_type;
  3955. +
  3956. + cri = cri->cri_next;
  3957. + swd = &((*swd)->sw_next);
  3958. + }
  3959. + return 0;
  3960. +}
  3961. +
  3962. +/*
  3963. + * Free a session.
  3964. + */
  3965. +static int
  3966. +swcr_freesession(device_t dev, u_int64_t tid)
  3967. +{
  3968. + struct swcr_data *swd;
  3969. + u_int32_t sid = CRYPTO_SESID2LID(tid);
  3970. +
  3971. + dprintk("%s()\n", __FUNCTION__);
  3972. + if (sid > swcr_sesnum || swcr_sessions == NULL ||
  3973. + swcr_sessions[sid] == NULL) {
  3974. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  3975. + return(EINVAL);
  3976. + }
  3977. +
  3978. + /* Silently accept and return */
  3979. + if (sid == 0)
  3980. + return(0);
  3981. +
  3982. + while ((swd = swcr_sessions[sid]) != NULL) {
  3983. + swcr_sessions[sid] = swd->sw_next;
  3984. + if (swd->sw_tfm)
  3985. + crypto_free_tfm(swd->sw_tfm);
  3986. + if (swd->sw_type == SW_TYPE_COMP) {
  3987. + if (swd->u.sw_comp_buf)
  3988. + kfree(swd->u.sw_comp_buf);
  3989. + } else {
  3990. + if (swd->u.hmac.sw_key)
  3991. + kfree(swd->u.hmac.sw_key);
  3992. + }
  3993. + kfree(swd);
  3994. + }
  3995. + return 0;
  3996. +}
  3997. +
  3998. +/*
  3999. + * Process a software request.
  4000. + */
  4001. +static int
  4002. +swcr_process(device_t dev, struct cryptop *crp, int hint)
  4003. +{
  4004. + struct cryptodesc *crd;
  4005. + struct swcr_data *sw;
  4006. + u_int32_t lid;
  4007. +#define SCATTERLIST_MAX 16
  4008. + struct scatterlist sg[SCATTERLIST_MAX];
  4009. + int sg_num, sg_len, skip;
  4010. + struct sk_buff *skb = NULL;
  4011. + struct uio *uiop = NULL;
  4012. +
  4013. + dprintk("%s()\n", __FUNCTION__);
  4014. + /* Sanity check */
  4015. + if (crp == NULL) {
  4016. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  4017. + return EINVAL;
  4018. + }
  4019. +
  4020. + crp->crp_etype = 0;
  4021. +
  4022. + if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  4023. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  4024. + crp->crp_etype = EINVAL;
  4025. + goto done;
  4026. + }
  4027. +
  4028. + lid = crp->crp_sid & 0xffffffff;
  4029. + if (lid >= swcr_sesnum || lid == 0 || swcr_sessions == NULL ||
  4030. + swcr_sessions[lid] == NULL) {
  4031. + crp->crp_etype = ENOENT;
  4032. + dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  4033. + goto done;
  4034. + }
  4035. +
  4036. + /*
  4037. + * do some error checking outside of the loop for SKB and IOV processing
  4038. + * this leaves us with valid skb or uiop pointers for later
  4039. + */
  4040. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  4041. + skb = (struct sk_buff *) crp->crp_buf;
  4042. + if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
  4043. + printk("%s,%d: %d nr_frags > SCATTERLIST_MAX", __FILE__, __LINE__,
  4044. + skb_shinfo(skb)->nr_frags);
  4045. + goto done;
  4046. + }
  4047. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  4048. + uiop = (struct uio *) crp->crp_buf;
  4049. + if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
  4050. + printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX", __FILE__, __LINE__,
  4051. + uiop->uio_iovcnt);
  4052. + goto done;
  4053. + }
  4054. + }
  4055. +
  4056. + /* Go through crypto descriptors, processing as we go */
  4057. + for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  4058. + /*
  4059. + * Find the crypto context.
  4060. + *
  4061. + * XXX Note that the logic here prevents us from having
  4062. + * XXX the same algorithm multiple times in a session
  4063. + * XXX (or rather, we can but it won't give us the right
  4064. + * XXX results). To do that, we'd need some way of differentiating
  4065. + * XXX between the various instances of an algorithm (so we can
  4066. + * XXX locate the correct crypto context).
  4067. + */
  4068. + for (sw = swcr_sessions[lid]; sw && sw->sw_alg != crd->crd_alg;
  4069. + sw = sw->sw_next)
  4070. + ;
  4071. +
  4072. + /* No such context ? */
  4073. + if (sw == NULL) {
  4074. + crp->crp_etype = EINVAL;
  4075. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  4076. + goto done;
  4077. + }
  4078. +
  4079. + skip = crd->crd_skip;
  4080. +
  4081. + /*
  4082. + * setup the SG list skip from the start of the buffer
  4083. + */
  4084. + memset(sg, 0, sizeof(sg));
  4085. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  4086. + int i, len;
  4087. +
  4088. + sg_num = 0;
  4089. + sg_len = 0;
  4090. +
  4091. + if (skip < skb_headlen(skb)) {
  4092. + len = skb_headlen(skb) - skip;
  4093. + if (len + sg_len > crd->crd_len)
  4094. + len = crd->crd_len - sg_len;
  4095. + sg_set_page(&sg[sg_num],
  4096. + virt_to_page(skb->data + skip), len,
  4097. + offset_in_page(skb->data + skip));
  4098. + sg_len += len;
  4099. + sg_num++;
  4100. + skip = 0;
  4101. + } else
  4102. + skip -= skb_headlen(skb);
  4103. +
  4104. + for (i = 0; sg_len < crd->crd_len &&
  4105. + i < skb_shinfo(skb)->nr_frags &&
  4106. + sg_num < SCATTERLIST_MAX; i++) {
  4107. + if (skip < skb_shinfo(skb)->frags[i].size) {
  4108. + len = skb_shinfo(skb)->frags[i].size - skip;
  4109. + if (len + sg_len > crd->crd_len)
  4110. + len = crd->crd_len - sg_len;
  4111. + sg_set_page(&sg[sg_num],
  4112. + skb_shinfo(skb)->frags[i].page,
  4113. + len,
  4114. + skb_shinfo(skb)->frags[i].page_offset + skip);
  4115. + sg_len += len;
  4116. + sg_num++;
  4117. + skip = 0;
  4118. + } else
  4119. + skip -= skb_shinfo(skb)->frags[i].size;
  4120. + }
  4121. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  4122. + int len;
  4123. +
  4124. + sg_len = 0;
  4125. + for (sg_num = 0; sg_len <= crd->crd_len &&
  4126. + sg_num < uiop->uio_iovcnt &&
  4127. + sg_num < SCATTERLIST_MAX; sg_num++) {
  4128. + if (skip <= uiop->uio_iov[sg_num].iov_len) {
  4129. + len = uiop->uio_iov[sg_num].iov_len - skip;
  4130. + if (len + sg_len > crd->crd_len)
  4131. + len = crd->crd_len - sg_len;
  4132. + sg_set_page(&sg[sg_num],
  4133. + virt_to_page(uiop->uio_iov[sg_num].iov_base+skip),
  4134. + len,
  4135. + offset_in_page(uiop->uio_iov[sg_num].iov_base+skip));
  4136. + sg_len += len;
  4137. + skip = 0;
  4138. + } else
  4139. + skip -= uiop->uio_iov[sg_num].iov_len;
  4140. + }
  4141. + } else {
  4142. + sg_len = (crp->crp_ilen - skip);
  4143. + if (sg_len > crd->crd_len)
  4144. + sg_len = crd->crd_len;
  4145. + sg_set_page(&sg[0], virt_to_page(crp->crp_buf + skip),
  4146. + sg_len, offset_in_page(crp->crp_buf + skip));
  4147. + sg_num = 1;
  4148. + }
  4149. +
  4150. +
  4151. + switch (sw->sw_type) {
  4152. + case SW_TYPE_BLKCIPHER: {
  4153. + unsigned char iv[EALG_MAX_BLOCK_LEN];
  4154. + unsigned char *ivp = iv;
  4155. + int ivsize =
  4156. + crypto_blkcipher_ivsize(crypto_blkcipher_cast(sw->sw_tfm));
  4157. + struct blkcipher_desc desc;
  4158. +
  4159. + if (sg_len < crypto_blkcipher_blocksize(
  4160. + crypto_blkcipher_cast(sw->sw_tfm))) {
  4161. + crp->crp_etype = EINVAL;
  4162. + dprintk("%s,%d: EINVAL len %d < %d\n", __FILE__, __LINE__,
  4163. + sg_len, crypto_blkcipher_blocksize(
  4164. + crypto_blkcipher_cast(sw->sw_tfm)));
  4165. + goto done;
  4166. + }
  4167. +
  4168. + if (ivsize > sizeof(iv)) {
  4169. + crp->crp_etype = EINVAL;
  4170. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  4171. + goto done;
  4172. + }
  4173. +
  4174. + if (crd->crd_flags & CRD_F_KEY_EXPLICIT) {
  4175. + int i, error;
  4176. +
  4177. + if (debug) {
  4178. + dprintk("%s key:", __FUNCTION__);
  4179. + for (i = 0; i < (crd->crd_klen + 7) / 8; i++)
  4180. + dprintk("%s0x%x", (i % 8) ? " " : "\n ",
  4181. + crd->crd_key[i]);
  4182. + dprintk("\n");
  4183. + }
  4184. + error = crypto_blkcipher_setkey(
  4185. + crypto_blkcipher_cast(sw->sw_tfm), crd->crd_key,
  4186. + (crd->crd_klen + 7) / 8);
  4187. + if (error) {
  4188. + dprintk("cryptosoft: setkey failed %d (crt_flags=0x%x)\n",
  4189. + error, sw->sw_tfm->crt_flags);
  4190. + crp->crp_etype = -error;
  4191. + }
  4192. + }
  4193. +
  4194. + memset(&desc, 0, sizeof(desc));
  4195. + desc.tfm = crypto_blkcipher_cast(sw->sw_tfm);
  4196. +
  4197. + if (crd->crd_flags & CRD_F_ENCRYPT) { /* encrypt */
  4198. +
  4199. + if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  4200. + ivp = crd->crd_iv;
  4201. + } else {
  4202. + get_random_bytes(ivp, ivsize);
  4203. + }
  4204. + /*
  4205. + * do we have to copy the IV back to the buffer ?
  4206. + */
  4207. + if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  4208. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  4209. + crd->crd_inject, ivsize, (caddr_t)ivp);
  4210. + }
  4211. + desc.info = ivp;
  4212. + crypto_blkcipher_encrypt_iv(&desc, sg, sg, sg_len);
  4213. +
  4214. + } else { /*decrypt */
  4215. +
  4216. + if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  4217. + ivp = crd->crd_iv;
  4218. + } else {
  4219. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  4220. + crd->crd_inject, ivsize, (caddr_t)ivp);
  4221. + }
  4222. + desc.info = ivp;
  4223. + crypto_blkcipher_decrypt_iv(&desc, sg, sg, sg_len);
  4224. + }
  4225. + } break;
  4226. + case SW_TYPE_HMAC:
  4227. + case SW_TYPE_HASH:
  4228. + {
  4229. + char result[HASH_MAX_LEN];
  4230. + struct hash_desc desc;
  4231. +
  4232. + /* check we have room for the result */
  4233. + if (crp->crp_ilen - crd->crd_inject < sw->u.hmac.sw_mlen) {
  4234. + dprintk(
  4235. + "cryptosoft: EINVAL crp_ilen=%d, len=%d, inject=%d digestsize=%d\n",
  4236. + crp->crp_ilen, crd->crd_skip + sg_len, crd->crd_inject,
  4237. + sw->u.hmac.sw_mlen);
  4238. + crp->crp_etype = EINVAL;
  4239. + goto done;
  4240. + }
  4241. +
  4242. + memset(&desc, 0, sizeof(desc));
  4243. + desc.tfm = crypto_hash_cast(sw->sw_tfm);
  4244. +
  4245. + memset(result, 0, sizeof(result));
  4246. +
  4247. + if (sw->sw_type == SW_TYPE_HMAC) {
  4248. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
  4249. + crypto_hmac(sw->sw_tfm, sw->u.hmac.sw_key, &sw->u.hmac.sw_klen,
  4250. + sg, sg_num, result);
  4251. +#else
  4252. + crypto_hash_setkey(desc.tfm, sw->u.hmac.sw_key,
  4253. + sw->u.hmac.sw_klen);
  4254. + crypto_hash_digest(&desc, sg, sg_len, result);
  4255. +#endif /* #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) */
  4256. +
  4257. + } else { /* SW_TYPE_HASH */
  4258. + crypto_hash_digest(&desc, sg, sg_len, result);
  4259. + }
  4260. +
  4261. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  4262. + crd->crd_inject, sw->u.hmac.sw_mlen, result);
  4263. + }
  4264. + break;
  4265. +
  4266. + case SW_TYPE_COMP: {
  4267. + void *ibuf = NULL;
  4268. + void *obuf = sw->u.sw_comp_buf;
  4269. + int ilen = sg_len, olen = CRYPTO_MAX_DATA_LEN;
  4270. + int ret = 0;
  4271. +
  4272. + /*
  4273. + * we need to use an additional copy if there is more than one
  4274. + * input chunk since the kernel comp routines do not handle
  4275. + * SG yet. Otherwise we just use the input buffer as is.
  4276. + * Rather than allocate another buffer we just split the tmp
  4277. + * buffer we already have.
  4278. + * Perhaps we should just use zlib directly ?
  4279. + */
  4280. + if (sg_num > 1) {
  4281. + int blk;
  4282. +
  4283. + ibuf = obuf;
  4284. + for (blk = 0; blk < sg_num; blk++) {
  4285. + memcpy(obuf, sg_virt(&sg[blk]),
  4286. + sg[blk].length);
  4287. + obuf += sg[blk].length;
  4288. + }
  4289. + olen -= sg_len;
  4290. + } else
  4291. + ibuf = sg_virt(&sg[0]);
  4292. +
  4293. + if (crd->crd_flags & CRD_F_ENCRYPT) { /* compress */
  4294. + ret = crypto_comp_compress(crypto_comp_cast(sw->sw_tfm),
  4295. + ibuf, ilen, obuf, &olen);
  4296. + if (!ret && olen > crd->crd_len) {
  4297. + dprintk("cryptosoft: ERANGE compress %d into %d\n",
  4298. + crd->crd_len, olen);
  4299. + if (swcr_fail_if_compression_grows)
  4300. + ret = ERANGE;
  4301. + }
  4302. + } else { /* decompress */
  4303. + ret = crypto_comp_decompress(crypto_comp_cast(sw->sw_tfm),
  4304. + ibuf, ilen, obuf, &olen);
  4305. + if (!ret && (olen + crd->crd_inject) > crp->crp_olen) {
  4306. + dprintk("cryptosoft: ETOOSMALL decompress %d into %d, "
  4307. + "space for %d,at offset %d\n",
  4308. + crd->crd_len, olen, crp->crp_olen, crd->crd_inject);
  4309. + ret = ETOOSMALL;
  4310. + }
  4311. + }
  4312. + if (ret)
  4313. + dprintk("%s,%d: ret = %d\n", __FILE__, __LINE__, ret);
  4314. +
  4315. + /*
  4316. + * on success copy result back,
  4317. + * linux crpyto API returns -errno, we need to fix that
  4318. + */
  4319. + crp->crp_etype = ret < 0 ? -ret : ret;
  4320. + if (ret == 0) {
  4321. + /* copy back the result and return it's size */
  4322. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  4323. + crd->crd_inject, olen, obuf);
  4324. + crp->crp_olen = olen;
  4325. + }
  4326. +
  4327. +
  4328. + } break;
  4329. +
  4330. + default:
  4331. + /* Unknown/unsupported algorithm */
  4332. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  4333. + crp->crp_etype = EINVAL;
  4334. + goto done;
  4335. + }
  4336. + }
  4337. +
  4338. +done:
  4339. + crypto_done(crp);
  4340. + return 0;
  4341. +}
  4342. +
  4343. +static int
  4344. +cryptosoft_init(void)
  4345. +{
  4346. + int i, sw_type, mode;
  4347. + char *algo;
  4348. +
  4349. + dprintk("%s(%p)\n", __FUNCTION__, cryptosoft_init);
  4350. +
  4351. + softc_device_init(&swcr_softc, "cryptosoft", 0, swcr_methods);
  4352. +
  4353. + swcr_id = crypto_get_driverid(softc_get_device(&swcr_softc),
  4354. + CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
  4355. + if (swcr_id < 0) {
  4356. + printk("Software crypto device cannot initialize!");
  4357. + return -ENODEV;
  4358. + }
  4359. +
  4360. +#define REGISTER(alg) \
  4361. + crypto_register(swcr_id, alg, 0,0);
  4362. +
  4363. + for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; ++i)
  4364. + {
  4365. +
  4366. + algo = crypto_details[i].alg_name;
  4367. + if (!algo || !*algo)
  4368. + {
  4369. + dprintk("%s:Algorithm %d not supported\n", __FUNCTION__, i);
  4370. + continue;
  4371. + }
  4372. +
  4373. + mode = crypto_details[i].mode;
  4374. + sw_type = crypto_details[i].sw_type;
  4375. +
  4376. + switch (sw_type)
  4377. + {
  4378. + case SW_TYPE_CIPHER:
  4379. + if (crypto_has_cipher(algo, 0, CRYPTO_ALG_ASYNC))
  4380. + {
  4381. + REGISTER(i);
  4382. + }
  4383. + else
  4384. + {
  4385. + dprintk("%s:CIPHER algorithm %d:'%s' not supported\n",
  4386. + __FUNCTION__, i, algo);
  4387. + }
  4388. + break;
  4389. + case SW_TYPE_HMAC:
  4390. + if (crypto_has_hash(algo, 0, CRYPTO_ALG_ASYNC))
  4391. + {
  4392. + REGISTER(i);
  4393. + }
  4394. + else
  4395. + {
  4396. + dprintk("%s:HMAC algorithm %d:'%s' not supported\n",
  4397. + __FUNCTION__, i, algo);
  4398. + }
  4399. + break;
  4400. + case SW_TYPE_HASH:
  4401. + if (crypto_has_hash(algo, 0, CRYPTO_ALG_ASYNC))
  4402. + {
  4403. + REGISTER(i);
  4404. + }
  4405. + else
  4406. + {
  4407. + dprintk("%s:HASH algorithm %d:'%s' not supported\n",
  4408. + __FUNCTION__, i, algo);
  4409. + }
  4410. + break;
  4411. + case SW_TYPE_COMP:
  4412. + if (crypto_has_comp(algo, 0, CRYPTO_ALG_ASYNC))
  4413. + {
  4414. + REGISTER(i);
  4415. + }
  4416. + else
  4417. + {
  4418. + dprintk("%s:COMP algorithm %d:'%s' not supported\n",
  4419. + __FUNCTION__, i, algo);
  4420. + }
  4421. + break;
  4422. + case SW_TYPE_BLKCIPHER:
  4423. + if (crypto_has_blkcipher(algo, 0, CRYPTO_ALG_ASYNC))
  4424. + {
  4425. + REGISTER(i);
  4426. + }
  4427. + else
  4428. + {
  4429. + dprintk("%s:BLKCIPHER algorithm %d:'%s' not supported\n",
  4430. + __FUNCTION__, i, algo);
  4431. + }
  4432. + break;
  4433. + default:
  4434. + dprintk(
  4435. + "%s:Algorithm Type %d not supported (algorithm %d:'%s')\n",
  4436. + __FUNCTION__, sw_type, i, algo);
  4437. + break;
  4438. + }
  4439. + }
  4440. +
  4441. + return(0);
  4442. +}
  4443. +
  4444. +static void
  4445. +cryptosoft_exit(void)
  4446. +{
  4447. + dprintk("%s()\n", __FUNCTION__);
  4448. + crypto_unregister_all(swcr_id);
  4449. + swcr_id = -1;
  4450. +}
  4451. +
  4452. +module_init(cryptosoft_init);
  4453. +module_exit(cryptosoft_exit);
  4454. +
  4455. +MODULE_LICENSE("Dual BSD/GPL");
  4456. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  4457. +MODULE_DESCRIPTION("Cryptosoft (OCF module for kernel crypto)");
  4458. diff -Nur linux-2.6.30.orig/crypto/ocf/ep80579/icp_asym.c linux-2.6.30/crypto/ocf/ep80579/icp_asym.c
  4459. --- linux-2.6.30.orig/crypto/ocf/ep80579/icp_asym.c 1970-01-01 01:00:00.000000000 +0100
  4460. +++ linux-2.6.30/crypto/ocf/ep80579/icp_asym.c 2009-06-11 10:55:27.000000000 +0200
  4461. @@ -0,0 +1,1375 @@
  4462. +/***************************************************************************
  4463. + *
  4464. + * This file is provided under a dual BSD/GPLv2 license. When using or
  4465. + * redistributing this file, you may do so under either license.
  4466. + *
  4467. + * GPL LICENSE SUMMARY
  4468. + *
  4469. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  4470. + *
  4471. + * This program is free software; you can redistribute it and/or modify
  4472. + * it under the terms of version 2 of the GNU General Public License as
  4473. + * published by the Free Software Foundation.
  4474. + *
  4475. + * This program is distributed in the hope that it will be useful, but
  4476. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  4477. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4478. + * General Public License for more details.
  4479. + *
  4480. + * You should have received a copy of the GNU General Public License
  4481. + * along with this program; if not, write to the Free Software
  4482. + * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  4483. + * The full GNU General Public License is included in this distribution
  4484. + * in the file called LICENSE.GPL.
  4485. + *
  4486. + * Contact Information:
  4487. + * Intel Corporation
  4488. + *
  4489. + * BSD LICENSE
  4490. + *
  4491. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  4492. + * All rights reserved.
  4493. + *
  4494. + * Redistribution and use in source and binary forms, with or without
  4495. + * modification, are permitted provided that the following conditions
  4496. + * are met:
  4497. + *
  4498. + * * Redistributions of source code must retain the above copyright
  4499. + * notice, this list of conditions and the following disclaimer.
  4500. + * * Redistributions in binary form must reproduce the above copyright
  4501. + * notice, this list of conditions and the following disclaimer in
  4502. + * the documentation and/or other materials provided with the
  4503. + * distribution.
  4504. + * * Neither the name of Intel Corporation nor the names of its
  4505. + * contributors may be used to endorse or promote products derived
  4506. + * from this software without specific prior written permission.
  4507. + *
  4508. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4509. + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4510. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4511. + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4512. + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4513. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4514. + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4515. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4516. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4517. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4518. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4519. + *
  4520. + *
  4521. + * version: Security.L.1.0.130
  4522. + *
  4523. + ***************************************************************************/
  4524. +
  4525. +#include "icp_ocf.h"
  4526. +
  4527. +/*The following define values (containing the word 'INDEX') are used to find
  4528. +the index of each input buffer of the crypto_kop struct (see OCF cryptodev.h).
  4529. +These values were found through analysis of the OCF OpenSSL patch. If the
  4530. +calling program uses different input buffer positions, these defines will have
  4531. +to be changed.*/
  4532. +
  4533. +/*DIFFIE HELLMAN buffer index values*/
  4534. +#define ICP_DH_KRP_PARAM_PRIME_INDEX (0)
  4535. +#define ICP_DH_KRP_PARAM_BASE_INDEX (1)
  4536. +#define ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX (2)
  4537. +#define ICP_DH_KRP_PARAM_RESULT_INDEX (3)
  4538. +
  4539. +/*MOD EXP buffer index values*/
  4540. +#define ICP_MOD_EXP_KRP_PARAM_BASE_INDEX (0)
  4541. +#define ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX (1)
  4542. +#define ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX (2)
  4543. +#define ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX (3)
  4544. +
  4545. +#define SINGLE_BYTE_VALUE (4)
  4546. +
  4547. +/*MOD EXP CRT buffer index values*/
  4548. +#define ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX (0)
  4549. +#define ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX (1)
  4550. +#define ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX (2)
  4551. +#define ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX (3)
  4552. +#define ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX (4)
  4553. +#define ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX (5)
  4554. +#define ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX (6)
  4555. +
  4556. +/*DSA sign buffer index values*/
  4557. +#define ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX (0)
  4558. +#define ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX (1)
  4559. +#define ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX (2)
  4560. +#define ICP_DSA_SIGN_KRP_PARAM_G_INDEX (3)
  4561. +#define ICP_DSA_SIGN_KRP_PARAM_X_INDEX (4)
  4562. +#define ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX (5)
  4563. +#define ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX (6)
  4564. +
  4565. +/*DSA verify buffer index values*/
  4566. +#define ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX (0)
  4567. +#define ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX (1)
  4568. +#define ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX (2)
  4569. +#define ICP_DSA_VERIFY_KRP_PARAM_G_INDEX (3)
  4570. +#define ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX (4)
  4571. +#define ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX (5)
  4572. +#define ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX (6)
  4573. +
  4574. +/*DSA sign prime Q vs random number K size check values*/
  4575. +#define DONT_RUN_LESS_THAN_CHECK (0)
  4576. +#define FAIL_A_IS_GREATER_THAN_B (1)
  4577. +#define FAIL_A_IS_EQUAL_TO_B (1)
  4578. +#define SUCCESS_A_IS_LESS_THAN_B (0)
  4579. +#define DSA_SIGN_RAND_GEN_VAL_CHECK_MAX_ITERATIONS (500)
  4580. +
  4581. +/* We need to set a cryptokp success value just in case it is set or allocated
  4582. + and not set to zero outside of this module */
  4583. +#define CRYPTO_OP_SUCCESS (0)
  4584. +
  4585. +static int icp_ocfDrvDHComputeKey(struct cryptkop *krp);
  4586. +
  4587. +static int icp_ocfDrvModExp(struct cryptkop *krp);
  4588. +
  4589. +static int icp_ocfDrvModExpCRT(struct cryptkop *krp);
  4590. +
  4591. +static int
  4592. +icp_ocfDrvCheckALessThanB(CpaFlatBuffer * pK, CpaFlatBuffer * pQ, int *doCheck);
  4593. +
  4594. +static int icp_ocfDrvDsaSign(struct cryptkop *krp);
  4595. +
  4596. +static int icp_ocfDrvDsaVerify(struct cryptkop *krp);
  4597. +
  4598. +static void
  4599. +icp_ocfDrvDhP1CallBack(void *callbackTag,
  4600. + CpaStatus status,
  4601. + void *pOpData, CpaFlatBuffer * pLocalOctetStringPV);
  4602. +
  4603. +static void
  4604. +icp_ocfDrvModExpCallBack(void *callbackTag,
  4605. + CpaStatus status,
  4606. + void *pOpData, CpaFlatBuffer * pResult);
  4607. +
  4608. +static void
  4609. +icp_ocfDrvModExpCRTCallBack(void *callbackTag,
  4610. + CpaStatus status,
  4611. + void *pOpData, CpaFlatBuffer * pOutputData);
  4612. +
  4613. +static void
  4614. +icp_ocfDrvDsaVerifyCallBack(void *callbackTag,
  4615. + CpaStatus status,
  4616. + void *pOpData, CpaBoolean verifyStatus);
  4617. +
  4618. +static void
  4619. +icp_ocfDrvDsaRSSignCallBack(void *callbackTag,
  4620. + CpaStatus status,
  4621. + void *pOpData,
  4622. + CpaBoolean protocolStatus,
  4623. + CpaFlatBuffer * pR, CpaFlatBuffer * pS);
  4624. +
  4625. +/* Name : icp_ocfDrvPkeProcess
  4626. + *
  4627. + * Description : This function will choose which PKE process to follow
  4628. + * based on the input arguments
  4629. + */
  4630. +int icp_ocfDrvPkeProcess(device_t dev, struct cryptkop *krp, int hint)
  4631. +{
  4632. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  4633. +
  4634. + if (NULL == krp) {
  4635. + DPRINTK("%s(): Invalid input parameters, cryptkop = %p\n",
  4636. + __FUNCTION__, krp);
  4637. + return EINVAL;
  4638. + }
  4639. +
  4640. + if (CPA_TRUE == atomic_read(&icp_ocfDrvIsExiting)) {
  4641. + krp->krp_status = ECANCELED;
  4642. + return ECANCELED;
  4643. + }
  4644. +
  4645. + switch (krp->krp_op) {
  4646. + case CRK_DH_COMPUTE_KEY:
  4647. + DPRINTK("%s() doing DH_COMPUTE_KEY\n", __FUNCTION__);
  4648. + lacStatus = icp_ocfDrvDHComputeKey(krp);
  4649. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4650. + EPRINTK("%s(): icp_ocfDrvDHComputeKey failed "
  4651. + "(%d).\n", __FUNCTION__, lacStatus);
  4652. + krp->krp_status = ECANCELED;
  4653. + return ECANCELED;
  4654. + }
  4655. +
  4656. + break;
  4657. +
  4658. + case CRK_MOD_EXP:
  4659. + DPRINTK("%s() doing MOD_EXP \n", __FUNCTION__);
  4660. + lacStatus = icp_ocfDrvModExp(krp);
  4661. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4662. + EPRINTK("%s(): icp_ocfDrvModExp failed (%d).\n",
  4663. + __FUNCTION__, lacStatus);
  4664. + krp->krp_status = ECANCELED;
  4665. + return ECANCELED;
  4666. + }
  4667. +
  4668. + break;
  4669. +
  4670. + case CRK_MOD_EXP_CRT:
  4671. + DPRINTK("%s() doing MOD_EXP_CRT \n", __FUNCTION__);
  4672. + lacStatus = icp_ocfDrvModExpCRT(krp);
  4673. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4674. + EPRINTK("%s(): icp_ocfDrvModExpCRT "
  4675. + "failed (%d).\n", __FUNCTION__, lacStatus);
  4676. + krp->krp_status = ECANCELED;
  4677. + return ECANCELED;
  4678. + }
  4679. +
  4680. + break;
  4681. +
  4682. + case CRK_DSA_SIGN:
  4683. + DPRINTK("%s() doing DSA_SIGN \n", __FUNCTION__);
  4684. + lacStatus = icp_ocfDrvDsaSign(krp);
  4685. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4686. + EPRINTK("%s(): icp_ocfDrvDsaSign "
  4687. + "failed (%d).\n", __FUNCTION__, lacStatus);
  4688. + krp->krp_status = ECANCELED;
  4689. + return ECANCELED;
  4690. + }
  4691. +
  4692. + break;
  4693. +
  4694. + case CRK_DSA_VERIFY:
  4695. + DPRINTK("%s() doing DSA_VERIFY \n", __FUNCTION__);
  4696. + lacStatus = icp_ocfDrvDsaVerify(krp);
  4697. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4698. + EPRINTK("%s(): icp_ocfDrvDsaVerify "
  4699. + "failed (%d).\n", __FUNCTION__, lacStatus);
  4700. + krp->krp_status = ECANCELED;
  4701. + return ECANCELED;
  4702. + }
  4703. +
  4704. + break;
  4705. +
  4706. + default:
  4707. + EPRINTK("%s(): Asymettric function not "
  4708. + "supported (%d).\n", __FUNCTION__, krp->krp_op);
  4709. + krp->krp_status = EOPNOTSUPP;
  4710. + return EOPNOTSUPP;
  4711. + }
  4712. +
  4713. + return ICP_OCF_DRV_STATUS_SUCCESS;
  4714. +}
  4715. +
  4716. +/* Name : icp_ocfDrvSwapBytes
  4717. + *
  4718. + * Description : This function is used to swap the byte order of a buffer.
  4719. + * It has been seen that in general we are passed little endian byte order
  4720. + * buffers, but LAC only accepts big endian byte order buffers.
  4721. + */
  4722. +static void inline
  4723. +icp_ocfDrvSwapBytes(u_int8_t * num, u_int32_t buff_len_bytes)
  4724. +{
  4725. +
  4726. + int i;
  4727. + u_int8_t *end_ptr;
  4728. + u_int8_t hold_val;
  4729. +
  4730. + end_ptr = num + (buff_len_bytes - 1);
  4731. + buff_len_bytes = buff_len_bytes >> 1;
  4732. + for (i = 0; i < buff_len_bytes; i++) {
  4733. + hold_val = *num;
  4734. + *num = *end_ptr;
  4735. + num++;
  4736. + *end_ptr = hold_val;
  4737. + end_ptr--;
  4738. + }
  4739. +}
  4740. +
  4741. +/* Name : icp_ocfDrvDHComputeKey
  4742. + *
  4743. + * Description : This function will map Diffie Hellman calls from OCF
  4744. + * to the LAC API. OCF uses this function for Diffie Hellman Phase1 and
  4745. + * Phase2. LAC has a separate Diffie Hellman Phase2 call, however both phases
  4746. + * break down to a modular exponentiation.
  4747. + */
  4748. +static int icp_ocfDrvDHComputeKey(struct cryptkop *krp)
  4749. +{
  4750. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  4751. + void *callbackTag = NULL;
  4752. + CpaCyDhPhase1KeyGenOpData *pPhase1OpData = NULL;
  4753. + CpaFlatBuffer *pLocalOctetStringPV = NULL;
  4754. + uint32_t dh_prime_len_bytes = 0, dh_prime_len_bits = 0;
  4755. +
  4756. + /* Input checks - check prime is a multiple of 8 bits to allow for
  4757. + allocation later */
  4758. + dh_prime_len_bits =
  4759. + (krp->krp_param[ICP_DH_KRP_PARAM_PRIME_INDEX].crp_nbits);
  4760. +
  4761. + /* LAC can reject prime lengths based on prime key sizes, we just
  4762. + need to make sure we can allocate space for the base and
  4763. + exponent buffers correctly */
  4764. + if ((dh_prime_len_bits % NUM_BITS_IN_BYTE) != 0) {
  4765. + APRINTK("%s(): Warning Prime number buffer size is not a "
  4766. + "multiple of 8 bits\n", __FUNCTION__);
  4767. + }
  4768. +
  4769. + /* Result storage space should be the same size as the prime as this
  4770. + value can take up the same amount of storage space */
  4771. + if (dh_prime_len_bits !=
  4772. + krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_nbits) {
  4773. + DPRINTK("%s(): Return Buffer must be the same size "
  4774. + "as the Prime buffer\n", __FUNCTION__);
  4775. + krp->krp_status = EINVAL;
  4776. + return EINVAL;
  4777. + }
  4778. + /* Switch to size in bytes */
  4779. + BITS_TO_BYTES(dh_prime_len_bytes, dh_prime_len_bits);
  4780. +
  4781. + callbackTag = krp;
  4782. +
  4783. + pPhase1OpData = kmem_cache_zalloc(drvDH_zone, GFP_KERNEL);
  4784. + if (NULL == pPhase1OpData) {
  4785. + APRINTK("%s():Failed to get memory for key gen data\n",
  4786. + __FUNCTION__);
  4787. + krp->krp_status = ENOMEM;
  4788. + return ENOMEM;
  4789. + }
  4790. +
  4791. + pLocalOctetStringPV = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
  4792. + if (NULL == pLocalOctetStringPV) {
  4793. + APRINTK("%s():Failed to get memory for pLocalOctetStringPV\n",
  4794. + __FUNCTION__);
  4795. + kmem_cache_free(drvDH_zone, pPhase1OpData);
  4796. + krp->krp_status = ENOMEM;
  4797. + return ENOMEM;
  4798. + }
  4799. +
  4800. + /* Link parameters */
  4801. + pPhase1OpData->primeP.pData =
  4802. + krp->krp_param[ICP_DH_KRP_PARAM_PRIME_INDEX].crp_p;
  4803. +
  4804. + pPhase1OpData->primeP.dataLenInBytes = dh_prime_len_bytes;
  4805. +
  4806. + icp_ocfDrvSwapBytes(pPhase1OpData->primeP.pData, dh_prime_len_bytes);
  4807. +
  4808. + pPhase1OpData->baseG.pData =
  4809. + krp->krp_param[ICP_DH_KRP_PARAM_BASE_INDEX].crp_p;
  4810. +
  4811. + BITS_TO_BYTES(pPhase1OpData->baseG.dataLenInBytes,
  4812. + krp->krp_param[ICP_DH_KRP_PARAM_BASE_INDEX].crp_nbits);
  4813. +
  4814. + icp_ocfDrvSwapBytes(pPhase1OpData->baseG.pData,
  4815. + pPhase1OpData->baseG.dataLenInBytes);
  4816. +
  4817. + pPhase1OpData->privateValueX.pData =
  4818. + krp->krp_param[ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX].crp_p;
  4819. +
  4820. + BITS_TO_BYTES(pPhase1OpData->privateValueX.dataLenInBytes,
  4821. + krp->krp_param[ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX].
  4822. + crp_nbits);
  4823. +
  4824. + icp_ocfDrvSwapBytes(pPhase1OpData->privateValueX.pData,
  4825. + pPhase1OpData->privateValueX.dataLenInBytes);
  4826. +
  4827. + /* Output parameters */
  4828. + pLocalOctetStringPV->pData =
  4829. + krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_p;
  4830. +
  4831. + BITS_TO_BYTES(pLocalOctetStringPV->dataLenInBytes,
  4832. + krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_nbits);
  4833. +
  4834. + lacStatus = cpaCyDhKeyGenPhase1(CPA_INSTANCE_HANDLE_SINGLE,
  4835. + icp_ocfDrvDhP1CallBack,
  4836. + callbackTag, pPhase1OpData,
  4837. + pLocalOctetStringPV);
  4838. +
  4839. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4840. + EPRINTK("%s(): DH Phase 1 Key Gen failed (%d).\n",
  4841. + __FUNCTION__, lacStatus);
  4842. + icp_ocfDrvFreeFlatBuffer(pLocalOctetStringPV);
  4843. + kmem_cache_free(drvDH_zone, pPhase1OpData);
  4844. + }
  4845. +
  4846. + return lacStatus;
  4847. +}
  4848. +
  4849. +/* Name : icp_ocfDrvModExp
  4850. + *
  4851. + * Description : This function will map ordinary Modular Exponentiation calls
  4852. + * from OCF to the LAC API.
  4853. + *
  4854. + */
  4855. +static int icp_ocfDrvModExp(struct cryptkop *krp)
  4856. +{
  4857. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  4858. + void *callbackTag = NULL;
  4859. + CpaCyLnModExpOpData *pModExpOpData = NULL;
  4860. + CpaFlatBuffer *pResult = NULL;
  4861. +
  4862. + if ((krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_nbits %
  4863. + NUM_BITS_IN_BYTE) != 0) {
  4864. + DPRINTK("%s(): Warning - modulus buffer size (%d) is not a "
  4865. + "multiple of 8 bits\n", __FUNCTION__,
  4866. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].
  4867. + crp_nbits);
  4868. + }
  4869. +
  4870. + /* Result storage space should be the same size as the prime as this
  4871. + value can take up the same amount of storage space */
  4872. + if (krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_nbits >
  4873. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].crp_nbits) {
  4874. + APRINTK("%s(): Return Buffer size must be the same or"
  4875. + " greater than the Modulus buffer\n", __FUNCTION__);
  4876. + krp->krp_status = EINVAL;
  4877. + return EINVAL;
  4878. + }
  4879. +
  4880. + callbackTag = krp;
  4881. +
  4882. + pModExpOpData = kmem_cache_zalloc(drvLnModExp_zone, GFP_KERNEL);
  4883. + if (NULL == pModExpOpData) {
  4884. + APRINTK("%s():Failed to get memory for key gen data\n",
  4885. + __FUNCTION__);
  4886. + krp->krp_status = ENOMEM;
  4887. + return ENOMEM;
  4888. + }
  4889. +
  4890. + pResult = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
  4891. + if (NULL == pResult) {
  4892. + APRINTK("%s():Failed to get memory for ModExp result\n",
  4893. + __FUNCTION__);
  4894. + kmem_cache_free(drvLnModExp_zone, pModExpOpData);
  4895. + krp->krp_status = ENOMEM;
  4896. + return ENOMEM;
  4897. + }
  4898. +
  4899. + /* Link parameters */
  4900. + pModExpOpData->modulus.pData =
  4901. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_p;
  4902. + BITS_TO_BYTES(pModExpOpData->modulus.dataLenInBytes,
  4903. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].
  4904. + crp_nbits);
  4905. +
  4906. + icp_ocfDrvSwapBytes(pModExpOpData->modulus.pData,
  4907. + pModExpOpData->modulus.dataLenInBytes);
  4908. +
  4909. + /*OCF patch to Openswan Pluto regularly sends the base value as 2
  4910. + bits in size. In this case, it has been found it is better to
  4911. + use the base size memory space as the input buffer (if the number
  4912. + is in bits is less than a byte, the number of bits is the input
  4913. + value) */
  4914. + if (krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits <
  4915. + NUM_BITS_IN_BYTE) {
  4916. + DPRINTK("%s : base is small (%d)\n", __FUNCTION__, krp->
  4917. + krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits);
  4918. + pModExpOpData->base.dataLenInBytes = SINGLE_BYTE_VALUE;
  4919. + pModExpOpData->base.pData =
  4920. + (uint8_t *) & (krp->
  4921. + krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
  4922. + crp_nbits);
  4923. + *((uint32_t *) pModExpOpData->base.pData) =
  4924. + htonl(*((uint32_t *) pModExpOpData->base.pData));
  4925. +
  4926. + } else {
  4927. +
  4928. + DPRINTK("%s : base is big (%d)\n", __FUNCTION__, krp->
  4929. + krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits);
  4930. + pModExpOpData->base.pData =
  4931. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_p;
  4932. + BITS_TO_BYTES(pModExpOpData->base.dataLenInBytes,
  4933. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
  4934. + crp_nbits);
  4935. + icp_ocfDrvSwapBytes(pModExpOpData->base.pData,
  4936. + pModExpOpData->base.dataLenInBytes);
  4937. + }
  4938. +
  4939. + pModExpOpData->exponent.pData =
  4940. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX].crp_p;
  4941. + BITS_TO_BYTES(pModExpOpData->exponent.dataLenInBytes,
  4942. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX].
  4943. + crp_nbits);
  4944. +
  4945. + icp_ocfDrvSwapBytes(pModExpOpData->exponent.pData,
  4946. + pModExpOpData->exponent.dataLenInBytes);
  4947. + /* Output parameters */
  4948. + pResult->pData =
  4949. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].crp_p,
  4950. + BITS_TO_BYTES(pResult->dataLenInBytes,
  4951. + krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].
  4952. + crp_nbits);
  4953. +
  4954. + lacStatus = cpaCyLnModExp(CPA_INSTANCE_HANDLE_SINGLE,
  4955. + icp_ocfDrvModExpCallBack,
  4956. + callbackTag, pModExpOpData, pResult);
  4957. +
  4958. + if (CPA_STATUS_SUCCESS != lacStatus) {
  4959. + EPRINTK("%s(): Mod Exp Operation failed (%d).\n",
  4960. + __FUNCTION__, lacStatus);
  4961. + krp->krp_status = ECANCELED;
  4962. + icp_ocfDrvFreeFlatBuffer(pResult);
  4963. + kmem_cache_free(drvLnModExp_zone, pModExpOpData);
  4964. + }
  4965. +
  4966. + return lacStatus;
  4967. +}
  4968. +
  4969. +/* Name : icp_ocfDrvModExpCRT
  4970. + *
  4971. + * Description : This function will map ordinary Modular Exponentiation Chinese
  4972. + * Remainder Theorem implementaion calls from OCF to the LAC API.
  4973. + *
  4974. + * Note : Mod Exp CRT for this driver is accelerated through LAC RSA type 2
  4975. + * decrypt operation. Therefore P and Q input values must always be prime
  4976. + * numbers. Although basic primality checks are done in LAC, it is up to the
  4977. + * user to do any correct prime number checking before passing the inputs.
  4978. + */
  4979. +
  4980. +static int icp_ocfDrvModExpCRT(struct cryptkop *krp)
  4981. +{
  4982. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  4983. + CpaCyRsaDecryptOpData *rsaDecryptOpData = NULL;
  4984. + void *callbackTag = NULL;
  4985. + CpaFlatBuffer *pOutputData = NULL;
  4986. +
  4987. + /*Parameter input checks are all done by LAC, no need to repeat
  4988. + them here. */
  4989. + callbackTag = krp;
  4990. +
  4991. + rsaDecryptOpData = kmem_cache_zalloc(drvRSADecrypt_zone, GFP_KERNEL);
  4992. + if (NULL == rsaDecryptOpData) {
  4993. + APRINTK("%s():Failed to get memory"
  4994. + " for MOD EXP CRT Op data struct\n", __FUNCTION__);
  4995. + krp->krp_status = ENOMEM;
  4996. + return ENOMEM;
  4997. + }
  4998. +
  4999. + rsaDecryptOpData->pRecipientPrivateKey
  5000. + = kmem_cache_zalloc(drvRSAPrivateKey_zone, GFP_KERNEL);
  5001. + if (NULL == rsaDecryptOpData->pRecipientPrivateKey) {
  5002. + APRINTK("%s():Failed to get memory for MOD EXP CRT"
  5003. + " private key values struct\n", __FUNCTION__);
  5004. + kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
  5005. + krp->krp_status = ENOMEM;
  5006. + return ENOMEM;
  5007. + }
  5008. +
  5009. + rsaDecryptOpData->pRecipientPrivateKey->
  5010. + version = CPA_CY_RSA_VERSION_TWO_PRIME;
  5011. + rsaDecryptOpData->pRecipientPrivateKey->
  5012. + privateKeyRepType = CPA_CY_RSA_PRIVATE_KEY_REP_TYPE_2;
  5013. +
  5014. + pOutputData = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
  5015. + if (NULL == pOutputData) {
  5016. + APRINTK("%s():Failed to get memory"
  5017. + " for MOD EXP CRT output data\n", __FUNCTION__);
  5018. + kmem_cache_free(drvRSAPrivateKey_zone,
  5019. + rsaDecryptOpData->pRecipientPrivateKey);
  5020. + kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
  5021. + krp->krp_status = ENOMEM;
  5022. + return ENOMEM;
  5023. + }
  5024. +
  5025. + rsaDecryptOpData->pRecipientPrivateKey->
  5026. + version = CPA_CY_RSA_VERSION_TWO_PRIME;
  5027. + rsaDecryptOpData->pRecipientPrivateKey->
  5028. + privateKeyRepType = CPA_CY_RSA_PRIVATE_KEY_REP_TYPE_2;
  5029. +
  5030. + /* Link parameters */
  5031. + rsaDecryptOpData->inputData.pData =
  5032. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX].crp_p;
  5033. + BITS_TO_BYTES(rsaDecryptOpData->inputData.dataLenInBytes,
  5034. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX].
  5035. + crp_nbits);
  5036. +
  5037. + icp_ocfDrvSwapBytes(rsaDecryptOpData->inputData.pData,
  5038. + rsaDecryptOpData->inputData.dataLenInBytes);
  5039. +
  5040. + rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.prime1P.pData =
  5041. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX].crp_p;
  5042. + BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
  5043. + prime1P.dataLenInBytes,
  5044. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX].
  5045. + crp_nbits);
  5046. +
  5047. + icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
  5048. + privateKeyRep2.prime1P.pData,
  5049. + rsaDecryptOpData->pRecipientPrivateKey->
  5050. + privateKeyRep2.prime1P.dataLenInBytes);
  5051. +
  5052. + rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.prime2Q.pData =
  5053. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX].crp_p;
  5054. + BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
  5055. + prime2Q.dataLenInBytes,
  5056. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX].
  5057. + crp_nbits);
  5058. +
  5059. + icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
  5060. + privateKeyRep2.prime2Q.pData,
  5061. + rsaDecryptOpData->pRecipientPrivateKey->
  5062. + privateKeyRep2.prime2Q.dataLenInBytes);
  5063. +
  5064. + rsaDecryptOpData->pRecipientPrivateKey->
  5065. + privateKeyRep2.exponent1Dp.pData =
  5066. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX].crp_p;
  5067. + BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
  5068. + exponent1Dp.dataLenInBytes,
  5069. + krp->
  5070. + krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX].
  5071. + crp_nbits);
  5072. +
  5073. + icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
  5074. + privateKeyRep2.exponent1Dp.pData,
  5075. + rsaDecryptOpData->pRecipientPrivateKey->
  5076. + privateKeyRep2.exponent1Dp.dataLenInBytes);
  5077. +
  5078. + rsaDecryptOpData->pRecipientPrivateKey->
  5079. + privateKeyRep2.exponent2Dq.pData =
  5080. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX].crp_p;
  5081. + BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->
  5082. + privateKeyRep2.exponent2Dq.dataLenInBytes,
  5083. + krp->
  5084. + krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX].
  5085. + crp_nbits);
  5086. +
  5087. + icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
  5088. + privateKeyRep2.exponent2Dq.pData,
  5089. + rsaDecryptOpData->pRecipientPrivateKey->
  5090. + privateKeyRep2.exponent2Dq.dataLenInBytes);
  5091. +
  5092. + rsaDecryptOpData->pRecipientPrivateKey->
  5093. + privateKeyRep2.coefficientQInv.pData =
  5094. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX].crp_p;
  5095. + BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->
  5096. + privateKeyRep2.coefficientQInv.dataLenInBytes,
  5097. + krp->
  5098. + krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX].
  5099. + crp_nbits);
  5100. +
  5101. + icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
  5102. + privateKeyRep2.coefficientQInv.pData,
  5103. + rsaDecryptOpData->pRecipientPrivateKey->
  5104. + privateKeyRep2.coefficientQInv.dataLenInBytes);
  5105. +
  5106. + /* Output Parameter */
  5107. + pOutputData->pData =
  5108. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX].crp_p;
  5109. + BITS_TO_BYTES(pOutputData->dataLenInBytes,
  5110. + krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX].
  5111. + crp_nbits);
  5112. +
  5113. + lacStatus = cpaCyRsaDecrypt(CPA_INSTANCE_HANDLE_SINGLE,
  5114. + icp_ocfDrvModExpCRTCallBack,
  5115. + callbackTag, rsaDecryptOpData, pOutputData);
  5116. +
  5117. + if (CPA_STATUS_SUCCESS != lacStatus) {
  5118. + EPRINTK("%s(): Mod Exp CRT Operation failed (%d).\n",
  5119. + __FUNCTION__, lacStatus);
  5120. + krp->krp_status = ECANCELED;
  5121. + icp_ocfDrvFreeFlatBuffer(pOutputData);
  5122. + kmem_cache_free(drvRSAPrivateKey_zone,
  5123. + rsaDecryptOpData->pRecipientPrivateKey);
  5124. + kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
  5125. + }
  5126. +
  5127. + return lacStatus;
  5128. +}
  5129. +
  5130. +/* Name : icp_ocfDrvCheckALessThanB
  5131. + *
  5132. + * Description : This function will check whether the first argument is less
  5133. + * than the second. It is used to check whether the DSA RS sign Random K
  5134. + * value is less than the Prime Q value (as defined in the specification)
  5135. + *
  5136. + */
  5137. +static int
  5138. +icp_ocfDrvCheckALessThanB(CpaFlatBuffer * pK, CpaFlatBuffer * pQ, int *doCheck)
  5139. +{
  5140. +
  5141. + uint8_t *MSB_K = pK->pData;
  5142. + uint8_t *MSB_Q = pQ->pData;
  5143. + uint32_t buffer_lengths_in_bytes = pQ->dataLenInBytes;
  5144. +
  5145. + if (DONT_RUN_LESS_THAN_CHECK == *doCheck) {
  5146. + return FAIL_A_IS_GREATER_THAN_B;
  5147. + }
  5148. +
  5149. +/*Check MSBs
  5150. +if A == B, check next MSB
  5151. +if A > B, return A_IS_GREATER_THAN_B
  5152. +if A < B, return A_IS_LESS_THAN_B (success)
  5153. +*/
  5154. + while (*MSB_K == *MSB_Q) {
  5155. + MSB_K++;
  5156. + MSB_Q++;
  5157. +
  5158. + buffer_lengths_in_bytes--;
  5159. + if (0 == buffer_lengths_in_bytes) {
  5160. + DPRINTK("%s() Buffers have equal value!!\n",
  5161. + __FUNCTION__);
  5162. + return FAIL_A_IS_EQUAL_TO_B;
  5163. + }
  5164. +
  5165. + }
  5166. +
  5167. + if (*MSB_K < *MSB_Q) {
  5168. + return SUCCESS_A_IS_LESS_THAN_B;
  5169. + } else {
  5170. + return FAIL_A_IS_GREATER_THAN_B;
  5171. + }
  5172. +
  5173. +}
  5174. +
  5175. +/* Name : icp_ocfDrvDsaSign
  5176. + *
  5177. + * Description : This function will map DSA RS Sign from OCF to the LAC API.
  5178. + *
  5179. + * NOTE: From looking at OCF patch to OpenSSL and even the number of input
  5180. + * parameters, OCF expects us to generate the random seed value. This value
  5181. + * is generated and passed to LAC, however the number is discared in the
  5182. + * callback and not returned to the user.
  5183. + */
  5184. +static int icp_ocfDrvDsaSign(struct cryptkop *krp)
  5185. +{
  5186. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  5187. + CpaCyDsaRSSignOpData *dsaRsSignOpData = NULL;
  5188. + void *callbackTag = NULL;
  5189. + CpaCyRandGenOpData randGenOpData;
  5190. + int primeQSizeInBytes = 0;
  5191. + int doCheck = 0;
  5192. + CpaFlatBuffer randData;
  5193. + CpaBoolean protocolStatus = CPA_FALSE;
  5194. + CpaFlatBuffer *pR = NULL;
  5195. + CpaFlatBuffer *pS = NULL;
  5196. +
  5197. + callbackTag = krp;
  5198. +
  5199. + BITS_TO_BYTES(primeQSizeInBytes,
  5200. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].
  5201. + crp_nbits);
  5202. +
  5203. + if (DSA_RS_SIGN_PRIMEQ_SIZE_IN_BYTES != primeQSizeInBytes) {
  5204. + APRINTK("%s(): DSA PRIME Q size not equal to the "
  5205. + "FIPS defined 20bytes, = %d\n",
  5206. + __FUNCTION__, primeQSizeInBytes);
  5207. + krp->krp_status = EDOM;
  5208. + return EDOM;
  5209. + }
  5210. +
  5211. + dsaRsSignOpData = kmem_cache_zalloc(drvDSARSSign_zone, GFP_KERNEL);
  5212. + if (NULL == dsaRsSignOpData) {
  5213. + APRINTK("%s():Failed to get memory"
  5214. + " for DSA RS Sign Op data struct\n", __FUNCTION__);
  5215. + krp->krp_status = ENOMEM;
  5216. + return ENOMEM;
  5217. + }
  5218. +
  5219. + dsaRsSignOpData->K.pData =
  5220. + kmem_cache_alloc(drvDSARSSignKValue_zone, GFP_ATOMIC);
  5221. +
  5222. + if (NULL == dsaRsSignOpData->K.pData) {
  5223. + APRINTK("%s():Failed to get memory"
  5224. + " for DSA RS Sign Op Random value\n", __FUNCTION__);
  5225. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5226. + krp->krp_status = ENOMEM;
  5227. + return ENOMEM;
  5228. + }
  5229. +
  5230. + pR = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
  5231. + if (NULL == pR) {
  5232. + APRINTK("%s():Failed to get memory"
  5233. + " for DSA signature R\n", __FUNCTION__);
  5234. + kmem_cache_free(drvDSARSSignKValue_zone,
  5235. + dsaRsSignOpData->K.pData);
  5236. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5237. + krp->krp_status = ENOMEM;
  5238. + return ENOMEM;
  5239. + }
  5240. +
  5241. + pS = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
  5242. + if (NULL == pS) {
  5243. + APRINTK("%s():Failed to get memory"
  5244. + " for DSA signature S\n", __FUNCTION__);
  5245. + icp_ocfDrvFreeFlatBuffer(pR);
  5246. + kmem_cache_free(drvDSARSSignKValue_zone,
  5247. + dsaRsSignOpData->K.pData);
  5248. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5249. + krp->krp_status = ENOMEM;
  5250. + return ENOMEM;
  5251. + }
  5252. +
  5253. + /*link prime number parameter for ease of processing */
  5254. + dsaRsSignOpData->P.pData =
  5255. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX].crp_p;
  5256. + BITS_TO_BYTES(dsaRsSignOpData->P.dataLenInBytes,
  5257. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX].
  5258. + crp_nbits);
  5259. +
  5260. + icp_ocfDrvSwapBytes(dsaRsSignOpData->P.pData,
  5261. + dsaRsSignOpData->P.dataLenInBytes);
  5262. +
  5263. + dsaRsSignOpData->Q.pData =
  5264. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].crp_p;
  5265. + BITS_TO_BYTES(dsaRsSignOpData->Q.dataLenInBytes,
  5266. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].
  5267. + crp_nbits);
  5268. +
  5269. + icp_ocfDrvSwapBytes(dsaRsSignOpData->Q.pData,
  5270. + dsaRsSignOpData->Q.dataLenInBytes);
  5271. +
  5272. + /*generate random number with equal buffer size to Prime value Q,
  5273. + but value less than Q */
  5274. + dsaRsSignOpData->K.dataLenInBytes = dsaRsSignOpData->Q.dataLenInBytes;
  5275. +
  5276. + randGenOpData.generateBits = CPA_TRUE;
  5277. + randGenOpData.lenInBytes = dsaRsSignOpData->K.dataLenInBytes;
  5278. +
  5279. + icp_ocfDrvPtrAndLenToFlatBuffer(dsaRsSignOpData->K.pData,
  5280. + dsaRsSignOpData->K.dataLenInBytes,
  5281. + &randData);
  5282. +
  5283. + doCheck = 0;
  5284. + while (icp_ocfDrvCheckALessThanB(&(dsaRsSignOpData->K),
  5285. + &(dsaRsSignOpData->Q), &doCheck)) {
  5286. +
  5287. + if (CPA_STATUS_SUCCESS
  5288. + != cpaCyRandGen(CPA_INSTANCE_HANDLE_SINGLE,
  5289. + NULL, NULL, &randGenOpData, &randData)) {
  5290. + APRINTK("%s(): ERROR - Failed to generate DSA RS Sign K"
  5291. + "value\n", __FUNCTION__);
  5292. + icp_ocfDrvFreeFlatBuffer(pS);
  5293. + icp_ocfDrvFreeFlatBuffer(pR);
  5294. + kmem_cache_free(drvDSARSSignKValue_zone,
  5295. + dsaRsSignOpData->K.pData);
  5296. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5297. + krp->krp_status = EAGAIN;
  5298. + return EAGAIN;
  5299. + }
  5300. +
  5301. + doCheck++;
  5302. + if (DSA_SIGN_RAND_GEN_VAL_CHECK_MAX_ITERATIONS == doCheck) {
  5303. + APRINTK("%s(): ERROR - Failed to find DSA RS Sign K "
  5304. + "value less than Q value\n", __FUNCTION__);
  5305. + icp_ocfDrvFreeFlatBuffer(pS);
  5306. + icp_ocfDrvFreeFlatBuffer(pR);
  5307. + kmem_cache_free(drvDSARSSignKValue_zone,
  5308. + dsaRsSignOpData->K.pData);
  5309. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5310. + krp->krp_status = EAGAIN;
  5311. + return EAGAIN;
  5312. + }
  5313. +
  5314. + }
  5315. + /*Rand Data - no need to swap bytes for pK */
  5316. +
  5317. + /* Link parameters */
  5318. + dsaRsSignOpData->G.pData =
  5319. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_G_INDEX].crp_p;
  5320. + BITS_TO_BYTES(dsaRsSignOpData->G.dataLenInBytes,
  5321. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_G_INDEX].crp_nbits);
  5322. +
  5323. + icp_ocfDrvSwapBytes(dsaRsSignOpData->G.pData,
  5324. + dsaRsSignOpData->G.dataLenInBytes);
  5325. +
  5326. + dsaRsSignOpData->X.pData =
  5327. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_X_INDEX].crp_p;
  5328. + BITS_TO_BYTES(dsaRsSignOpData->X.dataLenInBytes,
  5329. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_X_INDEX].crp_nbits);
  5330. + icp_ocfDrvSwapBytes(dsaRsSignOpData->X.pData,
  5331. + dsaRsSignOpData->X.dataLenInBytes);
  5332. +
  5333. + dsaRsSignOpData->M.pData =
  5334. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX].crp_p;
  5335. + BITS_TO_BYTES(dsaRsSignOpData->M.dataLenInBytes,
  5336. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX].
  5337. + crp_nbits);
  5338. + icp_ocfDrvSwapBytes(dsaRsSignOpData->M.pData,
  5339. + dsaRsSignOpData->M.dataLenInBytes);
  5340. +
  5341. + /* Output Parameters */
  5342. + pS->pData = krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX].crp_p;
  5343. + BITS_TO_BYTES(pS->dataLenInBytes,
  5344. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX].
  5345. + crp_nbits);
  5346. +
  5347. + pR->pData = krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX].crp_p;
  5348. + BITS_TO_BYTES(pR->dataLenInBytes,
  5349. + krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX].
  5350. + crp_nbits);
  5351. +
  5352. + lacStatus = cpaCyDsaSignRS(CPA_INSTANCE_HANDLE_SINGLE,
  5353. + icp_ocfDrvDsaRSSignCallBack,
  5354. + callbackTag, dsaRsSignOpData,
  5355. + &protocolStatus, pR, pS);
  5356. +
  5357. + if (CPA_STATUS_SUCCESS != lacStatus) {
  5358. + EPRINTK("%s(): DSA RS Sign Operation failed (%d).\n",
  5359. + __FUNCTION__, lacStatus);
  5360. + krp->krp_status = ECANCELED;
  5361. + icp_ocfDrvFreeFlatBuffer(pS);
  5362. + icp_ocfDrvFreeFlatBuffer(pR);
  5363. + kmem_cache_free(drvDSARSSignKValue_zone,
  5364. + dsaRsSignOpData->K.pData);
  5365. + kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
  5366. + }
  5367. +
  5368. + return lacStatus;
  5369. +}
  5370. +
  5371. +/* Name : icp_ocfDrvDsaVerify
  5372. + *
  5373. + * Description : This function will map DSA RS Verify from OCF to the LAC API.
  5374. + *
  5375. + */
  5376. +static int icp_ocfDrvDsaVerify(struct cryptkop *krp)
  5377. +{
  5378. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  5379. + CpaCyDsaVerifyOpData *dsaVerifyOpData = NULL;
  5380. + void *callbackTag = NULL;
  5381. + CpaBoolean verifyStatus = CPA_FALSE;
  5382. +
  5383. + callbackTag = krp;
  5384. +
  5385. + dsaVerifyOpData = kmem_cache_zalloc(drvDSAVerify_zone, GFP_KERNEL);
  5386. + if (NULL == dsaVerifyOpData) {
  5387. + APRINTK("%s():Failed to get memory"
  5388. + " for DSA Verify Op data struct\n", __FUNCTION__);
  5389. + krp->krp_status = ENOMEM;
  5390. + return ENOMEM;
  5391. + }
  5392. +
  5393. + /* Link parameters */
  5394. + dsaVerifyOpData->P.pData =
  5395. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX].crp_p;
  5396. + BITS_TO_BYTES(dsaVerifyOpData->P.dataLenInBytes,
  5397. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX].
  5398. + crp_nbits);
  5399. + icp_ocfDrvSwapBytes(dsaVerifyOpData->P.pData,
  5400. + dsaVerifyOpData->P.dataLenInBytes);
  5401. +
  5402. + dsaVerifyOpData->Q.pData =
  5403. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX].crp_p;
  5404. + BITS_TO_BYTES(dsaVerifyOpData->Q.dataLenInBytes,
  5405. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX].
  5406. + crp_nbits);
  5407. + icp_ocfDrvSwapBytes(dsaVerifyOpData->Q.pData,
  5408. + dsaVerifyOpData->Q.dataLenInBytes);
  5409. +
  5410. + dsaVerifyOpData->G.pData =
  5411. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_G_INDEX].crp_p;
  5412. + BITS_TO_BYTES(dsaVerifyOpData->G.dataLenInBytes,
  5413. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_G_INDEX].
  5414. + crp_nbits);
  5415. + icp_ocfDrvSwapBytes(dsaVerifyOpData->G.pData,
  5416. + dsaVerifyOpData->G.dataLenInBytes);
  5417. +
  5418. + dsaVerifyOpData->Y.pData =
  5419. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX].crp_p;
  5420. + BITS_TO_BYTES(dsaVerifyOpData->Y.dataLenInBytes,
  5421. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX].
  5422. + crp_nbits);
  5423. + icp_ocfDrvSwapBytes(dsaVerifyOpData->Y.pData,
  5424. + dsaVerifyOpData->Y.dataLenInBytes);
  5425. +
  5426. + dsaVerifyOpData->M.pData =
  5427. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX].crp_p;
  5428. + BITS_TO_BYTES(dsaVerifyOpData->M.dataLenInBytes,
  5429. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX].
  5430. + crp_nbits);
  5431. + icp_ocfDrvSwapBytes(dsaVerifyOpData->M.pData,
  5432. + dsaVerifyOpData->M.dataLenInBytes);
  5433. +
  5434. + dsaVerifyOpData->R.pData =
  5435. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX].crp_p;
  5436. + BITS_TO_BYTES(dsaVerifyOpData->R.dataLenInBytes,
  5437. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX].
  5438. + crp_nbits);
  5439. + icp_ocfDrvSwapBytes(dsaVerifyOpData->R.pData,
  5440. + dsaVerifyOpData->R.dataLenInBytes);
  5441. +
  5442. + dsaVerifyOpData->S.pData =
  5443. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX].crp_p;
  5444. + BITS_TO_BYTES(dsaVerifyOpData->S.dataLenInBytes,
  5445. + krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX].
  5446. + crp_nbits);
  5447. + icp_ocfDrvSwapBytes(dsaVerifyOpData->S.pData,
  5448. + dsaVerifyOpData->S.dataLenInBytes);
  5449. +
  5450. + lacStatus = cpaCyDsaVerify(CPA_INSTANCE_HANDLE_SINGLE,
  5451. + icp_ocfDrvDsaVerifyCallBack,
  5452. + callbackTag, dsaVerifyOpData, &verifyStatus);
  5453. +
  5454. + if (CPA_STATUS_SUCCESS != lacStatus) {
  5455. + EPRINTK("%s(): DSA Verify Operation failed (%d).\n",
  5456. + __FUNCTION__, lacStatus);
  5457. + kmem_cache_free(drvDSAVerify_zone, dsaVerifyOpData);
  5458. + krp->krp_status = ECANCELED;
  5459. + }
  5460. +
  5461. + return lacStatus;
  5462. +}
  5463. +
  5464. +/* Name : icp_ocfDrvReadRandom
  5465. + *
  5466. + * Description : This function will map RNG functionality calls from OCF
  5467. + * to the LAC API.
  5468. + */
  5469. +int icp_ocfDrvReadRandom(void *arg, uint32_t * buf, int maxwords)
  5470. +{
  5471. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  5472. + CpaCyRandGenOpData randGenOpData;
  5473. + CpaFlatBuffer randData;
  5474. +
  5475. + if (NULL == buf) {
  5476. + APRINTK("%s(): Invalid input parameters\n", __FUNCTION__);
  5477. + return EINVAL;
  5478. + }
  5479. +
  5480. + /* maxwords here is number of integers to generate data for */
  5481. + randGenOpData.generateBits = CPA_TRUE;
  5482. +
  5483. + randGenOpData.lenInBytes = maxwords * sizeof(uint32_t);
  5484. +
  5485. + icp_ocfDrvPtrAndLenToFlatBuffer((Cpa8U *) buf,
  5486. + randGenOpData.lenInBytes, &randData);
  5487. +
  5488. + lacStatus = cpaCyRandGen(CPA_INSTANCE_HANDLE_SINGLE,
  5489. + NULL, NULL, &randGenOpData, &randData);
  5490. + if (CPA_STATUS_SUCCESS != lacStatus) {
  5491. + EPRINTK("%s(): icp_LacSymRandGen failed (%d). \n",
  5492. + __FUNCTION__, lacStatus);
  5493. + return RETURN_RAND_NUM_GEN_FAILED;
  5494. + }
  5495. +
  5496. + return randGenOpData.lenInBytes / sizeof(uint32_t);
  5497. +}
  5498. +
  5499. +/* Name : icp_ocfDrvDhP1Callback
  5500. + *
  5501. + * Description : When this function returns it signifies that the LAC
  5502. + * component has completed the DH operation.
  5503. + */
  5504. +static void
  5505. +icp_ocfDrvDhP1CallBack(void *callbackTag,
  5506. + CpaStatus status,
  5507. + void *pOpData, CpaFlatBuffer * pLocalOctetStringPV)
  5508. +{
  5509. + struct cryptkop *krp = NULL;
  5510. + CpaCyDhPhase1KeyGenOpData *pPhase1OpData = NULL;
  5511. +
  5512. + if (NULL == callbackTag) {
  5513. + DPRINTK("%s(): Invalid input parameters - "
  5514. + "callbackTag data is NULL\n", __FUNCTION__);
  5515. + return;
  5516. + }
  5517. + krp = (struct cryptkop *)callbackTag;
  5518. +
  5519. + if (NULL == pOpData) {
  5520. + DPRINTK("%s(): Invalid input parameters - "
  5521. + "Operation Data is NULL\n", __FUNCTION__);
  5522. + krp->krp_status = ECANCELED;
  5523. + crypto_kdone(krp);
  5524. + return;
  5525. + }
  5526. + pPhase1OpData = (CpaCyDhPhase1KeyGenOpData *) pOpData;
  5527. +
  5528. + if (NULL == pLocalOctetStringPV) {
  5529. + DPRINTK("%s(): Invalid input parameters - "
  5530. + "pLocalOctetStringPV Data is NULL\n", __FUNCTION__);
  5531. + memset(pPhase1OpData, 0, sizeof(CpaCyDhPhase1KeyGenOpData));
  5532. + kmem_cache_free(drvDH_zone, pPhase1OpData);
  5533. + krp->krp_status = ECANCELED;
  5534. + crypto_kdone(krp);
  5535. + return;
  5536. + }
  5537. +
  5538. + if (CPA_STATUS_SUCCESS == status) {
  5539. + krp->krp_status = CRYPTO_OP_SUCCESS;
  5540. + } else {
  5541. + APRINTK("%s(): Diffie Hellman Phase1 Key Gen failed - "
  5542. + "Operation Status = %d\n", __FUNCTION__, status);
  5543. + krp->krp_status = ECANCELED;
  5544. + }
  5545. +
  5546. + icp_ocfDrvSwapBytes(pLocalOctetStringPV->pData,
  5547. + pLocalOctetStringPV->dataLenInBytes);
  5548. +
  5549. + icp_ocfDrvFreeFlatBuffer(pLocalOctetStringPV);
  5550. + memset(pPhase1OpData, 0, sizeof(CpaCyDhPhase1KeyGenOpData));
  5551. + kmem_cache_free(drvDH_zone, pPhase1OpData);
  5552. +
  5553. + crypto_kdone(krp);
  5554. +
  5555. + return;
  5556. +}
  5557. +
  5558. +/* Name : icp_ocfDrvModExpCallBack
  5559. + *
  5560. + * Description : When this function returns it signifies that the LAC
  5561. + * component has completed the Mod Exp operation.
  5562. + */
  5563. +static void
  5564. +icp_ocfDrvModExpCallBack(void *callbackTag,
  5565. + CpaStatus status,
  5566. + void *pOpdata, CpaFlatBuffer * pResult)
  5567. +{
  5568. + struct cryptkop *krp = NULL;
  5569. + CpaCyLnModExpOpData *pLnModExpOpData = NULL;
  5570. +
  5571. + if (NULL == callbackTag) {
  5572. + DPRINTK("%s(): Invalid input parameters - "
  5573. + "callbackTag data is NULL\n", __FUNCTION__);
  5574. + return;
  5575. + }
  5576. + krp = (struct cryptkop *)callbackTag;
  5577. +
  5578. + if (NULL == pOpdata) {
  5579. + DPRINTK("%s(): Invalid Mod Exp input parameters - "
  5580. + "Operation Data is NULL\n", __FUNCTION__);
  5581. + krp->krp_status = ECANCELED;
  5582. + crypto_kdone(krp);
  5583. + return;
  5584. + }
  5585. + pLnModExpOpData = (CpaCyLnModExpOpData *) pOpdata;
  5586. +
  5587. + if (NULL == pResult) {
  5588. + DPRINTK("%s(): Invalid input parameters - "
  5589. + "pResult data is NULL\n", __FUNCTION__);
  5590. + krp->krp_status = ECANCELED;
  5591. + memset(pLnModExpOpData, 0, sizeof(CpaCyLnModExpOpData));
  5592. + kmem_cache_free(drvLnModExp_zone, pLnModExpOpData);
  5593. + crypto_kdone(krp);
  5594. + return;
  5595. + }
  5596. +
  5597. + if (CPA_STATUS_SUCCESS == status) {
  5598. + krp->krp_status = CRYPTO_OP_SUCCESS;
  5599. + } else {
  5600. + APRINTK("%s(): LAC Mod Exp Operation failed - "
  5601. + "Operation Status = %d\n", __FUNCTION__, status);
  5602. + krp->krp_status = ECANCELED;
  5603. + }
  5604. +
  5605. + icp_ocfDrvSwapBytes(pResult->pData, pResult->dataLenInBytes);
  5606. +
  5607. + /*switch base size value back to original */
  5608. + if (pLnModExpOpData->base.pData ==
  5609. + (uint8_t *) & (krp->
  5610. + krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
  5611. + crp_nbits)) {
  5612. + *((uint32_t *) pLnModExpOpData->base.pData) =
  5613. + ntohl(*((uint32_t *) pLnModExpOpData->base.pData));
  5614. + }
  5615. + icp_ocfDrvFreeFlatBuffer(pResult);
  5616. + memset(pLnModExpOpData, 0, sizeof(CpaCyLnModExpOpData));
  5617. + kmem_cache_free(drvLnModExp_zone, pLnModExpOpData);
  5618. +
  5619. + crypto_kdone(krp);
  5620. +
  5621. + return;
  5622. +
  5623. +}
  5624. +
  5625. +/* Name : icp_ocfDrvModExpCRTCallBack
  5626. + *
  5627. + * Description : When this function returns it signifies that the LAC
  5628. + * component has completed the Mod Exp CRT operation.
  5629. + */
  5630. +static void
  5631. +icp_ocfDrvModExpCRTCallBack(void *callbackTag,
  5632. + CpaStatus status,
  5633. + void *pOpData, CpaFlatBuffer * pOutputData)
  5634. +{
  5635. + struct cryptkop *krp = NULL;
  5636. + CpaCyRsaDecryptOpData *pDecryptData = NULL;
  5637. +
  5638. + if (NULL == callbackTag) {
  5639. + DPRINTK("%s(): Invalid input parameters - "
  5640. + "callbackTag data is NULL\n", __FUNCTION__);
  5641. + return;
  5642. + }
  5643. +
  5644. + krp = (struct cryptkop *)callbackTag;
  5645. +
  5646. + if (NULL == pOpData) {
  5647. + DPRINTK("%s(): Invalid input parameters - "
  5648. + "Operation Data is NULL\n", __FUNCTION__);
  5649. + krp->krp_status = ECANCELED;
  5650. + crypto_kdone(krp);
  5651. + return;
  5652. + }
  5653. + pDecryptData = (CpaCyRsaDecryptOpData *) pOpData;
  5654. +
  5655. + if (NULL == pOutputData) {
  5656. + DPRINTK("%s(): Invalid input parameter - "
  5657. + "pOutputData is NULL\n", __FUNCTION__);
  5658. + memset(pDecryptData->pRecipientPrivateKey, 0,
  5659. + sizeof(CpaCyRsaPrivateKey));
  5660. + kmem_cache_free(drvRSAPrivateKey_zone,
  5661. + pDecryptData->pRecipientPrivateKey);
  5662. + memset(pDecryptData, 0, sizeof(CpaCyRsaDecryptOpData));
  5663. + kmem_cache_free(drvRSADecrypt_zone, pDecryptData);
  5664. + krp->krp_status = ECANCELED;
  5665. + crypto_kdone(krp);
  5666. + return;
  5667. + }
  5668. +
  5669. + if (CPA_STATUS_SUCCESS == status) {
  5670. + krp->krp_status = CRYPTO_OP_SUCCESS;
  5671. + } else {
  5672. + APRINTK("%s(): LAC Mod Exp CRT operation failed - "
  5673. + "Operation Status = %d\n", __FUNCTION__, status);
  5674. + krp->krp_status = ECANCELED;
  5675. + }
  5676. +
  5677. + icp_ocfDrvSwapBytes(pOutputData->pData, pOutputData->dataLenInBytes);
  5678. +
  5679. + icp_ocfDrvFreeFlatBuffer(pOutputData);
  5680. + memset(pDecryptData->pRecipientPrivateKey, 0,
  5681. + sizeof(CpaCyRsaPrivateKey));
  5682. + kmem_cache_free(drvRSAPrivateKey_zone,
  5683. + pDecryptData->pRecipientPrivateKey);
  5684. + memset(pDecryptData, 0, sizeof(CpaCyRsaDecryptOpData));
  5685. + kmem_cache_free(drvRSADecrypt_zone, pDecryptData);
  5686. +
  5687. + crypto_kdone(krp);
  5688. +
  5689. + return;
  5690. +}
  5691. +
  5692. +/* Name : icp_ocfDrvDsaRSSignCallBack
  5693. + *
  5694. + * Description : When this function returns it signifies that the LAC
  5695. + * component has completed the DSA RS sign operation.
  5696. + */
  5697. +static void
  5698. +icp_ocfDrvDsaRSSignCallBack(void *callbackTag,
  5699. + CpaStatus status,
  5700. + void *pOpData,
  5701. + CpaBoolean protocolStatus,
  5702. + CpaFlatBuffer * pR, CpaFlatBuffer * pS)
  5703. +{
  5704. + struct cryptkop *krp = NULL;
  5705. + CpaCyDsaRSSignOpData *pSignData = NULL;
  5706. +
  5707. + if (NULL == callbackTag) {
  5708. + DPRINTK("%s(): Invalid input parameters - "
  5709. + "callbackTag data is NULL\n", __FUNCTION__);
  5710. + return;
  5711. + }
  5712. +
  5713. + krp = (struct cryptkop *)callbackTag;
  5714. +
  5715. + if (NULL == pOpData) {
  5716. + DPRINTK("%s(): Invalid input parameters - "
  5717. + "Operation Data is NULL\n", __FUNCTION__);
  5718. + krp->krp_status = ECANCELED;
  5719. + crypto_kdone(krp);
  5720. + return;
  5721. + }
  5722. + pSignData = (CpaCyDsaRSSignOpData *) pOpData;
  5723. +
  5724. + if (NULL == pR) {
  5725. + DPRINTK("%s(): Invalid input parameter - "
  5726. + "pR sign is NULL\n", __FUNCTION__);
  5727. + icp_ocfDrvFreeFlatBuffer(pS);
  5728. + kmem_cache_free(drvDSARSSign_zone, pSignData);
  5729. + krp->krp_status = ECANCELED;
  5730. + crypto_kdone(krp);
  5731. + return;
  5732. + }
  5733. +
  5734. + if (NULL == pS) {
  5735. + DPRINTK("%s(): Invalid input parameter - "
  5736. + "pS sign is NULL\n", __FUNCTION__);
  5737. + icp_ocfDrvFreeFlatBuffer(pR);
  5738. + kmem_cache_free(drvDSARSSign_zone, pSignData);
  5739. + krp->krp_status = ECANCELED;
  5740. + crypto_kdone(krp);
  5741. + return;
  5742. + }
  5743. +
  5744. + if (CPA_STATUS_SUCCESS != status) {
  5745. + APRINTK("%s(): LAC DSA RS Sign operation failed - "
  5746. + "Operation Status = %d\n", __FUNCTION__, status);
  5747. + krp->krp_status = ECANCELED;
  5748. + } else {
  5749. + krp->krp_status = CRYPTO_OP_SUCCESS;
  5750. +
  5751. + if (CPA_TRUE != protocolStatus) {
  5752. + DPRINTK("%s(): LAC DSA RS Sign operation failed due "
  5753. + "to protocol error\n", __FUNCTION__);
  5754. + krp->krp_status = EIO;
  5755. + }
  5756. + }
  5757. +
  5758. + /* Swap bytes only when the callback status is successful and
  5759. + protocolStatus is set to true */
  5760. + if (CPA_STATUS_SUCCESS == status && CPA_TRUE == protocolStatus) {
  5761. + icp_ocfDrvSwapBytes(pR->pData, pR->dataLenInBytes);
  5762. + icp_ocfDrvSwapBytes(pS->pData, pS->dataLenInBytes);
  5763. + }
  5764. +
  5765. + icp_ocfDrvFreeFlatBuffer(pR);
  5766. + icp_ocfDrvFreeFlatBuffer(pS);
  5767. + memset(pSignData->K.pData, 0, pSignData->K.dataLenInBytes);
  5768. + kmem_cache_free(drvDSARSSignKValue_zone, pSignData->K.pData);
  5769. + memset(pSignData, 0, sizeof(CpaCyDsaRSSignOpData));
  5770. + kmem_cache_free(drvDSARSSign_zone, pSignData);
  5771. + crypto_kdone(krp);
  5772. +
  5773. + return;
  5774. +}
  5775. +
  5776. +/* Name : icp_ocfDrvDsaVerifyCallback
  5777. + *
  5778. + * Description : When this function returns it signifies that the LAC
  5779. + * component has completed the DSA Verify operation.
  5780. + */
  5781. +static void
  5782. +icp_ocfDrvDsaVerifyCallBack(void *callbackTag,
  5783. + CpaStatus status,
  5784. + void *pOpData, CpaBoolean verifyStatus)
  5785. +{
  5786. +
  5787. + struct cryptkop *krp = NULL;
  5788. + CpaCyDsaVerifyOpData *pVerData = NULL;
  5789. +
  5790. + if (NULL == callbackTag) {
  5791. + DPRINTK("%s(): Invalid input parameters - "
  5792. + "callbackTag data is NULL\n", __FUNCTION__);
  5793. + return;
  5794. + }
  5795. +
  5796. + krp = (struct cryptkop *)callbackTag;
  5797. +
  5798. + if (NULL == pOpData) {
  5799. + DPRINTK("%s(): Invalid input parameters - "
  5800. + "Operation Data is NULL\n", __FUNCTION__);
  5801. + krp->krp_status = ECANCELED;
  5802. + crypto_kdone(krp);
  5803. + return;
  5804. + }
  5805. + pVerData = (CpaCyDsaVerifyOpData *) pOpData;
  5806. +
  5807. + if (CPA_STATUS_SUCCESS != status) {
  5808. + APRINTK("%s(): LAC DSA Verify operation failed - "
  5809. + "Operation Status = %d\n", __FUNCTION__, status);
  5810. + krp->krp_status = ECANCELED;
  5811. + } else {
  5812. + krp->krp_status = CRYPTO_OP_SUCCESS;
  5813. +
  5814. + if (CPA_TRUE != verifyStatus) {
  5815. + DPRINTK("%s(): DSA signature invalid\n", __FUNCTION__);
  5816. + krp->krp_status = EIO;
  5817. + }
  5818. + }
  5819. +
  5820. + /* Swap bytes only when the callback status is successful and
  5821. + verifyStatus is set to true */
  5822. + /*Just swapping back the key values for now. Possibly all
  5823. + swapped buffers need to be reverted */
  5824. + if (CPA_STATUS_SUCCESS == status && CPA_TRUE == verifyStatus) {
  5825. + icp_ocfDrvSwapBytes(pVerData->R.pData,
  5826. + pVerData->R.dataLenInBytes);
  5827. + icp_ocfDrvSwapBytes(pVerData->S.pData,
  5828. + pVerData->S.dataLenInBytes);
  5829. + }
  5830. +
  5831. + memset(pVerData, 0, sizeof(CpaCyDsaVerifyOpData));
  5832. + kmem_cache_free(drvDSAVerify_zone, pVerData);
  5833. + crypto_kdone(krp);
  5834. +
  5835. + return;
  5836. +}
  5837. diff -Nur linux-2.6.30.orig/crypto/ocf/ep80579/icp_common.c linux-2.6.30/crypto/ocf/ep80579/icp_common.c
  5838. --- linux-2.6.30.orig/crypto/ocf/ep80579/icp_common.c 1970-01-01 01:00:00.000000000 +0100
  5839. +++ linux-2.6.30/crypto/ocf/ep80579/icp_common.c 2009-06-11 10:55:27.000000000 +0200
  5840. @@ -0,0 +1,891 @@
  5841. +/***************************************************************************
  5842. + *
  5843. + * This file is provided under a dual BSD/GPLv2 license. When using or
  5844. + * redistributing this file, you may do so under either license.
  5845. + *
  5846. + * GPL LICENSE SUMMARY
  5847. + *
  5848. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  5849. + *
  5850. + * This program is free software; you can redistribute it and/or modify
  5851. + * it under the terms of version 2 of the GNU General Public License as
  5852. + * published by the Free Software Foundation.
  5853. + *
  5854. + * This program is distributed in the hope that it will be useful, but
  5855. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  5856. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5857. + * General Public License for more details.
  5858. + *
  5859. + * You should have received a copy of the GNU General Public License
  5860. + * along with this program; if not, write to the Free Software
  5861. + * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  5862. + * The full GNU General Public License is included in this distribution
  5863. + * in the file called LICENSE.GPL.
  5864. + *
  5865. + * Contact Information:
  5866. + * Intel Corporation
  5867. + *
  5868. + * BSD LICENSE
  5869. + *
  5870. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  5871. + * All rights reserved.
  5872. + *
  5873. + * Redistribution and use in source and binary forms, with or without
  5874. + * modification, are permitted provided that the following conditions
  5875. + * are met:
  5876. + *
  5877. + * * Redistributions of source code must retain the above copyright
  5878. + * notice, this list of conditions and the following disclaimer.
  5879. + * * Redistributions in binary form must reproduce the above copyright
  5880. + * notice, this list of conditions and the following disclaimer in
  5881. + * the documentation and/or other materials provided with the
  5882. + * distribution.
  5883. + * * Neither the name of Intel Corporation nor the names of its
  5884. + * contributors may be used to endorse or promote products derived
  5885. + * from this software without specific prior written permission.
  5886. + *
  5887. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5888. + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5889. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5890. + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  5891. + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  5892. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5893. + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  5894. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  5895. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5896. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  5897. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5898. + *
  5899. + *
  5900. + * version: Security.L.1.0.130
  5901. + *
  5902. + ***************************************************************************/
  5903. +
  5904. +/*
  5905. + * An OCF module that uses Intel® QuickAssist Integrated Accelerator to do the
  5906. + * crypto.
  5907. + *
  5908. + * This driver requires the ICP Access Library that is available from Intel in
  5909. + * order to operate.
  5910. + */
  5911. +
  5912. +#include "icp_ocf.h"
  5913. +
  5914. +#define ICP_OCF_COMP_NAME "ICP_OCF"
  5915. +#define ICP_OCF_VER_MAIN (2)
  5916. +#define ICP_OCF_VER_MJR (0)
  5917. +#define ICP_OCF_VER_MNR (0)
  5918. +
  5919. +#define MAX_DEREG_RETRIES (100)
  5920. +#define DEFAULT_DEREG_RETRIES (10)
  5921. +#define DEFAULT_DEREG_DELAY_IN_JIFFIES (10)
  5922. +
  5923. +/* This defines the maximum number of sessions possible between OCF
  5924. + and the OCF Tolapai Driver. If set to zero, there is no limit. */
  5925. +#define DEFAULT_OCF_TO_DRV_MAX_SESSION_COUNT (0)
  5926. +#define NUM_SUPPORTED_CAPABILITIES (21)
  5927. +
  5928. +/*Slabs zones*/
  5929. +struct kmem_cache *drvSessionData_zone = NULL;
  5930. +struct kmem_cache *drvOpData_zone = NULL;
  5931. +struct kmem_cache *drvDH_zone = NULL;
  5932. +struct kmem_cache *drvLnModExp_zone = NULL;
  5933. +struct kmem_cache *drvRSADecrypt_zone = NULL;
  5934. +struct kmem_cache *drvRSAPrivateKey_zone = NULL;
  5935. +struct kmem_cache *drvDSARSSign_zone = NULL;
  5936. +struct kmem_cache *drvDSARSSignKValue_zone = NULL;
  5937. +struct kmem_cache *drvDSAVerify_zone = NULL;
  5938. +
  5939. +/*Slab zones for flatbuffers and bufferlist*/
  5940. +struct kmem_cache *drvFlatBuffer_zone = NULL;
  5941. +
  5942. +static int icp_ocfDrvInit(void);
  5943. +static void icp_ocfDrvExit(void);
  5944. +static void icp_ocfDrvFreeCaches(void);
  5945. +static void icp_ocfDrvDeferedFreeLacSessionProcess(void *arg);
  5946. +
  5947. +int32_t icp_ocfDrvDriverId = INVALID_DRIVER_ID;
  5948. +
  5949. +/* Module parameter - gives the number of times LAC deregistration shall be
  5950. + re-tried */
  5951. +int num_dereg_retries = DEFAULT_DEREG_RETRIES;
  5952. +
  5953. +/* Module parameter - gives the delay time in jiffies before a LAC session
  5954. + shall be attempted to be deregistered again */
  5955. +int dereg_retry_delay_in_jiffies = DEFAULT_DEREG_DELAY_IN_JIFFIES;
  5956. +
  5957. +/* Module parameter - gives the maximum number of sessions possible between
  5958. + OCF and the OCF Tolapai Driver. If set to zero, there is no limit.*/
  5959. +int max_sessions = DEFAULT_OCF_TO_DRV_MAX_SESSION_COUNT;
  5960. +
  5961. +/* This is set when the module is removed from the system, no further
  5962. + processing can take place if this is set */
  5963. +atomic_t icp_ocfDrvIsExiting = ATOMIC_INIT(0);
  5964. +
  5965. +/* This is used to show how many lac sessions were not deregistered*/
  5966. +atomic_t lac_session_failed_dereg_count = ATOMIC_INIT(0);
  5967. +
  5968. +/* This is used to track the number of registered sessions between OCF and
  5969. + * and the OCF Tolapai driver, when max_session is set to value other than
  5970. + * zero. This ensures that the max_session set for the OCF and the driver
  5971. + * is equal to the LAC registered sessions */
  5972. +atomic_t num_ocf_to_drv_registered_sessions = ATOMIC_INIT(0);
  5973. +
  5974. +/* Head of linked list used to store session data */
  5975. +struct list_head icp_ocfDrvGlobalSymListHead;
  5976. +struct list_head icp_ocfDrvGlobalSymListHead_FreeMemList;
  5977. +
  5978. +spinlock_t icp_ocfDrvSymSessInfoListSpinlock = SPIN_LOCK_UNLOCKED;
  5979. +rwlock_t icp_kmem_cache_destroy_alloc_lock = RW_LOCK_UNLOCKED;
  5980. +
  5981. +struct workqueue_struct *icp_ocfDrvFreeLacSessionWorkQ;
  5982. +
  5983. +struct icp_drvBuffListInfo defBuffListInfo;
  5984. +
  5985. +static struct {
  5986. + softc_device_decl sc_dev;
  5987. +} icpDev;
  5988. +
  5989. +static device_method_t icp_methods = {
  5990. + /* crypto device methods */
  5991. + DEVMETHOD(cryptodev_newsession, icp_ocfDrvNewSession),
  5992. + DEVMETHOD(cryptodev_freesession, icp_ocfDrvFreeLACSession),
  5993. + DEVMETHOD(cryptodev_process, icp_ocfDrvSymProcess),
  5994. + DEVMETHOD(cryptodev_kprocess, icp_ocfDrvPkeProcess),
  5995. +};
  5996. +
  5997. +module_param(num_dereg_retries, int, S_IRUGO);
  5998. +module_param(dereg_retry_delay_in_jiffies, int, S_IRUGO);
  5999. +module_param(max_sessions, int, S_IRUGO);
  6000. +
  6001. +MODULE_PARM_DESC(num_dereg_retries,
  6002. + "Number of times to retry LAC Sym Session Deregistration. "
  6003. + "Default 10, Max 100");
  6004. +MODULE_PARM_DESC(dereg_retry_delay_in_jiffies, "Delay in jiffies "
  6005. + "(added to a schedule() function call) before a LAC Sym "
  6006. + "Session Dereg is retried. Default 10");
  6007. +MODULE_PARM_DESC(max_sessions, "This sets the maximum number of sessions "
  6008. + "between OCF and this driver. If this value is set to zero, "
  6009. + "max session count checking is disabled. Default is zero(0)");
  6010. +
  6011. +/* Name : icp_ocfDrvInit
  6012. + *
  6013. + * Description : This function will register all the symmetric and asymmetric
  6014. + * functionality that will be accelerated by the hardware. It will also
  6015. + * get a unique driver ID from the OCF and initialise all slab caches
  6016. + */
  6017. +static int __init icp_ocfDrvInit(void)
  6018. +{
  6019. + int ocfStatus = 0;
  6020. +
  6021. + IPRINTK("=== %s ver %d.%d.%d ===\n", ICP_OCF_COMP_NAME,
  6022. + ICP_OCF_VER_MAIN, ICP_OCF_VER_MJR, ICP_OCF_VER_MNR);
  6023. +
  6024. + if (MAX_DEREG_RETRIES < num_dereg_retries) {
  6025. + EPRINTK("Session deregistration retry count set to greater "
  6026. + "than %d", MAX_DEREG_RETRIES);
  6027. + return -1;
  6028. + }
  6029. +
  6030. + /* Initialize and Start the Cryptographic component */
  6031. + if (CPA_STATUS_SUCCESS !=
  6032. + cpaCyStartInstance(CPA_INSTANCE_HANDLE_SINGLE)) {
  6033. + EPRINTK("Failed to initialize and start the instance "
  6034. + "of the Cryptographic component.\n");
  6035. + return -1;
  6036. + }
  6037. +
  6038. + /* Set the default size of BufferList to allocate */
  6039. + memset(&defBuffListInfo, 0, sizeof(struct icp_drvBuffListInfo));
  6040. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  6041. + icp_ocfDrvBufferListMemInfo(ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS,
  6042. + &defBuffListInfo)) {
  6043. + EPRINTK("Failed to get bufferlist memory info.\n");
  6044. + return -1;
  6045. + }
  6046. +
  6047. + /*Register OCF Tolapai Driver with OCF */
  6048. + memset(&icpDev, 0, sizeof(icpDev));
  6049. + softc_device_init(&icpDev, "icp", 0, icp_methods);
  6050. +
  6051. + icp_ocfDrvDriverId = crypto_get_driverid(softc_get_device(&icpDev),
  6052. + CRYPTOCAP_F_HARDWARE);
  6053. +
  6054. + if (icp_ocfDrvDriverId < 0) {
  6055. + EPRINTK("%s : ICP driver failed to register with OCF!\n",
  6056. + __FUNCTION__);
  6057. + return -ENODEV;
  6058. + }
  6059. +
  6060. + /*Create all the slab caches used by the OCF Tolapai Driver */
  6061. + drvSessionData_zone =
  6062. + ICP_CACHE_CREATE("ICP Session Data", struct icp_drvSessionData);
  6063. + ICP_CACHE_NULL_CHECK(drvSessionData_zone);
  6064. +
  6065. + /*
  6066. + * Allocation of the OpData includes the allocation space for meta data.
  6067. + * The memory after the opData structure is reserved for this meta data.
  6068. + */
  6069. + drvOpData_zone =
  6070. + kmem_cache_create("ICP Op Data", sizeof(struct icp_drvOpData) +
  6071. + defBuffListInfo.metaSize ,0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  6072. +
  6073. +
  6074. + ICP_CACHE_NULL_CHECK(drvOpData_zone);
  6075. +
  6076. + drvDH_zone = ICP_CACHE_CREATE("ICP DH data", CpaCyDhPhase1KeyGenOpData);
  6077. + ICP_CACHE_NULL_CHECK(drvDH_zone);
  6078. +
  6079. + drvLnModExp_zone =
  6080. + ICP_CACHE_CREATE("ICP ModExp data", CpaCyLnModExpOpData);
  6081. + ICP_CACHE_NULL_CHECK(drvLnModExp_zone);
  6082. +
  6083. + drvRSADecrypt_zone =
  6084. + ICP_CACHE_CREATE("ICP RSA decrypt data", CpaCyRsaDecryptOpData);
  6085. + ICP_CACHE_NULL_CHECK(drvRSADecrypt_zone);
  6086. +
  6087. + drvRSAPrivateKey_zone =
  6088. + ICP_CACHE_CREATE("ICP RSA private key data", CpaCyRsaPrivateKey);
  6089. + ICP_CACHE_NULL_CHECK(drvRSAPrivateKey_zone);
  6090. +
  6091. + drvDSARSSign_zone =
  6092. + ICP_CACHE_CREATE("ICP DSA Sign", CpaCyDsaRSSignOpData);
  6093. + ICP_CACHE_NULL_CHECK(drvDSARSSign_zone);
  6094. +
  6095. + /*too awkward to use a macro here */
  6096. + drvDSARSSignKValue_zone =
  6097. + kmem_cache_create("ICP DSA Sign Rand Val",
  6098. + DSA_RS_SIGN_PRIMEQ_SIZE_IN_BYTES, 0,
  6099. + SLAB_HWCACHE_ALIGN, NULL, NULL);
  6100. + ICP_CACHE_NULL_CHECK(drvDSARSSignKValue_zone);
  6101. +
  6102. + drvDSAVerify_zone =
  6103. + ICP_CACHE_CREATE("ICP DSA Verify", CpaCyDsaVerifyOpData);
  6104. + ICP_CACHE_NULL_CHECK(drvDSAVerify_zone);
  6105. +
  6106. + drvFlatBuffer_zone =
  6107. + ICP_CACHE_CREATE("ICP Flat Buffers", CpaFlatBuffer);
  6108. + ICP_CACHE_NULL_CHECK(drvFlatBuffer_zone);
  6109. +
  6110. + /* Register the ICP symmetric crypto support. */
  6111. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_NULL_CBC);
  6112. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_DES_CBC);
  6113. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_3DES_CBC);
  6114. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_AES_CBC);
  6115. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_ARC4);
  6116. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_MD5);
  6117. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_MD5_HMAC);
  6118. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA1);
  6119. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA1_HMAC);
  6120. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_256);
  6121. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_256_HMAC);
  6122. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_384);
  6123. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_384_HMAC);
  6124. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_512);
  6125. + ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(CRYPTO_SHA2_512_HMAC);
  6126. +
  6127. + /* Register the ICP asymmetric algorithm support */
  6128. + ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(CRK_DH_COMPUTE_KEY);
  6129. + ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(CRK_MOD_EXP);
  6130. + ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(CRK_MOD_EXP_CRT);
  6131. + ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(CRK_DSA_SIGN);
  6132. + ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(CRK_DSA_VERIFY);
  6133. +
  6134. + /* Register the ICP random number generator support */
  6135. + if (OCF_REGISTRATION_STATUS_SUCCESS ==
  6136. + crypto_rregister(icp_ocfDrvDriverId, icp_ocfDrvReadRandom, NULL)) {
  6137. + ocfStatus++;
  6138. + }
  6139. +
  6140. + if (OCF_ZERO_FUNCTIONALITY_REGISTERED == ocfStatus) {
  6141. + DPRINTK("%s: Failed to register any device capabilities\n",
  6142. + __FUNCTION__);
  6143. + icp_ocfDrvFreeCaches();
  6144. + icp_ocfDrvDriverId = INVALID_DRIVER_ID;
  6145. + return -ECANCELED;
  6146. + }
  6147. +
  6148. + DPRINTK("%s: Registered %d of %d device capabilities\n",
  6149. + __FUNCTION__, ocfStatus, NUM_SUPPORTED_CAPABILITIES);
  6150. +
  6151. +/*Session data linked list used during module exit*/
  6152. + INIT_LIST_HEAD(&icp_ocfDrvGlobalSymListHead);
  6153. + INIT_LIST_HEAD(&icp_ocfDrvGlobalSymListHead_FreeMemList);
  6154. +
  6155. + icp_ocfDrvFreeLacSessionWorkQ =
  6156. + create_singlethread_workqueue("ocfLacDeregWorkQueue");
  6157. +
  6158. + return 0;
  6159. +}
  6160. +
  6161. +/* Name : icp_ocfDrvExit
  6162. + *
  6163. + * Description : This function will deregister all the symmetric sessions
  6164. + * registered with the LAC component. It will also deregister all symmetric
  6165. + * and asymmetric functionality that can be accelerated by the hardware via OCF
  6166. + * and random number generation if it is enabled.
  6167. + */
  6168. +static void icp_ocfDrvExit(void)
  6169. +{
  6170. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  6171. + struct icp_drvSessionData *sessionData = NULL;
  6172. + struct icp_drvSessionData *tempSessionData = NULL;
  6173. + int i, remaining_delay_time_in_jiffies = 0;
  6174. + /* There is a possibility of a process or new session command being */
  6175. + /* sent before this variable is incremented. The aim of this variable */
  6176. + /* is to stop a loop of calls creating a deadlock situation which */
  6177. + /* would prevent the driver from exiting. */
  6178. +
  6179. + atomic_inc(&icp_ocfDrvIsExiting);
  6180. +
  6181. + /*Existing sessions will be routed to another driver after these calls */
  6182. + crypto_unregister_all(icp_ocfDrvDriverId);
  6183. + crypto_runregister_all(icp_ocfDrvDriverId);
  6184. +
  6185. + /*If any sessions are waiting to be deregistered, do that. This also
  6186. + flushes the work queue */
  6187. + destroy_workqueue(icp_ocfDrvFreeLacSessionWorkQ);
  6188. +
  6189. + /*ENTER CRITICAL SECTION */
  6190. + spin_lock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  6191. + list_for_each_entry_safe(tempSessionData, sessionData,
  6192. + &icp_ocfDrvGlobalSymListHead, listNode) {
  6193. + for (i = 0; i < num_dereg_retries; i++) {
  6194. + /*No harm if bad input - LAC will handle error cases */
  6195. + if (ICP_SESSION_RUNNING == tempSessionData->inUse) {
  6196. + lacStatus =
  6197. + cpaCySymRemoveSession
  6198. + (CPA_INSTANCE_HANDLE_SINGLE,
  6199. + tempSessionData->sessHandle);
  6200. + if (CPA_STATUS_SUCCESS == lacStatus) {
  6201. + /* Succesfully deregistered */
  6202. + break;
  6203. + } else if (CPA_STATUS_RETRY != lacStatus) {
  6204. + atomic_inc
  6205. + (&lac_session_failed_dereg_count);
  6206. + break;
  6207. + }
  6208. +
  6209. + /*schedule_timout returns the time left for completion if
  6210. + * this task is set to TASK_INTERRUPTIBLE */
  6211. + remaining_delay_time_in_jiffies =
  6212. + dereg_retry_delay_in_jiffies;
  6213. + while (0 > remaining_delay_time_in_jiffies) {
  6214. + remaining_delay_time_in_jiffies =
  6215. + schedule_timeout
  6216. + (remaining_delay_time_in_jiffies);
  6217. + }
  6218. +
  6219. + DPRINTK
  6220. + ("%s(): Retry %d to deregistrate the session\n",
  6221. + __FUNCTION__, i);
  6222. + }
  6223. + }
  6224. +
  6225. + /*remove from current list */
  6226. + list_del(&(tempSessionData->listNode));
  6227. + /*add to free mem linked list */
  6228. + list_add(&(tempSessionData->listNode),
  6229. + &icp_ocfDrvGlobalSymListHead_FreeMemList);
  6230. +
  6231. + }
  6232. +
  6233. + /*EXIT CRITICAL SECTION */
  6234. + spin_unlock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  6235. +
  6236. + /*set back to initial values */
  6237. + sessionData = NULL;
  6238. + /*still have a reference in our list! */
  6239. + tempSessionData = NULL;
  6240. + /*free memory */
  6241. + list_for_each_entry_safe(tempSessionData, sessionData,
  6242. + &icp_ocfDrvGlobalSymListHead_FreeMemList,
  6243. + listNode) {
  6244. +
  6245. + list_del(&(tempSessionData->listNode));
  6246. + /* Free allocated CpaCySymSessionCtx */
  6247. + if (NULL != tempSessionData->sessHandle) {
  6248. + kfree(tempSessionData->sessHandle);
  6249. + }
  6250. + memset(tempSessionData, 0, sizeof(struct icp_drvSessionData));
  6251. + kmem_cache_free(drvSessionData_zone, tempSessionData);
  6252. + }
  6253. +
  6254. + if (0 != atomic_read(&lac_session_failed_dereg_count)) {
  6255. + DPRINTK("%s(): %d LAC sessions were not deregistered "
  6256. + "correctly. This is not a clean exit! \n",
  6257. + __FUNCTION__,
  6258. + atomic_read(&lac_session_failed_dereg_count));
  6259. + }
  6260. +
  6261. + icp_ocfDrvFreeCaches();
  6262. + icp_ocfDrvDriverId = INVALID_DRIVER_ID;
  6263. +
  6264. + /* Shutdown the Cryptographic component */
  6265. + lacStatus = cpaCyStopInstance(CPA_INSTANCE_HANDLE_SINGLE);
  6266. + if (CPA_STATUS_SUCCESS != lacStatus) {
  6267. + DPRINTK("%s(): Failed to stop instance of the "
  6268. + "Cryptographic component.(status == %d)\n",
  6269. + __FUNCTION__, lacStatus);
  6270. + }
  6271. +
  6272. +}
  6273. +
  6274. +/* Name : icp_ocfDrvFreeCaches
  6275. + *
  6276. + * Description : This function deregisters all slab caches
  6277. + */
  6278. +static void icp_ocfDrvFreeCaches(void)
  6279. +{
  6280. + if (atomic_read(&icp_ocfDrvIsExiting) != CPA_TRUE) {
  6281. + atomic_set(&icp_ocfDrvIsExiting, 1);
  6282. + }
  6283. +
  6284. + /*Sym Zones */
  6285. + ICP_CACHE_DESTROY(drvSessionData_zone);
  6286. + ICP_CACHE_DESTROY(drvOpData_zone);
  6287. +
  6288. + /*Asym zones */
  6289. + ICP_CACHE_DESTROY(drvDH_zone);
  6290. + ICP_CACHE_DESTROY(drvLnModExp_zone);
  6291. + ICP_CACHE_DESTROY(drvRSADecrypt_zone);
  6292. + ICP_CACHE_DESTROY(drvRSAPrivateKey_zone);
  6293. + ICP_CACHE_DESTROY(drvDSARSSignKValue_zone);
  6294. + ICP_CACHE_DESTROY(drvDSARSSign_zone);
  6295. + ICP_CACHE_DESTROY(drvDSAVerify_zone);
  6296. +
  6297. + /*FlatBuffer and BufferList Zones */
  6298. + ICP_CACHE_DESTROY(drvFlatBuffer_zone);
  6299. +
  6300. +}
  6301. +
  6302. +/* Name : icp_ocfDrvDeregRetry
  6303. + *
  6304. + * Description : This function will try to farm the session deregistration
  6305. + * off to a work queue. If it fails, nothing more can be done and it
  6306. + * returns an error
  6307. + */
  6308. +
  6309. +int icp_ocfDrvDeregRetry(CpaCySymSessionCtx sessionToDeregister)
  6310. +{
  6311. + struct icp_ocfDrvFreeLacSession *workstore = NULL;
  6312. +
  6313. + DPRINTK("%s(): Retry - Deregistering session (%p)\n",
  6314. + __FUNCTION__, sessionToDeregister);
  6315. +
  6316. + /*make sure the session is not available to be allocated during this
  6317. + process */
  6318. + atomic_inc(&lac_session_failed_dereg_count);
  6319. +
  6320. + /*Farm off to work queue */
  6321. + workstore =
  6322. + kmalloc(sizeof(struct icp_ocfDrvFreeLacSession), GFP_ATOMIC);
  6323. + if (NULL == workstore) {
  6324. + DPRINTK("%s(): unable to free session - no memory available "
  6325. + "for work queue\n", __FUNCTION__);
  6326. + return ENOMEM;
  6327. + }
  6328. +
  6329. + workstore->sessionToDeregister = sessionToDeregister;
  6330. +
  6331. + INIT_WORK(&(workstore->work), icp_ocfDrvDeferedFreeLacSessionProcess,
  6332. + workstore);
  6333. + queue_work(icp_ocfDrvFreeLacSessionWorkQ, &(workstore->work));
  6334. +
  6335. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6336. +
  6337. +}
  6338. +
  6339. +/* Name : icp_ocfDrvDeferedFreeLacSessionProcess
  6340. + *
  6341. + * Description : This function will retry (module input parameter)
  6342. + * 'num_dereg_retries' times to deregister any symmetric session that recieves a
  6343. + * CPA_STATUS_RETRY message from the LAC component. This function is run in
  6344. + * Thread context because it is called from a worker thread
  6345. + */
  6346. +static void icp_ocfDrvDeferedFreeLacSessionProcess(void *arg)
  6347. +{
  6348. + struct icp_ocfDrvFreeLacSession *workstore = NULL;
  6349. + CpaCySymSessionCtx sessionToDeregister = NULL;
  6350. + int i = 0;
  6351. + int remaining_delay_time_in_jiffies = 0;
  6352. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  6353. +
  6354. + workstore = (struct icp_ocfDrvFreeLacSession *)arg;
  6355. + if (NULL == workstore) {
  6356. + DPRINTK("%s() function called with null parameter \n",
  6357. + __FUNCTION__);
  6358. + return;
  6359. + }
  6360. +
  6361. + sessionToDeregister = workstore->sessionToDeregister;
  6362. + kfree(workstore);
  6363. +
  6364. + /*if exiting, give deregistration one more blast only */
  6365. + if (atomic_read(&icp_ocfDrvIsExiting) == CPA_TRUE) {
  6366. + lacStatus = cpaCySymRemoveSession(CPA_INSTANCE_HANDLE_SINGLE,
  6367. + sessionToDeregister);
  6368. +
  6369. + if (lacStatus != CPA_STATUS_SUCCESS) {
  6370. + DPRINTK("%s() Failed to Dereg LAC session %p "
  6371. + "during module exit\n", __FUNCTION__,
  6372. + sessionToDeregister);
  6373. + return;
  6374. + }
  6375. +
  6376. + atomic_dec(&lac_session_failed_dereg_count);
  6377. + return;
  6378. + }
  6379. +
  6380. + for (i = 0; i <= num_dereg_retries; i++) {
  6381. + lacStatus = cpaCySymRemoveSession(CPA_INSTANCE_HANDLE_SINGLE,
  6382. + sessionToDeregister);
  6383. +
  6384. + if (lacStatus == CPA_STATUS_SUCCESS) {
  6385. + atomic_dec(&lac_session_failed_dereg_count);
  6386. + return;
  6387. + }
  6388. + if (lacStatus != CPA_STATUS_RETRY) {
  6389. + DPRINTK("%s() Failed to deregister session - lacStatus "
  6390. + " = %d", __FUNCTION__, lacStatus);
  6391. + break;
  6392. + }
  6393. +
  6394. + /*schedule_timout returns the time left for completion if this
  6395. + task is set to TASK_INTERRUPTIBLE */
  6396. + remaining_delay_time_in_jiffies = dereg_retry_delay_in_jiffies;
  6397. + while (0 > remaining_delay_time_in_jiffies) {
  6398. + remaining_delay_time_in_jiffies =
  6399. + schedule_timeout(remaining_delay_time_in_jiffies);
  6400. + }
  6401. +
  6402. + }
  6403. +
  6404. + DPRINTK("%s(): Unable to deregister session\n", __FUNCTION__);
  6405. + DPRINTK("%s(): Number of unavailable LAC sessions = %d\n", __FUNCTION__,
  6406. + atomic_read(&lac_session_failed_dereg_count));
  6407. +}
  6408. +
  6409. +/* Name : icp_ocfDrvPtrAndLenToFlatBuffer
  6410. + *
  6411. + * Description : This function converts a "pointer and length" buffer
  6412. + * structure to Fredericksburg Flat Buffer (CpaFlatBuffer) format.
  6413. + *
  6414. + * This function assumes that the data passed in are valid.
  6415. + */
  6416. +inline void
  6417. +icp_ocfDrvPtrAndLenToFlatBuffer(void *pData, uint32_t len,
  6418. + CpaFlatBuffer * pFlatBuffer)
  6419. +{
  6420. + pFlatBuffer->pData = pData;
  6421. + pFlatBuffer->dataLenInBytes = len;
  6422. +}
  6423. +
  6424. +/* Name : icp_ocfDrvSingleSkBuffToFlatBuffer
  6425. + *
  6426. + * Description : This function converts a single socket buffer (sk_buff)
  6427. + * structure to a Fredericksburg Flat Buffer (CpaFlatBuffer) format.
  6428. + *
  6429. + * This function assumes that the data passed in are valid.
  6430. + */
  6431. +static inline void
  6432. +icp_ocfDrvSingleSkBuffToFlatBuffer(struct sk_buff *pSkb,
  6433. + CpaFlatBuffer * pFlatBuffer)
  6434. +{
  6435. + pFlatBuffer->pData = pSkb->data;
  6436. + pFlatBuffer->dataLenInBytes = skb_headlen(pSkb);
  6437. +}
  6438. +
  6439. +/* Name : icp_ocfDrvSkBuffToBufferList
  6440. + *
  6441. + * Description : This function converts a socket buffer (sk_buff) structure to
  6442. + * Fredericksburg Scatter/Gather (CpaBufferList) buffer format.
  6443. + *
  6444. + * This function assumes that the bufferlist has been allocated with the correct
  6445. + * number of buffer arrays.
  6446. + *
  6447. + */
  6448. +inline int
  6449. +icp_ocfDrvSkBuffToBufferList(struct sk_buff *pSkb, CpaBufferList * bufferList)
  6450. +{
  6451. + CpaFlatBuffer *curFlatBuffer = NULL;
  6452. + char *skbuffPageAddr = NULL;
  6453. + struct sk_buff *pCurFrag = NULL;
  6454. + struct skb_shared_info *pShInfo = NULL;
  6455. + uint32_t page_offset = 0, i = 0;
  6456. +
  6457. + DPRINTK("%s(): Entry Point\n", __FUNCTION__);
  6458. +
  6459. + /*
  6460. + * In all cases, the first skb needs to be translated to FlatBuffer.
  6461. + * Perform a buffer translation for the first skbuff
  6462. + */
  6463. + curFlatBuffer = bufferList->pBuffers;
  6464. + icp_ocfDrvSingleSkBuffToFlatBuffer(pSkb, curFlatBuffer);
  6465. +
  6466. + /* Set the userData to point to the original sk_buff */
  6467. + bufferList->pUserData = (void *)pSkb;
  6468. +
  6469. + /* We now know we'll have at least one element in the SGL */
  6470. + bufferList->numBuffers = 1;
  6471. +
  6472. + if (0 == skb_is_nonlinear(pSkb)) {
  6473. + /* Is a linear buffer - therefore it's a single skbuff */
  6474. + DPRINTK("%s(): Exit Point\n", __FUNCTION__);
  6475. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6476. + }
  6477. +
  6478. + curFlatBuffer++;
  6479. + pShInfo = skb_shinfo(pSkb);
  6480. + if (pShInfo->frag_list != NULL && pShInfo->nr_frags != 0) {
  6481. + EPRINTK("%s():"
  6482. + "Translation for a combination of frag_list "
  6483. + "and frags[] array not supported!\n", __FUNCTION__);
  6484. + return ICP_OCF_DRV_STATUS_FAIL;
  6485. + } else if (pShInfo->frag_list != NULL) {
  6486. + /*
  6487. + * Non linear skbuff supported through frag_list
  6488. + * Perform translation for each fragment (sk_buff)
  6489. + * in the frag_list of the first sk_buff.
  6490. + */
  6491. + for (pCurFrag = pShInfo->frag_list;
  6492. + pCurFrag != NULL; pCurFrag = pCurFrag->next) {
  6493. + icp_ocfDrvSingleSkBuffToFlatBuffer(pCurFrag,
  6494. + curFlatBuffer);
  6495. + curFlatBuffer++;
  6496. + bufferList->numBuffers++;
  6497. + }
  6498. + } else if (pShInfo->nr_frags != 0) {
  6499. + /*
  6500. + * Perform translation for each fragment in frags array
  6501. + * and add to the BufferList
  6502. + */
  6503. + for (i = 0; i < pShInfo->nr_frags; i++) {
  6504. + /* Get the page address and offset of this frag */
  6505. + skbuffPageAddr = (char *)pShInfo->frags[i].page;
  6506. + page_offset = pShInfo->frags[i].page_offset;
  6507. +
  6508. + /* Convert a pointer and length to a flat buffer */
  6509. + icp_ocfDrvPtrAndLenToFlatBuffer(skbuffPageAddr +
  6510. + page_offset,
  6511. + pShInfo->frags[i].size,
  6512. + curFlatBuffer);
  6513. + curFlatBuffer++;
  6514. + bufferList->numBuffers++;
  6515. + }
  6516. + } else {
  6517. + EPRINTK("%s():" "Could not recognize skbuff fragments!\n",
  6518. + __FUNCTION__);
  6519. + return ICP_OCF_DRV_STATUS_FAIL;
  6520. + }
  6521. +
  6522. + DPRINTK("%s(): Exit Point\n", __FUNCTION__);
  6523. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6524. +}
  6525. +
  6526. +/* Name : icp_ocfDrvBufferListToSkBuff
  6527. + *
  6528. + * Description : This function converts a Fredericksburg Scatter/Gather
  6529. + * (CpaBufferList) buffer format to socket buffer structure.
  6530. + */
  6531. +inline int
  6532. +icp_ocfDrvBufferListToSkBuff(CpaBufferList * bufferList, struct sk_buff **skb)
  6533. +{
  6534. + DPRINTK("%s(): Entry Point\n", __FUNCTION__);
  6535. +
  6536. + /* Retrieve the orignal skbuff */
  6537. + *skb = (struct sk_buff *)bufferList->pUserData;
  6538. + if (NULL == *skb) {
  6539. + EPRINTK("%s():"
  6540. + "Error on converting from a BufferList. "
  6541. + "The BufferList does not contain an sk_buff.\n",
  6542. + __FUNCTION__);
  6543. + return ICP_OCF_DRV_STATUS_FAIL;
  6544. + }
  6545. + DPRINTK("%s(): Exit Point\n", __FUNCTION__);
  6546. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6547. +}
  6548. +
  6549. +/* Name : icp_ocfDrvPtrAndLenToBufferList
  6550. + *
  6551. + * Description : This function converts a "pointer and length" buffer
  6552. + * structure to Fredericksburg Scatter/Gather Buffer (CpaBufferList) format.
  6553. + *
  6554. + * This function assumes that the data passed in are valid.
  6555. + */
  6556. +inline void
  6557. +icp_ocfDrvPtrAndLenToBufferList(void *pDataIn, uint32_t length,
  6558. + CpaBufferList * pBufferList)
  6559. +{
  6560. + pBufferList->numBuffers = 1;
  6561. + pBufferList->pBuffers->pData = pDataIn;
  6562. + pBufferList->pBuffers->dataLenInBytes = length;
  6563. +}
  6564. +
  6565. +/* Name : icp_ocfDrvBufferListToPtrAndLen
  6566. + *
  6567. + * Description : This function converts Fredericksburg Scatter/Gather Buffer
  6568. + * (CpaBufferList) format to a "pointer and length" buffer structure.
  6569. + *
  6570. + * This function assumes that the data passed in are valid.
  6571. + */
  6572. +inline void
  6573. +icp_ocfDrvBufferListToPtrAndLen(CpaBufferList * pBufferList,
  6574. + void **ppDataOut, uint32_t * pLength)
  6575. +{
  6576. + *ppDataOut = pBufferList->pBuffers->pData;
  6577. + *pLength = pBufferList->pBuffers->dataLenInBytes;
  6578. +}
  6579. +
  6580. +/* Name : icp_ocfDrvBufferListMemInfo
  6581. + *
  6582. + * Description : This function will set the number of flat buffers in
  6583. + * bufferlist, the size of memory to allocate for the pPrivateMetaData
  6584. + * member of the CpaBufferList.
  6585. + */
  6586. +int
  6587. +icp_ocfDrvBufferListMemInfo(uint16_t numBuffers,
  6588. + struct icp_drvBuffListInfo *buffListInfo)
  6589. +{
  6590. + buffListInfo->numBuffers = numBuffers;
  6591. +
  6592. + if (CPA_STATUS_SUCCESS !=
  6593. + cpaCyBufferListGetMetaSize(CPA_INSTANCE_HANDLE_SINGLE,
  6594. + buffListInfo->numBuffers,
  6595. + &(buffListInfo->metaSize))) {
  6596. + EPRINTK("%s() Failed to get buffer list meta size.\n",
  6597. + __FUNCTION__);
  6598. + return ICP_OCF_DRV_STATUS_FAIL;
  6599. + }
  6600. +
  6601. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6602. +}
  6603. +
  6604. +/* Name : icp_ocfDrvGetSkBuffFrags
  6605. + *
  6606. + * Description : This function will determine the number of
  6607. + * fragments in a socket buffer(sk_buff).
  6608. + */
  6609. +inline uint16_t icp_ocfDrvGetSkBuffFrags(struct sk_buff * pSkb)
  6610. +{
  6611. + uint16_t numFrags = 0;
  6612. + struct sk_buff *pCurFrag = NULL;
  6613. + struct skb_shared_info *pShInfo = NULL;
  6614. +
  6615. + if (NULL == pSkb)
  6616. + return 0;
  6617. +
  6618. + numFrags = 1;
  6619. + if (0 == skb_is_nonlinear(pSkb)) {
  6620. + /* Linear buffer - it's a single skbuff */
  6621. + return numFrags;
  6622. + }
  6623. +
  6624. + pShInfo = skb_shinfo(pSkb);
  6625. + if (NULL != pShInfo->frag_list && 0 != pShInfo->nr_frags) {
  6626. + EPRINTK("%s(): Combination of frag_list "
  6627. + "and frags[] array not supported!\n", __FUNCTION__);
  6628. + return 0;
  6629. + } else if (0 != pShInfo->nr_frags) {
  6630. + numFrags += pShInfo->nr_frags;
  6631. + return numFrags;
  6632. + } else if (NULL != pShInfo->frag_list) {
  6633. + for (pCurFrag = pShInfo->frag_list;
  6634. + pCurFrag != NULL; pCurFrag = pCurFrag->next) {
  6635. + numFrags++;
  6636. + }
  6637. + return numFrags;
  6638. + } else {
  6639. + return 0;
  6640. + }
  6641. +}
  6642. +
  6643. +/* Name : icp_ocfDrvFreeFlatBuffer
  6644. + *
  6645. + * Description : This function will deallocate flat buffer.
  6646. + */
  6647. +inline void icp_ocfDrvFreeFlatBuffer(CpaFlatBuffer * pFlatBuffer)
  6648. +{
  6649. + if (pFlatBuffer != NULL) {
  6650. + memset(pFlatBuffer, 0, sizeof(CpaFlatBuffer));
  6651. + kmem_cache_free(drvFlatBuffer_zone, pFlatBuffer);
  6652. + }
  6653. +}
  6654. +
  6655. +/* Name : icp_ocfDrvAllocMetaData
  6656. + *
  6657. + * Description : This function will allocate memory for the
  6658. + * pPrivateMetaData member of CpaBufferList.
  6659. + */
  6660. +inline int
  6661. +icp_ocfDrvAllocMetaData(CpaBufferList * pBufferList,
  6662. + const struct icp_drvOpData *pOpData)
  6663. +{
  6664. + Cpa32U metaSize = 0;
  6665. +
  6666. + if (pBufferList->numBuffers <= ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS){
  6667. + void *pOpDataStartAddr = (void *)pOpData;
  6668. +
  6669. + if (0 == defBuffListInfo.metaSize) {
  6670. + pBufferList->pPrivateMetaData = NULL;
  6671. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6672. + }
  6673. + /*
  6674. + * The meta data allocation has been included as part of the
  6675. + * op data. It has been pre-allocated in memory just after the
  6676. + * icp_drvOpData structure.
  6677. + */
  6678. + pBufferList->pPrivateMetaData = pOpDataStartAddr +
  6679. + sizeof(struct icp_drvOpData);
  6680. + } else {
  6681. + if (CPA_STATUS_SUCCESS !=
  6682. + cpaCyBufferListGetMetaSize(CPA_INSTANCE_HANDLE_SINGLE,
  6683. + pBufferList->numBuffers,
  6684. + &metaSize)) {
  6685. + EPRINTK("%s() Failed to get buffer list meta size.\n",
  6686. + __FUNCTION__);
  6687. + return ICP_OCF_DRV_STATUS_FAIL;
  6688. + }
  6689. +
  6690. + if (0 == metaSize) {
  6691. + pBufferList->pPrivateMetaData = NULL;
  6692. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6693. + }
  6694. +
  6695. + pBufferList->pPrivateMetaData = kmalloc(metaSize, GFP_ATOMIC);
  6696. + }
  6697. + if (NULL == pBufferList->pPrivateMetaData) {
  6698. + EPRINTK("%s() Failed to allocate pPrivateMetaData.\n",
  6699. + __FUNCTION__);
  6700. + return ICP_OCF_DRV_STATUS_FAIL;
  6701. + }
  6702. +
  6703. + return ICP_OCF_DRV_STATUS_SUCCESS;
  6704. +}
  6705. +
  6706. +/* Name : icp_ocfDrvFreeMetaData
  6707. + *
  6708. + * Description : This function will deallocate pPrivateMetaData memory.
  6709. + */
  6710. +inline void icp_ocfDrvFreeMetaData(CpaBufferList * pBufferList)
  6711. +{
  6712. + if (NULL == pBufferList->pPrivateMetaData) {
  6713. + return;
  6714. + }
  6715. +
  6716. + /*
  6717. + * Only free the meta data if the BufferList has more than
  6718. + * ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS number of buffers.
  6719. + * Otherwise, the meta data shall be freed when the icp_drvOpData is
  6720. + * freed.
  6721. + */
  6722. + if (ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS < pBufferList->numBuffers){
  6723. + kfree(pBufferList->pPrivateMetaData);
  6724. + }
  6725. +}
  6726. +
  6727. +module_init(icp_ocfDrvInit);
  6728. +module_exit(icp_ocfDrvExit);
  6729. +MODULE_LICENSE("Dual BSD/GPL");
  6730. +MODULE_AUTHOR("Intel");
  6731. +MODULE_DESCRIPTION("OCF Driver for Intel Quick Assist crypto acceleration");
  6732. diff -Nur linux-2.6.30.orig/crypto/ocf/ep80579/icp_ocf.h linux-2.6.30/crypto/ocf/ep80579/icp_ocf.h
  6733. --- linux-2.6.30.orig/crypto/ocf/ep80579/icp_ocf.h 1970-01-01 01:00:00.000000000 +0100
  6734. +++ linux-2.6.30/crypto/ocf/ep80579/icp_ocf.h 2009-06-11 10:55:27.000000000 +0200
  6735. @@ -0,0 +1,363 @@
  6736. +/***************************************************************************
  6737. + *
  6738. + * This file is provided under a dual BSD/GPLv2 license. When using or
  6739. + * redistributing this file, you may do so under either license.
  6740. + *
  6741. + * GPL LICENSE SUMMARY
  6742. + *
  6743. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  6744. + *
  6745. + * This program is free software; you can redistribute it and/or modify
  6746. + * it under the terms of version 2 of the GNU General Public License as
  6747. + * published by the Free Software Foundation.
  6748. + *
  6749. + * This program is distributed in the hope that it will be useful, but
  6750. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  6751. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6752. + * General Public License for more details.
  6753. + *
  6754. + * You should have received a copy of the GNU General Public License
  6755. + * along with this program; if not, write to the Free Software
  6756. + * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  6757. + * The full GNU General Public License is included in this distribution
  6758. + * in the file called LICENSE.GPL.
  6759. + *
  6760. + * Contact Information:
  6761. + * Intel Corporation
  6762. + *
  6763. + * BSD LICENSE
  6764. + *
  6765. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  6766. + * All rights reserved.
  6767. + *
  6768. + * Redistribution and use in source and binary forms, with or without
  6769. + * modification, are permitted provided that the following conditions
  6770. + * are met:
  6771. + *
  6772. + * * Redistributions of source code must retain the above copyright
  6773. + * notice, this list of conditions and the following disclaimer.
  6774. + * * Redistributions in binary form must reproduce the above copyright
  6775. + * notice, this list of conditions and the following disclaimer in
  6776. + * the documentation and/or other materials provided with the
  6777. + * distribution.
  6778. + * * Neither the name of Intel Corporation nor the names of its
  6779. + * contributors may be used to endorse or promote products derived
  6780. + * from this software without specific prior written permission.
  6781. + *
  6782. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6783. + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6784. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6785. + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6786. + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6787. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6788. + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6789. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6790. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6791. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6792. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6793. + *
  6794. + *
  6795. + * version: Security.L.1.0.130
  6796. + *
  6797. + ***************************************************************************/
  6798. +
  6799. +/*
  6800. + * OCF drv driver header file for the Intel ICP processor.
  6801. + */
  6802. +
  6803. +#ifndef ICP_OCF_H
  6804. +#define ICP_OCF_H
  6805. +
  6806. +#include <linux/crypto.h>
  6807. +#include <linux/delay.h>
  6808. +#include <linux/skbuff.h>
  6809. +
  6810. +#include "cryptodev.h"
  6811. +#include "uio.h"
  6812. +
  6813. +#include "cpa.h"
  6814. +#include "cpa_cy_im.h"
  6815. +#include "cpa_cy_sym.h"
  6816. +#include "cpa_cy_rand.h"
  6817. +#include "cpa_cy_dh.h"
  6818. +#include "cpa_cy_rsa.h"
  6819. +#include "cpa_cy_ln.h"
  6820. +#include "cpa_cy_common.h"
  6821. +#include "cpa_cy_dsa.h"
  6822. +
  6823. +#define NUM_BITS_IN_BYTE (8)
  6824. +#define NUM_BITS_IN_BYTE_MINUS_ONE (NUM_BITS_IN_BYTE -1)
  6825. +#define INVALID_DRIVER_ID (-1)
  6826. +#define RETURN_RAND_NUM_GEN_FAILED (-1)
  6827. +
  6828. +/*This is define means only one operation can be chained to another
  6829. +(resulting in one chain of two operations)*/
  6830. +#define MAX_NUM_OF_CHAINED_OPS (1)
  6831. +/*This is the max block cipher initialisation vector*/
  6832. +#define MAX_IV_LEN_IN_BYTES (20)
  6833. +/*This is used to check whether the OCF to this driver session limit has
  6834. + been disabled*/
  6835. +#define NO_OCF_TO_DRV_MAX_SESSIONS (0)
  6836. +
  6837. +/*OCF values mapped here*/
  6838. +#define ICP_SHA1_DIGEST_SIZE_IN_BYTES (SHA1_HASH_LEN)
  6839. +#define ICP_SHA256_DIGEST_SIZE_IN_BYTES (SHA2_256_HASH_LEN)
  6840. +#define ICP_SHA384_DIGEST_SIZE_IN_BYTES (SHA2_384_HASH_LEN)
  6841. +#define ICP_SHA512_DIGEST_SIZE_IN_BYTES (SHA2_512_HASH_LEN)
  6842. +#define ICP_MD5_DIGEST_SIZE_IN_BYTES (MD5_HASH_LEN)
  6843. +#define ARC4_COUNTER_LEN (ARC4_BLOCK_LEN)
  6844. +
  6845. +#define OCF_REGISTRATION_STATUS_SUCCESS (0)
  6846. +#define OCF_ZERO_FUNCTIONALITY_REGISTERED (0)
  6847. +#define ICP_OCF_DRV_NO_CRYPTO_PROCESS_ERROR (0)
  6848. +#define ICP_OCF_DRV_STATUS_SUCCESS (0)
  6849. +#define ICP_OCF_DRV_STATUS_FAIL (1)
  6850. +
  6851. +/*Turn on/off debug options*/
  6852. +#define ICP_OCF_PRINT_DEBUG_MESSAGES (0)
  6853. +#define ICP_OCF_PRINT_KERN_ALERT (1)
  6854. +#define ICP_OCF_PRINT_KERN_ERRS (1)
  6855. +
  6856. +/*DSA Prime Q size in bytes (as defined in the standard) */
  6857. +#define DSA_RS_SIGN_PRIMEQ_SIZE_IN_BYTES (20)
  6858. +
  6859. +/*MACRO DEFINITIONS*/
  6860. +
  6861. +#define BITS_TO_BYTES(bytes, bits) \
  6862. + bytes = (bits + NUM_BITS_IN_BYTE_MINUS_ONE) / NUM_BITS_IN_BYTE
  6863. +
  6864. +#define ICP_CACHE_CREATE(cache_ID, cache_name) \
  6865. + kmem_cache_create(cache_ID, sizeof(cache_name),0, \
  6866. + SLAB_HWCACHE_ALIGN, NULL, NULL);
  6867. +
  6868. +#define ICP_CACHE_NULL_CHECK(slab_zone) \
  6869. +{ \
  6870. + if(NULL == slab_zone){ \
  6871. + icp_ocfDrvFreeCaches(); \
  6872. + EPRINTK("%s() line %d: Not enough memory!\n", \
  6873. + __FUNCTION__, __LINE__); \
  6874. + return ENOMEM; \
  6875. + } \
  6876. +}
  6877. +
  6878. +#define ICP_CACHE_DESTROY(slab_zone) \
  6879. +{ \
  6880. + if(NULL != slab_zone){ \
  6881. + kmem_cache_destroy(slab_zone); \
  6882. + slab_zone = NULL; \
  6883. + } \
  6884. +}
  6885. +
  6886. +#define ICP_REGISTER_SYM_FUNCTIONALITY_WITH_OCF(alg) \
  6887. +{ \
  6888. + if(OCF_REGISTRATION_STATUS_SUCCESS == \
  6889. + crypto_register(icp_ocfDrvDriverId, \
  6890. + alg, \
  6891. + 0, \
  6892. + 0)) { \
  6893. + ocfStatus++; \
  6894. + } \
  6895. +}
  6896. +
  6897. +#define ICP_REGISTER_ASYM_FUNCTIONALITY_WITH_OCF(alg) \
  6898. +{ \
  6899. + if(OCF_REGISTRATION_STATUS_SUCCESS == \
  6900. + crypto_kregister(icp_ocfDrvDriverId, \
  6901. + alg, \
  6902. + 0)){ \
  6903. + ocfStatus++; \
  6904. + } \
  6905. +}
  6906. +
  6907. +#if ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  6908. +#define DPRINTK(args...) \
  6909. +{ \
  6910. + printk(args); \
  6911. +}
  6912. +
  6913. +#else //ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  6914. +
  6915. +#define DPRINTK(args...)
  6916. +
  6917. +#endif //ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  6918. +
  6919. +#if ICP_OCF_PRINT_KERN_ALERT == 1
  6920. +#define APRINTK(args...) \
  6921. +{ \
  6922. + printk(KERN_ALERT args); \
  6923. +}
  6924. +
  6925. +#else //ICP_OCF_PRINT_KERN_ALERT == 1
  6926. +
  6927. +#define APRINTK(args...)
  6928. +
  6929. +#endif //ICP_OCF_PRINT_KERN_ALERT == 1
  6930. +
  6931. +#if ICP_OCF_PRINT_KERN_ERRS == 1
  6932. +#define EPRINTK(args...) \
  6933. +{ \
  6934. + printk(KERN_ERR args); \
  6935. +}
  6936. +
  6937. +#else //ICP_OCF_PRINT_KERN_ERRS == 1
  6938. +
  6939. +#define EPRINTK(args...)
  6940. +
  6941. +#endif //ICP_OCF_PRINT_KERN_ERRS == 1
  6942. +
  6943. +#define IPRINTK(args...) \
  6944. +{ \
  6945. + printk(KERN_INFO args); \
  6946. +}
  6947. +
  6948. +/*END OF MACRO DEFINITIONS*/
  6949. +
  6950. +typedef enum {
  6951. + ICP_OCF_DRV_ALG_CIPHER = 0,
  6952. + ICP_OCF_DRV_ALG_HASH
  6953. +} icp_ocf_drv_alg_type_t;
  6954. +
  6955. +/* These are all defined in icp_common.c */
  6956. +extern atomic_t lac_session_failed_dereg_count;
  6957. +extern atomic_t icp_ocfDrvIsExiting;
  6958. +extern atomic_t num_ocf_to_drv_registered_sessions;
  6959. +
  6960. +/*These are use inputs used in icp_sym.c and icp_common.c
  6961. + They are instantiated in icp_common.c*/
  6962. +extern int max_sessions;
  6963. +
  6964. +extern int32_t icp_ocfDrvDriverId;
  6965. +extern struct list_head icp_ocfDrvGlobalSymListHead;
  6966. +extern struct list_head icp_ocfDrvGlobalSymListHead_FreeMemList;
  6967. +extern struct workqueue_struct *icp_ocfDrvFreeLacSessionWorkQ;
  6968. +extern spinlock_t icp_ocfDrvSymSessInfoListSpinlock;
  6969. +extern rwlock_t icp_kmem_cache_destroy_alloc_lock;
  6970. +
  6971. +/*Slab zones for symettric functionality, instantiated in icp_common.c*/
  6972. +extern struct kmem_cache *drvSessionData_zone;
  6973. +extern struct kmem_cache *drvOpData_zone;
  6974. +
  6975. +/*Slabs zones for asymettric functionality, instantiated in icp_common.c*/
  6976. +extern struct kmem_cache *drvDH_zone;
  6977. +extern struct kmem_cache *drvLnModExp_zone;
  6978. +extern struct kmem_cache *drvRSADecrypt_zone;
  6979. +extern struct kmem_cache *drvRSAPrivateKey_zone;
  6980. +extern struct kmem_cache *drvDSARSSign_zone;
  6981. +extern struct kmem_cache *drvDSARSSignKValue_zone;
  6982. +extern struct kmem_cache *drvDSAVerify_zone;
  6983. +
  6984. +/*Slab zones for flatbuffers and bufferlist*/
  6985. +extern struct kmem_cache *drvFlatBuffer_zone;
  6986. +
  6987. +#define ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS (16)
  6988. +
  6989. +struct icp_drvBuffListInfo {
  6990. + Cpa16U numBuffers;
  6991. + Cpa32U metaSize;
  6992. + Cpa32U metaOffset;
  6993. + Cpa32U buffListSize;
  6994. +};
  6995. +extern struct icp_drvBuffListInfo defBuffListInfo;
  6996. +
  6997. +/*
  6998. +* This struct is used to keep a reference to the relevant node in the list
  6999. +* of sessionData structs, to the buffer type required by OCF and to the OCF
  7000. +* provided crp struct that needs to be returned. All this info is needed in
  7001. +* the callback function.
  7002. +*
  7003. +* IV can sometimes be stored in non-contiguous memory (e.g. skbuff
  7004. +* linked/frag list, therefore a contiguous memory space for the IV data must be
  7005. +* created and passed to LAC
  7006. +*
  7007. +*/
  7008. +struct icp_drvOpData {
  7009. + CpaCySymOpData lacOpData;
  7010. + uint32_t digestSizeInBytes;
  7011. + struct cryptop *crp;
  7012. + uint8_t bufferType;
  7013. + uint8_t ivData[MAX_IV_LEN_IN_BYTES];
  7014. + uint16_t numBufferListArray;
  7015. + CpaBufferList srcBuffer;
  7016. + CpaFlatBuffer bufferListArray[ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS];
  7017. + CpaBoolean verifyResult;
  7018. +};
  7019. +/*Values used to derisk chances of performs being called against
  7020. +deregistered sessions (for which the slab page has been reclaimed)
  7021. +This is not a fix - since page frames are reclaimed from a slab, one cannot
  7022. +rely on that memory not being re-used by another app.*/
  7023. +typedef enum {
  7024. + ICP_SESSION_INITIALISED = 0x5C5C5C,
  7025. + ICP_SESSION_RUNNING = 0x005C00,
  7026. + ICP_SESSION_DEREGISTERED = 0xC5C5C5
  7027. +} usage_derisk;
  7028. +
  7029. +/*
  7030. +This is the OCF<->OCF_DRV session object:
  7031. +
  7032. +1.The first member is a listNode. These session objects are added to a linked
  7033. + list in order to make it easier to remove them all at session exit time.
  7034. +2.The second member is used to give the session object state and derisk the
  7035. + possibility of OCF batch calls executing against a deregistered session (as
  7036. + described above).
  7037. +3.The third member is a LAC<->OCF_DRV session handle (initialised with the first
  7038. + perform request for that session).
  7039. +4.The fourth is the LAC session context. All the parameters for this structure
  7040. + are only known when the first perform request for this session occurs. That is
  7041. + why the OCF Tolapai Driver only registers a new LAC session at perform time
  7042. +*/
  7043. +struct icp_drvSessionData {
  7044. + struct list_head listNode;
  7045. + usage_derisk inUse;
  7046. + CpaCySymSessionCtx sessHandle;
  7047. + CpaCySymSessionSetupData lacSessCtx;
  7048. +};
  7049. +
  7050. +/* This struct is required for deferred session
  7051. + deregistration as a work queue function can
  7052. + only have one argument*/
  7053. +struct icp_ocfDrvFreeLacSession {
  7054. + CpaCySymSessionCtx sessionToDeregister;
  7055. + struct work_struct work;
  7056. +};
  7057. +
  7058. +int icp_ocfDrvNewSession(device_t dev, uint32_t * sild, struct cryptoini *cri);
  7059. +
  7060. +int icp_ocfDrvFreeLACSession(device_t dev, uint64_t sid);
  7061. +
  7062. +int icp_ocfDrvSymProcess(device_t dev, struct cryptop *crp, int hint);
  7063. +
  7064. +int icp_ocfDrvPkeProcess(device_t dev, struct cryptkop *krp, int hint);
  7065. +
  7066. +int icp_ocfDrvReadRandom(void *arg, uint32_t * buf, int maxwords);
  7067. +
  7068. +int icp_ocfDrvDeregRetry(CpaCySymSessionCtx sessionToDeregister);
  7069. +
  7070. +int icp_ocfDrvSkBuffToBufferList(struct sk_buff *skb,
  7071. + CpaBufferList * bufferList);
  7072. +
  7073. +int icp_ocfDrvBufferListToSkBuff(CpaBufferList * bufferList,
  7074. + struct sk_buff **skb);
  7075. +
  7076. +void icp_ocfDrvPtrAndLenToFlatBuffer(void *pData, uint32_t len,
  7077. + CpaFlatBuffer * pFlatBuffer);
  7078. +
  7079. +void icp_ocfDrvPtrAndLenToBufferList(void *pDataIn, uint32_t length,
  7080. + CpaBufferList * pBufferList);
  7081. +
  7082. +void icp_ocfDrvBufferListToPtrAndLen(CpaBufferList * pBufferList,
  7083. + void **ppDataOut, uint32_t * pLength);
  7084. +
  7085. +int icp_ocfDrvBufferListMemInfo(uint16_t numBuffers,
  7086. + struct icp_drvBuffListInfo *buffListInfo);
  7087. +
  7088. +uint16_t icp_ocfDrvGetSkBuffFrags(struct sk_buff *pSkb);
  7089. +
  7090. +void icp_ocfDrvFreeFlatBuffer(CpaFlatBuffer * pFlatBuffer);
  7091. +
  7092. +int icp_ocfDrvAllocMetaData(CpaBufferList * pBufferList,
  7093. + const struct icp_drvOpData *pOpData);
  7094. +
  7095. +void icp_ocfDrvFreeMetaData(CpaBufferList * pBufferList);
  7096. +
  7097. +#endif
  7098. +/* ICP_OCF_H */
  7099. diff -Nur linux-2.6.30.orig/crypto/ocf/ep80579/icp_sym.c linux-2.6.30/crypto/ocf/ep80579/icp_sym.c
  7100. --- linux-2.6.30.orig/crypto/ocf/ep80579/icp_sym.c 1970-01-01 01:00:00.000000000 +0100
  7101. +++ linux-2.6.30/crypto/ocf/ep80579/icp_sym.c 2009-06-11 10:55:27.000000000 +0200
  7102. @@ -0,0 +1,1382 @@
  7103. +/***************************************************************************
  7104. + *
  7105. + * This file is provided under a dual BSD/GPLv2 license. When using or
  7106. + * redistributing this file, you may do so under either license.
  7107. + *
  7108. + * GPL LICENSE SUMMARY
  7109. + *
  7110. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  7111. + *
  7112. + * This program is free software; you can redistribute it and/or modify
  7113. + * it under the terms of version 2 of the GNU General Public License as
  7114. + * published by the Free Software Foundation.
  7115. + *
  7116. + * This program is distributed in the hope that it will be useful, but
  7117. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  7118. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7119. + * General Public License for more details.
  7120. + *
  7121. + * You should have received a copy of the GNU General Public License
  7122. + * along with this program; if not, write to the Free Software
  7123. + * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  7124. + * The full GNU General Public License is included in this distribution
  7125. + * in the file called LICENSE.GPL.
  7126. + *
  7127. + * Contact Information:
  7128. + * Intel Corporation
  7129. + *
  7130. + * BSD LICENSE
  7131. + *
  7132. + * Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  7133. + * All rights reserved.
  7134. + *
  7135. + * Redistribution and use in source and binary forms, with or without
  7136. + * modification, are permitted provided that the following conditions
  7137. + * are met:
  7138. + *
  7139. + * * Redistributions of source code must retain the above copyright
  7140. + * notice, this list of conditions and the following disclaimer.
  7141. + * * Redistributions in binary form must reproduce the above copyright
  7142. + * notice, this list of conditions and the following disclaimer in
  7143. + * the documentation and/or other materials provided with the
  7144. + * distribution.
  7145. + * * Neither the name of Intel Corporation nor the names of its
  7146. + * contributors may be used to endorse or promote products derived
  7147. + * from this software without specific prior written permission.
  7148. + *
  7149. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7150. + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7151. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7152. + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7153. + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7154. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7155. + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7156. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7157. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7158. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7159. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7160. + *
  7161. + *
  7162. + * version: Security.L.1.0.130
  7163. + *
  7164. + ***************************************************************************/
  7165. +/*
  7166. + * An OCF module that uses the API for Intel® QuickAssist Technology to do the
  7167. + * cryptography.
  7168. + *
  7169. + * This driver requires the ICP Access Library that is available from Intel in
  7170. + * order to operate.
  7171. + */
  7172. +
  7173. +#include "icp_ocf.h"
  7174. +
  7175. +/*This is the call back function for all symmetric cryptographic processes.
  7176. + Its main functionality is to free driver crypto operation structure and to
  7177. + call back to OCF*/
  7178. +static void
  7179. +icp_ocfDrvSymCallBack(void *callbackTag,
  7180. + CpaStatus status,
  7181. + const CpaCySymOp operationType,
  7182. + void *pOpData,
  7183. + CpaBufferList * pDstBuffer, CpaBoolean verifyResult);
  7184. +
  7185. +/*This function is used to extract crypto processing information from the OCF
  7186. + inputs, so as that it may be passed onto LAC*/
  7187. +static int
  7188. +icp_ocfDrvProcessDataSetup(struct icp_drvOpData *drvOpData,
  7189. + struct cryptodesc *crp_desc);
  7190. +
  7191. +/*This function checks whether the crp_desc argument pertains to a digest or a
  7192. + cipher operation*/
  7193. +static int icp_ocfDrvAlgCheck(struct cryptodesc *crp_desc);
  7194. +
  7195. +/*This function copies all the passed in session context information and stores
  7196. + it in a LAC context structure*/
  7197. +static int
  7198. +icp_ocfDrvAlgorithmSetup(struct cryptoini *cri,
  7199. + CpaCySymSessionSetupData * lacSessCtx);
  7200. +
  7201. +/*This top level function is used to find a pointer to where a digest is
  7202. + stored/needs to be inserted. */
  7203. +static uint8_t *icp_ocfDrvDigestPointerFind(struct icp_drvOpData *drvOpData,
  7204. + struct cryptodesc *crp_desc);
  7205. +
  7206. +/*This function is called when a digest pointer has to be found within a
  7207. + SKBUFF.*/
  7208. +static inline uint8_t *icp_ocfDrvSkbuffDigestPointerFind(struct icp_drvOpData
  7209. + *drvOpData,
  7210. + int offsetInBytes,
  7211. + uint32_t
  7212. + digestSizeInBytes);
  7213. +
  7214. +/*The following two functions are called if the SKBUFF digest pointer is not
  7215. + positioned in the linear portion of the buffer (i.e. it is in a linked SKBUFF
  7216. + or page fragment).*/
  7217. +/*This function takes care of the page fragment case.*/
  7218. +static inline uint8_t *icp_ocfDrvDigestSkbNRFragsCheck(struct sk_buff *skb,
  7219. + struct skb_shared_info
  7220. + *skb_shared,
  7221. + int offsetInBytes,
  7222. + uint32_t
  7223. + digestSizeInBytes);
  7224. +
  7225. +/*This function takes care of the linked list case.*/
  7226. +static inline uint8_t *icp_ocfDrvDigestSkbFragListCheck(struct sk_buff *skb,
  7227. + struct skb_shared_info
  7228. + *skb_shared,
  7229. + int offsetInBytes,
  7230. + uint32_t
  7231. + digestSizeInBytes);
  7232. +
  7233. +/*This function is used to free an OCF->OCF_DRV session object*/
  7234. +static void icp_ocfDrvFreeOCFSession(struct icp_drvSessionData *sessionData);
  7235. +
  7236. +/*max IOV buffs supported in a UIO structure*/
  7237. +#define NUM_IOV_SUPPORTED (1)
  7238. +
  7239. +/* Name : icp_ocfDrvSymCallBack
  7240. + *
  7241. + * Description : When this function returns it signifies that the LAC
  7242. + * component has completed the relevant symmetric operation.
  7243. + *
  7244. + * Notes : The callbackTag is a pointer to an icp_drvOpData. This memory
  7245. + * object was passed to LAC for the cryptographic processing and contains all
  7246. + * the relevant information for cleaning up buffer handles etc. so that the
  7247. + * OCF Tolapai Driver portion of this crypto operation can be fully completed.
  7248. + */
  7249. +static void
  7250. +icp_ocfDrvSymCallBack(void *callbackTag,
  7251. + CpaStatus status,
  7252. + const CpaCySymOp operationType,
  7253. + void *pOpData,
  7254. + CpaBufferList * pDstBuffer, CpaBoolean verifyResult)
  7255. +{
  7256. + struct cryptop *crp = NULL;
  7257. + struct icp_drvOpData *temp_drvOpData =
  7258. + (struct icp_drvOpData *)callbackTag;
  7259. + uint64_t *tempBasePtr = NULL;
  7260. + uint32_t tempLen = 0;
  7261. +
  7262. + if (NULL == temp_drvOpData) {
  7263. + DPRINTK("%s(): The callback from the LAC component"
  7264. + " has failed due to Null userOpaque data"
  7265. + "(status == %d).\n", __FUNCTION__, status);
  7266. + DPRINTK("%s(): Unable to call OCF back! \n", __FUNCTION__);
  7267. + return;
  7268. + }
  7269. +
  7270. + crp = temp_drvOpData->crp;
  7271. + crp->crp_etype = ICP_OCF_DRV_NO_CRYPTO_PROCESS_ERROR;
  7272. +
  7273. + if (NULL == pOpData) {
  7274. + DPRINTK("%s(): The callback from the LAC component"
  7275. + " has failed due to Null Symmetric Op data"
  7276. + "(status == %d).\n", __FUNCTION__, status);
  7277. + crp->crp_etype = ECANCELED;
  7278. + crypto_done(crp);
  7279. + return;
  7280. + }
  7281. +
  7282. + if (NULL == pDstBuffer) {
  7283. + DPRINTK("%s(): The callback from the LAC component"
  7284. + " has failed due to Null Dst Bufferlist data"
  7285. + "(status == %d).\n", __FUNCTION__, status);
  7286. + crp->crp_etype = ECANCELED;
  7287. + crypto_done(crp);
  7288. + return;
  7289. + }
  7290. +
  7291. + if (CPA_STATUS_SUCCESS == status) {
  7292. +
  7293. + if (temp_drvOpData->bufferType == CRYPTO_F_SKBUF) {
  7294. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  7295. + icp_ocfDrvBufferListToSkBuff(pDstBuffer,
  7296. + (struct sk_buff **)
  7297. + &(crp->crp_buf))) {
  7298. + EPRINTK("%s(): BufferList to SkBuff "
  7299. + "conversion error.\n", __FUNCTION__);
  7300. + crp->crp_etype = EPERM;
  7301. + }
  7302. + } else {
  7303. + icp_ocfDrvBufferListToPtrAndLen(pDstBuffer,
  7304. + (void **)&tempBasePtr,
  7305. + &tempLen);
  7306. + crp->crp_olen = (int)tempLen;
  7307. + }
  7308. +
  7309. + } else {
  7310. + DPRINTK("%s(): The callback from the LAC component has failed"
  7311. + "(status == %d).\n", __FUNCTION__, status);
  7312. +
  7313. + crp->crp_etype = ECANCELED;
  7314. + }
  7315. +
  7316. + if (temp_drvOpData->numBufferListArray >
  7317. + ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS) {
  7318. + kfree(pDstBuffer->pBuffers);
  7319. + }
  7320. + icp_ocfDrvFreeMetaData(pDstBuffer);
  7321. + kmem_cache_free(drvOpData_zone, temp_drvOpData);
  7322. +
  7323. + /* Invoke the OCF callback function */
  7324. + crypto_done(crp);
  7325. +
  7326. + return;
  7327. +}
  7328. +
  7329. +/* Name : icp_ocfDrvNewSession
  7330. + *
  7331. + * Description : This function will create a new Driver<->OCF session
  7332. + *
  7333. + * Notes : LAC session registration happens during the first perform call.
  7334. + * That is the first time we know all information about a given session.
  7335. + */
  7336. +int icp_ocfDrvNewSession(device_t dev, uint32_t * sid, struct cryptoini *cri)
  7337. +{
  7338. + struct icp_drvSessionData *sessionData = NULL;
  7339. + uint32_t delete_session = 0;
  7340. +
  7341. + /* The SID passed in should be our driver ID. We can return the */
  7342. + /* local ID (LID) which is a unique identifier which we can use */
  7343. + /* to differentiate between the encrypt/decrypt LAC session handles */
  7344. + if (NULL == sid) {
  7345. + EPRINTK("%s(): Invalid input parameters - NULL sid.\n",
  7346. + __FUNCTION__);
  7347. + return EINVAL;
  7348. + }
  7349. +
  7350. + if (NULL == cri) {
  7351. + EPRINTK("%s(): Invalid input parameters - NULL cryptoini.\n",
  7352. + __FUNCTION__);
  7353. + return EINVAL;
  7354. + }
  7355. +
  7356. + if (icp_ocfDrvDriverId != *sid) {
  7357. + EPRINTK("%s(): Invalid input parameters - bad driver ID\n",
  7358. + __FUNCTION__);
  7359. + EPRINTK("\t sid = 0x08%p \n \t cri = 0x08%p \n", sid, cri);
  7360. + return EINVAL;
  7361. + }
  7362. +
  7363. + sessionData = kmem_cache_zalloc(drvSessionData_zone, GFP_ATOMIC);
  7364. + if (NULL == sessionData) {
  7365. + DPRINTK("%s():No memory for Session Data\n", __FUNCTION__);
  7366. + return ENOMEM;
  7367. + }
  7368. +
  7369. + /*ENTER CRITICAL SECTION */
  7370. + spin_lock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  7371. + /*put this check in the spinlock so no new sessions can be added to the
  7372. + linked list when we are exiting */
  7373. + if (CPA_TRUE == atomic_read(&icp_ocfDrvIsExiting)) {
  7374. + delete_session++;
  7375. +
  7376. + } else if (NO_OCF_TO_DRV_MAX_SESSIONS != max_sessions) {
  7377. + if (atomic_read(&num_ocf_to_drv_registered_sessions) >=
  7378. + (max_sessions -
  7379. + atomic_read(&lac_session_failed_dereg_count))) {
  7380. + delete_session++;
  7381. + } else {
  7382. + atomic_inc(&num_ocf_to_drv_registered_sessions);
  7383. + /* Add to session data linked list */
  7384. + list_add(&(sessionData->listNode),
  7385. + &icp_ocfDrvGlobalSymListHead);
  7386. + }
  7387. +
  7388. + } else if (NO_OCF_TO_DRV_MAX_SESSIONS == max_sessions) {
  7389. + list_add(&(sessionData->listNode),
  7390. + &icp_ocfDrvGlobalSymListHead);
  7391. + }
  7392. +
  7393. + sessionData->inUse = ICP_SESSION_INITIALISED;
  7394. +
  7395. + /*EXIT CRITICAL SECTION */
  7396. + spin_unlock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  7397. +
  7398. + if (delete_session) {
  7399. + DPRINTK("%s():No Session handles available\n", __FUNCTION__);
  7400. + kmem_cache_free(drvSessionData_zone, sessionData);
  7401. + return EPERM;
  7402. + }
  7403. +
  7404. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  7405. + icp_ocfDrvAlgorithmSetup(cri, &(sessionData->lacSessCtx))) {
  7406. + DPRINTK("%s():algorithm not supported\n", __FUNCTION__);
  7407. + icp_ocfDrvFreeOCFSession(sessionData);
  7408. + return EINVAL;
  7409. + }
  7410. +
  7411. + if (cri->cri_next) {
  7412. + if (cri->cri_next->cri_next != NULL) {
  7413. + DPRINTK("%s():only two chained algorithms supported\n",
  7414. + __FUNCTION__);
  7415. + icp_ocfDrvFreeOCFSession(sessionData);
  7416. + return EPERM;
  7417. + }
  7418. +
  7419. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  7420. + icp_ocfDrvAlgorithmSetup(cri->cri_next,
  7421. + &(sessionData->lacSessCtx))) {
  7422. + DPRINTK("%s():second algorithm not supported\n",
  7423. + __FUNCTION__);
  7424. + icp_ocfDrvFreeOCFSession(sessionData);
  7425. + return EINVAL;
  7426. + }
  7427. +
  7428. + sessionData->lacSessCtx.symOperation =
  7429. + CPA_CY_SYM_OP_ALGORITHM_CHAINING;
  7430. + }
  7431. +
  7432. + *sid = (uint32_t) sessionData;
  7433. +
  7434. + return ICP_OCF_DRV_STATUS_SUCCESS;
  7435. +}
  7436. +
  7437. +/* Name : icp_ocfDrvAlgorithmSetup
  7438. + *
  7439. + * Description : This function builds the session context data from the
  7440. + * information supplied through OCF. Algorithm chain order and whether the
  7441. + * session is Encrypt/Decrypt can only be found out at perform time however, so
  7442. + * the session is registered with LAC at that time.
  7443. + */
  7444. +static int
  7445. +icp_ocfDrvAlgorithmSetup(struct cryptoini *cri,
  7446. + CpaCySymSessionSetupData * lacSessCtx)
  7447. +{
  7448. +
  7449. + lacSessCtx->sessionPriority = CPA_CY_PRIORITY_NORMAL;
  7450. +
  7451. + switch (cri->cri_alg) {
  7452. +
  7453. + case CRYPTO_NULL_CBC:
  7454. + DPRINTK("%s(): NULL CBC\n", __FUNCTION__);
  7455. + lacSessCtx->symOperation = CPA_CY_SYM_OP_CIPHER;
  7456. + lacSessCtx->cipherSetupData.cipherAlgorithm =
  7457. + CPA_CY_SYM_CIPHER_NULL;
  7458. + lacSessCtx->cipherSetupData.cipherKeyLenInBytes =
  7459. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7460. + lacSessCtx->cipherSetupData.pCipherKey = cri->cri_key;
  7461. + break;
  7462. +
  7463. + case CRYPTO_DES_CBC:
  7464. + DPRINTK("%s(): DES CBC\n", __FUNCTION__);
  7465. + lacSessCtx->symOperation = CPA_CY_SYM_OP_CIPHER;
  7466. + lacSessCtx->cipherSetupData.cipherAlgorithm =
  7467. + CPA_CY_SYM_CIPHER_DES_CBC;
  7468. + lacSessCtx->cipherSetupData.cipherKeyLenInBytes =
  7469. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7470. + lacSessCtx->cipherSetupData.pCipherKey = cri->cri_key;
  7471. + break;
  7472. +
  7473. + case CRYPTO_3DES_CBC:
  7474. + DPRINTK("%s(): 3DES CBC\n", __FUNCTION__);
  7475. + lacSessCtx->symOperation = CPA_CY_SYM_OP_CIPHER;
  7476. + lacSessCtx->cipherSetupData.cipherAlgorithm =
  7477. + CPA_CY_SYM_CIPHER_3DES_CBC;
  7478. + lacSessCtx->cipherSetupData.cipherKeyLenInBytes =
  7479. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7480. + lacSessCtx->cipherSetupData.pCipherKey = cri->cri_key;
  7481. + break;
  7482. +
  7483. + case CRYPTO_AES_CBC:
  7484. + DPRINTK("%s(): AES CBC\n", __FUNCTION__);
  7485. + lacSessCtx->symOperation = CPA_CY_SYM_OP_CIPHER;
  7486. + lacSessCtx->cipherSetupData.cipherAlgorithm =
  7487. + CPA_CY_SYM_CIPHER_AES_CBC;
  7488. + lacSessCtx->cipherSetupData.cipherKeyLenInBytes =
  7489. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7490. + lacSessCtx->cipherSetupData.pCipherKey = cri->cri_key;
  7491. + break;
  7492. +
  7493. + case CRYPTO_ARC4:
  7494. + DPRINTK("%s(): ARC4\n", __FUNCTION__);
  7495. + lacSessCtx->symOperation = CPA_CY_SYM_OP_CIPHER;
  7496. + lacSessCtx->cipherSetupData.cipherAlgorithm =
  7497. + CPA_CY_SYM_CIPHER_ARC4;
  7498. + lacSessCtx->cipherSetupData.cipherKeyLenInBytes =
  7499. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7500. + lacSessCtx->cipherSetupData.pCipherKey = cri->cri_key;
  7501. + break;
  7502. +
  7503. + case CRYPTO_SHA1:
  7504. + DPRINTK("%s(): SHA1\n", __FUNCTION__);
  7505. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7506. + lacSessCtx->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
  7507. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
  7508. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7509. + (cri->cri_mlen ?
  7510. + cri->cri_mlen : ICP_SHA1_DIGEST_SIZE_IN_BYTES);
  7511. +
  7512. + break;
  7513. +
  7514. + case CRYPTO_SHA1_HMAC:
  7515. + DPRINTK("%s(): SHA1_HMAC\n", __FUNCTION__);
  7516. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7517. + lacSessCtx->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
  7518. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
  7519. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7520. + (cri->cri_mlen ?
  7521. + cri->cri_mlen : ICP_SHA1_DIGEST_SIZE_IN_BYTES);
  7522. + lacSessCtx->hashSetupData.authModeSetupData.authKey =
  7523. + cri->cri_key;
  7524. + lacSessCtx->hashSetupData.authModeSetupData.authKeyLenInBytes =
  7525. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7526. + lacSessCtx->hashSetupData.authModeSetupData.aadLenInBytes = 0;
  7527. +
  7528. + break;
  7529. +
  7530. + case CRYPTO_SHA2_256:
  7531. + DPRINTK("%s(): SHA256\n", __FUNCTION__);
  7532. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7533. + lacSessCtx->hashSetupData.hashAlgorithm =
  7534. + CPA_CY_SYM_HASH_SHA256;
  7535. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
  7536. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7537. + (cri->cri_mlen ?
  7538. + cri->cri_mlen : ICP_SHA256_DIGEST_SIZE_IN_BYTES);
  7539. +
  7540. + break;
  7541. +
  7542. + case CRYPTO_SHA2_256_HMAC:
  7543. + DPRINTK("%s(): SHA256_HMAC\n", __FUNCTION__);
  7544. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7545. + lacSessCtx->hashSetupData.hashAlgorithm =
  7546. + CPA_CY_SYM_HASH_SHA256;
  7547. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
  7548. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7549. + (cri->cri_mlen ?
  7550. + cri->cri_mlen : ICP_SHA256_DIGEST_SIZE_IN_BYTES);
  7551. + lacSessCtx->hashSetupData.authModeSetupData.authKey =
  7552. + cri->cri_key;
  7553. + lacSessCtx->hashSetupData.authModeSetupData.authKeyLenInBytes =
  7554. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7555. + lacSessCtx->hashSetupData.authModeSetupData.aadLenInBytes = 0;
  7556. +
  7557. + break;
  7558. +
  7559. + case CRYPTO_SHA2_384:
  7560. + DPRINTK("%s(): SHA384\n", __FUNCTION__);
  7561. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7562. + lacSessCtx->hashSetupData.hashAlgorithm =
  7563. + CPA_CY_SYM_HASH_SHA384;
  7564. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
  7565. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7566. + (cri->cri_mlen ?
  7567. + cri->cri_mlen : ICP_SHA384_DIGEST_SIZE_IN_BYTES);
  7568. +
  7569. + break;
  7570. +
  7571. + case CRYPTO_SHA2_384_HMAC:
  7572. + DPRINTK("%s(): SHA384_HMAC\n", __FUNCTION__);
  7573. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7574. + lacSessCtx->hashSetupData.hashAlgorithm =
  7575. + CPA_CY_SYM_HASH_SHA384;
  7576. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
  7577. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7578. + (cri->cri_mlen ?
  7579. + cri->cri_mlen : ICP_SHA384_DIGEST_SIZE_IN_BYTES);
  7580. + lacSessCtx->hashSetupData.authModeSetupData.authKey =
  7581. + cri->cri_key;
  7582. + lacSessCtx->hashSetupData.authModeSetupData.authKeyLenInBytes =
  7583. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7584. + lacSessCtx->hashSetupData.authModeSetupData.aadLenInBytes = 0;
  7585. +
  7586. + break;
  7587. +
  7588. + case CRYPTO_SHA2_512:
  7589. + DPRINTK("%s(): SHA512\n", __FUNCTION__);
  7590. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7591. + lacSessCtx->hashSetupData.hashAlgorithm =
  7592. + CPA_CY_SYM_HASH_SHA512;
  7593. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
  7594. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7595. + (cri->cri_mlen ?
  7596. + cri->cri_mlen : ICP_SHA512_DIGEST_SIZE_IN_BYTES);
  7597. +
  7598. + break;
  7599. +
  7600. + case CRYPTO_SHA2_512_HMAC:
  7601. + DPRINTK("%s(): SHA512_HMAC\n", __FUNCTION__);
  7602. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7603. + lacSessCtx->hashSetupData.hashAlgorithm =
  7604. + CPA_CY_SYM_HASH_SHA512;
  7605. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
  7606. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7607. + (cri->cri_mlen ?
  7608. + cri->cri_mlen : ICP_SHA512_DIGEST_SIZE_IN_BYTES);
  7609. + lacSessCtx->hashSetupData.authModeSetupData.authKey =
  7610. + cri->cri_key;
  7611. + lacSessCtx->hashSetupData.authModeSetupData.authKeyLenInBytes =
  7612. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7613. + lacSessCtx->hashSetupData.authModeSetupData.aadLenInBytes = 0;
  7614. +
  7615. + break;
  7616. +
  7617. + case CRYPTO_MD5:
  7618. + DPRINTK("%s(): MD5\n", __FUNCTION__);
  7619. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7620. + lacSessCtx->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_MD5;
  7621. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
  7622. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7623. + (cri->cri_mlen ?
  7624. + cri->cri_mlen : ICP_MD5_DIGEST_SIZE_IN_BYTES);
  7625. +
  7626. + break;
  7627. +
  7628. + case CRYPTO_MD5_HMAC:
  7629. + DPRINTK("%s(): MD5_HMAC\n", __FUNCTION__);
  7630. + lacSessCtx->symOperation = CPA_CY_SYM_OP_HASH;
  7631. + lacSessCtx->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_MD5;
  7632. + lacSessCtx->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
  7633. + lacSessCtx->hashSetupData.digestResultLenInBytes =
  7634. + (cri->cri_mlen ?
  7635. + cri->cri_mlen : ICP_MD5_DIGEST_SIZE_IN_BYTES);
  7636. + lacSessCtx->hashSetupData.authModeSetupData.authKey =
  7637. + cri->cri_key;
  7638. + lacSessCtx->hashSetupData.authModeSetupData.authKeyLenInBytes =
  7639. + cri->cri_klen / NUM_BITS_IN_BYTE;
  7640. + lacSessCtx->hashSetupData.authModeSetupData.aadLenInBytes = 0;
  7641. +
  7642. + break;
  7643. +
  7644. + default:
  7645. + DPRINTK("%s(): ALG Setup FAIL\n", __FUNCTION__);
  7646. + return ICP_OCF_DRV_STATUS_FAIL;
  7647. + }
  7648. +
  7649. + return ICP_OCF_DRV_STATUS_SUCCESS;
  7650. +}
  7651. +
  7652. +/* Name : icp_ocfDrvFreeOCFSession
  7653. + *
  7654. + * Description : This function deletes all existing Session data representing
  7655. + * the Cryptographic session established between OCF and this driver. This
  7656. + * also includes freeing the memory allocated for the session context. The
  7657. + * session object is also removed from the session linked list.
  7658. + */
  7659. +static void icp_ocfDrvFreeOCFSession(struct icp_drvSessionData *sessionData)
  7660. +{
  7661. +
  7662. + sessionData->inUse = ICP_SESSION_DEREGISTERED;
  7663. +
  7664. + /*ENTER CRITICAL SECTION */
  7665. + spin_lock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  7666. +
  7667. + if (CPA_TRUE == atomic_read(&icp_ocfDrvIsExiting)) {
  7668. + /*If the Driver is exiting, allow that process to
  7669. + handle any deletions */
  7670. + /*EXIT CRITICAL SECTION */
  7671. + spin_unlock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  7672. + return;
  7673. + }
  7674. +
  7675. + atomic_dec(&num_ocf_to_drv_registered_sessions);
  7676. +
  7677. + list_del(&(sessionData->listNode));
  7678. +
  7679. + /*EXIT CRITICAL SECTION */
  7680. + spin_unlock_bh(&icp_ocfDrvSymSessInfoListSpinlock);
  7681. +
  7682. + if (NULL != sessionData->sessHandle) {
  7683. + kfree(sessionData->sessHandle);
  7684. + }
  7685. + kmem_cache_free(drvSessionData_zone, sessionData);
  7686. +}
  7687. +
  7688. +/* Name : icp_ocfDrvFreeLACSession
  7689. + *
  7690. + * Description : This attempts to deregister a LAC session. If it fails, the
  7691. + * deregistation retry function is called.
  7692. + */
  7693. +int icp_ocfDrvFreeLACSession(device_t dev, uint64_t sid)
  7694. +{
  7695. + CpaCySymSessionCtx sessionToDeregister = NULL;
  7696. + struct icp_drvSessionData *sessionData = NULL;
  7697. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  7698. + int retval = 0;
  7699. +
  7700. + sessionData = (struct icp_drvSessionData *)CRYPTO_SESID2LID(sid);
  7701. + if (NULL == sessionData) {
  7702. + EPRINTK("%s(): OCF Free session called with Null Session ID.\n",
  7703. + __FUNCTION__);
  7704. + return EINVAL;
  7705. + }
  7706. +
  7707. + sessionToDeregister = sessionData->sessHandle;
  7708. +
  7709. + if (ICP_SESSION_INITIALISED == sessionData->inUse) {
  7710. + DPRINTK("%s() Session not registered with LAC\n", __FUNCTION__);
  7711. + } else if (NULL == sessionData->sessHandle) {
  7712. + EPRINTK
  7713. + ("%s(): OCF Free session called with Null Session Handle.\n",
  7714. + __FUNCTION__);
  7715. + return EINVAL;
  7716. + } else {
  7717. + lacStatus = cpaCySymRemoveSession(CPA_INSTANCE_HANDLE_SINGLE,
  7718. + sessionToDeregister);
  7719. + if (CPA_STATUS_RETRY == lacStatus) {
  7720. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  7721. + icp_ocfDrvDeregRetry(&sessionToDeregister)) {
  7722. + /* the retry function increments the
  7723. + dereg failed count */
  7724. + DPRINTK("%s(): LAC failed to deregister the "
  7725. + "session. (localSessionId= %p)\n",
  7726. + __FUNCTION__, sessionToDeregister);
  7727. + retval = EPERM;
  7728. + }
  7729. +
  7730. + } else if (CPA_STATUS_SUCCESS != lacStatus) {
  7731. + DPRINTK("%s(): LAC failed to deregister the session. "
  7732. + "localSessionId= %p, lacStatus = %d\n",
  7733. + __FUNCTION__, sessionToDeregister, lacStatus);
  7734. + atomic_inc(&lac_session_failed_dereg_count);
  7735. + retval = EPERM;
  7736. + }
  7737. + }
  7738. +
  7739. + icp_ocfDrvFreeOCFSession(sessionData);
  7740. + return retval;
  7741. +
  7742. +}
  7743. +
  7744. +/* Name : icp_ocfDrvAlgCheck
  7745. + *
  7746. + * Description : This function checks whether the cryptodesc argument pertains
  7747. + * to a sym or hash function
  7748. + */
  7749. +static int icp_ocfDrvAlgCheck(struct cryptodesc *crp_desc)
  7750. +{
  7751. +
  7752. + if (crp_desc->crd_alg == CRYPTO_3DES_CBC ||
  7753. + crp_desc->crd_alg == CRYPTO_AES_CBC ||
  7754. + crp_desc->crd_alg == CRYPTO_DES_CBC ||
  7755. + crp_desc->crd_alg == CRYPTO_NULL_CBC ||
  7756. + crp_desc->crd_alg == CRYPTO_ARC4) {
  7757. + return ICP_OCF_DRV_ALG_CIPHER;
  7758. + }
  7759. +
  7760. + return ICP_OCF_DRV_ALG_HASH;
  7761. +}
  7762. +
  7763. +/* Name : icp_ocfDrvSymProcess
  7764. + *
  7765. + * Description : This function will map symmetric functionality calls from OCF
  7766. + * to the LAC API. It will also allocate memory to store the session context.
  7767. + *
  7768. + * Notes: If it is the first perform call for a given session, then a LAC
  7769. + * session is registered. After the session is registered, no checks as
  7770. + * to whether session paramaters have changed (e.g. alg chain order) are
  7771. + * done.
  7772. + */
  7773. +int icp_ocfDrvSymProcess(device_t dev, struct cryptop *crp, int hint)
  7774. +{
  7775. + struct icp_drvSessionData *sessionData = NULL;
  7776. + struct icp_drvOpData *drvOpData = NULL;
  7777. + CpaStatus lacStatus = CPA_STATUS_SUCCESS;
  7778. + Cpa32U sessionCtxSizeInBytes = 0;
  7779. + uint16_t numBufferListArray = 0;
  7780. +
  7781. + if (NULL == crp) {
  7782. + DPRINTK("%s(): Invalid input parameters, cryptop is NULL\n",
  7783. + __FUNCTION__);
  7784. + return EINVAL;
  7785. + }
  7786. +
  7787. + if (NULL == crp->crp_desc) {
  7788. + DPRINTK("%s(): Invalid input parameters, no crp_desc attached "
  7789. + "to crp\n", __FUNCTION__);
  7790. + crp->crp_etype = EINVAL;
  7791. + return EINVAL;
  7792. + }
  7793. +
  7794. + if (NULL == crp->crp_buf) {
  7795. + DPRINTK("%s(): Invalid input parameters, no buffer attached "
  7796. + "to crp\n", __FUNCTION__);
  7797. + crp->crp_etype = EINVAL;
  7798. + return EINVAL;
  7799. + }
  7800. +
  7801. + if (CPA_TRUE == atomic_read(&icp_ocfDrvIsExiting)) {
  7802. + crp->crp_etype = EFAULT;
  7803. + return EFAULT;
  7804. + }
  7805. +
  7806. + sessionData = (struct icp_drvSessionData *)
  7807. + (CRYPTO_SESID2LID(crp->crp_sid));
  7808. + if (NULL == sessionData) {
  7809. + DPRINTK("%s(): Invalid input parameters, Null Session ID \n",
  7810. + __FUNCTION__);
  7811. + crp->crp_etype = EINVAL;
  7812. + return EINVAL;
  7813. + }
  7814. +
  7815. +/*If we get a request against a deregisted session, cancel operation*/
  7816. + if (ICP_SESSION_DEREGISTERED == sessionData->inUse) {
  7817. + DPRINTK("%s(): Session ID %d was deregistered \n",
  7818. + __FUNCTION__, (int)(CRYPTO_SESID2LID(crp->crp_sid)));
  7819. + crp->crp_etype = EFAULT;
  7820. + return EFAULT;
  7821. + }
  7822. +
  7823. +/*If none of the session states are set, then the session structure was either
  7824. + not initialised properly or we are reading from a freed memory area (possible
  7825. + due to OCF batch mode not removing queued requests against deregistered
  7826. + sessions*/
  7827. + if (ICP_SESSION_INITIALISED != sessionData->inUse &&
  7828. + ICP_SESSION_RUNNING != sessionData->inUse) {
  7829. + DPRINTK("%s(): Session - ID %d - not properly initialised or "
  7830. + "memory freed back to the kernel \n",
  7831. + __FUNCTION__, (int)(CRYPTO_SESID2LID(crp->crp_sid)));
  7832. + crp->crp_etype = EINVAL;
  7833. + return EINVAL;
  7834. + }
  7835. +
  7836. + /*For the below checks, remember error checking is already done in LAC.
  7837. + We're not validating inputs subsequent to registration */
  7838. + if (sessionData->inUse == ICP_SESSION_INITIALISED) {
  7839. + DPRINTK("%s(): Initialising session\n", __FUNCTION__);
  7840. +
  7841. + if (NULL != crp->crp_desc->crd_next) {
  7842. + if (ICP_OCF_DRV_ALG_CIPHER ==
  7843. + icp_ocfDrvAlgCheck(crp->crp_desc)) {
  7844. +
  7845. + sessionData->lacSessCtx.algChainOrder =
  7846. + CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
  7847. +
  7848. + if (crp->crp_desc->crd_flags & CRD_F_ENCRYPT) {
  7849. + sessionData->lacSessCtx.cipherSetupData.
  7850. + cipherDirection =
  7851. + CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
  7852. + } else {
  7853. + sessionData->lacSessCtx.cipherSetupData.
  7854. + cipherDirection =
  7855. + CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
  7856. + }
  7857. + } else {
  7858. + sessionData->lacSessCtx.algChainOrder =
  7859. + CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
  7860. +
  7861. + if (crp->crp_desc->crd_next->crd_flags &
  7862. + CRD_F_ENCRYPT) {
  7863. + sessionData->lacSessCtx.cipherSetupData.
  7864. + cipherDirection =
  7865. + CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
  7866. + } else {
  7867. + sessionData->lacSessCtx.cipherSetupData.
  7868. + cipherDirection =
  7869. + CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
  7870. + }
  7871. +
  7872. + }
  7873. +
  7874. + } else if (ICP_OCF_DRV_ALG_CIPHER ==
  7875. + icp_ocfDrvAlgCheck(crp->crp_desc)) {
  7876. + if (crp->crp_desc->crd_flags & CRD_F_ENCRYPT) {
  7877. + sessionData->lacSessCtx.cipherSetupData.
  7878. + cipherDirection =
  7879. + CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
  7880. + } else {
  7881. + sessionData->lacSessCtx.cipherSetupData.
  7882. + cipherDirection =
  7883. + CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
  7884. + }
  7885. +
  7886. + }
  7887. +
  7888. + /*No action required for standalone Auth here */
  7889. +
  7890. + /* Allocate memory for SymSessionCtx before the Session Registration */
  7891. + lacStatus =
  7892. + cpaCySymSessionCtxGetSize(CPA_INSTANCE_HANDLE_SINGLE,
  7893. + &(sessionData->lacSessCtx),
  7894. + &sessionCtxSizeInBytes);
  7895. + if (CPA_STATUS_SUCCESS != lacStatus) {
  7896. + EPRINTK("%s(): cpaCySymSessionCtxGetSize failed - %d\n",
  7897. + __FUNCTION__, lacStatus);
  7898. + return EINVAL;
  7899. + }
  7900. + sessionData->sessHandle =
  7901. + kmalloc(sessionCtxSizeInBytes, GFP_ATOMIC);
  7902. + if (NULL == sessionData->sessHandle) {
  7903. + EPRINTK
  7904. + ("%s(): Failed to get memory for SymSessionCtx\n",
  7905. + __FUNCTION__);
  7906. + return ENOMEM;
  7907. + }
  7908. +
  7909. + lacStatus = cpaCySymInitSession(CPA_INSTANCE_HANDLE_SINGLE,
  7910. + icp_ocfDrvSymCallBack,
  7911. + &(sessionData->lacSessCtx),
  7912. + sessionData->sessHandle);
  7913. +
  7914. + if (CPA_STATUS_SUCCESS != lacStatus) {
  7915. + EPRINTK("%s(): cpaCySymInitSession failed -%d \n",
  7916. + __FUNCTION__, lacStatus);
  7917. + return EFAULT;
  7918. + }
  7919. +
  7920. + sessionData->inUse = ICP_SESSION_RUNNING;
  7921. + }
  7922. +
  7923. + drvOpData = kmem_cache_zalloc(drvOpData_zone, GFP_ATOMIC);
  7924. + if (NULL == drvOpData) {
  7925. + EPRINTK("%s():Failed to get memory for drvOpData\n",
  7926. + __FUNCTION__);
  7927. + crp->crp_etype = ENOMEM;
  7928. + return ENOMEM;
  7929. + }
  7930. +
  7931. + drvOpData->lacOpData.pSessionCtx = sessionData->sessHandle;
  7932. + drvOpData->digestSizeInBytes = sessionData->lacSessCtx.hashSetupData.
  7933. + digestResultLenInBytes;
  7934. + drvOpData->crp = crp;
  7935. +
  7936. + /* Set the default buffer list array memory allocation */
  7937. + drvOpData->srcBuffer.pBuffers = drvOpData->bufferListArray;
  7938. + drvOpData->numBufferListArray = ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS;
  7939. +
  7940. + /*
  7941. + * Allocate buffer list array memory allocation if the
  7942. + * data fragment is more than the default allocation
  7943. + */
  7944. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  7945. + numBufferListArray = icp_ocfDrvGetSkBuffFrags((struct sk_buff *)
  7946. + crp->crp_buf);
  7947. + if (ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS < numBufferListArray) {
  7948. + DPRINTK("%s() numBufferListArray more than default\n",
  7949. + __FUNCTION__);
  7950. + drvOpData->srcBuffer.pBuffers = NULL;
  7951. + drvOpData->srcBuffer.pBuffers =
  7952. + kmalloc(numBufferListArray *
  7953. + sizeof(CpaFlatBuffer), GFP_ATOMIC);
  7954. + if (NULL == drvOpData->srcBuffer.pBuffers) {
  7955. + EPRINTK("%s() Failed to get memory for "
  7956. + "pBuffers\n", __FUNCTION__);
  7957. + kmem_cache_free(drvOpData_zone, drvOpData);
  7958. + crp->crp_etype = ENOMEM;
  7959. + return ENOMEM;
  7960. + }
  7961. + drvOpData->numBufferListArray = numBufferListArray;
  7962. + }
  7963. + }
  7964. +
  7965. + /*
  7966. + * Check the type of buffer structure we got and convert it into
  7967. + * CpaBufferList format.
  7968. + */
  7969. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  7970. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  7971. + icp_ocfDrvSkBuffToBufferList((struct sk_buff *)crp->crp_buf,
  7972. + &(drvOpData->srcBuffer))) {
  7973. + EPRINTK("%s():Failed to translate from SK_BUF "
  7974. + "to bufferlist\n", __FUNCTION__);
  7975. + crp->crp_etype = EINVAL;
  7976. + goto err;
  7977. + }
  7978. +
  7979. + drvOpData->bufferType = CRYPTO_F_SKBUF;
  7980. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  7981. + /* OCF only supports IOV of one entry. */
  7982. + if (NUM_IOV_SUPPORTED ==
  7983. + ((struct uio *)(crp->crp_buf))->uio_iovcnt) {
  7984. +
  7985. + icp_ocfDrvPtrAndLenToBufferList(((struct uio *)(crp->
  7986. + crp_buf))->
  7987. + uio_iov[0].iov_base,
  7988. + ((struct uio *)(crp->
  7989. + crp_buf))->
  7990. + uio_iov[0].iov_len,
  7991. + &(drvOpData->
  7992. + srcBuffer));
  7993. +
  7994. + drvOpData->bufferType = CRYPTO_F_IOV;
  7995. +
  7996. + } else {
  7997. + DPRINTK("%s():Unable to handle IOVs with lengths of "
  7998. + "greater than one!\n", __FUNCTION__);
  7999. + crp->crp_etype = EINVAL;
  8000. + goto err;
  8001. + }
  8002. +
  8003. + } else {
  8004. + icp_ocfDrvPtrAndLenToBufferList(crp->crp_buf,
  8005. + crp->crp_ilen,
  8006. + &(drvOpData->srcBuffer));
  8007. +
  8008. + drvOpData->bufferType = CRYPTO_BUF_CONTIG;
  8009. + }
  8010. +
  8011. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  8012. + icp_ocfDrvProcessDataSetup(drvOpData, drvOpData->crp->crp_desc)) {
  8013. + crp->crp_etype = EINVAL;
  8014. + goto err;
  8015. + }
  8016. +
  8017. + if (drvOpData->crp->crp_desc->crd_next != NULL) {
  8018. + if (icp_ocfDrvProcessDataSetup(drvOpData, drvOpData->crp->
  8019. + crp_desc->crd_next)) {
  8020. + crp->crp_etype = EINVAL;
  8021. + goto err;
  8022. + }
  8023. +
  8024. + }
  8025. +
  8026. + /* Allocate srcBuffer's private meta data */
  8027. + if (ICP_OCF_DRV_STATUS_SUCCESS !=
  8028. + icp_ocfDrvAllocMetaData(&(drvOpData->srcBuffer), drvOpData)) {
  8029. + EPRINTK("%s() icp_ocfDrvAllocMetaData failed\n", __FUNCTION__);
  8030. + memset(&(drvOpData->lacOpData), 0, sizeof(CpaCySymOpData));
  8031. + crp->crp_etype = EINVAL;
  8032. + goto err;
  8033. + }
  8034. +
  8035. + /* Perform "in-place" crypto operation */
  8036. + lacStatus = cpaCySymPerformOp(CPA_INSTANCE_HANDLE_SINGLE,
  8037. + (void *)drvOpData,
  8038. + &(drvOpData->lacOpData),
  8039. + &(drvOpData->srcBuffer),
  8040. + &(drvOpData->srcBuffer),
  8041. + &(drvOpData->verifyResult));
  8042. + if (CPA_STATUS_RETRY == lacStatus) {
  8043. + DPRINTK("%s(): cpaCySymPerformOp retry, lacStatus = %d\n",
  8044. + __FUNCTION__, lacStatus);
  8045. + memset(&(drvOpData->lacOpData), 0, sizeof(CpaCySymOpData));
  8046. + crp->crp_etype = EINVAL;
  8047. + goto err;
  8048. + }
  8049. + if (CPA_STATUS_SUCCESS != lacStatus) {
  8050. + EPRINTK("%s(): cpaCySymPerformOp failed, lacStatus = %d\n",
  8051. + __FUNCTION__, lacStatus);
  8052. + memset(&(drvOpData->lacOpData), 0, sizeof(CpaCySymOpData));
  8053. + crp->crp_etype = EINVAL;
  8054. + goto err;
  8055. + }
  8056. +
  8057. + return 0; //OCF success status value
  8058. +
  8059. + err:
  8060. + if (drvOpData->numBufferListArray > ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS) {
  8061. + kfree(drvOpData->srcBuffer.pBuffers);
  8062. + }
  8063. + icp_ocfDrvFreeMetaData(&(drvOpData->srcBuffer));
  8064. + kmem_cache_free(drvOpData_zone, drvOpData);
  8065. +
  8066. + return crp->crp_etype;
  8067. +}
  8068. +
  8069. +/* Name : icp_ocfDrvProcessDataSetup
  8070. + *
  8071. + * Description : This function will setup all the cryptographic operation data
  8072. + * that is required by LAC to execute the operation.
  8073. + */
  8074. +static int icp_ocfDrvProcessDataSetup(struct icp_drvOpData *drvOpData,
  8075. + struct cryptodesc *crp_desc)
  8076. +{
  8077. + CpaCyRandGenOpData randGenOpData;
  8078. + CpaFlatBuffer randData;
  8079. +
  8080. + drvOpData->lacOpData.packetType = CPA_CY_SYM_PACKET_TYPE_FULL;
  8081. +
  8082. + /* Convert from the cryptop to the ICP LAC crypto parameters */
  8083. + switch (crp_desc->crd_alg) {
  8084. + case CRYPTO_NULL_CBC:
  8085. + drvOpData->lacOpData.
  8086. + cryptoStartSrcOffsetInBytes = crp_desc->crd_skip;
  8087. + drvOpData->lacOpData.
  8088. + messageLenToCipherInBytes = crp_desc->crd_len;
  8089. + drvOpData->verifyResult = CPA_FALSE;
  8090. + drvOpData->lacOpData.ivLenInBytes = NULL_BLOCK_LEN;
  8091. + break;
  8092. + case CRYPTO_DES_CBC:
  8093. + drvOpData->lacOpData.
  8094. + cryptoStartSrcOffsetInBytes = crp_desc->crd_skip;
  8095. + drvOpData->lacOpData.
  8096. + messageLenToCipherInBytes = crp_desc->crd_len;
  8097. + drvOpData->verifyResult = CPA_FALSE;
  8098. + drvOpData->lacOpData.ivLenInBytes = DES_BLOCK_LEN;
  8099. + break;
  8100. + case CRYPTO_3DES_CBC:
  8101. + drvOpData->lacOpData.
  8102. + cryptoStartSrcOffsetInBytes = crp_desc->crd_skip;
  8103. + drvOpData->lacOpData.
  8104. + messageLenToCipherInBytes = crp_desc->crd_len;
  8105. + drvOpData->verifyResult = CPA_FALSE;
  8106. + drvOpData->lacOpData.ivLenInBytes = DES3_BLOCK_LEN;
  8107. + break;
  8108. + case CRYPTO_ARC4:
  8109. + drvOpData->lacOpData.
  8110. + cryptoStartSrcOffsetInBytes = crp_desc->crd_skip;
  8111. + drvOpData->lacOpData.
  8112. + messageLenToCipherInBytes = crp_desc->crd_len;
  8113. + drvOpData->verifyResult = CPA_FALSE;
  8114. + drvOpData->lacOpData.ivLenInBytes = ARC4_COUNTER_LEN;
  8115. + break;
  8116. + case CRYPTO_AES_CBC:
  8117. + drvOpData->lacOpData.
  8118. + cryptoStartSrcOffsetInBytes = crp_desc->crd_skip;
  8119. + drvOpData->lacOpData.
  8120. + messageLenToCipherInBytes = crp_desc->crd_len;
  8121. + drvOpData->verifyResult = CPA_FALSE;
  8122. + drvOpData->lacOpData.ivLenInBytes = RIJNDAEL128_BLOCK_LEN;
  8123. + break;
  8124. + case CRYPTO_SHA1:
  8125. + case CRYPTO_SHA1_HMAC:
  8126. + case CRYPTO_SHA2_256:
  8127. + case CRYPTO_SHA2_256_HMAC:
  8128. + case CRYPTO_SHA2_384:
  8129. + case CRYPTO_SHA2_384_HMAC:
  8130. + case CRYPTO_SHA2_512:
  8131. + case CRYPTO_SHA2_512_HMAC:
  8132. + case CRYPTO_MD5:
  8133. + case CRYPTO_MD5_HMAC:
  8134. + drvOpData->lacOpData.
  8135. + hashStartSrcOffsetInBytes = crp_desc->crd_skip;
  8136. + drvOpData->lacOpData.
  8137. + messageLenToHashInBytes = crp_desc->crd_len;
  8138. + drvOpData->lacOpData.
  8139. + pDigestResult =
  8140. + icp_ocfDrvDigestPointerFind(drvOpData, crp_desc);
  8141. +
  8142. + if (NULL == drvOpData->lacOpData.pDigestResult) {
  8143. + DPRINTK("%s(): ERROR - could not calculate "
  8144. + "Digest Result memory address\n", __FUNCTION__);
  8145. + return ICP_OCF_DRV_STATUS_FAIL;
  8146. + }
  8147. +
  8148. + drvOpData->lacOpData.digestVerify = CPA_FALSE;
  8149. + break;
  8150. + default:
  8151. + DPRINTK("%s(): Crypto process error - algorithm not "
  8152. + "found \n", __FUNCTION__);
  8153. + return ICP_OCF_DRV_STATUS_FAIL;
  8154. + }
  8155. +
  8156. + /* Figure out what the IV is supposed to be */
  8157. + if ((crp_desc->crd_alg == CRYPTO_DES_CBC) ||
  8158. + (crp_desc->crd_alg == CRYPTO_3DES_CBC) ||
  8159. + (crp_desc->crd_alg == CRYPTO_AES_CBC)) {
  8160. + /*ARC4 doesn't use an IV */
  8161. + if (crp_desc->crd_flags & CRD_F_IV_EXPLICIT) {
  8162. + /* Explicit IV provided to OCF */
  8163. + drvOpData->lacOpData.pIv = crp_desc->crd_iv;
  8164. + } else {
  8165. + /* IV is not explicitly provided to OCF */
  8166. +
  8167. + /* Point the LAC OP Data IV pointer to our allocated
  8168. + storage location for this session. */
  8169. + drvOpData->lacOpData.pIv = drvOpData->ivData;
  8170. +
  8171. + if ((crp_desc->crd_flags & CRD_F_ENCRYPT) &&
  8172. + ((crp_desc->crd_flags & CRD_F_IV_PRESENT) == 0)) {
  8173. +
  8174. + /* Encrypting - need to create IV */
  8175. + randGenOpData.generateBits = CPA_TRUE;
  8176. + randGenOpData.lenInBytes = MAX_IV_LEN_IN_BYTES;
  8177. +
  8178. + icp_ocfDrvPtrAndLenToFlatBuffer((Cpa8U *)
  8179. + drvOpData->
  8180. + ivData,
  8181. + MAX_IV_LEN_IN_BYTES,
  8182. + &randData);
  8183. +
  8184. + if (CPA_STATUS_SUCCESS !=
  8185. + cpaCyRandGen(CPA_INSTANCE_HANDLE_SINGLE,
  8186. + NULL, NULL,
  8187. + &randGenOpData, &randData)) {
  8188. + DPRINTK("%s(): ERROR - Failed to"
  8189. + " generate"
  8190. + " Initialisation Vector\n",
  8191. + __FUNCTION__);
  8192. + return ICP_OCF_DRV_STATUS_FAIL;
  8193. + }
  8194. +
  8195. + crypto_copyback(drvOpData->crp->
  8196. + crp_flags,
  8197. + drvOpData->crp->crp_buf,
  8198. + crp_desc->crd_inject,
  8199. + drvOpData->lacOpData.
  8200. + ivLenInBytes,
  8201. + (caddr_t) (drvOpData->lacOpData.
  8202. + pIv));
  8203. + } else {
  8204. + /* Reading IV from buffer */
  8205. + crypto_copydata(drvOpData->crp->
  8206. + crp_flags,
  8207. + drvOpData->crp->crp_buf,
  8208. + crp_desc->crd_inject,
  8209. + drvOpData->lacOpData.
  8210. + ivLenInBytes,
  8211. + (caddr_t) (drvOpData->lacOpData.
  8212. + pIv));
  8213. + }
  8214. +
  8215. + }
  8216. +
  8217. + }
  8218. +
  8219. + return ICP_OCF_DRV_STATUS_SUCCESS;
  8220. +}
  8221. +
  8222. +/* Name : icp_ocfDrvDigestPointerFind
  8223. + *
  8224. + * Description : This function is used to find the memory address of where the
  8225. + * digest information shall be stored in. Input buffer types are an skbuff, iov
  8226. + * or flat buffer. The address is found using the buffer data start address and
  8227. + * an offset.
  8228. + *
  8229. + * Note: In the case of a linux skbuff, the digest address may exist within
  8230. + * a memory space linked to from the start buffer. These linked memory spaces
  8231. + * must be traversed by the data length offset in order to find the digest start
  8232. + * address. Whether there is enough space for the digest must also be checked.
  8233. + */
  8234. +
  8235. +static uint8_t *icp_ocfDrvDigestPointerFind(struct icp_drvOpData *drvOpData,
  8236. + struct cryptodesc *crp_desc)
  8237. +{
  8238. +
  8239. + int offsetInBytes = crp_desc->crd_inject;
  8240. + uint32_t digestSizeInBytes = drvOpData->digestSizeInBytes;
  8241. + uint8_t *flat_buffer_base = NULL;
  8242. + int flat_buffer_length = 0;
  8243. + struct sk_buff *skb;
  8244. +
  8245. + if (drvOpData->crp->crp_flags & CRYPTO_F_SKBUF) {
  8246. + /*check if enough overall space to store hash */
  8247. + skb = (struct sk_buff *)(drvOpData->crp->crp_buf);
  8248. +
  8249. + if (skb->len < (offsetInBytes + digestSizeInBytes)) {
  8250. + DPRINTK("%s() Not enough space for Digest"
  8251. + " payload after the offset (%d), "
  8252. + "digest size (%d) \n", __FUNCTION__,
  8253. + offsetInBytes, digestSizeInBytes);
  8254. + return NULL;
  8255. + }
  8256. +
  8257. + return icp_ocfDrvSkbuffDigestPointerFind(drvOpData,
  8258. + offsetInBytes,
  8259. + digestSizeInBytes);
  8260. +
  8261. + } else {
  8262. + /* IOV or flat buffer */
  8263. + if (drvOpData->crp->crp_flags & CRYPTO_F_IOV) {
  8264. + /*single IOV check has already been done */
  8265. + flat_buffer_base = ((struct uio *)
  8266. + (drvOpData->crp->crp_buf))->
  8267. + uio_iov[0].iov_base;
  8268. + flat_buffer_length = ((struct uio *)
  8269. + (drvOpData->crp->crp_buf))->
  8270. + uio_iov[0].iov_len;
  8271. + } else {
  8272. + flat_buffer_base = (uint8_t *) drvOpData->crp->crp_buf;
  8273. + flat_buffer_length = drvOpData->crp->crp_ilen;
  8274. + }
  8275. +
  8276. + if (flat_buffer_length < (offsetInBytes + digestSizeInBytes)) {
  8277. + DPRINTK("%s() Not enough space for Digest "
  8278. + "(IOV/Flat Buffer) \n", __FUNCTION__);
  8279. + return NULL;
  8280. + } else {
  8281. + return (uint8_t *) (flat_buffer_base + offsetInBytes);
  8282. + }
  8283. + }
  8284. + DPRINTK("%s() Should not reach this point\n", __FUNCTION__);
  8285. + return NULL;
  8286. +}
  8287. +
  8288. +/* Name : icp_ocfDrvSkbuffDigestPointerFind
  8289. + *
  8290. + * Description : This function is used by icp_ocfDrvDigestPointerFind to process
  8291. + * the non-linear portion of the skbuff if the fragmentation type is a linked
  8292. + * list (frag_list is not NULL in the skb_shared_info structure)
  8293. + */
  8294. +static inline uint8_t *icp_ocfDrvSkbuffDigestPointerFind(struct icp_drvOpData
  8295. + *drvOpData,
  8296. + int offsetInBytes,
  8297. + uint32_t
  8298. + digestSizeInBytes)
  8299. +{
  8300. +
  8301. + struct sk_buff *skb = NULL;
  8302. + struct skb_shared_info *skb_shared = NULL;
  8303. +
  8304. + uint32_t skbuffisnonlinear = 0;
  8305. +
  8306. + uint32_t skbheadlen = 0;
  8307. +
  8308. + skb = (struct sk_buff *)(drvOpData->crp->crp_buf);
  8309. + skbuffisnonlinear = skb_is_nonlinear(skb);
  8310. +
  8311. + skbheadlen = skb_headlen(skb);
  8312. +
  8313. + /*Linear skb checks */
  8314. + if (skbheadlen > offsetInBytes) {
  8315. +
  8316. + if (skbheadlen >= (offsetInBytes + digestSizeInBytes)) {
  8317. + return (uint8_t *) (skb->data + offsetInBytes);
  8318. + } else {
  8319. + DPRINTK("%s() Auth payload stretches "
  8320. + "accross contiguous memory\n", __FUNCTION__);
  8321. + return NULL;
  8322. + }
  8323. + } else {
  8324. + if (skbuffisnonlinear) {
  8325. + offsetInBytes -= skbheadlen;
  8326. + } else {
  8327. + DPRINTK("%s() Offset outside of buffer boundaries\n",
  8328. + __FUNCTION__);
  8329. + return NULL;
  8330. + }
  8331. + }
  8332. +
  8333. + /*Non Linear checks */
  8334. + skb_shared = (struct skb_shared_info *)(skb->end);
  8335. + if (unlikely(NULL == skb_shared)) {
  8336. + DPRINTK("%s() skbuff shared info stucture is NULL! \n",
  8337. + __FUNCTION__);
  8338. + return NULL;
  8339. + } else if ((0 != skb_shared->nr_frags) &&
  8340. + (skb_shared->frag_list != NULL)) {
  8341. + DPRINTK("%s() skbuff nr_frags AND "
  8342. + "frag_list not supported \n", __FUNCTION__);
  8343. + return NULL;
  8344. + }
  8345. +
  8346. + /*TCP segmentation more likely than IP fragmentation */
  8347. + if (likely(0 != skb_shared->nr_frags)) {
  8348. + return icp_ocfDrvDigestSkbNRFragsCheck(skb, skb_shared,
  8349. + offsetInBytes,
  8350. + digestSizeInBytes);
  8351. + } else if (skb_shared->frag_list != NULL) {
  8352. + return icp_ocfDrvDigestSkbFragListCheck(skb, skb_shared,
  8353. + offsetInBytes,
  8354. + digestSizeInBytes);
  8355. + } else {
  8356. + DPRINTK("%s() skbuff is non-linear but does not show any "
  8357. + "linked data\n", __FUNCTION__);
  8358. + return NULL;
  8359. + }
  8360. +
  8361. +}
  8362. +
  8363. +/* Name : icp_ocfDrvDigestSkbNRFragsCheck
  8364. + *
  8365. + * Description : This function is used by icp_ocfDrvSkbuffDigestPointerFind to
  8366. + * process the non-linear portion of the skbuff, if the fragmentation type is
  8367. + * page fragments
  8368. + */
  8369. +static inline uint8_t *icp_ocfDrvDigestSkbNRFragsCheck(struct sk_buff *skb,
  8370. + struct skb_shared_info
  8371. + *skb_shared,
  8372. + int offsetInBytes,
  8373. + uint32_t
  8374. + digestSizeInBytes)
  8375. +{
  8376. + int i = 0;
  8377. + /*nr_frags starts from 1 */
  8378. + if (MAX_SKB_FRAGS < skb_shared->nr_frags) {
  8379. + DPRINTK("%s error processing skbuff "
  8380. + "page frame -- MAX FRAGS exceeded \n", __FUNCTION__);
  8381. + return NULL;
  8382. + }
  8383. +
  8384. + for (i = 0; i < skb_shared->nr_frags; i++) {
  8385. +
  8386. + if (offsetInBytes >= skb_shared->frags[i].size) {
  8387. + /*offset still greater than data position */
  8388. + offsetInBytes -= skb_shared->frags[i].size;
  8389. + } else {
  8390. + /* found the page containing start of hash */
  8391. +
  8392. + if (NULL == skb_shared->frags[i].page) {
  8393. + DPRINTK("%s() Linked page is NULL!\n",
  8394. + __FUNCTION__);
  8395. + return NULL;
  8396. + }
  8397. +
  8398. + if (offsetInBytes + digestSizeInBytes >
  8399. + skb_shared->frags[i].size) {
  8400. + DPRINTK("%s() Auth payload stretches accross "
  8401. + "contiguous memory\n", __FUNCTION__);
  8402. + return NULL;
  8403. + } else {
  8404. + return (uint8_t *) (skb_shared->frags[i].page +
  8405. + skb_shared->frags[i].
  8406. + page_offset +
  8407. + offsetInBytes);
  8408. + }
  8409. + }
  8410. + /*only possible if internal page sizes are set wrong */
  8411. + if (offsetInBytes < 0) {
  8412. + DPRINTK("%s error processing skbuff page frame "
  8413. + "-- offset calculation \n", __FUNCTION__);
  8414. + return NULL;
  8415. + }
  8416. + }
  8417. + /*only possible if internal page sizes are set wrong */
  8418. + DPRINTK("%s error processing skbuff page frame "
  8419. + "-- ran out of page fragments, remaining offset = %d \n",
  8420. + __FUNCTION__, offsetInBytes);
  8421. + return NULL;
  8422. +
  8423. +}
  8424. +
  8425. +/* Name : icp_ocfDrvDigestSkbFragListCheck
  8426. + *
  8427. + * Description : This function is used by icp_ocfDrvSkbuffDigestPointerFind to
  8428. + * process the non-linear portion of the skbuff, if the fragmentation type is
  8429. + * a linked list
  8430. + *
  8431. + */
  8432. +static inline uint8_t *icp_ocfDrvDigestSkbFragListCheck(struct sk_buff *skb,
  8433. + struct skb_shared_info
  8434. + *skb_shared,
  8435. + int offsetInBytes,
  8436. + uint32_t
  8437. + digestSizeInBytes)
  8438. +{
  8439. +
  8440. + struct sk_buff *skb_list = skb_shared->frag_list;
  8441. + /*check added for readability */
  8442. + if (NULL == skb_list) {
  8443. + DPRINTK("%s error processing skbuff "
  8444. + "-- no more list! \n", __FUNCTION__);
  8445. + return NULL;
  8446. + }
  8447. +
  8448. + for (; skb_list; skb_list = skb_list->next) {
  8449. + if (NULL == skb_list) {
  8450. + DPRINTK("%s error processing skbuff "
  8451. + "-- no more list! \n", __FUNCTION__);
  8452. + return NULL;
  8453. + }
  8454. +
  8455. + if (offsetInBytes >= skb_list->len) {
  8456. + offsetInBytes -= skb_list->len;
  8457. +
  8458. + } else {
  8459. + if (offsetInBytes + digestSizeInBytes > skb_list->len) {
  8460. + DPRINTK("%s() Auth payload stretches accross "
  8461. + "contiguous memory\n", __FUNCTION__);
  8462. + return NULL;
  8463. + } else {
  8464. + return (uint8_t *)
  8465. + (skb_list->data + offsetInBytes);
  8466. + }
  8467. +
  8468. + }
  8469. +
  8470. + /*This check is only needed if internal skb_list length values
  8471. + are set wrong. */
  8472. + if (0 > offsetInBytes) {
  8473. + DPRINTK("%s() error processing skbuff object -- offset "
  8474. + "calculation \n", __FUNCTION__);
  8475. + return NULL;
  8476. + }
  8477. +
  8478. + }
  8479. +
  8480. + /*catch all for unusual for-loop exit.
  8481. + This code should never be reached */
  8482. + DPRINTK("%s() Catch-All hit! Process error.\n", __FUNCTION__);
  8483. + return NULL;
  8484. +}
  8485. diff -Nur linux-2.6.30.orig/crypto/ocf/ep80579/Makefile linux-2.6.30/crypto/ocf/ep80579/Makefile
  8486. --- linux-2.6.30.orig/crypto/ocf/ep80579/Makefile 1970-01-01 01:00:00.000000000 +0100
  8487. +++ linux-2.6.30/crypto/ocf/ep80579/Makefile 2009-06-11 10:55:27.000000000 +0200
  8488. @@ -0,0 +1,107 @@
  8489. +#########################################################################
  8490. +#
  8491. +# Targets supported
  8492. +# all - builds everything and installs
  8493. +# install - identical to all
  8494. +# depend - build dependencies
  8495. +# clean - clears derived objects except the .depend files
  8496. +# distclean- clears all derived objects and the .depend file
  8497. +#
  8498. +# @par
  8499. +# This file is provided under a dual BSD/GPLv2 license. When using or
  8500. +# redistributing this file, you may do so under either license.
  8501. +#
  8502. +# GPL LICENSE SUMMARY
  8503. +#
  8504. +# Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  8505. +#
  8506. +# This program is free software; you can redistribute it and/or modify
  8507. +# it under the terms of version 2 of the GNU General Public License as
  8508. +# published by the Free Software Foundation.
  8509. +#
  8510. +# This program is distributed in the hope that it will be useful, but
  8511. +# WITHOUT ANY WARRANTY; without even the implied warranty of
  8512. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8513. +# General Public License for more details.
  8514. +#
  8515. +# You should have received a copy of the GNU General Public License
  8516. +# along with this program; if not, write to the Free Software
  8517. +# Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  8518. +# The full GNU General Public License is included in this distribution
  8519. +# in the file called LICENSE.GPL.
  8520. +#
  8521. +# Contact Information:
  8522. +# Intel Corporation
  8523. +#
  8524. +# BSD LICENSE
  8525. +#
  8526. +# Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
  8527. +# All rights reserved.
  8528. +#
  8529. +# Redistribution and use in source and binary forms, with or without
  8530. +# modification, are permitted provided that the following conditions
  8531. +# are met:
  8532. +#
  8533. +# * Redistributions of source code must retain the above copyright
  8534. +# notice, this list of conditions and the following disclaimer.
  8535. +# * Redistributions in binary form must reproduce the above copyright
  8536. +# notice, this list of conditions and the following disclaimer in
  8537. +# the documentation and/or other materials provided with the
  8538. +# distribution.
  8539. +# * Neither the name of Intel Corporation nor the names of its
  8540. +# contributors may be used to endorse or promote products derived
  8541. +# from this software without specific prior written permission.
  8542. +#
  8543. +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8544. +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8545. +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8546. +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8547. +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8548. +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8549. +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8550. +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8551. +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8552. +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  8553. +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8554. +#
  8555. +#
  8556. +# version: Security.L.1.0.130
  8557. +############################################################################
  8558. +
  8559. +
  8560. +####################Common variables and definitions########################
  8561. +
  8562. +# Ensure The ENV_DIR environmental var is defined.
  8563. +ifndef ICP_ENV_DIR
  8564. +$(error ICP_ENV_DIR is undefined. Please set the path to your environment makefile \
  8565. + "-> setenv ICP_ENV_DIR <path>")
  8566. +endif
  8567. +
  8568. +#Add your project environment Makefile
  8569. +include $(ICP_ENV_DIR)/environment.mk
  8570. +
  8571. +#include the makefile with all the default and common Make variable definitions
  8572. +include $(ICP_BUILDSYSTEM_PATH)/build_files/common.mk
  8573. +
  8574. +#Add the name for the executable, Library or Module output definitions
  8575. +OUTPUT_NAME= icp_ocf
  8576. +
  8577. +# List of Source Files to be compiled
  8578. +SOURCES= icp_common.c icp_sym.c icp_asym.c
  8579. +
  8580. +#common includes between all supported OSes
  8581. +INCLUDES= -I $(ICP_API_DIR) -I$(ICP_LAC_API) \
  8582. +-I$(ICP_OCF_SRC_DIR)
  8583. +
  8584. +# The location of the os level makefile needs to be changed.
  8585. +include $(ICP_ENV_DIR)/$(ICP_OS)_$(ICP_OS_LEVEL).mk
  8586. +
  8587. +# On the line directly below list the outputs you wish to build for,
  8588. +# e.g "lib_static lib_shared exe module" as show below
  8589. +install: module
  8590. +
  8591. +###################Include rules makefiles########################
  8592. +include $(ICP_BUILDSYSTEM_PATH)/build_files/rules.mk
  8593. +###################End of Rules inclusion#########################
  8594. +
  8595. +
  8596. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifn7751.c linux-2.6.30/crypto/ocf/hifn/hifn7751.c
  8597. --- linux-2.6.30.orig/crypto/ocf/hifn/hifn7751.c 1970-01-01 01:00:00.000000000 +0100
  8598. +++ linux-2.6.30/crypto/ocf/hifn/hifn7751.c 2009-06-11 10:55:27.000000000 +0200
  8599. @@ -0,0 +1,2970 @@
  8600. +/* $OpenBSD: hifn7751.c,v 1.120 2002/05/17 00:33:34 deraadt Exp $ */
  8601. +
  8602. +/*-
  8603. + * Invertex AEON / Hifn 7751 driver
  8604. + * Copyright (c) 1999 Invertex Inc. All rights reserved.
  8605. + * Copyright (c) 1999 Theo de Raadt
  8606. + * Copyright (c) 2000-2001 Network Security Technologies, Inc.
  8607. + * http://www.netsec.net
  8608. + * Copyright (c) 2003 Hifn Inc.
  8609. + *
  8610. + * This driver is based on a previous driver by Invertex, for which they
  8611. + * requested: Please send any comments, feedback, bug-fixes, or feature
  8612. + * requests to software@invertex.com.
  8613. + *
  8614. + * Redistribution and use in source and binary forms, with or without
  8615. + * modification, are permitted provided that the following conditions
  8616. + * are met:
  8617. + *
  8618. + * 1. Redistributions of source code must retain the above copyright
  8619. + * notice, this list of conditions and the following disclaimer.
  8620. + * 2. Redistributions in binary form must reproduce the above copyright
  8621. + * notice, this list of conditions and the following disclaimer in the
  8622. + * documentation and/or other materials provided with the distribution.
  8623. + * 3. The name of the author may not be used to endorse or promote products
  8624. + * derived from this software without specific prior written permission.
  8625. + *
  8626. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  8627. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  8628. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  8629. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  8630. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  8631. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8632. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8633. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8634. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  8635. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8636. + *
  8637. + * Effort sponsored in part by the Defense Advanced Research Projects
  8638. + * Agency (DARPA) and Air Force Research Laboratory, Air Force
  8639. + * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  8640. + *
  8641. + *
  8642. +__FBSDID("$FreeBSD: src/sys/dev/hifn/hifn7751.c,v 1.40 2007/03/21 03:42:49 sam Exp $");
  8643. + */
  8644. +
  8645. +/*
  8646. + * Driver for various Hifn encryption processors.
  8647. + */
  8648. +#ifndef AUTOCONF_INCLUDED
  8649. +#include <linux/config.h>
  8650. +#endif
  8651. +#include <linux/module.h>
  8652. +#include <linux/init.h>
  8653. +#include <linux/list.h>
  8654. +#include <linux/slab.h>
  8655. +#include <linux/wait.h>
  8656. +#include <linux/sched.h>
  8657. +#include <linux/pci.h>
  8658. +#include <linux/delay.h>
  8659. +#include <linux/interrupt.h>
  8660. +#include <linux/spinlock.h>
  8661. +#include <linux/random.h>
  8662. +#include <linux/version.h>
  8663. +#include <linux/skbuff.h>
  8664. +#include <asm/io.h>
  8665. +
  8666. +#include <cryptodev.h>
  8667. +#include <uio.h>
  8668. +#include <hifn/hifn7751reg.h>
  8669. +#include <hifn/hifn7751var.h>
  8670. +
  8671. +#if 1
  8672. +#define DPRINTF(a...) if (hifn_debug) { \
  8673. + printk("%s: ", sc ? \
  8674. + device_get_nameunit(sc->sc_dev) : "hifn"); \
  8675. + printk(a); \
  8676. + } else
  8677. +#else
  8678. +#define DPRINTF(a...)
  8679. +#endif
  8680. +
  8681. +static inline int
  8682. +pci_get_revid(struct pci_dev *dev)
  8683. +{
  8684. + u8 rid = 0;
  8685. + pci_read_config_byte(dev, PCI_REVISION_ID, &rid);
  8686. + return rid;
  8687. +}
  8688. +
  8689. +static struct hifn_stats hifnstats;
  8690. +
  8691. +#define debug hifn_debug
  8692. +int hifn_debug = 0;
  8693. +module_param(hifn_debug, int, 0644);
  8694. +MODULE_PARM_DESC(hifn_debug, "Enable debug");
  8695. +
  8696. +int hifn_maxbatch = 1;
  8697. +module_param(hifn_maxbatch, int, 0644);
  8698. +MODULE_PARM_DESC(hifn_maxbatch, "max ops to batch w/o interrupt");
  8699. +
  8700. +#ifdef MODULE_PARM
  8701. +char *hifn_pllconfig = NULL;
  8702. +MODULE_PARM(hifn_pllconfig, "s");
  8703. +#else
  8704. +char hifn_pllconfig[32]; /* This setting is RO after loading */
  8705. +module_param_string(hifn_pllconfig, hifn_pllconfig, 32, 0444);
  8706. +#endif
  8707. +MODULE_PARM_DESC(hifn_pllconfig, "PLL config, ie., pci66, ext33, ...");
  8708. +
  8709. +#ifdef HIFN_VULCANDEV
  8710. +#include <sys/conf.h>
  8711. +#include <sys/uio.h>
  8712. +
  8713. +static struct cdevsw vulcanpk_cdevsw; /* forward declaration */
  8714. +#endif
  8715. +
  8716. +/*
  8717. + * Prototypes and count for the pci_device structure
  8718. + */
  8719. +static int hifn_probe(struct pci_dev *dev, const struct pci_device_id *ent);
  8720. +static void hifn_remove(struct pci_dev *dev);
  8721. +
  8722. +static int hifn_newsession(device_t, u_int32_t *, struct cryptoini *);
  8723. +static int hifn_freesession(device_t, u_int64_t);
  8724. +static int hifn_process(device_t, struct cryptop *, int);
  8725. +
  8726. +static device_method_t hifn_methods = {
  8727. + /* crypto device methods */
  8728. + DEVMETHOD(cryptodev_newsession, hifn_newsession),
  8729. + DEVMETHOD(cryptodev_freesession,hifn_freesession),
  8730. + DEVMETHOD(cryptodev_process, hifn_process),
  8731. +};
  8732. +
  8733. +static void hifn_reset_board(struct hifn_softc *, int);
  8734. +static void hifn_reset_puc(struct hifn_softc *);
  8735. +static void hifn_puc_wait(struct hifn_softc *);
  8736. +static int hifn_enable_crypto(struct hifn_softc *);
  8737. +static void hifn_set_retry(struct hifn_softc *sc);
  8738. +static void hifn_init_dma(struct hifn_softc *);
  8739. +static void hifn_init_pci_registers(struct hifn_softc *);
  8740. +static int hifn_sramsize(struct hifn_softc *);
  8741. +static int hifn_dramsize(struct hifn_softc *);
  8742. +static int hifn_ramtype(struct hifn_softc *);
  8743. +static void hifn_sessions(struct hifn_softc *);
  8744. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  8745. +static irqreturn_t hifn_intr(int irq, void *arg);
  8746. +#else
  8747. +static irqreturn_t hifn_intr(int irq, void *arg, struct pt_regs *regs);
  8748. +#endif
  8749. +static u_int hifn_write_command(struct hifn_command *, u_int8_t *);
  8750. +static u_int32_t hifn_next_signature(u_int32_t a, u_int cnt);
  8751. +static void hifn_callback(struct hifn_softc *, struct hifn_command *, u_int8_t *);
  8752. +static int hifn_crypto(struct hifn_softc *, struct hifn_command *, struct cryptop *, int);
  8753. +static int hifn_readramaddr(struct hifn_softc *, int, u_int8_t *);
  8754. +static int hifn_writeramaddr(struct hifn_softc *, int, u_int8_t *);
  8755. +static int hifn_dmamap_load_src(struct hifn_softc *, struct hifn_command *);
  8756. +static int hifn_dmamap_load_dst(struct hifn_softc *, struct hifn_command *);
  8757. +static int hifn_init_pubrng(struct hifn_softc *);
  8758. +static void hifn_tick(unsigned long arg);
  8759. +static void hifn_abort(struct hifn_softc *);
  8760. +static void hifn_alloc_slot(struct hifn_softc *, int *, int *, int *, int *);
  8761. +
  8762. +static void hifn_write_reg_0(struct hifn_softc *, bus_size_t, u_int32_t);
  8763. +static void hifn_write_reg_1(struct hifn_softc *, bus_size_t, u_int32_t);
  8764. +
  8765. +#ifdef CONFIG_OCF_RANDOMHARVEST
  8766. +static int hifn_read_random(void *arg, u_int32_t *buf, int len);
  8767. +#endif
  8768. +
  8769. +#define HIFN_MAX_CHIPS 8
  8770. +static struct hifn_softc *hifn_chip_idx[HIFN_MAX_CHIPS];
  8771. +
  8772. +static __inline u_int32_t
  8773. +READ_REG_0(struct hifn_softc *sc, bus_size_t reg)
  8774. +{
  8775. + u_int32_t v = readl(sc->sc_bar0 + reg);
  8776. + sc->sc_bar0_lastreg = (bus_size_t) -1;
  8777. + return (v);
  8778. +}
  8779. +#define WRITE_REG_0(sc, reg, val) hifn_write_reg_0(sc, reg, val)
  8780. +
  8781. +static __inline u_int32_t
  8782. +READ_REG_1(struct hifn_softc *sc, bus_size_t reg)
  8783. +{
  8784. + u_int32_t v = readl(sc->sc_bar1 + reg);
  8785. + sc->sc_bar1_lastreg = (bus_size_t) -1;
  8786. + return (v);
  8787. +}
  8788. +#define WRITE_REG_1(sc, reg, val) hifn_write_reg_1(sc, reg, val)
  8789. +
  8790. +/*
  8791. + * map in a given buffer (great on some arches :-)
  8792. + */
  8793. +
  8794. +static int
  8795. +pci_map_uio(struct hifn_softc *sc, struct hifn_operand *buf, struct uio *uio)
  8796. +{
  8797. + struct iovec *iov = uio->uio_iov;
  8798. +
  8799. + DPRINTF("%s()\n", __FUNCTION__);
  8800. +
  8801. + buf->mapsize = 0;
  8802. + for (buf->nsegs = 0; buf->nsegs < uio->uio_iovcnt; ) {
  8803. + buf->segs[buf->nsegs].ds_addr = pci_map_single(sc->sc_pcidev,
  8804. + iov->iov_base, iov->iov_len,
  8805. + PCI_DMA_BIDIRECTIONAL);
  8806. + buf->segs[buf->nsegs].ds_len = iov->iov_len;
  8807. + buf->mapsize += iov->iov_len;
  8808. + iov++;
  8809. + buf->nsegs++;
  8810. + }
  8811. + /* identify this buffer by the first segment */
  8812. + buf->map = (void *) buf->segs[0].ds_addr;
  8813. + return(0);
  8814. +}
  8815. +
  8816. +/*
  8817. + * map in a given sk_buff
  8818. + */
  8819. +
  8820. +static int
  8821. +pci_map_skb(struct hifn_softc *sc,struct hifn_operand *buf,struct sk_buff *skb)
  8822. +{
  8823. + int i;
  8824. +
  8825. + DPRINTF("%s()\n", __FUNCTION__);
  8826. +
  8827. + buf->mapsize = 0;
  8828. +
  8829. + buf->segs[0].ds_addr = pci_map_single(sc->sc_pcidev,
  8830. + skb->data, skb_headlen(skb), PCI_DMA_BIDIRECTIONAL);
  8831. + buf->segs[0].ds_len = skb_headlen(skb);
  8832. + buf->mapsize += buf->segs[0].ds_len;
  8833. +
  8834. + buf->nsegs = 1;
  8835. +
  8836. + for (i = 0; i < skb_shinfo(skb)->nr_frags; ) {
  8837. + buf->segs[buf->nsegs].ds_len = skb_shinfo(skb)->frags[i].size;
  8838. + buf->segs[buf->nsegs].ds_addr = pci_map_single(sc->sc_pcidev,
  8839. + page_address(skb_shinfo(skb)->frags[i].page) +
  8840. + skb_shinfo(skb)->frags[i].page_offset,
  8841. + buf->segs[buf->nsegs].ds_len, PCI_DMA_BIDIRECTIONAL);
  8842. + buf->mapsize += buf->segs[buf->nsegs].ds_len;
  8843. + buf->nsegs++;
  8844. + }
  8845. +
  8846. + /* identify this buffer by the first segment */
  8847. + buf->map = (void *) buf->segs[0].ds_addr;
  8848. + return(0);
  8849. +}
  8850. +
  8851. +/*
  8852. + * map in a given contiguous buffer
  8853. + */
  8854. +
  8855. +static int
  8856. +pci_map_buf(struct hifn_softc *sc,struct hifn_operand *buf, void *b, int len)
  8857. +{
  8858. + DPRINTF("%s()\n", __FUNCTION__);
  8859. +
  8860. + buf->mapsize = 0;
  8861. + buf->segs[0].ds_addr = pci_map_single(sc->sc_pcidev,
  8862. + b, len, PCI_DMA_BIDIRECTIONAL);
  8863. + buf->segs[0].ds_len = len;
  8864. + buf->mapsize += buf->segs[0].ds_len;
  8865. + buf->nsegs = 1;
  8866. +
  8867. + /* identify this buffer by the first segment */
  8868. + buf->map = (void *) buf->segs[0].ds_addr;
  8869. + return(0);
  8870. +}
  8871. +
  8872. +#if 0 /* not needed at this time */
  8873. +static void
  8874. +pci_sync_iov(struct hifn_softc *sc, struct hifn_operand *buf)
  8875. +{
  8876. + int i;
  8877. +
  8878. + DPRINTF("%s()\n", __FUNCTION__);
  8879. + for (i = 0; i < buf->nsegs; i++)
  8880. + pci_dma_sync_single_for_cpu(sc->sc_pcidev, buf->segs[i].ds_addr,
  8881. + buf->segs[i].ds_len, PCI_DMA_BIDIRECTIONAL);
  8882. +}
  8883. +#endif
  8884. +
  8885. +static void
  8886. +pci_unmap_buf(struct hifn_softc *sc, struct hifn_operand *buf)
  8887. +{
  8888. + int i;
  8889. + DPRINTF("%s()\n", __FUNCTION__);
  8890. + for (i = 0; i < buf->nsegs; i++) {
  8891. + pci_unmap_single(sc->sc_pcidev, buf->segs[i].ds_addr,
  8892. + buf->segs[i].ds_len, PCI_DMA_BIDIRECTIONAL);
  8893. + buf->segs[i].ds_addr = 0;
  8894. + buf->segs[i].ds_len = 0;
  8895. + }
  8896. + buf->nsegs = 0;
  8897. + buf->mapsize = 0;
  8898. + buf->map = 0;
  8899. +}
  8900. +
  8901. +static const char*
  8902. +hifn_partname(struct hifn_softc *sc)
  8903. +{
  8904. + /* XXX sprintf numbers when not decoded */
  8905. + switch (pci_get_vendor(sc->sc_pcidev)) {
  8906. + case PCI_VENDOR_HIFN:
  8907. + switch (pci_get_device(sc->sc_pcidev)) {
  8908. + case PCI_PRODUCT_HIFN_6500: return "Hifn 6500";
  8909. + case PCI_PRODUCT_HIFN_7751: return "Hifn 7751";
  8910. + case PCI_PRODUCT_HIFN_7811: return "Hifn 7811";
  8911. + case PCI_PRODUCT_HIFN_7951: return "Hifn 7951";
  8912. + case PCI_PRODUCT_HIFN_7955: return "Hifn 7955";
  8913. + case PCI_PRODUCT_HIFN_7956: return "Hifn 7956";
  8914. + }
  8915. + return "Hifn unknown-part";
  8916. + case PCI_VENDOR_INVERTEX:
  8917. + switch (pci_get_device(sc->sc_pcidev)) {
  8918. + case PCI_PRODUCT_INVERTEX_AEON: return "Invertex AEON";
  8919. + }
  8920. + return "Invertex unknown-part";
  8921. + case PCI_VENDOR_NETSEC:
  8922. + switch (pci_get_device(sc->sc_pcidev)) {
  8923. + case PCI_PRODUCT_NETSEC_7751: return "NetSec 7751";
  8924. + }
  8925. + return "NetSec unknown-part";
  8926. + }
  8927. + return "Unknown-vendor unknown-part";
  8928. +}
  8929. +
  8930. +static u_int
  8931. +checkmaxmin(struct pci_dev *dev, const char *what, u_int v, u_int min, u_int max)
  8932. +{
  8933. + struct hifn_softc *sc = pci_get_drvdata(dev);
  8934. + if (v > max) {
  8935. + device_printf(sc->sc_dev, "Warning, %s %u out of range, "
  8936. + "using max %u\n", what, v, max);
  8937. + v = max;
  8938. + } else if (v < min) {
  8939. + device_printf(sc->sc_dev, "Warning, %s %u out of range, "
  8940. + "using min %u\n", what, v, min);
  8941. + v = min;
  8942. + }
  8943. + return v;
  8944. +}
  8945. +
  8946. +/*
  8947. + * Select PLL configuration for 795x parts. This is complicated in
  8948. + * that we cannot determine the optimal parameters without user input.
  8949. + * The reference clock is derived from an external clock through a
  8950. + * multiplier. The external clock is either the host bus (i.e. PCI)
  8951. + * or an external clock generator. When using the PCI bus we assume
  8952. + * the clock is either 33 or 66 MHz; for an external source we cannot
  8953. + * tell the speed.
  8954. + *
  8955. + * PLL configuration is done with a string: "pci" for PCI bus, or "ext"
  8956. + * for an external source, followed by the frequency. We calculate
  8957. + * the appropriate multiplier and PLL register contents accordingly.
  8958. + * When no configuration is given we default to "pci66" since that
  8959. + * always will allow the card to work. If a card is using the PCI
  8960. + * bus clock and in a 33MHz slot then it will be operating at half
  8961. + * speed until the correct information is provided.
  8962. + *
  8963. + * We use a default setting of "ext66" because according to Mike Ham
  8964. + * of HiFn, almost every board in existence has an external crystal
  8965. + * populated at 66Mhz. Using PCI can be a problem on modern motherboards,
  8966. + * because PCI33 can have clocks from 0 to 33Mhz, and some have
  8967. + * non-PCI-compliant spread-spectrum clocks, which can confuse the pll.
  8968. + */
  8969. +static void
  8970. +hifn_getpllconfig(struct pci_dev *dev, u_int *pll)
  8971. +{
  8972. + const char *pllspec = hifn_pllconfig;
  8973. + u_int freq, mul, fl, fh;
  8974. + u_int32_t pllconfig;
  8975. + char *nxt;
  8976. +
  8977. + if (pllspec == NULL)
  8978. + pllspec = "ext66";
  8979. + fl = 33, fh = 66;
  8980. + pllconfig = 0;
  8981. + if (strncmp(pllspec, "ext", 3) == 0) {
  8982. + pllspec += 3;
  8983. + pllconfig |= HIFN_PLL_REF_SEL;
  8984. + switch (pci_get_device(dev)) {
  8985. + case PCI_PRODUCT_HIFN_7955:
  8986. + case PCI_PRODUCT_HIFN_7956:
  8987. + fl = 20, fh = 100;
  8988. + break;
  8989. +#ifdef notyet
  8990. + case PCI_PRODUCT_HIFN_7954:
  8991. + fl = 20, fh = 66;
  8992. + break;
  8993. +#endif
  8994. + }
  8995. + } else if (strncmp(pllspec, "pci", 3) == 0)
  8996. + pllspec += 3;
  8997. + freq = strtoul(pllspec, &nxt, 10);
  8998. + if (nxt == pllspec)
  8999. + freq = 66;
  9000. + else
  9001. + freq = checkmaxmin(dev, "frequency", freq, fl, fh);
  9002. + /*
  9003. + * Calculate multiplier. We target a Fck of 266 MHz,
  9004. + * allowing only even values, possibly rounded down.
  9005. + * Multipliers > 8 must set the charge pump current.
  9006. + */
  9007. + mul = checkmaxmin(dev, "PLL divisor", (266 / freq) &~ 1, 2, 12);
  9008. + pllconfig |= (mul / 2 - 1) << HIFN_PLL_ND_SHIFT;
  9009. + if (mul > 8)
  9010. + pllconfig |= HIFN_PLL_IS;
  9011. + *pll = pllconfig;
  9012. +}
  9013. +
  9014. +/*
  9015. + * Attach an interface that successfully probed.
  9016. + */
  9017. +static int
  9018. +hifn_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  9019. +{
  9020. + struct hifn_softc *sc = NULL;
  9021. + char rbase;
  9022. + u_int16_t ena, rev;
  9023. + int rseg, rc;
  9024. + unsigned long mem_start, mem_len;
  9025. + static int num_chips = 0;
  9026. +
  9027. + DPRINTF("%s()\n", __FUNCTION__);
  9028. +
  9029. + if (pci_enable_device(dev) < 0)
  9030. + return(-ENODEV);
  9031. +
  9032. + if (pci_set_mwi(dev))
  9033. + return(-ENODEV);
  9034. +
  9035. + if (!dev->irq) {
  9036. + printk("hifn: found device with no IRQ assigned. check BIOS settings!");
  9037. + pci_disable_device(dev);
  9038. + return(-ENODEV);
  9039. + }
  9040. +
  9041. + sc = (struct hifn_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  9042. + if (!sc)
  9043. + return(-ENOMEM);
  9044. + memset(sc, 0, sizeof(*sc));
  9045. +
  9046. + softc_device_init(sc, "hifn", num_chips, hifn_methods);
  9047. +
  9048. + sc->sc_pcidev = dev;
  9049. + sc->sc_irq = -1;
  9050. + sc->sc_cid = -1;
  9051. + sc->sc_num = num_chips++;
  9052. + if (sc->sc_num < HIFN_MAX_CHIPS)
  9053. + hifn_chip_idx[sc->sc_num] = sc;
  9054. +
  9055. + pci_set_drvdata(sc->sc_pcidev, sc);
  9056. +
  9057. + spin_lock_init(&sc->sc_mtx);
  9058. +
  9059. + /* XXX handle power management */
  9060. +
  9061. + /*
  9062. + * The 7951 and 795x have a random number generator and
  9063. + * public key support; note this.
  9064. + */
  9065. + if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  9066. + (pci_get_device(dev) == PCI_PRODUCT_HIFN_7951 ||
  9067. + pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
  9068. + pci_get_device(dev) == PCI_PRODUCT_HIFN_7956))
  9069. + sc->sc_flags = HIFN_HAS_RNG | HIFN_HAS_PUBLIC;
  9070. + /*
  9071. + * The 7811 has a random number generator and
  9072. + * we also note it's identity 'cuz of some quirks.
  9073. + */
  9074. + if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  9075. + pci_get_device(dev) == PCI_PRODUCT_HIFN_7811)
  9076. + sc->sc_flags |= HIFN_IS_7811 | HIFN_HAS_RNG;
  9077. +
  9078. + /*
  9079. + * The 795x parts support AES.
  9080. + */
  9081. + if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  9082. + (pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
  9083. + pci_get_device(dev) == PCI_PRODUCT_HIFN_7956)) {
  9084. + sc->sc_flags |= HIFN_IS_7956 | HIFN_HAS_AES;
  9085. + /*
  9086. + * Select PLL configuration. This depends on the
  9087. + * bus and board design and must be manually configured
  9088. + * if the default setting is unacceptable.
  9089. + */
  9090. + hifn_getpllconfig(dev, &sc->sc_pllconfig);
  9091. + }
  9092. +
  9093. + /*
  9094. + * Setup PCI resources. Note that we record the bus
  9095. + * tag and handle for each register mapping, this is
  9096. + * used by the READ_REG_0, WRITE_REG_0, READ_REG_1,
  9097. + * and WRITE_REG_1 macros throughout the driver.
  9098. + */
  9099. + mem_start = pci_resource_start(sc->sc_pcidev, 0);
  9100. + mem_len = pci_resource_len(sc->sc_pcidev, 0);
  9101. + sc->sc_bar0 = (ocf_iomem_t) ioremap(mem_start, mem_len);
  9102. + if (!sc->sc_bar0) {
  9103. + device_printf(sc->sc_dev, "cannot map bar%d register space\n", 0);
  9104. + goto fail;
  9105. + }
  9106. + sc->sc_bar0_lastreg = (bus_size_t) -1;
  9107. +
  9108. + mem_start = pci_resource_start(sc->sc_pcidev, 1);
  9109. + mem_len = pci_resource_len(sc->sc_pcidev, 1);
  9110. + sc->sc_bar1 = (ocf_iomem_t) ioremap(mem_start, mem_len);
  9111. + if (!sc->sc_bar1) {
  9112. + device_printf(sc->sc_dev, "cannot map bar%d register space\n", 1);
  9113. + goto fail;
  9114. + }
  9115. + sc->sc_bar1_lastreg = (bus_size_t) -1;
  9116. +
  9117. + /* fix up the bus size */
  9118. + if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
  9119. + device_printf(sc->sc_dev, "No usable DMA configuration, aborting.\n");
  9120. + goto fail;
  9121. + }
  9122. + if (pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK)) {
  9123. + device_printf(sc->sc_dev,
  9124. + "No usable consistent DMA configuration, aborting.\n");
  9125. + goto fail;
  9126. + }
  9127. +
  9128. + hifn_set_retry(sc);
  9129. +
  9130. + /*
  9131. + * Setup the area where the Hifn DMA's descriptors
  9132. + * and associated data structures.
  9133. + */
  9134. + sc->sc_dma = (struct hifn_dma *) pci_alloc_consistent(dev,
  9135. + sizeof(*sc->sc_dma),
  9136. + &sc->sc_dma_physaddr);
  9137. + if (!sc->sc_dma) {
  9138. + device_printf(sc->sc_dev, "cannot alloc sc_dma\n");
  9139. + goto fail;
  9140. + }
  9141. + bzero(sc->sc_dma, sizeof(*sc->sc_dma));
  9142. +
  9143. + /*
  9144. + * Reset the board and do the ``secret handshake''
  9145. + * to enable the crypto support. Then complete the
  9146. + * initialization procedure by setting up the interrupt
  9147. + * and hooking in to the system crypto support so we'll
  9148. + * get used for system services like the crypto device,
  9149. + * IPsec, RNG device, etc.
  9150. + */
  9151. + hifn_reset_board(sc, 0);
  9152. +
  9153. + if (hifn_enable_crypto(sc) != 0) {
  9154. + device_printf(sc->sc_dev, "crypto enabling failed\n");
  9155. + goto fail;
  9156. + }
  9157. + hifn_reset_puc(sc);
  9158. +
  9159. + hifn_init_dma(sc);
  9160. + hifn_init_pci_registers(sc);
  9161. +
  9162. + pci_set_master(sc->sc_pcidev);
  9163. +
  9164. + /* XXX can't dynamically determine ram type for 795x; force dram */
  9165. + if (sc->sc_flags & HIFN_IS_7956)
  9166. + sc->sc_drammodel = 1;
  9167. + else if (hifn_ramtype(sc))
  9168. + goto fail;
  9169. +
  9170. + if (sc->sc_drammodel == 0)
  9171. + hifn_sramsize(sc);
  9172. + else
  9173. + hifn_dramsize(sc);
  9174. +
  9175. + /*
  9176. + * Workaround for NetSec 7751 rev A: half ram size because two
  9177. + * of the address lines were left floating
  9178. + */
  9179. + if (pci_get_vendor(dev) == PCI_VENDOR_NETSEC &&
  9180. + pci_get_device(dev) == PCI_PRODUCT_NETSEC_7751 &&
  9181. + pci_get_revid(dev) == 0x61) /*XXX???*/
  9182. + sc->sc_ramsize >>= 1;
  9183. +
  9184. + /*
  9185. + * Arrange the interrupt line.
  9186. + */
  9187. + rc = request_irq(dev->irq, hifn_intr, IRQF_SHARED, "hifn", sc);
  9188. + if (rc) {
  9189. + device_printf(sc->sc_dev, "could not map interrupt: %d\n", rc);
  9190. + goto fail;
  9191. + }
  9192. + sc->sc_irq = dev->irq;
  9193. +
  9194. + hifn_sessions(sc);
  9195. +
  9196. + /*
  9197. + * NB: Keep only the low 16 bits; this masks the chip id
  9198. + * from the 7951.
  9199. + */
  9200. + rev = READ_REG_1(sc, HIFN_1_REVID) & 0xffff;
  9201. +
  9202. + rseg = sc->sc_ramsize / 1024;
  9203. + rbase = 'K';
  9204. + if (sc->sc_ramsize >= (1024 * 1024)) {
  9205. + rbase = 'M';
  9206. + rseg /= 1024;
  9207. + }
  9208. + device_printf(sc->sc_dev, "%s, rev %u, %d%cB %cram",
  9209. + hifn_partname(sc), rev,
  9210. + rseg, rbase, sc->sc_drammodel ? 'd' : 's');
  9211. + if (sc->sc_flags & HIFN_IS_7956)
  9212. + printf(", pll=0x%x<%s clk, %ux mult>",
  9213. + sc->sc_pllconfig,
  9214. + sc->sc_pllconfig & HIFN_PLL_REF_SEL ? "ext" : "pci",
  9215. + 2 + 2*((sc->sc_pllconfig & HIFN_PLL_ND) >> 11));
  9216. + printf("\n");
  9217. +
  9218. + sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
  9219. + if (sc->sc_cid < 0) {
  9220. + device_printf(sc->sc_dev, "could not get crypto driver id\n");
  9221. + goto fail;
  9222. + }
  9223. +
  9224. + WRITE_REG_0(sc, HIFN_0_PUCNFG,
  9225. + READ_REG_0(sc, HIFN_0_PUCNFG) | HIFN_PUCNFG_CHIPID);
  9226. + ena = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  9227. +
  9228. + switch (ena) {
  9229. + case HIFN_PUSTAT_ENA_2:
  9230. + crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  9231. + crypto_register(sc->sc_cid, CRYPTO_ARC4, 0, 0);
  9232. + if (sc->sc_flags & HIFN_HAS_AES)
  9233. + crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  9234. + /*FALLTHROUGH*/
  9235. + case HIFN_PUSTAT_ENA_1:
  9236. + crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
  9237. + crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
  9238. + crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  9239. + crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  9240. + crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  9241. + break;
  9242. + }
  9243. +
  9244. + if (sc->sc_flags & (HIFN_HAS_PUBLIC | HIFN_HAS_RNG))
  9245. + hifn_init_pubrng(sc);
  9246. +
  9247. + init_timer(&sc->sc_tickto);
  9248. + sc->sc_tickto.function = hifn_tick;
  9249. + sc->sc_tickto.data = (unsigned long) sc->sc_num;
  9250. + mod_timer(&sc->sc_tickto, jiffies + HZ);
  9251. +
  9252. + return (0);
  9253. +
  9254. +fail:
  9255. + if (sc->sc_cid >= 0)
  9256. + crypto_unregister_all(sc->sc_cid);
  9257. + if (sc->sc_irq != -1)
  9258. + free_irq(sc->sc_irq, sc);
  9259. + if (sc->sc_dma) {
  9260. + /* Turn off DMA polling */
  9261. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  9262. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  9263. +
  9264. + pci_free_consistent(sc->sc_pcidev,
  9265. + sizeof(*sc->sc_dma),
  9266. + sc->sc_dma, sc->sc_dma_physaddr);
  9267. + }
  9268. + kfree(sc);
  9269. + return (-ENXIO);
  9270. +}
  9271. +
  9272. +/*
  9273. + * Detach an interface that successfully probed.
  9274. + */
  9275. +static void
  9276. +hifn_remove(struct pci_dev *dev)
  9277. +{
  9278. + struct hifn_softc *sc = pci_get_drvdata(dev);
  9279. + unsigned long l_flags;
  9280. +
  9281. + DPRINTF("%s()\n", __FUNCTION__);
  9282. +
  9283. + KASSERT(sc != NULL, ("hifn_detach: null software carrier!"));
  9284. +
  9285. + /* disable interrupts */
  9286. + HIFN_LOCK(sc);
  9287. + WRITE_REG_1(sc, HIFN_1_DMA_IER, 0);
  9288. + HIFN_UNLOCK(sc);
  9289. +
  9290. + /*XXX other resources */
  9291. + del_timer_sync(&sc->sc_tickto);
  9292. +
  9293. + /* Turn off DMA polling */
  9294. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  9295. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  9296. +
  9297. + crypto_unregister_all(sc->sc_cid);
  9298. +
  9299. + free_irq(sc->sc_irq, sc);
  9300. +
  9301. + pci_free_consistent(sc->sc_pcidev, sizeof(*sc->sc_dma),
  9302. + sc->sc_dma, sc->sc_dma_physaddr);
  9303. +}
  9304. +
  9305. +
  9306. +static int
  9307. +hifn_init_pubrng(struct hifn_softc *sc)
  9308. +{
  9309. + int i;
  9310. +
  9311. + DPRINTF("%s()\n", __FUNCTION__);
  9312. +
  9313. + if ((sc->sc_flags & HIFN_IS_7811) == 0) {
  9314. + /* Reset 7951 public key/rng engine */
  9315. + WRITE_REG_1(sc, HIFN_1_PUB_RESET,
  9316. + READ_REG_1(sc, HIFN_1_PUB_RESET) | HIFN_PUBRST_RESET);
  9317. +
  9318. + for (i = 0; i < 100; i++) {
  9319. + DELAY(1000);
  9320. + if ((READ_REG_1(sc, HIFN_1_PUB_RESET) &
  9321. + HIFN_PUBRST_RESET) == 0)
  9322. + break;
  9323. + }
  9324. +
  9325. + if (i == 100) {
  9326. + device_printf(sc->sc_dev, "public key init failed\n");
  9327. + return (1);
  9328. + }
  9329. + }
  9330. +
  9331. + /* Enable the rng, if available */
  9332. +#ifdef CONFIG_OCF_RANDOMHARVEST
  9333. + if (sc->sc_flags & HIFN_HAS_RNG) {
  9334. + if (sc->sc_flags & HIFN_IS_7811) {
  9335. + u_int32_t r;
  9336. + r = READ_REG_1(sc, HIFN_1_7811_RNGENA);
  9337. + if (r & HIFN_7811_RNGENA_ENA) {
  9338. + r &= ~HIFN_7811_RNGENA_ENA;
  9339. + WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
  9340. + }
  9341. + WRITE_REG_1(sc, HIFN_1_7811_RNGCFG,
  9342. + HIFN_7811_RNGCFG_DEFL);
  9343. + r |= HIFN_7811_RNGENA_ENA;
  9344. + WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
  9345. + } else
  9346. + WRITE_REG_1(sc, HIFN_1_RNG_CONFIG,
  9347. + READ_REG_1(sc, HIFN_1_RNG_CONFIG) |
  9348. + HIFN_RNGCFG_ENA);
  9349. +
  9350. + sc->sc_rngfirst = 1;
  9351. + crypto_rregister(sc->sc_cid, hifn_read_random, sc);
  9352. + }
  9353. +#endif
  9354. +
  9355. + /* Enable public key engine, if available */
  9356. + if (sc->sc_flags & HIFN_HAS_PUBLIC) {
  9357. + WRITE_REG_1(sc, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
  9358. + sc->sc_dmaier |= HIFN_DMAIER_PUBDONE;
  9359. + WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  9360. +#ifdef HIFN_VULCANDEV
  9361. + sc->sc_pkdev = make_dev(&vulcanpk_cdevsw, 0,
  9362. + UID_ROOT, GID_WHEEL, 0666,
  9363. + "vulcanpk");
  9364. + sc->sc_pkdev->si_drv1 = sc;
  9365. +#endif
  9366. + }
  9367. +
  9368. + return (0);
  9369. +}
  9370. +
  9371. +#ifdef CONFIG_OCF_RANDOMHARVEST
  9372. +static int
  9373. +hifn_read_random(void *arg, u_int32_t *buf, int len)
  9374. +{
  9375. + struct hifn_softc *sc = (struct hifn_softc *) arg;
  9376. + u_int32_t sts;
  9377. + int i, rc = 0;
  9378. +
  9379. + if (len <= 0)
  9380. + return rc;
  9381. +
  9382. + if (sc->sc_flags & HIFN_IS_7811) {
  9383. + /* ONLY VALID ON 7811!!!! */
  9384. + for (i = 0; i < 5; i++) {
  9385. + sts = READ_REG_1(sc, HIFN_1_7811_RNGSTS);
  9386. + if (sts & HIFN_7811_RNGSTS_UFL) {
  9387. + device_printf(sc->sc_dev,
  9388. + "RNG underflow: disabling\n");
  9389. + /* DAVIDM perhaps return -1 */
  9390. + break;
  9391. + }
  9392. + if ((sts & HIFN_7811_RNGSTS_RDY) == 0)
  9393. + break;
  9394. +
  9395. + /*
  9396. + * There are at least two words in the RNG FIFO
  9397. + * at this point.
  9398. + */
  9399. + if (rc < len)
  9400. + buf[rc++] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
  9401. + if (rc < len)
  9402. + buf[rc++] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
  9403. + }
  9404. + } else
  9405. + buf[rc++] = READ_REG_1(sc, HIFN_1_RNG_DATA);
  9406. +
  9407. + /* NB: discard first data read */
  9408. + if (sc->sc_rngfirst) {
  9409. + sc->sc_rngfirst = 0;
  9410. + rc = 0;
  9411. + }
  9412. +
  9413. + return(rc);
  9414. +}
  9415. +#endif /* CONFIG_OCF_RANDOMHARVEST */
  9416. +
  9417. +static void
  9418. +hifn_puc_wait(struct hifn_softc *sc)
  9419. +{
  9420. + int i;
  9421. + int reg = HIFN_0_PUCTRL;
  9422. +
  9423. + if (sc->sc_flags & HIFN_IS_7956) {
  9424. + reg = HIFN_0_PUCTRL2;
  9425. + }
  9426. +
  9427. + for (i = 5000; i > 0; i--) {
  9428. + DELAY(1);
  9429. + if (!(READ_REG_0(sc, reg) & HIFN_PUCTRL_RESET))
  9430. + break;
  9431. + }
  9432. + if (!i)
  9433. + device_printf(sc->sc_dev, "proc unit did not reset(0x%x)\n",
  9434. + READ_REG_0(sc, HIFN_0_PUCTRL));
  9435. +}
  9436. +
  9437. +/*
  9438. + * Reset the processing unit.
  9439. + */
  9440. +static void
  9441. +hifn_reset_puc(struct hifn_softc *sc)
  9442. +{
  9443. + /* Reset processing unit */
  9444. + int reg = HIFN_0_PUCTRL;
  9445. +
  9446. + if (sc->sc_flags & HIFN_IS_7956) {
  9447. + reg = HIFN_0_PUCTRL2;
  9448. + }
  9449. + WRITE_REG_0(sc, reg, HIFN_PUCTRL_DMAENA);
  9450. +
  9451. + hifn_puc_wait(sc);
  9452. +}
  9453. +
  9454. +/*
  9455. + * Set the Retry and TRDY registers; note that we set them to
  9456. + * zero because the 7811 locks up when forced to retry (section
  9457. + * 3.6 of "Specification Update SU-0014-04". Not clear if we
  9458. + * should do this for all Hifn parts, but it doesn't seem to hurt.
  9459. + */
  9460. +static void
  9461. +hifn_set_retry(struct hifn_softc *sc)
  9462. +{
  9463. + DPRINTF("%s()\n", __FUNCTION__);
  9464. + /* NB: RETRY only responds to 8-bit reads/writes */
  9465. + pci_write_config_byte(sc->sc_pcidev, HIFN_RETRY_TIMEOUT, 0);
  9466. + pci_write_config_dword(sc->sc_pcidev, HIFN_TRDY_TIMEOUT, 0);
  9467. +}
  9468. +
  9469. +/*
  9470. + * Resets the board. Values in the regesters are left as is
  9471. + * from the reset (i.e. initial values are assigned elsewhere).
  9472. + */
  9473. +static void
  9474. +hifn_reset_board(struct hifn_softc *sc, int full)
  9475. +{
  9476. + u_int32_t reg;
  9477. +
  9478. + DPRINTF("%s()\n", __FUNCTION__);
  9479. + /*
  9480. + * Set polling in the DMA configuration register to zero. 0x7 avoids
  9481. + * resetting the board and zeros out the other fields.
  9482. + */
  9483. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  9484. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  9485. +
  9486. + /*
  9487. + * Now that polling has been disabled, we have to wait 1 ms
  9488. + * before resetting the board.
  9489. + */
  9490. + DELAY(1000);
  9491. +
  9492. + /* Reset the DMA unit */
  9493. + if (full) {
  9494. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
  9495. + DELAY(1000);
  9496. + } else {
  9497. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG,
  9498. + HIFN_DMACNFG_MODE | HIFN_DMACNFG_MSTRESET);
  9499. + hifn_reset_puc(sc);
  9500. + }
  9501. +
  9502. + KASSERT(sc->sc_dma != NULL, ("hifn_reset_board: null DMA tag!"));
  9503. + bzero(sc->sc_dma, sizeof(*sc->sc_dma));
  9504. +
  9505. + /* Bring dma unit out of reset */
  9506. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  9507. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  9508. +
  9509. + hifn_puc_wait(sc);
  9510. + hifn_set_retry(sc);
  9511. +
  9512. + if (sc->sc_flags & HIFN_IS_7811) {
  9513. + for (reg = 0; reg < 1000; reg++) {
  9514. + if (READ_REG_1(sc, HIFN_1_7811_MIPSRST) &
  9515. + HIFN_MIPSRST_CRAMINIT)
  9516. + break;
  9517. + DELAY(1000);
  9518. + }
  9519. + if (reg == 1000)
  9520. + device_printf(sc->sc_dev, ": cram init timeout\n");
  9521. + } else {
  9522. + /* set up DMA configuration register #2 */
  9523. + /* turn off all PK and BAR0 swaps */
  9524. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG2,
  9525. + (3 << HIFN_DMACNFG2_INIT_WRITE_BURST_SHIFT)|
  9526. + (3 << HIFN_DMACNFG2_INIT_READ_BURST_SHIFT)|
  9527. + (2 << HIFN_DMACNFG2_TGT_WRITE_BURST_SHIFT)|
  9528. + (2 << HIFN_DMACNFG2_TGT_READ_BURST_SHIFT));
  9529. + }
  9530. +}
  9531. +
  9532. +static u_int32_t
  9533. +hifn_next_signature(u_int32_t a, u_int cnt)
  9534. +{
  9535. + int i;
  9536. + u_int32_t v;
  9537. +
  9538. + for (i = 0; i < cnt; i++) {
  9539. +
  9540. + /* get the parity */
  9541. + v = a & 0x80080125;
  9542. + v ^= v >> 16;
  9543. + v ^= v >> 8;
  9544. + v ^= v >> 4;
  9545. + v ^= v >> 2;
  9546. + v ^= v >> 1;
  9547. +
  9548. + a = (v & 1) ^ (a << 1);
  9549. + }
  9550. +
  9551. + return a;
  9552. +}
  9553. +
  9554. +
  9555. +/*
  9556. + * Checks to see if crypto is already enabled. If crypto isn't enable,
  9557. + * "hifn_enable_crypto" is called to enable it. The check is important,
  9558. + * as enabling crypto twice will lock the board.
  9559. + */
  9560. +static int
  9561. +hifn_enable_crypto(struct hifn_softc *sc)
  9562. +{
  9563. + u_int32_t dmacfg, ramcfg, encl, addr, i;
  9564. + char offtbl[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9565. + 0x00, 0x00, 0x00, 0x00 };
  9566. +
  9567. + DPRINTF("%s()\n", __FUNCTION__);
  9568. +
  9569. + ramcfg = READ_REG_0(sc, HIFN_0_PUCNFG);
  9570. + dmacfg = READ_REG_1(sc, HIFN_1_DMA_CNFG);
  9571. +
  9572. + /*
  9573. + * The RAM config register's encrypt level bit needs to be set before
  9574. + * every read performed on the encryption level register.
  9575. + */
  9576. + WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
  9577. +
  9578. + encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  9579. +
  9580. + /*
  9581. + * Make sure we don't re-unlock. Two unlocks kills chip until the
  9582. + * next reboot.
  9583. + */
  9584. + if (encl == HIFN_PUSTAT_ENA_1 || encl == HIFN_PUSTAT_ENA_2) {
  9585. +#ifdef HIFN_DEBUG
  9586. + if (hifn_debug)
  9587. + device_printf(sc->sc_dev,
  9588. + "Strong crypto already enabled!\n");
  9589. +#endif
  9590. + goto report;
  9591. + }
  9592. +
  9593. + if (encl != 0 && encl != HIFN_PUSTAT_ENA_0) {
  9594. +#ifdef HIFN_DEBUG
  9595. + if (hifn_debug)
  9596. + device_printf(sc->sc_dev,
  9597. + "Unknown encryption level 0x%x\n", encl);
  9598. +#endif
  9599. + return 1;
  9600. + }
  9601. +
  9602. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_UNLOCK |
  9603. + HIFN_DMACNFG_MSTRESET | HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  9604. + DELAY(1000);
  9605. + addr = READ_REG_1(sc, HIFN_UNLOCK_SECRET1);
  9606. + DELAY(1000);
  9607. + WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, 0);
  9608. + DELAY(1000);
  9609. +
  9610. + for (i = 0; i <= 12; i++) {
  9611. + addr = hifn_next_signature(addr, offtbl[i] + 0x101);
  9612. + WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, addr);
  9613. +
  9614. + DELAY(1000);
  9615. + }
  9616. +
  9617. + WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
  9618. + encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  9619. +
  9620. +#ifdef HIFN_DEBUG
  9621. + if (hifn_debug) {
  9622. + if (encl != HIFN_PUSTAT_ENA_1 && encl != HIFN_PUSTAT_ENA_2)
  9623. + device_printf(sc->sc_dev, "Engine is permanently "
  9624. + "locked until next system reset!\n");
  9625. + else
  9626. + device_printf(sc->sc_dev, "Engine enabled "
  9627. + "successfully!\n");
  9628. + }
  9629. +#endif
  9630. +
  9631. +report:
  9632. + WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
  9633. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
  9634. +
  9635. + switch (encl) {
  9636. + case HIFN_PUSTAT_ENA_1:
  9637. + case HIFN_PUSTAT_ENA_2:
  9638. + break;
  9639. + case HIFN_PUSTAT_ENA_0:
  9640. + default:
  9641. + device_printf(sc->sc_dev, "disabled\n");
  9642. + break;
  9643. + }
  9644. +
  9645. + return 0;
  9646. +}
  9647. +
  9648. +/*
  9649. + * Give initial values to the registers listed in the "Register Space"
  9650. + * section of the HIFN Software Development reference manual.
  9651. + */
  9652. +static void
  9653. +hifn_init_pci_registers(struct hifn_softc *sc)
  9654. +{
  9655. + DPRINTF("%s()\n", __FUNCTION__);
  9656. +
  9657. + /* write fixed values needed by the Initialization registers */
  9658. + WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  9659. + WRITE_REG_0(sc, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
  9660. + WRITE_REG_0(sc, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  9661. +
  9662. + /* write all 4 ring address registers */
  9663. + WRITE_REG_1(sc, HIFN_1_DMA_CRAR, sc->sc_dma_physaddr +
  9664. + offsetof(struct hifn_dma, cmdr[0]));
  9665. + WRITE_REG_1(sc, HIFN_1_DMA_SRAR, sc->sc_dma_physaddr +
  9666. + offsetof(struct hifn_dma, srcr[0]));
  9667. + WRITE_REG_1(sc, HIFN_1_DMA_DRAR, sc->sc_dma_physaddr +
  9668. + offsetof(struct hifn_dma, dstr[0]));
  9669. + WRITE_REG_1(sc, HIFN_1_DMA_RRAR, sc->sc_dma_physaddr +
  9670. + offsetof(struct hifn_dma, resr[0]));
  9671. +
  9672. + DELAY(2000);
  9673. +
  9674. + /* write status register */
  9675. + WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  9676. + HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  9677. + HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
  9678. + HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  9679. + HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  9680. + HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  9681. + HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  9682. + HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  9683. + HIFN_DMACSR_S_WAIT |
  9684. + HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  9685. + HIFN_DMACSR_C_WAIT |
  9686. + HIFN_DMACSR_ENGINE |
  9687. + ((sc->sc_flags & HIFN_HAS_PUBLIC) ?
  9688. + HIFN_DMACSR_PUBDONE : 0) |
  9689. + ((sc->sc_flags & HIFN_IS_7811) ?
  9690. + HIFN_DMACSR_ILLW | HIFN_DMACSR_ILLR : 0));
  9691. +
  9692. + sc->sc_d_busy = sc->sc_r_busy = sc->sc_s_busy = sc->sc_c_busy = 0;
  9693. + sc->sc_dmaier |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
  9694. + HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
  9695. + HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
  9696. + ((sc->sc_flags & HIFN_IS_7811) ?
  9697. + HIFN_DMAIER_ILLW | HIFN_DMAIER_ILLR : 0);
  9698. + sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
  9699. + WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  9700. +
  9701. +
  9702. + if (sc->sc_flags & HIFN_IS_7956) {
  9703. + u_int32_t pll;
  9704. +
  9705. + WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
  9706. + HIFN_PUCNFG_TCALLPHASES |
  9707. + HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32);
  9708. +
  9709. + /* turn off the clocks and insure bypass is set */
  9710. + pll = READ_REG_1(sc, HIFN_1_PLL);
  9711. + pll = (pll &~ (HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL))
  9712. + | HIFN_PLL_BP | HIFN_PLL_MBSET;
  9713. + WRITE_REG_1(sc, HIFN_1_PLL, pll);
  9714. + DELAY(10*1000); /* 10ms */
  9715. +
  9716. + /* change configuration */
  9717. + pll = (pll &~ HIFN_PLL_CONFIG) | sc->sc_pllconfig;
  9718. + WRITE_REG_1(sc, HIFN_1_PLL, pll);
  9719. + DELAY(10*1000); /* 10ms */
  9720. +
  9721. + /* disable bypass */
  9722. + pll &= ~HIFN_PLL_BP;
  9723. + WRITE_REG_1(sc, HIFN_1_PLL, pll);
  9724. + /* enable clocks with new configuration */
  9725. + pll |= HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL;
  9726. + WRITE_REG_1(sc, HIFN_1_PLL, pll);
  9727. + } else {
  9728. + WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
  9729. + HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
  9730. + HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
  9731. + (sc->sc_drammodel ? HIFN_PUCNFG_DRAM : HIFN_PUCNFG_SRAM));
  9732. + }
  9733. +
  9734. + WRITE_REG_0(sc, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  9735. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  9736. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
  9737. + ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
  9738. + ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
  9739. +}
  9740. +
  9741. +/*
  9742. + * The maximum number of sessions supported by the card
  9743. + * is dependent on the amount of context ram, which
  9744. + * encryption algorithms are enabled, and how compression
  9745. + * is configured. This should be configured before this
  9746. + * routine is called.
  9747. + */
  9748. +static void
  9749. +hifn_sessions(struct hifn_softc *sc)
  9750. +{
  9751. + u_int32_t pucnfg;
  9752. + int ctxsize;
  9753. +
  9754. + DPRINTF("%s()\n", __FUNCTION__);
  9755. +
  9756. + pucnfg = READ_REG_0(sc, HIFN_0_PUCNFG);
  9757. +
  9758. + if (pucnfg & HIFN_PUCNFG_COMPSING) {
  9759. + if (pucnfg & HIFN_PUCNFG_ENCCNFG)
  9760. + ctxsize = 128;
  9761. + else
  9762. + ctxsize = 512;
  9763. + /*
  9764. + * 7955/7956 has internal context memory of 32K
  9765. + */
  9766. + if (sc->sc_flags & HIFN_IS_7956)
  9767. + sc->sc_maxses = 32768 / ctxsize;
  9768. + else
  9769. + sc->sc_maxses = 1 +
  9770. + ((sc->sc_ramsize - 32768) / ctxsize);
  9771. + } else
  9772. + sc->sc_maxses = sc->sc_ramsize / 16384;
  9773. +
  9774. + if (sc->sc_maxses > 2048)
  9775. + sc->sc_maxses = 2048;
  9776. +}
  9777. +
  9778. +/*
  9779. + * Determine ram type (sram or dram). Board should be just out of a reset
  9780. + * state when this is called.
  9781. + */
  9782. +static int
  9783. +hifn_ramtype(struct hifn_softc *sc)
  9784. +{
  9785. + u_int8_t data[8], dataexpect[8];
  9786. + int i;
  9787. +
  9788. + for (i = 0; i < sizeof(data); i++)
  9789. + data[i] = dataexpect[i] = 0x55;
  9790. + if (hifn_writeramaddr(sc, 0, data))
  9791. + return (-1);
  9792. + if (hifn_readramaddr(sc, 0, data))
  9793. + return (-1);
  9794. + if (bcmp(data, dataexpect, sizeof(data)) != 0) {
  9795. + sc->sc_drammodel = 1;
  9796. + return (0);
  9797. + }
  9798. +
  9799. + for (i = 0; i < sizeof(data); i++)
  9800. + data[i] = dataexpect[i] = 0xaa;
  9801. + if (hifn_writeramaddr(sc, 0, data))
  9802. + return (-1);
  9803. + if (hifn_readramaddr(sc, 0, data))
  9804. + return (-1);
  9805. + if (bcmp(data, dataexpect, sizeof(data)) != 0) {
  9806. + sc->sc_drammodel = 1;
  9807. + return (0);
  9808. + }
  9809. +
  9810. + return (0);
  9811. +}
  9812. +
  9813. +#define HIFN_SRAM_MAX (32 << 20)
  9814. +#define HIFN_SRAM_STEP_SIZE 16384
  9815. +#define HIFN_SRAM_GRANULARITY (HIFN_SRAM_MAX / HIFN_SRAM_STEP_SIZE)
  9816. +
  9817. +static int
  9818. +hifn_sramsize(struct hifn_softc *sc)
  9819. +{
  9820. + u_int32_t a;
  9821. + u_int8_t data[8];
  9822. + u_int8_t dataexpect[sizeof(data)];
  9823. + int32_t i;
  9824. +
  9825. + for (i = 0; i < sizeof(data); i++)
  9826. + data[i] = dataexpect[i] = i ^ 0x5a;
  9827. +
  9828. + for (i = HIFN_SRAM_GRANULARITY - 1; i >= 0; i--) {
  9829. + a = i * HIFN_SRAM_STEP_SIZE;
  9830. + bcopy(&i, data, sizeof(i));
  9831. + hifn_writeramaddr(sc, a, data);
  9832. + }
  9833. +
  9834. + for (i = 0; i < HIFN_SRAM_GRANULARITY; i++) {
  9835. + a = i * HIFN_SRAM_STEP_SIZE;
  9836. + bcopy(&i, dataexpect, sizeof(i));
  9837. + if (hifn_readramaddr(sc, a, data) < 0)
  9838. + return (0);
  9839. + if (bcmp(data, dataexpect, sizeof(data)) != 0)
  9840. + return (0);
  9841. + sc->sc_ramsize = a + HIFN_SRAM_STEP_SIZE;
  9842. + }
  9843. +
  9844. + return (0);
  9845. +}
  9846. +
  9847. +/*
  9848. + * XXX For dram boards, one should really try all of the
  9849. + * HIFN_PUCNFG_DSZ_*'s. This just assumes that PUCNFG
  9850. + * is already set up correctly.
  9851. + */
  9852. +static int
  9853. +hifn_dramsize(struct hifn_softc *sc)
  9854. +{
  9855. + u_int32_t cnfg;
  9856. +
  9857. + if (sc->sc_flags & HIFN_IS_7956) {
  9858. + /*
  9859. + * 7955/7956 have a fixed internal ram of only 32K.
  9860. + */
  9861. + sc->sc_ramsize = 32768;
  9862. + } else {
  9863. + cnfg = READ_REG_0(sc, HIFN_0_PUCNFG) &
  9864. + HIFN_PUCNFG_DRAMMASK;
  9865. + sc->sc_ramsize = 1 << ((cnfg >> 13) + 18);
  9866. + }
  9867. + return (0);
  9868. +}
  9869. +
  9870. +static void
  9871. +hifn_alloc_slot(struct hifn_softc *sc, int *cmdp, int *srcp, int *dstp, int *resp)
  9872. +{
  9873. + struct hifn_dma *dma = sc->sc_dma;
  9874. +
  9875. + DPRINTF("%s()\n", __FUNCTION__);
  9876. +
  9877. + if (dma->cmdi == HIFN_D_CMD_RSIZE) {
  9878. + dma->cmdi = 0;
  9879. + dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  9880. + wmb();
  9881. + dma->cmdr[HIFN_D_CMD_RSIZE].l |= htole32(HIFN_D_VALID);
  9882. + HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
  9883. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  9884. + }
  9885. + *cmdp = dma->cmdi++;
  9886. + dma->cmdk = dma->cmdi;
  9887. +
  9888. + if (dma->srci == HIFN_D_SRC_RSIZE) {
  9889. + dma->srci = 0;
  9890. + dma->srcr[HIFN_D_SRC_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  9891. + wmb();
  9892. + dma->srcr[HIFN_D_SRC_RSIZE].l |= htole32(HIFN_D_VALID);
  9893. + HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
  9894. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  9895. + }
  9896. + *srcp = dma->srci++;
  9897. + dma->srck = dma->srci;
  9898. +
  9899. + if (dma->dsti == HIFN_D_DST_RSIZE) {
  9900. + dma->dsti = 0;
  9901. + dma->dstr[HIFN_D_DST_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  9902. + wmb();
  9903. + dma->dstr[HIFN_D_DST_RSIZE].l |= htole32(HIFN_D_VALID);
  9904. + HIFN_DSTR_SYNC(sc, HIFN_D_DST_RSIZE,
  9905. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  9906. + }
  9907. + *dstp = dma->dsti++;
  9908. + dma->dstk = dma->dsti;
  9909. +
  9910. + if (dma->resi == HIFN_D_RES_RSIZE) {
  9911. + dma->resi = 0;
  9912. + dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  9913. + wmb();
  9914. + dma->resr[HIFN_D_RES_RSIZE].l |= htole32(HIFN_D_VALID);
  9915. + HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
  9916. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  9917. + }
  9918. + *resp = dma->resi++;
  9919. + dma->resk = dma->resi;
  9920. +}
  9921. +
  9922. +static int
  9923. +hifn_writeramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
  9924. +{
  9925. + struct hifn_dma *dma = sc->sc_dma;
  9926. + hifn_base_command_t wc;
  9927. + const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
  9928. + int r, cmdi, resi, srci, dsti;
  9929. +
  9930. + DPRINTF("%s()\n", __FUNCTION__);
  9931. +
  9932. + wc.masks = htole16(3 << 13);
  9933. + wc.session_num = htole16(addr >> 14);
  9934. + wc.total_source_count = htole16(8);
  9935. + wc.total_dest_count = htole16(addr & 0x3fff);
  9936. +
  9937. + hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
  9938. +
  9939. + WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  9940. + HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  9941. + HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
  9942. +
  9943. + /* build write command */
  9944. + bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
  9945. + *(hifn_base_command_t *)dma->command_bufs[cmdi] = wc;
  9946. + bcopy(data, &dma->test_src, sizeof(dma->test_src));
  9947. +
  9948. + dma->srcr[srci].p = htole32(sc->sc_dma_physaddr
  9949. + + offsetof(struct hifn_dma, test_src));
  9950. + dma->dstr[dsti].p = htole32(sc->sc_dma_physaddr
  9951. + + offsetof(struct hifn_dma, test_dst));
  9952. +
  9953. + dma->cmdr[cmdi].l = htole32(16 | masks);
  9954. + dma->srcr[srci].l = htole32(8 | masks);
  9955. + dma->dstr[dsti].l = htole32(4 | masks);
  9956. + dma->resr[resi].l = htole32(4 | masks);
  9957. +
  9958. + for (r = 10000; r >= 0; r--) {
  9959. + DELAY(10);
  9960. + if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
  9961. + break;
  9962. + }
  9963. + if (r == 0) {
  9964. + device_printf(sc->sc_dev, "writeramaddr -- "
  9965. + "result[%d](addr %d) still valid\n", resi, addr);
  9966. + r = -1;
  9967. + return (-1);
  9968. + } else
  9969. + r = 0;
  9970. +
  9971. + WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  9972. + HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
  9973. + HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
  9974. +
  9975. + return (r);
  9976. +}
  9977. +
  9978. +static int
  9979. +hifn_readramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
  9980. +{
  9981. + struct hifn_dma *dma = sc->sc_dma;
  9982. + hifn_base_command_t rc;
  9983. + const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
  9984. + int r, cmdi, srci, dsti, resi;
  9985. +
  9986. + DPRINTF("%s()\n", __FUNCTION__);
  9987. +
  9988. + rc.masks = htole16(2 << 13);
  9989. + rc.session_num = htole16(addr >> 14);
  9990. + rc.total_source_count = htole16(addr & 0x3fff);
  9991. + rc.total_dest_count = htole16(8);
  9992. +
  9993. + hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
  9994. +
  9995. + WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  9996. + HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  9997. + HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
  9998. +
  9999. + bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
  10000. + *(hifn_base_command_t *)dma->command_bufs[cmdi] = rc;
  10001. +
  10002. + dma->srcr[srci].p = htole32(sc->sc_dma_physaddr +
  10003. + offsetof(struct hifn_dma, test_src));
  10004. + dma->test_src = 0;
  10005. + dma->dstr[dsti].p = htole32(sc->sc_dma_physaddr +
  10006. + offsetof(struct hifn_dma, test_dst));
  10007. + dma->test_dst = 0;
  10008. + dma->cmdr[cmdi].l = htole32(8 | masks);
  10009. + dma->srcr[srci].l = htole32(8 | masks);
  10010. + dma->dstr[dsti].l = htole32(8 | masks);
  10011. + dma->resr[resi].l = htole32(HIFN_MAX_RESULT | masks);
  10012. +
  10013. + for (r = 10000; r >= 0; r--) {
  10014. + DELAY(10);
  10015. + if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
  10016. + break;
  10017. + }
  10018. + if (r == 0) {
  10019. + device_printf(sc->sc_dev, "readramaddr -- "
  10020. + "result[%d](addr %d) still valid\n", resi, addr);
  10021. + r = -1;
  10022. + } else {
  10023. + r = 0;
  10024. + bcopy(&dma->test_dst, data, sizeof(dma->test_dst));
  10025. + }
  10026. +
  10027. + WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  10028. + HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
  10029. + HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
  10030. +
  10031. + return (r);
  10032. +}
  10033. +
  10034. +/*
  10035. + * Initialize the descriptor rings.
  10036. + */
  10037. +static void
  10038. +hifn_init_dma(struct hifn_softc *sc)
  10039. +{
  10040. + struct hifn_dma *dma = sc->sc_dma;
  10041. + int i;
  10042. +
  10043. + DPRINTF("%s()\n", __FUNCTION__);
  10044. +
  10045. + hifn_set_retry(sc);
  10046. +
  10047. + /* initialize static pointer values */
  10048. + for (i = 0; i < HIFN_D_CMD_RSIZE; i++)
  10049. + dma->cmdr[i].p = htole32(sc->sc_dma_physaddr +
  10050. + offsetof(struct hifn_dma, command_bufs[i][0]));
  10051. + for (i = 0; i < HIFN_D_RES_RSIZE; i++)
  10052. + dma->resr[i].p = htole32(sc->sc_dma_physaddr +
  10053. + offsetof(struct hifn_dma, result_bufs[i][0]));
  10054. +
  10055. + dma->cmdr[HIFN_D_CMD_RSIZE].p =
  10056. + htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, cmdr[0]));
  10057. + dma->srcr[HIFN_D_SRC_RSIZE].p =
  10058. + htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, srcr[0]));
  10059. + dma->dstr[HIFN_D_DST_RSIZE].p =
  10060. + htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, dstr[0]));
  10061. + dma->resr[HIFN_D_RES_RSIZE].p =
  10062. + htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, resr[0]));
  10063. +
  10064. + dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
  10065. + dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
  10066. + dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
  10067. +}
  10068. +
  10069. +/*
  10070. + * Writes out the raw command buffer space. Returns the
  10071. + * command buffer size.
  10072. + */
  10073. +static u_int
  10074. +hifn_write_command(struct hifn_command *cmd, u_int8_t *buf)
  10075. +{
  10076. + struct hifn_softc *sc = NULL;
  10077. + u_int8_t *buf_pos;
  10078. + hifn_base_command_t *base_cmd;
  10079. + hifn_mac_command_t *mac_cmd;
  10080. + hifn_crypt_command_t *cry_cmd;
  10081. + int using_mac, using_crypt, len, ivlen;
  10082. + u_int32_t dlen, slen;
  10083. +
  10084. + DPRINTF("%s()\n", __FUNCTION__);
  10085. +
  10086. + buf_pos = buf;
  10087. + using_mac = cmd->base_masks & HIFN_BASE_CMD_MAC;
  10088. + using_crypt = cmd->base_masks & HIFN_BASE_CMD_CRYPT;
  10089. +
  10090. + base_cmd = (hifn_base_command_t *)buf_pos;
  10091. + base_cmd->masks = htole16(cmd->base_masks);
  10092. + slen = cmd->src_mapsize;
  10093. + if (cmd->sloplen)
  10094. + dlen = cmd->dst_mapsize - cmd->sloplen + sizeof(u_int32_t);
  10095. + else
  10096. + dlen = cmd->dst_mapsize;
  10097. + base_cmd->total_source_count = htole16(slen & HIFN_BASE_CMD_LENMASK_LO);
  10098. + base_cmd->total_dest_count = htole16(dlen & HIFN_BASE_CMD_LENMASK_LO);
  10099. + dlen >>= 16;
  10100. + slen >>= 16;
  10101. + base_cmd->session_num = htole16(
  10102. + ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
  10103. + ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
  10104. + buf_pos += sizeof(hifn_base_command_t);
  10105. +
  10106. + if (using_mac) {
  10107. + mac_cmd = (hifn_mac_command_t *)buf_pos;
  10108. + dlen = cmd->maccrd->crd_len;
  10109. + mac_cmd->source_count = htole16(dlen & 0xffff);
  10110. + dlen >>= 16;
  10111. + mac_cmd->masks = htole16(cmd->mac_masks |
  10112. + ((dlen << HIFN_MAC_CMD_SRCLEN_S) & HIFN_MAC_CMD_SRCLEN_M));
  10113. + mac_cmd->header_skip = htole16(cmd->maccrd->crd_skip);
  10114. + mac_cmd->reserved = 0;
  10115. + buf_pos += sizeof(hifn_mac_command_t);
  10116. + }
  10117. +
  10118. + if (using_crypt) {
  10119. + cry_cmd = (hifn_crypt_command_t *)buf_pos;
  10120. + dlen = cmd->enccrd->crd_len;
  10121. + cry_cmd->source_count = htole16(dlen & 0xffff);
  10122. + dlen >>= 16;
  10123. + cry_cmd->masks = htole16(cmd->cry_masks |
  10124. + ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) & HIFN_CRYPT_CMD_SRCLEN_M));
  10125. + cry_cmd->header_skip = htole16(cmd->enccrd->crd_skip);
  10126. + cry_cmd->reserved = 0;
  10127. + buf_pos += sizeof(hifn_crypt_command_t);
  10128. + }
  10129. +
  10130. + if (using_mac && cmd->mac_masks & HIFN_MAC_CMD_NEW_KEY) {
  10131. + bcopy(cmd->mac, buf_pos, HIFN_MAC_KEY_LENGTH);
  10132. + buf_pos += HIFN_MAC_KEY_LENGTH;
  10133. + }
  10134. +
  10135. + if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_KEY) {
  10136. + switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
  10137. + case HIFN_CRYPT_CMD_ALG_3DES:
  10138. + bcopy(cmd->ck, buf_pos, HIFN_3DES_KEY_LENGTH);
  10139. + buf_pos += HIFN_3DES_KEY_LENGTH;
  10140. + break;
  10141. + case HIFN_CRYPT_CMD_ALG_DES:
  10142. + bcopy(cmd->ck, buf_pos, HIFN_DES_KEY_LENGTH);
  10143. + buf_pos += HIFN_DES_KEY_LENGTH;
  10144. + break;
  10145. + case HIFN_CRYPT_CMD_ALG_RC4:
  10146. + len = 256;
  10147. + do {
  10148. + int clen;
  10149. +
  10150. + clen = MIN(cmd->cklen, len);
  10151. + bcopy(cmd->ck, buf_pos, clen);
  10152. + len -= clen;
  10153. + buf_pos += clen;
  10154. + } while (len > 0);
  10155. + bzero(buf_pos, 4);
  10156. + buf_pos += 4;
  10157. + break;
  10158. + case HIFN_CRYPT_CMD_ALG_AES:
  10159. + /*
  10160. + * AES keys are variable 128, 192 and
  10161. + * 256 bits (16, 24 and 32 bytes).
  10162. + */
  10163. + bcopy(cmd->ck, buf_pos, cmd->cklen);
  10164. + buf_pos += cmd->cklen;
  10165. + break;
  10166. + }
  10167. + }
  10168. +
  10169. + if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_IV) {
  10170. + switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
  10171. + case HIFN_CRYPT_CMD_ALG_AES:
  10172. + ivlen = HIFN_AES_IV_LENGTH;
  10173. + break;
  10174. + default:
  10175. + ivlen = HIFN_IV_LENGTH;
  10176. + break;
  10177. + }
  10178. + bcopy(cmd->iv, buf_pos, ivlen);
  10179. + buf_pos += ivlen;
  10180. + }
  10181. +
  10182. + if ((cmd->base_masks & (HIFN_BASE_CMD_MAC|HIFN_BASE_CMD_CRYPT)) == 0) {
  10183. + bzero(buf_pos, 8);
  10184. + buf_pos += 8;
  10185. + }
  10186. +
  10187. + return (buf_pos - buf);
  10188. +}
  10189. +
  10190. +static int
  10191. +hifn_dmamap_aligned(struct hifn_operand *op)
  10192. +{
  10193. + struct hifn_softc *sc = NULL;
  10194. + int i;
  10195. +
  10196. + DPRINTF("%s()\n", __FUNCTION__);
  10197. +
  10198. + for (i = 0; i < op->nsegs; i++) {
  10199. + if (op->segs[i].ds_addr & 3)
  10200. + return (0);
  10201. + if ((i != (op->nsegs - 1)) && (op->segs[i].ds_len & 3))
  10202. + return (0);
  10203. + }
  10204. + return (1);
  10205. +}
  10206. +
  10207. +static __inline int
  10208. +hifn_dmamap_dstwrap(struct hifn_softc *sc, int idx)
  10209. +{
  10210. + struct hifn_dma *dma = sc->sc_dma;
  10211. +
  10212. + if (++idx == HIFN_D_DST_RSIZE) {
  10213. + dma->dstr[idx].l = htole32(HIFN_D_VALID | HIFN_D_JUMP |
  10214. + HIFN_D_MASKDONEIRQ);
  10215. + HIFN_DSTR_SYNC(sc, idx,
  10216. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10217. + idx = 0;
  10218. + }
  10219. + return (idx);
  10220. +}
  10221. +
  10222. +static int
  10223. +hifn_dmamap_load_dst(struct hifn_softc *sc, struct hifn_command *cmd)
  10224. +{
  10225. + struct hifn_dma *dma = sc->sc_dma;
  10226. + struct hifn_operand *dst = &cmd->dst;
  10227. + u_int32_t p, l;
  10228. + int idx, used = 0, i;
  10229. +
  10230. + DPRINTF("%s()\n", __FUNCTION__);
  10231. +
  10232. + idx = dma->dsti;
  10233. + for (i = 0; i < dst->nsegs - 1; i++) {
  10234. + dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
  10235. + dma->dstr[idx].l = htole32(HIFN_D_MASKDONEIRQ | dst->segs[i].ds_len);
  10236. + wmb();
  10237. + dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  10238. + HIFN_DSTR_SYNC(sc, idx,
  10239. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10240. + used++;
  10241. +
  10242. + idx = hifn_dmamap_dstwrap(sc, idx);
  10243. + }
  10244. +
  10245. + if (cmd->sloplen == 0) {
  10246. + p = dst->segs[i].ds_addr;
  10247. + l = HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
  10248. + dst->segs[i].ds_len;
  10249. + } else {
  10250. + p = sc->sc_dma_physaddr +
  10251. + offsetof(struct hifn_dma, slop[cmd->slopidx]);
  10252. + l = HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
  10253. + sizeof(u_int32_t);
  10254. +
  10255. + if ((dst->segs[i].ds_len - cmd->sloplen) != 0) {
  10256. + dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
  10257. + dma->dstr[idx].l = htole32(HIFN_D_MASKDONEIRQ |
  10258. + (dst->segs[i].ds_len - cmd->sloplen));
  10259. + wmb();
  10260. + dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  10261. + HIFN_DSTR_SYNC(sc, idx,
  10262. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10263. + used++;
  10264. +
  10265. + idx = hifn_dmamap_dstwrap(sc, idx);
  10266. + }
  10267. + }
  10268. + dma->dstr[idx].p = htole32(p);
  10269. + dma->dstr[idx].l = htole32(l);
  10270. + wmb();
  10271. + dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  10272. + HIFN_DSTR_SYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10273. + used++;
  10274. +
  10275. + idx = hifn_dmamap_dstwrap(sc, idx);
  10276. +
  10277. + dma->dsti = idx;
  10278. + dma->dstu += used;
  10279. + return (idx);
  10280. +}
  10281. +
  10282. +static __inline int
  10283. +hifn_dmamap_srcwrap(struct hifn_softc *sc, int idx)
  10284. +{
  10285. + struct hifn_dma *dma = sc->sc_dma;
  10286. +
  10287. + if (++idx == HIFN_D_SRC_RSIZE) {
  10288. + dma->srcr[idx].l = htole32(HIFN_D_VALID |
  10289. + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
  10290. + HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
  10291. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  10292. + idx = 0;
  10293. + }
  10294. + return (idx);
  10295. +}
  10296. +
  10297. +static int
  10298. +hifn_dmamap_load_src(struct hifn_softc *sc, struct hifn_command *cmd)
  10299. +{
  10300. + struct hifn_dma *dma = sc->sc_dma;
  10301. + struct hifn_operand *src = &cmd->src;
  10302. + int idx, i;
  10303. + u_int32_t last = 0;
  10304. +
  10305. + DPRINTF("%s()\n", __FUNCTION__);
  10306. +
  10307. + idx = dma->srci;
  10308. + for (i = 0; i < src->nsegs; i++) {
  10309. + if (i == src->nsegs - 1)
  10310. + last = HIFN_D_LAST;
  10311. +
  10312. + dma->srcr[idx].p = htole32(src->segs[i].ds_addr);
  10313. + dma->srcr[idx].l = htole32(src->segs[i].ds_len |
  10314. + HIFN_D_MASKDONEIRQ | last);
  10315. + wmb();
  10316. + dma->srcr[idx].l |= htole32(HIFN_D_VALID);
  10317. + HIFN_SRCR_SYNC(sc, idx,
  10318. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  10319. +
  10320. + idx = hifn_dmamap_srcwrap(sc, idx);
  10321. + }
  10322. + dma->srci = idx;
  10323. + dma->srcu += src->nsegs;
  10324. + return (idx);
  10325. +}
  10326. +
  10327. +
  10328. +static int
  10329. +hifn_crypto(
  10330. + struct hifn_softc *sc,
  10331. + struct hifn_command *cmd,
  10332. + struct cryptop *crp,
  10333. + int hint)
  10334. +{
  10335. + struct hifn_dma *dma = sc->sc_dma;
  10336. + u_int32_t cmdlen, csr;
  10337. + int cmdi, resi, err = 0;
  10338. + unsigned long l_flags;
  10339. +
  10340. + DPRINTF("%s()\n", __FUNCTION__);
  10341. +
  10342. + /*
  10343. + * need 1 cmd, and 1 res
  10344. + *
  10345. + * NB: check this first since it's easy.
  10346. + */
  10347. + HIFN_LOCK(sc);
  10348. + if ((dma->cmdu + 1) > HIFN_D_CMD_RSIZE ||
  10349. + (dma->resu + 1) > HIFN_D_RES_RSIZE) {
  10350. +#ifdef HIFN_DEBUG
  10351. + if (hifn_debug) {
  10352. + device_printf(sc->sc_dev,
  10353. + "cmd/result exhaustion, cmdu %u resu %u\n",
  10354. + dma->cmdu, dma->resu);
  10355. + }
  10356. +#endif
  10357. + hifnstats.hst_nomem_cr++;
  10358. + sc->sc_needwakeup |= CRYPTO_SYMQ;
  10359. + HIFN_UNLOCK(sc);
  10360. + return (ERESTART);
  10361. + }
  10362. +
  10363. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  10364. + if (pci_map_skb(sc, &cmd->src, cmd->src_skb)) {
  10365. + hifnstats.hst_nomem_load++;
  10366. + err = ENOMEM;
  10367. + goto err_srcmap1;
  10368. + }
  10369. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  10370. + if (pci_map_uio(sc, &cmd->src, cmd->src_io)) {
  10371. + hifnstats.hst_nomem_load++;
  10372. + err = ENOMEM;
  10373. + goto err_srcmap1;
  10374. + }
  10375. + } else {
  10376. + if (pci_map_buf(sc, &cmd->src, cmd->src_buf, crp->crp_ilen)) {
  10377. + hifnstats.hst_nomem_load++;
  10378. + err = ENOMEM;
  10379. + goto err_srcmap1;
  10380. + }
  10381. + }
  10382. +
  10383. + if (hifn_dmamap_aligned(&cmd->src)) {
  10384. + cmd->sloplen = cmd->src_mapsize & 3;
  10385. + cmd->dst = cmd->src;
  10386. + } else {
  10387. + if (crp->crp_flags & CRYPTO_F_IOV) {
  10388. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  10389. + err = EINVAL;
  10390. + goto err_srcmap;
  10391. + } else if (crp->crp_flags & CRYPTO_F_SKBUF) {
  10392. +#ifdef NOTYET
  10393. + int totlen, len;
  10394. + struct mbuf *m, *m0, *mlast;
  10395. +
  10396. + KASSERT(cmd->dst_m == cmd->src_m,
  10397. + ("hifn_crypto: dst_m initialized improperly"));
  10398. + hifnstats.hst_unaligned++;
  10399. + /*
  10400. + * Source is not aligned on a longword boundary.
  10401. + * Copy the data to insure alignment. If we fail
  10402. + * to allocate mbufs or clusters while doing this
  10403. + * we return ERESTART so the operation is requeued
  10404. + * at the crypto later, but only if there are
  10405. + * ops already posted to the hardware; otherwise we
  10406. + * have no guarantee that we'll be re-entered.
  10407. + */
  10408. + totlen = cmd->src_mapsize;
  10409. + if (cmd->src_m->m_flags & M_PKTHDR) {
  10410. + len = MHLEN;
  10411. + MGETHDR(m0, M_DONTWAIT, MT_DATA);
  10412. + if (m0 && !m_dup_pkthdr(m0, cmd->src_m, M_DONTWAIT)) {
  10413. + m_free(m0);
  10414. + m0 = NULL;
  10415. + }
  10416. + } else {
  10417. + len = MLEN;
  10418. + MGET(m0, M_DONTWAIT, MT_DATA);
  10419. + }
  10420. + if (m0 == NULL) {
  10421. + hifnstats.hst_nomem_mbuf++;
  10422. + err = dma->cmdu ? ERESTART : ENOMEM;
  10423. + goto err_srcmap;
  10424. + }
  10425. + if (totlen >= MINCLSIZE) {
  10426. + MCLGET(m0, M_DONTWAIT);
  10427. + if ((m0->m_flags & M_EXT) == 0) {
  10428. + hifnstats.hst_nomem_mcl++;
  10429. + err = dma->cmdu ? ERESTART : ENOMEM;
  10430. + m_freem(m0);
  10431. + goto err_srcmap;
  10432. + }
  10433. + len = MCLBYTES;
  10434. + }
  10435. + totlen -= len;
  10436. + m0->m_pkthdr.len = m0->m_len = len;
  10437. + mlast = m0;
  10438. +
  10439. + while (totlen > 0) {
  10440. + MGET(m, M_DONTWAIT, MT_DATA);
  10441. + if (m == NULL) {
  10442. + hifnstats.hst_nomem_mbuf++;
  10443. + err = dma->cmdu ? ERESTART : ENOMEM;
  10444. + m_freem(m0);
  10445. + goto err_srcmap;
  10446. + }
  10447. + len = MLEN;
  10448. + if (totlen >= MINCLSIZE) {
  10449. + MCLGET(m, M_DONTWAIT);
  10450. + if ((m->m_flags & M_EXT) == 0) {
  10451. + hifnstats.hst_nomem_mcl++;
  10452. + err = dma->cmdu ? ERESTART : ENOMEM;
  10453. + mlast->m_next = m;
  10454. + m_freem(m0);
  10455. + goto err_srcmap;
  10456. + }
  10457. + len = MCLBYTES;
  10458. + }
  10459. +
  10460. + m->m_len = len;
  10461. + m0->m_pkthdr.len += len;
  10462. + totlen -= len;
  10463. +
  10464. + mlast->m_next = m;
  10465. + mlast = m;
  10466. + }
  10467. + cmd->dst_m = m0;
  10468. +#else
  10469. + device_printf(sc->sc_dev,
  10470. + "%s,%d: CRYPTO_F_SKBUF unaligned not implemented\n",
  10471. + __FILE__, __LINE__);
  10472. + err = EINVAL;
  10473. + goto err_srcmap;
  10474. +#endif
  10475. + } else {
  10476. + device_printf(sc->sc_dev,
  10477. + "%s,%d: unaligned contig buffers not implemented\n",
  10478. + __FILE__, __LINE__);
  10479. + err = EINVAL;
  10480. + goto err_srcmap;
  10481. + }
  10482. + }
  10483. +
  10484. + if (cmd->dst_map == NULL) {
  10485. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  10486. + if (pci_map_skb(sc, &cmd->dst, cmd->dst_skb)) {
  10487. + hifnstats.hst_nomem_map++;
  10488. + err = ENOMEM;
  10489. + goto err_dstmap1;
  10490. + }
  10491. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  10492. + if (pci_map_uio(sc, &cmd->dst, cmd->dst_io)) {
  10493. + hifnstats.hst_nomem_load++;
  10494. + err = ENOMEM;
  10495. + goto err_dstmap1;
  10496. + }
  10497. + } else {
  10498. + if (pci_map_buf(sc, &cmd->dst, cmd->dst_buf, crp->crp_ilen)) {
  10499. + hifnstats.hst_nomem_load++;
  10500. + err = ENOMEM;
  10501. + goto err_dstmap1;
  10502. + }
  10503. + }
  10504. + }
  10505. +
  10506. +#ifdef HIFN_DEBUG
  10507. + if (hifn_debug) {
  10508. + device_printf(sc->sc_dev,
  10509. + "Entering cmd: stat %8x ien %8x u %d/%d/%d/%d n %d/%d\n",
  10510. + READ_REG_1(sc, HIFN_1_DMA_CSR),
  10511. + READ_REG_1(sc, HIFN_1_DMA_IER),
  10512. + dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  10513. + cmd->src_nsegs, cmd->dst_nsegs);
  10514. + }
  10515. +#endif
  10516. +
  10517. +#if 0
  10518. + if (cmd->src_map == cmd->dst_map) {
  10519. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  10520. + BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
  10521. + } else {
  10522. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  10523. + BUS_DMASYNC_PREWRITE);
  10524. + bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  10525. + BUS_DMASYNC_PREREAD);
  10526. + }
  10527. +#endif
  10528. +
  10529. + /*
  10530. + * need N src, and N dst
  10531. + */
  10532. + if ((dma->srcu + cmd->src_nsegs) > HIFN_D_SRC_RSIZE ||
  10533. + (dma->dstu + cmd->dst_nsegs + 1) > HIFN_D_DST_RSIZE) {
  10534. +#ifdef HIFN_DEBUG
  10535. + if (hifn_debug) {
  10536. + device_printf(sc->sc_dev,
  10537. + "src/dst exhaustion, srcu %u+%u dstu %u+%u\n",
  10538. + dma->srcu, cmd->src_nsegs,
  10539. + dma->dstu, cmd->dst_nsegs);
  10540. + }
  10541. +#endif
  10542. + hifnstats.hst_nomem_sd++;
  10543. + err = ERESTART;
  10544. + goto err_dstmap;
  10545. + }
  10546. +
  10547. + if (dma->cmdi == HIFN_D_CMD_RSIZE) {
  10548. + dma->cmdi = 0;
  10549. + dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  10550. + wmb();
  10551. + dma->cmdr[HIFN_D_CMD_RSIZE].l |= htole32(HIFN_D_VALID);
  10552. + HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
  10553. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  10554. + }
  10555. + cmdi = dma->cmdi++;
  10556. + cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
  10557. + HIFN_CMD_SYNC(sc, cmdi, BUS_DMASYNC_PREWRITE);
  10558. +
  10559. + /* .p for command/result already set */
  10560. + dma->cmdr[cmdi].l = htole32(cmdlen | HIFN_D_LAST |
  10561. + HIFN_D_MASKDONEIRQ);
  10562. + wmb();
  10563. + dma->cmdr[cmdi].l |= htole32(HIFN_D_VALID);
  10564. + HIFN_CMDR_SYNC(sc, cmdi,
  10565. + BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  10566. + dma->cmdu++;
  10567. +
  10568. + /*
  10569. + * We don't worry about missing an interrupt (which a "command wait"
  10570. + * interrupt salvages us from), unless there is more than one command
  10571. + * in the queue.
  10572. + */
  10573. + if (dma->cmdu > 1) {
  10574. + sc->sc_dmaier |= HIFN_DMAIER_C_WAIT;
  10575. + WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  10576. + }
  10577. +
  10578. + hifnstats.hst_ipackets++;
  10579. + hifnstats.hst_ibytes += cmd->src_mapsize;
  10580. +
  10581. + hifn_dmamap_load_src(sc, cmd);
  10582. +
  10583. + /*
  10584. + * Unlike other descriptors, we don't mask done interrupt from
  10585. + * result descriptor.
  10586. + */
  10587. +#ifdef HIFN_DEBUG
  10588. + if (hifn_debug)
  10589. + device_printf(sc->sc_dev, "load res\n");
  10590. +#endif
  10591. + if (dma->resi == HIFN_D_RES_RSIZE) {
  10592. + dma->resi = 0;
  10593. + dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  10594. + wmb();
  10595. + dma->resr[HIFN_D_RES_RSIZE].l |= htole32(HIFN_D_VALID);
  10596. + HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
  10597. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10598. + }
  10599. + resi = dma->resi++;
  10600. + KASSERT(dma->hifn_commands[resi] == NULL,
  10601. + ("hifn_crypto: command slot %u busy", resi));
  10602. + dma->hifn_commands[resi] = cmd;
  10603. + HIFN_RES_SYNC(sc, resi, BUS_DMASYNC_PREREAD);
  10604. + if ((hint & CRYPTO_HINT_MORE) && sc->sc_curbatch < hifn_maxbatch) {
  10605. + dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
  10606. + HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
  10607. + wmb();
  10608. + dma->resr[resi].l |= htole32(HIFN_D_VALID);
  10609. + sc->sc_curbatch++;
  10610. + if (sc->sc_curbatch > hifnstats.hst_maxbatch)
  10611. + hifnstats.hst_maxbatch = sc->sc_curbatch;
  10612. + hifnstats.hst_totbatch++;
  10613. + } else {
  10614. + dma->resr[resi].l = htole32(HIFN_MAX_RESULT | HIFN_D_LAST);
  10615. + wmb();
  10616. + dma->resr[resi].l |= htole32(HIFN_D_VALID);
  10617. + sc->sc_curbatch = 0;
  10618. + }
  10619. + HIFN_RESR_SYNC(sc, resi,
  10620. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10621. + dma->resu++;
  10622. +
  10623. + if (cmd->sloplen)
  10624. + cmd->slopidx = resi;
  10625. +
  10626. + hifn_dmamap_load_dst(sc, cmd);
  10627. +
  10628. + csr = 0;
  10629. + if (sc->sc_c_busy == 0) {
  10630. + csr |= HIFN_DMACSR_C_CTRL_ENA;
  10631. + sc->sc_c_busy = 1;
  10632. + }
  10633. + if (sc->sc_s_busy == 0) {
  10634. + csr |= HIFN_DMACSR_S_CTRL_ENA;
  10635. + sc->sc_s_busy = 1;
  10636. + }
  10637. + if (sc->sc_r_busy == 0) {
  10638. + csr |= HIFN_DMACSR_R_CTRL_ENA;
  10639. + sc->sc_r_busy = 1;
  10640. + }
  10641. + if (sc->sc_d_busy == 0) {
  10642. + csr |= HIFN_DMACSR_D_CTRL_ENA;
  10643. + sc->sc_d_busy = 1;
  10644. + }
  10645. + if (csr)
  10646. + WRITE_REG_1(sc, HIFN_1_DMA_CSR, csr);
  10647. +
  10648. +#ifdef HIFN_DEBUG
  10649. + if (hifn_debug) {
  10650. + device_printf(sc->sc_dev, "command: stat %8x ier %8x\n",
  10651. + READ_REG_1(sc, HIFN_1_DMA_CSR),
  10652. + READ_REG_1(sc, HIFN_1_DMA_IER));
  10653. + }
  10654. +#endif
  10655. +
  10656. + sc->sc_active = 5;
  10657. + HIFN_UNLOCK(sc);
  10658. + KASSERT(err == 0, ("hifn_crypto: success with error %u", err));
  10659. + return (err); /* success */
  10660. +
  10661. +err_dstmap:
  10662. + if (cmd->src_map != cmd->dst_map)
  10663. + pci_unmap_buf(sc, &cmd->dst);
  10664. +err_dstmap1:
  10665. +err_srcmap:
  10666. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  10667. + if (cmd->src_skb != cmd->dst_skb)
  10668. +#ifdef NOTYET
  10669. + m_freem(cmd->dst_m);
  10670. +#else
  10671. + device_printf(sc->sc_dev,
  10672. + "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  10673. + __FILE__, __LINE__);
  10674. +#endif
  10675. + }
  10676. + pci_unmap_buf(sc, &cmd->src);
  10677. +err_srcmap1:
  10678. + HIFN_UNLOCK(sc);
  10679. + return (err);
  10680. +}
  10681. +
  10682. +static void
  10683. +hifn_tick(unsigned long arg)
  10684. +{
  10685. + struct hifn_softc *sc;
  10686. + unsigned long l_flags;
  10687. +
  10688. + if (arg >= HIFN_MAX_CHIPS)
  10689. + return;
  10690. + sc = hifn_chip_idx[arg];
  10691. + if (!sc)
  10692. + return;
  10693. +
  10694. + HIFN_LOCK(sc);
  10695. + if (sc->sc_active == 0) {
  10696. + struct hifn_dma *dma = sc->sc_dma;
  10697. + u_int32_t r = 0;
  10698. +
  10699. + if (dma->cmdu == 0 && sc->sc_c_busy) {
  10700. + sc->sc_c_busy = 0;
  10701. + r |= HIFN_DMACSR_C_CTRL_DIS;
  10702. + }
  10703. + if (dma->srcu == 0 && sc->sc_s_busy) {
  10704. + sc->sc_s_busy = 0;
  10705. + r |= HIFN_DMACSR_S_CTRL_DIS;
  10706. + }
  10707. + if (dma->dstu == 0 && sc->sc_d_busy) {
  10708. + sc->sc_d_busy = 0;
  10709. + r |= HIFN_DMACSR_D_CTRL_DIS;
  10710. + }
  10711. + if (dma->resu == 0 && sc->sc_r_busy) {
  10712. + sc->sc_r_busy = 0;
  10713. + r |= HIFN_DMACSR_R_CTRL_DIS;
  10714. + }
  10715. + if (r)
  10716. + WRITE_REG_1(sc, HIFN_1_DMA_CSR, r);
  10717. + } else
  10718. + sc->sc_active--;
  10719. + HIFN_UNLOCK(sc);
  10720. + mod_timer(&sc->sc_tickto, jiffies + HZ);
  10721. +}
  10722. +
  10723. +static irqreturn_t
  10724. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  10725. +hifn_intr(int irq, void *arg)
  10726. +#else
  10727. +hifn_intr(int irq, void *arg, struct pt_regs *regs)
  10728. +#endif
  10729. +{
  10730. + struct hifn_softc *sc = arg;
  10731. + struct hifn_dma *dma;
  10732. + u_int32_t dmacsr, restart;
  10733. + int i, u;
  10734. + unsigned long l_flags;
  10735. +
  10736. + dmacsr = READ_REG_1(sc, HIFN_1_DMA_CSR);
  10737. +
  10738. + /* Nothing in the DMA unit interrupted */
  10739. + if ((dmacsr & sc->sc_dmaier) == 0)
  10740. + return IRQ_NONE;
  10741. +
  10742. + HIFN_LOCK(sc);
  10743. +
  10744. + dma = sc->sc_dma;
  10745. +
  10746. +#ifdef HIFN_DEBUG
  10747. + if (hifn_debug) {
  10748. + device_printf(sc->sc_dev,
  10749. + "irq: stat %08x ien %08x damier %08x i %d/%d/%d/%d k %d/%d/%d/%d u %d/%d/%d/%d\n",
  10750. + dmacsr, READ_REG_1(sc, HIFN_1_DMA_IER), sc->sc_dmaier,
  10751. + dma->cmdi, dma->srci, dma->dsti, dma->resi,
  10752. + dma->cmdk, dma->srck, dma->dstk, dma->resk,
  10753. + dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  10754. + }
  10755. +#endif
  10756. +
  10757. + WRITE_REG_1(sc, HIFN_1_DMA_CSR, dmacsr & sc->sc_dmaier);
  10758. +
  10759. + if ((sc->sc_flags & HIFN_HAS_PUBLIC) &&
  10760. + (dmacsr & HIFN_DMACSR_PUBDONE))
  10761. + WRITE_REG_1(sc, HIFN_1_PUB_STATUS,
  10762. + READ_REG_1(sc, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
  10763. +
  10764. + restart = dmacsr & (HIFN_DMACSR_D_OVER | HIFN_DMACSR_R_OVER);
  10765. + if (restart)
  10766. + device_printf(sc->sc_dev, "overrun %x\n", dmacsr);
  10767. +
  10768. + if (sc->sc_flags & HIFN_IS_7811) {
  10769. + if (dmacsr & HIFN_DMACSR_ILLR)
  10770. + device_printf(sc->sc_dev, "illegal read\n");
  10771. + if (dmacsr & HIFN_DMACSR_ILLW)
  10772. + device_printf(sc->sc_dev, "illegal write\n");
  10773. + }
  10774. +
  10775. + restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
  10776. + HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
  10777. + if (restart) {
  10778. + device_printf(sc->sc_dev, "abort, resetting.\n");
  10779. + hifnstats.hst_abort++;
  10780. + hifn_abort(sc);
  10781. + HIFN_UNLOCK(sc);
  10782. + return IRQ_HANDLED;
  10783. + }
  10784. +
  10785. + if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
  10786. + /*
  10787. + * If no slots to process and we receive a "waiting on
  10788. + * command" interrupt, we disable the "waiting on command"
  10789. + * (by clearing it).
  10790. + */
  10791. + sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
  10792. + WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  10793. + }
  10794. +
  10795. + /* clear the rings */
  10796. + i = dma->resk; u = dma->resu;
  10797. + while (u != 0) {
  10798. + HIFN_RESR_SYNC(sc, i,
  10799. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  10800. + if (dma->resr[i].l & htole32(HIFN_D_VALID)) {
  10801. + HIFN_RESR_SYNC(sc, i,
  10802. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10803. + break;
  10804. + }
  10805. +
  10806. + if (i != HIFN_D_RES_RSIZE) {
  10807. + struct hifn_command *cmd;
  10808. + u_int8_t *macbuf = NULL;
  10809. +
  10810. + HIFN_RES_SYNC(sc, i, BUS_DMASYNC_POSTREAD);
  10811. + cmd = dma->hifn_commands[i];
  10812. + KASSERT(cmd != NULL,
  10813. + ("hifn_intr: null command slot %u", i));
  10814. + dma->hifn_commands[i] = NULL;
  10815. +
  10816. + if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
  10817. + macbuf = dma->result_bufs[i];
  10818. + macbuf += 12;
  10819. + }
  10820. +
  10821. + hifn_callback(sc, cmd, macbuf);
  10822. + hifnstats.hst_opackets++;
  10823. + u--;
  10824. + }
  10825. +
  10826. + if (++i == (HIFN_D_RES_RSIZE + 1))
  10827. + i = 0;
  10828. + }
  10829. + dma->resk = i; dma->resu = u;
  10830. +
  10831. + i = dma->srck; u = dma->srcu;
  10832. + while (u != 0) {
  10833. + if (i == HIFN_D_SRC_RSIZE)
  10834. + i = 0;
  10835. + HIFN_SRCR_SYNC(sc, i,
  10836. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  10837. + if (dma->srcr[i].l & htole32(HIFN_D_VALID)) {
  10838. + HIFN_SRCR_SYNC(sc, i,
  10839. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10840. + break;
  10841. + }
  10842. + i++, u--;
  10843. + }
  10844. + dma->srck = i; dma->srcu = u;
  10845. +
  10846. + i = dma->cmdk; u = dma->cmdu;
  10847. + while (u != 0) {
  10848. + HIFN_CMDR_SYNC(sc, i,
  10849. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  10850. + if (dma->cmdr[i].l & htole32(HIFN_D_VALID)) {
  10851. + HIFN_CMDR_SYNC(sc, i,
  10852. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  10853. + break;
  10854. + }
  10855. + if (i != HIFN_D_CMD_RSIZE) {
  10856. + u--;
  10857. + HIFN_CMD_SYNC(sc, i, BUS_DMASYNC_POSTWRITE);
  10858. + }
  10859. + if (++i == (HIFN_D_CMD_RSIZE + 1))
  10860. + i = 0;
  10861. + }
  10862. + dma->cmdk = i; dma->cmdu = u;
  10863. +
  10864. + HIFN_UNLOCK(sc);
  10865. +
  10866. + if (sc->sc_needwakeup) { /* XXX check high watermark */
  10867. + int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
  10868. +#ifdef HIFN_DEBUG
  10869. + if (hifn_debug)
  10870. + device_printf(sc->sc_dev,
  10871. + "wakeup crypto (%x) u %d/%d/%d/%d\n",
  10872. + sc->sc_needwakeup,
  10873. + dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  10874. +#endif
  10875. + sc->sc_needwakeup &= ~wakeup;
  10876. + crypto_unblock(sc->sc_cid, wakeup);
  10877. + }
  10878. +
  10879. + return IRQ_HANDLED;
  10880. +}
  10881. +
  10882. +/*
  10883. + * Allocate a new 'session' and return an encoded session id. 'sidp'
  10884. + * contains our registration id, and should contain an encoded session
  10885. + * id on successful allocation.
  10886. + */
  10887. +static int
  10888. +hifn_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  10889. +{
  10890. + struct hifn_softc *sc = device_get_softc(dev);
  10891. + struct cryptoini *c;
  10892. + int mac = 0, cry = 0, sesn;
  10893. + struct hifn_session *ses = NULL;
  10894. + unsigned long l_flags;
  10895. +
  10896. + DPRINTF("%s()\n", __FUNCTION__);
  10897. +
  10898. + KASSERT(sc != NULL, ("hifn_newsession: null softc"));
  10899. + if (sidp == NULL || cri == NULL || sc == NULL) {
  10900. + DPRINTF("%s,%d: %s - EINVAL\n", __FILE__, __LINE__, __FUNCTION__);
  10901. + return (EINVAL);
  10902. + }
  10903. +
  10904. + HIFN_LOCK(sc);
  10905. + if (sc->sc_sessions == NULL) {
  10906. + ses = sc->sc_sessions = (struct hifn_session *)kmalloc(sizeof(*ses),
  10907. + SLAB_ATOMIC);
  10908. + if (ses == NULL) {
  10909. + HIFN_UNLOCK(sc);
  10910. + return (ENOMEM);
  10911. + }
  10912. + sesn = 0;
  10913. + sc->sc_nsessions = 1;
  10914. + } else {
  10915. + for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  10916. + if (!sc->sc_sessions[sesn].hs_used) {
  10917. + ses = &sc->sc_sessions[sesn];
  10918. + break;
  10919. + }
  10920. + }
  10921. +
  10922. + if (ses == NULL) {
  10923. + sesn = sc->sc_nsessions;
  10924. + ses = (struct hifn_session *)kmalloc((sesn + 1) * sizeof(*ses),
  10925. + SLAB_ATOMIC);
  10926. + if (ses == NULL) {
  10927. + HIFN_UNLOCK(sc);
  10928. + return (ENOMEM);
  10929. + }
  10930. + bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
  10931. + bzero(sc->sc_sessions, sesn * sizeof(*ses));
  10932. + kfree(sc->sc_sessions);
  10933. + sc->sc_sessions = ses;
  10934. + ses = &sc->sc_sessions[sesn];
  10935. + sc->sc_nsessions++;
  10936. + }
  10937. + }
  10938. + HIFN_UNLOCK(sc);
  10939. +
  10940. + bzero(ses, sizeof(*ses));
  10941. + ses->hs_used = 1;
  10942. +
  10943. + for (c = cri; c != NULL; c = c->cri_next) {
  10944. + switch (c->cri_alg) {
  10945. + case CRYPTO_MD5:
  10946. + case CRYPTO_SHA1:
  10947. + case CRYPTO_MD5_HMAC:
  10948. + case CRYPTO_SHA1_HMAC:
  10949. + if (mac) {
  10950. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  10951. + return (EINVAL);
  10952. + }
  10953. + mac = 1;
  10954. + ses->hs_mlen = c->cri_mlen;
  10955. + if (ses->hs_mlen == 0) {
  10956. + switch (c->cri_alg) {
  10957. + case CRYPTO_MD5:
  10958. + case CRYPTO_MD5_HMAC:
  10959. + ses->hs_mlen = 16;
  10960. + break;
  10961. + case CRYPTO_SHA1:
  10962. + case CRYPTO_SHA1_HMAC:
  10963. + ses->hs_mlen = 20;
  10964. + break;
  10965. + }
  10966. + }
  10967. + break;
  10968. + case CRYPTO_DES_CBC:
  10969. + case CRYPTO_3DES_CBC:
  10970. + case CRYPTO_AES_CBC:
  10971. + /* XXX this may read fewer, does it matter? */
  10972. + read_random(ses->hs_iv,
  10973. + c->cri_alg == CRYPTO_AES_CBC ?
  10974. + HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
  10975. + /*FALLTHROUGH*/
  10976. + case CRYPTO_ARC4:
  10977. + if (cry) {
  10978. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  10979. + return (EINVAL);
  10980. + }
  10981. + cry = 1;
  10982. + break;
  10983. + default:
  10984. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  10985. + return (EINVAL);
  10986. + }
  10987. + }
  10988. + if (mac == 0 && cry == 0) {
  10989. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  10990. + return (EINVAL);
  10991. + }
  10992. +
  10993. + *sidp = HIFN_SID(device_get_unit(sc->sc_dev), sesn);
  10994. +
  10995. + return (0);
  10996. +}
  10997. +
  10998. +/*
  10999. + * Deallocate a session.
  11000. + * XXX this routine should run a zero'd mac/encrypt key into context ram.
  11001. + * XXX to blow away any keys already stored there.
  11002. + */
  11003. +static int
  11004. +hifn_freesession(device_t dev, u_int64_t tid)
  11005. +{
  11006. + struct hifn_softc *sc = device_get_softc(dev);
  11007. + int session, error;
  11008. + u_int32_t sid = CRYPTO_SESID2LID(tid);
  11009. + unsigned long l_flags;
  11010. +
  11011. + DPRINTF("%s()\n", __FUNCTION__);
  11012. +
  11013. + KASSERT(sc != NULL, ("hifn_freesession: null softc"));
  11014. + if (sc == NULL) {
  11015. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11016. + return (EINVAL);
  11017. + }
  11018. +
  11019. + HIFN_LOCK(sc);
  11020. + session = HIFN_SESSION(sid);
  11021. + if (session < sc->sc_nsessions) {
  11022. + bzero(&sc->sc_sessions[session], sizeof(struct hifn_session));
  11023. + error = 0;
  11024. + } else {
  11025. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11026. + error = EINVAL;
  11027. + }
  11028. + HIFN_UNLOCK(sc);
  11029. +
  11030. + return (error);
  11031. +}
  11032. +
  11033. +static int
  11034. +hifn_process(device_t dev, struct cryptop *crp, int hint)
  11035. +{
  11036. + struct hifn_softc *sc = device_get_softc(dev);
  11037. + struct hifn_command *cmd = NULL;
  11038. + int session, err, ivlen;
  11039. + struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  11040. +
  11041. + DPRINTF("%s()\n", __FUNCTION__);
  11042. +
  11043. + if (crp == NULL || crp->crp_callback == NULL) {
  11044. + hifnstats.hst_invalid++;
  11045. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11046. + return (EINVAL);
  11047. + }
  11048. + session = HIFN_SESSION(crp->crp_sid);
  11049. +
  11050. + if (sc == NULL || session >= sc->sc_nsessions) {
  11051. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11052. + err = EINVAL;
  11053. + goto errout;
  11054. + }
  11055. +
  11056. + cmd = kmalloc(sizeof(struct hifn_command), SLAB_ATOMIC);
  11057. + if (cmd == NULL) {
  11058. + hifnstats.hst_nomem++;
  11059. + err = ENOMEM;
  11060. + goto errout;
  11061. + }
  11062. + memset(cmd, 0, sizeof(*cmd));
  11063. +
  11064. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  11065. + cmd->src_skb = (struct sk_buff *)crp->crp_buf;
  11066. + cmd->dst_skb = (struct sk_buff *)crp->crp_buf;
  11067. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  11068. + cmd->src_io = (struct uio *)crp->crp_buf;
  11069. + cmd->dst_io = (struct uio *)crp->crp_buf;
  11070. + } else {
  11071. + cmd->src_buf = crp->crp_buf;
  11072. + cmd->dst_buf = crp->crp_buf;
  11073. + }
  11074. +
  11075. + crd1 = crp->crp_desc;
  11076. + if (crd1 == NULL) {
  11077. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11078. + err = EINVAL;
  11079. + goto errout;
  11080. + }
  11081. + crd2 = crd1->crd_next;
  11082. +
  11083. + if (crd2 == NULL) {
  11084. + if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
  11085. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  11086. + crd1->crd_alg == CRYPTO_SHA1 ||
  11087. + crd1->crd_alg == CRYPTO_MD5) {
  11088. + maccrd = crd1;
  11089. + enccrd = NULL;
  11090. + } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
  11091. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  11092. + crd1->crd_alg == CRYPTO_AES_CBC ||
  11093. + crd1->crd_alg == CRYPTO_ARC4) {
  11094. + if ((crd1->crd_flags & CRD_F_ENCRYPT) == 0)
  11095. + cmd->base_masks |= HIFN_BASE_CMD_DECODE;
  11096. + maccrd = NULL;
  11097. + enccrd = crd1;
  11098. + } else {
  11099. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11100. + err = EINVAL;
  11101. + goto errout;
  11102. + }
  11103. + } else {
  11104. + if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
  11105. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  11106. + crd1->crd_alg == CRYPTO_MD5 ||
  11107. + crd1->crd_alg == CRYPTO_SHA1) &&
  11108. + (crd2->crd_alg == CRYPTO_DES_CBC ||
  11109. + crd2->crd_alg == CRYPTO_3DES_CBC ||
  11110. + crd2->crd_alg == CRYPTO_AES_CBC ||
  11111. + crd2->crd_alg == CRYPTO_ARC4) &&
  11112. + ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
  11113. + cmd->base_masks = HIFN_BASE_CMD_DECODE;
  11114. + maccrd = crd1;
  11115. + enccrd = crd2;
  11116. + } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
  11117. + crd1->crd_alg == CRYPTO_ARC4 ||
  11118. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  11119. + crd1->crd_alg == CRYPTO_AES_CBC) &&
  11120. + (crd2->crd_alg == CRYPTO_MD5_HMAC ||
  11121. + crd2->crd_alg == CRYPTO_SHA1_HMAC ||
  11122. + crd2->crd_alg == CRYPTO_MD5 ||
  11123. + crd2->crd_alg == CRYPTO_SHA1) &&
  11124. + (crd1->crd_flags & CRD_F_ENCRYPT)) {
  11125. + enccrd = crd1;
  11126. + maccrd = crd2;
  11127. + } else {
  11128. + /*
  11129. + * We cannot order the 7751 as requested
  11130. + */
  11131. + DPRINTF("%s,%d: %s %d,%d,%d - EINVAL\n",__FILE__,__LINE__,__FUNCTION__, crd1->crd_alg, crd2->crd_alg, crd1->crd_flags & CRD_F_ENCRYPT);
  11132. + err = EINVAL;
  11133. + goto errout;
  11134. + }
  11135. + }
  11136. +
  11137. + if (enccrd) {
  11138. + cmd->enccrd = enccrd;
  11139. + cmd->base_masks |= HIFN_BASE_CMD_CRYPT;
  11140. + switch (enccrd->crd_alg) {
  11141. + case CRYPTO_ARC4:
  11142. + cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_RC4;
  11143. + break;
  11144. + case CRYPTO_DES_CBC:
  11145. + cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_DES |
  11146. + HIFN_CRYPT_CMD_MODE_CBC |
  11147. + HIFN_CRYPT_CMD_NEW_IV;
  11148. + break;
  11149. + case CRYPTO_3DES_CBC:
  11150. + cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_3DES |
  11151. + HIFN_CRYPT_CMD_MODE_CBC |
  11152. + HIFN_CRYPT_CMD_NEW_IV;
  11153. + break;
  11154. + case CRYPTO_AES_CBC:
  11155. + cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_AES |
  11156. + HIFN_CRYPT_CMD_MODE_CBC |
  11157. + HIFN_CRYPT_CMD_NEW_IV;
  11158. + break;
  11159. + default:
  11160. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11161. + err = EINVAL;
  11162. + goto errout;
  11163. + }
  11164. + if (enccrd->crd_alg != CRYPTO_ARC4) {
  11165. + ivlen = ((enccrd->crd_alg == CRYPTO_AES_CBC) ?
  11166. + HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
  11167. + if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  11168. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  11169. + bcopy(enccrd->crd_iv, cmd->iv, ivlen);
  11170. + else
  11171. + bcopy(sc->sc_sessions[session].hs_iv,
  11172. + cmd->iv, ivlen);
  11173. +
  11174. + if ((enccrd->crd_flags & CRD_F_IV_PRESENT)
  11175. + == 0) {
  11176. + crypto_copyback(crp->crp_flags,
  11177. + crp->crp_buf, enccrd->crd_inject,
  11178. + ivlen, cmd->iv);
  11179. + }
  11180. + } else {
  11181. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  11182. + bcopy(enccrd->crd_iv, cmd->iv, ivlen);
  11183. + else {
  11184. + crypto_copydata(crp->crp_flags,
  11185. + crp->crp_buf, enccrd->crd_inject,
  11186. + ivlen, cmd->iv);
  11187. + }
  11188. + }
  11189. + }
  11190. +
  11191. + if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
  11192. + cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
  11193. + cmd->ck = enccrd->crd_key;
  11194. + cmd->cklen = enccrd->crd_klen >> 3;
  11195. + cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
  11196. +
  11197. + /*
  11198. + * Need to specify the size for the AES key in the masks.
  11199. + */
  11200. + if ((cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) ==
  11201. + HIFN_CRYPT_CMD_ALG_AES) {
  11202. + switch (cmd->cklen) {
  11203. + case 16:
  11204. + cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_128;
  11205. + break;
  11206. + case 24:
  11207. + cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_192;
  11208. + break;
  11209. + case 32:
  11210. + cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_256;
  11211. + break;
  11212. + default:
  11213. + DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  11214. + err = EINVAL;
  11215. + goto errout;
  11216. + }
  11217. + }
  11218. + }
  11219. +
  11220. + if (maccrd) {
  11221. + cmd->maccrd = maccrd;
  11222. + cmd->base_masks |= HIFN_BASE_CMD_MAC;
  11223. +
  11224. + switch (maccrd->crd_alg) {
  11225. + case CRYPTO_MD5:
  11226. + cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
  11227. + HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
  11228. + HIFN_MAC_CMD_POS_IPSEC;
  11229. + break;
  11230. + case CRYPTO_MD5_HMAC:
  11231. + cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
  11232. + HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
  11233. + HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
  11234. + break;
  11235. + case CRYPTO_SHA1:
  11236. + cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
  11237. + HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
  11238. + HIFN_MAC_CMD_POS_IPSEC;
  11239. + break;
  11240. + case CRYPTO_SHA1_HMAC:
  11241. + cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
  11242. + HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
  11243. + HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
  11244. + break;
  11245. + }
  11246. +
  11247. + if (maccrd->crd_alg == CRYPTO_SHA1_HMAC ||
  11248. + maccrd->crd_alg == CRYPTO_MD5_HMAC) {
  11249. + cmd->mac_masks |= HIFN_MAC_CMD_NEW_KEY;
  11250. + bcopy(maccrd->crd_key, cmd->mac, maccrd->crd_klen >> 3);
  11251. + bzero(cmd->mac + (maccrd->crd_klen >> 3),
  11252. + HIFN_MAC_KEY_LENGTH - (maccrd->crd_klen >> 3));
  11253. + }
  11254. + }
  11255. +
  11256. + cmd->crp = crp;
  11257. + cmd->session_num = session;
  11258. + cmd->softc = sc;
  11259. +
  11260. + err = hifn_crypto(sc, cmd, crp, hint);
  11261. + if (!err) {
  11262. + return 0;
  11263. + } else if (err == ERESTART) {
  11264. + /*
  11265. + * There weren't enough resources to dispatch the request
  11266. + * to the part. Notify the caller so they'll requeue this
  11267. + * request and resubmit it again soon.
  11268. + */
  11269. +#ifdef HIFN_DEBUG
  11270. + if (hifn_debug)
  11271. + device_printf(sc->sc_dev, "requeue request\n");
  11272. +#endif
  11273. + kfree(cmd);
  11274. + sc->sc_needwakeup |= CRYPTO_SYMQ;
  11275. + return (err);
  11276. + }
  11277. +
  11278. +errout:
  11279. + if (cmd != NULL)
  11280. + kfree(cmd);
  11281. + if (err == EINVAL)
  11282. + hifnstats.hst_invalid++;
  11283. + else
  11284. + hifnstats.hst_nomem++;
  11285. + crp->crp_etype = err;
  11286. + crypto_done(crp);
  11287. + return (err);
  11288. +}
  11289. +
  11290. +static void
  11291. +hifn_abort(struct hifn_softc *sc)
  11292. +{
  11293. + struct hifn_dma *dma = sc->sc_dma;
  11294. + struct hifn_command *cmd;
  11295. + struct cryptop *crp;
  11296. + int i, u;
  11297. +
  11298. + DPRINTF("%s()\n", __FUNCTION__);
  11299. +
  11300. + i = dma->resk; u = dma->resu;
  11301. + while (u != 0) {
  11302. + cmd = dma->hifn_commands[i];
  11303. + KASSERT(cmd != NULL, ("hifn_abort: null command slot %u", i));
  11304. + dma->hifn_commands[i] = NULL;
  11305. + crp = cmd->crp;
  11306. +
  11307. + if ((dma->resr[i].l & htole32(HIFN_D_VALID)) == 0) {
  11308. + /* Salvage what we can. */
  11309. + u_int8_t *macbuf;
  11310. +
  11311. + if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
  11312. + macbuf = dma->result_bufs[i];
  11313. + macbuf += 12;
  11314. + } else
  11315. + macbuf = NULL;
  11316. + hifnstats.hst_opackets++;
  11317. + hifn_callback(sc, cmd, macbuf);
  11318. + } else {
  11319. +#if 0
  11320. + if (cmd->src_map == cmd->dst_map) {
  11321. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  11322. + BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
  11323. + } else {
  11324. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  11325. + BUS_DMASYNC_POSTWRITE);
  11326. + bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  11327. + BUS_DMASYNC_POSTREAD);
  11328. + }
  11329. +#endif
  11330. +
  11331. + if (cmd->src_skb != cmd->dst_skb) {
  11332. +#ifdef NOTYET
  11333. + m_freem(cmd->src_m);
  11334. + crp->crp_buf = (caddr_t)cmd->dst_m;
  11335. +#else
  11336. + device_printf(sc->sc_dev,
  11337. + "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  11338. + __FILE__, __LINE__);
  11339. +#endif
  11340. + }
  11341. +
  11342. + /* non-shared buffers cannot be restarted */
  11343. + if (cmd->src_map != cmd->dst_map) {
  11344. + /*
  11345. + * XXX should be EAGAIN, delayed until
  11346. + * after the reset.
  11347. + */
  11348. + crp->crp_etype = ENOMEM;
  11349. + pci_unmap_buf(sc, &cmd->dst);
  11350. + } else
  11351. + crp->crp_etype = ENOMEM;
  11352. +
  11353. + pci_unmap_buf(sc, &cmd->src);
  11354. +
  11355. + kfree(cmd);
  11356. + if (crp->crp_etype != EAGAIN)
  11357. + crypto_done(crp);
  11358. + }
  11359. +
  11360. + if (++i == HIFN_D_RES_RSIZE)
  11361. + i = 0;
  11362. + u--;
  11363. + }
  11364. + dma->resk = i; dma->resu = u;
  11365. +
  11366. + hifn_reset_board(sc, 1);
  11367. + hifn_init_dma(sc);
  11368. + hifn_init_pci_registers(sc);
  11369. +}
  11370. +
  11371. +static void
  11372. +hifn_callback(struct hifn_softc *sc, struct hifn_command *cmd, u_int8_t *macbuf)
  11373. +{
  11374. + struct hifn_dma *dma = sc->sc_dma;
  11375. + struct cryptop *crp = cmd->crp;
  11376. + struct cryptodesc *crd;
  11377. + int i, u, ivlen;
  11378. +
  11379. + DPRINTF("%s()\n", __FUNCTION__);
  11380. +
  11381. +#if 0
  11382. + if (cmd->src_map == cmd->dst_map) {
  11383. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  11384. + BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
  11385. + } else {
  11386. + bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  11387. + BUS_DMASYNC_POSTWRITE);
  11388. + bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  11389. + BUS_DMASYNC_POSTREAD);
  11390. + }
  11391. +#endif
  11392. +
  11393. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  11394. + if (cmd->src_skb != cmd->dst_skb) {
  11395. +#ifdef NOTYET
  11396. + crp->crp_buf = (caddr_t)cmd->dst_m;
  11397. + totlen = cmd->src_mapsize;
  11398. + for (m = cmd->dst_m; m != NULL; m = m->m_next) {
  11399. + if (totlen < m->m_len) {
  11400. + m->m_len = totlen;
  11401. + totlen = 0;
  11402. + } else
  11403. + totlen -= m->m_len;
  11404. + }
  11405. + cmd->dst_m->m_pkthdr.len = cmd->src_m->m_pkthdr.len;
  11406. + m_freem(cmd->src_m);
  11407. +#else
  11408. + device_printf(sc->sc_dev,
  11409. + "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  11410. + __FILE__, __LINE__);
  11411. +#endif
  11412. + }
  11413. + }
  11414. +
  11415. + if (cmd->sloplen != 0) {
  11416. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  11417. + cmd->src_mapsize - cmd->sloplen, cmd->sloplen,
  11418. + (caddr_t)&dma->slop[cmd->slopidx]);
  11419. + }
  11420. +
  11421. + i = dma->dstk; u = dma->dstu;
  11422. + while (u != 0) {
  11423. + if (i == HIFN_D_DST_RSIZE)
  11424. + i = 0;
  11425. +#if 0
  11426. + bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
  11427. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  11428. +#endif
  11429. + if (dma->dstr[i].l & htole32(HIFN_D_VALID)) {
  11430. +#if 0
  11431. + bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
  11432. + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  11433. +#endif
  11434. + break;
  11435. + }
  11436. + i++, u--;
  11437. + }
  11438. + dma->dstk = i; dma->dstu = u;
  11439. +
  11440. + hifnstats.hst_obytes += cmd->dst_mapsize;
  11441. +
  11442. + if ((cmd->base_masks & (HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE)) ==
  11443. + HIFN_BASE_CMD_CRYPT) {
  11444. + for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  11445. + if (crd->crd_alg != CRYPTO_DES_CBC &&
  11446. + crd->crd_alg != CRYPTO_3DES_CBC &&
  11447. + crd->crd_alg != CRYPTO_AES_CBC)
  11448. + continue;
  11449. + ivlen = ((crd->crd_alg == CRYPTO_AES_CBC) ?
  11450. + HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
  11451. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  11452. + crd->crd_skip + crd->crd_len - ivlen, ivlen,
  11453. + cmd->softc->sc_sessions[cmd->session_num].hs_iv);
  11454. + break;
  11455. + }
  11456. + }
  11457. +
  11458. + if (macbuf != NULL) {
  11459. + for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  11460. + int len;
  11461. +
  11462. + if (crd->crd_alg != CRYPTO_MD5 &&
  11463. + crd->crd_alg != CRYPTO_SHA1 &&
  11464. + crd->crd_alg != CRYPTO_MD5_HMAC &&
  11465. + crd->crd_alg != CRYPTO_SHA1_HMAC) {
  11466. + continue;
  11467. + }
  11468. + len = cmd->softc->sc_sessions[cmd->session_num].hs_mlen;
  11469. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  11470. + crd->crd_inject, len, macbuf);
  11471. + break;
  11472. + }
  11473. + }
  11474. +
  11475. + if (cmd->src_map != cmd->dst_map)
  11476. + pci_unmap_buf(sc, &cmd->dst);
  11477. + pci_unmap_buf(sc, &cmd->src);
  11478. + kfree(cmd);
  11479. + crypto_done(crp);
  11480. +}
  11481. +
  11482. +/*
  11483. + * 7811 PB3 rev/2 parts lock-up on burst writes to Group 0
  11484. + * and Group 1 registers; avoid conditions that could create
  11485. + * burst writes by doing a read in between the writes.
  11486. + *
  11487. + * NB: The read we interpose is always to the same register;
  11488. + * we do this because reading from an arbitrary (e.g. last)
  11489. + * register may not always work.
  11490. + */
  11491. +static void
  11492. +hifn_write_reg_0(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
  11493. +{
  11494. + if (sc->sc_flags & HIFN_IS_7811) {
  11495. + if (sc->sc_bar0_lastreg == reg - 4)
  11496. + readl(sc->sc_bar0 + HIFN_0_PUCNFG);
  11497. + sc->sc_bar0_lastreg = reg;
  11498. + }
  11499. + writel(val, sc->sc_bar0 + reg);
  11500. +}
  11501. +
  11502. +static void
  11503. +hifn_write_reg_1(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
  11504. +{
  11505. + if (sc->sc_flags & HIFN_IS_7811) {
  11506. + if (sc->sc_bar1_lastreg == reg - 4)
  11507. + readl(sc->sc_bar1 + HIFN_1_REVID);
  11508. + sc->sc_bar1_lastreg = reg;
  11509. + }
  11510. + writel(val, sc->sc_bar1 + reg);
  11511. +}
  11512. +
  11513. +
  11514. +static struct pci_device_id hifn_pci_tbl[] = {
  11515. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7951,
  11516. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11517. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7955,
  11518. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11519. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7956,
  11520. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11521. + { PCI_VENDOR_NETSEC, PCI_PRODUCT_NETSEC_7751,
  11522. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11523. + { PCI_VENDOR_INVERTEX, PCI_PRODUCT_INVERTEX_AEON,
  11524. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11525. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7811,
  11526. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11527. + /*
  11528. + * Other vendors share this PCI ID as well, such as
  11529. + * http://www.powercrypt.com, and obviously they also
  11530. + * use the same key.
  11531. + */
  11532. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7751,
  11533. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  11534. + { 0, 0, 0, 0, 0, 0, }
  11535. +};
  11536. +MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
  11537. +
  11538. +static struct pci_driver hifn_driver = {
  11539. + .name = "hifn",
  11540. + .id_table = hifn_pci_tbl,
  11541. + .probe = hifn_probe,
  11542. + .remove = hifn_remove,
  11543. + /* add PM stuff here one day */
  11544. +};
  11545. +
  11546. +static int __init hifn_init (void)
  11547. +{
  11548. + struct hifn_softc *sc = NULL;
  11549. + int rc;
  11550. +
  11551. + DPRINTF("%s(%p)\n", __FUNCTION__, hifn_init);
  11552. +
  11553. + rc = pci_register_driver(&hifn_driver);
  11554. + pci_register_driver_compat(&hifn_driver, rc);
  11555. +
  11556. + return rc;
  11557. +}
  11558. +
  11559. +static void __exit hifn_exit (void)
  11560. +{
  11561. + pci_unregister_driver(&hifn_driver);
  11562. +}
  11563. +
  11564. +module_init(hifn_init);
  11565. +module_exit(hifn_exit);
  11566. +
  11567. +MODULE_LICENSE("BSD");
  11568. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  11569. +MODULE_DESCRIPTION("OCF driver for hifn PCI crypto devices");
  11570. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifn7751reg.h linux-2.6.30/crypto/ocf/hifn/hifn7751reg.h
  11571. --- linux-2.6.30.orig/crypto/ocf/hifn/hifn7751reg.h 1970-01-01 01:00:00.000000000 +0100
  11572. +++ linux-2.6.30/crypto/ocf/hifn/hifn7751reg.h 2009-06-11 10:55:27.000000000 +0200
  11573. @@ -0,0 +1,540 @@
  11574. +/* $FreeBSD: src/sys/dev/hifn/hifn7751reg.h,v 1.7 2007/03/21 03:42:49 sam Exp $ */
  11575. +/* $OpenBSD: hifn7751reg.h,v 1.35 2002/04/08 17:49:42 jason Exp $ */
  11576. +
  11577. +/*-
  11578. + * Invertex AEON / Hifn 7751 driver
  11579. + * Copyright (c) 1999 Invertex Inc. All rights reserved.
  11580. + * Copyright (c) 1999 Theo de Raadt
  11581. + * Copyright (c) 2000-2001 Network Security Technologies, Inc.
  11582. + * http://www.netsec.net
  11583. + *
  11584. + * Please send any comments, feedback, bug-fixes, or feature requests to
  11585. + * software@invertex.com.
  11586. + *
  11587. + * Redistribution and use in source and binary forms, with or without
  11588. + * modification, are permitted provided that the following conditions
  11589. + * are met:
  11590. + *
  11591. + * 1. Redistributions of source code must retain the above copyright
  11592. + * notice, this list of conditions and the following disclaimer.
  11593. + * 2. Redistributions in binary form must reproduce the above copyright
  11594. + * notice, this list of conditions and the following disclaimer in the
  11595. + * documentation and/or other materials provided with the distribution.
  11596. + * 3. The name of the author may not be used to endorse or promote products
  11597. + * derived from this software without specific prior written permission.
  11598. + *
  11599. + *
  11600. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  11601. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  11602. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  11603. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  11604. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  11605. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11606. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11607. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  11608. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  11609. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11610. + *
  11611. + * Effort sponsored in part by the Defense Advanced Research Projects
  11612. + * Agency (DARPA) and Air Force Research Laboratory, Air Force
  11613. + * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  11614. + *
  11615. + */
  11616. +#ifndef __HIFN_H__
  11617. +#define __HIFN_H__
  11618. +
  11619. +/*
  11620. + * Some PCI configuration space offset defines. The names were made
  11621. + * identical to the names used by the Linux kernel.
  11622. + */
  11623. +#define HIFN_BAR0 PCIR_BAR(0) /* PUC register map */
  11624. +#define HIFN_BAR1 PCIR_BAR(1) /* DMA register map */
  11625. +#define HIFN_TRDY_TIMEOUT 0x40
  11626. +#define HIFN_RETRY_TIMEOUT 0x41
  11627. +
  11628. +/*
  11629. + * PCI vendor and device identifiers
  11630. + * (the names are preserved from their OpenBSD source).
  11631. + */
  11632. +#define PCI_VENDOR_HIFN 0x13a3 /* Hifn */
  11633. +#define PCI_PRODUCT_HIFN_7751 0x0005 /* 7751 */
  11634. +#define PCI_PRODUCT_HIFN_6500 0x0006 /* 6500 */
  11635. +#define PCI_PRODUCT_HIFN_7811 0x0007 /* 7811 */
  11636. +#define PCI_PRODUCT_HIFN_7855 0x001f /* 7855 */
  11637. +#define PCI_PRODUCT_HIFN_7951 0x0012 /* 7951 */
  11638. +#define PCI_PRODUCT_HIFN_7955 0x0020 /* 7954/7955 */
  11639. +#define PCI_PRODUCT_HIFN_7956 0x001d /* 7956 */
  11640. +
  11641. +#define PCI_VENDOR_INVERTEX 0x14e1 /* Invertex */
  11642. +#define PCI_PRODUCT_INVERTEX_AEON 0x0005 /* AEON */
  11643. +
  11644. +#define PCI_VENDOR_NETSEC 0x1660 /* NetSec */
  11645. +#define PCI_PRODUCT_NETSEC_7751 0x7751 /* 7751 */
  11646. +
  11647. +/*
  11648. + * The values below should multiple of 4 -- and be large enough to handle
  11649. + * any command the driver implements.
  11650. + *
  11651. + * MAX_COMMAND = base command + mac command + encrypt command +
  11652. + * mac-key + rc4-key
  11653. + * MAX_RESULT = base result + mac result + mac + encrypt result
  11654. + *
  11655. + *
  11656. + */
  11657. +#define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
  11658. +#define HIFN_MAX_RESULT (8 + 4 + 20 + 4)
  11659. +
  11660. +/*
  11661. + * hifn_desc_t
  11662. + *
  11663. + * Holds an individual descriptor for any of the rings.
  11664. + */
  11665. +typedef struct hifn_desc {
  11666. + volatile u_int32_t l; /* length and status bits */
  11667. + volatile u_int32_t p;
  11668. +} hifn_desc_t;
  11669. +
  11670. +/*
  11671. + * Masks for the "length" field of struct hifn_desc.
  11672. + */
  11673. +#define HIFN_D_LENGTH 0x0000ffff /* length bit mask */
  11674. +#define HIFN_D_MASKDONEIRQ 0x02000000 /* mask the done interrupt */
  11675. +#define HIFN_D_DESTOVER 0x04000000 /* destination overflow */
  11676. +#define HIFN_D_OVER 0x08000000 /* overflow */
  11677. +#define HIFN_D_LAST 0x20000000 /* last descriptor in chain */
  11678. +#define HIFN_D_JUMP 0x40000000 /* jump descriptor */
  11679. +#define HIFN_D_VALID 0x80000000 /* valid bit */
  11680. +
  11681. +
  11682. +/*
  11683. + * Processing Unit Registers (offset from BASEREG0)
  11684. + */
  11685. +#define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
  11686. +#define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
  11687. +#define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
  11688. +#define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
  11689. +#define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
  11690. +#define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
  11691. +#define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
  11692. +#define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
  11693. +#define HIFN_0_PUCTRL2 0x28 /* Processing Unit Control (2nd map) */
  11694. +#define HIFN_0_MUTE1 0x80
  11695. +#define HIFN_0_MUTE2 0x90
  11696. +#define HIFN_0_SPACESIZE 0x100 /* Register space size */
  11697. +
  11698. +/* Processing Unit Control Register (HIFN_0_PUCTRL) */
  11699. +#define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
  11700. +#define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
  11701. +#define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
  11702. +#define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
  11703. +#define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
  11704. +
  11705. +/* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
  11706. +#define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
  11707. +#define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
  11708. +#define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  11709. +#define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  11710. +#define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
  11711. +#define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
  11712. +#define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
  11713. +#define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
  11714. +#define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
  11715. +#define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
  11716. +
  11717. +/* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
  11718. +#define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
  11719. +#define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
  11720. +#define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
  11721. +#define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
  11722. +#define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
  11723. +#define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
  11724. +#define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
  11725. +#define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
  11726. +#define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
  11727. +#define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
  11728. +#define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
  11729. +#define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
  11730. +#define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
  11731. +#define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
  11732. +#define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
  11733. +#define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
  11734. +#define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
  11735. +#define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
  11736. +#define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
  11737. +#define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
  11738. +#define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
  11739. +#define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
  11740. +#define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
  11741. +
  11742. +/* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
  11743. +#define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
  11744. +#define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
  11745. +#define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  11746. +#define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  11747. +#define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
  11748. +#define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
  11749. +#define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
  11750. +#define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
  11751. +#define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
  11752. +#define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
  11753. +
  11754. +/* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
  11755. +#define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
  11756. +#define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
  11757. +#define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  11758. +#define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  11759. +#define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
  11760. +#define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
  11761. +#define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
  11762. +#define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
  11763. +#define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
  11764. +#define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
  11765. +#define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
  11766. +#define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
  11767. +#define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
  11768. +#define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
  11769. +#define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
  11770. +#define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
  11771. +#define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
  11772. +
  11773. +/* FIFO Status Register (HIFN_0_FIFOSTAT) */
  11774. +#define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
  11775. +#define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
  11776. +
  11777. +/* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
  11778. +#define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as this value */
  11779. +
  11780. +/*
  11781. + * DMA Interface Registers (offset from BASEREG1)
  11782. + */
  11783. +#define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
  11784. +#define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
  11785. +#define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
  11786. +#define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
  11787. +#define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
  11788. +#define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
  11789. +#define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
  11790. +#define HIFN_1_PLL 0x4c /* 7955/7956: PLL config */
  11791. +#define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
  11792. +#define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
  11793. +#define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
  11794. +#define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
  11795. +#define HIFN_1_DMA_CNFG2 0x6c /* 7955/7956: dma config #2 */
  11796. +#define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
  11797. +#define HIFN_1_REVID 0x98 /* Revision ID */
  11798. +
  11799. +#define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
  11800. +#define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
  11801. +#define HIFN_1_PUB_OPLEN 0x304 /* 7951-compat Public Operand Length */
  11802. +#define HIFN_1_PUB_OP 0x308 /* 7951-compat Public Operand */
  11803. +#define HIFN_1_PUB_STATUS 0x30c /* 7951-compat Public Status */
  11804. +#define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
  11805. +#define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
  11806. +#define HIFN_1_RNG_DATA 0x318 /* RNG data */
  11807. +#define HIFN_1_PUB_MODE 0x320 /* PK mode */
  11808. +#define HIFN_1_PUB_FIFO_OPLEN 0x380 /* first element of oplen fifo */
  11809. +#define HIFN_1_PUB_FIFO_OP 0x384 /* first element of op fifo */
  11810. +#define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
  11811. +#define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
  11812. +
  11813. +/* DMA Status and Control Register (HIFN_1_DMA_CSR) */
  11814. +#define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
  11815. +#define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
  11816. +#define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
  11817. +#define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
  11818. +#define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
  11819. +#define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
  11820. +#define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
  11821. +#define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
  11822. +#define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
  11823. +#define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
  11824. +#define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
  11825. +#define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
  11826. +#define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
  11827. +#define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
  11828. +#define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
  11829. +#define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
  11830. +#define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
  11831. +#define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
  11832. +#define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
  11833. +#define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
  11834. +#define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
  11835. +#define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
  11836. +#define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
  11837. +#define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
  11838. +#define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
  11839. +#define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
  11840. +#define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
  11841. +#define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
  11842. +#define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
  11843. +#define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
  11844. +#define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
  11845. +#define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
  11846. +#define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
  11847. +#define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
  11848. +#define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
  11849. +#define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
  11850. +#define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
  11851. +#define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
  11852. +
  11853. +/* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
  11854. +#define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
  11855. +#define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
  11856. +#define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
  11857. +#define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
  11858. +#define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
  11859. +#define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
  11860. +#define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
  11861. +#define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
  11862. +#define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
  11863. +#define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
  11864. +#define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
  11865. +#define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
  11866. +#define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
  11867. +#define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
  11868. +#define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
  11869. +#define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
  11870. +#define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
  11871. +#define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
  11872. +#define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
  11873. +#define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
  11874. +#define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
  11875. +#define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
  11876. +
  11877. +/* DMA Configuration Register (HIFN_1_DMA_CNFG) */
  11878. +#define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
  11879. +#define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
  11880. +#define HIFN_DMACNFG_UNLOCK 0x00000800
  11881. +#define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
  11882. +#define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
  11883. +#define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
  11884. +#define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
  11885. +#define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
  11886. +
  11887. +/* DMA Configuration Register (HIFN_1_DMA_CNFG2) */
  11888. +#define HIFN_DMACNFG2_PKSWAP32 (1 << 19) /* swap the OPLEN/OP reg */
  11889. +#define HIFN_DMACNFG2_PKSWAP8 (1 << 18) /* swap the bits of OPLEN/OP */
  11890. +#define HIFN_DMACNFG2_BAR0_SWAP32 (1<<17) /* swap the bytes of BAR0 */
  11891. +#define HIFN_DMACNFG2_BAR1_SWAP8 (1<<16) /* swap the bits of BAR0 */
  11892. +#define HIFN_DMACNFG2_INIT_WRITE_BURST_SHIFT 12
  11893. +#define HIFN_DMACNFG2_INIT_READ_BURST_SHIFT 8
  11894. +#define HIFN_DMACNFG2_TGT_WRITE_BURST_SHIFT 4
  11895. +#define HIFN_DMACNFG2_TGT_READ_BURST_SHIFT 0
  11896. +
  11897. +/* 7811 RNG Enable Register (HIFN_1_7811_RNGENA) */
  11898. +#define HIFN_7811_RNGENA_ENA 0x00000001 /* enable RNG */
  11899. +
  11900. +/* 7811 RNG Config Register (HIFN_1_7811_RNGCFG) */
  11901. +#define HIFN_7811_RNGCFG_PRE1 0x00000f00 /* first prescalar */
  11902. +#define HIFN_7811_RNGCFG_OPRE 0x00000080 /* output prescalar */
  11903. +#define HIFN_7811_RNGCFG_DEFL 0x00000f80 /* 2 words/ 1/100 sec */
  11904. +
  11905. +/* 7811 RNG Status Register (HIFN_1_7811_RNGSTS) */
  11906. +#define HIFN_7811_RNGSTS_RDY 0x00004000 /* two numbers in FIFO */
  11907. +#define HIFN_7811_RNGSTS_UFL 0x00001000 /* rng underflow */
  11908. +
  11909. +/* 7811 MIPS Reset Register (HIFN_1_7811_MIPSRST) */
  11910. +#define HIFN_MIPSRST_BAR2SIZE 0xffff0000 /* sdram size */
  11911. +#define HIFN_MIPSRST_GPRAMINIT 0x00008000 /* gpram can be accessed */
  11912. +#define HIFN_MIPSRST_CRAMINIT 0x00004000 /* ctxram can be accessed */
  11913. +#define HIFN_MIPSRST_LED2 0x00000400 /* external LED2 */
  11914. +#define HIFN_MIPSRST_LED1 0x00000200 /* external LED1 */
  11915. +#define HIFN_MIPSRST_LED0 0x00000100 /* external LED0 */
  11916. +#define HIFN_MIPSRST_MIPSDIS 0x00000004 /* disable MIPS */
  11917. +#define HIFN_MIPSRST_MIPSRST 0x00000002 /* warm reset MIPS */
  11918. +#define HIFN_MIPSRST_MIPSCOLD 0x00000001 /* cold reset MIPS */
  11919. +
  11920. +/* Public key reset register (HIFN_1_PUB_RESET) */
  11921. +#define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
  11922. +
  11923. +/* Public operation register (HIFN_1_PUB_OP) */
  11924. +#define HIFN_PUBOP_AOFFSET 0x0000003e /* A offset */
  11925. +#define HIFN_PUBOP_BOFFSET 0x00000fc0 /* B offset */
  11926. +#define HIFN_PUBOP_MOFFSET 0x0003f000 /* M offset */
  11927. +#define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
  11928. +#define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
  11929. +#define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
  11930. +#define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
  11931. +#define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
  11932. +#define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
  11933. +#define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
  11934. +#define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
  11935. +#define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
  11936. +#define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
  11937. +#define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
  11938. +#define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
  11939. +#define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular Red */
  11940. +#define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular Exp */
  11941. +
  11942. +/* Public operand length register (HIFN_1_PUB_OPLEN) */
  11943. +#define HIFN_PUBOPLEN_MODLEN 0x0000007f
  11944. +#define HIFN_PUBOPLEN_EXPLEN 0x0003ff80
  11945. +#define HIFN_PUBOPLEN_REDLEN 0x003c0000
  11946. +
  11947. +/* Public status register (HIFN_1_PUB_STATUS) */
  11948. +#define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
  11949. +#define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
  11950. +#define HIFN_PUBSTS_FIFO_EMPTY 0x00000100 /* fifo empty */
  11951. +#define HIFN_PUBSTS_FIFO_FULL 0x00000200 /* fifo full */
  11952. +#define HIFN_PUBSTS_FIFO_OVFL 0x00000400 /* fifo overflow */
  11953. +#define HIFN_PUBSTS_FIFO_WRITE 0x000f0000 /* fifo write */
  11954. +#define HIFN_PUBSTS_FIFO_READ 0x0f000000 /* fifo read */
  11955. +
  11956. +/* Public interrupt enable register (HIFN_1_PUB_IEN) */
  11957. +#define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
  11958. +
  11959. +/* Random number generator config register (HIFN_1_RNG_CONFIG) */
  11960. +#define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
  11961. +
  11962. +/*
  11963. + * Register offsets in register set 1
  11964. + */
  11965. +
  11966. +#define HIFN_UNLOCK_SECRET1 0xf4
  11967. +#define HIFN_UNLOCK_SECRET2 0xfc
  11968. +
  11969. +/*
  11970. + * PLL config register
  11971. + *
  11972. + * This register is present only on 7954/7955/7956 parts. It must be
  11973. + * programmed according to the bus interface method used by the h/w.
  11974. + * Note that the parts require a stable clock. Since the PCI clock
  11975. + * may vary the reference clock must usually be used. To avoid
  11976. + * overclocking the core logic, setup must be done carefully, refer
  11977. + * to the driver for details. The exact multiplier required varies
  11978. + * by part and system configuration; refer to the Hifn documentation.
  11979. + */
  11980. +#define HIFN_PLL_REF_SEL 0x00000001 /* REF/HBI clk selection */
  11981. +#define HIFN_PLL_BP 0x00000002 /* bypass (used during setup) */
  11982. +/* bit 2 reserved */
  11983. +#define HIFN_PLL_PK_CLK_SEL 0x00000008 /* public key clk select */
  11984. +#define HIFN_PLL_PE_CLK_SEL 0x00000010 /* packet engine clk select */
  11985. +/* bits 5-9 reserved */
  11986. +#define HIFN_PLL_MBSET 0x00000400 /* must be set to 1 */
  11987. +#define HIFN_PLL_ND 0x00003800 /* Fpll_ref multiplier select */
  11988. +#define HIFN_PLL_ND_SHIFT 11
  11989. +#define HIFN_PLL_ND_2 0x00000000 /* 2x */
  11990. +#define HIFN_PLL_ND_4 0x00000800 /* 4x */
  11991. +#define HIFN_PLL_ND_6 0x00001000 /* 6x */
  11992. +#define HIFN_PLL_ND_8 0x00001800 /* 8x */
  11993. +#define HIFN_PLL_ND_10 0x00002000 /* 10x */
  11994. +#define HIFN_PLL_ND_12 0x00002800 /* 12x */
  11995. +/* bits 14-15 reserved */
  11996. +#define HIFN_PLL_IS 0x00010000 /* charge pump current select */
  11997. +/* bits 17-31 reserved */
  11998. +
  11999. +/*
  12000. + * Board configuration specifies only these bits.
  12001. + */
  12002. +#define HIFN_PLL_CONFIG (HIFN_PLL_IS|HIFN_PLL_ND|HIFN_PLL_REF_SEL)
  12003. +
  12004. +/*
  12005. + * Public Key Engine Mode Register
  12006. + */
  12007. +#define HIFN_PKMODE_HOSTINVERT (1 << 0) /* HOST INVERT */
  12008. +#define HIFN_PKMODE_ENHANCED (1 << 1) /* Enable enhanced mode */
  12009. +
  12010. +
  12011. +/*********************************************************************
  12012. + * Structs for board commands
  12013. + *
  12014. + *********************************************************************/
  12015. +
  12016. +/*
  12017. + * Structure to help build up the command data structure.
  12018. + */
  12019. +typedef struct hifn_base_command {
  12020. + volatile u_int16_t masks;
  12021. + volatile u_int16_t session_num;
  12022. + volatile u_int16_t total_source_count;
  12023. + volatile u_int16_t total_dest_count;
  12024. +} hifn_base_command_t;
  12025. +
  12026. +#define HIFN_BASE_CMD_MAC 0x0400
  12027. +#define HIFN_BASE_CMD_CRYPT 0x0800
  12028. +#define HIFN_BASE_CMD_DECODE 0x2000
  12029. +#define HIFN_BASE_CMD_SRCLEN_M 0xc000
  12030. +#define HIFN_BASE_CMD_SRCLEN_S 14
  12031. +#define HIFN_BASE_CMD_DSTLEN_M 0x3000
  12032. +#define HIFN_BASE_CMD_DSTLEN_S 12
  12033. +#define HIFN_BASE_CMD_LENMASK_HI 0x30000
  12034. +#define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
  12035. +
  12036. +/*
  12037. + * Structure to help build up the command data structure.
  12038. + */
  12039. +typedef struct hifn_crypt_command {
  12040. + volatile u_int16_t masks;
  12041. + volatile u_int16_t header_skip;
  12042. + volatile u_int16_t source_count;
  12043. + volatile u_int16_t reserved;
  12044. +} hifn_crypt_command_t;
  12045. +
  12046. +#define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
  12047. +#define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
  12048. +#define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
  12049. +#define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
  12050. +#define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
  12051. +#define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
  12052. +#define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
  12053. +#define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
  12054. +#define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
  12055. +#define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
  12056. +#define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
  12057. +#define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
  12058. +#define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
  12059. +
  12060. +#define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
  12061. +#define HIFN_CRYPT_CMD_SRCLEN_S 14
  12062. +
  12063. +#define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
  12064. +#define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
  12065. +#define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
  12066. +#define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
  12067. +
  12068. +/*
  12069. + * Structure to help build up the command data structure.
  12070. + */
  12071. +typedef struct hifn_mac_command {
  12072. + volatile u_int16_t masks;
  12073. + volatile u_int16_t header_skip;
  12074. + volatile u_int16_t source_count;
  12075. + volatile u_int16_t reserved;
  12076. +} hifn_mac_command_t;
  12077. +
  12078. +#define HIFN_MAC_CMD_ALG_MASK 0x0001
  12079. +#define HIFN_MAC_CMD_ALG_SHA1 0x0000
  12080. +#define HIFN_MAC_CMD_ALG_MD5 0x0001
  12081. +#define HIFN_MAC_CMD_MODE_MASK 0x000c
  12082. +#define HIFN_MAC_CMD_MODE_HMAC 0x0000
  12083. +#define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
  12084. +#define HIFN_MAC_CMD_MODE_HASH 0x0008
  12085. +#define HIFN_MAC_CMD_MODE_FULL 0x0004
  12086. +#define HIFN_MAC_CMD_TRUNC 0x0010
  12087. +#define HIFN_MAC_CMD_RESULT 0x0020
  12088. +#define HIFN_MAC_CMD_APPEND 0x0040
  12089. +#define HIFN_MAC_CMD_SRCLEN_M 0xc000
  12090. +#define HIFN_MAC_CMD_SRCLEN_S 14
  12091. +
  12092. +/*
  12093. + * MAC POS IPsec initiates authentication after encryption on encodes
  12094. + * and before decryption on decodes.
  12095. + */
  12096. +#define HIFN_MAC_CMD_POS_IPSEC 0x0200
  12097. +#define HIFN_MAC_CMD_NEW_KEY 0x0800
  12098. +
  12099. +/*
  12100. + * The poll frequency and poll scalar defines are unshifted values used
  12101. + * to set fields in the DMA Configuration Register.
  12102. + */
  12103. +#ifndef HIFN_POLL_FREQUENCY
  12104. +#define HIFN_POLL_FREQUENCY 0x1
  12105. +#endif
  12106. +
  12107. +#ifndef HIFN_POLL_SCALAR
  12108. +#define HIFN_POLL_SCALAR 0x0
  12109. +#endif
  12110. +
  12111. +#define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
  12112. +#define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
  12113. +#endif /* __HIFN_H__ */
  12114. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifn7751var.h linux-2.6.30/crypto/ocf/hifn/hifn7751var.h
  12115. --- linux-2.6.30.orig/crypto/ocf/hifn/hifn7751var.h 1970-01-01 01:00:00.000000000 +0100
  12116. +++ linux-2.6.30/crypto/ocf/hifn/hifn7751var.h 2009-06-11 10:55:27.000000000 +0200
  12117. @@ -0,0 +1,369 @@
  12118. +/* $FreeBSD: src/sys/dev/hifn/hifn7751var.h,v 1.9 2007/03/21 03:42:49 sam Exp $ */
  12119. +/* $OpenBSD: hifn7751var.h,v 1.42 2002/04/08 17:49:42 jason Exp $ */
  12120. +
  12121. +/*-
  12122. + * Invertex AEON / Hifn 7751 driver
  12123. + * Copyright (c) 1999 Invertex Inc. All rights reserved.
  12124. + * Copyright (c) 1999 Theo de Raadt
  12125. + * Copyright (c) 2000-2001 Network Security Technologies, Inc.
  12126. + * http://www.netsec.net
  12127. + *
  12128. + * Please send any comments, feedback, bug-fixes, or feature requests to
  12129. + * software@invertex.com.
  12130. + *
  12131. + * Redistribution and use in source and binary forms, with or without
  12132. + * modification, are permitted provided that the following conditions
  12133. + * are met:
  12134. + *
  12135. + * 1. Redistributions of source code must retain the above copyright
  12136. + * notice, this list of conditions and the following disclaimer.
  12137. + * 2. Redistributions in binary form must reproduce the above copyright
  12138. + * notice, this list of conditions and the following disclaimer in the
  12139. + * documentation and/or other materials provided with the distribution.
  12140. + * 3. The name of the author may not be used to endorse or promote products
  12141. + * derived from this software without specific prior written permission.
  12142. + *
  12143. + *
  12144. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12145. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  12146. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  12147. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  12148. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  12149. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12150. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12151. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12152. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  12153. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12154. + *
  12155. + * Effort sponsored in part by the Defense Advanced Research Projects
  12156. + * Agency (DARPA) and Air Force Research Laboratory, Air Force
  12157. + * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  12158. + *
  12159. + */
  12160. +
  12161. +#ifndef __HIFN7751VAR_H__
  12162. +#define __HIFN7751VAR_H__
  12163. +
  12164. +#ifdef __KERNEL__
  12165. +
  12166. +/*
  12167. + * Some configurable values for the driver. By default command+result
  12168. + * descriptor rings are the same size. The src+dst descriptor rings
  12169. + * are sized at 3.5x the number of potential commands. Slower parts
  12170. + * (e.g. 7951) tend to run out of src descriptors; faster parts (7811)
  12171. + * src+cmd/result descriptors. It's not clear that increasing the size
  12172. + * of the descriptor rings helps performance significantly as other
  12173. + * factors tend to come into play (e.g. copying misaligned packets).
  12174. + */
  12175. +#define HIFN_D_CMD_RSIZE 24 /* command descriptors */
  12176. +#define HIFN_D_SRC_RSIZE ((HIFN_D_CMD_RSIZE * 7) / 2) /* source descriptors */
  12177. +#define HIFN_D_RES_RSIZE HIFN_D_CMD_RSIZE /* result descriptors */
  12178. +#define HIFN_D_DST_RSIZE HIFN_D_SRC_RSIZE /* destination descriptors */
  12179. +
  12180. +/*
  12181. + * Length values for cryptography
  12182. + */
  12183. +#define HIFN_DES_KEY_LENGTH 8
  12184. +#define HIFN_3DES_KEY_LENGTH 24
  12185. +#define HIFN_MAX_CRYPT_KEY_LENGTH HIFN_3DES_KEY_LENGTH
  12186. +#define HIFN_IV_LENGTH 8
  12187. +#define HIFN_AES_IV_LENGTH 16
  12188. +#define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
  12189. +
  12190. +/*
  12191. + * Length values for authentication
  12192. + */
  12193. +#define HIFN_MAC_KEY_LENGTH 64
  12194. +#define HIFN_MD5_LENGTH 16
  12195. +#define HIFN_SHA1_LENGTH 20
  12196. +#define HIFN_MAC_TRUNC_LENGTH 12
  12197. +
  12198. +#define MAX_SCATTER 64
  12199. +
  12200. +/*
  12201. + * Data structure to hold all 4 rings and any other ring related data.
  12202. + */
  12203. +struct hifn_dma {
  12204. + /*
  12205. + * Descriptor rings. We add +1 to the size to accomidate the
  12206. + * jump descriptor.
  12207. + */
  12208. + struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1];
  12209. + struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1];
  12210. + struct hifn_desc dstr[HIFN_D_DST_RSIZE+1];
  12211. + struct hifn_desc resr[HIFN_D_RES_RSIZE+1];
  12212. +
  12213. + struct hifn_command *hifn_commands[HIFN_D_RES_RSIZE];
  12214. +
  12215. + u_char command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
  12216. + u_char result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
  12217. + u_int32_t slop[HIFN_D_CMD_RSIZE];
  12218. +
  12219. + u_int64_t test_src, test_dst;
  12220. +
  12221. + /*
  12222. + * Our current positions for insertion and removal from the desriptor
  12223. + * rings.
  12224. + */
  12225. + int cmdi, srci, dsti, resi;
  12226. + volatile int cmdu, srcu, dstu, resu;
  12227. + int cmdk, srck, dstk, resk;
  12228. +};
  12229. +
  12230. +struct hifn_session {
  12231. + int hs_used;
  12232. + int hs_mlen;
  12233. + u_int8_t hs_iv[HIFN_MAX_IV_LENGTH];
  12234. +};
  12235. +
  12236. +#define HIFN_RING_SYNC(sc, r, i, f) \
  12237. + /* DAVIDM bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f)) */
  12238. +
  12239. +#define HIFN_CMDR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), cmdr, (i), (f))
  12240. +#define HIFN_RESR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), resr, (i), (f))
  12241. +#define HIFN_SRCR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), srcr, (i), (f))
  12242. +#define HIFN_DSTR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), dstr, (i), (f))
  12243. +
  12244. +#define HIFN_CMD_SYNC(sc, i, f) \
  12245. + /* DAVIDM bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f)) */
  12246. +
  12247. +#define HIFN_RES_SYNC(sc, i, f) \
  12248. + /* DAVIDM bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f)) */
  12249. +
  12250. +typedef int bus_size_t;
  12251. +
  12252. +/*
  12253. + * Holds data specific to a single HIFN board.
  12254. + */
  12255. +struct hifn_softc {
  12256. + softc_device_decl sc_dev;
  12257. +
  12258. + struct pci_dev *sc_pcidev; /* PCI device pointer */
  12259. + spinlock_t sc_mtx; /* per-instance lock */
  12260. +
  12261. + int sc_num; /* for multiple devs */
  12262. +
  12263. + ocf_iomem_t sc_bar0;
  12264. + bus_size_t sc_bar0_lastreg;/* bar0 last reg written */
  12265. + ocf_iomem_t sc_bar1;
  12266. + bus_size_t sc_bar1_lastreg;/* bar1 last reg written */
  12267. +
  12268. + int sc_irq;
  12269. +
  12270. + u_int32_t sc_dmaier;
  12271. + u_int32_t sc_drammodel; /* 1=dram, 0=sram */
  12272. + u_int32_t sc_pllconfig; /* 7954/7955/7956 PLL config */
  12273. +
  12274. + struct hifn_dma *sc_dma;
  12275. + dma_addr_t sc_dma_physaddr;/* physical address of sc_dma */
  12276. +
  12277. + int sc_dmansegs;
  12278. + int32_t sc_cid;
  12279. + int sc_maxses;
  12280. + int sc_nsessions;
  12281. + struct hifn_session *sc_sessions;
  12282. + int sc_ramsize;
  12283. + int sc_flags;
  12284. +#define HIFN_HAS_RNG 0x1 /* includes random number generator */
  12285. +#define HIFN_HAS_PUBLIC 0x2 /* includes public key support */
  12286. +#define HIFN_HAS_AES 0x4 /* includes AES support */
  12287. +#define HIFN_IS_7811 0x8 /* Hifn 7811 part */
  12288. +#define HIFN_IS_7956 0x10 /* Hifn 7956/7955 don't have SDRAM */
  12289. +
  12290. + struct timer_list sc_tickto; /* for managing DMA */
  12291. +
  12292. + int sc_rngfirst;
  12293. + int sc_rnghz; /* RNG polling frequency */
  12294. +
  12295. + int sc_c_busy; /* command ring busy */
  12296. + int sc_s_busy; /* source data ring busy */
  12297. + int sc_d_busy; /* destination data ring busy */
  12298. + int sc_r_busy; /* result ring busy */
  12299. + int sc_active; /* for initial countdown */
  12300. + int sc_needwakeup; /* ops q'd wating on resources */
  12301. + int sc_curbatch; /* # ops submitted w/o int */
  12302. + int sc_suspended;
  12303. +#ifdef HIFN_VULCANDEV
  12304. + struct cdev *sc_pkdev;
  12305. +#endif
  12306. +};
  12307. +
  12308. +#define HIFN_LOCK(_sc) spin_lock_irqsave(&(_sc)->sc_mtx, l_flags)
  12309. +#define HIFN_UNLOCK(_sc) spin_unlock_irqrestore(&(_sc)->sc_mtx, l_flags)
  12310. +
  12311. +/*
  12312. + * hifn_command_t
  12313. + *
  12314. + * This is the control structure used to pass commands to hifn_encrypt().
  12315. + *
  12316. + * flags
  12317. + * -----
  12318. + * Flags is the bitwise "or" values for command configuration. A single
  12319. + * encrypt direction needs to be set:
  12320. + *
  12321. + * HIFN_ENCODE or HIFN_DECODE
  12322. + *
  12323. + * To use cryptography, a single crypto algorithm must be included:
  12324. + *
  12325. + * HIFN_CRYPT_3DES or HIFN_CRYPT_DES
  12326. + *
  12327. + * To use authentication is used, a single MAC algorithm must be included:
  12328. + *
  12329. + * HIFN_MAC_MD5 or HIFN_MAC_SHA1
  12330. + *
  12331. + * By default MD5 uses a 16 byte hash and SHA-1 uses a 20 byte hash.
  12332. + * If the value below is set, hash values are truncated or assumed
  12333. + * truncated to 12 bytes:
  12334. + *
  12335. + * HIFN_MAC_TRUNC
  12336. + *
  12337. + * Keys for encryption and authentication can be sent as part of a command,
  12338. + * or the last key value used with a particular session can be retrieved
  12339. + * and used again if either of these flags are not specified.
  12340. + *
  12341. + * HIFN_CRYPT_NEW_KEY, HIFN_MAC_NEW_KEY
  12342. + *
  12343. + * session_num
  12344. + * -----------
  12345. + * A number between 0 and 2048 (for DRAM models) or a number between
  12346. + * 0 and 768 (for SRAM models). Those who don't want to use session
  12347. + * numbers should leave value at zero and send a new crypt key and/or
  12348. + * new MAC key on every command. If you use session numbers and
  12349. + * don't send a key with a command, the last key sent for that same
  12350. + * session number will be used.
  12351. + *
  12352. + * Warning: Using session numbers and multiboard at the same time
  12353. + * is currently broken.
  12354. + *
  12355. + * mbuf
  12356. + * ----
  12357. + * Either fill in the mbuf pointer and npa=0 or
  12358. + * fill packp[] and packl[] and set npa to > 0
  12359. + *
  12360. + * mac_header_skip
  12361. + * ---------------
  12362. + * The number of bytes of the source_buf that are skipped over before
  12363. + * authentication begins. This must be a number between 0 and 2^16-1
  12364. + * and can be used by IPsec implementers to skip over IP headers.
  12365. + * *** Value ignored if authentication not used ***
  12366. + *
  12367. + * crypt_header_skip
  12368. + * -----------------
  12369. + * The number of bytes of the source_buf that are skipped over before
  12370. + * the cryptographic operation begins. This must be a number between 0
  12371. + * and 2^16-1. For IPsec, this number will always be 8 bytes larger
  12372. + * than the auth_header_skip (to skip over the ESP header).
  12373. + * *** Value ignored if cryptography not used ***
  12374. + *
  12375. + */
  12376. +struct hifn_operand {
  12377. + union {
  12378. + struct sk_buff *skb;
  12379. + struct uio *io;
  12380. + unsigned char *buf;
  12381. + } u;
  12382. + void *map;
  12383. + bus_size_t mapsize;
  12384. + int nsegs;
  12385. + struct {
  12386. + dma_addr_t ds_addr;
  12387. + int ds_len;
  12388. + } segs[MAX_SCATTER];
  12389. +};
  12390. +
  12391. +struct hifn_command {
  12392. + u_int16_t session_num;
  12393. + u_int16_t base_masks, cry_masks, mac_masks;
  12394. + u_int8_t iv[HIFN_MAX_IV_LENGTH], *ck, mac[HIFN_MAC_KEY_LENGTH];
  12395. + int cklen;
  12396. + int sloplen, slopidx;
  12397. +
  12398. + struct hifn_operand src;
  12399. + struct hifn_operand dst;
  12400. +
  12401. + struct hifn_softc *softc;
  12402. + struct cryptop *crp;
  12403. + struct cryptodesc *enccrd, *maccrd;
  12404. +};
  12405. +
  12406. +#define src_skb src.u.skb
  12407. +#define src_io src.u.io
  12408. +#define src_map src.map
  12409. +#define src_mapsize src.mapsize
  12410. +#define src_segs src.segs
  12411. +#define src_nsegs src.nsegs
  12412. +#define src_buf src.u.buf
  12413. +
  12414. +#define dst_skb dst.u.skb
  12415. +#define dst_io dst.u.io
  12416. +#define dst_map dst.map
  12417. +#define dst_mapsize dst.mapsize
  12418. +#define dst_segs dst.segs
  12419. +#define dst_nsegs dst.nsegs
  12420. +#define dst_buf dst.u.buf
  12421. +
  12422. +/*
  12423. + * Return values for hifn_crypto()
  12424. + */
  12425. +#define HIFN_CRYPTO_SUCCESS 0
  12426. +#define HIFN_CRYPTO_BAD_INPUT (-1)
  12427. +#define HIFN_CRYPTO_RINGS_FULL (-2)
  12428. +
  12429. +/**************************************************************************
  12430. + *
  12431. + * Function: hifn_crypto
  12432. + *
  12433. + * Purpose: Called by external drivers to begin an encryption on the
  12434. + * HIFN board.
  12435. + *
  12436. + * Blocking/Non-blocking Issues
  12437. + * ============================
  12438. + * The driver cannot block in hifn_crypto (no calls to tsleep) currently.
  12439. + * hifn_crypto() returns HIFN_CRYPTO_RINGS_FULL if there is not enough
  12440. + * room in any of the rings for the request to proceed.
  12441. + *
  12442. + * Return Values
  12443. + * =============
  12444. + * 0 for success, negative values on error
  12445. + *
  12446. + * Defines for negative error codes are:
  12447. + *
  12448. + * HIFN_CRYPTO_BAD_INPUT : The passed in command had invalid settings.
  12449. + * HIFN_CRYPTO_RINGS_FULL : All DMA rings were full and non-blocking
  12450. + * behaviour was requested.
  12451. + *
  12452. + *************************************************************************/
  12453. +
  12454. +/*
  12455. + * Convert back and forth from 'sid' to 'card' and 'session'
  12456. + */
  12457. +#define HIFN_CARD(sid) (((sid) & 0xf0000000) >> 28)
  12458. +#define HIFN_SESSION(sid) ((sid) & 0x000007ff)
  12459. +#define HIFN_SID(crd,ses) (((crd) << 28) | ((ses) & 0x7ff))
  12460. +
  12461. +#endif /* _KERNEL */
  12462. +
  12463. +struct hifn_stats {
  12464. + u_int64_t hst_ibytes;
  12465. + u_int64_t hst_obytes;
  12466. + u_int32_t hst_ipackets;
  12467. + u_int32_t hst_opackets;
  12468. + u_int32_t hst_invalid;
  12469. + u_int32_t hst_nomem; /* malloc or one of hst_nomem_* */
  12470. + u_int32_t hst_abort;
  12471. + u_int32_t hst_noirq; /* IRQ for no reason */
  12472. + u_int32_t hst_totbatch; /* ops submitted w/o interrupt */
  12473. + u_int32_t hst_maxbatch; /* max ops submitted together */
  12474. + u_int32_t hst_unaligned; /* unaligned src caused copy */
  12475. + /*
  12476. + * The following divides hst_nomem into more specific buckets.
  12477. + */
  12478. + u_int32_t hst_nomem_map; /* bus_dmamap_create failed */
  12479. + u_int32_t hst_nomem_load; /* bus_dmamap_load_* failed */
  12480. + u_int32_t hst_nomem_mbuf; /* MGET* failed */
  12481. + u_int32_t hst_nomem_mcl; /* MCLGET* failed */
  12482. + u_int32_t hst_nomem_cr; /* out of command/result descriptor */
  12483. + u_int32_t hst_nomem_sd; /* out of src/dst descriptors */
  12484. +};
  12485. +
  12486. +#endif /* __HIFN7751VAR_H__ */
  12487. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPP.c linux-2.6.30/crypto/ocf/hifn/hifnHIPP.c
  12488. --- linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPP.c 1970-01-01 01:00:00.000000000 +0100
  12489. +++ linux-2.6.30/crypto/ocf/hifn/hifnHIPP.c 2009-06-11 10:55:27.000000000 +0200
  12490. @@ -0,0 +1,420 @@
  12491. +/*-
  12492. + * Driver for Hifn HIPP-I/II chipset
  12493. + * Copyright (c) 2006 Michael Richardson <mcr@xelerance.com>
  12494. + *
  12495. + * Redistribution and use in source and binary forms, with or without
  12496. + * modification, are permitted provided that the following conditions
  12497. + * are met:
  12498. + *
  12499. + * 1. Redistributions of source code must retain the above copyright
  12500. + * notice, this list of conditions and the following disclaimer.
  12501. + * 2. Redistributions in binary form must reproduce the above copyright
  12502. + * notice, this list of conditions and the following disclaimer in the
  12503. + * documentation and/or other materials provided with the distribution.
  12504. + * 3. The name of the author may not be used to endorse or promote products
  12505. + * derived from this software without specific prior written permission.
  12506. + *
  12507. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12508. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  12509. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  12510. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  12511. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  12512. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12513. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12514. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12515. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  12516. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12517. + *
  12518. + * Effort sponsored by Hifn Inc.
  12519. + *
  12520. + */
  12521. +
  12522. +/*
  12523. + * Driver for various Hifn encryption processors.
  12524. + */
  12525. +#ifndef AUTOCONF_INCLUDED
  12526. +#include <linux/config.h>
  12527. +#endif
  12528. +#include <linux/module.h>
  12529. +#include <linux/init.h>
  12530. +#include <linux/list.h>
  12531. +#include <linux/slab.h>
  12532. +#include <linux/wait.h>
  12533. +#include <linux/sched.h>
  12534. +#include <linux/pci.h>
  12535. +#include <linux/delay.h>
  12536. +#include <linux/interrupt.h>
  12537. +#include <linux/spinlock.h>
  12538. +#include <linux/random.h>
  12539. +#include <linux/version.h>
  12540. +#include <linux/skbuff.h>
  12541. +#include <linux/uio.h>
  12542. +#include <linux/sysfs.h>
  12543. +#include <linux/miscdevice.h>
  12544. +#include <asm/io.h>
  12545. +
  12546. +#include <cryptodev.h>
  12547. +
  12548. +#include "hifnHIPPreg.h"
  12549. +#include "hifnHIPPvar.h"
  12550. +
  12551. +#if 1
  12552. +#define DPRINTF(a...) if (hipp_debug) { \
  12553. + printk("%s: ", sc ? \
  12554. + device_get_nameunit(sc->sc_dev) : "hifn"); \
  12555. + printk(a); \
  12556. + } else
  12557. +#else
  12558. +#define DPRINTF(a...)
  12559. +#endif
  12560. +
  12561. +typedef int bus_size_t;
  12562. +
  12563. +static inline int
  12564. +pci_get_revid(struct pci_dev *dev)
  12565. +{
  12566. + u8 rid = 0;
  12567. + pci_read_config_byte(dev, PCI_REVISION_ID, &rid);
  12568. + return rid;
  12569. +}
  12570. +
  12571. +#define debug hipp_debug
  12572. +int hipp_debug = 0;
  12573. +module_param(hipp_debug, int, 0644);
  12574. +MODULE_PARM_DESC(hipp_debug, "Enable debug");
  12575. +
  12576. +int hipp_maxbatch = 1;
  12577. +module_param(hipp_maxbatch, int, 0644);
  12578. +MODULE_PARM_DESC(hipp_maxbatch, "max ops to batch w/o interrupt");
  12579. +
  12580. +static int hipp_probe(struct pci_dev *dev, const struct pci_device_id *ent);
  12581. +static void hipp_remove(struct pci_dev *dev);
  12582. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  12583. +static irqreturn_t hipp_intr(int irq, void *arg);
  12584. +#else
  12585. +static irqreturn_t hipp_intr(int irq, void *arg, struct pt_regs *regs);
  12586. +#endif
  12587. +
  12588. +static int hipp_num_chips = 0;
  12589. +static struct hipp_softc *hipp_chip_idx[HIPP_MAX_CHIPS];
  12590. +
  12591. +static int hipp_newsession(device_t, u_int32_t *, struct cryptoini *);
  12592. +static int hipp_freesession(device_t, u_int64_t);
  12593. +static int hipp_process(device_t, struct cryptop *, int);
  12594. +
  12595. +static device_method_t hipp_methods = {
  12596. + /* crypto device methods */
  12597. + DEVMETHOD(cryptodev_newsession, hipp_newsession),
  12598. + DEVMETHOD(cryptodev_freesession,hipp_freesession),
  12599. + DEVMETHOD(cryptodev_process, hipp_process),
  12600. +};
  12601. +
  12602. +static __inline u_int32_t
  12603. +READ_REG(struct hipp_softc *sc, unsigned int barno, bus_size_t reg)
  12604. +{
  12605. + u_int32_t v = readl(sc->sc_bar[barno] + reg);
  12606. + //sc->sc_bar0_lastreg = (bus_size_t) -1;
  12607. + return (v);
  12608. +}
  12609. +static __inline void
  12610. +WRITE_REG(struct hipp_softc *sc, unsigned int barno, bus_size_t reg, u_int32_t val)
  12611. +{
  12612. + writel(val, sc->sc_bar[barno] + reg);
  12613. +}
  12614. +
  12615. +#define READ_REG_0(sc, reg) READ_REG(sc, 0, reg)
  12616. +#define WRITE_REG_0(sc, reg, val) WRITE_REG(sc,0, reg, val)
  12617. +#define READ_REG_1(sc, reg) READ_REG(sc, 1, reg)
  12618. +#define WRITE_REG_1(sc, reg, val) WRITE_REG(sc,1, reg, val)
  12619. +
  12620. +static int
  12621. +hipp_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  12622. +{
  12623. + return EINVAL;
  12624. +}
  12625. +
  12626. +static int
  12627. +hipp_freesession(device_t dev, u_int64_t tid)
  12628. +{
  12629. + return EINVAL;
  12630. +}
  12631. +
  12632. +static int
  12633. +hipp_process(device_t dev, struct cryptop *crp, int hint)
  12634. +{
  12635. + return EINVAL;
  12636. +}
  12637. +
  12638. +static const char*
  12639. +hipp_partname(struct hipp_softc *sc, char buf[128], size_t blen)
  12640. +{
  12641. + char *n = NULL;
  12642. +
  12643. + switch (pci_get_vendor(sc->sc_pcidev)) {
  12644. + case PCI_VENDOR_HIFN:
  12645. + switch (pci_get_device(sc->sc_pcidev)) {
  12646. + case PCI_PRODUCT_HIFN_7855: n = "Hifn 7855";
  12647. + case PCI_PRODUCT_HIFN_8155: n = "Hifn 8155";
  12648. + case PCI_PRODUCT_HIFN_6500: n = "Hifn 6500";
  12649. + }
  12650. + }
  12651. +
  12652. + if(n==NULL) {
  12653. + snprintf(buf, blen, "VID=%02x,PID=%02x",
  12654. + pci_get_vendor(sc->sc_pcidev),
  12655. + pci_get_device(sc->sc_pcidev));
  12656. + } else {
  12657. + buf[0]='\0';
  12658. + strncat(buf, n, blen);
  12659. + }
  12660. + return buf;
  12661. +}
  12662. +
  12663. +struct hipp_fs_entry {
  12664. + struct attribute attr;
  12665. + /* other stuff */
  12666. +};
  12667. +
  12668. +
  12669. +static ssize_t
  12670. +cryptoid_show(struct device *dev,
  12671. + struct device_attribute *attr,
  12672. + char *buf)
  12673. +{
  12674. + struct hipp_softc *sc;
  12675. +
  12676. + sc = pci_get_drvdata(to_pci_dev (dev));
  12677. + return sprintf (buf, "%d\n", sc->sc_cid);
  12678. +}
  12679. +
  12680. +struct device_attribute hipp_dev_cryptoid = __ATTR_RO(cryptoid);
  12681. +
  12682. +/*
  12683. + * Attach an interface that successfully probed.
  12684. + */
  12685. +static int
  12686. +hipp_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  12687. +{
  12688. + struct hipp_softc *sc = NULL;
  12689. + int i;
  12690. + //char rbase;
  12691. + //u_int16_t ena;
  12692. + int rev;
  12693. + //int rseg;
  12694. + int rc;
  12695. +
  12696. + DPRINTF("%s()\n", __FUNCTION__);
  12697. +
  12698. + if (pci_enable_device(dev) < 0)
  12699. + return(-ENODEV);
  12700. +
  12701. + if (pci_set_mwi(dev))
  12702. + return(-ENODEV);
  12703. +
  12704. + if (!dev->irq) {
  12705. + printk("hifn: found device with no IRQ assigned. check BIOS settings!");
  12706. + pci_disable_device(dev);
  12707. + return(-ENODEV);
  12708. + }
  12709. +
  12710. + sc = (struct hipp_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  12711. + if (!sc)
  12712. + return(-ENOMEM);
  12713. + memset(sc, 0, sizeof(*sc));
  12714. +
  12715. + softc_device_init(sc, "hifn-hipp", hipp_num_chips, hipp_methods);
  12716. +
  12717. + sc->sc_pcidev = dev;
  12718. + sc->sc_irq = -1;
  12719. + sc->sc_cid = -1;
  12720. + sc->sc_num = hipp_num_chips++;
  12721. +
  12722. + if (sc->sc_num < HIPP_MAX_CHIPS)
  12723. + hipp_chip_idx[sc->sc_num] = sc;
  12724. +
  12725. + pci_set_drvdata(sc->sc_pcidev, sc);
  12726. +
  12727. + spin_lock_init(&sc->sc_mtx);
  12728. +
  12729. + /*
  12730. + * Setup PCI resources.
  12731. + * The READ_REG_0, WRITE_REG_0, READ_REG_1,
  12732. + * and WRITE_REG_1 macros throughout the driver are used
  12733. + * to permit better debugging.
  12734. + */
  12735. + for(i=0; i<4; i++) {
  12736. + unsigned long mem_start, mem_len;
  12737. + mem_start = pci_resource_start(sc->sc_pcidev, i);
  12738. + mem_len = pci_resource_len(sc->sc_pcidev, i);
  12739. + sc->sc_barphy[i] = (caddr_t)mem_start;
  12740. + sc->sc_bar[i] = (ocf_iomem_t) ioremap(mem_start, mem_len);
  12741. + if (!sc->sc_bar[i]) {
  12742. + device_printf(sc->sc_dev, "cannot map bar%d register space\n", i);
  12743. + goto fail;
  12744. + }
  12745. + }
  12746. +
  12747. + //hipp_reset_board(sc, 0);
  12748. + pci_set_master(sc->sc_pcidev);
  12749. +
  12750. + /*
  12751. + * Arrange the interrupt line.
  12752. + */
  12753. + rc = request_irq(dev->irq, hipp_intr, IRQF_SHARED, "hifn", sc);
  12754. + if (rc) {
  12755. + device_printf(sc->sc_dev, "could not map interrupt: %d\n", rc);
  12756. + goto fail;
  12757. + }
  12758. + sc->sc_irq = dev->irq;
  12759. +
  12760. + rev = READ_REG_1(sc, HIPP_1_REVID) & 0xffff;
  12761. +
  12762. + {
  12763. + char b[32];
  12764. + device_printf(sc->sc_dev, "%s, rev %u",
  12765. + hipp_partname(sc, b, sizeof(b)), rev);
  12766. + }
  12767. +
  12768. +#if 0
  12769. + if (sc->sc_flags & HIFN_IS_7956)
  12770. + printf(", pll=0x%x<%s clk, %ux mult>",
  12771. + sc->sc_pllconfig,
  12772. + sc->sc_pllconfig & HIFN_PLL_REF_SEL ? "ext" : "pci",
  12773. + 2 + 2*((sc->sc_pllconfig & HIFN_PLL_ND) >> 11));
  12774. +#endif
  12775. + printf("\n");
  12776. +
  12777. + sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
  12778. + if (sc->sc_cid < 0) {
  12779. + device_printf(sc->sc_dev, "could not get crypto driver id\n");
  12780. + goto fail;
  12781. + }
  12782. +
  12783. +#if 0 /* cannot work with a non-GPL module */
  12784. + /* make a sysfs entry to let the world know what entry we got */
  12785. + sysfs_create_file(&sc->sc_pcidev->dev.kobj, &hipp_dev_cryptoid.attr);
  12786. +#endif
  12787. +
  12788. +#if 0
  12789. + init_timer(&sc->sc_tickto);
  12790. + sc->sc_tickto.function = hifn_tick;
  12791. + sc->sc_tickto.data = (unsigned long) sc->sc_num;
  12792. + mod_timer(&sc->sc_tickto, jiffies + HZ);
  12793. +#endif
  12794. +
  12795. +#if 0 /* no code here yet ?? */
  12796. + crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  12797. +#endif
  12798. +
  12799. + return (0);
  12800. +
  12801. +fail:
  12802. + if (sc->sc_cid >= 0)
  12803. + crypto_unregister_all(sc->sc_cid);
  12804. + if (sc->sc_irq != -1)
  12805. + free_irq(sc->sc_irq, sc);
  12806. +
  12807. +#if 0
  12808. + if (sc->sc_dma) {
  12809. + /* Turn off DMA polling */
  12810. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  12811. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  12812. +
  12813. + pci_free_consistent(sc->sc_pcidev,
  12814. + sizeof(*sc->sc_dma),
  12815. + sc->sc_dma, sc->sc_dma_physaddr);
  12816. + }
  12817. +#endif
  12818. + kfree(sc);
  12819. + return (-ENXIO);
  12820. +}
  12821. +
  12822. +/*
  12823. + * Detach an interface that successfully probed.
  12824. + */
  12825. +static void
  12826. +hipp_remove(struct pci_dev *dev)
  12827. +{
  12828. + struct hipp_softc *sc = pci_get_drvdata(dev);
  12829. + unsigned long l_flags;
  12830. +
  12831. + DPRINTF("%s()\n", __FUNCTION__);
  12832. +
  12833. + /* disable interrupts */
  12834. + HIPP_LOCK(sc);
  12835. +
  12836. +#if 0
  12837. + WRITE_REG_1(sc, HIFN_1_DMA_IER, 0);
  12838. + HIFN_UNLOCK(sc);
  12839. +
  12840. + /*XXX other resources */
  12841. + del_timer_sync(&sc->sc_tickto);
  12842. +
  12843. + /* Turn off DMA polling */
  12844. + WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  12845. + HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  12846. +#endif
  12847. +
  12848. + crypto_unregister_all(sc->sc_cid);
  12849. +
  12850. + free_irq(sc->sc_irq, sc);
  12851. +
  12852. +#if 0
  12853. + pci_free_consistent(sc->sc_pcidev, sizeof(*sc->sc_dma),
  12854. + sc->sc_dma, sc->sc_dma_physaddr);
  12855. +#endif
  12856. +}
  12857. +
  12858. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  12859. +static irqreturn_t hipp_intr(int irq, void *arg)
  12860. +#else
  12861. +static irqreturn_t hipp_intr(int irq, void *arg, struct pt_regs *regs)
  12862. +#endif
  12863. +{
  12864. + struct hipp_softc *sc = arg;
  12865. +
  12866. + sc = sc; /* shut up compiler */
  12867. +
  12868. + return IRQ_HANDLED;
  12869. +}
  12870. +
  12871. +static struct pci_device_id hipp_pci_tbl[] = {
  12872. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7855,
  12873. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  12874. + { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_8155,
  12875. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  12876. +};
  12877. +MODULE_DEVICE_TABLE(pci, hipp_pci_tbl);
  12878. +
  12879. +static struct pci_driver hipp_driver = {
  12880. + .name = "hipp",
  12881. + .id_table = hipp_pci_tbl,
  12882. + .probe = hipp_probe,
  12883. + .remove = hipp_remove,
  12884. + /* add PM stuff here one day */
  12885. +};
  12886. +
  12887. +static int __init hipp_init (void)
  12888. +{
  12889. + struct hipp_softc *sc = NULL;
  12890. + int rc;
  12891. +
  12892. + DPRINTF("%s(%p)\n", __FUNCTION__, hipp_init);
  12893. +
  12894. + rc = pci_register_driver(&hipp_driver);
  12895. + pci_register_driver_compat(&hipp_driver, rc);
  12896. +
  12897. + return rc;
  12898. +}
  12899. +
  12900. +static void __exit hipp_exit (void)
  12901. +{
  12902. + pci_unregister_driver(&hipp_driver);
  12903. +}
  12904. +
  12905. +module_init(hipp_init);
  12906. +module_exit(hipp_exit);
  12907. +
  12908. +MODULE_LICENSE("BSD");
  12909. +MODULE_AUTHOR("Michael Richardson <mcr@xelerance.com>");
  12910. +MODULE_DESCRIPTION("OCF driver for hifn HIPP-I/II PCI crypto devices");
  12911. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPPreg.h linux-2.6.30/crypto/ocf/hifn/hifnHIPPreg.h
  12912. --- linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPPreg.h 1970-01-01 01:00:00.000000000 +0100
  12913. +++ linux-2.6.30/crypto/ocf/hifn/hifnHIPPreg.h 2009-06-11 10:55:27.000000000 +0200
  12914. @@ -0,0 +1,46 @@
  12915. +/*-
  12916. + * Hifn HIPP-I/HIPP-II (7855/8155) driver.
  12917. + * Copyright (c) 2006 Michael Richardson <mcr@xelerance.com>
  12918. + *
  12919. + * Redistribution and use in source and binary forms, with or without
  12920. + * modification, are permitted provided that the following conditions
  12921. + * are met:
  12922. + *
  12923. + * 1. Redistributions of source code must retain the above copyright
  12924. + * notice, this list of conditions and the following disclaimer.
  12925. + * 2. Redistributions in binary form must reproduce the above copyright
  12926. + * notice, this list of conditions and the following disclaimer in the
  12927. + * documentation and/or other materials provided with the distribution.
  12928. + * 3. The name of the author may not be used to endorse or promote products
  12929. + * derived from this software without specific prior written permission.
  12930. + *
  12931. + *
  12932. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12933. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  12934. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  12935. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  12936. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  12937. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12938. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12939. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12940. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  12941. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12942. + *
  12943. + * Effort sponsored by Hifn inc.
  12944. + *
  12945. + */
  12946. +
  12947. +#ifndef __HIFNHIPP_H__
  12948. +#define __HIFNHIPP_H__
  12949. +
  12950. +/*
  12951. + * PCI vendor and device identifiers
  12952. + */
  12953. +#define PCI_VENDOR_HIFN 0x13a3 /* Hifn */
  12954. +#define PCI_PRODUCT_HIFN_6500 0x0006 /* 6500 */
  12955. +#define PCI_PRODUCT_HIFN_7855 0x001f /* 7855 */
  12956. +#define PCI_PRODUCT_HIFN_8155 0x999 /* XXX 8155 */
  12957. +
  12958. +#define HIPP_1_REVID 0x01 /* BOGUS */
  12959. +
  12960. +#endif /* __HIPP_H__ */
  12961. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPPvar.h linux-2.6.30/crypto/ocf/hifn/hifnHIPPvar.h
  12962. --- linux-2.6.30.orig/crypto/ocf/hifn/hifnHIPPvar.h 1970-01-01 01:00:00.000000000 +0100
  12963. +++ linux-2.6.30/crypto/ocf/hifn/hifnHIPPvar.h 2009-06-11 10:55:27.000000000 +0200
  12964. @@ -0,0 +1,93 @@
  12965. +/*
  12966. + * Hifn HIPP-I/HIPP-II (7855/8155) driver.
  12967. + * Copyright (c) 2006 Michael Richardson <mcr@xelerance.com> *
  12968. + *
  12969. + * Redistribution and use in source and binary forms, with or without
  12970. + * modification, are permitted provided that the following conditions
  12971. + * are met:
  12972. + *
  12973. + * 1. Redistributions of source code must retain the above copyright
  12974. + * notice, this list of conditions and the following disclaimer.
  12975. + * 2. Redistributions in binary form must reproduce the above copyright
  12976. + * notice, this list of conditions and the following disclaimer in the
  12977. + * documentation and/or other materials provided with the distribution.
  12978. + * 3. The name of the author may not be used to endorse or promote products
  12979. + * derived from this software without specific prior written permission.
  12980. + *
  12981. + *
  12982. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12983. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  12984. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  12985. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  12986. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  12987. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12988. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12989. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12990. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  12991. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12992. + *
  12993. + * Effort sponsored by Hifn inc.
  12994. + *
  12995. + */
  12996. +
  12997. +#ifndef __HIFNHIPPVAR_H__
  12998. +#define __HIFNHIPPVAR_H__
  12999. +
  13000. +#define HIPP_MAX_CHIPS 8
  13001. +
  13002. +/*
  13003. + * Holds data specific to a single Hifn HIPP-I board.
  13004. + */
  13005. +struct hipp_softc {
  13006. + softc_device_decl sc_dev;
  13007. +
  13008. + struct pci_dev *sc_pcidev; /* device backpointer */
  13009. + ocf_iomem_t sc_bar[5];
  13010. + caddr_t sc_barphy[5]; /* physical address */
  13011. + int sc_num; /* for multiple devs */
  13012. + spinlock_t sc_mtx; /* per-instance lock */
  13013. + int32_t sc_cid;
  13014. + int sc_irq;
  13015. +
  13016. +#if 0
  13017. +
  13018. + u_int32_t sc_dmaier;
  13019. + u_int32_t sc_drammodel; /* 1=dram, 0=sram */
  13020. + u_int32_t sc_pllconfig; /* 7954/7955/7956 PLL config */
  13021. +
  13022. + struct hifn_dma *sc_dma;
  13023. + dma_addr_t sc_dma_physaddr;/* physical address of sc_dma */
  13024. +
  13025. + int sc_dmansegs;
  13026. + int sc_maxses;
  13027. + int sc_nsessions;
  13028. + struct hifn_session *sc_sessions;
  13029. + int sc_ramsize;
  13030. + int sc_flags;
  13031. +#define HIFN_HAS_RNG 0x1 /* includes random number generator */
  13032. +#define HIFN_HAS_PUBLIC 0x2 /* includes public key support */
  13033. +#define HIFN_HAS_AES 0x4 /* includes AES support */
  13034. +#define HIFN_IS_7811 0x8 /* Hifn 7811 part */
  13035. +#define HIFN_IS_7956 0x10 /* Hifn 7956/7955 don't have SDRAM */
  13036. +
  13037. + struct timer_list sc_tickto; /* for managing DMA */
  13038. +
  13039. + int sc_rngfirst;
  13040. + int sc_rnghz; /* RNG polling frequency */
  13041. +
  13042. + int sc_c_busy; /* command ring busy */
  13043. + int sc_s_busy; /* source data ring busy */
  13044. + int sc_d_busy; /* destination data ring busy */
  13045. + int sc_r_busy; /* result ring busy */
  13046. + int sc_active; /* for initial countdown */
  13047. + int sc_needwakeup; /* ops q'd wating on resources */
  13048. + int sc_curbatch; /* # ops submitted w/o int */
  13049. + int sc_suspended;
  13050. + struct miscdevice sc_miscdev;
  13051. +#endif
  13052. +};
  13053. +
  13054. +#define HIPP_LOCK(_sc) spin_lock_irqsave(&(_sc)->sc_mtx, l_flags)
  13055. +#define HIPP_UNLOCK(_sc) spin_unlock_irqrestore(&(_sc)->sc_mtx, l_flags)
  13056. +
  13057. +#endif /* __HIFNHIPPVAR_H__ */
  13058. diff -Nur linux-2.6.30.orig/crypto/ocf/hifn/Makefile linux-2.6.30/crypto/ocf/hifn/Makefile
  13059. --- linux-2.6.30.orig/crypto/ocf/hifn/Makefile 1970-01-01 01:00:00.000000000 +0100
  13060. +++ linux-2.6.30/crypto/ocf/hifn/Makefile 2009-06-11 10:55:27.000000000 +0200
  13061. @@ -0,0 +1,13 @@
  13062. +# for SGlinux builds
  13063. +-include $(ROOTDIR)/modules/.config
  13064. +
  13065. +obj-$(CONFIG_OCF_HIFN) += hifn7751.o
  13066. +obj-$(CONFIG_OCF_HIFNHIPP) += hifnHIPP.o
  13067. +
  13068. +obj ?= .
  13069. +EXTRA_CFLAGS += -I$(obj)/.. -I$(obj)/
  13070. +
  13071. +ifdef TOPDIR
  13072. +-include $(TOPDIR)/Rules.make
  13073. +endif
  13074. +
  13075. diff -Nur linux-2.6.30.orig/crypto/ocf/ixp4xx/ixp4xx.c linux-2.6.30/crypto/ocf/ixp4xx/ixp4xx.c
  13076. --- linux-2.6.30.orig/crypto/ocf/ixp4xx/ixp4xx.c 1970-01-01 01:00:00.000000000 +0100
  13077. +++ linux-2.6.30/crypto/ocf/ixp4xx/ixp4xx.c 2009-06-11 10:55:27.000000000 +0200
  13078. @@ -0,0 +1,1328 @@
  13079. +/*
  13080. + * An OCF module that uses Intels IXP CryptACC API to do the crypto.
  13081. + * This driver requires the IXP400 Access Library that is available
  13082. + * from Intel in order to operate (or compile).
  13083. + *
  13084. + * Written by David McCullough <david_mccullough@securecomputing.com>
  13085. + * Copyright (C) 2006-2007 David McCullough
  13086. + * Copyright (C) 2004-2005 Intel Corporation.
  13087. + *
  13088. + * LICENSE TERMS
  13089. + *
  13090. + * The free distribution and use of this software in both source and binary
  13091. + * form is allowed (with or without changes) provided that:
  13092. + *
  13093. + * 1. distributions of this source code include the above copyright
  13094. + * notice, this list of conditions and the following disclaimer;
  13095. + *
  13096. + * 2. distributions in binary form include the above copyright
  13097. + * notice, this list of conditions and the following disclaimer
  13098. + * in the documentation and/or other associated materials;
  13099. + *
  13100. + * 3. the copyright holder's name is not used to endorse products
  13101. + * built using this software without specific written permission.
  13102. + *
  13103. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  13104. + * may be distributed under the terms of the GNU General Public License (GPL),
  13105. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  13106. + *
  13107. + * DISCLAIMER
  13108. + *
  13109. + * This software is provided 'as is' with no explicit or implied warranties
  13110. + * in respect of its properties, including, but not limited to, correctness
  13111. + * and/or fitness for purpose.
  13112. + */
  13113. +
  13114. +#ifndef AUTOCONF_INCLUDED
  13115. +#include <linux/config.h>
  13116. +#endif
  13117. +#include <linux/module.h>
  13118. +#include <linux/init.h>
  13119. +#include <linux/list.h>
  13120. +#include <linux/slab.h>
  13121. +#include <linux/sched.h>
  13122. +#include <linux/wait.h>
  13123. +#include <linux/crypto.h>
  13124. +#include <linux/interrupt.h>
  13125. +#include <asm/scatterlist.h>
  13126. +
  13127. +#include <IxTypes.h>
  13128. +#include <IxOsBuffMgt.h>
  13129. +#include <IxNpeDl.h>
  13130. +#include <IxCryptoAcc.h>
  13131. +#include <IxQMgr.h>
  13132. +#include <IxOsServices.h>
  13133. +#include <IxOsCacheMMU.h>
  13134. +
  13135. +#include <cryptodev.h>
  13136. +#include <uio.h>
  13137. +
  13138. +#ifndef IX_MBUF_PRIV
  13139. +#define IX_MBUF_PRIV(x) ((x)->priv)
  13140. +#endif
  13141. +
  13142. +struct ixp_data;
  13143. +
  13144. +struct ixp_q {
  13145. + struct list_head ixp_q_list;
  13146. + struct ixp_data *ixp_q_data;
  13147. + struct cryptop *ixp_q_crp;
  13148. + struct cryptodesc *ixp_q_ccrd;
  13149. + struct cryptodesc *ixp_q_acrd;
  13150. + IX_MBUF ixp_q_mbuf;
  13151. + UINT8 *ixp_hash_dest; /* Location for hash in client buffer */
  13152. + UINT8 *ixp_hash_src; /* Location of hash in internal buffer */
  13153. + unsigned char ixp_q_iv_data[IX_CRYPTO_ACC_MAX_CIPHER_IV_LENGTH];
  13154. + unsigned char *ixp_q_iv;
  13155. +};
  13156. +
  13157. +struct ixp_data {
  13158. + int ixp_registered; /* is the context registered */
  13159. + int ixp_crd_flags; /* detect direction changes */
  13160. +
  13161. + int ixp_cipher_alg;
  13162. + int ixp_auth_alg;
  13163. +
  13164. + UINT32 ixp_ctx_id;
  13165. + UINT32 ixp_hash_key_id; /* used when hashing */
  13166. + IxCryptoAccCtx ixp_ctx;
  13167. + IX_MBUF ixp_pri_mbuf;
  13168. + IX_MBUF ixp_sec_mbuf;
  13169. +
  13170. + struct work_struct ixp_pending_work;
  13171. + struct work_struct ixp_registration_work;
  13172. + struct list_head ixp_q; /* unprocessed requests */
  13173. +};
  13174. +
  13175. +#ifdef __ixp46X
  13176. +
  13177. +#define MAX_IOP_SIZE 64 /* words */
  13178. +#define MAX_OOP_SIZE 128
  13179. +
  13180. +#define MAX_PARAMS 3
  13181. +
  13182. +struct ixp_pkq {
  13183. + struct list_head pkq_list;
  13184. + struct cryptkop *pkq_krp;
  13185. +
  13186. + IxCryptoAccPkeEauInOperands pkq_op;
  13187. + IxCryptoAccPkeEauOpResult pkq_result;
  13188. +
  13189. + UINT32 pkq_ibuf0[MAX_IOP_SIZE];
  13190. + UINT32 pkq_ibuf1[MAX_IOP_SIZE];
  13191. + UINT32 pkq_ibuf2[MAX_IOP_SIZE];
  13192. + UINT32 pkq_obuf[MAX_OOP_SIZE];
  13193. +};
  13194. +
  13195. +static LIST_HEAD(ixp_pkq); /* current PK wait list */
  13196. +static struct ixp_pkq *ixp_pk_cur;
  13197. +static spinlock_t ixp_pkq_lock;
  13198. +
  13199. +#endif /* __ixp46X */
  13200. +
  13201. +static int ixp_blocked = 0;
  13202. +
  13203. +static int32_t ixp_id = -1;
  13204. +static struct ixp_data **ixp_sessions = NULL;
  13205. +static u_int32_t ixp_sesnum = 0;
  13206. +
  13207. +static int ixp_process(device_t, struct cryptop *, int);
  13208. +static int ixp_newsession(device_t, u_int32_t *, struct cryptoini *);
  13209. +static int ixp_freesession(device_t, u_int64_t);
  13210. +#ifdef __ixp46X
  13211. +static int ixp_kprocess(device_t, struct cryptkop *krp, int hint);
  13212. +#endif
  13213. +
  13214. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  13215. +static kmem_cache_t *qcache;
  13216. +#else
  13217. +static struct kmem_cache *qcache;
  13218. +#endif
  13219. +
  13220. +#define debug ixp_debug
  13221. +static int ixp_debug = 0;
  13222. +module_param(ixp_debug, int, 0644);
  13223. +MODULE_PARM_DESC(ixp_debug, "Enable debug");
  13224. +
  13225. +static int ixp_init_crypto = 1;
  13226. +module_param(ixp_init_crypto, int, 0444); /* RO after load/boot */
  13227. +MODULE_PARM_DESC(ixp_init_crypto, "Call ixCryptoAccInit (default is 1)");
  13228. +
  13229. +static void ixp_process_pending(void *arg);
  13230. +static void ixp_registration(void *arg);
  13231. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  13232. +static void ixp_process_pending_wq(struct work_struct *work);
  13233. +static void ixp_registration_wq(struct work_struct *work);
  13234. +#endif
  13235. +
  13236. +/*
  13237. + * dummy device structure
  13238. + */
  13239. +
  13240. +static struct {
  13241. + softc_device_decl sc_dev;
  13242. +} ixpdev;
  13243. +
  13244. +static device_method_t ixp_methods = {
  13245. + /* crypto device methods */
  13246. + DEVMETHOD(cryptodev_newsession, ixp_newsession),
  13247. + DEVMETHOD(cryptodev_freesession,ixp_freesession),
  13248. + DEVMETHOD(cryptodev_process, ixp_process),
  13249. +#ifdef __ixp46X
  13250. + DEVMETHOD(cryptodev_kprocess, ixp_kprocess),
  13251. +#endif
  13252. +};
  13253. +
  13254. +/*
  13255. + * Generate a new software session.
  13256. + */
  13257. +static int
  13258. +ixp_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
  13259. +{
  13260. + struct ixp_data *ixp;
  13261. + u_int32_t i;
  13262. +#define AUTH_LEN(cri, def) \
  13263. + (cri->cri_mlen ? cri->cri_mlen : (def))
  13264. +
  13265. + dprintk("%s():alg %d\n", __FUNCTION__,cri->cri_alg);
  13266. + if (sid == NULL || cri == NULL) {
  13267. + dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  13268. + return EINVAL;
  13269. + }
  13270. +
  13271. + if (ixp_sessions) {
  13272. + for (i = 1; i < ixp_sesnum; i++)
  13273. + if (ixp_sessions[i] == NULL)
  13274. + break;
  13275. + } else
  13276. + i = 1; /* NB: to silence compiler warning */
  13277. +
  13278. + if (ixp_sessions == NULL || i == ixp_sesnum) {
  13279. + struct ixp_data **ixpd;
  13280. +
  13281. + if (ixp_sessions == NULL) {
  13282. + i = 1; /* We leave ixp_sessions[0] empty */
  13283. + ixp_sesnum = CRYPTO_SW_SESSIONS;
  13284. + } else
  13285. + ixp_sesnum *= 2;
  13286. +
  13287. + ixpd = kmalloc(ixp_sesnum * sizeof(struct ixp_data *), SLAB_ATOMIC);
  13288. + if (ixpd == NULL) {
  13289. + /* Reset session number */
  13290. + if (ixp_sesnum == CRYPTO_SW_SESSIONS)
  13291. + ixp_sesnum = 0;
  13292. + else
  13293. + ixp_sesnum /= 2;
  13294. + dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  13295. + return ENOBUFS;
  13296. + }
  13297. + memset(ixpd, 0, ixp_sesnum * sizeof(struct ixp_data *));
  13298. +
  13299. + /* Copy existing sessions */
  13300. + if (ixp_sessions) {
  13301. + memcpy(ixpd, ixp_sessions,
  13302. + (ixp_sesnum / 2) * sizeof(struct ixp_data *));
  13303. + kfree(ixp_sessions);
  13304. + }
  13305. +
  13306. + ixp_sessions = ixpd;
  13307. + }
  13308. +
  13309. + ixp_sessions[i] = (struct ixp_data *) kmalloc(sizeof(struct ixp_data),
  13310. + SLAB_ATOMIC);
  13311. + if (ixp_sessions[i] == NULL) {
  13312. + ixp_freesession(NULL, i);
  13313. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  13314. + return ENOBUFS;
  13315. + }
  13316. +
  13317. + *sid = i;
  13318. +
  13319. + ixp = ixp_sessions[i];
  13320. + memset(ixp, 0, sizeof(*ixp));
  13321. +
  13322. + ixp->ixp_cipher_alg = -1;
  13323. + ixp->ixp_auth_alg = -1;
  13324. + ixp->ixp_ctx_id = -1;
  13325. + INIT_LIST_HEAD(&ixp->ixp_q);
  13326. +
  13327. + ixp->ixp_ctx.useDifferentSrcAndDestMbufs = 0;
  13328. +
  13329. + while (cri) {
  13330. + switch (cri->cri_alg) {
  13331. + case CRYPTO_DES_CBC:
  13332. + ixp->ixp_cipher_alg = cri->cri_alg;
  13333. + ixp->ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_DES;
  13334. + ixp->ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  13335. + ixp->ixp_ctx.cipherCtx.cipherKeyLen = (cri->cri_klen + 7) / 8;
  13336. + ixp->ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
  13337. + ixp->ixp_ctx.cipherCtx.cipherInitialVectorLen =
  13338. + IX_CRYPTO_ACC_DES_IV_64;
  13339. + memcpy(ixp->ixp_ctx.cipherCtx.key.cipherKey,
  13340. + cri->cri_key, (cri->cri_klen + 7) / 8);
  13341. + break;
  13342. +
  13343. + case CRYPTO_3DES_CBC:
  13344. + ixp->ixp_cipher_alg = cri->cri_alg;
  13345. + ixp->ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_3DES;
  13346. + ixp->ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  13347. + ixp->ixp_ctx.cipherCtx.cipherKeyLen = (cri->cri_klen + 7) / 8;
  13348. + ixp->ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
  13349. + ixp->ixp_ctx.cipherCtx.cipherInitialVectorLen =
  13350. + IX_CRYPTO_ACC_DES_IV_64;
  13351. + memcpy(ixp->ixp_ctx.cipherCtx.key.cipherKey,
  13352. + cri->cri_key, (cri->cri_klen + 7) / 8);
  13353. + break;
  13354. +
  13355. + case CRYPTO_RIJNDAEL128_CBC:
  13356. + ixp->ixp_cipher_alg = cri->cri_alg;
  13357. + ixp->ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_AES;
  13358. + ixp->ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  13359. + ixp->ixp_ctx.cipherCtx.cipherKeyLen = (cri->cri_klen + 7) / 8;
  13360. + ixp->ixp_ctx.cipherCtx.cipherBlockLen = 16;
  13361. + ixp->ixp_ctx.cipherCtx.cipherInitialVectorLen = 16;
  13362. + memcpy(ixp->ixp_ctx.cipherCtx.key.cipherKey,
  13363. + cri->cri_key, (cri->cri_klen + 7) / 8);
  13364. + break;
  13365. +
  13366. + case CRYPTO_MD5:
  13367. + case CRYPTO_MD5_HMAC:
  13368. + ixp->ixp_auth_alg = cri->cri_alg;
  13369. + ixp->ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_MD5;
  13370. + ixp->ixp_ctx.authCtx.authDigestLen = AUTH_LEN(cri, MD5_HASH_LEN);
  13371. + ixp->ixp_ctx.authCtx.aadLen = 0;
  13372. + /* Only MD5_HMAC needs a key */
  13373. + if (cri->cri_alg == CRYPTO_MD5_HMAC) {
  13374. + ixp->ixp_ctx.authCtx.authKeyLen = (cri->cri_klen + 7) / 8;
  13375. + if (ixp->ixp_ctx.authCtx.authKeyLen >
  13376. + sizeof(ixp->ixp_ctx.authCtx.key.authKey)) {
  13377. + printk(
  13378. + "ixp4xx: Invalid key length for MD5_HMAC - %d bits\n",
  13379. + cri->cri_klen);
  13380. + ixp_freesession(NULL, i);
  13381. + return EINVAL;
  13382. + }
  13383. + memcpy(ixp->ixp_ctx.authCtx.key.authKey,
  13384. + cri->cri_key, (cri->cri_klen + 7) / 8);
  13385. + }
  13386. + break;
  13387. +
  13388. + case CRYPTO_SHA1:
  13389. + case CRYPTO_SHA1_HMAC:
  13390. + ixp->ixp_auth_alg = cri->cri_alg;
  13391. + ixp->ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_SHA1;
  13392. + ixp->ixp_ctx.authCtx.authDigestLen = AUTH_LEN(cri, SHA1_HASH_LEN);
  13393. + ixp->ixp_ctx.authCtx.aadLen = 0;
  13394. + /* Only SHA1_HMAC needs a key */
  13395. + if (cri->cri_alg == CRYPTO_SHA1_HMAC) {
  13396. + ixp->ixp_ctx.authCtx.authKeyLen = (cri->cri_klen + 7) / 8;
  13397. + if (ixp->ixp_ctx.authCtx.authKeyLen >
  13398. + sizeof(ixp->ixp_ctx.authCtx.key.authKey)) {
  13399. + printk(
  13400. + "ixp4xx: Invalid key length for SHA1_HMAC - %d bits\n",
  13401. + cri->cri_klen);
  13402. + ixp_freesession(NULL, i);
  13403. + return EINVAL;
  13404. + }
  13405. + memcpy(ixp->ixp_ctx.authCtx.key.authKey,
  13406. + cri->cri_key, (cri->cri_klen + 7) / 8);
  13407. + }
  13408. + break;
  13409. +
  13410. + default:
  13411. + printk("ixp: unknown algo 0x%x\n", cri->cri_alg);
  13412. + ixp_freesession(NULL, i);
  13413. + return EINVAL;
  13414. + }
  13415. + cri = cri->cri_next;
  13416. + }
  13417. +
  13418. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  13419. + INIT_WORK(&ixp->ixp_pending_work, ixp_process_pending_wq);
  13420. + INIT_WORK(&ixp->ixp_registration_work, ixp_registration_wq);
  13421. +#else
  13422. + INIT_WORK(&ixp->ixp_pending_work, ixp_process_pending, ixp);
  13423. + INIT_WORK(&ixp->ixp_registration_work, ixp_registration, ixp);
  13424. +#endif
  13425. +
  13426. + return 0;
  13427. +}
  13428. +
  13429. +
  13430. +/*
  13431. + * Free a session.
  13432. + */
  13433. +static int
  13434. +ixp_freesession(device_t dev, u_int64_t tid)
  13435. +{
  13436. + u_int32_t sid = CRYPTO_SESID2LID(tid);
  13437. +
  13438. + dprintk("%s()\n", __FUNCTION__);
  13439. + if (sid > ixp_sesnum || ixp_sessions == NULL ||
  13440. + ixp_sessions[sid] == NULL) {
  13441. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  13442. + return EINVAL;
  13443. + }
  13444. +
  13445. + /* Silently accept and return */
  13446. + if (sid == 0)
  13447. + return 0;
  13448. +
  13449. + if (ixp_sessions[sid]) {
  13450. + if (ixp_sessions[sid]->ixp_ctx_id != -1) {
  13451. + ixCryptoAccCtxUnregister(ixp_sessions[sid]->ixp_ctx_id);
  13452. + ixp_sessions[sid]->ixp_ctx_id = -1;
  13453. + }
  13454. +
  13455. + flush_scheduled_work();
  13456. +
  13457. + kfree(ixp_sessions[sid]);
  13458. + }
  13459. + ixp_sessions[sid] = NULL;
  13460. + if (ixp_blocked) {
  13461. + ixp_blocked = 0;
  13462. + crypto_unblock(ixp_id, CRYPTO_SYMQ);
  13463. + }
  13464. + return 0;
  13465. +}
  13466. +
  13467. +
  13468. +/*
  13469. + * callback for when hash processing is complete
  13470. + */
  13471. +
  13472. +static void
  13473. +ixp_hash_perform_cb(
  13474. + UINT32 hash_key_id,
  13475. + IX_MBUF *bufp,
  13476. + IxCryptoAccStatus status)
  13477. +{
  13478. + struct ixp_q *q;
  13479. +
  13480. + dprintk("%s(%u, %p, 0x%x)\n", __FUNCTION__, hash_key_id, bufp, status);
  13481. +
  13482. + if (bufp == NULL) {
  13483. + printk("ixp: NULL buf in %s\n", __FUNCTION__);
  13484. + return;
  13485. + }
  13486. +
  13487. + q = IX_MBUF_PRIV(bufp);
  13488. + if (q == NULL) {
  13489. + printk("ixp: NULL priv in %s\n", __FUNCTION__);
  13490. + return;
  13491. + }
  13492. +
  13493. + if (status == IX_CRYPTO_ACC_STATUS_SUCCESS) {
  13494. + /* On success, need to copy hash back into original client buffer */
  13495. + memcpy(q->ixp_hash_dest, q->ixp_hash_src,
  13496. + (q->ixp_q_data->ixp_auth_alg == CRYPTO_SHA1) ?
  13497. + SHA1_HASH_LEN : MD5_HASH_LEN);
  13498. + }
  13499. + else {
  13500. + printk("ixp: hash perform failed status=%d\n", status);
  13501. + q->ixp_q_crp->crp_etype = EINVAL;
  13502. + }
  13503. +
  13504. + /* Free internal buffer used for hashing */
  13505. + kfree(IX_MBUF_MDATA(&q->ixp_q_mbuf));
  13506. +
  13507. + crypto_done(q->ixp_q_crp);
  13508. + kmem_cache_free(qcache, q);
  13509. +}
  13510. +
  13511. +/*
  13512. + * setup a request and perform it
  13513. + */
  13514. +static void
  13515. +ixp_q_process(struct ixp_q *q)
  13516. +{
  13517. + IxCryptoAccStatus status;
  13518. + struct ixp_data *ixp = q->ixp_q_data;
  13519. + int auth_off = 0;
  13520. + int auth_len = 0;
  13521. + int crypt_off = 0;
  13522. + int crypt_len = 0;
  13523. + int icv_off = 0;
  13524. + char *crypt_func;
  13525. +
  13526. + dprintk("%s(%p)\n", __FUNCTION__, q);
  13527. +
  13528. + if (q->ixp_q_ccrd) {
  13529. + if (q->ixp_q_ccrd->crd_flags & CRD_F_IV_EXPLICIT) {
  13530. + q->ixp_q_iv = q->ixp_q_ccrd->crd_iv;
  13531. + } else {
  13532. + q->ixp_q_iv = q->ixp_q_iv_data;
  13533. + crypto_copydata(q->ixp_q_crp->crp_flags, q->ixp_q_crp->crp_buf,
  13534. + q->ixp_q_ccrd->crd_inject,
  13535. + ixp->ixp_ctx.cipherCtx.cipherInitialVectorLen,
  13536. + (caddr_t) q->ixp_q_iv);
  13537. + }
  13538. +
  13539. + if (q->ixp_q_acrd) {
  13540. + auth_off = q->ixp_q_acrd->crd_skip;
  13541. + auth_len = q->ixp_q_acrd->crd_len;
  13542. + icv_off = q->ixp_q_acrd->crd_inject;
  13543. + }
  13544. +
  13545. + crypt_off = q->ixp_q_ccrd->crd_skip;
  13546. + crypt_len = q->ixp_q_ccrd->crd_len;
  13547. + } else { /* if (q->ixp_q_acrd) */
  13548. + auth_off = q->ixp_q_acrd->crd_skip;
  13549. + auth_len = q->ixp_q_acrd->crd_len;
  13550. + icv_off = q->ixp_q_acrd->crd_inject;
  13551. + }
  13552. +
  13553. + if (q->ixp_q_crp->crp_flags & CRYPTO_F_SKBUF) {
  13554. + struct sk_buff *skb = (struct sk_buff *) q->ixp_q_crp->crp_buf;
  13555. + if (skb_shinfo(skb)->nr_frags) {
  13556. + /*
  13557. + * DAVIDM fix this limitation one day by using
  13558. + * a buffer pool and chaining, it is not currently
  13559. + * needed for current user/kernel space acceleration
  13560. + */
  13561. + printk("ixp: Cannot handle fragmented skb's yet !\n");
  13562. + q->ixp_q_crp->crp_etype = ENOENT;
  13563. + goto done;
  13564. + }
  13565. + IX_MBUF_MLEN(&q->ixp_q_mbuf) =
  13566. + IX_MBUF_PKT_LEN(&q->ixp_q_mbuf) = skb->len;
  13567. + IX_MBUF_MDATA(&q->ixp_q_mbuf) = skb->data;
  13568. + } else if (q->ixp_q_crp->crp_flags & CRYPTO_F_IOV) {
  13569. + struct uio *uiop = (struct uio *) q->ixp_q_crp->crp_buf;
  13570. + if (uiop->uio_iovcnt != 1) {
  13571. + /*
  13572. + * DAVIDM fix this limitation one day by using
  13573. + * a buffer pool and chaining, it is not currently
  13574. + * needed for current user/kernel space acceleration
  13575. + */
  13576. + printk("ixp: Cannot handle more than 1 iovec yet !\n");
  13577. + q->ixp_q_crp->crp_etype = ENOENT;
  13578. + goto done;
  13579. + }
  13580. + IX_MBUF_MLEN(&q->ixp_q_mbuf) =
  13581. + IX_MBUF_PKT_LEN(&q->ixp_q_mbuf) = uiop->uio_iov[0].iov_len;
  13582. + IX_MBUF_MDATA(&q->ixp_q_mbuf) = uiop->uio_iov[0].iov_base;
  13583. + } else /* contig buffer */ {
  13584. + IX_MBUF_MLEN(&q->ixp_q_mbuf) =
  13585. + IX_MBUF_PKT_LEN(&q->ixp_q_mbuf) = q->ixp_q_crp->crp_ilen;
  13586. + IX_MBUF_MDATA(&q->ixp_q_mbuf) = q->ixp_q_crp->crp_buf;
  13587. + }
  13588. +
  13589. + IX_MBUF_PRIV(&q->ixp_q_mbuf) = q;
  13590. +
  13591. + if (ixp->ixp_auth_alg == CRYPTO_SHA1 || ixp->ixp_auth_alg == CRYPTO_MD5) {
  13592. + /*
  13593. + * For SHA1 and MD5 hash, need to create an internal buffer that is big
  13594. + * enough to hold the original data + the appropriate padding for the
  13595. + * hash algorithm.
  13596. + */
  13597. + UINT8 *tbuf = NULL;
  13598. +
  13599. + IX_MBUF_MLEN(&q->ixp_q_mbuf) = IX_MBUF_PKT_LEN(&q->ixp_q_mbuf) =
  13600. + ((IX_MBUF_MLEN(&q->ixp_q_mbuf) * 8) + 72 + 511) / 8;
  13601. + tbuf = kmalloc(IX_MBUF_MLEN(&q->ixp_q_mbuf), SLAB_ATOMIC);
  13602. +
  13603. + if (IX_MBUF_MDATA(&q->ixp_q_mbuf) == NULL) {
  13604. + printk("ixp: kmalloc(%u, SLAB_ATOMIC) failed\n",
  13605. + IX_MBUF_MLEN(&q->ixp_q_mbuf));
  13606. + q->ixp_q_crp->crp_etype = ENOMEM;
  13607. + goto done;
  13608. + }
  13609. + memcpy(tbuf, &(IX_MBUF_MDATA(&q->ixp_q_mbuf))[auth_off], auth_len);
  13610. +
  13611. + /* Set location in client buffer to copy hash into */
  13612. + q->ixp_hash_dest =
  13613. + &(IX_MBUF_MDATA(&q->ixp_q_mbuf))[auth_off + auth_len];
  13614. +
  13615. + IX_MBUF_MDATA(&q->ixp_q_mbuf) = tbuf;
  13616. +
  13617. + /* Set location in internal buffer for where hash starts */
  13618. + q->ixp_hash_src = &(IX_MBUF_MDATA(&q->ixp_q_mbuf))[auth_len];
  13619. +
  13620. + crypt_func = "ixCryptoAccHashPerform";
  13621. + status = ixCryptoAccHashPerform(ixp->ixp_ctx.authCtx.authAlgo,
  13622. + &q->ixp_q_mbuf, ixp_hash_perform_cb, 0, auth_len, auth_len,
  13623. + &ixp->ixp_hash_key_id);
  13624. + }
  13625. + else {
  13626. + crypt_func = "ixCryptoAccAuthCryptPerform";
  13627. + status = ixCryptoAccAuthCryptPerform(ixp->ixp_ctx_id, &q->ixp_q_mbuf,
  13628. + NULL, auth_off, auth_len, crypt_off, crypt_len, icv_off,
  13629. + q->ixp_q_iv);
  13630. + }
  13631. +
  13632. + if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  13633. + return;
  13634. +
  13635. + if (IX_CRYPTO_ACC_STATUS_QUEUE_FULL == status) {
  13636. + q->ixp_q_crp->crp_etype = ENOMEM;
  13637. + goto done;
  13638. + }
  13639. +
  13640. + printk("ixp: %s failed %u\n", crypt_func, status);
  13641. + q->ixp_q_crp->crp_etype = EINVAL;
  13642. +
  13643. +done:
  13644. + crypto_done(q->ixp_q_crp);
  13645. + kmem_cache_free(qcache, q);
  13646. +}
  13647. +
  13648. +
  13649. +/*
  13650. + * because we cannot process the Q from the Register callback
  13651. + * we do it here on a task Q.
  13652. + */
  13653. +
  13654. +static void
  13655. +ixp_process_pending(void *arg)
  13656. +{
  13657. + struct ixp_data *ixp = arg;
  13658. + struct ixp_q *q = NULL;
  13659. +
  13660. + dprintk("%s(%p)\n", __FUNCTION__, arg);
  13661. +
  13662. + if (!ixp)
  13663. + return;
  13664. +
  13665. + while (!list_empty(&ixp->ixp_q)) {
  13666. + q = list_entry(ixp->ixp_q.next, struct ixp_q, ixp_q_list);
  13667. + list_del(&q->ixp_q_list);
  13668. + ixp_q_process(q);
  13669. + }
  13670. +}
  13671. +
  13672. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  13673. +static void
  13674. +ixp_process_pending_wq(struct work_struct *work)
  13675. +{
  13676. + struct ixp_data *ixp = container_of(work, struct ixp_data,
  13677. + ixp_pending_work);
  13678. + ixp_process_pending(ixp);
  13679. +}
  13680. +#endif
  13681. +
  13682. +/*
  13683. + * callback for when context registration is complete
  13684. + */
  13685. +
  13686. +static void
  13687. +ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp, IxCryptoAccStatus status)
  13688. +{
  13689. + int i;
  13690. + struct ixp_data *ixp;
  13691. + struct ixp_q *q;
  13692. +
  13693. + dprintk("%s(%d, %p, %d)\n", __FUNCTION__, ctx_id, bufp, status);
  13694. +
  13695. + /*
  13696. + * free any buffer passed in to this routine
  13697. + */
  13698. + if (bufp) {
  13699. + IX_MBUF_MLEN(bufp) = IX_MBUF_PKT_LEN(bufp) = 0;
  13700. + kfree(IX_MBUF_MDATA(bufp));
  13701. + IX_MBUF_MDATA(bufp) = NULL;
  13702. + }
  13703. +
  13704. + for (i = 0; i < ixp_sesnum; i++) {
  13705. + ixp = ixp_sessions[i];
  13706. + if (ixp && ixp->ixp_ctx_id == ctx_id)
  13707. + break;
  13708. + }
  13709. + if (i >= ixp_sesnum) {
  13710. + printk("ixp: invalid context id %d\n", ctx_id);
  13711. + return;
  13712. + }
  13713. +
  13714. + if (IX_CRYPTO_ACC_STATUS_WAIT == status) {
  13715. + /* this is normal to free the first of two buffers */
  13716. + dprintk("ixp: register not finished yet.\n");
  13717. + return;
  13718. + }
  13719. +
  13720. + if (IX_CRYPTO_ACC_STATUS_SUCCESS != status) {
  13721. + printk("ixp: register failed 0x%x\n", status);
  13722. + while (!list_empty(&ixp->ixp_q)) {
  13723. + q = list_entry(ixp->ixp_q.next, struct ixp_q, ixp_q_list);
  13724. + list_del(&q->ixp_q_list);
  13725. + q->ixp_q_crp->crp_etype = EINVAL;
  13726. + crypto_done(q->ixp_q_crp);
  13727. + kmem_cache_free(qcache, q);
  13728. + }
  13729. + return;
  13730. + }
  13731. +
  13732. + /*
  13733. + * we are now registered, we cannot start processing the Q here
  13734. + * or we get strange errors with AES (DES/3DES seem to be ok).
  13735. + */
  13736. + ixp->ixp_registered = 1;
  13737. + schedule_work(&ixp->ixp_pending_work);
  13738. +}
  13739. +
  13740. +
  13741. +/*
  13742. + * callback for when data processing is complete
  13743. + */
  13744. +
  13745. +static void
  13746. +ixp_perform_cb(
  13747. + UINT32 ctx_id,
  13748. + IX_MBUF *sbufp,
  13749. + IX_MBUF *dbufp,
  13750. + IxCryptoAccStatus status)
  13751. +{
  13752. + struct ixp_q *q;
  13753. +
  13754. + dprintk("%s(%d, %p, %p, 0x%x)\n", __FUNCTION__, ctx_id, sbufp,
  13755. + dbufp, status);
  13756. +
  13757. + if (sbufp == NULL) {
  13758. + printk("ixp: NULL sbuf in ixp_perform_cb\n");
  13759. + return;
  13760. + }
  13761. +
  13762. + q = IX_MBUF_PRIV(sbufp);
  13763. + if (q == NULL) {
  13764. + printk("ixp: NULL priv in ixp_perform_cb\n");
  13765. + return;
  13766. + }
  13767. +
  13768. + if (status != IX_CRYPTO_ACC_STATUS_SUCCESS) {
  13769. + printk("ixp: perform failed status=%d\n", status);
  13770. + q->ixp_q_crp->crp_etype = EINVAL;
  13771. + }
  13772. +
  13773. + crypto_done(q->ixp_q_crp);
  13774. + kmem_cache_free(qcache, q);
  13775. +}
  13776. +
  13777. +
  13778. +/*
  13779. + * registration is not callable at IRQ time, so we defer
  13780. + * to a task queue, this routines completes the registration for us
  13781. + * when the task queue runs
  13782. + *
  13783. + * Unfortunately this means we cannot tell OCF that the driver is blocked,
  13784. + * we do that on the next request.
  13785. + */
  13786. +
  13787. +static void
  13788. +ixp_registration(void *arg)
  13789. +{
  13790. + struct ixp_data *ixp = arg;
  13791. + struct ixp_q *q = NULL;
  13792. + IX_MBUF *pri = NULL, *sec = NULL;
  13793. + int status = IX_CRYPTO_ACC_STATUS_SUCCESS;
  13794. +
  13795. + if (!ixp) {
  13796. + printk("ixp: ixp_registration with no arg\n");
  13797. + return;
  13798. + }
  13799. +
  13800. + if (ixp->ixp_ctx_id != -1) {
  13801. + ixCryptoAccCtxUnregister(ixp->ixp_ctx_id);
  13802. + ixp->ixp_ctx_id = -1;
  13803. + }
  13804. +
  13805. + if (list_empty(&ixp->ixp_q)) {
  13806. + printk("ixp: ixp_registration with no Q\n");
  13807. + return;
  13808. + }
  13809. +
  13810. + /*
  13811. + * setup the primary and secondary buffers
  13812. + */
  13813. + q = list_entry(ixp->ixp_q.next, struct ixp_q, ixp_q_list);
  13814. + if (q->ixp_q_acrd) {
  13815. + pri = &ixp->ixp_pri_mbuf;
  13816. + sec = &ixp->ixp_sec_mbuf;
  13817. + IX_MBUF_MLEN(pri) = IX_MBUF_PKT_LEN(pri) = 128;
  13818. + IX_MBUF_MDATA(pri) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  13819. + IX_MBUF_MLEN(sec) = IX_MBUF_PKT_LEN(sec) = 128;
  13820. + IX_MBUF_MDATA(sec) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  13821. + }
  13822. +
  13823. + /* Only need to register if a crypt op or HMAC op */
  13824. + if (!(ixp->ixp_auth_alg == CRYPTO_SHA1 ||
  13825. + ixp->ixp_auth_alg == CRYPTO_MD5)) {
  13826. + status = ixCryptoAccCtxRegister(
  13827. + &ixp->ixp_ctx,
  13828. + pri, sec,
  13829. + ixp_register_cb,
  13830. + ixp_perform_cb,
  13831. + &ixp->ixp_ctx_id);
  13832. + }
  13833. + else {
  13834. + /* Otherwise we start processing pending q */
  13835. + schedule_work(&ixp->ixp_pending_work);
  13836. + }
  13837. +
  13838. + if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  13839. + return;
  13840. +
  13841. + if (IX_CRYPTO_ACC_STATUS_EXCEED_MAX_TUNNELS == status) {
  13842. + printk("ixp: ixCryptoAccCtxRegister failed (out of tunnels)\n");
  13843. + ixp_blocked = 1;
  13844. + /* perhaps we should return EGAIN on queued ops ? */
  13845. + return;
  13846. + }
  13847. +
  13848. + printk("ixp: ixCryptoAccCtxRegister failed %d\n", status);
  13849. + ixp->ixp_ctx_id = -1;
  13850. +
  13851. + /*
  13852. + * everything waiting is toasted
  13853. + */
  13854. + while (!list_empty(&ixp->ixp_q)) {
  13855. + q = list_entry(ixp->ixp_q.next, struct ixp_q, ixp_q_list);
  13856. + list_del(&q->ixp_q_list);
  13857. + q->ixp_q_crp->crp_etype = ENOENT;
  13858. + crypto_done(q->ixp_q_crp);
  13859. + kmem_cache_free(qcache, q);
  13860. + }
  13861. +}
  13862. +
  13863. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  13864. +static void
  13865. +ixp_registration_wq(struct work_struct *work)
  13866. +{
  13867. + struct ixp_data *ixp = container_of(work, struct ixp_data,
  13868. + ixp_registration_work);
  13869. + ixp_registration(ixp);
  13870. +}
  13871. +#endif
  13872. +
  13873. +/*
  13874. + * Process a request.
  13875. + */
  13876. +static int
  13877. +ixp_process(device_t dev, struct cryptop *crp, int hint)
  13878. +{
  13879. + struct ixp_data *ixp;
  13880. + unsigned int lid;
  13881. + struct ixp_q *q = NULL;
  13882. + int status;
  13883. +
  13884. + dprintk("%s()\n", __FUNCTION__);
  13885. +
  13886. + /* Sanity check */
  13887. + if (crp == NULL) {
  13888. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  13889. + return EINVAL;
  13890. + }
  13891. +
  13892. + crp->crp_etype = 0;
  13893. +
  13894. + if (ixp_blocked)
  13895. + return ERESTART;
  13896. +
  13897. + if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  13898. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  13899. + crp->crp_etype = EINVAL;
  13900. + goto done;
  13901. + }
  13902. +
  13903. + /*
  13904. + * find the session we are using
  13905. + */
  13906. +
  13907. + lid = crp->crp_sid & 0xffffffff;
  13908. + if (lid >= ixp_sesnum || lid == 0 || ixp_sessions == NULL ||
  13909. + ixp_sessions[lid] == NULL) {
  13910. + crp->crp_etype = ENOENT;
  13911. + dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  13912. + goto done;
  13913. + }
  13914. + ixp = ixp_sessions[lid];
  13915. +
  13916. + /*
  13917. + * setup a new request ready for queuing
  13918. + */
  13919. + q = kmem_cache_alloc(qcache, SLAB_ATOMIC);
  13920. + if (q == NULL) {
  13921. + dprintk("%s,%d: ENOMEM\n", __FILE__, __LINE__);
  13922. + crp->crp_etype = ENOMEM;
  13923. + goto done;
  13924. + }
  13925. + /*
  13926. + * save some cycles by only zeroing the important bits
  13927. + */
  13928. + memset(&q->ixp_q_mbuf, 0, sizeof(q->ixp_q_mbuf));
  13929. + q->ixp_q_ccrd = NULL;
  13930. + q->ixp_q_acrd = NULL;
  13931. + q->ixp_q_crp = crp;
  13932. + q->ixp_q_data = ixp;
  13933. +
  13934. + /*
  13935. + * point the cipher and auth descriptors appropriately
  13936. + * check that we have something to do
  13937. + */
  13938. + if (crp->crp_desc->crd_alg == ixp->ixp_cipher_alg)
  13939. + q->ixp_q_ccrd = crp->crp_desc;
  13940. + else if (crp->crp_desc->crd_alg == ixp->ixp_auth_alg)
  13941. + q->ixp_q_acrd = crp->crp_desc;
  13942. + else {
  13943. + crp->crp_etype = ENOENT;
  13944. + dprintk("%s,%d: bad desc match: ENOENT\n", __FILE__, __LINE__);
  13945. + goto done;
  13946. + }
  13947. + if (crp->crp_desc->crd_next) {
  13948. + if (crp->crp_desc->crd_next->crd_alg == ixp->ixp_cipher_alg)
  13949. + q->ixp_q_ccrd = crp->crp_desc->crd_next;
  13950. + else if (crp->crp_desc->crd_next->crd_alg == ixp->ixp_auth_alg)
  13951. + q->ixp_q_acrd = crp->crp_desc->crd_next;
  13952. + else {
  13953. + crp->crp_etype = ENOENT;
  13954. + dprintk("%s,%d: bad desc match: ENOENT\n", __FILE__, __LINE__);
  13955. + goto done;
  13956. + }
  13957. + }
  13958. +
  13959. + /*
  13960. + * If there is a direction change for this context then we mark it as
  13961. + * unregistered and re-register is for the new direction. This is not
  13962. + * a very expensive operation and currently only tends to happen when
  13963. + * user-space application are doing benchmarks
  13964. + *
  13965. + * DM - we should be checking for pending requests before unregistering.
  13966. + */
  13967. + if (q->ixp_q_ccrd && ixp->ixp_registered &&
  13968. + ixp->ixp_crd_flags != (q->ixp_q_ccrd->crd_flags & CRD_F_ENCRYPT)) {
  13969. + dprintk("%s - detected direction change on session\n", __FUNCTION__);
  13970. + ixp->ixp_registered = 0;
  13971. + }
  13972. +
  13973. + /*
  13974. + * if we are registered, call straight into the perform code
  13975. + */
  13976. + if (ixp->ixp_registered) {
  13977. + ixp_q_process(q);
  13978. + return 0;
  13979. + }
  13980. +
  13981. + /*
  13982. + * the only part of the context not set in newsession is the direction
  13983. + * dependent parts
  13984. + */
  13985. + if (q->ixp_q_ccrd) {
  13986. + ixp->ixp_crd_flags = (q->ixp_q_ccrd->crd_flags & CRD_F_ENCRYPT);
  13987. + if (q->ixp_q_ccrd->crd_flags & CRD_F_ENCRYPT) {
  13988. + ixp->ixp_ctx.operation = q->ixp_q_acrd ?
  13989. + IX_CRYPTO_ACC_OP_ENCRYPT_AUTH : IX_CRYPTO_ACC_OP_ENCRYPT;
  13990. + } else {
  13991. + ixp->ixp_ctx.operation = q->ixp_q_acrd ?
  13992. + IX_CRYPTO_ACC_OP_AUTH_DECRYPT : IX_CRYPTO_ACC_OP_DECRYPT;
  13993. + }
  13994. + } else {
  13995. + /* q->ixp_q_acrd must be set if we are here */
  13996. + ixp->ixp_ctx.operation = IX_CRYPTO_ACC_OP_AUTH_CALC;
  13997. + }
  13998. +
  13999. + status = list_empty(&ixp->ixp_q);
  14000. + list_add_tail(&q->ixp_q_list, &ixp->ixp_q);
  14001. + if (status)
  14002. + schedule_work(&ixp->ixp_registration_work);
  14003. + return 0;
  14004. +
  14005. +done:
  14006. + if (q)
  14007. + kmem_cache_free(qcache, q);
  14008. + crypto_done(crp);
  14009. + return 0;
  14010. +}
  14011. +
  14012. +
  14013. +#ifdef __ixp46X
  14014. +/*
  14015. + * key processing support for the ixp465
  14016. + */
  14017. +
  14018. +
  14019. +/*
  14020. + * copy a BN (LE) into a buffer (BE) an fill out the op appropriately
  14021. + * assume zeroed and only copy bits that are significant
  14022. + */
  14023. +
  14024. +static int
  14025. +ixp_copy_ibuf(struct crparam *p, IxCryptoAccPkeEauOperand *op, UINT32 *buf)
  14026. +{
  14027. + unsigned char *src = (unsigned char *) p->crp_p;
  14028. + unsigned char *dst;
  14029. + int len, bits = p->crp_nbits;
  14030. +
  14031. + dprintk("%s()\n", __FUNCTION__);
  14032. +
  14033. + if (bits > MAX_IOP_SIZE * sizeof(UINT32) * 8) {
  14034. + dprintk("%s - ibuf too big (%d > %d)\n", __FUNCTION__,
  14035. + bits, MAX_IOP_SIZE * sizeof(UINT32) * 8);
  14036. + return -1;
  14037. + }
  14038. +
  14039. + len = (bits + 31) / 32; /* the number UINT32's needed */
  14040. +
  14041. + dst = (unsigned char *) &buf[len];
  14042. + dst--;
  14043. +
  14044. + while (bits > 0) {
  14045. + *dst-- = *src++;
  14046. + bits -= 8;
  14047. + }
  14048. +
  14049. +#if 0 /* no need to zero remaining bits as it is done during request alloc */
  14050. + while (dst > (unsigned char *) buf)
  14051. + *dst-- = '\0';
  14052. +#endif
  14053. +
  14054. + op->pData = buf;
  14055. + op->dataLen = len;
  14056. + return 0;
  14057. +}
  14058. +
  14059. +/*
  14060. + * copy out the result, be as forgiving as we can about small output buffers
  14061. + */
  14062. +
  14063. +static int
  14064. +ixp_copy_obuf(struct crparam *p, IxCryptoAccPkeEauOpResult *op, UINT32 *buf)
  14065. +{
  14066. + unsigned char *dst = (unsigned char *) p->crp_p;
  14067. + unsigned char *src = (unsigned char *) buf;
  14068. + int len, z, bits = p->crp_nbits;
  14069. +
  14070. + dprintk("%s()\n", __FUNCTION__);
  14071. +
  14072. + len = op->dataLen * sizeof(UINT32);
  14073. +
  14074. + /* skip leading zeroes to be small buffer friendly */
  14075. + z = 0;
  14076. + while (z < len && src[z] == '\0')
  14077. + z++;
  14078. +
  14079. + src += len;
  14080. + src--;
  14081. + len -= z;
  14082. +
  14083. + while (len > 0 && bits > 0) {
  14084. + *dst++ = *src--;
  14085. + len--;
  14086. + bits -= 8;
  14087. + }
  14088. +
  14089. + while (bits > 0) {
  14090. + *dst++ = '\0';
  14091. + bits -= 8;
  14092. + }
  14093. +
  14094. + if (len > 0) {
  14095. + dprintk("%s - obuf is %d (z=%d, ob=%d) bytes too small\n",
  14096. + __FUNCTION__, len, z, p->crp_nbits / 8);
  14097. + return -1;
  14098. + }
  14099. +
  14100. + return 0;
  14101. +}
  14102. +
  14103. +
  14104. +/*
  14105. + * the parameter offsets for exp_mod
  14106. + */
  14107. +
  14108. +#define IXP_PARAM_BASE 0
  14109. +#define IXP_PARAM_EXP 1
  14110. +#define IXP_PARAM_MOD 2
  14111. +#define IXP_PARAM_RES 3
  14112. +
  14113. +/*
  14114. + * key processing complete callback, is also used to start processing
  14115. + * by passing a NULL for pResult
  14116. + */
  14117. +
  14118. +static void
  14119. +ixp_kperform_cb(
  14120. + IxCryptoAccPkeEauOperation operation,
  14121. + IxCryptoAccPkeEauOpResult *pResult,
  14122. + BOOL carryOrBorrow,
  14123. + IxCryptoAccStatus status)
  14124. +{
  14125. + struct ixp_pkq *q, *tmp;
  14126. + unsigned long flags;
  14127. +
  14128. + dprintk("%s(0x%x, %p, %d, 0x%x)\n", __FUNCTION__, operation, pResult,
  14129. + carryOrBorrow, status);
  14130. +
  14131. + /* handle a completed request */
  14132. + if (pResult) {
  14133. + if (ixp_pk_cur && &ixp_pk_cur->pkq_result == pResult) {
  14134. + q = ixp_pk_cur;
  14135. + if (status != IX_CRYPTO_ACC_STATUS_SUCCESS) {
  14136. + dprintk("%s() - op failed 0x%x\n", __FUNCTION__, status);
  14137. + q->pkq_krp->krp_status = ERANGE; /* could do better */
  14138. + } else {
  14139. + /* copy out the result */
  14140. + if (ixp_copy_obuf(&q->pkq_krp->krp_param[IXP_PARAM_RES],
  14141. + &q->pkq_result, q->pkq_obuf))
  14142. + q->pkq_krp->krp_status = ERANGE;
  14143. + }
  14144. + crypto_kdone(q->pkq_krp);
  14145. + kfree(q);
  14146. + ixp_pk_cur = NULL;
  14147. + } else
  14148. + printk("%s - callback with invalid result pointer\n", __FUNCTION__);
  14149. + }
  14150. +
  14151. + spin_lock_irqsave(&ixp_pkq_lock, flags);
  14152. + if (ixp_pk_cur || list_empty(&ixp_pkq)) {
  14153. + spin_unlock_irqrestore(&ixp_pkq_lock, flags);
  14154. + return;
  14155. + }
  14156. +
  14157. + list_for_each_entry_safe(q, tmp, &ixp_pkq, pkq_list) {
  14158. +
  14159. + list_del(&q->pkq_list);
  14160. + ixp_pk_cur = q;
  14161. +
  14162. + spin_unlock_irqrestore(&ixp_pkq_lock, flags);
  14163. +
  14164. + status = ixCryptoAccPkeEauPerform(
  14165. + IX_CRYPTO_ACC_OP_EAU_MOD_EXP,
  14166. + &q->pkq_op,
  14167. + ixp_kperform_cb,
  14168. + &q->pkq_result);
  14169. +
  14170. + if (status == IX_CRYPTO_ACC_STATUS_SUCCESS) {
  14171. + dprintk("%s() - ixCryptoAccPkeEauPerform SUCCESS\n", __FUNCTION__);
  14172. + return; /* callback will return here for callback */
  14173. + } else if (status == IX_CRYPTO_ACC_STATUS_RETRY) {
  14174. + printk("%s() - ixCryptoAccPkeEauPerform RETRY\n", __FUNCTION__);
  14175. + } else {
  14176. + printk("%s() - ixCryptoAccPkeEauPerform failed %d\n",
  14177. + __FUNCTION__, status);
  14178. + }
  14179. + q->pkq_krp->krp_status = ERANGE; /* could do better */
  14180. + crypto_kdone(q->pkq_krp);
  14181. + kfree(q);
  14182. + spin_lock_irqsave(&ixp_pkq_lock, flags);
  14183. + }
  14184. + spin_unlock_irqrestore(&ixp_pkq_lock, flags);
  14185. +}
  14186. +
  14187. +
  14188. +static int
  14189. +ixp_kprocess(device_t dev, struct cryptkop *krp, int hint)
  14190. +{
  14191. + struct ixp_pkq *q;
  14192. + int rc = 0;
  14193. + unsigned long flags;
  14194. +
  14195. + dprintk("%s l1=%d l2=%d l3=%d l4=%d\n", __FUNCTION__,
  14196. + krp->krp_param[IXP_PARAM_BASE].crp_nbits,
  14197. + krp->krp_param[IXP_PARAM_EXP].crp_nbits,
  14198. + krp->krp_param[IXP_PARAM_MOD].crp_nbits,
  14199. + krp->krp_param[IXP_PARAM_RES].crp_nbits);
  14200. +
  14201. +
  14202. + if (krp->krp_op != CRK_MOD_EXP) {
  14203. + krp->krp_status = EOPNOTSUPP;
  14204. + goto err;
  14205. + }
  14206. +
  14207. + q = (struct ixp_pkq *) kmalloc(sizeof(*q), GFP_KERNEL);
  14208. + if (q == NULL) {
  14209. + krp->krp_status = ENOMEM;
  14210. + goto err;
  14211. + }
  14212. +
  14213. + /*
  14214. + * The PKE engine does not appear to zero the output buffer
  14215. + * appropriately, so we need to do it all here.
  14216. + */
  14217. + memset(q, 0, sizeof(*q));
  14218. +
  14219. + q->pkq_krp = krp;
  14220. + INIT_LIST_HEAD(&q->pkq_list);
  14221. +
  14222. + if (ixp_copy_ibuf(&krp->krp_param[IXP_PARAM_BASE], &q->pkq_op.modExpOpr.M,
  14223. + q->pkq_ibuf0))
  14224. + rc = 1;
  14225. + if (!rc && ixp_copy_ibuf(&krp->krp_param[IXP_PARAM_EXP],
  14226. + &q->pkq_op.modExpOpr.e, q->pkq_ibuf1))
  14227. + rc = 2;
  14228. + if (!rc && ixp_copy_ibuf(&krp->krp_param[IXP_PARAM_MOD],
  14229. + &q->pkq_op.modExpOpr.N, q->pkq_ibuf2))
  14230. + rc = 3;
  14231. +
  14232. + if (rc) {
  14233. + kfree(q);
  14234. + krp->krp_status = ERANGE;
  14235. + goto err;
  14236. + }
  14237. +
  14238. + q->pkq_result.pData = q->pkq_obuf;
  14239. + q->pkq_result.dataLen =
  14240. + (krp->krp_param[IXP_PARAM_RES].crp_nbits + 31) / 32;
  14241. +
  14242. + spin_lock_irqsave(&ixp_pkq_lock, flags);
  14243. + list_add_tail(&q->pkq_list, &ixp_pkq);
  14244. + spin_unlock_irqrestore(&ixp_pkq_lock, flags);
  14245. +
  14246. + if (!ixp_pk_cur)
  14247. + ixp_kperform_cb(0, NULL, 0, 0);
  14248. + return (0);
  14249. +
  14250. +err:
  14251. + crypto_kdone(krp);
  14252. + return (0);
  14253. +}
  14254. +
  14255. +
  14256. +
  14257. +#ifdef CONFIG_OCF_RANDOMHARVEST
  14258. +/*
  14259. + * We run the random number generator output through SHA so that it
  14260. + * is FIPS compliant.
  14261. + */
  14262. +
  14263. +static volatile int sha_done = 0;
  14264. +static unsigned char sha_digest[20];
  14265. +
  14266. +static void
  14267. +ixp_hash_cb(UINT8 *digest, IxCryptoAccStatus status)
  14268. +{
  14269. + dprintk("%s(%p, %d)\n", __FUNCTION__, digest, status);
  14270. + if (sha_digest != digest)
  14271. + printk("digest error\n");
  14272. + if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  14273. + sha_done = 1;
  14274. + else
  14275. + sha_done = -status;
  14276. +}
  14277. +
  14278. +static int
  14279. +ixp_read_random(void *arg, u_int32_t *buf, int maxwords)
  14280. +{
  14281. + IxCryptoAccStatus status;
  14282. + int i, n, rc;
  14283. +
  14284. + dprintk("%s(%p, %d)\n", __FUNCTION__, buf, maxwords);
  14285. + memset(buf, 0, maxwords * sizeof(*buf));
  14286. + status = ixCryptoAccPkePseudoRandomNumberGet(maxwords, buf);
  14287. + if (status != IX_CRYPTO_ACC_STATUS_SUCCESS) {
  14288. + dprintk("%s: ixCryptoAccPkePseudoRandomNumberGet failed %d\n",
  14289. + __FUNCTION__, status);
  14290. + return 0;
  14291. + }
  14292. +
  14293. + /*
  14294. + * run the random data through SHA to make it look more random
  14295. + */
  14296. +
  14297. + n = sizeof(sha_digest); /* process digest bytes at a time */
  14298. +
  14299. + rc = 0;
  14300. + for (i = 0; i < maxwords; i += n / sizeof(*buf)) {
  14301. + if ((maxwords - i) * sizeof(*buf) < n)
  14302. + n = (maxwords - i) * sizeof(*buf);
  14303. + sha_done = 0;
  14304. + status = ixCryptoAccPkeHashPerform(IX_CRYPTO_ACC_AUTH_SHA1,
  14305. + (UINT8 *) &buf[i], n, ixp_hash_cb, sha_digest);
  14306. + if (status != IX_CRYPTO_ACC_STATUS_SUCCESS) {
  14307. + dprintk("ixCryptoAccPkeHashPerform failed %d\n", status);
  14308. + return -EIO;
  14309. + }
  14310. + while (!sha_done)
  14311. + schedule();
  14312. + if (sha_done < 0) {
  14313. + dprintk("ixCryptoAccPkeHashPerform failed CB %d\n", -sha_done);
  14314. + return 0;
  14315. + }
  14316. + memcpy(&buf[i], sha_digest, n);
  14317. + rc += n / sizeof(*buf);;
  14318. + }
  14319. +
  14320. + return rc;
  14321. +}
  14322. +#endif /* CONFIG_OCF_RANDOMHARVEST */
  14323. +
  14324. +#endif /* __ixp46X */
  14325. +
  14326. +
  14327. +
  14328. +/*
  14329. + * our driver startup and shutdown routines
  14330. + */
  14331. +
  14332. +static int
  14333. +ixp_init(void)
  14334. +{
  14335. + dprintk("%s(%p)\n", __FUNCTION__, ixp_init);
  14336. +
  14337. + if (ixp_init_crypto && ixCryptoAccInit() != IX_CRYPTO_ACC_STATUS_SUCCESS)
  14338. + printk("ixCryptoAccInit failed, assuming already initialised!\n");
  14339. +
  14340. + qcache = kmem_cache_create("ixp4xx_q", sizeof(struct ixp_q), 0,
  14341. + SLAB_HWCACHE_ALIGN, NULL
  14342. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  14343. + , NULL
  14344. +#endif
  14345. + );
  14346. + if (!qcache) {
  14347. + printk("failed to create Qcache\n");
  14348. + return -ENOENT;
  14349. + }
  14350. +
  14351. + memset(&ixpdev, 0, sizeof(ixpdev));
  14352. + softc_device_init(&ixpdev, "ixp4xx", 0, ixp_methods);
  14353. +
  14354. + ixp_id = crypto_get_driverid(softc_get_device(&ixpdev),
  14355. + CRYPTOCAP_F_HARDWARE);
  14356. + if (ixp_id < 0)
  14357. + panic("IXP/OCF crypto device cannot initialize!");
  14358. +
  14359. +#define REGISTER(alg) \
  14360. + crypto_register(ixp_id,alg,0,0)
  14361. +
  14362. + REGISTER(CRYPTO_DES_CBC);
  14363. + REGISTER(CRYPTO_3DES_CBC);
  14364. + REGISTER(CRYPTO_RIJNDAEL128_CBC);
  14365. +#ifdef CONFIG_OCF_IXP4XX_SHA1_MD5
  14366. + REGISTER(CRYPTO_MD5);
  14367. + REGISTER(CRYPTO_SHA1);
  14368. +#endif
  14369. + REGISTER(CRYPTO_MD5_HMAC);
  14370. + REGISTER(CRYPTO_SHA1_HMAC);
  14371. +#undef REGISTER
  14372. +
  14373. +#ifdef __ixp46X
  14374. + spin_lock_init(&ixp_pkq_lock);
  14375. + /*
  14376. + * we do not enable the go fast options here as they can potentially
  14377. + * allow timing based attacks
  14378. + *
  14379. + * http://www.openssl.org/news/secadv_20030219.txt
  14380. + */
  14381. + ixCryptoAccPkeEauExpConfig(0, 0);
  14382. + crypto_kregister(ixp_id, CRK_MOD_EXP, 0);
  14383. +#ifdef CONFIG_OCF_RANDOMHARVEST
  14384. + crypto_rregister(ixp_id, ixp_read_random, NULL);
  14385. +#endif
  14386. +#endif
  14387. +
  14388. + return 0;
  14389. +}
  14390. +
  14391. +static void
  14392. +ixp_exit(void)
  14393. +{
  14394. + dprintk("%s()\n", __FUNCTION__);
  14395. + crypto_unregister_all(ixp_id);
  14396. + ixp_id = -1;
  14397. + kmem_cache_destroy(qcache);
  14398. + qcache = NULL;
  14399. +}
  14400. +
  14401. +module_init(ixp_init);
  14402. +module_exit(ixp_exit);
  14403. +
  14404. +MODULE_LICENSE("Dual BSD/GPL");
  14405. +MODULE_AUTHOR("David McCullough <dmccullough@cyberguard.com>");
  14406. +MODULE_DESCRIPTION("ixp (OCF module for IXP4xx crypto)");
  14407. diff -Nur linux-2.6.30.orig/crypto/ocf/ixp4xx/Makefile linux-2.6.30/crypto/ocf/ixp4xx/Makefile
  14408. --- linux-2.6.30.orig/crypto/ocf/ixp4xx/Makefile 1970-01-01 01:00:00.000000000 +0100
  14409. +++ linux-2.6.30/crypto/ocf/ixp4xx/Makefile 2009-06-11 10:55:27.000000000 +0200
  14410. @@ -0,0 +1,104 @@
  14411. +# for SGlinux builds
  14412. +-include $(ROOTDIR)/modules/.config
  14413. +
  14414. +#
  14415. +# You will need to point this at your Intel ixp425 includes, this portion
  14416. +# of the Makefile only really works under SGLinux with the appropriate libs
  14417. +# installed. They can be downloaded from http://www.snapgear.org/
  14418. +#
  14419. +ifeq ($(CONFIG_CPU_IXP46X),y)
  14420. +IXPLATFORM = ixp46X
  14421. +else
  14422. +ifeq ($(CONFIG_CPU_IXP43X),y)
  14423. +IXPLATFORM = ixp43X
  14424. +else
  14425. +IXPLATFORM = ixp42X
  14426. +endif
  14427. +endif
  14428. +
  14429. +ifdef CONFIG_IXP400_LIB_2_4
  14430. +IX_XSCALE_SW = $(ROOTDIR)/modules/ixp425/ixp400-2.4/ixp400_xscale_sw
  14431. +OSAL_DIR = $(ROOTDIR)/modules/ixp425/ixp400-2.4/ixp_osal
  14432. +endif
  14433. +ifdef CONFIG_IXP400_LIB_2_1
  14434. +IX_XSCALE_SW = $(ROOTDIR)/modules/ixp425/ixp400-2.1/ixp400_xscale_sw
  14435. +OSAL_DIR = $(ROOTDIR)/modules/ixp425/ixp400-2.1/ixp_osal
  14436. +endif
  14437. +ifdef CONFIG_IXP400_LIB_2_0
  14438. +IX_XSCALE_SW = $(ROOTDIR)/modules/ixp425/ixp400-2.0/ixp400_xscale_sw
  14439. +OSAL_DIR = $(ROOTDIR)/modules/ixp425/ixp400-2.0/ixp_osal
  14440. +endif
  14441. +ifdef IX_XSCALE_SW
  14442. +ifdef CONFIG_IXP400_LIB_2_4
  14443. +IXP_CFLAGS = \
  14444. + -I$(ROOTDIR)/. \
  14445. + -I$(IX_XSCALE_SW)/src/include \
  14446. + -I$(OSAL_DIR)/common/include/ \
  14447. + -I$(OSAL_DIR)/common/include/modules/ \
  14448. + -I$(OSAL_DIR)/common/include/modules/ddk/ \
  14449. + -I$(OSAL_DIR)/common/include/modules/bufferMgt/ \
  14450. + -I$(OSAL_DIR)/common/include/modules/ioMem/ \
  14451. + -I$(OSAL_DIR)/common/os/linux/include/ \
  14452. + -I$(OSAL_DIR)/common/os/linux/include/core/ \
  14453. + -I$(OSAL_DIR)/common/os/linux/include/modules/ \
  14454. + -I$(OSAL_DIR)/common/os/linux/include/modules/ddk/ \
  14455. + -I$(OSAL_DIR)/common/os/linux/include/modules/bufferMgt/ \
  14456. + -I$(OSAL_DIR)/common/os/linux/include/modules/ioMem/ \
  14457. + -I$(OSAL_DIR)/platforms/$(IXPLATFORM)/include/ \
  14458. + -I$(OSAL_DIR)/platforms/$(IXPLATFORM)/os/linux/include/ \
  14459. + -DENABLE_IOMEM -DENABLE_BUFFERMGT -DENABLE_DDK \
  14460. + -DUSE_IXP4XX_CRYPTO
  14461. +else
  14462. +IXP_CFLAGS = \
  14463. + -I$(ROOTDIR)/. \
  14464. + -I$(IX_XSCALE_SW)/src/include \
  14465. + -I$(OSAL_DIR)/ \
  14466. + -I$(OSAL_DIR)/os/linux/include/ \
  14467. + -I$(OSAL_DIR)/os/linux/include/modules/ \
  14468. + -I$(OSAL_DIR)/os/linux/include/modules/ioMem/ \
  14469. + -I$(OSAL_DIR)/os/linux/include/modules/bufferMgt/ \
  14470. + -I$(OSAL_DIR)/os/linux/include/core/ \
  14471. + -I$(OSAL_DIR)/os/linux/include/platforms/ \
  14472. + -I$(OSAL_DIR)/os/linux/include/platforms/ixp400/ \
  14473. + -I$(OSAL_DIR)/os/linux/include/platforms/ixp400/ixp425 \
  14474. + -I$(OSAL_DIR)/os/linux/include/platforms/ixp400/ixp465 \
  14475. + -I$(OSAL_DIR)/os/linux/include/core/ \
  14476. + -I$(OSAL_DIR)/include/ \
  14477. + -I$(OSAL_DIR)/include/modules/ \
  14478. + -I$(OSAL_DIR)/include/modules/bufferMgt/ \
  14479. + -I$(OSAL_DIR)/include/modules/ioMem/ \
  14480. + -I$(OSAL_DIR)/include/platforms/ \
  14481. + -I$(OSAL_DIR)/include/platforms/ixp400/ \
  14482. + -DUSE_IXP4XX_CRYPTO
  14483. +endif
  14484. +endif
  14485. +ifdef CONFIG_IXP400_LIB_1_4
  14486. +IXP_CFLAGS = \
  14487. + -I$(ROOTDIR)/. \
  14488. + -I$(ROOTDIR)/modules/ixp425/ixp400-1.4/ixp400_xscale_sw/src/include \
  14489. + -I$(ROOTDIR)/modules/ixp425/ixp400-1.4/ixp400_xscale_sw/src/linux \
  14490. + -DUSE_IXP4XX_CRYPTO
  14491. +endif
  14492. +ifndef IXPDIR
  14493. +IXPDIR = ixp-version-is-not-supported
  14494. +endif
  14495. +
  14496. +ifeq ($(CONFIG_CPU_IXP46X),y)
  14497. +IXP_CFLAGS += -D__ixp46X
  14498. +else
  14499. +ifeq ($(CONFIG_CPU_IXP43X),y)
  14500. +IXP_CFLAGS += -D__ixp43X
  14501. +else
  14502. +IXP_CFLAGS += -D__ixp42X
  14503. +endif
  14504. +endif
  14505. +
  14506. +obj-$(CONFIG_OCF_IXP4XX) += ixp4xx.o
  14507. +
  14508. +obj ?= .
  14509. +EXTRA_CFLAGS += $(IXP_CFLAGS) -I$(obj)/.. -I$(obj)/.
  14510. +
  14511. +ifdef TOPDIR
  14512. +-include $(TOPDIR)/Rules.make
  14513. +endif
  14514. +
  14515. diff -Nur linux-2.6.30.orig/crypto/ocf/Kconfig linux-2.6.30/crypto/ocf/Kconfig
  14516. --- linux-2.6.30.orig/crypto/ocf/Kconfig 1970-01-01 01:00:00.000000000 +0100
  14517. +++ linux-2.6.30/crypto/ocf/Kconfig 2009-06-11 10:55:27.000000000 +0200
  14518. @@ -0,0 +1,101 @@
  14519. +menu "OCF Configuration"
  14520. +
  14521. +config OCF_OCF
  14522. + tristate "OCF (Open Cryptograhic Framework)"
  14523. + help
  14524. + A linux port of the OpenBSD/FreeBSD crypto framework.
  14525. +
  14526. +config OCF_RANDOMHARVEST
  14527. + bool "crypto random --- harvest entropy for /dev/random"
  14528. + depends on OCF_OCF
  14529. + help
  14530. + Includes code to harvest random numbers from devices that support it.
  14531. +
  14532. +config OCF_FIPS
  14533. + bool "enable fips RNG checks"
  14534. + depends on OCF_OCF && OCF_RANDOMHARVEST
  14535. + help
  14536. + Run all RNG provided data through a fips check before
  14537. + adding it /dev/random's entropy pool.
  14538. +
  14539. +config OCF_CRYPTODEV
  14540. + tristate "cryptodev (user space support)"
  14541. + depends on OCF_OCF
  14542. + help
  14543. + The user space API to access crypto hardware.
  14544. +
  14545. +config OCF_CRYPTOSOFT
  14546. + tristate "cryptosoft (software crypto engine)"
  14547. + depends on OCF_OCF
  14548. + help
  14549. + A software driver for the OCF framework that uses
  14550. + the kernel CryptoAPI.
  14551. +
  14552. +config OCF_SAFE
  14553. + tristate "safenet (HW crypto engine)"
  14554. + depends on OCF_OCF
  14555. + help
  14556. + A driver for a number of the safenet Excel crypto accelerators.
  14557. + Currently tested and working on the 1141 and 1741.
  14558. +
  14559. +config OCF_IXP4XX
  14560. + tristate "IXP4xx (HW crypto engine)"
  14561. + depends on OCF_OCF
  14562. + help
  14563. + XScale IXP4xx crypto accelerator driver. Requires the
  14564. + Intel Access library.
  14565. +
  14566. +config OCF_IXP4XX_SHA1_MD5
  14567. + bool "IXP4xx SHA1 and MD5 Hashing"
  14568. + depends on OCF_IXP4XX
  14569. + help
  14570. + Allows the IXP4xx crypto accelerator to perform SHA1 and MD5 hashing.
  14571. + Note: this is MUCH slower than using cryptosoft (software crypto engine).
  14572. +
  14573. +config OCF_HIFN
  14574. + tristate "hifn (HW crypto engine)"
  14575. + depends on OCF_OCF
  14576. + help
  14577. + OCF driver for various HIFN based crypto accelerators.
  14578. + (7951, 7955, 7956, 7751, 7811)
  14579. +
  14580. +config OCF_HIFNHIPP
  14581. + tristate "Hifn HIPP (HW packet crypto engine)"
  14582. + depends on OCF_OCF
  14583. + help
  14584. + OCF driver for various HIFN (HIPP) based crypto accelerators
  14585. + (7855)
  14586. +
  14587. +config OCF_TALITOS
  14588. + tristate "talitos (HW crypto engine)"
  14589. + depends on OCF_OCF
  14590. + help
  14591. + OCF driver for Freescale's security engine (SEC/talitos).
  14592. +
  14593. +config OCF_PASEMI
  14594. + tristate "pasemi (HW crypto engine)"
  14595. + depends on OCF_OCF && PPC_PASEMI
  14596. + help
  14597. + OCF driver for the PA Semi PWRficient DMA Engine
  14598. +
  14599. +config OCF_EP80579
  14600. + tristate "ep80579 (HW crypto engine)"
  14601. + depends on OCF_OCF
  14602. + help
  14603. + OCF driver for the Intel EP80579 Integrated Processor Product Line.
  14604. +
  14605. +config OCF_OCFNULL
  14606. + tristate "ocfnull (fake crypto engine)"
  14607. + depends on OCF_OCF
  14608. + help
  14609. + OCF driver for measuring ipsec overheads (does no crypto)
  14610. +
  14611. +config OCF_BENCH
  14612. + tristate "ocf-bench (HW crypto in-kernel benchmark)"
  14613. + depends on OCF_OCF
  14614. + help
  14615. + A very simple encryption test for the in-kernel interface
  14616. + of OCF. Also includes code to benchmark the IXP Access library
  14617. + for comparison.
  14618. +
  14619. +endmenu
  14620. diff -Nur linux-2.6.30.orig/crypto/ocf/Makefile linux-2.6.30/crypto/ocf/Makefile
  14621. --- linux-2.6.30.orig/crypto/ocf/Makefile 1970-01-01 01:00:00.000000000 +0100
  14622. +++ linux-2.6.30/crypto/ocf/Makefile 2009-06-11 10:55:27.000000000 +0200
  14623. @@ -0,0 +1,121 @@
  14624. +# for SGlinux builds
  14625. +-include $(ROOTDIR)/modules/.config
  14626. +
  14627. +OCF_OBJS = crypto.o criov.o
  14628. +
  14629. +ifdef CONFIG_OCF_RANDOMHARVEST
  14630. + OCF_OBJS += random.o
  14631. +endif
  14632. +
  14633. +ifdef CONFIG_OCF_FIPS
  14634. + OCF_OBJS += rndtest.o
  14635. +endif
  14636. +
  14637. +# Add in autoconf.h to get #defines for CONFIG_xxx
  14638. +AUTOCONF_H=$(ROOTDIR)/modules/autoconf.h
  14639. +ifeq ($(AUTOCONF_H), $(wildcard $(AUTOCONF_H)))
  14640. + EXTRA_CFLAGS += -include $(AUTOCONF_H)
  14641. + export EXTRA_CFLAGS
  14642. +endif
  14643. +
  14644. +ifndef obj
  14645. + obj ?= .
  14646. + _obj = subdir
  14647. + mod-subdirs := safe hifn ixp4xx talitos ocfnull
  14648. + export-objs += crypto.o criov.o random.o
  14649. + list-multi += ocf.o
  14650. + _slash :=
  14651. +else
  14652. + _obj = obj
  14653. + _slash := /
  14654. +endif
  14655. +
  14656. +EXTRA_CFLAGS += -I$(obj)/.
  14657. +
  14658. +obj-$(CONFIG_OCF_OCF) += ocf.o
  14659. +obj-$(CONFIG_OCF_CRYPTODEV) += cryptodev.o
  14660. +obj-$(CONFIG_OCF_CRYPTOSOFT) += cryptosoft.o
  14661. +obj-$(CONFIG_OCF_BENCH) += ocf-bench.o
  14662. +
  14663. +$(_obj)-$(CONFIG_OCF_SAFE) += safe$(_slash)
  14664. +$(_obj)-$(CONFIG_OCF_HIFN) += hifn$(_slash)
  14665. +$(_obj)-$(CONFIG_OCF_IXP4XX) += ixp4xx$(_slash)
  14666. +$(_obj)-$(CONFIG_OCF_TALITOS) += talitos$(_slash)
  14667. +$(_obj)-$(CONFIG_OCF_PASEMI) += pasemi$(_slash)
  14668. +$(_obj)-$(CONFIG_OCF_EP80579) += ep80579$(_slash)
  14669. +$(_obj)-$(CONFIG_OCF_OCFNULL) += ocfnull$(_slash)
  14670. +
  14671. +ocf-objs := $(OCF_OBJS)
  14672. +
  14673. +$(list-multi) dummy1: $(ocf-objs)
  14674. + $(LD) -r -o $@ $(ocf-objs)
  14675. +
  14676. +.PHONY:
  14677. +clean:
  14678. + rm -f *.o *.ko .*.o.flags .*.ko.cmd .*.o.cmd .*.mod.o.cmd *.mod.c
  14679. + rm -f */*.o */*.ko */.*.o.cmd */.*.ko.cmd */.*.mod.o.cmd */*.mod.c */.*.o.flags
  14680. +
  14681. +ifdef TOPDIR
  14682. +-include $(TOPDIR)/Rules.make
  14683. +endif
  14684. +
  14685. +#
  14686. +# release gen targets
  14687. +#
  14688. +
  14689. +.PHONY: patch
  14690. +patch:
  14691. + REL=`date +%Y%m%d`; \
  14692. + patch=ocf-linux-$$REL.patch; \
  14693. + patch24=ocf-linux-24-$$REL.patch; \
  14694. + patch26=ocf-linux-26-$$REL.patch; \
  14695. + ( \
  14696. + find . -name Makefile; \
  14697. + find . -name Config.in; \
  14698. + find . -name Kconfig; \
  14699. + find . -name README; \
  14700. + find . -name '*.[ch]' | grep -v '.mod.c'; \
  14701. + ) | while read t; do \
  14702. + diff -Nau /dev/null $$t | sed 's?^+++ \./?+++ linux/crypto/ocf/?'; \
  14703. + done > $$patch; \
  14704. + cat patches/linux-2.4.35-ocf.patch $$patch > $$patch24; \
  14705. + cat patches/linux-2.6.26-ocf.patch $$patch > $$patch26
  14706. +
  14707. +.PHONY: tarball
  14708. +tarball:
  14709. + REL=`date +%Y%m%d`; RELDIR=/tmp/ocf-linux-$$REL; \
  14710. + CURDIR=`pwd`; \
  14711. + rm -rf /tmp/ocf-linux-$$REL*; \
  14712. + mkdir -p $$RELDIR/tools; \
  14713. + cp README* $$RELDIR; \
  14714. + cp patches/openss*.patch $$RELDIR; \
  14715. + cp patches/crypto-tools.patch $$RELDIR; \
  14716. + cp tools/[!C]* $$RELDIR/tools; \
  14717. + cd ..; \
  14718. + tar cvf $$RELDIR/ocf-linux.tar \
  14719. + --exclude=CVS \
  14720. + --exclude=.* \
  14721. + --exclude=*.o \
  14722. + --exclude=*.ko \
  14723. + --exclude=*.mod.* \
  14724. + --exclude=README* \
  14725. + --exclude=ocf-*.patch \
  14726. + --exclude=ocf/patches/openss*.patch \
  14727. + --exclude=ocf/patches/crypto-tools.patch \
  14728. + --exclude=ocf/tools \
  14729. + ocf; \
  14730. + gzip -9 $$RELDIR/ocf-linux.tar; \
  14731. + cd /tmp; \
  14732. + tar cvf ocf-linux-$$REL.tar ocf-linux-$$REL; \
  14733. + gzip -9 ocf-linux-$$REL.tar; \
  14734. + cd $$CURDIR/../../user; \
  14735. + rm -rf /tmp/crypto-tools-$$REL*; \
  14736. + tar cvf /tmp/crypto-tools-$$REL.tar \
  14737. + --exclude=CVS \
  14738. + --exclude=.* \
  14739. + --exclude=*.o \
  14740. + --exclude=cryptotest \
  14741. + --exclude=cryptokeytest \
  14742. + crypto-tools; \
  14743. + gzip -9 /tmp/crypto-tools-$$REL.tar
  14744. +
  14745. diff -Nur linux-2.6.30.orig/crypto/ocf/ocf-bench.c linux-2.6.30/crypto/ocf/ocf-bench.c
  14746. --- linux-2.6.30.orig/crypto/ocf/ocf-bench.c 1970-01-01 01:00:00.000000000 +0100
  14747. +++ linux-2.6.30/crypto/ocf/ocf-bench.c 2009-06-11 10:55:27.000000000 +0200
  14748. @@ -0,0 +1,436 @@
  14749. +/*
  14750. + * A loadable module that benchmarks the OCF crypto speed from kernel space.
  14751. + *
  14752. + * Copyright (C) 2004-2007 David McCullough <david_mccullough@securecomputing.com>
  14753. + *
  14754. + * LICENSE TERMS
  14755. + *
  14756. + * The free distribution and use of this software in both source and binary
  14757. + * form is allowed (with or without changes) provided that:
  14758. + *
  14759. + * 1. distributions of this source code include the above copyright
  14760. + * notice, this list of conditions and the following disclaimer;
  14761. + *
  14762. + * 2. distributions in binary form include the above copyright
  14763. + * notice, this list of conditions and the following disclaimer
  14764. + * in the documentation and/or other associated materials;
  14765. + *
  14766. + * 3. the copyright holder's name is not used to endorse products
  14767. + * built using this software without specific written permission.
  14768. + *
  14769. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  14770. + * may be distributed under the terms of the GNU General Public License (GPL),
  14771. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  14772. + *
  14773. + * DISCLAIMER
  14774. + *
  14775. + * This software is provided 'as is' with no explicit or implied warranties
  14776. + * in respect of its properties, including, but not limited to, correctness
  14777. + * and/or fitness for purpose.
  14778. + */
  14779. +
  14780. +
  14781. +#ifndef AUTOCONF_INCLUDED
  14782. +#include <linux/config.h>
  14783. +#endif
  14784. +#include <linux/module.h>
  14785. +#include <linux/init.h>
  14786. +#include <linux/list.h>
  14787. +#include <linux/slab.h>
  14788. +#include <linux/wait.h>
  14789. +#include <linux/sched.h>
  14790. +#include <linux/spinlock.h>
  14791. +#include <linux/version.h>
  14792. +#include <linux/interrupt.h>
  14793. +#include <cryptodev.h>
  14794. +
  14795. +#ifdef I_HAVE_AN_XSCALE_WITH_INTEL_SDK
  14796. +#define BENCH_IXP_ACCESS_LIB 1
  14797. +#endif
  14798. +#ifdef BENCH_IXP_ACCESS_LIB
  14799. +#include <IxTypes.h>
  14800. +#include <IxOsBuffMgt.h>
  14801. +#include <IxNpeDl.h>
  14802. +#include <IxCryptoAcc.h>
  14803. +#include <IxQMgr.h>
  14804. +#include <IxOsServices.h>
  14805. +#include <IxOsCacheMMU.h>
  14806. +#endif
  14807. +
  14808. +/*
  14809. + * support for access lib version 1.4
  14810. + */
  14811. +#ifndef IX_MBUF_PRIV
  14812. +#define IX_MBUF_PRIV(x) ((x)->priv)
  14813. +#endif
  14814. +
  14815. +/*
  14816. + * the number of simultaneously active requests
  14817. + */
  14818. +static int request_q_len = 20;
  14819. +module_param(request_q_len, int, 0);
  14820. +MODULE_PARM_DESC(request_q_len, "Number of outstanding requests");
  14821. +/*
  14822. + * how many requests we want to have processed
  14823. + */
  14824. +static int request_num = 1024;
  14825. +module_param(request_num, int, 0);
  14826. +MODULE_PARM_DESC(request_num, "run for at least this many requests");
  14827. +/*
  14828. + * the size of each request
  14829. + */
  14830. +static int request_size = 1500;
  14831. +module_param(request_size, int, 0);
  14832. +MODULE_PARM_DESC(request_size, "size of each request");
  14833. +
  14834. +/*
  14835. + * a structure for each request
  14836. + */
  14837. +typedef struct {
  14838. + struct work_struct work;
  14839. +#ifdef BENCH_IXP_ACCESS_LIB
  14840. + IX_MBUF mbuf;
  14841. +#endif
  14842. + unsigned char *buffer;
  14843. +} request_t;
  14844. +
  14845. +static request_t *requests;
  14846. +
  14847. +static int outstanding;
  14848. +static int total;
  14849. +
  14850. +/*************************************************************************/
  14851. +/*
  14852. + * OCF benchmark routines
  14853. + */
  14854. +
  14855. +static uint64_t ocf_cryptoid;
  14856. +static int ocf_init(void);
  14857. +static int ocf_cb(struct cryptop *crp);
  14858. +static void ocf_request(void *arg);
  14859. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  14860. +static void ocf_request_wq(struct work_struct *work);
  14861. +#endif
  14862. +
  14863. +static int
  14864. +ocf_init(void)
  14865. +{
  14866. + int error;
  14867. + struct cryptoini crie, cria;
  14868. + struct cryptodesc crda, crde;
  14869. +
  14870. + memset(&crie, 0, sizeof(crie));
  14871. + memset(&cria, 0, sizeof(cria));
  14872. + memset(&crde, 0, sizeof(crde));
  14873. + memset(&crda, 0, sizeof(crda));
  14874. +
  14875. + cria.cri_alg = CRYPTO_SHA1_HMAC;
  14876. + cria.cri_klen = 20 * 8;
  14877. + cria.cri_key = "0123456789abcdefghij";
  14878. +
  14879. + crie.cri_alg = CRYPTO_3DES_CBC;
  14880. + crie.cri_klen = 24 * 8;
  14881. + crie.cri_key = "0123456789abcdefghijklmn";
  14882. +
  14883. + crie.cri_next = &cria;
  14884. +
  14885. + error = crypto_newsession(&ocf_cryptoid, &crie, 0);
  14886. + if (error) {
  14887. + printk("crypto_newsession failed %d\n", error);
  14888. + return -1;
  14889. + }
  14890. + return 0;
  14891. +}
  14892. +
  14893. +static int
  14894. +ocf_cb(struct cryptop *crp)
  14895. +{
  14896. + request_t *r = (request_t *) crp->crp_opaque;
  14897. +
  14898. + if (crp->crp_etype)
  14899. + printk("Error in OCF processing: %d\n", crp->crp_etype);
  14900. + total++;
  14901. + crypto_freereq(crp);
  14902. + crp = NULL;
  14903. +
  14904. + if (total > request_num) {
  14905. + outstanding--;
  14906. + return 0;
  14907. + }
  14908. +
  14909. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  14910. + INIT_WORK(&r->work, ocf_request_wq);
  14911. +#else
  14912. + INIT_WORK(&r->work, ocf_request, r);
  14913. +#endif
  14914. + schedule_work(&r->work);
  14915. + return 0;
  14916. +}
  14917. +
  14918. +
  14919. +static void
  14920. +ocf_request(void *arg)
  14921. +{
  14922. + request_t *r = arg;
  14923. + struct cryptop *crp = crypto_getreq(2);
  14924. + struct cryptodesc *crde, *crda;
  14925. +
  14926. + if (!crp) {
  14927. + outstanding--;
  14928. + return;
  14929. + }
  14930. +
  14931. + crde = crp->crp_desc;
  14932. + crda = crde->crd_next;
  14933. +
  14934. + crda->crd_skip = 0;
  14935. + crda->crd_flags = 0;
  14936. + crda->crd_len = request_size;
  14937. + crda->crd_inject = request_size;
  14938. + crda->crd_alg = CRYPTO_SHA1_HMAC;
  14939. + crda->crd_key = "0123456789abcdefghij";
  14940. + crda->crd_klen = 20 * 8;
  14941. +
  14942. + crde->crd_skip = 0;
  14943. + crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_ENCRYPT;
  14944. + crde->crd_len = request_size;
  14945. + crde->crd_inject = request_size;
  14946. + crde->crd_alg = CRYPTO_3DES_CBC;
  14947. + crde->crd_key = "0123456789abcdefghijklmn";
  14948. + crde->crd_klen = 24 * 8;
  14949. +
  14950. + crp->crp_ilen = request_size + 64;
  14951. + crp->crp_flags = CRYPTO_F_CBIMM;
  14952. + crp->crp_buf = (caddr_t) r->buffer;
  14953. + crp->crp_callback = ocf_cb;
  14954. + crp->crp_sid = ocf_cryptoid;
  14955. + crp->crp_opaque = (caddr_t) r;
  14956. + crypto_dispatch(crp);
  14957. +}
  14958. +
  14959. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  14960. +static void
  14961. +ocf_request_wq(struct work_struct *work)
  14962. +{
  14963. + request_t *r = container_of(work, request_t, work);
  14964. + ocf_request(r);
  14965. +}
  14966. +#endif
  14967. +
  14968. +/*************************************************************************/
  14969. +#ifdef BENCH_IXP_ACCESS_LIB
  14970. +/*************************************************************************/
  14971. +/*
  14972. + * CryptoAcc benchmark routines
  14973. + */
  14974. +
  14975. +static IxCryptoAccCtx ixp_ctx;
  14976. +static UINT32 ixp_ctx_id;
  14977. +static IX_MBUF ixp_pri;
  14978. +static IX_MBUF ixp_sec;
  14979. +static int ixp_registered = 0;
  14980. +
  14981. +static void ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp,
  14982. + IxCryptoAccStatus status);
  14983. +static void ixp_perform_cb(UINT32 ctx_id, IX_MBUF *sbufp, IX_MBUF *dbufp,
  14984. + IxCryptoAccStatus status);
  14985. +static void ixp_request(void *arg);
  14986. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  14987. +static void ixp_request_wq(struct work_struct *work);
  14988. +#endif
  14989. +
  14990. +static int
  14991. +ixp_init(void)
  14992. +{
  14993. + IxCryptoAccStatus status;
  14994. +
  14995. + ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_3DES;
  14996. + ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  14997. + ixp_ctx.cipherCtx.cipherKeyLen = 24;
  14998. + ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
  14999. + ixp_ctx.cipherCtx.cipherInitialVectorLen = IX_CRYPTO_ACC_DES_IV_64;
  15000. + memcpy(ixp_ctx.cipherCtx.key.cipherKey, "0123456789abcdefghijklmn", 24);
  15001. +
  15002. + ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_SHA1;
  15003. + ixp_ctx.authCtx.authDigestLen = 12;
  15004. + ixp_ctx.authCtx.aadLen = 0;
  15005. + ixp_ctx.authCtx.authKeyLen = 20;
  15006. + memcpy(ixp_ctx.authCtx.key.authKey, "0123456789abcdefghij", 20);
  15007. +
  15008. + ixp_ctx.useDifferentSrcAndDestMbufs = 0;
  15009. + ixp_ctx.operation = IX_CRYPTO_ACC_OP_ENCRYPT_AUTH ;
  15010. +
  15011. + IX_MBUF_MLEN(&ixp_pri) = IX_MBUF_PKT_LEN(&ixp_pri) = 128;
  15012. + IX_MBUF_MDATA(&ixp_pri) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  15013. + IX_MBUF_MLEN(&ixp_sec) = IX_MBUF_PKT_LEN(&ixp_sec) = 128;
  15014. + IX_MBUF_MDATA(&ixp_sec) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  15015. +
  15016. + status = ixCryptoAccCtxRegister(&ixp_ctx, &ixp_pri, &ixp_sec,
  15017. + ixp_register_cb, ixp_perform_cb, &ixp_ctx_id);
  15018. +
  15019. + if (IX_CRYPTO_ACC_STATUS_SUCCESS == status) {
  15020. + while (!ixp_registered)
  15021. + schedule();
  15022. + return ixp_registered < 0 ? -1 : 0;
  15023. + }
  15024. +
  15025. + printk("ixp: ixCryptoAccCtxRegister failed %d\n", status);
  15026. + return -1;
  15027. +}
  15028. +
  15029. +static void
  15030. +ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp, IxCryptoAccStatus status)
  15031. +{
  15032. + if (bufp) {
  15033. + IX_MBUF_MLEN(bufp) = IX_MBUF_PKT_LEN(bufp) = 0;
  15034. + kfree(IX_MBUF_MDATA(bufp));
  15035. + IX_MBUF_MDATA(bufp) = NULL;
  15036. + }
  15037. +
  15038. + if (IX_CRYPTO_ACC_STATUS_WAIT == status)
  15039. + return;
  15040. + if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  15041. + ixp_registered = 1;
  15042. + else
  15043. + ixp_registered = -1;
  15044. +}
  15045. +
  15046. +static void
  15047. +ixp_perform_cb(
  15048. + UINT32 ctx_id,
  15049. + IX_MBUF *sbufp,
  15050. + IX_MBUF *dbufp,
  15051. + IxCryptoAccStatus status)
  15052. +{
  15053. + request_t *r = NULL;
  15054. +
  15055. + total++;
  15056. + if (total > request_num) {
  15057. + outstanding--;
  15058. + return;
  15059. + }
  15060. +
  15061. + if (!sbufp || !(r = IX_MBUF_PRIV(sbufp))) {
  15062. + printk("crappo %p %p\n", sbufp, r);
  15063. + outstanding--;
  15064. + return;
  15065. + }
  15066. +
  15067. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  15068. + INIT_WORK(&r->work, ixp_request_wq);
  15069. +#else
  15070. + INIT_WORK(&r->work, ixp_request, r);
  15071. +#endif
  15072. + schedule_work(&r->work);
  15073. +}
  15074. +
  15075. +static void
  15076. +ixp_request(void *arg)
  15077. +{
  15078. + request_t *r = arg;
  15079. + IxCryptoAccStatus status;
  15080. +
  15081. + memset(&r->mbuf, 0, sizeof(r->mbuf));
  15082. + IX_MBUF_MLEN(&r->mbuf) = IX_MBUF_PKT_LEN(&r->mbuf) = request_size + 64;
  15083. + IX_MBUF_MDATA(&r->mbuf) = r->buffer;
  15084. + IX_MBUF_PRIV(&r->mbuf) = r;
  15085. + status = ixCryptoAccAuthCryptPerform(ixp_ctx_id, &r->mbuf, NULL,
  15086. + 0, request_size, 0, request_size, request_size, r->buffer);
  15087. + if (IX_CRYPTO_ACC_STATUS_SUCCESS != status) {
  15088. + printk("status1 = %d\n", status);
  15089. + outstanding--;
  15090. + return;
  15091. + }
  15092. + return;
  15093. +}
  15094. +
  15095. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  15096. +static void
  15097. +ixp_request_wq(struct work_struct *work)
  15098. +{
  15099. + request_t *r = container_of(work, request_t, work);
  15100. + ixp_request(r);
  15101. +}
  15102. +#endif
  15103. +
  15104. +/*************************************************************************/
  15105. +#endif /* BENCH_IXP_ACCESS_LIB */
  15106. +/*************************************************************************/
  15107. +
  15108. +int
  15109. +ocfbench_init(void)
  15110. +{
  15111. + int i, jstart, jstop;
  15112. +
  15113. + printk("Crypto Speed tests\n");
  15114. +
  15115. + requests = kmalloc(sizeof(request_t) * request_q_len, GFP_KERNEL);
  15116. + if (!requests) {
  15117. + printk("malloc failed\n");
  15118. + return -EINVAL;
  15119. + }
  15120. +
  15121. + for (i = 0; i < request_q_len; i++) {
  15122. + /* +64 for return data */
  15123. + requests[i].buffer = kmalloc(request_size + 128, GFP_DMA);
  15124. + if (!requests[i].buffer) {
  15125. + printk("malloc failed\n");
  15126. + return -EINVAL;
  15127. + }
  15128. + memset(requests[i].buffer, '0' + i, request_size + 128);
  15129. + }
  15130. +
  15131. + /*
  15132. + * OCF benchmark
  15133. + */
  15134. + printk("OCF: testing ...\n");
  15135. + ocf_init();
  15136. + total = outstanding = 0;
  15137. + jstart = jiffies;
  15138. + for (i = 0; i < request_q_len; i++) {
  15139. + outstanding++;
  15140. + ocf_request(&requests[i]);
  15141. + }
  15142. + while (outstanding > 0)
  15143. + schedule();
  15144. + jstop = jiffies;
  15145. +
  15146. + printk("OCF: %d requests of %d bytes in %d jiffies\n", total, request_size,
  15147. + jstop - jstart);
  15148. +
  15149. +#ifdef BENCH_IXP_ACCESS_LIB
  15150. + /*
  15151. + * IXP benchmark
  15152. + */
  15153. + printk("IXP: testing ...\n");
  15154. + ixp_init();
  15155. + total = outstanding = 0;
  15156. + jstart = jiffies;
  15157. + for (i = 0; i < request_q_len; i++) {
  15158. + outstanding++;
  15159. + ixp_request(&requests[i]);
  15160. + }
  15161. + while (outstanding > 0)
  15162. + schedule();
  15163. + jstop = jiffies;
  15164. +
  15165. + printk("IXP: %d requests of %d bytes in %d jiffies\n", total, request_size,
  15166. + jstop - jstart);
  15167. +#endif /* BENCH_IXP_ACCESS_LIB */
  15168. +
  15169. + for (i = 0; i < request_q_len; i++)
  15170. + kfree(requests[i].buffer);
  15171. + kfree(requests);
  15172. + return -EINVAL; /* always fail to load so it can be re-run quickly ;-) */
  15173. +}
  15174. +
  15175. +static void __exit ocfbench_exit(void)
  15176. +{
  15177. +}
  15178. +
  15179. +module_init(ocfbench_init);
  15180. +module_exit(ocfbench_exit);
  15181. +
  15182. +MODULE_LICENSE("BSD");
  15183. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  15184. +MODULE_DESCRIPTION("Benchmark various in-kernel crypto speeds");
  15185. diff -Nur linux-2.6.30.orig/crypto/ocf/ocf-compat.h linux-2.6.30/crypto/ocf/ocf-compat.h
  15186. --- linux-2.6.30.orig/crypto/ocf/ocf-compat.h 1970-01-01 01:00:00.000000000 +0100
  15187. +++ linux-2.6.30/crypto/ocf/ocf-compat.h 2009-06-11 10:55:27.000000000 +0200
  15188. @@ -0,0 +1,270 @@
  15189. +#ifndef _BSD_COMPAT_H_
  15190. +#define _BSD_COMPAT_H_ 1
  15191. +/****************************************************************************/
  15192. +/*
  15193. + * Provide compat routines for older linux kernels and BSD kernels
  15194. + *
  15195. + * Written by David McCullough <david_mccullough@securecomputing.com>
  15196. + * Copyright (C) 2007 David McCullough <david_mccullough@securecomputing.com>
  15197. + *
  15198. + * LICENSE TERMS
  15199. + *
  15200. + * The free distribution and use of this software in both source and binary
  15201. + * form is allowed (with or without changes) provided that:
  15202. + *
  15203. + * 1. distributions of this source code include the above copyright
  15204. + * notice, this list of conditions and the following disclaimer;
  15205. + *
  15206. + * 2. distributions in binary form include the above copyright
  15207. + * notice, this list of conditions and the following disclaimer
  15208. + * in the documentation and/or other associated materials;
  15209. + *
  15210. + * 3. the copyright holder's name is not used to endorse products
  15211. + * built using this software without specific written permission.
  15212. + *
  15213. + * ALTERNATIVELY, provided that this notice is retained in full, this file
  15214. + * may be distributed under the terms of the GNU General Public License (GPL),
  15215. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  15216. + *
  15217. + * DISCLAIMER
  15218. + *
  15219. + * This software is provided 'as is' with no explicit or implied warranties
  15220. + * in respect of its properties, including, but not limited to, correctness
  15221. + * and/or fitness for purpose.
  15222. + */
  15223. +/****************************************************************************/
  15224. +#ifdef __KERNEL__
  15225. +/*
  15226. + * fake some BSD driver interface stuff specifically for OCF use
  15227. + */
  15228. +
  15229. +typedef struct ocf_device *device_t;
  15230. +
  15231. +typedef struct {
  15232. + int (*cryptodev_newsession)(device_t dev, u_int32_t *sidp, struct cryptoini *cri);
  15233. + int (*cryptodev_freesession)(device_t dev, u_int64_t tid);
  15234. + int (*cryptodev_process)(device_t dev, struct cryptop *crp, int hint);
  15235. + int (*cryptodev_kprocess)(device_t dev, struct cryptkop *krp, int hint);
  15236. +} device_method_t;
  15237. +#define DEVMETHOD(id, func) id: func
  15238. +
  15239. +struct ocf_device {
  15240. + char name[32]; /* the driver name */
  15241. + char nameunit[32]; /* the driver name + HW instance */
  15242. + int unit;
  15243. + device_method_t methods;
  15244. + void *softc;
  15245. +};
  15246. +
  15247. +#define CRYPTODEV_NEWSESSION(dev, sid, cri) \
  15248. + ((*(dev)->methods.cryptodev_newsession)(dev,sid,cri))
  15249. +#define CRYPTODEV_FREESESSION(dev, sid) \
  15250. + ((*(dev)->methods.cryptodev_freesession)(dev, sid))
  15251. +#define CRYPTODEV_PROCESS(dev, crp, hint) \
  15252. + ((*(dev)->methods.cryptodev_process)(dev, crp, hint))
  15253. +#define CRYPTODEV_KPROCESS(dev, krp, hint) \
  15254. + ((*(dev)->methods.cryptodev_kprocess)(dev, krp, hint))
  15255. +
  15256. +#define device_get_name(dev) ((dev)->name)
  15257. +#define device_get_nameunit(dev) ((dev)->nameunit)
  15258. +#define device_get_unit(dev) ((dev)->unit)
  15259. +#define device_get_softc(dev) ((dev)->softc)
  15260. +
  15261. +#define softc_device_decl \
  15262. + struct ocf_device _device; \
  15263. + device_t
  15264. +
  15265. +#define softc_device_init(_sc, _name, _unit, _methods) \
  15266. + if (1) {\
  15267. + strncpy((_sc)->_device.name, _name, sizeof((_sc)->_device.name) - 1); \
  15268. + snprintf((_sc)->_device.nameunit, sizeof((_sc)->_device.name), "%s%d", _name, _unit); \
  15269. + (_sc)->_device.unit = _unit; \
  15270. + (_sc)->_device.methods = _methods; \
  15271. + (_sc)->_device.softc = (void *) _sc; \
  15272. + *(device_t *)((softc_get_device(_sc))+1) = &(_sc)->_device; \
  15273. + } else
  15274. +
  15275. +#define softc_get_device(_sc) (&(_sc)->_device)
  15276. +
  15277. +/*
  15278. + * iomem support for 2.4 and 2.6 kernels
  15279. + */
  15280. +#include <linux/version.h>
  15281. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  15282. +#define ocf_iomem_t unsigned long
  15283. +
  15284. +/*
  15285. + * implement simple workqueue like support for older kernels
  15286. + */
  15287. +
  15288. +#include <linux/tqueue.h>
  15289. +
  15290. +#define work_struct tq_struct
  15291. +
  15292. +#define INIT_WORK(wp, fp, ap) \
  15293. + do { \
  15294. + (wp)->sync = 0; \
  15295. + (wp)->routine = (fp); \
  15296. + (wp)->data = (ap); \
  15297. + } while (0)
  15298. +
  15299. +#define schedule_work(wp) \
  15300. + do { \
  15301. + queue_task((wp), &tq_immediate); \
  15302. + mark_bh(IMMEDIATE_BH); \
  15303. + } while (0)
  15304. +
  15305. +#define flush_scheduled_work() run_task_queue(&tq_immediate)
  15306. +
  15307. +#else
  15308. +#define ocf_iomem_t void __iomem *
  15309. +
  15310. +#include <linux/workqueue.h>
  15311. +
  15312. +#endif
  15313. +
  15314. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
  15315. +#include <linux/fdtable.h>
  15316. +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  15317. +#define files_fdtable(files) (files)
  15318. +#endif
  15319. +
  15320. +#ifdef MODULE_PARM
  15321. +#undef module_param /* just in case */
  15322. +#define module_param(a,b,c) MODULE_PARM(a,"i")
  15323. +#endif
  15324. +
  15325. +#define bzero(s,l) memset(s,0,l)
  15326. +#define bcopy(s,d,l) memcpy(d,s,l)
  15327. +#define bcmp(x, y, l) memcmp(x,y,l)
  15328. +
  15329. +#define MIN(x,y) ((x) < (y) ? (x) : (y))
  15330. +
  15331. +#define device_printf(dev, a...) ({ \
  15332. + printk("%s: ", device_get_nameunit(dev)); printk(a); \
  15333. + })
  15334. +
  15335. +#undef printf
  15336. +#define printf(fmt...) printk(fmt)
  15337. +
  15338. +#define KASSERT(c,p) if (!(c)) { printk p ; } else
  15339. +
  15340. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  15341. +#define ocf_daemonize(str) \
  15342. + daemonize(); \
  15343. + spin_lock_irq(&current->sigmask_lock); \
  15344. + sigemptyset(&current->blocked); \
  15345. + recalc_sigpending(current); \
  15346. + spin_unlock_irq(&current->sigmask_lock); \
  15347. + sprintf(current->comm, str);
  15348. +#else
  15349. +#define ocf_daemonize(str) daemonize(str);
  15350. +#endif
  15351. +
  15352. +#define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
  15353. +#define TAILQ_EMPTY(q) list_empty(q)
  15354. +#define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
  15355. +
  15356. +#define read_random(p,l) get_random_bytes(p,l)
  15357. +
  15358. +#define DELAY(x) ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
  15359. +#define strtoul simple_strtoul
  15360. +
  15361. +#define pci_get_vendor(dev) ((dev)->vendor)
  15362. +#define pci_get_device(dev) ((dev)->device)
  15363. +
  15364. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  15365. +#define pci_set_consistent_dma_mask(dev, mask) (0)
  15366. +#endif
  15367. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
  15368. +#define pci_dma_sync_single_for_cpu pci_dma_sync_single
  15369. +#endif
  15370. +
  15371. +#ifndef DMA_32BIT_MASK
  15372. +#define DMA_32BIT_MASK 0x00000000ffffffffULL
  15373. +#endif
  15374. +
  15375. +#define htole32(x) cpu_to_le32(x)
  15376. +#define htobe32(x) cpu_to_be32(x)
  15377. +#define htole16(x) cpu_to_le16(x)
  15378. +#define htobe16(x) cpu_to_be16(x)
  15379. +
  15380. +/* older kernels don't have these */
  15381. +
  15382. +#ifndef IRQ_NONE
  15383. +#define IRQ_NONE
  15384. +#define IRQ_HANDLED
  15385. +#define irqreturn_t void
  15386. +#endif
  15387. +#ifndef IRQF_SHARED
  15388. +#define IRQF_SHARED SA_SHIRQ
  15389. +#endif
  15390. +
  15391. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  15392. +# define strlcpy(dest,src,len) \
  15393. + ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
  15394. +#endif
  15395. +
  15396. +#ifndef MAX_ERRNO
  15397. +#define MAX_ERRNO 4095
  15398. +#endif
  15399. +#ifndef IS_ERR_VALUE
  15400. +#define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
  15401. +#endif
  15402. +
  15403. +/*
  15404. + * common debug for all
  15405. + */
  15406. +#if 1
  15407. +#define dprintk(a...) do { if (debug) printk(a); } while(0)
  15408. +#else
  15409. +#define dprintk(a...)
  15410. +#endif
  15411. +
  15412. +#ifndef SLAB_ATOMIC
  15413. +/* Changed in 2.6.20, must use GFP_ATOMIC now */
  15414. +#define SLAB_ATOMIC GFP_ATOMIC
  15415. +#endif
  15416. +
  15417. +/*
  15418. + * need some additional support for older kernels */
  15419. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
  15420. +#define pci_register_driver_compat(driver, rc) \
  15421. + do { \
  15422. + if ((rc) > 0) { \
  15423. + (rc) = 0; \
  15424. + } else if (rc == 0) { \
  15425. + (rc) = -ENODEV; \
  15426. + } else { \
  15427. + pci_unregister_driver(driver); \
  15428. + } \
  15429. + } while (0)
  15430. +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
  15431. +#define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
  15432. +#else
  15433. +#define pci_register_driver_compat(driver,rc)
  15434. +#endif
  15435. +
  15436. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
  15437. +
  15438. +#include <asm/scatterlist.h>
  15439. +
  15440. +static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  15441. + unsigned int len, unsigned int offset)
  15442. +{
  15443. + sg->page = page;
  15444. + sg->offset = offset;
  15445. + sg->length = len;
  15446. +}
  15447. +
  15448. +static inline void *sg_virt(struct scatterlist *sg)
  15449. +{
  15450. + return page_address(sg->page) + sg->offset;
  15451. +}
  15452. +
  15453. +#endif
  15454. +
  15455. +#endif /* __KERNEL__ */
  15456. +
  15457. +/****************************************************************************/
  15458. +#endif /* _BSD_COMPAT_H_ */
  15459. diff -Nur linux-2.6.30.orig/crypto/ocf/ocfnull/Makefile linux-2.6.30/crypto/ocf/ocfnull/Makefile
  15460. --- linux-2.6.30.orig/crypto/ocf/ocfnull/Makefile 1970-01-01 01:00:00.000000000 +0100
  15461. +++ linux-2.6.30/crypto/ocf/ocfnull/Makefile 2009-06-11 10:55:27.000000000 +0200
  15462. @@ -0,0 +1,12 @@
  15463. +# for SGlinux builds
  15464. +-include $(ROOTDIR)/modules/.config
  15465. +
  15466. +obj-$(CONFIG_OCF_OCFNULL) += ocfnull.o
  15467. +
  15468. +obj ?= .
  15469. +EXTRA_CFLAGS += -I$(obj)/..
  15470. +
  15471. +ifdef TOPDIR
  15472. +-include $(TOPDIR)/Rules.make
  15473. +endif
  15474. +
  15475. diff -Nur linux-2.6.30.orig/crypto/ocf/ocfnull/ocfnull.c linux-2.6.30/crypto/ocf/ocfnull/ocfnull.c
  15476. --- linux-2.6.30.orig/crypto/ocf/ocfnull/ocfnull.c 1970-01-01 01:00:00.000000000 +0100
  15477. +++ linux-2.6.30/crypto/ocf/ocfnull/ocfnull.c 2009-06-11 10:55:27.000000000 +0200
  15478. @@ -0,0 +1,203 @@
  15479. +/*
  15480. + * An OCF module for determining the cost of crypto versus the cost of
  15481. + * IPSec processing outside of OCF. This modules gives us the effect of
  15482. + * zero cost encryption, of course you will need to run it at both ends
  15483. + * since it does no crypto at all.
  15484. + *
  15485. + * Written by David McCullough <david_mccullough@securecomputing.com>
  15486. + * Copyright (C) 2006-2007 David McCullough
  15487. + *
  15488. + * LICENSE TERMS
  15489. + *
  15490. + * The free distribution and use of this software in both source and binary
  15491. + * form is allowed (with or without changes) provided that:
  15492. + *
  15493. + * 1. distributions of this source code include the above copyright
  15494. + * notice, this list of conditions and the following disclaimer;
  15495. + *
  15496. + * 2. distributions in binary form include the above copyright
  15497. + * notice, this list of conditions and the following disclaimer
  15498. + * in the documentation and/or other associated materials;
  15499. + *
  15500. + * 3. the copyright holder's name is not used to endorse products
  15501. + * built using this software without specific written permission.
  15502. + *
  15503. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  15504. + * may be distributed under the terms of the GNU General Public License (GPL),
  15505. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  15506. + *
  15507. + * DISCLAIMER
  15508. + *
  15509. + * This software is provided 'as is' with no explicit or implied warranties
  15510. + * in respect of its properties, including, but not limited to, correctness
  15511. + * and/or fitness for purpose.
  15512. + */
  15513. +
  15514. +#ifndef AUTOCONF_INCLUDED
  15515. +#include <linux/config.h>
  15516. +#endif
  15517. +#include <linux/module.h>
  15518. +#include <linux/init.h>
  15519. +#include <linux/list.h>
  15520. +#include <linux/slab.h>
  15521. +#include <linux/sched.h>
  15522. +#include <linux/wait.h>
  15523. +#include <linux/crypto.h>
  15524. +#include <linux/interrupt.h>
  15525. +
  15526. +#include <cryptodev.h>
  15527. +#include <uio.h>
  15528. +
  15529. +static int32_t null_id = -1;
  15530. +static u_int32_t null_sesnum = 0;
  15531. +
  15532. +static int null_process(device_t, struct cryptop *, int);
  15533. +static int null_newsession(device_t, u_int32_t *, struct cryptoini *);
  15534. +static int null_freesession(device_t, u_int64_t);
  15535. +
  15536. +#define debug ocfnull_debug
  15537. +int ocfnull_debug = 0;
  15538. +module_param(ocfnull_debug, int, 0644);
  15539. +MODULE_PARM_DESC(ocfnull_debug, "Enable debug");
  15540. +
  15541. +/*
  15542. + * dummy device structure
  15543. + */
  15544. +
  15545. +static struct {
  15546. + softc_device_decl sc_dev;
  15547. +} nulldev;
  15548. +
  15549. +static device_method_t null_methods = {
  15550. + /* crypto device methods */
  15551. + DEVMETHOD(cryptodev_newsession, null_newsession),
  15552. + DEVMETHOD(cryptodev_freesession,null_freesession),
  15553. + DEVMETHOD(cryptodev_process, null_process),
  15554. +};
  15555. +
  15556. +/*
  15557. + * Generate a new software session.
  15558. + */
  15559. +static int
  15560. +null_newsession(device_t arg, u_int32_t *sid, struct cryptoini *cri)
  15561. +{
  15562. + dprintk("%s()\n", __FUNCTION__);
  15563. + if (sid == NULL || cri == NULL) {
  15564. + dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  15565. + return EINVAL;
  15566. + }
  15567. +
  15568. + if (null_sesnum == 0)
  15569. + null_sesnum++;
  15570. + *sid = null_sesnum++;
  15571. + return 0;
  15572. +}
  15573. +
  15574. +
  15575. +/*
  15576. + * Free a session.
  15577. + */
  15578. +static int
  15579. +null_freesession(device_t arg, u_int64_t tid)
  15580. +{
  15581. + u_int32_t sid = CRYPTO_SESID2LID(tid);
  15582. +
  15583. + dprintk("%s()\n", __FUNCTION__);
  15584. + if (sid > null_sesnum) {
  15585. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  15586. + return EINVAL;
  15587. + }
  15588. +
  15589. + /* Silently accept and return */
  15590. + if (sid == 0)
  15591. + return 0;
  15592. + return 0;
  15593. +}
  15594. +
  15595. +
  15596. +/*
  15597. + * Process a request.
  15598. + */
  15599. +static int
  15600. +null_process(device_t arg, struct cryptop *crp, int hint)
  15601. +{
  15602. + unsigned int lid;
  15603. +
  15604. + dprintk("%s()\n", __FUNCTION__);
  15605. +
  15606. + /* Sanity check */
  15607. + if (crp == NULL) {
  15608. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  15609. + return EINVAL;
  15610. + }
  15611. +
  15612. + crp->crp_etype = 0;
  15613. +
  15614. + if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  15615. + dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  15616. + crp->crp_etype = EINVAL;
  15617. + goto done;
  15618. + }
  15619. +
  15620. + /*
  15621. + * find the session we are using
  15622. + */
  15623. +
  15624. + lid = crp->crp_sid & 0xffffffff;
  15625. + if (lid >= null_sesnum || lid == 0) {
  15626. + crp->crp_etype = ENOENT;
  15627. + dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  15628. + goto done;
  15629. + }
  15630. +
  15631. +done:
  15632. + crypto_done(crp);
  15633. + return 0;
  15634. +}
  15635. +
  15636. +
  15637. +/*
  15638. + * our driver startup and shutdown routines
  15639. + */
  15640. +
  15641. +static int
  15642. +null_init(void)
  15643. +{
  15644. + dprintk("%s(%p)\n", __FUNCTION__, null_init);
  15645. +
  15646. + memset(&nulldev, 0, sizeof(nulldev));
  15647. + softc_device_init(&nulldev, "ocfnull", 0, null_methods);
  15648. +
  15649. + null_id = crypto_get_driverid(softc_get_device(&nulldev),
  15650. + CRYPTOCAP_F_HARDWARE);
  15651. + if (null_id < 0)
  15652. + panic("ocfnull: crypto device cannot initialize!");
  15653. +
  15654. +#define REGISTER(alg) \
  15655. + crypto_register(null_id,alg,0,0)
  15656. + REGISTER(CRYPTO_DES_CBC);
  15657. + REGISTER(CRYPTO_3DES_CBC);
  15658. + REGISTER(CRYPTO_RIJNDAEL128_CBC);
  15659. + REGISTER(CRYPTO_MD5);
  15660. + REGISTER(CRYPTO_SHA1);
  15661. + REGISTER(CRYPTO_MD5_HMAC);
  15662. + REGISTER(CRYPTO_SHA1_HMAC);
  15663. +#undef REGISTER
  15664. +
  15665. + return 0;
  15666. +}
  15667. +
  15668. +static void
  15669. +null_exit(void)
  15670. +{
  15671. + dprintk("%s()\n", __FUNCTION__);
  15672. + crypto_unregister_all(null_id);
  15673. + null_id = -1;
  15674. +}
  15675. +
  15676. +module_init(null_init);
  15677. +module_exit(null_exit);
  15678. +
  15679. +MODULE_LICENSE("Dual BSD/GPL");
  15680. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  15681. +MODULE_DESCRIPTION("ocfnull - claims a lot but does nothing");
  15682. diff -Nur linux-2.6.30.orig/crypto/ocf/pasemi/Makefile linux-2.6.30/crypto/ocf/pasemi/Makefile
  15683. --- linux-2.6.30.orig/crypto/ocf/pasemi/Makefile 1970-01-01 01:00:00.000000000 +0100
  15684. +++ linux-2.6.30/crypto/ocf/pasemi/Makefile 2009-06-11 10:55:27.000000000 +0200
  15685. @@ -0,0 +1,12 @@
  15686. +# for SGlinux builds
  15687. +-include $(ROOTDIR)/modules/.config
  15688. +
  15689. +obj-$(CONFIG_OCF_PASEMI) += pasemi.o
  15690. +
  15691. +obj ?= .
  15692. +EXTRA_CFLAGS += -I$(obj)/.. -I$(obj)/
  15693. +
  15694. +ifdef TOPDIR
  15695. +-include $(TOPDIR)/Rules.make
  15696. +endif
  15697. +
  15698. diff -Nur linux-2.6.30.orig/crypto/ocf/pasemi/pasemi.c linux-2.6.30/crypto/ocf/pasemi/pasemi.c
  15699. --- linux-2.6.30.orig/crypto/ocf/pasemi/pasemi.c 1970-01-01 01:00:00.000000000 +0100
  15700. +++ linux-2.6.30/crypto/ocf/pasemi/pasemi.c 2009-06-11 10:55:27.000000000 +0200
  15701. @@ -0,0 +1,1009 @@
  15702. +/*
  15703. + * Copyright (C) 2007 PA Semi, Inc
  15704. + *
  15705. + * Driver for the PA Semi PWRficient DMA Crypto Engine
  15706. + *
  15707. + * This program is free software; you can redistribute it and/or modify
  15708. + * it under the terms of the GNU General Public License version 2 as
  15709. + * published by the Free Software Foundation.
  15710. + *
  15711. + * This program is distributed in the hope that it will be useful,
  15712. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15713. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15714. + * GNU General Public License for more details.
  15715. + *
  15716. + * You should have received a copy of the GNU General Public License
  15717. + * along with this program; if not, write to the Free Software
  15718. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15719. + */
  15720. +
  15721. +#ifndef AUTOCONF_INCLUDED
  15722. +#include <linux/config.h>
  15723. +#endif
  15724. +#include <linux/module.h>
  15725. +#include <linux/init.h>
  15726. +#include <linux/interrupt.h>
  15727. +#include <linux/timer.h>
  15728. +#include <linux/random.h>
  15729. +#include <linux/skbuff.h>
  15730. +#include <asm/scatterlist.h>
  15731. +#include <linux/moduleparam.h>
  15732. +#include <linux/pci.h>
  15733. +#include <cryptodev.h>
  15734. +#include <uio.h>
  15735. +#include "pasemi_fnu.h"
  15736. +
  15737. +#define DRV_NAME "pasemi"
  15738. +
  15739. +#define TIMER_INTERVAL 1000
  15740. +
  15741. +static void __devexit pasemi_dma_remove(struct pci_dev *pdev);
  15742. +static struct pasdma_status volatile * dma_status;
  15743. +
  15744. +static int debug;
  15745. +module_param(debug, int, 0644);
  15746. +MODULE_PARM_DESC(debug, "Enable debug");
  15747. +
  15748. +static void pasemi_desc_start(struct pasemi_desc *desc, u64 hdr)
  15749. +{
  15750. + desc->postop = 0;
  15751. + desc->quad[0] = hdr;
  15752. + desc->quad_cnt = 1;
  15753. + desc->size = 1;
  15754. +}
  15755. +
  15756. +static void pasemi_desc_build(struct pasemi_desc *desc, u64 val)
  15757. +{
  15758. + desc->quad[desc->quad_cnt++] = val;
  15759. + desc->size = (desc->quad_cnt + 1) / 2;
  15760. +}
  15761. +
  15762. +static void pasemi_desc_hdr(struct pasemi_desc *desc, u64 hdr)
  15763. +{
  15764. + desc->quad[0] |= hdr;
  15765. +}
  15766. +
  15767. +static int pasemi_desc_size(struct pasemi_desc *desc)
  15768. +{
  15769. + return desc->size;
  15770. +}
  15771. +
  15772. +static void pasemi_ring_add_desc(
  15773. + struct pasemi_fnu_txring *ring,
  15774. + struct pasemi_desc *desc,
  15775. + struct cryptop *crp) {
  15776. + int i;
  15777. + int ring_index = 2 * (ring->next_to_fill & (TX_RING_SIZE-1));
  15778. +
  15779. + TX_DESC_INFO(ring, ring->next_to_fill).desc_size = desc->size;
  15780. + TX_DESC_INFO(ring, ring->next_to_fill).desc_postop = desc->postop;
  15781. + TX_DESC_INFO(ring, ring->next_to_fill).cf_crp = crp;
  15782. +
  15783. + for (i = 0; i < desc->quad_cnt; i += 2) {
  15784. + ring_index = 2 * (ring->next_to_fill & (TX_RING_SIZE-1));
  15785. + ring->desc[ring_index] = desc->quad[i];
  15786. + ring->desc[ring_index + 1] = desc->quad[i + 1];
  15787. + ring->next_to_fill++;
  15788. + }
  15789. +
  15790. + if (desc->quad_cnt & 1)
  15791. + ring->desc[ring_index + 1] = 0;
  15792. +}
  15793. +
  15794. +static void pasemi_ring_incr(struct pasemi_softc *sc, int chan_index, int incr)
  15795. +{
  15796. + out_le32(sc->dma_regs + PAS_DMA_TXCHAN_INCR(sc->base_chan + chan_index),
  15797. + incr);
  15798. +}
  15799. +
  15800. +/*
  15801. + * Generate a new software session.
  15802. + */
  15803. +static int
  15804. +pasemi_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  15805. +{
  15806. + struct cryptoini *c, *encini = NULL, *macini = NULL;
  15807. + struct pasemi_softc *sc = device_get_softc(dev);
  15808. + struct pasemi_session *ses = NULL, **sespp;
  15809. + int sesn, blksz = 0;
  15810. + u64 ccmd = 0;
  15811. + unsigned long flags;
  15812. + struct pasemi_desc init_desc;
  15813. + struct pasemi_fnu_txring *txring;
  15814. +
  15815. + DPRINTF("%s()\n", __FUNCTION__);
  15816. + if (sidp == NULL || cri == NULL || sc == NULL) {
  15817. + DPRINTF("%s,%d - EINVAL\n", __FILE__, __LINE__);
  15818. + return -EINVAL;
  15819. + }
  15820. + for (c = cri; c != NULL; c = c->cri_next) {
  15821. + if (ALG_IS_SIG(c->cri_alg)) {
  15822. + if (macini)
  15823. + return -EINVAL;
  15824. + macini = c;
  15825. + } else if (ALG_IS_CIPHER(c->cri_alg)) {
  15826. + if (encini)
  15827. + return -EINVAL;
  15828. + encini = c;
  15829. + } else {
  15830. + DPRINTF("UNKNOWN c->cri_alg %d\n", c->cri_alg);
  15831. + return -EINVAL;
  15832. + }
  15833. + }
  15834. + if (encini == NULL && macini == NULL)
  15835. + return -EINVAL;
  15836. + if (encini) {
  15837. + /* validate key length */
  15838. + switch (encini->cri_alg) {
  15839. + case CRYPTO_DES_CBC:
  15840. + if (encini->cri_klen != 64)
  15841. + return -EINVAL;
  15842. + ccmd = DMA_CALGO_DES;
  15843. + break;
  15844. + case CRYPTO_3DES_CBC:
  15845. + if (encini->cri_klen != 192)
  15846. + return -EINVAL;
  15847. + ccmd = DMA_CALGO_3DES;
  15848. + break;
  15849. + case CRYPTO_AES_CBC:
  15850. + if (encini->cri_klen != 128 &&
  15851. + encini->cri_klen != 192 &&
  15852. + encini->cri_klen != 256)
  15853. + return -EINVAL;
  15854. + ccmd = DMA_CALGO_AES;
  15855. + break;
  15856. + case CRYPTO_ARC4:
  15857. + if (encini->cri_klen != 128)
  15858. + return -EINVAL;
  15859. + ccmd = DMA_CALGO_ARC;
  15860. + break;
  15861. + default:
  15862. + DPRINTF("UNKNOWN encini->cri_alg %d\n",
  15863. + encini->cri_alg);
  15864. + return -EINVAL;
  15865. + }
  15866. + }
  15867. +
  15868. + if (macini) {
  15869. + switch (macini->cri_alg) {
  15870. + case CRYPTO_MD5:
  15871. + case CRYPTO_MD5_HMAC:
  15872. + blksz = 16;
  15873. + break;
  15874. + case CRYPTO_SHA1:
  15875. + case CRYPTO_SHA1_HMAC:
  15876. + blksz = 20;
  15877. + break;
  15878. + default:
  15879. + DPRINTF("UNKNOWN macini->cri_alg %d\n",
  15880. + macini->cri_alg);
  15881. + return -EINVAL;
  15882. + }
  15883. + if (((macini->cri_klen + 7) / 8) > blksz) {
  15884. + DPRINTF("key length %d bigger than blksize %d not supported\n",
  15885. + ((macini->cri_klen + 7) / 8), blksz);
  15886. + return -EINVAL;
  15887. + }
  15888. + }
  15889. +
  15890. + for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  15891. + if (sc->sc_sessions[sesn] == NULL) {
  15892. + sc->sc_sessions[sesn] = (struct pasemi_session *)
  15893. + kzalloc(sizeof(struct pasemi_session), GFP_ATOMIC);
  15894. + ses = sc->sc_sessions[sesn];
  15895. + break;
  15896. + } else if (sc->sc_sessions[sesn]->used == 0) {
  15897. + ses = sc->sc_sessions[sesn];
  15898. + break;
  15899. + }
  15900. + }
  15901. +
  15902. + if (ses == NULL) {
  15903. + sespp = (struct pasemi_session **)
  15904. + kzalloc(sc->sc_nsessions * 2 *
  15905. + sizeof(struct pasemi_session *), GFP_ATOMIC);
  15906. + if (sespp == NULL)
  15907. + return -ENOMEM;
  15908. + memcpy(sespp, sc->sc_sessions,
  15909. + sc->sc_nsessions * sizeof(struct pasemi_session *));
  15910. + kfree(sc->sc_sessions);
  15911. + sc->sc_sessions = sespp;
  15912. + sesn = sc->sc_nsessions;
  15913. + ses = sc->sc_sessions[sesn] = (struct pasemi_session *)
  15914. + kzalloc(sizeof(struct pasemi_session), GFP_ATOMIC);
  15915. + if (ses == NULL)
  15916. + return -ENOMEM;
  15917. + sc->sc_nsessions *= 2;
  15918. + }
  15919. +
  15920. + ses->used = 1;
  15921. +
  15922. + ses->dma_addr = pci_map_single(sc->dma_pdev, (void *) ses->civ,
  15923. + sizeof(struct pasemi_session), DMA_TO_DEVICE);
  15924. +
  15925. + /* enter the channel scheduler */
  15926. + spin_lock_irqsave(&sc->sc_chnlock, flags);
  15927. +
  15928. + /* ARC4 has to be processed by the even channel */
  15929. + if (encini && (encini->cri_alg == CRYPTO_ARC4))
  15930. + ses->chan = sc->sc_lastchn & ~1;
  15931. + else
  15932. + ses->chan = sc->sc_lastchn;
  15933. + sc->sc_lastchn = (sc->sc_lastchn + 1) % sc->sc_num_channels;
  15934. +
  15935. + spin_unlock_irqrestore(&sc->sc_chnlock, flags);
  15936. +
  15937. + txring = &sc->tx[ses->chan];
  15938. +
  15939. + if (encini) {
  15940. + ses->ccmd = ccmd;
  15941. +
  15942. + /* get an IV */
  15943. + /* XXX may read fewer than requested */
  15944. + get_random_bytes(ses->civ, sizeof(ses->civ));
  15945. +
  15946. + ses->keysz = (encini->cri_klen - 63) / 64;
  15947. + memcpy(ses->key, encini->cri_key, (ses->keysz + 1) * 8);
  15948. +
  15949. + pasemi_desc_start(&init_desc,
  15950. + XCT_CTRL_HDR(ses->chan, (encini && macini) ? 0x68 : 0x40, DMA_FN_CIV0));
  15951. + pasemi_desc_build(&init_desc,
  15952. + XCT_FUN_SRC_PTR((encini && macini) ? 0x68 : 0x40, ses->dma_addr));
  15953. + }
  15954. + if (macini) {
  15955. + if (macini->cri_alg == CRYPTO_MD5_HMAC ||
  15956. + macini->cri_alg == CRYPTO_SHA1_HMAC)
  15957. + memcpy(ses->hkey, macini->cri_key, blksz);
  15958. + else {
  15959. + /* Load initialization constants(RFC 1321, 3174) */
  15960. + ses->hiv[0] = 0x67452301efcdab89ULL;
  15961. + ses->hiv[1] = 0x98badcfe10325476ULL;
  15962. + ses->hiv[2] = 0xc3d2e1f000000000ULL;
  15963. + }
  15964. + ses->hseq = 0ULL;
  15965. + }
  15966. +
  15967. + spin_lock_irqsave(&txring->fill_lock, flags);
  15968. +
  15969. + if (((txring->next_to_fill + pasemi_desc_size(&init_desc)) -
  15970. + txring->next_to_clean) > TX_RING_SIZE) {
  15971. + spin_unlock_irqrestore(&txring->fill_lock, flags);
  15972. + return ERESTART;
  15973. + }
  15974. +
  15975. + if (encini) {
  15976. + pasemi_ring_add_desc(txring, &init_desc, NULL);
  15977. + pasemi_ring_incr(sc, ses->chan,
  15978. + pasemi_desc_size(&init_desc));
  15979. + }
  15980. +
  15981. + txring->sesn = sesn;
  15982. + spin_unlock_irqrestore(&txring->fill_lock, flags);
  15983. +
  15984. + *sidp = PASEMI_SID(sesn);
  15985. + return 0;
  15986. +}
  15987. +
  15988. +/*
  15989. + * Deallocate a session.
  15990. + */
  15991. +static int
  15992. +pasemi_freesession(device_t dev, u_int64_t tid)
  15993. +{
  15994. + struct pasemi_softc *sc = device_get_softc(dev);
  15995. + int session;
  15996. + u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
  15997. +
  15998. + DPRINTF("%s()\n", __FUNCTION__);
  15999. +
  16000. + if (sc == NULL)
  16001. + return -EINVAL;
  16002. + session = PASEMI_SESSION(sid);
  16003. + if (session >= sc->sc_nsessions || !sc->sc_sessions[session])
  16004. + return -EINVAL;
  16005. +
  16006. + pci_unmap_single(sc->dma_pdev,
  16007. + sc->sc_sessions[session]->dma_addr,
  16008. + sizeof(struct pasemi_session), DMA_TO_DEVICE);
  16009. + memset(sc->sc_sessions[session], 0,
  16010. + sizeof(struct pasemi_session));
  16011. +
  16012. + return 0;
  16013. +}
  16014. +
  16015. +static int
  16016. +pasemi_process(device_t dev, struct cryptop *crp, int hint)
  16017. +{
  16018. +
  16019. + int err = 0, ivsize, srclen = 0, reinit = 0, reinit_size = 0, chsel;
  16020. + struct pasemi_softc *sc = device_get_softc(dev);
  16021. + struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  16022. + caddr_t ivp;
  16023. + struct pasemi_desc init_desc, work_desc;
  16024. + struct pasemi_session *ses;
  16025. + struct sk_buff *skb;
  16026. + struct uio *uiop;
  16027. + unsigned long flags;
  16028. + struct pasemi_fnu_txring *txring;
  16029. +
  16030. + DPRINTF("%s()\n", __FUNCTION__);
  16031. +
  16032. + if (crp == NULL || crp->crp_callback == NULL || sc == NULL)
  16033. + return -EINVAL;
  16034. +
  16035. + crp->crp_etype = 0;
  16036. + if (PASEMI_SESSION(crp->crp_sid) >= sc->sc_nsessions)
  16037. + return -EINVAL;
  16038. +
  16039. + ses = sc->sc_sessions[PASEMI_SESSION(crp->crp_sid)];
  16040. +
  16041. + crd1 = crp->crp_desc;
  16042. + if (crd1 == NULL) {
  16043. + err = -EINVAL;
  16044. + goto errout;
  16045. + }
  16046. + crd2 = crd1->crd_next;
  16047. +
  16048. + if (ALG_IS_SIG(crd1->crd_alg)) {
  16049. + maccrd = crd1;
  16050. + if (crd2 == NULL)
  16051. + enccrd = NULL;
  16052. + else if (ALG_IS_CIPHER(crd2->crd_alg) &&
  16053. + (crd2->crd_flags & CRD_F_ENCRYPT) == 0)
  16054. + enccrd = crd2;
  16055. + else
  16056. + goto erralg;
  16057. + } else if (ALG_IS_CIPHER(crd1->crd_alg)) {
  16058. + enccrd = crd1;
  16059. + if (crd2 == NULL)
  16060. + maccrd = NULL;
  16061. + else if (ALG_IS_SIG(crd2->crd_alg) &&
  16062. + (crd1->crd_flags & CRD_F_ENCRYPT))
  16063. + maccrd = crd2;
  16064. + else
  16065. + goto erralg;
  16066. + } else
  16067. + goto erralg;
  16068. +
  16069. + chsel = ses->chan;
  16070. +
  16071. + txring = &sc->tx[chsel];
  16072. +
  16073. + if (enccrd && !maccrd) {
  16074. + if (enccrd->crd_alg == CRYPTO_ARC4)
  16075. + reinit = 1;
  16076. + reinit_size = 0x40;
  16077. + srclen = crp->crp_ilen;
  16078. +
  16079. + pasemi_desc_start(&work_desc, XCT_FUN_O | XCT_FUN_I
  16080. + | XCT_FUN_FUN(chsel));
  16081. + if (enccrd->crd_flags & CRD_F_ENCRYPT)
  16082. + pasemi_desc_hdr(&work_desc, XCT_FUN_CRM_ENC);
  16083. + else
  16084. + pasemi_desc_hdr(&work_desc, XCT_FUN_CRM_DEC);
  16085. + } else if (enccrd && maccrd) {
  16086. + if (enccrd->crd_alg == CRYPTO_ARC4)
  16087. + reinit = 1;
  16088. + reinit_size = 0x68;
  16089. +
  16090. + if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  16091. + /* Encrypt -> Authenticate */
  16092. + pasemi_desc_start(&work_desc, XCT_FUN_O | XCT_FUN_I | XCT_FUN_CRM_ENC_SIG
  16093. + | XCT_FUN_A | XCT_FUN_FUN(chsel));
  16094. + srclen = maccrd->crd_skip + maccrd->crd_len;
  16095. + } else {
  16096. + /* Authenticate -> Decrypt */
  16097. + pasemi_desc_start(&work_desc, XCT_FUN_O | XCT_FUN_I | XCT_FUN_CRM_SIG_DEC
  16098. + | XCT_FUN_24BRES | XCT_FUN_FUN(chsel));
  16099. + pasemi_desc_build(&work_desc, 0);
  16100. + pasemi_desc_build(&work_desc, 0);
  16101. + pasemi_desc_build(&work_desc, 0);
  16102. + work_desc.postop = PASEMI_CHECK_SIG;
  16103. + srclen = crp->crp_ilen;
  16104. + }
  16105. +
  16106. + pasemi_desc_hdr(&work_desc, XCT_FUN_SHL(maccrd->crd_skip / 4));
  16107. + pasemi_desc_hdr(&work_desc, XCT_FUN_CHL(enccrd->crd_skip - maccrd->crd_skip));
  16108. + } else if (!enccrd && maccrd) {
  16109. + srclen = maccrd->crd_len;
  16110. +
  16111. + pasemi_desc_start(&init_desc,
  16112. + XCT_CTRL_HDR(chsel, 0x58, DMA_FN_HKEY0));
  16113. + pasemi_desc_build(&init_desc,
  16114. + XCT_FUN_SRC_PTR(0x58, ((struct pasemi_session *)ses->dma_addr)->hkey));
  16115. +
  16116. + pasemi_desc_start(&work_desc, XCT_FUN_O | XCT_FUN_I | XCT_FUN_CRM_SIG
  16117. + | XCT_FUN_A | XCT_FUN_FUN(chsel));
  16118. + }
  16119. +
  16120. + if (enccrd) {
  16121. + switch (enccrd->crd_alg) {
  16122. + case CRYPTO_3DES_CBC:
  16123. + pasemi_desc_hdr(&work_desc, XCT_FUN_ALG_3DES |
  16124. + XCT_FUN_BCM_CBC);
  16125. + ivsize = sizeof(u64);
  16126. + break;
  16127. + case CRYPTO_DES_CBC:
  16128. + pasemi_desc_hdr(&work_desc, XCT_FUN_ALG_DES |
  16129. + XCT_FUN_BCM_CBC);
  16130. + ivsize = sizeof(u64);
  16131. + break;
  16132. + case CRYPTO_AES_CBC:
  16133. + pasemi_desc_hdr(&work_desc, XCT_FUN_ALG_AES |
  16134. + XCT_FUN_BCM_CBC);
  16135. + ivsize = 2 * sizeof(u64);
  16136. + break;
  16137. + case CRYPTO_ARC4:
  16138. + pasemi_desc_hdr(&work_desc, XCT_FUN_ALG_ARC);
  16139. + ivsize = 0;
  16140. + break;
  16141. + default:
  16142. + printk(DRV_NAME ": unimplemented enccrd->crd_alg %d\n",
  16143. + enccrd->crd_alg);
  16144. + err = -EINVAL;
  16145. + goto errout;
  16146. + }
  16147. +
  16148. + ivp = (ivsize == sizeof(u64)) ? (caddr_t) &ses->civ[1] : (caddr_t) &ses->civ[0];
  16149. + if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  16150. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  16151. + memcpy(ivp, enccrd->crd_iv, ivsize);
  16152. + /* If IV is not present in the buffer already, it has to be copied there */
  16153. + if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
  16154. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  16155. + enccrd->crd_inject, ivsize, ivp);
  16156. + } else {
  16157. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  16158. + /* IV is provided expicitly in descriptor */
  16159. + memcpy(ivp, enccrd->crd_iv, ivsize);
  16160. + else
  16161. + /* IV is provided in the packet */
  16162. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  16163. + enccrd->crd_inject, ivsize,
  16164. + ivp);
  16165. + }
  16166. + }
  16167. +
  16168. + if (maccrd) {
  16169. + switch (maccrd->crd_alg) {
  16170. + case CRYPTO_MD5:
  16171. + pasemi_desc_hdr(&work_desc, XCT_FUN_SIG_MD5 |
  16172. + XCT_FUN_HSZ((crp->crp_ilen - maccrd->crd_inject) / 4));
  16173. + break;
  16174. + case CRYPTO_SHA1:
  16175. + pasemi_desc_hdr(&work_desc, XCT_FUN_SIG_SHA1 |
  16176. + XCT_FUN_HSZ((crp->crp_ilen - maccrd->crd_inject) / 4));
  16177. + break;
  16178. + case CRYPTO_MD5_HMAC:
  16179. + pasemi_desc_hdr(&work_desc, XCT_FUN_SIG_HMAC_MD5 |
  16180. + XCT_FUN_HSZ((crp->crp_ilen - maccrd->crd_inject) / 4));
  16181. + break;
  16182. + case CRYPTO_SHA1_HMAC:
  16183. + pasemi_desc_hdr(&work_desc, XCT_FUN_SIG_HMAC_SHA1 |
  16184. + XCT_FUN_HSZ((crp->crp_ilen - maccrd->crd_inject) / 4));
  16185. + break;
  16186. + default:
  16187. + printk(DRV_NAME ": unimplemented maccrd->crd_alg %d\n",
  16188. + maccrd->crd_alg);
  16189. + err = -EINVAL;
  16190. + goto errout;
  16191. + }
  16192. + }
  16193. +
  16194. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  16195. + /* using SKB buffers */
  16196. + skb = (struct sk_buff *)crp->crp_buf;
  16197. + if (skb_shinfo(skb)->nr_frags) {
  16198. + printk(DRV_NAME ": skb frags unimplemented\n");
  16199. + err = -EINVAL;
  16200. + goto errout;
  16201. + }
  16202. + pasemi_desc_build(
  16203. + &work_desc,
  16204. + XCT_FUN_DST_PTR(skb->len, pci_map_single(
  16205. + sc->dma_pdev, skb->data,
  16206. + skb->len, DMA_TO_DEVICE)));
  16207. + pasemi_desc_build(
  16208. + &work_desc,
  16209. + XCT_FUN_SRC_PTR(
  16210. + srclen, pci_map_single(
  16211. + sc->dma_pdev, skb->data,
  16212. + srclen, DMA_TO_DEVICE)));
  16213. + pasemi_desc_hdr(&work_desc, XCT_FUN_LLEN(srclen));
  16214. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  16215. + /* using IOV buffers */
  16216. + uiop = (struct uio *)crp->crp_buf;
  16217. + if (uiop->uio_iovcnt > 1) {
  16218. + printk(DRV_NAME ": iov frags unimplemented\n");
  16219. + err = -EINVAL;
  16220. + goto errout;
  16221. + }
  16222. +
  16223. + /* crp_olen is never set; always use crp_ilen */
  16224. + pasemi_desc_build(
  16225. + &work_desc,
  16226. + XCT_FUN_DST_PTR(crp->crp_ilen, pci_map_single(
  16227. + sc->dma_pdev,
  16228. + uiop->uio_iov->iov_base,
  16229. + crp->crp_ilen, DMA_TO_DEVICE)));
  16230. + pasemi_desc_hdr(&work_desc, XCT_FUN_LLEN(srclen));
  16231. +
  16232. + pasemi_desc_build(
  16233. + &work_desc,
  16234. + XCT_FUN_SRC_PTR(srclen, pci_map_single(
  16235. + sc->dma_pdev,
  16236. + uiop->uio_iov->iov_base,
  16237. + srclen, DMA_TO_DEVICE)));
  16238. + } else {
  16239. + /* using contig buffers */
  16240. + pasemi_desc_build(
  16241. + &work_desc,
  16242. + XCT_FUN_DST_PTR(crp->crp_ilen, pci_map_single(
  16243. + sc->dma_pdev,
  16244. + crp->crp_buf,
  16245. + crp->crp_ilen, DMA_TO_DEVICE)));
  16246. + pasemi_desc_build(
  16247. + &work_desc,
  16248. + XCT_FUN_SRC_PTR(srclen, pci_map_single(
  16249. + sc->dma_pdev,
  16250. + crp->crp_buf, srclen,
  16251. + DMA_TO_DEVICE)));
  16252. + pasemi_desc_hdr(&work_desc, XCT_FUN_LLEN(srclen));
  16253. + }
  16254. +
  16255. + spin_lock_irqsave(&txring->fill_lock, flags);
  16256. +
  16257. + if (txring->sesn != PASEMI_SESSION(crp->crp_sid)) {
  16258. + txring->sesn = PASEMI_SESSION(crp->crp_sid);
  16259. + reinit = 1;
  16260. + }
  16261. +
  16262. + if (enccrd) {
  16263. + pasemi_desc_start(&init_desc,
  16264. + XCT_CTRL_HDR(chsel, reinit ? reinit_size : 0x10, DMA_FN_CIV0));
  16265. + pasemi_desc_build(&init_desc,
  16266. + XCT_FUN_SRC_PTR(reinit ? reinit_size : 0x10, ses->dma_addr));
  16267. + }
  16268. +
  16269. + if (((txring->next_to_fill + pasemi_desc_size(&init_desc) +
  16270. + pasemi_desc_size(&work_desc)) -
  16271. + txring->next_to_clean) > TX_RING_SIZE) {
  16272. + spin_unlock_irqrestore(&txring->fill_lock, flags);
  16273. + err = ERESTART;
  16274. + goto errout;
  16275. + }
  16276. +
  16277. + pasemi_ring_add_desc(txring, &init_desc, NULL);
  16278. + pasemi_ring_add_desc(txring, &work_desc, crp);
  16279. +
  16280. + pasemi_ring_incr(sc, chsel,
  16281. + pasemi_desc_size(&init_desc) +
  16282. + pasemi_desc_size(&work_desc));
  16283. +
  16284. + spin_unlock_irqrestore(&txring->fill_lock, flags);
  16285. +
  16286. + mod_timer(&txring->crypto_timer, jiffies + TIMER_INTERVAL);
  16287. +
  16288. + return 0;
  16289. +
  16290. +erralg:
  16291. + printk(DRV_NAME ": unsupported algorithm or algorithm order alg1 %d alg2 %d\n",
  16292. + crd1->crd_alg, crd2->crd_alg);
  16293. + err = -EINVAL;
  16294. +
  16295. +errout:
  16296. + if (err != ERESTART) {
  16297. + crp->crp_etype = err;
  16298. + crypto_done(crp);
  16299. + }
  16300. + return err;
  16301. +}
  16302. +
  16303. +static int pasemi_clean_tx(struct pasemi_softc *sc, int chan)
  16304. +{
  16305. + int i, j, ring_idx;
  16306. + struct pasemi_fnu_txring *ring = &sc->tx[chan];
  16307. + u16 delta_cnt;
  16308. + int flags, loops = 10;
  16309. + int desc_size;
  16310. + struct cryptop *crp;
  16311. +
  16312. + spin_lock_irqsave(&ring->clean_lock, flags);
  16313. +
  16314. + while ((delta_cnt = (dma_status->tx_sta[sc->base_chan + chan]
  16315. + & PAS_STATUS_PCNT_M) - ring->total_pktcnt)
  16316. + && loops--) {
  16317. +
  16318. + for (i = 0; i < delta_cnt; i++) {
  16319. + desc_size = TX_DESC_INFO(ring, ring->next_to_clean).desc_size;
  16320. + crp = TX_DESC_INFO(ring, ring->next_to_clean).cf_crp;
  16321. + if (crp) {
  16322. + ring_idx = 2 * (ring->next_to_clean & (TX_RING_SIZE-1));
  16323. + if (TX_DESC_INFO(ring, ring->next_to_clean).desc_postop & PASEMI_CHECK_SIG) {
  16324. + /* Need to make sure signature matched,
  16325. + * if not - return error */
  16326. + if (!(ring->desc[ring_idx + 1] & (1ULL << 63)))
  16327. + crp->crp_etype = -EINVAL;
  16328. + }
  16329. + crypto_done(TX_DESC_INFO(ring,
  16330. + ring->next_to_clean).cf_crp);
  16331. + TX_DESC_INFO(ring, ring->next_to_clean).cf_crp = NULL;
  16332. + pci_unmap_single(
  16333. + sc->dma_pdev,
  16334. + XCT_PTR_ADDR_LEN(ring->desc[ring_idx + 1]),
  16335. + PCI_DMA_TODEVICE);
  16336. +
  16337. + ring->desc[ring_idx] = ring->desc[ring_idx + 1] = 0;
  16338. +
  16339. + ring->next_to_clean++;
  16340. + for (j = 1; j < desc_size; j++) {
  16341. + ring_idx = 2 *
  16342. + (ring->next_to_clean &
  16343. + (TX_RING_SIZE-1));
  16344. + pci_unmap_single(
  16345. + sc->dma_pdev,
  16346. + XCT_PTR_ADDR_LEN(ring->desc[ring_idx]),
  16347. + PCI_DMA_TODEVICE);
  16348. + if (ring->desc[ring_idx + 1])
  16349. + pci_unmap_single(
  16350. + sc->dma_pdev,
  16351. + XCT_PTR_ADDR_LEN(
  16352. + ring->desc[
  16353. + ring_idx + 1]),
  16354. + PCI_DMA_TODEVICE);
  16355. + ring->desc[ring_idx] =
  16356. + ring->desc[ring_idx + 1] = 0;
  16357. + ring->next_to_clean++;
  16358. + }
  16359. + } else {
  16360. + for (j = 0; j < desc_size; j++) {
  16361. + ring_idx = 2 * (ring->next_to_clean & (TX_RING_SIZE-1));
  16362. + ring->desc[ring_idx] =
  16363. + ring->desc[ring_idx + 1] = 0;
  16364. + ring->next_to_clean++;
  16365. + }
  16366. + }
  16367. + }
  16368. +
  16369. + ring->total_pktcnt += delta_cnt;
  16370. + }
  16371. + spin_unlock_irqrestore(&ring->clean_lock, flags);
  16372. +
  16373. + return 0;
  16374. +}
  16375. +
  16376. +static void sweepup_tx(struct pasemi_softc *sc)
  16377. +{
  16378. + int i;
  16379. +
  16380. + for (i = 0; i < sc->sc_num_channels; i++)
  16381. + pasemi_clean_tx(sc, i);
  16382. +}
  16383. +
  16384. +static irqreturn_t pasemi_intr(int irq, void *arg, struct pt_regs *regs)
  16385. +{
  16386. + struct pasemi_softc *sc = arg;
  16387. + unsigned int reg;
  16388. + int chan = irq - sc->base_irq;
  16389. + int chan_index = sc->base_chan + chan;
  16390. + u64 stat = dma_status->tx_sta[chan_index];
  16391. +
  16392. + DPRINTF("%s()\n", __FUNCTION__);
  16393. +
  16394. + if (!(stat & PAS_STATUS_CAUSE_M))
  16395. + return IRQ_NONE;
  16396. +
  16397. + pasemi_clean_tx(sc, chan);
  16398. +
  16399. + stat = dma_status->tx_sta[chan_index];
  16400. +
  16401. + reg = PAS_IOB_DMA_TXCH_RESET_PINTC |
  16402. + PAS_IOB_DMA_TXCH_RESET_PCNT(sc->tx[chan].total_pktcnt);
  16403. +
  16404. + if (stat & PAS_STATUS_SOFT)
  16405. + reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
  16406. +
  16407. + out_le32(sc->iob_regs + PAS_IOB_DMA_TXCH_RESET(chan_index), reg);
  16408. +
  16409. +
  16410. + return IRQ_HANDLED;
  16411. +}
  16412. +
  16413. +static int pasemi_dma_setup_tx_resources(struct pasemi_softc *sc, int chan)
  16414. +{
  16415. + u32 val;
  16416. + int chan_index = chan + sc->base_chan;
  16417. + int ret;
  16418. + struct pasemi_fnu_txring *ring;
  16419. +
  16420. + ring = &sc->tx[chan];
  16421. +
  16422. + spin_lock_init(&ring->fill_lock);
  16423. + spin_lock_init(&ring->clean_lock);
  16424. +
  16425. + ring->desc_info = kzalloc(sizeof(struct pasemi_desc_info) *
  16426. + TX_RING_SIZE, GFP_KERNEL);
  16427. + if (!ring->desc_info)
  16428. + return -ENOMEM;
  16429. +
  16430. + /* Allocate descriptors */
  16431. + ring->desc = dma_alloc_coherent(&sc->dma_pdev->dev,
  16432. + TX_RING_SIZE *
  16433. + 2 * sizeof(u64),
  16434. + &ring->dma, GFP_KERNEL);
  16435. + if (!ring->desc)
  16436. + return -ENOMEM;
  16437. +
  16438. + memset((void *) ring->desc, 0, TX_RING_SIZE * 2 * sizeof(u64));
  16439. +
  16440. + out_le32(sc->iob_regs + PAS_IOB_DMA_TXCH_RESET(chan_index), 0x30);
  16441. +
  16442. + ring->total_pktcnt = 0;
  16443. +
  16444. + out_le32(sc->dma_regs + PAS_DMA_TXCHAN_BASEL(chan_index),
  16445. + PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
  16446. +
  16447. + val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
  16448. + val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
  16449. +
  16450. + out_le32(sc->dma_regs + PAS_DMA_TXCHAN_BASEU(chan_index), val);
  16451. +
  16452. + out_le32(sc->dma_regs + PAS_DMA_TXCHAN_CFG(chan_index),
  16453. + PAS_DMA_TXCHAN_CFG_TY_FUNC |
  16454. + PAS_DMA_TXCHAN_CFG_TATTR(chan) |
  16455. + PAS_DMA_TXCHAN_CFG_WT(2));
  16456. +
  16457. + /* enable tx channel */
  16458. + out_le32(sc->dma_regs +
  16459. + PAS_DMA_TXCHAN_TCMDSTA(chan_index),
  16460. + PAS_DMA_TXCHAN_TCMDSTA_EN);
  16461. +
  16462. + out_le32(sc->iob_regs + PAS_IOB_DMA_TXCH_CFG(chan_index),
  16463. + PAS_IOB_DMA_TXCH_CFG_CNTTH(1000));
  16464. +
  16465. + ring->next_to_fill = 0;
  16466. + ring->next_to_clean = 0;
  16467. +
  16468. + snprintf(ring->irq_name, sizeof(ring->irq_name),
  16469. + "%s%d", "crypto", chan);
  16470. +
  16471. + ring->irq = irq_create_mapping(NULL, sc->base_irq + chan);
  16472. + ret = request_irq(ring->irq, (irq_handler_t)
  16473. + pasemi_intr, IRQF_DISABLED, ring->irq_name, sc);
  16474. + if (ret) {
  16475. + printk(KERN_ERR DRV_NAME ": failed to hook irq %d ret %d\n",
  16476. + ring->irq, ret);
  16477. + ring->irq = -1;
  16478. + return ret;
  16479. + }
  16480. +
  16481. + setup_timer(&ring->crypto_timer, (void *) sweepup_tx, (unsigned long) sc);
  16482. +
  16483. + return 0;
  16484. +}
  16485. +
  16486. +static device_method_t pasemi_methods = {
  16487. + /* crypto device methods */
  16488. + DEVMETHOD(cryptodev_newsession, pasemi_newsession),
  16489. + DEVMETHOD(cryptodev_freesession, pasemi_freesession),
  16490. + DEVMETHOD(cryptodev_process, pasemi_process),
  16491. +};
  16492. +
  16493. +/* Set up the crypto device structure, private data,
  16494. + * and anything else we need before we start */
  16495. +
  16496. +static int __devinit
  16497. +pasemi_dma_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  16498. +{
  16499. + struct pasemi_softc *sc;
  16500. + int ret, i;
  16501. +
  16502. + DPRINTF(KERN_ERR "%s()\n", __FUNCTION__);
  16503. +
  16504. + sc = kzalloc(sizeof(*sc), GFP_KERNEL);
  16505. + if (!sc)
  16506. + return -ENOMEM;
  16507. +
  16508. + softc_device_init(sc, DRV_NAME, 1, pasemi_methods);
  16509. +
  16510. + pci_set_drvdata(pdev, sc);
  16511. +
  16512. + spin_lock_init(&sc->sc_chnlock);
  16513. +
  16514. + sc->sc_sessions = (struct pasemi_session **)
  16515. + kzalloc(PASEMI_INITIAL_SESSIONS *
  16516. + sizeof(struct pasemi_session *), GFP_ATOMIC);
  16517. + if (sc->sc_sessions == NULL) {
  16518. + ret = -ENOMEM;
  16519. + goto out;
  16520. + }
  16521. +
  16522. + sc->sc_nsessions = PASEMI_INITIAL_SESSIONS;
  16523. + sc->sc_lastchn = 0;
  16524. + sc->base_irq = pdev->irq + 6;
  16525. + sc->base_chan = 6;
  16526. + sc->sc_cid = -1;
  16527. + sc->dma_pdev = pdev;
  16528. +
  16529. + sc->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
  16530. + if (!sc->iob_pdev) {
  16531. + dev_err(&pdev->dev, "Can't find I/O Bridge\n");
  16532. + ret = -ENODEV;
  16533. + goto out;
  16534. + }
  16535. +
  16536. + /* This is hardcoded and ugly, but we have some firmware versions
  16537. + * who don't provide the register space in the device tree. Luckily
  16538. + * they are at well-known locations so we can just do the math here.
  16539. + */
  16540. + sc->dma_regs =
  16541. + ioremap(0xe0000000 + (sc->dma_pdev->devfn << 12), 0x2000);
  16542. + sc->iob_regs =
  16543. + ioremap(0xe0000000 + (sc->iob_pdev->devfn << 12), 0x2000);
  16544. + if (!sc->dma_regs || !sc->iob_regs) {
  16545. + dev_err(&pdev->dev, "Can't map registers\n");
  16546. + ret = -ENODEV;
  16547. + goto out;
  16548. + }
  16549. +
  16550. + dma_status = __ioremap(0xfd800000, 0x1000, 0);
  16551. + if (!dma_status) {
  16552. + ret = -ENODEV;
  16553. + dev_err(&pdev->dev, "Can't map dmastatus space\n");
  16554. + goto out;
  16555. + }
  16556. +
  16557. + sc->tx = (struct pasemi_fnu_txring *)
  16558. + kzalloc(sizeof(struct pasemi_fnu_txring)
  16559. + * 8, GFP_KERNEL);
  16560. + if (!sc->tx) {
  16561. + ret = -ENOMEM;
  16562. + goto out;
  16563. + }
  16564. +
  16565. + /* Initialize the h/w */
  16566. + out_le32(sc->dma_regs + PAS_DMA_COM_CFG,
  16567. + (in_le32(sc->dma_regs + PAS_DMA_COM_CFG) |
  16568. + PAS_DMA_COM_CFG_FWF));
  16569. + out_le32(sc->dma_regs + PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
  16570. +
  16571. + for (i = 0; i < PASEMI_FNU_CHANNELS; i++) {
  16572. + sc->sc_num_channels++;
  16573. + ret = pasemi_dma_setup_tx_resources(sc, i);
  16574. + if (ret)
  16575. + goto out;
  16576. + }
  16577. +
  16578. + sc->sc_cid = crypto_get_driverid(softc_get_device(sc),
  16579. + CRYPTOCAP_F_HARDWARE);
  16580. + if (sc->sc_cid < 0) {
  16581. + printk(KERN_ERR DRV_NAME ": could not get crypto driver id\n");
  16582. + ret = -ENXIO;
  16583. + goto out;
  16584. + }
  16585. +
  16586. + /* register algorithms with the framework */
  16587. + printk(DRV_NAME ":");
  16588. +
  16589. + crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  16590. + crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  16591. + crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  16592. + crypto_register(sc->sc_cid, CRYPTO_ARC4, 0, 0);
  16593. + crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
  16594. + crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
  16595. + crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  16596. + crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  16597. +
  16598. + return 0;
  16599. +
  16600. +out:
  16601. + pasemi_dma_remove(pdev);
  16602. + return ret;
  16603. +}
  16604. +
  16605. +#define MAX_RETRIES 5000
  16606. +
  16607. +static void pasemi_free_tx_resources(struct pasemi_softc *sc, int chan)
  16608. +{
  16609. + struct pasemi_fnu_txring *ring = &sc->tx[chan];
  16610. + int chan_index = chan + sc->base_chan;
  16611. + int retries;
  16612. + u32 stat;
  16613. +
  16614. + /* Stop the channel */
  16615. + out_le32(sc->dma_regs +
  16616. + PAS_DMA_TXCHAN_TCMDSTA(chan_index),
  16617. + PAS_DMA_TXCHAN_TCMDSTA_ST);
  16618. +
  16619. + for (retries = 0; retries < MAX_RETRIES; retries++) {
  16620. + stat = in_le32(sc->dma_regs +
  16621. + PAS_DMA_TXCHAN_TCMDSTA(chan_index));
  16622. + if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
  16623. + break;
  16624. + cond_resched();
  16625. + }
  16626. +
  16627. + if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
  16628. + dev_err(&sc->dma_pdev->dev, "Failed to stop tx channel %d\n",
  16629. + chan_index);
  16630. +
  16631. + /* Disable the channel */
  16632. + out_le32(sc->dma_regs +
  16633. + PAS_DMA_TXCHAN_TCMDSTA(chan_index),
  16634. + 0);
  16635. +
  16636. + if (ring->desc_info)
  16637. + kfree((void *) ring->desc_info);
  16638. + if (ring->desc)
  16639. + dma_free_coherent(&sc->dma_pdev->dev,
  16640. + TX_RING_SIZE *
  16641. + 2 * sizeof(u64),
  16642. + (void *) ring->desc, ring->dma);
  16643. + if (ring->irq != -1)
  16644. + free_irq(ring->irq, sc);
  16645. +
  16646. + del_timer(&ring->crypto_timer);
  16647. +}
  16648. +
  16649. +static void __devexit pasemi_dma_remove(struct pci_dev *pdev)
  16650. +{
  16651. + struct pasemi_softc *sc = pci_get_drvdata(pdev);
  16652. + int i;
  16653. +
  16654. + DPRINTF("%s()\n", __FUNCTION__);
  16655. +
  16656. + if (sc->sc_cid >= 0) {
  16657. + crypto_unregister_all(sc->sc_cid);
  16658. + }
  16659. +
  16660. + if (sc->tx) {
  16661. + for (i = 0; i < sc->sc_num_channels; i++)
  16662. + pasemi_free_tx_resources(sc, i);
  16663. +
  16664. + kfree(sc->tx);
  16665. + }
  16666. + if (sc->sc_sessions) {
  16667. + for (i = 0; i < sc->sc_nsessions; i++)
  16668. + kfree(sc->sc_sessions[i]);
  16669. + kfree(sc->sc_sessions);
  16670. + }
  16671. + if (sc->iob_pdev)
  16672. + pci_dev_put(sc->iob_pdev);
  16673. + if (sc->dma_regs)
  16674. + iounmap(sc->dma_regs);
  16675. + if (sc->iob_regs)
  16676. + iounmap(sc->iob_regs);
  16677. + kfree(sc);
  16678. +}
  16679. +
  16680. +static struct pci_device_id pasemi_dma_pci_tbl[] = {
  16681. + { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa007) },
  16682. +};
  16683. +
  16684. +MODULE_DEVICE_TABLE(pci, pasemi_dma_pci_tbl);
  16685. +
  16686. +static struct pci_driver pasemi_dma_driver = {
  16687. + .name = "pasemi_dma",
  16688. + .id_table = pasemi_dma_pci_tbl,
  16689. + .probe = pasemi_dma_probe,
  16690. + .remove = __devexit_p(pasemi_dma_remove),
  16691. +};
  16692. +
  16693. +static void __exit pasemi_dma_cleanup_module(void)
  16694. +{
  16695. + pci_unregister_driver(&pasemi_dma_driver);
  16696. + __iounmap(dma_status);
  16697. + dma_status = NULL;
  16698. +}
  16699. +
  16700. +int pasemi_dma_init_module(void)
  16701. +{
  16702. + return pci_register_driver(&pasemi_dma_driver);
  16703. +}
  16704. +
  16705. +module_init(pasemi_dma_init_module);
  16706. +module_exit(pasemi_dma_cleanup_module);
  16707. +
  16708. +MODULE_LICENSE("Dual BSD/GPL");
  16709. +MODULE_AUTHOR("Egor Martovetsky egor@pasemi.com");
  16710. +MODULE_DESCRIPTION("OCF driver for PA Semi PWRficient DMA Crypto Engine");
  16711. diff -Nur linux-2.6.30.orig/crypto/ocf/pasemi/pasemi_fnu.h linux-2.6.30/crypto/ocf/pasemi/pasemi_fnu.h
  16712. --- linux-2.6.30.orig/crypto/ocf/pasemi/pasemi_fnu.h 1970-01-01 01:00:00.000000000 +0100
  16713. +++ linux-2.6.30/crypto/ocf/pasemi/pasemi_fnu.h 2009-06-11 10:55:27.000000000 +0200
  16714. @@ -0,0 +1,410 @@
  16715. +/*
  16716. + * Copyright (C) 2007 PA Semi, Inc
  16717. + *
  16718. + * Driver for the PA Semi PWRficient DMA Crypto Engine, soft state and
  16719. + * hardware register layouts.
  16720. + *
  16721. + * This program is free software; you can redistribute it and/or modify
  16722. + * it under the terms of the GNU General Public License version 2 as
  16723. + * published by the Free Software Foundation.
  16724. + *
  16725. + * This program is distributed in the hope that it will be useful,
  16726. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16727. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16728. + * GNU General Public License for more details.
  16729. + *
  16730. + * You should have received a copy of the GNU General Public License
  16731. + * along with this program; if not, write to the Free Software
  16732. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16733. + */
  16734. +
  16735. +#ifndef PASEMI_FNU_H
  16736. +#define PASEMI_FNU_H
  16737. +
  16738. +#include <linux/spinlock.h>
  16739. +
  16740. +#define PASEMI_SESSION(sid) ((sid) & 0xffffffff)
  16741. +#define PASEMI_SID(sesn) ((sesn) & 0xffffffff)
  16742. +#define DPRINTF(a...) if (debug) { printk(DRV_NAME ": " a); }
  16743. +
  16744. +/* Must be a power of two */
  16745. +#define RX_RING_SIZE 512
  16746. +#define TX_RING_SIZE 512
  16747. +#define TX_DESC(ring, num) ((ring)->desc[2 * (num & (TX_RING_SIZE-1))])
  16748. +#define TX_DESC_INFO(ring, num) ((ring)->desc_info[(num) & (TX_RING_SIZE-1)])
  16749. +#define MAX_DESC_SIZE 8
  16750. +#define PASEMI_INITIAL_SESSIONS 10
  16751. +#define PASEMI_FNU_CHANNELS 8
  16752. +
  16753. +/* DMA descriptor */
  16754. +struct pasemi_desc {
  16755. + u64 quad[2*MAX_DESC_SIZE];
  16756. + int quad_cnt;
  16757. + int size;
  16758. + int postop;
  16759. +};
  16760. +
  16761. +/*
  16762. + * Holds per descriptor data
  16763. + */
  16764. +struct pasemi_desc_info {
  16765. + int desc_size;
  16766. + int desc_postop;
  16767. +#define PASEMI_CHECK_SIG 0x1
  16768. +
  16769. + struct cryptop *cf_crp;
  16770. +};
  16771. +
  16772. +/*
  16773. + * Holds per channel data
  16774. + */
  16775. +struct pasemi_fnu_txring {
  16776. + volatile u64 *desc;
  16777. + volatile struct
  16778. + pasemi_desc_info *desc_info;
  16779. + dma_addr_t dma;
  16780. + struct timer_list crypto_timer;
  16781. + spinlock_t fill_lock;
  16782. + spinlock_t clean_lock;
  16783. + unsigned int next_to_fill;
  16784. + unsigned int next_to_clean;
  16785. + u16 total_pktcnt;
  16786. + int irq;
  16787. + int sesn;
  16788. + char irq_name[10];
  16789. +};
  16790. +
  16791. +/*
  16792. + * Holds data specific to a single pasemi device.
  16793. + */
  16794. +struct pasemi_softc {
  16795. + softc_device_decl sc_cdev;
  16796. + struct pci_dev *dma_pdev; /* device backpointer */
  16797. + struct pci_dev *iob_pdev; /* device backpointer */
  16798. + void __iomem *dma_regs;
  16799. + void __iomem *iob_regs;
  16800. + int base_irq;
  16801. + int base_chan;
  16802. + int32_t sc_cid; /* crypto tag */
  16803. + int sc_nsessions;
  16804. + struct pasemi_session **sc_sessions;
  16805. + int sc_num_channels;/* number of crypto channels */
  16806. +
  16807. + /* pointer to the array of txring datastructures, one txring per channel */
  16808. + struct pasemi_fnu_txring *tx;
  16809. +
  16810. + /*
  16811. + * mutual exclusion for the channel scheduler
  16812. + */
  16813. + spinlock_t sc_chnlock;
  16814. + /* last channel used, for now use round-robin to allocate channels */
  16815. + int sc_lastchn;
  16816. +};
  16817. +
  16818. +struct pasemi_session {
  16819. + u64 civ[2];
  16820. + u64 keysz;
  16821. + u64 key[4];
  16822. + u64 ccmd;
  16823. + u64 hkey[4];
  16824. + u64 hseq;
  16825. + u64 giv[2];
  16826. + u64 hiv[4];
  16827. +
  16828. + int used;
  16829. + dma_addr_t dma_addr;
  16830. + int chan;
  16831. +};
  16832. +
  16833. +/* status register layout in IOB region, at 0xfd800000 */
  16834. +struct pasdma_status {
  16835. + u64 rx_sta[64];
  16836. + u64 tx_sta[20];
  16837. +};
  16838. +
  16839. +#define ALG_IS_CIPHER(alg) ((alg == CRYPTO_DES_CBC) || \
  16840. + (alg == CRYPTO_3DES_CBC) || \
  16841. + (alg == CRYPTO_AES_CBC) || \
  16842. + (alg == CRYPTO_ARC4) || \
  16843. + (alg == CRYPTO_NULL_CBC))
  16844. +
  16845. +#define ALG_IS_SIG(alg) ((alg == CRYPTO_MD5) || \
  16846. + (alg == CRYPTO_MD5_HMAC) || \
  16847. + (alg == CRYPTO_SHA1) || \
  16848. + (alg == CRYPTO_SHA1_HMAC) || \
  16849. + (alg == CRYPTO_NULL_HMAC))
  16850. +
  16851. +enum {
  16852. + PAS_DMA_COM_TXCMD = 0x100, /* Transmit Command Register */
  16853. + PAS_DMA_COM_TXSTA = 0x104, /* Transmit Status Register */
  16854. + PAS_DMA_COM_RXCMD = 0x108, /* Receive Command Register */
  16855. + PAS_DMA_COM_RXSTA = 0x10c, /* Receive Status Register */
  16856. + PAS_DMA_COM_CFG = 0x114, /* DMA Configuration Register */
  16857. +};
  16858. +
  16859. +/* All these registers live in the PCI configuration space for the DMA PCI
  16860. + * device. Use the normal PCI config access functions for them.
  16861. + */
  16862. +
  16863. +#define PAS_DMA_COM_CFG_FWF 0x18000000
  16864. +
  16865. +#define PAS_DMA_COM_TXCMD_EN 0x00000001 /* enable */
  16866. +#define PAS_DMA_COM_TXSTA_ACT 0x00000001 /* active */
  16867. +#define PAS_DMA_COM_RXCMD_EN 0x00000001 /* enable */
  16868. +#define PAS_DMA_COM_RXSTA_ACT 0x00000001 /* active */
  16869. +
  16870. +#define _PAS_DMA_TXCHAN_STRIDE 0x20 /* Size per channel */
  16871. +#define _PAS_DMA_TXCHAN_TCMDSTA 0x300 /* Command / Status */
  16872. +#define _PAS_DMA_TXCHAN_CFG 0x304 /* Configuration */
  16873. +#define _PAS_DMA_TXCHAN_DSCRBU 0x308 /* Descriptor BU Allocation */
  16874. +#define _PAS_DMA_TXCHAN_INCR 0x310 /* Descriptor increment */
  16875. +#define _PAS_DMA_TXCHAN_CNT 0x314 /* Descriptor count/offset */
  16876. +#define _PAS_DMA_TXCHAN_BASEL 0x318 /* Descriptor ring base (low) */
  16877. +#define _PAS_DMA_TXCHAN_BASEU 0x31c /* (high) */
  16878. +#define PAS_DMA_TXCHAN_TCMDSTA(c) (0x300+(c)*_PAS_DMA_TXCHAN_STRIDE)
  16879. +#define PAS_DMA_TXCHAN_TCMDSTA_EN 0x00000001 /* Enabled */
  16880. +#define PAS_DMA_TXCHAN_TCMDSTA_ST 0x00000002 /* Stop interface */
  16881. +#define PAS_DMA_TXCHAN_TCMDSTA_ACT 0x00010000 /* Active */
  16882. +#define PAS_DMA_TXCHAN_CFG(c) (0x304+(c)*_PAS_DMA_TXCHAN_STRIDE)
  16883. +#define PAS_DMA_TXCHAN_CFG_TY_FUNC 0x00000002 /* Type = interface */
  16884. +#define PAS_DMA_TXCHAN_CFG_TY_IFACE 0x00000000 /* Type = interface */
  16885. +#define PAS_DMA_TXCHAN_CFG_TATTR_M 0x0000003c
  16886. +#define PAS_DMA_TXCHAN_CFG_TATTR_S 2
  16887. +#define PAS_DMA_TXCHAN_CFG_TATTR(x) (((x) << PAS_DMA_TXCHAN_CFG_TATTR_S) & \
  16888. + PAS_DMA_TXCHAN_CFG_TATTR_M)
  16889. +#define PAS_DMA_TXCHAN_CFG_WT_M 0x000001c0
  16890. +#define PAS_DMA_TXCHAN_CFG_WT_S 6
  16891. +#define PAS_DMA_TXCHAN_CFG_WT(x) (((x) << PAS_DMA_TXCHAN_CFG_WT_S) & \
  16892. + PAS_DMA_TXCHAN_CFG_WT_M)
  16893. +#define PAS_DMA_TXCHAN_CFG_LPSQ_FAST 0x00000400
  16894. +#define PAS_DMA_TXCHAN_CFG_LPDQ_FAST 0x00000800
  16895. +#define PAS_DMA_TXCHAN_CFG_CF 0x00001000 /* Clean first line */
  16896. +#define PAS_DMA_TXCHAN_CFG_CL 0x00002000 /* Clean last line */
  16897. +#define PAS_DMA_TXCHAN_CFG_UP 0x00004000 /* update tx descr when sent */
  16898. +#define PAS_DMA_TXCHAN_INCR(c) (0x310+(c)*_PAS_DMA_TXCHAN_STRIDE)
  16899. +#define PAS_DMA_TXCHAN_BASEL(c) (0x318+(c)*_PAS_DMA_TXCHAN_STRIDE)
  16900. +#define PAS_DMA_TXCHAN_BASEL_BRBL_M 0xffffffc0
  16901. +#define PAS_DMA_TXCHAN_BASEL_BRBL_S 0
  16902. +#define PAS_DMA_TXCHAN_BASEL_BRBL(x) (((x) << PAS_DMA_TXCHAN_BASEL_BRBL_S) & \
  16903. + PAS_DMA_TXCHAN_BASEL_BRBL_M)
  16904. +#define PAS_DMA_TXCHAN_BASEU(c) (0x31c+(c)*_PAS_DMA_TXCHAN_STRIDE)
  16905. +#define PAS_DMA_TXCHAN_BASEU_BRBH_M 0x00000fff
  16906. +#define PAS_DMA_TXCHAN_BASEU_BRBH_S 0
  16907. +#define PAS_DMA_TXCHAN_BASEU_BRBH(x) (((x) << PAS_DMA_TXCHAN_BASEU_BRBH_S) & \
  16908. + PAS_DMA_TXCHAN_BASEU_BRBH_M)
  16909. +/* # of cache lines worth of buffer ring */
  16910. +#define PAS_DMA_TXCHAN_BASEU_SIZ_M 0x3fff0000
  16911. +#define PAS_DMA_TXCHAN_BASEU_SIZ_S 16 /* 0 = 16K */
  16912. +#define PAS_DMA_TXCHAN_BASEU_SIZ(x) (((x) << PAS_DMA_TXCHAN_BASEU_SIZ_S) & \
  16913. + PAS_DMA_TXCHAN_BASEU_SIZ_M)
  16914. +
  16915. +#define PAS_STATUS_PCNT_M 0x000000000000ffffull
  16916. +#define PAS_STATUS_PCNT_S 0
  16917. +#define PAS_STATUS_DCNT_M 0x00000000ffff0000ull
  16918. +#define PAS_STATUS_DCNT_S 16
  16919. +#define PAS_STATUS_BPCNT_M 0x0000ffff00000000ull
  16920. +#define PAS_STATUS_BPCNT_S 32
  16921. +#define PAS_STATUS_CAUSE_M 0xf000000000000000ull
  16922. +#define PAS_STATUS_TIMER 0x1000000000000000ull
  16923. +#define PAS_STATUS_ERROR 0x2000000000000000ull
  16924. +#define PAS_STATUS_SOFT 0x4000000000000000ull
  16925. +#define PAS_STATUS_INT 0x8000000000000000ull
  16926. +
  16927. +#define PAS_IOB_DMA_RXCH_CFG(i) (0x1100 + (i)*4)
  16928. +#define PAS_IOB_DMA_RXCH_CFG_CNTTH_M 0x00000fff
  16929. +#define PAS_IOB_DMA_RXCH_CFG_CNTTH_S 0
  16930. +#define PAS_IOB_DMA_RXCH_CFG_CNTTH(x) (((x) << PAS_IOB_DMA_RXCH_CFG_CNTTH_S) & \
  16931. + PAS_IOB_DMA_RXCH_CFG_CNTTH_M)
  16932. +#define PAS_IOB_DMA_TXCH_CFG(i) (0x1200 + (i)*4)
  16933. +#define PAS_IOB_DMA_TXCH_CFG_CNTTH_M 0x00000fff
  16934. +#define PAS_IOB_DMA_TXCH_CFG_CNTTH_S 0
  16935. +#define PAS_IOB_DMA_TXCH_CFG_CNTTH(x) (((x) << PAS_IOB_DMA_TXCH_CFG_CNTTH_S) & \
  16936. + PAS_IOB_DMA_TXCH_CFG_CNTTH_M)
  16937. +#define PAS_IOB_DMA_RXCH_STAT(i) (0x1300 + (i)*4)
  16938. +#define PAS_IOB_DMA_RXCH_STAT_INTGEN 0x00001000
  16939. +#define PAS_IOB_DMA_RXCH_STAT_CNTDEL_M 0x00000fff
  16940. +#define PAS_IOB_DMA_RXCH_STAT_CNTDEL_S 0
  16941. +#define PAS_IOB_DMA_RXCH_STAT_CNTDEL(x) (((x) << PAS_IOB_DMA_RXCH_STAT_CNTDEL_S) &\
  16942. + PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
  16943. +#define PAS_IOB_DMA_TXCH_STAT(i) (0x1400 + (i)*4)
  16944. +#define PAS_IOB_DMA_TXCH_STAT_INTGEN 0x00001000
  16945. +#define PAS_IOB_DMA_TXCH_STAT_CNTDEL_M 0x00000fff
  16946. +#define PAS_IOB_DMA_TXCH_STAT_CNTDEL_S 0
  16947. +#define PAS_IOB_DMA_TXCH_STAT_CNTDEL(x) (((x) << PAS_IOB_DMA_TXCH_STAT_CNTDEL_S) &\
  16948. + PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
  16949. +#define PAS_IOB_DMA_RXCH_RESET(i) (0x1500 + (i)*4)
  16950. +#define PAS_IOB_DMA_RXCH_RESET_PCNT_M 0xffff0000
  16951. +#define PAS_IOB_DMA_RXCH_RESET_PCNT_S 16
  16952. +#define PAS_IOB_DMA_RXCH_RESET_PCNT(x) (((x) << PAS_IOB_DMA_RXCH_RESET_PCNT_S) & \
  16953. + PAS_IOB_DMA_RXCH_RESET_PCNT_M)
  16954. +#define PAS_IOB_DMA_RXCH_RESET_PCNTRST 0x00000020
  16955. +#define PAS_IOB_DMA_RXCH_RESET_DCNTRST 0x00000010
  16956. +#define PAS_IOB_DMA_RXCH_RESET_TINTC 0x00000008
  16957. +#define PAS_IOB_DMA_RXCH_RESET_DINTC 0x00000004
  16958. +#define PAS_IOB_DMA_RXCH_RESET_SINTC 0x00000002
  16959. +#define PAS_IOB_DMA_RXCH_RESET_PINTC 0x00000001
  16960. +#define PAS_IOB_DMA_TXCH_RESET(i) (0x1600 + (i)*4)
  16961. +#define PAS_IOB_DMA_TXCH_RESET_PCNT_M 0xffff0000
  16962. +#define PAS_IOB_DMA_TXCH_RESET_PCNT_S 16
  16963. +#define PAS_IOB_DMA_TXCH_RESET_PCNT(x) (((x) << PAS_IOB_DMA_TXCH_RESET_PCNT_S) & \
  16964. + PAS_IOB_DMA_TXCH_RESET_PCNT_M)
  16965. +#define PAS_IOB_DMA_TXCH_RESET_PCNTRST 0x00000020
  16966. +#define PAS_IOB_DMA_TXCH_RESET_DCNTRST 0x00000010
  16967. +#define PAS_IOB_DMA_TXCH_RESET_TINTC 0x00000008
  16968. +#define PAS_IOB_DMA_TXCH_RESET_DINTC 0x00000004
  16969. +#define PAS_IOB_DMA_TXCH_RESET_SINTC 0x00000002
  16970. +#define PAS_IOB_DMA_TXCH_RESET_PINTC 0x00000001
  16971. +
  16972. +#define PAS_IOB_DMA_COM_TIMEOUTCFG 0x1700
  16973. +#define PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT_M 0x00ffffff
  16974. +#define PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT_S 0
  16975. +#define PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(x) (((x) << PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT_S) & \
  16976. + PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT_M)
  16977. +
  16978. +/* Transmit descriptor fields */
  16979. +#define XCT_MACTX_T 0x8000000000000000ull
  16980. +#define XCT_MACTX_ST 0x4000000000000000ull
  16981. +#define XCT_MACTX_NORES 0x0000000000000000ull
  16982. +#define XCT_MACTX_8BRES 0x1000000000000000ull
  16983. +#define XCT_MACTX_24BRES 0x2000000000000000ull
  16984. +#define XCT_MACTX_40BRES 0x3000000000000000ull
  16985. +#define XCT_MACTX_I 0x0800000000000000ull
  16986. +#define XCT_MACTX_O 0x0400000000000000ull
  16987. +#define XCT_MACTX_E 0x0200000000000000ull
  16988. +#define XCT_MACTX_VLAN_M 0x0180000000000000ull
  16989. +#define XCT_MACTX_VLAN_NOP 0x0000000000000000ull
  16990. +#define XCT_MACTX_VLAN_REMOVE 0x0080000000000000ull
  16991. +#define XCT_MACTX_VLAN_INSERT 0x0100000000000000ull
  16992. +#define XCT_MACTX_VLAN_REPLACE 0x0180000000000000ull
  16993. +#define XCT_MACTX_CRC_M 0x0060000000000000ull
  16994. +#define XCT_MACTX_CRC_NOP 0x0000000000000000ull
  16995. +#define XCT_MACTX_CRC_INSERT 0x0020000000000000ull
  16996. +#define XCT_MACTX_CRC_PAD 0x0040000000000000ull
  16997. +#define XCT_MACTX_CRC_REPLACE 0x0060000000000000ull
  16998. +#define XCT_MACTX_SS 0x0010000000000000ull
  16999. +#define XCT_MACTX_LLEN_M 0x00007fff00000000ull
  17000. +#define XCT_MACTX_LLEN_S 32ull
  17001. +#define XCT_MACTX_LLEN(x) ((((long)(x)) << XCT_MACTX_LLEN_S) & \
  17002. + XCT_MACTX_LLEN_M)
  17003. +#define XCT_MACTX_IPH_M 0x00000000f8000000ull
  17004. +#define XCT_MACTX_IPH_S 27ull
  17005. +#define XCT_MACTX_IPH(x) ((((long)(x)) << XCT_MACTX_IPH_S) & \
  17006. + XCT_MACTX_IPH_M)
  17007. +#define XCT_MACTX_IPO_M 0x0000000007c00000ull
  17008. +#define XCT_MACTX_IPO_S 22ull
  17009. +#define XCT_MACTX_IPO(x) ((((long)(x)) << XCT_MACTX_IPO_S) & \
  17010. + XCT_MACTX_IPO_M)
  17011. +#define XCT_MACTX_CSUM_M 0x0000000000000060ull
  17012. +#define XCT_MACTX_CSUM_NOP 0x0000000000000000ull
  17013. +#define XCT_MACTX_CSUM_TCP 0x0000000000000040ull
  17014. +#define XCT_MACTX_CSUM_UDP 0x0000000000000060ull
  17015. +#define XCT_MACTX_V6 0x0000000000000010ull
  17016. +#define XCT_MACTX_C 0x0000000000000004ull
  17017. +#define XCT_MACTX_AL2 0x0000000000000002ull
  17018. +
  17019. +#define XCT_PTR_T 0x8000000000000000ull
  17020. +#define XCT_PTR_LEN_M 0x7ffff00000000000ull
  17021. +#define XCT_PTR_LEN_S 44
  17022. +#define XCT_PTR_LEN(x) ((((long)(x)) << XCT_PTR_LEN_S) & \
  17023. + XCT_PTR_LEN_M)
  17024. +#define XCT_PTR_ADDR_M 0x00000fffffffffffull
  17025. +#define XCT_PTR_ADDR_S 0
  17026. +#define XCT_PTR_ADDR(x) ((((long)(x)) << XCT_PTR_ADDR_S) & \
  17027. + XCT_PTR_ADDR_M)
  17028. +
  17029. +/* Function descriptor fields */
  17030. +#define XCT_FUN_T 0x8000000000000000ull
  17031. +#define XCT_FUN_ST 0x4000000000000000ull
  17032. +#define XCT_FUN_NORES 0x0000000000000000ull
  17033. +#define XCT_FUN_8BRES 0x1000000000000000ull
  17034. +#define XCT_FUN_24BRES 0x2000000000000000ull
  17035. +#define XCT_FUN_40BRES 0x3000000000000000ull
  17036. +#define XCT_FUN_I 0x0800000000000000ull
  17037. +#define XCT_FUN_O 0x0400000000000000ull
  17038. +#define XCT_FUN_E 0x0200000000000000ull
  17039. +#define XCT_FUN_FUN_S 54
  17040. +#define XCT_FUN_FUN_M 0x01c0000000000000ull
  17041. +#define XCT_FUN_FUN(num) ((((long)(num)) << XCT_FUN_FUN_S) & \
  17042. + XCT_FUN_FUN_M)
  17043. +#define XCT_FUN_CRM_NOP 0x0000000000000000ull
  17044. +#define XCT_FUN_CRM_SIG 0x0008000000000000ull
  17045. +#define XCT_FUN_CRM_ENC 0x0010000000000000ull
  17046. +#define XCT_FUN_CRM_DEC 0x0018000000000000ull
  17047. +#define XCT_FUN_CRM_SIG_ENC 0x0020000000000000ull
  17048. +#define XCT_FUN_CRM_ENC_SIG 0x0028000000000000ull
  17049. +#define XCT_FUN_CRM_SIG_DEC 0x0030000000000000ull
  17050. +#define XCT_FUN_CRM_DEC_SIG 0x0038000000000000ull
  17051. +#define XCT_FUN_LLEN_M 0x0007ffff00000000ull
  17052. +#define XCT_FUN_LLEN_S 32ULL
  17053. +#define XCT_FUN_LLEN(x) ((((long)(x)) << XCT_FUN_LLEN_S) & \
  17054. + XCT_FUN_LLEN_M)
  17055. +#define XCT_FUN_SHL_M 0x00000000f8000000ull
  17056. +#define XCT_FUN_SHL_S 27ull
  17057. +#define XCT_FUN_SHL(x) ((((long)(x)) << XCT_FUN_SHL_S) & \
  17058. + XCT_FUN_SHL_M)
  17059. +#define XCT_FUN_CHL_M 0x0000000007c00000ull
  17060. +#define XCT_FUN_CHL_S 22ull
  17061. +#define XCT_FUN_CHL(x) ((((long)(x)) << XCT_FUN_CHL_S) & \
  17062. + XCT_FUN_CHL_M)
  17063. +#define XCT_FUN_HSZ_M 0x00000000003c0000ull
  17064. +#define XCT_FUN_HSZ_S 18ull
  17065. +#define XCT_FUN_HSZ(x) ((((long)(x)) << XCT_FUN_HSZ_S) & \
  17066. + XCT_FUN_HSZ_M)
  17067. +#define XCT_FUN_ALG_DES 0x0000000000000000ull
  17068. +#define XCT_FUN_ALG_3DES 0x0000000000008000ull
  17069. +#define XCT_FUN_ALG_AES 0x0000000000010000ull
  17070. +#define XCT_FUN_ALG_ARC 0x0000000000018000ull
  17071. +#define XCT_FUN_ALG_KASUMI 0x0000000000020000ull
  17072. +#define XCT_FUN_BCM_ECB 0x0000000000000000ull
  17073. +#define XCT_FUN_BCM_CBC 0x0000000000001000ull
  17074. +#define XCT_FUN_BCM_CFB 0x0000000000002000ull
  17075. +#define XCT_FUN_BCM_OFB 0x0000000000003000ull
  17076. +#define XCT_FUN_BCM_CNT 0x0000000000003800ull
  17077. +#define XCT_FUN_BCM_KAS_F8 0x0000000000002800ull
  17078. +#define XCT_FUN_BCM_KAS_F9 0x0000000000001800ull
  17079. +#define XCT_FUN_BCP_NO_PAD 0x0000000000000000ull
  17080. +#define XCT_FUN_BCP_ZRO 0x0000000000000200ull
  17081. +#define XCT_FUN_BCP_PL 0x0000000000000400ull
  17082. +#define XCT_FUN_BCP_INCR 0x0000000000000600ull
  17083. +#define XCT_FUN_SIG_MD5 (0ull << 4)
  17084. +#define XCT_FUN_SIG_SHA1 (2ull << 4)
  17085. +#define XCT_FUN_SIG_HMAC_MD5 (8ull << 4)
  17086. +#define XCT_FUN_SIG_HMAC_SHA1 (10ull << 4)
  17087. +#define XCT_FUN_A 0x0000000000000008ull
  17088. +#define XCT_FUN_C 0x0000000000000004ull
  17089. +#define XCT_FUN_AL2 0x0000000000000002ull
  17090. +#define XCT_FUN_SE 0x0000000000000001ull
  17091. +
  17092. +#define XCT_FUN_SRC_PTR(len, addr) (XCT_PTR_LEN(len) | XCT_PTR_ADDR(addr))
  17093. +#define XCT_FUN_DST_PTR(len, addr) (XCT_FUN_SRC_PTR(len, addr) | \
  17094. + 0x8000000000000000ull)
  17095. +
  17096. +#define XCT_CTRL_HDR_FUN_NUM_M 0x01c0000000000000ull
  17097. +#define XCT_CTRL_HDR_FUN_NUM_S 54
  17098. +#define XCT_CTRL_HDR_LEN_M 0x0007ffff00000000ull
  17099. +#define XCT_CTRL_HDR_LEN_S 32
  17100. +#define XCT_CTRL_HDR_REG_M 0x00000000000000ffull
  17101. +#define XCT_CTRL_HDR_REG_S 0
  17102. +
  17103. +#define XCT_CTRL_HDR(funcN,len,reg) (0x9400000000000000ull | \
  17104. + ((((long)(funcN)) << XCT_CTRL_HDR_FUN_NUM_S) \
  17105. + & XCT_CTRL_HDR_FUN_NUM_M) | \
  17106. + ((((long)(len)) << \
  17107. + XCT_CTRL_HDR_LEN_S) & XCT_CTRL_HDR_LEN_M) | \
  17108. + ((((long)(reg)) << \
  17109. + XCT_CTRL_HDR_REG_S) & XCT_CTRL_HDR_REG_M))
  17110. +
  17111. +/* Function config command options */
  17112. +#define DMA_CALGO_DES 0x00
  17113. +#define DMA_CALGO_3DES 0x01
  17114. +#define DMA_CALGO_AES 0x02
  17115. +#define DMA_CALGO_ARC 0x03
  17116. +
  17117. +#define DMA_FN_CIV0 0x02
  17118. +#define DMA_FN_CIV1 0x03
  17119. +#define DMA_FN_HKEY0 0x0a
  17120. +
  17121. +#define XCT_PTR_ADDR_LEN(ptr) ((ptr) & XCT_PTR_ADDR_M), \
  17122. + (((ptr) & XCT_PTR_LEN_M) >> XCT_PTR_LEN_S)
  17123. +
  17124. +#endif /* PASEMI_FNU_H */
  17125. diff -Nur linux-2.6.30.orig/crypto/ocf/random.c linux-2.6.30/crypto/ocf/random.c
  17126. --- linux-2.6.30.orig/crypto/ocf/random.c 1970-01-01 01:00:00.000000000 +0100
  17127. +++ linux-2.6.30/crypto/ocf/random.c 2009-06-11 10:55:27.000000000 +0200
  17128. @@ -0,0 +1,317 @@
  17129. +/*
  17130. + * A system independant way of adding entropy to the kernels pool
  17131. + * this way the drivers can focus on the real work and we can take
  17132. + * care of pushing it to the appropriate place in the kernel.
  17133. + *
  17134. + * This should be fast and callable from timers/interrupts
  17135. + *
  17136. + * Written by David McCullough <david_mccullough@securecomputing.com>
  17137. + * Copyright (C) 2006-2007 David McCullough
  17138. + * Copyright (C) 2004-2005 Intel Corporation.
  17139. + *
  17140. + * LICENSE TERMS
  17141. + *
  17142. + * The free distribution and use of this software in both source and binary
  17143. + * form is allowed (with or without changes) provided that:
  17144. + *
  17145. + * 1. distributions of this source code include the above copyright
  17146. + * notice, this list of conditions and the following disclaimer;
  17147. + *
  17148. + * 2. distributions in binary form include the above copyright
  17149. + * notice, this list of conditions and the following disclaimer
  17150. + * in the documentation and/or other associated materials;
  17151. + *
  17152. + * 3. the copyright holder's name is not used to endorse products
  17153. + * built using this software without specific written permission.
  17154. + *
  17155. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  17156. + * may be distributed under the terms of the GNU General Public License (GPL),
  17157. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  17158. + *
  17159. + * DISCLAIMER
  17160. + *
  17161. + * This software is provided 'as is' with no explicit or implied warranties
  17162. + * in respect of its properties, including, but not limited to, correctness
  17163. + * and/or fitness for purpose.
  17164. + */
  17165. +
  17166. +#ifndef AUTOCONF_INCLUDED
  17167. +#include <linux/config.h>
  17168. +#endif
  17169. +#include <linux/module.h>
  17170. +#include <linux/init.h>
  17171. +#include <linux/list.h>
  17172. +#include <linux/slab.h>
  17173. +#include <linux/wait.h>
  17174. +#include <linux/sched.h>
  17175. +#include <linux/spinlock.h>
  17176. +#include <linux/version.h>
  17177. +#include <linux/unistd.h>
  17178. +#include <linux/poll.h>
  17179. +#include <linux/random.h>
  17180. +#include <cryptodev.h>
  17181. +
  17182. +#ifdef CONFIG_OCF_FIPS
  17183. +#include "rndtest.h"
  17184. +#endif
  17185. +
  17186. +#ifndef HAS_RANDOM_INPUT_WAIT
  17187. +#error "Please do not enable OCF_RANDOMHARVEST unless you have applied patches"
  17188. +#endif
  17189. +
  17190. +/*
  17191. + * a hack to access the debug levels from the crypto driver
  17192. + */
  17193. +extern int crypto_debug;
  17194. +#define debug crypto_debug
  17195. +
  17196. +/*
  17197. + * a list of all registered random providers
  17198. + */
  17199. +static LIST_HEAD(random_ops);
  17200. +static int started = 0;
  17201. +static int initted = 0;
  17202. +
  17203. +struct random_op {
  17204. + struct list_head random_list;
  17205. + u_int32_t driverid;
  17206. + int (*read_random)(void *arg, u_int32_t *buf, int len);
  17207. + void *arg;
  17208. +};
  17209. +
  17210. +static int random_proc(void *arg);
  17211. +
  17212. +static pid_t randomproc = (pid_t) -1;
  17213. +static spinlock_t random_lock;
  17214. +
  17215. +/*
  17216. + * just init the spin locks
  17217. + */
  17218. +static int
  17219. +crypto_random_init(void)
  17220. +{
  17221. + spin_lock_init(&random_lock);
  17222. + initted = 1;
  17223. + return(0);
  17224. +}
  17225. +
  17226. +/*
  17227. + * Add the given random reader to our list (if not present)
  17228. + * and start the thread (if not already started)
  17229. + *
  17230. + * we have to assume that driver id is ok for now
  17231. + */
  17232. +int
  17233. +crypto_rregister(
  17234. + u_int32_t driverid,
  17235. + int (*read_random)(void *arg, u_int32_t *buf, int len),
  17236. + void *arg)
  17237. +{
  17238. + unsigned long flags;
  17239. + int ret = 0;
  17240. + struct random_op *rops, *tmp;
  17241. +
  17242. + dprintk("%s,%d: %s(0x%x, %p, %p)\n", __FILE__, __LINE__,
  17243. + __FUNCTION__, driverid, read_random, arg);
  17244. +
  17245. + if (!initted)
  17246. + crypto_random_init();
  17247. +
  17248. +#if 0
  17249. + struct cryptocap *cap;
  17250. +
  17251. + cap = crypto_checkdriver(driverid);
  17252. + if (!cap)
  17253. + return EINVAL;
  17254. +#endif
  17255. +
  17256. + list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  17257. + if (rops->driverid == driverid && rops->read_random == read_random)
  17258. + return EEXIST;
  17259. + }
  17260. +
  17261. + rops = (struct random_op *) kmalloc(sizeof(*rops), GFP_KERNEL);
  17262. + if (!rops)
  17263. + return ENOMEM;
  17264. +
  17265. + rops->driverid = driverid;
  17266. + rops->read_random = read_random;
  17267. + rops->arg = arg;
  17268. +
  17269. + spin_lock_irqsave(&random_lock, flags);
  17270. + list_add_tail(&rops->random_list, &random_ops);
  17271. + if (!started) {
  17272. + randomproc = kernel_thread(random_proc, NULL, CLONE_FS|CLONE_FILES);
  17273. + if (randomproc < 0) {
  17274. + ret = randomproc;
  17275. + printk("crypto: crypto_rregister cannot start random thread; "
  17276. + "error %d", ret);
  17277. + } else
  17278. + started = 1;
  17279. + }
  17280. + spin_unlock_irqrestore(&random_lock, flags);
  17281. +
  17282. + return ret;
  17283. +}
  17284. +EXPORT_SYMBOL(crypto_rregister);
  17285. +
  17286. +int
  17287. +crypto_runregister_all(u_int32_t driverid)
  17288. +{
  17289. + struct random_op *rops, *tmp;
  17290. + unsigned long flags;
  17291. +
  17292. + dprintk("%s,%d: %s(0x%x)\n", __FILE__, __LINE__, __FUNCTION__, driverid);
  17293. +
  17294. + list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  17295. + if (rops->driverid == driverid) {
  17296. + list_del(&rops->random_list);
  17297. + kfree(rops);
  17298. + }
  17299. + }
  17300. +
  17301. + spin_lock_irqsave(&random_lock, flags);
  17302. + if (list_empty(&random_ops) && started)
  17303. + kill_pid(randomproc, SIGKILL, 1);
  17304. + spin_unlock_irqrestore(&random_lock, flags);
  17305. + return(0);
  17306. +}
  17307. +EXPORT_SYMBOL(crypto_runregister_all);
  17308. +
  17309. +/*
  17310. + * while we can add entropy to random.c continue to read random data from
  17311. + * the drivers and push it to random.
  17312. + */
  17313. +static int
  17314. +random_proc(void *arg)
  17315. +{
  17316. + int n;
  17317. + int wantcnt;
  17318. + int bufcnt = 0;
  17319. + int retval = 0;
  17320. + int *buf = NULL;
  17321. +
  17322. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  17323. + daemonize();
  17324. + spin_lock_irq(&current->sigmask_lock);
  17325. + sigemptyset(&current->blocked);
  17326. + recalc_sigpending(current);
  17327. + spin_unlock_irq(&current->sigmask_lock);
  17328. + sprintf(current->comm, "ocf-random");
  17329. +#else
  17330. + daemonize("ocf-random");
  17331. + allow_signal(SIGKILL);
  17332. +#endif
  17333. +
  17334. + (void) get_fs();
  17335. + set_fs(get_ds());
  17336. +
  17337. +#ifdef CONFIG_OCF_FIPS
  17338. +#define NUM_INT (RNDTEST_NBYTES/sizeof(int))
  17339. +#else
  17340. +#define NUM_INT 32
  17341. +#endif
  17342. +
  17343. + /*
  17344. + * some devices can transferr their RNG data direct into memory,
  17345. + * so make sure it is device friendly
  17346. + */
  17347. + buf = kmalloc(NUM_INT * sizeof(int), GFP_DMA);
  17348. + if (NULL == buf) {
  17349. + printk("crypto: RNG could not allocate memory\n");
  17350. + retval = -ENOMEM;
  17351. + goto bad_alloc;
  17352. + }
  17353. +
  17354. + wantcnt = NUM_INT; /* start by adding some entropy */
  17355. +
  17356. + /*
  17357. + * its possible due to errors or driver removal that we no longer
  17358. + * have anything to do, if so exit or we will consume all the CPU
  17359. + * doing nothing
  17360. + */
  17361. + while (!list_empty(&random_ops)) {
  17362. + struct random_op *rops, *tmp;
  17363. +
  17364. +#ifdef CONFIG_OCF_FIPS
  17365. + if (wantcnt)
  17366. + wantcnt = NUM_INT; /* FIPs mode can do 20000 bits or none */
  17367. +#endif
  17368. +
  17369. + /* see if we can get enough entropy to make the world
  17370. + * a better place.
  17371. + */
  17372. + while (bufcnt < wantcnt && bufcnt < NUM_INT) {
  17373. + list_for_each_entry_safe(rops, tmp, &random_ops, random_list) {
  17374. +
  17375. + n = (*rops->read_random)(rops->arg, &buf[bufcnt],
  17376. + NUM_INT - bufcnt);
  17377. +
  17378. + /* on failure remove the random number generator */
  17379. + if (n == -1) {
  17380. + list_del(&rops->random_list);
  17381. + printk("crypto: RNG (driverid=0x%x) failed, disabling\n",
  17382. + rops->driverid);
  17383. + kfree(rops);
  17384. + } else if (n > 0)
  17385. + bufcnt += n;
  17386. + }
  17387. + /* give up CPU for a bit, just in case as this is a loop */
  17388. + schedule();
  17389. + }
  17390. +
  17391. +
  17392. +#ifdef CONFIG_OCF_FIPS
  17393. + if (bufcnt > 0 && rndtest_buf((unsigned char *) &buf[0])) {
  17394. + dprintk("crypto: buffer had fips errors, discarding\n");
  17395. + bufcnt = 0;
  17396. + }
  17397. +#endif
  17398. +
  17399. + /*
  17400. + * if we have a certified buffer, we can send some data
  17401. + * to /dev/random and move along
  17402. + */
  17403. + if (bufcnt > 0) {
  17404. + /* add what we have */
  17405. + random_input_words(buf, bufcnt, bufcnt*sizeof(int)*8);
  17406. + bufcnt = 0;
  17407. + }
  17408. +
  17409. + /* give up CPU for a bit so we don't hog while filling */
  17410. + schedule();
  17411. +
  17412. + /* wait for needing more */
  17413. + wantcnt = random_input_wait();
  17414. +
  17415. + if (wantcnt <= 0)
  17416. + wantcnt = 0; /* try to get some info again */
  17417. + else
  17418. + /* round up to one word or we can loop forever */
  17419. + wantcnt = (wantcnt + (sizeof(int)*8)) / (sizeof(int)*8);
  17420. + if (wantcnt > NUM_INT) {
  17421. + wantcnt = NUM_INT;
  17422. + }
  17423. +
  17424. + if (signal_pending(current)) {
  17425. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  17426. + spin_lock_irq(&current->sigmask_lock);
  17427. +#endif
  17428. + flush_signals(current);
  17429. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  17430. + spin_unlock_irq(&current->sigmask_lock);
  17431. +#endif
  17432. + }
  17433. + }
  17434. +
  17435. + kfree(buf);
  17436. +
  17437. +bad_alloc:
  17438. + spin_lock_irq(&random_lock);
  17439. + randomproc = (pid_t) -1;
  17440. + started = 0;
  17441. + spin_unlock_irq(&random_lock);
  17442. +
  17443. + return retval;
  17444. +}
  17445. +
  17446. diff -Nur linux-2.6.30.orig/crypto/ocf/README linux-2.6.30/crypto/ocf/README
  17447. --- linux-2.6.30.orig/crypto/ocf/README 1970-01-01 01:00:00.000000000 +0100
  17448. +++ linux-2.6.30/crypto/ocf/README 2009-06-11 10:55:27.000000000 +0200
  17449. @@ -0,0 +1,167 @@
  17450. +README - ocf-linux-20071215
  17451. +---------------------------
  17452. +
  17453. +This README provides instructions for getting ocf-linux compiled and
  17454. +operating in a generic linux environment. For other information you
  17455. +might like to visit the home page for this project:
  17456. +
  17457. + http://ocf-linux.sourceforge.net/
  17458. +
  17459. +Adding OCF to linux
  17460. +-------------------
  17461. +
  17462. + Not much in this file for now, just some notes. I usually build
  17463. + the ocf support as modules but it can be built into the kernel as
  17464. + well. To use it:
  17465. +
  17466. + * mknod /dev/crypto c 10 70
  17467. +
  17468. + * to add OCF to your kernel source, you have two options. Apply
  17469. + the kernel specific patch:
  17470. +
  17471. + cd linux-2.4*; gunzip < ocf-linux-24-XXXXXXXX.patch.gz | patch -p1
  17472. + cd linux-2.6*; gunzip < ocf-linux-26-XXXXXXXX.patch.gz | patch -p1
  17473. +
  17474. + if you do one of the above, then you can proceed to the next step,
  17475. + or you can do the above process by hand with using the patches against
  17476. + linux-2.4.35 and 2.6.23 to include the ocf code under crypto/ocf.
  17477. + Here's how to add it:
  17478. +
  17479. + for 2.4.35 (and later)
  17480. +
  17481. + cd linux-2.4.35/crypto
  17482. + tar xvzf ocf-linux.tar.gz
  17483. + cd ..
  17484. + patch -p1 < crypto/ocf/patches/linux-2.4.35-ocf.patch
  17485. +
  17486. + for 2.6.23 (and later), find the kernel patch specific (or nearest)
  17487. + to your kernel versions and then:
  17488. +
  17489. + cd linux-2.6.NN/crypto
  17490. + tar xvzf ocf-linux.tar.gz
  17491. + cd ..
  17492. + patch -p1 < crypto/ocf/patches/linux-2.6.NN-ocf.patch
  17493. +
  17494. + It should be easy to take this patch and apply it to other more
  17495. + recent versions of the kernels. The same patches should also work
  17496. + relatively easily on kernels as old as 2.6.11 and 2.4.18.
  17497. +
  17498. + * under 2.4 if you are on a non-x86 platform, you may need to:
  17499. +
  17500. + cp linux-2.X.x/include/asm-i386/kmap_types.h linux-2.X.x/include/asm-YYY
  17501. +
  17502. + so that you can build the kernel crypto support needed for the cryptosoft
  17503. + driver.
  17504. +
  17505. + * For simplicity you should enable all the crypto support in your kernel
  17506. + except for the test driver. Likewise for the OCF options. Do not
  17507. + enable OCF crypto drivers for HW that you do not have (for example
  17508. + ixp4xx will not compile on non-Xscale systems).
  17509. +
  17510. + * make sure that cryptodev.h (from ocf-linux.tar.gz) is installed as
  17511. + crypto/cryptodev.h in an include directory that is used for building
  17512. + applications for your platform. For example on a host system that
  17513. + might be:
  17514. +
  17515. + /usr/include/crypto/cryptodev.h
  17516. +
  17517. + * patch your openssl-0.9.8i code with the openssl-0.9.8i.patch.
  17518. + (NOTE: there is no longer a need to patch ssh). The patch is against:
  17519. + openssl-0_9_8e
  17520. +
  17521. + If you need a patch for an older version of openssl, you should look
  17522. + to older OCF releases. This patch is unlikely to work on older
  17523. + openssl versions.
  17524. +
  17525. + openssl-0.9.8i.patch
  17526. + - enables --with-cryptodev for non BSD systems
  17527. + - adds -cpu option to openssl speed for calculating CPU load
  17528. + under linux
  17529. + - fixes null pointer in openssl speed multi thread output.
  17530. + - fixes test keys to work with linux crypto's more stringent
  17531. + key checking.
  17532. + - adds MD5/SHA acceleration (Ronen Shitrit), only enabled
  17533. + with the --with-cryptodev-digests option
  17534. + - fixes bug in engine code caching.
  17535. +
  17536. + * build crypto-tools-XXXXXXXX.tar.gz if you want to try some of the BSD
  17537. + tools for testing OCF (ie., cryptotest).
  17538. +
  17539. +How to load the OCF drivers
  17540. +---------------------------
  17541. +
  17542. + First insert the base modules:
  17543. +
  17544. + insmod ocf
  17545. + insmod cryptodev
  17546. +
  17547. + You can then install the software OCF driver with:
  17548. +
  17549. + insmod cryptosoft
  17550. +
  17551. + and one or more of the OCF HW drivers with:
  17552. +
  17553. + insmod safe
  17554. + insmod hifn7751
  17555. + insmod ixp4xx
  17556. + ...
  17557. +
  17558. + all the drivers take a debug option to enable verbose debug so that
  17559. + you can see what is going on. For debug you load them as:
  17560. +
  17561. + insmod ocf crypto_debug=1
  17562. + insmod cryptodev cryptodev_debug=1
  17563. + insmod cryptosoft swcr_debug=1
  17564. +
  17565. + You may load more than one OCF crypto driver but then there is no guarantee
  17566. + as to which will be used.
  17567. +
  17568. + You can also enable debug at run time on 2.6 systems with the following:
  17569. +
  17570. + echo 1 > /sys/module/ocf/parameters/crypto_debug
  17571. + echo 1 > /sys/module/cryptodev/parameters/cryptodev_debug
  17572. + echo 1 > /sys/module/cryptosoft/parameters/swcr_debug
  17573. + echo 1 > /sys/module/hifn7751/parameters/hifn_debug
  17574. + echo 1 > /sys/module/safe/parameters/safe_debug
  17575. + echo 1 > /sys/module/ixp4xx/parameters/ixp_debug
  17576. + ...
  17577. +
  17578. +Testing the OCF support
  17579. +-----------------------
  17580. +
  17581. + run "cryptotest", it should do a short test for a couple of
  17582. + des packets. If it does everything is working.
  17583. +
  17584. + If this works, then ssh will use the driver when invoked as:
  17585. +
  17586. + ssh -c 3des username@host
  17587. +
  17588. + to see for sure that it is operating, enable debug as defined above.
  17589. +
  17590. + To get a better idea of performance run:
  17591. +
  17592. + cryptotest 100 4096
  17593. +
  17594. + There are more options to cryptotest, see the help.
  17595. +
  17596. + It is also possible to use openssl to test the speed of the crypto
  17597. + drivers.
  17598. +
  17599. + openssl speed -evp des -engine cryptodev -elapsed
  17600. + openssl speed -evp des3 -engine cryptodev -elapsed
  17601. + openssl speed -evp aes128 -engine cryptodev -elapsed
  17602. +
  17603. + and multiple threads (10) with:
  17604. +
  17605. + openssl speed -evp des -engine cryptodev -elapsed -multi 10
  17606. + openssl speed -evp des3 -engine cryptodev -elapsed -multi 10
  17607. + openssl speed -evp aes128 -engine cryptodev -elapsed -multi 10
  17608. +
  17609. + for public key testing you can try:
  17610. +
  17611. + cryptokeytest
  17612. + openssl speed -engine cryptodev rsa -elapsed
  17613. + openssl speed -engine cryptodev dsa -elapsed
  17614. +
  17615. +David McCullough
  17616. +david_mccullough@securecomputing.com
  17617. diff -Nur linux-2.6.30.orig/crypto/ocf/rndtest.c linux-2.6.30/crypto/ocf/rndtest.c
  17618. --- linux-2.6.30.orig/crypto/ocf/rndtest.c 1970-01-01 01:00:00.000000000 +0100
  17619. +++ linux-2.6.30/crypto/ocf/rndtest.c 2009-06-11 10:55:27.000000000 +0200
  17620. @@ -0,0 +1,300 @@
  17621. +/* $OpenBSD$ */
  17622. +
  17623. +/*
  17624. + * OCF/Linux port done by David McCullough <david_mccullough@securecomputing.com>
  17625. + * Copyright (C) 2006-2007 David McCullough
  17626. + * Copyright (C) 2004-2005 Intel Corporation.
  17627. + * The license and original author are listed below.
  17628. + *
  17629. + * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
  17630. + * All rights reserved.
  17631. + *
  17632. + * Redistribution and use in source and binary forms, with or without
  17633. + * modification, are permitted provided that the following conditions
  17634. + * are met:
  17635. + * 1. Redistributions of source code must retain the above copyright
  17636. + * notice, this list of conditions and the following disclaimer.
  17637. + * 2. Redistributions in binary form must reproduce the above copyright
  17638. + * notice, this list of conditions and the following disclaimer in the
  17639. + * documentation and/or other materials provided with the distribution.
  17640. + * 3. All advertising materials mentioning features or use of this software
  17641. + * must display the following acknowledgement:
  17642. + * This product includes software developed by Jason L. Wright
  17643. + * 4. The name of the author may not be used to endorse or promote products
  17644. + * derived from this software without specific prior written permission.
  17645. + *
  17646. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17647. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17648. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17649. + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  17650. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17651. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17652. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  17653. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  17654. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  17655. + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  17656. + * POSSIBILITY OF SUCH DAMAGE.
  17657. + */
  17658. +
  17659. +#ifndef AUTOCONF_INCLUDED
  17660. +#include <linux/config.h>
  17661. +#endif
  17662. +#include <linux/module.h>
  17663. +#include <linux/list.h>
  17664. +#include <linux/wait.h>
  17665. +#include <linux/time.h>
  17666. +#include <linux/version.h>
  17667. +#include <linux/unistd.h>
  17668. +#include <linux/kernel.h>
  17669. +#include <linux/string.h>
  17670. +#include <linux/time.h>
  17671. +#include <cryptodev.h>
  17672. +#include "rndtest.h"
  17673. +
  17674. +static struct rndtest_stats rndstats;
  17675. +
  17676. +static void rndtest_test(struct rndtest_state *);
  17677. +
  17678. +/* The tests themselves */
  17679. +static int rndtest_monobit(struct rndtest_state *);
  17680. +static int rndtest_runs(struct rndtest_state *);
  17681. +static int rndtest_longruns(struct rndtest_state *);
  17682. +static int rndtest_chi_4(struct rndtest_state *);
  17683. +
  17684. +static int rndtest_runs_check(struct rndtest_state *, int, int *);
  17685. +static void rndtest_runs_record(struct rndtest_state *, int, int *);
  17686. +
  17687. +static const struct rndtest_testfunc {
  17688. + int (*test)(struct rndtest_state *);
  17689. +} rndtest_funcs[] = {
  17690. + { rndtest_monobit },
  17691. + { rndtest_runs },
  17692. + { rndtest_chi_4 },
  17693. + { rndtest_longruns },
  17694. +};
  17695. +
  17696. +#define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
  17697. +
  17698. +static void
  17699. +rndtest_test(struct rndtest_state *rsp)
  17700. +{
  17701. + int i, rv = 0;
  17702. +
  17703. + rndstats.rst_tests++;
  17704. + for (i = 0; i < RNDTEST_NTESTS; i++)
  17705. + rv |= (*rndtest_funcs[i].test)(rsp);
  17706. + rsp->rs_discard = (rv != 0);
  17707. +}
  17708. +
  17709. +
  17710. +extern int crypto_debug;
  17711. +#define rndtest_verbose 2
  17712. +#define rndtest_report(rsp, failure, fmt, a...) \
  17713. + { if (failure || crypto_debug) { printk("rng_test: " fmt "\n", a); } else; }
  17714. +
  17715. +#define RNDTEST_MONOBIT_MINONES 9725
  17716. +#define RNDTEST_MONOBIT_MAXONES 10275
  17717. +
  17718. +static int
  17719. +rndtest_monobit(struct rndtest_state *rsp)
  17720. +{
  17721. + int i, ones = 0, j;
  17722. + u_int8_t r;
  17723. +
  17724. + for (i = 0; i < RNDTEST_NBYTES; i++) {
  17725. + r = rsp->rs_buf[i];
  17726. + for (j = 0; j < 8; j++, r <<= 1)
  17727. + if (r & 0x80)
  17728. + ones++;
  17729. + }
  17730. + if (ones > RNDTEST_MONOBIT_MINONES &&
  17731. + ones < RNDTEST_MONOBIT_MAXONES) {
  17732. + if (rndtest_verbose > 1)
  17733. + rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
  17734. + RNDTEST_MONOBIT_MINONES, ones,
  17735. + RNDTEST_MONOBIT_MAXONES);
  17736. + return (0);
  17737. + } else {
  17738. + if (rndtest_verbose)
  17739. + rndtest_report(rsp, 1,
  17740. + "monobit failed (%d ones)", ones);
  17741. + rndstats.rst_monobit++;
  17742. + return (-1);
  17743. + }
  17744. +}
  17745. +
  17746. +#define RNDTEST_RUNS_NINTERVAL 6
  17747. +
  17748. +static const struct rndtest_runs_tabs {
  17749. + u_int16_t min, max;
  17750. +} rndtest_runs_tab[] = {
  17751. + { 2343, 2657 },
  17752. + { 1135, 1365 },
  17753. + { 542, 708 },
  17754. + { 251, 373 },
  17755. + { 111, 201 },
  17756. + { 111, 201 },
  17757. +};
  17758. +
  17759. +static int
  17760. +rndtest_runs(struct rndtest_state *rsp)
  17761. +{
  17762. + int i, j, ones, zeros, rv = 0;
  17763. + int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
  17764. + u_int8_t c;
  17765. +
  17766. + bzero(onei, sizeof(onei));
  17767. + bzero(zeroi, sizeof(zeroi));
  17768. + ones = zeros = 0;
  17769. + for (i = 0; i < RNDTEST_NBYTES; i++) {
  17770. + c = rsp->rs_buf[i];
  17771. + for (j = 0; j < 8; j++, c <<= 1) {
  17772. + if (c & 0x80) {
  17773. + ones++;
  17774. + rndtest_runs_record(rsp, zeros, zeroi);
  17775. + zeros = 0;
  17776. + } else {
  17777. + zeros++;
  17778. + rndtest_runs_record(rsp, ones, onei);
  17779. + ones = 0;
  17780. + }
  17781. + }
  17782. + }
  17783. + rndtest_runs_record(rsp, ones, onei);
  17784. + rndtest_runs_record(rsp, zeros, zeroi);
  17785. +
  17786. + rv |= rndtest_runs_check(rsp, 0, zeroi);
  17787. + rv |= rndtest_runs_check(rsp, 1, onei);
  17788. +
  17789. + if (rv)
  17790. + rndstats.rst_runs++;
  17791. +
  17792. + return (rv);
  17793. +}
  17794. +
  17795. +static void
  17796. +rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
  17797. +{
  17798. + if (len == 0)
  17799. + return;
  17800. + if (len > RNDTEST_RUNS_NINTERVAL)
  17801. + len = RNDTEST_RUNS_NINTERVAL;
  17802. + len -= 1;
  17803. + intrv[len]++;
  17804. +}
  17805. +
  17806. +static int
  17807. +rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
  17808. +{
  17809. + int i, rv = 0;
  17810. +
  17811. + for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
  17812. + if (src[i] < rndtest_runs_tab[i].min ||
  17813. + src[i] > rndtest_runs_tab[i].max) {
  17814. + rndtest_report(rsp, 1,
  17815. + "%s interval %d failed (%d, %d-%d)",
  17816. + val ? "ones" : "zeros",
  17817. + i + 1, src[i], rndtest_runs_tab[i].min,
  17818. + rndtest_runs_tab[i].max);
  17819. + rv = -1;
  17820. + } else {
  17821. + rndtest_report(rsp, 0,
  17822. + "runs pass %s interval %d (%d < %d < %d)",
  17823. + val ? "ones" : "zeros",
  17824. + i + 1, rndtest_runs_tab[i].min, src[i],
  17825. + rndtest_runs_tab[i].max);
  17826. + }
  17827. + }
  17828. + return (rv);
  17829. +}
  17830. +
  17831. +static int
  17832. +rndtest_longruns(struct rndtest_state *rsp)
  17833. +{
  17834. + int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
  17835. + u_int8_t c;
  17836. +
  17837. + for (i = 0; i < RNDTEST_NBYTES; i++) {
  17838. + c = rsp->rs_buf[i];
  17839. + for (j = 0; j < 8; j++, c <<= 1) {
  17840. + if (c & 0x80) {
  17841. + zeros = 0;
  17842. + ones++;
  17843. + if (ones > maxones)
  17844. + maxones = ones;
  17845. + } else {
  17846. + ones = 0;
  17847. + zeros++;
  17848. + if (zeros > maxzeros)
  17849. + maxzeros = zeros;
  17850. + }
  17851. + }
  17852. + }
  17853. +
  17854. + if (maxones < 26 && maxzeros < 26) {
  17855. + rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
  17856. + maxones, maxzeros);
  17857. + return (0);
  17858. + } else {
  17859. + rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
  17860. + maxones, maxzeros);
  17861. + rndstats.rst_longruns++;
  17862. + return (-1);
  17863. + }
  17864. +}
  17865. +
  17866. +/*
  17867. + * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
  17868. + * but it is really the chi^2 test over 4 bits (the poker test as described
  17869. + * by Knuth vol 2 is something different, and I take him as authoritative
  17870. + * on nomenclature over NIST).
  17871. + */
  17872. +#define RNDTEST_CHI4_K 16
  17873. +#define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
  17874. +
  17875. +/*
  17876. + * The unnormalized values are used so that we don't have to worry about
  17877. + * fractional precision. The "real" value is found by:
  17878. + * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
  17879. + */
  17880. +#define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
  17881. +#define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
  17882. +
  17883. +static int
  17884. +rndtest_chi_4(struct rndtest_state *rsp)
  17885. +{
  17886. + unsigned int freq[RNDTEST_CHI4_K], i, sum;
  17887. +
  17888. + for (i = 0; i < RNDTEST_CHI4_K; i++)
  17889. + freq[i] = 0;
  17890. +
  17891. + /* Get number of occurances of each 4 bit pattern */
  17892. + for (i = 0; i < RNDTEST_NBYTES; i++) {
  17893. + freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
  17894. + freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
  17895. + }
  17896. +
  17897. + for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
  17898. + sum += freq[i] * freq[i];
  17899. +
  17900. + if (sum >= 1563181 && sum <= 1576929) {
  17901. + rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
  17902. + return (0);
  17903. + } else {
  17904. + rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
  17905. + rndstats.rst_chi++;
  17906. + return (-1);
  17907. + }
  17908. +}
  17909. +
  17910. +int
  17911. +rndtest_buf(unsigned char *buf)
  17912. +{
  17913. + struct rndtest_state rsp;
  17914. +
  17915. + memset(&rsp, 0, sizeof(rsp));
  17916. + rsp.rs_buf = buf;
  17917. + rndtest_test(&rsp);
  17918. + return(rsp.rs_discard);
  17919. +}
  17920. +
  17921. diff -Nur linux-2.6.30.orig/crypto/ocf/rndtest.h linux-2.6.30/crypto/ocf/rndtest.h
  17922. --- linux-2.6.30.orig/crypto/ocf/rndtest.h 1970-01-01 01:00:00.000000000 +0100
  17923. +++ linux-2.6.30/crypto/ocf/rndtest.h 2009-06-11 10:55:27.000000000 +0200
  17924. @@ -0,0 +1,54 @@
  17925. +/* $FreeBSD: src/sys/dev/rndtest/rndtest.h,v 1.1 2003/03/11 22:54:44 sam Exp $ */
  17926. +/* $OpenBSD$ */
  17927. +
  17928. +/*
  17929. + * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
  17930. + * All rights reserved.
  17931. + *
  17932. + * Redistribution and use in source and binary forms, with or without
  17933. + * modification, are permitted provided that the following conditions
  17934. + * are met:
  17935. + * 1. Redistributions of source code must retain the above copyright
  17936. + * notice, this list of conditions and the following disclaimer.
  17937. + * 2. Redistributions in binary form must reproduce the above copyright
  17938. + * notice, this list of conditions and the following disclaimer in the
  17939. + * documentation and/or other materials provided with the distribution.
  17940. + * 3. All advertising materials mentioning features or use of this software
  17941. + * must display the following acknowledgement:
  17942. + * This product includes software developed by Jason L. Wright
  17943. + * 4. The name of the author may not be used to endorse or promote products
  17944. + * derived from this software without specific prior written permission.
  17945. + *
  17946. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17947. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17948. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17949. + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  17950. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17951. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17952. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  17953. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  17954. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  17955. + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  17956. + * POSSIBILITY OF SUCH DAMAGE.
  17957. + */
  17958. +
  17959. +
  17960. +/* Some of the tests depend on these values */
  17961. +#define RNDTEST_NBYTES 2500
  17962. +#define RNDTEST_NBITS (8 * RNDTEST_NBYTES)
  17963. +
  17964. +struct rndtest_state {
  17965. + int rs_discard; /* discard/accept random data */
  17966. + u_int8_t *rs_buf;
  17967. +};
  17968. +
  17969. +struct rndtest_stats {
  17970. + u_int32_t rst_discard; /* number of bytes discarded */
  17971. + u_int32_t rst_tests; /* number of test runs */
  17972. + u_int32_t rst_monobit; /* monobit test failures */
  17973. + u_int32_t rst_runs; /* 0/1 runs failures */
  17974. + u_int32_t rst_longruns; /* longruns failures */
  17975. + u_int32_t rst_chi; /* chi^2 failures */
  17976. +};
  17977. +
  17978. +extern int rndtest_buf(unsigned char *buf);
  17979. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/Makefile linux-2.6.30/crypto/ocf/safe/Makefile
  17980. --- linux-2.6.30.orig/crypto/ocf/safe/Makefile 1970-01-01 01:00:00.000000000 +0100
  17981. +++ linux-2.6.30/crypto/ocf/safe/Makefile 2009-06-11 10:55:27.000000000 +0200
  17982. @@ -0,0 +1,12 @@
  17983. +# for SGlinux builds
  17984. +-include $(ROOTDIR)/modules/.config
  17985. +
  17986. +obj-$(CONFIG_OCF_SAFE) += safe.o
  17987. +
  17988. +obj ?= .
  17989. +EXTRA_CFLAGS += -I$(obj)/.. -I$(obj)/
  17990. +
  17991. +ifdef TOPDIR
  17992. +-include $(TOPDIR)/Rules.make
  17993. +endif
  17994. +
  17995. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/md5.c linux-2.6.30/crypto/ocf/safe/md5.c
  17996. --- linux-2.6.30.orig/crypto/ocf/safe/md5.c 1970-01-01 01:00:00.000000000 +0100
  17997. +++ linux-2.6.30/crypto/ocf/safe/md5.c 2009-06-11 10:55:27.000000000 +0200
  17998. @@ -0,0 +1,308 @@
  17999. +/* $KAME: md5.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
  18000. +/*
  18001. + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  18002. + * All rights reserved.
  18003. + *
  18004. + * Redistribution and use in source and binary forms, with or without
  18005. + * modification, are permitted provided that the following conditions
  18006. + * are met:
  18007. + * 1. Redistributions of source code must retain the above copyright
  18008. + * notice, this list of conditions and the following disclaimer.
  18009. + * 2. Redistributions in binary form must reproduce the above copyright
  18010. + * notice, this list of conditions and the following disclaimer in the
  18011. + * documentation and/or other materials provided with the distribution.
  18012. + * 3. Neither the name of the project nor the names of its contributors
  18013. + * may be used to endorse or promote products derived from this software
  18014. + * without specific prior written permission.
  18015. + *
  18016. + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18017. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18018. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18019. + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  18020. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18021. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18022. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18023. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18024. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18025. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18026. + * SUCH DAMAGE.
  18027. + */
  18028. +
  18029. +#if 0
  18030. +#include <sys/cdefs.h>
  18031. +__FBSDID("$FreeBSD: src/sys/crypto/md5.c,v 1.9 2004/01/27 19:49:19 des Exp $");
  18032. +
  18033. +#include <sys/types.h>
  18034. +#include <sys/cdefs.h>
  18035. +#include <sys/time.h>
  18036. +#include <sys/systm.h>
  18037. +#include <crypto/md5.h>
  18038. +#endif
  18039. +
  18040. +#define SHIFT(X, s) (((X) << (s)) | ((X) >> (32 - (s))))
  18041. +
  18042. +#define F(X, Y, Z) (((X) & (Y)) | ((~X) & (Z)))
  18043. +#define G(X, Y, Z) (((X) & (Z)) | ((Y) & (~Z)))
  18044. +#define H(X, Y, Z) ((X) ^ (Y) ^ (Z))
  18045. +#define I(X, Y, Z) ((Y) ^ ((X) | (~Z)))
  18046. +
  18047. +#define ROUND1(a, b, c, d, k, s, i) { \
  18048. + (a) = (a) + F((b), (c), (d)) + X[(k)] + T[(i)]; \
  18049. + (a) = SHIFT((a), (s)); \
  18050. + (a) = (b) + (a); \
  18051. +}
  18052. +
  18053. +#define ROUND2(a, b, c, d, k, s, i) { \
  18054. + (a) = (a) + G((b), (c), (d)) + X[(k)] + T[(i)]; \
  18055. + (a) = SHIFT((a), (s)); \
  18056. + (a) = (b) + (a); \
  18057. +}
  18058. +
  18059. +#define ROUND3(a, b, c, d, k, s, i) { \
  18060. + (a) = (a) + H((b), (c), (d)) + X[(k)] + T[(i)]; \
  18061. + (a) = SHIFT((a), (s)); \
  18062. + (a) = (b) + (a); \
  18063. +}
  18064. +
  18065. +#define ROUND4(a, b, c, d, k, s, i) { \
  18066. + (a) = (a) + I((b), (c), (d)) + X[(k)] + T[(i)]; \
  18067. + (a) = SHIFT((a), (s)); \
  18068. + (a) = (b) + (a); \
  18069. +}
  18070. +
  18071. +#define Sa 7
  18072. +#define Sb 12
  18073. +#define Sc 17
  18074. +#define Sd 22
  18075. +
  18076. +#define Se 5
  18077. +#define Sf 9
  18078. +#define Sg 14
  18079. +#define Sh 20
  18080. +
  18081. +#define Si 4
  18082. +#define Sj 11
  18083. +#define Sk 16
  18084. +#define Sl 23
  18085. +
  18086. +#define Sm 6
  18087. +#define Sn 10
  18088. +#define So 15
  18089. +#define Sp 21
  18090. +
  18091. +#define MD5_A0 0x67452301
  18092. +#define MD5_B0 0xefcdab89
  18093. +#define MD5_C0 0x98badcfe
  18094. +#define MD5_D0 0x10325476
  18095. +
  18096. +/* Integer part of 4294967296 times abs(sin(i)), where i is in radians. */
  18097. +static const u_int32_t T[65] = {
  18098. + 0,
  18099. + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  18100. + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  18101. + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  18102. + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  18103. +
  18104. + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  18105. + 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
  18106. + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  18107. + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  18108. +
  18109. + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  18110. + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  18111. + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  18112. + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  18113. +
  18114. + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  18115. + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  18116. + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  18117. + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
  18118. +};
  18119. +
  18120. +static const u_int8_t md5_paddat[MD5_BUFLEN] = {
  18121. + 0x80, 0, 0, 0, 0, 0, 0, 0,
  18122. + 0, 0, 0, 0, 0, 0, 0, 0,
  18123. + 0, 0, 0, 0, 0, 0, 0, 0,
  18124. + 0, 0, 0, 0, 0, 0, 0, 0,
  18125. + 0, 0, 0, 0, 0, 0, 0, 0,
  18126. + 0, 0, 0, 0, 0, 0, 0, 0,
  18127. + 0, 0, 0, 0, 0, 0, 0, 0,
  18128. + 0, 0, 0, 0, 0, 0, 0, 0,
  18129. +};
  18130. +
  18131. +static void md5_calc(u_int8_t *, md5_ctxt *);
  18132. +
  18133. +void md5_init(ctxt)
  18134. + md5_ctxt *ctxt;
  18135. +{
  18136. + ctxt->md5_n = 0;
  18137. + ctxt->md5_i = 0;
  18138. + ctxt->md5_sta = MD5_A0;
  18139. + ctxt->md5_stb = MD5_B0;
  18140. + ctxt->md5_stc = MD5_C0;
  18141. + ctxt->md5_std = MD5_D0;
  18142. + bzero(ctxt->md5_buf, sizeof(ctxt->md5_buf));
  18143. +}
  18144. +
  18145. +void md5_loop(ctxt, input, len)
  18146. + md5_ctxt *ctxt;
  18147. + u_int8_t *input;
  18148. + u_int len; /* number of bytes */
  18149. +{
  18150. + u_int gap, i;
  18151. +
  18152. + ctxt->md5_n += len * 8; /* byte to bit */
  18153. + gap = MD5_BUFLEN - ctxt->md5_i;
  18154. +
  18155. + if (len >= gap) {
  18156. + bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i),
  18157. + gap);
  18158. + md5_calc(ctxt->md5_buf, ctxt);
  18159. +
  18160. + for (i = gap; i + MD5_BUFLEN <= len; i += MD5_BUFLEN) {
  18161. + md5_calc((u_int8_t *)(input + i), ctxt);
  18162. + }
  18163. +
  18164. + ctxt->md5_i = len - i;
  18165. + bcopy((void *)(input + i), (void *)ctxt->md5_buf, ctxt->md5_i);
  18166. + } else {
  18167. + bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i),
  18168. + len);
  18169. + ctxt->md5_i += len;
  18170. + }
  18171. +}
  18172. +
  18173. +void md5_pad(ctxt)
  18174. + md5_ctxt *ctxt;
  18175. +{
  18176. + u_int gap;
  18177. +
  18178. + /* Don't count up padding. Keep md5_n. */
  18179. + gap = MD5_BUFLEN - ctxt->md5_i;
  18180. + if (gap > 8) {
  18181. + bcopy(md5_paddat,
  18182. + (void *)(ctxt->md5_buf + ctxt->md5_i),
  18183. + gap - sizeof(ctxt->md5_n));
  18184. + } else {
  18185. + /* including gap == 8 */
  18186. + bcopy(md5_paddat, (void *)(ctxt->md5_buf + ctxt->md5_i),
  18187. + gap);
  18188. + md5_calc(ctxt->md5_buf, ctxt);
  18189. + bcopy((md5_paddat + gap),
  18190. + (void *)ctxt->md5_buf,
  18191. + MD5_BUFLEN - sizeof(ctxt->md5_n));
  18192. + }
  18193. +
  18194. + /* 8 byte word */
  18195. +#if BYTE_ORDER == LITTLE_ENDIAN
  18196. + bcopy(&ctxt->md5_n8[0], &ctxt->md5_buf[56], 8);
  18197. +#endif
  18198. +#if BYTE_ORDER == BIG_ENDIAN
  18199. + ctxt->md5_buf[56] = ctxt->md5_n8[7];
  18200. + ctxt->md5_buf[57] = ctxt->md5_n8[6];
  18201. + ctxt->md5_buf[58] = ctxt->md5_n8[5];
  18202. + ctxt->md5_buf[59] = ctxt->md5_n8[4];
  18203. + ctxt->md5_buf[60] = ctxt->md5_n8[3];
  18204. + ctxt->md5_buf[61] = ctxt->md5_n8[2];
  18205. + ctxt->md5_buf[62] = ctxt->md5_n8[1];
  18206. + ctxt->md5_buf[63] = ctxt->md5_n8[0];
  18207. +#endif
  18208. +
  18209. + md5_calc(ctxt->md5_buf, ctxt);
  18210. +}
  18211. +
  18212. +void md5_result(digest, ctxt)
  18213. + u_int8_t *digest;
  18214. + md5_ctxt *ctxt;
  18215. +{
  18216. + /* 4 byte words */
  18217. +#if BYTE_ORDER == LITTLE_ENDIAN
  18218. + bcopy(&ctxt->md5_st8[0], digest, 16);
  18219. +#endif
  18220. +#if BYTE_ORDER == BIG_ENDIAN
  18221. + digest[ 0] = ctxt->md5_st8[ 3]; digest[ 1] = ctxt->md5_st8[ 2];
  18222. + digest[ 2] = ctxt->md5_st8[ 1]; digest[ 3] = ctxt->md5_st8[ 0];
  18223. + digest[ 4] = ctxt->md5_st8[ 7]; digest[ 5] = ctxt->md5_st8[ 6];
  18224. + digest[ 6] = ctxt->md5_st8[ 5]; digest[ 7] = ctxt->md5_st8[ 4];
  18225. + digest[ 8] = ctxt->md5_st8[11]; digest[ 9] = ctxt->md5_st8[10];
  18226. + digest[10] = ctxt->md5_st8[ 9]; digest[11] = ctxt->md5_st8[ 8];
  18227. + digest[12] = ctxt->md5_st8[15]; digest[13] = ctxt->md5_st8[14];
  18228. + digest[14] = ctxt->md5_st8[13]; digest[15] = ctxt->md5_st8[12];
  18229. +#endif
  18230. +}
  18231. +
  18232. +static void md5_calc(b64, ctxt)
  18233. + u_int8_t *b64;
  18234. + md5_ctxt *ctxt;
  18235. +{
  18236. + u_int32_t A = ctxt->md5_sta;
  18237. + u_int32_t B = ctxt->md5_stb;
  18238. + u_int32_t C = ctxt->md5_stc;
  18239. + u_int32_t D = ctxt->md5_std;
  18240. +#if BYTE_ORDER == LITTLE_ENDIAN
  18241. + u_int32_t *X = (u_int32_t *)b64;
  18242. +#endif
  18243. +#if BYTE_ORDER == BIG_ENDIAN
  18244. + /* 4 byte words */
  18245. + /* what a brute force but fast! */
  18246. + u_int32_t X[16];
  18247. + u_int8_t *y = (u_int8_t *)X;
  18248. + y[ 0] = b64[ 3]; y[ 1] = b64[ 2]; y[ 2] = b64[ 1]; y[ 3] = b64[ 0];
  18249. + y[ 4] = b64[ 7]; y[ 5] = b64[ 6]; y[ 6] = b64[ 5]; y[ 7] = b64[ 4];
  18250. + y[ 8] = b64[11]; y[ 9] = b64[10]; y[10] = b64[ 9]; y[11] = b64[ 8];
  18251. + y[12] = b64[15]; y[13] = b64[14]; y[14] = b64[13]; y[15] = b64[12];
  18252. + y[16] = b64[19]; y[17] = b64[18]; y[18] = b64[17]; y[19] = b64[16];
  18253. + y[20] = b64[23]; y[21] = b64[22]; y[22] = b64[21]; y[23] = b64[20];
  18254. + y[24] = b64[27]; y[25] = b64[26]; y[26] = b64[25]; y[27] = b64[24];
  18255. + y[28] = b64[31]; y[29] = b64[30]; y[30] = b64[29]; y[31] = b64[28];
  18256. + y[32] = b64[35]; y[33] = b64[34]; y[34] = b64[33]; y[35] = b64[32];
  18257. + y[36] = b64[39]; y[37] = b64[38]; y[38] = b64[37]; y[39] = b64[36];
  18258. + y[40] = b64[43]; y[41] = b64[42]; y[42] = b64[41]; y[43] = b64[40];
  18259. + y[44] = b64[47]; y[45] = b64[46]; y[46] = b64[45]; y[47] = b64[44];
  18260. + y[48] = b64[51]; y[49] = b64[50]; y[50] = b64[49]; y[51] = b64[48];
  18261. + y[52] = b64[55]; y[53] = b64[54]; y[54] = b64[53]; y[55] = b64[52];
  18262. + y[56] = b64[59]; y[57] = b64[58]; y[58] = b64[57]; y[59] = b64[56];
  18263. + y[60] = b64[63]; y[61] = b64[62]; y[62] = b64[61]; y[63] = b64[60];
  18264. +#endif
  18265. +
  18266. + ROUND1(A, B, C, D, 0, Sa, 1); ROUND1(D, A, B, C, 1, Sb, 2);
  18267. + ROUND1(C, D, A, B, 2, Sc, 3); ROUND1(B, C, D, A, 3, Sd, 4);
  18268. + ROUND1(A, B, C, D, 4, Sa, 5); ROUND1(D, A, B, C, 5, Sb, 6);
  18269. + ROUND1(C, D, A, B, 6, Sc, 7); ROUND1(B, C, D, A, 7, Sd, 8);
  18270. + ROUND1(A, B, C, D, 8, Sa, 9); ROUND1(D, A, B, C, 9, Sb, 10);
  18271. + ROUND1(C, D, A, B, 10, Sc, 11); ROUND1(B, C, D, A, 11, Sd, 12);
  18272. + ROUND1(A, B, C, D, 12, Sa, 13); ROUND1(D, A, B, C, 13, Sb, 14);
  18273. + ROUND1(C, D, A, B, 14, Sc, 15); ROUND1(B, C, D, A, 15, Sd, 16);
  18274. +
  18275. + ROUND2(A, B, C, D, 1, Se, 17); ROUND2(D, A, B, C, 6, Sf, 18);
  18276. + ROUND2(C, D, A, B, 11, Sg, 19); ROUND2(B, C, D, A, 0, Sh, 20);
  18277. + ROUND2(A, B, C, D, 5, Se, 21); ROUND2(D, A, B, C, 10, Sf, 22);
  18278. + ROUND2(C, D, A, B, 15, Sg, 23); ROUND2(B, C, D, A, 4, Sh, 24);
  18279. + ROUND2(A, B, C, D, 9, Se, 25); ROUND2(D, A, B, C, 14, Sf, 26);
  18280. + ROUND2(C, D, A, B, 3, Sg, 27); ROUND2(B, C, D, A, 8, Sh, 28);
  18281. + ROUND2(A, B, C, D, 13, Se, 29); ROUND2(D, A, B, C, 2, Sf, 30);
  18282. + ROUND2(C, D, A, B, 7, Sg, 31); ROUND2(B, C, D, A, 12, Sh, 32);
  18283. +
  18284. + ROUND3(A, B, C, D, 5, Si, 33); ROUND3(D, A, B, C, 8, Sj, 34);
  18285. + ROUND3(C, D, A, B, 11, Sk, 35); ROUND3(B, C, D, A, 14, Sl, 36);
  18286. + ROUND3(A, B, C, D, 1, Si, 37); ROUND3(D, A, B, C, 4, Sj, 38);
  18287. + ROUND3(C, D, A, B, 7, Sk, 39); ROUND3(B, C, D, A, 10, Sl, 40);
  18288. + ROUND3(A, B, C, D, 13, Si, 41); ROUND3(D, A, B, C, 0, Sj, 42);
  18289. + ROUND3(C, D, A, B, 3, Sk, 43); ROUND3(B, C, D, A, 6, Sl, 44);
  18290. + ROUND3(A, B, C, D, 9, Si, 45); ROUND3(D, A, B, C, 12, Sj, 46);
  18291. + ROUND3(C, D, A, B, 15, Sk, 47); ROUND3(B, C, D, A, 2, Sl, 48);
  18292. +
  18293. + ROUND4(A, B, C, D, 0, Sm, 49); ROUND4(D, A, B, C, 7, Sn, 50);
  18294. + ROUND4(C, D, A, B, 14, So, 51); ROUND4(B, C, D, A, 5, Sp, 52);
  18295. + ROUND4(A, B, C, D, 12, Sm, 53); ROUND4(D, A, B, C, 3, Sn, 54);
  18296. + ROUND4(C, D, A, B, 10, So, 55); ROUND4(B, C, D, A, 1, Sp, 56);
  18297. + ROUND4(A, B, C, D, 8, Sm, 57); ROUND4(D, A, B, C, 15, Sn, 58);
  18298. + ROUND4(C, D, A, B, 6, So, 59); ROUND4(B, C, D, A, 13, Sp, 60);
  18299. + ROUND4(A, B, C, D, 4, Sm, 61); ROUND4(D, A, B, C, 11, Sn, 62);
  18300. + ROUND4(C, D, A, B, 2, So, 63); ROUND4(B, C, D, A, 9, Sp, 64);
  18301. +
  18302. + ctxt->md5_sta += A;
  18303. + ctxt->md5_stb += B;
  18304. + ctxt->md5_stc += C;
  18305. + ctxt->md5_std += D;
  18306. +}
  18307. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/md5.h linux-2.6.30/crypto/ocf/safe/md5.h
  18308. --- linux-2.6.30.orig/crypto/ocf/safe/md5.h 1970-01-01 01:00:00.000000000 +0100
  18309. +++ linux-2.6.30/crypto/ocf/safe/md5.h 2009-06-11 10:55:27.000000000 +0200
  18310. @@ -0,0 +1,76 @@
  18311. +/* $FreeBSD: src/sys/crypto/md5.h,v 1.4 2002/03/20 05:13:50 alfred Exp $ */
  18312. +/* $KAME: md5.h,v 1.4 2000/03/27 04:36:22 sumikawa Exp $ */
  18313. +
  18314. +/*
  18315. + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  18316. + * All rights reserved.
  18317. + *
  18318. + * Redistribution and use in source and binary forms, with or without
  18319. + * modification, are permitted provided that the following conditions
  18320. + * are met:
  18321. + * 1. Redistributions of source code must retain the above copyright
  18322. + * notice, this list of conditions and the following disclaimer.
  18323. + * 2. Redistributions in binary form must reproduce the above copyright
  18324. + * notice, this list of conditions and the following disclaimer in the
  18325. + * documentation and/or other materials provided with the distribution.
  18326. + * 3. Neither the name of the project nor the names of its contributors
  18327. + * may be used to endorse or promote products derived from this software
  18328. + * without specific prior written permission.
  18329. + *
  18330. + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18331. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18332. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18333. + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  18334. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18335. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18336. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18337. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18338. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18339. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18340. + * SUCH DAMAGE.
  18341. + */
  18342. +
  18343. +#ifndef _NETINET6_MD5_H_
  18344. +#define _NETINET6_MD5_H_
  18345. +
  18346. +#define MD5_BUFLEN 64
  18347. +
  18348. +typedef struct {
  18349. + union {
  18350. + u_int32_t md5_state32[4];
  18351. + u_int8_t md5_state8[16];
  18352. + } md5_st;
  18353. +
  18354. +#define md5_sta md5_st.md5_state32[0]
  18355. +#define md5_stb md5_st.md5_state32[1]
  18356. +#define md5_stc md5_st.md5_state32[2]
  18357. +#define md5_std md5_st.md5_state32[3]
  18358. +#define md5_st8 md5_st.md5_state8
  18359. +
  18360. + union {
  18361. + u_int64_t md5_count64;
  18362. + u_int8_t md5_count8[8];
  18363. + } md5_count;
  18364. +#define md5_n md5_count.md5_count64
  18365. +#define md5_n8 md5_count.md5_count8
  18366. +
  18367. + u_int md5_i;
  18368. + u_int8_t md5_buf[MD5_BUFLEN];
  18369. +} md5_ctxt;
  18370. +
  18371. +extern void md5_init(md5_ctxt *);
  18372. +extern void md5_loop(md5_ctxt *, u_int8_t *, u_int);
  18373. +extern void md5_pad(md5_ctxt *);
  18374. +extern void md5_result(u_int8_t *, md5_ctxt *);
  18375. +
  18376. +/* compatibility */
  18377. +#define MD5_CTX md5_ctxt
  18378. +#define MD5Init(x) md5_init((x))
  18379. +#define MD5Update(x, y, z) md5_loop((x), (y), (z))
  18380. +#define MD5Final(x, y) \
  18381. +do { \
  18382. + md5_pad((y)); \
  18383. + md5_result((x), (y)); \
  18384. +} while (0)
  18385. +
  18386. +#endif /* ! _NETINET6_MD5_H_*/
  18387. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/safe.c linux-2.6.30/crypto/ocf/safe/safe.c
  18388. --- linux-2.6.30.orig/crypto/ocf/safe/safe.c 1970-01-01 01:00:00.000000000 +0100
  18389. +++ linux-2.6.30/crypto/ocf/safe/safe.c 2009-06-11 10:55:27.000000000 +0200
  18390. @@ -0,0 +1,2288 @@
  18391. +/*-
  18392. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  18393. + * Copyright (C) 2004-2007 David McCullough
  18394. + * The license and original author are listed below.
  18395. + *
  18396. + * Copyright (c) 2003 Sam Leffler, Errno Consulting
  18397. + * Copyright (c) 2003 Global Technology Associates, Inc.
  18398. + * All rights reserved.
  18399. + *
  18400. + * Redistribution and use in source and binary forms, with or without
  18401. + * modification, are permitted provided that the following conditions
  18402. + * are met:
  18403. + * 1. Redistributions of source code must retain the above copyright
  18404. + * notice, this list of conditions and the following disclaimer.
  18405. + * 2. Redistributions in binary form must reproduce the above copyright
  18406. + * notice, this list of conditions and the following disclaimer in the
  18407. + * documentation and/or other materials provided with the distribution.
  18408. + *
  18409. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18410. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18411. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18412. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18413. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18414. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18415. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18416. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18417. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18418. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18419. + * SUCH DAMAGE.
  18420. + *
  18421. +__FBSDID("$FreeBSD: src/sys/dev/safe/safe.c,v 1.18 2007/03/21 03:42:50 sam Exp $");
  18422. + */
  18423. +
  18424. +#ifndef AUTOCONF_INCLUDED
  18425. +#include <linux/config.h>
  18426. +#endif
  18427. +#include <linux/module.h>
  18428. +#include <linux/kernel.h>
  18429. +#include <linux/init.h>
  18430. +#include <linux/list.h>
  18431. +#include <linux/slab.h>
  18432. +#include <linux/wait.h>
  18433. +#include <linux/sched.h>
  18434. +#include <linux/pci.h>
  18435. +#include <linux/delay.h>
  18436. +#include <linux/interrupt.h>
  18437. +#include <linux/spinlock.h>
  18438. +#include <linux/random.h>
  18439. +#include <linux/version.h>
  18440. +#include <linux/skbuff.h>
  18441. +#include <asm/io.h>
  18442. +
  18443. +/*
  18444. + * SafeNet SafeXcel-1141 hardware crypto accelerator
  18445. + */
  18446. +
  18447. +#include <cryptodev.h>
  18448. +#include <uio.h>
  18449. +#include <safe/safereg.h>
  18450. +#include <safe/safevar.h>
  18451. +
  18452. +#if 1
  18453. +#define DPRINTF(a) do { \
  18454. + if (debug) { \
  18455. + printk("%s: ", sc ? \
  18456. + device_get_nameunit(sc->sc_dev) : "safe"); \
  18457. + printk a; \
  18458. + } \
  18459. + } while (0)
  18460. +#else
  18461. +#define DPRINTF(a)
  18462. +#endif
  18463. +
  18464. +/*
  18465. + * until we find a cleaner way, include the BSD md5/sha1 code
  18466. + * here
  18467. + */
  18468. +#define HMAC_HACK 1
  18469. +#ifdef HMAC_HACK
  18470. +#define LITTLE_ENDIAN 1234
  18471. +#define BIG_ENDIAN 4321
  18472. +#ifdef __LITTLE_ENDIAN
  18473. +#define BYTE_ORDER LITTLE_ENDIAN
  18474. +#endif
  18475. +#ifdef __BIG_ENDIAN
  18476. +#define BYTE_ORDER BIG_ENDIAN
  18477. +#endif
  18478. +#include <safe/md5.h>
  18479. +#include <safe/md5.c>
  18480. +#include <safe/sha1.h>
  18481. +#include <safe/sha1.c>
  18482. +
  18483. +u_int8_t hmac_ipad_buffer[64] = {
  18484. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18485. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18486. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18487. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18488. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18489. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18490. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18491. + 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
  18492. +};
  18493. +
  18494. +u_int8_t hmac_opad_buffer[64] = {
  18495. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18496. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18497. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18498. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18499. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18500. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18501. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
  18502. + 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C
  18503. +};
  18504. +#endif /* HMAC_HACK */
  18505. +
  18506. +/* add proc entry for this */
  18507. +struct safe_stats safestats;
  18508. +
  18509. +#define debug safe_debug
  18510. +int safe_debug = 0;
  18511. +module_param(safe_debug, int, 0644);
  18512. +MODULE_PARM_DESC(safe_debug, "Enable debug");
  18513. +
  18514. +static void safe_callback(struct safe_softc *, struct safe_ringentry *);
  18515. +static void safe_feed(struct safe_softc *, struct safe_ringentry *);
  18516. +#if defined(CONFIG_OCF_RANDOMHARVEST) && !defined(SAFE_NO_RNG)
  18517. +static void safe_rng_init(struct safe_softc *);
  18518. +int safe_rngbufsize = 8; /* 32 bytes each read */
  18519. +module_param(safe_rngbufsize, int, 0644);
  18520. +MODULE_PARM_DESC(safe_rngbufsize, "RNG polling buffer size (32-bit words)");
  18521. +int safe_rngmaxalarm = 8; /* max alarms before reset */
  18522. +module_param(safe_rngmaxalarm, int, 0644);
  18523. +MODULE_PARM_DESC(safe_rngmaxalarm, "RNG max alarms before reset");
  18524. +#endif /* SAFE_NO_RNG */
  18525. +
  18526. +static void safe_totalreset(struct safe_softc *sc);
  18527. +static int safe_dmamap_aligned(struct safe_softc *sc, const struct safe_operand *op);
  18528. +static int safe_dmamap_uniform(struct safe_softc *sc, const struct safe_operand *op);
  18529. +static int safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re);
  18530. +static int safe_kprocess(device_t dev, struct cryptkop *krp, int hint);
  18531. +static int safe_kstart(struct safe_softc *sc);
  18532. +static int safe_ksigbits(struct safe_softc *sc, struct crparam *cr);
  18533. +static void safe_kfeed(struct safe_softc *sc);
  18534. +static void safe_kpoll(unsigned long arg);
  18535. +static void safe_kload_reg(struct safe_softc *sc, u_int32_t off,
  18536. + u_int32_t len, struct crparam *n);
  18537. +
  18538. +static int safe_newsession(device_t, u_int32_t *, struct cryptoini *);
  18539. +static int safe_freesession(device_t, u_int64_t);
  18540. +static int safe_process(device_t, struct cryptop *, int);
  18541. +
  18542. +static device_method_t safe_methods = {
  18543. + /* crypto device methods */
  18544. + DEVMETHOD(cryptodev_newsession, safe_newsession),
  18545. + DEVMETHOD(cryptodev_freesession,safe_freesession),
  18546. + DEVMETHOD(cryptodev_process, safe_process),
  18547. + DEVMETHOD(cryptodev_kprocess, safe_kprocess),
  18548. +};
  18549. +
  18550. +#define READ_REG(sc,r) readl((sc)->sc_base_addr + (r))
  18551. +#define WRITE_REG(sc,r,val) writel((val), (sc)->sc_base_addr + (r))
  18552. +
  18553. +#define SAFE_MAX_CHIPS 8
  18554. +static struct safe_softc *safe_chip_idx[SAFE_MAX_CHIPS];
  18555. +
  18556. +/*
  18557. + * split our buffers up into safe DMAable byte fragments to avoid lockup
  18558. + * bug in 1141 HW on rev 1.0.
  18559. + */
  18560. +
  18561. +static int
  18562. +pci_map_linear(
  18563. + struct safe_softc *sc,
  18564. + struct safe_operand *buf,
  18565. + void *addr,
  18566. + int len)
  18567. +{
  18568. + dma_addr_t tmp;
  18569. + int chunk, tlen = len;
  18570. +
  18571. + tmp = pci_map_single(sc->sc_pcidev, addr, len, PCI_DMA_BIDIRECTIONAL);
  18572. +
  18573. + buf->mapsize += len;
  18574. + while (len > 0) {
  18575. + chunk = (len > sc->sc_max_dsize) ? sc->sc_max_dsize : len;
  18576. + buf->segs[buf->nsegs].ds_addr = tmp;
  18577. + buf->segs[buf->nsegs].ds_len = chunk;
  18578. + buf->segs[buf->nsegs].ds_tlen = tlen;
  18579. + buf->nsegs++;
  18580. + tmp += chunk;
  18581. + len -= chunk;
  18582. + tlen = 0;
  18583. + }
  18584. + return 0;
  18585. +}
  18586. +
  18587. +/*
  18588. + * map in a given uio buffer (great on some arches :-)
  18589. + */
  18590. +
  18591. +static int
  18592. +pci_map_uio(struct safe_softc *sc, struct safe_operand *buf, struct uio *uio)
  18593. +{
  18594. + struct iovec *iov = uio->uio_iov;
  18595. + int n;
  18596. +
  18597. + DPRINTF(("%s()\n", __FUNCTION__));
  18598. +
  18599. + buf->mapsize = 0;
  18600. + buf->nsegs = 0;
  18601. +
  18602. + for (n = 0; n < uio->uio_iovcnt; n++) {
  18603. + pci_map_linear(sc, buf, iov->iov_base, iov->iov_len);
  18604. + iov++;
  18605. + }
  18606. +
  18607. + /* identify this buffer by the first segment */
  18608. + buf->map = (void *) buf->segs[0].ds_addr;
  18609. + return(0);
  18610. +}
  18611. +
  18612. +/*
  18613. + * map in a given sk_buff
  18614. + */
  18615. +
  18616. +static int
  18617. +pci_map_skb(struct safe_softc *sc,struct safe_operand *buf,struct sk_buff *skb)
  18618. +{
  18619. + int i;
  18620. +
  18621. + DPRINTF(("%s()\n", __FUNCTION__));
  18622. +
  18623. + buf->mapsize = 0;
  18624. + buf->nsegs = 0;
  18625. +
  18626. + pci_map_linear(sc, buf, skb->data, skb_headlen(skb));
  18627. +
  18628. + for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  18629. + pci_map_linear(sc, buf,
  18630. + page_address(skb_shinfo(skb)->frags[i].page) +
  18631. + skb_shinfo(skb)->frags[i].page_offset,
  18632. + skb_shinfo(skb)->frags[i].size);
  18633. + }
  18634. +
  18635. + /* identify this buffer by the first segment */
  18636. + buf->map = (void *) buf->segs[0].ds_addr;
  18637. + return(0);
  18638. +}
  18639. +
  18640. +
  18641. +#if 0 /* not needed at this time */
  18642. +static void
  18643. +pci_sync_operand(struct safe_softc *sc, struct safe_operand *buf)
  18644. +{
  18645. + int i;
  18646. +
  18647. + DPRINTF(("%s()\n", __FUNCTION__));
  18648. + for (i = 0; i < buf->nsegs; i++)
  18649. + pci_dma_sync_single_for_cpu(sc->sc_pcidev, buf->segs[i].ds_addr,
  18650. + buf->segs[i].ds_len, PCI_DMA_BIDIRECTIONAL);
  18651. +}
  18652. +#endif
  18653. +
  18654. +static void
  18655. +pci_unmap_operand(struct safe_softc *sc, struct safe_operand *buf)
  18656. +{
  18657. + int i;
  18658. + DPRINTF(("%s()\n", __FUNCTION__));
  18659. + for (i = 0; i < buf->nsegs; i++) {
  18660. + if (buf->segs[i].ds_tlen) {
  18661. + DPRINTF(("%s - unmap %d 0x%x %d\n", __FUNCTION__, i, buf->segs[i].ds_addr, buf->segs[i].ds_tlen));
  18662. + pci_unmap_single(sc->sc_pcidev, buf->segs[i].ds_addr,
  18663. + buf->segs[i].ds_tlen, PCI_DMA_BIDIRECTIONAL);
  18664. + DPRINTF(("%s - unmap %d 0x%x %d done\n", __FUNCTION__, i, buf->segs[i].ds_addr, buf->segs[i].ds_tlen));
  18665. + }
  18666. + buf->segs[i].ds_addr = 0;
  18667. + buf->segs[i].ds_len = 0;
  18668. + buf->segs[i].ds_tlen = 0;
  18669. + }
  18670. + buf->nsegs = 0;
  18671. + buf->mapsize = 0;
  18672. + buf->map = 0;
  18673. +}
  18674. +
  18675. +
  18676. +/*
  18677. + * SafeXcel Interrupt routine
  18678. + */
  18679. +static irqreturn_t
  18680. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  18681. +safe_intr(int irq, void *arg)
  18682. +#else
  18683. +safe_intr(int irq, void *arg, struct pt_regs *regs)
  18684. +#endif
  18685. +{
  18686. + struct safe_softc *sc = arg;
  18687. + int stat;
  18688. + unsigned long flags;
  18689. +
  18690. + stat = READ_REG(sc, SAFE_HM_STAT);
  18691. +
  18692. + DPRINTF(("%s(stat=0x%x)\n", __FUNCTION__, stat));
  18693. +
  18694. + if (stat == 0) /* shared irq, not for us */
  18695. + return IRQ_NONE;
  18696. +
  18697. + WRITE_REG(sc, SAFE_HI_CLR, stat); /* IACK */
  18698. +
  18699. + if ((stat & SAFE_INT_PE_DDONE)) {
  18700. + /*
  18701. + * Descriptor(s) done; scan the ring and
  18702. + * process completed operations.
  18703. + */
  18704. + spin_lock_irqsave(&sc->sc_ringmtx, flags);
  18705. + while (sc->sc_back != sc->sc_front) {
  18706. + struct safe_ringentry *re = sc->sc_back;
  18707. +
  18708. +#ifdef SAFE_DEBUG
  18709. + if (debug) {
  18710. + safe_dump_ringstate(sc, __func__);
  18711. + safe_dump_request(sc, __func__, re);
  18712. + }
  18713. +#endif
  18714. + /*
  18715. + * safe_process marks ring entries that were allocated
  18716. + * but not used with a csr of zero. This insures the
  18717. + * ring front pointer never needs to be set backwards
  18718. + * in the event that an entry is allocated but not used
  18719. + * because of a setup error.
  18720. + */
  18721. + DPRINTF(("%s re->re_desc.d_csr=0x%x\n", __FUNCTION__, re->re_desc.d_csr));
  18722. + if (re->re_desc.d_csr != 0) {
  18723. + if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr)) {
  18724. + DPRINTF(("%s !CSR_IS_DONE\n", __FUNCTION__));
  18725. + break;
  18726. + }
  18727. + if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len)) {
  18728. + DPRINTF(("%s !LEN_IS_DONE\n", __FUNCTION__));
  18729. + break;
  18730. + }
  18731. + sc->sc_nqchip--;
  18732. + safe_callback(sc, re);
  18733. + }
  18734. + if (++(sc->sc_back) == sc->sc_ringtop)
  18735. + sc->sc_back = sc->sc_ring;
  18736. + }
  18737. + spin_unlock_irqrestore(&sc->sc_ringmtx, flags);
  18738. + }
  18739. +
  18740. + /*
  18741. + * Check to see if we got any DMA Error
  18742. + */
  18743. + if (stat & SAFE_INT_PE_ERROR) {
  18744. + printk("%s: dmaerr dmastat %08x\n", device_get_nameunit(sc->sc_dev),
  18745. + (int)READ_REG(sc, SAFE_PE_DMASTAT));
  18746. + safestats.st_dmaerr++;
  18747. + safe_totalreset(sc);
  18748. +#if 0
  18749. + safe_feed(sc);
  18750. +#endif
  18751. + }
  18752. +
  18753. + if (sc->sc_needwakeup) { /* XXX check high watermark */
  18754. + int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
  18755. + DPRINTF(("%s: wakeup crypto %x\n", __func__,
  18756. + sc->sc_needwakeup));
  18757. + sc->sc_needwakeup &= ~wakeup;
  18758. + crypto_unblock(sc->sc_cid, wakeup);
  18759. + }
  18760. +
  18761. + return IRQ_HANDLED;
  18762. +}
  18763. +
  18764. +/*
  18765. + * safe_feed() - post a request to chip
  18766. + */
  18767. +static void
  18768. +safe_feed(struct safe_softc *sc, struct safe_ringentry *re)
  18769. +{
  18770. + DPRINTF(("%s()\n", __FUNCTION__));
  18771. +#ifdef SAFE_DEBUG
  18772. + if (debug) {
  18773. + safe_dump_ringstate(sc, __func__);
  18774. + safe_dump_request(sc, __func__, re);
  18775. + }
  18776. +#endif
  18777. + sc->sc_nqchip++;
  18778. + if (sc->sc_nqchip > safestats.st_maxqchip)
  18779. + safestats.st_maxqchip = sc->sc_nqchip;
  18780. + /* poke h/w to check descriptor ring, any value can be written */
  18781. + WRITE_REG(sc, SAFE_HI_RD_DESCR, 0);
  18782. +}
  18783. +
  18784. +#define N(a) (sizeof(a) / sizeof (a[0]))
  18785. +static void
  18786. +safe_setup_enckey(struct safe_session *ses, caddr_t key)
  18787. +{
  18788. + int i;
  18789. +
  18790. + bcopy(key, ses->ses_key, ses->ses_klen / 8);
  18791. +
  18792. + /* PE is little-endian, insure proper byte order */
  18793. + for (i = 0; i < N(ses->ses_key); i++)
  18794. + ses->ses_key[i] = htole32(ses->ses_key[i]);
  18795. +}
  18796. +
  18797. +static void
  18798. +safe_setup_mackey(struct safe_session *ses, int algo, caddr_t key, int klen)
  18799. +{
  18800. +#ifdef HMAC_HACK
  18801. + MD5_CTX md5ctx;
  18802. + SHA1_CTX sha1ctx;
  18803. + int i;
  18804. +
  18805. +
  18806. + for (i = 0; i < klen; i++)
  18807. + key[i] ^= HMAC_IPAD_VAL;
  18808. +
  18809. + if (algo == CRYPTO_MD5_HMAC) {
  18810. + MD5Init(&md5ctx);
  18811. + MD5Update(&md5ctx, key, klen);
  18812. + MD5Update(&md5ctx, hmac_ipad_buffer, MD5_HMAC_BLOCK_LEN - klen);
  18813. + bcopy(md5ctx.md5_st8, ses->ses_hminner, sizeof(md5ctx.md5_st8));
  18814. + } else {
  18815. + SHA1Init(&sha1ctx);
  18816. + SHA1Update(&sha1ctx, key, klen);
  18817. + SHA1Update(&sha1ctx, hmac_ipad_buffer,
  18818. + SHA1_HMAC_BLOCK_LEN - klen);
  18819. + bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32));
  18820. + }
  18821. +
  18822. + for (i = 0; i < klen; i++)
  18823. + key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
  18824. +
  18825. + if (algo == CRYPTO_MD5_HMAC) {
  18826. + MD5Init(&md5ctx);
  18827. + MD5Update(&md5ctx, key, klen);
  18828. + MD5Update(&md5ctx, hmac_opad_buffer, MD5_HMAC_BLOCK_LEN - klen);
  18829. + bcopy(md5ctx.md5_st8, ses->ses_hmouter, sizeof(md5ctx.md5_st8));
  18830. + } else {
  18831. + SHA1Init(&sha1ctx);
  18832. + SHA1Update(&sha1ctx, key, klen);
  18833. + SHA1Update(&sha1ctx, hmac_opad_buffer,
  18834. + SHA1_HMAC_BLOCK_LEN - klen);
  18835. + bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32));
  18836. + }
  18837. +
  18838. + for (i = 0; i < klen; i++)
  18839. + key[i] ^= HMAC_OPAD_VAL;
  18840. +
  18841. +#if 0
  18842. + /*
  18843. + * this code prevents SHA working on a BE host,
  18844. + * so it is obviously wrong. I think the byte
  18845. + * swap setup we do with the chip fixes this for us
  18846. + */
  18847. +
  18848. + /* PE is little-endian, insure proper byte order */
  18849. + for (i = 0; i < N(ses->ses_hminner); i++) {
  18850. + ses->ses_hminner[i] = htole32(ses->ses_hminner[i]);
  18851. + ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]);
  18852. + }
  18853. +#endif
  18854. +#else /* HMAC_HACK */
  18855. + printk("safe: md5/sha not implemented\n");
  18856. +#endif /* HMAC_HACK */
  18857. +}
  18858. +#undef N
  18859. +
  18860. +/*
  18861. + * Allocate a new 'session' and return an encoded session id. 'sidp'
  18862. + * contains our registration id, and should contain an encoded session
  18863. + * id on successful allocation.
  18864. + */
  18865. +static int
  18866. +safe_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  18867. +{
  18868. + struct safe_softc *sc = device_get_softc(dev);
  18869. + struct cryptoini *c, *encini = NULL, *macini = NULL;
  18870. + struct safe_session *ses = NULL;
  18871. + int sesn;
  18872. +
  18873. + DPRINTF(("%s()\n", __FUNCTION__));
  18874. +
  18875. + if (sidp == NULL || cri == NULL || sc == NULL)
  18876. + return (EINVAL);
  18877. +
  18878. + for (c = cri; c != NULL; c = c->cri_next) {
  18879. + if (c->cri_alg == CRYPTO_MD5_HMAC ||
  18880. + c->cri_alg == CRYPTO_SHA1_HMAC ||
  18881. + c->cri_alg == CRYPTO_NULL_HMAC) {
  18882. + if (macini)
  18883. + return (EINVAL);
  18884. + macini = c;
  18885. + } else if (c->cri_alg == CRYPTO_DES_CBC ||
  18886. + c->cri_alg == CRYPTO_3DES_CBC ||
  18887. + c->cri_alg == CRYPTO_AES_CBC ||
  18888. + c->cri_alg == CRYPTO_NULL_CBC) {
  18889. + if (encini)
  18890. + return (EINVAL);
  18891. + encini = c;
  18892. + } else
  18893. + return (EINVAL);
  18894. + }
  18895. + if (encini == NULL && macini == NULL)
  18896. + return (EINVAL);
  18897. + if (encini) { /* validate key length */
  18898. + switch (encini->cri_alg) {
  18899. + case CRYPTO_DES_CBC:
  18900. + if (encini->cri_klen != 64)
  18901. + return (EINVAL);
  18902. + break;
  18903. + case CRYPTO_3DES_CBC:
  18904. + if (encini->cri_klen != 192)
  18905. + return (EINVAL);
  18906. + break;
  18907. + case CRYPTO_AES_CBC:
  18908. + if (encini->cri_klen != 128 &&
  18909. + encini->cri_klen != 192 &&
  18910. + encini->cri_klen != 256)
  18911. + return (EINVAL);
  18912. + break;
  18913. + }
  18914. + }
  18915. +
  18916. + if (sc->sc_sessions == NULL) {
  18917. + ses = sc->sc_sessions = (struct safe_session *)
  18918. + kmalloc(sizeof(struct safe_session), SLAB_ATOMIC);
  18919. + if (ses == NULL)
  18920. + return (ENOMEM);
  18921. + memset(ses, 0, sizeof(struct safe_session));
  18922. + sesn = 0;
  18923. + sc->sc_nsessions = 1;
  18924. + } else {
  18925. + for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  18926. + if (sc->sc_sessions[sesn].ses_used == 0) {
  18927. + ses = &sc->sc_sessions[sesn];
  18928. + break;
  18929. + }
  18930. + }
  18931. +
  18932. + if (ses == NULL) {
  18933. + sesn = sc->sc_nsessions;
  18934. + ses = (struct safe_session *)
  18935. + kmalloc((sesn + 1) * sizeof(struct safe_session), SLAB_ATOMIC);
  18936. + if (ses == NULL)
  18937. + return (ENOMEM);
  18938. + memset(ses, 0, (sesn + 1) * sizeof(struct safe_session));
  18939. + bcopy(sc->sc_sessions, ses, sesn *
  18940. + sizeof(struct safe_session));
  18941. + bzero(sc->sc_sessions, sesn *
  18942. + sizeof(struct safe_session));
  18943. + kfree(sc->sc_sessions);
  18944. + sc->sc_sessions = ses;
  18945. + ses = &sc->sc_sessions[sesn];
  18946. + sc->sc_nsessions++;
  18947. + }
  18948. + }
  18949. +
  18950. + bzero(ses, sizeof(struct safe_session));
  18951. + ses->ses_used = 1;
  18952. +
  18953. + if (encini) {
  18954. + /* get an IV */
  18955. + /* XXX may read fewer than requested */
  18956. + read_random(ses->ses_iv, sizeof(ses->ses_iv));
  18957. +
  18958. + ses->ses_klen = encini->cri_klen;
  18959. + if (encini->cri_key != NULL)
  18960. + safe_setup_enckey(ses, encini->cri_key);
  18961. + }
  18962. +
  18963. + if (macini) {
  18964. + ses->ses_mlen = macini->cri_mlen;
  18965. + if (ses->ses_mlen == 0) {
  18966. + if (macini->cri_alg == CRYPTO_MD5_HMAC)
  18967. + ses->ses_mlen = MD5_HASH_LEN;
  18968. + else
  18969. + ses->ses_mlen = SHA1_HASH_LEN;
  18970. + }
  18971. +
  18972. + if (macini->cri_key != NULL) {
  18973. + safe_setup_mackey(ses, macini->cri_alg, macini->cri_key,
  18974. + macini->cri_klen / 8);
  18975. + }
  18976. + }
  18977. +
  18978. + *sidp = SAFE_SID(device_get_unit(sc->sc_dev), sesn);
  18979. + return (0);
  18980. +}
  18981. +
  18982. +/*
  18983. + * Deallocate a session.
  18984. + */
  18985. +static int
  18986. +safe_freesession(device_t dev, u_int64_t tid)
  18987. +{
  18988. + struct safe_softc *sc = device_get_softc(dev);
  18989. + int session, ret;
  18990. + u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
  18991. +
  18992. + DPRINTF(("%s()\n", __FUNCTION__));
  18993. +
  18994. + if (sc == NULL)
  18995. + return (EINVAL);
  18996. +
  18997. + session = SAFE_SESSION(sid);
  18998. + if (session < sc->sc_nsessions) {
  18999. + bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
  19000. + ret = 0;
  19001. + } else
  19002. + ret = EINVAL;
  19003. + return (ret);
  19004. +}
  19005. +
  19006. +
  19007. +static int
  19008. +safe_process(device_t dev, struct cryptop *crp, int hint)
  19009. +{
  19010. + struct safe_softc *sc = device_get_softc(dev);
  19011. + int err = 0, i, nicealign, uniform;
  19012. + struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  19013. + int bypass, oplen, ivsize;
  19014. + caddr_t iv;
  19015. + int16_t coffset;
  19016. + struct safe_session *ses;
  19017. + struct safe_ringentry *re;
  19018. + struct safe_sarec *sa;
  19019. + struct safe_pdesc *pd;
  19020. + u_int32_t cmd0, cmd1, staterec;
  19021. + unsigned long flags;
  19022. +
  19023. + DPRINTF(("%s()\n", __FUNCTION__));
  19024. +
  19025. + if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
  19026. + safestats.st_invalid++;
  19027. + return (EINVAL);
  19028. + }
  19029. + if (SAFE_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
  19030. + safestats.st_badsession++;
  19031. + return (EINVAL);
  19032. + }
  19033. +
  19034. + spin_lock_irqsave(&sc->sc_ringmtx, flags);
  19035. + if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) {
  19036. + safestats.st_ringfull++;
  19037. + sc->sc_needwakeup |= CRYPTO_SYMQ;
  19038. + spin_unlock_irqrestore(&sc->sc_ringmtx, flags);
  19039. + return (ERESTART);
  19040. + }
  19041. + re = sc->sc_front;
  19042. +
  19043. + staterec = re->re_sa.sa_staterec; /* save */
  19044. + /* NB: zero everything but the PE descriptor */
  19045. + bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc));
  19046. + re->re_sa.sa_staterec = staterec; /* restore */
  19047. +
  19048. + re->re_crp = crp;
  19049. + re->re_sesn = SAFE_SESSION(crp->crp_sid);
  19050. +
  19051. + re->re_src.nsegs = 0;
  19052. + re->re_dst.nsegs = 0;
  19053. +
  19054. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  19055. + re->re_src_skb = (struct sk_buff *)crp->crp_buf;
  19056. + re->re_dst_skb = (struct sk_buff *)crp->crp_buf;
  19057. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  19058. + re->re_src_io = (struct uio *)crp->crp_buf;
  19059. + re->re_dst_io = (struct uio *)crp->crp_buf;
  19060. + } else {
  19061. + safestats.st_badflags++;
  19062. + err = EINVAL;
  19063. + goto errout; /* XXX we don't handle contiguous blocks! */
  19064. + }
  19065. +
  19066. + sa = &re->re_sa;
  19067. + ses = &sc->sc_sessions[re->re_sesn];
  19068. +
  19069. + crd1 = crp->crp_desc;
  19070. + if (crd1 == NULL) {
  19071. + safestats.st_nodesc++;
  19072. + err = EINVAL;
  19073. + goto errout;
  19074. + }
  19075. + crd2 = crd1->crd_next;
  19076. +
  19077. + cmd0 = SAFE_SA_CMD0_BASIC; /* basic group operation */
  19078. + cmd1 = 0;
  19079. + if (crd2 == NULL) {
  19080. + if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
  19081. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  19082. + crd1->crd_alg == CRYPTO_NULL_HMAC) {
  19083. + maccrd = crd1;
  19084. + enccrd = NULL;
  19085. + cmd0 |= SAFE_SA_CMD0_OP_HASH;
  19086. + } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
  19087. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  19088. + crd1->crd_alg == CRYPTO_AES_CBC ||
  19089. + crd1->crd_alg == CRYPTO_NULL_CBC) {
  19090. + maccrd = NULL;
  19091. + enccrd = crd1;
  19092. + cmd0 |= SAFE_SA_CMD0_OP_CRYPT;
  19093. + } else {
  19094. + safestats.st_badalg++;
  19095. + err = EINVAL;
  19096. + goto errout;
  19097. + }
  19098. + } else {
  19099. + if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
  19100. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  19101. + crd1->crd_alg == CRYPTO_NULL_HMAC) &&
  19102. + (crd2->crd_alg == CRYPTO_DES_CBC ||
  19103. + crd2->crd_alg == CRYPTO_3DES_CBC ||
  19104. + crd2->crd_alg == CRYPTO_AES_CBC ||
  19105. + crd2->crd_alg == CRYPTO_NULL_CBC) &&
  19106. + ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
  19107. + maccrd = crd1;
  19108. + enccrd = crd2;
  19109. + } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
  19110. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  19111. + crd1->crd_alg == CRYPTO_AES_CBC ||
  19112. + crd1->crd_alg == CRYPTO_NULL_CBC) &&
  19113. + (crd2->crd_alg == CRYPTO_MD5_HMAC ||
  19114. + crd2->crd_alg == CRYPTO_SHA1_HMAC ||
  19115. + crd2->crd_alg == CRYPTO_NULL_HMAC) &&
  19116. + (crd1->crd_flags & CRD_F_ENCRYPT)) {
  19117. + enccrd = crd1;
  19118. + maccrd = crd2;
  19119. + } else {
  19120. + safestats.st_badalg++;
  19121. + err = EINVAL;
  19122. + goto errout;
  19123. + }
  19124. + cmd0 |= SAFE_SA_CMD0_OP_BOTH;
  19125. + }
  19126. +
  19127. + if (enccrd) {
  19128. + if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
  19129. + safe_setup_enckey(ses, enccrd->crd_key);
  19130. +
  19131. + if (enccrd->crd_alg == CRYPTO_DES_CBC) {
  19132. + cmd0 |= SAFE_SA_CMD0_DES;
  19133. + cmd1 |= SAFE_SA_CMD1_CBC;
  19134. + ivsize = 2*sizeof(u_int32_t);
  19135. + } else if (enccrd->crd_alg == CRYPTO_3DES_CBC) {
  19136. + cmd0 |= SAFE_SA_CMD0_3DES;
  19137. + cmd1 |= SAFE_SA_CMD1_CBC;
  19138. + ivsize = 2*sizeof(u_int32_t);
  19139. + } else if (enccrd->crd_alg == CRYPTO_AES_CBC) {
  19140. + cmd0 |= SAFE_SA_CMD0_AES;
  19141. + cmd1 |= SAFE_SA_CMD1_CBC;
  19142. + if (ses->ses_klen == 128)
  19143. + cmd1 |= SAFE_SA_CMD1_AES128;
  19144. + else if (ses->ses_klen == 192)
  19145. + cmd1 |= SAFE_SA_CMD1_AES192;
  19146. + else
  19147. + cmd1 |= SAFE_SA_CMD1_AES256;
  19148. + ivsize = 4*sizeof(u_int32_t);
  19149. + } else {
  19150. + cmd0 |= SAFE_SA_CMD0_CRYPT_NULL;
  19151. + ivsize = 0;
  19152. + }
  19153. +
  19154. + /*
  19155. + * Setup encrypt/decrypt state. When using basic ops
  19156. + * we can't use an inline IV because hash/crypt offset
  19157. + * must be from the end of the IV to the start of the
  19158. + * crypt data and this leaves out the preceding header
  19159. + * from the hash calculation. Instead we place the IV
  19160. + * in the state record and set the hash/crypt offset to
  19161. + * copy both the header+IV.
  19162. + */
  19163. + if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  19164. + cmd0 |= SAFE_SA_CMD0_OUTBOUND;
  19165. +
  19166. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  19167. + iv = enccrd->crd_iv;
  19168. + else
  19169. + iv = (caddr_t) ses->ses_iv;
  19170. + if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  19171. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  19172. + enccrd->crd_inject, ivsize, iv);
  19173. + }
  19174. + bcopy(iv, re->re_sastate.sa_saved_iv, ivsize);
  19175. + /* make iv LE */
  19176. + for (i = 0; i < ivsize/sizeof(re->re_sastate.sa_saved_iv[0]); i++)
  19177. + re->re_sastate.sa_saved_iv[i] =
  19178. + cpu_to_le32(re->re_sastate.sa_saved_iv[i]);
  19179. + cmd0 |= SAFE_SA_CMD0_IVLD_STATE | SAFE_SA_CMD0_SAVEIV;
  19180. + re->re_flags |= SAFE_QFLAGS_COPYOUTIV;
  19181. + } else {
  19182. + cmd0 |= SAFE_SA_CMD0_INBOUND;
  19183. +
  19184. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
  19185. + bcopy(enccrd->crd_iv,
  19186. + re->re_sastate.sa_saved_iv, ivsize);
  19187. + } else {
  19188. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  19189. + enccrd->crd_inject, ivsize,
  19190. + (caddr_t)re->re_sastate.sa_saved_iv);
  19191. + }
  19192. + /* make iv LE */
  19193. + for (i = 0; i < ivsize/sizeof(re->re_sastate.sa_saved_iv[0]); i++)
  19194. + re->re_sastate.sa_saved_iv[i] =
  19195. + cpu_to_le32(re->re_sastate.sa_saved_iv[i]);
  19196. + cmd0 |= SAFE_SA_CMD0_IVLD_STATE;
  19197. + }
  19198. + /*
  19199. + * For basic encryption use the zero pad algorithm.
  19200. + * This pads results to an 8-byte boundary and
  19201. + * suppresses padding verification for inbound (i.e.
  19202. + * decrypt) operations.
  19203. + *
  19204. + * NB: Not sure if the 8-byte pad boundary is a problem.
  19205. + */
  19206. + cmd0 |= SAFE_SA_CMD0_PAD_ZERO;
  19207. +
  19208. + /* XXX assert key bufs have the same size */
  19209. + bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key));
  19210. + }
  19211. +
  19212. + if (maccrd) {
  19213. + if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
  19214. + safe_setup_mackey(ses, maccrd->crd_alg,
  19215. + maccrd->crd_key, maccrd->crd_klen / 8);
  19216. + }
  19217. +
  19218. + if (maccrd->crd_alg == CRYPTO_MD5_HMAC) {
  19219. + cmd0 |= SAFE_SA_CMD0_MD5;
  19220. + cmd1 |= SAFE_SA_CMD1_HMAC; /* NB: enable HMAC */
  19221. + } else if (maccrd->crd_alg == CRYPTO_SHA1_HMAC) {
  19222. + cmd0 |= SAFE_SA_CMD0_SHA1;
  19223. + cmd1 |= SAFE_SA_CMD1_HMAC; /* NB: enable HMAC */
  19224. + } else {
  19225. + cmd0 |= SAFE_SA_CMD0_HASH_NULL;
  19226. + }
  19227. + /*
  19228. + * Digest data is loaded from the SA and the hash
  19229. + * result is saved to the state block where we
  19230. + * retrieve it for return to the caller.
  19231. + */
  19232. + /* XXX assert digest bufs have the same size */
  19233. + bcopy(ses->ses_hminner, sa->sa_indigest,
  19234. + sizeof(sa->sa_indigest));
  19235. + bcopy(ses->ses_hmouter, sa->sa_outdigest,
  19236. + sizeof(sa->sa_outdigest));
  19237. +
  19238. + cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH;
  19239. + re->re_flags |= SAFE_QFLAGS_COPYOUTICV;
  19240. + }
  19241. +
  19242. + if (enccrd && maccrd) {
  19243. + /*
  19244. + * The offset from hash data to the start of
  19245. + * crypt data is the difference in the skips.
  19246. + */
  19247. + bypass = maccrd->crd_skip;
  19248. + coffset = enccrd->crd_skip - maccrd->crd_skip;
  19249. + if (coffset < 0) {
  19250. + DPRINTF(("%s: hash does not precede crypt; "
  19251. + "mac skip %u enc skip %u\n",
  19252. + __func__, maccrd->crd_skip, enccrd->crd_skip));
  19253. + safestats.st_skipmismatch++;
  19254. + err = EINVAL;
  19255. + goto errout;
  19256. + }
  19257. + oplen = enccrd->crd_skip + enccrd->crd_len;
  19258. + if (maccrd->crd_skip + maccrd->crd_len != oplen) {
  19259. + DPRINTF(("%s: hash amount %u != crypt amount %u\n",
  19260. + __func__, maccrd->crd_skip + maccrd->crd_len,
  19261. + oplen));
  19262. + safestats.st_lenmismatch++;
  19263. + err = EINVAL;
  19264. + goto errout;
  19265. + }
  19266. +#ifdef SAFE_DEBUG
  19267. + if (debug) {
  19268. + printf("mac: skip %d, len %d, inject %d\n",
  19269. + maccrd->crd_skip, maccrd->crd_len,
  19270. + maccrd->crd_inject);
  19271. + printf("enc: skip %d, len %d, inject %d\n",
  19272. + enccrd->crd_skip, enccrd->crd_len,
  19273. + enccrd->crd_inject);
  19274. + printf("bypass %d coffset %d oplen %d\n",
  19275. + bypass, coffset, oplen);
  19276. + }
  19277. +#endif
  19278. + if (coffset & 3) { /* offset must be 32-bit aligned */
  19279. + DPRINTF(("%s: coffset %u misaligned\n",
  19280. + __func__, coffset));
  19281. + safestats.st_coffmisaligned++;
  19282. + err = EINVAL;
  19283. + goto errout;
  19284. + }
  19285. + coffset >>= 2;
  19286. + if (coffset > 255) { /* offset must be <256 dwords */
  19287. + DPRINTF(("%s: coffset %u too big\n",
  19288. + __func__, coffset));
  19289. + safestats.st_cofftoobig++;
  19290. + err = EINVAL;
  19291. + goto errout;
  19292. + }
  19293. + /*
  19294. + * Tell the hardware to copy the header to the output.
  19295. + * The header is defined as the data from the end of
  19296. + * the bypass to the start of data to be encrypted.
  19297. + * Typically this is the inline IV. Note that you need
  19298. + * to do this even if src+dst are the same; it appears
  19299. + * that w/o this bit the crypted data is written
  19300. + * immediately after the bypass data.
  19301. + */
  19302. + cmd1 |= SAFE_SA_CMD1_HDRCOPY;
  19303. + /*
  19304. + * Disable IP header mutable bit handling. This is
  19305. + * needed to get correct HMAC calculations.
  19306. + */
  19307. + cmd1 |= SAFE_SA_CMD1_MUTABLE;
  19308. + } else {
  19309. + if (enccrd) {
  19310. + bypass = enccrd->crd_skip;
  19311. + oplen = bypass + enccrd->crd_len;
  19312. + } else {
  19313. + bypass = maccrd->crd_skip;
  19314. + oplen = bypass + maccrd->crd_len;
  19315. + }
  19316. + coffset = 0;
  19317. + }
  19318. + /* XXX verify multiple of 4 when using s/g */
  19319. + if (bypass > 96) { /* bypass offset must be <= 96 bytes */
  19320. + DPRINTF(("%s: bypass %u too big\n", __func__, bypass));
  19321. + safestats.st_bypasstoobig++;
  19322. + err = EINVAL;
  19323. + goto errout;
  19324. + }
  19325. +
  19326. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  19327. + if (pci_map_skb(sc, &re->re_src, re->re_src_skb)) {
  19328. + safestats.st_noload++;
  19329. + err = ENOMEM;
  19330. + goto errout;
  19331. + }
  19332. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  19333. + if (pci_map_uio(sc, &re->re_src, re->re_src_io)) {
  19334. + safestats.st_noload++;
  19335. + err = ENOMEM;
  19336. + goto errout;
  19337. + }
  19338. + }
  19339. + nicealign = safe_dmamap_aligned(sc, &re->re_src);
  19340. + uniform = safe_dmamap_uniform(sc, &re->re_src);
  19341. +
  19342. + DPRINTF(("src nicealign %u uniform %u nsegs %u\n",
  19343. + nicealign, uniform, re->re_src.nsegs));
  19344. + if (re->re_src.nsegs > 1) {
  19345. + re->re_desc.d_src = sc->sc_spalloc.dma_paddr +
  19346. + ((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring);
  19347. + for (i = 0; i < re->re_src_nsegs; i++) {
  19348. + /* NB: no need to check if there's space */
  19349. + pd = sc->sc_spfree;
  19350. + if (++(sc->sc_spfree) == sc->sc_springtop)
  19351. + sc->sc_spfree = sc->sc_spring;
  19352. +
  19353. + KASSERT((pd->pd_flags&3) == 0 ||
  19354. + (pd->pd_flags&3) == SAFE_PD_DONE,
  19355. + ("bogus source particle descriptor; flags %x",
  19356. + pd->pd_flags));
  19357. + pd->pd_addr = re->re_src_segs[i].ds_addr;
  19358. + pd->pd_size = re->re_src_segs[i].ds_len;
  19359. + pd->pd_flags = SAFE_PD_READY;
  19360. + }
  19361. + cmd0 |= SAFE_SA_CMD0_IGATHER;
  19362. + } else {
  19363. + /*
  19364. + * No need for gather, reference the operand directly.
  19365. + */
  19366. + re->re_desc.d_src = re->re_src_segs[0].ds_addr;
  19367. + }
  19368. +
  19369. + if (enccrd == NULL && maccrd != NULL) {
  19370. + /*
  19371. + * Hash op; no destination needed.
  19372. + */
  19373. + } else {
  19374. + if (crp->crp_flags & (CRYPTO_F_IOV|CRYPTO_F_SKBUF)) {
  19375. + if (!nicealign) {
  19376. + safestats.st_iovmisaligned++;
  19377. + err = EINVAL;
  19378. + goto errout;
  19379. + }
  19380. + if (uniform != 1) {
  19381. + device_printf(sc->sc_dev, "!uniform source\n");
  19382. + if (!uniform) {
  19383. + /*
  19384. + * There's no way to handle the DMA
  19385. + * requirements with this uio. We
  19386. + * could create a separate DMA area for
  19387. + * the result and then copy it back,
  19388. + * but for now we just bail and return
  19389. + * an error. Note that uio requests
  19390. + * > SAFE_MAX_DSIZE are handled because
  19391. + * the DMA map and segment list for the
  19392. + * destination wil result in a
  19393. + * destination particle list that does
  19394. + * the necessary scatter DMA.
  19395. + */
  19396. + safestats.st_iovnotuniform++;
  19397. + err = EINVAL;
  19398. + goto errout;
  19399. + }
  19400. + } else
  19401. + re->re_dst = re->re_src;
  19402. + } else {
  19403. + safestats.st_badflags++;
  19404. + err = EINVAL;
  19405. + goto errout;
  19406. + }
  19407. +
  19408. + if (re->re_dst.nsegs > 1) {
  19409. + re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr +
  19410. + ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring);
  19411. + for (i = 0; i < re->re_dst_nsegs; i++) {
  19412. + pd = sc->sc_dpfree;
  19413. + KASSERT((pd->pd_flags&3) == 0 ||
  19414. + (pd->pd_flags&3) == SAFE_PD_DONE,
  19415. + ("bogus dest particle descriptor; flags %x",
  19416. + pd->pd_flags));
  19417. + if (++(sc->sc_dpfree) == sc->sc_dpringtop)
  19418. + sc->sc_dpfree = sc->sc_dpring;
  19419. + pd->pd_addr = re->re_dst_segs[i].ds_addr;
  19420. + pd->pd_flags = SAFE_PD_READY;
  19421. + }
  19422. + cmd0 |= SAFE_SA_CMD0_OSCATTER;
  19423. + } else {
  19424. + /*
  19425. + * No need for scatter, reference the operand directly.
  19426. + */
  19427. + re->re_desc.d_dst = re->re_dst_segs[0].ds_addr;
  19428. + }
  19429. + }
  19430. +
  19431. + /*
  19432. + * All done with setup; fillin the SA command words
  19433. + * and the packet engine descriptor. The operation
  19434. + * is now ready for submission to the hardware.
  19435. + */
  19436. + sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI;
  19437. + sa->sa_cmd1 = cmd1
  19438. + | (coffset << SAFE_SA_CMD1_OFFSET_S)
  19439. + | SAFE_SA_CMD1_SAREV1 /* Rev 1 SA data structure */
  19440. + | SAFE_SA_CMD1_SRPCI
  19441. + ;
  19442. + /*
  19443. + * NB: the order of writes is important here. In case the
  19444. + * chip is scanning the ring because of an outstanding request
  19445. + * it might nab this one too. In that case we need to make
  19446. + * sure the setup is complete before we write the length
  19447. + * field of the descriptor as it signals the descriptor is
  19448. + * ready for processing.
  19449. + */
  19450. + re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI;
  19451. + if (maccrd)
  19452. + re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL;
  19453. + wmb();
  19454. + re->re_desc.d_len = oplen
  19455. + | SAFE_PE_LEN_READY
  19456. + | (bypass << SAFE_PE_LEN_BYPASS_S)
  19457. + ;
  19458. +
  19459. + safestats.st_ipackets++;
  19460. + safestats.st_ibytes += oplen;
  19461. +
  19462. + if (++(sc->sc_front) == sc->sc_ringtop)
  19463. + sc->sc_front = sc->sc_ring;
  19464. +
  19465. + /* XXX honor batching */
  19466. + safe_feed(sc, re);
  19467. + spin_unlock_irqrestore(&sc->sc_ringmtx, flags);
  19468. + return (0);
  19469. +
  19470. +errout:
  19471. + if (re->re_src.map != re->re_dst.map)
  19472. + pci_unmap_operand(sc, &re->re_dst);
  19473. + if (re->re_src.map)
  19474. + pci_unmap_operand(sc, &re->re_src);
  19475. + spin_unlock_irqrestore(&sc->sc_ringmtx, flags);
  19476. + if (err != ERESTART) {
  19477. + crp->crp_etype = err;
  19478. + crypto_done(crp);
  19479. + } else {
  19480. + sc->sc_needwakeup |= CRYPTO_SYMQ;
  19481. + }
  19482. + return (err);
  19483. +}
  19484. +
  19485. +static void
  19486. +safe_callback(struct safe_softc *sc, struct safe_ringentry *re)
  19487. +{
  19488. + struct cryptop *crp = (struct cryptop *)re->re_crp;
  19489. + struct cryptodesc *crd;
  19490. +
  19491. + DPRINTF(("%s()\n", __FUNCTION__));
  19492. +
  19493. + safestats.st_opackets++;
  19494. + safestats.st_obytes += re->re_dst.mapsize;
  19495. +
  19496. + if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) {
  19497. + device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n",
  19498. + re->re_desc.d_csr,
  19499. + re->re_sa.sa_cmd0, re->re_sa.sa_cmd1);
  19500. + safestats.st_peoperr++;
  19501. + crp->crp_etype = EIO; /* something more meaningful? */
  19502. + }
  19503. +
  19504. + if (re->re_dst.map != NULL && re->re_dst.map != re->re_src.map)
  19505. + pci_unmap_operand(sc, &re->re_dst);
  19506. + pci_unmap_operand(sc, &re->re_src);
  19507. +
  19508. + /*
  19509. + * If result was written to a differet mbuf chain, swap
  19510. + * it in as the return value and reclaim the original.
  19511. + */
  19512. + if ((crp->crp_flags & CRYPTO_F_SKBUF) && re->re_src_skb != re->re_dst_skb) {
  19513. + device_printf(sc->sc_dev, "no CRYPTO_F_SKBUF swapping support\n");
  19514. + /* kfree_skb(skb) */
  19515. + /* crp->crp_buf = (caddr_t)re->re_dst_skb */
  19516. + return;
  19517. + }
  19518. +
  19519. + if (re->re_flags & SAFE_QFLAGS_COPYOUTIV) {
  19520. + /* copy out IV for future use */
  19521. + for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  19522. + int i;
  19523. + int ivsize;
  19524. +
  19525. + if (crd->crd_alg == CRYPTO_DES_CBC ||
  19526. + crd->crd_alg == CRYPTO_3DES_CBC) {
  19527. + ivsize = 2*sizeof(u_int32_t);
  19528. + } else if (crd->crd_alg == CRYPTO_AES_CBC) {
  19529. + ivsize = 4*sizeof(u_int32_t);
  19530. + } else
  19531. + continue;
  19532. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  19533. + crd->crd_skip + crd->crd_len - ivsize, ivsize,
  19534. + (caddr_t)sc->sc_sessions[re->re_sesn].ses_iv);
  19535. + for (i = 0;
  19536. + i < ivsize/sizeof(sc->sc_sessions[re->re_sesn].ses_iv[0]);
  19537. + i++)
  19538. + sc->sc_sessions[re->re_sesn].ses_iv[i] =
  19539. + cpu_to_le32(sc->sc_sessions[re->re_sesn].ses_iv[i]);
  19540. + break;
  19541. + }
  19542. + }
  19543. +
  19544. + if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) {
  19545. + /* copy out ICV result */
  19546. + for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  19547. + if (!(crd->crd_alg == CRYPTO_MD5_HMAC ||
  19548. + crd->crd_alg == CRYPTO_SHA1_HMAC ||
  19549. + crd->crd_alg == CRYPTO_NULL_HMAC))
  19550. + continue;
  19551. + if (crd->crd_alg == CRYPTO_SHA1_HMAC) {
  19552. + /*
  19553. + * SHA-1 ICV's are byte-swapped; fix 'em up
  19554. + * before copy them to their destination.
  19555. + */
  19556. + re->re_sastate.sa_saved_indigest[0] =
  19557. + cpu_to_be32(re->re_sastate.sa_saved_indigest[0]);
  19558. + re->re_sastate.sa_saved_indigest[1] =
  19559. + cpu_to_be32(re->re_sastate.sa_saved_indigest[1]);
  19560. + re->re_sastate.sa_saved_indigest[2] =
  19561. + cpu_to_be32(re->re_sastate.sa_saved_indigest[2]);
  19562. + } else {
  19563. + re->re_sastate.sa_saved_indigest[0] =
  19564. + cpu_to_le32(re->re_sastate.sa_saved_indigest[0]);
  19565. + re->re_sastate.sa_saved_indigest[1] =
  19566. + cpu_to_le32(re->re_sastate.sa_saved_indigest[1]);
  19567. + re->re_sastate.sa_saved_indigest[2] =
  19568. + cpu_to_le32(re->re_sastate.sa_saved_indigest[2]);
  19569. + }
  19570. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  19571. + crd->crd_inject,
  19572. + sc->sc_sessions[re->re_sesn].ses_mlen,
  19573. + (caddr_t)re->re_sastate.sa_saved_indigest);
  19574. + break;
  19575. + }
  19576. + }
  19577. + crypto_done(crp);
  19578. +}
  19579. +
  19580. +
  19581. +#if defined(CONFIG_OCF_RANDOMHARVEST) && !defined(SAFE_NO_RNG)
  19582. +#define SAFE_RNG_MAXWAIT 1000
  19583. +
  19584. +static void
  19585. +safe_rng_init(struct safe_softc *sc)
  19586. +{
  19587. + u_int32_t w, v;
  19588. + int i;
  19589. +
  19590. + DPRINTF(("%s()\n", __FUNCTION__));
  19591. +
  19592. + WRITE_REG(sc, SAFE_RNG_CTRL, 0);
  19593. + /* use default value according to the manual */
  19594. + WRITE_REG(sc, SAFE_RNG_CNFG, 0x834); /* magic from SafeNet */
  19595. + WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
  19596. +
  19597. + /*
  19598. + * There is a bug in rev 1.0 of the 1140 that when the RNG
  19599. + * is brought out of reset the ready status flag does not
  19600. + * work until the RNG has finished its internal initialization.
  19601. + *
  19602. + * So in order to determine the device is through its
  19603. + * initialization we must read the data register, using the
  19604. + * status reg in the read in case it is initialized. Then read
  19605. + * the data register until it changes from the first read.
  19606. + * Once it changes read the data register until it changes
  19607. + * again. At this time the RNG is considered initialized.
  19608. + * This could take between 750ms - 1000ms in time.
  19609. + */
  19610. + i = 0;
  19611. + w = READ_REG(sc, SAFE_RNG_OUT);
  19612. + do {
  19613. + v = READ_REG(sc, SAFE_RNG_OUT);
  19614. + if (v != w) {
  19615. + w = v;
  19616. + break;
  19617. + }
  19618. + DELAY(10);
  19619. + } while (++i < SAFE_RNG_MAXWAIT);
  19620. +
  19621. + /* Wait Until data changes again */
  19622. + i = 0;
  19623. + do {
  19624. + v = READ_REG(sc, SAFE_RNG_OUT);
  19625. + if (v != w)
  19626. + break;
  19627. + DELAY(10);
  19628. + } while (++i < SAFE_RNG_MAXWAIT);
  19629. +}
  19630. +
  19631. +static __inline void
  19632. +safe_rng_disable_short_cycle(struct safe_softc *sc)
  19633. +{
  19634. + DPRINTF(("%s()\n", __FUNCTION__));
  19635. +
  19636. + WRITE_REG(sc, SAFE_RNG_CTRL,
  19637. + READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN);
  19638. +}
  19639. +
  19640. +static __inline void
  19641. +safe_rng_enable_short_cycle(struct safe_softc *sc)
  19642. +{
  19643. + DPRINTF(("%s()\n", __FUNCTION__));
  19644. +
  19645. + WRITE_REG(sc, SAFE_RNG_CTRL,
  19646. + READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN);
  19647. +}
  19648. +
  19649. +static __inline u_int32_t
  19650. +safe_rng_read(struct safe_softc *sc)
  19651. +{
  19652. + int i;
  19653. +
  19654. + i = 0;
  19655. + while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT)
  19656. + ;
  19657. + return READ_REG(sc, SAFE_RNG_OUT);
  19658. +}
  19659. +
  19660. +static int
  19661. +safe_read_random(void *arg, u_int32_t *buf, int maxwords)
  19662. +{
  19663. + struct safe_softc *sc = (struct safe_softc *) arg;
  19664. + int i, rc;
  19665. +
  19666. + DPRINTF(("%s()\n", __FUNCTION__));
  19667. +
  19668. + safestats.st_rng++;
  19669. + /*
  19670. + * Fetch the next block of data.
  19671. + */
  19672. + if (maxwords > safe_rngbufsize)
  19673. + maxwords = safe_rngbufsize;
  19674. + if (maxwords > SAFE_RNG_MAXBUFSIZ)
  19675. + maxwords = SAFE_RNG_MAXBUFSIZ;
  19676. +retry:
  19677. + /* read as much as we can */
  19678. + for (rc = 0; rc < maxwords; rc++) {
  19679. + if (READ_REG(sc, SAFE_RNG_STAT) != 0)
  19680. + break;
  19681. + buf[rc] = READ_REG(sc, SAFE_RNG_OUT);
  19682. + }
  19683. + if (rc == 0)
  19684. + return 0;
  19685. + /*
  19686. + * Check the comparator alarm count and reset the h/w if
  19687. + * it exceeds our threshold. This guards against the
  19688. + * hardware oscillators resonating with external signals.
  19689. + */
  19690. + if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) {
  19691. + u_int32_t freq_inc, w;
  19692. +
  19693. + DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__,
  19694. + (unsigned)READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm));
  19695. + safestats.st_rngalarm++;
  19696. + safe_rng_enable_short_cycle(sc);
  19697. + freq_inc = 18;
  19698. + for (i = 0; i < 64; i++) {
  19699. + w = READ_REG(sc, SAFE_RNG_CNFG);
  19700. + freq_inc = ((w + freq_inc) & 0x3fL);
  19701. + w = ((w & ~0x3fL) | freq_inc);
  19702. + WRITE_REG(sc, SAFE_RNG_CNFG, w);
  19703. +
  19704. + WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
  19705. +
  19706. + (void) safe_rng_read(sc);
  19707. + DELAY(25);
  19708. +
  19709. + if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) {
  19710. + safe_rng_disable_short_cycle(sc);
  19711. + goto retry;
  19712. + }
  19713. + freq_inc = 1;
  19714. + }
  19715. + safe_rng_disable_short_cycle(sc);
  19716. + } else
  19717. + WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
  19718. +
  19719. + return(rc);
  19720. +}
  19721. +#endif /* defined(CONFIG_OCF_RANDOMHARVEST) && !defined(SAFE_NO_RNG) */
  19722. +
  19723. +
  19724. +/*
  19725. + * Resets the board. Values in the regesters are left as is
  19726. + * from the reset (i.e. initial values are assigned elsewhere).
  19727. + */
  19728. +static void
  19729. +safe_reset_board(struct safe_softc *sc)
  19730. +{
  19731. + u_int32_t v;
  19732. + /*
  19733. + * Reset the device. The manual says no delay
  19734. + * is needed between marking and clearing reset.
  19735. + */
  19736. + DPRINTF(("%s()\n", __FUNCTION__));
  19737. +
  19738. + v = READ_REG(sc, SAFE_PE_DMACFG) &~
  19739. + (SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET |
  19740. + SAFE_PE_DMACFG_SGRESET);
  19741. + WRITE_REG(sc, SAFE_PE_DMACFG, v
  19742. + | SAFE_PE_DMACFG_PERESET
  19743. + | SAFE_PE_DMACFG_PDRRESET
  19744. + | SAFE_PE_DMACFG_SGRESET);
  19745. + WRITE_REG(sc, SAFE_PE_DMACFG, v);
  19746. +}
  19747. +
  19748. +/*
  19749. + * Initialize registers we need to touch only once.
  19750. + */
  19751. +static void
  19752. +safe_init_board(struct safe_softc *sc)
  19753. +{
  19754. + u_int32_t v, dwords;
  19755. +
  19756. + DPRINTF(("%s()\n", __FUNCTION__));
  19757. +
  19758. + v = READ_REG(sc, SAFE_PE_DMACFG);
  19759. + v &=~ ( SAFE_PE_DMACFG_PEMODE
  19760. + | SAFE_PE_DMACFG_FSENA /* failsafe enable */
  19761. + | SAFE_PE_DMACFG_GPRPCI /* gather ring on PCI */
  19762. + | SAFE_PE_DMACFG_SPRPCI /* scatter ring on PCI */
  19763. + | SAFE_PE_DMACFG_ESDESC /* endian-swap descriptors */
  19764. + | SAFE_PE_DMACFG_ESPDESC /* endian-swap part. desc's */
  19765. + | SAFE_PE_DMACFG_ESSA /* endian-swap SA's */
  19766. + | SAFE_PE_DMACFG_ESPACKET /* swap the packet data */
  19767. + );
  19768. + v |= SAFE_PE_DMACFG_FSENA /* failsafe enable */
  19769. + | SAFE_PE_DMACFG_GPRPCI /* gather ring on PCI */
  19770. + | SAFE_PE_DMACFG_SPRPCI /* scatter ring on PCI */
  19771. + | SAFE_PE_DMACFG_ESDESC /* endian-swap descriptors */
  19772. + | SAFE_PE_DMACFG_ESPDESC /* endian-swap part. desc's */
  19773. + | SAFE_PE_DMACFG_ESSA /* endian-swap SA's */
  19774. +#if 0
  19775. + | SAFE_PE_DMACFG_ESPACKET /* swap the packet data */
  19776. +#endif
  19777. + ;
  19778. + WRITE_REG(sc, SAFE_PE_DMACFG, v);
  19779. +
  19780. +#ifdef __BIG_ENDIAN
  19781. + /* tell the safenet that we are 4321 and not 1234 */
  19782. + WRITE_REG(sc, SAFE_ENDIAN, 0xe4e41b1b);
  19783. +#endif
  19784. +
  19785. + if (sc->sc_chiprev == SAFE_REV(1,0)) {
  19786. + /*
  19787. + * Avoid large PCI DMA transfers. Rev 1.0 has a bug where
  19788. + * "target mode transfers" done while the chip is DMA'ing
  19789. + * >1020 bytes cause the hardware to lockup. To avoid this
  19790. + * we reduce the max PCI transfer size and use small source
  19791. + * particle descriptors (<= 256 bytes).
  19792. + */
  19793. + WRITE_REG(sc, SAFE_DMA_CFG, 256);
  19794. + device_printf(sc->sc_dev,
  19795. + "Reduce max DMA size to %u words for rev %u.%u WAR\n",
  19796. + (unsigned) ((READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff),
  19797. + (unsigned) SAFE_REV_MAJ(sc->sc_chiprev),
  19798. + (unsigned) SAFE_REV_MIN(sc->sc_chiprev));
  19799. + sc->sc_max_dsize = 256;
  19800. + } else {
  19801. + sc->sc_max_dsize = SAFE_MAX_DSIZE;
  19802. + }
  19803. +
  19804. + /* NB: operands+results are overlaid */
  19805. + WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr);
  19806. + WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr);
  19807. + /*
  19808. + * Configure ring entry size and number of items in the ring.
  19809. + */
  19810. + KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0,
  19811. + ("PE ring entry not 32-bit aligned!"));
  19812. + dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t);
  19813. + WRITE_REG(sc, SAFE_PE_RINGCFG,
  19814. + (dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE);
  19815. + WRITE_REG(sc, SAFE_PE_RINGPOLL, 0); /* disable polling */
  19816. +
  19817. + WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr);
  19818. + WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr);
  19819. + WRITE_REG(sc, SAFE_PE_PARTSIZE,
  19820. + (SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART);
  19821. + /*
  19822. + * NB: destination particles are fixed size. We use
  19823. + * an mbuf cluster and require all results go to
  19824. + * clusters or smaller.
  19825. + */
  19826. + WRITE_REG(sc, SAFE_PE_PARTCFG, sc->sc_max_dsize);
  19827. +
  19828. + /* it's now safe to enable PE mode, do it */
  19829. + WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE);
  19830. +
  19831. + /*
  19832. + * Configure hardware to use level-triggered interrupts and
  19833. + * to interrupt after each descriptor is processed.
  19834. + */
  19835. + WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL);
  19836. + WRITE_REG(sc, SAFE_HI_CLR, 0xffffffff);
  19837. + WRITE_REG(sc, SAFE_HI_DESC_CNT, 1);
  19838. + WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR);
  19839. +}
  19840. +
  19841. +
  19842. +/*
  19843. + * Clean up after a chip crash.
  19844. + * It is assumed that the caller in splimp()
  19845. + */
  19846. +static void
  19847. +safe_cleanchip(struct safe_softc *sc)
  19848. +{
  19849. + DPRINTF(("%s()\n", __FUNCTION__));
  19850. +
  19851. + if (sc->sc_nqchip != 0) {
  19852. + struct safe_ringentry *re = sc->sc_back;
  19853. +
  19854. + while (re != sc->sc_front) {
  19855. + if (re->re_desc.d_csr != 0)
  19856. + safe_free_entry(sc, re);
  19857. + if (++re == sc->sc_ringtop)
  19858. + re = sc->sc_ring;
  19859. + }
  19860. + sc->sc_back = re;
  19861. + sc->sc_nqchip = 0;
  19862. + }
  19863. +}
  19864. +
  19865. +/*
  19866. + * free a safe_q
  19867. + * It is assumed that the caller is within splimp().
  19868. + */
  19869. +static int
  19870. +safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re)
  19871. +{
  19872. + struct cryptop *crp;
  19873. +
  19874. + DPRINTF(("%s()\n", __FUNCTION__));
  19875. +
  19876. + /*
  19877. + * Free header MCR
  19878. + */
  19879. + if ((re->re_dst_skb != NULL) && (re->re_src_skb != re->re_dst_skb))
  19880. +#ifdef NOTYET
  19881. + m_freem(re->re_dst_m);
  19882. +#else
  19883. + printk("%s,%d: SKB not supported\n", __FILE__, __LINE__);
  19884. +#endif
  19885. +
  19886. + crp = (struct cryptop *)re->re_crp;
  19887. +
  19888. + re->re_desc.d_csr = 0;
  19889. +
  19890. + crp->crp_etype = EFAULT;
  19891. + crypto_done(crp);
  19892. + return(0);
  19893. +}
  19894. +
  19895. +/*
  19896. + * Routine to reset the chip and clean up.
  19897. + * It is assumed that the caller is in splimp()
  19898. + */
  19899. +static void
  19900. +safe_totalreset(struct safe_softc *sc)
  19901. +{
  19902. + DPRINTF(("%s()\n", __FUNCTION__));
  19903. +
  19904. + safe_reset_board(sc);
  19905. + safe_init_board(sc);
  19906. + safe_cleanchip(sc);
  19907. +}
  19908. +
  19909. +/*
  19910. + * Is the operand suitable aligned for direct DMA. Each
  19911. + * segment must be aligned on a 32-bit boundary and all
  19912. + * but the last segment must be a multiple of 4 bytes.
  19913. + */
  19914. +static int
  19915. +safe_dmamap_aligned(struct safe_softc *sc, const struct safe_operand *op)
  19916. +{
  19917. + int i;
  19918. +
  19919. + DPRINTF(("%s()\n", __FUNCTION__));
  19920. +
  19921. + for (i = 0; i < op->nsegs; i++) {
  19922. + if (op->segs[i].ds_addr & 3)
  19923. + return (0);
  19924. + if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3))
  19925. + return (0);
  19926. + }
  19927. + return (1);
  19928. +}
  19929. +
  19930. +/*
  19931. + * Is the operand suitable for direct DMA as the destination
  19932. + * of an operation. The hardware requires that each ``particle''
  19933. + * but the last in an operation result have the same size. We
  19934. + * fix that size at SAFE_MAX_DSIZE bytes. This routine returns
  19935. + * 0 if some segment is not a multiple of of this size, 1 if all
  19936. + * segments are exactly this size, or 2 if segments are at worst
  19937. + * a multple of this size.
  19938. + */
  19939. +static int
  19940. +safe_dmamap_uniform(struct safe_softc *sc, const struct safe_operand *op)
  19941. +{
  19942. + int result = 1;
  19943. +
  19944. + DPRINTF(("%s()\n", __FUNCTION__));
  19945. +
  19946. + if (op->nsegs > 0) {
  19947. + int i;
  19948. +
  19949. + for (i = 0; i < op->nsegs-1; i++) {
  19950. + if (op->segs[i].ds_len % sc->sc_max_dsize)
  19951. + return (0);
  19952. + if (op->segs[i].ds_len != sc->sc_max_dsize)
  19953. + result = 2;
  19954. + }
  19955. + }
  19956. + return (result);
  19957. +}
  19958. +
  19959. +static int
  19960. +safe_kprocess(device_t dev, struct cryptkop *krp, int hint)
  19961. +{
  19962. + struct safe_softc *sc = device_get_softc(dev);
  19963. + struct safe_pkq *q;
  19964. + unsigned long flags;
  19965. +
  19966. + DPRINTF(("%s()\n", __FUNCTION__));
  19967. +
  19968. + if (sc == NULL) {
  19969. + krp->krp_status = EINVAL;
  19970. + goto err;
  19971. + }
  19972. +
  19973. + if (krp->krp_op != CRK_MOD_EXP) {
  19974. + krp->krp_status = EOPNOTSUPP;
  19975. + goto err;
  19976. + }
  19977. +
  19978. + q = (struct safe_pkq *) kmalloc(sizeof(*q), GFP_KERNEL);
  19979. + if (q == NULL) {
  19980. + krp->krp_status = ENOMEM;
  19981. + goto err;
  19982. + }
  19983. + memset(q, 0, sizeof(*q));
  19984. + q->pkq_krp = krp;
  19985. + INIT_LIST_HEAD(&q->pkq_list);
  19986. +
  19987. + spin_lock_irqsave(&sc->sc_pkmtx, flags);
  19988. + list_add_tail(&q->pkq_list, &sc->sc_pkq);
  19989. + safe_kfeed(sc);
  19990. + spin_unlock_irqrestore(&sc->sc_pkmtx, flags);
  19991. + return (0);
  19992. +
  19993. +err:
  19994. + crypto_kdone(krp);
  19995. + return (0);
  19996. +}
  19997. +
  19998. +#define SAFE_CRK_PARAM_BASE 0
  19999. +#define SAFE_CRK_PARAM_EXP 1
  20000. +#define SAFE_CRK_PARAM_MOD 2
  20001. +
  20002. +static int
  20003. +safe_kstart(struct safe_softc *sc)
  20004. +{
  20005. + struct cryptkop *krp = sc->sc_pkq_cur->pkq_krp;
  20006. + int exp_bits, mod_bits, base_bits;
  20007. + u_int32_t op, a_off, b_off, c_off, d_off;
  20008. +
  20009. + DPRINTF(("%s()\n", __FUNCTION__));
  20010. +
  20011. + if (krp->krp_iparams < 3 || krp->krp_oparams != 1) {
  20012. + krp->krp_status = EINVAL;
  20013. + return (1);
  20014. + }
  20015. +
  20016. + base_bits = safe_ksigbits(sc, &krp->krp_param[SAFE_CRK_PARAM_BASE]);
  20017. + if (base_bits > 2048)
  20018. + goto too_big;
  20019. + if (base_bits <= 0) /* 5. base not zero */
  20020. + goto too_small;
  20021. +
  20022. + exp_bits = safe_ksigbits(sc, &krp->krp_param[SAFE_CRK_PARAM_EXP]);
  20023. + if (exp_bits > 2048)
  20024. + goto too_big;
  20025. + if (exp_bits <= 0) /* 1. exponent word length > 0 */
  20026. + goto too_small; /* 4. exponent not zero */
  20027. +
  20028. + mod_bits = safe_ksigbits(sc, &krp->krp_param[SAFE_CRK_PARAM_MOD]);
  20029. + if (mod_bits > 2048)
  20030. + goto too_big;
  20031. + if (mod_bits <= 32) /* 2. modulus word length > 1 */
  20032. + goto too_small; /* 8. MSW of modulus != zero */
  20033. + if (mod_bits < exp_bits) /* 3 modulus len >= exponent len */
  20034. + goto too_small;
  20035. + if ((krp->krp_param[SAFE_CRK_PARAM_MOD].crp_p[0] & 1) == 0)
  20036. + goto bad_domain; /* 6. modulus is odd */
  20037. + if (mod_bits > krp->krp_param[krp->krp_iparams].crp_nbits)
  20038. + goto too_small; /* make sure result will fit */
  20039. +
  20040. + /* 7. modulus > base */
  20041. + if (mod_bits < base_bits)
  20042. + goto too_small;
  20043. + if (mod_bits == base_bits) {
  20044. + u_int8_t *basep, *modp;
  20045. + int i;
  20046. +
  20047. + basep = krp->krp_param[SAFE_CRK_PARAM_BASE].crp_p +
  20048. + ((base_bits + 7) / 8) - 1;
  20049. + modp = krp->krp_param[SAFE_CRK_PARAM_MOD].crp_p +
  20050. + ((mod_bits + 7) / 8) - 1;
  20051. +
  20052. + for (i = 0; i < (mod_bits + 7) / 8; i++, basep--, modp--) {
  20053. + if (*modp < *basep)
  20054. + goto too_small;
  20055. + if (*modp > *basep)
  20056. + break;
  20057. + }
  20058. + }
  20059. +
  20060. + /* And on the 9th step, he rested. */
  20061. +
  20062. + WRITE_REG(sc, SAFE_PK_A_LEN, (exp_bits + 31) / 32);
  20063. + WRITE_REG(sc, SAFE_PK_B_LEN, (mod_bits + 31) / 32);
  20064. + if (mod_bits > 1024) {
  20065. + op = SAFE_PK_FUNC_EXP4;
  20066. + a_off = 0x000;
  20067. + b_off = 0x100;
  20068. + c_off = 0x200;
  20069. + d_off = 0x300;
  20070. + } else {
  20071. + op = SAFE_PK_FUNC_EXP16;
  20072. + a_off = 0x000;
  20073. + b_off = 0x080;
  20074. + c_off = 0x100;
  20075. + d_off = 0x180;
  20076. + }
  20077. + sc->sc_pk_reslen = b_off - a_off;
  20078. + sc->sc_pk_resoff = d_off;
  20079. +
  20080. + /* A is exponent, B is modulus, C is base, D is result */
  20081. + safe_kload_reg(sc, a_off, b_off - a_off,
  20082. + &krp->krp_param[SAFE_CRK_PARAM_EXP]);
  20083. + WRITE_REG(sc, SAFE_PK_A_ADDR, a_off >> 2);
  20084. + safe_kload_reg(sc, b_off, b_off - a_off,
  20085. + &krp->krp_param[SAFE_CRK_PARAM_MOD]);
  20086. + WRITE_REG(sc, SAFE_PK_B_ADDR, b_off >> 2);
  20087. + safe_kload_reg(sc, c_off, b_off - a_off,
  20088. + &krp->krp_param[SAFE_CRK_PARAM_BASE]);
  20089. + WRITE_REG(sc, SAFE_PK_C_ADDR, c_off >> 2);
  20090. + WRITE_REG(sc, SAFE_PK_D_ADDR, d_off >> 2);
  20091. +
  20092. + WRITE_REG(sc, SAFE_PK_FUNC, op | SAFE_PK_FUNC_RUN);
  20093. +
  20094. + return (0);
  20095. +
  20096. +too_big:
  20097. + krp->krp_status = E2BIG;
  20098. + return (1);
  20099. +too_small:
  20100. + krp->krp_status = ERANGE;
  20101. + return (1);
  20102. +bad_domain:
  20103. + krp->krp_status = EDOM;
  20104. + return (1);
  20105. +}
  20106. +
  20107. +static int
  20108. +safe_ksigbits(struct safe_softc *sc, struct crparam *cr)
  20109. +{
  20110. + u_int plen = (cr->crp_nbits + 7) / 8;
  20111. + int i, sig = plen * 8;
  20112. + u_int8_t c, *p = cr->crp_p;
  20113. +
  20114. + DPRINTF(("%s()\n", __FUNCTION__));
  20115. +
  20116. + for (i = plen - 1; i >= 0; i--) {
  20117. + c = p[i];
  20118. + if (c != 0) {
  20119. + while ((c & 0x80) == 0) {
  20120. + sig--;
  20121. + c <<= 1;
  20122. + }
  20123. + break;
  20124. + }
  20125. + sig -= 8;
  20126. + }
  20127. + return (sig);
  20128. +}
  20129. +
  20130. +static void
  20131. +safe_kfeed(struct safe_softc *sc)
  20132. +{
  20133. + struct safe_pkq *q, *tmp;
  20134. +
  20135. + DPRINTF(("%s()\n", __FUNCTION__));
  20136. +
  20137. + if (list_empty(&sc->sc_pkq) && sc->sc_pkq_cur == NULL)
  20138. + return;
  20139. + if (sc->sc_pkq_cur != NULL)
  20140. + return;
  20141. + list_for_each_entry_safe(q, tmp, &sc->sc_pkq, pkq_list) {
  20142. + sc->sc_pkq_cur = q;
  20143. + list_del(&q->pkq_list);
  20144. + if (safe_kstart(sc) != 0) {
  20145. + crypto_kdone(q->pkq_krp);
  20146. + kfree(q);
  20147. + sc->sc_pkq_cur = NULL;
  20148. + } else {
  20149. + /* op started, start polling */
  20150. + mod_timer(&sc->sc_pkto, jiffies + 1);
  20151. + break;
  20152. + }
  20153. + }
  20154. +}
  20155. +
  20156. +static void
  20157. +safe_kpoll(unsigned long arg)
  20158. +{
  20159. + struct safe_softc *sc = NULL;
  20160. + struct safe_pkq *q;
  20161. + struct crparam *res;
  20162. + int i;
  20163. + u_int32_t buf[64];
  20164. + unsigned long flags;
  20165. +
  20166. + DPRINTF(("%s()\n", __FUNCTION__));
  20167. +
  20168. + if (arg >= SAFE_MAX_CHIPS)
  20169. + return;
  20170. + sc = safe_chip_idx[arg];
  20171. + if (!sc) {
  20172. + DPRINTF(("%s() - bad callback\n", __FUNCTION__));
  20173. + return;
  20174. + }
  20175. +
  20176. + spin_lock_irqsave(&sc->sc_pkmtx, flags);
  20177. + if (sc->sc_pkq_cur == NULL)
  20178. + goto out;
  20179. + if (READ_REG(sc, SAFE_PK_FUNC) & SAFE_PK_FUNC_RUN) {
  20180. + /* still running, check back later */
  20181. + mod_timer(&sc->sc_pkto, jiffies + 1);
  20182. + goto out;
  20183. + }
  20184. +
  20185. + q = sc->sc_pkq_cur;
  20186. + res = &q->pkq_krp->krp_param[q->pkq_krp->krp_iparams];
  20187. + bzero(buf, sizeof(buf));
  20188. + bzero(res->crp_p, (res->crp_nbits + 7) / 8);
  20189. + for (i = 0; i < sc->sc_pk_reslen >> 2; i++)
  20190. + buf[i] = le32_to_cpu(READ_REG(sc, SAFE_PK_RAM_START +
  20191. + sc->sc_pk_resoff + (i << 2)));
  20192. + bcopy(buf, res->crp_p, (res->crp_nbits + 7) / 8);
  20193. + /*
  20194. + * reduce the bits that need copying if possible
  20195. + */
  20196. + res->crp_nbits = min(res->crp_nbits,sc->sc_pk_reslen * 8);
  20197. + res->crp_nbits = safe_ksigbits(sc, res);
  20198. +
  20199. + for (i = SAFE_PK_RAM_START; i < SAFE_PK_RAM_END; i += 4)
  20200. + WRITE_REG(sc, i, 0);
  20201. +
  20202. + crypto_kdone(q->pkq_krp);
  20203. + kfree(q);
  20204. + sc->sc_pkq_cur = NULL;
  20205. +
  20206. + safe_kfeed(sc);
  20207. +out:
  20208. + spin_unlock_irqrestore(&sc->sc_pkmtx, flags);
  20209. +}
  20210. +
  20211. +static void
  20212. +safe_kload_reg(struct safe_softc *sc, u_int32_t off, u_int32_t len,
  20213. + struct crparam *n)
  20214. +{
  20215. + u_int32_t buf[64], i;
  20216. +
  20217. + DPRINTF(("%s()\n", __FUNCTION__));
  20218. +
  20219. + bzero(buf, sizeof(buf));
  20220. + bcopy(n->crp_p, buf, (n->crp_nbits + 7) / 8);
  20221. +
  20222. + for (i = 0; i < len >> 2; i++)
  20223. + WRITE_REG(sc, SAFE_PK_RAM_START + off + (i << 2),
  20224. + cpu_to_le32(buf[i]));
  20225. +}
  20226. +
  20227. +#ifdef SAFE_DEBUG
  20228. +static void
  20229. +safe_dump_dmastatus(struct safe_softc *sc, const char *tag)
  20230. +{
  20231. + printf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n"
  20232. + , tag
  20233. + , READ_REG(sc, SAFE_DMA_ENDIAN)
  20234. + , READ_REG(sc, SAFE_DMA_SRCADDR)
  20235. + , READ_REG(sc, SAFE_DMA_DSTADDR)
  20236. + , READ_REG(sc, SAFE_DMA_STAT)
  20237. + );
  20238. +}
  20239. +
  20240. +static void
  20241. +safe_dump_intrstate(struct safe_softc *sc, const char *tag)
  20242. +{
  20243. + printf("%s: HI_CFG 0x%x HI_MASK 0x%x HI_DESC_CNT 0x%x HU_STAT 0x%x HM_STAT 0x%x\n"
  20244. + , tag
  20245. + , READ_REG(sc, SAFE_HI_CFG)
  20246. + , READ_REG(sc, SAFE_HI_MASK)
  20247. + , READ_REG(sc, SAFE_HI_DESC_CNT)
  20248. + , READ_REG(sc, SAFE_HU_STAT)
  20249. + , READ_REG(sc, SAFE_HM_STAT)
  20250. + );
  20251. +}
  20252. +
  20253. +static void
  20254. +safe_dump_ringstate(struct safe_softc *sc, const char *tag)
  20255. +{
  20256. + u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT);
  20257. +
  20258. + /* NB: assume caller has lock on ring */
  20259. + printf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n",
  20260. + tag,
  20261. + estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S),
  20262. + (unsigned long)(sc->sc_back - sc->sc_ring),
  20263. + (unsigned long)(sc->sc_front - sc->sc_ring));
  20264. +}
  20265. +
  20266. +static void
  20267. +safe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re)
  20268. +{
  20269. + int ix, nsegs;
  20270. +
  20271. + ix = re - sc->sc_ring;
  20272. + printf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n"
  20273. + , tag
  20274. + , re, ix
  20275. + , re->re_desc.d_csr
  20276. + , re->re_desc.d_src
  20277. + , re->re_desc.d_dst
  20278. + , re->re_desc.d_sa
  20279. + , re->re_desc.d_len
  20280. + );
  20281. + if (re->re_src.nsegs > 1) {
  20282. + ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) /
  20283. + sizeof(struct safe_pdesc);
  20284. + for (nsegs = re->re_src.nsegs; nsegs; nsegs--) {
  20285. + printf(" spd[%u] %p: %p size %u flags %x"
  20286. + , ix, &sc->sc_spring[ix]
  20287. + , (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr
  20288. + , sc->sc_spring[ix].pd_size
  20289. + , sc->sc_spring[ix].pd_flags
  20290. + );
  20291. + if (sc->sc_spring[ix].pd_size == 0)
  20292. + printf(" (zero!)");
  20293. + printf("\n");
  20294. + if (++ix == SAFE_TOTAL_SPART)
  20295. + ix = 0;
  20296. + }
  20297. + }
  20298. + if (re->re_dst.nsegs > 1) {
  20299. + ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) /
  20300. + sizeof(struct safe_pdesc);
  20301. + for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) {
  20302. + printf(" dpd[%u] %p: %p flags %x\n"
  20303. + , ix, &sc->sc_dpring[ix]
  20304. + , (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr
  20305. + , sc->sc_dpring[ix].pd_flags
  20306. + );
  20307. + if (++ix == SAFE_TOTAL_DPART)
  20308. + ix = 0;
  20309. + }
  20310. + }
  20311. + printf("sa: cmd0 %08x cmd1 %08x staterec %x\n",
  20312. + re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec);
  20313. + printf("sa: key %x %x %x %x %x %x %x %x\n"
  20314. + , re->re_sa.sa_key[0]
  20315. + , re->re_sa.sa_key[1]
  20316. + , re->re_sa.sa_key[2]
  20317. + , re->re_sa.sa_key[3]
  20318. + , re->re_sa.sa_key[4]
  20319. + , re->re_sa.sa_key[5]
  20320. + , re->re_sa.sa_key[6]
  20321. + , re->re_sa.sa_key[7]
  20322. + );
  20323. + printf("sa: indigest %x %x %x %x %x\n"
  20324. + , re->re_sa.sa_indigest[0]
  20325. + , re->re_sa.sa_indigest[1]
  20326. + , re->re_sa.sa_indigest[2]
  20327. + , re->re_sa.sa_indigest[3]
  20328. + , re->re_sa.sa_indigest[4]
  20329. + );
  20330. + printf("sa: outdigest %x %x %x %x %x\n"
  20331. + , re->re_sa.sa_outdigest[0]
  20332. + , re->re_sa.sa_outdigest[1]
  20333. + , re->re_sa.sa_outdigest[2]
  20334. + , re->re_sa.sa_outdigest[3]
  20335. + , re->re_sa.sa_outdigest[4]
  20336. + );
  20337. + printf("sr: iv %x %x %x %x\n"
  20338. + , re->re_sastate.sa_saved_iv[0]
  20339. + , re->re_sastate.sa_saved_iv[1]
  20340. + , re->re_sastate.sa_saved_iv[2]
  20341. + , re->re_sastate.sa_saved_iv[3]
  20342. + );
  20343. + printf("sr: hashbc %u indigest %x %x %x %x %x\n"
  20344. + , re->re_sastate.sa_saved_hashbc
  20345. + , re->re_sastate.sa_saved_indigest[0]
  20346. + , re->re_sastate.sa_saved_indigest[1]
  20347. + , re->re_sastate.sa_saved_indigest[2]
  20348. + , re->re_sastate.sa_saved_indigest[3]
  20349. + , re->re_sastate.sa_saved_indigest[4]
  20350. + );
  20351. +}
  20352. +
  20353. +static void
  20354. +safe_dump_ring(struct safe_softc *sc, const char *tag)
  20355. +{
  20356. + unsigned long flags;
  20357. +
  20358. + spin_lock_irqsave(&sc->sc_ringmtx, flags);
  20359. + printf("\nSafeNet Ring State:\n");
  20360. + safe_dump_intrstate(sc, tag);
  20361. + safe_dump_dmastatus(sc, tag);
  20362. + safe_dump_ringstate(sc, tag);
  20363. + if (sc->sc_nqchip) {
  20364. + struct safe_ringentry *re = sc->sc_back;
  20365. + do {
  20366. + safe_dump_request(sc, tag, re);
  20367. + if (++re == sc->sc_ringtop)
  20368. + re = sc->sc_ring;
  20369. + } while (re != sc->sc_front);
  20370. + }
  20371. + spin_unlock_irqrestore(&sc->sc_ringmtx, flags);
  20372. +}
  20373. +#endif /* SAFE_DEBUG */
  20374. +
  20375. +
  20376. +static int safe_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  20377. +{
  20378. + struct safe_softc *sc = NULL;
  20379. + u32 mem_start, mem_len, cmd;
  20380. + int i, rc, devinfo;
  20381. + dma_addr_t raddr;
  20382. + static int num_chips = 0;
  20383. +
  20384. + DPRINTF(("%s()\n", __FUNCTION__));
  20385. +
  20386. + if (pci_enable_device(dev) < 0)
  20387. + return(-ENODEV);
  20388. +
  20389. + if (!dev->irq) {
  20390. + printk("safe: found device with no IRQ assigned. check BIOS settings!");
  20391. + pci_disable_device(dev);
  20392. + return(-ENODEV);
  20393. + }
  20394. +
  20395. + if (pci_set_mwi(dev)) {
  20396. + printk("safe: pci_set_mwi failed!");
  20397. + return(-ENODEV);
  20398. + }
  20399. +
  20400. + sc = (struct safe_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  20401. + if (!sc)
  20402. + return(-ENOMEM);
  20403. + memset(sc, 0, sizeof(*sc));
  20404. +
  20405. + softc_device_init(sc, "safe", num_chips, safe_methods);
  20406. +
  20407. + sc->sc_irq = -1;
  20408. + sc->sc_cid = -1;
  20409. + sc->sc_pcidev = dev;
  20410. + if (num_chips < SAFE_MAX_CHIPS) {
  20411. + safe_chip_idx[device_get_unit(sc->sc_dev)] = sc;
  20412. + num_chips++;
  20413. + }
  20414. +
  20415. + INIT_LIST_HEAD(&sc->sc_pkq);
  20416. + spin_lock_init(&sc->sc_pkmtx);
  20417. +
  20418. + pci_set_drvdata(sc->sc_pcidev, sc);
  20419. +
  20420. + /* we read its hardware registers as memory */
  20421. + mem_start = pci_resource_start(sc->sc_pcidev, 0);
  20422. + mem_len = pci_resource_len(sc->sc_pcidev, 0);
  20423. +
  20424. + sc->sc_base_addr = (ocf_iomem_t) ioremap(mem_start, mem_len);
  20425. + if (!sc->sc_base_addr) {
  20426. + device_printf(sc->sc_dev, "failed to ioremap 0x%x-0x%x\n",
  20427. + mem_start, mem_start + mem_len - 1);
  20428. + goto out;
  20429. + }
  20430. +
  20431. + /* fix up the bus size */
  20432. + if (pci_set_dma_mask(sc->sc_pcidev, DMA_32BIT_MASK)) {
  20433. + device_printf(sc->sc_dev, "No usable DMA configuration, aborting.\n");
  20434. + goto out;
  20435. + }
  20436. + if (pci_set_consistent_dma_mask(sc->sc_pcidev, DMA_32BIT_MASK)) {
  20437. + device_printf(sc->sc_dev, "No usable consistent DMA configuration, aborting.\n");
  20438. + goto out;
  20439. + }
  20440. +
  20441. + pci_set_master(sc->sc_pcidev);
  20442. +
  20443. + pci_read_config_dword(sc->sc_pcidev, PCI_COMMAND, &cmd);
  20444. +
  20445. + if (!(cmd & PCI_COMMAND_MEMORY)) {
  20446. + device_printf(sc->sc_dev, "failed to enable memory mapping\n");
  20447. + goto out;
  20448. + }
  20449. +
  20450. + if (!(cmd & PCI_COMMAND_MASTER)) {
  20451. + device_printf(sc->sc_dev, "failed to enable bus mastering\n");
  20452. + goto out;
  20453. + }
  20454. +
  20455. + rc = request_irq(dev->irq, safe_intr, IRQF_SHARED, "safe", sc);
  20456. + if (rc) {
  20457. + device_printf(sc->sc_dev, "failed to hook irq %d\n", sc->sc_irq);
  20458. + goto out;
  20459. + }
  20460. + sc->sc_irq = dev->irq;
  20461. +
  20462. + sc->sc_chiprev = READ_REG(sc, SAFE_DEVINFO) &
  20463. + (SAFE_DEVINFO_REV_MAJ | SAFE_DEVINFO_REV_MIN);
  20464. +
  20465. + /*
  20466. + * Allocate packet engine descriptors.
  20467. + */
  20468. + sc->sc_ringalloc.dma_vaddr = pci_alloc_consistent(sc->sc_pcidev,
  20469. + SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
  20470. + &sc->sc_ringalloc.dma_paddr);
  20471. + if (!sc->sc_ringalloc.dma_vaddr) {
  20472. + device_printf(sc->sc_dev, "cannot allocate PE descriptor ring\n");
  20473. + goto out;
  20474. + }
  20475. +
  20476. + /*
  20477. + * Hookup the static portion of all our data structures.
  20478. + */
  20479. + sc->sc_ring = (struct safe_ringentry *) sc->sc_ringalloc.dma_vaddr;
  20480. + sc->sc_ringtop = sc->sc_ring + SAFE_MAX_NQUEUE;
  20481. + sc->sc_front = sc->sc_ring;
  20482. + sc->sc_back = sc->sc_ring;
  20483. + raddr = sc->sc_ringalloc.dma_paddr;
  20484. + bzero(sc->sc_ring, SAFE_MAX_NQUEUE * sizeof(struct safe_ringentry));
  20485. + for (i = 0; i < SAFE_MAX_NQUEUE; i++) {
  20486. + struct safe_ringentry *re = &sc->sc_ring[i];
  20487. +
  20488. + re->re_desc.d_sa = raddr +
  20489. + offsetof(struct safe_ringentry, re_sa);
  20490. + re->re_sa.sa_staterec = raddr +
  20491. + offsetof(struct safe_ringentry, re_sastate);
  20492. +
  20493. + raddr += sizeof (struct safe_ringentry);
  20494. + }
  20495. + spin_lock_init(&sc->sc_ringmtx);
  20496. +
  20497. + /*
  20498. + * Allocate scatter and gather particle descriptors.
  20499. + */
  20500. + sc->sc_spalloc.dma_vaddr = pci_alloc_consistent(sc->sc_pcidev,
  20501. + SAFE_TOTAL_SPART * sizeof (struct safe_pdesc),
  20502. + &sc->sc_spalloc.dma_paddr);
  20503. + if (!sc->sc_spalloc.dma_vaddr) {
  20504. + device_printf(sc->sc_dev, "cannot allocate source particle descriptor ring\n");
  20505. + goto out;
  20506. + }
  20507. + sc->sc_spring = (struct safe_pdesc *) sc->sc_spalloc.dma_vaddr;
  20508. + sc->sc_springtop = sc->sc_spring + SAFE_TOTAL_SPART;
  20509. + sc->sc_spfree = sc->sc_spring;
  20510. + bzero(sc->sc_spring, SAFE_TOTAL_SPART * sizeof(struct safe_pdesc));
  20511. +
  20512. + sc->sc_dpalloc.dma_vaddr = pci_alloc_consistent(sc->sc_pcidev,
  20513. + SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
  20514. + &sc->sc_dpalloc.dma_paddr);
  20515. + if (!sc->sc_dpalloc.dma_vaddr) {
  20516. + device_printf(sc->sc_dev, "cannot allocate destination particle descriptor ring\n");
  20517. + goto out;
  20518. + }
  20519. + sc->sc_dpring = (struct safe_pdesc *) sc->sc_dpalloc.dma_vaddr;
  20520. + sc->sc_dpringtop = sc->sc_dpring + SAFE_TOTAL_DPART;
  20521. + sc->sc_dpfree = sc->sc_dpring;
  20522. + bzero(sc->sc_dpring, SAFE_TOTAL_DPART * sizeof(struct safe_pdesc));
  20523. +
  20524. + sc->sc_cid = crypto_get_driverid(softc_get_device(sc), CRYPTOCAP_F_HARDWARE);
  20525. + if (sc->sc_cid < 0) {
  20526. + device_printf(sc->sc_dev, "could not get crypto driver id\n");
  20527. + goto out;
  20528. + }
  20529. +
  20530. + printf("%s:", device_get_nameunit(sc->sc_dev));
  20531. +
  20532. + devinfo = READ_REG(sc, SAFE_DEVINFO);
  20533. + if (devinfo & SAFE_DEVINFO_RNG) {
  20534. + sc->sc_flags |= SAFE_FLAGS_RNG;
  20535. + printf(" rng");
  20536. + }
  20537. + if (devinfo & SAFE_DEVINFO_PKEY) {
  20538. + printf(" key");
  20539. + sc->sc_flags |= SAFE_FLAGS_KEY;
  20540. + crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0);
  20541. +#if 0
  20542. + crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0);
  20543. +#endif
  20544. + init_timer(&sc->sc_pkto);
  20545. + sc->sc_pkto.function = safe_kpoll;
  20546. + sc->sc_pkto.data = (unsigned long) device_get_unit(sc->sc_dev);
  20547. + }
  20548. + if (devinfo & SAFE_DEVINFO_DES) {
  20549. + printf(" des/3des");
  20550. + crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  20551. + crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  20552. + }
  20553. + if (devinfo & SAFE_DEVINFO_AES) {
  20554. + printf(" aes");
  20555. + crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  20556. + }
  20557. + if (devinfo & SAFE_DEVINFO_MD5) {
  20558. + printf(" md5");
  20559. + crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  20560. + }
  20561. + if (devinfo & SAFE_DEVINFO_SHA1) {
  20562. + printf(" sha1");
  20563. + crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  20564. + }
  20565. + printf(" null");
  20566. + crypto_register(sc->sc_cid, CRYPTO_NULL_CBC, 0, 0);
  20567. + crypto_register(sc->sc_cid, CRYPTO_NULL_HMAC, 0, 0);
  20568. + /* XXX other supported algorithms */
  20569. + printf("\n");
  20570. +
  20571. + safe_reset_board(sc); /* reset h/w */
  20572. + safe_init_board(sc); /* init h/w */
  20573. +
  20574. +#if defined(CONFIG_OCF_RANDOMHARVEST) && !defined(SAFE_NO_RNG)
  20575. + if (sc->sc_flags & SAFE_FLAGS_RNG) {
  20576. + safe_rng_init(sc);
  20577. + crypto_rregister(sc->sc_cid, safe_read_random, sc);
  20578. + }
  20579. +#endif /* SAFE_NO_RNG */
  20580. +
  20581. + return (0);
  20582. +
  20583. +out:
  20584. + if (sc->sc_cid >= 0)
  20585. + crypto_unregister_all(sc->sc_cid);
  20586. + if (sc->sc_irq != -1)
  20587. + free_irq(sc->sc_irq, sc);
  20588. + if (sc->sc_ringalloc.dma_vaddr)
  20589. + pci_free_consistent(sc->sc_pcidev,
  20590. + SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
  20591. + sc->sc_ringalloc.dma_vaddr, sc->sc_ringalloc.dma_paddr);
  20592. + if (sc->sc_spalloc.dma_vaddr)
  20593. + pci_free_consistent(sc->sc_pcidev,
  20594. + SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
  20595. + sc->sc_spalloc.dma_vaddr, sc->sc_spalloc.dma_paddr);
  20596. + if (sc->sc_dpalloc.dma_vaddr)
  20597. + pci_free_consistent(sc->sc_pcidev,
  20598. + SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
  20599. + sc->sc_dpalloc.dma_vaddr, sc->sc_dpalloc.dma_paddr);
  20600. + kfree(sc);
  20601. + return(-ENODEV);
  20602. +}
  20603. +
  20604. +static void safe_remove(struct pci_dev *dev)
  20605. +{
  20606. + struct safe_softc *sc = pci_get_drvdata(dev);
  20607. +
  20608. + DPRINTF(("%s()\n", __FUNCTION__));
  20609. +
  20610. + /* XXX wait/abort active ops */
  20611. +
  20612. + WRITE_REG(sc, SAFE_HI_MASK, 0); /* disable interrupts */
  20613. +
  20614. + del_timer_sync(&sc->sc_pkto);
  20615. +
  20616. + crypto_unregister_all(sc->sc_cid);
  20617. +
  20618. + safe_cleanchip(sc);
  20619. +
  20620. + if (sc->sc_irq != -1)
  20621. + free_irq(sc->sc_irq, sc);
  20622. + if (sc->sc_ringalloc.dma_vaddr)
  20623. + pci_free_consistent(sc->sc_pcidev,
  20624. + SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
  20625. + sc->sc_ringalloc.dma_vaddr, sc->sc_ringalloc.dma_paddr);
  20626. + if (sc->sc_spalloc.dma_vaddr)
  20627. + pci_free_consistent(sc->sc_pcidev,
  20628. + SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
  20629. + sc->sc_spalloc.dma_vaddr, sc->sc_spalloc.dma_paddr);
  20630. + if (sc->sc_dpalloc.dma_vaddr)
  20631. + pci_free_consistent(sc->sc_pcidev,
  20632. + SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
  20633. + sc->sc_dpalloc.dma_vaddr, sc->sc_dpalloc.dma_paddr);
  20634. + sc->sc_irq = -1;
  20635. + sc->sc_ringalloc.dma_vaddr = NULL;
  20636. + sc->sc_spalloc.dma_vaddr = NULL;
  20637. + sc->sc_dpalloc.dma_vaddr = NULL;
  20638. +}
  20639. +
  20640. +static struct pci_device_id safe_pci_tbl[] = {
  20641. + { PCI_VENDOR_SAFENET, PCI_PRODUCT_SAFEXCEL,
  20642. + PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  20643. + { },
  20644. +};
  20645. +MODULE_DEVICE_TABLE(pci, safe_pci_tbl);
  20646. +
  20647. +static struct pci_driver safe_driver = {
  20648. + .name = "safe",
  20649. + .id_table = safe_pci_tbl,
  20650. + .probe = safe_probe,
  20651. + .remove = safe_remove,
  20652. + /* add PM stuff here one day */
  20653. +};
  20654. +
  20655. +static int __init safe_init (void)
  20656. +{
  20657. + struct safe_softc *sc = NULL;
  20658. + int rc;
  20659. +
  20660. + DPRINTF(("%s(%p)\n", __FUNCTION__, safe_init));
  20661. +
  20662. + rc = pci_register_driver(&safe_driver);
  20663. + pci_register_driver_compat(&safe_driver, rc);
  20664. +
  20665. + return rc;
  20666. +}
  20667. +
  20668. +static void __exit safe_exit (void)
  20669. +{
  20670. + pci_unregister_driver(&safe_driver);
  20671. +}
  20672. +
  20673. +module_init(safe_init);
  20674. +module_exit(safe_exit);
  20675. +
  20676. +MODULE_LICENSE("BSD");
  20677. +MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
  20678. +MODULE_DESCRIPTION("OCF driver for safenet PCI crypto devices");
  20679. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/safereg.h linux-2.6.30/crypto/ocf/safe/safereg.h
  20680. --- linux-2.6.30.orig/crypto/ocf/safe/safereg.h 1970-01-01 01:00:00.000000000 +0100
  20681. +++ linux-2.6.30/crypto/ocf/safe/safereg.h 2009-06-11 10:55:27.000000000 +0200
  20682. @@ -0,0 +1,421 @@
  20683. +/*-
  20684. + * Copyright (c) 2003 Sam Leffler, Errno Consulting
  20685. + * Copyright (c) 2003 Global Technology Associates, Inc.
  20686. + * All rights reserved.
  20687. + *
  20688. + * Redistribution and use in source and binary forms, with or without
  20689. + * modification, are permitted provided that the following conditions
  20690. + * are met:
  20691. + * 1. Redistributions of source code must retain the above copyright
  20692. + * notice, this list of conditions and the following disclaimer.
  20693. + * 2. Redistributions in binary form must reproduce the above copyright
  20694. + * notice, this list of conditions and the following disclaimer in the
  20695. + * documentation and/or other materials provided with the distribution.
  20696. + *
  20697. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20698. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20699. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20700. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20701. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20702. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20703. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20704. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20705. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  20706. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  20707. + * SUCH DAMAGE.
  20708. + *
  20709. + * $FreeBSD: src/sys/dev/safe/safereg.h,v 1.1 2003/07/21 21:46:07 sam Exp $
  20710. + */
  20711. +#ifndef _SAFE_SAFEREG_H_
  20712. +#define _SAFE_SAFEREG_H_
  20713. +
  20714. +/*
  20715. + * Register definitions for SafeNet SafeXcel-1141 crypto device.
  20716. + * Definitions from revision 1.3 (Nov 6 2002) of the User's Manual.
  20717. + */
  20718. +
  20719. +#define BS_BAR 0x10 /* DMA base address register */
  20720. +#define BS_TRDY_TIMEOUT 0x40 /* TRDY timeout */
  20721. +#define BS_RETRY_TIMEOUT 0x41 /* DMA retry timeout */
  20722. +
  20723. +#define PCI_VENDOR_SAFENET 0x16ae /* SafeNet, Inc. */
  20724. +
  20725. +/* SafeNet */
  20726. +#define PCI_PRODUCT_SAFEXCEL 0x1141 /* 1141 */
  20727. +
  20728. +#define SAFE_PE_CSR 0x0000 /* Packet Enginge Ctrl/Status */
  20729. +#define SAFE_PE_SRC 0x0004 /* Packet Engine Source */
  20730. +#define SAFE_PE_DST 0x0008 /* Packet Engine Destination */
  20731. +#define SAFE_PE_SA 0x000c /* Packet Engine SA */
  20732. +#define SAFE_PE_LEN 0x0010 /* Packet Engine Length */
  20733. +#define SAFE_PE_DMACFG 0x0040 /* Packet Engine DMA Configuration */
  20734. +#define SAFE_PE_DMASTAT 0x0044 /* Packet Engine DMA Status */
  20735. +#define SAFE_PE_PDRBASE 0x0048 /* Packet Engine Descriptor Ring Base */
  20736. +#define SAFE_PE_RDRBASE 0x004c /* Packet Engine Result Ring Base */
  20737. +#define SAFE_PE_RINGCFG 0x0050 /* Packet Engine Ring Configuration */
  20738. +#define SAFE_PE_RINGPOLL 0x0054 /* Packet Engine Ring Poll */
  20739. +#define SAFE_PE_IRNGSTAT 0x0058 /* Packet Engine Internal Ring Status */
  20740. +#define SAFE_PE_ERNGSTAT 0x005c /* Packet Engine External Ring Status */
  20741. +#define SAFE_PE_IOTHRESH 0x0060 /* Packet Engine I/O Threshold */
  20742. +#define SAFE_PE_GRNGBASE 0x0064 /* Packet Engine Gather Ring Base */
  20743. +#define SAFE_PE_SRNGBASE 0x0068 /* Packet Engine Scatter Ring Base */
  20744. +#define SAFE_PE_PARTSIZE 0x006c /* Packet Engine Particlar Ring Size */
  20745. +#define SAFE_PE_PARTCFG 0x0070 /* Packet Engine Particle Ring Config */
  20746. +#define SAFE_CRYPTO_CTRL 0x0080 /* Crypto Control */
  20747. +#define SAFE_DEVID 0x0084 /* Device ID */
  20748. +#define SAFE_DEVINFO 0x0088 /* Device Info */
  20749. +#define SAFE_HU_STAT 0x00a0 /* Host Unmasked Status */
  20750. +#define SAFE_HM_STAT 0x00a4 /* Host Masked Status (read-only) */
  20751. +#define SAFE_HI_CLR 0x00a4 /* Host Clear Interrupt (write-only) */
  20752. +#define SAFE_HI_MASK 0x00a8 /* Host Mask Control */
  20753. +#define SAFE_HI_CFG 0x00ac /* Interrupt Configuration */
  20754. +#define SAFE_HI_RD_DESCR 0x00b4 /* Force Descriptor Read */
  20755. +#define SAFE_HI_DESC_CNT 0x00b8 /* Host Descriptor Done Count */
  20756. +#define SAFE_DMA_ENDIAN 0x00c0 /* Master Endian Status */
  20757. +#define SAFE_DMA_SRCADDR 0x00c4 /* DMA Source Address Status */
  20758. +#define SAFE_DMA_DSTADDR 0x00c8 /* DMA Destination Address Status */
  20759. +#define SAFE_DMA_STAT 0x00cc /* DMA Current Status */
  20760. +#define SAFE_DMA_CFG 0x00d4 /* DMA Configuration/Status */
  20761. +#define SAFE_ENDIAN 0x00e0 /* Endian Configuration */
  20762. +#define SAFE_PK_A_ADDR 0x0800 /* Public Key A Address */
  20763. +#define SAFE_PK_B_ADDR 0x0804 /* Public Key B Address */
  20764. +#define SAFE_PK_C_ADDR 0x0808 /* Public Key C Address */
  20765. +#define SAFE_PK_D_ADDR 0x080c /* Public Key D Address */
  20766. +#define SAFE_PK_A_LEN 0x0810 /* Public Key A Length */
  20767. +#define SAFE_PK_B_LEN 0x0814 /* Public Key B Length */
  20768. +#define SAFE_PK_SHIFT 0x0818 /* Public Key Shift */
  20769. +#define SAFE_PK_FUNC 0x081c /* Public Key Function */
  20770. +#define SAFE_PK_RAM_START 0x1000 /* Public Key RAM start address */
  20771. +#define SAFE_PK_RAM_END 0x1fff /* Public Key RAM end address */
  20772. +
  20773. +#define SAFE_RNG_OUT 0x0100 /* RNG Output */
  20774. +#define SAFE_RNG_STAT 0x0104 /* RNG Status */
  20775. +#define SAFE_RNG_CTRL 0x0108 /* RNG Control */
  20776. +#define SAFE_RNG_A 0x010c /* RNG A */
  20777. +#define SAFE_RNG_B 0x0110 /* RNG B */
  20778. +#define SAFE_RNG_X_LO 0x0114 /* RNG X [31:0] */
  20779. +#define SAFE_RNG_X_MID 0x0118 /* RNG X [63:32] */
  20780. +#define SAFE_RNG_X_HI 0x011c /* RNG X [80:64] */
  20781. +#define SAFE_RNG_X_CNTR 0x0120 /* RNG Counter */
  20782. +#define SAFE_RNG_ALM_CNT 0x0124 /* RNG Alarm Count */
  20783. +#define SAFE_RNG_CNFG 0x0128 /* RNG Configuration */
  20784. +#define SAFE_RNG_LFSR1_LO 0x012c /* RNG LFSR1 [31:0] */
  20785. +#define SAFE_RNG_LFSR1_HI 0x0130 /* RNG LFSR1 [47:32] */
  20786. +#define SAFE_RNG_LFSR2_LO 0x0134 /* RNG LFSR1 [31:0] */
  20787. +#define SAFE_RNG_LFSR2_HI 0x0138 /* RNG LFSR1 [47:32] */
  20788. +
  20789. +#define SAFE_PE_CSR_READY 0x00000001 /* ready for processing */
  20790. +#define SAFE_PE_CSR_DONE 0x00000002 /* h/w completed processing */
  20791. +#define SAFE_PE_CSR_LOADSA 0x00000004 /* load SA digests */
  20792. +#define SAFE_PE_CSR_HASHFINAL 0x00000010 /* do hash pad & write result */
  20793. +#define SAFE_PE_CSR_SABUSID 0x000000c0 /* bus id for SA */
  20794. +#define SAFE_PE_CSR_SAPCI 0x00000040 /* PCI bus id for SA */
  20795. +#define SAFE_PE_CSR_NXTHDR 0x0000ff00 /* next hdr value for IPsec */
  20796. +#define SAFE_PE_CSR_FPAD 0x0000ff00 /* fixed pad for basic ops */
  20797. +#define SAFE_PE_CSR_STATUS 0x00ff0000 /* operation result status */
  20798. +#define SAFE_PE_CSR_AUTH_FAIL 0x00010000 /* ICV mismatch (inbound) */
  20799. +#define SAFE_PE_CSR_PAD_FAIL 0x00020000 /* pad verify fail (inbound) */
  20800. +#define SAFE_PE_CSR_SEQ_FAIL 0x00040000 /* sequence number (inbound) */
  20801. +#define SAFE_PE_CSR_XERROR 0x00080000 /* extended error follows */
  20802. +#define SAFE_PE_CSR_XECODE 0x00f00000 /* extended error code */
  20803. +#define SAFE_PE_CSR_XECODE_S 20
  20804. +#define SAFE_PE_CSR_XECODE_BADCMD 0 /* invalid command */
  20805. +#define SAFE_PE_CSR_XECODE_BADALG 1 /* invalid algorithm */
  20806. +#define SAFE_PE_CSR_XECODE_ALGDIS 2 /* algorithm disabled */
  20807. +#define SAFE_PE_CSR_XECODE_ZEROLEN 3 /* zero packet length */
  20808. +#define SAFE_PE_CSR_XECODE_DMAERR 4 /* bus DMA error */
  20809. +#define SAFE_PE_CSR_XECODE_PIPEABORT 5 /* secondary bus DMA error */
  20810. +#define SAFE_PE_CSR_XECODE_BADSPI 6 /* IPsec SPI mismatch */
  20811. +#define SAFE_PE_CSR_XECODE_TIMEOUT 10 /* failsafe timeout */
  20812. +#define SAFE_PE_CSR_PAD 0xff000000 /* ESP padding control/status */
  20813. +#define SAFE_PE_CSR_PAD_MIN 0x00000000 /* minimum IPsec padding */
  20814. +#define SAFE_PE_CSR_PAD_16 0x08000000 /* pad to 16-byte boundary */
  20815. +#define SAFE_PE_CSR_PAD_32 0x10000000 /* pad to 32-byte boundary */
  20816. +#define SAFE_PE_CSR_PAD_64 0x20000000 /* pad to 64-byte boundary */
  20817. +#define SAFE_PE_CSR_PAD_128 0x40000000 /* pad to 128-byte boundary */
  20818. +#define SAFE_PE_CSR_PAD_256 0x80000000 /* pad to 256-byte boundary */
  20819. +
  20820. +/*
  20821. + * Check the CSR to see if the PE has returned ownership to
  20822. + * the host. Note that before processing a descriptor this
  20823. + * must be done followed by a check of the SAFE_PE_LEN register
  20824. + * status bits to avoid premature processing of a descriptor
  20825. + * on its way back to the host.
  20826. + */
  20827. +#define SAFE_PE_CSR_IS_DONE(_csr) \
  20828. + (((_csr) & (SAFE_PE_CSR_READY | SAFE_PE_CSR_DONE)) == SAFE_PE_CSR_DONE)
  20829. +
  20830. +#define SAFE_PE_LEN_LENGTH 0x000fffff /* total length (bytes) */
  20831. +#define SAFE_PE_LEN_READY 0x00400000 /* ready for processing */
  20832. +#define SAFE_PE_LEN_DONE 0x00800000 /* h/w completed processing */
  20833. +#define SAFE_PE_LEN_BYPASS 0xff000000 /* bypass offset (bytes) */
  20834. +#define SAFE_PE_LEN_BYPASS_S 24
  20835. +
  20836. +#define SAFE_PE_LEN_IS_DONE(_len) \
  20837. + (((_len) & (SAFE_PE_LEN_READY | SAFE_PE_LEN_DONE)) == SAFE_PE_LEN_DONE)
  20838. +
  20839. +/* NB: these apply to HU_STAT, HM_STAT, HI_CLR, and HI_MASK */
  20840. +#define SAFE_INT_PE_CDONE 0x00000002 /* PE context done */
  20841. +#define SAFE_INT_PE_DDONE 0x00000008 /* PE descriptor done */
  20842. +#define SAFE_INT_PE_ERROR 0x00000010 /* PE error */
  20843. +#define SAFE_INT_PE_ODONE 0x00000020 /* PE operation done */
  20844. +
  20845. +#define SAFE_HI_CFG_PULSE 0x00000001 /* use pulse interrupt */
  20846. +#define SAFE_HI_CFG_LEVEL 0x00000000 /* use level interrupt */
  20847. +#define SAFE_HI_CFG_AUTOCLR 0x00000002 /* auto-clear pulse interrupt */
  20848. +
  20849. +#define SAFE_ENDIAN_PASS 0x000000e4 /* straight pass-thru */
  20850. +#define SAFE_ENDIAN_SWAB 0x0000001b /* swap bytes in 32-bit word */
  20851. +
  20852. +#define SAFE_PE_DMACFG_PERESET 0x00000001 /* reset packet engine */
  20853. +#define SAFE_PE_DMACFG_PDRRESET 0x00000002 /* reset PDR counters/ptrs */
  20854. +#define SAFE_PE_DMACFG_SGRESET 0x00000004 /* reset scatter/gather cache */
  20855. +#define SAFE_PE_DMACFG_FSENA 0x00000008 /* enable failsafe reset */
  20856. +#define SAFE_PE_DMACFG_PEMODE 0x00000100 /* packet engine mode */
  20857. +#define SAFE_PE_DMACFG_SAPREC 0x00000200 /* SA precedes packet */
  20858. +#define SAFE_PE_DMACFG_PKFOLL 0x00000400 /* packet follows descriptor */
  20859. +#define SAFE_PE_DMACFG_GPRBID 0x00003000 /* gather particle ring busid */
  20860. +#define SAFE_PE_DMACFG_GPRPCI 0x00001000 /* PCI gather particle ring */
  20861. +#define SAFE_PE_DMACFG_SPRBID 0x0000c000 /* scatter part. ring busid */
  20862. +#define SAFE_PE_DMACFG_SPRPCI 0x00004000 /* PCI scatter part. ring */
  20863. +#define SAFE_PE_DMACFG_ESDESC 0x00010000 /* endian swap descriptors */
  20864. +#define SAFE_PE_DMACFG_ESSA 0x00020000 /* endian swap SA data */
  20865. +#define SAFE_PE_DMACFG_ESPACKET 0x00040000 /* endian swap packet data */
  20866. +#define SAFE_PE_DMACFG_ESPDESC 0x00080000 /* endian swap particle desc. */
  20867. +#define SAFE_PE_DMACFG_NOPDRUP 0x00100000 /* supp. PDR ownership update */
  20868. +#define SAFE_PD_EDMACFG_PCIMODE 0x01000000 /* PCI target mode */
  20869. +
  20870. +#define SAFE_PE_DMASTAT_PEIDONE 0x00000001 /* PE core input done */
  20871. +#define SAFE_PE_DMASTAT_PEODONE 0x00000002 /* PE core output done */
  20872. +#define SAFE_PE_DMASTAT_ENCDONE 0x00000004 /* encryption done */
  20873. +#define SAFE_PE_DMASTAT_IHDONE 0x00000008 /* inner hash done */
  20874. +#define SAFE_PE_DMASTAT_OHDONE 0x00000010 /* outer hash (HMAC) done */
  20875. +#define SAFE_PE_DMASTAT_PADFLT 0x00000020 /* crypto pad fault */
  20876. +#define SAFE_PE_DMASTAT_ICVFLT 0x00000040 /* ICV fault */
  20877. +#define SAFE_PE_DMASTAT_SPIMIS 0x00000080 /* SPI mismatch */
  20878. +#define SAFE_PE_DMASTAT_CRYPTO 0x00000100 /* crypto engine timeout */
  20879. +#define SAFE_PE_DMASTAT_CQACT 0x00000200 /* command queue active */
  20880. +#define SAFE_PE_DMASTAT_IRACT 0x00000400 /* input request active */
  20881. +#define SAFE_PE_DMASTAT_ORACT 0x00000800 /* output request active */
  20882. +#define SAFE_PE_DMASTAT_PEISIZE 0x003ff000 /* PE input size:32-bit words */
  20883. +#define SAFE_PE_DMASTAT_PEOSIZE 0xffc00000 /* PE out. size:32-bit words */
  20884. +
  20885. +#define SAFE_PE_RINGCFG_SIZE 0x000003ff /* ring size (descriptors) */
  20886. +#define SAFE_PE_RINGCFG_OFFSET 0xffff0000 /* offset btw desc's (dwords) */
  20887. +#define SAFE_PE_RINGCFG_OFFSET_S 16
  20888. +
  20889. +#define SAFE_PE_RINGPOLL_POLL 0x00000fff /* polling frequency/divisor */
  20890. +#define SAFE_PE_RINGPOLL_RETRY 0x03ff0000 /* polling frequency/divisor */
  20891. +#define SAFE_PE_RINGPOLL_CONT 0x80000000 /* continuously poll */
  20892. +
  20893. +#define SAFE_PE_IRNGSTAT_CQAVAIL 0x00000001 /* command queue available */
  20894. +
  20895. +#define SAFE_PE_ERNGSTAT_NEXT 0x03ff0000 /* index of next packet desc. */
  20896. +#define SAFE_PE_ERNGSTAT_NEXT_S 16
  20897. +
  20898. +#define SAFE_PE_IOTHRESH_INPUT 0x000003ff /* input threshold (dwords) */
  20899. +#define SAFE_PE_IOTHRESH_OUTPUT 0x03ff0000 /* output threshold (dwords) */
  20900. +
  20901. +#define SAFE_PE_PARTCFG_SIZE 0x0000ffff /* scatter particle size */
  20902. +#define SAFE_PE_PARTCFG_GBURST 0x00030000 /* gather particle burst */
  20903. +#define SAFE_PE_PARTCFG_GBURST_2 0x00000000
  20904. +#define SAFE_PE_PARTCFG_GBURST_4 0x00010000
  20905. +#define SAFE_PE_PARTCFG_GBURST_8 0x00020000
  20906. +#define SAFE_PE_PARTCFG_GBURST_16 0x00030000
  20907. +#define SAFE_PE_PARTCFG_SBURST 0x000c0000 /* scatter particle burst */
  20908. +#define SAFE_PE_PARTCFG_SBURST_2 0x00000000
  20909. +#define SAFE_PE_PARTCFG_SBURST_4 0x00040000
  20910. +#define SAFE_PE_PARTCFG_SBURST_8 0x00080000
  20911. +#define SAFE_PE_PARTCFG_SBURST_16 0x000c0000
  20912. +
  20913. +#define SAFE_PE_PARTSIZE_SCAT 0xffff0000 /* scatter particle ring size */
  20914. +#define SAFE_PE_PARTSIZE_GATH 0x0000ffff /* gather particle ring size */
  20915. +
  20916. +#define SAFE_CRYPTO_CTRL_3DES 0x00000001 /* enable 3DES support */
  20917. +#define SAFE_CRYPTO_CTRL_PKEY 0x00010000 /* enable public key support */
  20918. +#define SAFE_CRYPTO_CTRL_RNG 0x00020000 /* enable RNG support */
  20919. +
  20920. +#define SAFE_DEVINFO_REV_MIN 0x0000000f /* minor rev for chip */
  20921. +#define SAFE_DEVINFO_REV_MAJ 0x000000f0 /* major rev for chip */
  20922. +#define SAFE_DEVINFO_REV_MAJ_S 4
  20923. +#define SAFE_DEVINFO_DES 0x00000100 /* DES/3DES support present */
  20924. +#define SAFE_DEVINFO_ARC4 0x00000200 /* ARC4 support present */
  20925. +#define SAFE_DEVINFO_AES 0x00000400 /* AES support present */
  20926. +#define SAFE_DEVINFO_MD5 0x00001000 /* MD5 support present */
  20927. +#define SAFE_DEVINFO_SHA1 0x00002000 /* SHA-1 support present */
  20928. +#define SAFE_DEVINFO_RIPEMD 0x00004000 /* RIPEMD support present */
  20929. +#define SAFE_DEVINFO_DEFLATE 0x00010000 /* Deflate support present */
  20930. +#define SAFE_DEVINFO_SARAM 0x00100000 /* on-chip SA RAM present */
  20931. +#define SAFE_DEVINFO_EMIBUS 0x00200000 /* EMI bus present */
  20932. +#define SAFE_DEVINFO_PKEY 0x00400000 /* public key support present */
  20933. +#define SAFE_DEVINFO_RNG 0x00800000 /* RNG present */
  20934. +
  20935. +#define SAFE_REV(_maj, _min) (((_maj) << SAFE_DEVINFO_REV_MAJ_S) | (_min))
  20936. +#define SAFE_REV_MAJ(_chiprev) \
  20937. + (((_chiprev) & SAFE_DEVINFO_REV_MAJ) >> SAFE_DEVINFO_REV_MAJ_S)
  20938. +#define SAFE_REV_MIN(_chiprev) ((_chiprev) & SAFE_DEVINFO_REV_MIN)
  20939. +
  20940. +#define SAFE_PK_FUNC_MULT 0x00000001 /* Multiply function */
  20941. +#define SAFE_PK_FUNC_SQUARE 0x00000004 /* Square function */
  20942. +#define SAFE_PK_FUNC_ADD 0x00000010 /* Add function */
  20943. +#define SAFE_PK_FUNC_SUB 0x00000020 /* Subtract function */
  20944. +#define SAFE_PK_FUNC_LSHIFT 0x00000040 /* Left-shift function */
  20945. +#define SAFE_PK_FUNC_RSHIFT 0x00000080 /* Right-shift function */
  20946. +#define SAFE_PK_FUNC_DIV 0x00000100 /* Divide function */
  20947. +#define SAFE_PK_FUNC_CMP 0x00000400 /* Compare function */
  20948. +#define SAFE_PK_FUNC_COPY 0x00000800 /* Copy function */
  20949. +#define SAFE_PK_FUNC_EXP16 0x00002000 /* Exponentiate (4-bit ACT) */
  20950. +#define SAFE_PK_FUNC_EXP4 0x00004000 /* Exponentiate (2-bit ACT) */
  20951. +#define SAFE_PK_FUNC_RUN 0x00008000 /* start/status */
  20952. +
  20953. +#define SAFE_RNG_STAT_BUSY 0x00000001 /* busy, data not valid */
  20954. +
  20955. +#define SAFE_RNG_CTRL_PRE_LFSR 0x00000001 /* enable output pre-LFSR */
  20956. +#define SAFE_RNG_CTRL_TST_MODE 0x00000002 /* enable test mode */
  20957. +#define SAFE_RNG_CTRL_TST_RUN 0x00000004 /* start test state machine */
  20958. +#define SAFE_RNG_CTRL_ENA_RING1 0x00000008 /* test entropy oscillator #1 */
  20959. +#define SAFE_RNG_CTRL_ENA_RING2 0x00000010 /* test entropy oscillator #2 */
  20960. +#define SAFE_RNG_CTRL_DIS_ALARM 0x00000020 /* disable RNG alarm reports */
  20961. +#define SAFE_RNG_CTRL_TST_CLOCK 0x00000040 /* enable test clock */
  20962. +#define SAFE_RNG_CTRL_SHORTEN 0x00000080 /* shorten state timers */
  20963. +#define SAFE_RNG_CTRL_TST_ALARM 0x00000100 /* simulate alarm state */
  20964. +#define SAFE_RNG_CTRL_RST_LFSR 0x00000200 /* reset LFSR */
  20965. +
  20966. +/*
  20967. + * Packet engine descriptor. Note that d_csr is a copy of the
  20968. + * SAFE_PE_CSR register and all definitions apply, and d_len
  20969. + * is a copy of the SAFE_PE_LEN register and all definitions apply.
  20970. + * d_src and d_len may point directly to contiguous data or to a
  20971. + * list of ``particle descriptors'' when using scatter/gather i/o.
  20972. + */
  20973. +struct safe_desc {
  20974. + u_int32_t d_csr; /* per-packet control/status */
  20975. + u_int32_t d_src; /* source address */
  20976. + u_int32_t d_dst; /* destination address */
  20977. + u_int32_t d_sa; /* SA address */
  20978. + u_int32_t d_len; /* length, bypass, status */
  20979. +};
  20980. +
  20981. +/*
  20982. + * Scatter/Gather particle descriptor.
  20983. + *
  20984. + * NB: scatter descriptors do not specify a size; this is fixed
  20985. + * by the setting of the SAFE_PE_PARTCFG register.
  20986. + */
  20987. +struct safe_pdesc {
  20988. + u_int32_t pd_addr; /* particle address */
  20989. +#ifdef __BIG_ENDIAN
  20990. + u_int16_t pd_flags; /* control word */
  20991. + u_int16_t pd_size; /* particle size (bytes) */
  20992. +#else
  20993. + u_int16_t pd_flags; /* control word */
  20994. + u_int16_t pd_size; /* particle size (bytes) */
  20995. +#endif
  20996. +};
  20997. +
  20998. +#define SAFE_PD_READY 0x0001 /* ready for processing */
  20999. +#define SAFE_PD_DONE 0x0002 /* h/w completed processing */
  21000. +
  21001. +/*
  21002. + * Security Association (SA) Record (Rev 1). One of these is
  21003. + * required for each operation processed by the packet engine.
  21004. + */
  21005. +struct safe_sarec {
  21006. + u_int32_t sa_cmd0;
  21007. + u_int32_t sa_cmd1;
  21008. + u_int32_t sa_resv0;
  21009. + u_int32_t sa_resv1;
  21010. + u_int32_t sa_key[8]; /* DES/3DES/AES key */
  21011. + u_int32_t sa_indigest[5]; /* inner digest */
  21012. + u_int32_t sa_outdigest[5]; /* outer digest */
  21013. + u_int32_t sa_spi; /* SPI */
  21014. + u_int32_t sa_seqnum; /* sequence number */
  21015. + u_int32_t sa_seqmask[2]; /* sequence number mask */
  21016. + u_int32_t sa_resv2;
  21017. + u_int32_t sa_staterec; /* address of state record */
  21018. + u_int32_t sa_resv3[2];
  21019. + u_int32_t sa_samgmt0; /* SA management field 0 */
  21020. + u_int32_t sa_samgmt1; /* SA management field 0 */
  21021. +};
  21022. +
  21023. +#define SAFE_SA_CMD0_OP 0x00000007 /* operation code */
  21024. +#define SAFE_SA_CMD0_OP_CRYPT 0x00000000 /* encrypt/decrypt (basic) */
  21025. +#define SAFE_SA_CMD0_OP_BOTH 0x00000001 /* encrypt-hash/hash-decrypto */
  21026. +#define SAFE_SA_CMD0_OP_HASH 0x00000003 /* hash (outbound-only) */
  21027. +#define SAFE_SA_CMD0_OP_ESP 0x00000000 /* ESP in/out (proto) */
  21028. +#define SAFE_SA_CMD0_OP_AH 0x00000001 /* AH in/out (proto) */
  21029. +#define SAFE_SA_CMD0_INBOUND 0x00000008 /* inbound operation */
  21030. +#define SAFE_SA_CMD0_OUTBOUND 0x00000000 /* outbound operation */
  21031. +#define SAFE_SA_CMD0_GROUP 0x00000030 /* operation group */
  21032. +#define SAFE_SA_CMD0_BASIC 0x00000000 /* basic operation */
  21033. +#define SAFE_SA_CMD0_PROTO 0x00000010 /* protocol/packet operation */
  21034. +#define SAFE_SA_CMD0_BUNDLE 0x00000020 /* bundled operation (resvd) */
  21035. +#define SAFE_SA_CMD0_PAD 0x000000c0 /* crypto pad method */
  21036. +#define SAFE_SA_CMD0_PAD_IPSEC 0x00000000 /* IPsec padding */
  21037. +#define SAFE_SA_CMD0_PAD_PKCS7 0x00000040 /* PKCS#7 padding */
  21038. +#define SAFE_SA_CMD0_PAD_CONS 0x00000080 /* constant padding */
  21039. +#define SAFE_SA_CMD0_PAD_ZERO 0x000000c0 /* zero padding */
  21040. +#define SAFE_SA_CMD0_CRYPT_ALG 0x00000f00 /* symmetric crypto algorithm */
  21041. +#define SAFE_SA_CMD0_DES 0x00000000 /* DES crypto algorithm */
  21042. +#define SAFE_SA_CMD0_3DES 0x00000100 /* 3DES crypto algorithm */
  21043. +#define SAFE_SA_CMD0_AES 0x00000300 /* AES crypto algorithm */
  21044. +#define SAFE_SA_CMD0_CRYPT_NULL 0x00000f00 /* null crypto algorithm */
  21045. +#define SAFE_SA_CMD0_HASH_ALG 0x0000f000 /* hash algorithm */
  21046. +#define SAFE_SA_CMD0_MD5 0x00000000 /* MD5 hash algorithm */
  21047. +#define SAFE_SA_CMD0_SHA1 0x00001000 /* SHA-1 hash algorithm */
  21048. +#define SAFE_SA_CMD0_HASH_NULL 0x0000f000 /* null hash algorithm */
  21049. +#define SAFE_SA_CMD0_HDR_PROC 0x00080000 /* header processing */
  21050. +#define SAFE_SA_CMD0_IBUSID 0x00300000 /* input bus id */
  21051. +#define SAFE_SA_CMD0_IPCI 0x00100000 /* PCI input bus id */
  21052. +#define SAFE_SA_CMD0_OBUSID 0x00c00000 /* output bus id */
  21053. +#define SAFE_SA_CMD0_OPCI 0x00400000 /* PCI output bus id */
  21054. +#define SAFE_SA_CMD0_IVLD 0x03000000 /* IV loading */
  21055. +#define SAFE_SA_CMD0_IVLD_NONE 0x00000000 /* IV no load (reuse) */
  21056. +#define SAFE_SA_CMD0_IVLD_IBUF 0x01000000 /* IV load from input buffer */
  21057. +#define SAFE_SA_CMD0_IVLD_STATE 0x02000000 /* IV load from state */
  21058. +#define SAFE_SA_CMD0_HSLD 0x0c000000 /* hash state loading */
  21059. +#define SAFE_SA_CMD0_HSLD_SA 0x00000000 /* hash state load from SA */
  21060. +#define SAFE_SA_CMD0_HSLD_STATE 0x08000000 /* hash state load from state */
  21061. +#define SAFE_SA_CMD0_HSLD_NONE 0x0c000000 /* hash state no load */
  21062. +#define SAFE_SA_CMD0_SAVEIV 0x10000000 /* save IV */
  21063. +#define SAFE_SA_CMD0_SAVEHASH 0x20000000 /* save hash state */
  21064. +#define SAFE_SA_CMD0_IGATHER 0x40000000 /* input gather */
  21065. +#define SAFE_SA_CMD0_OSCATTER 0x80000000 /* output scatter */
  21066. +
  21067. +#define SAFE_SA_CMD1_HDRCOPY 0x00000002 /* copy header to output */
  21068. +#define SAFE_SA_CMD1_PAYCOPY 0x00000004 /* copy payload to output */
  21069. +#define SAFE_SA_CMD1_PADCOPY 0x00000008 /* copy pad to output */
  21070. +#define SAFE_SA_CMD1_IPV4 0x00000000 /* IPv4 protocol */
  21071. +#define SAFE_SA_CMD1_IPV6 0x00000010 /* IPv6 protocol */
  21072. +#define SAFE_SA_CMD1_MUTABLE 0x00000020 /* mutable bit processing */
  21073. +#define SAFE_SA_CMD1_SRBUSID 0x000000c0 /* state record bus id */
  21074. +#define SAFE_SA_CMD1_SRPCI 0x00000040 /* state record from PCI */
  21075. +#define SAFE_SA_CMD1_CRMODE 0x00000300 /* crypto mode */
  21076. +#define SAFE_SA_CMD1_ECB 0x00000000 /* ECB crypto mode */
  21077. +#define SAFE_SA_CMD1_CBC 0x00000100 /* CBC crypto mode */
  21078. +#define SAFE_SA_CMD1_OFB 0x00000200 /* OFB crypto mode */
  21079. +#define SAFE_SA_CMD1_CFB 0x00000300 /* CFB crypto mode */
  21080. +#define SAFE_SA_CMD1_CRFEEDBACK 0x00000c00 /* crypto feedback mode */
  21081. +#define SAFE_SA_CMD1_64BIT 0x00000000 /* 64-bit crypto feedback */
  21082. +#define SAFE_SA_CMD1_8BIT 0x00000400 /* 8-bit crypto feedback */
  21083. +#define SAFE_SA_CMD1_1BIT 0x00000800 /* 1-bit crypto feedback */
  21084. +#define SAFE_SA_CMD1_128BIT 0x00000c00 /* 128-bit crypto feedback */
  21085. +#define SAFE_SA_CMD1_OPTIONS 0x00001000 /* HMAC/options mutable bit */
  21086. +#define SAFE_SA_CMD1_HMAC SAFE_SA_CMD1_OPTIONS
  21087. +#define SAFE_SA_CMD1_SAREV1 0x00008000 /* SA Revision 1 */
  21088. +#define SAFE_SA_CMD1_OFFSET 0x00ff0000 /* hash/crypto offset(dwords) */
  21089. +#define SAFE_SA_CMD1_OFFSET_S 16
  21090. +#define SAFE_SA_CMD1_AESKEYLEN 0x0f000000 /* AES key length */
  21091. +#define SAFE_SA_CMD1_AES128 0x02000000 /* 128-bit AES key */
  21092. +#define SAFE_SA_CMD1_AES192 0x03000000 /* 192-bit AES key */
  21093. +#define SAFE_SA_CMD1_AES256 0x04000000 /* 256-bit AES key */
  21094. +
  21095. +/*
  21096. + * Security Associate State Record (Rev 1).
  21097. + */
  21098. +struct safe_sastate {
  21099. + u_int32_t sa_saved_iv[4]; /* saved IV (DES/3DES/AES) */
  21100. + u_int32_t sa_saved_hashbc; /* saved hash byte count */
  21101. + u_int32_t sa_saved_indigest[5]; /* saved inner digest */
  21102. +};
  21103. +#endif /* _SAFE_SAFEREG_H_ */
  21104. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/safevar.h linux-2.6.30/crypto/ocf/safe/safevar.h
  21105. --- linux-2.6.30.orig/crypto/ocf/safe/safevar.h 1970-01-01 01:00:00.000000000 +0100
  21106. +++ linux-2.6.30/crypto/ocf/safe/safevar.h 2009-06-11 10:55:27.000000000 +0200
  21107. @@ -0,0 +1,230 @@
  21108. +/*-
  21109. + * The linux port of this code done by David McCullough
  21110. + * Copyright (C) 2004-2007 David McCullough <david_mccullough@securecomputing.com>
  21111. + * The license and original author are listed below.
  21112. + *
  21113. + * Copyright (c) 2003 Sam Leffler, Errno Consulting
  21114. + * Copyright (c) 2003 Global Technology Associates, Inc.
  21115. + * All rights reserved.
  21116. + *
  21117. + * Redistribution and use in source and binary forms, with or without
  21118. + * modification, are permitted provided that the following conditions
  21119. + * are met:
  21120. + * 1. Redistributions of source code must retain the above copyright
  21121. + * notice, this list of conditions and the following disclaimer.
  21122. + * 2. Redistributions in binary form must reproduce the above copyright
  21123. + * notice, this list of conditions and the following disclaimer in the
  21124. + * documentation and/or other materials provided with the distribution.
  21125. + *
  21126. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21127. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21128. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21129. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21130. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21131. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21132. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21133. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21134. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21135. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21136. + * SUCH DAMAGE.
  21137. + *
  21138. + * $FreeBSD: src/sys/dev/safe/safevar.h,v 1.2 2006/05/17 18:34:26 pjd Exp $
  21139. + */
  21140. +#ifndef _SAFE_SAFEVAR_H_
  21141. +#define _SAFE_SAFEVAR_H_
  21142. +
  21143. +/* Maximum queue length */
  21144. +#ifndef SAFE_MAX_NQUEUE
  21145. +#define SAFE_MAX_NQUEUE 60
  21146. +#endif
  21147. +
  21148. +#define SAFE_MAX_PART 64 /* Maximum scatter/gather depth */
  21149. +#define SAFE_DMA_BOUNDARY 0 /* No boundary for source DMA ops */
  21150. +#define SAFE_MAX_DSIZE 2048 /* MCLBYTES Fixed scatter particle size */
  21151. +#define SAFE_MAX_SSIZE 0x0ffff /* Maximum gather particle size */
  21152. +#define SAFE_MAX_DMA 0xfffff /* Maximum PE operand size (20 bits) */
  21153. +/* total src+dst particle descriptors */
  21154. +#define SAFE_TOTAL_DPART (SAFE_MAX_NQUEUE * SAFE_MAX_PART)
  21155. +#define SAFE_TOTAL_SPART (SAFE_MAX_NQUEUE * SAFE_MAX_PART)
  21156. +
  21157. +#define SAFE_RNG_MAXBUFSIZ 128 /* 32-bit words */
  21158. +
  21159. +#define SAFE_CARD(sid) (((sid) & 0xf0000000) >> 28)
  21160. +#define SAFE_SESSION(sid) ( (sid) & 0x0fffffff)
  21161. +#define SAFE_SID(crd, sesn) (((crd) << 28) | ((sesn) & 0x0fffffff))
  21162. +
  21163. +#define SAFE_DEF_RTY 0xff /* PCI Retry Timeout */
  21164. +#define SAFE_DEF_TOUT 0xff /* PCI TRDY Timeout */
  21165. +#define SAFE_DEF_CACHELINE 0x01 /* Cache Line setting */
  21166. +
  21167. +#ifdef __KERNEL__
  21168. +/*
  21169. + * State associated with the allocation of each chunk
  21170. + * of memory setup for DMA.
  21171. + */
  21172. +struct safe_dma_alloc {
  21173. + dma_addr_t dma_paddr;
  21174. + void *dma_vaddr;
  21175. +};
  21176. +
  21177. +/*
  21178. + * Cryptographic operand state. One of these exists for each
  21179. + * source and destination operand passed in from the crypto
  21180. + * subsystem. When possible source and destination operands
  21181. + * refer to the same memory. More often they are distinct.
  21182. + * We track the virtual address of each operand as well as
  21183. + * where each is mapped for DMA.
  21184. + */
  21185. +struct safe_operand {
  21186. + union {
  21187. + struct sk_buff *skb;
  21188. + struct uio *io;
  21189. + } u;
  21190. + void *map;
  21191. + int mapsize; /* total number of bytes in segs */
  21192. + struct {
  21193. + dma_addr_t ds_addr;
  21194. + int ds_len;
  21195. + int ds_tlen;
  21196. + } segs[SAFE_MAX_PART];
  21197. + int nsegs;
  21198. +};
  21199. +
  21200. +/*
  21201. + * Packet engine ring entry and cryptographic operation state.
  21202. + * The packet engine requires a ring of descriptors that contain
  21203. + * pointers to various cryptographic state. However the ring
  21204. + * configuration register allows you to specify an arbitrary size
  21205. + * for ring entries. We use this feature to collect most of the
  21206. + * state for each cryptographic request into one spot. Other than
  21207. + * ring entries only the ``particle descriptors'' (scatter/gather
  21208. + * lists) and the actual operand data are kept separate. The
  21209. + * particle descriptors must also be organized in rings. The
  21210. + * operand data can be located aribtrarily (modulo alignment constraints).
  21211. + *
  21212. + * Note that the descriptor ring is mapped onto the PCI bus so
  21213. + * the hardware can DMA data. This means the entire ring must be
  21214. + * contiguous.
  21215. + */
  21216. +struct safe_ringentry {
  21217. + struct safe_desc re_desc; /* command descriptor */
  21218. + struct safe_sarec re_sa; /* SA record */
  21219. + struct safe_sastate re_sastate; /* SA state record */
  21220. +
  21221. + struct cryptop *re_crp; /* crypto operation */
  21222. +
  21223. + struct safe_operand re_src; /* source operand */
  21224. + struct safe_operand re_dst; /* destination operand */
  21225. +
  21226. + int re_sesn; /* crypto session ID */
  21227. + int re_flags;
  21228. +#define SAFE_QFLAGS_COPYOUTIV 0x1 /* copy back on completion */
  21229. +#define SAFE_QFLAGS_COPYOUTICV 0x2 /* copy back on completion */
  21230. +};
  21231. +
  21232. +#define re_src_skb re_src.u.skb
  21233. +#define re_src_io re_src.u.io
  21234. +#define re_src_map re_src.map
  21235. +#define re_src_nsegs re_src.nsegs
  21236. +#define re_src_segs re_src.segs
  21237. +#define re_src_mapsize re_src.mapsize
  21238. +
  21239. +#define re_dst_skb re_dst.u.skb
  21240. +#define re_dst_io re_dst.u.io
  21241. +#define re_dst_map re_dst.map
  21242. +#define re_dst_nsegs re_dst.nsegs
  21243. +#define re_dst_segs re_dst.segs
  21244. +#define re_dst_mapsize re_dst.mapsize
  21245. +
  21246. +struct rndstate_test;
  21247. +
  21248. +struct safe_session {
  21249. + u_int32_t ses_used;
  21250. + u_int32_t ses_klen; /* key length in bits */
  21251. + u_int32_t ses_key[8]; /* DES/3DES/AES key */
  21252. + u_int32_t ses_mlen; /* hmac length in bytes */
  21253. + u_int32_t ses_hminner[5]; /* hmac inner state */
  21254. + u_int32_t ses_hmouter[5]; /* hmac outer state */
  21255. + u_int32_t ses_iv[4]; /* DES/3DES/AES iv */
  21256. +};
  21257. +
  21258. +struct safe_pkq {
  21259. + struct list_head pkq_list;
  21260. + struct cryptkop *pkq_krp;
  21261. +};
  21262. +
  21263. +struct safe_softc {
  21264. + softc_device_decl sc_dev;
  21265. + u32 sc_irq;
  21266. +
  21267. + struct pci_dev *sc_pcidev;
  21268. + ocf_iomem_t sc_base_addr;
  21269. +
  21270. + u_int sc_chiprev; /* major/minor chip revision */
  21271. + int sc_flags; /* device specific flags */
  21272. +#define SAFE_FLAGS_KEY 0x01 /* has key accelerator */
  21273. +#define SAFE_FLAGS_RNG 0x02 /* hardware rng */
  21274. + int sc_suspended;
  21275. + int sc_needwakeup; /* notify crypto layer */
  21276. + int32_t sc_cid; /* crypto tag */
  21277. +
  21278. + struct safe_dma_alloc sc_ringalloc; /* PE ring allocation state */
  21279. + struct safe_ringentry *sc_ring; /* PE ring */
  21280. + struct safe_ringentry *sc_ringtop; /* PE ring top */
  21281. + struct safe_ringentry *sc_front; /* next free entry */
  21282. + struct safe_ringentry *sc_back; /* next pending entry */
  21283. + int sc_nqchip; /* # passed to chip */
  21284. + spinlock_t sc_ringmtx; /* PE ring lock */
  21285. + struct safe_pdesc *sc_spring; /* src particle ring */
  21286. + struct safe_pdesc *sc_springtop; /* src particle ring top */
  21287. + struct safe_pdesc *sc_spfree; /* next free src particle */
  21288. + struct safe_dma_alloc sc_spalloc; /* src particle ring state */
  21289. + struct safe_pdesc *sc_dpring; /* dest particle ring */
  21290. + struct safe_pdesc *sc_dpringtop; /* dest particle ring top */
  21291. + struct safe_pdesc *sc_dpfree; /* next free dest particle */
  21292. + struct safe_dma_alloc sc_dpalloc; /* dst particle ring state */
  21293. + int sc_nsessions; /* # of sessions */
  21294. + struct safe_session *sc_sessions; /* sessions */
  21295. +
  21296. + struct timer_list sc_pkto; /* PK polling */
  21297. + spinlock_t sc_pkmtx; /* PK lock */
  21298. + struct list_head sc_pkq; /* queue of PK requests */
  21299. + struct safe_pkq *sc_pkq_cur; /* current processing request */
  21300. + u_int32_t sc_pk_reslen, sc_pk_resoff;
  21301. +
  21302. + int sc_max_dsize; /* maximum safe DMA size */
  21303. +};
  21304. +#endif /* __KERNEL__ */
  21305. +
  21306. +struct safe_stats {
  21307. + u_int64_t st_ibytes;
  21308. + u_int64_t st_obytes;
  21309. + u_int32_t st_ipackets;
  21310. + u_int32_t st_opackets;
  21311. + u_int32_t st_invalid; /* invalid argument */
  21312. + u_int32_t st_badsession; /* invalid session id */
  21313. + u_int32_t st_badflags; /* flags indicate !(mbuf | uio) */
  21314. + u_int32_t st_nodesc; /* op submitted w/o descriptors */
  21315. + u_int32_t st_badalg; /* unsupported algorithm */
  21316. + u_int32_t st_ringfull; /* PE descriptor ring full */
  21317. + u_int32_t st_peoperr; /* PE marked error */
  21318. + u_int32_t st_dmaerr; /* PE DMA error */
  21319. + u_int32_t st_bypasstoobig; /* bypass > 96 bytes */
  21320. + u_int32_t st_skipmismatch; /* enc part begins before auth part */
  21321. + u_int32_t st_lenmismatch; /* enc length different auth length */
  21322. + u_int32_t st_coffmisaligned; /* crypto offset not 32-bit aligned */
  21323. + u_int32_t st_cofftoobig; /* crypto offset > 255 words */
  21324. + u_int32_t st_iovmisaligned; /* iov op not aligned */
  21325. + u_int32_t st_iovnotuniform; /* iov op not suitable */
  21326. + u_int32_t st_unaligned; /* unaligned src caused copy */
  21327. + u_int32_t st_notuniform; /* non-uniform src caused copy */
  21328. + u_int32_t st_nomap; /* bus_dmamap_create failed */
  21329. + u_int32_t st_noload; /* bus_dmamap_load_* failed */
  21330. + u_int32_t st_nombuf; /* MGET* failed */
  21331. + u_int32_t st_nomcl; /* MCLGET* failed */
  21332. + u_int32_t st_maxqchip; /* max mcr1 ops out for processing */
  21333. + u_int32_t st_rng; /* RNG requests */
  21334. + u_int32_t st_rngalarm; /* RNG alarm requests */
  21335. + u_int32_t st_noicvcopy; /* ICV data copies suppressed */
  21336. +};
  21337. +#endif /* _SAFE_SAFEVAR_H_ */
  21338. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/sha1.c linux-2.6.30/crypto/ocf/safe/sha1.c
  21339. --- linux-2.6.30.orig/crypto/ocf/safe/sha1.c 1970-01-01 01:00:00.000000000 +0100
  21340. +++ linux-2.6.30/crypto/ocf/safe/sha1.c 2009-06-11 10:55:27.000000000 +0200
  21341. @@ -0,0 +1,279 @@
  21342. +/* $KAME: sha1.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
  21343. +/*
  21344. + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  21345. + * All rights reserved.
  21346. + *
  21347. + * Redistribution and use in source and binary forms, with or without
  21348. + * modification, are permitted provided that the following conditions
  21349. + * are met:
  21350. + * 1. Redistributions of source code must retain the above copyright
  21351. + * notice, this list of conditions and the following disclaimer.
  21352. + * 2. Redistributions in binary form must reproduce the above copyright
  21353. + * notice, this list of conditions and the following disclaimer in the
  21354. + * documentation and/or other materials provided with the distribution.
  21355. + * 3. Neither the name of the project nor the names of its contributors
  21356. + * may be used to endorse or promote products derived from this software
  21357. + * without specific prior written permission.
  21358. + *
  21359. + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21360. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21361. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21362. + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21363. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21364. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21365. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21366. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21367. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21368. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21369. + * SUCH DAMAGE.
  21370. + */
  21371. +
  21372. +/*
  21373. + * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
  21374. + * based on: http://csrc.nist.gov/fips/fip180-1.txt
  21375. + * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
  21376. + */
  21377. +
  21378. +#if 0
  21379. +#include <sys/cdefs.h>
  21380. +__FBSDID("$FreeBSD: src/sys/crypto/sha1.c,v 1.9 2003/06/10 21:36:57 obrien Exp $");
  21381. +
  21382. +#include <sys/types.h>
  21383. +#include <sys/cdefs.h>
  21384. +#include <sys/time.h>
  21385. +#include <sys/systm.h>
  21386. +
  21387. +#include <crypto/sha1.h>
  21388. +#endif
  21389. +
  21390. +/* sanity check */
  21391. +#if BYTE_ORDER != BIG_ENDIAN
  21392. +# if BYTE_ORDER != LITTLE_ENDIAN
  21393. +# define unsupported 1
  21394. +# endif
  21395. +#endif
  21396. +
  21397. +#ifndef unsupported
  21398. +
  21399. +/* constant table */
  21400. +static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
  21401. +#define K(t) _K[(t) / 20]
  21402. +
  21403. +#define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
  21404. +#define F1(b, c, d) (((b) ^ (c)) ^ (d))
  21405. +#define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
  21406. +#define F3(b, c, d) (((b) ^ (c)) ^ (d))
  21407. +
  21408. +#define S(n, x) (((x) << (n)) | ((x) >> (32 - n)))
  21409. +
  21410. +#undef H
  21411. +#define H(n) (ctxt->h.b32[(n)])
  21412. +#define COUNT (ctxt->count)
  21413. +#define BCOUNT (ctxt->c.b64[0] / 8)
  21414. +#define W(n) (ctxt->m.b32[(n)])
  21415. +
  21416. +#define PUTBYTE(x) { \
  21417. + ctxt->m.b8[(COUNT % 64)] = (x); \
  21418. + COUNT++; \
  21419. + COUNT %= 64; \
  21420. + ctxt->c.b64[0] += 8; \
  21421. + if (COUNT % 64 == 0) \
  21422. + sha1_step(ctxt); \
  21423. + }
  21424. +
  21425. +#define PUTPAD(x) { \
  21426. + ctxt->m.b8[(COUNT % 64)] = (x); \
  21427. + COUNT++; \
  21428. + COUNT %= 64; \
  21429. + if (COUNT % 64 == 0) \
  21430. + sha1_step(ctxt); \
  21431. + }
  21432. +
  21433. +static void sha1_step(struct sha1_ctxt *);
  21434. +
  21435. +static void
  21436. +sha1_step(ctxt)
  21437. + struct sha1_ctxt *ctxt;
  21438. +{
  21439. + u_int32_t a, b, c, d, e;
  21440. + size_t t, s;
  21441. + u_int32_t tmp;
  21442. +
  21443. +#if BYTE_ORDER == LITTLE_ENDIAN
  21444. + struct sha1_ctxt tctxt;
  21445. + bcopy(&ctxt->m.b8[0], &tctxt.m.b8[0], 64);
  21446. + ctxt->m.b8[0] = tctxt.m.b8[3]; ctxt->m.b8[1] = tctxt.m.b8[2];
  21447. + ctxt->m.b8[2] = tctxt.m.b8[1]; ctxt->m.b8[3] = tctxt.m.b8[0];
  21448. + ctxt->m.b8[4] = tctxt.m.b8[7]; ctxt->m.b8[5] = tctxt.m.b8[6];
  21449. + ctxt->m.b8[6] = tctxt.m.b8[5]; ctxt->m.b8[7] = tctxt.m.b8[4];
  21450. + ctxt->m.b8[8] = tctxt.m.b8[11]; ctxt->m.b8[9] = tctxt.m.b8[10];
  21451. + ctxt->m.b8[10] = tctxt.m.b8[9]; ctxt->m.b8[11] = tctxt.m.b8[8];
  21452. + ctxt->m.b8[12] = tctxt.m.b8[15]; ctxt->m.b8[13] = tctxt.m.b8[14];
  21453. + ctxt->m.b8[14] = tctxt.m.b8[13]; ctxt->m.b8[15] = tctxt.m.b8[12];
  21454. + ctxt->m.b8[16] = tctxt.m.b8[19]; ctxt->m.b8[17] = tctxt.m.b8[18];
  21455. + ctxt->m.b8[18] = tctxt.m.b8[17]; ctxt->m.b8[19] = tctxt.m.b8[16];
  21456. + ctxt->m.b8[20] = tctxt.m.b8[23]; ctxt->m.b8[21] = tctxt.m.b8[22];
  21457. + ctxt->m.b8[22] = tctxt.m.b8[21]; ctxt->m.b8[23] = tctxt.m.b8[20];
  21458. + ctxt->m.b8[24] = tctxt.m.b8[27]; ctxt->m.b8[25] = tctxt.m.b8[26];
  21459. + ctxt->m.b8[26] = tctxt.m.b8[25]; ctxt->m.b8[27] = tctxt.m.b8[24];
  21460. + ctxt->m.b8[28] = tctxt.m.b8[31]; ctxt->m.b8[29] = tctxt.m.b8[30];
  21461. + ctxt->m.b8[30] = tctxt.m.b8[29]; ctxt->m.b8[31] = tctxt.m.b8[28];
  21462. + ctxt->m.b8[32] = tctxt.m.b8[35]; ctxt->m.b8[33] = tctxt.m.b8[34];
  21463. + ctxt->m.b8[34] = tctxt.m.b8[33]; ctxt->m.b8[35] = tctxt.m.b8[32];
  21464. + ctxt->m.b8[36] = tctxt.m.b8[39]; ctxt->m.b8[37] = tctxt.m.b8[38];
  21465. + ctxt->m.b8[38] = tctxt.m.b8[37]; ctxt->m.b8[39] = tctxt.m.b8[36];
  21466. + ctxt->m.b8[40] = tctxt.m.b8[43]; ctxt->m.b8[41] = tctxt.m.b8[42];
  21467. + ctxt->m.b8[42] = tctxt.m.b8[41]; ctxt->m.b8[43] = tctxt.m.b8[40];
  21468. + ctxt->m.b8[44] = tctxt.m.b8[47]; ctxt->m.b8[45] = tctxt.m.b8[46];
  21469. + ctxt->m.b8[46] = tctxt.m.b8[45]; ctxt->m.b8[47] = tctxt.m.b8[44];
  21470. + ctxt->m.b8[48] = tctxt.m.b8[51]; ctxt->m.b8[49] = tctxt.m.b8[50];
  21471. + ctxt->m.b8[50] = tctxt.m.b8[49]; ctxt->m.b8[51] = tctxt.m.b8[48];
  21472. + ctxt->m.b8[52] = tctxt.m.b8[55]; ctxt->m.b8[53] = tctxt.m.b8[54];
  21473. + ctxt->m.b8[54] = tctxt.m.b8[53]; ctxt->m.b8[55] = tctxt.m.b8[52];
  21474. + ctxt->m.b8[56] = tctxt.m.b8[59]; ctxt->m.b8[57] = tctxt.m.b8[58];
  21475. + ctxt->m.b8[58] = tctxt.m.b8[57]; ctxt->m.b8[59] = tctxt.m.b8[56];
  21476. + ctxt->m.b8[60] = tctxt.m.b8[63]; ctxt->m.b8[61] = tctxt.m.b8[62];
  21477. + ctxt->m.b8[62] = tctxt.m.b8[61]; ctxt->m.b8[63] = tctxt.m.b8[60];
  21478. +#endif
  21479. +
  21480. + a = H(0); b = H(1); c = H(2); d = H(3); e = H(4);
  21481. +
  21482. + for (t = 0; t < 20; t++) {
  21483. + s = t & 0x0f;
  21484. + if (t >= 16) {
  21485. + W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
  21486. + }
  21487. + tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
  21488. + e = d; d = c; c = S(30, b); b = a; a = tmp;
  21489. + }
  21490. + for (t = 20; t < 40; t++) {
  21491. + s = t & 0x0f;
  21492. + W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
  21493. + tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
  21494. + e = d; d = c; c = S(30, b); b = a; a = tmp;
  21495. + }
  21496. + for (t = 40; t < 60; t++) {
  21497. + s = t & 0x0f;
  21498. + W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
  21499. + tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
  21500. + e = d; d = c; c = S(30, b); b = a; a = tmp;
  21501. + }
  21502. + for (t = 60; t < 80; t++) {
  21503. + s = t & 0x0f;
  21504. + W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
  21505. + tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
  21506. + e = d; d = c; c = S(30, b); b = a; a = tmp;
  21507. + }
  21508. +
  21509. + H(0) = H(0) + a;
  21510. + H(1) = H(1) + b;
  21511. + H(2) = H(2) + c;
  21512. + H(3) = H(3) + d;
  21513. + H(4) = H(4) + e;
  21514. +
  21515. + bzero(&ctxt->m.b8[0], 64);
  21516. +}
  21517. +
  21518. +/*------------------------------------------------------------*/
  21519. +
  21520. +void
  21521. +sha1_init(ctxt)
  21522. + struct sha1_ctxt *ctxt;
  21523. +{
  21524. + bzero(ctxt, sizeof(struct sha1_ctxt));
  21525. + H(0) = 0x67452301;
  21526. + H(1) = 0xefcdab89;
  21527. + H(2) = 0x98badcfe;
  21528. + H(3) = 0x10325476;
  21529. + H(4) = 0xc3d2e1f0;
  21530. +}
  21531. +
  21532. +void
  21533. +sha1_pad(ctxt)
  21534. + struct sha1_ctxt *ctxt;
  21535. +{
  21536. + size_t padlen; /*pad length in bytes*/
  21537. + size_t padstart;
  21538. +
  21539. + PUTPAD(0x80);
  21540. +
  21541. + padstart = COUNT % 64;
  21542. + padlen = 64 - padstart;
  21543. + if (padlen < 8) {
  21544. + bzero(&ctxt->m.b8[padstart], padlen);
  21545. + COUNT += padlen;
  21546. + COUNT %= 64;
  21547. + sha1_step(ctxt);
  21548. + padstart = COUNT % 64; /* should be 0 */
  21549. + padlen = 64 - padstart; /* should be 64 */
  21550. + }
  21551. + bzero(&ctxt->m.b8[padstart], padlen - 8);
  21552. + COUNT += (padlen - 8);
  21553. + COUNT %= 64;
  21554. +#if BYTE_ORDER == BIG_ENDIAN
  21555. + PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]);
  21556. + PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]);
  21557. + PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]);
  21558. + PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]);
  21559. +#else
  21560. + PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]);
  21561. + PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]);
  21562. + PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]);
  21563. + PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]);
  21564. +#endif
  21565. +}
  21566. +
  21567. +void
  21568. +sha1_loop(ctxt, input, len)
  21569. + struct sha1_ctxt *ctxt;
  21570. + const u_int8_t *input;
  21571. + size_t len;
  21572. +{
  21573. + size_t gaplen;
  21574. + size_t gapstart;
  21575. + size_t off;
  21576. + size_t copysiz;
  21577. +
  21578. + off = 0;
  21579. +
  21580. + while (off < len) {
  21581. + gapstart = COUNT % 64;
  21582. + gaplen = 64 - gapstart;
  21583. +
  21584. + copysiz = (gaplen < len - off) ? gaplen : len - off;
  21585. + bcopy(&input[off], &ctxt->m.b8[gapstart], copysiz);
  21586. + COUNT += copysiz;
  21587. + COUNT %= 64;
  21588. + ctxt->c.b64[0] += copysiz * 8;
  21589. + if (COUNT % 64 == 0)
  21590. + sha1_step(ctxt);
  21591. + off += copysiz;
  21592. + }
  21593. +}
  21594. +
  21595. +void
  21596. +sha1_result(ctxt, digest0)
  21597. + struct sha1_ctxt *ctxt;
  21598. + caddr_t digest0;
  21599. +{
  21600. + u_int8_t *digest;
  21601. +
  21602. + digest = (u_int8_t *)digest0;
  21603. + sha1_pad(ctxt);
  21604. +#if BYTE_ORDER == BIG_ENDIAN
  21605. + bcopy(&ctxt->h.b8[0], digest, 20);
  21606. +#else
  21607. + digest[0] = ctxt->h.b8[3]; digest[1] = ctxt->h.b8[2];
  21608. + digest[2] = ctxt->h.b8[1]; digest[3] = ctxt->h.b8[0];
  21609. + digest[4] = ctxt->h.b8[7]; digest[5] = ctxt->h.b8[6];
  21610. + digest[6] = ctxt->h.b8[5]; digest[7] = ctxt->h.b8[4];
  21611. + digest[8] = ctxt->h.b8[11]; digest[9] = ctxt->h.b8[10];
  21612. + digest[10] = ctxt->h.b8[9]; digest[11] = ctxt->h.b8[8];
  21613. + digest[12] = ctxt->h.b8[15]; digest[13] = ctxt->h.b8[14];
  21614. + digest[14] = ctxt->h.b8[13]; digest[15] = ctxt->h.b8[12];
  21615. + digest[16] = ctxt->h.b8[19]; digest[17] = ctxt->h.b8[18];
  21616. + digest[18] = ctxt->h.b8[17]; digest[19] = ctxt->h.b8[16];
  21617. +#endif
  21618. +}
  21619. +
  21620. +#endif /*unsupported*/
  21621. diff -Nur linux-2.6.30.orig/crypto/ocf/safe/sha1.h linux-2.6.30/crypto/ocf/safe/sha1.h
  21622. --- linux-2.6.30.orig/crypto/ocf/safe/sha1.h 1970-01-01 01:00:00.000000000 +0100
  21623. +++ linux-2.6.30/crypto/ocf/safe/sha1.h 2009-06-11 10:55:27.000000000 +0200
  21624. @@ -0,0 +1,72 @@
  21625. +/* $FreeBSD: src/sys/crypto/sha1.h,v 1.8 2002/03/20 05:13:50 alfred Exp $ */
  21626. +/* $KAME: sha1.h,v 1.5 2000/03/27 04:36:23 sumikawa Exp $ */
  21627. +
  21628. +/*
  21629. + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  21630. + * All rights reserved.
  21631. + *
  21632. + * Redistribution and use in source and binary forms, with or without
  21633. + * modification, are permitted provided that the following conditions
  21634. + * are met:
  21635. + * 1. Redistributions of source code must retain the above copyright
  21636. + * notice, this list of conditions and the following disclaimer.
  21637. + * 2. Redistributions in binary form must reproduce the above copyright
  21638. + * notice, this list of conditions and the following disclaimer in the
  21639. + * documentation and/or other materials provided with the distribution.
  21640. + * 3. Neither the name of the project nor the names of its contributors
  21641. + * may be used to endorse or promote products derived from this software
  21642. + * without specific prior written permission.
  21643. + *
  21644. + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21645. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21646. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21647. + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21648. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21649. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21650. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21651. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21652. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21653. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21654. + * SUCH DAMAGE.
  21655. + */
  21656. +/*
  21657. + * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
  21658. + * based on: http://csrc.nist.gov/fips/fip180-1.txt
  21659. + * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
  21660. + */
  21661. +
  21662. +#ifndef _NETINET6_SHA1_H_
  21663. +#define _NETINET6_SHA1_H_
  21664. +
  21665. +struct sha1_ctxt {
  21666. + union {
  21667. + u_int8_t b8[20];
  21668. + u_int32_t b32[5];
  21669. + } h;
  21670. + union {
  21671. + u_int8_t b8[8];
  21672. + u_int64_t b64[1];
  21673. + } c;
  21674. + union {
  21675. + u_int8_t b8[64];
  21676. + u_int32_t b32[16];
  21677. + } m;
  21678. + u_int8_t count;
  21679. +};
  21680. +
  21681. +#ifdef __KERNEL__
  21682. +extern void sha1_init(struct sha1_ctxt *);
  21683. +extern void sha1_pad(struct sha1_ctxt *);
  21684. +extern void sha1_loop(struct sha1_ctxt *, const u_int8_t *, size_t);
  21685. +extern void sha1_result(struct sha1_ctxt *, caddr_t);
  21686. +
  21687. +/* compatibilty with other SHA1 source codes */
  21688. +typedef struct sha1_ctxt SHA1_CTX;
  21689. +#define SHA1Init(x) sha1_init((x))
  21690. +#define SHA1Update(x, y, z) sha1_loop((x), (y), (z))
  21691. +#define SHA1Final(x, y) sha1_result((y), (x))
  21692. +#endif /* __KERNEL__ */
  21693. +
  21694. +#define SHA1_RESULTLEN (160/8)
  21695. +
  21696. +#endif /*_NETINET6_SHA1_H_*/
  21697. diff -Nur linux-2.6.30.orig/crypto/ocf/talitos/Makefile linux-2.6.30/crypto/ocf/talitos/Makefile
  21698. --- linux-2.6.30.orig/crypto/ocf/talitos/Makefile 1970-01-01 01:00:00.000000000 +0100
  21699. +++ linux-2.6.30/crypto/ocf/talitos/Makefile 2009-06-11 10:55:27.000000000 +0200
  21700. @@ -0,0 +1,12 @@
  21701. +# for SGlinux builds
  21702. +-include $(ROOTDIR)/modules/.config
  21703. +
  21704. +obj-$(CONFIG_OCF_TALITOS) += talitos.o
  21705. +
  21706. +obj ?= .
  21707. +EXTRA_CFLAGS += -I$(obj)/.. -I$(obj)/
  21708. +
  21709. +ifdef TOPDIR
  21710. +-include $(TOPDIR)/Rules.make
  21711. +endif
  21712. +
  21713. diff -Nur linux-2.6.30.orig/crypto/ocf/talitos/talitos.c linux-2.6.30/crypto/ocf/talitos/talitos.c
  21714. --- linux-2.6.30.orig/crypto/ocf/talitos/talitos.c 1970-01-01 01:00:00.000000000 +0100
  21715. +++ linux-2.6.30/crypto/ocf/talitos/talitos.c 2009-06-11 10:55:27.000000000 +0200
  21716. @@ -0,0 +1,1359 @@
  21717. +/*
  21718. + * crypto/ocf/talitos/talitos.c
  21719. + *
  21720. + * An OCF-Linux module that uses Freescale's SEC to do the crypto.
  21721. + * Based on crypto/ocf/hifn and crypto/ocf/safe OCF drivers
  21722. + *
  21723. + * Copyright (c) 2006 Freescale Semiconductor, Inc.
  21724. + *
  21725. + * This code written by Kim A. B. Phillips <kim.phillips@freescale.com>
  21726. + * some code copied from files with the following:
  21727. + * Copyright (C) 2004-2007 David McCullough <david_mccullough@securecomputing.com
  21728. + *
  21729. + * Redistribution and use in source and binary forms, with or without
  21730. + * modification, are permitted provided that the following conditions
  21731. + * are met:
  21732. + *
  21733. + * 1. Redistributions of source code must retain the above copyright
  21734. + * notice, this list of conditions and the following disclaimer.
  21735. + * 2. Redistributions in binary form must reproduce the above copyright
  21736. + * notice, this list of conditions and the following disclaimer in the
  21737. + * documentation and/or other materials provided with the distribution.
  21738. + * 3. The name of the author may not be used to endorse or promote products
  21739. + * derived from this software without specific prior written permission.
  21740. + *
  21741. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21742. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21743. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21744. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21745. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21746. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21747. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21748. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21749. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21750. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21751. + *
  21752. + * ---------------------------------------------------------------------------
  21753. + *
  21754. + * NOTES:
  21755. + *
  21756. + * The Freescale SEC (also known as 'talitos') resides on the
  21757. + * internal bus, and runs asynchronous to the processor core. It has
  21758. + * a wide gamut of cryptographic acceleration features, including single-
  21759. + * pass IPsec (also known as algorithm chaining). To properly utilize
  21760. + * all of the SEC's performance enhancing features, further reworking
  21761. + * of higher level code (framework, applications) will be necessary.
  21762. + *
  21763. + * The following table shows which SEC version is present in which devices:
  21764. + *
  21765. + * Devices SEC version
  21766. + *
  21767. + * 8272, 8248 SEC 1.0
  21768. + * 885, 875 SEC 1.2
  21769. + * 8555E, 8541E SEC 2.0
  21770. + * 8349E SEC 2.01
  21771. + * 8548E SEC 2.1
  21772. + *
  21773. + * The following table shows the features offered by each SEC version:
  21774. + *
  21775. + * Max. chan-
  21776. + * version Bus I/F Clock nels DEU AESU AFEU MDEU PKEU RNG KEU
  21777. + *
  21778. + * SEC 1.0 internal 64b 100MHz 4 1 1 1 1 1 1 0
  21779. + * SEC 1.2 internal 32b 66MHz 1 1 1 0 1 0 0 0
  21780. + * SEC 2.0 internal 64b 166MHz 4 1 1 1 1 1 1 0
  21781. + * SEC 2.01 internal 64b 166MHz 4 1 1 1 1 1 1 0
  21782. + * SEC 2.1 internal 64b 333MHz 4 1 1 1 1 1 1 1
  21783. + *
  21784. + * Each execution unit in the SEC has two modes of execution; channel and
  21785. + * slave/debug. This driver employs the channel infrastructure in the
  21786. + * device for convenience. Only the RNG is directly accessed due to the
  21787. + * convenience of its random fifo pool. The relationship between the
  21788. + * channels and execution units is depicted in the following diagram:
  21789. + *
  21790. + * ------- ------------
  21791. + * ---| ch0 |---| |
  21792. + * ------- | |
  21793. + * | |------+-------+-------+-------+------------
  21794. + * ------- | | | | | | |
  21795. + * ---| ch1 |---| | | | | | |
  21796. + * ------- | | ------ ------ ------ ------ ------
  21797. + * |controller| |DEU | |AESU| |MDEU| |PKEU| ... |RNG |
  21798. + * ------- | | ------ ------ ------ ------ ------
  21799. + * ---| ch2 |---| | | | | | |
  21800. + * ------- | | | | | | |
  21801. + * | |------+-------+-------+-------+------------
  21802. + * ------- | |
  21803. + * ---| ch3 |---| |
  21804. + * ------- ------------
  21805. + *
  21806. + * Channel ch0 may drive an aes operation to the aes unit (AESU),
  21807. + * and, at the same time, ch1 may drive a message digest operation
  21808. + * to the mdeu. Each channel has an input descriptor FIFO, and the
  21809. + * FIFO can contain, e.g. on the 8541E, up to 24 entries, before a
  21810. + * a buffer overrun error is triggered. The controller is responsible
  21811. + * for fetching the data from descriptor pointers, and passing the
  21812. + * data to the appropriate EUs. The controller also writes the
  21813. + * cryptographic operation's result to memory. The SEC notifies
  21814. + * completion by triggering an interrupt and/or setting the 1st byte
  21815. + * of the hdr field to 0xff.
  21816. + *
  21817. + * TODO:
  21818. + * o support more algorithms
  21819. + * o support more versions of the SEC
  21820. + * o add support for linux 2.4
  21821. + * o scatter-gather (sg) support
  21822. + * o add support for public key ops (PKEU)
  21823. + * o add statistics
  21824. + */
  21825. +
  21826. +#ifndef AUTOCONF_INCLUDED
  21827. +#include <linux/config.h>
  21828. +#endif
  21829. +#include <linux/module.h>
  21830. +#include <linux/init.h>
  21831. +#include <linux/interrupt.h>
  21832. +#include <linux/spinlock.h>
  21833. +#include <linux/random.h>
  21834. +#include <linux/skbuff.h>
  21835. +#include <asm/scatterlist.h>
  21836. +#include <linux/dma-mapping.h> /* dma_map_single() */
  21837. +#include <linux/moduleparam.h>
  21838. +
  21839. +#include <linux/version.h>
  21840. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
  21841. +#include <linux/platform_device.h>
  21842. +#endif
  21843. +
  21844. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  21845. +#include <linux/of_platform.h>
  21846. +#endif
  21847. +
  21848. +#include <cryptodev.h>
  21849. +#include <uio.h>
  21850. +
  21851. +#define DRV_NAME "talitos"
  21852. +
  21853. +#include "talitos_dev.h"
  21854. +#include "talitos_soft.h"
  21855. +
  21856. +#define read_random(p,l) get_random_bytes(p,l)
  21857. +
  21858. +const char talitos_driver_name[] = "Talitos OCF";
  21859. +const char talitos_driver_version[] = "0.2";
  21860. +
  21861. +static int talitos_newsession(device_t dev, u_int32_t *sidp,
  21862. + struct cryptoini *cri);
  21863. +static int talitos_freesession(device_t dev, u_int64_t tid);
  21864. +static int talitos_process(device_t dev, struct cryptop *crp, int hint);
  21865. +static void dump_talitos_status(struct talitos_softc *sc);
  21866. +static int talitos_submit(struct talitos_softc *sc, struct talitos_desc *td,
  21867. + int chsel);
  21868. +static void talitos_doneprocessing(struct talitos_softc *sc);
  21869. +static void talitos_init_device(struct talitos_softc *sc);
  21870. +static void talitos_reset_device_master(struct talitos_softc *sc);
  21871. +static void talitos_reset_device(struct talitos_softc *sc);
  21872. +static void talitos_errorprocessing(struct talitos_softc *sc);
  21873. +#ifdef CONFIG_PPC_MERGE
  21874. +static int talitos_probe(struct of_device *ofdev, const struct of_device_id *match);
  21875. +static int talitos_remove(struct of_device *ofdev);
  21876. +#else
  21877. +static int talitos_probe(struct platform_device *pdev);
  21878. +static int talitos_remove(struct platform_device *pdev);
  21879. +#endif
  21880. +#ifdef CONFIG_OCF_RANDOMHARVEST
  21881. +static int talitos_read_random(void *arg, u_int32_t *buf, int maxwords);
  21882. +static void talitos_rng_init(struct talitos_softc *sc);
  21883. +#endif
  21884. +
  21885. +static device_method_t talitos_methods = {
  21886. + /* crypto device methods */
  21887. + DEVMETHOD(cryptodev_newsession, talitos_newsession),
  21888. + DEVMETHOD(cryptodev_freesession,talitos_freesession),
  21889. + DEVMETHOD(cryptodev_process, talitos_process),
  21890. +};
  21891. +
  21892. +#define debug talitos_debug
  21893. +int talitos_debug = 0;
  21894. +module_param(talitos_debug, int, 0644);
  21895. +MODULE_PARM_DESC(talitos_debug, "Enable debug");
  21896. +
  21897. +static inline void talitos_write(volatile unsigned *addr, u32 val)
  21898. +{
  21899. + out_be32(addr, val);
  21900. +}
  21901. +
  21902. +static inline u32 talitos_read(volatile unsigned *addr)
  21903. +{
  21904. + u32 val;
  21905. + val = in_be32(addr);
  21906. + return val;
  21907. +}
  21908. +
  21909. +static void dump_talitos_status(struct talitos_softc *sc)
  21910. +{
  21911. + unsigned int v, v_hi, i, *ptr;
  21912. + v = talitos_read(sc->sc_base_addr + TALITOS_MCR);
  21913. + v_hi = talitos_read(sc->sc_base_addr + TALITOS_MCR_HI);
  21914. + printk(KERN_INFO "%s: MCR 0x%08x_%08x\n",
  21915. + device_get_nameunit(sc->sc_cdev), v, v_hi);
  21916. + v = talitos_read(sc->sc_base_addr + TALITOS_IMR);
  21917. + v_hi = talitos_read(sc->sc_base_addr + TALITOS_IMR_HI);
  21918. + printk(KERN_INFO "%s: IMR 0x%08x_%08x\n",
  21919. + device_get_nameunit(sc->sc_cdev), v, v_hi);
  21920. + v = talitos_read(sc->sc_base_addr + TALITOS_ISR);
  21921. + v_hi = talitos_read(sc->sc_base_addr + TALITOS_ISR_HI);
  21922. + printk(KERN_INFO "%s: ISR 0x%08x_%08x\n",
  21923. + device_get_nameunit(sc->sc_cdev), v, v_hi);
  21924. + for (i = 0; i < sc->sc_num_channels; i++) {
  21925. + v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  21926. + TALITOS_CH_CDPR);
  21927. + v_hi = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  21928. + TALITOS_CH_CDPR_HI);
  21929. + printk(KERN_INFO "%s: CDPR ch%d 0x%08x_%08x\n",
  21930. + device_get_nameunit(sc->sc_cdev), i, v, v_hi);
  21931. + }
  21932. + for (i = 0; i < sc->sc_num_channels; i++) {
  21933. + v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  21934. + TALITOS_CH_CCPSR);
  21935. + v_hi = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  21936. + TALITOS_CH_CCPSR_HI);
  21937. + printk(KERN_INFO "%s: CCPSR ch%d 0x%08x_%08x\n",
  21938. + device_get_nameunit(sc->sc_cdev), i, v, v_hi);
  21939. + }
  21940. + ptr = sc->sc_base_addr + TALITOS_CH_DESCBUF;
  21941. + for (i = 0; i < 16; i++) {
  21942. + v = talitos_read(ptr++); v_hi = talitos_read(ptr++);
  21943. + printk(KERN_INFO "%s: DESCBUF ch0 0x%08x_%08x (tdp%02d)\n",
  21944. + device_get_nameunit(sc->sc_cdev), v, v_hi, i);
  21945. + }
  21946. + return;
  21947. +}
  21948. +
  21949. +
  21950. +#ifdef CONFIG_OCF_RANDOMHARVEST
  21951. +/*
  21952. + * pull random numbers off the RNG FIFO, not exceeding amount available
  21953. + */
  21954. +static int
  21955. +talitos_read_random(void *arg, u_int32_t *buf, int maxwords)
  21956. +{
  21957. + struct talitos_softc *sc = (struct talitos_softc *) arg;
  21958. + int rc;
  21959. + u_int32_t v;
  21960. +
  21961. + DPRINTF("%s()\n", __FUNCTION__);
  21962. +
  21963. + /* check for things like FIFO underflow */
  21964. + v = talitos_read(sc->sc_base_addr + TALITOS_RNGISR_HI);
  21965. + if (unlikely(v)) {
  21966. + printk(KERN_ERR "%s: RNGISR_HI error %08x\n",
  21967. + device_get_nameunit(sc->sc_cdev), v);
  21968. + return 0;
  21969. + }
  21970. + /*
  21971. + * OFL is number of available 64-bit words,
  21972. + * shift and convert to a 32-bit word count
  21973. + */
  21974. + v = talitos_read(sc->sc_base_addr + TALITOS_RNGSR_HI);
  21975. + v = (v & TALITOS_RNGSR_HI_OFL) >> (16 - 1);
  21976. + if (maxwords > v)
  21977. + maxwords = v;
  21978. + for (rc = 0; rc < maxwords; rc++) {
  21979. + buf[rc] = talitos_read(sc->sc_base_addr +
  21980. + TALITOS_RNG_FIFO + rc*sizeof(u_int32_t));
  21981. + }
  21982. + if (maxwords & 1) {
  21983. + /*
  21984. + * RNG will complain with an AE in the RNGISR
  21985. + * if we don't complete the pairs of 32-bit reads
  21986. + * to its 64-bit register based FIFO
  21987. + */
  21988. + v = talitos_read(sc->sc_base_addr +
  21989. + TALITOS_RNG_FIFO + rc*sizeof(u_int32_t));
  21990. + }
  21991. +
  21992. + return rc;
  21993. +}
  21994. +
  21995. +static void
  21996. +talitos_rng_init(struct talitos_softc *sc)
  21997. +{
  21998. + u_int32_t v;
  21999. +
  22000. + DPRINTF("%s()\n", __FUNCTION__);
  22001. + /* reset RNG EU */
  22002. + v = talitos_read(sc->sc_base_addr + TALITOS_RNGRCR_HI);
  22003. + v |= TALITOS_RNGRCR_HI_SR;
  22004. + talitos_write(sc->sc_base_addr + TALITOS_RNGRCR_HI, v);
  22005. + while ((talitos_read(sc->sc_base_addr + TALITOS_RNGSR_HI)
  22006. + & TALITOS_RNGSR_HI_RD) == 0)
  22007. + cpu_relax();
  22008. + /*
  22009. + * we tell the RNG to start filling the RNG FIFO
  22010. + * by writing the RNGDSR
  22011. + */
  22012. + v = talitos_read(sc->sc_base_addr + TALITOS_RNGDSR_HI);
  22013. + talitos_write(sc->sc_base_addr + TALITOS_RNGDSR_HI, v);
  22014. + /*
  22015. + * 64 bits of data will be pushed onto the FIFO every
  22016. + * 256 SEC cycles until the FIFO is full. The RNG then
  22017. + * attempts to keep the FIFO full.
  22018. + */
  22019. + v = talitos_read(sc->sc_base_addr + TALITOS_RNGISR_HI);
  22020. + if (v) {
  22021. + printk(KERN_ERR "%s: RNGISR_HI error %08x\n",
  22022. + device_get_nameunit(sc->sc_cdev), v);
  22023. + return;
  22024. + }
  22025. + /*
  22026. + * n.b. we need to add a FIPS test here - if the RNG is going
  22027. + * to fail, it's going to fail at reset time
  22028. + */
  22029. + return;
  22030. +}
  22031. +#endif /* CONFIG_OCF_RANDOMHARVEST */
  22032. +
  22033. +/*
  22034. + * Generate a new software session.
  22035. + */
  22036. +static int
  22037. +talitos_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  22038. +{
  22039. + struct cryptoini *c, *encini = NULL, *macini = NULL;
  22040. + struct talitos_softc *sc = device_get_softc(dev);
  22041. + struct talitos_session *ses = NULL;
  22042. + int sesn;
  22043. +
  22044. + DPRINTF("%s()\n", __FUNCTION__);
  22045. + if (sidp == NULL || cri == NULL || sc == NULL) {
  22046. + DPRINTF("%s,%d - EINVAL\n", __FILE__, __LINE__);
  22047. + return EINVAL;
  22048. + }
  22049. + for (c = cri; c != NULL; c = c->cri_next) {
  22050. + if (c->cri_alg == CRYPTO_MD5 ||
  22051. + c->cri_alg == CRYPTO_MD5_HMAC ||
  22052. + c->cri_alg == CRYPTO_SHA1 ||
  22053. + c->cri_alg == CRYPTO_SHA1_HMAC ||
  22054. + c->cri_alg == CRYPTO_NULL_HMAC) {
  22055. + if (macini)
  22056. + return EINVAL;
  22057. + macini = c;
  22058. + } else if (c->cri_alg == CRYPTO_DES_CBC ||
  22059. + c->cri_alg == CRYPTO_3DES_CBC ||
  22060. + c->cri_alg == CRYPTO_AES_CBC ||
  22061. + c->cri_alg == CRYPTO_NULL_CBC) {
  22062. + if (encini)
  22063. + return EINVAL;
  22064. + encini = c;
  22065. + } else {
  22066. + DPRINTF("UNKNOWN c->cri_alg %d\n", encini->cri_alg);
  22067. + return EINVAL;
  22068. + }
  22069. + }
  22070. + if (encini == NULL && macini == NULL)
  22071. + return EINVAL;
  22072. + if (encini) {
  22073. + /* validate key length */
  22074. + switch (encini->cri_alg) {
  22075. + case CRYPTO_DES_CBC:
  22076. + if (encini->cri_klen != 64)
  22077. + return EINVAL;
  22078. + break;
  22079. + case CRYPTO_3DES_CBC:
  22080. + if (encini->cri_klen != 192) {
  22081. + return EINVAL;
  22082. + }
  22083. + break;
  22084. + case CRYPTO_AES_CBC:
  22085. + if (encini->cri_klen != 128 &&
  22086. + encini->cri_klen != 192 &&
  22087. + encini->cri_klen != 256)
  22088. + return EINVAL;
  22089. + break;
  22090. + default:
  22091. + DPRINTF("UNKNOWN encini->cri_alg %d\n",
  22092. + encini->cri_alg);
  22093. + return EINVAL;
  22094. + }
  22095. + }
  22096. +
  22097. + if (sc->sc_sessions == NULL) {
  22098. + ses = sc->sc_sessions = (struct talitos_session *)
  22099. + kmalloc(sizeof(struct talitos_session), SLAB_ATOMIC);
  22100. + if (ses == NULL)
  22101. + return ENOMEM;
  22102. + memset(ses, 0, sizeof(struct talitos_session));
  22103. + sesn = 0;
  22104. + sc->sc_nsessions = 1;
  22105. + } else {
  22106. + for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  22107. + if (sc->sc_sessions[sesn].ses_used == 0) {
  22108. + ses = &sc->sc_sessions[sesn];
  22109. + break;
  22110. + }
  22111. + }
  22112. +
  22113. + if (ses == NULL) {
  22114. + /* allocating session */
  22115. + sesn = sc->sc_nsessions;
  22116. + ses = (struct talitos_session *) kmalloc(
  22117. + (sesn + 1) * sizeof(struct talitos_session),
  22118. + SLAB_ATOMIC);
  22119. + if (ses == NULL)
  22120. + return ENOMEM;
  22121. + memset(ses, 0,
  22122. + (sesn + 1) * sizeof(struct talitos_session));
  22123. + memcpy(ses, sc->sc_sessions,
  22124. + sesn * sizeof(struct talitos_session));
  22125. + memset(sc->sc_sessions, 0,
  22126. + sesn * sizeof(struct talitos_session));
  22127. + kfree(sc->sc_sessions);
  22128. + sc->sc_sessions = ses;
  22129. + ses = &sc->sc_sessions[sesn];
  22130. + sc->sc_nsessions++;
  22131. + }
  22132. + }
  22133. +
  22134. + ses->ses_used = 1;
  22135. +
  22136. + if (encini) {
  22137. + /* get an IV */
  22138. + /* XXX may read fewer than requested */
  22139. + read_random(ses->ses_iv, sizeof(ses->ses_iv));
  22140. +
  22141. + ses->ses_klen = (encini->cri_klen + 7) / 8;
  22142. + memcpy(ses->ses_key, encini->cri_key, ses->ses_klen);
  22143. + if (macini) {
  22144. + /* doing hash on top of cipher */
  22145. + ses->ses_hmac_len = (macini->cri_klen + 7) / 8;
  22146. + memcpy(ses->ses_hmac, macini->cri_key,
  22147. + ses->ses_hmac_len);
  22148. + }
  22149. + } else if (macini) {
  22150. + /* doing hash */
  22151. + ses->ses_klen = (macini->cri_klen + 7) / 8;
  22152. + memcpy(ses->ses_key, macini->cri_key, ses->ses_klen);
  22153. + }
  22154. +
  22155. + /* back compat way of determining MSC result len */
  22156. + if (macini) {
  22157. + ses->ses_mlen = macini->cri_mlen;
  22158. + if (ses->ses_mlen == 0) {
  22159. + if (macini->cri_alg == CRYPTO_MD5_HMAC)
  22160. + ses->ses_mlen = MD5_HASH_LEN;
  22161. + else
  22162. + ses->ses_mlen = SHA1_HASH_LEN;
  22163. + }
  22164. + }
  22165. +
  22166. + /* really should make up a template td here,
  22167. + * and only fill things like i/o and direction in process() */
  22168. +
  22169. + /* assign session ID */
  22170. + *sidp = TALITOS_SID(sc->sc_num, sesn);
  22171. + return 0;
  22172. +}
  22173. +
  22174. +/*
  22175. + * Deallocate a session.
  22176. + */
  22177. +static int
  22178. +talitos_freesession(device_t dev, u_int64_t tid)
  22179. +{
  22180. + struct talitos_softc *sc = device_get_softc(dev);
  22181. + int session, ret;
  22182. + u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
  22183. +
  22184. + if (sc == NULL)
  22185. + return EINVAL;
  22186. + session = TALITOS_SESSION(sid);
  22187. + if (session < sc->sc_nsessions) {
  22188. + memset(&sc->sc_sessions[session], 0,
  22189. + sizeof(sc->sc_sessions[session]));
  22190. + ret = 0;
  22191. + } else
  22192. + ret = EINVAL;
  22193. + return ret;
  22194. +}
  22195. +
  22196. +/*
  22197. + * launch device processing - it will come back with done notification
  22198. + * in the form of an interrupt and/or HDR_DONE_BITS in header
  22199. + */
  22200. +static int
  22201. +talitos_submit(
  22202. + struct talitos_softc *sc,
  22203. + struct talitos_desc *td,
  22204. + int chsel)
  22205. +{
  22206. + u_int32_t v;
  22207. +
  22208. + v = dma_map_single(NULL, td, sizeof(*td), DMA_TO_DEVICE);
  22209. + talitos_write(sc->sc_base_addr +
  22210. + chsel*TALITOS_CH_OFFSET + TALITOS_CH_FF, 0);
  22211. + talitos_write(sc->sc_base_addr +
  22212. + chsel*TALITOS_CH_OFFSET + TALITOS_CH_FF_HI, v);
  22213. + return 0;
  22214. +}
  22215. +
  22216. +static int
  22217. +talitos_process(device_t dev, struct cryptop *crp, int hint)
  22218. +{
  22219. + int i, err = 0, ivsize;
  22220. + struct talitos_softc *sc = device_get_softc(dev);
  22221. + struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  22222. + caddr_t iv;
  22223. + struct talitos_session *ses;
  22224. + struct talitos_desc *td;
  22225. + unsigned long flags;
  22226. + /* descriptor mappings */
  22227. + int hmac_key, hmac_data, cipher_iv, cipher_key,
  22228. + in_fifo, out_fifo, cipher_iv_out;
  22229. + static int chsel = -1;
  22230. +
  22231. + DPRINTF("%s()\n", __FUNCTION__);
  22232. +
  22233. + if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
  22234. + return EINVAL;
  22235. + }
  22236. + crp->crp_etype = 0;
  22237. + if (TALITOS_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
  22238. + return EINVAL;
  22239. + }
  22240. +
  22241. + ses = &sc->sc_sessions[TALITOS_SESSION(crp->crp_sid)];
  22242. +
  22243. + /* enter the channel scheduler */
  22244. + spin_lock_irqsave(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  22245. +
  22246. + /* reuse channel that already had/has requests for the required EU */
  22247. + for (i = 0; i < sc->sc_num_channels; i++) {
  22248. + if (sc->sc_chnlastalg[i] == crp->crp_desc->crd_alg)
  22249. + break;
  22250. + }
  22251. + if (i == sc->sc_num_channels) {
  22252. + /*
  22253. + * haven't seen this algo the last sc_num_channels or more
  22254. + * use round robin in this case
  22255. + * nb: sc->sc_num_channels must be power of 2
  22256. + */
  22257. + chsel = (chsel + 1) & (sc->sc_num_channels - 1);
  22258. + } else {
  22259. + /*
  22260. + * matches channel with same target execution unit;
  22261. + * use same channel in this case
  22262. + */
  22263. + chsel = i;
  22264. + }
  22265. + sc->sc_chnlastalg[chsel] = crp->crp_desc->crd_alg;
  22266. +
  22267. + /* release the channel scheduler lock */
  22268. + spin_unlock_irqrestore(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  22269. +
  22270. + /* acquire the selected channel fifo lock */
  22271. + spin_lock_irqsave(&sc->sc_chnfifolock[chsel], flags);
  22272. +
  22273. + /* find and reserve next available descriptor-cryptop pair */
  22274. + for (i = 0; i < sc->sc_chfifo_len; i++) {
  22275. + if (sc->sc_chnfifo[chsel][i].cf_desc.hdr == 0) {
  22276. + /*
  22277. + * ensure correct descriptor formation by
  22278. + * avoiding inadvertently setting "optional" entries
  22279. + * e.g. not using "optional" dptr2 for MD/HMAC descs
  22280. + */
  22281. + memset(&sc->sc_chnfifo[chsel][i].cf_desc,
  22282. + 0, sizeof(*td));
  22283. + /* reserve it with done notification request bit */
  22284. + sc->sc_chnfifo[chsel][i].cf_desc.hdr |=
  22285. + TALITOS_DONE_NOTIFY;
  22286. + break;
  22287. + }
  22288. + }
  22289. + spin_unlock_irqrestore(&sc->sc_chnfifolock[chsel], flags);
  22290. +
  22291. + if (i == sc->sc_chfifo_len) {
  22292. + /* fifo full */
  22293. + err = ERESTART;
  22294. + goto errout;
  22295. + }
  22296. +
  22297. + td = &sc->sc_chnfifo[chsel][i].cf_desc;
  22298. + sc->sc_chnfifo[chsel][i].cf_crp = crp;
  22299. +
  22300. + crd1 = crp->crp_desc;
  22301. + if (crd1 == NULL) {
  22302. + err = EINVAL;
  22303. + goto errout;
  22304. + }
  22305. + crd2 = crd1->crd_next;
  22306. + /* prevent compiler warning */
  22307. + hmac_key = 0;
  22308. + hmac_data = 0;
  22309. + if (crd2 == NULL) {
  22310. + td->hdr |= TD_TYPE_COMMON_NONSNOOP_NO_AFEU;
  22311. + /* assign descriptor dword ptr mappings for this desc. type */
  22312. + cipher_iv = 1;
  22313. + cipher_key = 2;
  22314. + in_fifo = 3;
  22315. + cipher_iv_out = 5;
  22316. + if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
  22317. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  22318. + crd1->crd_alg == CRYPTO_SHA1 ||
  22319. + crd1->crd_alg == CRYPTO_MD5) {
  22320. + out_fifo = 5;
  22321. + maccrd = crd1;
  22322. + enccrd = NULL;
  22323. + } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
  22324. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  22325. + crd1->crd_alg == CRYPTO_AES_CBC ||
  22326. + crd1->crd_alg == CRYPTO_ARC4) {
  22327. + out_fifo = 4;
  22328. + maccrd = NULL;
  22329. + enccrd = crd1;
  22330. + } else {
  22331. + DPRINTF("UNKNOWN crd1->crd_alg %d\n", crd1->crd_alg);
  22332. + err = EINVAL;
  22333. + goto errout;
  22334. + }
  22335. + } else {
  22336. + if (sc->sc_desc_types & TALITOS_HAS_DT_IPSEC_ESP) {
  22337. + td->hdr |= TD_TYPE_IPSEC_ESP;
  22338. + } else {
  22339. + DPRINTF("unimplemented: multiple descriptor ipsec\n");
  22340. + err = EINVAL;
  22341. + goto errout;
  22342. + }
  22343. + /* assign descriptor dword ptr mappings for this desc. type */
  22344. + hmac_key = 0;
  22345. + hmac_data = 1;
  22346. + cipher_iv = 2;
  22347. + cipher_key = 3;
  22348. + in_fifo = 4;
  22349. + out_fifo = 5;
  22350. + cipher_iv_out = 6;
  22351. + if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
  22352. + crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  22353. + crd1->crd_alg == CRYPTO_MD5 ||
  22354. + crd1->crd_alg == CRYPTO_SHA1) &&
  22355. + (crd2->crd_alg == CRYPTO_DES_CBC ||
  22356. + crd2->crd_alg == CRYPTO_3DES_CBC ||
  22357. + crd2->crd_alg == CRYPTO_AES_CBC ||
  22358. + crd2->crd_alg == CRYPTO_ARC4) &&
  22359. + ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
  22360. + maccrd = crd1;
  22361. + enccrd = crd2;
  22362. + } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
  22363. + crd1->crd_alg == CRYPTO_ARC4 ||
  22364. + crd1->crd_alg == CRYPTO_3DES_CBC ||
  22365. + crd1->crd_alg == CRYPTO_AES_CBC) &&
  22366. + (crd2->crd_alg == CRYPTO_MD5_HMAC ||
  22367. + crd2->crd_alg == CRYPTO_SHA1_HMAC ||
  22368. + crd2->crd_alg == CRYPTO_MD5 ||
  22369. + crd2->crd_alg == CRYPTO_SHA1) &&
  22370. + (crd1->crd_flags & CRD_F_ENCRYPT)) {
  22371. + enccrd = crd1;
  22372. + maccrd = crd2;
  22373. + } else {
  22374. + /* We cannot order the SEC as requested */
  22375. + printk("%s: cannot do the order\n",
  22376. + device_get_nameunit(sc->sc_cdev));
  22377. + err = EINVAL;
  22378. + goto errout;
  22379. + }
  22380. + }
  22381. + /* assign in_fifo and out_fifo based on input/output struct type */
  22382. + if (crp->crp_flags & CRYPTO_F_SKBUF) {
  22383. + /* using SKB buffers */
  22384. + struct sk_buff *skb = (struct sk_buff *)crp->crp_buf;
  22385. + if (skb_shinfo(skb)->nr_frags) {
  22386. + printk("%s: skb frags unimplemented\n",
  22387. + device_get_nameunit(sc->sc_cdev));
  22388. + err = EINVAL;
  22389. + goto errout;
  22390. + }
  22391. + td->ptr[in_fifo].ptr = dma_map_single(NULL, skb->data,
  22392. + skb->len, DMA_TO_DEVICE);
  22393. + td->ptr[in_fifo].len = skb->len;
  22394. + td->ptr[out_fifo].ptr = dma_map_single(NULL, skb->data,
  22395. + skb->len, DMA_TO_DEVICE);
  22396. + td->ptr[out_fifo].len = skb->len;
  22397. + td->ptr[hmac_data].ptr = dma_map_single(NULL, skb->data,
  22398. + skb->len, DMA_TO_DEVICE);
  22399. + } else if (crp->crp_flags & CRYPTO_F_IOV) {
  22400. + /* using IOV buffers */
  22401. + struct uio *uiop = (struct uio *)crp->crp_buf;
  22402. + if (uiop->uio_iovcnt > 1) {
  22403. + printk("%s: iov frags unimplemented\n",
  22404. + device_get_nameunit(sc->sc_cdev));
  22405. + err = EINVAL;
  22406. + goto errout;
  22407. + }
  22408. + td->ptr[in_fifo].ptr = dma_map_single(NULL,
  22409. + uiop->uio_iov->iov_base, crp->crp_ilen, DMA_TO_DEVICE);
  22410. + td->ptr[in_fifo].len = crp->crp_ilen;
  22411. + /* crp_olen is never set; always use crp_ilen */
  22412. + td->ptr[out_fifo].ptr = dma_map_single(NULL,
  22413. + uiop->uio_iov->iov_base,
  22414. + crp->crp_ilen, DMA_TO_DEVICE);
  22415. + td->ptr[out_fifo].len = crp->crp_ilen;
  22416. + } else {
  22417. + /* using contig buffers */
  22418. + td->ptr[in_fifo].ptr = dma_map_single(NULL,
  22419. + crp->crp_buf, crp->crp_ilen, DMA_TO_DEVICE);
  22420. + td->ptr[in_fifo].len = crp->crp_ilen;
  22421. + td->ptr[out_fifo].ptr = dma_map_single(NULL,
  22422. + crp->crp_buf, crp->crp_ilen, DMA_TO_DEVICE);
  22423. + td->ptr[out_fifo].len = crp->crp_ilen;
  22424. + }
  22425. + if (enccrd) {
  22426. + switch (enccrd->crd_alg) {
  22427. + case CRYPTO_3DES_CBC:
  22428. + td->hdr |= TALITOS_MODE0_DEU_3DES;
  22429. + /* FALLTHROUGH */
  22430. + case CRYPTO_DES_CBC:
  22431. + td->hdr |= TALITOS_SEL0_DEU
  22432. + | TALITOS_MODE0_DEU_CBC;
  22433. + if (enccrd->crd_flags & CRD_F_ENCRYPT)
  22434. + td->hdr |= TALITOS_MODE0_DEU_ENC;
  22435. + ivsize = 2*sizeof(u_int32_t);
  22436. + DPRINTF("%cDES ses %d ch %d len %d\n",
  22437. + (td->hdr & TALITOS_MODE0_DEU_3DES)?'3':'1',
  22438. + (u32)TALITOS_SESSION(crp->crp_sid),
  22439. + chsel, td->ptr[in_fifo].len);
  22440. + break;
  22441. + case CRYPTO_AES_CBC:
  22442. + td->hdr |= TALITOS_SEL0_AESU
  22443. + | TALITOS_MODE0_AESU_CBC;
  22444. + if (enccrd->crd_flags & CRD_F_ENCRYPT)
  22445. + td->hdr |= TALITOS_MODE0_AESU_ENC;
  22446. + ivsize = 4*sizeof(u_int32_t);
  22447. + DPRINTF("AES ses %d ch %d len %d\n",
  22448. + (u32)TALITOS_SESSION(crp->crp_sid),
  22449. + chsel, td->ptr[in_fifo].len);
  22450. + break;
  22451. + default:
  22452. + printk("%s: unimplemented enccrd->crd_alg %d\n",
  22453. + device_get_nameunit(sc->sc_cdev), enccrd->crd_alg);
  22454. + err = EINVAL;
  22455. + goto errout;
  22456. + }
  22457. + /*
  22458. + * Setup encrypt/decrypt state. When using basic ops
  22459. + * we can't use an inline IV because hash/crypt offset
  22460. + * must be from the end of the IV to the start of the
  22461. + * crypt data and this leaves out the preceding header
  22462. + * from the hash calculation. Instead we place the IV
  22463. + * in the state record and set the hash/crypt offset to
  22464. + * copy both the header+IV.
  22465. + */
  22466. + if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  22467. + td->hdr |= TALITOS_DIR_OUTBOUND;
  22468. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  22469. + iv = enccrd->crd_iv;
  22470. + else
  22471. + iv = (caddr_t) ses->ses_iv;
  22472. + if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  22473. + crypto_copyback(crp->crp_flags, crp->crp_buf,
  22474. + enccrd->crd_inject, ivsize, iv);
  22475. + }
  22476. + } else {
  22477. + td->hdr |= TALITOS_DIR_INBOUND;
  22478. + if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
  22479. + iv = enccrd->crd_iv;
  22480. + bcopy(enccrd->crd_iv, iv, ivsize);
  22481. + } else {
  22482. + iv = (caddr_t) ses->ses_iv;
  22483. + crypto_copydata(crp->crp_flags, crp->crp_buf,
  22484. + enccrd->crd_inject, ivsize, iv);
  22485. + }
  22486. + }
  22487. + td->ptr[cipher_iv].ptr = dma_map_single(NULL, iv, ivsize,
  22488. + DMA_TO_DEVICE);
  22489. + td->ptr[cipher_iv].len = ivsize;
  22490. + /*
  22491. + * we don't need the cipher iv out length/pointer
  22492. + * field to do ESP IPsec. Therefore we set the len field as 0,
  22493. + * which tells the SEC not to do anything with this len/ptr
  22494. + * field. Previously, when length/pointer as pointing to iv,
  22495. + * it gave us corruption of packets.
  22496. + */
  22497. + td->ptr[cipher_iv_out].len = 0;
  22498. + }
  22499. + if (enccrd && maccrd) {
  22500. + /* this is ipsec only for now */
  22501. + td->hdr |= TALITOS_SEL1_MDEU
  22502. + | TALITOS_MODE1_MDEU_INIT
  22503. + | TALITOS_MODE1_MDEU_PAD;
  22504. + switch (maccrd->crd_alg) {
  22505. + case CRYPTO_MD5:
  22506. + td->hdr |= TALITOS_MODE1_MDEU_MD5;
  22507. + break;
  22508. + case CRYPTO_MD5_HMAC:
  22509. + td->hdr |= TALITOS_MODE1_MDEU_MD5_HMAC;
  22510. + break;
  22511. + case CRYPTO_SHA1:
  22512. + td->hdr |= TALITOS_MODE1_MDEU_SHA1;
  22513. + break;
  22514. + case CRYPTO_SHA1_HMAC:
  22515. + td->hdr |= TALITOS_MODE1_MDEU_SHA1_HMAC;
  22516. + break;
  22517. + default:
  22518. + /* We cannot order the SEC as requested */
  22519. + printk("%s: cannot do the order\n",
  22520. + device_get_nameunit(sc->sc_cdev));
  22521. + err = EINVAL;
  22522. + goto errout;
  22523. + }
  22524. + if ((maccrd->crd_alg == CRYPTO_MD5_HMAC) ||
  22525. + (maccrd->crd_alg == CRYPTO_SHA1_HMAC)) {
  22526. + /*
  22527. + * The offset from hash data to the start of
  22528. + * crypt data is the difference in the skips.
  22529. + */
  22530. + /* ipsec only for now */
  22531. + td->ptr[hmac_key].ptr = dma_map_single(NULL,
  22532. + ses->ses_hmac, ses->ses_hmac_len, DMA_TO_DEVICE);
  22533. + td->ptr[hmac_key].len = ses->ses_hmac_len;
  22534. + td->ptr[in_fifo].ptr += enccrd->crd_skip;
  22535. + td->ptr[in_fifo].len = enccrd->crd_len;
  22536. + td->ptr[out_fifo].ptr += enccrd->crd_skip;
  22537. + td->ptr[out_fifo].len = enccrd->crd_len;
  22538. + /* bytes of HMAC to postpend to ciphertext */
  22539. + td->ptr[out_fifo].extent = ses->ses_mlen;
  22540. + td->ptr[hmac_data].ptr += maccrd->crd_skip;
  22541. + td->ptr[hmac_data].len = enccrd->crd_skip - maccrd->crd_skip;
  22542. + }
  22543. + if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
  22544. + printk("%s: CRD_F_KEY_EXPLICIT unimplemented\n",
  22545. + device_get_nameunit(sc->sc_cdev));
  22546. + }
  22547. + }
  22548. + if (!enccrd && maccrd) {
  22549. + /* single MD5 or SHA */
  22550. + td->hdr |= TALITOS_SEL0_MDEU
  22551. + | TALITOS_MODE0_MDEU_INIT
  22552. + | TALITOS_MODE0_MDEU_PAD;
  22553. + switch (maccrd->crd_alg) {
  22554. + case CRYPTO_MD5:
  22555. + td->hdr |= TALITOS_MODE0_MDEU_MD5;
  22556. + DPRINTF("MD5 ses %d ch %d len %d\n",
  22557. + (u32)TALITOS_SESSION(crp->crp_sid),
  22558. + chsel, td->ptr[in_fifo].len);
  22559. + break;
  22560. + case CRYPTO_MD5_HMAC:
  22561. + td->hdr |= TALITOS_MODE0_MDEU_MD5_HMAC;
  22562. + break;
  22563. + case CRYPTO_SHA1:
  22564. + td->hdr |= TALITOS_MODE0_MDEU_SHA1;
  22565. + DPRINTF("SHA1 ses %d ch %d len %d\n",
  22566. + (u32)TALITOS_SESSION(crp->crp_sid),
  22567. + chsel, td->ptr[in_fifo].len);
  22568. + break;
  22569. + case CRYPTO_SHA1_HMAC:
  22570. + td->hdr |= TALITOS_MODE0_MDEU_SHA1_HMAC;
  22571. + break;
  22572. + default:
  22573. + /* We cannot order the SEC as requested */
  22574. + DPRINTF("cannot do the order\n");
  22575. + err = EINVAL;
  22576. + goto errout;
  22577. + }
  22578. +
  22579. + if (crp->crp_flags & CRYPTO_F_IOV)
  22580. + td->ptr[out_fifo].ptr += maccrd->crd_inject;
  22581. +
  22582. + if ((maccrd->crd_alg == CRYPTO_MD5_HMAC) ||
  22583. + (maccrd->crd_alg == CRYPTO_SHA1_HMAC)) {
  22584. + td->ptr[hmac_key].ptr = dma_map_single(NULL,
  22585. + ses->ses_hmac, ses->ses_hmac_len,
  22586. + DMA_TO_DEVICE);
  22587. + td->ptr[hmac_key].len = ses->ses_hmac_len;
  22588. + }
  22589. + }
  22590. + else {
  22591. + /* using process key (session data has duplicate) */
  22592. + td->ptr[cipher_key].ptr = dma_map_single(NULL,
  22593. + enccrd->crd_key, (enccrd->crd_klen + 7) / 8,
  22594. + DMA_TO_DEVICE);
  22595. + td->ptr[cipher_key].len = (enccrd->crd_klen + 7) / 8;
  22596. + }
  22597. + /* descriptor complete - GO! */
  22598. + return talitos_submit(sc, td, chsel);
  22599. +
  22600. +errout:
  22601. + if (err != ERESTART) {
  22602. + crp->crp_etype = err;
  22603. + crypto_done(crp);
  22604. + }
  22605. + return err;
  22606. +}
  22607. +
  22608. +/* go through all channels descriptors, notifying OCF what has
  22609. + * _and_hasn't_ successfully completed and reset the device
  22610. + * (otherwise it's up to decoding desc hdrs!)
  22611. + */
  22612. +static void talitos_errorprocessing(struct talitos_softc *sc)
  22613. +{
  22614. + unsigned long flags;
  22615. + int i, j;
  22616. +
  22617. + /* disable further scheduling until under control */
  22618. + spin_lock_irqsave(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  22619. +
  22620. + if (debug) dump_talitos_status(sc);
  22621. + /* go through descriptors, try and salvage those successfully done,
  22622. + * and EIO those that weren't
  22623. + */
  22624. + for (i = 0; i < sc->sc_num_channels; i++) {
  22625. + spin_lock_irqsave(&sc->sc_chnfifolock[i], flags);
  22626. + for (j = 0; j < sc->sc_chfifo_len; j++) {
  22627. + if (sc->sc_chnfifo[i][j].cf_desc.hdr) {
  22628. + if ((sc->sc_chnfifo[i][j].cf_desc.hdr
  22629. + & TALITOS_HDR_DONE_BITS)
  22630. + != TALITOS_HDR_DONE_BITS) {
  22631. + /* this one didn't finish */
  22632. + /* signify in crp->etype */
  22633. + sc->sc_chnfifo[i][j].cf_crp->crp_etype
  22634. + = EIO;
  22635. + }
  22636. + } else
  22637. + continue; /* free entry */
  22638. + /* either way, notify ocf */
  22639. + crypto_done(sc->sc_chnfifo[i][j].cf_crp);
  22640. + /* and tag it available again
  22641. + *
  22642. + * memset to ensure correct descriptor formation by
  22643. + * avoiding inadvertently setting "optional" entries
  22644. + * e.g. not using "optional" dptr2 MD/HMAC processing
  22645. + */
  22646. + memset(&sc->sc_chnfifo[i][j].cf_desc,
  22647. + 0, sizeof(struct talitos_desc));
  22648. + }
  22649. + spin_unlock_irqrestore(&sc->sc_chnfifolock[i], flags);
  22650. + }
  22651. + /* reset and initialize the SEC h/w device */
  22652. + talitos_reset_device(sc);
  22653. + talitos_init_device(sc);
  22654. +#ifdef CONFIG_OCF_RANDOMHARVEST
  22655. + if (sc->sc_exec_units & TALITOS_HAS_EU_RNG)
  22656. + talitos_rng_init(sc);
  22657. +#endif
  22658. +
  22659. + /* Okay. Stand by. */
  22660. + spin_unlock_irqrestore(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  22661. +
  22662. + return;
  22663. +}
  22664. +
  22665. +/* go through all channels descriptors, notifying OCF what's been done */
  22666. +static void talitos_doneprocessing(struct talitos_softc *sc)
  22667. +{
  22668. + unsigned long flags;
  22669. + int i, j;
  22670. +
  22671. + /* go through descriptors looking for done bits */
  22672. + for (i = 0; i < sc->sc_num_channels; i++) {
  22673. + spin_lock_irqsave(&sc->sc_chnfifolock[i], flags);
  22674. + for (j = 0; j < sc->sc_chfifo_len; j++) {
  22675. + /* descriptor has done bits set? */
  22676. + if ((sc->sc_chnfifo[i][j].cf_desc.hdr
  22677. + & TALITOS_HDR_DONE_BITS)
  22678. + == TALITOS_HDR_DONE_BITS) {
  22679. + /* notify ocf */
  22680. + crypto_done(sc->sc_chnfifo[i][j].cf_crp);
  22681. + /* and tag it available again
  22682. + *
  22683. + * memset to ensure correct descriptor formation by
  22684. + * avoiding inadvertently setting "optional" entries
  22685. + * e.g. not using "optional" dptr2 MD/HMAC processing
  22686. + */
  22687. + memset(&sc->sc_chnfifo[i][j].cf_desc,
  22688. + 0, sizeof(struct talitos_desc));
  22689. + }
  22690. + }
  22691. + spin_unlock_irqrestore(&sc->sc_chnfifolock[i], flags);
  22692. + }
  22693. + return;
  22694. +}
  22695. +
  22696. +static irqreturn_t
  22697. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  22698. +talitos_intr(int irq, void *arg)
  22699. +#else
  22700. +talitos_intr(int irq, void *arg, struct pt_regs *regs)
  22701. +#endif
  22702. +{
  22703. + struct talitos_softc *sc = arg;
  22704. + u_int32_t v, v_hi;
  22705. +
  22706. + /* ack */
  22707. + v = talitos_read(sc->sc_base_addr + TALITOS_ISR);
  22708. + v_hi = talitos_read(sc->sc_base_addr + TALITOS_ISR_HI);
  22709. + talitos_write(sc->sc_base_addr + TALITOS_ICR, v);
  22710. + talitos_write(sc->sc_base_addr + TALITOS_ICR_HI, v_hi);
  22711. +
  22712. + if (unlikely(v & TALITOS_ISR_ERROR)) {
  22713. + /* Okay, Houston, we've had a problem here. */
  22714. + printk(KERN_DEBUG "%s: got error interrupt - ISR 0x%08x_%08x\n",
  22715. + device_get_nameunit(sc->sc_cdev), v, v_hi);
  22716. + talitos_errorprocessing(sc);
  22717. + } else
  22718. + if (likely(v & TALITOS_ISR_DONE)) {
  22719. + talitos_doneprocessing(sc);
  22720. + }
  22721. + return IRQ_HANDLED;
  22722. +}
  22723. +
  22724. +/*
  22725. + * Initialize registers we need to touch only once.
  22726. + */
  22727. +static void
  22728. +talitos_init_device(struct talitos_softc *sc)
  22729. +{
  22730. + u_int32_t v;
  22731. + int i;
  22732. +
  22733. + DPRINTF("%s()\n", __FUNCTION__);
  22734. +
  22735. + /* init all channels */
  22736. + for (i = 0; i < sc->sc_num_channels; i++) {
  22737. + v = talitos_read(sc->sc_base_addr +
  22738. + i*TALITOS_CH_OFFSET + TALITOS_CH_CCCR_HI);
  22739. + v |= TALITOS_CH_CCCR_HI_CDWE
  22740. + | TALITOS_CH_CCCR_HI_CDIE; /* invoke interrupt if done */
  22741. + talitos_write(sc->sc_base_addr +
  22742. + i*TALITOS_CH_OFFSET + TALITOS_CH_CCCR_HI, v);
  22743. + }
  22744. + /* enable all interrupts */
  22745. + v = talitos_read(sc->sc_base_addr + TALITOS_IMR);
  22746. + v |= TALITOS_IMR_ALL;
  22747. + talitos_write(sc->sc_base_addr + TALITOS_IMR, v);
  22748. + v = talitos_read(sc->sc_base_addr + TALITOS_IMR_HI);
  22749. + v |= TALITOS_IMR_HI_ERRONLY;
  22750. + talitos_write(sc->sc_base_addr + TALITOS_IMR_HI, v);
  22751. + return;
  22752. +}
  22753. +
  22754. +/*
  22755. + * set the master reset bit on the device.
  22756. + */
  22757. +static void
  22758. +talitos_reset_device_master(struct talitos_softc *sc)
  22759. +{
  22760. + u_int32_t v;
  22761. +
  22762. + /* Reset the device by writing 1 to MCR:SWR and waiting 'til cleared */
  22763. + v = talitos_read(sc->sc_base_addr + TALITOS_MCR);
  22764. + talitos_write(sc->sc_base_addr + TALITOS_MCR, v | TALITOS_MCR_SWR);
  22765. +
  22766. + while (talitos_read(sc->sc_base_addr + TALITOS_MCR) & TALITOS_MCR_SWR)
  22767. + cpu_relax();
  22768. +
  22769. + return;
  22770. +}
  22771. +
  22772. +/*
  22773. + * Resets the device. Values in the registers are left as is
  22774. + * from the reset (i.e. initial values are assigned elsewhere).
  22775. + */
  22776. +static void
  22777. +talitos_reset_device(struct talitos_softc *sc)
  22778. +{
  22779. + u_int32_t v;
  22780. + int i;
  22781. +
  22782. + DPRINTF("%s()\n", __FUNCTION__);
  22783. +
  22784. + /*
  22785. + * Master reset
  22786. + * errata documentation: warning: certain SEC interrupts
  22787. + * are not fully cleared by writing the MCR:SWR bit,
  22788. + * set bit twice to completely reset
  22789. + */
  22790. + talitos_reset_device_master(sc); /* once */
  22791. + talitos_reset_device_master(sc); /* and once again */
  22792. +
  22793. + /* reset all channels */
  22794. + for (i = 0; i < sc->sc_num_channels; i++) {
  22795. + v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  22796. + TALITOS_CH_CCCR);
  22797. + talitos_write(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  22798. + TALITOS_CH_CCCR, v | TALITOS_CH_CCCR_RESET);
  22799. + }
  22800. +}
  22801. +
  22802. +/* Set up the crypto device structure, private data,
  22803. + * and anything else we need before we start */
  22804. +#ifdef CONFIG_PPC_MERGE
  22805. +static int talitos_probe(struct of_device *ofdev, const struct of_device_id *match)
  22806. +#else
  22807. +static int talitos_probe(struct platform_device *pdev)
  22808. +#endif
  22809. +{
  22810. + struct talitos_softc *sc = NULL;
  22811. + struct resource *r;
  22812. +#ifdef CONFIG_PPC_MERGE
  22813. + struct device *device = &ofdev->dev;
  22814. + struct device_node *np = ofdev->node;
  22815. + const unsigned int *prop;
  22816. + int err;
  22817. + struct resource res;
  22818. +#endif
  22819. + static int num_chips = 0;
  22820. + int rc;
  22821. + int i;
  22822. +
  22823. + DPRINTF("%s()\n", __FUNCTION__);
  22824. +
  22825. + sc = (struct talitos_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  22826. + if (!sc)
  22827. + return -ENOMEM;
  22828. + memset(sc, 0, sizeof(*sc));
  22829. +
  22830. + softc_device_init(sc, DRV_NAME, num_chips, talitos_methods);
  22831. +
  22832. + sc->sc_irq = -1;
  22833. + sc->sc_cid = -1;
  22834. +#ifndef CONFIG_PPC_MERGE
  22835. + sc->sc_dev = pdev;
  22836. +#endif
  22837. + sc->sc_num = num_chips++;
  22838. +
  22839. +#ifdef CONFIG_PPC_MERGE
  22840. + dev_set_drvdata(device, sc);
  22841. +#else
  22842. + platform_set_drvdata(sc->sc_dev, sc);
  22843. +#endif
  22844. +
  22845. + /* get the irq line */
  22846. +#ifdef CONFIG_PPC_MERGE
  22847. + err = of_address_to_resource(np, 0, &res);
  22848. + if (err)
  22849. + return -EINVAL;
  22850. + r = &res;
  22851. +
  22852. + sc->sc_irq = irq_of_parse_and_map(np, 0);
  22853. +#else
  22854. + /* get a pointer to the register memory */
  22855. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  22856. +
  22857. + sc->sc_irq = platform_get_irq(pdev, 0);
  22858. +#endif
  22859. + rc = request_irq(sc->sc_irq, talitos_intr, 0,
  22860. + device_get_nameunit(sc->sc_cdev), sc);
  22861. + if (rc) {
  22862. + printk(KERN_ERR "%s: failed to hook irq %d\n",
  22863. + device_get_nameunit(sc->sc_cdev), sc->sc_irq);
  22864. + sc->sc_irq = -1;
  22865. + goto out;
  22866. + }
  22867. +
  22868. + sc->sc_base_addr = (ocf_iomem_t) ioremap(r->start, (r->end - r->start));
  22869. + if (!sc->sc_base_addr) {
  22870. + printk(KERN_ERR "%s: failed to ioremap\n",
  22871. + device_get_nameunit(sc->sc_cdev));
  22872. + goto out;
  22873. + }
  22874. +
  22875. + /* figure out our SEC's properties and capabilities */
  22876. + sc->sc_chiprev = (u64)talitos_read(sc->sc_base_addr + TALITOS_ID) << 32
  22877. + | talitos_read(sc->sc_base_addr + TALITOS_ID_HI);
  22878. + DPRINTF("sec id 0x%llx\n", sc->sc_chiprev);
  22879. +
  22880. +#ifdef CONFIG_PPC_MERGE
  22881. + /* get SEC properties from device tree, defaulting to SEC 2.0 */
  22882. +
  22883. + prop = of_get_property(np, "num-channels", NULL);
  22884. + sc->sc_num_channels = prop ? *prop : TALITOS_NCHANNELS_SEC_2_0;
  22885. +
  22886. + prop = of_get_property(np, "channel-fifo-len", NULL);
  22887. + sc->sc_chfifo_len = prop ? *prop : TALITOS_CHFIFOLEN_SEC_2_0;
  22888. +
  22889. + prop = of_get_property(np, "exec-units-mask", NULL);
  22890. + sc->sc_exec_units = prop ? *prop : TALITOS_HAS_EUS_SEC_2_0;
  22891. +
  22892. + prop = of_get_property(np, "descriptor-types-mask", NULL);
  22893. + sc->sc_desc_types = prop ? *prop : TALITOS_HAS_DESCTYPES_SEC_2_0;
  22894. +#else
  22895. + /* bulk should go away with openfirmware flat device tree support */
  22896. + if (sc->sc_chiprev & TALITOS_ID_SEC_2_0) {
  22897. + sc->sc_num_channels = TALITOS_NCHANNELS_SEC_2_0;
  22898. + sc->sc_chfifo_len = TALITOS_CHFIFOLEN_SEC_2_0;
  22899. + sc->sc_exec_units = TALITOS_HAS_EUS_SEC_2_0;
  22900. + sc->sc_desc_types = TALITOS_HAS_DESCTYPES_SEC_2_0;
  22901. + } else {
  22902. + printk(KERN_ERR "%s: failed to id device\n",
  22903. + device_get_nameunit(sc->sc_cdev));
  22904. + goto out;
  22905. + }
  22906. +#endif
  22907. +
  22908. + /* + 1 is for the meta-channel lock used by the channel scheduler */
  22909. + sc->sc_chnfifolock = (spinlock_t *) kmalloc(
  22910. + (sc->sc_num_channels + 1) * sizeof(spinlock_t), GFP_KERNEL);
  22911. + if (!sc->sc_chnfifolock)
  22912. + goto out;
  22913. + for (i = 0; i < sc->sc_num_channels + 1; i++) {
  22914. + spin_lock_init(&sc->sc_chnfifolock[i]);
  22915. + }
  22916. +
  22917. + sc->sc_chnlastalg = (int *) kmalloc(
  22918. + sc->sc_num_channels * sizeof(int), GFP_KERNEL);
  22919. + if (!sc->sc_chnlastalg)
  22920. + goto out;
  22921. + memset(sc->sc_chnlastalg, 0, sc->sc_num_channels * sizeof(int));
  22922. +
  22923. + sc->sc_chnfifo = (struct desc_cryptop_pair **) kmalloc(
  22924. + sc->sc_num_channels * sizeof(struct desc_cryptop_pair *),
  22925. + GFP_KERNEL);
  22926. + if (!sc->sc_chnfifo)
  22927. + goto out;
  22928. + for (i = 0; i < sc->sc_num_channels; i++) {
  22929. + sc->sc_chnfifo[i] = (struct desc_cryptop_pair *) kmalloc(
  22930. + sc->sc_chfifo_len * sizeof(struct desc_cryptop_pair),
  22931. + GFP_KERNEL);
  22932. + if (!sc->sc_chnfifo[i])
  22933. + goto out;
  22934. + memset(sc->sc_chnfifo[i], 0,
  22935. + sc->sc_chfifo_len * sizeof(struct desc_cryptop_pair));
  22936. + }
  22937. +
  22938. + /* reset and initialize the SEC h/w device */
  22939. + talitos_reset_device(sc);
  22940. + talitos_init_device(sc);
  22941. +
  22942. + sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
  22943. + if (sc->sc_cid < 0) {
  22944. + printk(KERN_ERR "%s: could not get crypto driver id\n",
  22945. + device_get_nameunit(sc->sc_cdev));
  22946. + goto out;
  22947. + }
  22948. +
  22949. + /* register algorithms with the framework */
  22950. + printk("%s:", device_get_nameunit(sc->sc_cdev));
  22951. +
  22952. + if (sc->sc_exec_units & TALITOS_HAS_EU_RNG) {
  22953. + printk(" rng");
  22954. +#ifdef CONFIG_OCF_RANDOMHARVEST
  22955. + talitos_rng_init(sc);
  22956. + crypto_rregister(sc->sc_cid, talitos_read_random, sc);
  22957. +#endif
  22958. + }
  22959. + if (sc->sc_exec_units & TALITOS_HAS_EU_DEU) {
  22960. + printk(" des/3des");
  22961. + crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  22962. + crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  22963. + }
  22964. + if (sc->sc_exec_units & TALITOS_HAS_EU_AESU) {
  22965. + printk(" aes");
  22966. + crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  22967. + }
  22968. + if (sc->sc_exec_units & TALITOS_HAS_EU_MDEU) {
  22969. + printk(" md5");
  22970. + crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
  22971. + /* HMAC support only with IPsec for now */
  22972. + crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  22973. + printk(" sha1");
  22974. + crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
  22975. + /* HMAC support only with IPsec for now */
  22976. + crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  22977. + }
  22978. + printk("\n");
  22979. + return 0;
  22980. +
  22981. +out:
  22982. +#ifndef CONFIG_PPC_MERGE
  22983. + talitos_remove(pdev);
  22984. +#endif
  22985. + return -ENOMEM;
  22986. +}
  22987. +
  22988. +#ifdef CONFIG_PPC_MERGE
  22989. +static int talitos_remove(struct of_device *ofdev)
  22990. +#else
  22991. +static int talitos_remove(struct platform_device *pdev)
  22992. +#endif
  22993. +{
  22994. +#ifdef CONFIG_PPC_MERGE
  22995. + struct talitos_softc *sc = dev_get_drvdata(&ofdev->dev);
  22996. +#else
  22997. + struct talitos_softc *sc = platform_get_drvdata(pdev);
  22998. +#endif
  22999. + int i;
  23000. +
  23001. + DPRINTF("%s()\n", __FUNCTION__);
  23002. + if (sc->sc_cid >= 0)
  23003. + crypto_unregister_all(sc->sc_cid);
  23004. + if (sc->sc_chnfifo) {
  23005. + for (i = 0; i < sc->sc_num_channels; i++)
  23006. + if (sc->sc_chnfifo[i])
  23007. + kfree(sc->sc_chnfifo[i]);
  23008. + kfree(sc->sc_chnfifo);
  23009. + }
  23010. + if (sc->sc_chnlastalg)
  23011. + kfree(sc->sc_chnlastalg);
  23012. + if (sc->sc_chnfifolock)
  23013. + kfree(sc->sc_chnfifolock);
  23014. + if (sc->sc_irq != -1)
  23015. + free_irq(sc->sc_irq, sc);
  23016. + if (sc->sc_base_addr)
  23017. + iounmap((void *) sc->sc_base_addr);
  23018. + kfree(sc);
  23019. + return 0;
  23020. +}
  23021. +
  23022. +#ifdef CONFIG_PPC_MERGE
  23023. +static struct of_device_id talitos_match[] = {
  23024. + {
  23025. + .type = "crypto",
  23026. + .compatible = "talitos",
  23027. + },
  23028. + {},
  23029. +};
  23030. +
  23031. +MODULE_DEVICE_TABLE(of, talitos_match);
  23032. +
  23033. +static struct of_platform_driver talitos_driver = {
  23034. + .name = DRV_NAME,
  23035. + .match_table = talitos_match,
  23036. + .probe = talitos_probe,
  23037. + .remove = talitos_remove,
  23038. +};
  23039. +
  23040. +static int __init talitos_init(void)
  23041. +{
  23042. + return of_register_platform_driver(&talitos_driver);
  23043. +}
  23044. +
  23045. +static void __exit talitos_exit(void)
  23046. +{
  23047. + of_unregister_platform_driver(&talitos_driver);
  23048. +}
  23049. +#else
  23050. +/* Structure for a platform device driver */
  23051. +static struct platform_driver talitos_driver = {
  23052. + .probe = talitos_probe,
  23053. + .remove = talitos_remove,
  23054. + .driver = {
  23055. + .name = "fsl-sec2",
  23056. + }
  23057. +};
  23058. +
  23059. +static int __init talitos_init(void)
  23060. +{
  23061. + return platform_driver_register(&talitos_driver);
  23062. +}
  23063. +
  23064. +static void __exit talitos_exit(void)
  23065. +{
  23066. + platform_driver_unregister(&talitos_driver);
  23067. +}
  23068. +#endif
  23069. +
  23070. +module_init(talitos_init);
  23071. +module_exit(talitos_exit);
  23072. +
  23073. +MODULE_LICENSE("Dual BSD/GPL");
  23074. +MODULE_AUTHOR("kim.phillips@freescale.com");
  23075. +MODULE_DESCRIPTION("OCF driver for Freescale SEC (talitos)");
  23076. diff -Nur linux-2.6.30.orig/crypto/ocf/talitos/talitos_dev.h linux-2.6.30/crypto/ocf/talitos/talitos_dev.h
  23077. --- linux-2.6.30.orig/crypto/ocf/talitos/talitos_dev.h 1970-01-01 01:00:00.000000000 +0100
  23078. +++ linux-2.6.30/crypto/ocf/talitos/talitos_dev.h 2009-06-11 10:55:27.000000000 +0200
  23079. @@ -0,0 +1,277 @@
  23080. +/*
  23081. + * Freescale SEC (talitos) device dependent data structures
  23082. + *
  23083. + * Copyright (c) 2006 Freescale Semiconductor, Inc.
  23084. + *
  23085. + * Redistribution and use in source and binary forms, with or without
  23086. + * modification, are permitted provided that the following conditions
  23087. + * are met:
  23088. + *
  23089. + * 1. Redistributions of source code must retain the above copyright
  23090. + * notice, this list of conditions and the following disclaimer.
  23091. + * 2. Redistributions in binary form must reproduce the above copyright
  23092. + * notice, this list of conditions and the following disclaimer in the
  23093. + * documentation and/or other materials provided with the distribution.
  23094. + * 3. The name of the author may not be used to endorse or promote products
  23095. + * derived from this software without specific prior written permission.
  23096. + *
  23097. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23098. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23099. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23100. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23101. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23102. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23103. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23104. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23105. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23106. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23107. + *
  23108. + */
  23109. +
  23110. +/* device ID register values */
  23111. +#define TALITOS_ID_SEC_2_0 0x40
  23112. +#define TALITOS_ID_SEC_2_1 0x40 /* cross ref with IP block revision reg */
  23113. +
  23114. +/*
  23115. + * following num_channels, channel-fifo-depth, exec-unit-mask, and
  23116. + * descriptor-types-mask are for forward-compatibility with openfirmware
  23117. + * flat device trees
  23118. + */
  23119. +
  23120. +/*
  23121. + * num_channels : the number of channels available in each SEC version.
  23122. + */
  23123. +
  23124. +/* n.b. this driver requires these values be a power of 2 */
  23125. +#define TALITOS_NCHANNELS_SEC_1_0 4
  23126. +#define TALITOS_NCHANNELS_SEC_1_2 1
  23127. +#define TALITOS_NCHANNELS_SEC_2_0 4
  23128. +#define TALITOS_NCHANNELS_SEC_2_01 4
  23129. +#define TALITOS_NCHANNELS_SEC_2_1 4
  23130. +#define TALITOS_NCHANNELS_SEC_2_4 4
  23131. +
  23132. +/*
  23133. + * channel-fifo-depth : The number of descriptor
  23134. + * pointers a channel fetch fifo can hold.
  23135. + */
  23136. +#define TALITOS_CHFIFOLEN_SEC_1_0 1
  23137. +#define TALITOS_CHFIFOLEN_SEC_1_2 1
  23138. +#define TALITOS_CHFIFOLEN_SEC_2_0 24
  23139. +#define TALITOS_CHFIFOLEN_SEC_2_01 24
  23140. +#define TALITOS_CHFIFOLEN_SEC_2_1 24
  23141. +#define TALITOS_CHFIFOLEN_SEC_2_4 24
  23142. +
  23143. +/*
  23144. + * exec-unit-mask : The bitmask representing what Execution Units (EUs)
  23145. + * are available. EU information should be encoded following the SEC's
  23146. + * EU_SEL0 bitfield documentation, i.e. as follows:
  23147. + *
  23148. + * bit 31 = set if SEC permits no-EU selection (should be always set)
  23149. + * bit 30 = set if SEC has the ARC4 EU (AFEU)
  23150. + * bit 29 = set if SEC has the des/3des EU (DEU)
  23151. + * bit 28 = set if SEC has the message digest EU (MDEU)
  23152. + * bit 27 = set if SEC has the random number generator EU (RNG)
  23153. + * bit 26 = set if SEC has the public key EU (PKEU)
  23154. + * bit 25 = set if SEC has the aes EU (AESU)
  23155. + * bit 24 = set if SEC has the Kasumi EU (KEU)
  23156. + *
  23157. + */
  23158. +#define TALITOS_HAS_EU_NONE (1<<0)
  23159. +#define TALITOS_HAS_EU_AFEU (1<<1)
  23160. +#define TALITOS_HAS_EU_DEU (1<<2)
  23161. +#define TALITOS_HAS_EU_MDEU (1<<3)
  23162. +#define TALITOS_HAS_EU_RNG (1<<4)
  23163. +#define TALITOS_HAS_EU_PKEU (1<<5)
  23164. +#define TALITOS_HAS_EU_AESU (1<<6)
  23165. +#define TALITOS_HAS_EU_KEU (1<<7)
  23166. +
  23167. +/* the corresponding masks for each SEC version */
  23168. +#define TALITOS_HAS_EUS_SEC_1_0 0x7f
  23169. +#define TALITOS_HAS_EUS_SEC_1_2 0x4d
  23170. +#define TALITOS_HAS_EUS_SEC_2_0 0x7f
  23171. +#define TALITOS_HAS_EUS_SEC_2_01 0x7f
  23172. +#define TALITOS_HAS_EUS_SEC_2_1 0xff
  23173. +#define TALITOS_HAS_EUS_SEC_2_4 0x7f
  23174. +
  23175. +/*
  23176. + * descriptor-types-mask : The bitmask representing what descriptors
  23177. + * are available. Descriptor type information should be encoded
  23178. + * following the SEC's Descriptor Header Dword DESC_TYPE field
  23179. + * documentation, i.e. as follows:
  23180. + *
  23181. + * bit 0 = set if SEC supports the aesu_ctr_nonsnoop desc. type
  23182. + * bit 1 = set if SEC supports the ipsec_esp descriptor type
  23183. + * bit 2 = set if SEC supports the common_nonsnoop desc. type
  23184. + * bit 3 = set if SEC supports the 802.11i AES ccmp desc. type
  23185. + * bit 4 = set if SEC supports the hmac_snoop_no_afeu desc. type
  23186. + * bit 5 = set if SEC supports the srtp descriptor type
  23187. + * bit 6 = set if SEC supports the non_hmac_snoop_no_afeu desc.type
  23188. + * bit 7 = set if SEC supports the pkeu_assemble descriptor type
  23189. + * bit 8 = set if SEC supports the aesu_key_expand_output desc.type
  23190. + * bit 9 = set if SEC supports the pkeu_ptmul descriptor type
  23191. + * bit 10 = set if SEC supports the common_nonsnoop_afeu desc. type
  23192. + * bit 11 = set if SEC supports the pkeu_ptadd_dbl descriptor type
  23193. + *
  23194. + * ..and so on and so forth.
  23195. + */
  23196. +#define TALITOS_HAS_DT_AESU_CTR_NONSNOOP (1<<0)
  23197. +#define TALITOS_HAS_DT_IPSEC_ESP (1<<1)
  23198. +#define TALITOS_HAS_DT_COMMON_NONSNOOP (1<<2)
  23199. +
  23200. +/* the corresponding masks for each SEC version */
  23201. +#define TALITOS_HAS_DESCTYPES_SEC_2_0 0x01010ebf
  23202. +#define TALITOS_HAS_DESCTYPES_SEC_2_1 0x012b0ebf
  23203. +
  23204. +/*
  23205. + * a TALITOS_xxx_HI address points to the low data bits (32-63) of the register
  23206. + */
  23207. +
  23208. +/* global register offset addresses */
  23209. +#define TALITOS_ID 0x1020
  23210. +#define TALITOS_ID_HI 0x1024
  23211. +#define TALITOS_MCR 0x1030 /* master control register */
  23212. +#define TALITOS_MCR_HI 0x1038 /* master control register */
  23213. +#define TALITOS_MCR_SWR 0x1
  23214. +#define TALITOS_IMR 0x1008 /* interrupt mask register */
  23215. +#define TALITOS_IMR_ALL 0x00010fff /* enable all interrupts mask */
  23216. +#define TALITOS_IMR_ERRONLY 0x00010aaa /* enable error interrupts */
  23217. +#define TALITOS_IMR_HI 0x100C /* interrupt mask register */
  23218. +#define TALITOS_IMR_HI_ALL 0x00323333 /* enable all interrupts mask */
  23219. +#define TALITOS_IMR_HI_ERRONLY 0x00222222 /* enable error interrupts */
  23220. +#define TALITOS_ISR 0x1010 /* interrupt status register */
  23221. +#define TALITOS_ISR_ERROR 0x00010faa /* errors mask */
  23222. +#define TALITOS_ISR_DONE 0x00000055 /* channel(s) done mask */
  23223. +#define TALITOS_ISR_HI 0x1014 /* interrupt status register */
  23224. +#define TALITOS_ICR 0x1018 /* interrupt clear register */
  23225. +#define TALITOS_ICR_HI 0x101C /* interrupt clear register */
  23226. +
  23227. +/* channel register address stride */
  23228. +#define TALITOS_CH_OFFSET 0x100
  23229. +
  23230. +/* channel register offset addresses and bits */
  23231. +#define TALITOS_CH_CCCR 0x1108 /* Crypto-Channel Config Register */
  23232. +#define TALITOS_CH_CCCR_RESET 0x1 /* Channel Reset bit */
  23233. +#define TALITOS_CH_CCCR_HI 0x110c /* Crypto-Channel Config Register */
  23234. +#define TALITOS_CH_CCCR_HI_CDWE 0x10 /* Channel done writeback enable bit */
  23235. +#define TALITOS_CH_CCCR_HI_NT 0x4 /* Notification type bit */
  23236. +#define TALITOS_CH_CCCR_HI_CDIE 0x2 /* Channel Done Interrupt Enable bit */
  23237. +#define TALITOS_CH_CCPSR 0x1110 /* Crypto-Channel Pointer Status Reg */
  23238. +#define TALITOS_CH_CCPSR_HI 0x1114 /* Crypto-Channel Pointer Status Reg */
  23239. +#define TALITOS_CH_FF 0x1148 /* Fetch FIFO */
  23240. +#define TALITOS_CH_FF_HI 0x114c /* Fetch FIFO's FETCH_ADRS */
  23241. +#define TALITOS_CH_CDPR 0x1140 /* Crypto-Channel Pointer Status Reg */
  23242. +#define TALITOS_CH_CDPR_HI 0x1144 /* Crypto-Channel Pointer Status Reg */
  23243. +#define TALITOS_CH_DESCBUF 0x1180 /* (thru 11bf) Crypto-Channel
  23244. + * Descriptor Buffer (debug) */
  23245. +
  23246. +/* execution unit register offset addresses and bits */
  23247. +#define TALITOS_DEUSR 0x2028 /* DEU status register */
  23248. +#define TALITOS_DEUSR_HI 0x202c /* DEU status register */
  23249. +#define TALITOS_DEUISR 0x2030 /* DEU interrupt status register */
  23250. +#define TALITOS_DEUISR_HI 0x2034 /* DEU interrupt status register */
  23251. +#define TALITOS_DEUICR 0x2038 /* DEU interrupt control register */
  23252. +#define TALITOS_DEUICR_HI 0x203c /* DEU interrupt control register */
  23253. +#define TALITOS_AESUISR 0x4030 /* AESU interrupt status register */
  23254. +#define TALITOS_AESUISR_HI 0x4034 /* AESU interrupt status register */
  23255. +#define TALITOS_AESUICR 0x4038 /* AESU interrupt control register */
  23256. +#define TALITOS_AESUICR_HI 0x403c /* AESU interrupt control register */
  23257. +#define TALITOS_MDEUISR 0x6030 /* MDEU interrupt status register */
  23258. +#define TALITOS_MDEUISR_HI 0x6034 /* MDEU interrupt status register */
  23259. +#define TALITOS_RNGSR 0xa028 /* RNG status register */
  23260. +#define TALITOS_RNGSR_HI 0xa02c /* RNG status register */
  23261. +#define TALITOS_RNGSR_HI_RD 0x1 /* RNG Reset done */
  23262. +#define TALITOS_RNGSR_HI_OFL 0xff0000/* number of dwords in RNG output FIFO*/
  23263. +#define TALITOS_RNGDSR 0xa010 /* RNG data size register */
  23264. +#define TALITOS_RNGDSR_HI 0xa014 /* RNG data size register */
  23265. +#define TALITOS_RNG_FIFO 0xa800 /* RNG FIFO - pool of random numbers */
  23266. +#define TALITOS_RNGISR 0xa030 /* RNG Interrupt status register */
  23267. +#define TALITOS_RNGISR_HI 0xa034 /* RNG Interrupt status register */
  23268. +#define TALITOS_RNGRCR 0xa018 /* RNG Reset control register */
  23269. +#define TALITOS_RNGRCR_HI 0xa01c /* RNG Reset control register */
  23270. +#define TALITOS_RNGRCR_HI_SR 0x1 /* RNG RNGRCR:Software Reset */
  23271. +
  23272. +/* descriptor pointer entry */
  23273. +struct talitos_desc_ptr {
  23274. + u16 len; /* length */
  23275. + u8 extent; /* jump (to s/g link table) and extent */
  23276. + u8 res; /* reserved */
  23277. + u32 ptr; /* pointer */
  23278. +};
  23279. +
  23280. +/* descriptor */
  23281. +struct talitos_desc {
  23282. + u32 hdr; /* header */
  23283. + u32 res; /* reserved */
  23284. + struct talitos_desc_ptr ptr[7]; /* ptr/len pair array */
  23285. +};
  23286. +
  23287. +/* talitos descriptor header (hdr) bits */
  23288. +
  23289. +/* primary execution unit select */
  23290. +#define TALITOS_SEL0_AFEU 0x10000000
  23291. +#define TALITOS_SEL0_DEU 0x20000000
  23292. +#define TALITOS_SEL0_MDEU 0x30000000
  23293. +#define TALITOS_SEL0_RNG 0x40000000
  23294. +#define TALITOS_SEL0_PKEU 0x50000000
  23295. +#define TALITOS_SEL0_AESU 0x60000000
  23296. +
  23297. +/* primary execution unit mode (MODE0) and derivatives */
  23298. +#define TALITOS_MODE0_AESU_CBC 0x00200000
  23299. +#define TALITOS_MODE0_AESU_ENC 0x00100000
  23300. +#define TALITOS_MODE0_DEU_CBC 0x00400000
  23301. +#define TALITOS_MODE0_DEU_3DES 0x00200000
  23302. +#define TALITOS_MODE0_DEU_ENC 0x00100000
  23303. +#define TALITOS_MODE0_MDEU_INIT 0x01000000 /* init starting regs */
  23304. +#define TALITOS_MODE0_MDEU_HMAC 0x00800000
  23305. +#define TALITOS_MODE0_MDEU_PAD 0x00400000 /* PD */
  23306. +#define TALITOS_MODE0_MDEU_MD5 0x00200000
  23307. +#define TALITOS_MODE0_MDEU_SHA256 0x00100000
  23308. +#define TALITOS_MODE0_MDEU_SHA1 0x00000000 /* SHA-160 */
  23309. +#define TALITOS_MODE0_MDEU_MD5_HMAC \
  23310. + (TALITOS_MODE0_MDEU_MD5 | TALITOS_MODE0_MDEU_HMAC)
  23311. +#define TALITOS_MODE0_MDEU_SHA256_HMAC \
  23312. + (TALITOS_MODE0_MDEU_SHA256 | TALITOS_MODE0_MDEU_HMAC)
  23313. +#define TALITOS_MODE0_MDEU_SHA1_HMAC \
  23314. + (TALITOS_MODE0_MDEU_SHA1 | TALITOS_MODE0_MDEU_HMAC)
  23315. +
  23316. +/* secondary execution unit select (SEL1) */
  23317. +/* it's MDEU or nothing */
  23318. +#define TALITOS_SEL1_MDEU 0x00030000
  23319. +
  23320. +/* secondary execution unit mode (MODE1) and derivatives */
  23321. +#define TALITOS_MODE1_MDEU_INIT 0x00001000 /* init starting regs */
  23322. +#define TALITOS_MODE1_MDEU_HMAC 0x00000800
  23323. +#define TALITOS_MODE1_MDEU_PAD 0x00000400 /* PD */
  23324. +#define TALITOS_MODE1_MDEU_MD5 0x00000200
  23325. +#define TALITOS_MODE1_MDEU_SHA256 0x00000100
  23326. +#define TALITOS_MODE1_MDEU_SHA1 0x00000000 /* SHA-160 */
  23327. +#define TALITOS_MODE1_MDEU_MD5_HMAC \
  23328. + (TALITOS_MODE1_MDEU_MD5 | TALITOS_MODE1_MDEU_HMAC)
  23329. +#define TALITOS_MODE1_MDEU_SHA256_HMAC \
  23330. + (TALITOS_MODE1_MDEU_SHA256 | TALITOS_MODE1_MDEU_HMAC)
  23331. +#define TALITOS_MODE1_MDEU_SHA1_HMAC \
  23332. + (TALITOS_MODE1_MDEU_SHA1 | TALITOS_MODE1_MDEU_HMAC)
  23333. +
  23334. +/* direction of overall data flow (DIR) */
  23335. +#define TALITOS_DIR_OUTBOUND 0x00000000
  23336. +#define TALITOS_DIR_INBOUND 0x00000002
  23337. +
  23338. +/* done notification (DN) */
  23339. +#define TALITOS_DONE_NOTIFY 0x00000001
  23340. +
  23341. +/* descriptor types */
  23342. +/* odd numbers here are valid on SEC2 and greater only (e.g. ipsec_esp) */
  23343. +#define TD_TYPE_AESU_CTR_NONSNOOP (0 << 3)
  23344. +#define TD_TYPE_IPSEC_ESP (1 << 3)
  23345. +#define TD_TYPE_COMMON_NONSNOOP_NO_AFEU (2 << 3)
  23346. +#define TD_TYPE_HMAC_SNOOP_NO_AFEU (4 << 3)
  23347. +
  23348. +#define TALITOS_HDR_DONE_BITS 0xff000000
  23349. +
  23350. +#define DPRINTF(a...) do { \
  23351. + if (debug) { \
  23352. + printk("%s: ", sc ? \
  23353. + device_get_nameunit(sc->sc_cdev) : "talitos"); \
  23354. + printk(a); \
  23355. + } \
  23356. + } while (0)
  23357. diff -Nur linux-2.6.30.orig/crypto/ocf/talitos/talitos_soft.h linux-2.6.30/crypto/ocf/talitos/talitos_soft.h
  23358. --- linux-2.6.30.orig/crypto/ocf/talitos/talitos_soft.h 1970-01-01 01:00:00.000000000 +0100
  23359. +++ linux-2.6.30/crypto/ocf/talitos/talitos_soft.h 2009-06-11 10:55:27.000000000 +0200
  23360. @@ -0,0 +1,77 @@
  23361. +/*
  23362. + * Freescale SEC data structures for integration with ocf-linux
  23363. + *
  23364. + * Copyright (c) 2006 Freescale Semiconductor, Inc.
  23365. + *
  23366. + * Redistribution and use in source and binary forms, with or without
  23367. + * modification, are permitted provided that the following conditions
  23368. + * are met:
  23369. + *
  23370. + * 1. Redistributions of source code must retain the above copyright
  23371. + * notice, this list of conditions and the following disclaimer.
  23372. + * 2. Redistributions in binary form must reproduce the above copyright
  23373. + * notice, this list of conditions and the following disclaimer in the
  23374. + * documentation and/or other materials provided with the distribution.
  23375. + * 3. The name of the author may not be used to endorse or promote products
  23376. + * derived from this software without specific prior written permission.
  23377. + *
  23378. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23379. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23380. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23381. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23382. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23383. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23384. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23385. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23386. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23387. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23388. + */
  23389. +
  23390. +/*
  23391. + * paired descriptor and associated crypto operation
  23392. + */
  23393. +struct desc_cryptop_pair {
  23394. + struct talitos_desc cf_desc; /* descriptor ptr */
  23395. + struct cryptop *cf_crp; /* cryptop ptr */
  23396. +};
  23397. +
  23398. +/*
  23399. + * Holds data specific to a single talitos device.
  23400. + */
  23401. +struct talitos_softc {
  23402. + softc_device_decl sc_cdev;
  23403. + struct platform_device *sc_dev; /* device backpointer */
  23404. + ocf_iomem_t sc_base_addr;
  23405. + int sc_irq;
  23406. + int sc_num; /* if we have multiple chips */
  23407. + int32_t sc_cid; /* crypto tag */
  23408. + u64 sc_chiprev; /* major/minor chip revision */
  23409. + int sc_nsessions;
  23410. + struct talitos_session *sc_sessions;
  23411. + int sc_num_channels;/* number of crypto channels */
  23412. + int sc_chfifo_len; /* channel fetch fifo len */
  23413. + int sc_exec_units; /* execution units mask */
  23414. + int sc_desc_types; /* descriptor types mask */
  23415. + /*
  23416. + * mutual exclusion for intra-channel resources, e.g. fetch fifos
  23417. + * the last entry is a meta-channel lock used by the channel scheduler
  23418. + */
  23419. + spinlock_t *sc_chnfifolock;
  23420. + /* sc_chnlastalgo contains last algorithm for that channel */
  23421. + int *sc_chnlastalg;
  23422. + /* sc_chnfifo holds pending descriptor--crypto operation pairs */
  23423. + struct desc_cryptop_pair **sc_chnfifo;
  23424. +};
  23425. +
  23426. +struct talitos_session {
  23427. + u_int32_t ses_used;
  23428. + u_int32_t ses_klen; /* key length in bits */
  23429. + u_int32_t ses_key[8]; /* DES/3DES/AES key */
  23430. + u_int32_t ses_hmac[5]; /* hmac inner state */
  23431. + u_int32_t ses_hmac_len; /* hmac length */
  23432. + u_int32_t ses_iv[4]; /* DES/3DES/AES iv */
  23433. + u_int32_t ses_mlen; /* desired hash result len (12=ipsec or 16) */
  23434. +};
  23435. +
  23436. +#define TALITOS_SESSION(sid) ((sid) & 0x0fffffff)
  23437. +#define TALITOS_SID(crd, sesn) (((crd) << 28) | ((sesn) & 0x0fffffff))
  23438. diff -Nur linux-2.6.30.orig/crypto/ocf/uio.h linux-2.6.30/crypto/ocf/uio.h
  23439. --- linux-2.6.30.orig/crypto/ocf/uio.h 1970-01-01 01:00:00.000000000 +0100
  23440. +++ linux-2.6.30/crypto/ocf/uio.h 2009-06-11 10:55:27.000000000 +0200
  23441. @@ -0,0 +1,54 @@
  23442. +#ifndef _OCF_UIO_H_
  23443. +#define _OCF_UIO_H_
  23444. +
  23445. +#include <linux/uio.h>
  23446. +
  23447. +/*
  23448. + * The linux uio.h doesn't have all we need. To be fully api compatible
  23449. + * with the BSD cryptodev, we need to keep this around. Perhaps this can
  23450. + * be moved back into the linux/uio.h
  23451. + *
  23452. + * Linux port done by David McCullough <david_mccullough@securecomputing.com>
  23453. + * Copyright (C) 2006-2007 David McCullough
  23454. + * Copyright (C) 2004-2005 Intel Corporation.
  23455. + *
  23456. + * LICENSE TERMS
  23457. + *
  23458. + * The free distribution and use of this software in both source and binary
  23459. + * form is allowed (with or without changes) provided that:
  23460. + *
  23461. + * 1. distributions of this source code include the above copyright
  23462. + * notice, this list of conditions and the following disclaimer;
  23463. + *
  23464. + * 2. distributions in binary form include the above copyright
  23465. + * notice, this list of conditions and the following disclaimer
  23466. + * in the documentation and/or other associated materials;
  23467. + *
  23468. + * 3. the copyright holder's name is not used to endorse products
  23469. + * built using this software without specific written permission.
  23470. + *
  23471. + * ALTERNATIVELY, provided that this notice is retained in full, this product
  23472. + * may be distributed under the terms of the GNU General Public License (GPL),
  23473. + * in which case the provisions of the GPL apply INSTEAD OF those given above.
  23474. + *
  23475. + * DISCLAIMER
  23476. + *
  23477. + * This software is provided 'as is' with no explicit or implied warranties
  23478. + * in respect of its properties, including, but not limited to, correctness
  23479. + * and/or fitness for purpose.
  23480. + * ---------------------------------------------------------------------------
  23481. + */
  23482. +
  23483. +struct uio {
  23484. + struct iovec *uio_iov;
  23485. + int uio_iovcnt;
  23486. + off_t uio_offset;
  23487. + int uio_resid;
  23488. +#if 0
  23489. + enum uio_seg uio_segflg;
  23490. + enum uio_rw uio_rw;
  23491. + struct thread *uio_td;
  23492. +#endif
  23493. +};
  23494. +
  23495. +#endif
  23496. diff -Nur linux-2.6.30.orig/drivers/char/random.c linux-2.6.30/drivers/char/random.c
  23497. --- linux-2.6.30.orig/drivers/char/random.c 2009-06-10 05:05:27.000000000 +0200
  23498. +++ linux-2.6.30/drivers/char/random.c 2009-06-11 10:55:27.000000000 +0200
  23499. @@ -129,6 +129,9 @@
  23500. * unsigned int value);
  23501. * void add_interrupt_randomness(int irq);
  23502. *
  23503. + * void random_input_words(__u32 *buf, size_t wordcount, int ent_count)
  23504. + * int random_input_wait(void);
  23505. + *
  23506. * add_input_randomness() uses the input layer interrupt timing, as well as
  23507. * the event type information from the hardware.
  23508. *
  23509. @@ -140,6 +143,13 @@
  23510. * a better measure, since the timing of the disk interrupts are more
  23511. * unpredictable.
  23512. *
  23513. + * random_input_words() just provides a raw block of entropy to the input
  23514. + * pool, such as from a hardware entropy generator.
  23515. + *
  23516. + * random_input_wait() suspends the caller until such time as the
  23517. + * entropy pool falls below the write threshold, and returns a count of how
  23518. + * much entropy (in bits) is needed to sustain the pool.
  23519. + *
  23520. * All of these routines try to estimate how many bits of randomness a
  23521. * particular randomness source. They do this by keeping track of the
  23522. * first and second order deltas of the event timings.
  23523. @@ -712,6 +722,61 @@
  23524. }
  23525. #endif
  23526. +/*
  23527. + * random_input_words - add bulk entropy to pool
  23528. + *
  23529. + * @buf: buffer to add
  23530. + * @wordcount: number of __u32 words to add
  23531. + * @ent_count: total amount of entropy (in bits) to credit
  23532. + *
  23533. + * this provides bulk input of entropy to the input pool
  23534. + *
  23535. + */
  23536. +void random_input_words(__u32 *buf, size_t wordcount, int ent_count)
  23537. +{
  23538. + mix_pool_bytes(&input_pool, buf, wordcount*4);
  23539. +
  23540. + credit_entropy_bits(&input_pool, ent_count);
  23541. +
  23542. + DEBUG_ENT("crediting %d bits => %d\n",
  23543. + ent_count, input_pool.entropy_count);
  23544. + /*
  23545. + * Wake up waiting processes if we have enough
  23546. + * entropy.
  23547. + */
  23548. + if (input_pool.entropy_count >= random_read_wakeup_thresh)
  23549. + wake_up_interruptible(&random_read_wait);
  23550. +}
  23551. +EXPORT_SYMBOL(random_input_words);
  23552. +
  23553. +/*
  23554. + * random_input_wait - wait until random needs entropy
  23555. + *
  23556. + * this function sleeps until the /dev/random subsystem actually
  23557. + * needs more entropy, and then return the amount of entropy
  23558. + * that it would be nice to have added to the system.
  23559. + */
  23560. +int random_input_wait(void)
  23561. +{
  23562. + int count;
  23563. +
  23564. + wait_event_interruptible(random_write_wait,
  23565. + input_pool.entropy_count < random_write_wakeup_thresh);
  23566. +
  23567. + count = random_write_wakeup_thresh - input_pool.entropy_count;
  23568. +
  23569. + /* likely we got woken up due to a signal */
  23570. + if (count <= 0) count = random_read_wakeup_thresh;
  23571. +
  23572. + DEBUG_ENT("requesting %d bits from input_wait()er %d<%d\n",
  23573. + count,
  23574. + input_pool.entropy_count, random_write_wakeup_thresh);
  23575. +
  23576. + return count;
  23577. +}
  23578. +EXPORT_SYMBOL(random_input_wait);
  23579. +
  23580. +
  23581. #define EXTRACT_SIZE 10
  23582. /*********************************************************************
  23583. diff -Nur linux-2.6.30.orig/fs/fcntl.c linux-2.6.30/fs/fcntl.c
  23584. --- linux-2.6.30.orig/fs/fcntl.c 2009-06-10 05:05:27.000000000 +0200
  23585. +++ linux-2.6.30/fs/fcntl.c 2009-06-11 10:55:27.000000000 +0200
  23586. @@ -142,6 +142,7 @@
  23587. }
  23588. return ret;
  23589. }
  23590. +EXPORT_SYMBOL(sys_dup);
  23591. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
  23592. diff -Nur linux-2.6.30.orig/include/linux/miscdevice.h linux-2.6.30/include/linux/miscdevice.h
  23593. --- linux-2.6.30.orig/include/linux/miscdevice.h 2009-06-10 05:05:27.000000000 +0200
  23594. +++ linux-2.6.30/include/linux/miscdevice.h 2009-06-11 10:55:27.000000000 +0200
  23595. @@ -12,6 +12,7 @@
  23596. #define APOLLO_MOUSE_MINOR 7
  23597. #define PC110PAD_MINOR 9
  23598. /*#define ADB_MOUSE_MINOR 10 FIXME OBSOLETE */
  23599. +#define CRYPTODEV_MINOR 70 /* /dev/crypto */
  23600. #define WATCHDOG_MINOR 130 /* Watchdog timer */
  23601. #define TEMP_MINOR 131 /* Temperature Sensor */
  23602. #define RTC_MINOR 135
  23603. diff -Nur linux-2.6.30.orig/include/linux/random.h linux-2.6.30/include/linux/random.h
  23604. --- linux-2.6.30.orig/include/linux/random.h 2009-06-10 05:05:27.000000000 +0200
  23605. +++ linux-2.6.30/include/linux/random.h 2009-06-11 10:55:27.000000000 +0200
  23606. @@ -34,6 +34,30 @@
  23607. /* Clear the entropy pool and associated counters. (Superuser only.) */
  23608. #define RNDCLEARPOOL _IO( 'R', 0x06 )
  23609. +#ifdef CONFIG_FIPS_RNG
  23610. +
  23611. +/* Size of seed value - equal to AES blocksize */
  23612. +#define AES_BLOCK_SIZE_BYTES 16
  23613. +#define SEED_SIZE_BYTES AES_BLOCK_SIZE_BYTES
  23614. +/* Size of AES key */
  23615. +#define KEY_SIZE_BYTES 16
  23616. +
  23617. +/* ioctl() structure used by FIPS 140-2 Tests */
  23618. +struct rand_fips_test {
  23619. + unsigned char key[KEY_SIZE_BYTES]; /* Input */
  23620. + unsigned char datetime[SEED_SIZE_BYTES]; /* Input */
  23621. + unsigned char seed[SEED_SIZE_BYTES]; /* Input */
  23622. + unsigned char result[SEED_SIZE_BYTES]; /* Output */
  23623. +};
  23624. +
  23625. +/* FIPS 140-2 RNG Variable Seed Test. (Superuser only.) */
  23626. +#define RNDFIPSVST _IOWR('R', 0x10, struct rand_fips_test)
  23627. +
  23628. +/* FIPS 140-2 RNG Monte Carlo Test. (Superuser only.) */
  23629. +#define RNDFIPSMCT _IOWR('R', 0x11, struct rand_fips_test)
  23630. +
  23631. +#endif /* #ifdef CONFIG_FIPS_RNG */
  23632. +
  23633. struct rand_pool_info {
  23634. int entropy_count;
  23635. int buf_size;
  23636. @@ -50,6 +74,10 @@
  23637. unsigned int value);
  23638. extern void add_interrupt_randomness(int irq);
  23639. +extern void random_input_words(__u32 *buf, size_t wordcount, int ent_count);
  23640. +extern int random_input_wait(void);
  23641. +#define HAS_RANDOM_INPUT_WAIT 1
  23642. +
  23643. extern void get_random_bytes(void *buf, int nbytes);
  23644. void generate_random_uuid(unsigned char uuid_out[16]);