z90main.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394
  1. /*
  2. * linux/drivers/s390/crypto/z90main.c
  3. *
  4. * z90crypt 1.3.2
  5. *
  6. * Copyright (C) 2001, 2004 IBM Corporation
  7. * Author(s): Robert Burroughs (burrough@us.ibm.com)
  8. * Eric Rossman (edrossma@us.ibm.com)
  9. *
  10. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <asm/uaccess.h> // copy_(from|to)_user
  27. #include <linux/compat.h>
  28. #include <linux/compiler.h>
  29. #include <linux/delay.h> // mdelay
  30. #include <linux/init.h>
  31. #include <linux/interrupt.h> // for tasklets
  32. #include <linux/ioctl32.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/syscalls.h>
  38. #include "z90crypt.h"
  39. #include "z90common.h"
  40. #define VERSION_Z90MAIN_C "$Revision: 1.62 $"
  41. static char z90main_version[] __initdata =
  42. "z90main.o (" VERSION_Z90MAIN_C "/"
  43. VERSION_Z90COMMON_H "/" VERSION_Z90CRYPT_H ")";
  44. extern char z90hardware_version[];
  45. /**
  46. * Defaults that may be modified.
  47. */
  48. /**
  49. * You can specify a different minor at compile time.
  50. */
  51. #ifndef Z90CRYPT_MINOR
  52. #define Z90CRYPT_MINOR MISC_DYNAMIC_MINOR
  53. #endif
  54. /**
  55. * You can specify a different domain at compile time or on the insmod
  56. * command line.
  57. */
  58. #ifndef DOMAIN_INDEX
  59. #define DOMAIN_INDEX -1
  60. #endif
  61. /**
  62. * This is the name under which the device is registered in /proc/modules.
  63. */
  64. #define REG_NAME "z90crypt"
  65. /**
  66. * Cleanup should run every CLEANUPTIME seconds and should clean up requests
  67. * older than CLEANUPTIME seconds in the past.
  68. */
  69. #ifndef CLEANUPTIME
  70. #define CLEANUPTIME 15
  71. #endif
  72. /**
  73. * Config should run every CONFIGTIME seconds
  74. */
  75. #ifndef CONFIGTIME
  76. #define CONFIGTIME 30
  77. #endif
  78. /**
  79. * The first execution of the config task should take place
  80. * immediately after initialization
  81. */
  82. #ifndef INITIAL_CONFIGTIME
  83. #define INITIAL_CONFIGTIME 1
  84. #endif
  85. /**
  86. * Reader should run every READERTIME milliseconds
  87. * With the 100Hz patch for s390, z90crypt can lock the system solid while
  88. * under heavy load. We'll try to avoid that.
  89. */
  90. #ifndef READERTIME
  91. #if HZ > 1000
  92. #define READERTIME 2
  93. #else
  94. #define READERTIME 10
  95. #endif
  96. #endif
  97. /**
  98. * turn long device array index into device pointer
  99. */
  100. #define LONG2DEVPTR(ndx) (z90crypt.device_p[(ndx)])
  101. /**
  102. * turn short device array index into long device array index
  103. */
  104. #define SHRT2LONG(ndx) (z90crypt.overall_device_x.device_index[(ndx)])
  105. /**
  106. * turn short device array index into device pointer
  107. */
  108. #define SHRT2DEVPTR(ndx) LONG2DEVPTR(SHRT2LONG(ndx))
  109. /**
  110. * Status for a work-element
  111. */
  112. #define STAT_DEFAULT 0x00 // request has not been processed
  113. #define STAT_ROUTED 0x80 // bit 7: requests get routed to specific device
  114. // else, device is determined each write
  115. #define STAT_FAILED 0x40 // bit 6: this bit is set if the request failed
  116. // before being sent to the hardware.
  117. #define STAT_WRITTEN 0x30 // bits 5-4: work to be done, not sent to device
  118. // 0x20 // UNUSED state
  119. #define STAT_READPEND 0x10 // bits 5-4: work done, we're returning data now
  120. #define STAT_NOWORK 0x00 // bits off: no work on any queue
  121. #define STAT_RDWRMASK 0x30 // mask for bits 5-4
  122. /**
  123. * Macros to check the status RDWRMASK
  124. */
  125. #define CHK_RDWRMASK(statbyte) ((statbyte) & STAT_RDWRMASK)
  126. #define SET_RDWRMASK(statbyte, newval) \
  127. {(statbyte) &= ~STAT_RDWRMASK; (statbyte) |= newval;}
  128. /**
  129. * Audit Trail. Progress of a Work element
  130. * audit[0]: Unless noted otherwise, these bits are all set by the process
  131. */
  132. #define FP_COPYFROM 0x80 // Caller's buffer has been copied to work element
  133. #define FP_BUFFREQ 0x40 // Low Level buffer requested
  134. #define FP_BUFFGOT 0x20 // Low Level buffer obtained
  135. #define FP_SENT 0x10 // Work element sent to a crypto device
  136. // (may be set by process or by reader task)
  137. #define FP_PENDING 0x08 // Work element placed on pending queue
  138. // (may be set by process or by reader task)
  139. #define FP_REQUEST 0x04 // Work element placed on request queue
  140. #define FP_ASLEEP 0x02 // Work element about to sleep
  141. #define FP_AWAKE 0x01 // Work element has been awakened
  142. /**
  143. * audit[1]: These bits are set by the reader task and/or the cleanup task
  144. */
  145. #define FP_NOTPENDING 0x80 // Work element removed from pending queue
  146. #define FP_AWAKENING 0x40 // Caller about to be awakened
  147. #define FP_TIMEDOUT 0x20 // Caller timed out
  148. #define FP_RESPSIZESET 0x10 // Response size copied to work element
  149. #define FP_RESPADDRCOPIED 0x08 // Response address copied to work element
  150. #define FP_RESPBUFFCOPIED 0x04 // Response buffer copied to work element
  151. #define FP_REMREQUEST 0x02 // Work element removed from request queue
  152. #define FP_SIGNALED 0x01 // Work element was awakened by a signal
  153. /**
  154. * audit[2]: unused
  155. */
  156. /**
  157. * state of the file handle in private_data.status
  158. */
  159. #define STAT_OPEN 0
  160. #define STAT_CLOSED 1
  161. /**
  162. * PID() expands to the process ID of the current process
  163. */
  164. #define PID() (current->pid)
  165. /**
  166. * Selected Constants. The number of APs and the number of devices
  167. */
  168. #ifndef Z90CRYPT_NUM_APS
  169. #define Z90CRYPT_NUM_APS 64
  170. #endif
  171. #ifndef Z90CRYPT_NUM_DEVS
  172. #define Z90CRYPT_NUM_DEVS Z90CRYPT_NUM_APS
  173. #endif
  174. /**
  175. * Buffer size for receiving responses. The maximum Response Size
  176. * is actually the maximum request size, since in an error condition
  177. * the request itself may be returned unchanged.
  178. */
  179. #define MAX_RESPONSE_SIZE 0x0000077C
  180. /**
  181. * A count and status-byte mask
  182. */
  183. struct status {
  184. int st_count; // # of enabled devices
  185. int disabled_count; // # of disabled devices
  186. int user_disabled_count; // # of devices disabled via proc fs
  187. unsigned char st_mask[Z90CRYPT_NUM_APS]; // current status mask
  188. };
  189. /**
  190. * The array of device indexes is a mechanism for fast indexing into
  191. * a long (and sparse) array. For instance, if APs 3, 9 and 47 are
  192. * installed, z90CDeviceIndex[0] is 3, z90CDeviceIndex[1] is 9, and
  193. * z90CDeviceIndex[2] is 47.
  194. */
  195. struct device_x {
  196. int device_index[Z90CRYPT_NUM_DEVS];
  197. };
  198. /**
  199. * All devices are arranged in a single array: 64 APs
  200. */
  201. struct device {
  202. int dev_type; // PCICA, PCICC, PCIXCC_MCL2,
  203. // PCIXCC_MCL3, CEX2C, CEX2A
  204. enum devstat dev_stat; // current device status
  205. int dev_self_x; // Index in array
  206. int disabled; // Set when device is in error
  207. int user_disabled; // Set when device is disabled by user
  208. int dev_q_depth; // q depth
  209. unsigned char * dev_resp_p; // Response buffer address
  210. int dev_resp_l; // Response Buffer length
  211. int dev_caller_count; // Number of callers
  212. int dev_total_req_cnt; // # requests for device since load
  213. struct list_head dev_caller_list; // List of callers
  214. };
  215. /**
  216. * There's a struct status and a struct device_x for each device type.
  217. */
  218. struct hdware_block {
  219. struct status hdware_mask;
  220. struct status type_mask[Z90CRYPT_NUM_TYPES];
  221. struct device_x type_x_addr[Z90CRYPT_NUM_TYPES];
  222. unsigned char device_type_array[Z90CRYPT_NUM_APS];
  223. };
  224. /**
  225. * z90crypt is the topmost data structure in the hierarchy.
  226. */
  227. struct z90crypt {
  228. int max_count; // Nr of possible crypto devices
  229. struct status mask;
  230. int q_depth_array[Z90CRYPT_NUM_DEVS];
  231. int dev_type_array[Z90CRYPT_NUM_DEVS];
  232. struct device_x overall_device_x; // array device indexes
  233. struct device * device_p[Z90CRYPT_NUM_DEVS];
  234. int terminating;
  235. int domain_established;// TRUE: domain has been found
  236. int cdx; // Crypto Domain Index
  237. int len; // Length of this data structure
  238. struct hdware_block *hdware_info;
  239. };
  240. /**
  241. * An array of these structures is pointed to from dev_caller
  242. * The length of the array depends on the device type. For APs,
  243. * there are 8.
  244. *
  245. * The caller buffer is allocated to the user at OPEN. At WRITE,
  246. * it contains the request; at READ, the response. The function
  247. * send_to_crypto_device converts the request to device-dependent
  248. * form and use the caller's OPEN-allocated buffer for the response.
  249. *
  250. * For the contents of caller_dev_dep_req and caller_dev_dep_req_p
  251. * because that points to it, see the discussion in z90hardware.c.
  252. * Search for "extended request message block".
  253. */
  254. struct caller {
  255. int caller_buf_l; // length of original request
  256. unsigned char * caller_buf_p; // Original request on WRITE
  257. int caller_dev_dep_req_l; // len device dependent request
  258. unsigned char * caller_dev_dep_req_p; // Device dependent form
  259. unsigned char caller_id[8]; // caller-supplied message id
  260. struct list_head caller_liste;
  261. unsigned char caller_dev_dep_req[MAX_RESPONSE_SIZE];
  262. };
  263. /**
  264. * Function prototypes from z90hardware.c
  265. */
  266. enum hdstat query_online(int deviceNr, int cdx, int resetNr, int *q_depth,
  267. int *dev_type);
  268. enum devstat reset_device(int deviceNr, int cdx, int resetNr);
  269. enum devstat send_to_AP(int dev_nr, int cdx, int msg_len, unsigned char *msg_ext);
  270. enum devstat receive_from_AP(int dev_nr, int cdx, int resplen,
  271. unsigned char *resp, unsigned char *psmid);
  272. int convert_request(unsigned char *buffer, int func, unsigned short function,
  273. int cdx, int dev_type, int *msg_l_p, unsigned char *msg_p);
  274. int convert_response(unsigned char *response, unsigned char *buffer,
  275. int *respbufflen_p, unsigned char *resp_buff);
  276. /**
  277. * Low level function prototypes
  278. */
  279. static int create_z90crypt(int *cdx_p);
  280. static int refresh_z90crypt(int *cdx_p);
  281. static int find_crypto_devices(struct status *deviceMask);
  282. static int create_crypto_device(int index);
  283. static int destroy_crypto_device(int index);
  284. static void destroy_z90crypt(void);
  285. static int refresh_index_array(struct status *status_str,
  286. struct device_x *index_array);
  287. static int probe_device_type(struct device *devPtr);
  288. static int probe_PCIXCC_type(struct device *devPtr);
  289. /**
  290. * proc fs definitions
  291. */
  292. static struct proc_dir_entry *z90crypt_entry;
  293. /**
  294. * data structures
  295. */
  296. /**
  297. * work_element.opener points back to this structure
  298. */
  299. struct priv_data {
  300. pid_t opener_pid;
  301. unsigned char status; // 0: open 1: closed
  302. };
  303. /**
  304. * A work element is allocated for each request
  305. */
  306. struct work_element {
  307. struct priv_data *priv_data;
  308. pid_t pid;
  309. int devindex; // index of device processing this w_e
  310. // (If request did not specify device,
  311. // -1 until placed onto a queue)
  312. int devtype;
  313. struct list_head liste; // used for requestq and pendingq
  314. char buffer[128]; // local copy of user request
  315. int buff_size; // size of the buffer for the request
  316. char resp_buff[RESPBUFFSIZE];
  317. int resp_buff_size;
  318. char __user * resp_addr; // address of response in user space
  319. unsigned int funccode; // function code of request
  320. wait_queue_head_t waitq;
  321. unsigned long requestsent; // time at which the request was sent
  322. atomic_t alarmrung; // wake-up signal
  323. unsigned char caller_id[8]; // pid + counter, for this w_e
  324. unsigned char status[1]; // bits to mark status of the request
  325. unsigned char audit[3]; // record of work element's progress
  326. unsigned char * requestptr; // address of request buffer
  327. int retcode; // return code of request
  328. };
  329. /**
  330. * High level function prototypes
  331. */
  332. static int z90crypt_open(struct inode *, struct file *);
  333. static int z90crypt_release(struct inode *, struct file *);
  334. static ssize_t z90crypt_read(struct file *, char __user *, size_t, loff_t *);
  335. static ssize_t z90crypt_write(struct file *, const char __user *,
  336. size_t, loff_t *);
  337. static long z90crypt_unlocked_ioctl(struct file *, unsigned int, unsigned long);
  338. static long z90crypt_compat_ioctl(struct file *, unsigned int, unsigned long);
  339. static void z90crypt_reader_task(unsigned long);
  340. static void z90crypt_schedule_reader_task(unsigned long);
  341. static void z90crypt_config_task(unsigned long);
  342. static void z90crypt_cleanup_task(unsigned long);
  343. static int z90crypt_status(char *, char **, off_t, int, int *, void *);
  344. static int z90crypt_status_write(struct file *, const char __user *,
  345. unsigned long, void *);
  346. /**
  347. * Storage allocated at initialization and used throughout the life of
  348. * this insmod
  349. */
  350. static int domain = DOMAIN_INDEX;
  351. static struct z90crypt z90crypt;
  352. static int quiesce_z90crypt;
  353. static spinlock_t queuespinlock;
  354. static struct list_head request_list;
  355. static int requestq_count;
  356. static struct list_head pending_list;
  357. static int pendingq_count;
  358. static struct tasklet_struct reader_tasklet;
  359. static struct timer_list reader_timer;
  360. static struct timer_list config_timer;
  361. static struct timer_list cleanup_timer;
  362. static atomic_t total_open;
  363. static atomic_t z90crypt_step;
  364. static struct file_operations z90crypt_fops = {
  365. .owner = THIS_MODULE,
  366. .read = z90crypt_read,
  367. .write = z90crypt_write,
  368. .unlocked_ioctl = z90crypt_unlocked_ioctl,
  369. #ifdef CONFIG_COMPAT
  370. .compat_ioctl = z90crypt_compat_ioctl,
  371. #endif
  372. .open = z90crypt_open,
  373. .release = z90crypt_release
  374. };
  375. static struct miscdevice z90crypt_misc_device = {
  376. .minor = Z90CRYPT_MINOR,
  377. .name = DEV_NAME,
  378. .fops = &z90crypt_fops,
  379. .devfs_name = DEV_NAME
  380. };
  381. /**
  382. * Documentation values.
  383. */
  384. MODULE_AUTHOR("zSeries Linux Crypto Team: Robert H. Burroughs, Eric D. Rossman"
  385. "and Jochen Roehrig");
  386. MODULE_DESCRIPTION("zSeries Linux Cryptographic Coprocessor device driver, "
  387. "Copyright 2001, 2005 IBM Corporation");
  388. MODULE_LICENSE("GPL");
  389. module_param(domain, int, 0);
  390. MODULE_PARM_DESC(domain, "domain index for device");
  391. #ifdef CONFIG_COMPAT
  392. /**
  393. * ioctl32 conversion routines
  394. */
  395. struct ica_rsa_modexpo_32 { // For 32-bit callers
  396. compat_uptr_t inputdata;
  397. unsigned int inputdatalength;
  398. compat_uptr_t outputdata;
  399. unsigned int outputdatalength;
  400. compat_uptr_t b_key;
  401. compat_uptr_t n_modulus;
  402. };
  403. static long
  404. trans_modexpo32(struct file *filp, unsigned int cmd, unsigned long arg)
  405. {
  406. struct ica_rsa_modexpo_32 __user *mex32u = compat_ptr(arg);
  407. struct ica_rsa_modexpo_32 mex32k;
  408. struct ica_rsa_modexpo __user *mex64;
  409. long ret = 0;
  410. unsigned int i;
  411. if (!access_ok(VERIFY_WRITE, mex32u, sizeof(struct ica_rsa_modexpo_32)))
  412. return -EFAULT;
  413. mex64 = compat_alloc_user_space(sizeof(struct ica_rsa_modexpo));
  414. if (!access_ok(VERIFY_WRITE, mex64, sizeof(struct ica_rsa_modexpo)))
  415. return -EFAULT;
  416. if (copy_from_user(&mex32k, mex32u, sizeof(struct ica_rsa_modexpo_32)))
  417. return -EFAULT;
  418. if (__put_user(compat_ptr(mex32k.inputdata), &mex64->inputdata) ||
  419. __put_user(mex32k.inputdatalength, &mex64->inputdatalength) ||
  420. __put_user(compat_ptr(mex32k.outputdata), &mex64->outputdata) ||
  421. __put_user(mex32k.outputdatalength, &mex64->outputdatalength) ||
  422. __put_user(compat_ptr(mex32k.b_key), &mex64->b_key) ||
  423. __put_user(compat_ptr(mex32k.n_modulus), &mex64->n_modulus))
  424. return -EFAULT;
  425. ret = z90crypt_unlocked_ioctl(filp, cmd, (unsigned long)mex64);
  426. if (!ret)
  427. if (__get_user(i, &mex64->outputdatalength) ||
  428. __put_user(i, &mex32u->outputdatalength))
  429. ret = -EFAULT;
  430. return ret;
  431. }
  432. struct ica_rsa_modexpo_crt_32 { // For 32-bit callers
  433. compat_uptr_t inputdata;
  434. unsigned int inputdatalength;
  435. compat_uptr_t outputdata;
  436. unsigned int outputdatalength;
  437. compat_uptr_t bp_key;
  438. compat_uptr_t bq_key;
  439. compat_uptr_t np_prime;
  440. compat_uptr_t nq_prime;
  441. compat_uptr_t u_mult_inv;
  442. };
  443. static long
  444. trans_modexpo_crt32(struct file *filp, unsigned int cmd, unsigned long arg)
  445. {
  446. struct ica_rsa_modexpo_crt_32 __user *crt32u = compat_ptr(arg);
  447. struct ica_rsa_modexpo_crt_32 crt32k;
  448. struct ica_rsa_modexpo_crt __user *crt64;
  449. long ret = 0;
  450. unsigned int i;
  451. if (!access_ok(VERIFY_WRITE, crt32u,
  452. sizeof(struct ica_rsa_modexpo_crt_32)))
  453. return -EFAULT;
  454. crt64 = compat_alloc_user_space(sizeof(struct ica_rsa_modexpo_crt));
  455. if (!access_ok(VERIFY_WRITE, crt64, sizeof(struct ica_rsa_modexpo_crt)))
  456. return -EFAULT;
  457. if (copy_from_user(&crt32k, crt32u,
  458. sizeof(struct ica_rsa_modexpo_crt_32)))
  459. return -EFAULT;
  460. if (__put_user(compat_ptr(crt32k.inputdata), &crt64->inputdata) ||
  461. __put_user(crt32k.inputdatalength, &crt64->inputdatalength) ||
  462. __put_user(compat_ptr(crt32k.outputdata), &crt64->outputdata) ||
  463. __put_user(crt32k.outputdatalength, &crt64->outputdatalength) ||
  464. __put_user(compat_ptr(crt32k.bp_key), &crt64->bp_key) ||
  465. __put_user(compat_ptr(crt32k.bq_key), &crt64->bq_key) ||
  466. __put_user(compat_ptr(crt32k.np_prime), &crt64->np_prime) ||
  467. __put_user(compat_ptr(crt32k.nq_prime), &crt64->nq_prime) ||
  468. __put_user(compat_ptr(crt32k.u_mult_inv), &crt64->u_mult_inv))
  469. return -EFAULT;
  470. ret = z90crypt_unlocked_ioctl(filp, cmd, (unsigned long)crt64);
  471. if (!ret)
  472. if (__get_user(i, &crt64->outputdatalength) ||
  473. __put_user(i, &crt32u->outputdatalength))
  474. ret = -EFAULT;
  475. return ret;
  476. }
  477. static long
  478. z90crypt_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  479. {
  480. switch (cmd) {
  481. case ICAZ90STATUS:
  482. case Z90QUIESCE:
  483. case Z90STAT_TOTALCOUNT:
  484. case Z90STAT_PCICACOUNT:
  485. case Z90STAT_PCICCCOUNT:
  486. case Z90STAT_PCIXCCCOUNT:
  487. case Z90STAT_PCIXCCMCL2COUNT:
  488. case Z90STAT_PCIXCCMCL3COUNT:
  489. case Z90STAT_CEX2CCOUNT:
  490. case Z90STAT_REQUESTQ_COUNT:
  491. case Z90STAT_PENDINGQ_COUNT:
  492. case Z90STAT_TOTALOPEN_COUNT:
  493. case Z90STAT_DOMAIN_INDEX:
  494. case Z90STAT_STATUS_MASK:
  495. case Z90STAT_QDEPTH_MASK:
  496. case Z90STAT_PERDEV_REQCNT:
  497. return z90crypt_unlocked_ioctl(filp, cmd, arg);
  498. case ICARSAMODEXPO:
  499. return trans_modexpo32(filp, cmd, arg);
  500. case ICARSACRT:
  501. return trans_modexpo_crt32(filp, cmd, arg);
  502. default:
  503. return -ENOIOCTLCMD;
  504. }
  505. }
  506. #endif
  507. /**
  508. * The module initialization code.
  509. */
  510. static int __init
  511. z90crypt_init_module(void)
  512. {
  513. int result, nresult;
  514. struct proc_dir_entry *entry;
  515. PDEBUG("PID %d\n", PID());
  516. if ((domain < -1) || (domain > 15)) {
  517. PRINTKW("Invalid param: domain = %d. Not loading.\n", domain);
  518. return -EINVAL;
  519. }
  520. /* Register as misc device with given minor (or get a dynamic one). */
  521. result = misc_register(&z90crypt_misc_device);
  522. if (result < 0) {
  523. PRINTKW(KERN_ERR "misc_register (minor %d) failed with %d\n",
  524. z90crypt_misc_device.minor, result);
  525. return result;
  526. }
  527. PDEBUG("Registered " DEV_NAME " with result %d\n", result);
  528. result = create_z90crypt(&domain);
  529. if (result != 0) {
  530. PRINTKW("create_z90crypt (domain index %d) failed with %d.\n",
  531. domain, result);
  532. result = -ENOMEM;
  533. goto init_module_cleanup;
  534. }
  535. if (result == 0) {
  536. PRINTKN("Version %d.%d.%d loaded, built on %s %s\n",
  537. z90crypt_VERSION, z90crypt_RELEASE, z90crypt_VARIANT,
  538. __DATE__, __TIME__);
  539. PRINTKN("%s\n", z90main_version);
  540. PRINTKN("%s\n", z90hardware_version);
  541. PDEBUG("create_z90crypt (domain index %d) successful.\n",
  542. domain);
  543. } else
  544. PRINTK("No devices at startup\n");
  545. /* Initialize globals. */
  546. spin_lock_init(&queuespinlock);
  547. INIT_LIST_HEAD(&pending_list);
  548. pendingq_count = 0;
  549. INIT_LIST_HEAD(&request_list);
  550. requestq_count = 0;
  551. quiesce_z90crypt = 0;
  552. atomic_set(&total_open, 0);
  553. atomic_set(&z90crypt_step, 0);
  554. /* Set up the cleanup task. */
  555. init_timer(&cleanup_timer);
  556. cleanup_timer.function = z90crypt_cleanup_task;
  557. cleanup_timer.data = 0;
  558. cleanup_timer.expires = jiffies + (CLEANUPTIME * HZ);
  559. add_timer(&cleanup_timer);
  560. /* Set up the proc file system */
  561. entry = create_proc_entry("driver/z90crypt", 0644, 0);
  562. if (entry) {
  563. entry->nlink = 1;
  564. entry->data = 0;
  565. entry->read_proc = z90crypt_status;
  566. entry->write_proc = z90crypt_status_write;
  567. }
  568. else
  569. PRINTK("Couldn't create z90crypt proc entry\n");
  570. z90crypt_entry = entry;
  571. /* Set up the configuration task. */
  572. init_timer(&config_timer);
  573. config_timer.function = z90crypt_config_task;
  574. config_timer.data = 0;
  575. config_timer.expires = jiffies + (INITIAL_CONFIGTIME * HZ);
  576. add_timer(&config_timer);
  577. /* Set up the reader task */
  578. tasklet_init(&reader_tasklet, z90crypt_reader_task, 0);
  579. init_timer(&reader_timer);
  580. reader_timer.function = z90crypt_schedule_reader_task;
  581. reader_timer.data = 0;
  582. reader_timer.expires = jiffies + (READERTIME * HZ / 1000);
  583. add_timer(&reader_timer);
  584. return 0; // success
  585. init_module_cleanup:
  586. if ((nresult = misc_deregister(&z90crypt_misc_device)))
  587. PRINTK("misc_deregister failed with %d.\n", nresult);
  588. else
  589. PDEBUG("misc_deregister successful.\n");
  590. return result; // failure
  591. }
  592. /**
  593. * The module termination code
  594. */
  595. static void __exit
  596. z90crypt_cleanup_module(void)
  597. {
  598. int nresult;
  599. PDEBUG("PID %d\n", PID());
  600. remove_proc_entry("driver/z90crypt", 0);
  601. if ((nresult = misc_deregister(&z90crypt_misc_device)))
  602. PRINTK("misc_deregister failed with %d.\n", nresult);
  603. else
  604. PDEBUG("misc_deregister successful.\n");
  605. /* Remove the tasks */
  606. tasklet_kill(&reader_tasklet);
  607. del_timer(&reader_timer);
  608. del_timer(&config_timer);
  609. del_timer(&cleanup_timer);
  610. destroy_z90crypt();
  611. PRINTKN("Unloaded.\n");
  612. }
  613. /**
  614. * Functions running under a process id
  615. *
  616. * The I/O functions:
  617. * z90crypt_open
  618. * z90crypt_release
  619. * z90crypt_read
  620. * z90crypt_write
  621. * z90crypt_unlocked_ioctl
  622. * z90crypt_status
  623. * z90crypt_status_write
  624. * disable_card
  625. * enable_card
  626. *
  627. * Helper functions:
  628. * z90crypt_rsa
  629. * z90crypt_prepare
  630. * z90crypt_send
  631. * z90crypt_process_results
  632. *
  633. */
  634. static int
  635. z90crypt_open(struct inode *inode, struct file *filp)
  636. {
  637. struct priv_data *private_data_p;
  638. if (quiesce_z90crypt)
  639. return -EQUIESCE;
  640. private_data_p = kmalloc(sizeof(struct priv_data), GFP_KERNEL);
  641. if (!private_data_p) {
  642. PRINTK("Memory allocate failed\n");
  643. return -ENOMEM;
  644. }
  645. memset((void *)private_data_p, 0, sizeof(struct priv_data));
  646. private_data_p->status = STAT_OPEN;
  647. private_data_p->opener_pid = PID();
  648. filp->private_data = private_data_p;
  649. atomic_inc(&total_open);
  650. return 0;
  651. }
  652. static int
  653. z90crypt_release(struct inode *inode, struct file *filp)
  654. {
  655. struct priv_data *private_data_p = filp->private_data;
  656. PDEBUG("PID %d (filp %p)\n", PID(), filp);
  657. private_data_p->status = STAT_CLOSED;
  658. memset(private_data_p, 0, sizeof(struct priv_data));
  659. kfree(private_data_p);
  660. atomic_dec(&total_open);
  661. return 0;
  662. }
  663. /*
  664. * there are two read functions, of which compile options will choose one
  665. * without USE_GET_RANDOM_BYTES
  666. * => read() always returns -EPERM;
  667. * otherwise
  668. * => read() uses get_random_bytes() kernel function
  669. */
  670. #ifndef USE_GET_RANDOM_BYTES
  671. /**
  672. * z90crypt_read will not be supported beyond z90crypt 1.3.1
  673. */
  674. static ssize_t
  675. z90crypt_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  676. {
  677. PDEBUG("filp %p (PID %d)\n", filp, PID());
  678. return -EPERM;
  679. }
  680. #else // we want to use get_random_bytes
  681. /**
  682. * read() just returns a string of random bytes. Since we have no way
  683. * to generate these cryptographically, we just execute get_random_bytes
  684. * for the length specified.
  685. */
  686. #include <linux/random.h>
  687. static ssize_t
  688. z90crypt_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  689. {
  690. unsigned char *temp_buff;
  691. PDEBUG("filp %p (PID %d)\n", filp, PID());
  692. if (quiesce_z90crypt)
  693. return -EQUIESCE;
  694. if (count < 0) {
  695. PRINTK("Requested random byte count negative: %ld\n", count);
  696. return -EINVAL;
  697. }
  698. if (count > RESPBUFFSIZE) {
  699. PDEBUG("count[%d] > RESPBUFFSIZE", count);
  700. return -EINVAL;
  701. }
  702. if (count == 0)
  703. return 0;
  704. temp_buff = kmalloc(RESPBUFFSIZE, GFP_KERNEL);
  705. if (!temp_buff) {
  706. PRINTK("Memory allocate failed\n");
  707. return -ENOMEM;
  708. }
  709. get_random_bytes(temp_buff, count);
  710. if (copy_to_user(buf, temp_buff, count) != 0) {
  711. kfree(temp_buff);
  712. return -EFAULT;
  713. }
  714. kfree(temp_buff);
  715. return count;
  716. }
  717. #endif
  718. /**
  719. * Write is is not allowed
  720. */
  721. static ssize_t
  722. z90crypt_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
  723. {
  724. PDEBUG("filp %p (PID %d)\n", filp, PID());
  725. return -EPERM;
  726. }
  727. /**
  728. * New status functions
  729. */
  730. static inline int
  731. get_status_totalcount(void)
  732. {
  733. return z90crypt.hdware_info->hdware_mask.st_count;
  734. }
  735. static inline int
  736. get_status_PCICAcount(void)
  737. {
  738. return z90crypt.hdware_info->type_mask[PCICA].st_count;
  739. }
  740. static inline int
  741. get_status_PCICCcount(void)
  742. {
  743. return z90crypt.hdware_info->type_mask[PCICC].st_count;
  744. }
  745. static inline int
  746. get_status_PCIXCCcount(void)
  747. {
  748. return z90crypt.hdware_info->type_mask[PCIXCC_MCL2].st_count +
  749. z90crypt.hdware_info->type_mask[PCIXCC_MCL3].st_count;
  750. }
  751. static inline int
  752. get_status_PCIXCCMCL2count(void)
  753. {
  754. return z90crypt.hdware_info->type_mask[PCIXCC_MCL2].st_count;
  755. }
  756. static inline int
  757. get_status_PCIXCCMCL3count(void)
  758. {
  759. return z90crypt.hdware_info->type_mask[PCIXCC_MCL3].st_count;
  760. }
  761. static inline int
  762. get_status_CEX2Ccount(void)
  763. {
  764. return z90crypt.hdware_info->type_mask[CEX2C].st_count;
  765. }
  766. static inline int
  767. get_status_CEX2Acount(void)
  768. {
  769. return z90crypt.hdware_info->type_mask[CEX2A].st_count;
  770. }
  771. static inline int
  772. get_status_requestq_count(void)
  773. {
  774. return requestq_count;
  775. }
  776. static inline int
  777. get_status_pendingq_count(void)
  778. {
  779. return pendingq_count;
  780. }
  781. static inline int
  782. get_status_totalopen_count(void)
  783. {
  784. return atomic_read(&total_open);
  785. }
  786. static inline int
  787. get_status_domain_index(void)
  788. {
  789. return z90crypt.cdx;
  790. }
  791. static inline unsigned char *
  792. get_status_status_mask(unsigned char status[Z90CRYPT_NUM_APS])
  793. {
  794. int i, ix;
  795. memcpy(status, z90crypt.hdware_info->device_type_array,
  796. Z90CRYPT_NUM_APS);
  797. for (i = 0; i < get_status_totalcount(); i++) {
  798. ix = SHRT2LONG(i);
  799. if (LONG2DEVPTR(ix)->user_disabled)
  800. status[ix] = 0x0d;
  801. }
  802. return status;
  803. }
  804. static inline unsigned char *
  805. get_status_qdepth_mask(unsigned char qdepth[Z90CRYPT_NUM_APS])
  806. {
  807. int i, ix;
  808. memset(qdepth, 0, Z90CRYPT_NUM_APS);
  809. for (i = 0; i < get_status_totalcount(); i++) {
  810. ix = SHRT2LONG(i);
  811. qdepth[ix] = LONG2DEVPTR(ix)->dev_caller_count;
  812. }
  813. return qdepth;
  814. }
  815. static inline unsigned int *
  816. get_status_perdevice_reqcnt(unsigned int reqcnt[Z90CRYPT_NUM_APS])
  817. {
  818. int i, ix;
  819. memset(reqcnt, 0, Z90CRYPT_NUM_APS * sizeof(int));
  820. for (i = 0; i < get_status_totalcount(); i++) {
  821. ix = SHRT2LONG(i);
  822. reqcnt[ix] = LONG2DEVPTR(ix)->dev_total_req_cnt;
  823. }
  824. return reqcnt;
  825. }
  826. static inline void
  827. init_work_element(struct work_element *we_p,
  828. struct priv_data *priv_data, pid_t pid)
  829. {
  830. int step;
  831. we_p->requestptr = (unsigned char *)we_p + sizeof(struct work_element);
  832. /* Come up with a unique id for this caller. */
  833. step = atomic_inc_return(&z90crypt_step);
  834. memcpy(we_p->caller_id+0, (void *) &pid, sizeof(pid));
  835. memcpy(we_p->caller_id+4, (void *) &step, sizeof(step));
  836. we_p->pid = pid;
  837. we_p->priv_data = priv_data;
  838. we_p->status[0] = STAT_DEFAULT;
  839. we_p->audit[0] = 0x00;
  840. we_p->audit[1] = 0x00;
  841. we_p->audit[2] = 0x00;
  842. we_p->resp_buff_size = 0;
  843. we_p->retcode = 0;
  844. we_p->devindex = -1;
  845. we_p->devtype = -1;
  846. atomic_set(&we_p->alarmrung, 0);
  847. init_waitqueue_head(&we_p->waitq);
  848. INIT_LIST_HEAD(&(we_p->liste));
  849. }
  850. static inline int
  851. allocate_work_element(struct work_element **we_pp,
  852. struct priv_data *priv_data_p, pid_t pid)
  853. {
  854. struct work_element *we_p;
  855. we_p = (struct work_element *) get_zeroed_page(GFP_KERNEL);
  856. if (!we_p)
  857. return -ENOMEM;
  858. init_work_element(we_p, priv_data_p, pid);
  859. *we_pp = we_p;
  860. return 0;
  861. }
  862. static inline void
  863. remove_device(struct device *device_p)
  864. {
  865. if (!device_p || (device_p->disabled != 0))
  866. return;
  867. device_p->disabled = 1;
  868. z90crypt.hdware_info->type_mask[device_p->dev_type].disabled_count++;
  869. z90crypt.hdware_info->hdware_mask.disabled_count++;
  870. }
  871. /**
  872. * Bitlength limits for each card
  873. *
  874. * There are new MCLs which allow more bitlengths. See the table for details.
  875. * The MCL must be applied and the newer bitlengths enabled for these to work.
  876. *
  877. * Card Type Old limit New limit
  878. * PCICA ??-2048 same (the lower limit is less than 128 bit...)
  879. * PCICC 512-1024 512-2048
  880. * PCIXCC_MCL2 512-2048 ----- (applying any GA LIC will make an MCL3 card)
  881. * PCIXCC_MCL3 ----- 128-2048
  882. * CEX2C 512-2048 128-2048
  883. *
  884. * ext_bitlens (extended bitlengths) is a global, since you should not apply an
  885. * MCL to just one card in a machine. We assume, at first, that all cards have
  886. * these capabilities.
  887. */
  888. int ext_bitlens = 1; // This is global
  889. #define PCIXCC_MIN_MOD_SIZE 16 // 128 bits
  890. #define OLD_PCIXCC_MIN_MOD_SIZE 64 // 512 bits
  891. #define PCICC_MIN_MOD_SIZE 64 // 512 bits
  892. #define OLD_PCICC_MAX_MOD_SIZE 128 // 1024 bits
  893. #define MAX_MOD_SIZE 256 // 2048 bits
  894. static inline int
  895. select_device_type(int *dev_type_p, int bytelength)
  896. {
  897. static int count = 0;
  898. int PCICA_avail, PCIXCC_MCL3_avail, CEX2C_avail, CEX2A_avail,
  899. index_to_use;
  900. struct status *stat;
  901. if ((*dev_type_p != PCICC) && (*dev_type_p != PCICA) &&
  902. (*dev_type_p != PCIXCC_MCL2) && (*dev_type_p != PCIXCC_MCL3) &&
  903. (*dev_type_p != CEX2C) && (*dev_type_p != CEX2A) &&
  904. (*dev_type_p != ANYDEV))
  905. return -1;
  906. if (*dev_type_p != ANYDEV) {
  907. stat = &z90crypt.hdware_info->type_mask[*dev_type_p];
  908. if (stat->st_count >
  909. (stat->disabled_count + stat->user_disabled_count))
  910. return 0;
  911. return -1;
  912. }
  913. /**
  914. * Assumption: PCICA, PCIXCC_MCL3, CEX2C, and CEX2A are all similar in
  915. * speed.
  916. *
  917. * PCICA and CEX2A do NOT co-exist, so it would be either one or the
  918. * other present.
  919. */
  920. stat = &z90crypt.hdware_info->type_mask[PCICA];
  921. PCICA_avail = stat->st_count -
  922. (stat->disabled_count + stat->user_disabled_count);
  923. stat = &z90crypt.hdware_info->type_mask[PCIXCC_MCL3];
  924. PCIXCC_MCL3_avail = stat->st_count -
  925. (stat->disabled_count + stat->user_disabled_count);
  926. stat = &z90crypt.hdware_info->type_mask[CEX2C];
  927. CEX2C_avail = stat->st_count -
  928. (stat->disabled_count + stat->user_disabled_count);
  929. stat = &z90crypt.hdware_info->type_mask[CEX2A];
  930. CEX2A_avail = stat->st_count -
  931. (stat->disabled_count + stat->user_disabled_count);
  932. if (PCICA_avail || PCIXCC_MCL3_avail || CEX2C_avail || CEX2A_avail) {
  933. /**
  934. * bitlength is a factor, PCICA or CEX2A are the most capable,
  935. * even with the new MCL for PCIXCC.
  936. */
  937. if ((bytelength < PCIXCC_MIN_MOD_SIZE) ||
  938. (!ext_bitlens && (bytelength < OLD_PCIXCC_MIN_MOD_SIZE))) {
  939. if (PCICA_avail) {
  940. *dev_type_p = PCICA;
  941. return 0;
  942. }
  943. if (CEX2A_avail) {
  944. *dev_type_p = CEX2A;
  945. return 0;
  946. }
  947. return -1;
  948. }
  949. index_to_use = count % (PCICA_avail + PCIXCC_MCL3_avail +
  950. CEX2C_avail + CEX2A_avail);
  951. if (index_to_use < PCICA_avail)
  952. *dev_type_p = PCICA;
  953. else if (index_to_use < (PCICA_avail + PCIXCC_MCL3_avail))
  954. *dev_type_p = PCIXCC_MCL3;
  955. else if (index_to_use < (PCICA_avail + PCIXCC_MCL3_avail +
  956. CEX2C_avail))
  957. *dev_type_p = CEX2C;
  958. else
  959. *dev_type_p = CEX2A;
  960. count++;
  961. return 0;
  962. }
  963. /* Less than OLD_PCIXCC_MIN_MOD_SIZE cannot go to a PCIXCC_MCL2 */
  964. if (bytelength < OLD_PCIXCC_MIN_MOD_SIZE)
  965. return -1;
  966. stat = &z90crypt.hdware_info->type_mask[PCIXCC_MCL2];
  967. if (stat->st_count >
  968. (stat->disabled_count + stat->user_disabled_count)) {
  969. *dev_type_p = PCIXCC_MCL2;
  970. return 0;
  971. }
  972. /**
  973. * Less than PCICC_MIN_MOD_SIZE or more than OLD_PCICC_MAX_MOD_SIZE
  974. * (if we don't have the MCL applied and the newer bitlengths enabled)
  975. * cannot go to a PCICC
  976. */
  977. if ((bytelength < PCICC_MIN_MOD_SIZE) ||
  978. (!ext_bitlens && (bytelength > OLD_PCICC_MAX_MOD_SIZE))) {
  979. return -1;
  980. }
  981. stat = &z90crypt.hdware_info->type_mask[PCICC];
  982. if (stat->st_count >
  983. (stat->disabled_count + stat->user_disabled_count)) {
  984. *dev_type_p = PCICC;
  985. return 0;
  986. }
  987. return -1;
  988. }
  989. /**
  990. * Try the selected number, then the selected type (can be ANYDEV)
  991. */
  992. static inline int
  993. select_device(int *dev_type_p, int *device_nr_p, int bytelength)
  994. {
  995. int i, indx, devTp, low_count, low_indx;
  996. struct device_x *index_p;
  997. struct device *dev_ptr;
  998. PDEBUG("device type = %d, index = %d\n", *dev_type_p, *device_nr_p);
  999. if ((*device_nr_p >= 0) && (*device_nr_p < Z90CRYPT_NUM_DEVS)) {
  1000. PDEBUG("trying index = %d\n", *device_nr_p);
  1001. dev_ptr = z90crypt.device_p[*device_nr_p];
  1002. if (dev_ptr &&
  1003. (dev_ptr->dev_stat != DEV_GONE) &&
  1004. (dev_ptr->disabled == 0) &&
  1005. (dev_ptr->user_disabled == 0)) {
  1006. PDEBUG("selected by number, index = %d\n",
  1007. *device_nr_p);
  1008. *dev_type_p = dev_ptr->dev_type;
  1009. return *device_nr_p;
  1010. }
  1011. }
  1012. *device_nr_p = -1;
  1013. PDEBUG("trying type = %d\n", *dev_type_p);
  1014. devTp = *dev_type_p;
  1015. if (select_device_type(&devTp, bytelength) == -1) {
  1016. PDEBUG("failed to select by type\n");
  1017. return -1;
  1018. }
  1019. PDEBUG("selected type = %d\n", devTp);
  1020. index_p = &z90crypt.hdware_info->type_x_addr[devTp];
  1021. low_count = 0x0000FFFF;
  1022. low_indx = -1;
  1023. for (i = 0; i < z90crypt.hdware_info->type_mask[devTp].st_count; i++) {
  1024. indx = index_p->device_index[i];
  1025. dev_ptr = z90crypt.device_p[indx];
  1026. if (dev_ptr &&
  1027. (dev_ptr->dev_stat != DEV_GONE) &&
  1028. (dev_ptr->disabled == 0) &&
  1029. (dev_ptr->user_disabled == 0) &&
  1030. (devTp == dev_ptr->dev_type) &&
  1031. (low_count > dev_ptr->dev_caller_count)) {
  1032. low_count = dev_ptr->dev_caller_count;
  1033. low_indx = indx;
  1034. }
  1035. }
  1036. *device_nr_p = low_indx;
  1037. return low_indx;
  1038. }
  1039. static inline int
  1040. send_to_crypto_device(struct work_element *we_p)
  1041. {
  1042. struct caller *caller_p;
  1043. struct device *device_p;
  1044. int dev_nr;
  1045. int bytelen = ((struct ica_rsa_modexpo *)we_p->buffer)->inputdatalength;
  1046. if (!we_p->requestptr)
  1047. return SEN_FATAL_ERROR;
  1048. caller_p = (struct caller *)we_p->requestptr;
  1049. dev_nr = we_p->devindex;
  1050. if (select_device(&we_p->devtype, &dev_nr, bytelen) == -1) {
  1051. if (z90crypt.hdware_info->hdware_mask.st_count != 0)
  1052. return SEN_RETRY;
  1053. else
  1054. return SEN_NOT_AVAIL;
  1055. }
  1056. we_p->devindex = dev_nr;
  1057. device_p = z90crypt.device_p[dev_nr];
  1058. if (!device_p)
  1059. return SEN_NOT_AVAIL;
  1060. if (device_p->dev_type != we_p->devtype)
  1061. return SEN_RETRY;
  1062. if (device_p->dev_caller_count >= device_p->dev_q_depth)
  1063. return SEN_QUEUE_FULL;
  1064. PDEBUG("device number prior to send: %d\n", dev_nr);
  1065. switch (send_to_AP(dev_nr, z90crypt.cdx,
  1066. caller_p->caller_dev_dep_req_l,
  1067. caller_p->caller_dev_dep_req_p)) {
  1068. case DEV_SEN_EXCEPTION:
  1069. PRINTKC("Exception during send to device %d\n", dev_nr);
  1070. z90crypt.terminating = 1;
  1071. return SEN_FATAL_ERROR;
  1072. case DEV_GONE:
  1073. PRINTK("Device %d not available\n", dev_nr);
  1074. remove_device(device_p);
  1075. return SEN_NOT_AVAIL;
  1076. case DEV_EMPTY:
  1077. return SEN_NOT_AVAIL;
  1078. case DEV_NO_WORK:
  1079. return SEN_FATAL_ERROR;
  1080. case DEV_BAD_MESSAGE:
  1081. return SEN_USER_ERROR;
  1082. case DEV_QUEUE_FULL:
  1083. return SEN_QUEUE_FULL;
  1084. default:
  1085. case DEV_ONLINE:
  1086. break;
  1087. }
  1088. list_add_tail(&(caller_p->caller_liste), &(device_p->dev_caller_list));
  1089. device_p->dev_caller_count++;
  1090. return 0;
  1091. }
  1092. /**
  1093. * Send puts the user's work on one of two queues:
  1094. * the pending queue if the send was successful
  1095. * the request queue if the send failed because device full or busy
  1096. */
  1097. static inline int
  1098. z90crypt_send(struct work_element *we_p, const char *buf)
  1099. {
  1100. int rv;
  1101. PDEBUG("PID %d\n", PID());
  1102. if (CHK_RDWRMASK(we_p->status[0]) != STAT_NOWORK) {
  1103. PDEBUG("PID %d tried to send more work but has outstanding "
  1104. "work.\n", PID());
  1105. return -EWORKPEND;
  1106. }
  1107. we_p->devindex = -1; // Reset device number
  1108. spin_lock_irq(&queuespinlock);
  1109. rv = send_to_crypto_device(we_p);
  1110. switch (rv) {
  1111. case 0:
  1112. we_p->requestsent = jiffies;
  1113. we_p->audit[0] |= FP_SENT;
  1114. list_add_tail(&we_p->liste, &pending_list);
  1115. ++pendingq_count;
  1116. we_p->audit[0] |= FP_PENDING;
  1117. break;
  1118. case SEN_BUSY:
  1119. case SEN_QUEUE_FULL:
  1120. rv = 0;
  1121. we_p->devindex = -1; // any device will do
  1122. we_p->requestsent = jiffies;
  1123. list_add_tail(&we_p->liste, &request_list);
  1124. ++requestq_count;
  1125. we_p->audit[0] |= FP_REQUEST;
  1126. break;
  1127. case SEN_RETRY:
  1128. rv = -ERESTARTSYS;
  1129. break;
  1130. case SEN_NOT_AVAIL:
  1131. PRINTK("*** No devices available.\n");
  1132. rv = we_p->retcode = -ENODEV;
  1133. we_p->status[0] |= STAT_FAILED;
  1134. break;
  1135. case REC_OPERAND_INV:
  1136. case REC_OPERAND_SIZE:
  1137. case REC_EVEN_MOD:
  1138. case REC_INVALID_PAD:
  1139. rv = we_p->retcode = -EINVAL;
  1140. we_p->status[0] |= STAT_FAILED;
  1141. break;
  1142. default:
  1143. we_p->retcode = rv;
  1144. we_p->status[0] |= STAT_FAILED;
  1145. break;
  1146. }
  1147. if (rv != -ERESTARTSYS)
  1148. SET_RDWRMASK(we_p->status[0], STAT_WRITTEN);
  1149. spin_unlock_irq(&queuespinlock);
  1150. if (rv == 0)
  1151. tasklet_schedule(&reader_tasklet);
  1152. return rv;
  1153. }
  1154. /**
  1155. * process_results copies the user's work from kernel space.
  1156. */
  1157. static inline int
  1158. z90crypt_process_results(struct work_element *we_p, char __user *buf)
  1159. {
  1160. int rv;
  1161. PDEBUG("we_p %p (PID %d)\n", we_p, PID());
  1162. LONG2DEVPTR(we_p->devindex)->dev_total_req_cnt++;
  1163. SET_RDWRMASK(we_p->status[0], STAT_READPEND);
  1164. rv = 0;
  1165. if (!we_p->buffer) {
  1166. PRINTK("we_p %p PID %d in STAT_READPEND: buffer NULL.\n",
  1167. we_p, PID());
  1168. rv = -ENOBUFF;
  1169. }
  1170. if (!rv)
  1171. if ((rv = copy_to_user(buf, we_p->buffer, we_p->buff_size))) {
  1172. PDEBUG("copy_to_user failed: rv = %d\n", rv);
  1173. rv = -EFAULT;
  1174. }
  1175. if (!rv)
  1176. rv = we_p->retcode;
  1177. if (!rv)
  1178. if (we_p->resp_buff_size
  1179. && copy_to_user(we_p->resp_addr, we_p->resp_buff,
  1180. we_p->resp_buff_size))
  1181. rv = -EFAULT;
  1182. SET_RDWRMASK(we_p->status[0], STAT_NOWORK);
  1183. return rv;
  1184. }
  1185. static unsigned char NULL_psmid[8] =
  1186. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  1187. /**
  1188. * Used in device configuration functions
  1189. */
  1190. #define MAX_RESET 90
  1191. /**
  1192. * This is used only for PCICC support
  1193. */
  1194. static inline int
  1195. is_PKCS11_padded(unsigned char *buffer, int length)
  1196. {
  1197. int i;
  1198. if ((buffer[0] != 0x00) || (buffer[1] != 0x01))
  1199. return 0;
  1200. for (i = 2; i < length; i++)
  1201. if (buffer[i] != 0xFF)
  1202. break;
  1203. if ((i < 10) || (i == length))
  1204. return 0;
  1205. if (buffer[i] != 0x00)
  1206. return 0;
  1207. return 1;
  1208. }
  1209. /**
  1210. * This is used only for PCICC support
  1211. */
  1212. static inline int
  1213. is_PKCS12_padded(unsigned char *buffer, int length)
  1214. {
  1215. int i;
  1216. if ((buffer[0] != 0x00) || (buffer[1] != 0x02))
  1217. return 0;
  1218. for (i = 2; i < length; i++)
  1219. if (buffer[i] == 0x00)
  1220. break;
  1221. if ((i < 10) || (i == length))
  1222. return 0;
  1223. if (buffer[i] != 0x00)
  1224. return 0;
  1225. return 1;
  1226. }
  1227. /**
  1228. * builds struct caller and converts message from generic format to
  1229. * device-dependent format
  1230. * func is ICARSAMODEXPO or ICARSACRT
  1231. * function is PCI_FUNC_KEY_ENCRYPT or PCI_FUNC_KEY_DECRYPT
  1232. */
  1233. static inline int
  1234. build_caller(struct work_element *we_p, short function)
  1235. {
  1236. int rv;
  1237. struct caller *caller_p = (struct caller *)we_p->requestptr;
  1238. if ((we_p->devtype != PCICC) && (we_p->devtype != PCICA) &&
  1239. (we_p->devtype != PCIXCC_MCL2) && (we_p->devtype != PCIXCC_MCL3) &&
  1240. (we_p->devtype != CEX2C) && (we_p->devtype != CEX2A))
  1241. return SEN_NOT_AVAIL;
  1242. memcpy(caller_p->caller_id, we_p->caller_id,
  1243. sizeof(caller_p->caller_id));
  1244. caller_p->caller_dev_dep_req_p = caller_p->caller_dev_dep_req;
  1245. caller_p->caller_dev_dep_req_l = MAX_RESPONSE_SIZE;
  1246. caller_p->caller_buf_p = we_p->buffer;
  1247. INIT_LIST_HEAD(&(caller_p->caller_liste));
  1248. rv = convert_request(we_p->buffer, we_p->funccode, function,
  1249. z90crypt.cdx, we_p->devtype,
  1250. &caller_p->caller_dev_dep_req_l,
  1251. caller_p->caller_dev_dep_req_p);
  1252. if (rv) {
  1253. if (rv == SEN_NOT_AVAIL)
  1254. PDEBUG("request can't be processed on hdwr avail\n");
  1255. else
  1256. PRINTK("Error from convert_request: %d\n", rv);
  1257. }
  1258. else
  1259. memcpy(&(caller_p->caller_dev_dep_req_p[4]), we_p->caller_id,8);
  1260. return rv;
  1261. }
  1262. static inline void
  1263. unbuild_caller(struct device *device_p, struct caller *caller_p)
  1264. {
  1265. if (!caller_p)
  1266. return;
  1267. if (caller_p->caller_liste.next && caller_p->caller_liste.prev)
  1268. if (!list_empty(&caller_p->caller_liste)) {
  1269. list_del_init(&caller_p->caller_liste);
  1270. device_p->dev_caller_count--;
  1271. }
  1272. memset(caller_p->caller_id, 0, sizeof(caller_p->caller_id));
  1273. }
  1274. static inline int
  1275. get_crypto_request_buffer(struct work_element *we_p)
  1276. {
  1277. struct ica_rsa_modexpo *mex_p;
  1278. struct ica_rsa_modexpo_crt *crt_p;
  1279. unsigned char *temp_buffer;
  1280. short function;
  1281. int rv;
  1282. mex_p = (struct ica_rsa_modexpo *) we_p->buffer;
  1283. crt_p = (struct ica_rsa_modexpo_crt *) we_p->buffer;
  1284. PDEBUG("device type input = %d\n", we_p->devtype);
  1285. if (z90crypt.terminating)
  1286. return REC_NO_RESPONSE;
  1287. if (memcmp(we_p->caller_id, NULL_psmid, 8) == 0) {
  1288. PRINTK("psmid zeroes\n");
  1289. return SEN_FATAL_ERROR;
  1290. }
  1291. if (!we_p->buffer) {
  1292. PRINTK("buffer pointer NULL\n");
  1293. return SEN_USER_ERROR;
  1294. }
  1295. if (!we_p->requestptr) {
  1296. PRINTK("caller pointer NULL\n");
  1297. return SEN_USER_ERROR;
  1298. }
  1299. if ((we_p->devtype != PCICA) && (we_p->devtype != PCICC) &&
  1300. (we_p->devtype != PCIXCC_MCL2) && (we_p->devtype != PCIXCC_MCL3) &&
  1301. (we_p->devtype != CEX2C) && (we_p->devtype != CEX2A) &&
  1302. (we_p->devtype != ANYDEV)) {
  1303. PRINTK("invalid device type\n");
  1304. return SEN_USER_ERROR;
  1305. }
  1306. if ((mex_p->inputdatalength < 1) ||
  1307. (mex_p->inputdatalength > MAX_MOD_SIZE)) {
  1308. PRINTK("inputdatalength[%d] is not valid\n",
  1309. mex_p->inputdatalength);
  1310. return SEN_USER_ERROR;
  1311. }
  1312. if (mex_p->outputdatalength < mex_p->inputdatalength) {
  1313. PRINTK("outputdatalength[%d] < inputdatalength[%d]\n",
  1314. mex_p->outputdatalength, mex_p->inputdatalength);
  1315. return SEN_USER_ERROR;
  1316. }
  1317. if (!mex_p->inputdata || !mex_p->outputdata) {
  1318. PRINTK("inputdata[%p] or outputdata[%p] is NULL\n",
  1319. mex_p->outputdata, mex_p->inputdata);
  1320. return SEN_USER_ERROR;
  1321. }
  1322. /**
  1323. * As long as outputdatalength is big enough, we can set the
  1324. * outputdatalength equal to the inputdatalength, since that is the
  1325. * number of bytes we will copy in any case
  1326. */
  1327. mex_p->outputdatalength = mex_p->inputdatalength;
  1328. rv = 0;
  1329. switch (we_p->funccode) {
  1330. case ICARSAMODEXPO:
  1331. if (!mex_p->b_key || !mex_p->n_modulus)
  1332. rv = SEN_USER_ERROR;
  1333. break;
  1334. case ICARSACRT:
  1335. if (!IS_EVEN(crt_p->inputdatalength)) {
  1336. PRINTK("inputdatalength[%d] is odd, CRT form\n",
  1337. crt_p->inputdatalength);
  1338. rv = SEN_USER_ERROR;
  1339. break;
  1340. }
  1341. if (!crt_p->bp_key ||
  1342. !crt_p->bq_key ||
  1343. !crt_p->np_prime ||
  1344. !crt_p->nq_prime ||
  1345. !crt_p->u_mult_inv) {
  1346. PRINTK("CRT form, bad data: %p/%p/%p/%p/%p\n",
  1347. crt_p->bp_key, crt_p->bq_key,
  1348. crt_p->np_prime, crt_p->nq_prime,
  1349. crt_p->u_mult_inv);
  1350. rv = SEN_USER_ERROR;
  1351. }
  1352. break;
  1353. default:
  1354. PRINTK("bad func = %d\n", we_p->funccode);
  1355. rv = SEN_USER_ERROR;
  1356. break;
  1357. }
  1358. if (rv != 0)
  1359. return rv;
  1360. if (select_device_type(&we_p->devtype, mex_p->inputdatalength) < 0)
  1361. return SEN_NOT_AVAIL;
  1362. temp_buffer = (unsigned char *)we_p + sizeof(struct work_element) +
  1363. sizeof(struct caller);
  1364. if (copy_from_user(temp_buffer, mex_p->inputdata,
  1365. mex_p->inputdatalength) != 0)
  1366. return SEN_RELEASED;
  1367. function = PCI_FUNC_KEY_ENCRYPT;
  1368. switch (we_p->devtype) {
  1369. /* PCICA and CEX2A do everything with a simple RSA mod-expo operation */
  1370. case PCICA:
  1371. case CEX2A:
  1372. function = PCI_FUNC_KEY_ENCRYPT;
  1373. break;
  1374. /**
  1375. * PCIXCC_MCL2 does all Mod-Expo form with a simple RSA mod-expo
  1376. * operation, and all CRT forms with a PKCS-1.2 format decrypt.
  1377. * PCIXCC_MCL3 and CEX2C do all Mod-Expo and CRT forms with a simple RSA
  1378. * mod-expo operation
  1379. */
  1380. case PCIXCC_MCL2:
  1381. if (we_p->funccode == ICARSAMODEXPO)
  1382. function = PCI_FUNC_KEY_ENCRYPT;
  1383. else
  1384. function = PCI_FUNC_KEY_DECRYPT;
  1385. break;
  1386. case PCIXCC_MCL3:
  1387. case CEX2C:
  1388. if (we_p->funccode == ICARSAMODEXPO)
  1389. function = PCI_FUNC_KEY_ENCRYPT;
  1390. else
  1391. function = PCI_FUNC_KEY_DECRYPT;
  1392. break;
  1393. /**
  1394. * PCICC does everything as a PKCS-1.2 format request
  1395. */
  1396. case PCICC:
  1397. /* PCICC cannot handle input that is is PKCS#1.1 padded */
  1398. if (is_PKCS11_padded(temp_buffer, mex_p->inputdatalength)) {
  1399. return SEN_NOT_AVAIL;
  1400. }
  1401. if (we_p->funccode == ICARSAMODEXPO) {
  1402. if (is_PKCS12_padded(temp_buffer,
  1403. mex_p->inputdatalength))
  1404. function = PCI_FUNC_KEY_ENCRYPT;
  1405. else
  1406. function = PCI_FUNC_KEY_DECRYPT;
  1407. } else
  1408. /* all CRT forms are decrypts */
  1409. function = PCI_FUNC_KEY_DECRYPT;
  1410. break;
  1411. }
  1412. PDEBUG("function: %04x\n", function);
  1413. rv = build_caller(we_p, function);
  1414. PDEBUG("rv from build_caller = %d\n", rv);
  1415. return rv;
  1416. }
  1417. static inline int
  1418. z90crypt_prepare(struct work_element *we_p, unsigned int funccode,
  1419. const char __user *buffer)
  1420. {
  1421. int rv;
  1422. we_p->devindex = -1;
  1423. if (funccode == ICARSAMODEXPO)
  1424. we_p->buff_size = sizeof(struct ica_rsa_modexpo);
  1425. else
  1426. we_p->buff_size = sizeof(struct ica_rsa_modexpo_crt);
  1427. if (copy_from_user(we_p->buffer, buffer, we_p->buff_size))
  1428. return -EFAULT;
  1429. we_p->audit[0] |= FP_COPYFROM;
  1430. SET_RDWRMASK(we_p->status[0], STAT_WRITTEN);
  1431. we_p->funccode = funccode;
  1432. we_p->devtype = -1;
  1433. we_p->audit[0] |= FP_BUFFREQ;
  1434. rv = get_crypto_request_buffer(we_p);
  1435. switch (rv) {
  1436. case 0:
  1437. we_p->audit[0] |= FP_BUFFGOT;
  1438. break;
  1439. case SEN_USER_ERROR:
  1440. rv = -EINVAL;
  1441. break;
  1442. case SEN_QUEUE_FULL:
  1443. rv = 0;
  1444. break;
  1445. case SEN_RELEASED:
  1446. rv = -EFAULT;
  1447. break;
  1448. case REC_NO_RESPONSE:
  1449. rv = -ENODEV;
  1450. break;
  1451. case SEN_NOT_AVAIL:
  1452. case EGETBUFF:
  1453. rv = -EGETBUFF;
  1454. break;
  1455. default:
  1456. PRINTK("rv = %d\n", rv);
  1457. rv = -EGETBUFF;
  1458. break;
  1459. }
  1460. if (CHK_RDWRMASK(we_p->status[0]) == STAT_WRITTEN)
  1461. SET_RDWRMASK(we_p->status[0], STAT_DEFAULT);
  1462. return rv;
  1463. }
  1464. static inline void
  1465. purge_work_element(struct work_element *we_p)
  1466. {
  1467. struct list_head *lptr;
  1468. spin_lock_irq(&queuespinlock);
  1469. list_for_each(lptr, &request_list) {
  1470. if (lptr == &we_p->liste) {
  1471. list_del_init(lptr);
  1472. requestq_count--;
  1473. break;
  1474. }
  1475. }
  1476. list_for_each(lptr, &pending_list) {
  1477. if (lptr == &we_p->liste) {
  1478. list_del_init(lptr);
  1479. pendingq_count--;
  1480. break;
  1481. }
  1482. }
  1483. spin_unlock_irq(&queuespinlock);
  1484. }
  1485. /**
  1486. * Build the request and send it.
  1487. */
  1488. static inline int
  1489. z90crypt_rsa(struct priv_data *private_data_p, pid_t pid,
  1490. unsigned int cmd, unsigned long arg)
  1491. {
  1492. struct work_element *we_p;
  1493. int rv;
  1494. if ((rv = allocate_work_element(&we_p, private_data_p, pid))) {
  1495. PDEBUG("PID %d: allocate_work_element returned ENOMEM\n", pid);
  1496. return rv;
  1497. }
  1498. if ((rv = z90crypt_prepare(we_p, cmd, (const char __user *)arg)))
  1499. PDEBUG("PID %d: rv = %d from z90crypt_prepare\n", pid, rv);
  1500. if (!rv)
  1501. if ((rv = z90crypt_send(we_p, (const char *)arg)))
  1502. PDEBUG("PID %d: rv %d from z90crypt_send.\n", pid, rv);
  1503. if (!rv) {
  1504. we_p->audit[0] |= FP_ASLEEP;
  1505. wait_event(we_p->waitq, atomic_read(&we_p->alarmrung));
  1506. we_p->audit[0] |= FP_AWAKE;
  1507. rv = we_p->retcode;
  1508. }
  1509. if (!rv)
  1510. rv = z90crypt_process_results(we_p, (char __user *)arg);
  1511. if ((we_p->status[0] & STAT_FAILED)) {
  1512. switch (rv) {
  1513. /**
  1514. * EINVAL *after* receive is almost always a padding error or
  1515. * length error issued by a coprocessor (not an accelerator).
  1516. * We convert this return value to -EGETBUFF which should
  1517. * trigger a fallback to software.
  1518. */
  1519. case -EINVAL:
  1520. if ((we_p->devtype != PCICA) &&
  1521. (we_p->devtype != CEX2A))
  1522. rv = -EGETBUFF;
  1523. break;
  1524. case -ETIMEOUT:
  1525. if (z90crypt.mask.st_count > 0)
  1526. rv = -ERESTARTSYS; // retry with another
  1527. else
  1528. rv = -ENODEV; // no cards left
  1529. /* fall through to clean up request queue */
  1530. case -ERESTARTSYS:
  1531. case -ERELEASED:
  1532. switch (CHK_RDWRMASK(we_p->status[0])) {
  1533. case STAT_WRITTEN:
  1534. purge_work_element(we_p);
  1535. break;
  1536. case STAT_READPEND:
  1537. case STAT_NOWORK:
  1538. default:
  1539. break;
  1540. }
  1541. break;
  1542. default:
  1543. we_p->status[0] ^= STAT_FAILED;
  1544. break;
  1545. }
  1546. }
  1547. free_page((long)we_p);
  1548. return rv;
  1549. }
  1550. /**
  1551. * This function is a little long, but it's really just one large switch
  1552. * statement.
  1553. */
  1554. static long
  1555. z90crypt_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1556. {
  1557. struct priv_data *private_data_p = filp->private_data;
  1558. unsigned char *status;
  1559. unsigned char *qdepth;
  1560. unsigned int *reqcnt;
  1561. struct ica_z90_status *pstat;
  1562. int ret, i, loopLim, tempstat;
  1563. static int deprecated_msg_count1 = 0;
  1564. static int deprecated_msg_count2 = 0;
  1565. PDEBUG("filp %p (PID %d), cmd 0x%08X\n", filp, PID(), cmd);
  1566. PDEBUG("cmd 0x%08X: dir %s, size 0x%04X, type 0x%02X, nr 0x%02X\n",
  1567. cmd,
  1568. !_IOC_DIR(cmd) ? "NO"
  1569. : ((_IOC_DIR(cmd) == (_IOC_READ|_IOC_WRITE)) ? "RW"
  1570. : ((_IOC_DIR(cmd) == _IOC_READ) ? "RD"
  1571. : "WR")),
  1572. _IOC_SIZE(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd));
  1573. if (_IOC_TYPE(cmd) != Z90_IOCTL_MAGIC) {
  1574. PRINTK("cmd 0x%08X contains bad magic\n", cmd);
  1575. return -ENOTTY;
  1576. }
  1577. ret = 0;
  1578. switch (cmd) {
  1579. case ICARSAMODEXPO:
  1580. case ICARSACRT:
  1581. if (quiesce_z90crypt) {
  1582. ret = -EQUIESCE;
  1583. break;
  1584. }
  1585. ret = -ENODEV; // Default if no devices
  1586. loopLim = z90crypt.hdware_info->hdware_mask.st_count -
  1587. (z90crypt.hdware_info->hdware_mask.disabled_count +
  1588. z90crypt.hdware_info->hdware_mask.user_disabled_count);
  1589. for (i = 0; i < loopLim; i++) {
  1590. ret = z90crypt_rsa(private_data_p, PID(), cmd, arg);
  1591. if (ret != -ERESTARTSYS)
  1592. break;
  1593. }
  1594. if (ret == -ERESTARTSYS)
  1595. ret = -ENODEV;
  1596. break;
  1597. case Z90STAT_TOTALCOUNT:
  1598. tempstat = get_status_totalcount();
  1599. if (copy_to_user((int __user *)arg, &tempstat,sizeof(int)) != 0)
  1600. ret = -EFAULT;
  1601. break;
  1602. case Z90STAT_PCICACOUNT:
  1603. tempstat = get_status_PCICAcount();
  1604. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1605. ret = -EFAULT;
  1606. break;
  1607. case Z90STAT_PCICCCOUNT:
  1608. tempstat = get_status_PCICCcount();
  1609. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1610. ret = -EFAULT;
  1611. break;
  1612. case Z90STAT_PCIXCCMCL2COUNT:
  1613. tempstat = get_status_PCIXCCMCL2count();
  1614. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1615. ret = -EFAULT;
  1616. break;
  1617. case Z90STAT_PCIXCCMCL3COUNT:
  1618. tempstat = get_status_PCIXCCMCL3count();
  1619. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1620. ret = -EFAULT;
  1621. break;
  1622. case Z90STAT_CEX2CCOUNT:
  1623. tempstat = get_status_CEX2Ccount();
  1624. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1625. ret = -EFAULT;
  1626. break;
  1627. case Z90STAT_CEX2ACOUNT:
  1628. tempstat = get_status_CEX2Acount();
  1629. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1630. ret = -EFAULT;
  1631. break;
  1632. case Z90STAT_REQUESTQ_COUNT:
  1633. tempstat = get_status_requestq_count();
  1634. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1635. ret = -EFAULT;
  1636. break;
  1637. case Z90STAT_PENDINGQ_COUNT:
  1638. tempstat = get_status_pendingq_count();
  1639. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1640. ret = -EFAULT;
  1641. break;
  1642. case Z90STAT_TOTALOPEN_COUNT:
  1643. tempstat = get_status_totalopen_count();
  1644. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1645. ret = -EFAULT;
  1646. break;
  1647. case Z90STAT_DOMAIN_INDEX:
  1648. tempstat = get_status_domain_index();
  1649. if (copy_to_user((int __user *)arg, &tempstat, sizeof(int)) != 0)
  1650. ret = -EFAULT;
  1651. break;
  1652. case Z90STAT_STATUS_MASK:
  1653. status = kmalloc(Z90CRYPT_NUM_APS, GFP_KERNEL);
  1654. if (!status) {
  1655. PRINTK("kmalloc for status failed!\n");
  1656. ret = -ENOMEM;
  1657. break;
  1658. }
  1659. get_status_status_mask(status);
  1660. if (copy_to_user((char __user *) arg, status, Z90CRYPT_NUM_APS)
  1661. != 0)
  1662. ret = -EFAULT;
  1663. kfree(status);
  1664. break;
  1665. case Z90STAT_QDEPTH_MASK:
  1666. qdepth = kmalloc(Z90CRYPT_NUM_APS, GFP_KERNEL);
  1667. if (!qdepth) {
  1668. PRINTK("kmalloc for qdepth failed!\n");
  1669. ret = -ENOMEM;
  1670. break;
  1671. }
  1672. get_status_qdepth_mask(qdepth);
  1673. if (copy_to_user((char __user *) arg, qdepth, Z90CRYPT_NUM_APS) != 0)
  1674. ret = -EFAULT;
  1675. kfree(qdepth);
  1676. break;
  1677. case Z90STAT_PERDEV_REQCNT:
  1678. reqcnt = kmalloc(sizeof(int) * Z90CRYPT_NUM_APS, GFP_KERNEL);
  1679. if (!reqcnt) {
  1680. PRINTK("kmalloc for reqcnt failed!\n");
  1681. ret = -ENOMEM;
  1682. break;
  1683. }
  1684. get_status_perdevice_reqcnt(reqcnt);
  1685. if (copy_to_user((char __user *) arg, reqcnt,
  1686. Z90CRYPT_NUM_APS * sizeof(int)) != 0)
  1687. ret = -EFAULT;
  1688. kfree(reqcnt);
  1689. break;
  1690. /* THIS IS DEPRECATED. USE THE NEW STATUS CALLS */
  1691. case ICAZ90STATUS:
  1692. if (deprecated_msg_count1 < 20) {
  1693. PRINTK("deprecated call to ioctl (ICAZ90STATUS)!\n");
  1694. deprecated_msg_count1++;
  1695. if (deprecated_msg_count1 == 20)
  1696. PRINTK("No longer issuing messages related to "
  1697. "deprecated call to ICAZ90STATUS.\n");
  1698. }
  1699. pstat = kmalloc(sizeof(struct ica_z90_status), GFP_KERNEL);
  1700. if (!pstat) {
  1701. PRINTK("kmalloc for pstat failed!\n");
  1702. ret = -ENOMEM;
  1703. break;
  1704. }
  1705. pstat->totalcount = get_status_totalcount();
  1706. pstat->leedslitecount = get_status_PCICAcount();
  1707. pstat->leeds2count = get_status_PCICCcount();
  1708. pstat->requestqWaitCount = get_status_requestq_count();
  1709. pstat->pendingqWaitCount = get_status_pendingq_count();
  1710. pstat->totalOpenCount = get_status_totalopen_count();
  1711. pstat->cryptoDomain = get_status_domain_index();
  1712. get_status_status_mask(pstat->status);
  1713. get_status_qdepth_mask(pstat->qdepth);
  1714. if (copy_to_user((struct ica_z90_status __user *) arg, pstat,
  1715. sizeof(struct ica_z90_status)) != 0)
  1716. ret = -EFAULT;
  1717. kfree(pstat);
  1718. break;
  1719. /* THIS IS DEPRECATED. USE THE NEW STATUS CALLS */
  1720. case Z90STAT_PCIXCCCOUNT:
  1721. if (deprecated_msg_count2 < 20) {
  1722. PRINTK("deprecated ioctl (Z90STAT_PCIXCCCOUNT)!\n");
  1723. deprecated_msg_count2++;
  1724. if (deprecated_msg_count2 == 20)
  1725. PRINTK("No longer issuing messages about depre"
  1726. "cated ioctl Z90STAT_PCIXCCCOUNT.\n");
  1727. }
  1728. tempstat = get_status_PCIXCCcount();
  1729. if (copy_to_user((int *)arg, &tempstat, sizeof(int)) != 0)
  1730. ret = -EFAULT;
  1731. break;
  1732. case Z90QUIESCE:
  1733. if (current->euid != 0) {
  1734. PRINTK("QUIESCE fails: euid %d\n",
  1735. current->euid);
  1736. ret = -EACCES;
  1737. } else {
  1738. PRINTK("QUIESCE device from PID %d\n", PID());
  1739. quiesce_z90crypt = 1;
  1740. }
  1741. break;
  1742. default:
  1743. /* user passed an invalid IOCTL number */
  1744. PDEBUG("cmd 0x%08X contains invalid ioctl code\n", cmd);
  1745. ret = -ENOTTY;
  1746. break;
  1747. }
  1748. return ret;
  1749. }
  1750. static inline int
  1751. sprintcl(unsigned char *outaddr, unsigned char *addr, unsigned int len)
  1752. {
  1753. int hl, i;
  1754. hl = 0;
  1755. for (i = 0; i < len; i++)
  1756. hl += sprintf(outaddr+hl, "%01x", (unsigned int) addr[i]);
  1757. hl += sprintf(outaddr+hl, " ");
  1758. return hl;
  1759. }
  1760. static inline int
  1761. sprintrw(unsigned char *outaddr, unsigned char *addr, unsigned int len)
  1762. {
  1763. int hl, inl, c, cx;
  1764. hl = sprintf(outaddr, " ");
  1765. inl = 0;
  1766. for (c = 0; c < (len / 16); c++) {
  1767. hl += sprintcl(outaddr+hl, addr+inl, 16);
  1768. inl += 16;
  1769. }
  1770. cx = len%16;
  1771. if (cx) {
  1772. hl += sprintcl(outaddr+hl, addr+inl, cx);
  1773. inl += cx;
  1774. }
  1775. hl += sprintf(outaddr+hl, "\n");
  1776. return hl;
  1777. }
  1778. static inline int
  1779. sprinthx(unsigned char *title, unsigned char *outaddr,
  1780. unsigned char *addr, unsigned int len)
  1781. {
  1782. int hl, inl, r, rx;
  1783. hl = sprintf(outaddr, "\n%s\n", title);
  1784. inl = 0;
  1785. for (r = 0; r < (len / 64); r++) {
  1786. hl += sprintrw(outaddr+hl, addr+inl, 64);
  1787. inl += 64;
  1788. }
  1789. rx = len % 64;
  1790. if (rx) {
  1791. hl += sprintrw(outaddr+hl, addr+inl, rx);
  1792. inl += rx;
  1793. }
  1794. hl += sprintf(outaddr+hl, "\n");
  1795. return hl;
  1796. }
  1797. static inline int
  1798. sprinthx4(unsigned char *title, unsigned char *outaddr,
  1799. unsigned int *array, unsigned int len)
  1800. {
  1801. int hl, r;
  1802. hl = sprintf(outaddr, "\n%s\n", title);
  1803. for (r = 0; r < len; r++) {
  1804. if ((r % 8) == 0)
  1805. hl += sprintf(outaddr+hl, " ");
  1806. hl += sprintf(outaddr+hl, "%08X ", array[r]);
  1807. if ((r % 8) == 7)
  1808. hl += sprintf(outaddr+hl, "\n");
  1809. }
  1810. hl += sprintf(outaddr+hl, "\n");
  1811. return hl;
  1812. }
  1813. static int
  1814. z90crypt_status(char *resp_buff, char **start, off_t offset,
  1815. int count, int *eof, void *data)
  1816. {
  1817. unsigned char *workarea;
  1818. int len;
  1819. /* resp_buff is a page. Use the right half for a work area */
  1820. workarea = resp_buff+2000;
  1821. len = 0;
  1822. len += sprintf(resp_buff+len, "\nz90crypt version: %d.%d.%d\n",
  1823. z90crypt_VERSION, z90crypt_RELEASE, z90crypt_VARIANT);
  1824. len += sprintf(resp_buff+len, "Cryptographic domain: %d\n",
  1825. get_status_domain_index());
  1826. len += sprintf(resp_buff+len, "Total device count: %d\n",
  1827. get_status_totalcount());
  1828. len += sprintf(resp_buff+len, "PCICA count: %d\n",
  1829. get_status_PCICAcount());
  1830. len += sprintf(resp_buff+len, "PCICC count: %d\n",
  1831. get_status_PCICCcount());
  1832. len += sprintf(resp_buff+len, "PCIXCC MCL2 count: %d\n",
  1833. get_status_PCIXCCMCL2count());
  1834. len += sprintf(resp_buff+len, "PCIXCC MCL3 count: %d\n",
  1835. get_status_PCIXCCMCL3count());
  1836. len += sprintf(resp_buff+len, "CEX2C count: %d\n",
  1837. get_status_CEX2Ccount());
  1838. len += sprintf(resp_buff+len, "CEX2A count: %d\n",
  1839. get_status_CEX2Acount());
  1840. len += sprintf(resp_buff+len, "requestq count: %d\n",
  1841. get_status_requestq_count());
  1842. len += sprintf(resp_buff+len, "pendingq count: %d\n",
  1843. get_status_pendingq_count());
  1844. len += sprintf(resp_buff+len, "Total open handles: %d\n\n",
  1845. get_status_totalopen_count());
  1846. len += sprinthx(
  1847. "Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  1848. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A",
  1849. resp_buff+len,
  1850. get_status_status_mask(workarea),
  1851. Z90CRYPT_NUM_APS);
  1852. len += sprinthx("Waiting work element counts",
  1853. resp_buff+len,
  1854. get_status_qdepth_mask(workarea),
  1855. Z90CRYPT_NUM_APS);
  1856. len += sprinthx4(
  1857. "Per-device successfully completed request counts",
  1858. resp_buff+len,
  1859. get_status_perdevice_reqcnt((unsigned int *)workarea),
  1860. Z90CRYPT_NUM_APS);
  1861. *eof = 1;
  1862. memset(workarea, 0, Z90CRYPT_NUM_APS * sizeof(unsigned int));
  1863. return len;
  1864. }
  1865. static inline void
  1866. disable_card(int card_index)
  1867. {
  1868. struct device *devp;
  1869. devp = LONG2DEVPTR(card_index);
  1870. if (!devp || devp->user_disabled)
  1871. return;
  1872. devp->user_disabled = 1;
  1873. z90crypt.hdware_info->hdware_mask.user_disabled_count++;
  1874. if (devp->dev_type == -1)
  1875. return;
  1876. z90crypt.hdware_info->type_mask[devp->dev_type].user_disabled_count++;
  1877. }
  1878. static inline void
  1879. enable_card(int card_index)
  1880. {
  1881. struct device *devp;
  1882. devp = LONG2DEVPTR(card_index);
  1883. if (!devp || !devp->user_disabled)
  1884. return;
  1885. devp->user_disabled = 0;
  1886. z90crypt.hdware_info->hdware_mask.user_disabled_count--;
  1887. if (devp->dev_type == -1)
  1888. return;
  1889. z90crypt.hdware_info->type_mask[devp->dev_type].user_disabled_count--;
  1890. }
  1891. static int
  1892. z90crypt_status_write(struct file *file, const char __user *buffer,
  1893. unsigned long count, void *data)
  1894. {
  1895. int j, eol;
  1896. unsigned char *lbuf, *ptr;
  1897. unsigned int local_count;
  1898. #define LBUFSIZE 1200
  1899. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1900. if (!lbuf) {
  1901. PRINTK("kmalloc failed!\n");
  1902. return 0;
  1903. }
  1904. if (count <= 0)
  1905. return 0;
  1906. local_count = UMIN((unsigned int)count, LBUFSIZE-1);
  1907. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1908. kfree(lbuf);
  1909. return -EFAULT;
  1910. }
  1911. lbuf[local_count] = '\0';
  1912. ptr = strstr(lbuf, "Online devices");
  1913. if (ptr == 0) {
  1914. PRINTK("Unable to parse data (missing \"Online devices\")\n");
  1915. kfree(lbuf);
  1916. return count;
  1917. }
  1918. ptr = strstr(ptr, "\n");
  1919. if (ptr == 0) {
  1920. PRINTK("Unable to parse data (missing newline after \"Online devices\")\n");
  1921. kfree(lbuf);
  1922. return count;
  1923. }
  1924. ptr++;
  1925. if (strstr(ptr, "Waiting work element counts") == NULL) {
  1926. PRINTK("Unable to parse data (missing \"Waiting work element counts\")\n");
  1927. kfree(lbuf);
  1928. return count;
  1929. }
  1930. j = 0;
  1931. eol = 0;
  1932. while ((j < 64) && (*ptr != '\0')) {
  1933. switch (*ptr) {
  1934. case '\t':
  1935. case ' ':
  1936. break;
  1937. case '\n':
  1938. default:
  1939. eol = 1;
  1940. break;
  1941. case '0': // no device
  1942. case '1': // PCICA
  1943. case '2': // PCICC
  1944. case '3': // PCIXCC_MCL2
  1945. case '4': // PCIXCC_MCL3
  1946. case '5': // CEX2C
  1947. case '6': // CEX2A
  1948. j++;
  1949. break;
  1950. case 'd':
  1951. case 'D':
  1952. disable_card(j);
  1953. j++;
  1954. break;
  1955. case 'e':
  1956. case 'E':
  1957. enable_card(j);
  1958. j++;
  1959. break;
  1960. }
  1961. if (eol)
  1962. break;
  1963. ptr++;
  1964. }
  1965. kfree(lbuf);
  1966. return count;
  1967. }
  1968. /**
  1969. * Functions that run under a timer, with no process id
  1970. *
  1971. * The task functions:
  1972. * z90crypt_reader_task
  1973. * helper_send_work
  1974. * helper_handle_work_element
  1975. * helper_receive_rc
  1976. * z90crypt_config_task
  1977. * z90crypt_cleanup_task
  1978. *
  1979. * Helper functions:
  1980. * z90crypt_schedule_reader_timer
  1981. * z90crypt_schedule_reader_task
  1982. * z90crypt_schedule_config_task
  1983. * z90crypt_schedule_cleanup_task
  1984. */
  1985. static inline int
  1986. receive_from_crypto_device(int index, unsigned char *psmid, int *buff_len_p,
  1987. unsigned char *buff, unsigned char __user **dest_p_p)
  1988. {
  1989. int dv, rv;
  1990. struct device *dev_ptr;
  1991. struct caller *caller_p;
  1992. struct ica_rsa_modexpo *icaMsg_p;
  1993. struct list_head *ptr, *tptr;
  1994. memcpy(psmid, NULL_psmid, sizeof(NULL_psmid));
  1995. if (z90crypt.terminating)
  1996. return REC_FATAL_ERROR;
  1997. caller_p = 0;
  1998. dev_ptr = z90crypt.device_p[index];
  1999. rv = 0;
  2000. do {
  2001. if (!dev_ptr || dev_ptr->disabled) {
  2002. rv = REC_NO_WORK; // a disabled device can't return work
  2003. break;
  2004. }
  2005. if (dev_ptr->dev_self_x != index) {
  2006. PRINTKC("Corrupt dev ptr\n");
  2007. z90crypt.terminating = 1;
  2008. rv = REC_FATAL_ERROR;
  2009. break;
  2010. }
  2011. if (!dev_ptr->dev_resp_l || !dev_ptr->dev_resp_p) {
  2012. dv = DEV_REC_EXCEPTION;
  2013. PRINTK("dev_resp_l = %d, dev_resp_p = %p\n",
  2014. dev_ptr->dev_resp_l, dev_ptr->dev_resp_p);
  2015. } else {
  2016. PDEBUG("Dequeue called for device %d\n", index);
  2017. dv = receive_from_AP(index, z90crypt.cdx,
  2018. dev_ptr->dev_resp_l,
  2019. dev_ptr->dev_resp_p, psmid);
  2020. }
  2021. switch (dv) {
  2022. case DEV_REC_EXCEPTION:
  2023. rv = REC_FATAL_ERROR;
  2024. z90crypt.terminating = 1;
  2025. PRINTKC("Exception in receive from device %d\n",
  2026. index);
  2027. break;
  2028. case DEV_ONLINE:
  2029. rv = 0;
  2030. break;
  2031. case DEV_EMPTY:
  2032. rv = REC_EMPTY;
  2033. break;
  2034. case DEV_NO_WORK:
  2035. rv = REC_NO_WORK;
  2036. break;
  2037. case DEV_BAD_MESSAGE:
  2038. case DEV_GONE:
  2039. case REC_HARDWAR_ERR:
  2040. default:
  2041. rv = REC_NO_RESPONSE;
  2042. break;
  2043. }
  2044. if (rv)
  2045. break;
  2046. if (dev_ptr->dev_caller_count <= 0) {
  2047. rv = REC_USER_GONE;
  2048. break;
  2049. }
  2050. list_for_each_safe(ptr, tptr, &dev_ptr->dev_caller_list) {
  2051. caller_p = list_entry(ptr, struct caller, caller_liste);
  2052. if (!memcmp(caller_p->caller_id, psmid,
  2053. sizeof(caller_p->caller_id))) {
  2054. if (!list_empty(&caller_p->caller_liste)) {
  2055. list_del_init(ptr);
  2056. dev_ptr->dev_caller_count--;
  2057. break;
  2058. }
  2059. }
  2060. caller_p = 0;
  2061. }
  2062. if (!caller_p) {
  2063. PRINTKW("Unable to locate PSMID %02X%02X%02X%02X%02X"
  2064. "%02X%02X%02X in device list\n",
  2065. psmid[0], psmid[1], psmid[2], psmid[3],
  2066. psmid[4], psmid[5], psmid[6], psmid[7]);
  2067. rv = REC_USER_GONE;
  2068. break;
  2069. }
  2070. PDEBUG("caller_p after successful receive: %p\n", caller_p);
  2071. rv = convert_response(dev_ptr->dev_resp_p,
  2072. caller_p->caller_buf_p, buff_len_p, buff);
  2073. switch (rv) {
  2074. case REC_USE_PCICA:
  2075. break;
  2076. case REC_OPERAND_INV:
  2077. case REC_OPERAND_SIZE:
  2078. case REC_EVEN_MOD:
  2079. case REC_INVALID_PAD:
  2080. PDEBUG("device %d: 'user error' %d\n", index, rv);
  2081. break;
  2082. case WRONG_DEVICE_TYPE:
  2083. case REC_HARDWAR_ERR:
  2084. case REC_BAD_MESSAGE:
  2085. PRINTKW("device %d: hardware error %d\n", index, rv);
  2086. rv = REC_NO_RESPONSE;
  2087. break;
  2088. default:
  2089. PDEBUG("device %d: rv = %d\n", index, rv);
  2090. break;
  2091. }
  2092. } while (0);
  2093. switch (rv) {
  2094. case 0:
  2095. PDEBUG("Successful receive from device %d\n", index);
  2096. icaMsg_p = (struct ica_rsa_modexpo *)caller_p->caller_buf_p;
  2097. *dest_p_p = icaMsg_p->outputdata;
  2098. if (*buff_len_p == 0)
  2099. PRINTK("Zero *buff_len_p\n");
  2100. break;
  2101. case REC_NO_RESPONSE:
  2102. PRINTKW("Removing device %d from availability\n", index);
  2103. remove_device(dev_ptr);
  2104. break;
  2105. }
  2106. if (caller_p)
  2107. unbuild_caller(dev_ptr, caller_p);
  2108. return rv;
  2109. }
  2110. static inline void
  2111. helper_send_work(int index)
  2112. {
  2113. struct work_element *rq_p;
  2114. int rv;
  2115. if (list_empty(&request_list))
  2116. return;
  2117. requestq_count--;
  2118. rq_p = list_entry(request_list.next, struct work_element, liste);
  2119. list_del_init(&rq_p->liste);
  2120. rq_p->audit[1] |= FP_REMREQUEST;
  2121. if (rq_p->devtype == SHRT2DEVPTR(index)->dev_type) {
  2122. rq_p->devindex = SHRT2LONG(index);
  2123. rv = send_to_crypto_device(rq_p);
  2124. if (rv == 0) {
  2125. rq_p->requestsent = jiffies;
  2126. rq_p->audit[0] |= FP_SENT;
  2127. list_add_tail(&rq_p->liste, &pending_list);
  2128. ++pendingq_count;
  2129. rq_p->audit[0] |= FP_PENDING;
  2130. } else {
  2131. switch (rv) {
  2132. case REC_OPERAND_INV:
  2133. case REC_OPERAND_SIZE:
  2134. case REC_EVEN_MOD:
  2135. case REC_INVALID_PAD:
  2136. rq_p->retcode = -EINVAL;
  2137. break;
  2138. case SEN_NOT_AVAIL:
  2139. case SEN_RETRY:
  2140. case REC_NO_RESPONSE:
  2141. default:
  2142. if (z90crypt.mask.st_count > 1)
  2143. rq_p->retcode =
  2144. -ERESTARTSYS;
  2145. else
  2146. rq_p->retcode = -ENODEV;
  2147. break;
  2148. }
  2149. rq_p->status[0] |= STAT_FAILED;
  2150. rq_p->audit[1] |= FP_AWAKENING;
  2151. atomic_set(&rq_p->alarmrung, 1);
  2152. wake_up(&rq_p->waitq);
  2153. }
  2154. } else {
  2155. if (z90crypt.mask.st_count > 1)
  2156. rq_p->retcode = -ERESTARTSYS;
  2157. else
  2158. rq_p->retcode = -ENODEV;
  2159. rq_p->status[0] |= STAT_FAILED;
  2160. rq_p->audit[1] |= FP_AWAKENING;
  2161. atomic_set(&rq_p->alarmrung, 1);
  2162. wake_up(&rq_p->waitq);
  2163. }
  2164. }
  2165. static inline void
  2166. helper_handle_work_element(int index, unsigned char psmid[8], int rc,
  2167. int buff_len, unsigned char *buff,
  2168. unsigned char __user *resp_addr)
  2169. {
  2170. struct work_element *pq_p;
  2171. struct list_head *lptr, *tptr;
  2172. pq_p = 0;
  2173. list_for_each_safe(lptr, tptr, &pending_list) {
  2174. pq_p = list_entry(lptr, struct work_element, liste);
  2175. if (!memcmp(pq_p->caller_id, psmid, sizeof(pq_p->caller_id))) {
  2176. list_del_init(lptr);
  2177. pendingq_count--;
  2178. pq_p->audit[1] |= FP_NOTPENDING;
  2179. break;
  2180. }
  2181. pq_p = 0;
  2182. }
  2183. if (!pq_p) {
  2184. PRINTK("device %d has work but no caller exists on pending Q\n",
  2185. SHRT2LONG(index));
  2186. return;
  2187. }
  2188. switch (rc) {
  2189. case 0:
  2190. pq_p->resp_buff_size = buff_len;
  2191. pq_p->audit[1] |= FP_RESPSIZESET;
  2192. if (buff_len) {
  2193. pq_p->resp_addr = resp_addr;
  2194. pq_p->audit[1] |= FP_RESPADDRCOPIED;
  2195. memcpy(pq_p->resp_buff, buff, buff_len);
  2196. pq_p->audit[1] |= FP_RESPBUFFCOPIED;
  2197. }
  2198. break;
  2199. case REC_OPERAND_INV:
  2200. case REC_OPERAND_SIZE:
  2201. case REC_EVEN_MOD:
  2202. case REC_INVALID_PAD:
  2203. PDEBUG("-EINVAL after application error %d\n", rc);
  2204. pq_p->retcode = -EINVAL;
  2205. pq_p->status[0] |= STAT_FAILED;
  2206. break;
  2207. case REC_USE_PCICA:
  2208. pq_p->retcode = -ERESTARTSYS;
  2209. pq_p->status[0] |= STAT_FAILED;
  2210. break;
  2211. case REC_NO_RESPONSE:
  2212. default:
  2213. if (z90crypt.mask.st_count > 1)
  2214. pq_p->retcode = -ERESTARTSYS;
  2215. else
  2216. pq_p->retcode = -ENODEV;
  2217. pq_p->status[0] |= STAT_FAILED;
  2218. break;
  2219. }
  2220. if ((pq_p->status[0] != STAT_FAILED) || (pq_p->retcode != -ERELEASED)) {
  2221. pq_p->audit[1] |= FP_AWAKENING;
  2222. atomic_set(&pq_p->alarmrung, 1);
  2223. wake_up(&pq_p->waitq);
  2224. }
  2225. }
  2226. /**
  2227. * return TRUE if the work element should be removed from the queue
  2228. */
  2229. static inline int
  2230. helper_receive_rc(int index, int *rc_p)
  2231. {
  2232. switch (*rc_p) {
  2233. case 0:
  2234. case REC_OPERAND_INV:
  2235. case REC_OPERAND_SIZE:
  2236. case REC_EVEN_MOD:
  2237. case REC_INVALID_PAD:
  2238. case REC_USE_PCICA:
  2239. break;
  2240. case REC_BUSY:
  2241. case REC_NO_WORK:
  2242. case REC_EMPTY:
  2243. case REC_RETRY_DEV:
  2244. case REC_FATAL_ERROR:
  2245. return 0;
  2246. case REC_NO_RESPONSE:
  2247. break;
  2248. default:
  2249. PRINTK("rc %d, device %d converted to REC_NO_RESPONSE\n",
  2250. *rc_p, SHRT2LONG(index));
  2251. *rc_p = REC_NO_RESPONSE;
  2252. break;
  2253. }
  2254. return 1;
  2255. }
  2256. static inline void
  2257. z90crypt_schedule_reader_timer(void)
  2258. {
  2259. if (timer_pending(&reader_timer))
  2260. return;
  2261. if (mod_timer(&reader_timer, jiffies+(READERTIME*HZ/1000)) != 0)
  2262. PRINTK("Timer pending while modifying reader timer\n");
  2263. }
  2264. static void
  2265. z90crypt_reader_task(unsigned long ptr)
  2266. {
  2267. int workavail, index, rc, buff_len;
  2268. unsigned char psmid[8];
  2269. unsigned char __user *resp_addr;
  2270. static unsigned char buff[1024];
  2271. /**
  2272. * we use workavail = 2 to ensure 2 passes with nothing dequeued before
  2273. * exiting the loop. If (pendingq_count+requestq_count) == 0 after the
  2274. * loop, there is no work remaining on the queues.
  2275. */
  2276. resp_addr = 0;
  2277. workavail = 2;
  2278. buff_len = 0;
  2279. while (workavail) {
  2280. workavail--;
  2281. rc = 0;
  2282. spin_lock_irq(&queuespinlock);
  2283. memset(buff, 0x00, sizeof(buff));
  2284. /* Dequeue once from each device in round robin. */
  2285. for (index = 0; index < z90crypt.mask.st_count; index++) {
  2286. PDEBUG("About to receive.\n");
  2287. rc = receive_from_crypto_device(SHRT2LONG(index),
  2288. psmid,
  2289. &buff_len,
  2290. buff,
  2291. &resp_addr);
  2292. PDEBUG("Dequeued: rc = %d.\n", rc);
  2293. if (helper_receive_rc(index, &rc)) {
  2294. if (rc != REC_NO_RESPONSE) {
  2295. helper_send_work(index);
  2296. workavail = 2;
  2297. }
  2298. helper_handle_work_element(index, psmid, rc,
  2299. buff_len, buff,
  2300. resp_addr);
  2301. }
  2302. if (rc == REC_FATAL_ERROR)
  2303. PRINTKW("REC_FATAL_ERROR from device %d!\n",
  2304. SHRT2LONG(index));
  2305. }
  2306. spin_unlock_irq(&queuespinlock);
  2307. }
  2308. if (pendingq_count + requestq_count)
  2309. z90crypt_schedule_reader_timer();
  2310. }
  2311. static inline void
  2312. z90crypt_schedule_config_task(unsigned int expiration)
  2313. {
  2314. if (timer_pending(&config_timer))
  2315. return;
  2316. if (mod_timer(&config_timer, jiffies+(expiration*HZ)) != 0)
  2317. PRINTK("Timer pending while modifying config timer\n");
  2318. }
  2319. static void
  2320. z90crypt_config_task(unsigned long ptr)
  2321. {
  2322. int rc;
  2323. PDEBUG("jiffies %ld\n", jiffies);
  2324. if ((rc = refresh_z90crypt(&z90crypt.cdx)))
  2325. PRINTK("Error %d detected in refresh_z90crypt.\n", rc);
  2326. /* If return was fatal, don't bother reconfiguring */
  2327. if ((rc != TSQ_FATAL_ERROR) && (rc != RSQ_FATAL_ERROR))
  2328. z90crypt_schedule_config_task(CONFIGTIME);
  2329. }
  2330. static inline void
  2331. z90crypt_schedule_cleanup_task(void)
  2332. {
  2333. if (timer_pending(&cleanup_timer))
  2334. return;
  2335. if (mod_timer(&cleanup_timer, jiffies+(CLEANUPTIME*HZ)) != 0)
  2336. PRINTK("Timer pending while modifying cleanup timer\n");
  2337. }
  2338. static inline void
  2339. helper_drain_queues(void)
  2340. {
  2341. struct work_element *pq_p;
  2342. struct list_head *lptr, *tptr;
  2343. list_for_each_safe(lptr, tptr, &pending_list) {
  2344. pq_p = list_entry(lptr, struct work_element, liste);
  2345. pq_p->retcode = -ENODEV;
  2346. pq_p->status[0] |= STAT_FAILED;
  2347. unbuild_caller(LONG2DEVPTR(pq_p->devindex),
  2348. (struct caller *)pq_p->requestptr);
  2349. list_del_init(lptr);
  2350. pendingq_count--;
  2351. pq_p->audit[1] |= FP_NOTPENDING;
  2352. pq_p->audit[1] |= FP_AWAKENING;
  2353. atomic_set(&pq_p->alarmrung, 1);
  2354. wake_up(&pq_p->waitq);
  2355. }
  2356. list_for_each_safe(lptr, tptr, &request_list) {
  2357. pq_p = list_entry(lptr, struct work_element, liste);
  2358. pq_p->retcode = -ENODEV;
  2359. pq_p->status[0] |= STAT_FAILED;
  2360. list_del_init(lptr);
  2361. requestq_count--;
  2362. pq_p->audit[1] |= FP_REMREQUEST;
  2363. pq_p->audit[1] |= FP_AWAKENING;
  2364. atomic_set(&pq_p->alarmrung, 1);
  2365. wake_up(&pq_p->waitq);
  2366. }
  2367. }
  2368. static inline void
  2369. helper_timeout_requests(void)
  2370. {
  2371. struct work_element *pq_p;
  2372. struct list_head *lptr, *tptr;
  2373. long timelimit;
  2374. timelimit = jiffies - (CLEANUPTIME * HZ);
  2375. /* The list is in strict chronological order */
  2376. list_for_each_safe(lptr, tptr, &pending_list) {
  2377. pq_p = list_entry(lptr, struct work_element, liste);
  2378. if (pq_p->requestsent >= timelimit)
  2379. break;
  2380. PRINTKW("Purging(PQ) PSMID %02X%02X%02X%02X%02X%02X%02X%02X\n",
  2381. ((struct caller *)pq_p->requestptr)->caller_id[0],
  2382. ((struct caller *)pq_p->requestptr)->caller_id[1],
  2383. ((struct caller *)pq_p->requestptr)->caller_id[2],
  2384. ((struct caller *)pq_p->requestptr)->caller_id[3],
  2385. ((struct caller *)pq_p->requestptr)->caller_id[4],
  2386. ((struct caller *)pq_p->requestptr)->caller_id[5],
  2387. ((struct caller *)pq_p->requestptr)->caller_id[6],
  2388. ((struct caller *)pq_p->requestptr)->caller_id[7]);
  2389. pq_p->retcode = -ETIMEOUT;
  2390. pq_p->status[0] |= STAT_FAILED;
  2391. /* get this off any caller queue it may be on */
  2392. unbuild_caller(LONG2DEVPTR(pq_p->devindex),
  2393. (struct caller *) pq_p->requestptr);
  2394. list_del_init(lptr);
  2395. pendingq_count--;
  2396. pq_p->audit[1] |= FP_TIMEDOUT;
  2397. pq_p->audit[1] |= FP_NOTPENDING;
  2398. pq_p->audit[1] |= FP_AWAKENING;
  2399. atomic_set(&pq_p->alarmrung, 1);
  2400. wake_up(&pq_p->waitq);
  2401. }
  2402. /**
  2403. * If pending count is zero, items left on the request queue may
  2404. * never be processed.
  2405. */
  2406. if (pendingq_count <= 0) {
  2407. list_for_each_safe(lptr, tptr, &request_list) {
  2408. pq_p = list_entry(lptr, struct work_element, liste);
  2409. if (pq_p->requestsent >= timelimit)
  2410. break;
  2411. PRINTKW("Purging(RQ) PSMID %02X%02X%02X%02X%02X%02X%02X%02X\n",
  2412. ((struct caller *)pq_p->requestptr)->caller_id[0],
  2413. ((struct caller *)pq_p->requestptr)->caller_id[1],
  2414. ((struct caller *)pq_p->requestptr)->caller_id[2],
  2415. ((struct caller *)pq_p->requestptr)->caller_id[3],
  2416. ((struct caller *)pq_p->requestptr)->caller_id[4],
  2417. ((struct caller *)pq_p->requestptr)->caller_id[5],
  2418. ((struct caller *)pq_p->requestptr)->caller_id[6],
  2419. ((struct caller *)pq_p->requestptr)->caller_id[7]);
  2420. pq_p->retcode = -ETIMEOUT;
  2421. pq_p->status[0] |= STAT_FAILED;
  2422. list_del_init(lptr);
  2423. requestq_count--;
  2424. pq_p->audit[1] |= FP_TIMEDOUT;
  2425. pq_p->audit[1] |= FP_REMREQUEST;
  2426. pq_p->audit[1] |= FP_AWAKENING;
  2427. atomic_set(&pq_p->alarmrung, 1);
  2428. wake_up(&pq_p->waitq);
  2429. }
  2430. }
  2431. }
  2432. static void
  2433. z90crypt_cleanup_task(unsigned long ptr)
  2434. {
  2435. PDEBUG("jiffies %ld\n", jiffies);
  2436. spin_lock_irq(&queuespinlock);
  2437. if (z90crypt.mask.st_count <= 0) // no devices!
  2438. helper_drain_queues();
  2439. else
  2440. helper_timeout_requests();
  2441. spin_unlock_irq(&queuespinlock);
  2442. z90crypt_schedule_cleanup_task();
  2443. }
  2444. static void
  2445. z90crypt_schedule_reader_task(unsigned long ptr)
  2446. {
  2447. tasklet_schedule(&reader_tasklet);
  2448. }
  2449. /**
  2450. * Lowlevel Functions:
  2451. *
  2452. * create_z90crypt: creates and initializes basic data structures
  2453. * refresh_z90crypt: re-initializes basic data structures
  2454. * find_crypto_devices: returns a count and mask of hardware status
  2455. * create_crypto_device: builds the descriptor for a device
  2456. * destroy_crypto_device: unallocates the descriptor for a device
  2457. * destroy_z90crypt: drains all work, unallocates structs
  2458. */
  2459. /**
  2460. * build the z90crypt root structure using the given domain index
  2461. */
  2462. static int
  2463. create_z90crypt(int *cdx_p)
  2464. {
  2465. struct hdware_block *hdware_blk_p;
  2466. memset(&z90crypt, 0x00, sizeof(struct z90crypt));
  2467. z90crypt.domain_established = 0;
  2468. z90crypt.len = sizeof(struct z90crypt);
  2469. z90crypt.max_count = Z90CRYPT_NUM_DEVS;
  2470. z90crypt.cdx = *cdx_p;
  2471. hdware_blk_p = (struct hdware_block *)
  2472. kmalloc(sizeof(struct hdware_block), GFP_ATOMIC);
  2473. if (!hdware_blk_p) {
  2474. PDEBUG("kmalloc for hardware block failed\n");
  2475. return ENOMEM;
  2476. }
  2477. memset(hdware_blk_p, 0x00, sizeof(struct hdware_block));
  2478. z90crypt.hdware_info = hdware_blk_p;
  2479. return 0;
  2480. }
  2481. static inline int
  2482. helper_scan_devices(int cdx_array[16], int *cdx_p, int *correct_cdx_found)
  2483. {
  2484. enum hdstat hd_stat;
  2485. int q_depth, dev_type;
  2486. int indx, chkdom, numdomains;
  2487. q_depth = dev_type = numdomains = 0;
  2488. for (chkdom = 0; chkdom <= 15; cdx_array[chkdom++] = -1);
  2489. for (indx = 0; indx < z90crypt.max_count; indx++) {
  2490. hd_stat = HD_NOT_THERE;
  2491. numdomains = 0;
  2492. for (chkdom = 0; chkdom <= 15; chkdom++) {
  2493. hd_stat = query_online(indx, chkdom, MAX_RESET,
  2494. &q_depth, &dev_type);
  2495. if (hd_stat == HD_TSQ_EXCEPTION) {
  2496. z90crypt.terminating = 1;
  2497. PRINTKC("exception taken!\n");
  2498. break;
  2499. }
  2500. if (hd_stat == HD_ONLINE) {
  2501. cdx_array[numdomains++] = chkdom;
  2502. if (*cdx_p == chkdom) {
  2503. *correct_cdx_found = 1;
  2504. break;
  2505. }
  2506. }
  2507. }
  2508. if ((*correct_cdx_found == 1) || (numdomains != 0))
  2509. break;
  2510. if (z90crypt.terminating)
  2511. break;
  2512. }
  2513. return numdomains;
  2514. }
  2515. static inline int
  2516. probe_crypto_domain(int *cdx_p)
  2517. {
  2518. int cdx_array[16];
  2519. char cdx_array_text[53], temp[5];
  2520. int correct_cdx_found, numdomains;
  2521. correct_cdx_found = 0;
  2522. numdomains = helper_scan_devices(cdx_array, cdx_p, &correct_cdx_found);
  2523. if (z90crypt.terminating)
  2524. return TSQ_FATAL_ERROR;
  2525. if (correct_cdx_found)
  2526. return 0;
  2527. if (numdomains == 0) {
  2528. PRINTKW("Unable to find crypto domain: No devices found\n");
  2529. return Z90C_NO_DEVICES;
  2530. }
  2531. if (numdomains == 1) {
  2532. if (*cdx_p == -1) {
  2533. *cdx_p = cdx_array[0];
  2534. return 0;
  2535. }
  2536. PRINTKW("incorrect domain: specified = %d, found = %d\n",
  2537. *cdx_p, cdx_array[0]);
  2538. return Z90C_INCORRECT_DOMAIN;
  2539. }
  2540. numdomains--;
  2541. sprintf(cdx_array_text, "%d", cdx_array[numdomains]);
  2542. while (numdomains) {
  2543. numdomains--;
  2544. sprintf(temp, ", %d", cdx_array[numdomains]);
  2545. strcat(cdx_array_text, temp);
  2546. }
  2547. PRINTKW("ambiguous domain detected: specified = %d, found array = %s\n",
  2548. *cdx_p, cdx_array_text);
  2549. return Z90C_AMBIGUOUS_DOMAIN;
  2550. }
  2551. static int
  2552. refresh_z90crypt(int *cdx_p)
  2553. {
  2554. int i, j, indx, rv;
  2555. static struct status local_mask;
  2556. struct device *devPtr;
  2557. unsigned char oldStat, newStat;
  2558. int return_unchanged;
  2559. if (z90crypt.len != sizeof(z90crypt))
  2560. return ENOTINIT;
  2561. if (z90crypt.terminating)
  2562. return TSQ_FATAL_ERROR;
  2563. rv = 0;
  2564. if (!z90crypt.hdware_info->hdware_mask.st_count &&
  2565. !z90crypt.domain_established) {
  2566. rv = probe_crypto_domain(cdx_p);
  2567. if (z90crypt.terminating)
  2568. return TSQ_FATAL_ERROR;
  2569. if (rv == Z90C_NO_DEVICES)
  2570. return 0; // try later
  2571. if (rv)
  2572. return rv;
  2573. z90crypt.cdx = *cdx_p;
  2574. z90crypt.domain_established = 1;
  2575. }
  2576. rv = find_crypto_devices(&local_mask);
  2577. if (rv) {
  2578. PRINTK("find crypto devices returned %d\n", rv);
  2579. return rv;
  2580. }
  2581. if (!memcmp(&local_mask, &z90crypt.hdware_info->hdware_mask,
  2582. sizeof(struct status))) {
  2583. return_unchanged = 1;
  2584. for (i = 0; i < Z90CRYPT_NUM_TYPES; i++) {
  2585. /**
  2586. * Check for disabled cards. If any device is marked
  2587. * disabled, destroy it.
  2588. */
  2589. for (j = 0;
  2590. j < z90crypt.hdware_info->type_mask[i].st_count;
  2591. j++) {
  2592. indx = z90crypt.hdware_info->type_x_addr[i].
  2593. device_index[j];
  2594. devPtr = z90crypt.device_p[indx];
  2595. if (devPtr && devPtr->disabled) {
  2596. local_mask.st_mask[indx] = HD_NOT_THERE;
  2597. return_unchanged = 0;
  2598. }
  2599. }
  2600. }
  2601. if (return_unchanged == 1)
  2602. return 0;
  2603. }
  2604. spin_lock_irq(&queuespinlock);
  2605. for (i = 0; i < z90crypt.max_count; i++) {
  2606. oldStat = z90crypt.hdware_info->hdware_mask.st_mask[i];
  2607. newStat = local_mask.st_mask[i];
  2608. if ((oldStat == HD_ONLINE) && (newStat != HD_ONLINE))
  2609. destroy_crypto_device(i);
  2610. else if ((oldStat != HD_ONLINE) && (newStat == HD_ONLINE)) {
  2611. rv = create_crypto_device(i);
  2612. if (rv >= REC_FATAL_ERROR)
  2613. return rv;
  2614. if (rv != 0) {
  2615. local_mask.st_mask[i] = HD_NOT_THERE;
  2616. local_mask.st_count--;
  2617. }
  2618. }
  2619. }
  2620. memcpy(z90crypt.hdware_info->hdware_mask.st_mask, local_mask.st_mask,
  2621. sizeof(local_mask.st_mask));
  2622. z90crypt.hdware_info->hdware_mask.st_count = local_mask.st_count;
  2623. z90crypt.hdware_info->hdware_mask.disabled_count =
  2624. local_mask.disabled_count;
  2625. refresh_index_array(&z90crypt.mask, &z90crypt.overall_device_x);
  2626. for (i = 0; i < Z90CRYPT_NUM_TYPES; i++)
  2627. refresh_index_array(&(z90crypt.hdware_info->type_mask[i]),
  2628. &(z90crypt.hdware_info->type_x_addr[i]));
  2629. spin_unlock_irq(&queuespinlock);
  2630. return rv;
  2631. }
  2632. static int
  2633. find_crypto_devices(struct status *deviceMask)
  2634. {
  2635. int i, q_depth, dev_type;
  2636. enum hdstat hd_stat;
  2637. deviceMask->st_count = 0;
  2638. deviceMask->disabled_count = 0;
  2639. deviceMask->user_disabled_count = 0;
  2640. for (i = 0; i < z90crypt.max_count; i++) {
  2641. hd_stat = query_online(i, z90crypt.cdx, MAX_RESET, &q_depth,
  2642. &dev_type);
  2643. if (hd_stat == HD_TSQ_EXCEPTION) {
  2644. z90crypt.terminating = 1;
  2645. PRINTKC("Exception during probe for crypto devices\n");
  2646. return TSQ_FATAL_ERROR;
  2647. }
  2648. deviceMask->st_mask[i] = hd_stat;
  2649. if (hd_stat == HD_ONLINE) {
  2650. PDEBUG("Got an online crypto!: %d\n", i);
  2651. PDEBUG("Got a queue depth of %d\n", q_depth);
  2652. PDEBUG("Got a device type of %d\n", dev_type);
  2653. if (q_depth <= 0)
  2654. return TSQ_FATAL_ERROR;
  2655. deviceMask->st_count++;
  2656. z90crypt.q_depth_array[i] = q_depth;
  2657. z90crypt.dev_type_array[i] = dev_type;
  2658. }
  2659. }
  2660. return 0;
  2661. }
  2662. static int
  2663. refresh_index_array(struct status *status_str, struct device_x *index_array)
  2664. {
  2665. int i, count;
  2666. enum devstat stat;
  2667. i = -1;
  2668. count = 0;
  2669. do {
  2670. stat = status_str->st_mask[++i];
  2671. if (stat == DEV_ONLINE)
  2672. index_array->device_index[count++] = i;
  2673. } while ((i < Z90CRYPT_NUM_DEVS) && (count < status_str->st_count));
  2674. return count;
  2675. }
  2676. static int
  2677. create_crypto_device(int index)
  2678. {
  2679. int rv, devstat, total_size;
  2680. struct device *dev_ptr;
  2681. struct status *type_str_p;
  2682. int deviceType;
  2683. dev_ptr = z90crypt.device_p[index];
  2684. if (!dev_ptr) {
  2685. total_size = sizeof(struct device) +
  2686. z90crypt.q_depth_array[index] * sizeof(int);
  2687. dev_ptr = (struct device *) kmalloc(total_size, GFP_ATOMIC);
  2688. if (!dev_ptr) {
  2689. PRINTK("kmalloc device %d failed\n", index);
  2690. return ENOMEM;
  2691. }
  2692. memset(dev_ptr, 0, total_size);
  2693. dev_ptr->dev_resp_p = kmalloc(MAX_RESPONSE_SIZE, GFP_ATOMIC);
  2694. if (!dev_ptr->dev_resp_p) {
  2695. kfree(dev_ptr);
  2696. PRINTK("kmalloc device %d rec buffer failed\n", index);
  2697. return ENOMEM;
  2698. }
  2699. dev_ptr->dev_resp_l = MAX_RESPONSE_SIZE;
  2700. INIT_LIST_HEAD(&(dev_ptr->dev_caller_list));
  2701. }
  2702. devstat = reset_device(index, z90crypt.cdx, MAX_RESET);
  2703. if (devstat == DEV_RSQ_EXCEPTION) {
  2704. PRINTK("exception during reset device %d\n", index);
  2705. kfree(dev_ptr->dev_resp_p);
  2706. kfree(dev_ptr);
  2707. return RSQ_FATAL_ERROR;
  2708. }
  2709. if (devstat == DEV_ONLINE) {
  2710. dev_ptr->dev_self_x = index;
  2711. dev_ptr->dev_type = z90crypt.dev_type_array[index];
  2712. if (dev_ptr->dev_type == NILDEV) {
  2713. rv = probe_device_type(dev_ptr);
  2714. if (rv) {
  2715. PRINTK("rv = %d from probe_device_type %d\n",
  2716. rv, index);
  2717. kfree(dev_ptr->dev_resp_p);
  2718. kfree(dev_ptr);
  2719. return rv;
  2720. }
  2721. }
  2722. if (dev_ptr->dev_type == PCIXCC_UNK) {
  2723. rv = probe_PCIXCC_type(dev_ptr);
  2724. if (rv) {
  2725. PRINTK("rv = %d from probe_PCIXCC_type %d\n",
  2726. rv, index);
  2727. kfree(dev_ptr->dev_resp_p);
  2728. kfree(dev_ptr);
  2729. return rv;
  2730. }
  2731. }
  2732. deviceType = dev_ptr->dev_type;
  2733. z90crypt.dev_type_array[index] = deviceType;
  2734. if (deviceType == PCICA)
  2735. z90crypt.hdware_info->device_type_array[index] = 1;
  2736. else if (deviceType == PCICC)
  2737. z90crypt.hdware_info->device_type_array[index] = 2;
  2738. else if (deviceType == PCIXCC_MCL2)
  2739. z90crypt.hdware_info->device_type_array[index] = 3;
  2740. else if (deviceType == PCIXCC_MCL3)
  2741. z90crypt.hdware_info->device_type_array[index] = 4;
  2742. else if (deviceType == CEX2C)
  2743. z90crypt.hdware_info->device_type_array[index] = 5;
  2744. else if (deviceType == CEX2A)
  2745. z90crypt.hdware_info->device_type_array[index] = 6;
  2746. else // No idea how this would happen.
  2747. z90crypt.hdware_info->device_type_array[index] = -1;
  2748. }
  2749. /**
  2750. * 'q_depth' returned by the hardware is one less than
  2751. * the actual depth
  2752. */
  2753. dev_ptr->dev_q_depth = z90crypt.q_depth_array[index];
  2754. dev_ptr->dev_type = z90crypt.dev_type_array[index];
  2755. dev_ptr->dev_stat = devstat;
  2756. dev_ptr->disabled = 0;
  2757. z90crypt.device_p[index] = dev_ptr;
  2758. if (devstat == DEV_ONLINE) {
  2759. if (z90crypt.mask.st_mask[index] != DEV_ONLINE) {
  2760. z90crypt.mask.st_mask[index] = DEV_ONLINE;
  2761. z90crypt.mask.st_count++;
  2762. }
  2763. deviceType = dev_ptr->dev_type;
  2764. type_str_p = &z90crypt.hdware_info->type_mask[deviceType];
  2765. if (type_str_p->st_mask[index] != DEV_ONLINE) {
  2766. type_str_p->st_mask[index] = DEV_ONLINE;
  2767. type_str_p->st_count++;
  2768. }
  2769. }
  2770. return 0;
  2771. }
  2772. static int
  2773. destroy_crypto_device(int index)
  2774. {
  2775. struct device *dev_ptr;
  2776. int t, disabledFlag;
  2777. dev_ptr = z90crypt.device_p[index];
  2778. /* remember device type; get rid of device struct */
  2779. if (dev_ptr) {
  2780. disabledFlag = dev_ptr->disabled;
  2781. t = dev_ptr->dev_type;
  2782. kfree(dev_ptr->dev_resp_p);
  2783. kfree(dev_ptr);
  2784. } else {
  2785. disabledFlag = 0;
  2786. t = -1;
  2787. }
  2788. z90crypt.device_p[index] = 0;
  2789. /* if the type is valid, remove the device from the type_mask */
  2790. if ((t != -1) && z90crypt.hdware_info->type_mask[t].st_mask[index]) {
  2791. z90crypt.hdware_info->type_mask[t].st_mask[index] = 0x00;
  2792. z90crypt.hdware_info->type_mask[t].st_count--;
  2793. if (disabledFlag == 1)
  2794. z90crypt.hdware_info->type_mask[t].disabled_count--;
  2795. }
  2796. if (z90crypt.mask.st_mask[index] != DEV_GONE) {
  2797. z90crypt.mask.st_mask[index] = DEV_GONE;
  2798. z90crypt.mask.st_count--;
  2799. }
  2800. z90crypt.hdware_info->device_type_array[index] = 0;
  2801. return 0;
  2802. }
  2803. static void
  2804. destroy_z90crypt(void)
  2805. {
  2806. int i;
  2807. for (i = 0; i < z90crypt.max_count; i++)
  2808. if (z90crypt.device_p[i])
  2809. destroy_crypto_device(i);
  2810. kfree(z90crypt.hdware_info);
  2811. memset((void *)&z90crypt, 0, sizeof(z90crypt));
  2812. }
  2813. static unsigned char static_testmsg[384] = {
  2814. 0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x00,0x06,0x00,0x00,
  2815. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,
  2816. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x43,0x43,
  2817. 0x41,0x2d,0x41,0x50,0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,0x00,0x00,0x00,0x00,
  2818. 0x50,0x4b,0x00,0x00,0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2819. 0x00,0x00,0x00,0x00,0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2820. 0x00,0x00,0x00,0x00,0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x54,0x32,
  2821. 0x01,0x00,0xa0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2822. 0xb8,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2823. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2824. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2825. 0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2826. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x49,0x43,0x53,0x46,
  2827. 0x20,0x20,0x20,0x20,0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,0x2d,0x31,0x2e,0x32,
  2828. 0x37,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
  2829. 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  2830. 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
  2831. 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,0x88,0x1e,0x00,0x00,
  2832. 0x57,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,0x03,0x02,0x00,0x00,
  2833. 0x40,0x01,0x00,0x01,0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,0xf6,0xd2,0x7b,0x58,
  2834. 0x4b,0xf9,0x28,0x68,0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,0x63,0x42,0xef,0xf8,
  2835. 0xfd,0xa4,0xf8,0xb0,0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,0x53,0x8c,0x6f,0x4e,
  2836. 0x72,0x8f,0x6c,0x04,0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,0xf7,0xdd,0xfd,0x4f,
  2837. 0x11,0x36,0x95,0x5d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  2838. };
  2839. static int
  2840. probe_device_type(struct device *devPtr)
  2841. {
  2842. int rv, dv, i, index, length;
  2843. unsigned char psmid[8];
  2844. static unsigned char loc_testmsg[sizeof(static_testmsg)];
  2845. index = devPtr->dev_self_x;
  2846. rv = 0;
  2847. do {
  2848. memcpy(loc_testmsg, static_testmsg, sizeof(static_testmsg));
  2849. length = sizeof(static_testmsg) - 24;
  2850. /* the -24 allows for the header */
  2851. dv = send_to_AP(index, z90crypt.cdx, length, loc_testmsg);
  2852. if (dv) {
  2853. PDEBUG("dv returned by send during probe: %d\n", dv);
  2854. if (dv == DEV_SEN_EXCEPTION) {
  2855. rv = SEN_FATAL_ERROR;
  2856. PRINTKC("exception in send to AP %d\n", index);
  2857. break;
  2858. }
  2859. PDEBUG("return value from send_to_AP: %d\n", rv);
  2860. switch (dv) {
  2861. case DEV_GONE:
  2862. PDEBUG("dev %d not available\n", index);
  2863. rv = SEN_NOT_AVAIL;
  2864. break;
  2865. case DEV_ONLINE:
  2866. rv = 0;
  2867. break;
  2868. case DEV_EMPTY:
  2869. rv = SEN_NOT_AVAIL;
  2870. break;
  2871. case DEV_NO_WORK:
  2872. rv = SEN_FATAL_ERROR;
  2873. break;
  2874. case DEV_BAD_MESSAGE:
  2875. rv = SEN_USER_ERROR;
  2876. break;
  2877. case DEV_QUEUE_FULL:
  2878. rv = SEN_QUEUE_FULL;
  2879. break;
  2880. default:
  2881. PRINTK("unknown dv=%d for dev %d\n", dv, index);
  2882. rv = SEN_NOT_AVAIL;
  2883. break;
  2884. }
  2885. }
  2886. if (rv)
  2887. break;
  2888. for (i = 0; i < 6; i++) {
  2889. mdelay(300);
  2890. dv = receive_from_AP(index, z90crypt.cdx,
  2891. devPtr->dev_resp_l,
  2892. devPtr->dev_resp_p, psmid);
  2893. PDEBUG("dv returned by DQ = %d\n", dv);
  2894. if (dv == DEV_REC_EXCEPTION) {
  2895. rv = REC_FATAL_ERROR;
  2896. PRINTKC("exception in dequeue %d\n",
  2897. index);
  2898. break;
  2899. }
  2900. switch (dv) {
  2901. case DEV_ONLINE:
  2902. rv = 0;
  2903. break;
  2904. case DEV_EMPTY:
  2905. rv = REC_EMPTY;
  2906. break;
  2907. case DEV_NO_WORK:
  2908. rv = REC_NO_WORK;
  2909. break;
  2910. case DEV_BAD_MESSAGE:
  2911. case DEV_GONE:
  2912. default:
  2913. rv = REC_NO_RESPONSE;
  2914. break;
  2915. }
  2916. if ((rv != 0) && (rv != REC_NO_WORK))
  2917. break;
  2918. if (rv == 0)
  2919. break;
  2920. }
  2921. if (rv)
  2922. break;
  2923. rv = (devPtr->dev_resp_p[0] == 0x00) &&
  2924. (devPtr->dev_resp_p[1] == 0x86);
  2925. if (rv)
  2926. devPtr->dev_type = PCICC;
  2927. else
  2928. devPtr->dev_type = PCICA;
  2929. rv = 0;
  2930. } while (0);
  2931. /* In a general error case, the card is not marked online */
  2932. return rv;
  2933. }
  2934. static unsigned char MCL3_testmsg[] = {
  2935. 0x00,0x00,0x00,0x00,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,
  2936. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2937. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2938. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2939. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  2940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  2941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  2942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  2943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2945. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2946. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2947. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2949. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2950. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2951. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2952. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2955. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  2956. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  2957. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  2958. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  2959. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  2960. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  2961. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  2962. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  2963. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  2964. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  2965. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  2966. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  2967. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  2968. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  2969. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,0xF1,0x3D,0x93,0x53
  2970. };
  2971. static int
  2972. probe_PCIXCC_type(struct device *devPtr)
  2973. {
  2974. int rv, dv, i, index, length;
  2975. unsigned char psmid[8];
  2976. static unsigned char loc_testmsg[548];
  2977. struct CPRBX *cprbx_p;
  2978. index = devPtr->dev_self_x;
  2979. rv = 0;
  2980. do {
  2981. memcpy(loc_testmsg, MCL3_testmsg, sizeof(MCL3_testmsg));
  2982. length = sizeof(MCL3_testmsg) - 0x0C;
  2983. dv = send_to_AP(index, z90crypt.cdx, length, loc_testmsg);
  2984. if (dv) {
  2985. PDEBUG("dv returned = %d\n", dv);
  2986. if (dv == DEV_SEN_EXCEPTION) {
  2987. rv = SEN_FATAL_ERROR;
  2988. PRINTKC("exception in send to AP %d\n", index);
  2989. break;
  2990. }
  2991. PDEBUG("return value from send_to_AP: %d\n", rv);
  2992. switch (dv) {
  2993. case DEV_GONE:
  2994. PDEBUG("dev %d not available\n", index);
  2995. rv = SEN_NOT_AVAIL;
  2996. break;
  2997. case DEV_ONLINE:
  2998. rv = 0;
  2999. break;
  3000. case DEV_EMPTY:
  3001. rv = SEN_NOT_AVAIL;
  3002. break;
  3003. case DEV_NO_WORK:
  3004. rv = SEN_FATAL_ERROR;
  3005. break;
  3006. case DEV_BAD_MESSAGE:
  3007. rv = SEN_USER_ERROR;
  3008. break;
  3009. case DEV_QUEUE_FULL:
  3010. rv = SEN_QUEUE_FULL;
  3011. break;
  3012. default:
  3013. PRINTK("unknown dv=%d for dev %d\n", dv, index);
  3014. rv = SEN_NOT_AVAIL;
  3015. break;
  3016. }
  3017. }
  3018. if (rv)
  3019. break;
  3020. for (i = 0; i < 6; i++) {
  3021. mdelay(300);
  3022. dv = receive_from_AP(index, z90crypt.cdx,
  3023. devPtr->dev_resp_l,
  3024. devPtr->dev_resp_p, psmid);
  3025. PDEBUG("dv returned by DQ = %d\n", dv);
  3026. if (dv == DEV_REC_EXCEPTION) {
  3027. rv = REC_FATAL_ERROR;
  3028. PRINTKC("exception in dequeue %d\n",
  3029. index);
  3030. break;
  3031. }
  3032. switch (dv) {
  3033. case DEV_ONLINE:
  3034. rv = 0;
  3035. break;
  3036. case DEV_EMPTY:
  3037. rv = REC_EMPTY;
  3038. break;
  3039. case DEV_NO_WORK:
  3040. rv = REC_NO_WORK;
  3041. break;
  3042. case DEV_BAD_MESSAGE:
  3043. case DEV_GONE:
  3044. default:
  3045. rv = REC_NO_RESPONSE;
  3046. break;
  3047. }
  3048. if ((rv != 0) && (rv != REC_NO_WORK))
  3049. break;
  3050. if (rv == 0)
  3051. break;
  3052. }
  3053. if (rv)
  3054. break;
  3055. cprbx_p = (struct CPRBX *) (devPtr->dev_resp_p + 48);
  3056. if ((cprbx_p->ccp_rtcode == 8) && (cprbx_p->ccp_rscode == 33)) {
  3057. devPtr->dev_type = PCIXCC_MCL2;
  3058. PDEBUG("device %d is MCL2\n", index);
  3059. } else {
  3060. devPtr->dev_type = PCIXCC_MCL3;
  3061. PDEBUG("device %d is MCL3\n", index);
  3062. }
  3063. } while (0);
  3064. /* In a general error case, the card is not marked online */
  3065. return rv;
  3066. }
  3067. module_init(z90crypt_init_module);
  3068. module_exit(z90crypt_cleanup_module);