z90main.c 90 KB

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