ide-tape.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889
  1. /*
  2. * IDE ATAPI streaming tape driver.
  3. *
  4. * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
  5. * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
  6. *
  7. * This driver was constructed as a student project in the software laboratory
  8. * of the faculty of electrical engineering in the Technion - Israel's
  9. * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
  10. *
  11. * It is hereby placed under the terms of the GNU general public license.
  12. * (See linux/COPYING).
  13. *
  14. * For a historical changelog see
  15. * Documentation/ide/ChangeLog.ide-tape.1995-2002
  16. */
  17. #define IDETAPE_VERSION "1.19"
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/major.h>
  28. #include <linux/errno.h>
  29. #include <linux/genhd.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/ide.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/completion.h>
  35. #include <linux/bitops.h>
  36. #include <linux/mutex.h>
  37. #include <scsi/scsi.h>
  38. #include <asm/byteorder.h>
  39. #include <asm/irq.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/io.h>
  42. #include <asm/unaligned.h>
  43. #include <linux/mtio.h>
  44. enum {
  45. /* output errors only */
  46. DBG_ERR = (1 << 0),
  47. /* output all sense key/asc */
  48. DBG_SENSE = (1 << 1),
  49. /* info regarding all chrdev-related procedures */
  50. DBG_CHRDEV = (1 << 2),
  51. /* all remaining procedures */
  52. DBG_PROCS = (1 << 3),
  53. /* buffer alloc info (pc_stack & rq_stack) */
  54. DBG_PCRQ_STACK = (1 << 4),
  55. };
  56. /* define to see debug info */
  57. #define IDETAPE_DEBUG_LOG 0
  58. #if IDETAPE_DEBUG_LOG
  59. #define debug_log(lvl, fmt, args...) \
  60. { \
  61. if (tape->debug_mask & lvl) \
  62. printk(KERN_INFO "ide-tape: " fmt, ## args); \
  63. }
  64. #else
  65. #define debug_log(lvl, fmt, args...) do {} while (0)
  66. #endif
  67. /**************************** Tunable parameters *****************************/
  68. /*
  69. * Pipelined mode parameters.
  70. *
  71. * We try to use the minimum number of stages which is enough to
  72. * keep the tape constantly streaming. To accomplish that, we implement
  73. * a feedback loop around the maximum number of stages:
  74. *
  75. * We start from MIN maximum stages (we will not even use MIN stages
  76. * if we don't need them), increment it by RATE*(MAX-MIN)
  77. * whenever we sense that the pipeline is empty, until we reach
  78. * the optimum value or until we reach MAX.
  79. *
  80. * Setting the following parameter to 0 is illegal: the pipelined mode
  81. * cannot be disabled (idetape_calculate_speeds() divides by
  82. * tape->max_stages.)
  83. */
  84. #define IDETAPE_MIN_PIPELINE_STAGES 1
  85. #define IDETAPE_MAX_PIPELINE_STAGES 400
  86. #define IDETAPE_INCREASE_STAGES_RATE 20
  87. /*
  88. * After each failed packet command we issue a request sense command
  89. * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
  90. *
  91. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  92. */
  93. #define IDETAPE_MAX_PC_RETRIES 3
  94. /*
  95. * With each packet command, we allocate a buffer of
  96. * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
  97. * commands (Not for READ/WRITE commands).
  98. */
  99. #define IDETAPE_PC_BUFFER_SIZE 256
  100. /*
  101. * In various places in the driver, we need to allocate storage
  102. * for packet commands and requests, which will remain valid while
  103. * we leave the driver to wait for an interrupt or a timeout event.
  104. */
  105. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  106. /*
  107. * Some drives (for example, Seagate STT3401A Travan) require a very long
  108. * timeout, because they don't return an interrupt or clear their busy bit
  109. * until after the command completes (even retension commands).
  110. */
  111. #define IDETAPE_WAIT_CMD (900*HZ)
  112. /*
  113. * The following parameter is used to select the point in the internal
  114. * tape fifo in which we will start to refill the buffer. Decreasing
  115. * the following parameter will improve the system's latency and
  116. * interactive response, while using a high value might improve system
  117. * throughput.
  118. */
  119. #define IDETAPE_FIFO_THRESHOLD 2
  120. /*
  121. * DSC polling parameters.
  122. *
  123. * Polling for DSC (a single bit in the status register) is a very
  124. * important function in ide-tape. There are two cases in which we
  125. * poll for DSC:
  126. *
  127. * 1. Before a read/write packet command, to ensure that we
  128. * can transfer data from/to the tape's data buffers, without
  129. * causing an actual media access. In case the tape is not
  130. * ready yet, we take out our request from the device
  131. * request queue, so that ide.c will service requests from
  132. * the other device on the same interface meanwhile.
  133. *
  134. * 2. After the successful initialization of a "media access
  135. * packet command", which is a command which can take a long
  136. * time to complete (it can be several seconds or even an hour).
  137. *
  138. * Again, we postpone our request in the middle to free the bus
  139. * for the other device. The polling frequency here should be
  140. * lower than the read/write frequency since those media access
  141. * commands are slow. We start from a "fast" frequency -
  142. * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
  143. * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
  144. * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
  145. *
  146. * We also set a timeout for the timer, in case something goes wrong.
  147. * The timeout should be longer then the maximum execution time of a
  148. * tape operation.
  149. */
  150. /*
  151. * DSC timings.
  152. */
  153. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  154. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  155. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  156. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  157. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  158. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  159. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  160. /*************************** End of tunable parameters ***********************/
  161. /*
  162. * Read/Write error simulation
  163. */
  164. #define SIMULATE_ERRORS 0
  165. /*
  166. * For general magnetic tape device compatibility.
  167. */
  168. typedef enum {
  169. idetape_direction_none,
  170. idetape_direction_read,
  171. idetape_direction_write
  172. } idetape_chrdev_direction_t;
  173. struct idetape_bh {
  174. u32 b_size;
  175. atomic_t b_count;
  176. struct idetape_bh *b_reqnext;
  177. char *b_data;
  178. };
  179. /*
  180. * Our view of a packet command.
  181. */
  182. typedef struct idetape_packet_command_s {
  183. u8 c[12]; /* Actual packet bytes */
  184. int retries; /* On each retry, we increment retries */
  185. int error; /* Error code */
  186. int request_transfer; /* Bytes to transfer */
  187. int actually_transferred; /* Bytes actually transferred */
  188. int buffer_size; /* Size of our data buffer */
  189. struct idetape_bh *bh;
  190. char *b_data;
  191. int b_count;
  192. u8 *buffer; /* Data buffer */
  193. u8 *current_position; /* Pointer into the above buffer */
  194. ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  195. u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
  196. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  197. } idetape_pc_t;
  198. /*
  199. * Packet command flag bits.
  200. */
  201. /* Set when an error is considered normal - We won't retry */
  202. #define PC_ABORT 0
  203. /* 1 When polling for DSC on a media access command */
  204. #define PC_WAIT_FOR_DSC 1
  205. /* 1 when we prefer to use DMA if possible */
  206. #define PC_DMA_RECOMMENDED 2
  207. /* 1 while DMA in progress */
  208. #define PC_DMA_IN_PROGRESS 3
  209. /* 1 when encountered problem during DMA */
  210. #define PC_DMA_ERROR 4
  211. /* Data direction */
  212. #define PC_WRITING 5
  213. /*
  214. * A pipeline stage.
  215. */
  216. typedef struct idetape_stage_s {
  217. struct request rq; /* The corresponding request */
  218. struct idetape_bh *bh; /* The data buffers */
  219. struct idetape_stage_s *next; /* Pointer to the next stage */
  220. } idetape_stage_t;
  221. /*
  222. * Most of our global data which we need to save even as we leave the
  223. * driver due to an interrupt or a timer event is stored in a variable
  224. * of type idetape_tape_t, defined below.
  225. */
  226. typedef struct ide_tape_obj {
  227. ide_drive_t *drive;
  228. ide_driver_t *driver;
  229. struct gendisk *disk;
  230. struct kref kref;
  231. /*
  232. * Since a typical character device operation requires more
  233. * than one packet command, we provide here enough memory
  234. * for the maximum of interconnected packet commands.
  235. * The packet commands are stored in the circular array pc_stack.
  236. * pc_stack_index points to the last used entry, and warps around
  237. * to the start when we get to the last array entry.
  238. *
  239. * pc points to the current processed packet command.
  240. *
  241. * failed_pc points to the last failed packet command, or contains
  242. * NULL if we do not need to retry any packet command. This is
  243. * required since an additional packet command is needed before the
  244. * retry, to get detailed information on what went wrong.
  245. */
  246. /* Current packet command */
  247. idetape_pc_t *pc;
  248. /* Last failed packet command */
  249. idetape_pc_t *failed_pc;
  250. /* Packet command stack */
  251. idetape_pc_t pc_stack[IDETAPE_PC_STACK];
  252. /* Next free packet command storage space */
  253. int pc_stack_index;
  254. struct request rq_stack[IDETAPE_PC_STACK];
  255. /* We implement a circular array */
  256. int rq_stack_index;
  257. /*
  258. * DSC polling variables.
  259. *
  260. * While polling for DSC we use postponed_rq to postpone the
  261. * current request so that ide.c will be able to service
  262. * pending requests on the other device. Note that at most
  263. * we will have only one DSC (usually data transfer) request
  264. * in the device request queue. Additional requests can be
  265. * queued in our internal pipeline, but they will be visible
  266. * to ide.c only one at a time.
  267. */
  268. struct request *postponed_rq;
  269. /* The time in which we started polling for DSC */
  270. unsigned long dsc_polling_start;
  271. /* Timer used to poll for dsc */
  272. struct timer_list dsc_timer;
  273. /* Read/Write dsc polling frequency */
  274. unsigned long best_dsc_rw_frequency;
  275. /* The current polling frequency */
  276. unsigned long dsc_polling_frequency;
  277. /* Maximum waiting time */
  278. unsigned long dsc_timeout;
  279. /*
  280. * Read position information
  281. */
  282. u8 partition;
  283. /* Current block */
  284. unsigned int first_frame_position;
  285. unsigned int last_frame_position;
  286. unsigned int blocks_in_buffer;
  287. /*
  288. * Last error information
  289. */
  290. u8 sense_key, asc, ascq;
  291. /*
  292. * Character device operation
  293. */
  294. unsigned int minor;
  295. /* device name */
  296. char name[4];
  297. /* Current character device data transfer direction */
  298. idetape_chrdev_direction_t chrdev_direction;
  299. /*
  300. * Device information
  301. */
  302. /* Usually 512 or 1024 bytes */
  303. unsigned short tape_block_size;
  304. int user_bs_factor;
  305. /* Copy of the tape's Capabilities and Mechanical Page */
  306. u8 caps[20];
  307. /*
  308. * Active data transfer request parameters.
  309. *
  310. * At most, there is only one ide-tape originated data transfer
  311. * request in the device request queue. This allows ide.c to
  312. * easily service requests from the other device when we
  313. * postpone our active request. In the pipelined operation
  314. * mode, we use our internal pipeline structure to hold
  315. * more data requests.
  316. *
  317. * The data buffer size is chosen based on the tape's
  318. * recommendation.
  319. */
  320. /* Pointer to the request which is waiting in the device request queue */
  321. struct request *active_data_request;
  322. /* Data buffer size (chosen based on the tape's recommendation */
  323. int stage_size;
  324. idetape_stage_t *merge_stage;
  325. int merge_stage_size;
  326. struct idetape_bh *bh;
  327. char *b_data;
  328. int b_count;
  329. /*
  330. * Pipeline parameters.
  331. *
  332. * To accomplish non-pipelined mode, we simply set the following
  333. * variables to zero (or NULL, where appropriate).
  334. */
  335. /* Number of currently used stages */
  336. int nr_stages;
  337. /* Number of pending stages */
  338. int nr_pending_stages;
  339. /* We will not allocate more than this number of stages */
  340. int max_stages, min_pipeline, max_pipeline;
  341. /* The first stage which will be removed from the pipeline */
  342. idetape_stage_t *first_stage;
  343. /* The currently active stage */
  344. idetape_stage_t *active_stage;
  345. /* Will be serviced after the currently active request */
  346. idetape_stage_t *next_stage;
  347. /* New requests will be added to the pipeline here */
  348. idetape_stage_t *last_stage;
  349. /* Optional free stage which we can use */
  350. idetape_stage_t *cache_stage;
  351. int pages_per_stage;
  352. /* Wasted space in each stage */
  353. int excess_bh_size;
  354. /* Status/Action flags: long for set_bit */
  355. unsigned long flags;
  356. /* protects the ide-tape queue */
  357. spinlock_t spinlock;
  358. /*
  359. * Measures average tape speed
  360. */
  361. unsigned long avg_time;
  362. int avg_size;
  363. int avg_speed;
  364. char vendor_id[10];
  365. char product_id[18];
  366. char firmware_revision[6];
  367. int firmware_revision_num;
  368. /* the door is currently locked */
  369. int door_locked;
  370. /* the tape hardware is write protected */
  371. char drv_write_prot;
  372. /* the tape is write protected (hardware or opened as read-only) */
  373. char write_prot;
  374. /*
  375. * Limit the number of times a request can
  376. * be postponed, to avoid an infinite postpone
  377. * deadlock.
  378. */
  379. /* request postpone count limit */
  380. int postpone_cnt;
  381. /*
  382. * Measures number of frames:
  383. *
  384. * 1. written/read to/from the driver pipeline (pipeline_head).
  385. * 2. written/read to/from the tape buffers (idetape_bh).
  386. * 3. written/read by the tape to/from the media (tape_head).
  387. */
  388. int pipeline_head;
  389. int buffer_head;
  390. int tape_head;
  391. int last_tape_head;
  392. /*
  393. * Speed control at the tape buffers input/output
  394. */
  395. unsigned long insert_time;
  396. int insert_size;
  397. int insert_speed;
  398. int max_insert_speed;
  399. int measure_insert_time;
  400. /*
  401. * Measure tape still time, in milliseconds
  402. */
  403. unsigned long tape_still_time_begin;
  404. int tape_still_time;
  405. /*
  406. * Speed regulation negative feedback loop
  407. */
  408. int speed_control;
  409. int pipeline_head_speed;
  410. int controlled_pipeline_head_speed;
  411. int uncontrolled_pipeline_head_speed;
  412. int controlled_last_pipeline_head;
  413. int uncontrolled_last_pipeline_head;
  414. unsigned long uncontrolled_pipeline_head_time;
  415. unsigned long controlled_pipeline_head_time;
  416. int controlled_previous_pipeline_head;
  417. int uncontrolled_previous_pipeline_head;
  418. unsigned long controlled_previous_head_time;
  419. unsigned long uncontrolled_previous_head_time;
  420. int restart_speed_control_req;
  421. u32 debug_mask;
  422. } idetape_tape_t;
  423. static DEFINE_MUTEX(idetape_ref_mutex);
  424. static struct class *idetape_sysfs_class;
  425. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  426. #define ide_tape_g(disk) \
  427. container_of((disk)->private_data, struct ide_tape_obj, driver)
  428. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  429. {
  430. struct ide_tape_obj *tape = NULL;
  431. mutex_lock(&idetape_ref_mutex);
  432. tape = ide_tape_g(disk);
  433. if (tape)
  434. kref_get(&tape->kref);
  435. mutex_unlock(&idetape_ref_mutex);
  436. return tape;
  437. }
  438. static void ide_tape_release(struct kref *);
  439. static void ide_tape_put(struct ide_tape_obj *tape)
  440. {
  441. mutex_lock(&idetape_ref_mutex);
  442. kref_put(&tape->kref, ide_tape_release);
  443. mutex_unlock(&idetape_ref_mutex);
  444. }
  445. /*
  446. * Tape door status
  447. */
  448. #define DOOR_UNLOCKED 0
  449. #define DOOR_LOCKED 1
  450. #define DOOR_EXPLICITLY_LOCKED 2
  451. /*
  452. * Tape flag bits values.
  453. */
  454. #define IDETAPE_IGNORE_DSC 0
  455. #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
  456. #define IDETAPE_BUSY 2 /* Device already opened */
  457. #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
  458. #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
  459. #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
  460. #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
  461. #define IDETAPE_READ_ERROR 7
  462. #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
  463. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  464. #define IDETAPE_MEDIUM_PRESENT 9
  465. /*
  466. * Some defines for the READ BUFFER command
  467. */
  468. #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
  469. /*
  470. * Some defines for the SPACE command
  471. */
  472. #define IDETAPE_SPACE_OVER_FILEMARK 1
  473. #define IDETAPE_SPACE_TO_EOD 3
  474. /*
  475. * Some defines for the LOAD UNLOAD command
  476. */
  477. #define IDETAPE_LU_LOAD_MASK 1
  478. #define IDETAPE_LU_RETENSION_MASK 2
  479. #define IDETAPE_LU_EOT_MASK 4
  480. /*
  481. * Special requests for our block device strategy routine.
  482. *
  483. * In order to service a character device command, we add special
  484. * requests to the tail of our block device request queue and wait
  485. * for their completion.
  486. */
  487. enum {
  488. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  489. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  490. REQ_IDETAPE_READ = (1 << 2),
  491. REQ_IDETAPE_WRITE = (1 << 3),
  492. REQ_IDETAPE_READ_BUFFER = (1 << 4),
  493. };
  494. /*
  495. * Error codes which are returned in rq->errors to the higher part
  496. * of the driver.
  497. */
  498. #define IDETAPE_ERROR_GENERAL 101
  499. #define IDETAPE_ERROR_FILEMARK 102
  500. #define IDETAPE_ERROR_EOD 103
  501. /*
  502. * The following is used to format the general configuration word of
  503. * the ATAPI IDENTIFY DEVICE command.
  504. */
  505. struct idetape_id_gcw {
  506. unsigned packet_size :2; /* Packet Size */
  507. unsigned reserved234 :3; /* Reserved */
  508. unsigned drq_type :2; /* Command packet DRQ type */
  509. unsigned removable :1; /* Removable media */
  510. unsigned device_type :5; /* Device type */
  511. unsigned reserved13 :1; /* Reserved */
  512. unsigned protocol :2; /* Protocol type */
  513. };
  514. /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
  515. #define IDETAPE_BLOCK_DESCRIPTOR 0
  516. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  517. /*
  518. * The variables below are used for the character device interface.
  519. * Additional state variables are defined in our ide_drive_t structure.
  520. */
  521. static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
  522. #define ide_tape_f(file) ((file)->private_data)
  523. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  524. {
  525. struct ide_tape_obj *tape = NULL;
  526. mutex_lock(&idetape_ref_mutex);
  527. tape = idetape_devs[i];
  528. if (tape)
  529. kref_get(&tape->kref);
  530. mutex_unlock(&idetape_ref_mutex);
  531. return tape;
  532. }
  533. /*
  534. * Function declarations
  535. *
  536. */
  537. static int idetape_chrdev_release (struct inode *inode, struct file *filp);
  538. static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
  539. /*
  540. * Too bad. The drive wants to send us data which we are not ready to accept.
  541. * Just throw it away.
  542. */
  543. static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
  544. {
  545. while (bcount--)
  546. (void) HWIF(drive)->INB(IDE_DATA_REG);
  547. }
  548. static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  549. {
  550. struct idetape_bh *bh = pc->bh;
  551. int count;
  552. while (bcount) {
  553. if (bh == NULL) {
  554. printk(KERN_ERR "ide-tape: bh == NULL in "
  555. "idetape_input_buffers\n");
  556. idetape_discard_data(drive, bcount);
  557. return;
  558. }
  559. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
  560. HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
  561. bcount -= count;
  562. atomic_add(count, &bh->b_count);
  563. if (atomic_read(&bh->b_count) == bh->b_size) {
  564. bh = bh->b_reqnext;
  565. if (bh)
  566. atomic_set(&bh->b_count, 0);
  567. }
  568. }
  569. pc->bh = bh;
  570. }
  571. static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  572. {
  573. struct idetape_bh *bh = pc->bh;
  574. int count;
  575. while (bcount) {
  576. if (bh == NULL) {
  577. printk(KERN_ERR "ide-tape: bh == NULL in "
  578. "idetape_output_buffers\n");
  579. return;
  580. }
  581. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  582. HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
  583. bcount -= count;
  584. pc->b_data += count;
  585. pc->b_count -= count;
  586. if (!pc->b_count) {
  587. pc->bh = bh = bh->b_reqnext;
  588. if (bh) {
  589. pc->b_data = bh->b_data;
  590. pc->b_count = atomic_read(&bh->b_count);
  591. }
  592. }
  593. }
  594. }
  595. static void idetape_update_buffers (idetape_pc_t *pc)
  596. {
  597. struct idetape_bh *bh = pc->bh;
  598. int count;
  599. unsigned int bcount = pc->actually_transferred;
  600. if (test_bit(PC_WRITING, &pc->flags))
  601. return;
  602. while (bcount) {
  603. if (bh == NULL) {
  604. printk(KERN_ERR "ide-tape: bh == NULL in "
  605. "idetape_update_buffers\n");
  606. return;
  607. }
  608. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  609. atomic_set(&bh->b_count, count);
  610. if (atomic_read(&bh->b_count) == bh->b_size)
  611. bh = bh->b_reqnext;
  612. bcount -= count;
  613. }
  614. pc->bh = bh;
  615. }
  616. /*
  617. * idetape_next_pc_storage returns a pointer to a place in which we can
  618. * safely store a packet command, even though we intend to leave the
  619. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  620. * commands is allocated at initialization time.
  621. */
  622. static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
  623. {
  624. idetape_tape_t *tape = drive->driver_data;
  625. debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
  626. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  627. tape->pc_stack_index=0;
  628. return (&tape->pc_stack[tape->pc_stack_index++]);
  629. }
  630. /*
  631. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  632. * Since we queue packet commands in the request queue, we need to
  633. * allocate a request, along with the allocation of a packet command.
  634. */
  635. /**************************************************************
  636. * *
  637. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  638. * followed later on by kfree(). -ml *
  639. * *
  640. **************************************************************/
  641. static struct request *idetape_next_rq_storage (ide_drive_t *drive)
  642. {
  643. idetape_tape_t *tape = drive->driver_data;
  644. debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
  645. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  646. tape->rq_stack_index=0;
  647. return (&tape->rq_stack[tape->rq_stack_index++]);
  648. }
  649. /*
  650. * idetape_init_pc initializes a packet command.
  651. */
  652. static void idetape_init_pc (idetape_pc_t *pc)
  653. {
  654. memset(pc->c, 0, 12);
  655. pc->retries = 0;
  656. pc->flags = 0;
  657. pc->request_transfer = 0;
  658. pc->buffer = pc->pc_buffer;
  659. pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
  660. pc->bh = NULL;
  661. pc->b_data = NULL;
  662. }
  663. /*
  664. * called on each failed packet command retry to analyze the request sense. We
  665. * currently do not utilize this information.
  666. */
  667. static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
  668. {
  669. idetape_tape_t *tape = drive->driver_data;
  670. idetape_pc_t *pc = tape->failed_pc;
  671. tape->sense_key = sense[2] & 0xF;
  672. tape->asc = sense[12];
  673. tape->ascq = sense[13];
  674. debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
  675. pc->c[0], tape->sense_key, tape->asc, tape->ascq);
  676. /* Correct pc->actually_transferred by asking the tape. */
  677. if (test_bit(PC_DMA_ERROR, &pc->flags)) {
  678. pc->actually_transferred = pc->request_transfer -
  679. tape->tape_block_size *
  680. be32_to_cpu(get_unaligned((u32 *)&sense[3]));
  681. idetape_update_buffers(pc);
  682. }
  683. /*
  684. * If error was the result of a zero-length read or write command,
  685. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  686. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  687. */
  688. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  689. /* length == 0 */
  690. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  691. if (tape->sense_key == 5) {
  692. /* don't report an error, everything's ok */
  693. pc->error = 0;
  694. /* don't retry read/write */
  695. set_bit(PC_ABORT, &pc->flags);
  696. }
  697. }
  698. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  699. pc->error = IDETAPE_ERROR_FILEMARK;
  700. set_bit(PC_ABORT, &pc->flags);
  701. }
  702. if (pc->c[0] == WRITE_6) {
  703. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  704. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  705. pc->error = IDETAPE_ERROR_EOD;
  706. set_bit(PC_ABORT, &pc->flags);
  707. }
  708. }
  709. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  710. if (tape->sense_key == 8) {
  711. pc->error = IDETAPE_ERROR_EOD;
  712. set_bit(PC_ABORT, &pc->flags);
  713. }
  714. if (!test_bit(PC_ABORT, &pc->flags) &&
  715. pc->actually_transferred)
  716. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  717. }
  718. }
  719. static void idetape_activate_next_stage(ide_drive_t *drive)
  720. {
  721. idetape_tape_t *tape = drive->driver_data;
  722. idetape_stage_t *stage = tape->next_stage;
  723. struct request *rq = &stage->rq;
  724. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  725. if (stage == NULL) {
  726. printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
  727. " existing stage\n");
  728. return;
  729. }
  730. rq->rq_disk = tape->disk;
  731. rq->buffer = NULL;
  732. rq->special = (void *)stage->bh;
  733. tape->active_data_request = rq;
  734. tape->active_stage = stage;
  735. tape->next_stage = stage->next;
  736. }
  737. /*
  738. * idetape_increase_max_pipeline_stages is a part of the feedback
  739. * loop which tries to find the optimum number of stages. In the
  740. * feedback loop, we are starting from a minimum maximum number of
  741. * stages, and if we sense that the pipeline is empty, we try to
  742. * increase it, until we reach the user compile time memory limit.
  743. */
  744. static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
  745. {
  746. idetape_tape_t *tape = drive->driver_data;
  747. int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
  748. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  749. tape->max_stages += max(increase, 1);
  750. tape->max_stages = max(tape->max_stages, tape->min_pipeline);
  751. tape->max_stages = min(tape->max_stages, tape->max_pipeline);
  752. }
  753. /*
  754. * idetape_kfree_stage calls kfree to completely free a stage, along with
  755. * its related buffers.
  756. */
  757. static void __idetape_kfree_stage (idetape_stage_t *stage)
  758. {
  759. struct idetape_bh *prev_bh, *bh = stage->bh;
  760. int size;
  761. while (bh != NULL) {
  762. if (bh->b_data != NULL) {
  763. size = (int) bh->b_size;
  764. while (size > 0) {
  765. free_page((unsigned long) bh->b_data);
  766. size -= PAGE_SIZE;
  767. bh->b_data += PAGE_SIZE;
  768. }
  769. }
  770. prev_bh = bh;
  771. bh = bh->b_reqnext;
  772. kfree(prev_bh);
  773. }
  774. kfree(stage);
  775. }
  776. static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
  777. {
  778. __idetape_kfree_stage(stage);
  779. }
  780. /*
  781. * idetape_remove_stage_head removes tape->first_stage from the pipeline.
  782. * The caller should avoid race conditions.
  783. */
  784. static void idetape_remove_stage_head (ide_drive_t *drive)
  785. {
  786. idetape_tape_t *tape = drive->driver_data;
  787. idetape_stage_t *stage;
  788. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  789. if (tape->first_stage == NULL) {
  790. printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
  791. return;
  792. }
  793. if (tape->active_stage == tape->first_stage) {
  794. printk(KERN_ERR "ide-tape: bug: Trying to free our active "
  795. "pipeline stage\n");
  796. return;
  797. }
  798. stage = tape->first_stage;
  799. tape->first_stage = stage->next;
  800. idetape_kfree_stage(tape, stage);
  801. tape->nr_stages--;
  802. if (tape->first_stage == NULL) {
  803. tape->last_stage = NULL;
  804. if (tape->next_stage != NULL)
  805. printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
  806. if (tape->nr_stages)
  807. printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
  808. }
  809. }
  810. /*
  811. * This will free all the pipeline stages starting from new_last_stage->next
  812. * to the end of the list, and point tape->last_stage to new_last_stage.
  813. */
  814. static void idetape_abort_pipeline(ide_drive_t *drive,
  815. idetape_stage_t *new_last_stage)
  816. {
  817. idetape_tape_t *tape = drive->driver_data;
  818. idetape_stage_t *stage = new_last_stage->next;
  819. idetape_stage_t *nstage;
  820. debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
  821. while (stage) {
  822. nstage = stage->next;
  823. idetape_kfree_stage(tape, stage);
  824. --tape->nr_stages;
  825. --tape->nr_pending_stages;
  826. stage = nstage;
  827. }
  828. if (new_last_stage)
  829. new_last_stage->next = NULL;
  830. tape->last_stage = new_last_stage;
  831. tape->next_stage = NULL;
  832. }
  833. /*
  834. * idetape_end_request is used to finish servicing a request, and to
  835. * insert a pending pipeline request into the main device queue.
  836. */
  837. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  838. {
  839. struct request *rq = HWGROUP(drive)->rq;
  840. idetape_tape_t *tape = drive->driver_data;
  841. unsigned long flags;
  842. int error;
  843. int remove_stage = 0;
  844. idetape_stage_t *active_stage;
  845. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  846. switch (uptodate) {
  847. case 0: error = IDETAPE_ERROR_GENERAL; break;
  848. case 1: error = 0; break;
  849. default: error = uptodate;
  850. }
  851. rq->errors = error;
  852. if (error)
  853. tape->failed_pc = NULL;
  854. if (!blk_special_request(rq)) {
  855. ide_end_request(drive, uptodate, nr_sects);
  856. return 0;
  857. }
  858. spin_lock_irqsave(&tape->spinlock, flags);
  859. /* The request was a pipelined data transfer request */
  860. if (tape->active_data_request == rq) {
  861. active_stage = tape->active_stage;
  862. tape->active_stage = NULL;
  863. tape->active_data_request = NULL;
  864. tape->nr_pending_stages--;
  865. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  866. remove_stage = 1;
  867. if (error) {
  868. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  869. if (error == IDETAPE_ERROR_EOD)
  870. idetape_abort_pipeline(drive, active_stage);
  871. }
  872. } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
  873. if (error == IDETAPE_ERROR_EOD) {
  874. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  875. idetape_abort_pipeline(drive, active_stage);
  876. }
  877. }
  878. if (tape->next_stage != NULL) {
  879. idetape_activate_next_stage(drive);
  880. /*
  881. * Insert the next request into the request queue.
  882. */
  883. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  884. } else if (!error) {
  885. idetape_increase_max_pipeline_stages(drive);
  886. }
  887. }
  888. ide_end_drive_cmd(drive, 0, 0);
  889. // blkdev_dequeue_request(rq);
  890. // drive->rq = NULL;
  891. // end_that_request_last(rq);
  892. if (remove_stage)
  893. idetape_remove_stage_head(drive);
  894. if (tape->active_data_request == NULL)
  895. clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  896. spin_unlock_irqrestore(&tape->spinlock, flags);
  897. return 0;
  898. }
  899. static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
  900. {
  901. idetape_tape_t *tape = drive->driver_data;
  902. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  903. if (!tape->pc->error) {
  904. idetape_analyze_error(drive, tape->pc->buffer);
  905. idetape_end_request(drive, 1, 0);
  906. } else {
  907. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
  908. idetape_end_request(drive, 0, 0);
  909. }
  910. return ide_stopped;
  911. }
  912. static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
  913. {
  914. idetape_init_pc(pc);
  915. pc->c[0] = REQUEST_SENSE;
  916. pc->c[4] = 20;
  917. pc->request_transfer = 20;
  918. pc->callback = &idetape_request_sense_callback;
  919. }
  920. static void idetape_init_rq(struct request *rq, u8 cmd)
  921. {
  922. memset(rq, 0, sizeof(*rq));
  923. rq->cmd_type = REQ_TYPE_SPECIAL;
  924. rq->cmd[0] = cmd;
  925. }
  926. /*
  927. * idetape_queue_pc_head generates a new packet command request in front
  928. * of the request queue, before the current request, so that it will be
  929. * processed immediately, on the next pass through the driver.
  930. *
  931. * idetape_queue_pc_head is called from the request handling part of
  932. * the driver (the "bottom" part). Safe storage for the request should
  933. * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
  934. * before calling idetape_queue_pc_head.
  935. *
  936. * Memory for those requests is pre-allocated at initialization time, and
  937. * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
  938. * space for the maximum possible number of inter-dependent packet commands.
  939. *
  940. * The higher level of the driver - The ioctl handler and the character
  941. * device handling functions should queue request to the lower level part
  942. * and wait for their completion using idetape_queue_pc_tail or
  943. * idetape_queue_rw_tail.
  944. */
  945. static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
  946. {
  947. struct ide_tape_obj *tape = drive->driver_data;
  948. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  949. rq->buffer = (char *) pc;
  950. rq->rq_disk = tape->disk;
  951. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  952. }
  953. /*
  954. * idetape_retry_pc is called when an error was detected during the
  955. * last packet command. We queue a request sense packet command in
  956. * the head of the request list.
  957. */
  958. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  959. {
  960. idetape_tape_t *tape = drive->driver_data;
  961. idetape_pc_t *pc;
  962. struct request *rq;
  963. (void)ide_read_error(drive);
  964. pc = idetape_next_pc_storage(drive);
  965. rq = idetape_next_rq_storage(drive);
  966. idetape_create_request_sense_cmd(pc);
  967. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  968. idetape_queue_pc_head(drive, pc, rq);
  969. return ide_stopped;
  970. }
  971. /*
  972. * idetape_postpone_request postpones the current request so that
  973. * ide.c will be able to service requests from another device on
  974. * the same hwgroup while we are polling for DSC.
  975. */
  976. static void idetape_postpone_request (ide_drive_t *drive)
  977. {
  978. idetape_tape_t *tape = drive->driver_data;
  979. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  980. tape->postponed_rq = HWGROUP(drive)->rq;
  981. ide_stall_queue(drive, tape->dsc_polling_frequency);
  982. }
  983. typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
  984. /*
  985. * This is the usual interrupt handler which will be called during a packet
  986. * command. We will transfer some of the data (as requested by the drive) and
  987. * will re-point interrupt handler to us. When data transfer is finished, we
  988. * will act according to the algorithm described before
  989. * idetape_issue_packet_command.
  990. */
  991. static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
  992. {
  993. ide_hwif_t *hwif = drive->hwif;
  994. idetape_tape_t *tape = drive->driver_data;
  995. idetape_pc_t *pc = tape->pc;
  996. xfer_func_t *xferfunc;
  997. idetape_io_buf *iobuf;
  998. unsigned int temp;
  999. #if SIMULATE_ERRORS
  1000. static int error_sim_count = 0;
  1001. #endif
  1002. u16 bcount;
  1003. u8 stat, ireason;
  1004. debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
  1005. /* Clear the interrupt */
  1006. stat = ide_read_status(drive);
  1007. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1008. if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
  1009. /*
  1010. * A DMA error is sometimes expected. For example,
  1011. * if the tape is crossing a filemark during a
  1012. * READ command, it will issue an irq and position
  1013. * itself before the filemark, so that only a partial
  1014. * data transfer will occur (which causes the DMA
  1015. * error). In that case, we will later ask the tape
  1016. * how much bytes of the original request were
  1017. * actually transferred (we can't receive that
  1018. * information from the DMA engine on most chipsets).
  1019. */
  1020. /*
  1021. * On the contrary, a DMA error is never expected;
  1022. * it usually indicates a hardware error or abort.
  1023. * If the tape crosses a filemark during a READ
  1024. * command, it will issue an irq and position itself
  1025. * after the filemark (not before). Only a partial
  1026. * data transfer will occur, but no DMA error.
  1027. * (AS, 19 Apr 2001)
  1028. */
  1029. set_bit(PC_DMA_ERROR, &pc->flags);
  1030. } else {
  1031. pc->actually_transferred = pc->request_transfer;
  1032. idetape_update_buffers(pc);
  1033. }
  1034. debug_log(DBG_PROCS, "DMA finished\n");
  1035. }
  1036. /* No more interrupts */
  1037. if ((stat & DRQ_STAT) == 0) {
  1038. debug_log(DBG_SENSE, "Packet command completed, %d bytes"
  1039. " transferred\n", pc->actually_transferred);
  1040. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1041. local_irq_enable();
  1042. #if SIMULATE_ERRORS
  1043. if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
  1044. (++error_sim_count % 100) == 0) {
  1045. printk(KERN_INFO "ide-tape: %s: simulating error\n",
  1046. tape->name);
  1047. stat |= ERR_STAT;
  1048. }
  1049. #endif
  1050. if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
  1051. stat &= ~ERR_STAT;
  1052. if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
  1053. /* Error detected */
  1054. debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
  1055. if (pc->c[0] == REQUEST_SENSE) {
  1056. printk(KERN_ERR "ide-tape: I/O error in request"
  1057. " sense command\n");
  1058. return ide_do_reset(drive);
  1059. }
  1060. debug_log(DBG_ERR, "[cmd %x]: check condition\n",
  1061. pc->c[0]);
  1062. /* Retry operation */
  1063. return idetape_retry_pc(drive);
  1064. }
  1065. pc->error = 0;
  1066. if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
  1067. (stat & SEEK_STAT) == 0) {
  1068. /* Media access command */
  1069. tape->dsc_polling_start = jiffies;
  1070. tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
  1071. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  1072. /* Allow ide.c to handle other requests */
  1073. idetape_postpone_request(drive);
  1074. return ide_stopped;
  1075. }
  1076. if (tape->failed_pc == pc)
  1077. tape->failed_pc = NULL;
  1078. /* Command finished - Call the callback function */
  1079. return pc->callback(drive);
  1080. }
  1081. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1082. printk(KERN_ERR "ide-tape: The tape wants to issue more "
  1083. "interrupts in DMA mode\n");
  1084. printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
  1085. ide_dma_off(drive);
  1086. return ide_do_reset(drive);
  1087. }
  1088. /* Get the number of bytes to transfer on this interrupt. */
  1089. bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
  1090. hwif->INB(IDE_BCOUNTL_REG);
  1091. ireason = hwif->INB(IDE_IREASON_REG);
  1092. if (ireason & CD) {
  1093. printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
  1094. return ide_do_reset(drive);
  1095. }
  1096. if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
  1097. /* Hopefully, we will never get here */
  1098. printk(KERN_ERR "ide-tape: We wanted to %s, ",
  1099. (ireason & IO) ? "Write" : "Read");
  1100. printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
  1101. (ireason & IO) ? "Read" : "Write");
  1102. return ide_do_reset(drive);
  1103. }
  1104. if (!test_bit(PC_WRITING, &pc->flags)) {
  1105. /* Reading - Check that we have enough space */
  1106. temp = pc->actually_transferred + bcount;
  1107. if (temp > pc->request_transfer) {
  1108. if (temp > pc->buffer_size) {
  1109. printk(KERN_ERR "ide-tape: The tape wants to "
  1110. "send us more data than expected "
  1111. "- discarding data\n");
  1112. idetape_discard_data(drive, bcount);
  1113. ide_set_handler(drive, &idetape_pc_intr,
  1114. IDETAPE_WAIT_CMD, NULL);
  1115. return ide_started;
  1116. }
  1117. debug_log(DBG_SENSE, "The tape wants to send us more "
  1118. "data than expected - allowing transfer\n");
  1119. }
  1120. iobuf = &idetape_input_buffers;
  1121. xferfunc = hwif->atapi_input_bytes;
  1122. } else {
  1123. iobuf = &idetape_output_buffers;
  1124. xferfunc = hwif->atapi_output_bytes;
  1125. }
  1126. if (pc->bh)
  1127. iobuf(drive, pc, bcount);
  1128. else
  1129. xferfunc(drive, pc->current_position, bcount);
  1130. /* Update the current position */
  1131. pc->actually_transferred += bcount;
  1132. pc->current_position += bcount;
  1133. debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
  1134. pc->c[0], bcount);
  1135. /* And set the interrupt handler again */
  1136. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1137. return ide_started;
  1138. }
  1139. /*
  1140. * Packet Command Interface
  1141. *
  1142. * The current Packet Command is available in tape->pc, and will not
  1143. * change until we finish handling it. Each packet command is associated
  1144. * with a callback function that will be called when the command is
  1145. * finished.
  1146. *
  1147. * The handling will be done in three stages:
  1148. *
  1149. * 1. idetape_issue_packet_command will send the packet command to the
  1150. * drive, and will set the interrupt handler to idetape_pc_intr.
  1151. *
  1152. * 2. On each interrupt, idetape_pc_intr will be called. This step
  1153. * will be repeated until the device signals us that no more
  1154. * interrupts will be issued.
  1155. *
  1156. * 3. ATAPI Tape media access commands have immediate status with a
  1157. * delayed process. In case of a successful initiation of a
  1158. * media access packet command, the DSC bit will be set when the
  1159. * actual execution of the command is finished.
  1160. * Since the tape drive will not issue an interrupt, we have to
  1161. * poll for this event. In this case, we define the request as
  1162. * "low priority request" by setting rq_status to
  1163. * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
  1164. * the driver.
  1165. *
  1166. * ide.c will then give higher priority to requests which
  1167. * originate from the other device, until will change rq_status
  1168. * to RQ_ACTIVE.
  1169. *
  1170. * 4. When the packet command is finished, it will be checked for errors.
  1171. *
  1172. * 5. In case an error was found, we queue a request sense packet
  1173. * command in front of the request queue and retry the operation
  1174. * up to IDETAPE_MAX_PC_RETRIES times.
  1175. *
  1176. * 6. In case no error was found, or we decided to give up and not
  1177. * to retry again, the callback function will be called and then
  1178. * we will handle the next request.
  1179. *
  1180. */
  1181. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  1182. {
  1183. ide_hwif_t *hwif = drive->hwif;
  1184. idetape_tape_t *tape = drive->driver_data;
  1185. idetape_pc_t *pc = tape->pc;
  1186. int retries = 100;
  1187. ide_startstop_t startstop;
  1188. u8 ireason;
  1189. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  1190. printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
  1191. return startstop;
  1192. }
  1193. ireason = hwif->INB(IDE_IREASON_REG);
  1194. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  1195. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
  1196. "a packet command, retrying\n");
  1197. udelay(100);
  1198. ireason = hwif->INB(IDE_IREASON_REG);
  1199. if (retries == 0) {
  1200. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
  1201. "issuing a packet command, ignoring\n");
  1202. ireason |= CD;
  1203. ireason &= ~IO;
  1204. }
  1205. }
  1206. if ((ireason & CD) == 0 || (ireason & IO)) {
  1207. printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
  1208. "a packet command\n");
  1209. return ide_do_reset(drive);
  1210. }
  1211. /* Set the interrupt routine */
  1212. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1213. #ifdef CONFIG_BLK_DEV_IDEDMA
  1214. /* Begin DMA, if necessary */
  1215. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
  1216. hwif->dma_start(drive);
  1217. #endif
  1218. /* Send the actual packet */
  1219. HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
  1220. return ide_started;
  1221. }
  1222. static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
  1223. {
  1224. ide_hwif_t *hwif = drive->hwif;
  1225. idetape_tape_t *tape = drive->driver_data;
  1226. int dma_ok = 0;
  1227. u16 bcount;
  1228. if (tape->pc->c[0] == REQUEST_SENSE &&
  1229. pc->c[0] == REQUEST_SENSE) {
  1230. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  1231. "Two request sense in serial were issued\n");
  1232. }
  1233. if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
  1234. tape->failed_pc = pc;
  1235. /* Set the current packet command */
  1236. tape->pc = pc;
  1237. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  1238. test_bit(PC_ABORT, &pc->flags)) {
  1239. /*
  1240. * We will "abort" retrying a packet command in case
  1241. * a legitimate error code was received (crossing a
  1242. * filemark, or end of the media, for example).
  1243. */
  1244. if (!test_bit(PC_ABORT, &pc->flags)) {
  1245. if (!(pc->c[0] == TEST_UNIT_READY &&
  1246. tape->sense_key == 2 && tape->asc == 4 &&
  1247. (tape->ascq == 1 || tape->ascq == 8))) {
  1248. printk(KERN_ERR "ide-tape: %s: I/O error, "
  1249. "pc = %2x, key = %2x, "
  1250. "asc = %2x, ascq = %2x\n",
  1251. tape->name, pc->c[0],
  1252. tape->sense_key, tape->asc,
  1253. tape->ascq);
  1254. }
  1255. /* Giving up */
  1256. pc->error = IDETAPE_ERROR_GENERAL;
  1257. }
  1258. tape->failed_pc = NULL;
  1259. return pc->callback(drive);
  1260. }
  1261. debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
  1262. pc->retries++;
  1263. /* We haven't transferred any data yet */
  1264. pc->actually_transferred = 0;
  1265. pc->current_position = pc->buffer;
  1266. /* Request to transfer the entire buffer at once */
  1267. bcount = pc->request_transfer;
  1268. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
  1269. printk(KERN_WARNING "ide-tape: DMA disabled, "
  1270. "reverting to PIO\n");
  1271. ide_dma_off(drive);
  1272. }
  1273. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  1274. dma_ok = !hwif->dma_setup(drive);
  1275. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
  1276. IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
  1277. if (dma_ok) /* Will begin DMA later */
  1278. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1279. if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
  1280. ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
  1281. IDETAPE_WAIT_CMD, NULL);
  1282. return ide_started;
  1283. } else {
  1284. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  1285. return idetape_transfer_pc(drive);
  1286. }
  1287. }
  1288. /*
  1289. * General packet command callback function.
  1290. */
  1291. static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
  1292. {
  1293. idetape_tape_t *tape = drive->driver_data;
  1294. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1295. idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
  1296. return ide_stopped;
  1297. }
  1298. /*
  1299. * A mode sense command is used to "sense" tape parameters.
  1300. */
  1301. static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
  1302. {
  1303. idetape_init_pc(pc);
  1304. pc->c[0] = MODE_SENSE;
  1305. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  1306. pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
  1307. pc->c[2] = page_code;
  1308. /*
  1309. * Changed pc->c[3] to 0 (255 will at best return unused info).
  1310. *
  1311. * For SCSI this byte is defined as subpage instead of high byte
  1312. * of length and some IDE drives seem to interpret it this way
  1313. * and return an error when 255 is used.
  1314. */
  1315. pc->c[3] = 0;
  1316. pc->c[4] = 255; /* (We will just discard data in that case) */
  1317. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  1318. pc->request_transfer = 12;
  1319. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  1320. pc->request_transfer = 24;
  1321. else
  1322. pc->request_transfer = 50;
  1323. pc->callback = &idetape_pc_callback;
  1324. }
  1325. static void idetape_calculate_speeds(ide_drive_t *drive)
  1326. {
  1327. idetape_tape_t *tape = drive->driver_data;
  1328. if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
  1329. tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
  1330. tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
  1331. tape->controlled_last_pipeline_head = tape->pipeline_head;
  1332. tape->controlled_pipeline_head_time = jiffies;
  1333. }
  1334. if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
  1335. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
  1336. else if (time_after(jiffies, tape->controlled_previous_head_time))
  1337. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
  1338. if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
  1339. /* -1 for read mode error recovery */
  1340. if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
  1341. tape->uncontrolled_pipeline_head_time = jiffies;
  1342. tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
  1343. }
  1344. } else {
  1345. tape->uncontrolled_previous_head_time = jiffies;
  1346. tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
  1347. if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
  1348. tape->uncontrolled_pipeline_head_time = jiffies;
  1349. }
  1350. }
  1351. tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
  1352. if (tape->speed_control == 1) {
  1353. if (tape->nr_pending_stages >= tape->max_stages / 2)
  1354. tape->max_insert_speed = tape->pipeline_head_speed +
  1355. (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
  1356. else
  1357. tape->max_insert_speed = 500 +
  1358. (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
  1359. if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
  1360. tape->max_insert_speed = 5000;
  1361. } else
  1362. tape->max_insert_speed = tape->speed_control;
  1363. tape->max_insert_speed = max(tape->max_insert_speed, 500);
  1364. }
  1365. static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
  1366. {
  1367. idetape_tape_t *tape = drive->driver_data;
  1368. idetape_pc_t *pc = tape->pc;
  1369. u8 stat;
  1370. stat = ide_read_status(drive);
  1371. if (stat & SEEK_STAT) {
  1372. if (stat & ERR_STAT) {
  1373. /* Error detected */
  1374. if (pc->c[0] != TEST_UNIT_READY)
  1375. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  1376. tape->name);
  1377. /* Retry operation */
  1378. return idetape_retry_pc(drive);
  1379. }
  1380. pc->error = 0;
  1381. if (tape->failed_pc == pc)
  1382. tape->failed_pc = NULL;
  1383. } else {
  1384. pc->error = IDETAPE_ERROR_GENERAL;
  1385. tape->failed_pc = NULL;
  1386. }
  1387. return pc->callback(drive);
  1388. }
  1389. static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
  1390. {
  1391. idetape_tape_t *tape = drive->driver_data;
  1392. struct request *rq = HWGROUP(drive)->rq;
  1393. int blocks = tape->pc->actually_transferred / tape->tape_block_size;
  1394. tape->avg_size += blocks * tape->tape_block_size;
  1395. tape->insert_size += blocks * tape->tape_block_size;
  1396. if (tape->insert_size > 1024 * 1024)
  1397. tape->measure_insert_time = 1;
  1398. if (tape->measure_insert_time) {
  1399. tape->measure_insert_time = 0;
  1400. tape->insert_time = jiffies;
  1401. tape->insert_size = 0;
  1402. }
  1403. if (time_after(jiffies, tape->insert_time))
  1404. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  1405. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  1406. tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
  1407. tape->avg_size = 0;
  1408. tape->avg_time = jiffies;
  1409. }
  1410. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1411. tape->first_frame_position += blocks;
  1412. rq->current_nr_sectors -= blocks;
  1413. if (!tape->pc->error)
  1414. idetape_end_request(drive, 1, 0);
  1415. else
  1416. idetape_end_request(drive, tape->pc->error, 0);
  1417. return ide_stopped;
  1418. }
  1419. static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1420. {
  1421. idetape_init_pc(pc);
  1422. pc->c[0] = READ_6;
  1423. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  1424. pc->c[1] = 1;
  1425. pc->callback = &idetape_rw_callback;
  1426. pc->bh = bh;
  1427. atomic_set(&bh->b_count, 0);
  1428. pc->buffer = NULL;
  1429. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  1430. if (pc->request_transfer == tape->stage_size)
  1431. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1432. }
  1433. static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1434. {
  1435. int size = 32768;
  1436. struct idetape_bh *p = bh;
  1437. idetape_init_pc(pc);
  1438. pc->c[0] = READ_BUFFER;
  1439. pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
  1440. pc->c[7] = size >> 8;
  1441. pc->c[8] = size & 0xff;
  1442. pc->callback = &idetape_pc_callback;
  1443. pc->bh = bh;
  1444. atomic_set(&bh->b_count, 0);
  1445. pc->buffer = NULL;
  1446. while (p) {
  1447. atomic_set(&p->b_count, 0);
  1448. p = p->b_reqnext;
  1449. }
  1450. pc->request_transfer = pc->buffer_size = size;
  1451. }
  1452. static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1453. {
  1454. idetape_init_pc(pc);
  1455. pc->c[0] = WRITE_6;
  1456. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  1457. pc->c[1] = 1;
  1458. pc->callback = &idetape_rw_callback;
  1459. set_bit(PC_WRITING, &pc->flags);
  1460. pc->bh = bh;
  1461. pc->b_data = bh->b_data;
  1462. pc->b_count = atomic_read(&bh->b_count);
  1463. pc->buffer = NULL;
  1464. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  1465. if (pc->request_transfer == tape->stage_size)
  1466. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1467. }
  1468. /*
  1469. * idetape_do_request is our request handling function.
  1470. */
  1471. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  1472. struct request *rq, sector_t block)
  1473. {
  1474. idetape_tape_t *tape = drive->driver_data;
  1475. idetape_pc_t *pc = NULL;
  1476. struct request *postponed_rq = tape->postponed_rq;
  1477. u8 stat;
  1478. debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
  1479. " current_nr_sectors: %d\n",
  1480. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  1481. if (!blk_special_request(rq)) {
  1482. /*
  1483. * We do not support buffer cache originated requests.
  1484. */
  1485. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  1486. "request queue (%d)\n", drive->name, rq->cmd_type);
  1487. ide_end_request(drive, 0, 0);
  1488. return ide_stopped;
  1489. }
  1490. /*
  1491. * Retry a failed packet command
  1492. */
  1493. if (tape->failed_pc != NULL &&
  1494. tape->pc->c[0] == REQUEST_SENSE) {
  1495. return idetape_issue_packet_command(drive, tape->failed_pc);
  1496. }
  1497. if (postponed_rq != NULL)
  1498. if (rq != postponed_rq) {
  1499. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  1500. "Two DSC requests were queued\n");
  1501. idetape_end_request(drive, 0, 0);
  1502. return ide_stopped;
  1503. }
  1504. tape->postponed_rq = NULL;
  1505. /*
  1506. * If the tape is still busy, postpone our request and service
  1507. * the other device meanwhile.
  1508. */
  1509. stat = ide_read_status(drive);
  1510. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  1511. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1512. if (drive->post_reset == 1) {
  1513. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1514. drive->post_reset = 0;
  1515. }
  1516. if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
  1517. tape->measure_insert_time = 1;
  1518. if (time_after(jiffies, tape->insert_time))
  1519. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  1520. idetape_calculate_speeds(drive);
  1521. if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
  1522. (stat & SEEK_STAT) == 0) {
  1523. if (postponed_rq == NULL) {
  1524. tape->dsc_polling_start = jiffies;
  1525. tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
  1526. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  1527. } else if (time_after(jiffies, tape->dsc_timeout)) {
  1528. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  1529. tape->name);
  1530. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1531. idetape_media_access_finished(drive);
  1532. return ide_stopped;
  1533. } else {
  1534. return ide_do_reset(drive);
  1535. }
  1536. } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
  1537. tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
  1538. idetape_postpone_request(drive);
  1539. return ide_stopped;
  1540. }
  1541. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1542. tape->buffer_head++;
  1543. tape->postpone_cnt = 0;
  1544. pc = idetape_next_pc_storage(drive);
  1545. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1546. goto out;
  1547. }
  1548. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1549. tape->buffer_head++;
  1550. tape->postpone_cnt = 0;
  1551. pc = idetape_next_pc_storage(drive);
  1552. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1553. goto out;
  1554. }
  1555. if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
  1556. tape->postpone_cnt = 0;
  1557. pc = idetape_next_pc_storage(drive);
  1558. idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1559. goto out;
  1560. }
  1561. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  1562. pc = (idetape_pc_t *) rq->buffer;
  1563. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  1564. rq->cmd[0] |= REQ_IDETAPE_PC2;
  1565. goto out;
  1566. }
  1567. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1568. idetape_media_access_finished(drive);
  1569. return ide_stopped;
  1570. }
  1571. BUG();
  1572. out:
  1573. return idetape_issue_packet_command(drive, pc);
  1574. }
  1575. /*
  1576. * Pipeline related functions
  1577. */
  1578. static inline int idetape_pipeline_active (idetape_tape_t *tape)
  1579. {
  1580. int rc1, rc2;
  1581. rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1582. rc2 = (tape->active_data_request != NULL);
  1583. return rc1;
  1584. }
  1585. /*
  1586. * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
  1587. * stage, along with all the necessary small buffers which together make
  1588. * a buffer of size tape->stage_size (or a bit more). We attempt to
  1589. * combine sequential pages as much as possible.
  1590. *
  1591. * Returns a pointer to the new allocated stage, or NULL if we
  1592. * can't (or don't want to) allocate a stage.
  1593. *
  1594. * Pipeline stages are optional and are used to increase performance.
  1595. * If we can't allocate them, we'll manage without them.
  1596. */
  1597. static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
  1598. {
  1599. idetape_stage_t *stage;
  1600. struct idetape_bh *prev_bh, *bh;
  1601. int pages = tape->pages_per_stage;
  1602. char *b_data = NULL;
  1603. if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
  1604. return NULL;
  1605. stage->next = NULL;
  1606. bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1607. if (bh == NULL)
  1608. goto abort;
  1609. bh->b_reqnext = NULL;
  1610. if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  1611. goto abort;
  1612. if (clear)
  1613. memset(bh->b_data, 0, PAGE_SIZE);
  1614. bh->b_size = PAGE_SIZE;
  1615. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1616. while (--pages) {
  1617. if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  1618. goto abort;
  1619. if (clear)
  1620. memset(b_data, 0, PAGE_SIZE);
  1621. if (bh->b_data == b_data + PAGE_SIZE) {
  1622. bh->b_size += PAGE_SIZE;
  1623. bh->b_data -= PAGE_SIZE;
  1624. if (full)
  1625. atomic_add(PAGE_SIZE, &bh->b_count);
  1626. continue;
  1627. }
  1628. if (b_data == bh->b_data + bh->b_size) {
  1629. bh->b_size += PAGE_SIZE;
  1630. if (full)
  1631. atomic_add(PAGE_SIZE, &bh->b_count);
  1632. continue;
  1633. }
  1634. prev_bh = bh;
  1635. if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
  1636. free_page((unsigned long) b_data);
  1637. goto abort;
  1638. }
  1639. bh->b_reqnext = NULL;
  1640. bh->b_data = b_data;
  1641. bh->b_size = PAGE_SIZE;
  1642. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1643. prev_bh->b_reqnext = bh;
  1644. }
  1645. bh->b_size -= tape->excess_bh_size;
  1646. if (full)
  1647. atomic_sub(tape->excess_bh_size, &bh->b_count);
  1648. return stage;
  1649. abort:
  1650. __idetape_kfree_stage(stage);
  1651. return NULL;
  1652. }
  1653. static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
  1654. {
  1655. idetape_stage_t *cache_stage = tape->cache_stage;
  1656. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1657. if (tape->nr_stages >= tape->max_stages)
  1658. return NULL;
  1659. if (cache_stage != NULL) {
  1660. tape->cache_stage = NULL;
  1661. return cache_stage;
  1662. }
  1663. return __idetape_kmalloc_stage(tape, 0, 0);
  1664. }
  1665. static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
  1666. {
  1667. struct idetape_bh *bh = tape->bh;
  1668. int count;
  1669. int ret = 0;
  1670. while (n) {
  1671. if (bh == NULL) {
  1672. printk(KERN_ERR "ide-tape: bh == NULL in "
  1673. "idetape_copy_stage_from_user\n");
  1674. return 1;
  1675. }
  1676. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
  1677. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
  1678. ret = 1;
  1679. n -= count;
  1680. atomic_add(count, &bh->b_count);
  1681. buf += count;
  1682. if (atomic_read(&bh->b_count) == bh->b_size) {
  1683. bh = bh->b_reqnext;
  1684. if (bh)
  1685. atomic_set(&bh->b_count, 0);
  1686. }
  1687. }
  1688. tape->bh = bh;
  1689. return ret;
  1690. }
  1691. static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
  1692. {
  1693. struct idetape_bh *bh = tape->bh;
  1694. int count;
  1695. int ret = 0;
  1696. while (n) {
  1697. if (bh == NULL) {
  1698. printk(KERN_ERR "ide-tape: bh == NULL in "
  1699. "idetape_copy_stage_to_user\n");
  1700. return 1;
  1701. }
  1702. count = min(tape->b_count, n);
  1703. if (copy_to_user(buf, tape->b_data, count))
  1704. ret = 1;
  1705. n -= count;
  1706. tape->b_data += count;
  1707. tape->b_count -= count;
  1708. buf += count;
  1709. if (!tape->b_count) {
  1710. tape->bh = bh = bh->b_reqnext;
  1711. if (bh) {
  1712. tape->b_data = bh->b_data;
  1713. tape->b_count = atomic_read(&bh->b_count);
  1714. }
  1715. }
  1716. }
  1717. return ret;
  1718. }
  1719. static void idetape_init_merge_stage (idetape_tape_t *tape)
  1720. {
  1721. struct idetape_bh *bh = tape->merge_stage->bh;
  1722. tape->bh = bh;
  1723. if (tape->chrdev_direction == idetape_direction_write)
  1724. atomic_set(&bh->b_count, 0);
  1725. else {
  1726. tape->b_data = bh->b_data;
  1727. tape->b_count = atomic_read(&bh->b_count);
  1728. }
  1729. }
  1730. static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
  1731. {
  1732. struct idetape_bh *tmp;
  1733. tmp = stage->bh;
  1734. stage->bh = tape->merge_stage->bh;
  1735. tape->merge_stage->bh = tmp;
  1736. idetape_init_merge_stage(tape);
  1737. }
  1738. /*
  1739. * idetape_add_stage_tail adds a new stage at the end of the pipeline.
  1740. */
  1741. static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
  1742. {
  1743. idetape_tape_t *tape = drive->driver_data;
  1744. unsigned long flags;
  1745. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1746. spin_lock_irqsave(&tape->spinlock, flags);
  1747. stage->next = NULL;
  1748. if (tape->last_stage != NULL)
  1749. tape->last_stage->next=stage;
  1750. else
  1751. tape->first_stage = tape->next_stage=stage;
  1752. tape->last_stage = stage;
  1753. if (tape->next_stage == NULL)
  1754. tape->next_stage = tape->last_stage;
  1755. tape->nr_stages++;
  1756. tape->nr_pending_stages++;
  1757. spin_unlock_irqrestore(&tape->spinlock, flags);
  1758. }
  1759. /*
  1760. * idetape_wait_for_request installs a completion in a pending request
  1761. * and sleeps until it is serviced.
  1762. *
  1763. * The caller should ensure that the request will not be serviced
  1764. * before we install the completion (usually by disabling interrupts).
  1765. */
  1766. static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
  1767. {
  1768. DECLARE_COMPLETION_ONSTACK(wait);
  1769. idetape_tape_t *tape = drive->driver_data;
  1770. if (rq == NULL || !blk_special_request(rq)) {
  1771. printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
  1772. return;
  1773. }
  1774. rq->end_io_data = &wait;
  1775. rq->end_io = blk_end_sync_rq;
  1776. spin_unlock_irq(&tape->spinlock);
  1777. wait_for_completion(&wait);
  1778. /* The stage and its struct request have been deallocated */
  1779. spin_lock_irq(&tape->spinlock);
  1780. }
  1781. static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
  1782. {
  1783. idetape_tape_t *tape = drive->driver_data;
  1784. u8 *readpos = tape->pc->buffer;
  1785. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1786. if (!tape->pc->error) {
  1787. debug_log(DBG_SENSE, "BOP - %s\n",
  1788. (readpos[0] & 0x80) ? "Yes" : "No");
  1789. debug_log(DBG_SENSE, "EOP - %s\n",
  1790. (readpos[0] & 0x40) ? "Yes" : "No");
  1791. if (readpos[0] & 0x4) {
  1792. printk(KERN_INFO "ide-tape: Block location is unknown"
  1793. "to the tape\n");
  1794. clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  1795. idetape_end_request(drive, 0, 0);
  1796. } else {
  1797. debug_log(DBG_SENSE, "Block Location - %u\n",
  1798. be32_to_cpu(*(u32 *)&readpos[4]));
  1799. tape->partition = readpos[1];
  1800. tape->first_frame_position =
  1801. be32_to_cpu(*(u32 *)&readpos[4]);
  1802. tape->last_frame_position =
  1803. be32_to_cpu(*(u32 *)&readpos[8]);
  1804. tape->blocks_in_buffer = readpos[15];
  1805. set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  1806. idetape_end_request(drive, 1, 0);
  1807. }
  1808. } else {
  1809. idetape_end_request(drive, 0, 0);
  1810. }
  1811. return ide_stopped;
  1812. }
  1813. /*
  1814. * idetape_create_write_filemark_cmd will:
  1815. *
  1816. * 1. Write a filemark if write_filemark=1.
  1817. * 2. Flush the device buffers without writing a filemark
  1818. * if write_filemark=0.
  1819. *
  1820. */
  1821. static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
  1822. {
  1823. idetape_init_pc(pc);
  1824. pc->c[0] = WRITE_FILEMARKS;
  1825. pc->c[4] = write_filemark;
  1826. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  1827. pc->callback = &idetape_pc_callback;
  1828. }
  1829. static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
  1830. {
  1831. idetape_init_pc(pc);
  1832. pc->c[0] = TEST_UNIT_READY;
  1833. pc->callback = &idetape_pc_callback;
  1834. }
  1835. /*
  1836. * idetape_queue_pc_tail is based on the following functions:
  1837. *
  1838. * ide_do_drive_cmd from ide.c
  1839. * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
  1840. *
  1841. * We add a special packet command request to the tail of the request
  1842. * queue, and wait for it to be serviced.
  1843. *
  1844. * This is not to be called from within the request handling part
  1845. * of the driver ! We allocate here data in the stack, and it is valid
  1846. * until the request is finished. This is not the case for the bottom
  1847. * part of the driver, where we are always leaving the functions to wait
  1848. * for an interrupt or a timer event.
  1849. *
  1850. * From the bottom part of the driver, we should allocate safe memory
  1851. * using idetape_next_pc_storage and idetape_next_rq_storage, and add
  1852. * the request to the request list without waiting for it to be serviced !
  1853. * In that case, we usually use idetape_queue_pc_head.
  1854. */
  1855. static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
  1856. {
  1857. struct ide_tape_obj *tape = drive->driver_data;
  1858. struct request rq;
  1859. idetape_init_rq(&rq, REQ_IDETAPE_PC1);
  1860. rq.buffer = (char *) pc;
  1861. rq.rq_disk = tape->disk;
  1862. return ide_do_drive_cmd(drive, &rq, ide_wait);
  1863. }
  1864. static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
  1865. {
  1866. idetape_init_pc(pc);
  1867. pc->c[0] = START_STOP;
  1868. pc->c[4] = cmd;
  1869. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  1870. pc->callback = &idetape_pc_callback;
  1871. }
  1872. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  1873. {
  1874. idetape_tape_t *tape = drive->driver_data;
  1875. idetape_pc_t pc;
  1876. int load_attempted = 0;
  1877. /*
  1878. * Wait for the tape to become ready
  1879. */
  1880. set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  1881. timeout += jiffies;
  1882. while (time_before(jiffies, timeout)) {
  1883. idetape_create_test_unit_ready_cmd(&pc);
  1884. if (!__idetape_queue_pc_tail(drive, &pc))
  1885. return 0;
  1886. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  1887. || (tape->asc == 0x3A)) { /* no media */
  1888. if (load_attempted)
  1889. return -ENOMEDIUM;
  1890. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  1891. __idetape_queue_pc_tail(drive, &pc);
  1892. load_attempted = 1;
  1893. /* not about to be ready */
  1894. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  1895. (tape->ascq == 1 || tape->ascq == 8)))
  1896. return -EIO;
  1897. msleep(100);
  1898. }
  1899. return -EIO;
  1900. }
  1901. static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
  1902. {
  1903. return __idetape_queue_pc_tail(drive, pc);
  1904. }
  1905. static int idetape_flush_tape_buffers (ide_drive_t *drive)
  1906. {
  1907. idetape_pc_t pc;
  1908. int rc;
  1909. idetape_create_write_filemark_cmd(drive, &pc, 0);
  1910. if ((rc = idetape_queue_pc_tail(drive, &pc)))
  1911. return rc;
  1912. idetape_wait_ready(drive, 60 * 5 * HZ);
  1913. return 0;
  1914. }
  1915. static void idetape_create_read_position_cmd (idetape_pc_t *pc)
  1916. {
  1917. idetape_init_pc(pc);
  1918. pc->c[0] = READ_POSITION;
  1919. pc->request_transfer = 20;
  1920. pc->callback = &idetape_read_position_callback;
  1921. }
  1922. static int idetape_read_position (ide_drive_t *drive)
  1923. {
  1924. idetape_tape_t *tape = drive->driver_data;
  1925. idetape_pc_t pc;
  1926. int position;
  1927. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1928. idetape_create_read_position_cmd(&pc);
  1929. if (idetape_queue_pc_tail(drive, &pc))
  1930. return -1;
  1931. position = tape->first_frame_position;
  1932. return position;
  1933. }
  1934. static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
  1935. {
  1936. idetape_init_pc(pc);
  1937. pc->c[0] = POSITION_TO_ELEMENT;
  1938. pc->c[1] = 2;
  1939. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  1940. pc->c[8] = partition;
  1941. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  1942. pc->callback = &idetape_pc_callback;
  1943. }
  1944. static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
  1945. {
  1946. idetape_tape_t *tape = drive->driver_data;
  1947. /* device supports locking according to capabilities page */
  1948. if (!(tape->caps[6] & 0x01))
  1949. return 0;
  1950. idetape_init_pc(pc);
  1951. pc->c[0] = ALLOW_MEDIUM_REMOVAL;
  1952. pc->c[4] = prevent;
  1953. pc->callback = &idetape_pc_callback;
  1954. return 1;
  1955. }
  1956. static int __idetape_discard_read_pipeline (ide_drive_t *drive)
  1957. {
  1958. idetape_tape_t *tape = drive->driver_data;
  1959. unsigned long flags;
  1960. int cnt;
  1961. if (tape->chrdev_direction != idetape_direction_read)
  1962. return 0;
  1963. /* Remove merge stage. */
  1964. cnt = tape->merge_stage_size / tape->tape_block_size;
  1965. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  1966. ++cnt; /* Filemarks count as 1 sector */
  1967. tape->merge_stage_size = 0;
  1968. if (tape->merge_stage != NULL) {
  1969. __idetape_kfree_stage(tape->merge_stage);
  1970. tape->merge_stage = NULL;
  1971. }
  1972. /* Clear pipeline flags. */
  1973. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1974. tape->chrdev_direction = idetape_direction_none;
  1975. /* Remove pipeline stages. */
  1976. if (tape->first_stage == NULL)
  1977. return 0;
  1978. spin_lock_irqsave(&tape->spinlock, flags);
  1979. tape->next_stage = NULL;
  1980. if (idetape_pipeline_active(tape))
  1981. idetape_wait_for_request(drive, tape->active_data_request);
  1982. spin_unlock_irqrestore(&tape->spinlock, flags);
  1983. while (tape->first_stage != NULL) {
  1984. struct request *rq_ptr = &tape->first_stage->rq;
  1985. cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
  1986. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  1987. ++cnt;
  1988. idetape_remove_stage_head(drive);
  1989. }
  1990. tape->nr_pending_stages = 0;
  1991. tape->max_stages = tape->min_pipeline;
  1992. return cnt;
  1993. }
  1994. /*
  1995. * idetape_position_tape positions the tape to the requested block
  1996. * using the LOCATE packet command. A READ POSITION command is then
  1997. * issued to check where we are positioned.
  1998. *
  1999. * Like all higher level operations, we queue the commands at the tail
  2000. * of the request queue and wait for their completion.
  2001. *
  2002. */
  2003. static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
  2004. {
  2005. idetape_tape_t *tape = drive->driver_data;
  2006. int retval;
  2007. idetape_pc_t pc;
  2008. if (tape->chrdev_direction == idetape_direction_read)
  2009. __idetape_discard_read_pipeline(drive);
  2010. idetape_wait_ready(drive, 60 * 5 * HZ);
  2011. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  2012. retval = idetape_queue_pc_tail(drive, &pc);
  2013. if (retval)
  2014. return (retval);
  2015. idetape_create_read_position_cmd(&pc);
  2016. return (idetape_queue_pc_tail(drive, &pc));
  2017. }
  2018. static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
  2019. {
  2020. idetape_tape_t *tape = drive->driver_data;
  2021. int cnt;
  2022. int seek, position;
  2023. cnt = __idetape_discard_read_pipeline(drive);
  2024. if (restore_position) {
  2025. position = idetape_read_position(drive);
  2026. seek = position > cnt ? position - cnt : 0;
  2027. if (idetape_position_tape(drive, seek, 0, 0)) {
  2028. printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
  2029. return;
  2030. }
  2031. }
  2032. }
  2033. /*
  2034. * idetape_queue_rw_tail generates a read/write request for the block
  2035. * device interface and wait for it to be serviced.
  2036. */
  2037. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
  2038. {
  2039. idetape_tape_t *tape = drive->driver_data;
  2040. struct request rq;
  2041. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  2042. if (idetape_pipeline_active(tape)) {
  2043. printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
  2044. __func__);
  2045. return (0);
  2046. }
  2047. idetape_init_rq(&rq, cmd);
  2048. rq.rq_disk = tape->disk;
  2049. rq.special = (void *)bh;
  2050. rq.sector = tape->first_frame_position;
  2051. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2052. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  2053. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  2054. return 0;
  2055. if (tape->merge_stage)
  2056. idetape_init_merge_stage(tape);
  2057. if (rq.errors == IDETAPE_ERROR_GENERAL)
  2058. return -EIO;
  2059. return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
  2060. }
  2061. /*
  2062. * idetape_insert_pipeline_into_queue is used to start servicing the
  2063. * pipeline stages, starting from tape->next_stage.
  2064. */
  2065. static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
  2066. {
  2067. idetape_tape_t *tape = drive->driver_data;
  2068. if (tape->next_stage == NULL)
  2069. return;
  2070. if (!idetape_pipeline_active(tape)) {
  2071. set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2072. idetape_activate_next_stage(drive);
  2073. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  2074. }
  2075. }
  2076. static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
  2077. {
  2078. idetape_init_pc(pc);
  2079. pc->c[0] = INQUIRY;
  2080. pc->c[4] = pc->request_transfer = 254;
  2081. pc->callback = &idetape_pc_callback;
  2082. }
  2083. static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
  2084. {
  2085. idetape_init_pc(pc);
  2086. pc->c[0] = REZERO_UNIT;
  2087. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2088. pc->callback = &idetape_pc_callback;
  2089. }
  2090. static void idetape_create_erase_cmd (idetape_pc_t *pc)
  2091. {
  2092. idetape_init_pc(pc);
  2093. pc->c[0] = ERASE;
  2094. pc->c[1] = 1;
  2095. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2096. pc->callback = &idetape_pc_callback;
  2097. }
  2098. static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
  2099. {
  2100. idetape_init_pc(pc);
  2101. pc->c[0] = SPACE;
  2102. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  2103. pc->c[1] = cmd;
  2104. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2105. pc->callback = &idetape_pc_callback;
  2106. }
  2107. static void idetape_wait_first_stage (ide_drive_t *drive)
  2108. {
  2109. idetape_tape_t *tape = drive->driver_data;
  2110. unsigned long flags;
  2111. if (tape->first_stage == NULL)
  2112. return;
  2113. spin_lock_irqsave(&tape->spinlock, flags);
  2114. if (tape->active_stage == tape->first_stage)
  2115. idetape_wait_for_request(drive, tape->active_data_request);
  2116. spin_unlock_irqrestore(&tape->spinlock, flags);
  2117. }
  2118. /*
  2119. * idetape_add_chrdev_write_request tries to add a character device
  2120. * originated write request to our pipeline. In case we don't succeed,
  2121. * we revert to non-pipelined operation mode for this request.
  2122. *
  2123. * 1. Try to allocate a new pipeline stage.
  2124. * 2. If we can't, wait for more and more requests to be serviced
  2125. * and try again each time.
  2126. * 3. If we still can't allocate a stage, fallback to
  2127. * non-pipelined operation mode for this request.
  2128. */
  2129. static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
  2130. {
  2131. idetape_tape_t *tape = drive->driver_data;
  2132. idetape_stage_t *new_stage;
  2133. unsigned long flags;
  2134. struct request *rq;
  2135. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2136. /*
  2137. * Attempt to allocate a new stage.
  2138. * Pay special attention to possible race conditions.
  2139. */
  2140. while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
  2141. spin_lock_irqsave(&tape->spinlock, flags);
  2142. if (idetape_pipeline_active(tape)) {
  2143. idetape_wait_for_request(drive, tape->active_data_request);
  2144. spin_unlock_irqrestore(&tape->spinlock, flags);
  2145. } else {
  2146. spin_unlock_irqrestore(&tape->spinlock, flags);
  2147. idetape_insert_pipeline_into_queue(drive);
  2148. if (idetape_pipeline_active(tape))
  2149. continue;
  2150. /*
  2151. * Linux is short on memory. Fallback to
  2152. * non-pipelined operation mode for this request.
  2153. */
  2154. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2155. }
  2156. }
  2157. rq = &new_stage->rq;
  2158. idetape_init_rq(rq, REQ_IDETAPE_WRITE);
  2159. /* Doesn't actually matter - We always assume sequential access */
  2160. rq->sector = tape->first_frame_position;
  2161. rq->nr_sectors = rq->current_nr_sectors = blocks;
  2162. idetape_switch_buffers(tape, new_stage);
  2163. idetape_add_stage_tail(drive, new_stage);
  2164. tape->pipeline_head++;
  2165. idetape_calculate_speeds(drive);
  2166. /*
  2167. * Estimate whether the tape has stopped writing by checking
  2168. * if our write pipeline is currently empty. If we are not
  2169. * writing anymore, wait for the pipeline to be full enough
  2170. * (90%) before starting to service requests, so that we will
  2171. * be able to keep up with the higher speeds of the tape.
  2172. */
  2173. if (!idetape_pipeline_active(tape)) {
  2174. if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
  2175. tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
  2176. tape->measure_insert_time = 1;
  2177. tape->insert_time = jiffies;
  2178. tape->insert_size = 0;
  2179. tape->insert_speed = 0;
  2180. idetape_insert_pipeline_into_queue(drive);
  2181. }
  2182. }
  2183. if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  2184. /* Return a deferred error */
  2185. return -EIO;
  2186. return blocks;
  2187. }
  2188. /*
  2189. * idetape_wait_for_pipeline will wait until all pending pipeline
  2190. * requests are serviced. Typically called on device close.
  2191. */
  2192. static void idetape_wait_for_pipeline (ide_drive_t *drive)
  2193. {
  2194. idetape_tape_t *tape = drive->driver_data;
  2195. unsigned long flags;
  2196. while (tape->next_stage || idetape_pipeline_active(tape)) {
  2197. idetape_insert_pipeline_into_queue(drive);
  2198. spin_lock_irqsave(&tape->spinlock, flags);
  2199. if (idetape_pipeline_active(tape))
  2200. idetape_wait_for_request(drive, tape->active_data_request);
  2201. spin_unlock_irqrestore(&tape->spinlock, flags);
  2202. }
  2203. }
  2204. static void idetape_empty_write_pipeline (ide_drive_t *drive)
  2205. {
  2206. idetape_tape_t *tape = drive->driver_data;
  2207. int blocks, min;
  2208. struct idetape_bh *bh;
  2209. if (tape->chrdev_direction != idetape_direction_write) {
  2210. printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
  2211. return;
  2212. }
  2213. if (tape->merge_stage_size > tape->stage_size) {
  2214. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  2215. tape->merge_stage_size = tape->stage_size;
  2216. }
  2217. if (tape->merge_stage_size) {
  2218. blocks = tape->merge_stage_size / tape->tape_block_size;
  2219. if (tape->merge_stage_size % tape->tape_block_size) {
  2220. unsigned int i;
  2221. blocks++;
  2222. i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
  2223. bh = tape->bh->b_reqnext;
  2224. while (bh) {
  2225. atomic_set(&bh->b_count, 0);
  2226. bh = bh->b_reqnext;
  2227. }
  2228. bh = tape->bh;
  2229. while (i) {
  2230. if (bh == NULL) {
  2231. printk(KERN_INFO "ide-tape: bug, bh NULL\n");
  2232. break;
  2233. }
  2234. min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
  2235. memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
  2236. atomic_add(min, &bh->b_count);
  2237. i -= min;
  2238. bh = bh->b_reqnext;
  2239. }
  2240. }
  2241. (void) idetape_add_chrdev_write_request(drive, blocks);
  2242. tape->merge_stage_size = 0;
  2243. }
  2244. idetape_wait_for_pipeline(drive);
  2245. if (tape->merge_stage != NULL) {
  2246. __idetape_kfree_stage(tape->merge_stage);
  2247. tape->merge_stage = NULL;
  2248. }
  2249. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2250. tape->chrdev_direction = idetape_direction_none;
  2251. /*
  2252. * On the next backup, perform the feedback loop again.
  2253. * (I don't want to keep sense information between backups,
  2254. * as some systems are constantly on, and the system load
  2255. * can be totally different on the next backup).
  2256. */
  2257. tape->max_stages = tape->min_pipeline;
  2258. if (tape->first_stage != NULL ||
  2259. tape->next_stage != NULL ||
  2260. tape->last_stage != NULL ||
  2261. tape->nr_stages != 0) {
  2262. printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
  2263. "first_stage %p, next_stage %p, "
  2264. "last_stage %p, nr_stages %d\n",
  2265. tape->first_stage, tape->next_stage,
  2266. tape->last_stage, tape->nr_stages);
  2267. }
  2268. }
  2269. static void idetape_restart_speed_control (ide_drive_t *drive)
  2270. {
  2271. idetape_tape_t *tape = drive->driver_data;
  2272. tape->restart_speed_control_req = 0;
  2273. tape->pipeline_head = 0;
  2274. tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
  2275. tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
  2276. tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
  2277. tape->uncontrolled_pipeline_head_speed = 0;
  2278. tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
  2279. tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
  2280. }
  2281. static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
  2282. {
  2283. idetape_tape_t *tape = drive->driver_data;
  2284. idetape_stage_t *new_stage;
  2285. struct request rq;
  2286. int bytes_read;
  2287. u16 blocks = *(u16 *)&tape->caps[12];
  2288. /* Initialize read operation */
  2289. if (tape->chrdev_direction != idetape_direction_read) {
  2290. if (tape->chrdev_direction == idetape_direction_write) {
  2291. idetape_empty_write_pipeline(drive);
  2292. idetape_flush_tape_buffers(drive);
  2293. }
  2294. if (tape->merge_stage || tape->merge_stage_size) {
  2295. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
  2296. tape->merge_stage_size = 0;
  2297. }
  2298. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  2299. return -ENOMEM;
  2300. tape->chrdev_direction = idetape_direction_read;
  2301. /*
  2302. * Issue a read 0 command to ensure that DSC handshake
  2303. * is switched from completion mode to buffer available
  2304. * mode.
  2305. * No point in issuing this if DSC overlap isn't supported,
  2306. * some drives (Seagate STT3401A) will return an error.
  2307. */
  2308. if (drive->dsc_overlap) {
  2309. bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
  2310. if (bytes_read < 0) {
  2311. __idetape_kfree_stage(tape->merge_stage);
  2312. tape->merge_stage = NULL;
  2313. tape->chrdev_direction = idetape_direction_none;
  2314. return bytes_read;
  2315. }
  2316. }
  2317. }
  2318. if (tape->restart_speed_control_req)
  2319. idetape_restart_speed_control(drive);
  2320. idetape_init_rq(&rq, REQ_IDETAPE_READ);
  2321. rq.sector = tape->first_frame_position;
  2322. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2323. if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
  2324. tape->nr_stages < max_stages) {
  2325. new_stage = idetape_kmalloc_stage(tape);
  2326. while (new_stage != NULL) {
  2327. new_stage->rq = rq;
  2328. idetape_add_stage_tail(drive, new_stage);
  2329. if (tape->nr_stages >= max_stages)
  2330. break;
  2331. new_stage = idetape_kmalloc_stage(tape);
  2332. }
  2333. }
  2334. if (!idetape_pipeline_active(tape)) {
  2335. if (tape->nr_pending_stages >= 3 * max_stages / 4) {
  2336. tape->measure_insert_time = 1;
  2337. tape->insert_time = jiffies;
  2338. tape->insert_size = 0;
  2339. tape->insert_speed = 0;
  2340. idetape_insert_pipeline_into_queue(drive);
  2341. }
  2342. }
  2343. return 0;
  2344. }
  2345. /*
  2346. * idetape_add_chrdev_read_request is called from idetape_chrdev_read
  2347. * to service a character device read request and add read-ahead
  2348. * requests to our pipeline.
  2349. */
  2350. static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
  2351. {
  2352. idetape_tape_t *tape = drive->driver_data;
  2353. unsigned long flags;
  2354. struct request *rq_ptr;
  2355. int bytes_read;
  2356. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  2357. /*
  2358. * If we are at a filemark, return a read length of 0
  2359. */
  2360. if (test_bit(IDETAPE_FILEMARK, &tape->flags))
  2361. return 0;
  2362. /*
  2363. * Wait for the next block to be available at the head
  2364. * of the pipeline
  2365. */
  2366. idetape_initiate_read(drive, tape->max_stages);
  2367. if (tape->first_stage == NULL) {
  2368. if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  2369. return 0;
  2370. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
  2371. }
  2372. idetape_wait_first_stage(drive);
  2373. rq_ptr = &tape->first_stage->rq;
  2374. bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
  2375. rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
  2376. if (rq_ptr->errors == IDETAPE_ERROR_EOD)
  2377. return 0;
  2378. else {
  2379. idetape_switch_buffers(tape, tape->first_stage);
  2380. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  2381. set_bit(IDETAPE_FILEMARK, &tape->flags);
  2382. spin_lock_irqsave(&tape->spinlock, flags);
  2383. idetape_remove_stage_head(drive);
  2384. spin_unlock_irqrestore(&tape->spinlock, flags);
  2385. tape->pipeline_head++;
  2386. idetape_calculate_speeds(drive);
  2387. }
  2388. if (bytes_read > blocks * tape->tape_block_size) {
  2389. printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
  2390. bytes_read = blocks * tape->tape_block_size;
  2391. }
  2392. return (bytes_read);
  2393. }
  2394. static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
  2395. {
  2396. idetape_tape_t *tape = drive->driver_data;
  2397. struct idetape_bh *bh;
  2398. int blocks;
  2399. while (bcount) {
  2400. unsigned int count;
  2401. bh = tape->merge_stage->bh;
  2402. count = min(tape->stage_size, bcount);
  2403. bcount -= count;
  2404. blocks = count / tape->tape_block_size;
  2405. while (count) {
  2406. atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
  2407. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  2408. count -= atomic_read(&bh->b_count);
  2409. bh = bh->b_reqnext;
  2410. }
  2411. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2412. }
  2413. }
  2414. static int idetape_pipeline_size (ide_drive_t *drive)
  2415. {
  2416. idetape_tape_t *tape = drive->driver_data;
  2417. idetape_stage_t *stage;
  2418. struct request *rq;
  2419. int size = 0;
  2420. idetape_wait_for_pipeline(drive);
  2421. stage = tape->first_stage;
  2422. while (stage != NULL) {
  2423. rq = &stage->rq;
  2424. size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
  2425. if (rq->errors == IDETAPE_ERROR_FILEMARK)
  2426. size += tape->tape_block_size;
  2427. stage = stage->next;
  2428. }
  2429. size += tape->merge_stage_size;
  2430. return size;
  2431. }
  2432. /*
  2433. * Rewinds the tape to the Beginning Of the current Partition (BOP).
  2434. *
  2435. * We currently support only one partition.
  2436. */
  2437. static int idetape_rewind_tape (ide_drive_t *drive)
  2438. {
  2439. int retval;
  2440. idetape_pc_t pc;
  2441. idetape_tape_t *tape;
  2442. tape = drive->driver_data;
  2443. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  2444. idetape_create_rewind_cmd(drive, &pc);
  2445. retval = idetape_queue_pc_tail(drive, &pc);
  2446. if (retval)
  2447. return retval;
  2448. idetape_create_read_position_cmd(&pc);
  2449. retval = idetape_queue_pc_tail(drive, &pc);
  2450. if (retval)
  2451. return retval;
  2452. return 0;
  2453. }
  2454. /*
  2455. * Our special ide-tape ioctl's.
  2456. *
  2457. * Currently there aren't any ioctl's.
  2458. * mtio.h compatible commands should be issued to the character device
  2459. * interface.
  2460. */
  2461. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  2462. {
  2463. idetape_tape_t *tape = drive->driver_data;
  2464. void __user *argp = (void __user *)arg;
  2465. struct idetape_config {
  2466. int dsc_rw_frequency;
  2467. int dsc_media_access_frequency;
  2468. int nr_stages;
  2469. } config;
  2470. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  2471. switch (cmd) {
  2472. case 0x0340:
  2473. if (copy_from_user(&config, argp, sizeof(config)))
  2474. return -EFAULT;
  2475. tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
  2476. tape->max_stages = config.nr_stages;
  2477. break;
  2478. case 0x0350:
  2479. config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
  2480. config.nr_stages = tape->max_stages;
  2481. if (copy_to_user(argp, &config, sizeof(config)))
  2482. return -EFAULT;
  2483. break;
  2484. default:
  2485. return -EIO;
  2486. }
  2487. return 0;
  2488. }
  2489. /*
  2490. * idetape_space_over_filemarks is now a bit more complicated than just
  2491. * passing the command to the tape since we may have crossed some
  2492. * filemarks during our pipelined read-ahead mode.
  2493. *
  2494. * As a minor side effect, the pipeline enables us to support MTFSFM when
  2495. * the filemark is in our internal pipeline even if the tape doesn't
  2496. * support spacing over filemarks in the reverse direction.
  2497. */
  2498. static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
  2499. {
  2500. idetape_tape_t *tape = drive->driver_data;
  2501. idetape_pc_t pc;
  2502. unsigned long flags;
  2503. int retval,count=0;
  2504. int sprev = !!(tape->caps[4] & 0x20);
  2505. if (mt_count == 0)
  2506. return 0;
  2507. if (MTBSF == mt_op || MTBSFM == mt_op) {
  2508. if (!sprev)
  2509. return -EIO;
  2510. mt_count = - mt_count;
  2511. }
  2512. if (tape->chrdev_direction == idetape_direction_read) {
  2513. /*
  2514. * We have a read-ahead buffer. Scan it for crossed
  2515. * filemarks.
  2516. */
  2517. tape->merge_stage_size = 0;
  2518. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  2519. ++count;
  2520. while (tape->first_stage != NULL) {
  2521. if (count == mt_count) {
  2522. if (mt_op == MTFSFM)
  2523. set_bit(IDETAPE_FILEMARK, &tape->flags);
  2524. return 0;
  2525. }
  2526. spin_lock_irqsave(&tape->spinlock, flags);
  2527. if (tape->first_stage == tape->active_stage) {
  2528. /*
  2529. * We have reached the active stage in the read pipeline.
  2530. * There is no point in allowing the drive to continue
  2531. * reading any farther, so we stop the pipeline.
  2532. *
  2533. * This section should be moved to a separate subroutine,
  2534. * because a similar function is performed in
  2535. * __idetape_discard_read_pipeline(), for example.
  2536. */
  2537. tape->next_stage = NULL;
  2538. spin_unlock_irqrestore(&tape->spinlock, flags);
  2539. idetape_wait_first_stage(drive);
  2540. tape->next_stage = tape->first_stage->next;
  2541. } else
  2542. spin_unlock_irqrestore(&tape->spinlock, flags);
  2543. if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
  2544. ++count;
  2545. idetape_remove_stage_head(drive);
  2546. }
  2547. idetape_discard_read_pipeline(drive, 0);
  2548. }
  2549. /*
  2550. * The filemark was not found in our internal pipeline.
  2551. * Now we can issue the space command.
  2552. */
  2553. switch (mt_op) {
  2554. case MTFSF:
  2555. case MTBSF:
  2556. idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
  2557. return (idetape_queue_pc_tail(drive, &pc));
  2558. case MTFSFM:
  2559. case MTBSFM:
  2560. if (!sprev)
  2561. return (-EIO);
  2562. retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
  2563. if (retval) return (retval);
  2564. count = (MTBSFM == mt_op ? 1 : -1);
  2565. return (idetape_space_over_filemarks(drive, MTFSF, count));
  2566. default:
  2567. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
  2568. return (-EIO);
  2569. }
  2570. }
  2571. /*
  2572. * Our character device read / write functions.
  2573. *
  2574. * The tape is optimized to maximize throughput when it is transferring
  2575. * an integral number of the "continuous transfer limit", which is
  2576. * a parameter of the specific tape (26 KB on my particular tape).
  2577. * (32 kB for Onstream)
  2578. *
  2579. * As of version 1.3 of the driver, the character device provides an
  2580. * abstract continuous view of the media - any mix of block sizes (even 1
  2581. * byte) on the same backup/restore procedure is supported. The driver
  2582. * will internally convert the requests to the recommended transfer unit,
  2583. * so that an unmatch between the user's block size to the recommended
  2584. * size will only result in a (slightly) increased driver overhead, but
  2585. * will no longer hit performance.
  2586. * This is not applicable to Onstream.
  2587. */
  2588. static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
  2589. size_t count, loff_t *ppos)
  2590. {
  2591. struct ide_tape_obj *tape = ide_tape_f(file);
  2592. ide_drive_t *drive = tape->drive;
  2593. ssize_t bytes_read,temp, actually_read = 0, rc;
  2594. ssize_t ret = 0;
  2595. u16 ctl = *(u16 *)&tape->caps[12];
  2596. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  2597. if (tape->chrdev_direction != idetape_direction_read) {
  2598. if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
  2599. if (count > tape->tape_block_size &&
  2600. (count % tape->tape_block_size) == 0)
  2601. tape->user_bs_factor = count / tape->tape_block_size;
  2602. }
  2603. if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
  2604. return rc;
  2605. if (count == 0)
  2606. return (0);
  2607. if (tape->merge_stage_size) {
  2608. actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
  2609. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
  2610. ret = -EFAULT;
  2611. buf += actually_read;
  2612. tape->merge_stage_size -= actually_read;
  2613. count -= actually_read;
  2614. }
  2615. while (count >= tape->stage_size) {
  2616. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  2617. if (bytes_read <= 0)
  2618. goto finish;
  2619. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
  2620. ret = -EFAULT;
  2621. buf += bytes_read;
  2622. count -= bytes_read;
  2623. actually_read += bytes_read;
  2624. }
  2625. if (count) {
  2626. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  2627. if (bytes_read <= 0)
  2628. goto finish;
  2629. temp = min((unsigned long)count, (unsigned long)bytes_read);
  2630. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
  2631. ret = -EFAULT;
  2632. actually_read += temp;
  2633. tape->merge_stage_size = bytes_read-temp;
  2634. }
  2635. finish:
  2636. if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
  2637. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  2638. idetape_space_over_filemarks(drive, MTFSF, 1);
  2639. return 0;
  2640. }
  2641. return (ret) ? ret : actually_read;
  2642. }
  2643. static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
  2644. size_t count, loff_t *ppos)
  2645. {
  2646. struct ide_tape_obj *tape = ide_tape_f(file);
  2647. ide_drive_t *drive = tape->drive;
  2648. ssize_t actually_written = 0;
  2649. ssize_t ret = 0;
  2650. u16 ctl = *(u16 *)&tape->caps[12];
  2651. /* The drive is write protected. */
  2652. if (tape->write_prot)
  2653. return -EACCES;
  2654. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  2655. /* Initialize write operation */
  2656. if (tape->chrdev_direction != idetape_direction_write) {
  2657. if (tape->chrdev_direction == idetape_direction_read)
  2658. idetape_discard_read_pipeline(drive, 1);
  2659. if (tape->merge_stage || tape->merge_stage_size) {
  2660. printk(KERN_ERR "ide-tape: merge_stage_size "
  2661. "should be 0 now\n");
  2662. tape->merge_stage_size = 0;
  2663. }
  2664. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  2665. return -ENOMEM;
  2666. tape->chrdev_direction = idetape_direction_write;
  2667. idetape_init_merge_stage(tape);
  2668. /*
  2669. * Issue a write 0 command to ensure that DSC handshake
  2670. * is switched from completion mode to buffer available
  2671. * mode.
  2672. * No point in issuing this if DSC overlap isn't supported,
  2673. * some drives (Seagate STT3401A) will return an error.
  2674. */
  2675. if (drive->dsc_overlap) {
  2676. ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
  2677. if (retval < 0) {
  2678. __idetape_kfree_stage(tape->merge_stage);
  2679. tape->merge_stage = NULL;
  2680. tape->chrdev_direction = idetape_direction_none;
  2681. return retval;
  2682. }
  2683. }
  2684. }
  2685. if (count == 0)
  2686. return (0);
  2687. if (tape->restart_speed_control_req)
  2688. idetape_restart_speed_control(drive);
  2689. if (tape->merge_stage_size) {
  2690. if (tape->merge_stage_size >= tape->stage_size) {
  2691. printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
  2692. tape->merge_stage_size = 0;
  2693. }
  2694. actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
  2695. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
  2696. ret = -EFAULT;
  2697. buf += actually_written;
  2698. tape->merge_stage_size += actually_written;
  2699. count -= actually_written;
  2700. if (tape->merge_stage_size == tape->stage_size) {
  2701. ssize_t retval;
  2702. tape->merge_stage_size = 0;
  2703. retval = idetape_add_chrdev_write_request(drive, ctl);
  2704. if (retval <= 0)
  2705. return (retval);
  2706. }
  2707. }
  2708. while (count >= tape->stage_size) {
  2709. ssize_t retval;
  2710. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
  2711. ret = -EFAULT;
  2712. buf += tape->stage_size;
  2713. count -= tape->stage_size;
  2714. retval = idetape_add_chrdev_write_request(drive, ctl);
  2715. actually_written += tape->stage_size;
  2716. if (retval <= 0)
  2717. return (retval);
  2718. }
  2719. if (count) {
  2720. actually_written += count;
  2721. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
  2722. ret = -EFAULT;
  2723. tape->merge_stage_size += count;
  2724. }
  2725. return (ret) ? ret : actually_written;
  2726. }
  2727. static int idetape_write_filemark (ide_drive_t *drive)
  2728. {
  2729. idetape_pc_t pc;
  2730. /* Write a filemark */
  2731. idetape_create_write_filemark_cmd(drive, &pc, 1);
  2732. if (idetape_queue_pc_tail(drive, &pc)) {
  2733. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  2734. return -EIO;
  2735. }
  2736. return 0;
  2737. }
  2738. /*
  2739. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  2740. * requested.
  2741. *
  2742. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  2743. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  2744. * usually not supported (it is supported in the rare case in which we crossed
  2745. * the filemark during our read-ahead pipelined operation mode).
  2746. *
  2747. * The following commands are currently not supported:
  2748. *
  2749. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  2750. * MT_ST_WRITE_THRESHOLD.
  2751. */
  2752. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  2753. {
  2754. idetape_tape_t *tape = drive->driver_data;
  2755. idetape_pc_t pc;
  2756. int i,retval;
  2757. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  2758. mt_op, mt_count);
  2759. /*
  2760. * Commands which need our pipelined read-ahead stages.
  2761. */
  2762. switch (mt_op) {
  2763. case MTFSF:
  2764. case MTFSFM:
  2765. case MTBSF:
  2766. case MTBSFM:
  2767. if (!mt_count)
  2768. return (0);
  2769. return (idetape_space_over_filemarks(drive,mt_op,mt_count));
  2770. default:
  2771. break;
  2772. }
  2773. switch (mt_op) {
  2774. case MTWEOF:
  2775. if (tape->write_prot)
  2776. return -EACCES;
  2777. idetape_discard_read_pipeline(drive, 1);
  2778. for (i = 0; i < mt_count; i++) {
  2779. retval = idetape_write_filemark(drive);
  2780. if (retval)
  2781. return retval;
  2782. }
  2783. return (0);
  2784. case MTREW:
  2785. idetape_discard_read_pipeline(drive, 0);
  2786. if (idetape_rewind_tape(drive))
  2787. return -EIO;
  2788. return 0;
  2789. case MTLOAD:
  2790. idetape_discard_read_pipeline(drive, 0);
  2791. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  2792. return (idetape_queue_pc_tail(drive, &pc));
  2793. case MTUNLOAD:
  2794. case MTOFFL:
  2795. /*
  2796. * If door is locked, attempt to unlock before
  2797. * attempting to eject.
  2798. */
  2799. if (tape->door_locked) {
  2800. if (idetape_create_prevent_cmd(drive, &pc, 0))
  2801. if (!idetape_queue_pc_tail(drive, &pc))
  2802. tape->door_locked = DOOR_UNLOCKED;
  2803. }
  2804. idetape_discard_read_pipeline(drive, 0);
  2805. idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
  2806. retval = idetape_queue_pc_tail(drive, &pc);
  2807. if (!retval)
  2808. clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  2809. return retval;
  2810. case MTNOP:
  2811. idetape_discard_read_pipeline(drive, 0);
  2812. return (idetape_flush_tape_buffers(drive));
  2813. case MTRETEN:
  2814. idetape_discard_read_pipeline(drive, 0);
  2815. idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  2816. return (idetape_queue_pc_tail(drive, &pc));
  2817. case MTEOM:
  2818. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  2819. return (idetape_queue_pc_tail(drive, &pc));
  2820. case MTERASE:
  2821. (void) idetape_rewind_tape(drive);
  2822. idetape_create_erase_cmd(&pc);
  2823. return (idetape_queue_pc_tail(drive, &pc));
  2824. case MTSETBLK:
  2825. if (mt_count) {
  2826. if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
  2827. return -EIO;
  2828. tape->user_bs_factor = mt_count / tape->tape_block_size;
  2829. clear_bit(IDETAPE_DETECT_BS, &tape->flags);
  2830. } else
  2831. set_bit(IDETAPE_DETECT_BS, &tape->flags);
  2832. return 0;
  2833. case MTSEEK:
  2834. idetape_discard_read_pipeline(drive, 0);
  2835. return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
  2836. case MTSETPART:
  2837. idetape_discard_read_pipeline(drive, 0);
  2838. return (idetape_position_tape(drive, 0, mt_count, 0));
  2839. case MTFSR:
  2840. case MTBSR:
  2841. case MTLOCK:
  2842. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  2843. return 0;
  2844. retval = idetape_queue_pc_tail(drive, &pc);
  2845. if (retval) return retval;
  2846. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  2847. return 0;
  2848. case MTUNLOCK:
  2849. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  2850. return 0;
  2851. retval = idetape_queue_pc_tail(drive, &pc);
  2852. if (retval) return retval;
  2853. tape->door_locked = DOOR_UNLOCKED;
  2854. return 0;
  2855. default:
  2856. printk(KERN_ERR "ide-tape: MTIO operation %d not "
  2857. "supported\n", mt_op);
  2858. return (-EIO);
  2859. }
  2860. }
  2861. /*
  2862. * Our character device ioctls. General mtio.h magnetic io commands are
  2863. * supported here, and not in the corresponding block interface. Our own
  2864. * ide-tape ioctls are supported on both interfaces.
  2865. */
  2866. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  2867. unsigned int cmd, unsigned long arg)
  2868. {
  2869. struct ide_tape_obj *tape = ide_tape_f(file);
  2870. ide_drive_t *drive = tape->drive;
  2871. struct mtop mtop;
  2872. struct mtget mtget;
  2873. struct mtpos mtpos;
  2874. int block_offset = 0, position = tape->first_frame_position;
  2875. void __user *argp = (void __user *)arg;
  2876. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  2877. tape->restart_speed_control_req = 1;
  2878. if (tape->chrdev_direction == idetape_direction_write) {
  2879. idetape_empty_write_pipeline(drive);
  2880. idetape_flush_tape_buffers(drive);
  2881. }
  2882. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  2883. block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
  2884. if ((position = idetape_read_position(drive)) < 0)
  2885. return -EIO;
  2886. }
  2887. switch (cmd) {
  2888. case MTIOCTOP:
  2889. if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
  2890. return -EFAULT;
  2891. return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
  2892. case MTIOCGET:
  2893. memset(&mtget, 0, sizeof (struct mtget));
  2894. mtget.mt_type = MT_ISSCSI2;
  2895. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  2896. mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  2897. if (tape->drv_write_prot) {
  2898. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  2899. }
  2900. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  2901. return -EFAULT;
  2902. return 0;
  2903. case MTIOCPOS:
  2904. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  2905. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  2906. return -EFAULT;
  2907. return 0;
  2908. default:
  2909. if (tape->chrdev_direction == idetape_direction_read)
  2910. idetape_discard_read_pipeline(drive, 1);
  2911. return idetape_blkdev_ioctl(drive, cmd, arg);
  2912. }
  2913. }
  2914. /*
  2915. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  2916. * block size with the reported value.
  2917. */
  2918. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  2919. {
  2920. idetape_tape_t *tape = drive->driver_data;
  2921. idetape_pc_t pc;
  2922. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  2923. if (idetape_queue_pc_tail(drive, &pc)) {
  2924. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  2925. if (tape->tape_block_size == 0) {
  2926. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  2927. "block size, assuming 32k\n");
  2928. tape->tape_block_size = 32768;
  2929. }
  2930. return;
  2931. }
  2932. tape->tape_block_size = (pc.buffer[4 + 5] << 16) +
  2933. (pc.buffer[4 + 6] << 8) +
  2934. pc.buffer[4 + 7];
  2935. tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
  2936. }
  2937. /*
  2938. * Our character device open function.
  2939. */
  2940. static int idetape_chrdev_open (struct inode *inode, struct file *filp)
  2941. {
  2942. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  2943. ide_drive_t *drive;
  2944. idetape_tape_t *tape;
  2945. idetape_pc_t pc;
  2946. int retval;
  2947. if (i >= MAX_HWIFS * MAX_DRIVES)
  2948. return -ENXIO;
  2949. tape = ide_tape_chrdev_get(i);
  2950. if (!tape)
  2951. return -ENXIO;
  2952. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2953. /*
  2954. * We really want to do nonseekable_open(inode, filp); here, but some
  2955. * versions of tar incorrectly call lseek on tapes and bail out if that
  2956. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  2957. */
  2958. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  2959. drive = tape->drive;
  2960. filp->private_data = tape;
  2961. if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
  2962. retval = -EBUSY;
  2963. goto out_put_tape;
  2964. }
  2965. retval = idetape_wait_ready(drive, 60 * HZ);
  2966. if (retval) {
  2967. clear_bit(IDETAPE_BUSY, &tape->flags);
  2968. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  2969. goto out_put_tape;
  2970. }
  2971. idetape_read_position(drive);
  2972. if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
  2973. (void)idetape_rewind_tape(drive);
  2974. if (tape->chrdev_direction != idetape_direction_read)
  2975. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2976. /* Read block size and write protect status from drive. */
  2977. ide_tape_get_bsize_from_bdesc(drive);
  2978. /* Set write protect flag if device is opened as read-only. */
  2979. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  2980. tape->write_prot = 1;
  2981. else
  2982. tape->write_prot = tape->drv_write_prot;
  2983. /* Make sure drive isn't write protected if user wants to write. */
  2984. if (tape->write_prot) {
  2985. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  2986. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  2987. clear_bit(IDETAPE_BUSY, &tape->flags);
  2988. retval = -EROFS;
  2989. goto out_put_tape;
  2990. }
  2991. }
  2992. /*
  2993. * Lock the tape drive door so user can't eject.
  2994. */
  2995. if (tape->chrdev_direction == idetape_direction_none) {
  2996. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  2997. if (!idetape_queue_pc_tail(drive, &pc)) {
  2998. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  2999. tape->door_locked = DOOR_LOCKED;
  3000. }
  3001. }
  3002. }
  3003. idetape_restart_speed_control(drive);
  3004. tape->restart_speed_control_req = 0;
  3005. return 0;
  3006. out_put_tape:
  3007. ide_tape_put(tape);
  3008. return retval;
  3009. }
  3010. static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
  3011. {
  3012. idetape_tape_t *tape = drive->driver_data;
  3013. idetape_empty_write_pipeline(drive);
  3014. tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
  3015. if (tape->merge_stage != NULL) {
  3016. idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
  3017. __idetape_kfree_stage(tape->merge_stage);
  3018. tape->merge_stage = NULL;
  3019. }
  3020. idetape_write_filemark(drive);
  3021. idetape_flush_tape_buffers(drive);
  3022. idetape_flush_tape_buffers(drive);
  3023. }
  3024. /*
  3025. * Our character device release function.
  3026. */
  3027. static int idetape_chrdev_release (struct inode *inode, struct file *filp)
  3028. {
  3029. struct ide_tape_obj *tape = ide_tape_f(filp);
  3030. ide_drive_t *drive = tape->drive;
  3031. idetape_pc_t pc;
  3032. unsigned int minor = iminor(inode);
  3033. lock_kernel();
  3034. tape = drive->driver_data;
  3035. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  3036. if (tape->chrdev_direction == idetape_direction_write)
  3037. idetape_write_release(drive, minor);
  3038. if (tape->chrdev_direction == idetape_direction_read) {
  3039. if (minor < 128)
  3040. idetape_discard_read_pipeline(drive, 1);
  3041. else
  3042. idetape_wait_for_pipeline(drive);
  3043. }
  3044. if (tape->cache_stage != NULL) {
  3045. __idetape_kfree_stage(tape->cache_stage);
  3046. tape->cache_stage = NULL;
  3047. }
  3048. if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
  3049. (void) idetape_rewind_tape(drive);
  3050. if (tape->chrdev_direction == idetape_direction_none) {
  3051. if (tape->door_locked == DOOR_LOCKED) {
  3052. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  3053. if (!idetape_queue_pc_tail(drive, &pc))
  3054. tape->door_locked = DOOR_UNLOCKED;
  3055. }
  3056. }
  3057. }
  3058. clear_bit(IDETAPE_BUSY, &tape->flags);
  3059. ide_tape_put(tape);
  3060. unlock_kernel();
  3061. return 0;
  3062. }
  3063. /*
  3064. * idetape_identify_device is called to check the contents of the
  3065. * ATAPI IDENTIFY command results. We return:
  3066. *
  3067. * 1 If the tape can be supported by us, based on the information
  3068. * we have so far.
  3069. *
  3070. * 0 If this tape driver is not currently supported by us.
  3071. */
  3072. static int idetape_identify_device (ide_drive_t *drive)
  3073. {
  3074. struct idetape_id_gcw gcw;
  3075. struct hd_driveid *id = drive->id;
  3076. if (drive->id_read == 0)
  3077. return 1;
  3078. *((unsigned short *) &gcw) = id->config;
  3079. /* Check that we can support this device */
  3080. if (gcw.protocol != 2)
  3081. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  3082. gcw.protocol);
  3083. else if (gcw.device_type != 1)
  3084. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  3085. "to tape\n", gcw.device_type);
  3086. else if (!gcw.removable)
  3087. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  3088. else if (gcw.packet_size != 0) {
  3089. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
  3090. "bytes long\n", gcw.packet_size);
  3091. } else
  3092. return 1;
  3093. return 0;
  3094. }
  3095. static void idetape_get_inquiry_results(ide_drive_t *drive)
  3096. {
  3097. char *r;
  3098. idetape_tape_t *tape = drive->driver_data;
  3099. idetape_pc_t pc;
  3100. idetape_create_inquiry_cmd(&pc);
  3101. if (idetape_queue_pc_tail(drive, &pc)) {
  3102. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  3103. tape->name);
  3104. return;
  3105. }
  3106. memcpy(tape->vendor_id, &pc.buffer[8], 8);
  3107. memcpy(tape->product_id, &pc.buffer[16], 16);
  3108. memcpy(tape->firmware_revision, &pc.buffer[32], 4);
  3109. ide_fixstring(tape->vendor_id, 10, 0);
  3110. ide_fixstring(tape->product_id, 18, 0);
  3111. ide_fixstring(tape->firmware_revision, 6, 0);
  3112. r = tape->firmware_revision;
  3113. if (*(r + 1) == '.')
  3114. tape->firmware_revision_num = (*r - '0') * 100 +
  3115. (*(r + 2) - '0') * 10 + *(r + 3) - '0';
  3116. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
  3117. drive->name, tape->name, tape->vendor_id,
  3118. tape->product_id, tape->firmware_revision);
  3119. }
  3120. /*
  3121. * Ask the tape about its various parameters. In particular, we will adjust our
  3122. * data transfer buffer size to the recommended value as returned by the tape.
  3123. */
  3124. static void idetape_get_mode_sense_results (ide_drive_t *drive)
  3125. {
  3126. idetape_tape_t *tape = drive->driver_data;
  3127. idetape_pc_t pc;
  3128. u8 *caps;
  3129. u8 speed, max_speed;
  3130. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  3131. if (idetape_queue_pc_tail(drive, &pc)) {
  3132. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  3133. " some default values\n");
  3134. tape->tape_block_size = 512;
  3135. put_unaligned(52, (u16 *)&tape->caps[12]);
  3136. put_unaligned(540, (u16 *)&tape->caps[14]);
  3137. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  3138. return;
  3139. }
  3140. caps = pc.buffer + 4 + pc.buffer[3];
  3141. /* convert to host order and save for later use */
  3142. speed = be16_to_cpu(*(u16 *)&caps[14]);
  3143. max_speed = be16_to_cpu(*(u16 *)&caps[8]);
  3144. put_unaligned(max_speed, (u16 *)&caps[8]);
  3145. put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
  3146. put_unaligned(speed, (u16 *)&caps[14]);
  3147. put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
  3148. if (!speed) {
  3149. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  3150. "(assuming 650KB/sec)\n", drive->name);
  3151. put_unaligned(650, (u16 *)&caps[14]);
  3152. }
  3153. if (!max_speed) {
  3154. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  3155. "(assuming 650KB/sec)\n", drive->name);
  3156. put_unaligned(650, (u16 *)&caps[8]);
  3157. }
  3158. memcpy(&tape->caps, caps, 20);
  3159. if (caps[7] & 0x02)
  3160. tape->tape_block_size = 512;
  3161. else if (caps[7] & 0x04)
  3162. tape->tape_block_size = 1024;
  3163. }
  3164. #ifdef CONFIG_IDE_PROC_FS
  3165. static void idetape_add_settings (ide_drive_t *drive)
  3166. {
  3167. idetape_tape_t *tape = drive->driver_data;
  3168. /*
  3169. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  3170. */
  3171. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  3172. 1, 2, (u16 *)&tape->caps[16], NULL);
  3173. ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
  3174. ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
  3175. ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
  3176. ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
  3177. ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
  3178. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  3179. 1, 1, (u16 *)&tape->caps[14], NULL);
  3180. ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
  3181. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
  3182. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  3183. ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
  3184. ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
  3185. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
  3186. ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
  3187. 1, &tape->debug_mask, NULL);
  3188. }
  3189. #else
  3190. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  3191. #endif
  3192. /*
  3193. * ide_setup is called to:
  3194. *
  3195. * 1. Initialize our various state variables.
  3196. * 2. Ask the tape for its capabilities.
  3197. * 3. Allocate a buffer which will be used for data
  3198. * transfer. The buffer size is chosen based on
  3199. * the recommendation which we received in step (2).
  3200. *
  3201. * Note that at this point ide.c already assigned us an irq, so that
  3202. * we can queue requests here and wait for their completion.
  3203. */
  3204. static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
  3205. {
  3206. unsigned long t1, tmid, tn, t;
  3207. int speed;
  3208. struct idetape_id_gcw gcw;
  3209. int stage_size;
  3210. struct sysinfo si;
  3211. u16 *ctl = (u16 *)&tape->caps[12];
  3212. spin_lock_init(&tape->spinlock);
  3213. drive->dsc_overlap = 1;
  3214. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  3215. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  3216. tape->name);
  3217. drive->dsc_overlap = 0;
  3218. }
  3219. /* Seagate Travan drives do not support DSC overlap. */
  3220. if (strstr(drive->id->model, "Seagate STT3401"))
  3221. drive->dsc_overlap = 0;
  3222. tape->minor = minor;
  3223. tape->name[0] = 'h';
  3224. tape->name[1] = 't';
  3225. tape->name[2] = '0' + minor;
  3226. tape->chrdev_direction = idetape_direction_none;
  3227. tape->pc = tape->pc_stack;
  3228. tape->max_insert_speed = 10000;
  3229. tape->speed_control = 1;
  3230. *((unsigned short *) &gcw) = drive->id->config;
  3231. if (gcw.drq_type == 1)
  3232. set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
  3233. tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
  3234. idetape_get_inquiry_results(drive);
  3235. idetape_get_mode_sense_results(drive);
  3236. ide_tape_get_bsize_from_bdesc(drive);
  3237. tape->user_bs_factor = 1;
  3238. tape->stage_size = *ctl * tape->tape_block_size;
  3239. while (tape->stage_size > 0xffff) {
  3240. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  3241. *ctl /= 2;
  3242. tape->stage_size = *ctl * tape->tape_block_size;
  3243. }
  3244. stage_size = tape->stage_size;
  3245. tape->pages_per_stage = stage_size / PAGE_SIZE;
  3246. if (stage_size % PAGE_SIZE) {
  3247. tape->pages_per_stage++;
  3248. tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
  3249. }
  3250. /* Select the "best" DSC read/write polling freq and pipeline size. */
  3251. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  3252. tape->max_stages = speed * 1000 * 10 / tape->stage_size;
  3253. /*
  3254. * Limit memory use for pipeline to 10% of physical memory
  3255. */
  3256. si_meminfo(&si);
  3257. if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
  3258. tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
  3259. tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
  3260. tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
  3261. tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
  3262. if (tape->max_stages == 0)
  3263. tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
  3264. t1 = (tape->stage_size * HZ) / (speed * 1000);
  3265. tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
  3266. tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
  3267. if (tape->max_stages)
  3268. t = tn;
  3269. else
  3270. t = t1;
  3271. /*
  3272. * Ensure that the number we got makes sense; limit
  3273. * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  3274. */
  3275. tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
  3276. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  3277. "%dkB pipeline, %lums tDSC%s\n",
  3278. drive->name, tape->name, *(u16 *)&tape->caps[14],
  3279. (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
  3280. tape->stage_size / 1024,
  3281. tape->max_stages * tape->stage_size / 1024,
  3282. tape->best_dsc_rw_frequency * 1000 / HZ,
  3283. drive->using_dma ? ", DMA":"");
  3284. idetape_add_settings(drive);
  3285. }
  3286. static void ide_tape_remove(ide_drive_t *drive)
  3287. {
  3288. idetape_tape_t *tape = drive->driver_data;
  3289. ide_proc_unregister_driver(drive, tape->driver);
  3290. ide_unregister_region(tape->disk);
  3291. ide_tape_put(tape);
  3292. }
  3293. static void ide_tape_release(struct kref *kref)
  3294. {
  3295. struct ide_tape_obj *tape = to_ide_tape(kref);
  3296. ide_drive_t *drive = tape->drive;
  3297. struct gendisk *g = tape->disk;
  3298. BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
  3299. drive->dsc_overlap = 0;
  3300. drive->driver_data = NULL;
  3301. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  3302. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  3303. idetape_devs[tape->minor] = NULL;
  3304. g->private_data = NULL;
  3305. put_disk(g);
  3306. kfree(tape);
  3307. }
  3308. #ifdef CONFIG_IDE_PROC_FS
  3309. static int proc_idetape_read_name
  3310. (char *page, char **start, off_t off, int count, int *eof, void *data)
  3311. {
  3312. ide_drive_t *drive = (ide_drive_t *) data;
  3313. idetape_tape_t *tape = drive->driver_data;
  3314. char *out = page;
  3315. int len;
  3316. len = sprintf(out, "%s\n", tape->name);
  3317. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  3318. }
  3319. static ide_proc_entry_t idetape_proc[] = {
  3320. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  3321. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  3322. { NULL, 0, NULL, NULL }
  3323. };
  3324. #endif
  3325. static int ide_tape_probe(ide_drive_t *);
  3326. static ide_driver_t idetape_driver = {
  3327. .gen_driver = {
  3328. .owner = THIS_MODULE,
  3329. .name = "ide-tape",
  3330. .bus = &ide_bus_type,
  3331. },
  3332. .probe = ide_tape_probe,
  3333. .remove = ide_tape_remove,
  3334. .version = IDETAPE_VERSION,
  3335. .media = ide_tape,
  3336. .supports_dsc_overlap = 1,
  3337. .do_request = idetape_do_request,
  3338. .end_request = idetape_end_request,
  3339. .error = __ide_error,
  3340. .abort = __ide_abort,
  3341. #ifdef CONFIG_IDE_PROC_FS
  3342. .proc = idetape_proc,
  3343. #endif
  3344. };
  3345. /*
  3346. * Our character device supporting functions, passed to register_chrdev.
  3347. */
  3348. static const struct file_operations idetape_fops = {
  3349. .owner = THIS_MODULE,
  3350. .read = idetape_chrdev_read,
  3351. .write = idetape_chrdev_write,
  3352. .ioctl = idetape_chrdev_ioctl,
  3353. .open = idetape_chrdev_open,
  3354. .release = idetape_chrdev_release,
  3355. };
  3356. static int idetape_open(struct inode *inode, struct file *filp)
  3357. {
  3358. struct gendisk *disk = inode->i_bdev->bd_disk;
  3359. struct ide_tape_obj *tape;
  3360. if (!(tape = ide_tape_get(disk)))
  3361. return -ENXIO;
  3362. return 0;
  3363. }
  3364. static int idetape_release(struct inode *inode, struct file *filp)
  3365. {
  3366. struct gendisk *disk = inode->i_bdev->bd_disk;
  3367. struct ide_tape_obj *tape = ide_tape_g(disk);
  3368. ide_tape_put(tape);
  3369. return 0;
  3370. }
  3371. static int idetape_ioctl(struct inode *inode, struct file *file,
  3372. unsigned int cmd, unsigned long arg)
  3373. {
  3374. struct block_device *bdev = inode->i_bdev;
  3375. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  3376. ide_drive_t *drive = tape->drive;
  3377. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  3378. if (err == -EINVAL)
  3379. err = idetape_blkdev_ioctl(drive, cmd, arg);
  3380. return err;
  3381. }
  3382. static struct block_device_operations idetape_block_ops = {
  3383. .owner = THIS_MODULE,
  3384. .open = idetape_open,
  3385. .release = idetape_release,
  3386. .ioctl = idetape_ioctl,
  3387. };
  3388. static int ide_tape_probe(ide_drive_t *drive)
  3389. {
  3390. idetape_tape_t *tape;
  3391. struct gendisk *g;
  3392. int minor;
  3393. if (!strstr("ide-tape", drive->driver_req))
  3394. goto failed;
  3395. if (!drive->present)
  3396. goto failed;
  3397. if (drive->media != ide_tape)
  3398. goto failed;
  3399. if (!idetape_identify_device (drive)) {
  3400. printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
  3401. goto failed;
  3402. }
  3403. if (drive->scsi) {
  3404. printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
  3405. goto failed;
  3406. }
  3407. if (strstr(drive->id->model, "OnStream DI-")) {
  3408. printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
  3409. printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
  3410. }
  3411. tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
  3412. if (tape == NULL) {
  3413. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
  3414. goto failed;
  3415. }
  3416. g = alloc_disk(1 << PARTN_BITS);
  3417. if (!g)
  3418. goto out_free_tape;
  3419. ide_init_disk(g, drive);
  3420. ide_proc_register_driver(drive, &idetape_driver);
  3421. kref_init(&tape->kref);
  3422. tape->drive = drive;
  3423. tape->driver = &idetape_driver;
  3424. tape->disk = g;
  3425. g->private_data = &tape->driver;
  3426. drive->driver_data = tape;
  3427. mutex_lock(&idetape_ref_mutex);
  3428. for (minor = 0; idetape_devs[minor]; minor++)
  3429. ;
  3430. idetape_devs[minor] = tape;
  3431. mutex_unlock(&idetape_ref_mutex);
  3432. idetape_setup(drive, tape, minor);
  3433. device_create(idetape_sysfs_class, &drive->gendev,
  3434. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  3435. device_create(idetape_sysfs_class, &drive->gendev,
  3436. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  3437. g->fops = &idetape_block_ops;
  3438. ide_register_region(g);
  3439. return 0;
  3440. out_free_tape:
  3441. kfree(tape);
  3442. failed:
  3443. return -ENODEV;
  3444. }
  3445. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  3446. MODULE_LICENSE("GPL");
  3447. static void __exit idetape_exit (void)
  3448. {
  3449. driver_unregister(&idetape_driver.gen_driver);
  3450. class_destroy(idetape_sysfs_class);
  3451. unregister_chrdev(IDETAPE_MAJOR, "ht");
  3452. }
  3453. static int __init idetape_init(void)
  3454. {
  3455. int error = 1;
  3456. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  3457. if (IS_ERR(idetape_sysfs_class)) {
  3458. idetape_sysfs_class = NULL;
  3459. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  3460. error = -EBUSY;
  3461. goto out;
  3462. }
  3463. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  3464. printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
  3465. error = -EBUSY;
  3466. goto out_free_class;
  3467. }
  3468. error = driver_register(&idetape_driver.gen_driver);
  3469. if (error)
  3470. goto out_free_driver;
  3471. return 0;
  3472. out_free_driver:
  3473. driver_unregister(&idetape_driver.gen_driver);
  3474. out_free_class:
  3475. class_destroy(idetape_sysfs_class);
  3476. out:
  3477. return error;
  3478. }
  3479. MODULE_ALIAS("ide:*m-tape*");
  3480. module_init(idetape_init);
  3481. module_exit(idetape_exit);
  3482. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);