z90main.c 90 KB

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