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