z90main.c 89 KB

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