ide-tape.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  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.20"
  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 <linux/irq.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/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. * After each failed packet command we issue a request sense command and retry
  70. * the packet command IDETAPE_MAX_PC_RETRIES times.
  71. *
  72. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  73. */
  74. #define IDETAPE_MAX_PC_RETRIES 3
  75. /*
  76. * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
  77. * bytes. This is used for several packet commands (Not for READ/WRITE commands)
  78. */
  79. #define IDETAPE_PC_BUFFER_SIZE 256
  80. /*
  81. * In various places in the driver, we need to allocate storage
  82. * for packet commands and requests, which will remain valid while
  83. * we leave the driver to wait for an interrupt or a timeout event.
  84. */
  85. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  86. /*
  87. * Some drives (for example, Seagate STT3401A Travan) require a very long
  88. * timeout, because they don't return an interrupt or clear their busy bit
  89. * until after the command completes (even retension commands).
  90. */
  91. #define IDETAPE_WAIT_CMD (900*HZ)
  92. /*
  93. * The following parameter is used to select the point in the internal tape fifo
  94. * in which we will start to refill the buffer. Decreasing the following
  95. * parameter will improve the system's latency and interactive response, while
  96. * using a high value might improve system throughput.
  97. */
  98. #define IDETAPE_FIFO_THRESHOLD 2
  99. /*
  100. * DSC polling parameters.
  101. *
  102. * Polling for DSC (a single bit in the status register) is a very important
  103. * function in ide-tape. There are two cases in which we poll for DSC:
  104. *
  105. * 1. Before a read/write packet command, to ensure that we can transfer data
  106. * from/to the tape's data buffers, without causing an actual media access.
  107. * In case the tape is not ready yet, we take out our request from the device
  108. * request queue, so that ide.c could service requests from the other device
  109. * on the same interface in the meantime.
  110. *
  111. * 2. After the successful initialization of a "media access packet command",
  112. * which is a command that can take a long time to complete (the interval can
  113. * range from several seconds to even an hour). Again, we postpone our request
  114. * in the middle to free the bus for the other device. The polling frequency
  115. * here should be lower than the read/write frequency since those media access
  116. * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
  117. * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
  118. * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
  119. *
  120. * We also set a timeout for the timer, in case something goes wrong. The
  121. * timeout should be longer then the maximum execution time of a tape operation.
  122. */
  123. /* DSC timings. */
  124. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  125. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  126. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  127. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  128. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  129. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  130. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  131. /*************************** End of tunable parameters ***********************/
  132. /* Read/Write error simulation */
  133. #define SIMULATE_ERRORS 0
  134. /* tape directions */
  135. enum {
  136. IDETAPE_DIR_NONE = (1 << 0),
  137. IDETAPE_DIR_READ = (1 << 1),
  138. IDETAPE_DIR_WRITE = (1 << 2),
  139. };
  140. struct idetape_bh {
  141. u32 b_size;
  142. atomic_t b_count;
  143. struct idetape_bh *b_reqnext;
  144. char *b_data;
  145. };
  146. /* Tape door status */
  147. #define DOOR_UNLOCKED 0
  148. #define DOOR_LOCKED 1
  149. #define DOOR_EXPLICITLY_LOCKED 2
  150. /* Some defines for the SPACE command */
  151. #define IDETAPE_SPACE_OVER_FILEMARK 1
  152. #define IDETAPE_SPACE_TO_EOD 3
  153. /* Some defines for the LOAD UNLOAD command */
  154. #define IDETAPE_LU_LOAD_MASK 1
  155. #define IDETAPE_LU_RETENSION_MASK 2
  156. #define IDETAPE_LU_EOT_MASK 4
  157. /*
  158. * Special requests for our block device strategy routine.
  159. *
  160. * In order to service a character device command, we add special requests to
  161. * the tail of our block device request queue and wait for their completion.
  162. */
  163. enum {
  164. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  165. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  166. REQ_IDETAPE_READ = (1 << 2),
  167. REQ_IDETAPE_WRITE = (1 << 3),
  168. };
  169. /* Error codes returned in rq->errors to the higher part of the driver. */
  170. #define IDETAPE_ERROR_GENERAL 101
  171. #define IDETAPE_ERROR_FILEMARK 102
  172. #define IDETAPE_ERROR_EOD 103
  173. /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
  174. #define IDETAPE_BLOCK_DESCRIPTOR 0
  175. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  176. /* Tape flag bits values. */
  177. enum {
  178. IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
  179. /* 0 When the tape position is unknown */
  180. IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
  181. /* Device already opened */
  182. IDETAPE_FLAG_BUSY = (1 << 2),
  183. /* Attempt to auto-detect the current user block size */
  184. IDETAPE_FLAG_DETECT_BS = (1 << 3),
  185. /* Currently on a filemark */
  186. IDETAPE_FLAG_FILEMARK = (1 << 4),
  187. /* DRQ interrupt device */
  188. IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 5),
  189. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  190. IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 6),
  191. };
  192. /*
  193. * Most of our global data which we need to save even as we leave the driver due
  194. * to an interrupt or a timer event is stored in the struct defined below.
  195. */
  196. typedef struct ide_tape_obj {
  197. ide_drive_t *drive;
  198. ide_driver_t *driver;
  199. struct gendisk *disk;
  200. struct kref kref;
  201. /*
  202. * Since a typical character device operation requires more
  203. * than one packet command, we provide here enough memory
  204. * for the maximum of interconnected packet commands.
  205. * The packet commands are stored in the circular array pc_stack.
  206. * pc_stack_index points to the last used entry, and warps around
  207. * to the start when we get to the last array entry.
  208. *
  209. * pc points to the current processed packet command.
  210. *
  211. * failed_pc points to the last failed packet command, or contains
  212. * NULL if we do not need to retry any packet command. This is
  213. * required since an additional packet command is needed before the
  214. * retry, to get detailed information on what went wrong.
  215. */
  216. /* Current packet command */
  217. struct ide_atapi_pc *pc;
  218. /* Last failed packet command */
  219. struct ide_atapi_pc *failed_pc;
  220. /* Packet command stack */
  221. struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
  222. /* Next free packet command storage space */
  223. int pc_stack_index;
  224. struct request rq_stack[IDETAPE_PC_STACK];
  225. /* We implement a circular array */
  226. int rq_stack_index;
  227. /*
  228. * DSC polling variables.
  229. *
  230. * While polling for DSC we use postponed_rq to postpone the current
  231. * request so that ide.c will be able to service pending requests on the
  232. * other device. Note that at most we will have only one DSC (usually
  233. * data transfer) request in the device request queue.
  234. */
  235. struct request *postponed_rq;
  236. /* The time in which we started polling for DSC */
  237. unsigned long dsc_polling_start;
  238. /* Timer used to poll for dsc */
  239. struct timer_list dsc_timer;
  240. /* Read/Write dsc polling frequency */
  241. unsigned long best_dsc_rw_freq;
  242. unsigned long dsc_poll_freq;
  243. unsigned long dsc_timeout;
  244. /* Read position information */
  245. u8 partition;
  246. /* Current block */
  247. unsigned int first_frame;
  248. /* Last error information */
  249. u8 sense_key, asc, ascq;
  250. /* Character device operation */
  251. unsigned int minor;
  252. /* device name */
  253. char name[4];
  254. /* Current character device data transfer direction */
  255. u8 chrdev_dir;
  256. /* tape block size, usually 512 or 1024 bytes */
  257. unsigned short blk_size;
  258. int user_bs_factor;
  259. /* Copy of the tape's Capabilities and Mechanical Page */
  260. u8 caps[20];
  261. /*
  262. * Active data transfer request parameters.
  263. *
  264. * At most, there is only one ide-tape originated data transfer request
  265. * in the device request queue. This allows ide.c to easily service
  266. * requests from the other device when we postpone our active request.
  267. */
  268. /* Data buffer size chosen based on the tape's recommendation */
  269. int buffer_size;
  270. /* merge buffer */
  271. struct idetape_bh *merge_bh;
  272. /* size of the merge buffer */
  273. int merge_bh_size;
  274. /* pointer to current buffer head within the merge buffer */
  275. struct idetape_bh *bh;
  276. char *b_data;
  277. int b_count;
  278. int pages_per_buffer;
  279. /* Wasted space in each stage */
  280. int excess_bh_size;
  281. /* Status/Action flags: long for set_bit */
  282. unsigned long flags;
  283. /* protects the ide-tape queue */
  284. spinlock_t lock;
  285. /* Measures average tape speed */
  286. unsigned long avg_time;
  287. int avg_size;
  288. int avg_speed;
  289. /* the door is currently locked */
  290. int door_locked;
  291. /* the tape hardware is write protected */
  292. char drv_write_prot;
  293. /* the tape is write protected (hardware or opened as read-only) */
  294. char write_prot;
  295. u32 debug_mask;
  296. } idetape_tape_t;
  297. static DEFINE_MUTEX(idetape_ref_mutex);
  298. static struct class *idetape_sysfs_class;
  299. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  300. #define ide_tape_g(disk) \
  301. container_of((disk)->private_data, struct ide_tape_obj, driver)
  302. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  303. {
  304. struct ide_tape_obj *tape = NULL;
  305. mutex_lock(&idetape_ref_mutex);
  306. tape = ide_tape_g(disk);
  307. if (tape)
  308. kref_get(&tape->kref);
  309. mutex_unlock(&idetape_ref_mutex);
  310. return tape;
  311. }
  312. static void ide_tape_release(struct kref *);
  313. static void ide_tape_put(struct ide_tape_obj *tape)
  314. {
  315. mutex_lock(&idetape_ref_mutex);
  316. kref_put(&tape->kref, ide_tape_release);
  317. mutex_unlock(&idetape_ref_mutex);
  318. }
  319. /*
  320. * The variables below are used for the character device interface. Additional
  321. * state variables are defined in our ide_drive_t structure.
  322. */
  323. static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
  324. #define ide_tape_f(file) ((file)->private_data)
  325. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  326. {
  327. struct ide_tape_obj *tape = NULL;
  328. mutex_lock(&idetape_ref_mutex);
  329. tape = idetape_devs[i];
  330. if (tape)
  331. kref_get(&tape->kref);
  332. mutex_unlock(&idetape_ref_mutex);
  333. return tape;
  334. }
  335. static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  336. unsigned int bcount)
  337. {
  338. struct idetape_bh *bh = pc->bh;
  339. int count;
  340. while (bcount) {
  341. if (bh == NULL) {
  342. printk(KERN_ERR "ide-tape: bh == NULL in "
  343. "idetape_input_buffers\n");
  344. ide_pad_transfer(drive, 0, bcount);
  345. return;
  346. }
  347. count = min(
  348. (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
  349. bcount);
  350. drive->hwif->input_data(drive, NULL, bh->b_data +
  351. atomic_read(&bh->b_count), count);
  352. bcount -= count;
  353. atomic_add(count, &bh->b_count);
  354. if (atomic_read(&bh->b_count) == bh->b_size) {
  355. bh = bh->b_reqnext;
  356. if (bh)
  357. atomic_set(&bh->b_count, 0);
  358. }
  359. }
  360. pc->bh = bh;
  361. }
  362. static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  363. unsigned int bcount)
  364. {
  365. struct idetape_bh *bh = pc->bh;
  366. int count;
  367. while (bcount) {
  368. if (bh == NULL) {
  369. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  370. __func__);
  371. return;
  372. }
  373. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  374. drive->hwif->output_data(drive, NULL, pc->b_data, count);
  375. bcount -= count;
  376. pc->b_data += count;
  377. pc->b_count -= count;
  378. if (!pc->b_count) {
  379. bh = bh->b_reqnext;
  380. pc->bh = bh;
  381. if (bh) {
  382. pc->b_data = bh->b_data;
  383. pc->b_count = atomic_read(&bh->b_count);
  384. }
  385. }
  386. }
  387. }
  388. static void idetape_update_buffers(struct ide_atapi_pc *pc)
  389. {
  390. struct idetape_bh *bh = pc->bh;
  391. int count;
  392. unsigned int bcount = pc->xferred;
  393. if (pc->flags & PC_FLAG_WRITING)
  394. return;
  395. while (bcount) {
  396. if (bh == NULL) {
  397. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  398. __func__);
  399. return;
  400. }
  401. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  402. atomic_set(&bh->b_count, count);
  403. if (atomic_read(&bh->b_count) == bh->b_size)
  404. bh = bh->b_reqnext;
  405. bcount -= count;
  406. }
  407. pc->bh = bh;
  408. }
  409. /*
  410. * idetape_next_pc_storage returns a pointer to a place in which we can
  411. * safely store a packet command, even though we intend to leave the
  412. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  413. * commands is allocated at initialization time.
  414. */
  415. static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
  416. {
  417. idetape_tape_t *tape = drive->driver_data;
  418. debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
  419. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  420. tape->pc_stack_index = 0;
  421. return (&tape->pc_stack[tape->pc_stack_index++]);
  422. }
  423. /*
  424. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  425. * Since we queue packet commands in the request queue, we need to
  426. * allocate a request, along with the allocation of a packet command.
  427. */
  428. /**************************************************************
  429. * *
  430. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  431. * followed later on by kfree(). -ml *
  432. * *
  433. **************************************************************/
  434. static struct request *idetape_next_rq_storage(ide_drive_t *drive)
  435. {
  436. idetape_tape_t *tape = drive->driver_data;
  437. debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
  438. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  439. tape->rq_stack_index = 0;
  440. return (&tape->rq_stack[tape->rq_stack_index++]);
  441. }
  442. static void idetape_init_pc(struct ide_atapi_pc *pc)
  443. {
  444. memset(pc->c, 0, 12);
  445. pc->retries = 0;
  446. pc->flags = 0;
  447. pc->req_xfer = 0;
  448. pc->buf = pc->pc_buf;
  449. pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
  450. pc->bh = NULL;
  451. pc->b_data = NULL;
  452. }
  453. /*
  454. * called on each failed packet command retry to analyze the request sense. We
  455. * currently do not utilize this information.
  456. */
  457. static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
  458. {
  459. idetape_tape_t *tape = drive->driver_data;
  460. struct ide_atapi_pc *pc = tape->failed_pc;
  461. tape->sense_key = sense[2] & 0xF;
  462. tape->asc = sense[12];
  463. tape->ascq = sense[13];
  464. debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
  465. pc->c[0], tape->sense_key, tape->asc, tape->ascq);
  466. /* Correct pc->xferred by asking the tape. */
  467. if (pc->flags & PC_FLAG_DMA_ERROR) {
  468. pc->xferred = pc->req_xfer -
  469. tape->blk_size *
  470. get_unaligned_be32(&sense[3]);
  471. idetape_update_buffers(pc);
  472. }
  473. /*
  474. * If error was the result of a zero-length read or write command,
  475. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  476. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  477. */
  478. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  479. /* length == 0 */
  480. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  481. if (tape->sense_key == 5) {
  482. /* don't report an error, everything's ok */
  483. pc->error = 0;
  484. /* don't retry read/write */
  485. pc->flags |= PC_FLAG_ABORT;
  486. }
  487. }
  488. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  489. pc->error = IDETAPE_ERROR_FILEMARK;
  490. pc->flags |= PC_FLAG_ABORT;
  491. }
  492. if (pc->c[0] == WRITE_6) {
  493. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  494. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  495. pc->error = IDETAPE_ERROR_EOD;
  496. pc->flags |= PC_FLAG_ABORT;
  497. }
  498. }
  499. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  500. if (tape->sense_key == 8) {
  501. pc->error = IDETAPE_ERROR_EOD;
  502. pc->flags |= PC_FLAG_ABORT;
  503. }
  504. if (!(pc->flags & PC_FLAG_ABORT) &&
  505. pc->xferred)
  506. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  507. }
  508. }
  509. /* Free data buffers completely. */
  510. static void ide_tape_kfree_buffer(idetape_tape_t *tape)
  511. {
  512. struct idetape_bh *prev_bh, *bh = tape->merge_bh;
  513. while (bh) {
  514. u32 size = bh->b_size;
  515. while (size) {
  516. unsigned int order = fls(size >> PAGE_SHIFT)-1;
  517. if (bh->b_data)
  518. free_pages((unsigned long)bh->b_data, order);
  519. size &= (order-1);
  520. bh->b_data += (1 << order) * PAGE_SIZE;
  521. }
  522. prev_bh = bh;
  523. bh = bh->b_reqnext;
  524. kfree(prev_bh);
  525. }
  526. kfree(tape->merge_bh);
  527. }
  528. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  529. {
  530. struct request *rq = HWGROUP(drive)->rq;
  531. idetape_tape_t *tape = drive->driver_data;
  532. unsigned long flags;
  533. int error;
  534. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  535. switch (uptodate) {
  536. case 0: error = IDETAPE_ERROR_GENERAL; break;
  537. case 1: error = 0; break;
  538. default: error = uptodate;
  539. }
  540. rq->errors = error;
  541. if (error)
  542. tape->failed_pc = NULL;
  543. if (!blk_special_request(rq)) {
  544. ide_end_request(drive, uptodate, nr_sects);
  545. return 0;
  546. }
  547. spin_lock_irqsave(&tape->lock, flags);
  548. ide_end_drive_cmd(drive, 0, 0);
  549. spin_unlock_irqrestore(&tape->lock, flags);
  550. return 0;
  551. }
  552. static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
  553. {
  554. idetape_tape_t *tape = drive->driver_data;
  555. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  556. if (!tape->pc->error) {
  557. idetape_analyze_error(drive, tape->pc->buf);
  558. idetape_end_request(drive, 1, 0);
  559. } else {
  560. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
  561. "Aborting request!\n");
  562. idetape_end_request(drive, 0, 0);
  563. }
  564. return ide_stopped;
  565. }
  566. static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
  567. {
  568. idetape_init_pc(pc);
  569. pc->c[0] = REQUEST_SENSE;
  570. pc->c[4] = 20;
  571. pc->req_xfer = 20;
  572. pc->idetape_callback = &idetape_request_sense_callback;
  573. }
  574. static void idetape_init_rq(struct request *rq, u8 cmd)
  575. {
  576. blk_rq_init(NULL, rq);
  577. rq->cmd_type = REQ_TYPE_SPECIAL;
  578. rq->cmd[0] = cmd;
  579. }
  580. /*
  581. * Generate a new packet command request in front of the request queue, before
  582. * the current request, so that it will be processed immediately, on the next
  583. * pass through the driver. The function below is called from the request
  584. * handling part of the driver (the "bottom" part). Safe storage for the request
  585. * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
  586. *
  587. * Memory for those requests is pre-allocated at initialization time, and is
  588. * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
  589. * the maximum possible number of inter-dependent packet commands.
  590. *
  591. * The higher level of the driver - The ioctl handler and the character device
  592. * handling functions should queue request to the lower level part and wait for
  593. * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
  594. */
  595. static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
  596. struct request *rq)
  597. {
  598. struct ide_tape_obj *tape = drive->driver_data;
  599. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  600. rq->cmd_flags |= REQ_PREEMPT;
  601. rq->buffer = (char *) pc;
  602. rq->rq_disk = tape->disk;
  603. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  604. }
  605. /*
  606. * idetape_retry_pc is called when an error was detected during the
  607. * last packet command. We queue a request sense packet command in
  608. * the head of the request list.
  609. */
  610. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  611. {
  612. idetape_tape_t *tape = drive->driver_data;
  613. struct ide_atapi_pc *pc;
  614. struct request *rq;
  615. (void)ide_read_error(drive);
  616. pc = idetape_next_pc_storage(drive);
  617. rq = idetape_next_rq_storage(drive);
  618. idetape_create_request_sense_cmd(pc);
  619. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  620. idetape_queue_pc_head(drive, pc, rq);
  621. return ide_stopped;
  622. }
  623. /*
  624. * Postpone the current request so that ide.c will be able to service requests
  625. * from another device on the same hwgroup while we are polling for DSC.
  626. */
  627. static void idetape_postpone_request(ide_drive_t *drive)
  628. {
  629. idetape_tape_t *tape = drive->driver_data;
  630. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  631. tape->postponed_rq = HWGROUP(drive)->rq;
  632. ide_stall_queue(drive, tape->dsc_poll_freq);
  633. }
  634. typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
  635. /*
  636. * This is the usual interrupt handler which will be called during a packet
  637. * command. We will transfer some of the data (as requested by the drive) and
  638. * will re-point interrupt handler to us. When data transfer is finished, we
  639. * will act according to the algorithm described before
  640. * idetape_issue_pc.
  641. */
  642. static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
  643. {
  644. ide_hwif_t *hwif = drive->hwif;
  645. idetape_tape_t *tape = drive->driver_data;
  646. struct ide_atapi_pc *pc = tape->pc;
  647. xfer_func_t *xferfunc;
  648. idetape_io_buf *iobuf;
  649. unsigned int temp;
  650. #if SIMULATE_ERRORS
  651. static int error_sim_count;
  652. #endif
  653. u16 bcount;
  654. u8 stat, ireason;
  655. debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
  656. /* Clear the interrupt */
  657. stat = ide_read_status(drive);
  658. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  659. if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
  660. /*
  661. * A DMA error is sometimes expected. For example,
  662. * if the tape is crossing a filemark during a
  663. * READ command, it will issue an irq and position
  664. * itself before the filemark, so that only a partial
  665. * data transfer will occur (which causes the DMA
  666. * error). In that case, we will later ask the tape
  667. * how much bytes of the original request were
  668. * actually transferred (we can't receive that
  669. * information from the DMA engine on most chipsets).
  670. */
  671. /*
  672. * On the contrary, a DMA error is never expected;
  673. * it usually indicates a hardware error or abort.
  674. * If the tape crosses a filemark during a READ
  675. * command, it will issue an irq and position itself
  676. * after the filemark (not before). Only a partial
  677. * data transfer will occur, but no DMA error.
  678. * (AS, 19 Apr 2001)
  679. */
  680. pc->flags |= PC_FLAG_DMA_ERROR;
  681. } else {
  682. pc->xferred = pc->req_xfer;
  683. idetape_update_buffers(pc);
  684. }
  685. debug_log(DBG_PROCS, "DMA finished\n");
  686. }
  687. /* No more interrupts */
  688. if ((stat & DRQ_STAT) == 0) {
  689. debug_log(DBG_SENSE, "Packet command completed, %d bytes"
  690. " transferred\n", pc->xferred);
  691. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  692. local_irq_enable();
  693. #if SIMULATE_ERRORS
  694. if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
  695. (++error_sim_count % 100) == 0) {
  696. printk(KERN_INFO "ide-tape: %s: simulating error\n",
  697. tape->name);
  698. stat |= ERR_STAT;
  699. }
  700. #endif
  701. if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
  702. stat &= ~ERR_STAT;
  703. if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  704. /* Error detected */
  705. debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
  706. if (pc->c[0] == REQUEST_SENSE) {
  707. printk(KERN_ERR "ide-tape: I/O error in request"
  708. " sense command\n");
  709. return ide_do_reset(drive);
  710. }
  711. debug_log(DBG_ERR, "[cmd %x]: check condition\n",
  712. pc->c[0]);
  713. /* Retry operation */
  714. return idetape_retry_pc(drive);
  715. }
  716. pc->error = 0;
  717. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
  718. (stat & SEEK_STAT) == 0) {
  719. /* Media access command */
  720. tape->dsc_polling_start = jiffies;
  721. tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
  722. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  723. /* Allow ide.c to handle other requests */
  724. idetape_postpone_request(drive);
  725. return ide_stopped;
  726. }
  727. if (tape->failed_pc == pc)
  728. tape->failed_pc = NULL;
  729. /* Command finished - Call the callback function */
  730. return pc->idetape_callback(drive);
  731. }
  732. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  733. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  734. printk(KERN_ERR "ide-tape: The tape wants to issue more "
  735. "interrupts in DMA mode\n");
  736. printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
  737. ide_dma_off(drive);
  738. return ide_do_reset(drive);
  739. }
  740. /* Get the number of bytes to transfer on this interrupt. */
  741. bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
  742. hwif->INB(hwif->io_ports.lbam_addr);
  743. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  744. if (ireason & CD) {
  745. printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
  746. return ide_do_reset(drive);
  747. }
  748. if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
  749. /* Hopefully, we will never get here */
  750. printk(KERN_ERR "ide-tape: We wanted to %s, ",
  751. (ireason & IO) ? "Write" : "Read");
  752. printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
  753. (ireason & IO) ? "Read" : "Write");
  754. return ide_do_reset(drive);
  755. }
  756. if (!(pc->flags & PC_FLAG_WRITING)) {
  757. /* Reading - Check that we have enough space */
  758. temp = pc->xferred + bcount;
  759. if (temp > pc->req_xfer) {
  760. if (temp > pc->buf_size) {
  761. printk(KERN_ERR "ide-tape: The tape wants to "
  762. "send us more data than expected "
  763. "- discarding data\n");
  764. ide_pad_transfer(drive, 0, bcount);
  765. ide_set_handler(drive, &idetape_pc_intr,
  766. IDETAPE_WAIT_CMD, NULL);
  767. return ide_started;
  768. }
  769. debug_log(DBG_SENSE, "The tape wants to send us more "
  770. "data than expected - allowing transfer\n");
  771. }
  772. iobuf = &idetape_input_buffers;
  773. xferfunc = hwif->input_data;
  774. } else {
  775. iobuf = &idetape_output_buffers;
  776. xferfunc = hwif->output_data;
  777. }
  778. if (pc->bh)
  779. iobuf(drive, pc, bcount);
  780. else
  781. xferfunc(drive, NULL, pc->cur_pos, bcount);
  782. /* Update the current position */
  783. pc->xferred += bcount;
  784. pc->cur_pos += bcount;
  785. debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
  786. pc->c[0], bcount);
  787. /* And set the interrupt handler again */
  788. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  789. return ide_started;
  790. }
  791. /*
  792. * Packet Command Interface
  793. *
  794. * The current Packet Command is available in tape->pc, and will not change
  795. * until we finish handling it. Each packet command is associated with a
  796. * callback function that will be called when the command is finished.
  797. *
  798. * The handling will be done in three stages:
  799. *
  800. * 1. idetape_issue_pc will send the packet command to the drive, and will set
  801. * the interrupt handler to idetape_pc_intr.
  802. *
  803. * 2. On each interrupt, idetape_pc_intr will be called. This step will be
  804. * repeated until the device signals us that no more interrupts will be issued.
  805. *
  806. * 3. ATAPI Tape media access commands have immediate status with a delayed
  807. * process. In case of a successful initiation of a media access packet command,
  808. * the DSC bit will be set when the actual execution of the command is finished.
  809. * Since the tape drive will not issue an interrupt, we have to poll for this
  810. * event. In this case, we define the request as "low priority request" by
  811. * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
  812. * exit the driver.
  813. *
  814. * ide.c will then give higher priority to requests which originate from the
  815. * other device, until will change rq_status to RQ_ACTIVE.
  816. *
  817. * 4. When the packet command is finished, it will be checked for errors.
  818. *
  819. * 5. In case an error was found, we queue a request sense packet command in
  820. * front of the request queue and retry the operation up to
  821. * IDETAPE_MAX_PC_RETRIES times.
  822. *
  823. * 6. In case no error was found, or we decided to give up and not to retry
  824. * again, the callback function will be called and then we will handle the next
  825. * request.
  826. */
  827. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  828. {
  829. ide_hwif_t *hwif = drive->hwif;
  830. idetape_tape_t *tape = drive->driver_data;
  831. struct ide_atapi_pc *pc = tape->pc;
  832. int retries = 100;
  833. ide_startstop_t startstop;
  834. u8 ireason;
  835. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  836. printk(KERN_ERR "ide-tape: Strange, packet command initiated "
  837. "yet DRQ isn't asserted\n");
  838. return startstop;
  839. }
  840. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  841. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  842. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
  843. "a packet command, retrying\n");
  844. udelay(100);
  845. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  846. if (retries == 0) {
  847. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
  848. "issuing a packet command, ignoring\n");
  849. ireason |= CD;
  850. ireason &= ~IO;
  851. }
  852. }
  853. if ((ireason & CD) == 0 || (ireason & IO)) {
  854. printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
  855. "a packet command\n");
  856. return ide_do_reset(drive);
  857. }
  858. /* Set the interrupt routine */
  859. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  860. #ifdef CONFIG_BLK_DEV_IDEDMA
  861. /* Begin DMA, if necessary */
  862. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
  863. hwif->dma_ops->dma_start(drive);
  864. #endif
  865. /* Send the actual packet */
  866. hwif->output_data(drive, NULL, pc->c, 12);
  867. return ide_started;
  868. }
  869. static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
  870. struct ide_atapi_pc *pc)
  871. {
  872. ide_hwif_t *hwif = drive->hwif;
  873. idetape_tape_t *tape = drive->driver_data;
  874. int dma_ok = 0;
  875. u16 bcount;
  876. if (tape->pc->c[0] == REQUEST_SENSE &&
  877. pc->c[0] == REQUEST_SENSE) {
  878. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  879. "Two request sense in serial were issued\n");
  880. }
  881. if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
  882. tape->failed_pc = pc;
  883. /* Set the current packet command */
  884. tape->pc = pc;
  885. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  886. (pc->flags & PC_FLAG_ABORT)) {
  887. /*
  888. * We will "abort" retrying a packet command in case legitimate
  889. * error code was received (crossing a filemark, or end of the
  890. * media, for example).
  891. */
  892. if (!(pc->flags & PC_FLAG_ABORT)) {
  893. if (!(pc->c[0] == TEST_UNIT_READY &&
  894. tape->sense_key == 2 && tape->asc == 4 &&
  895. (tape->ascq == 1 || tape->ascq == 8))) {
  896. printk(KERN_ERR "ide-tape: %s: I/O error, "
  897. "pc = %2x, key = %2x, "
  898. "asc = %2x, ascq = %2x\n",
  899. tape->name, pc->c[0],
  900. tape->sense_key, tape->asc,
  901. tape->ascq);
  902. }
  903. /* Giving up */
  904. pc->error = IDETAPE_ERROR_GENERAL;
  905. }
  906. tape->failed_pc = NULL;
  907. return pc->idetape_callback(drive);
  908. }
  909. debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
  910. pc->retries++;
  911. /* We haven't transferred any data yet */
  912. pc->xferred = 0;
  913. pc->cur_pos = pc->buf;
  914. /* Request to transfer the entire buffer at once */
  915. bcount = pc->req_xfer;
  916. if (pc->flags & PC_FLAG_DMA_ERROR) {
  917. pc->flags &= ~PC_FLAG_DMA_ERROR;
  918. printk(KERN_WARNING "ide-tape: DMA disabled, "
  919. "reverting to PIO\n");
  920. ide_dma_off(drive);
  921. }
  922. if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
  923. dma_ok = !hwif->dma_ops->dma_setup(drive);
  924. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
  925. IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
  926. if (dma_ok)
  927. /* Will begin DMA later */
  928. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  929. if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
  930. ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
  931. IDETAPE_WAIT_CMD, NULL);
  932. return ide_started;
  933. } else {
  934. ide_execute_pkt_cmd(drive);
  935. return idetape_transfer_pc(drive);
  936. }
  937. }
  938. static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
  939. {
  940. idetape_tape_t *tape = drive->driver_data;
  941. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  942. idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
  943. return ide_stopped;
  944. }
  945. /* A mode sense command is used to "sense" tape parameters. */
  946. static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  947. {
  948. idetape_init_pc(pc);
  949. pc->c[0] = MODE_SENSE;
  950. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  951. /* DBD = 1 - Don't return block descriptors */
  952. pc->c[1] = 8;
  953. pc->c[2] = page_code;
  954. /*
  955. * Changed pc->c[3] to 0 (255 will at best return unused info).
  956. *
  957. * For SCSI this byte is defined as subpage instead of high byte
  958. * of length and some IDE drives seem to interpret it this way
  959. * and return an error when 255 is used.
  960. */
  961. pc->c[3] = 0;
  962. /* We will just discard data in that case */
  963. pc->c[4] = 255;
  964. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  965. pc->req_xfer = 12;
  966. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  967. pc->req_xfer = 24;
  968. else
  969. pc->req_xfer = 50;
  970. pc->idetape_callback = &idetape_pc_callback;
  971. }
  972. static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
  973. {
  974. idetape_tape_t *tape = drive->driver_data;
  975. struct ide_atapi_pc *pc = tape->pc;
  976. u8 stat;
  977. stat = ide_read_status(drive);
  978. if (stat & SEEK_STAT) {
  979. if (stat & ERR_STAT) {
  980. /* Error detected */
  981. if (pc->c[0] != TEST_UNIT_READY)
  982. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  983. tape->name);
  984. /* Retry operation */
  985. return idetape_retry_pc(drive);
  986. }
  987. pc->error = 0;
  988. if (tape->failed_pc == pc)
  989. tape->failed_pc = NULL;
  990. } else {
  991. pc->error = IDETAPE_ERROR_GENERAL;
  992. tape->failed_pc = NULL;
  993. }
  994. return pc->idetape_callback(drive);
  995. }
  996. static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
  997. {
  998. idetape_tape_t *tape = drive->driver_data;
  999. struct request *rq = HWGROUP(drive)->rq;
  1000. int blocks = tape->pc->xferred / tape->blk_size;
  1001. tape->avg_size += blocks * tape->blk_size;
  1002. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  1003. tape->avg_speed = tape->avg_size * HZ /
  1004. (jiffies - tape->avg_time) / 1024;
  1005. tape->avg_size = 0;
  1006. tape->avg_time = jiffies;
  1007. }
  1008. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1009. tape->first_frame += blocks;
  1010. rq->current_nr_sectors -= blocks;
  1011. if (!tape->pc->error)
  1012. idetape_end_request(drive, 1, 0);
  1013. else
  1014. idetape_end_request(drive, tape->pc->error, 0);
  1015. return ide_stopped;
  1016. }
  1017. static void idetape_create_read_cmd(idetape_tape_t *tape,
  1018. struct ide_atapi_pc *pc,
  1019. unsigned int length, struct idetape_bh *bh)
  1020. {
  1021. idetape_init_pc(pc);
  1022. pc->c[0] = READ_6;
  1023. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  1024. pc->c[1] = 1;
  1025. pc->idetape_callback = &idetape_rw_callback;
  1026. pc->bh = bh;
  1027. atomic_set(&bh->b_count, 0);
  1028. pc->buf = NULL;
  1029. pc->buf_size = length * tape->blk_size;
  1030. pc->req_xfer = pc->buf_size;
  1031. if (pc->req_xfer == tape->buffer_size)
  1032. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  1033. }
  1034. static void idetape_create_write_cmd(idetape_tape_t *tape,
  1035. struct ide_atapi_pc *pc,
  1036. unsigned int length, struct idetape_bh *bh)
  1037. {
  1038. idetape_init_pc(pc);
  1039. pc->c[0] = WRITE_6;
  1040. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  1041. pc->c[1] = 1;
  1042. pc->idetape_callback = &idetape_rw_callback;
  1043. pc->flags |= PC_FLAG_WRITING;
  1044. pc->bh = bh;
  1045. pc->b_data = bh->b_data;
  1046. pc->b_count = atomic_read(&bh->b_count);
  1047. pc->buf = NULL;
  1048. pc->buf_size = length * tape->blk_size;
  1049. pc->req_xfer = pc->buf_size;
  1050. if (pc->req_xfer == tape->buffer_size)
  1051. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  1052. }
  1053. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  1054. struct request *rq, sector_t block)
  1055. {
  1056. idetape_tape_t *tape = drive->driver_data;
  1057. struct ide_atapi_pc *pc = NULL;
  1058. struct request *postponed_rq = tape->postponed_rq;
  1059. u8 stat;
  1060. debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
  1061. " current_nr_sectors: %d\n",
  1062. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  1063. if (!blk_special_request(rq)) {
  1064. /* We do not support buffer cache originated requests. */
  1065. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  1066. "request queue (%d)\n", drive->name, rq->cmd_type);
  1067. ide_end_request(drive, 0, 0);
  1068. return ide_stopped;
  1069. }
  1070. /* Retry a failed packet command */
  1071. if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
  1072. return idetape_issue_pc(drive, tape->failed_pc);
  1073. if (postponed_rq != NULL)
  1074. if (rq != postponed_rq) {
  1075. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  1076. "Two DSC requests were queued\n");
  1077. idetape_end_request(drive, 0, 0);
  1078. return ide_stopped;
  1079. }
  1080. tape->postponed_rq = NULL;
  1081. /*
  1082. * If the tape is still busy, postpone our request and service
  1083. * the other device meanwhile.
  1084. */
  1085. stat = ide_read_status(drive);
  1086. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  1087. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  1088. if (drive->post_reset == 1) {
  1089. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  1090. drive->post_reset = 0;
  1091. }
  1092. if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
  1093. (stat & SEEK_STAT) == 0) {
  1094. if (postponed_rq == NULL) {
  1095. tape->dsc_polling_start = jiffies;
  1096. tape->dsc_poll_freq = tape->best_dsc_rw_freq;
  1097. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  1098. } else if (time_after(jiffies, tape->dsc_timeout)) {
  1099. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  1100. tape->name);
  1101. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1102. idetape_media_access_finished(drive);
  1103. return ide_stopped;
  1104. } else {
  1105. return ide_do_reset(drive);
  1106. }
  1107. } else if (time_after(jiffies,
  1108. tape->dsc_polling_start +
  1109. IDETAPE_DSC_MA_THRESHOLD))
  1110. tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
  1111. idetape_postpone_request(drive);
  1112. return ide_stopped;
  1113. }
  1114. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1115. pc = idetape_next_pc_storage(drive);
  1116. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
  1117. (struct idetape_bh *)rq->special);
  1118. goto out;
  1119. }
  1120. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1121. pc = idetape_next_pc_storage(drive);
  1122. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
  1123. (struct idetape_bh *)rq->special);
  1124. goto out;
  1125. }
  1126. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  1127. pc = (struct ide_atapi_pc *) rq->buffer;
  1128. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  1129. rq->cmd[0] |= REQ_IDETAPE_PC2;
  1130. goto out;
  1131. }
  1132. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1133. idetape_media_access_finished(drive);
  1134. return ide_stopped;
  1135. }
  1136. BUG();
  1137. out:
  1138. return idetape_issue_pc(drive, pc);
  1139. }
  1140. /*
  1141. * The function below uses __get_free_pages to allocate a data buffer of size
  1142. * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
  1143. * much as possible.
  1144. *
  1145. * It returns a pointer to the newly allocated buffer, or NULL in case of
  1146. * failure.
  1147. */
  1148. static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
  1149. int full, int clear)
  1150. {
  1151. struct idetape_bh *prev_bh, *bh, *merge_bh;
  1152. int pages = tape->pages_per_buffer;
  1153. unsigned int order, b_allocd;
  1154. char *b_data = NULL;
  1155. merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1156. bh = merge_bh;
  1157. if (bh == NULL)
  1158. goto abort;
  1159. order = fls(pages) - 1;
  1160. bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  1161. if (!bh->b_data)
  1162. goto abort;
  1163. b_allocd = (1 << order) * PAGE_SIZE;
  1164. pages &= (order-1);
  1165. if (clear)
  1166. memset(bh->b_data, 0, b_allocd);
  1167. bh->b_reqnext = NULL;
  1168. bh->b_size = b_allocd;
  1169. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1170. while (pages) {
  1171. order = fls(pages) - 1;
  1172. b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  1173. if (!b_data)
  1174. goto abort;
  1175. b_allocd = (1 << order) * PAGE_SIZE;
  1176. if (clear)
  1177. memset(b_data, 0, b_allocd);
  1178. /* newly allocated page frames below buffer header or ...*/
  1179. if (bh->b_data == b_data + b_allocd) {
  1180. bh->b_size += b_allocd;
  1181. bh->b_data -= b_allocd;
  1182. if (full)
  1183. atomic_add(b_allocd, &bh->b_count);
  1184. continue;
  1185. }
  1186. /* they are above the header */
  1187. if (b_data == bh->b_data + bh->b_size) {
  1188. bh->b_size += b_allocd;
  1189. if (full)
  1190. atomic_add(b_allocd, &bh->b_count);
  1191. continue;
  1192. }
  1193. prev_bh = bh;
  1194. bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1195. if (!bh) {
  1196. free_pages((unsigned long) b_data, order);
  1197. goto abort;
  1198. }
  1199. bh->b_reqnext = NULL;
  1200. bh->b_data = b_data;
  1201. bh->b_size = b_allocd;
  1202. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1203. prev_bh->b_reqnext = bh;
  1204. pages &= (order-1);
  1205. }
  1206. bh->b_size -= tape->excess_bh_size;
  1207. if (full)
  1208. atomic_sub(tape->excess_bh_size, &bh->b_count);
  1209. return merge_bh;
  1210. abort:
  1211. ide_tape_kfree_buffer(tape);
  1212. return NULL;
  1213. }
  1214. static int idetape_copy_stage_from_user(idetape_tape_t *tape,
  1215. const char __user *buf, int n)
  1216. {
  1217. struct idetape_bh *bh = tape->bh;
  1218. int count;
  1219. int ret = 0;
  1220. while (n) {
  1221. if (bh == NULL) {
  1222. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1223. __func__);
  1224. return 1;
  1225. }
  1226. count = min((unsigned int)
  1227. (bh->b_size - atomic_read(&bh->b_count)),
  1228. (unsigned int)n);
  1229. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
  1230. count))
  1231. ret = 1;
  1232. n -= count;
  1233. atomic_add(count, &bh->b_count);
  1234. buf += count;
  1235. if (atomic_read(&bh->b_count) == bh->b_size) {
  1236. bh = bh->b_reqnext;
  1237. if (bh)
  1238. atomic_set(&bh->b_count, 0);
  1239. }
  1240. }
  1241. tape->bh = bh;
  1242. return ret;
  1243. }
  1244. static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
  1245. int n)
  1246. {
  1247. struct idetape_bh *bh = tape->bh;
  1248. int count;
  1249. int ret = 0;
  1250. while (n) {
  1251. if (bh == NULL) {
  1252. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1253. __func__);
  1254. return 1;
  1255. }
  1256. count = min(tape->b_count, n);
  1257. if (copy_to_user(buf, tape->b_data, count))
  1258. ret = 1;
  1259. n -= count;
  1260. tape->b_data += count;
  1261. tape->b_count -= count;
  1262. buf += count;
  1263. if (!tape->b_count) {
  1264. bh = bh->b_reqnext;
  1265. tape->bh = bh;
  1266. if (bh) {
  1267. tape->b_data = bh->b_data;
  1268. tape->b_count = atomic_read(&bh->b_count);
  1269. }
  1270. }
  1271. }
  1272. return ret;
  1273. }
  1274. static void idetape_init_merge_buffer(idetape_tape_t *tape)
  1275. {
  1276. struct idetape_bh *bh = tape->merge_bh;
  1277. tape->bh = tape->merge_bh;
  1278. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1279. atomic_set(&bh->b_count, 0);
  1280. else {
  1281. tape->b_data = bh->b_data;
  1282. tape->b_count = atomic_read(&bh->b_count);
  1283. }
  1284. }
  1285. static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
  1286. {
  1287. idetape_tape_t *tape = drive->driver_data;
  1288. u8 *readpos = tape->pc->buf;
  1289. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1290. if (!tape->pc->error) {
  1291. debug_log(DBG_SENSE, "BOP - %s\n",
  1292. (readpos[0] & 0x80) ? "Yes" : "No");
  1293. debug_log(DBG_SENSE, "EOP - %s\n",
  1294. (readpos[0] & 0x40) ? "Yes" : "No");
  1295. if (readpos[0] & 0x4) {
  1296. printk(KERN_INFO "ide-tape: Block location is unknown"
  1297. "to the tape\n");
  1298. clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
  1299. idetape_end_request(drive, 0, 0);
  1300. } else {
  1301. debug_log(DBG_SENSE, "Block Location - %u\n",
  1302. be32_to_cpu(*(u32 *)&readpos[4]));
  1303. tape->partition = readpos[1];
  1304. tape->first_frame =
  1305. be32_to_cpu(*(u32 *)&readpos[4]);
  1306. set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
  1307. idetape_end_request(drive, 1, 0);
  1308. }
  1309. } else {
  1310. idetape_end_request(drive, 0, 0);
  1311. }
  1312. return ide_stopped;
  1313. }
  1314. /*
  1315. * Write a filemark if write_filemark=1. Flush the device buffers without
  1316. * writing a filemark otherwise.
  1317. */
  1318. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  1319. struct ide_atapi_pc *pc, int write_filemark)
  1320. {
  1321. idetape_init_pc(pc);
  1322. pc->c[0] = WRITE_FILEMARKS;
  1323. pc->c[4] = write_filemark;
  1324. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1325. pc->idetape_callback = &idetape_pc_callback;
  1326. }
  1327. static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
  1328. {
  1329. idetape_init_pc(pc);
  1330. pc->c[0] = TEST_UNIT_READY;
  1331. pc->idetape_callback = &idetape_pc_callback;
  1332. }
  1333. /*
  1334. * We add a special packet command request to the tail of the request queue, and
  1335. * wait for it to be serviced. This is not to be called from within the request
  1336. * handling part of the driver! We allocate here data on the stack and it is
  1337. * valid until the request is finished. This is not the case for the bottom part
  1338. * of the driver, where we are always leaving the functions to wait for an
  1339. * interrupt or a timer event.
  1340. *
  1341. * From the bottom part of the driver, we should allocate safe memory using
  1342. * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
  1343. * to the request list without waiting for it to be serviced! In that case, we
  1344. * usually use idetape_queue_pc_head().
  1345. */
  1346. static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  1347. {
  1348. struct ide_tape_obj *tape = drive->driver_data;
  1349. struct request rq;
  1350. idetape_init_rq(&rq, REQ_IDETAPE_PC1);
  1351. rq.buffer = (char *) pc;
  1352. rq.rq_disk = tape->disk;
  1353. return ide_do_drive_cmd(drive, &rq, ide_wait);
  1354. }
  1355. static void idetape_create_load_unload_cmd(ide_drive_t *drive,
  1356. struct ide_atapi_pc *pc, int cmd)
  1357. {
  1358. idetape_init_pc(pc);
  1359. pc->c[0] = START_STOP;
  1360. pc->c[4] = cmd;
  1361. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1362. pc->idetape_callback = &idetape_pc_callback;
  1363. }
  1364. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  1365. {
  1366. idetape_tape_t *tape = drive->driver_data;
  1367. struct ide_atapi_pc pc;
  1368. int load_attempted = 0;
  1369. /* Wait for the tape to become ready */
  1370. set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  1371. timeout += jiffies;
  1372. while (time_before(jiffies, timeout)) {
  1373. idetape_create_test_unit_ready_cmd(&pc);
  1374. if (!idetape_queue_pc_tail(drive, &pc))
  1375. return 0;
  1376. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  1377. || (tape->asc == 0x3A)) {
  1378. /* no media */
  1379. if (load_attempted)
  1380. return -ENOMEDIUM;
  1381. idetape_create_load_unload_cmd(drive, &pc,
  1382. IDETAPE_LU_LOAD_MASK);
  1383. idetape_queue_pc_tail(drive, &pc);
  1384. load_attempted = 1;
  1385. /* not about to be ready */
  1386. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  1387. (tape->ascq == 1 || tape->ascq == 8)))
  1388. return -EIO;
  1389. msleep(100);
  1390. }
  1391. return -EIO;
  1392. }
  1393. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  1394. {
  1395. struct ide_atapi_pc pc;
  1396. int rc;
  1397. idetape_create_write_filemark_cmd(drive, &pc, 0);
  1398. rc = idetape_queue_pc_tail(drive, &pc);
  1399. if (rc)
  1400. return rc;
  1401. idetape_wait_ready(drive, 60 * 5 * HZ);
  1402. return 0;
  1403. }
  1404. static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
  1405. {
  1406. idetape_init_pc(pc);
  1407. pc->c[0] = READ_POSITION;
  1408. pc->req_xfer = 20;
  1409. pc->idetape_callback = &idetape_read_position_callback;
  1410. }
  1411. static int idetape_read_position(ide_drive_t *drive)
  1412. {
  1413. idetape_tape_t *tape = drive->driver_data;
  1414. struct ide_atapi_pc pc;
  1415. int position;
  1416. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1417. idetape_create_read_position_cmd(&pc);
  1418. if (idetape_queue_pc_tail(drive, &pc))
  1419. return -1;
  1420. position = tape->first_frame;
  1421. return position;
  1422. }
  1423. static void idetape_create_locate_cmd(ide_drive_t *drive,
  1424. struct ide_atapi_pc *pc,
  1425. unsigned int block, u8 partition, int skip)
  1426. {
  1427. idetape_init_pc(pc);
  1428. pc->c[0] = POSITION_TO_ELEMENT;
  1429. pc->c[1] = 2;
  1430. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  1431. pc->c[8] = partition;
  1432. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1433. pc->idetape_callback = &idetape_pc_callback;
  1434. }
  1435. static int idetape_create_prevent_cmd(ide_drive_t *drive,
  1436. struct ide_atapi_pc *pc, int prevent)
  1437. {
  1438. idetape_tape_t *tape = drive->driver_data;
  1439. /* device supports locking according to capabilities page */
  1440. if (!(tape->caps[6] & 0x01))
  1441. return 0;
  1442. idetape_init_pc(pc);
  1443. pc->c[0] = ALLOW_MEDIUM_REMOVAL;
  1444. pc->c[4] = prevent;
  1445. pc->idetape_callback = &idetape_pc_callback;
  1446. return 1;
  1447. }
  1448. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  1449. {
  1450. idetape_tape_t *tape = drive->driver_data;
  1451. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  1452. return;
  1453. clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
  1454. tape->merge_bh_size = 0;
  1455. if (tape->merge_bh != NULL) {
  1456. ide_tape_kfree_buffer(tape);
  1457. tape->merge_bh = NULL;
  1458. }
  1459. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1460. }
  1461. /*
  1462. * Position the tape to the requested block using the LOCATE packet command.
  1463. * A READ POSITION command is then issued to check where we are positioned. Like
  1464. * all higher level operations, we queue the commands at the tail of the request
  1465. * queue and wait for their completion.
  1466. */
  1467. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  1468. u8 partition, int skip)
  1469. {
  1470. idetape_tape_t *tape = drive->driver_data;
  1471. int retval;
  1472. struct ide_atapi_pc pc;
  1473. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1474. __ide_tape_discard_merge_buffer(drive);
  1475. idetape_wait_ready(drive, 60 * 5 * HZ);
  1476. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  1477. retval = idetape_queue_pc_tail(drive, &pc);
  1478. if (retval)
  1479. return (retval);
  1480. idetape_create_read_position_cmd(&pc);
  1481. return (idetape_queue_pc_tail(drive, &pc));
  1482. }
  1483. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  1484. int restore_position)
  1485. {
  1486. idetape_tape_t *tape = drive->driver_data;
  1487. int seek, position;
  1488. __ide_tape_discard_merge_buffer(drive);
  1489. if (restore_position) {
  1490. position = idetape_read_position(drive);
  1491. seek = position > 0 ? position : 0;
  1492. if (idetape_position_tape(drive, seek, 0, 0)) {
  1493. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1494. " %s\n", tape->name, __func__);
  1495. return;
  1496. }
  1497. }
  1498. }
  1499. /*
  1500. * Generate a read/write request for the block device interface and wait for it
  1501. * to be serviced.
  1502. */
  1503. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1504. struct idetape_bh *bh)
  1505. {
  1506. idetape_tape_t *tape = drive->driver_data;
  1507. struct request rq;
  1508. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1509. idetape_init_rq(&rq, cmd);
  1510. rq.rq_disk = tape->disk;
  1511. rq.special = (void *)bh;
  1512. rq.sector = tape->first_frame;
  1513. rq.nr_sectors = blocks;
  1514. rq.current_nr_sectors = blocks;
  1515. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  1516. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1517. return 0;
  1518. if (tape->merge_bh)
  1519. idetape_init_merge_buffer(tape);
  1520. if (rq.errors == IDETAPE_ERROR_GENERAL)
  1521. return -EIO;
  1522. return (tape->blk_size * (blocks-rq.current_nr_sectors));
  1523. }
  1524. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1525. {
  1526. idetape_init_pc(pc);
  1527. pc->c[0] = INQUIRY;
  1528. pc->c[4] = 254;
  1529. pc->req_xfer = 254;
  1530. pc->idetape_callback = &idetape_pc_callback;
  1531. }
  1532. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1533. struct ide_atapi_pc *pc)
  1534. {
  1535. idetape_init_pc(pc);
  1536. pc->c[0] = REZERO_UNIT;
  1537. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1538. pc->idetape_callback = &idetape_pc_callback;
  1539. }
  1540. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1541. {
  1542. idetape_init_pc(pc);
  1543. pc->c[0] = ERASE;
  1544. pc->c[1] = 1;
  1545. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1546. pc->idetape_callback = &idetape_pc_callback;
  1547. }
  1548. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1549. {
  1550. idetape_init_pc(pc);
  1551. pc->c[0] = SPACE;
  1552. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1553. pc->c[1] = cmd;
  1554. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1555. pc->idetape_callback = &idetape_pc_callback;
  1556. }
  1557. /* Queue up a character device originated write request. */
  1558. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1559. {
  1560. idetape_tape_t *tape = drive->driver_data;
  1561. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1562. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1563. blocks, tape->merge_bh);
  1564. }
  1565. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  1566. {
  1567. idetape_tape_t *tape = drive->driver_data;
  1568. int blocks, min;
  1569. struct idetape_bh *bh;
  1570. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1571. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  1572. " but we are not writing.\n");
  1573. return;
  1574. }
  1575. if (tape->merge_bh_size > tape->buffer_size) {
  1576. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1577. tape->merge_bh_size = tape->buffer_size;
  1578. }
  1579. if (tape->merge_bh_size) {
  1580. blocks = tape->merge_bh_size / tape->blk_size;
  1581. if (tape->merge_bh_size % tape->blk_size) {
  1582. unsigned int i;
  1583. blocks++;
  1584. i = tape->blk_size - tape->merge_bh_size %
  1585. tape->blk_size;
  1586. bh = tape->bh->b_reqnext;
  1587. while (bh) {
  1588. atomic_set(&bh->b_count, 0);
  1589. bh = bh->b_reqnext;
  1590. }
  1591. bh = tape->bh;
  1592. while (i) {
  1593. if (bh == NULL) {
  1594. printk(KERN_INFO "ide-tape: bug,"
  1595. " bh NULL\n");
  1596. break;
  1597. }
  1598. min = min(i, (unsigned int)(bh->b_size -
  1599. atomic_read(&bh->b_count)));
  1600. memset(bh->b_data + atomic_read(&bh->b_count),
  1601. 0, min);
  1602. atomic_add(min, &bh->b_count);
  1603. i -= min;
  1604. bh = bh->b_reqnext;
  1605. }
  1606. }
  1607. (void) idetape_add_chrdev_write_request(drive, blocks);
  1608. tape->merge_bh_size = 0;
  1609. }
  1610. if (tape->merge_bh != NULL) {
  1611. ide_tape_kfree_buffer(tape);
  1612. tape->merge_bh = NULL;
  1613. }
  1614. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1615. }
  1616. static int idetape_init_read(ide_drive_t *drive)
  1617. {
  1618. idetape_tape_t *tape = drive->driver_data;
  1619. int bytes_read;
  1620. /* Initialize read operation */
  1621. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1622. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1623. ide_tape_flush_merge_buffer(drive);
  1624. idetape_flush_tape_buffers(drive);
  1625. }
  1626. if (tape->merge_bh || tape->merge_bh_size) {
  1627. printk(KERN_ERR "ide-tape: merge_bh_size should be"
  1628. " 0 now\n");
  1629. tape->merge_bh_size = 0;
  1630. }
  1631. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1632. if (!tape->merge_bh)
  1633. return -ENOMEM;
  1634. tape->chrdev_dir = IDETAPE_DIR_READ;
  1635. /*
  1636. * Issue a read 0 command to ensure that DSC handshake is
  1637. * switched from completion mode to buffer available mode.
  1638. * No point in issuing this if DSC overlap isn't supported, some
  1639. * drives (Seagate STT3401A) will return an error.
  1640. */
  1641. if (drive->dsc_overlap) {
  1642. bytes_read = idetape_queue_rw_tail(drive,
  1643. REQ_IDETAPE_READ, 0,
  1644. tape->merge_bh);
  1645. if (bytes_read < 0) {
  1646. ide_tape_kfree_buffer(tape);
  1647. tape->merge_bh = NULL;
  1648. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1649. return bytes_read;
  1650. }
  1651. }
  1652. }
  1653. return 0;
  1654. }
  1655. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1656. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1657. {
  1658. idetape_tape_t *tape = drive->driver_data;
  1659. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1660. /* If we are at a filemark, return a read length of 0 */
  1661. if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1662. return 0;
  1663. idetape_init_read(drive);
  1664. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1665. tape->merge_bh);
  1666. }
  1667. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1668. {
  1669. idetape_tape_t *tape = drive->driver_data;
  1670. struct idetape_bh *bh;
  1671. int blocks;
  1672. while (bcount) {
  1673. unsigned int count;
  1674. bh = tape->merge_bh;
  1675. count = min(tape->buffer_size, bcount);
  1676. bcount -= count;
  1677. blocks = count / tape->blk_size;
  1678. while (count) {
  1679. atomic_set(&bh->b_count,
  1680. min(count, (unsigned int)bh->b_size));
  1681. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1682. count -= atomic_read(&bh->b_count);
  1683. bh = bh->b_reqnext;
  1684. }
  1685. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1686. tape->merge_bh);
  1687. }
  1688. }
  1689. /*
  1690. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1691. * currently support only one partition.
  1692. */
  1693. static int idetape_rewind_tape(ide_drive_t *drive)
  1694. {
  1695. int retval;
  1696. struct ide_atapi_pc pc;
  1697. idetape_tape_t *tape;
  1698. tape = drive->driver_data;
  1699. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1700. idetape_create_rewind_cmd(drive, &pc);
  1701. retval = idetape_queue_pc_tail(drive, &pc);
  1702. if (retval)
  1703. return retval;
  1704. idetape_create_read_position_cmd(&pc);
  1705. retval = idetape_queue_pc_tail(drive, &pc);
  1706. if (retval)
  1707. return retval;
  1708. return 0;
  1709. }
  1710. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1711. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1712. unsigned long arg)
  1713. {
  1714. idetape_tape_t *tape = drive->driver_data;
  1715. void __user *argp = (void __user *)arg;
  1716. struct idetape_config {
  1717. int dsc_rw_frequency;
  1718. int dsc_media_access_frequency;
  1719. int nr_stages;
  1720. } config;
  1721. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1722. switch (cmd) {
  1723. case 0x0340:
  1724. if (copy_from_user(&config, argp, sizeof(config)))
  1725. return -EFAULT;
  1726. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1727. break;
  1728. case 0x0350:
  1729. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1730. config.nr_stages = 1;
  1731. if (copy_to_user(argp, &config, sizeof(config)))
  1732. return -EFAULT;
  1733. break;
  1734. default:
  1735. return -EIO;
  1736. }
  1737. return 0;
  1738. }
  1739. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1740. int mt_count)
  1741. {
  1742. idetape_tape_t *tape = drive->driver_data;
  1743. struct ide_atapi_pc pc;
  1744. int retval, count = 0;
  1745. int sprev = !!(tape->caps[4] & 0x20);
  1746. if (mt_count == 0)
  1747. return 0;
  1748. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1749. if (!sprev)
  1750. return -EIO;
  1751. mt_count = -mt_count;
  1752. }
  1753. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1754. tape->merge_bh_size = 0;
  1755. if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1756. ++count;
  1757. ide_tape_discard_merge_buffer(drive, 0);
  1758. }
  1759. switch (mt_op) {
  1760. case MTFSF:
  1761. case MTBSF:
  1762. idetape_create_space_cmd(&pc, mt_count - count,
  1763. IDETAPE_SPACE_OVER_FILEMARK);
  1764. return idetape_queue_pc_tail(drive, &pc);
  1765. case MTFSFM:
  1766. case MTBSFM:
  1767. if (!sprev)
  1768. return -EIO;
  1769. retval = idetape_space_over_filemarks(drive, MTFSF,
  1770. mt_count - count);
  1771. if (retval)
  1772. return retval;
  1773. count = (MTBSFM == mt_op ? 1 : -1);
  1774. return idetape_space_over_filemarks(drive, MTFSF, count);
  1775. default:
  1776. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1777. mt_op);
  1778. return -EIO;
  1779. }
  1780. }
  1781. /*
  1782. * Our character device read / write functions.
  1783. *
  1784. * The tape is optimized to maximize throughput when it is transferring an
  1785. * integral number of the "continuous transfer limit", which is a parameter of
  1786. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1787. *
  1788. * As of version 1.3 of the driver, the character device provides an abstract
  1789. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1790. * same backup/restore procedure is supported. The driver will internally
  1791. * convert the requests to the recommended transfer unit, so that an unmatch
  1792. * between the user's block size to the recommended size will only result in a
  1793. * (slightly) increased driver overhead, but will no longer hit performance.
  1794. * This is not applicable to Onstream.
  1795. */
  1796. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1797. size_t count, loff_t *ppos)
  1798. {
  1799. struct ide_tape_obj *tape = ide_tape_f(file);
  1800. ide_drive_t *drive = tape->drive;
  1801. ssize_t bytes_read, temp, actually_read = 0, rc;
  1802. ssize_t ret = 0;
  1803. u16 ctl = *(u16 *)&tape->caps[12];
  1804. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1805. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1806. if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
  1807. if (count > tape->blk_size &&
  1808. (count % tape->blk_size) == 0)
  1809. tape->user_bs_factor = count / tape->blk_size;
  1810. }
  1811. rc = idetape_init_read(drive);
  1812. if (rc < 0)
  1813. return rc;
  1814. if (count == 0)
  1815. return (0);
  1816. if (tape->merge_bh_size) {
  1817. actually_read = min((unsigned int)(tape->merge_bh_size),
  1818. (unsigned int)count);
  1819. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1820. ret = -EFAULT;
  1821. buf += actually_read;
  1822. tape->merge_bh_size -= actually_read;
  1823. count -= actually_read;
  1824. }
  1825. while (count >= tape->buffer_size) {
  1826. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1827. if (bytes_read <= 0)
  1828. goto finish;
  1829. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1830. ret = -EFAULT;
  1831. buf += bytes_read;
  1832. count -= bytes_read;
  1833. actually_read += bytes_read;
  1834. }
  1835. if (count) {
  1836. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1837. if (bytes_read <= 0)
  1838. goto finish;
  1839. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1840. if (idetape_copy_stage_to_user(tape, buf, temp))
  1841. ret = -EFAULT;
  1842. actually_read += temp;
  1843. tape->merge_bh_size = bytes_read-temp;
  1844. }
  1845. finish:
  1846. if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
  1847. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1848. idetape_space_over_filemarks(drive, MTFSF, 1);
  1849. return 0;
  1850. }
  1851. return ret ? ret : actually_read;
  1852. }
  1853. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1854. size_t count, loff_t *ppos)
  1855. {
  1856. struct ide_tape_obj *tape = ide_tape_f(file);
  1857. ide_drive_t *drive = tape->drive;
  1858. ssize_t actually_written = 0;
  1859. ssize_t ret = 0;
  1860. u16 ctl = *(u16 *)&tape->caps[12];
  1861. /* The drive is write protected. */
  1862. if (tape->write_prot)
  1863. return -EACCES;
  1864. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1865. /* Initialize write operation */
  1866. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1867. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1868. ide_tape_discard_merge_buffer(drive, 1);
  1869. if (tape->merge_bh || tape->merge_bh_size) {
  1870. printk(KERN_ERR "ide-tape: merge_bh_size "
  1871. "should be 0 now\n");
  1872. tape->merge_bh_size = 0;
  1873. }
  1874. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1875. if (!tape->merge_bh)
  1876. return -ENOMEM;
  1877. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1878. idetape_init_merge_buffer(tape);
  1879. /*
  1880. * Issue a write 0 command to ensure that DSC handshake is
  1881. * switched from completion mode to buffer available mode. No
  1882. * point in issuing this if DSC overlap isn't supported, some
  1883. * drives (Seagate STT3401A) will return an error.
  1884. */
  1885. if (drive->dsc_overlap) {
  1886. ssize_t retval = idetape_queue_rw_tail(drive,
  1887. REQ_IDETAPE_WRITE, 0,
  1888. tape->merge_bh);
  1889. if (retval < 0) {
  1890. ide_tape_kfree_buffer(tape);
  1891. tape->merge_bh = NULL;
  1892. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1893. return retval;
  1894. }
  1895. }
  1896. }
  1897. if (count == 0)
  1898. return (0);
  1899. if (tape->merge_bh_size) {
  1900. if (tape->merge_bh_size >= tape->buffer_size) {
  1901. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1902. tape->merge_bh_size = 0;
  1903. }
  1904. actually_written = min((unsigned int)
  1905. (tape->buffer_size - tape->merge_bh_size),
  1906. (unsigned int)count);
  1907. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1908. ret = -EFAULT;
  1909. buf += actually_written;
  1910. tape->merge_bh_size += actually_written;
  1911. count -= actually_written;
  1912. if (tape->merge_bh_size == tape->buffer_size) {
  1913. ssize_t retval;
  1914. tape->merge_bh_size = 0;
  1915. retval = idetape_add_chrdev_write_request(drive, ctl);
  1916. if (retval <= 0)
  1917. return (retval);
  1918. }
  1919. }
  1920. while (count >= tape->buffer_size) {
  1921. ssize_t retval;
  1922. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1923. ret = -EFAULT;
  1924. buf += tape->buffer_size;
  1925. count -= tape->buffer_size;
  1926. retval = idetape_add_chrdev_write_request(drive, ctl);
  1927. actually_written += tape->buffer_size;
  1928. if (retval <= 0)
  1929. return (retval);
  1930. }
  1931. if (count) {
  1932. actually_written += count;
  1933. if (idetape_copy_stage_from_user(tape, buf, count))
  1934. ret = -EFAULT;
  1935. tape->merge_bh_size += count;
  1936. }
  1937. return ret ? ret : actually_written;
  1938. }
  1939. static int idetape_write_filemark(ide_drive_t *drive)
  1940. {
  1941. struct ide_atapi_pc pc;
  1942. /* Write a filemark */
  1943. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1944. if (idetape_queue_pc_tail(drive, &pc)) {
  1945. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1946. return -EIO;
  1947. }
  1948. return 0;
  1949. }
  1950. /*
  1951. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1952. * requested.
  1953. *
  1954. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1955. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1956. * usually not supported.
  1957. *
  1958. * The following commands are currently not supported:
  1959. *
  1960. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1961. * MT_ST_WRITE_THRESHOLD.
  1962. */
  1963. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1964. {
  1965. idetape_tape_t *tape = drive->driver_data;
  1966. struct ide_atapi_pc pc;
  1967. int i, retval;
  1968. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1969. mt_op, mt_count);
  1970. switch (mt_op) {
  1971. case MTFSF:
  1972. case MTFSFM:
  1973. case MTBSF:
  1974. case MTBSFM:
  1975. if (!mt_count)
  1976. return 0;
  1977. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1978. default:
  1979. break;
  1980. }
  1981. switch (mt_op) {
  1982. case MTWEOF:
  1983. if (tape->write_prot)
  1984. return -EACCES;
  1985. ide_tape_discard_merge_buffer(drive, 1);
  1986. for (i = 0; i < mt_count; i++) {
  1987. retval = idetape_write_filemark(drive);
  1988. if (retval)
  1989. return retval;
  1990. }
  1991. return 0;
  1992. case MTREW:
  1993. ide_tape_discard_merge_buffer(drive, 0);
  1994. if (idetape_rewind_tape(drive))
  1995. return -EIO;
  1996. return 0;
  1997. case MTLOAD:
  1998. ide_tape_discard_merge_buffer(drive, 0);
  1999. idetape_create_load_unload_cmd(drive, &pc,
  2000. IDETAPE_LU_LOAD_MASK);
  2001. return idetape_queue_pc_tail(drive, &pc);
  2002. case MTUNLOAD:
  2003. case MTOFFL:
  2004. /*
  2005. * If door is locked, attempt to unlock before
  2006. * attempting to eject.
  2007. */
  2008. if (tape->door_locked) {
  2009. if (idetape_create_prevent_cmd(drive, &pc, 0))
  2010. if (!idetape_queue_pc_tail(drive, &pc))
  2011. tape->door_locked = DOOR_UNLOCKED;
  2012. }
  2013. ide_tape_discard_merge_buffer(drive, 0);
  2014. idetape_create_load_unload_cmd(drive, &pc,
  2015. !IDETAPE_LU_LOAD_MASK);
  2016. retval = idetape_queue_pc_tail(drive, &pc);
  2017. if (!retval)
  2018. clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  2019. return retval;
  2020. case MTNOP:
  2021. ide_tape_discard_merge_buffer(drive, 0);
  2022. return idetape_flush_tape_buffers(drive);
  2023. case MTRETEN:
  2024. ide_tape_discard_merge_buffer(drive, 0);
  2025. idetape_create_load_unload_cmd(drive, &pc,
  2026. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  2027. return idetape_queue_pc_tail(drive, &pc);
  2028. case MTEOM:
  2029. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  2030. return idetape_queue_pc_tail(drive, &pc);
  2031. case MTERASE:
  2032. (void)idetape_rewind_tape(drive);
  2033. idetape_create_erase_cmd(&pc);
  2034. return idetape_queue_pc_tail(drive, &pc);
  2035. case MTSETBLK:
  2036. if (mt_count) {
  2037. if (mt_count < tape->blk_size ||
  2038. mt_count % tape->blk_size)
  2039. return -EIO;
  2040. tape->user_bs_factor = mt_count / tape->blk_size;
  2041. clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  2042. } else
  2043. set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  2044. return 0;
  2045. case MTSEEK:
  2046. ide_tape_discard_merge_buffer(drive, 0);
  2047. return idetape_position_tape(drive,
  2048. mt_count * tape->user_bs_factor, tape->partition, 0);
  2049. case MTSETPART:
  2050. ide_tape_discard_merge_buffer(drive, 0);
  2051. return idetape_position_tape(drive, 0, mt_count, 0);
  2052. case MTFSR:
  2053. case MTBSR:
  2054. case MTLOCK:
  2055. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  2056. return 0;
  2057. retval = idetape_queue_pc_tail(drive, &pc);
  2058. if (retval)
  2059. return retval;
  2060. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  2061. return 0;
  2062. case MTUNLOCK:
  2063. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  2064. return 0;
  2065. retval = idetape_queue_pc_tail(drive, &pc);
  2066. if (retval)
  2067. return retval;
  2068. tape->door_locked = DOOR_UNLOCKED;
  2069. return 0;
  2070. default:
  2071. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  2072. mt_op);
  2073. return -EIO;
  2074. }
  2075. }
  2076. /*
  2077. * Our character device ioctls. General mtio.h magnetic io commands are
  2078. * supported here, and not in the corresponding block interface. Our own
  2079. * ide-tape ioctls are supported on both interfaces.
  2080. */
  2081. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  2082. unsigned int cmd, unsigned long arg)
  2083. {
  2084. struct ide_tape_obj *tape = ide_tape_f(file);
  2085. ide_drive_t *drive = tape->drive;
  2086. struct mtop mtop;
  2087. struct mtget mtget;
  2088. struct mtpos mtpos;
  2089. int block_offset = 0, position = tape->first_frame;
  2090. void __user *argp = (void __user *)arg;
  2091. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  2092. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  2093. ide_tape_flush_merge_buffer(drive);
  2094. idetape_flush_tape_buffers(drive);
  2095. }
  2096. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  2097. block_offset = tape->merge_bh_size /
  2098. (tape->blk_size * tape->user_bs_factor);
  2099. position = idetape_read_position(drive);
  2100. if (position < 0)
  2101. return -EIO;
  2102. }
  2103. switch (cmd) {
  2104. case MTIOCTOP:
  2105. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  2106. return -EFAULT;
  2107. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  2108. case MTIOCGET:
  2109. memset(&mtget, 0, sizeof(struct mtget));
  2110. mtget.mt_type = MT_ISSCSI2;
  2111. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  2112. mtget.mt_dsreg =
  2113. ((tape->blk_size * tape->user_bs_factor)
  2114. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  2115. if (tape->drv_write_prot)
  2116. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  2117. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  2118. return -EFAULT;
  2119. return 0;
  2120. case MTIOCPOS:
  2121. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  2122. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  2123. return -EFAULT;
  2124. return 0;
  2125. default:
  2126. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  2127. ide_tape_discard_merge_buffer(drive, 1);
  2128. return idetape_blkdev_ioctl(drive, cmd, arg);
  2129. }
  2130. }
  2131. /*
  2132. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  2133. * block size with the reported value.
  2134. */
  2135. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  2136. {
  2137. idetape_tape_t *tape = drive->driver_data;
  2138. struct ide_atapi_pc pc;
  2139. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  2140. if (idetape_queue_pc_tail(drive, &pc)) {
  2141. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  2142. if (tape->blk_size == 0) {
  2143. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  2144. "block size, assuming 32k\n");
  2145. tape->blk_size = 32768;
  2146. }
  2147. return;
  2148. }
  2149. tape->blk_size = (pc.buf[4 + 5] << 16) +
  2150. (pc.buf[4 + 6] << 8) +
  2151. pc.buf[4 + 7];
  2152. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  2153. }
  2154. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  2155. {
  2156. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  2157. ide_drive_t *drive;
  2158. idetape_tape_t *tape;
  2159. struct ide_atapi_pc pc;
  2160. int retval;
  2161. if (i >= MAX_HWIFS * MAX_DRIVES)
  2162. return -ENXIO;
  2163. tape = ide_tape_chrdev_get(i);
  2164. if (!tape)
  2165. return -ENXIO;
  2166. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2167. /*
  2168. * We really want to do nonseekable_open(inode, filp); here, but some
  2169. * versions of tar incorrectly call lseek on tapes and bail out if that
  2170. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  2171. */
  2172. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  2173. drive = tape->drive;
  2174. filp->private_data = tape;
  2175. if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
  2176. retval = -EBUSY;
  2177. goto out_put_tape;
  2178. }
  2179. retval = idetape_wait_ready(drive, 60 * HZ);
  2180. if (retval) {
  2181. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2182. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  2183. goto out_put_tape;
  2184. }
  2185. idetape_read_position(drive);
  2186. if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
  2187. (void)idetape_rewind_tape(drive);
  2188. /* Read block size and write protect status from drive. */
  2189. ide_tape_get_bsize_from_bdesc(drive);
  2190. /* Set write protect flag if device is opened as read-only. */
  2191. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  2192. tape->write_prot = 1;
  2193. else
  2194. tape->write_prot = tape->drv_write_prot;
  2195. /* Make sure drive isn't write protected if user wants to write. */
  2196. if (tape->write_prot) {
  2197. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  2198. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  2199. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2200. retval = -EROFS;
  2201. goto out_put_tape;
  2202. }
  2203. }
  2204. /* Lock the tape drive door so user can't eject. */
  2205. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2206. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  2207. if (!idetape_queue_pc_tail(drive, &pc)) {
  2208. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  2209. tape->door_locked = DOOR_LOCKED;
  2210. }
  2211. }
  2212. }
  2213. return 0;
  2214. out_put_tape:
  2215. ide_tape_put(tape);
  2216. return retval;
  2217. }
  2218. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  2219. {
  2220. idetape_tape_t *tape = drive->driver_data;
  2221. ide_tape_flush_merge_buffer(drive);
  2222. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
  2223. if (tape->merge_bh != NULL) {
  2224. idetape_pad_zeros(drive, tape->blk_size *
  2225. (tape->user_bs_factor - 1));
  2226. ide_tape_kfree_buffer(tape);
  2227. tape->merge_bh = NULL;
  2228. }
  2229. idetape_write_filemark(drive);
  2230. idetape_flush_tape_buffers(drive);
  2231. idetape_flush_tape_buffers(drive);
  2232. }
  2233. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  2234. {
  2235. struct ide_tape_obj *tape = ide_tape_f(filp);
  2236. ide_drive_t *drive = tape->drive;
  2237. struct ide_atapi_pc pc;
  2238. unsigned int minor = iminor(inode);
  2239. lock_kernel();
  2240. tape = drive->driver_data;
  2241. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2242. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  2243. idetape_write_release(drive, minor);
  2244. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  2245. if (minor < 128)
  2246. ide_tape_discard_merge_buffer(drive, 1);
  2247. }
  2248. if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
  2249. (void) idetape_rewind_tape(drive);
  2250. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2251. if (tape->door_locked == DOOR_LOCKED) {
  2252. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  2253. if (!idetape_queue_pc_tail(drive, &pc))
  2254. tape->door_locked = DOOR_UNLOCKED;
  2255. }
  2256. }
  2257. }
  2258. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2259. ide_tape_put(tape);
  2260. unlock_kernel();
  2261. return 0;
  2262. }
  2263. /*
  2264. * check the contents of the ATAPI IDENTIFY command results. We return:
  2265. *
  2266. * 1 - If the tape can be supported by us, based on the information we have so
  2267. * far.
  2268. *
  2269. * 0 - If this tape driver is not currently supported by us.
  2270. */
  2271. static int idetape_identify_device(ide_drive_t *drive)
  2272. {
  2273. u8 gcw[2], protocol, device_type, removable, packet_size;
  2274. if (drive->id_read == 0)
  2275. return 1;
  2276. *((unsigned short *) &gcw) = drive->id->config;
  2277. protocol = (gcw[1] & 0xC0) >> 6;
  2278. device_type = gcw[1] & 0x1F;
  2279. removable = !!(gcw[0] & 0x80);
  2280. packet_size = gcw[0] & 0x3;
  2281. /* Check that we can support this device */
  2282. if (protocol != 2)
  2283. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  2284. protocol);
  2285. else if (device_type != 1)
  2286. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  2287. "to tape\n", device_type);
  2288. else if (!removable)
  2289. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  2290. else if (packet_size != 0) {
  2291. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
  2292. " bytes\n", packet_size);
  2293. } else
  2294. return 1;
  2295. return 0;
  2296. }
  2297. static void idetape_get_inquiry_results(ide_drive_t *drive)
  2298. {
  2299. idetape_tape_t *tape = drive->driver_data;
  2300. struct ide_atapi_pc pc;
  2301. char fw_rev[6], vendor_id[10], product_id[18];
  2302. idetape_create_inquiry_cmd(&pc);
  2303. if (idetape_queue_pc_tail(drive, &pc)) {
  2304. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  2305. tape->name);
  2306. return;
  2307. }
  2308. memcpy(vendor_id, &pc.buf[8], 8);
  2309. memcpy(product_id, &pc.buf[16], 16);
  2310. memcpy(fw_rev, &pc.buf[32], 4);
  2311. ide_fixstring(vendor_id, 10, 0);
  2312. ide_fixstring(product_id, 18, 0);
  2313. ide_fixstring(fw_rev, 6, 0);
  2314. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
  2315. drive->name, tape->name, vendor_id, product_id, fw_rev);
  2316. }
  2317. /*
  2318. * Ask the tape about its various parameters. In particular, we will adjust our
  2319. * data transfer buffer size to the recommended value as returned by the tape.
  2320. */
  2321. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  2322. {
  2323. idetape_tape_t *tape = drive->driver_data;
  2324. struct ide_atapi_pc pc;
  2325. u8 *caps;
  2326. u8 speed, max_speed;
  2327. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  2328. if (idetape_queue_pc_tail(drive, &pc)) {
  2329. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  2330. " some default values\n");
  2331. tape->blk_size = 512;
  2332. put_unaligned(52, (u16 *)&tape->caps[12]);
  2333. put_unaligned(540, (u16 *)&tape->caps[14]);
  2334. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  2335. return;
  2336. }
  2337. caps = pc.buf + 4 + pc.buf[3];
  2338. /* convert to host order and save for later use */
  2339. speed = be16_to_cpu(*(u16 *)&caps[14]);
  2340. max_speed = be16_to_cpu(*(u16 *)&caps[8]);
  2341. put_unaligned(max_speed, (u16 *)&caps[8]);
  2342. put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
  2343. put_unaligned(speed, (u16 *)&caps[14]);
  2344. put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
  2345. if (!speed) {
  2346. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  2347. "(assuming 650KB/sec)\n", drive->name);
  2348. put_unaligned(650, (u16 *)&caps[14]);
  2349. }
  2350. if (!max_speed) {
  2351. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  2352. "(assuming 650KB/sec)\n", drive->name);
  2353. put_unaligned(650, (u16 *)&caps[8]);
  2354. }
  2355. memcpy(&tape->caps, caps, 20);
  2356. if (caps[7] & 0x02)
  2357. tape->blk_size = 512;
  2358. else if (caps[7] & 0x04)
  2359. tape->blk_size = 1024;
  2360. }
  2361. #ifdef CONFIG_IDE_PROC_FS
  2362. static void idetape_add_settings(ide_drive_t *drive)
  2363. {
  2364. idetape_tape_t *tape = drive->driver_data;
  2365. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2366. 1, 2, (u16 *)&tape->caps[16], NULL);
  2367. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2368. 1, 1, (u16 *)&tape->caps[14], NULL);
  2369. ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
  2370. 1, 1024, &tape->buffer_size, NULL);
  2371. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
  2372. IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
  2373. NULL);
  2374. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
  2375. 1, &drive->dsc_overlap, NULL);
  2376. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
  2377. 1, 1, &tape->avg_speed, NULL);
  2378. ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
  2379. 1, &tape->debug_mask, NULL);
  2380. }
  2381. #else
  2382. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  2383. #endif
  2384. /*
  2385. * The function below is called to:
  2386. *
  2387. * 1. Initialize our various state variables.
  2388. * 2. Ask the tape for its capabilities.
  2389. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  2390. * is chosen based on the recommendation which we received in step 2.
  2391. *
  2392. * Note that at this point ide.c already assigned us an irq, so that we can
  2393. * queue requests here and wait for their completion.
  2394. */
  2395. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2396. {
  2397. unsigned long t;
  2398. int speed;
  2399. int buffer_size;
  2400. u8 gcw[2];
  2401. u16 *ctl = (u16 *)&tape->caps[12];
  2402. spin_lock_init(&tape->lock);
  2403. drive->dsc_overlap = 1;
  2404. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  2405. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  2406. tape->name);
  2407. drive->dsc_overlap = 0;
  2408. }
  2409. /* Seagate Travan drives do not support DSC overlap. */
  2410. if (strstr(drive->id->model, "Seagate STT3401"))
  2411. drive->dsc_overlap = 0;
  2412. tape->minor = minor;
  2413. tape->name[0] = 'h';
  2414. tape->name[1] = 't';
  2415. tape->name[2] = '0' + minor;
  2416. tape->chrdev_dir = IDETAPE_DIR_NONE;
  2417. tape->pc = tape->pc_stack;
  2418. *((unsigned short *) &gcw) = drive->id->config;
  2419. /* Command packet DRQ type */
  2420. if (((gcw[0] & 0x60) >> 5) == 1)
  2421. set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
  2422. idetape_get_inquiry_results(drive);
  2423. idetape_get_mode_sense_results(drive);
  2424. ide_tape_get_bsize_from_bdesc(drive);
  2425. tape->user_bs_factor = 1;
  2426. tape->buffer_size = *ctl * tape->blk_size;
  2427. while (tape->buffer_size > 0xffff) {
  2428. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  2429. *ctl /= 2;
  2430. tape->buffer_size = *ctl * tape->blk_size;
  2431. }
  2432. buffer_size = tape->buffer_size;
  2433. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  2434. if (buffer_size % PAGE_SIZE) {
  2435. tape->pages_per_buffer++;
  2436. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  2437. }
  2438. /* select the "best" DSC read/write polling freq */
  2439. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  2440. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  2441. /*
  2442. * Ensure that the number we got makes sense; limit it within
  2443. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2444. */
  2445. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  2446. IDETAPE_DSC_RW_MAX);
  2447. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  2448. "%lums tDSC%s\n",
  2449. drive->name, tape->name, *(u16 *)&tape->caps[14],
  2450. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  2451. tape->buffer_size / 1024,
  2452. tape->best_dsc_rw_freq * 1000 / HZ,
  2453. drive->using_dma ? ", DMA":"");
  2454. idetape_add_settings(drive);
  2455. }
  2456. static void ide_tape_remove(ide_drive_t *drive)
  2457. {
  2458. idetape_tape_t *tape = drive->driver_data;
  2459. ide_proc_unregister_driver(drive, tape->driver);
  2460. ide_unregister_region(tape->disk);
  2461. ide_tape_put(tape);
  2462. }
  2463. static void ide_tape_release(struct kref *kref)
  2464. {
  2465. struct ide_tape_obj *tape = to_ide_tape(kref);
  2466. ide_drive_t *drive = tape->drive;
  2467. struct gendisk *g = tape->disk;
  2468. BUG_ON(tape->merge_bh_size);
  2469. drive->dsc_overlap = 0;
  2470. drive->driver_data = NULL;
  2471. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  2472. device_destroy(idetape_sysfs_class,
  2473. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  2474. idetape_devs[tape->minor] = NULL;
  2475. g->private_data = NULL;
  2476. put_disk(g);
  2477. kfree(tape);
  2478. }
  2479. #ifdef CONFIG_IDE_PROC_FS
  2480. static int proc_idetape_read_name
  2481. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2482. {
  2483. ide_drive_t *drive = (ide_drive_t *) data;
  2484. idetape_tape_t *tape = drive->driver_data;
  2485. char *out = page;
  2486. int len;
  2487. len = sprintf(out, "%s\n", tape->name);
  2488. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2489. }
  2490. static ide_proc_entry_t idetape_proc[] = {
  2491. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  2492. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2493. { NULL, 0, NULL, NULL }
  2494. };
  2495. #endif
  2496. static int ide_tape_probe(ide_drive_t *);
  2497. static ide_driver_t idetape_driver = {
  2498. .gen_driver = {
  2499. .owner = THIS_MODULE,
  2500. .name = "ide-tape",
  2501. .bus = &ide_bus_type,
  2502. },
  2503. .probe = ide_tape_probe,
  2504. .remove = ide_tape_remove,
  2505. .version = IDETAPE_VERSION,
  2506. .media = ide_tape,
  2507. .supports_dsc_overlap = 1,
  2508. .do_request = idetape_do_request,
  2509. .end_request = idetape_end_request,
  2510. .error = __ide_error,
  2511. .abort = __ide_abort,
  2512. #ifdef CONFIG_IDE_PROC_FS
  2513. .proc = idetape_proc,
  2514. #endif
  2515. };
  2516. /* Our character device supporting functions, passed to register_chrdev. */
  2517. static const struct file_operations idetape_fops = {
  2518. .owner = THIS_MODULE,
  2519. .read = idetape_chrdev_read,
  2520. .write = idetape_chrdev_write,
  2521. .ioctl = idetape_chrdev_ioctl,
  2522. .open = idetape_chrdev_open,
  2523. .release = idetape_chrdev_release,
  2524. };
  2525. static int idetape_open(struct inode *inode, struct file *filp)
  2526. {
  2527. struct gendisk *disk = inode->i_bdev->bd_disk;
  2528. struct ide_tape_obj *tape;
  2529. tape = ide_tape_get(disk);
  2530. if (!tape)
  2531. return -ENXIO;
  2532. return 0;
  2533. }
  2534. static int idetape_release(struct inode *inode, struct file *filp)
  2535. {
  2536. struct gendisk *disk = inode->i_bdev->bd_disk;
  2537. struct ide_tape_obj *tape = ide_tape_g(disk);
  2538. ide_tape_put(tape);
  2539. return 0;
  2540. }
  2541. static int idetape_ioctl(struct inode *inode, struct file *file,
  2542. unsigned int cmd, unsigned long arg)
  2543. {
  2544. struct block_device *bdev = inode->i_bdev;
  2545. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  2546. ide_drive_t *drive = tape->drive;
  2547. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  2548. if (err == -EINVAL)
  2549. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2550. return err;
  2551. }
  2552. static struct block_device_operations idetape_block_ops = {
  2553. .owner = THIS_MODULE,
  2554. .open = idetape_open,
  2555. .release = idetape_release,
  2556. .ioctl = idetape_ioctl,
  2557. };
  2558. static int ide_tape_probe(ide_drive_t *drive)
  2559. {
  2560. idetape_tape_t *tape;
  2561. struct gendisk *g;
  2562. int minor;
  2563. if (!strstr("ide-tape", drive->driver_req))
  2564. goto failed;
  2565. if (!drive->present)
  2566. goto failed;
  2567. if (drive->media != ide_tape)
  2568. goto failed;
  2569. if (!idetape_identify_device(drive)) {
  2570. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2571. " the driver\n", drive->name);
  2572. goto failed;
  2573. }
  2574. if (drive->scsi) {
  2575. printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
  2576. " emulation.\n", drive->name);
  2577. goto failed;
  2578. }
  2579. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2580. if (tape == NULL) {
  2581. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2582. drive->name);
  2583. goto failed;
  2584. }
  2585. g = alloc_disk(1 << PARTN_BITS);
  2586. if (!g)
  2587. goto out_free_tape;
  2588. ide_init_disk(g, drive);
  2589. ide_proc_register_driver(drive, &idetape_driver);
  2590. kref_init(&tape->kref);
  2591. tape->drive = drive;
  2592. tape->driver = &idetape_driver;
  2593. tape->disk = g;
  2594. g->private_data = &tape->driver;
  2595. drive->driver_data = tape;
  2596. mutex_lock(&idetape_ref_mutex);
  2597. for (minor = 0; idetape_devs[minor]; minor++)
  2598. ;
  2599. idetape_devs[minor] = tape;
  2600. mutex_unlock(&idetape_ref_mutex);
  2601. idetape_setup(drive, tape, minor);
  2602. device_create(idetape_sysfs_class, &drive->gendev,
  2603. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  2604. device_create(idetape_sysfs_class, &drive->gendev,
  2605. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  2606. g->fops = &idetape_block_ops;
  2607. ide_register_region(g);
  2608. return 0;
  2609. out_free_tape:
  2610. kfree(tape);
  2611. failed:
  2612. return -ENODEV;
  2613. }
  2614. static void __exit idetape_exit(void)
  2615. {
  2616. driver_unregister(&idetape_driver.gen_driver);
  2617. class_destroy(idetape_sysfs_class);
  2618. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2619. }
  2620. static int __init idetape_init(void)
  2621. {
  2622. int error = 1;
  2623. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2624. if (IS_ERR(idetape_sysfs_class)) {
  2625. idetape_sysfs_class = NULL;
  2626. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2627. error = -EBUSY;
  2628. goto out;
  2629. }
  2630. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2631. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2632. " interface\n");
  2633. error = -EBUSY;
  2634. goto out_free_class;
  2635. }
  2636. error = driver_register(&idetape_driver.gen_driver);
  2637. if (error)
  2638. goto out_free_driver;
  2639. return 0;
  2640. out_free_driver:
  2641. driver_unregister(&idetape_driver.gen_driver);
  2642. out_free_class:
  2643. class_destroy(idetape_sysfs_class);
  2644. out:
  2645. return error;
  2646. }
  2647. MODULE_ALIAS("ide:*m-tape*");
  2648. module_init(idetape_init);
  2649. module_exit(idetape_exit);
  2650. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2651. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2652. MODULE_LICENSE("GPL");