ide.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414
  1. /*
  2. * QEMU IDE disk and CD/DVD-ROM Emulator
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. * Copyright (c) 2006 Openedhand Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw.h"
  26. #include "pc.h"
  27. #include "pci.h"
  28. #include "scsi-disk.h"
  29. #include "pcmcia.h"
  30. #include "block.h"
  31. #include "block_int.h"
  32. #include "qemu-timer.h"
  33. #include "sysemu.h"
  34. #include "ppc_mac.h"
  35. #include "mac_dbdma.h"
  36. #include "sh.h"
  37. #include "dma.h"
  38. /* debug IDE devices */
  39. //#define DEBUG_IDE
  40. //#define DEBUG_IDE_ATAPI
  41. //#define DEBUG_AIO
  42. #define USE_DMA_CDROM
  43. /* Bits of HD_STATUS */
  44. #define ERR_STAT 0x01
  45. #define INDEX_STAT 0x02
  46. #define ECC_STAT 0x04 /* Corrected error */
  47. #define DRQ_STAT 0x08
  48. #define SEEK_STAT 0x10
  49. #define SRV_STAT 0x10
  50. #define WRERR_STAT 0x20
  51. #define READY_STAT 0x40
  52. #define BUSY_STAT 0x80
  53. /* Bits for HD_ERROR */
  54. #define MARK_ERR 0x01 /* Bad address mark */
  55. #define TRK0_ERR 0x02 /* couldn't find track 0 */
  56. #define ABRT_ERR 0x04 /* Command aborted */
  57. #define MCR_ERR 0x08 /* media change request */
  58. #define ID_ERR 0x10 /* ID field not found */
  59. #define MC_ERR 0x20 /* media changed */
  60. #define ECC_ERR 0x40 /* Uncorrectable ECC error */
  61. #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
  62. #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
  63. /* Bits of HD_NSECTOR */
  64. #define CD 0x01
  65. #define IO 0x02
  66. #define REL 0x04
  67. #define TAG_MASK 0xf8
  68. #define IDE_CMD_RESET 0x04
  69. #define IDE_CMD_DISABLE_IRQ 0x02
  70. /* ATA/ATAPI Commands pre T13 Spec */
  71. #define WIN_NOP 0x00
  72. /*
  73. * 0x01->0x02 Reserved
  74. */
  75. #define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */
  76. /*
  77. * 0x04->0x07 Reserved
  78. */
  79. #define WIN_SRST 0x08 /* ATAPI soft reset command */
  80. #define WIN_DEVICE_RESET 0x08
  81. /*
  82. * 0x09->0x0F Reserved
  83. */
  84. #define WIN_RECAL 0x10
  85. #define WIN_RESTORE WIN_RECAL
  86. /*
  87. * 0x10->0x1F Reserved
  88. */
  89. #define WIN_READ 0x20 /* 28-Bit */
  90. #define WIN_READ_ONCE 0x21 /* 28-Bit without retries */
  91. #define WIN_READ_LONG 0x22 /* 28-Bit */
  92. #define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */
  93. #define WIN_READ_EXT 0x24 /* 48-Bit */
  94. #define WIN_READDMA_EXT 0x25 /* 48-Bit */
  95. #define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */
  96. #define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */
  97. /*
  98. * 0x28
  99. */
  100. #define WIN_MULTREAD_EXT 0x29 /* 48-Bit */
  101. /*
  102. * 0x2A->0x2F Reserved
  103. */
  104. #define WIN_WRITE 0x30 /* 28-Bit */
  105. #define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */
  106. #define WIN_WRITE_LONG 0x32 /* 28-Bit */
  107. #define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */
  108. #define WIN_WRITE_EXT 0x34 /* 48-Bit */
  109. #define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */
  110. #define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */
  111. #define WIN_SET_MAX_EXT 0x37 /* 48-Bit */
  112. #define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */
  113. #define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */
  114. /*
  115. * 0x3A->0x3B Reserved
  116. */
  117. #define WIN_WRITE_VERIFY 0x3C /* 28-Bit */
  118. /*
  119. * 0x3D->0x3F Reserved
  120. */
  121. #define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */
  122. #define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */
  123. #define WIN_VERIFY_EXT 0x42 /* 48-Bit */
  124. /*
  125. * 0x43->0x4F Reserved
  126. */
  127. #define WIN_FORMAT 0x50
  128. /*
  129. * 0x51->0x5F Reserved
  130. */
  131. #define WIN_INIT 0x60
  132. /*
  133. * 0x61->0x5F Reserved
  134. */
  135. #define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */
  136. #define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */
  137. #define WIN_DIAGNOSE 0x90
  138. #define WIN_SPECIFY 0x91 /* set drive geometry translation */
  139. #define WIN_DOWNLOAD_MICROCODE 0x92
  140. #define WIN_STANDBYNOW2 0x94
  141. #define CFA_IDLEIMMEDIATE 0x95 /* force drive to become "ready" */
  142. #define WIN_STANDBY2 0x96
  143. #define WIN_SETIDLE2 0x97
  144. #define WIN_CHECKPOWERMODE2 0x98
  145. #define WIN_SLEEPNOW2 0x99
  146. /*
  147. * 0x9A VENDOR
  148. */
  149. #define WIN_PACKETCMD 0xA0 /* Send a packet command. */
  150. #define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */
  151. #define WIN_QUEUED_SERVICE 0xA2
  152. #define WIN_SMART 0xB0 /* self-monitoring and reporting */
  153. #define CFA_ACCESS_METADATA_STORAGE 0xB8
  154. #define CFA_ERASE_SECTORS 0xC0 /* microdrives implement as NOP */
  155. #define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/
  156. #define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */
  157. #define WIN_SETMULT 0xC6 /* enable/disable multiple mode */
  158. #define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */
  159. #define WIN_READDMA 0xC8 /* read sectors using DMA transfers */
  160. #define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */
  161. #define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */
  162. #define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */
  163. #define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */
  164. #define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */
  165. #define WIN_GETMEDIASTATUS 0xDA
  166. #define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */
  167. #define WIN_POSTBOOT 0xDC
  168. #define WIN_PREBOOT 0xDD
  169. #define WIN_DOORLOCK 0xDE /* lock door on removable drives */
  170. #define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */
  171. #define WIN_STANDBYNOW1 0xE0
  172. #define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */
  173. #define WIN_STANDBY 0xE2 /* Set device in Standby Mode */
  174. #define WIN_SETIDLE1 0xE3
  175. #define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */
  176. #define WIN_CHECKPOWERMODE1 0xE5
  177. #define WIN_SLEEPNOW1 0xE6
  178. #define WIN_FLUSH_CACHE 0xE7
  179. #define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */
  180. #define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */
  181. /* SET_FEATURES 0x22 or 0xDD */
  182. #define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */
  183. #define WIN_IDENTIFY 0xEC /* ask drive to identify itself */
  184. #define WIN_MEDIAEJECT 0xED
  185. #define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */
  186. #define WIN_SETFEATURES 0xEF /* set special drive features */
  187. #define EXABYTE_ENABLE_NEST 0xF0
  188. #define IBM_SENSE_CONDITION 0xF0 /* measure disk temperature */
  189. #define WIN_SECURITY_SET_PASS 0xF1
  190. #define WIN_SECURITY_UNLOCK 0xF2
  191. #define WIN_SECURITY_ERASE_PREPARE 0xF3
  192. #define WIN_SECURITY_ERASE_UNIT 0xF4
  193. #define WIN_SECURITY_FREEZE_LOCK 0xF5
  194. #define CFA_WEAR_LEVEL 0xF5 /* microdrives implement as NOP */
  195. #define WIN_SECURITY_DISABLE 0xF6
  196. #define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */
  197. #define WIN_SET_MAX 0xF9
  198. #define DISABLE_SEAGATE 0xFB
  199. /* set to 1 set disable mult support */
  200. #define MAX_MULT_SECTORS 16
  201. #define IDE_DMA_BUF_SECTORS 256
  202. #if (IDE_DMA_BUF_SECTORS < MAX_MULT_SECTORS)
  203. #error "IDE_DMA_BUF_SECTORS must be bigger or equal to MAX_MULT_SECTORS"
  204. #endif
  205. /* ATAPI defines */
  206. #define ATAPI_PACKET_SIZE 12
  207. /* The generic packet command opcodes for CD/DVD Logical Units,
  208. * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
  209. #define GPCMD_BLANK 0xa1
  210. #define GPCMD_CLOSE_TRACK 0x5b
  211. #define GPCMD_FLUSH_CACHE 0x35
  212. #define GPCMD_FORMAT_UNIT 0x04
  213. #define GPCMD_GET_CONFIGURATION 0x46
  214. #define GPCMD_GET_EVENT_STATUS_NOTIFICATION 0x4a
  215. #define GPCMD_GET_PERFORMANCE 0xac
  216. #define GPCMD_INQUIRY 0x12
  217. #define GPCMD_LOAD_UNLOAD 0xa6
  218. #define GPCMD_MECHANISM_STATUS 0xbd
  219. #define GPCMD_MODE_SELECT_10 0x55
  220. #define GPCMD_MODE_SENSE_10 0x5a
  221. #define GPCMD_PAUSE_RESUME 0x4b
  222. #define GPCMD_PLAY_AUDIO_10 0x45
  223. #define GPCMD_PLAY_AUDIO_MSF 0x47
  224. #define GPCMD_PLAY_AUDIO_TI 0x48
  225. #define GPCMD_PLAY_CD 0xbc
  226. #define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
  227. #define GPCMD_READ_10 0x28
  228. #define GPCMD_READ_12 0xa8
  229. #define GPCMD_READ_CDVD_CAPACITY 0x25
  230. #define GPCMD_READ_CD 0xbe
  231. #define GPCMD_READ_CD_MSF 0xb9
  232. #define GPCMD_READ_DISC_INFO 0x51
  233. #define GPCMD_READ_DVD_STRUCTURE 0xad
  234. #define GPCMD_READ_FORMAT_CAPACITIES 0x23
  235. #define GPCMD_READ_HEADER 0x44
  236. #define GPCMD_READ_TRACK_RZONE_INFO 0x52
  237. #define GPCMD_READ_SUBCHANNEL 0x42
  238. #define GPCMD_READ_TOC_PMA_ATIP 0x43
  239. #define GPCMD_REPAIR_RZONE_TRACK 0x58
  240. #define GPCMD_REPORT_KEY 0xa4
  241. #define GPCMD_REQUEST_SENSE 0x03
  242. #define GPCMD_RESERVE_RZONE_TRACK 0x53
  243. #define GPCMD_SCAN 0xba
  244. #define GPCMD_SEEK 0x2b
  245. #define GPCMD_SEND_DVD_STRUCTURE 0xad
  246. #define GPCMD_SEND_EVENT 0xa2
  247. #define GPCMD_SEND_KEY 0xa3
  248. #define GPCMD_SEND_OPC 0x54
  249. #define GPCMD_SET_READ_AHEAD 0xa7
  250. #define GPCMD_SET_STREAMING 0xb6
  251. #define GPCMD_START_STOP_UNIT 0x1b
  252. #define GPCMD_STOP_PLAY_SCAN 0x4e
  253. #define GPCMD_TEST_UNIT_READY 0x00
  254. #define GPCMD_VERIFY_10 0x2f
  255. #define GPCMD_WRITE_10 0x2a
  256. #define GPCMD_WRITE_AND_VERIFY_10 0x2e
  257. /* This is listed as optional in ATAPI 2.6, but is (curiously)
  258. * missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji
  259. * Table 377 as an MMC command for SCSi devices though... Most ATAPI
  260. * drives support it. */
  261. #define GPCMD_SET_SPEED 0xbb
  262. /* This seems to be a SCSI specific CD-ROM opcode
  263. * to play data at track/index */
  264. #define GPCMD_PLAYAUDIO_TI 0x48
  265. /*
  266. * From MS Media Status Notification Support Specification. For
  267. * older drives only.
  268. */
  269. #define GPCMD_GET_MEDIA_STATUS 0xda
  270. #define GPCMD_MODE_SENSE_6 0x1a
  271. /* Mode page codes for mode sense/set */
  272. #define GPMODE_R_W_ERROR_PAGE 0x01
  273. #define GPMODE_WRITE_PARMS_PAGE 0x05
  274. #define GPMODE_AUDIO_CTL_PAGE 0x0e
  275. #define GPMODE_POWER_PAGE 0x1a
  276. #define GPMODE_FAULT_FAIL_PAGE 0x1c
  277. #define GPMODE_TO_PROTECT_PAGE 0x1d
  278. #define GPMODE_CAPABILITIES_PAGE 0x2a
  279. #define GPMODE_ALL_PAGES 0x3f
  280. /* Not in Mt. Fuji, but in ATAPI 2.6 -- depricated now in favor
  281. * of MODE_SENSE_POWER_PAGE */
  282. #define GPMODE_CDROM_PAGE 0x0d
  283. /*
  284. * Based on values from <linux/cdrom.h> but extending CD_MINS
  285. * to the maximum common size allowed by the Orange's Book ATIP
  286. *
  287. * 90 and 99 min CDs are also available but using them as the
  288. * upper limit reduces the effectiveness of the heuristic to
  289. * detect DVDs burned to less than 25% of their maximum capacity
  290. */
  291. /* Some generally useful CD-ROM information */
  292. #define CD_MINS 80 /* max. minutes per CD */
  293. #define CD_SECS 60 /* seconds per minute */
  294. #define CD_FRAMES 75 /* frames per second */
  295. #define CD_FRAMESIZE 2048 /* bytes per frame, "cooked" mode */
  296. #define CD_MAX_BYTES (CD_MINS * CD_SECS * CD_FRAMES * CD_FRAMESIZE)
  297. #define CD_MAX_SECTORS (CD_MAX_BYTES / 512)
  298. /*
  299. * The MMC values are not IDE specific and might need to be moved
  300. * to a common header if they are also needed for the SCSI emulation
  301. */
  302. /* Profile list from MMC-6 revision 1 table 91 */
  303. #define MMC_PROFILE_NONE 0x0000
  304. #define MMC_PROFILE_CD_ROM 0x0008
  305. #define MMC_PROFILE_CD_R 0x0009
  306. #define MMC_PROFILE_CD_RW 0x000A
  307. #define MMC_PROFILE_DVD_ROM 0x0010
  308. #define MMC_PROFILE_DVD_R_SR 0x0011
  309. #define MMC_PROFILE_DVD_RAM 0x0012
  310. #define MMC_PROFILE_DVD_RW_RO 0x0013
  311. #define MMC_PROFILE_DVD_RW_SR 0x0014
  312. #define MMC_PROFILE_DVD_R_DL_SR 0x0015
  313. #define MMC_PROFILE_DVD_R_DL_JR 0x0016
  314. #define MMC_PROFILE_DVD_RW_DL 0x0017
  315. #define MMC_PROFILE_DVD_DDR 0x0018
  316. #define MMC_PROFILE_DVD_PLUS_RW 0x001A
  317. #define MMC_PROFILE_DVD_PLUS_R 0x001B
  318. #define MMC_PROFILE_DVD_PLUS_RW_DL 0x002A
  319. #define MMC_PROFILE_DVD_PLUS_R_DL 0x002B
  320. #define MMC_PROFILE_BD_ROM 0x0040
  321. #define MMC_PROFILE_BD_R_SRM 0x0041
  322. #define MMC_PROFILE_BD_R_RRM 0x0042
  323. #define MMC_PROFILE_BD_RE 0x0043
  324. #define MMC_PROFILE_HDDVD_ROM 0x0050
  325. #define MMC_PROFILE_HDDVD_R 0x0051
  326. #define MMC_PROFILE_HDDVD_RAM 0x0052
  327. #define MMC_PROFILE_HDDVD_RW 0x0053
  328. #define MMC_PROFILE_HDDVD_R_DL 0x0058
  329. #define MMC_PROFILE_HDDVD_RW_DL 0x005A
  330. #define MMC_PROFILE_INVALID 0xFFFF
  331. #define ATAPI_INT_REASON_CD 0x01 /* 0 = data transfer */
  332. #define ATAPI_INT_REASON_IO 0x02 /* 1 = transfer to the host */
  333. #define ATAPI_INT_REASON_REL 0x04
  334. #define ATAPI_INT_REASON_TAG 0xf8
  335. /* same constants as bochs */
  336. #define ASC_ILLEGAL_OPCODE 0x20
  337. #define ASC_LOGICAL_BLOCK_OOR 0x21
  338. #define ASC_INV_FIELD_IN_CMD_PACKET 0x24
  339. #define ASC_MEDIUM_MAY_HAVE_CHANGED 0x28
  340. #define ASC_INCOMPATIBLE_FORMAT 0x30
  341. #define ASC_MEDIUM_NOT_PRESENT 0x3a
  342. #define ASC_SAVING_PARAMETERS_NOT_SUPPORTED 0x39
  343. #define ASC_MEDIA_REMOVAL_PREVENTED 0x53
  344. #define CFA_NO_ERROR 0x00
  345. #define CFA_MISC_ERROR 0x09
  346. #define CFA_INVALID_COMMAND 0x20
  347. #define CFA_INVALID_ADDRESS 0x21
  348. #define CFA_ADDRESS_OVERFLOW 0x2f
  349. #define SENSE_NONE 0
  350. #define SENSE_NOT_READY 2
  351. #define SENSE_ILLEGAL_REQUEST 5
  352. #define SENSE_UNIT_ATTENTION 6
  353. struct IDEState;
  354. typedef void EndTransferFunc(struct IDEState *);
  355. /* NOTE: IDEState represents in fact one drive */
  356. typedef struct IDEState {
  357. /* ide config */
  358. int is_cdrom;
  359. int is_cf;
  360. int cylinders, heads, sectors;
  361. int64_t nb_sectors;
  362. int mult_sectors;
  363. int identify_set;
  364. uint16_t identify_data[256];
  365. qemu_irq irq;
  366. PCIDevice *pci_dev;
  367. struct BMDMAState *bmdma;
  368. int drive_serial;
  369. char drive_serial_str[21];
  370. /* ide regs */
  371. uint8_t feature;
  372. uint8_t error;
  373. uint32_t nsector;
  374. uint8_t sector;
  375. uint8_t lcyl;
  376. uint8_t hcyl;
  377. /* other part of tf for lba48 support */
  378. uint8_t hob_feature;
  379. uint8_t hob_nsector;
  380. uint8_t hob_sector;
  381. uint8_t hob_lcyl;
  382. uint8_t hob_hcyl;
  383. uint8_t select;
  384. uint8_t status;
  385. /* 0x3f6 command, only meaningful for drive 0 */
  386. uint8_t cmd;
  387. /* set for lba48 access */
  388. uint8_t lba48;
  389. /* depends on bit 4 in select, only meaningful for drive 0 */
  390. struct IDEState *cur_drive;
  391. BlockDriverState *bs;
  392. /* ATAPI specific */
  393. uint8_t sense_key;
  394. uint8_t asc;
  395. uint8_t cdrom_changed;
  396. int packet_transfer_size;
  397. int elementary_transfer_size;
  398. int io_buffer_index;
  399. int lba;
  400. int cd_sector_size;
  401. int atapi_dma; /* true if dma is requested for the packet cmd */
  402. /* ATA DMA state */
  403. int io_buffer_size;
  404. QEMUSGList sg;
  405. /* PIO transfer handling */
  406. int req_nb_sectors; /* number of sectors per interrupt */
  407. EndTransferFunc *end_transfer_func;
  408. uint8_t *data_ptr;
  409. uint8_t *data_end;
  410. uint8_t *io_buffer;
  411. QEMUTimer *sector_write_timer; /* only used for win2k install hack */
  412. uint32_t irq_count; /* counts IRQs when using win2k install hack */
  413. /* CF-ATA extended error */
  414. uint8_t ext_error;
  415. /* CF-ATA metadata storage */
  416. uint32_t mdata_size;
  417. uint8_t *mdata_storage;
  418. int media_changed;
  419. /* for pmac */
  420. int is_read;
  421. } IDEState;
  422. /* XXX: DVDs that could fit on a CD will be reported as a CD */
  423. static inline int media_present(IDEState *s)
  424. {
  425. return (s->nb_sectors > 0);
  426. }
  427. static inline int media_is_dvd(IDEState *s)
  428. {
  429. return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
  430. }
  431. static inline int media_is_cd(IDEState *s)
  432. {
  433. return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
  434. }
  435. #define BM_STATUS_DMAING 0x01
  436. #define BM_STATUS_ERROR 0x02
  437. #define BM_STATUS_INT 0x04
  438. #define BM_STATUS_DMA_RETRY 0x08
  439. #define BM_STATUS_PIO_RETRY 0x10
  440. #define BM_CMD_START 0x01
  441. #define BM_CMD_READ 0x08
  442. #define IDE_TYPE_PIIX3 0
  443. #define IDE_TYPE_CMD646 1
  444. #define IDE_TYPE_PIIX4 2
  445. /* CMD646 specific */
  446. #define MRDMODE 0x71
  447. #define MRDMODE_INTR_CH0 0x04
  448. #define MRDMODE_INTR_CH1 0x08
  449. #define MRDMODE_BLK_CH0 0x10
  450. #define MRDMODE_BLK_CH1 0x20
  451. #define UDIDETCR0 0x73
  452. #define UDIDETCR1 0x7B
  453. typedef struct BMDMAState {
  454. uint8_t cmd;
  455. uint8_t status;
  456. uint32_t addr;
  457. struct PCIIDEState *pci_dev;
  458. /* current transfer state */
  459. uint32_t cur_addr;
  460. uint32_t cur_prd_last;
  461. uint32_t cur_prd_addr;
  462. uint32_t cur_prd_len;
  463. IDEState *ide_if;
  464. BlockDriverCompletionFunc *dma_cb;
  465. BlockDriverAIOCB *aiocb;
  466. int64_t sector_num;
  467. uint32_t nsector;
  468. } BMDMAState;
  469. typedef struct PCIIDEState {
  470. PCIDevice dev;
  471. IDEState ide_if[4];
  472. BMDMAState bmdma[2];
  473. int type; /* see IDE_TYPE_xxx */
  474. } PCIIDEState;
  475. static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb);
  476. static void ide_dma_restart(IDEState *s);
  477. static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
  478. static void padstr(char *str, const char *src, int len)
  479. {
  480. int i, v;
  481. for(i = 0; i < len; i++) {
  482. if (*src)
  483. v = *src++;
  484. else
  485. v = ' ';
  486. str[i^1] = v;
  487. }
  488. }
  489. static void padstr8(uint8_t *buf, int buf_size, const char *src)
  490. {
  491. int i;
  492. for(i = 0; i < buf_size; i++) {
  493. if (*src)
  494. buf[i] = *src++;
  495. else
  496. buf[i] = ' ';
  497. }
  498. }
  499. static void put_le16(uint16_t *p, unsigned int v)
  500. {
  501. *p = cpu_to_le16(v);
  502. }
  503. static void ide_identify(IDEState *s)
  504. {
  505. uint16_t *p;
  506. unsigned int oldsize;
  507. if (s->identify_set) {
  508. memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
  509. return;
  510. }
  511. memset(s->io_buffer, 0, 512);
  512. p = (uint16_t *)s->io_buffer;
  513. put_le16(p + 0, 0x0040);
  514. put_le16(p + 1, s->cylinders);
  515. put_le16(p + 3, s->heads);
  516. put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
  517. put_le16(p + 5, 512); /* XXX: retired, remove ? */
  518. put_le16(p + 6, s->sectors);
  519. padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
  520. put_le16(p + 20, 3); /* XXX: retired, remove ? */
  521. put_le16(p + 21, 512); /* cache size in sectors */
  522. put_le16(p + 22, 4); /* ecc bytes */
  523. padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware version */
  524. padstr((char *)(p + 27), "QEMU HARDDISK", 40); /* model */
  525. #if MAX_MULT_SECTORS > 1
  526. put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
  527. #endif
  528. put_le16(p + 48, 1); /* dword I/O */
  529. put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
  530. put_le16(p + 51, 0x200); /* PIO transfer cycle */
  531. put_le16(p + 52, 0x200); /* DMA transfer cycle */
  532. put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
  533. put_le16(p + 54, s->cylinders);
  534. put_le16(p + 55, s->heads);
  535. put_le16(p + 56, s->sectors);
  536. oldsize = s->cylinders * s->heads * s->sectors;
  537. put_le16(p + 57, oldsize);
  538. put_le16(p + 58, oldsize >> 16);
  539. if (s->mult_sectors)
  540. put_le16(p + 59, 0x100 | s->mult_sectors);
  541. put_le16(p + 60, s->nb_sectors);
  542. put_le16(p + 61, s->nb_sectors >> 16);
  543. put_le16(p + 62, 0x07); /* single word dma0-2 supported */
  544. put_le16(p + 63, 0x07); /* mdma0-2 supported */
  545. put_le16(p + 65, 120);
  546. put_le16(p + 66, 120);
  547. put_le16(p + 67, 120);
  548. put_le16(p + 68, 120);
  549. put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
  550. put_le16(p + 81, 0x16); /* conforms to ata5 */
  551. put_le16(p + 82, (1 << 14));
  552. /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
  553. put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
  554. put_le16(p + 84, (1 << 14));
  555. put_le16(p + 85, (1 << 14));
  556. /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
  557. put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
  558. put_le16(p + 87, (1 << 14));
  559. put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
  560. put_le16(p + 93, 1 | (1 << 14) | 0x2000);
  561. put_le16(p + 100, s->nb_sectors);
  562. put_le16(p + 101, s->nb_sectors >> 16);
  563. put_le16(p + 102, s->nb_sectors >> 32);
  564. put_le16(p + 103, s->nb_sectors >> 48);
  565. memcpy(s->identify_data, p, sizeof(s->identify_data));
  566. s->identify_set = 1;
  567. }
  568. static void ide_atapi_identify(IDEState *s)
  569. {
  570. uint16_t *p;
  571. if (s->identify_set) {
  572. memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
  573. return;
  574. }
  575. memset(s->io_buffer, 0, 512);
  576. p = (uint16_t *)s->io_buffer;
  577. /* Removable CDROM, 50us response, 12 byte packets */
  578. put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
  579. padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
  580. put_le16(p + 20, 3); /* buffer type */
  581. put_le16(p + 21, 512); /* cache size in sectors */
  582. put_le16(p + 22, 4); /* ecc bytes */
  583. padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware version */
  584. padstr((char *)(p + 27), "QEMU DVD-ROM", 40); /* model */
  585. put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
  586. #ifdef USE_DMA_CDROM
  587. put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
  588. put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
  589. put_le16(p + 62, 7); /* single word dma0-2 supported */
  590. put_le16(p + 63, 7); /* mdma0-2 supported */
  591. put_le16(p + 64, 0x3f); /* PIO modes supported */
  592. #else
  593. put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
  594. put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
  595. put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
  596. put_le16(p + 64, 1); /* PIO modes */
  597. #endif
  598. put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
  599. put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
  600. put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
  601. put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
  602. put_le16(p + 71, 30); /* in ns */
  603. put_le16(p + 72, 30); /* in ns */
  604. put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
  605. #ifdef USE_DMA_CDROM
  606. put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
  607. #endif
  608. memcpy(s->identify_data, p, sizeof(s->identify_data));
  609. s->identify_set = 1;
  610. }
  611. static void ide_cfata_identify(IDEState *s)
  612. {
  613. uint16_t *p;
  614. uint32_t cur_sec;
  615. p = (uint16_t *) s->identify_data;
  616. if (s->identify_set)
  617. goto fill_buffer;
  618. memset(p, 0, sizeof(s->identify_data));
  619. cur_sec = s->cylinders * s->heads * s->sectors;
  620. put_le16(p + 0, 0x848a); /* CF Storage Card signature */
  621. put_le16(p + 1, s->cylinders); /* Default cylinders */
  622. put_le16(p + 3, s->heads); /* Default heads */
  623. put_le16(p + 6, s->sectors); /* Default sectors per track */
  624. put_le16(p + 7, s->nb_sectors >> 16); /* Sectors per card */
  625. put_le16(p + 8, s->nb_sectors); /* Sectors per card */
  626. padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
  627. put_le16(p + 22, 0x0004); /* ECC bytes */
  628. padstr((char *) (p + 23), QEMU_VERSION, 8); /* Firmware Revision */
  629. padstr((char *) (p + 27), "QEMU MICRODRIVE", 40);/* Model number */
  630. #if MAX_MULT_SECTORS > 1
  631. put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
  632. #else
  633. put_le16(p + 47, 0x0000);
  634. #endif
  635. put_le16(p + 49, 0x0f00); /* Capabilities */
  636. put_le16(p + 51, 0x0002); /* PIO cycle timing mode */
  637. put_le16(p + 52, 0x0001); /* DMA cycle timing mode */
  638. put_le16(p + 53, 0x0003); /* Translation params valid */
  639. put_le16(p + 54, s->cylinders); /* Current cylinders */
  640. put_le16(p + 55, s->heads); /* Current heads */
  641. put_le16(p + 56, s->sectors); /* Current sectors */
  642. put_le16(p + 57, cur_sec); /* Current capacity */
  643. put_le16(p + 58, cur_sec >> 16); /* Current capacity */
  644. if (s->mult_sectors) /* Multiple sector setting */
  645. put_le16(p + 59, 0x100 | s->mult_sectors);
  646. put_le16(p + 60, s->nb_sectors); /* Total LBA sectors */
  647. put_le16(p + 61, s->nb_sectors >> 16); /* Total LBA sectors */
  648. put_le16(p + 63, 0x0203); /* Multiword DMA capability */
  649. put_le16(p + 64, 0x0001); /* Flow Control PIO support */
  650. put_le16(p + 65, 0x0096); /* Min. Multiword DMA cycle */
  651. put_le16(p + 66, 0x0096); /* Rec. Multiword DMA cycle */
  652. put_le16(p + 68, 0x00b4); /* Min. PIO cycle time */
  653. put_le16(p + 82, 0x400c); /* Command Set supported */
  654. put_le16(p + 83, 0x7068); /* Command Set supported */
  655. put_le16(p + 84, 0x4000); /* Features supported */
  656. put_le16(p + 85, 0x000c); /* Command Set enabled */
  657. put_le16(p + 86, 0x7044); /* Command Set enabled */
  658. put_le16(p + 87, 0x4000); /* Features enabled */
  659. put_le16(p + 91, 0x4060); /* Current APM level */
  660. put_le16(p + 129, 0x0002); /* Current features option */
  661. put_le16(p + 130, 0x0005); /* Reassigned sectors */
  662. put_le16(p + 131, 0x0001); /* Initial power mode */
  663. put_le16(p + 132, 0x0000); /* User signature */
  664. put_le16(p + 160, 0x8100); /* Power requirement */
  665. put_le16(p + 161, 0x8001); /* CF command set */
  666. s->identify_set = 1;
  667. fill_buffer:
  668. memcpy(s->io_buffer, p, sizeof(s->identify_data));
  669. }
  670. static void ide_set_signature(IDEState *s)
  671. {
  672. s->select &= 0xf0; /* clear head */
  673. /* put signature */
  674. s->nsector = 1;
  675. s->sector = 1;
  676. if (s->is_cdrom) {
  677. s->lcyl = 0x14;
  678. s->hcyl = 0xeb;
  679. } else if (s->bs) {
  680. s->lcyl = 0;
  681. s->hcyl = 0;
  682. } else {
  683. s->lcyl = 0xff;
  684. s->hcyl = 0xff;
  685. }
  686. }
  687. static inline void ide_abort_command(IDEState *s)
  688. {
  689. s->status = READY_STAT | ERR_STAT;
  690. s->error = ABRT_ERR;
  691. }
  692. static inline void ide_dma_submit_check(IDEState *s,
  693. BlockDriverCompletionFunc *dma_cb, BMDMAState *bm)
  694. {
  695. if (bm->aiocb)
  696. return;
  697. dma_cb(bm, -1);
  698. }
  699. static inline void ide_set_irq(IDEState *s)
  700. {
  701. BMDMAState *bm = s->bmdma;
  702. if (!(s->cmd & IDE_CMD_DISABLE_IRQ)) {
  703. if (bm) {
  704. bm->status |= BM_STATUS_INT;
  705. }
  706. qemu_irq_raise(s->irq);
  707. }
  708. }
  709. /* prepare data transfer and tell what to do after */
  710. static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
  711. EndTransferFunc *end_transfer_func)
  712. {
  713. s->end_transfer_func = end_transfer_func;
  714. s->data_ptr = buf;
  715. s->data_end = buf + size;
  716. if (!(s->status & ERR_STAT))
  717. s->status |= DRQ_STAT;
  718. }
  719. static void ide_transfer_stop(IDEState *s)
  720. {
  721. s->end_transfer_func = ide_transfer_stop;
  722. s->data_ptr = s->io_buffer;
  723. s->data_end = s->io_buffer;
  724. s->status &= ~DRQ_STAT;
  725. }
  726. static int64_t ide_get_sector(IDEState *s)
  727. {
  728. int64_t sector_num;
  729. if (s->select & 0x40) {
  730. /* lba */
  731. if (!s->lba48) {
  732. sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
  733. (s->lcyl << 8) | s->sector;
  734. } else {
  735. sector_num = ((int64_t)s->hob_hcyl << 40) |
  736. ((int64_t) s->hob_lcyl << 32) |
  737. ((int64_t) s->hob_sector << 24) |
  738. ((int64_t) s->hcyl << 16) |
  739. ((int64_t) s->lcyl << 8) | s->sector;
  740. }
  741. } else {
  742. sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
  743. (s->select & 0x0f) * s->sectors + (s->sector - 1);
  744. }
  745. return sector_num;
  746. }
  747. static void ide_set_sector(IDEState *s, int64_t sector_num)
  748. {
  749. unsigned int cyl, r;
  750. if (s->select & 0x40) {
  751. if (!s->lba48) {
  752. s->select = (s->select & 0xf0) | (sector_num >> 24);
  753. s->hcyl = (sector_num >> 16);
  754. s->lcyl = (sector_num >> 8);
  755. s->sector = (sector_num);
  756. } else {
  757. s->sector = sector_num;
  758. s->lcyl = sector_num >> 8;
  759. s->hcyl = sector_num >> 16;
  760. s->hob_sector = sector_num >> 24;
  761. s->hob_lcyl = sector_num >> 32;
  762. s->hob_hcyl = sector_num >> 40;
  763. }
  764. } else {
  765. cyl = sector_num / (s->heads * s->sectors);
  766. r = sector_num % (s->heads * s->sectors);
  767. s->hcyl = cyl >> 8;
  768. s->lcyl = cyl;
  769. s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
  770. s->sector = (r % s->sectors) + 1;
  771. }
  772. }
  773. static void ide_rw_error(IDEState *s) {
  774. ide_abort_command(s);
  775. ide_set_irq(s);
  776. }
  777. static void ide_sector_read(IDEState *s)
  778. {
  779. int64_t sector_num;
  780. int ret, n;
  781. s->status = READY_STAT | SEEK_STAT;
  782. s->error = 0; /* not needed by IDE spec, but needed by Windows */
  783. sector_num = ide_get_sector(s);
  784. n = s->nsector;
  785. if (n == 0) {
  786. /* no more sector to read from disk */
  787. ide_transfer_stop(s);
  788. } else {
  789. #if defined(DEBUG_IDE)
  790. printf("read sector=%" PRId64 "\n", sector_num);
  791. #endif
  792. if (n > s->req_nb_sectors)
  793. n = s->req_nb_sectors;
  794. ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
  795. if (ret != 0) {
  796. ide_rw_error(s);
  797. return;
  798. }
  799. ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
  800. ide_set_irq(s);
  801. ide_set_sector(s, sector_num + n);
  802. s->nsector -= n;
  803. }
  804. }
  805. /* return 0 if buffer completed */
  806. static int dma_buf_prepare(BMDMAState *bm, int is_write)
  807. {
  808. IDEState *s = bm->ide_if;
  809. struct {
  810. uint32_t addr;
  811. uint32_t size;
  812. } prd;
  813. int l, len;
  814. qemu_sglist_init(&s->sg, s->nsector / (TARGET_PAGE_SIZE/512) + 1);
  815. s->io_buffer_size = 0;
  816. for(;;) {
  817. if (bm->cur_prd_len == 0) {
  818. /* end of table (with a fail safe of one page) */
  819. if (bm->cur_prd_last ||
  820. (bm->cur_addr - bm->addr) >= 4096)
  821. return s->io_buffer_size != 0;
  822. cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
  823. bm->cur_addr += 8;
  824. prd.addr = le32_to_cpu(prd.addr);
  825. prd.size = le32_to_cpu(prd.size);
  826. len = prd.size & 0xfffe;
  827. if (len == 0)
  828. len = 0x10000;
  829. bm->cur_prd_len = len;
  830. bm->cur_prd_addr = prd.addr;
  831. bm->cur_prd_last = (prd.size & 0x80000000);
  832. }
  833. l = bm->cur_prd_len;
  834. if (l > 0) {
  835. qemu_sglist_add(&s->sg, bm->cur_prd_addr, l);
  836. bm->cur_prd_addr += l;
  837. bm->cur_prd_len -= l;
  838. s->io_buffer_size += l;
  839. }
  840. }
  841. return 1;
  842. }
  843. static void dma_buf_commit(IDEState *s, int is_write)
  844. {
  845. qemu_sglist_destroy(&s->sg);
  846. }
  847. static void ide_dma_error(IDEState *s)
  848. {
  849. ide_transfer_stop(s);
  850. s->error = ABRT_ERR;
  851. s->status = READY_STAT | ERR_STAT;
  852. ide_set_irq(s);
  853. }
  854. static int ide_handle_write_error(IDEState *s, int error, int op)
  855. {
  856. BlockInterfaceErrorAction action = drive_get_onerror(s->bs);
  857. if (action == BLOCK_ERR_IGNORE)
  858. return 0;
  859. if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
  860. || action == BLOCK_ERR_STOP_ANY) {
  861. s->bmdma->ide_if = s;
  862. s->bmdma->status |= op;
  863. vm_stop(0);
  864. } else {
  865. if (op == BM_STATUS_DMA_RETRY) {
  866. dma_buf_commit(s, 0);
  867. ide_dma_error(s);
  868. } else {
  869. ide_rw_error(s);
  870. }
  871. }
  872. return 1;
  873. }
  874. /* return 0 if buffer completed */
  875. static int dma_buf_rw(BMDMAState *bm, int is_write)
  876. {
  877. IDEState *s = bm->ide_if;
  878. struct {
  879. uint32_t addr;
  880. uint32_t size;
  881. } prd;
  882. int l, len;
  883. for(;;) {
  884. l = s->io_buffer_size - s->io_buffer_index;
  885. if (l <= 0)
  886. break;
  887. if (bm->cur_prd_len == 0) {
  888. /* end of table (with a fail safe of one page) */
  889. if (bm->cur_prd_last ||
  890. (bm->cur_addr - bm->addr) >= 4096)
  891. return 0;
  892. cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
  893. bm->cur_addr += 8;
  894. prd.addr = le32_to_cpu(prd.addr);
  895. prd.size = le32_to_cpu(prd.size);
  896. len = prd.size & 0xfffe;
  897. if (len == 0)
  898. len = 0x10000;
  899. bm->cur_prd_len = len;
  900. bm->cur_prd_addr = prd.addr;
  901. bm->cur_prd_last = (prd.size & 0x80000000);
  902. }
  903. if (l > bm->cur_prd_len)
  904. l = bm->cur_prd_len;
  905. if (l > 0) {
  906. if (is_write) {
  907. cpu_physical_memory_write(bm->cur_prd_addr,
  908. s->io_buffer + s->io_buffer_index, l);
  909. } else {
  910. cpu_physical_memory_read(bm->cur_prd_addr,
  911. s->io_buffer + s->io_buffer_index, l);
  912. }
  913. bm->cur_prd_addr += l;
  914. bm->cur_prd_len -= l;
  915. s->io_buffer_index += l;
  916. }
  917. }
  918. return 1;
  919. }
  920. static void ide_read_dma_cb(void *opaque, int ret)
  921. {
  922. BMDMAState *bm = opaque;
  923. IDEState *s = bm->ide_if;
  924. int n;
  925. int64_t sector_num;
  926. if (ret < 0) {
  927. dma_buf_commit(s, 1);
  928. ide_dma_error(s);
  929. return;
  930. }
  931. n = s->io_buffer_size >> 9;
  932. sector_num = ide_get_sector(s);
  933. if (n > 0) {
  934. dma_buf_commit(s, 1);
  935. sector_num += n;
  936. ide_set_sector(s, sector_num);
  937. s->nsector -= n;
  938. }
  939. /* end of transfer ? */
  940. if (s->nsector == 0) {
  941. s->status = READY_STAT | SEEK_STAT;
  942. ide_set_irq(s);
  943. eot:
  944. bm->status &= ~BM_STATUS_DMAING;
  945. bm->status |= BM_STATUS_INT;
  946. bm->dma_cb = NULL;
  947. bm->ide_if = NULL;
  948. bm->aiocb = NULL;
  949. return;
  950. }
  951. /* launch next transfer */
  952. n = s->nsector;
  953. s->io_buffer_index = 0;
  954. s->io_buffer_size = n * 512;
  955. if (dma_buf_prepare(bm, 1) == 0)
  956. goto eot;
  957. #ifdef DEBUG_AIO
  958. printf("aio_read: sector_num=%" PRId64 " n=%d\n", sector_num, n);
  959. #endif
  960. bm->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num, ide_read_dma_cb, bm);
  961. ide_dma_submit_check(s, ide_read_dma_cb, bm);
  962. }
  963. static void ide_sector_read_dma(IDEState *s)
  964. {
  965. s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
  966. s->io_buffer_index = 0;
  967. s->io_buffer_size = 0;
  968. s->is_read = 1;
  969. ide_dma_start(s, ide_read_dma_cb);
  970. }
  971. static void ide_sector_write_timer_cb(void *opaque)
  972. {
  973. IDEState *s = opaque;
  974. ide_set_irq(s);
  975. }
  976. static void ide_sector_write(IDEState *s)
  977. {
  978. int64_t sector_num;
  979. int ret, n, n1;
  980. s->status = READY_STAT | SEEK_STAT;
  981. sector_num = ide_get_sector(s);
  982. #if defined(DEBUG_IDE)
  983. printf("write sector=%" PRId64 "\n", sector_num);
  984. #endif
  985. n = s->nsector;
  986. if (n > s->req_nb_sectors)
  987. n = s->req_nb_sectors;
  988. ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
  989. if (ret != 0) {
  990. if (ide_handle_write_error(s, -ret, BM_STATUS_PIO_RETRY))
  991. return;
  992. }
  993. s->nsector -= n;
  994. if (s->nsector == 0) {
  995. /* no more sectors to write */
  996. ide_transfer_stop(s);
  997. } else {
  998. n1 = s->nsector;
  999. if (n1 > s->req_nb_sectors)
  1000. n1 = s->req_nb_sectors;
  1001. ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
  1002. }
  1003. ide_set_sector(s, sector_num + n);
  1004. #ifdef TARGET_I386
  1005. if (win2k_install_hack && ((++s->irq_count % 16) == 0)) {
  1006. /* It seems there is a bug in the Windows 2000 installer HDD
  1007. IDE driver which fills the disk with empty logs when the
  1008. IDE write IRQ comes too early. This hack tries to correct
  1009. that at the expense of slower write performances. Use this
  1010. option _only_ to install Windows 2000. You must disable it
  1011. for normal use. */
  1012. qemu_mod_timer(s->sector_write_timer,
  1013. qemu_get_clock(vm_clock) + (ticks_per_sec / 1000));
  1014. } else
  1015. #endif
  1016. {
  1017. ide_set_irq(s);
  1018. }
  1019. }
  1020. static void ide_dma_restart_cb(void *opaque, int running, int reason)
  1021. {
  1022. BMDMAState *bm = opaque;
  1023. if (!running)
  1024. return;
  1025. if (bm->status & BM_STATUS_DMA_RETRY) {
  1026. bm->status &= ~BM_STATUS_DMA_RETRY;
  1027. ide_dma_restart(bm->ide_if);
  1028. } else if (bm->status & BM_STATUS_PIO_RETRY) {
  1029. bm->status &= ~BM_STATUS_PIO_RETRY;
  1030. ide_sector_write(bm->ide_if);
  1031. }
  1032. }
  1033. static void ide_write_dma_cb(void *opaque, int ret)
  1034. {
  1035. BMDMAState *bm = opaque;
  1036. IDEState *s = bm->ide_if;
  1037. int n;
  1038. int64_t sector_num;
  1039. if (ret < 0) {
  1040. if (ide_handle_write_error(s, -ret, BM_STATUS_DMA_RETRY))
  1041. return;
  1042. }
  1043. n = s->io_buffer_size >> 9;
  1044. sector_num = ide_get_sector(s);
  1045. if (n > 0) {
  1046. dma_buf_commit(s, 0);
  1047. sector_num += n;
  1048. ide_set_sector(s, sector_num);
  1049. s->nsector -= n;
  1050. }
  1051. /* end of transfer ? */
  1052. if (s->nsector == 0) {
  1053. s->status = READY_STAT | SEEK_STAT;
  1054. ide_set_irq(s);
  1055. eot:
  1056. bm->status &= ~BM_STATUS_DMAING;
  1057. bm->status |= BM_STATUS_INT;
  1058. bm->dma_cb = NULL;
  1059. bm->ide_if = NULL;
  1060. bm->aiocb = NULL;
  1061. return;
  1062. }
  1063. n = s->nsector;
  1064. s->io_buffer_size = n * 512;
  1065. /* launch next transfer */
  1066. if (dma_buf_prepare(bm, 0) == 0)
  1067. goto eot;
  1068. #ifdef DEBUG_AIO
  1069. printf("aio_write: sector_num=%" PRId64 " n=%d\n", sector_num, n);
  1070. #endif
  1071. bm->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num, ide_write_dma_cb, bm);
  1072. ide_dma_submit_check(s, ide_write_dma_cb, bm);
  1073. }
  1074. static void ide_sector_write_dma(IDEState *s)
  1075. {
  1076. s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
  1077. s->io_buffer_index = 0;
  1078. s->io_buffer_size = 0;
  1079. s->is_read = 0;
  1080. ide_dma_start(s, ide_write_dma_cb);
  1081. }
  1082. static void ide_atapi_cmd_ok(IDEState *s)
  1083. {
  1084. s->error = 0;
  1085. s->status = READY_STAT | SEEK_STAT;
  1086. s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
  1087. ide_set_irq(s);
  1088. }
  1089. static void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
  1090. {
  1091. #ifdef DEBUG_IDE_ATAPI
  1092. printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
  1093. #endif
  1094. s->error = sense_key << 4;
  1095. s->status = READY_STAT | ERR_STAT;
  1096. s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
  1097. s->sense_key = sense_key;
  1098. s->asc = asc;
  1099. ide_set_irq(s);
  1100. }
  1101. static void ide_atapi_cmd_check_status(IDEState *s)
  1102. {
  1103. #ifdef DEBUG_IDE_ATAPI
  1104. printf("atapi_cmd_check_status\n");
  1105. #endif
  1106. s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
  1107. s->status = ERR_STAT;
  1108. s->nsector = 0;
  1109. ide_set_irq(s);
  1110. }
  1111. static inline void cpu_to_ube16(uint8_t *buf, int val)
  1112. {
  1113. buf[0] = val >> 8;
  1114. buf[1] = val;
  1115. }
  1116. static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
  1117. {
  1118. buf[0] = val >> 24;
  1119. buf[1] = val >> 16;
  1120. buf[2] = val >> 8;
  1121. buf[3] = val;
  1122. }
  1123. static inline int ube16_to_cpu(const uint8_t *buf)
  1124. {
  1125. return (buf[0] << 8) | buf[1];
  1126. }
  1127. static inline int ube32_to_cpu(const uint8_t *buf)
  1128. {
  1129. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1130. }
  1131. static void lba_to_msf(uint8_t *buf, int lba)
  1132. {
  1133. lba += 150;
  1134. buf[0] = (lba / 75) / 60;
  1135. buf[1] = (lba / 75) % 60;
  1136. buf[2] = lba % 75;
  1137. }
  1138. static void cd_data_to_raw(uint8_t *buf, int lba)
  1139. {
  1140. /* sync bytes */
  1141. buf[0] = 0x00;
  1142. memset(buf + 1, 0xff, 10);
  1143. buf[11] = 0x00;
  1144. buf += 12;
  1145. /* MSF */
  1146. lba_to_msf(buf, lba);
  1147. buf[3] = 0x01; /* mode 1 data */
  1148. buf += 4;
  1149. /* data */
  1150. buf += 2048;
  1151. /* XXX: ECC not computed */
  1152. memset(buf, 0, 288);
  1153. }
  1154. static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
  1155. int sector_size)
  1156. {
  1157. int ret;
  1158. switch(sector_size) {
  1159. case 2048:
  1160. ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
  1161. break;
  1162. case 2352:
  1163. ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
  1164. if (ret < 0)
  1165. return ret;
  1166. cd_data_to_raw(buf, lba);
  1167. break;
  1168. default:
  1169. ret = -EIO;
  1170. break;
  1171. }
  1172. return ret;
  1173. }
  1174. static void ide_atapi_io_error(IDEState *s, int ret)
  1175. {
  1176. /* XXX: handle more errors */
  1177. if (ret == -ENOMEDIUM) {
  1178. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1179. ASC_MEDIUM_NOT_PRESENT);
  1180. } else {
  1181. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1182. ASC_LOGICAL_BLOCK_OOR);
  1183. }
  1184. }
  1185. /* The whole ATAPI transfer logic is handled in this function */
  1186. static void ide_atapi_cmd_reply_end(IDEState *s)
  1187. {
  1188. int byte_count_limit, size, ret;
  1189. #ifdef DEBUG_IDE_ATAPI
  1190. printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
  1191. s->packet_transfer_size,
  1192. s->elementary_transfer_size,
  1193. s->io_buffer_index);
  1194. #endif
  1195. if (s->packet_transfer_size <= 0) {
  1196. /* end of transfer */
  1197. ide_transfer_stop(s);
  1198. s->status = READY_STAT | SEEK_STAT;
  1199. s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
  1200. ide_set_irq(s);
  1201. #ifdef DEBUG_IDE_ATAPI
  1202. printf("status=0x%x\n", s->status);
  1203. #endif
  1204. } else {
  1205. /* see if a new sector must be read */
  1206. if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
  1207. ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
  1208. if (ret < 0) {
  1209. ide_transfer_stop(s);
  1210. ide_atapi_io_error(s, ret);
  1211. return;
  1212. }
  1213. s->lba++;
  1214. s->io_buffer_index = 0;
  1215. }
  1216. if (s->elementary_transfer_size > 0) {
  1217. /* there are some data left to transmit in this elementary
  1218. transfer */
  1219. size = s->cd_sector_size - s->io_buffer_index;
  1220. if (size > s->elementary_transfer_size)
  1221. size = s->elementary_transfer_size;
  1222. ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
  1223. size, ide_atapi_cmd_reply_end);
  1224. s->packet_transfer_size -= size;
  1225. s->elementary_transfer_size -= size;
  1226. s->io_buffer_index += size;
  1227. } else {
  1228. /* a new transfer is needed */
  1229. s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
  1230. byte_count_limit = s->lcyl | (s->hcyl << 8);
  1231. #ifdef DEBUG_IDE_ATAPI
  1232. printf("byte_count_limit=%d\n", byte_count_limit);
  1233. #endif
  1234. if (byte_count_limit == 0xffff)
  1235. byte_count_limit--;
  1236. size = s->packet_transfer_size;
  1237. if (size > byte_count_limit) {
  1238. /* byte count limit must be even if this case */
  1239. if (byte_count_limit & 1)
  1240. byte_count_limit--;
  1241. size = byte_count_limit;
  1242. }
  1243. s->lcyl = size;
  1244. s->hcyl = size >> 8;
  1245. s->elementary_transfer_size = size;
  1246. /* we cannot transmit more than one sector at a time */
  1247. if (s->lba != -1) {
  1248. if (size > (s->cd_sector_size - s->io_buffer_index))
  1249. size = (s->cd_sector_size - s->io_buffer_index);
  1250. }
  1251. ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
  1252. size, ide_atapi_cmd_reply_end);
  1253. s->packet_transfer_size -= size;
  1254. s->elementary_transfer_size -= size;
  1255. s->io_buffer_index += size;
  1256. ide_set_irq(s);
  1257. #ifdef DEBUG_IDE_ATAPI
  1258. printf("status=0x%x\n", s->status);
  1259. #endif
  1260. }
  1261. }
  1262. }
  1263. /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
  1264. static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
  1265. {
  1266. if (size > max_size)
  1267. size = max_size;
  1268. s->lba = -1; /* no sector read */
  1269. s->packet_transfer_size = size;
  1270. s->io_buffer_size = size; /* dma: send the reply data as one chunk */
  1271. s->elementary_transfer_size = 0;
  1272. s->io_buffer_index = 0;
  1273. if (s->atapi_dma) {
  1274. s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
  1275. ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
  1276. } else {
  1277. s->status = READY_STAT | SEEK_STAT;
  1278. ide_atapi_cmd_reply_end(s);
  1279. }
  1280. }
  1281. /* start a CD-CDROM read command */
  1282. static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
  1283. int sector_size)
  1284. {
  1285. s->lba = lba;
  1286. s->packet_transfer_size = nb_sectors * sector_size;
  1287. s->elementary_transfer_size = 0;
  1288. s->io_buffer_index = sector_size;
  1289. s->cd_sector_size = sector_size;
  1290. s->status = READY_STAT | SEEK_STAT;
  1291. ide_atapi_cmd_reply_end(s);
  1292. }
  1293. /* ATAPI DMA support */
  1294. /* XXX: handle read errors */
  1295. static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
  1296. {
  1297. BMDMAState *bm = opaque;
  1298. IDEState *s = bm->ide_if;
  1299. int data_offset, n;
  1300. if (ret < 0) {
  1301. ide_atapi_io_error(s, ret);
  1302. goto eot;
  1303. }
  1304. if (s->io_buffer_size > 0) {
  1305. /*
  1306. * For a cdrom read sector command (s->lba != -1),
  1307. * adjust the lba for the next s->io_buffer_size chunk
  1308. * and dma the current chunk.
  1309. * For a command != read (s->lba == -1), just transfer
  1310. * the reply data.
  1311. */
  1312. if (s->lba != -1) {
  1313. if (s->cd_sector_size == 2352) {
  1314. n = 1;
  1315. cd_data_to_raw(s->io_buffer, s->lba);
  1316. } else {
  1317. n = s->io_buffer_size >> 11;
  1318. }
  1319. s->lba += n;
  1320. }
  1321. s->packet_transfer_size -= s->io_buffer_size;
  1322. if (dma_buf_rw(bm, 1) == 0)
  1323. goto eot;
  1324. }
  1325. if (s->packet_transfer_size <= 0) {
  1326. s->status = READY_STAT | SEEK_STAT;
  1327. s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
  1328. ide_set_irq(s);
  1329. eot:
  1330. bm->status &= ~BM_STATUS_DMAING;
  1331. bm->status |= BM_STATUS_INT;
  1332. bm->dma_cb = NULL;
  1333. bm->ide_if = NULL;
  1334. bm->aiocb = NULL;
  1335. return;
  1336. }
  1337. s->io_buffer_index = 0;
  1338. if (s->cd_sector_size == 2352) {
  1339. n = 1;
  1340. s->io_buffer_size = s->cd_sector_size;
  1341. data_offset = 16;
  1342. } else {
  1343. n = s->packet_transfer_size >> 11;
  1344. if (n > (IDE_DMA_BUF_SECTORS / 4))
  1345. n = (IDE_DMA_BUF_SECTORS / 4);
  1346. s->io_buffer_size = n * 2048;
  1347. data_offset = 0;
  1348. }
  1349. #ifdef DEBUG_AIO
  1350. printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
  1351. #endif
  1352. bm->aiocb = bdrv_aio_read(s->bs, (int64_t)s->lba << 2,
  1353. s->io_buffer + data_offset, n * 4,
  1354. ide_atapi_cmd_read_dma_cb, bm);
  1355. if (!bm->aiocb) {
  1356. /* Note: media not present is the most likely case */
  1357. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1358. ASC_MEDIUM_NOT_PRESENT);
  1359. goto eot;
  1360. }
  1361. }
  1362. /* start a CD-CDROM read command with DMA */
  1363. /* XXX: test if DMA is available */
  1364. static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
  1365. int sector_size)
  1366. {
  1367. s->lba = lba;
  1368. s->packet_transfer_size = nb_sectors * sector_size;
  1369. s->io_buffer_index = 0;
  1370. s->io_buffer_size = 0;
  1371. s->cd_sector_size = sector_size;
  1372. /* XXX: check if BUSY_STAT should be set */
  1373. s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
  1374. ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
  1375. }
  1376. static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
  1377. int sector_size)
  1378. {
  1379. #ifdef DEBUG_IDE_ATAPI
  1380. printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
  1381. lba, nb_sectors);
  1382. #endif
  1383. if (s->atapi_dma) {
  1384. ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
  1385. } else {
  1386. ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
  1387. }
  1388. }
  1389. static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
  1390. uint16_t profile)
  1391. {
  1392. uint8_t *buf_profile = buf + 12; /* start of profiles */
  1393. buf_profile += ((*index) * 4); /* start of indexed profile */
  1394. cpu_to_ube16 (buf_profile, profile);
  1395. buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
  1396. /* each profile adds 4 bytes to the response */
  1397. (*index)++;
  1398. buf[11] += 4; /* Additional Length */
  1399. return 4;
  1400. }
  1401. static int ide_dvd_read_structure(IDEState *s, int format,
  1402. const uint8_t *packet, uint8_t *buf)
  1403. {
  1404. switch (format) {
  1405. case 0x0: /* Physical format information */
  1406. {
  1407. int layer = packet[6];
  1408. uint64_t total_sectors;
  1409. if (layer != 0)
  1410. return -ASC_INV_FIELD_IN_CMD_PACKET;
  1411. bdrv_get_geometry(s->bs, &total_sectors);
  1412. total_sectors >>= 2;
  1413. if (total_sectors == 0)
  1414. return -ASC_MEDIUM_NOT_PRESENT;
  1415. buf[4] = 1; /* DVD-ROM, part version 1 */
  1416. buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
  1417. buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
  1418. buf[7] = 0; /* default densities */
  1419. /* FIXME: 0x30000 per spec? */
  1420. cpu_to_ube32(buf + 8, 0); /* start sector */
  1421. cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
  1422. cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
  1423. /* Size of buffer, not including 2 byte size field */
  1424. cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
  1425. /* 2k data + 4 byte header */
  1426. return (2048 + 4);
  1427. }
  1428. case 0x01: /* DVD copyright information */
  1429. buf[4] = 0; /* no copyright data */
  1430. buf[5] = 0; /* no region restrictions */
  1431. /* Size of buffer, not including 2 byte size field */
  1432. cpu_to_be16wu((uint16_t *)buf, 4 + 2);
  1433. /* 4 byte header + 4 byte data */
  1434. return (4 + 4);
  1435. case 0x03: /* BCA information - invalid field for no BCA info */
  1436. return -ASC_INV_FIELD_IN_CMD_PACKET;
  1437. case 0x04: /* DVD disc manufacturing information */
  1438. /* Size of buffer, not including 2 byte size field */
  1439. cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
  1440. /* 2k data + 4 byte header */
  1441. return (2048 + 4);
  1442. case 0xff:
  1443. /*
  1444. * This lists all the command capabilities above. Add new ones
  1445. * in order and update the length and buffer return values.
  1446. */
  1447. buf[4] = 0x00; /* Physical format */
  1448. buf[5] = 0x40; /* Not writable, is readable */
  1449. cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
  1450. buf[8] = 0x01; /* Copyright info */
  1451. buf[9] = 0x40; /* Not writable, is readable */
  1452. cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
  1453. buf[12] = 0x03; /* BCA info */
  1454. buf[13] = 0x40; /* Not writable, is readable */
  1455. cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
  1456. buf[16] = 0x04; /* Manufacturing info */
  1457. buf[17] = 0x40; /* Not writable, is readable */
  1458. cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
  1459. /* Size of buffer, not including 2 byte size field */
  1460. cpu_to_be16wu((uint16_t *)buf, 16 + 2);
  1461. /* data written + 4 byte header */
  1462. return (16 + 4);
  1463. default: /* TODO: formats beyond DVD-ROM requires */
  1464. return -ASC_INV_FIELD_IN_CMD_PACKET;
  1465. }
  1466. }
  1467. static void ide_atapi_cmd(IDEState *s)
  1468. {
  1469. const uint8_t *packet;
  1470. uint8_t *buf;
  1471. int max_len;
  1472. packet = s->io_buffer;
  1473. buf = s->io_buffer;
  1474. #ifdef DEBUG_IDE_ATAPI
  1475. {
  1476. int i;
  1477. printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
  1478. for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
  1479. printf(" %02x", packet[i]);
  1480. }
  1481. printf("\n");
  1482. }
  1483. #endif
  1484. /* If there's a UNIT_ATTENTION condition pending, only
  1485. REQUEST_SENSE and INQUIRY commands are allowed to complete. */
  1486. if (s->sense_key == SENSE_UNIT_ATTENTION &&
  1487. s->io_buffer[0] != GPCMD_REQUEST_SENSE &&
  1488. s->io_buffer[0] != GPCMD_INQUIRY) {
  1489. ide_atapi_cmd_check_status(s);
  1490. return;
  1491. }
  1492. switch(s->io_buffer[0]) {
  1493. case GPCMD_TEST_UNIT_READY:
  1494. if (bdrv_is_inserted(s->bs) && !s->cdrom_changed) {
  1495. ide_atapi_cmd_ok(s);
  1496. } else {
  1497. s->cdrom_changed = 0;
  1498. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1499. ASC_MEDIUM_NOT_PRESENT);
  1500. }
  1501. break;
  1502. case GPCMD_MODE_SENSE_6:
  1503. case GPCMD_MODE_SENSE_10:
  1504. {
  1505. int action, code;
  1506. if (packet[0] == GPCMD_MODE_SENSE_10)
  1507. max_len = ube16_to_cpu(packet + 7);
  1508. else
  1509. max_len = packet[4];
  1510. action = packet[2] >> 6;
  1511. code = packet[2] & 0x3f;
  1512. switch(action) {
  1513. case 0: /* current values */
  1514. switch(code) {
  1515. case 0x01: /* error recovery */
  1516. cpu_to_ube16(&buf[0], 16 + 6);
  1517. buf[2] = 0x70;
  1518. buf[3] = 0;
  1519. buf[4] = 0;
  1520. buf[5] = 0;
  1521. buf[6] = 0;
  1522. buf[7] = 0;
  1523. buf[8] = 0x01;
  1524. buf[9] = 0x06;
  1525. buf[10] = 0x00;
  1526. buf[11] = 0x05;
  1527. buf[12] = 0x00;
  1528. buf[13] = 0x00;
  1529. buf[14] = 0x00;
  1530. buf[15] = 0x00;
  1531. ide_atapi_cmd_reply(s, 16, max_len);
  1532. break;
  1533. case 0x2a:
  1534. cpu_to_ube16(&buf[0], 28 + 6);
  1535. buf[2] = 0x70;
  1536. buf[3] = 0;
  1537. buf[4] = 0;
  1538. buf[5] = 0;
  1539. buf[6] = 0;
  1540. buf[7] = 0;
  1541. buf[8] = 0x2a;
  1542. buf[9] = 0x12;
  1543. buf[10] = 0x00;
  1544. buf[11] = 0x00;
  1545. /* Claim PLAY_AUDIO capability (0x01) since some Linux
  1546. code checks for this to automount media. */
  1547. buf[12] = 0x71;
  1548. buf[13] = 3 << 5;
  1549. buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
  1550. if (bdrv_is_locked(s->bs))
  1551. buf[6] |= 1 << 1;
  1552. buf[15] = 0x00;
  1553. cpu_to_ube16(&buf[16], 706);
  1554. buf[18] = 0;
  1555. buf[19] = 2;
  1556. cpu_to_ube16(&buf[20], 512);
  1557. cpu_to_ube16(&buf[22], 706);
  1558. buf[24] = 0;
  1559. buf[25] = 0;
  1560. buf[26] = 0;
  1561. buf[27] = 0;
  1562. ide_atapi_cmd_reply(s, 28, max_len);
  1563. break;
  1564. default:
  1565. goto error_cmd;
  1566. }
  1567. break;
  1568. case 1: /* changeable values */
  1569. goto error_cmd;
  1570. case 2: /* default values */
  1571. goto error_cmd;
  1572. default:
  1573. case 3: /* saved values */
  1574. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1575. ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
  1576. break;
  1577. }
  1578. }
  1579. break;
  1580. case GPCMD_REQUEST_SENSE:
  1581. max_len = packet[4];
  1582. memset(buf, 0, 18);
  1583. buf[0] = 0x70 | (1 << 7);
  1584. buf[2] = s->sense_key;
  1585. buf[7] = 10;
  1586. buf[12] = s->asc;
  1587. if (s->sense_key == SENSE_UNIT_ATTENTION)
  1588. s->sense_key = SENSE_NONE;
  1589. ide_atapi_cmd_reply(s, 18, max_len);
  1590. break;
  1591. case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  1592. if (bdrv_is_inserted(s->bs)) {
  1593. bdrv_set_locked(s->bs, packet[4] & 1);
  1594. ide_atapi_cmd_ok(s);
  1595. } else {
  1596. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1597. ASC_MEDIUM_NOT_PRESENT);
  1598. }
  1599. break;
  1600. case GPCMD_READ_10:
  1601. case GPCMD_READ_12:
  1602. {
  1603. int nb_sectors, lba;
  1604. if (packet[0] == GPCMD_READ_10)
  1605. nb_sectors = ube16_to_cpu(packet + 7);
  1606. else
  1607. nb_sectors = ube32_to_cpu(packet + 6);
  1608. lba = ube32_to_cpu(packet + 2);
  1609. if (nb_sectors == 0) {
  1610. ide_atapi_cmd_ok(s);
  1611. break;
  1612. }
  1613. ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
  1614. }
  1615. break;
  1616. case GPCMD_READ_CD:
  1617. {
  1618. int nb_sectors, lba, transfer_request;
  1619. nb_sectors = (packet[6] << 16) | (packet[7] << 8) | packet[8];
  1620. lba = ube32_to_cpu(packet + 2);
  1621. if (nb_sectors == 0) {
  1622. ide_atapi_cmd_ok(s);
  1623. break;
  1624. }
  1625. transfer_request = packet[9];
  1626. switch(transfer_request & 0xf8) {
  1627. case 0x00:
  1628. /* nothing */
  1629. ide_atapi_cmd_ok(s);
  1630. break;
  1631. case 0x10:
  1632. /* normal read */
  1633. ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
  1634. break;
  1635. case 0xf8:
  1636. /* read all data */
  1637. ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
  1638. break;
  1639. default:
  1640. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1641. ASC_INV_FIELD_IN_CMD_PACKET);
  1642. break;
  1643. }
  1644. }
  1645. break;
  1646. case GPCMD_SEEK:
  1647. {
  1648. unsigned int lba;
  1649. uint64_t total_sectors;
  1650. bdrv_get_geometry(s->bs, &total_sectors);
  1651. total_sectors >>= 2;
  1652. if (total_sectors == 0) {
  1653. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1654. ASC_MEDIUM_NOT_PRESENT);
  1655. break;
  1656. }
  1657. lba = ube32_to_cpu(packet + 2);
  1658. if (lba >= total_sectors) {
  1659. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1660. ASC_LOGICAL_BLOCK_OOR);
  1661. break;
  1662. }
  1663. ide_atapi_cmd_ok(s);
  1664. }
  1665. break;
  1666. case GPCMD_START_STOP_UNIT:
  1667. {
  1668. int start, eject, err = 0;
  1669. start = packet[4] & 1;
  1670. eject = (packet[4] >> 1) & 1;
  1671. if (eject) {
  1672. err = bdrv_eject(s->bs, !start);
  1673. }
  1674. switch (err) {
  1675. case 0:
  1676. ide_atapi_cmd_ok(s);
  1677. break;
  1678. case -EBUSY:
  1679. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1680. ASC_MEDIA_REMOVAL_PREVENTED);
  1681. break;
  1682. default:
  1683. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1684. ASC_MEDIUM_NOT_PRESENT);
  1685. break;
  1686. }
  1687. }
  1688. break;
  1689. case GPCMD_MECHANISM_STATUS:
  1690. {
  1691. max_len = ube16_to_cpu(packet + 8);
  1692. cpu_to_ube16(buf, 0);
  1693. /* no current LBA */
  1694. buf[2] = 0;
  1695. buf[3] = 0;
  1696. buf[4] = 0;
  1697. buf[5] = 1;
  1698. cpu_to_ube16(buf + 6, 0);
  1699. ide_atapi_cmd_reply(s, 8, max_len);
  1700. }
  1701. break;
  1702. case GPCMD_READ_TOC_PMA_ATIP:
  1703. {
  1704. int format, msf, start_track, len;
  1705. uint64_t total_sectors;
  1706. bdrv_get_geometry(s->bs, &total_sectors);
  1707. total_sectors >>= 2;
  1708. if (total_sectors == 0) {
  1709. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1710. ASC_MEDIUM_NOT_PRESENT);
  1711. break;
  1712. }
  1713. max_len = ube16_to_cpu(packet + 7);
  1714. format = packet[9] >> 6;
  1715. msf = (packet[1] >> 1) & 1;
  1716. start_track = packet[6];
  1717. switch(format) {
  1718. case 0:
  1719. len = cdrom_read_toc(total_sectors, buf, msf, start_track);
  1720. if (len < 0)
  1721. goto error_cmd;
  1722. ide_atapi_cmd_reply(s, len, max_len);
  1723. break;
  1724. case 1:
  1725. /* multi session : only a single session defined */
  1726. memset(buf, 0, 12);
  1727. buf[1] = 0x0a;
  1728. buf[2] = 0x01;
  1729. buf[3] = 0x01;
  1730. ide_atapi_cmd_reply(s, 12, max_len);
  1731. break;
  1732. case 2:
  1733. len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
  1734. if (len < 0)
  1735. goto error_cmd;
  1736. ide_atapi_cmd_reply(s, len, max_len);
  1737. break;
  1738. default:
  1739. error_cmd:
  1740. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1741. ASC_INV_FIELD_IN_CMD_PACKET);
  1742. break;
  1743. }
  1744. }
  1745. break;
  1746. case GPCMD_READ_CDVD_CAPACITY:
  1747. {
  1748. uint64_t total_sectors;
  1749. bdrv_get_geometry(s->bs, &total_sectors);
  1750. total_sectors >>= 2;
  1751. if (total_sectors == 0) {
  1752. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  1753. ASC_MEDIUM_NOT_PRESENT);
  1754. break;
  1755. }
  1756. /* NOTE: it is really the number of sectors minus 1 */
  1757. cpu_to_ube32(buf, total_sectors - 1);
  1758. cpu_to_ube32(buf + 4, 2048);
  1759. ide_atapi_cmd_reply(s, 8, 8);
  1760. }
  1761. break;
  1762. case GPCMD_READ_DVD_STRUCTURE:
  1763. {
  1764. int media = packet[1];
  1765. int format = packet[7];
  1766. int ret;
  1767. max_len = ube16_to_cpu(packet + 8);
  1768. if (format < 0xff) {
  1769. if (media_is_cd(s)) {
  1770. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1771. ASC_INCOMPATIBLE_FORMAT);
  1772. break;
  1773. } else if (!media_present(s)) {
  1774. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1775. ASC_INV_FIELD_IN_CMD_PACKET);
  1776. break;
  1777. }
  1778. }
  1779. memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
  1780. IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
  1781. switch (format) {
  1782. case 0x00 ... 0x7f:
  1783. case 0xff:
  1784. if (media == 0) {
  1785. ret = ide_dvd_read_structure(s, format, packet, buf);
  1786. if (ret < 0)
  1787. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
  1788. else
  1789. ide_atapi_cmd_reply(s, ret, max_len);
  1790. break;
  1791. }
  1792. /* TODO: BD support, fall through for now */
  1793. /* Generic disk structures */
  1794. case 0x80: /* TODO: AACS volume identifier */
  1795. case 0x81: /* TODO: AACS media serial number */
  1796. case 0x82: /* TODO: AACS media identifier */
  1797. case 0x83: /* TODO: AACS media key block */
  1798. case 0x90: /* TODO: List of recognized format layers */
  1799. case 0xc0: /* TODO: Write protection status */
  1800. default:
  1801. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1802. ASC_INV_FIELD_IN_CMD_PACKET);
  1803. break;
  1804. }
  1805. }
  1806. break;
  1807. case GPCMD_SET_SPEED:
  1808. ide_atapi_cmd_ok(s);
  1809. break;
  1810. case GPCMD_INQUIRY:
  1811. max_len = packet[4];
  1812. buf[0] = 0x05; /* CD-ROM */
  1813. buf[1] = 0x80; /* removable */
  1814. buf[2] = 0x00; /* ISO */
  1815. buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
  1816. buf[4] = 31; /* additional length */
  1817. buf[5] = 0; /* reserved */
  1818. buf[6] = 0; /* reserved */
  1819. buf[7] = 0; /* reserved */
  1820. padstr8(buf + 8, 8, "QEMU");
  1821. padstr8(buf + 16, 16, "QEMU DVD-ROM");
  1822. padstr8(buf + 32, 4, QEMU_VERSION);
  1823. ide_atapi_cmd_reply(s, 36, max_len);
  1824. break;
  1825. case GPCMD_GET_CONFIGURATION:
  1826. {
  1827. uint32_t len;
  1828. uint8_t index = 0;
  1829. /* only feature 0 is supported */
  1830. if (packet[2] != 0 || packet[3] != 0) {
  1831. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1832. ASC_INV_FIELD_IN_CMD_PACKET);
  1833. break;
  1834. }
  1835. /* XXX: could result in alignment problems in some architectures */
  1836. max_len = ube16_to_cpu(packet + 7);
  1837. /*
  1838. * XXX: avoid overflow for io_buffer if max_len is bigger than
  1839. * the size of that buffer (dimensioned to max number of
  1840. * sectors to transfer at once)
  1841. *
  1842. * Only a problem if the feature/profiles grow.
  1843. */
  1844. if (max_len > 512) /* XXX: assume 1 sector */
  1845. max_len = 512;
  1846. memset(buf, 0, max_len);
  1847. /*
  1848. * the number of sectors from the media tells us which profile
  1849. * to use as current. 0 means there is no media
  1850. */
  1851. if (media_is_dvd(s))
  1852. cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
  1853. else if (media_is_cd(s))
  1854. cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
  1855. buf[10] = 0x02 | 0x01; /* persistent and current */
  1856. len = 12; /* headers: 8 + 4 */
  1857. len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
  1858. len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
  1859. cpu_to_ube32(buf, len - 4); /* data length */
  1860. ide_atapi_cmd_reply(s, len, max_len);
  1861. break;
  1862. }
  1863. default:
  1864. ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
  1865. ASC_ILLEGAL_OPCODE);
  1866. break;
  1867. }
  1868. }
  1869. static void ide_cfata_metadata_inquiry(IDEState *s)
  1870. {
  1871. uint16_t *p;
  1872. uint32_t spd;
  1873. p = (uint16_t *) s->io_buffer;
  1874. memset(p, 0, 0x200);
  1875. spd = ((s->mdata_size - 1) >> 9) + 1;
  1876. put_le16(p + 0, 0x0001); /* Data format revision */
  1877. put_le16(p + 1, 0x0000); /* Media property: silicon */
  1878. put_le16(p + 2, s->media_changed); /* Media status */
  1879. put_le16(p + 3, s->mdata_size & 0xffff); /* Capacity in bytes (low) */
  1880. put_le16(p + 4, s->mdata_size >> 16); /* Capacity in bytes (high) */
  1881. put_le16(p + 5, spd & 0xffff); /* Sectors per device (low) */
  1882. put_le16(p + 6, spd >> 16); /* Sectors per device (high) */
  1883. }
  1884. static void ide_cfata_metadata_read(IDEState *s)
  1885. {
  1886. uint16_t *p;
  1887. if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
  1888. s->status = ERR_STAT;
  1889. s->error = ABRT_ERR;
  1890. return;
  1891. }
  1892. p = (uint16_t *) s->io_buffer;
  1893. memset(p, 0, 0x200);
  1894. put_le16(p + 0, s->media_changed); /* Media status */
  1895. memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
  1896. MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
  1897. s->nsector << 9), 0x200 - 2));
  1898. }
  1899. static void ide_cfata_metadata_write(IDEState *s)
  1900. {
  1901. if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
  1902. s->status = ERR_STAT;
  1903. s->error = ABRT_ERR;
  1904. return;
  1905. }
  1906. s->media_changed = 0;
  1907. memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
  1908. s->io_buffer + 2,
  1909. MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
  1910. s->nsector << 9), 0x200 - 2));
  1911. }
  1912. /* called when the inserted state of the media has changed */
  1913. static void cdrom_change_cb(void *opaque)
  1914. {
  1915. IDEState *s = opaque;
  1916. uint64_t nb_sectors;
  1917. bdrv_get_geometry(s->bs, &nb_sectors);
  1918. s->nb_sectors = nb_sectors;
  1919. s->sense_key = SENSE_UNIT_ATTENTION;
  1920. s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
  1921. s->cdrom_changed = 1;
  1922. ide_set_irq(s);
  1923. }
  1924. static void ide_cmd_lba48_transform(IDEState *s, int lba48)
  1925. {
  1926. s->lba48 = lba48;
  1927. /* handle the 'magic' 0 nsector count conversion here. to avoid
  1928. * fiddling with the rest of the read logic, we just store the
  1929. * full sector count in ->nsector and ignore ->hob_nsector from now
  1930. */
  1931. if (!s->lba48) {
  1932. if (!s->nsector)
  1933. s->nsector = 256;
  1934. } else {
  1935. if (!s->nsector && !s->hob_nsector)
  1936. s->nsector = 65536;
  1937. else {
  1938. int lo = s->nsector;
  1939. int hi = s->hob_nsector;
  1940. s->nsector = (hi << 8) | lo;
  1941. }
  1942. }
  1943. }
  1944. static void ide_clear_hob(IDEState *ide_if)
  1945. {
  1946. /* any write clears HOB high bit of device control register */
  1947. ide_if[0].select &= ~(1 << 7);
  1948. ide_if[1].select &= ~(1 << 7);
  1949. }
  1950. static void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  1951. {
  1952. IDEState *ide_if = opaque;
  1953. IDEState *s;
  1954. int unit, n;
  1955. int lba48 = 0;
  1956. #ifdef DEBUG_IDE
  1957. printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
  1958. #endif
  1959. addr &= 7;
  1960. /* ignore writes to command block while busy with previous command */
  1961. if (addr != 7 && (ide_if->cur_drive->status & (BUSY_STAT|DRQ_STAT)))
  1962. return;
  1963. switch(addr) {
  1964. case 0:
  1965. break;
  1966. case 1:
  1967. ide_clear_hob(ide_if);
  1968. /* NOTE: data is written to the two drives */
  1969. ide_if[0].hob_feature = ide_if[0].feature;
  1970. ide_if[1].hob_feature = ide_if[1].feature;
  1971. ide_if[0].feature = val;
  1972. ide_if[1].feature = val;
  1973. break;
  1974. case 2:
  1975. ide_clear_hob(ide_if);
  1976. ide_if[0].hob_nsector = ide_if[0].nsector;
  1977. ide_if[1].hob_nsector = ide_if[1].nsector;
  1978. ide_if[0].nsector = val;
  1979. ide_if[1].nsector = val;
  1980. break;
  1981. case 3:
  1982. ide_clear_hob(ide_if);
  1983. ide_if[0].hob_sector = ide_if[0].sector;
  1984. ide_if[1].hob_sector = ide_if[1].sector;
  1985. ide_if[0].sector = val;
  1986. ide_if[1].sector = val;
  1987. break;
  1988. case 4:
  1989. ide_clear_hob(ide_if);
  1990. ide_if[0].hob_lcyl = ide_if[0].lcyl;
  1991. ide_if[1].hob_lcyl = ide_if[1].lcyl;
  1992. ide_if[0].lcyl = val;
  1993. ide_if[1].lcyl = val;
  1994. break;
  1995. case 5:
  1996. ide_clear_hob(ide_if);
  1997. ide_if[0].hob_hcyl = ide_if[0].hcyl;
  1998. ide_if[1].hob_hcyl = ide_if[1].hcyl;
  1999. ide_if[0].hcyl = val;
  2000. ide_if[1].hcyl = val;
  2001. break;
  2002. case 6:
  2003. /* FIXME: HOB readback uses bit 7 */
  2004. ide_if[0].select = (val & ~0x10) | 0xa0;
  2005. ide_if[1].select = (val | 0x10) | 0xa0;
  2006. /* select drive */
  2007. unit = (val >> 4) & 1;
  2008. s = ide_if + unit;
  2009. ide_if->cur_drive = s;
  2010. break;
  2011. default:
  2012. case 7:
  2013. /* command */
  2014. #if defined(DEBUG_IDE)
  2015. printf("ide: CMD=%02x\n", val);
  2016. #endif
  2017. s = ide_if->cur_drive;
  2018. /* ignore commands to non existant slave */
  2019. if (s != ide_if && !s->bs)
  2020. break;
  2021. /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
  2022. if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
  2023. break;
  2024. switch(val) {
  2025. case WIN_IDENTIFY:
  2026. if (s->bs && !s->is_cdrom) {
  2027. if (!s->is_cf)
  2028. ide_identify(s);
  2029. else
  2030. ide_cfata_identify(s);
  2031. s->status = READY_STAT | SEEK_STAT;
  2032. ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
  2033. } else {
  2034. if (s->is_cdrom) {
  2035. ide_set_signature(s);
  2036. }
  2037. ide_abort_command(s);
  2038. }
  2039. ide_set_irq(s);
  2040. break;
  2041. case WIN_SPECIFY:
  2042. case WIN_RECAL:
  2043. s->error = 0;
  2044. s->status = READY_STAT | SEEK_STAT;
  2045. ide_set_irq(s);
  2046. break;
  2047. case WIN_SETMULT:
  2048. if (s->is_cf && s->nsector == 0) {
  2049. /* Disable Read and Write Multiple */
  2050. s->mult_sectors = 0;
  2051. s->status = READY_STAT | SEEK_STAT;
  2052. } else if ((s->nsector & 0xff) != 0 &&
  2053. ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
  2054. (s->nsector & (s->nsector - 1)) != 0)) {
  2055. ide_abort_command(s);
  2056. } else {
  2057. s->mult_sectors = s->nsector & 0xff;
  2058. s->status = READY_STAT | SEEK_STAT;
  2059. }
  2060. ide_set_irq(s);
  2061. break;
  2062. case WIN_VERIFY_EXT:
  2063. lba48 = 1;
  2064. case WIN_VERIFY:
  2065. case WIN_VERIFY_ONCE:
  2066. /* do sector number check ? */
  2067. ide_cmd_lba48_transform(s, lba48);
  2068. s->status = READY_STAT | SEEK_STAT;
  2069. ide_set_irq(s);
  2070. break;
  2071. case WIN_READ_EXT:
  2072. lba48 = 1;
  2073. case WIN_READ:
  2074. case WIN_READ_ONCE:
  2075. if (!s->bs)
  2076. goto abort_cmd;
  2077. ide_cmd_lba48_transform(s, lba48);
  2078. s->req_nb_sectors = 1;
  2079. ide_sector_read(s);
  2080. break;
  2081. case WIN_WRITE_EXT:
  2082. lba48 = 1;
  2083. case WIN_WRITE:
  2084. case WIN_WRITE_ONCE:
  2085. case CFA_WRITE_SECT_WO_ERASE:
  2086. case WIN_WRITE_VERIFY:
  2087. ide_cmd_lba48_transform(s, lba48);
  2088. s->error = 0;
  2089. s->status = SEEK_STAT | READY_STAT;
  2090. s->req_nb_sectors = 1;
  2091. ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
  2092. s->media_changed = 1;
  2093. break;
  2094. case WIN_MULTREAD_EXT:
  2095. lba48 = 1;
  2096. case WIN_MULTREAD:
  2097. if (!s->mult_sectors)
  2098. goto abort_cmd;
  2099. ide_cmd_lba48_transform(s, lba48);
  2100. s->req_nb_sectors = s->mult_sectors;
  2101. ide_sector_read(s);
  2102. break;
  2103. case WIN_MULTWRITE_EXT:
  2104. lba48 = 1;
  2105. case WIN_MULTWRITE:
  2106. case CFA_WRITE_MULTI_WO_ERASE:
  2107. if (!s->mult_sectors)
  2108. goto abort_cmd;
  2109. ide_cmd_lba48_transform(s, lba48);
  2110. s->error = 0;
  2111. s->status = SEEK_STAT | READY_STAT;
  2112. s->req_nb_sectors = s->mult_sectors;
  2113. n = s->nsector;
  2114. if (n > s->req_nb_sectors)
  2115. n = s->req_nb_sectors;
  2116. ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
  2117. s->media_changed = 1;
  2118. break;
  2119. case WIN_READDMA_EXT:
  2120. lba48 = 1;
  2121. case WIN_READDMA:
  2122. case WIN_READDMA_ONCE:
  2123. if (!s->bs)
  2124. goto abort_cmd;
  2125. ide_cmd_lba48_transform(s, lba48);
  2126. ide_sector_read_dma(s);
  2127. break;
  2128. case WIN_WRITEDMA_EXT:
  2129. lba48 = 1;
  2130. case WIN_WRITEDMA:
  2131. case WIN_WRITEDMA_ONCE:
  2132. if (!s->bs)
  2133. goto abort_cmd;
  2134. ide_cmd_lba48_transform(s, lba48);
  2135. ide_sector_write_dma(s);
  2136. s->media_changed = 1;
  2137. break;
  2138. case WIN_READ_NATIVE_MAX_EXT:
  2139. lba48 = 1;
  2140. case WIN_READ_NATIVE_MAX:
  2141. ide_cmd_lba48_transform(s, lba48);
  2142. ide_set_sector(s, s->nb_sectors - 1);
  2143. s->status = READY_STAT | SEEK_STAT;
  2144. ide_set_irq(s);
  2145. break;
  2146. case WIN_CHECKPOWERMODE1:
  2147. case WIN_CHECKPOWERMODE2:
  2148. s->nsector = 0xff; /* device active or idle */
  2149. s->status = READY_STAT | SEEK_STAT;
  2150. ide_set_irq(s);
  2151. break;
  2152. case WIN_SETFEATURES:
  2153. if (!s->bs)
  2154. goto abort_cmd;
  2155. /* XXX: valid for CDROM ? */
  2156. switch(s->feature) {
  2157. case 0xcc: /* reverting to power-on defaults enable */
  2158. case 0x66: /* reverting to power-on defaults disable */
  2159. case 0x02: /* write cache enable */
  2160. case 0x82: /* write cache disable */
  2161. case 0xaa: /* read look-ahead enable */
  2162. case 0x55: /* read look-ahead disable */
  2163. case 0x05: /* set advanced power management mode */
  2164. case 0x85: /* disable advanced power management mode */
  2165. case 0x69: /* NOP */
  2166. case 0x67: /* NOP */
  2167. case 0x96: /* NOP */
  2168. case 0x9a: /* NOP */
  2169. case 0x42: /* enable Automatic Acoustic Mode */
  2170. case 0xc2: /* disable Automatic Acoustic Mode */
  2171. s->status = READY_STAT | SEEK_STAT;
  2172. ide_set_irq(s);
  2173. break;
  2174. case 0x03: { /* set transfer mode */
  2175. uint8_t val = s->nsector & 0x07;
  2176. switch (s->nsector >> 3) {
  2177. case 0x00: /* pio default */
  2178. case 0x01: /* pio mode */
  2179. put_le16(s->identify_data + 62,0x07);
  2180. put_le16(s->identify_data + 63,0x07);
  2181. put_le16(s->identify_data + 88,0x3f);
  2182. break;
  2183. case 0x02: /* sigle word dma mode*/
  2184. put_le16(s->identify_data + 62,0x07 | (1 << (val + 8)));
  2185. put_le16(s->identify_data + 63,0x07);
  2186. put_le16(s->identify_data + 88,0x3f);
  2187. break;
  2188. case 0x04: /* mdma mode */
  2189. put_le16(s->identify_data + 62,0x07);
  2190. put_le16(s->identify_data + 63,0x07 | (1 << (val + 8)));
  2191. put_le16(s->identify_data + 88,0x3f);
  2192. break;
  2193. case 0x08: /* udma mode */
  2194. put_le16(s->identify_data + 62,0x07);
  2195. put_le16(s->identify_data + 63,0x07);
  2196. put_le16(s->identify_data + 88,0x3f | (1 << (val + 8)));
  2197. break;
  2198. default:
  2199. goto abort_cmd;
  2200. }
  2201. s->status = READY_STAT | SEEK_STAT;
  2202. ide_set_irq(s);
  2203. break;
  2204. }
  2205. default:
  2206. goto abort_cmd;
  2207. }
  2208. break;
  2209. case WIN_FLUSH_CACHE:
  2210. case WIN_FLUSH_CACHE_EXT:
  2211. if (s->bs)
  2212. bdrv_flush(s->bs);
  2213. s->status = READY_STAT | SEEK_STAT;
  2214. ide_set_irq(s);
  2215. break;
  2216. case WIN_STANDBY:
  2217. case WIN_STANDBY2:
  2218. case WIN_STANDBYNOW1:
  2219. case WIN_STANDBYNOW2:
  2220. case WIN_IDLEIMMEDIATE:
  2221. case CFA_IDLEIMMEDIATE:
  2222. case WIN_SETIDLE1:
  2223. case WIN_SETIDLE2:
  2224. case WIN_SLEEPNOW1:
  2225. case WIN_SLEEPNOW2:
  2226. s->status = READY_STAT;
  2227. ide_set_irq(s);
  2228. break;
  2229. case WIN_SEEK:
  2230. if(s->is_cdrom)
  2231. goto abort_cmd;
  2232. /* XXX: Check that seek is within bounds */
  2233. s->status = READY_STAT | SEEK_STAT;
  2234. ide_set_irq(s);
  2235. break;
  2236. /* ATAPI commands */
  2237. case WIN_PIDENTIFY:
  2238. if (s->is_cdrom) {
  2239. ide_atapi_identify(s);
  2240. s->status = READY_STAT | SEEK_STAT;
  2241. ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
  2242. } else {
  2243. ide_abort_command(s);
  2244. }
  2245. ide_set_irq(s);
  2246. break;
  2247. case WIN_DIAGNOSE:
  2248. ide_set_signature(s);
  2249. if (s->is_cdrom)
  2250. s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
  2251. * devices to return a clear status register
  2252. * with READY_STAT *not* set. */
  2253. else
  2254. s->status = READY_STAT | SEEK_STAT;
  2255. s->error = 0x01; /* Device 0 passed, Device 1 passed or not
  2256. * present.
  2257. */
  2258. ide_set_irq(s);
  2259. break;
  2260. case WIN_SRST:
  2261. if (!s->is_cdrom)
  2262. goto abort_cmd;
  2263. ide_set_signature(s);
  2264. s->status = 0x00; /* NOTE: READY is _not_ set */
  2265. s->error = 0x01;
  2266. break;
  2267. case WIN_PACKETCMD:
  2268. if (!s->is_cdrom)
  2269. goto abort_cmd;
  2270. /* overlapping commands not supported */
  2271. if (s->feature & 0x02)
  2272. goto abort_cmd;
  2273. s->status = READY_STAT | SEEK_STAT;
  2274. s->atapi_dma = s->feature & 1;
  2275. s->nsector = 1;
  2276. ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
  2277. ide_atapi_cmd);
  2278. break;
  2279. /* CF-ATA commands */
  2280. case CFA_REQ_EXT_ERROR_CODE:
  2281. if (!s->is_cf)
  2282. goto abort_cmd;
  2283. s->error = 0x09; /* miscellaneous error */
  2284. s->status = READY_STAT | SEEK_STAT;
  2285. ide_set_irq(s);
  2286. break;
  2287. case CFA_ERASE_SECTORS:
  2288. case CFA_WEAR_LEVEL:
  2289. if (!s->is_cf)
  2290. goto abort_cmd;
  2291. if (val == CFA_WEAR_LEVEL)
  2292. s->nsector = 0;
  2293. if (val == CFA_ERASE_SECTORS)
  2294. s->media_changed = 1;
  2295. s->error = 0x00;
  2296. s->status = READY_STAT | SEEK_STAT;
  2297. ide_set_irq(s);
  2298. break;
  2299. case CFA_TRANSLATE_SECTOR:
  2300. if (!s->is_cf)
  2301. goto abort_cmd;
  2302. s->error = 0x00;
  2303. s->status = READY_STAT | SEEK_STAT;
  2304. memset(s->io_buffer, 0, 0x200);
  2305. s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
  2306. s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
  2307. s->io_buffer[0x02] = s->select; /* Head */
  2308. s->io_buffer[0x03] = s->sector; /* Sector */
  2309. s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
  2310. s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
  2311. s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
  2312. s->io_buffer[0x13] = 0x00; /* Erase flag */
  2313. s->io_buffer[0x18] = 0x00; /* Hot count */
  2314. s->io_buffer[0x19] = 0x00; /* Hot count */
  2315. s->io_buffer[0x1a] = 0x01; /* Hot count */
  2316. ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
  2317. ide_set_irq(s);
  2318. break;
  2319. case CFA_ACCESS_METADATA_STORAGE:
  2320. if (!s->is_cf)
  2321. goto abort_cmd;
  2322. switch (s->feature) {
  2323. case 0x02: /* Inquiry Metadata Storage */
  2324. ide_cfata_metadata_inquiry(s);
  2325. break;
  2326. case 0x03: /* Read Metadata Storage */
  2327. ide_cfata_metadata_read(s);
  2328. break;
  2329. case 0x04: /* Write Metadata Storage */
  2330. ide_cfata_metadata_write(s);
  2331. break;
  2332. default:
  2333. goto abort_cmd;
  2334. }
  2335. ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
  2336. s->status = 0x00; /* NOTE: READY is _not_ set */
  2337. ide_set_irq(s);
  2338. break;
  2339. case IBM_SENSE_CONDITION:
  2340. if (!s->is_cf)
  2341. goto abort_cmd;
  2342. switch (s->feature) {
  2343. case 0x01: /* sense temperature in device */
  2344. s->nsector = 0x50; /* +20 C */
  2345. break;
  2346. default:
  2347. goto abort_cmd;
  2348. }
  2349. s->status = READY_STAT | SEEK_STAT;
  2350. ide_set_irq(s);
  2351. break;
  2352. default:
  2353. abort_cmd:
  2354. ide_abort_command(s);
  2355. ide_set_irq(s);
  2356. break;
  2357. }
  2358. }
  2359. }
  2360. static uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
  2361. {
  2362. IDEState *ide_if = opaque;
  2363. IDEState *s = ide_if->cur_drive;
  2364. uint32_t addr;
  2365. int ret, hob;
  2366. addr = addr1 & 7;
  2367. /* FIXME: HOB readback uses bit 7, but it's always set right now */
  2368. //hob = s->select & (1 << 7);
  2369. hob = 0;
  2370. switch(addr) {
  2371. case 0:
  2372. ret = 0xff;
  2373. break;
  2374. case 1:
  2375. if ((!ide_if[0].bs && !ide_if[1].bs) ||
  2376. (s != ide_if && !s->bs))
  2377. ret = 0;
  2378. else if (!hob)
  2379. ret = s->error;
  2380. else
  2381. ret = s->hob_feature;
  2382. break;
  2383. case 2:
  2384. if (!ide_if[0].bs && !ide_if[1].bs)
  2385. ret = 0;
  2386. else if (!hob)
  2387. ret = s->nsector & 0xff;
  2388. else
  2389. ret = s->hob_nsector;
  2390. break;
  2391. case 3:
  2392. if (!ide_if[0].bs && !ide_if[1].bs)
  2393. ret = 0;
  2394. else if (!hob)
  2395. ret = s->sector;
  2396. else
  2397. ret = s->hob_sector;
  2398. break;
  2399. case 4:
  2400. if (!ide_if[0].bs && !ide_if[1].bs)
  2401. ret = 0;
  2402. else if (!hob)
  2403. ret = s->lcyl;
  2404. else
  2405. ret = s->hob_lcyl;
  2406. break;
  2407. case 5:
  2408. if (!ide_if[0].bs && !ide_if[1].bs)
  2409. ret = 0;
  2410. else if (!hob)
  2411. ret = s->hcyl;
  2412. else
  2413. ret = s->hob_hcyl;
  2414. break;
  2415. case 6:
  2416. if (!ide_if[0].bs && !ide_if[1].bs)
  2417. ret = 0;
  2418. else
  2419. ret = s->select;
  2420. break;
  2421. default:
  2422. case 7:
  2423. if ((!ide_if[0].bs && !ide_if[1].bs) ||
  2424. (s != ide_if && !s->bs))
  2425. ret = 0;
  2426. else
  2427. ret = s->status;
  2428. qemu_irq_lower(s->irq);
  2429. break;
  2430. }
  2431. #ifdef DEBUG_IDE
  2432. printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
  2433. #endif
  2434. return ret;
  2435. }
  2436. static uint32_t ide_status_read(void *opaque, uint32_t addr)
  2437. {
  2438. IDEState *ide_if = opaque;
  2439. IDEState *s = ide_if->cur_drive;
  2440. int ret;
  2441. if ((!ide_if[0].bs && !ide_if[1].bs) ||
  2442. (s != ide_if && !s->bs))
  2443. ret = 0;
  2444. else
  2445. ret = s->status;
  2446. #ifdef DEBUG_IDE
  2447. printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
  2448. #endif
  2449. return ret;
  2450. }
  2451. static void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
  2452. {
  2453. IDEState *ide_if = opaque;
  2454. IDEState *s;
  2455. int i;
  2456. #ifdef DEBUG_IDE
  2457. printf("ide: write control addr=0x%x val=%02x\n", addr, val);
  2458. #endif
  2459. /* common for both drives */
  2460. if (!(ide_if[0].cmd & IDE_CMD_RESET) &&
  2461. (val & IDE_CMD_RESET)) {
  2462. /* reset low to high */
  2463. for(i = 0;i < 2; i++) {
  2464. s = &ide_if[i];
  2465. s->status = BUSY_STAT | SEEK_STAT;
  2466. s->error = 0x01;
  2467. }
  2468. } else if ((ide_if[0].cmd & IDE_CMD_RESET) &&
  2469. !(val & IDE_CMD_RESET)) {
  2470. /* high to low */
  2471. for(i = 0;i < 2; i++) {
  2472. s = &ide_if[i];
  2473. if (s->is_cdrom)
  2474. s->status = 0x00; /* NOTE: READY is _not_ set */
  2475. else
  2476. s->status = READY_STAT | SEEK_STAT;
  2477. ide_set_signature(s);
  2478. }
  2479. }
  2480. ide_if[0].cmd = val;
  2481. ide_if[1].cmd = val;
  2482. }
  2483. static void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
  2484. {
  2485. IDEState *s = ((IDEState *)opaque)->cur_drive;
  2486. uint8_t *p;
  2487. /* PIO data access allowed only when DRQ bit is set */
  2488. if (!(s->status & DRQ_STAT))
  2489. return;
  2490. p = s->data_ptr;
  2491. *(uint16_t *)p = le16_to_cpu(val);
  2492. p += 2;
  2493. s->data_ptr = p;
  2494. if (p >= s->data_end)
  2495. s->end_transfer_func(s);
  2496. }
  2497. static uint32_t ide_data_readw(void *opaque, uint32_t addr)
  2498. {
  2499. IDEState *s = ((IDEState *)opaque)->cur_drive;
  2500. uint8_t *p;
  2501. int ret;
  2502. /* PIO data access allowed only when DRQ bit is set */
  2503. if (!(s->status & DRQ_STAT))
  2504. return 0;
  2505. p = s->data_ptr;
  2506. ret = cpu_to_le16(*(uint16_t *)p);
  2507. p += 2;
  2508. s->data_ptr = p;
  2509. if (p >= s->data_end)
  2510. s->end_transfer_func(s);
  2511. return ret;
  2512. }
  2513. static void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
  2514. {
  2515. IDEState *s = ((IDEState *)opaque)->cur_drive;
  2516. uint8_t *p;
  2517. /* PIO data access allowed only when DRQ bit is set */
  2518. if (!(s->status & DRQ_STAT))
  2519. return;
  2520. p = s->data_ptr;
  2521. *(uint32_t *)p = le32_to_cpu(val);
  2522. p += 4;
  2523. s->data_ptr = p;
  2524. if (p >= s->data_end)
  2525. s->end_transfer_func(s);
  2526. }
  2527. static uint32_t ide_data_readl(void *opaque, uint32_t addr)
  2528. {
  2529. IDEState *s = ((IDEState *)opaque)->cur_drive;
  2530. uint8_t *p;
  2531. int ret;
  2532. /* PIO data access allowed only when DRQ bit is set */
  2533. if (!(s->status & DRQ_STAT))
  2534. return 0;
  2535. p = s->data_ptr;
  2536. ret = cpu_to_le32(*(uint32_t *)p);
  2537. p += 4;
  2538. s->data_ptr = p;
  2539. if (p >= s->data_end)
  2540. s->end_transfer_func(s);
  2541. return ret;
  2542. }
  2543. static void ide_dummy_transfer_stop(IDEState *s)
  2544. {
  2545. s->data_ptr = s->io_buffer;
  2546. s->data_end = s->io_buffer;
  2547. s->io_buffer[0] = 0xff;
  2548. s->io_buffer[1] = 0xff;
  2549. s->io_buffer[2] = 0xff;
  2550. s->io_buffer[3] = 0xff;
  2551. }
  2552. static void ide_reset(IDEState *s)
  2553. {
  2554. if (s->is_cf)
  2555. s->mult_sectors = 0;
  2556. else
  2557. s->mult_sectors = MAX_MULT_SECTORS;
  2558. s->cur_drive = s;
  2559. s->select = 0xa0;
  2560. s->status = READY_STAT | SEEK_STAT;
  2561. ide_set_signature(s);
  2562. /* init the transfer handler so that 0xffff is returned on data
  2563. accesses */
  2564. s->end_transfer_func = ide_dummy_transfer_stop;
  2565. ide_dummy_transfer_stop(s);
  2566. s->media_changed = 0;
  2567. }
  2568. static void ide_init2(IDEState *ide_state,
  2569. BlockDriverState *hd0, BlockDriverState *hd1,
  2570. qemu_irq irq)
  2571. {
  2572. IDEState *s;
  2573. static int drive_serial = 1;
  2574. int i, cylinders, heads, secs;
  2575. uint64_t nb_sectors;
  2576. for(i = 0; i < 2; i++) {
  2577. s = ide_state + i;
  2578. s->io_buffer = qemu_memalign(512, IDE_DMA_BUF_SECTORS*512 + 4);
  2579. if (i == 0)
  2580. s->bs = hd0;
  2581. else
  2582. s->bs = hd1;
  2583. if (s->bs) {
  2584. bdrv_get_geometry(s->bs, &nb_sectors);
  2585. bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
  2586. s->cylinders = cylinders;
  2587. s->heads = heads;
  2588. s->sectors = secs;
  2589. s->nb_sectors = nb_sectors;
  2590. if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
  2591. s->is_cdrom = 1;
  2592. bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
  2593. }
  2594. }
  2595. s->drive_serial = drive_serial++;
  2596. strncpy(s->drive_serial_str, drive_get_serial(s->bs),
  2597. sizeof(s->drive_serial_str));
  2598. if (strlen(s->drive_serial_str) == 0)
  2599. snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
  2600. "QM%05d", s->drive_serial);
  2601. s->irq = irq;
  2602. s->sector_write_timer = qemu_new_timer(vm_clock,
  2603. ide_sector_write_timer_cb, s);
  2604. ide_reset(s);
  2605. }
  2606. }
  2607. static void ide_init_ioport(IDEState *ide_state, int iobase, int iobase2)
  2608. {
  2609. register_ioport_write(iobase, 8, 1, ide_ioport_write, ide_state);
  2610. register_ioport_read(iobase, 8, 1, ide_ioport_read, ide_state);
  2611. if (iobase2) {
  2612. register_ioport_read(iobase2, 1, 1, ide_status_read, ide_state);
  2613. register_ioport_write(iobase2, 1, 1, ide_cmd_write, ide_state);
  2614. }
  2615. /* data ports */
  2616. register_ioport_write(iobase, 2, 2, ide_data_writew, ide_state);
  2617. register_ioport_read(iobase, 2, 2, ide_data_readw, ide_state);
  2618. register_ioport_write(iobase, 4, 4, ide_data_writel, ide_state);
  2619. register_ioport_read(iobase, 4, 4, ide_data_readl, ide_state);
  2620. }
  2621. /* save per IDE drive data */
  2622. static void ide_save(QEMUFile* f, IDEState *s)
  2623. {
  2624. qemu_put_be32(f, s->mult_sectors);
  2625. qemu_put_be32(f, s->identify_set);
  2626. if (s->identify_set) {
  2627. qemu_put_buffer(f, (const uint8_t *)s->identify_data, 512);
  2628. }
  2629. qemu_put_8s(f, &s->feature);
  2630. qemu_put_8s(f, &s->error);
  2631. qemu_put_be32s(f, &s->nsector);
  2632. qemu_put_8s(f, &s->sector);
  2633. qemu_put_8s(f, &s->lcyl);
  2634. qemu_put_8s(f, &s->hcyl);
  2635. qemu_put_8s(f, &s->hob_feature);
  2636. qemu_put_8s(f, &s->hob_nsector);
  2637. qemu_put_8s(f, &s->hob_sector);
  2638. qemu_put_8s(f, &s->hob_lcyl);
  2639. qemu_put_8s(f, &s->hob_hcyl);
  2640. qemu_put_8s(f, &s->select);
  2641. qemu_put_8s(f, &s->status);
  2642. qemu_put_8s(f, &s->lba48);
  2643. qemu_put_8s(f, &s->sense_key);
  2644. qemu_put_8s(f, &s->asc);
  2645. /* XXX: if a transfer is pending, we do not save it yet */
  2646. }
  2647. /* load per IDE drive data */
  2648. static void ide_load(QEMUFile* f, IDEState *s, int version_id)
  2649. {
  2650. s->mult_sectors=qemu_get_be32(f);
  2651. s->identify_set=qemu_get_be32(f);
  2652. if (s->identify_set) {
  2653. qemu_get_buffer(f, (uint8_t *)s->identify_data, 512);
  2654. }
  2655. qemu_get_8s(f, &s->feature);
  2656. qemu_get_8s(f, &s->error);
  2657. qemu_get_be32s(f, &s->nsector);
  2658. qemu_get_8s(f, &s->sector);
  2659. qemu_get_8s(f, &s->lcyl);
  2660. qemu_get_8s(f, &s->hcyl);
  2661. qemu_get_8s(f, &s->hob_feature);
  2662. qemu_get_8s(f, &s->hob_nsector);
  2663. qemu_get_8s(f, &s->hob_sector);
  2664. qemu_get_8s(f, &s->hob_lcyl);
  2665. qemu_get_8s(f, &s->hob_hcyl);
  2666. qemu_get_8s(f, &s->select);
  2667. qemu_get_8s(f, &s->status);
  2668. qemu_get_8s(f, &s->lba48);
  2669. qemu_get_8s(f, &s->sense_key);
  2670. qemu_get_8s(f, &s->asc);
  2671. if (version_id == 3) {
  2672. qemu_get_8s(f, &s->cdrom_changed);
  2673. } else {
  2674. if (s->sense_key == SENSE_UNIT_ATTENTION &&
  2675. s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED)
  2676. s->cdrom_changed = 1;
  2677. }
  2678. /* XXX: if a transfer is pending, we do not save it yet */
  2679. }
  2680. /***********************************************************/
  2681. /* ISA IDE definitions */
  2682. void isa_ide_init(int iobase, int iobase2, qemu_irq irq,
  2683. BlockDriverState *hd0, BlockDriverState *hd1)
  2684. {
  2685. IDEState *ide_state;
  2686. ide_state = qemu_mallocz(sizeof(IDEState) * 2);
  2687. ide_init2(ide_state, hd0, hd1, irq);
  2688. ide_init_ioport(ide_state, iobase, iobase2);
  2689. }
  2690. /***********************************************************/
  2691. /* PCI IDE definitions */
  2692. static void cmd646_update_irq(PCIIDEState *d);
  2693. static void ide_map(PCIDevice *pci_dev, int region_num,
  2694. uint32_t addr, uint32_t size, int type)
  2695. {
  2696. PCIIDEState *d = (PCIIDEState *)pci_dev;
  2697. IDEState *ide_state;
  2698. if (region_num <= 3) {
  2699. ide_state = &d->ide_if[(region_num >> 1) * 2];
  2700. if (region_num & 1) {
  2701. register_ioport_read(addr + 2, 1, 1, ide_status_read, ide_state);
  2702. register_ioport_write(addr + 2, 1, 1, ide_cmd_write, ide_state);
  2703. } else {
  2704. register_ioport_write(addr, 8, 1, ide_ioport_write, ide_state);
  2705. register_ioport_read(addr, 8, 1, ide_ioport_read, ide_state);
  2706. /* data ports */
  2707. register_ioport_write(addr, 2, 2, ide_data_writew, ide_state);
  2708. register_ioport_read(addr, 2, 2, ide_data_readw, ide_state);
  2709. register_ioport_write(addr, 4, 4, ide_data_writel, ide_state);
  2710. register_ioport_read(addr, 4, 4, ide_data_readl, ide_state);
  2711. }
  2712. }
  2713. }
  2714. static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb)
  2715. {
  2716. BMDMAState *bm = s->bmdma;
  2717. if(!bm)
  2718. return;
  2719. bm->ide_if = s;
  2720. bm->dma_cb = dma_cb;
  2721. bm->cur_prd_last = 0;
  2722. bm->cur_prd_addr = 0;
  2723. bm->cur_prd_len = 0;
  2724. bm->sector_num = ide_get_sector(s);
  2725. bm->nsector = s->nsector;
  2726. if (bm->status & BM_STATUS_DMAING) {
  2727. bm->dma_cb(bm, 0);
  2728. }
  2729. }
  2730. static void ide_dma_restart(IDEState *s)
  2731. {
  2732. BMDMAState *bm = s->bmdma;
  2733. ide_set_sector(s, bm->sector_num);
  2734. s->io_buffer_index = 0;
  2735. s->io_buffer_size = 0;
  2736. s->nsector = bm->nsector;
  2737. bm->cur_addr = bm->addr;
  2738. bm->dma_cb = ide_write_dma_cb;
  2739. ide_dma_start(s, bm->dma_cb);
  2740. }
  2741. static void ide_dma_cancel(BMDMAState *bm)
  2742. {
  2743. if (bm->status & BM_STATUS_DMAING) {
  2744. bm->status &= ~BM_STATUS_DMAING;
  2745. /* cancel DMA request */
  2746. bm->ide_if = NULL;
  2747. bm->dma_cb = NULL;
  2748. if (bm->aiocb) {
  2749. #ifdef DEBUG_AIO
  2750. printf("aio_cancel\n");
  2751. #endif
  2752. bdrv_aio_cancel(bm->aiocb);
  2753. bm->aiocb = NULL;
  2754. }
  2755. }
  2756. }
  2757. static void bmdma_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
  2758. {
  2759. BMDMAState *bm = opaque;
  2760. #ifdef DEBUG_IDE
  2761. printf("%s: 0x%08x\n", __func__, val);
  2762. #endif
  2763. if (!(val & BM_CMD_START)) {
  2764. /* XXX: do it better */
  2765. ide_dma_cancel(bm);
  2766. bm->cmd = val & 0x09;
  2767. } else {
  2768. if (!(bm->status & BM_STATUS_DMAING)) {
  2769. bm->status |= BM_STATUS_DMAING;
  2770. /* start dma transfer if possible */
  2771. if (bm->dma_cb)
  2772. bm->dma_cb(bm, 0);
  2773. }
  2774. bm->cmd = val & 0x09;
  2775. }
  2776. }
  2777. static uint32_t bmdma_readb(void *opaque, uint32_t addr)
  2778. {
  2779. BMDMAState *bm = opaque;
  2780. PCIIDEState *pci_dev;
  2781. uint32_t val;
  2782. switch(addr & 3) {
  2783. case 0:
  2784. val = bm->cmd;
  2785. break;
  2786. case 1:
  2787. pci_dev = bm->pci_dev;
  2788. if (pci_dev->type == IDE_TYPE_CMD646) {
  2789. val = pci_dev->dev.config[MRDMODE];
  2790. } else {
  2791. val = 0xff;
  2792. }
  2793. break;
  2794. case 2:
  2795. val = bm->status;
  2796. break;
  2797. case 3:
  2798. pci_dev = bm->pci_dev;
  2799. if (pci_dev->type == IDE_TYPE_CMD646) {
  2800. if (bm == &pci_dev->bmdma[0])
  2801. val = pci_dev->dev.config[UDIDETCR0];
  2802. else
  2803. val = pci_dev->dev.config[UDIDETCR1];
  2804. } else {
  2805. val = 0xff;
  2806. }
  2807. break;
  2808. default:
  2809. val = 0xff;
  2810. break;
  2811. }
  2812. #ifdef DEBUG_IDE
  2813. printf("bmdma: readb 0x%02x : 0x%02x\n", addr, val);
  2814. #endif
  2815. return val;
  2816. }
  2817. static void bmdma_writeb(void *opaque, uint32_t addr, uint32_t val)
  2818. {
  2819. BMDMAState *bm = opaque;
  2820. PCIIDEState *pci_dev;
  2821. #ifdef DEBUG_IDE
  2822. printf("bmdma: writeb 0x%02x : 0x%02x\n", addr, val);
  2823. #endif
  2824. switch(addr & 3) {
  2825. case 1:
  2826. pci_dev = bm->pci_dev;
  2827. if (pci_dev->type == IDE_TYPE_CMD646) {
  2828. pci_dev->dev.config[MRDMODE] =
  2829. (pci_dev->dev.config[MRDMODE] & ~0x30) | (val & 0x30);
  2830. cmd646_update_irq(pci_dev);
  2831. }
  2832. break;
  2833. case 2:
  2834. bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06);
  2835. break;
  2836. case 3:
  2837. pci_dev = bm->pci_dev;
  2838. if (pci_dev->type == IDE_TYPE_CMD646) {
  2839. if (bm == &pci_dev->bmdma[0])
  2840. pci_dev->dev.config[UDIDETCR0] = val;
  2841. else
  2842. pci_dev->dev.config[UDIDETCR1] = val;
  2843. }
  2844. break;
  2845. }
  2846. }
  2847. static uint32_t bmdma_addr_readb(void *opaque, uint32_t addr)
  2848. {
  2849. BMDMAState *bm = opaque;
  2850. uint32_t val;
  2851. val = (bm->addr >> ((addr & 3) * 8)) & 0xff;
  2852. #ifdef DEBUG_IDE
  2853. printf("%s: 0x%08x\n", __func__, val);
  2854. #endif
  2855. return val;
  2856. }
  2857. static void bmdma_addr_writeb(void *opaque, uint32_t addr, uint32_t val)
  2858. {
  2859. BMDMAState *bm = opaque;
  2860. int shift = (addr & 3) * 8;
  2861. #ifdef DEBUG_IDE
  2862. printf("%s: 0x%08x\n", __func__, val);
  2863. #endif
  2864. bm->addr &= ~(0xFF << shift);
  2865. bm->addr |= ((val & 0xFF) << shift) & ~3;
  2866. bm->cur_addr = bm->addr;
  2867. }
  2868. static uint32_t bmdma_addr_readw(void *opaque, uint32_t addr)
  2869. {
  2870. BMDMAState *bm = opaque;
  2871. uint32_t val;
  2872. val = (bm->addr >> ((addr & 3) * 8)) & 0xffff;
  2873. #ifdef DEBUG_IDE
  2874. printf("%s: 0x%08x\n", __func__, val);
  2875. #endif
  2876. return val;
  2877. }
  2878. static void bmdma_addr_writew(void *opaque, uint32_t addr, uint32_t val)
  2879. {
  2880. BMDMAState *bm = opaque;
  2881. int shift = (addr & 3) * 8;
  2882. #ifdef DEBUG_IDE
  2883. printf("%s: 0x%08x\n", __func__, val);
  2884. #endif
  2885. bm->addr &= ~(0xFFFF << shift);
  2886. bm->addr |= ((val & 0xFFFF) << shift) & ~3;
  2887. bm->cur_addr = bm->addr;
  2888. }
  2889. static uint32_t bmdma_addr_readl(void *opaque, uint32_t addr)
  2890. {
  2891. BMDMAState *bm = opaque;
  2892. uint32_t val;
  2893. val = bm->addr;
  2894. #ifdef DEBUG_IDE
  2895. printf("%s: 0x%08x\n", __func__, val);
  2896. #endif
  2897. return val;
  2898. }
  2899. static void bmdma_addr_writel(void *opaque, uint32_t addr, uint32_t val)
  2900. {
  2901. BMDMAState *bm = opaque;
  2902. #ifdef DEBUG_IDE
  2903. printf("%s: 0x%08x\n", __func__, val);
  2904. #endif
  2905. bm->addr = val & ~3;
  2906. bm->cur_addr = bm->addr;
  2907. }
  2908. static void bmdma_map(PCIDevice *pci_dev, int region_num,
  2909. uint32_t addr, uint32_t size, int type)
  2910. {
  2911. PCIIDEState *d = (PCIIDEState *)pci_dev;
  2912. int i;
  2913. for(i = 0;i < 2; i++) {
  2914. BMDMAState *bm = &d->bmdma[i];
  2915. d->ide_if[2 * i].bmdma = bm;
  2916. d->ide_if[2 * i + 1].bmdma = bm;
  2917. bm->pci_dev = (PCIIDEState *)pci_dev;
  2918. qemu_add_vm_change_state_handler(ide_dma_restart_cb, bm);
  2919. register_ioport_write(addr, 1, 1, bmdma_cmd_writeb, bm);
  2920. register_ioport_write(addr + 1, 3, 1, bmdma_writeb, bm);
  2921. register_ioport_read(addr, 4, 1, bmdma_readb, bm);
  2922. register_ioport_write(addr + 4, 4, 1, bmdma_addr_writeb, bm);
  2923. register_ioport_read(addr + 4, 4, 1, bmdma_addr_readb, bm);
  2924. register_ioport_write(addr + 4, 4, 2, bmdma_addr_writew, bm);
  2925. register_ioport_read(addr + 4, 4, 2, bmdma_addr_readw, bm);
  2926. register_ioport_write(addr + 4, 4, 4, bmdma_addr_writel, bm);
  2927. register_ioport_read(addr + 4, 4, 4, bmdma_addr_readl, bm);
  2928. addr += 8;
  2929. }
  2930. }
  2931. static void pci_ide_save(QEMUFile* f, void *opaque)
  2932. {
  2933. PCIIDEState *d = opaque;
  2934. int i;
  2935. pci_device_save(&d->dev, f);
  2936. for(i = 0; i < 2; i++) {
  2937. BMDMAState *bm = &d->bmdma[i];
  2938. uint8_t ifidx;
  2939. qemu_put_8s(f, &bm->cmd);
  2940. qemu_put_8s(f, &bm->status);
  2941. qemu_put_be32s(f, &bm->addr);
  2942. qemu_put_sbe64s(f, &bm->sector_num);
  2943. qemu_put_be32s(f, &bm->nsector);
  2944. ifidx = bm->ide_if ? bm->ide_if - d->ide_if : 0;
  2945. qemu_put_8s(f, &ifidx);
  2946. /* XXX: if a transfer is pending, we do not save it yet */
  2947. }
  2948. /* per IDE interface data */
  2949. for(i = 0; i < 2; i++) {
  2950. IDEState *s = &d->ide_if[i * 2];
  2951. uint8_t drive1_selected;
  2952. qemu_put_8s(f, &s->cmd);
  2953. drive1_selected = (s->cur_drive != s);
  2954. qemu_put_8s(f, &drive1_selected);
  2955. }
  2956. /* per IDE drive data */
  2957. for(i = 0; i < 4; i++) {
  2958. ide_save(f, &d->ide_if[i]);
  2959. }
  2960. }
  2961. static int pci_ide_load(QEMUFile* f, void *opaque, int version_id)
  2962. {
  2963. PCIIDEState *d = opaque;
  2964. int ret, i;
  2965. if (version_id != 2 && version_id != 3)
  2966. return -EINVAL;
  2967. ret = pci_device_load(&d->dev, f);
  2968. if (ret < 0)
  2969. return ret;
  2970. for(i = 0; i < 2; i++) {
  2971. BMDMAState *bm = &d->bmdma[i];
  2972. uint8_t ifidx;
  2973. qemu_get_8s(f, &bm->cmd);
  2974. qemu_get_8s(f, &bm->status);
  2975. qemu_get_be32s(f, &bm->addr);
  2976. qemu_get_sbe64s(f, &bm->sector_num);
  2977. qemu_get_be32s(f, &bm->nsector);
  2978. qemu_get_8s(f, &ifidx);
  2979. bm->ide_if = &d->ide_if[ifidx];
  2980. /* XXX: if a transfer is pending, we do not save it yet */
  2981. }
  2982. /* per IDE interface data */
  2983. for(i = 0; i < 2; i++) {
  2984. IDEState *s = &d->ide_if[i * 2];
  2985. uint8_t drive1_selected;
  2986. qemu_get_8s(f, &s->cmd);
  2987. qemu_get_8s(f, &drive1_selected);
  2988. s->cur_drive = &d->ide_if[i * 2 + (drive1_selected != 0)];
  2989. }
  2990. /* per IDE drive data */
  2991. for(i = 0; i < 4; i++) {
  2992. ide_load(f, &d->ide_if[i], version_id);
  2993. }
  2994. return 0;
  2995. }
  2996. /* XXX: call it also when the MRDMODE is changed from the PCI config
  2997. registers */
  2998. static void cmd646_update_irq(PCIIDEState *d)
  2999. {
  3000. int pci_level;
  3001. pci_level = ((d->dev.config[MRDMODE] & MRDMODE_INTR_CH0) &&
  3002. !(d->dev.config[MRDMODE] & MRDMODE_BLK_CH0)) ||
  3003. ((d->dev.config[MRDMODE] & MRDMODE_INTR_CH1) &&
  3004. !(d->dev.config[MRDMODE] & MRDMODE_BLK_CH1));
  3005. qemu_set_irq(d->dev.irq[0], pci_level);
  3006. }
  3007. /* the PCI irq level is the logical OR of the two channels */
  3008. static void cmd646_set_irq(void *opaque, int channel, int level)
  3009. {
  3010. PCIIDEState *d = opaque;
  3011. int irq_mask;
  3012. irq_mask = MRDMODE_INTR_CH0 << channel;
  3013. if (level)
  3014. d->dev.config[MRDMODE] |= irq_mask;
  3015. else
  3016. d->dev.config[MRDMODE] &= ~irq_mask;
  3017. cmd646_update_irq(d);
  3018. }
  3019. static void cmd646_reset(void *opaque)
  3020. {
  3021. PCIIDEState *d = opaque;
  3022. unsigned int i;
  3023. for (i = 0; i < 2; i++)
  3024. ide_dma_cancel(&d->bmdma[i]);
  3025. }
  3026. /* CMD646 PCI IDE controller */
  3027. void pci_cmd646_ide_init(PCIBus *bus, BlockDriverState **hd_table,
  3028. int secondary_ide_enabled)
  3029. {
  3030. PCIIDEState *d;
  3031. uint8_t *pci_conf;
  3032. int i;
  3033. qemu_irq *irq;
  3034. d = (PCIIDEState *)pci_register_device(bus, "CMD646 IDE",
  3035. sizeof(PCIIDEState),
  3036. -1,
  3037. NULL, NULL);
  3038. d->type = IDE_TYPE_CMD646;
  3039. pci_conf = d->dev.config;
  3040. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_CMD);
  3041. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_CMD_646);
  3042. pci_conf[0x08] = 0x07; // IDE controller revision
  3043. pci_conf[0x09] = 0x8f;
  3044. pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
  3045. pci_conf[0x0e] = 0x00; // header_type
  3046. pci_conf[0x51] = 0x04; // enable IDE0
  3047. if (secondary_ide_enabled) {
  3048. /* XXX: if not enabled, really disable the seconday IDE controller */
  3049. pci_conf[0x51] |= 0x08; /* enable IDE1 */
  3050. }
  3051. pci_register_io_region((PCIDevice *)d, 0, 0x8,
  3052. PCI_ADDRESS_SPACE_IO, ide_map);
  3053. pci_register_io_region((PCIDevice *)d, 1, 0x4,
  3054. PCI_ADDRESS_SPACE_IO, ide_map);
  3055. pci_register_io_region((PCIDevice *)d, 2, 0x8,
  3056. PCI_ADDRESS_SPACE_IO, ide_map);
  3057. pci_register_io_region((PCIDevice *)d, 3, 0x4,
  3058. PCI_ADDRESS_SPACE_IO, ide_map);
  3059. pci_register_io_region((PCIDevice *)d, 4, 0x10,
  3060. PCI_ADDRESS_SPACE_IO, bmdma_map);
  3061. pci_conf[0x3d] = 0x01; // interrupt on pin 1
  3062. for(i = 0; i < 4; i++)
  3063. d->ide_if[i].pci_dev = (PCIDevice *)d;
  3064. irq = qemu_allocate_irqs(cmd646_set_irq, d, 2);
  3065. ide_init2(&d->ide_if[0], hd_table[0], hd_table[1], irq[0]);
  3066. ide_init2(&d->ide_if[2], hd_table[2], hd_table[3], irq[1]);
  3067. register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
  3068. qemu_register_reset(cmd646_reset, d);
  3069. cmd646_reset(d);
  3070. }
  3071. static void piix3_reset(void *opaque)
  3072. {
  3073. PCIIDEState *d = opaque;
  3074. uint8_t *pci_conf = d->dev.config;
  3075. int i;
  3076. for (i = 0; i < 2; i++)
  3077. ide_dma_cancel(&d->bmdma[i]);
  3078. pci_conf[0x04] = 0x00;
  3079. pci_conf[0x05] = 0x00;
  3080. pci_conf[0x06] = 0x80; /* FBC */
  3081. pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
  3082. pci_conf[0x20] = 0x01; /* BMIBA: 20-23h */
  3083. }
  3084. /* hd_table must contain 4 block drivers */
  3085. /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
  3086. void pci_piix3_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn,
  3087. qemu_irq *pic)
  3088. {
  3089. PCIIDEState *d;
  3090. uint8_t *pci_conf;
  3091. int i;
  3092. /* register a function 1 of PIIX3 */
  3093. d = (PCIIDEState *)pci_register_device(bus, "PIIX3 IDE",
  3094. sizeof(PCIIDEState),
  3095. devfn,
  3096. NULL, NULL);
  3097. d->type = IDE_TYPE_PIIX3;
  3098. pci_conf = d->dev.config;
  3099. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  3100. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_1);
  3101. pci_conf[0x09] = 0x80; // legacy ATA mode
  3102. pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
  3103. pci_conf[0x0e] = 0x00; // header_type
  3104. qemu_register_reset(piix3_reset, d);
  3105. piix3_reset(d);
  3106. pci_register_io_region((PCIDevice *)d, 4, 0x10,
  3107. PCI_ADDRESS_SPACE_IO, bmdma_map);
  3108. ide_init2(&d->ide_if[0], hd_table[0], hd_table[1], pic[14]);
  3109. ide_init2(&d->ide_if[2], hd_table[2], hd_table[3], pic[15]);
  3110. ide_init_ioport(&d->ide_if[0], 0x1f0, 0x3f6);
  3111. ide_init_ioport(&d->ide_if[2], 0x170, 0x376);
  3112. for (i = 0; i < 4; i++)
  3113. if (hd_table[i])
  3114. hd_table[i]->private = &d->dev;
  3115. register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
  3116. }
  3117. /* hd_table must contain 4 block drivers */
  3118. /* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
  3119. void pci_piix4_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn,
  3120. qemu_irq *pic)
  3121. {
  3122. PCIIDEState *d;
  3123. uint8_t *pci_conf;
  3124. /* register a function 1 of PIIX4 */
  3125. d = (PCIIDEState *)pci_register_device(bus, "PIIX4 IDE",
  3126. sizeof(PCIIDEState),
  3127. devfn,
  3128. NULL, NULL);
  3129. d->type = IDE_TYPE_PIIX4;
  3130. pci_conf = d->dev.config;
  3131. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  3132. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB);
  3133. pci_conf[0x09] = 0x80; // legacy ATA mode
  3134. pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
  3135. pci_conf[0x0e] = 0x00; // header_type
  3136. qemu_register_reset(piix3_reset, d);
  3137. piix3_reset(d);
  3138. pci_register_io_region((PCIDevice *)d, 4, 0x10,
  3139. PCI_ADDRESS_SPACE_IO, bmdma_map);
  3140. ide_init2(&d->ide_if[0], hd_table[0], hd_table[1], pic[14]);
  3141. ide_init2(&d->ide_if[2], hd_table[2], hd_table[3], pic[15]);
  3142. ide_init_ioport(&d->ide_if[0], 0x1f0, 0x3f6);
  3143. ide_init_ioport(&d->ide_if[2], 0x170, 0x376);
  3144. register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
  3145. }
  3146. #if defined(TARGET_PPC)
  3147. /***********************************************************/
  3148. /* MacIO based PowerPC IDE */
  3149. typedef struct MACIOIDEState {
  3150. IDEState ide_if[2];
  3151. BlockDriverAIOCB *aiocb;
  3152. } MACIOIDEState;
  3153. static void pmac_ide_atapi_transfer_cb(void *opaque, int ret)
  3154. {
  3155. DBDMA_io *io = opaque;
  3156. MACIOIDEState *m = io->opaque;
  3157. IDEState *s = m->ide_if->cur_drive;
  3158. if (ret < 0) {
  3159. m->aiocb = NULL;
  3160. qemu_sglist_destroy(&s->sg);
  3161. ide_atapi_io_error(s, ret);
  3162. io->dma_end(opaque);
  3163. return;
  3164. }
  3165. if (s->io_buffer_size > 0) {
  3166. m->aiocb = NULL;
  3167. qemu_sglist_destroy(&s->sg);
  3168. s->packet_transfer_size -= s->io_buffer_size;
  3169. s->io_buffer_index += s->io_buffer_size;
  3170. s->lba += s->io_buffer_index >> 11;
  3171. s->io_buffer_index &= 0x7ff;
  3172. }
  3173. if (s->packet_transfer_size <= 0)
  3174. ide_atapi_cmd_ok(s);
  3175. if (io->len == 0) {
  3176. io->dma_end(opaque);
  3177. return;
  3178. }
  3179. /* launch next transfer */
  3180. s->io_buffer_size = io->len;
  3181. qemu_sglist_init(&s->sg, io->len / TARGET_PAGE_SIZE + 1);
  3182. qemu_sglist_add(&s->sg, io->addr, io->len);
  3183. io->addr += io->len;
  3184. io->len = 0;
  3185. m->aiocb = dma_bdrv_read(s->bs, &s->sg,
  3186. (int64_t)(s->lba << 2) + (s->io_buffer_index >> 9),
  3187. pmac_ide_atapi_transfer_cb, io);
  3188. if (!m->aiocb) {
  3189. qemu_sglist_destroy(&s->sg);
  3190. /* Note: media not present is the most likely case */
  3191. ide_atapi_cmd_error(s, SENSE_NOT_READY,
  3192. ASC_MEDIUM_NOT_PRESENT);
  3193. io->dma_end(opaque);
  3194. return;
  3195. }
  3196. }
  3197. static void pmac_ide_transfer_cb(void *opaque, int ret)
  3198. {
  3199. DBDMA_io *io = opaque;
  3200. MACIOIDEState *m = io->opaque;
  3201. IDEState *s = m->ide_if->cur_drive;
  3202. int n;
  3203. int64_t sector_num;
  3204. if (ret < 0) {
  3205. m->aiocb = NULL;
  3206. qemu_sglist_destroy(&s->sg);
  3207. ide_dma_error(s);
  3208. io->dma_end(io);
  3209. return;
  3210. }
  3211. sector_num = ide_get_sector(s);
  3212. if (s->io_buffer_size > 0) {
  3213. m->aiocb = NULL;
  3214. qemu_sglist_destroy(&s->sg);
  3215. n = (s->io_buffer_size + 0x1ff) >> 9;
  3216. sector_num += n;
  3217. ide_set_sector(s, sector_num);
  3218. s->nsector -= n;
  3219. }
  3220. /* end of transfer ? */
  3221. if (s->nsector == 0) {
  3222. s->status = READY_STAT | SEEK_STAT;
  3223. ide_set_irq(s);
  3224. }
  3225. /* end of DMA ? */
  3226. if (io->len == 0) {
  3227. io->dma_end(io);
  3228. return;
  3229. }
  3230. /* launch next transfer */
  3231. s->io_buffer_index = 0;
  3232. s->io_buffer_size = io->len;
  3233. qemu_sglist_init(&s->sg, io->len / TARGET_PAGE_SIZE + 1);
  3234. qemu_sglist_add(&s->sg, io->addr, io->len);
  3235. io->addr += io->len;
  3236. io->len = 0;
  3237. if (s->is_read)
  3238. m->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num,
  3239. pmac_ide_transfer_cb, io);
  3240. else
  3241. m->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num,
  3242. pmac_ide_transfer_cb, io);
  3243. if (!m->aiocb)
  3244. pmac_ide_transfer_cb(io, -1);
  3245. }
  3246. static void pmac_ide_transfer(DBDMA_io *io)
  3247. {
  3248. MACIOIDEState *m = io->opaque;
  3249. IDEState *s = m->ide_if->cur_drive;
  3250. s->io_buffer_size = 0;
  3251. if (s->is_cdrom) {
  3252. pmac_ide_atapi_transfer_cb(io, 0);
  3253. return;
  3254. }
  3255. pmac_ide_transfer_cb(io, 0);
  3256. }
  3257. static void pmac_ide_flush(DBDMA_io *io)
  3258. {
  3259. MACIOIDEState *m = io->opaque;
  3260. if (m->aiocb)
  3261. qemu_aio_flush();
  3262. }
  3263. /* PowerMac IDE memory IO */
  3264. static void pmac_ide_writeb (void *opaque,
  3265. target_phys_addr_t addr, uint32_t val)
  3266. {
  3267. MACIOIDEState *d = opaque;
  3268. addr = (addr & 0xFFF) >> 4;
  3269. switch (addr) {
  3270. case 1 ... 7:
  3271. ide_ioport_write(d->ide_if, addr, val);
  3272. break;
  3273. case 8:
  3274. case 22:
  3275. ide_cmd_write(d->ide_if, 0, val);
  3276. break;
  3277. default:
  3278. break;
  3279. }
  3280. }
  3281. static uint32_t pmac_ide_readb (void *opaque,target_phys_addr_t addr)
  3282. {
  3283. uint8_t retval;
  3284. MACIOIDEState *d = opaque;
  3285. addr = (addr & 0xFFF) >> 4;
  3286. switch (addr) {
  3287. case 1 ... 7:
  3288. retval = ide_ioport_read(d->ide_if, addr);
  3289. break;
  3290. case 8:
  3291. case 22:
  3292. retval = ide_status_read(d->ide_if, 0);
  3293. break;
  3294. default:
  3295. retval = 0xFF;
  3296. break;
  3297. }
  3298. return retval;
  3299. }
  3300. static void pmac_ide_writew (void *opaque,
  3301. target_phys_addr_t addr, uint32_t val)
  3302. {
  3303. MACIOIDEState *d = opaque;
  3304. addr = (addr & 0xFFF) >> 4;
  3305. #ifdef TARGET_WORDS_BIGENDIAN
  3306. val = bswap16(val);
  3307. #endif
  3308. if (addr == 0) {
  3309. ide_data_writew(d->ide_if, 0, val);
  3310. }
  3311. }
  3312. static uint32_t pmac_ide_readw (void *opaque,target_phys_addr_t addr)
  3313. {
  3314. uint16_t retval;
  3315. MACIOIDEState *d = opaque;
  3316. addr = (addr & 0xFFF) >> 4;
  3317. if (addr == 0) {
  3318. retval = ide_data_readw(d->ide_if, 0);
  3319. } else {
  3320. retval = 0xFFFF;
  3321. }
  3322. #ifdef TARGET_WORDS_BIGENDIAN
  3323. retval = bswap16(retval);
  3324. #endif
  3325. return retval;
  3326. }
  3327. static void pmac_ide_writel (void *opaque,
  3328. target_phys_addr_t addr, uint32_t val)
  3329. {
  3330. MACIOIDEState *d = opaque;
  3331. addr = (addr & 0xFFF) >> 4;
  3332. #ifdef TARGET_WORDS_BIGENDIAN
  3333. val = bswap32(val);
  3334. #endif
  3335. if (addr == 0) {
  3336. ide_data_writel(d->ide_if, 0, val);
  3337. }
  3338. }
  3339. static uint32_t pmac_ide_readl (void *opaque,target_phys_addr_t addr)
  3340. {
  3341. uint32_t retval;
  3342. MACIOIDEState *d = opaque;
  3343. addr = (addr & 0xFFF) >> 4;
  3344. if (addr == 0) {
  3345. retval = ide_data_readl(d->ide_if, 0);
  3346. } else {
  3347. retval = 0xFFFFFFFF;
  3348. }
  3349. #ifdef TARGET_WORDS_BIGENDIAN
  3350. retval = bswap32(retval);
  3351. #endif
  3352. return retval;
  3353. }
  3354. static CPUWriteMemoryFunc *pmac_ide_write[] = {
  3355. pmac_ide_writeb,
  3356. pmac_ide_writew,
  3357. pmac_ide_writel,
  3358. };
  3359. static CPUReadMemoryFunc *pmac_ide_read[] = {
  3360. pmac_ide_readb,
  3361. pmac_ide_readw,
  3362. pmac_ide_readl,
  3363. };
  3364. static void pmac_ide_save(QEMUFile *f, void *opaque)
  3365. {
  3366. MACIOIDEState *d = opaque;
  3367. IDEState *s = d->ide_if;
  3368. uint8_t drive1_selected;
  3369. unsigned int i;
  3370. /* per IDE interface data */
  3371. qemu_put_8s(f, &s->cmd);
  3372. drive1_selected = (s->cur_drive != s);
  3373. qemu_put_8s(f, &drive1_selected);
  3374. /* per IDE drive data */
  3375. for(i = 0; i < 2; i++) {
  3376. ide_save(f, &s[i]);
  3377. }
  3378. }
  3379. static int pmac_ide_load(QEMUFile *f, void *opaque, int version_id)
  3380. {
  3381. MACIOIDEState *d = opaque;
  3382. IDEState *s = d->ide_if;
  3383. uint8_t drive1_selected;
  3384. unsigned int i;
  3385. if (version_id != 1 && version_id != 3)
  3386. return -EINVAL;
  3387. /* per IDE interface data */
  3388. qemu_get_8s(f, &s->cmd);
  3389. qemu_get_8s(f, &drive1_selected);
  3390. s->cur_drive = &s[(drive1_selected != 0)];
  3391. /* per IDE drive data */
  3392. for(i = 0; i < 2; i++) {
  3393. ide_load(f, &s[i], version_id);
  3394. }
  3395. return 0;
  3396. }
  3397. static void pmac_ide_reset(void *opaque)
  3398. {
  3399. MACIOIDEState *d = opaque;
  3400. IDEState *s = d->ide_if;
  3401. ide_reset(&s[0]);
  3402. ide_reset(&s[1]);
  3403. }
  3404. /* hd_table must contain 4 block drivers */
  3405. /* PowerMac uses memory mapped registers, not I/O. Return the memory
  3406. I/O index to access the ide. */
  3407. int pmac_ide_init (BlockDriverState **hd_table, qemu_irq irq,
  3408. void *dbdma, int channel, qemu_irq dma_irq)
  3409. {
  3410. MACIOIDEState *d;
  3411. int pmac_ide_memory;
  3412. d = qemu_mallocz(sizeof(MACIOIDEState));
  3413. ide_init2(d->ide_if, hd_table[0], hd_table[1], irq);
  3414. if (dbdma)
  3415. DBDMA_register_channel(dbdma, channel, dma_irq, pmac_ide_transfer, pmac_ide_flush, d);
  3416. pmac_ide_memory = cpu_register_io_memory(0, pmac_ide_read,
  3417. pmac_ide_write, d);
  3418. register_savevm("ide", 0, 3, pmac_ide_save, pmac_ide_load, d);
  3419. qemu_register_reset(pmac_ide_reset, d);
  3420. pmac_ide_reset(d);
  3421. return pmac_ide_memory;
  3422. }
  3423. #endif /* TARGET_PPC */
  3424. /***********************************************************/
  3425. /* MMIO based ide port
  3426. * This emulates IDE device connected directly to the CPU bus without
  3427. * dedicated ide controller, which is often seen on embedded boards.
  3428. */
  3429. typedef struct {
  3430. void *dev;
  3431. int shift;
  3432. } MMIOState;
  3433. static uint32_t mmio_ide_read (void *opaque, target_phys_addr_t addr)
  3434. {
  3435. MMIOState *s = (MMIOState*)opaque;
  3436. IDEState *ide = (IDEState*)s->dev;
  3437. addr >>= s->shift;
  3438. if (addr & 7)
  3439. return ide_ioport_read(ide, addr);
  3440. else
  3441. return ide_data_readw(ide, 0);
  3442. }
  3443. static void mmio_ide_write (void *opaque, target_phys_addr_t addr,
  3444. uint32_t val)
  3445. {
  3446. MMIOState *s = (MMIOState*)opaque;
  3447. IDEState *ide = (IDEState*)s->dev;
  3448. addr >>= s->shift;
  3449. if (addr & 7)
  3450. ide_ioport_write(ide, addr, val);
  3451. else
  3452. ide_data_writew(ide, 0, val);
  3453. }
  3454. static CPUReadMemoryFunc *mmio_ide_reads[] = {
  3455. mmio_ide_read,
  3456. mmio_ide_read,
  3457. mmio_ide_read,
  3458. };
  3459. static CPUWriteMemoryFunc *mmio_ide_writes[] = {
  3460. mmio_ide_write,
  3461. mmio_ide_write,
  3462. mmio_ide_write,
  3463. };
  3464. static uint32_t mmio_ide_status_read (void *opaque, target_phys_addr_t addr)
  3465. {
  3466. MMIOState *s= (MMIOState*)opaque;
  3467. IDEState *ide = (IDEState*)s->dev;
  3468. return ide_status_read(ide, 0);
  3469. }
  3470. static void mmio_ide_cmd_write (void *opaque, target_phys_addr_t addr,
  3471. uint32_t val)
  3472. {
  3473. MMIOState *s = (MMIOState*)opaque;
  3474. IDEState *ide = (IDEState*)s->dev;
  3475. ide_cmd_write(ide, 0, val);
  3476. }
  3477. static CPUReadMemoryFunc *mmio_ide_status[] = {
  3478. mmio_ide_status_read,
  3479. mmio_ide_status_read,
  3480. mmio_ide_status_read,
  3481. };
  3482. static CPUWriteMemoryFunc *mmio_ide_cmd[] = {
  3483. mmio_ide_cmd_write,
  3484. mmio_ide_cmd_write,
  3485. mmio_ide_cmd_write,
  3486. };
  3487. void mmio_ide_init (target_phys_addr_t membase, target_phys_addr_t membase2,
  3488. qemu_irq irq, int shift,
  3489. BlockDriverState *hd0, BlockDriverState *hd1)
  3490. {
  3491. MMIOState *s = qemu_mallocz(sizeof(MMIOState));
  3492. IDEState *ide = qemu_mallocz(sizeof(IDEState) * 2);
  3493. int mem1, mem2;
  3494. ide_init2(ide, hd0, hd1, irq);
  3495. s->dev = ide;
  3496. s->shift = shift;
  3497. mem1 = cpu_register_io_memory(0, mmio_ide_reads, mmio_ide_writes, s);
  3498. mem2 = cpu_register_io_memory(0, mmio_ide_status, mmio_ide_cmd, s);
  3499. cpu_register_physical_memory(membase, 16 << shift, mem1);
  3500. cpu_register_physical_memory(membase2, 2 << shift, mem2);
  3501. }
  3502. /***********************************************************/
  3503. /* CF-ATA Microdrive */
  3504. #define METADATA_SIZE 0x20
  3505. /* DSCM-1XXXX Microdrive hard disk with CF+ II / PCMCIA interface. */
  3506. struct md_s {
  3507. IDEState ide[2];
  3508. struct pcmcia_card_s card;
  3509. uint32_t attr_base;
  3510. uint32_t io_base;
  3511. /* Card state */
  3512. uint8_t opt;
  3513. uint8_t stat;
  3514. uint8_t pins;
  3515. uint8_t ctrl;
  3516. uint16_t io;
  3517. int cycle;
  3518. };
  3519. /* Register bitfields */
  3520. enum md_opt {
  3521. OPT_MODE_MMAP = 0,
  3522. OPT_MODE_IOMAP16 = 1,
  3523. OPT_MODE_IOMAP1 = 2,
  3524. OPT_MODE_IOMAP2 = 3,
  3525. OPT_MODE = 0x3f,
  3526. OPT_LEVIREQ = 0x40,
  3527. OPT_SRESET = 0x80,
  3528. };
  3529. enum md_cstat {
  3530. STAT_INT = 0x02,
  3531. STAT_PWRDWN = 0x04,
  3532. STAT_XE = 0x10,
  3533. STAT_IOIS8 = 0x20,
  3534. STAT_SIGCHG = 0x40,
  3535. STAT_CHANGED = 0x80,
  3536. };
  3537. enum md_pins {
  3538. PINS_MRDY = 0x02,
  3539. PINS_CRDY = 0x20,
  3540. };
  3541. enum md_ctrl {
  3542. CTRL_IEN = 0x02,
  3543. CTRL_SRST = 0x04,
  3544. };
  3545. static inline void md_interrupt_update(struct md_s *s)
  3546. {
  3547. if (!s->card.slot)
  3548. return;
  3549. qemu_set_irq(s->card.slot->irq,
  3550. !(s->stat & STAT_INT) && /* Inverted */
  3551. !(s->ctrl & (CTRL_IEN | CTRL_SRST)) &&
  3552. !(s->opt & OPT_SRESET));
  3553. }
  3554. static void md_set_irq(void *opaque, int irq, int level)
  3555. {
  3556. struct md_s *s = (struct md_s *) opaque;
  3557. if (level)
  3558. s->stat |= STAT_INT;
  3559. else
  3560. s->stat &= ~STAT_INT;
  3561. md_interrupt_update(s);
  3562. }
  3563. static void md_reset(struct md_s *s)
  3564. {
  3565. s->opt = OPT_MODE_MMAP;
  3566. s->stat = 0;
  3567. s->pins = 0;
  3568. s->cycle = 0;
  3569. s->ctrl = 0;
  3570. ide_reset(s->ide);
  3571. }
  3572. static uint8_t md_attr_read(void *opaque, uint32_t at)
  3573. {
  3574. struct md_s *s = (struct md_s *) opaque;
  3575. if (at < s->attr_base) {
  3576. if (at < s->card.cis_len)
  3577. return s->card.cis[at];
  3578. else
  3579. return 0x00;
  3580. }
  3581. at -= s->attr_base;
  3582. switch (at) {
  3583. case 0x00: /* Configuration Option Register */
  3584. return s->opt;
  3585. case 0x02: /* Card Configuration Status Register */
  3586. if (s->ctrl & CTRL_IEN)
  3587. return s->stat & ~STAT_INT;
  3588. else
  3589. return s->stat;
  3590. case 0x04: /* Pin Replacement Register */
  3591. return (s->pins & PINS_CRDY) | 0x0c;
  3592. case 0x06: /* Socket and Copy Register */
  3593. return 0x00;
  3594. #ifdef VERBOSE
  3595. default:
  3596. printf("%s: Bad attribute space register %02x\n", __FUNCTION__, at);
  3597. #endif
  3598. }
  3599. return 0;
  3600. }
  3601. static void md_attr_write(void *opaque, uint32_t at, uint8_t value)
  3602. {
  3603. struct md_s *s = (struct md_s *) opaque;
  3604. at -= s->attr_base;
  3605. switch (at) {
  3606. case 0x00: /* Configuration Option Register */
  3607. s->opt = value & 0xcf;
  3608. if (value & OPT_SRESET)
  3609. md_reset(s);
  3610. md_interrupt_update(s);
  3611. break;
  3612. case 0x02: /* Card Configuration Status Register */
  3613. if ((s->stat ^ value) & STAT_PWRDWN)
  3614. s->pins |= PINS_CRDY;
  3615. s->stat &= 0x82;
  3616. s->stat |= value & 0x74;
  3617. md_interrupt_update(s);
  3618. /* Word 170 in Identify Device must be equal to STAT_XE */
  3619. break;
  3620. case 0x04: /* Pin Replacement Register */
  3621. s->pins &= PINS_CRDY;
  3622. s->pins |= value & PINS_MRDY;
  3623. break;
  3624. case 0x06: /* Socket and Copy Register */
  3625. break;
  3626. default:
  3627. printf("%s: Bad attribute space register %02x\n", __FUNCTION__, at);
  3628. }
  3629. }
  3630. static uint16_t md_common_read(void *opaque, uint32_t at)
  3631. {
  3632. struct md_s *s = (struct md_s *) opaque;
  3633. uint16_t ret;
  3634. at -= s->io_base;
  3635. switch (s->opt & OPT_MODE) {
  3636. case OPT_MODE_MMAP:
  3637. if ((at & ~0x3ff) == 0x400)
  3638. at = 0;
  3639. break;
  3640. case OPT_MODE_IOMAP16:
  3641. at &= 0xf;
  3642. break;
  3643. case OPT_MODE_IOMAP1:
  3644. if ((at & ~0xf) == 0x3f0)
  3645. at -= 0x3e8;
  3646. else if ((at & ~0xf) == 0x1f0)
  3647. at -= 0x1f0;
  3648. break;
  3649. case OPT_MODE_IOMAP2:
  3650. if ((at & ~0xf) == 0x370)
  3651. at -= 0x368;
  3652. else if ((at & ~0xf) == 0x170)
  3653. at -= 0x170;
  3654. }
  3655. switch (at) {
  3656. case 0x0: /* Even RD Data */
  3657. case 0x8:
  3658. return ide_data_readw(s->ide, 0);
  3659. /* TODO: 8-bit accesses */
  3660. if (s->cycle)
  3661. ret = s->io >> 8;
  3662. else {
  3663. s->io = ide_data_readw(s->ide, 0);
  3664. ret = s->io & 0xff;
  3665. }
  3666. s->cycle = !s->cycle;
  3667. return ret;
  3668. case 0x9: /* Odd RD Data */
  3669. return s->io >> 8;
  3670. case 0xd: /* Error */
  3671. return ide_ioport_read(s->ide, 0x1);
  3672. case 0xe: /* Alternate Status */
  3673. if (s->ide->cur_drive->bs)
  3674. return s->ide->cur_drive->status;
  3675. else
  3676. return 0;
  3677. case 0xf: /* Device Address */
  3678. return 0xc2 | ((~s->ide->select << 2) & 0x3c);
  3679. default:
  3680. return ide_ioport_read(s->ide, at);
  3681. }
  3682. return 0;
  3683. }
  3684. static void md_common_write(void *opaque, uint32_t at, uint16_t value)
  3685. {
  3686. struct md_s *s = (struct md_s *) opaque;
  3687. at -= s->io_base;
  3688. switch (s->opt & OPT_MODE) {
  3689. case OPT_MODE_MMAP:
  3690. if ((at & ~0x3ff) == 0x400)
  3691. at = 0;
  3692. break;
  3693. case OPT_MODE_IOMAP16:
  3694. at &= 0xf;
  3695. break;
  3696. case OPT_MODE_IOMAP1:
  3697. if ((at & ~0xf) == 0x3f0)
  3698. at -= 0x3e8;
  3699. else if ((at & ~0xf) == 0x1f0)
  3700. at -= 0x1f0;
  3701. break;
  3702. case OPT_MODE_IOMAP2:
  3703. if ((at & ~0xf) == 0x370)
  3704. at -= 0x368;
  3705. else if ((at & ~0xf) == 0x170)
  3706. at -= 0x170;
  3707. }
  3708. switch (at) {
  3709. case 0x0: /* Even WR Data */
  3710. case 0x8:
  3711. ide_data_writew(s->ide, 0, value);
  3712. break;
  3713. /* TODO: 8-bit accesses */
  3714. if (s->cycle)
  3715. ide_data_writew(s->ide, 0, s->io | (value << 8));
  3716. else
  3717. s->io = value & 0xff;
  3718. s->cycle = !s->cycle;
  3719. break;
  3720. case 0x9:
  3721. s->io = value & 0xff;
  3722. s->cycle = !s->cycle;
  3723. break;
  3724. case 0xd: /* Features */
  3725. ide_ioport_write(s->ide, 0x1, value);
  3726. break;
  3727. case 0xe: /* Device Control */
  3728. s->ctrl = value;
  3729. if (value & CTRL_SRST)
  3730. md_reset(s);
  3731. md_interrupt_update(s);
  3732. break;
  3733. default:
  3734. if (s->stat & STAT_PWRDWN) {
  3735. s->pins |= PINS_CRDY;
  3736. s->stat &= ~STAT_PWRDWN;
  3737. }
  3738. ide_ioport_write(s->ide, at, value);
  3739. }
  3740. }
  3741. static void md_save(QEMUFile *f, void *opaque)
  3742. {
  3743. struct md_s *s = (struct md_s *) opaque;
  3744. int i;
  3745. uint8_t drive1_selected;
  3746. qemu_put_8s(f, &s->opt);
  3747. qemu_put_8s(f, &s->stat);
  3748. qemu_put_8s(f, &s->pins);
  3749. qemu_put_8s(f, &s->ctrl);
  3750. qemu_put_be16s(f, &s->io);
  3751. qemu_put_byte(f, s->cycle);
  3752. drive1_selected = (s->ide->cur_drive != s->ide);
  3753. qemu_put_8s(f, &s->ide->cmd);
  3754. qemu_put_8s(f, &drive1_selected);
  3755. for (i = 0; i < 2; i ++)
  3756. ide_save(f, &s->ide[i]);
  3757. }
  3758. static int md_load(QEMUFile *f, void *opaque, int version_id)
  3759. {
  3760. struct md_s *s = (struct md_s *) opaque;
  3761. int i;
  3762. uint8_t drive1_selected;
  3763. if (version_id != 0 && version_id != 3)
  3764. return -EINVAL;
  3765. qemu_get_8s(f, &s->opt);
  3766. qemu_get_8s(f, &s->stat);
  3767. qemu_get_8s(f, &s->pins);
  3768. qemu_get_8s(f, &s->ctrl);
  3769. qemu_get_be16s(f, &s->io);
  3770. s->cycle = qemu_get_byte(f);
  3771. qemu_get_8s(f, &s->ide->cmd);
  3772. qemu_get_8s(f, &drive1_selected);
  3773. s->ide->cur_drive = &s->ide[(drive1_selected != 0)];
  3774. for (i = 0; i < 2; i ++)
  3775. ide_load(f, &s->ide[i], version_id);
  3776. return 0;
  3777. }
  3778. static const uint8_t dscm1xxxx_cis[0x14a] = {
  3779. [0x000] = CISTPL_DEVICE, /* 5V Device Information */
  3780. [0x002] = 0x03, /* Tuple length = 4 bytes */
  3781. [0x004] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
  3782. [0x006] = 0x01, /* Size = 2K bytes */
  3783. [0x008] = CISTPL_ENDMARK,
  3784. [0x00a] = CISTPL_DEVICE_OC, /* Additional Device Information */
  3785. [0x00c] = 0x04, /* Tuple length = 4 byest */
  3786. [0x00e] = 0x03, /* Conditions: Ext = 0, Vcc 3.3V, MWAIT = 1 */
  3787. [0x010] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
  3788. [0x012] = 0x01, /* Size = 2K bytes */
  3789. [0x014] = CISTPL_ENDMARK,
  3790. [0x016] = CISTPL_JEDEC_C, /* JEDEC ID */
  3791. [0x018] = 0x02, /* Tuple length = 2 bytes */
  3792. [0x01a] = 0xdf, /* PC Card ATA with no Vpp required */
  3793. [0x01c] = 0x01,
  3794. [0x01e] = CISTPL_MANFID, /* Manufacture ID */
  3795. [0x020] = 0x04, /* Tuple length = 4 bytes */
  3796. [0x022] = 0xa4, /* TPLMID_MANF = 00a4 (IBM) */
  3797. [0x024] = 0x00,
  3798. [0x026] = 0x00, /* PLMID_CARD = 0000 */
  3799. [0x028] = 0x00,
  3800. [0x02a] = CISTPL_VERS_1, /* Level 1 Version */
  3801. [0x02c] = 0x12, /* Tuple length = 23 bytes */
  3802. [0x02e] = 0x04, /* Major Version = JEIDA 4.2 / PCMCIA 2.1 */
  3803. [0x030] = 0x01, /* Minor Version = 1 */
  3804. [0x032] = 'I',
  3805. [0x034] = 'B',
  3806. [0x036] = 'M',
  3807. [0x038] = 0x00,
  3808. [0x03a] = 'm',
  3809. [0x03c] = 'i',
  3810. [0x03e] = 'c',
  3811. [0x040] = 'r',
  3812. [0x042] = 'o',
  3813. [0x044] = 'd',
  3814. [0x046] = 'r',
  3815. [0x048] = 'i',
  3816. [0x04a] = 'v',
  3817. [0x04c] = 'e',
  3818. [0x04e] = 0x00,
  3819. [0x050] = CISTPL_ENDMARK,
  3820. [0x052] = CISTPL_FUNCID, /* Function ID */
  3821. [0x054] = 0x02, /* Tuple length = 2 bytes */
  3822. [0x056] = 0x04, /* TPLFID_FUNCTION = Fixed Disk */
  3823. [0x058] = 0x01, /* TPLFID_SYSINIT: POST = 1, ROM = 0 */
  3824. [0x05a] = CISTPL_FUNCE, /* Function Extension */
  3825. [0x05c] = 0x02, /* Tuple length = 2 bytes */
  3826. [0x05e] = 0x01, /* TPLFE_TYPE = Disk Device Interface */
  3827. [0x060] = 0x01, /* TPLFE_DATA = PC Card ATA Interface */
  3828. [0x062] = CISTPL_FUNCE, /* Function Extension */
  3829. [0x064] = 0x03, /* Tuple length = 3 bytes */
  3830. [0x066] = 0x02, /* TPLFE_TYPE = Basic PC Card ATA Interface */
  3831. [0x068] = 0x08, /* TPLFE_DATA: Rotating, Unique, Single */
  3832. [0x06a] = 0x0f, /* TPLFE_DATA: Sleep, Standby, Idle, Auto */
  3833. [0x06c] = CISTPL_CONFIG, /* Configuration */
  3834. [0x06e] = 0x05, /* Tuple length = 5 bytes */
  3835. [0x070] = 0x01, /* TPCC_RASZ = 2 bytes, TPCC_RMSZ = 1 byte */
  3836. [0x072] = 0x07, /* TPCC_LAST = 7 */
  3837. [0x074] = 0x00, /* TPCC_RADR = 0200 */
  3838. [0x076] = 0x02,
  3839. [0x078] = 0x0f, /* TPCC_RMSK = 200, 202, 204, 206 */
  3840. [0x07a] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3841. [0x07c] = 0x0b, /* Tuple length = 11 bytes */
  3842. [0x07e] = 0xc0, /* TPCE_INDX = Memory Mode, Default, Iface */
  3843. [0x080] = 0xc0, /* TPCE_IF = Memory, no BVDs, no WP, READY */
  3844. [0x082] = 0xa1, /* TPCE_FS = Vcc only, no I/O, Memory, Misc */
  3845. [0x084] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
  3846. [0x086] = 0x55, /* NomV: 5.0 V */
  3847. [0x088] = 0x4d, /* MinV: 4.5 V */
  3848. [0x08a] = 0x5d, /* MaxV: 5.5 V */
  3849. [0x08c] = 0x4e, /* Peakl: 450 mA */
  3850. [0x08e] = 0x08, /* TPCE_MS = 1 window, 1 byte, Host address */
  3851. [0x090] = 0x00, /* Window descriptor: Window length = 0 */
  3852. [0x092] = 0x20, /* TPCE_MI: support power down mode, RW */
  3853. [0x094] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3854. [0x096] = 0x06, /* Tuple length = 6 bytes */
  3855. [0x098] = 0x00, /* TPCE_INDX = Memory Mode, no Default */
  3856. [0x09a] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
  3857. [0x09c] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
  3858. [0x09e] = 0xb5, /* NomV: 3.3 V */
  3859. [0x0a0] = 0x1e,
  3860. [0x0a2] = 0x3e, /* Peakl: 350 mA */
  3861. [0x0a4] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3862. [0x0a6] = 0x0d, /* Tuple length = 13 bytes */
  3863. [0x0a8] = 0xc1, /* TPCE_INDX = I/O and Memory Mode, Default */
  3864. [0x0aa] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
  3865. [0x0ac] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
  3866. [0x0ae] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
  3867. [0x0b0] = 0x55, /* NomV: 5.0 V */
  3868. [0x0b2] = 0x4d, /* MinV: 4.5 V */
  3869. [0x0b4] = 0x5d, /* MaxV: 5.5 V */
  3870. [0x0b6] = 0x4e, /* Peakl: 450 mA */
  3871. [0x0b8] = 0x64, /* TPCE_IO = 16-byte boundary, 16/8 accesses */
  3872. [0x0ba] = 0xf0, /* TPCE_IR = MASK, Level, Pulse, Share */
  3873. [0x0bc] = 0xff, /* IRQ0..IRQ7 supported */
  3874. [0x0be] = 0xff, /* IRQ8..IRQ15 supported */
  3875. [0x0c0] = 0x20, /* TPCE_MI = support power down mode */
  3876. [0x0c2] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3877. [0x0c4] = 0x06, /* Tuple length = 6 bytes */
  3878. [0x0c6] = 0x01, /* TPCE_INDX = I/O and Memory Mode */
  3879. [0x0c8] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
  3880. [0x0ca] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
  3881. [0x0cc] = 0xb5, /* NomV: 3.3 V */
  3882. [0x0ce] = 0x1e,
  3883. [0x0d0] = 0x3e, /* Peakl: 350 mA */
  3884. [0x0d2] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3885. [0x0d4] = 0x12, /* Tuple length = 18 bytes */
  3886. [0x0d6] = 0xc2, /* TPCE_INDX = I/O Primary Mode */
  3887. [0x0d8] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
  3888. [0x0da] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
  3889. [0x0dc] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
  3890. [0x0de] = 0x55, /* NomV: 5.0 V */
  3891. [0x0e0] = 0x4d, /* MinV: 4.5 V */
  3892. [0x0e2] = 0x5d, /* MaxV: 5.5 V */
  3893. [0x0e4] = 0x4e, /* Peakl: 450 mA */
  3894. [0x0e6] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
  3895. [0x0e8] = 0x61, /* Range: 2 fields, 2 bytes addr, 1 byte len */
  3896. [0x0ea] = 0xf0, /* Field 1 address = 0x01f0 */
  3897. [0x0ec] = 0x01,
  3898. [0x0ee] = 0x07, /* Address block length = 8 */
  3899. [0x0f0] = 0xf6, /* Field 2 address = 0x03f6 */
  3900. [0x0f2] = 0x03,
  3901. [0x0f4] = 0x01, /* Address block length = 2 */
  3902. [0x0f6] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
  3903. [0x0f8] = 0x20, /* TPCE_MI = support power down mode */
  3904. [0x0fa] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3905. [0x0fc] = 0x06, /* Tuple length = 6 bytes */
  3906. [0x0fe] = 0x02, /* TPCE_INDX = I/O Primary Mode, no Default */
  3907. [0x100] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
  3908. [0x102] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
  3909. [0x104] = 0xb5, /* NomV: 3.3 V */
  3910. [0x106] = 0x1e,
  3911. [0x108] = 0x3e, /* Peakl: 350 mA */
  3912. [0x10a] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3913. [0x10c] = 0x12, /* Tuple length = 18 bytes */
  3914. [0x10e] = 0xc3, /* TPCE_INDX = I/O Secondary Mode, Default */
  3915. [0x110] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
  3916. [0x112] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
  3917. [0x114] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
  3918. [0x116] = 0x55, /* NomV: 5.0 V */
  3919. [0x118] = 0x4d, /* MinV: 4.5 V */
  3920. [0x11a] = 0x5d, /* MaxV: 5.5 V */
  3921. [0x11c] = 0x4e, /* Peakl: 450 mA */
  3922. [0x11e] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
  3923. [0x120] = 0x61, /* Range: 2 fields, 2 byte addr, 1 byte len */
  3924. [0x122] = 0x70, /* Field 1 address = 0x0170 */
  3925. [0x124] = 0x01,
  3926. [0x126] = 0x07, /* Address block length = 8 */
  3927. [0x128] = 0x76, /* Field 2 address = 0x0376 */
  3928. [0x12a] = 0x03,
  3929. [0x12c] = 0x01, /* Address block length = 2 */
  3930. [0x12e] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
  3931. [0x130] = 0x20, /* TPCE_MI = support power down mode */
  3932. [0x132] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
  3933. [0x134] = 0x06, /* Tuple length = 6 bytes */
  3934. [0x136] = 0x03, /* TPCE_INDX = I/O Secondary Mode */
  3935. [0x138] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
  3936. [0x13a] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
  3937. [0x13c] = 0xb5, /* NomV: 3.3 V */
  3938. [0x13e] = 0x1e,
  3939. [0x140] = 0x3e, /* Peakl: 350 mA */
  3940. [0x142] = CISTPL_NO_LINK, /* No Link */
  3941. [0x144] = 0x00, /* Tuple length = 0 bytes */
  3942. [0x146] = CISTPL_END, /* Tuple End */
  3943. };
  3944. static int dscm1xxxx_attach(void *opaque)
  3945. {
  3946. struct md_s *md = (struct md_s *) opaque;
  3947. md->card.attr_read = md_attr_read;
  3948. md->card.attr_write = md_attr_write;
  3949. md->card.common_read = md_common_read;
  3950. md->card.common_write = md_common_write;
  3951. md->card.io_read = md_common_read;
  3952. md->card.io_write = md_common_write;
  3953. md->attr_base = md->card.cis[0x74] | (md->card.cis[0x76] << 8);
  3954. md->io_base = 0x0;
  3955. md_reset(md);
  3956. md_interrupt_update(md);
  3957. md->card.slot->card_string = "DSCM-1xxxx Hitachi Microdrive";
  3958. return 0;
  3959. }
  3960. static int dscm1xxxx_detach(void *opaque)
  3961. {
  3962. struct md_s *md = (struct md_s *) opaque;
  3963. md_reset(md);
  3964. return 0;
  3965. }
  3966. struct pcmcia_card_s *dscm1xxxx_init(BlockDriverState *bdrv)
  3967. {
  3968. struct md_s *md = (struct md_s *) qemu_mallocz(sizeof(struct md_s));
  3969. md->card.state = md;
  3970. md->card.attach = dscm1xxxx_attach;
  3971. md->card.detach = dscm1xxxx_detach;
  3972. md->card.cis = dscm1xxxx_cis;
  3973. md->card.cis_len = sizeof(dscm1xxxx_cis);
  3974. ide_init2(md->ide, bdrv, 0, qemu_allocate_irqs(md_set_irq, md, 1)[0]);
  3975. md->ide->is_cf = 1;
  3976. md->ide->mdata_size = METADATA_SIZE;
  3977. md->ide->mdata_storage = (uint8_t *) qemu_mallocz(METADATA_SIZE);
  3978. register_savevm("microdrive", -1, 3, md_save, md_load, md);
  3979. return &md->card;
  3980. }