ide-tape.c 75 KB

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