ide-tape.c 68 KB

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