ide-tape.c 68 KB

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