pktcdvd.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685
  1. /*
  2. * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
  3. * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
  4. *
  5. * May be copied or modified under the terms of the GNU General Public
  6. * License. See linux/COPYING for more information.
  7. *
  8. * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
  9. * DVD-RAM devices.
  10. *
  11. * Theory of operation:
  12. *
  13. * At the lowest level, there is the standard driver for the CD/DVD device,
  14. * typically ide-cd.c or sr.c. This driver can handle read and write requests,
  15. * but it doesn't know anything about the special restrictions that apply to
  16. * packet writing. One restriction is that write requests must be aligned to
  17. * packet boundaries on the physical media, and the size of a write request
  18. * must be equal to the packet size. Another restriction is that a
  19. * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
  20. * command, if the previous command was a write.
  21. *
  22. * The purpose of the packet writing driver is to hide these restrictions from
  23. * higher layers, such as file systems, and present a block device that can be
  24. * randomly read and written using 2kB-sized blocks.
  25. *
  26. * The lowest layer in the packet writing driver is the packet I/O scheduler.
  27. * Its data is defined by the struct packet_iosched and includes two bio
  28. * queues with pending read and write requests. These queues are processed
  29. * by the pkt_iosched_process_queue() function. The write requests in this
  30. * queue are already properly aligned and sized. This layer is responsible for
  31. * issuing the flush cache commands and scheduling the I/O in a good order.
  32. *
  33. * The next layer transforms unaligned write requests to aligned writes. This
  34. * transformation requires reading missing pieces of data from the underlying
  35. * block device, assembling the pieces to full packets and queuing them to the
  36. * packet I/O scheduler.
  37. *
  38. * At the top layer there is a custom make_request_fn function that forwards
  39. * read requests directly to the iosched queue and puts write requests in the
  40. * unaligned write queue. A kernel thread performs the necessary read
  41. * gathering to convert the unaligned writes to aligned writes and then feeds
  42. * them to the packet I/O scheduler.
  43. *
  44. *************************************************************************/
  45. #include <linux/pktcdvd.h>
  46. #include <linux/config.h>
  47. #include <linux/module.h>
  48. #include <linux/types.h>
  49. #include <linux/kernel.h>
  50. #include <linux/kthread.h>
  51. #include <linux/errno.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/file.h>
  54. #include <linux/proc_fs.h>
  55. #include <linux/seq_file.h>
  56. #include <linux/miscdevice.h>
  57. #include <linux/suspend.h>
  58. #include <scsi/scsi_cmnd.h>
  59. #include <scsi/scsi_ioctl.h>
  60. #include <scsi/scsi.h>
  61. #include <asm/uaccess.h>
  62. #if PACKET_DEBUG
  63. #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
  64. #else
  65. #define DPRINTK(fmt, args...)
  66. #endif
  67. #if PACKET_DEBUG > 1
  68. #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
  69. #else
  70. #define VPRINTK(fmt, args...)
  71. #endif
  72. #define MAX_SPEED 0xffff
  73. #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
  74. static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
  75. static struct proc_dir_entry *pkt_proc;
  76. static int pkt_major;
  77. static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
  78. static mempool_t *psd_pool;
  79. static void pkt_bio_finished(struct pktcdvd_device *pd)
  80. {
  81. BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
  82. if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
  83. VPRINTK("pktcdvd: queue empty\n");
  84. atomic_set(&pd->iosched.attention, 1);
  85. wake_up(&pd->wqueue);
  86. }
  87. }
  88. static void pkt_bio_destructor(struct bio *bio)
  89. {
  90. kfree(bio->bi_io_vec);
  91. kfree(bio);
  92. }
  93. static struct bio *pkt_bio_alloc(int nr_iovecs)
  94. {
  95. struct bio_vec *bvl = NULL;
  96. struct bio *bio;
  97. bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
  98. if (!bio)
  99. goto no_bio;
  100. bio_init(bio);
  101. bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
  102. if (!bvl)
  103. goto no_bvl;
  104. bio->bi_max_vecs = nr_iovecs;
  105. bio->bi_io_vec = bvl;
  106. bio->bi_destructor = pkt_bio_destructor;
  107. return bio;
  108. no_bvl:
  109. kfree(bio);
  110. no_bio:
  111. return NULL;
  112. }
  113. /*
  114. * Allocate a packet_data struct
  115. */
  116. static struct packet_data *pkt_alloc_packet_data(int frames)
  117. {
  118. int i;
  119. struct packet_data *pkt;
  120. pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
  121. if (!pkt)
  122. goto no_pkt;
  123. pkt->frames = frames;
  124. pkt->w_bio = pkt_bio_alloc(frames);
  125. if (!pkt->w_bio)
  126. goto no_bio;
  127. for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
  128. pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  129. if (!pkt->pages[i])
  130. goto no_page;
  131. }
  132. spin_lock_init(&pkt->lock);
  133. for (i = 0; i < frames; i++) {
  134. struct bio *bio = pkt_bio_alloc(1);
  135. if (!bio)
  136. goto no_rd_bio;
  137. pkt->r_bios[i] = bio;
  138. }
  139. return pkt;
  140. no_rd_bio:
  141. for (i = 0; i < frames; i++) {
  142. struct bio *bio = pkt->r_bios[i];
  143. if (bio)
  144. bio_put(bio);
  145. }
  146. no_page:
  147. for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
  148. if (pkt->pages[i])
  149. __free_page(pkt->pages[i]);
  150. bio_put(pkt->w_bio);
  151. no_bio:
  152. kfree(pkt);
  153. no_pkt:
  154. return NULL;
  155. }
  156. /*
  157. * Free a packet_data struct
  158. */
  159. static void pkt_free_packet_data(struct packet_data *pkt)
  160. {
  161. int i;
  162. for (i = 0; i < pkt->frames; i++) {
  163. struct bio *bio = pkt->r_bios[i];
  164. if (bio)
  165. bio_put(bio);
  166. }
  167. for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
  168. __free_page(pkt->pages[i]);
  169. bio_put(pkt->w_bio);
  170. kfree(pkt);
  171. }
  172. static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
  173. {
  174. struct packet_data *pkt, *next;
  175. BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
  176. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
  177. pkt_free_packet_data(pkt);
  178. }
  179. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  180. }
  181. static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
  182. {
  183. struct packet_data *pkt;
  184. BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
  185. while (nr_packets > 0) {
  186. pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
  187. if (!pkt) {
  188. pkt_shrink_pktlist(pd);
  189. return 0;
  190. }
  191. pkt->id = nr_packets;
  192. pkt->pd = pd;
  193. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  194. nr_packets--;
  195. }
  196. return 1;
  197. }
  198. static void *pkt_rb_alloc(gfp_t gfp_mask, void *data)
  199. {
  200. return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
  201. }
  202. static void pkt_rb_free(void *ptr, void *data)
  203. {
  204. kfree(ptr);
  205. }
  206. static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
  207. {
  208. struct rb_node *n = rb_next(&node->rb_node);
  209. if (!n)
  210. return NULL;
  211. return rb_entry(n, struct pkt_rb_node, rb_node);
  212. }
  213. static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  214. {
  215. rb_erase(&node->rb_node, &pd->bio_queue);
  216. mempool_free(node, pd->rb_pool);
  217. pd->bio_queue_size--;
  218. BUG_ON(pd->bio_queue_size < 0);
  219. }
  220. /*
  221. * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
  222. */
  223. static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
  224. {
  225. struct rb_node *n = pd->bio_queue.rb_node;
  226. struct rb_node *next;
  227. struct pkt_rb_node *tmp;
  228. if (!n) {
  229. BUG_ON(pd->bio_queue_size > 0);
  230. return NULL;
  231. }
  232. for (;;) {
  233. tmp = rb_entry(n, struct pkt_rb_node, rb_node);
  234. if (s <= tmp->bio->bi_sector)
  235. next = n->rb_left;
  236. else
  237. next = n->rb_right;
  238. if (!next)
  239. break;
  240. n = next;
  241. }
  242. if (s > tmp->bio->bi_sector) {
  243. tmp = pkt_rbtree_next(tmp);
  244. if (!tmp)
  245. return NULL;
  246. }
  247. BUG_ON(s > tmp->bio->bi_sector);
  248. return tmp;
  249. }
  250. /*
  251. * Insert a node into the pd->bio_queue rb tree.
  252. */
  253. static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  254. {
  255. struct rb_node **p = &pd->bio_queue.rb_node;
  256. struct rb_node *parent = NULL;
  257. sector_t s = node->bio->bi_sector;
  258. struct pkt_rb_node *tmp;
  259. while (*p) {
  260. parent = *p;
  261. tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
  262. if (s < tmp->bio->bi_sector)
  263. p = &(*p)->rb_left;
  264. else
  265. p = &(*p)->rb_right;
  266. }
  267. rb_link_node(&node->rb_node, parent, p);
  268. rb_insert_color(&node->rb_node, &pd->bio_queue);
  269. pd->bio_queue_size++;
  270. }
  271. /*
  272. * Add a bio to a single linked list defined by its head and tail pointers.
  273. */
  274. static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
  275. {
  276. bio->bi_next = NULL;
  277. if (*list_tail) {
  278. BUG_ON((*list_head) == NULL);
  279. (*list_tail)->bi_next = bio;
  280. (*list_tail) = bio;
  281. } else {
  282. BUG_ON((*list_head) != NULL);
  283. (*list_head) = bio;
  284. (*list_tail) = bio;
  285. }
  286. }
  287. /*
  288. * Remove and return the first bio from a single linked list defined by its
  289. * head and tail pointers.
  290. */
  291. static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
  292. {
  293. struct bio *bio;
  294. if (*list_head == NULL)
  295. return NULL;
  296. bio = *list_head;
  297. *list_head = bio->bi_next;
  298. if (*list_head == NULL)
  299. *list_tail = NULL;
  300. bio->bi_next = NULL;
  301. return bio;
  302. }
  303. /*
  304. * Send a packet_command to the underlying block device and
  305. * wait for completion.
  306. */
  307. static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
  308. {
  309. char sense[SCSI_SENSE_BUFFERSIZE];
  310. request_queue_t *q;
  311. struct request *rq;
  312. DECLARE_COMPLETION(wait);
  313. int err = 0;
  314. q = bdev_get_queue(pd->bdev);
  315. rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
  316. __GFP_WAIT);
  317. rq->errors = 0;
  318. rq->rq_disk = pd->bdev->bd_disk;
  319. rq->bio = NULL;
  320. rq->buffer = NULL;
  321. rq->timeout = 60*HZ;
  322. rq->data = cgc->buffer;
  323. rq->data_len = cgc->buflen;
  324. rq->sense = sense;
  325. memset(sense, 0, sizeof(sense));
  326. rq->sense_len = 0;
  327. rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
  328. if (cgc->quiet)
  329. rq->flags |= REQ_QUIET;
  330. memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
  331. if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
  332. memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
  333. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  334. rq->ref_count++;
  335. rq->flags |= REQ_NOMERGE;
  336. rq->waiting = &wait;
  337. rq->end_io = blk_end_sync_rq;
  338. elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
  339. generic_unplug_device(q);
  340. wait_for_completion(&wait);
  341. if (rq->errors)
  342. err = -EIO;
  343. blk_put_request(rq);
  344. return err;
  345. }
  346. /*
  347. * A generic sense dump / resolve mechanism should be implemented across
  348. * all ATAPI + SCSI devices.
  349. */
  350. static void pkt_dump_sense(struct packet_command *cgc)
  351. {
  352. static char *info[9] = { "No sense", "Recovered error", "Not ready",
  353. "Medium error", "Hardware error", "Illegal request",
  354. "Unit attention", "Data protect", "Blank check" };
  355. int i;
  356. struct request_sense *sense = cgc->sense;
  357. printk("pktcdvd:");
  358. for (i = 0; i < CDROM_PACKET_SIZE; i++)
  359. printk(" %02x", cgc->cmd[i]);
  360. printk(" - ");
  361. if (sense == NULL) {
  362. printk("no sense\n");
  363. return;
  364. }
  365. printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
  366. if (sense->sense_key > 8) {
  367. printk(" (INVALID)\n");
  368. return;
  369. }
  370. printk(" (%s)\n", info[sense->sense_key]);
  371. }
  372. /*
  373. * flush the drive cache to media
  374. */
  375. static int pkt_flush_cache(struct pktcdvd_device *pd)
  376. {
  377. struct packet_command cgc;
  378. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  379. cgc.cmd[0] = GPCMD_FLUSH_CACHE;
  380. cgc.quiet = 1;
  381. /*
  382. * the IMMED bit -- we default to not setting it, although that
  383. * would allow a much faster close, this is safer
  384. */
  385. #if 0
  386. cgc.cmd[1] = 1 << 1;
  387. #endif
  388. return pkt_generic_packet(pd, &cgc);
  389. }
  390. /*
  391. * speed is given as the normal factor, e.g. 4 for 4x
  392. */
  393. static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
  394. {
  395. struct packet_command cgc;
  396. struct request_sense sense;
  397. int ret;
  398. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  399. cgc.sense = &sense;
  400. cgc.cmd[0] = GPCMD_SET_SPEED;
  401. cgc.cmd[2] = (read_speed >> 8) & 0xff;
  402. cgc.cmd[3] = read_speed & 0xff;
  403. cgc.cmd[4] = (write_speed >> 8) & 0xff;
  404. cgc.cmd[5] = write_speed & 0xff;
  405. if ((ret = pkt_generic_packet(pd, &cgc)))
  406. pkt_dump_sense(&cgc);
  407. return ret;
  408. }
  409. /*
  410. * Queue a bio for processing by the low-level CD device. Must be called
  411. * from process context.
  412. */
  413. static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
  414. {
  415. spin_lock(&pd->iosched.lock);
  416. if (bio_data_dir(bio) == READ) {
  417. pkt_add_list_last(bio, &pd->iosched.read_queue,
  418. &pd->iosched.read_queue_tail);
  419. } else {
  420. pkt_add_list_last(bio, &pd->iosched.write_queue,
  421. &pd->iosched.write_queue_tail);
  422. }
  423. spin_unlock(&pd->iosched.lock);
  424. atomic_set(&pd->iosched.attention, 1);
  425. wake_up(&pd->wqueue);
  426. }
  427. /*
  428. * Process the queued read/write requests. This function handles special
  429. * requirements for CDRW drives:
  430. * - A cache flush command must be inserted before a read request if the
  431. * previous request was a write.
  432. * - Switching between reading and writing is slow, so don't do it more often
  433. * than necessary.
  434. * - Optimize for throughput at the expense of latency. This means that streaming
  435. * writes will never be interrupted by a read, but if the drive has to seek
  436. * before the next write, switch to reading instead if there are any pending
  437. * read requests.
  438. * - Set the read speed according to current usage pattern. When only reading
  439. * from the device, it's best to use the highest possible read speed, but
  440. * when switching often between reading and writing, it's better to have the
  441. * same read and write speeds.
  442. */
  443. static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
  444. {
  445. if (atomic_read(&pd->iosched.attention) == 0)
  446. return;
  447. atomic_set(&pd->iosched.attention, 0);
  448. for (;;) {
  449. struct bio *bio;
  450. int reads_queued, writes_queued;
  451. spin_lock(&pd->iosched.lock);
  452. reads_queued = (pd->iosched.read_queue != NULL);
  453. writes_queued = (pd->iosched.write_queue != NULL);
  454. spin_unlock(&pd->iosched.lock);
  455. if (!reads_queued && !writes_queued)
  456. break;
  457. if (pd->iosched.writing) {
  458. int need_write_seek = 1;
  459. spin_lock(&pd->iosched.lock);
  460. bio = pd->iosched.write_queue;
  461. spin_unlock(&pd->iosched.lock);
  462. if (bio && (bio->bi_sector == pd->iosched.last_write))
  463. need_write_seek = 0;
  464. if (need_write_seek && reads_queued) {
  465. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  466. VPRINTK("pktcdvd: write, waiting\n");
  467. break;
  468. }
  469. pkt_flush_cache(pd);
  470. pd->iosched.writing = 0;
  471. }
  472. } else {
  473. if (!reads_queued && writes_queued) {
  474. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  475. VPRINTK("pktcdvd: read, waiting\n");
  476. break;
  477. }
  478. pd->iosched.writing = 1;
  479. }
  480. }
  481. spin_lock(&pd->iosched.lock);
  482. if (pd->iosched.writing) {
  483. bio = pkt_get_list_first(&pd->iosched.write_queue,
  484. &pd->iosched.write_queue_tail);
  485. } else {
  486. bio = pkt_get_list_first(&pd->iosched.read_queue,
  487. &pd->iosched.read_queue_tail);
  488. }
  489. spin_unlock(&pd->iosched.lock);
  490. if (!bio)
  491. continue;
  492. if (bio_data_dir(bio) == READ)
  493. pd->iosched.successive_reads += bio->bi_size >> 10;
  494. else {
  495. pd->iosched.successive_reads = 0;
  496. pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
  497. }
  498. if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
  499. if (pd->read_speed == pd->write_speed) {
  500. pd->read_speed = MAX_SPEED;
  501. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  502. }
  503. } else {
  504. if (pd->read_speed != pd->write_speed) {
  505. pd->read_speed = pd->write_speed;
  506. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  507. }
  508. }
  509. atomic_inc(&pd->cdrw.pending_bios);
  510. generic_make_request(bio);
  511. }
  512. }
  513. /*
  514. * Special care is needed if the underlying block device has a small
  515. * max_phys_segments value.
  516. */
  517. static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
  518. {
  519. if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
  520. /*
  521. * The cdrom device can handle one segment/frame
  522. */
  523. clear_bit(PACKET_MERGE_SEGS, &pd->flags);
  524. return 0;
  525. } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
  526. /*
  527. * We can handle this case at the expense of some extra memory
  528. * copies during write operations
  529. */
  530. set_bit(PACKET_MERGE_SEGS, &pd->flags);
  531. return 0;
  532. } else {
  533. printk("pktcdvd: cdrom max_phys_segments too small\n");
  534. return -EIO;
  535. }
  536. }
  537. /*
  538. * Copy CD_FRAMESIZE bytes from src_bio into a destination page
  539. */
  540. static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
  541. {
  542. unsigned int copy_size = CD_FRAMESIZE;
  543. while (copy_size > 0) {
  544. struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
  545. void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
  546. src_bvl->bv_offset + offs;
  547. void *vto = page_address(dst_page) + dst_offs;
  548. int len = min_t(int, copy_size, src_bvl->bv_len - offs);
  549. BUG_ON(len < 0);
  550. memcpy(vto, vfrom, len);
  551. kunmap_atomic(vfrom, KM_USER0);
  552. seg++;
  553. offs = 0;
  554. dst_offs += len;
  555. copy_size -= len;
  556. }
  557. }
  558. /*
  559. * Copy all data for this packet to pkt->pages[], so that
  560. * a) The number of required segments for the write bio is minimized, which
  561. * is necessary for some scsi controllers.
  562. * b) The data can be used as cache to avoid read requests if we receive a
  563. * new write request for the same zone.
  564. */
  565. static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
  566. {
  567. int f, p, offs;
  568. /* Copy all data to pkt->pages[] */
  569. p = 0;
  570. offs = 0;
  571. for (f = 0; f < pkt->frames; f++) {
  572. if (bvec[f].bv_page != pkt->pages[p]) {
  573. void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
  574. void *vto = page_address(pkt->pages[p]) + offs;
  575. memcpy(vto, vfrom, CD_FRAMESIZE);
  576. kunmap_atomic(vfrom, KM_USER0);
  577. bvec[f].bv_page = pkt->pages[p];
  578. bvec[f].bv_offset = offs;
  579. } else {
  580. BUG_ON(bvec[f].bv_offset != offs);
  581. }
  582. offs += CD_FRAMESIZE;
  583. if (offs >= PAGE_SIZE) {
  584. offs = 0;
  585. p++;
  586. }
  587. }
  588. }
  589. static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
  590. {
  591. struct packet_data *pkt = bio->bi_private;
  592. struct pktcdvd_device *pd = pkt->pd;
  593. BUG_ON(!pd);
  594. if (bio->bi_size)
  595. return 1;
  596. VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
  597. (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
  598. if (err)
  599. atomic_inc(&pkt->io_errors);
  600. if (atomic_dec_and_test(&pkt->io_wait)) {
  601. atomic_inc(&pkt->run_sm);
  602. wake_up(&pd->wqueue);
  603. }
  604. pkt_bio_finished(pd);
  605. return 0;
  606. }
  607. static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
  608. {
  609. struct packet_data *pkt = bio->bi_private;
  610. struct pktcdvd_device *pd = pkt->pd;
  611. BUG_ON(!pd);
  612. if (bio->bi_size)
  613. return 1;
  614. VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
  615. pd->stats.pkt_ended++;
  616. pkt_bio_finished(pd);
  617. atomic_dec(&pkt->io_wait);
  618. atomic_inc(&pkt->run_sm);
  619. wake_up(&pd->wqueue);
  620. return 0;
  621. }
  622. /*
  623. * Schedule reads for the holes in a packet
  624. */
  625. static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  626. {
  627. int frames_read = 0;
  628. struct bio *bio;
  629. int f;
  630. char written[PACKET_MAX_SIZE];
  631. BUG_ON(!pkt->orig_bios);
  632. atomic_set(&pkt->io_wait, 0);
  633. atomic_set(&pkt->io_errors, 0);
  634. /*
  635. * Figure out which frames we need to read before we can write.
  636. */
  637. memset(written, 0, sizeof(written));
  638. spin_lock(&pkt->lock);
  639. for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
  640. int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
  641. int num_frames = bio->bi_size / CD_FRAMESIZE;
  642. pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
  643. BUG_ON(first_frame < 0);
  644. BUG_ON(first_frame + num_frames > pkt->frames);
  645. for (f = first_frame; f < first_frame + num_frames; f++)
  646. written[f] = 1;
  647. }
  648. spin_unlock(&pkt->lock);
  649. if (pkt->cache_valid) {
  650. VPRINTK("pkt_gather_data: zone %llx cached\n",
  651. (unsigned long long)pkt->sector);
  652. goto out_account;
  653. }
  654. /*
  655. * Schedule reads for missing parts of the packet.
  656. */
  657. for (f = 0; f < pkt->frames; f++) {
  658. int p, offset;
  659. if (written[f])
  660. continue;
  661. bio = pkt->r_bios[f];
  662. bio_init(bio);
  663. bio->bi_max_vecs = 1;
  664. bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
  665. bio->bi_bdev = pd->bdev;
  666. bio->bi_end_io = pkt_end_io_read;
  667. bio->bi_private = pkt;
  668. p = (f * CD_FRAMESIZE) / PAGE_SIZE;
  669. offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  670. VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
  671. f, pkt->pages[p], offset);
  672. if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
  673. BUG();
  674. atomic_inc(&pkt->io_wait);
  675. bio->bi_rw = READ;
  676. pkt_queue_bio(pd, bio);
  677. frames_read++;
  678. }
  679. out_account:
  680. VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
  681. frames_read, (unsigned long long)pkt->sector);
  682. pd->stats.pkt_started++;
  683. pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
  684. }
  685. /*
  686. * Find a packet matching zone, or the least recently used packet if
  687. * there is no match.
  688. */
  689. static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
  690. {
  691. struct packet_data *pkt;
  692. list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
  693. if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
  694. list_del_init(&pkt->list);
  695. if (pkt->sector != zone)
  696. pkt->cache_valid = 0;
  697. return pkt;
  698. }
  699. }
  700. BUG();
  701. return NULL;
  702. }
  703. static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  704. {
  705. if (pkt->cache_valid) {
  706. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  707. } else {
  708. list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
  709. }
  710. }
  711. /*
  712. * recover a failed write, query for relocation if possible
  713. *
  714. * returns 1 if recovery is possible, or 0 if not
  715. *
  716. */
  717. static int pkt_start_recovery(struct packet_data *pkt)
  718. {
  719. /*
  720. * FIXME. We need help from the file system to implement
  721. * recovery handling.
  722. */
  723. return 0;
  724. #if 0
  725. struct request *rq = pkt->rq;
  726. struct pktcdvd_device *pd = rq->rq_disk->private_data;
  727. struct block_device *pkt_bdev;
  728. struct super_block *sb = NULL;
  729. unsigned long old_block, new_block;
  730. sector_t new_sector;
  731. pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
  732. if (pkt_bdev) {
  733. sb = get_super(pkt_bdev);
  734. bdput(pkt_bdev);
  735. }
  736. if (!sb)
  737. return 0;
  738. if (!sb->s_op || !sb->s_op->relocate_blocks)
  739. goto out;
  740. old_block = pkt->sector / (CD_FRAMESIZE >> 9);
  741. if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
  742. goto out;
  743. new_sector = new_block * (CD_FRAMESIZE >> 9);
  744. pkt->sector = new_sector;
  745. pkt->bio->bi_sector = new_sector;
  746. pkt->bio->bi_next = NULL;
  747. pkt->bio->bi_flags = 1 << BIO_UPTODATE;
  748. pkt->bio->bi_idx = 0;
  749. BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
  750. BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
  751. BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
  752. BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
  753. BUG_ON(pkt->bio->bi_private != pkt);
  754. drop_super(sb);
  755. return 1;
  756. out:
  757. drop_super(sb);
  758. return 0;
  759. #endif
  760. }
  761. static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
  762. {
  763. #if PACKET_DEBUG > 1
  764. static const char *state_name[] = {
  765. "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
  766. };
  767. enum packet_data_state old_state = pkt->state;
  768. VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
  769. state_name[old_state], state_name[state]);
  770. #endif
  771. pkt->state = state;
  772. }
  773. /*
  774. * Scan the work queue to see if we can start a new packet.
  775. * returns non-zero if any work was done.
  776. */
  777. static int pkt_handle_queue(struct pktcdvd_device *pd)
  778. {
  779. struct packet_data *pkt, *p;
  780. struct bio *bio = NULL;
  781. sector_t zone = 0; /* Suppress gcc warning */
  782. struct pkt_rb_node *node, *first_node;
  783. struct rb_node *n;
  784. VPRINTK("handle_queue\n");
  785. atomic_set(&pd->scan_queue, 0);
  786. if (list_empty(&pd->cdrw.pkt_free_list)) {
  787. VPRINTK("handle_queue: no pkt\n");
  788. return 0;
  789. }
  790. /*
  791. * Try to find a zone we are not already working on.
  792. */
  793. spin_lock(&pd->lock);
  794. first_node = pkt_rbtree_find(pd, pd->current_sector);
  795. if (!first_node) {
  796. n = rb_first(&pd->bio_queue);
  797. if (n)
  798. first_node = rb_entry(n, struct pkt_rb_node, rb_node);
  799. }
  800. node = first_node;
  801. while (node) {
  802. bio = node->bio;
  803. zone = ZONE(bio->bi_sector, pd);
  804. list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
  805. if (p->sector == zone) {
  806. bio = NULL;
  807. goto try_next_bio;
  808. }
  809. }
  810. break;
  811. try_next_bio:
  812. node = pkt_rbtree_next(node);
  813. if (!node) {
  814. n = rb_first(&pd->bio_queue);
  815. if (n)
  816. node = rb_entry(n, struct pkt_rb_node, rb_node);
  817. }
  818. if (node == first_node)
  819. node = NULL;
  820. }
  821. spin_unlock(&pd->lock);
  822. if (!bio) {
  823. VPRINTK("handle_queue: no bio\n");
  824. return 0;
  825. }
  826. pkt = pkt_get_packet_data(pd, zone);
  827. pd->current_sector = zone + pd->settings.size;
  828. pkt->sector = zone;
  829. BUG_ON(pkt->frames != pd->settings.size >> 2);
  830. pkt->write_size = 0;
  831. /*
  832. * Scan work queue for bios in the same zone and link them
  833. * to this packet.
  834. */
  835. spin_lock(&pd->lock);
  836. VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
  837. while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
  838. bio = node->bio;
  839. VPRINTK("pkt_handle_queue: found zone=%llx\n",
  840. (unsigned long long)ZONE(bio->bi_sector, pd));
  841. if (ZONE(bio->bi_sector, pd) != zone)
  842. break;
  843. pkt_rbtree_erase(pd, node);
  844. spin_lock(&pkt->lock);
  845. pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
  846. pkt->write_size += bio->bi_size / CD_FRAMESIZE;
  847. spin_unlock(&pkt->lock);
  848. }
  849. spin_unlock(&pd->lock);
  850. pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
  851. pkt_set_state(pkt, PACKET_WAITING_STATE);
  852. atomic_set(&pkt->run_sm, 1);
  853. spin_lock(&pd->cdrw.active_list_lock);
  854. list_add(&pkt->list, &pd->cdrw.pkt_active_list);
  855. spin_unlock(&pd->cdrw.active_list_lock);
  856. return 1;
  857. }
  858. /*
  859. * Assemble a bio to write one packet and queue the bio for processing
  860. * by the underlying block device.
  861. */
  862. static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
  863. {
  864. struct bio *bio;
  865. int f;
  866. int frames_write;
  867. struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
  868. for (f = 0; f < pkt->frames; f++) {
  869. bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
  870. bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  871. }
  872. /*
  873. * Fill-in bvec with data from orig_bios.
  874. */
  875. frames_write = 0;
  876. spin_lock(&pkt->lock);
  877. for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
  878. int segment = bio->bi_idx;
  879. int src_offs = 0;
  880. int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
  881. int num_frames = bio->bi_size / CD_FRAMESIZE;
  882. BUG_ON(first_frame < 0);
  883. BUG_ON(first_frame + num_frames > pkt->frames);
  884. for (f = first_frame; f < first_frame + num_frames; f++) {
  885. struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
  886. while (src_offs >= src_bvl->bv_len) {
  887. src_offs -= src_bvl->bv_len;
  888. segment++;
  889. BUG_ON(segment >= bio->bi_vcnt);
  890. src_bvl = bio_iovec_idx(bio, segment);
  891. }
  892. if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
  893. bvec[f].bv_page = src_bvl->bv_page;
  894. bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
  895. } else {
  896. pkt_copy_bio_data(bio, segment, src_offs,
  897. bvec[f].bv_page, bvec[f].bv_offset);
  898. }
  899. src_offs += CD_FRAMESIZE;
  900. frames_write++;
  901. }
  902. }
  903. pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
  904. spin_unlock(&pkt->lock);
  905. VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
  906. frames_write, (unsigned long long)pkt->sector);
  907. BUG_ON(frames_write != pkt->write_size);
  908. if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
  909. pkt_make_local_copy(pkt, bvec);
  910. pkt->cache_valid = 1;
  911. } else {
  912. pkt->cache_valid = 0;
  913. }
  914. /* Start the write request */
  915. bio_init(pkt->w_bio);
  916. pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
  917. pkt->w_bio->bi_sector = pkt->sector;
  918. pkt->w_bio->bi_bdev = pd->bdev;
  919. pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
  920. pkt->w_bio->bi_private = pkt;
  921. for (f = 0; f < pkt->frames; f++)
  922. if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
  923. BUG();
  924. VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
  925. atomic_set(&pkt->io_wait, 1);
  926. pkt->w_bio->bi_rw = WRITE;
  927. pkt_queue_bio(pd, pkt->w_bio);
  928. }
  929. static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
  930. {
  931. struct bio *bio, *next;
  932. if (!uptodate)
  933. pkt->cache_valid = 0;
  934. /* Finish all bios corresponding to this packet */
  935. bio = pkt->orig_bios;
  936. while (bio) {
  937. next = bio->bi_next;
  938. bio->bi_next = NULL;
  939. bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
  940. bio = next;
  941. }
  942. pkt->orig_bios = pkt->orig_bios_tail = NULL;
  943. }
  944. static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
  945. {
  946. int uptodate;
  947. VPRINTK("run_state_machine: pkt %d\n", pkt->id);
  948. for (;;) {
  949. switch (pkt->state) {
  950. case PACKET_WAITING_STATE:
  951. if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
  952. return;
  953. pkt->sleep_time = 0;
  954. pkt_gather_data(pd, pkt);
  955. pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
  956. break;
  957. case PACKET_READ_WAIT_STATE:
  958. if (atomic_read(&pkt->io_wait) > 0)
  959. return;
  960. if (atomic_read(&pkt->io_errors) > 0) {
  961. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  962. } else {
  963. pkt_start_write(pd, pkt);
  964. }
  965. break;
  966. case PACKET_WRITE_WAIT_STATE:
  967. if (atomic_read(&pkt->io_wait) > 0)
  968. return;
  969. if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
  970. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  971. } else {
  972. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  973. }
  974. break;
  975. case PACKET_RECOVERY_STATE:
  976. if (pkt_start_recovery(pkt)) {
  977. pkt_start_write(pd, pkt);
  978. } else {
  979. VPRINTK("No recovery possible\n");
  980. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  981. }
  982. break;
  983. case PACKET_FINISHED_STATE:
  984. uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
  985. pkt_finish_packet(pkt, uptodate);
  986. return;
  987. default:
  988. BUG();
  989. break;
  990. }
  991. }
  992. }
  993. static void pkt_handle_packets(struct pktcdvd_device *pd)
  994. {
  995. struct packet_data *pkt, *next;
  996. VPRINTK("pkt_handle_packets\n");
  997. /*
  998. * Run state machine for active packets
  999. */
  1000. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1001. if (atomic_read(&pkt->run_sm) > 0) {
  1002. atomic_set(&pkt->run_sm, 0);
  1003. pkt_run_state_machine(pd, pkt);
  1004. }
  1005. }
  1006. /*
  1007. * Move no longer active packets to the free list
  1008. */
  1009. spin_lock(&pd->cdrw.active_list_lock);
  1010. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
  1011. if (pkt->state == PACKET_FINISHED_STATE) {
  1012. list_del(&pkt->list);
  1013. pkt_put_packet_data(pd, pkt);
  1014. pkt_set_state(pkt, PACKET_IDLE_STATE);
  1015. atomic_set(&pd->scan_queue, 1);
  1016. }
  1017. }
  1018. spin_unlock(&pd->cdrw.active_list_lock);
  1019. }
  1020. static void pkt_count_states(struct pktcdvd_device *pd, int *states)
  1021. {
  1022. struct packet_data *pkt;
  1023. int i;
  1024. for (i = 0; i < PACKET_NUM_STATES; i++)
  1025. states[i] = 0;
  1026. spin_lock(&pd->cdrw.active_list_lock);
  1027. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1028. states[pkt->state]++;
  1029. }
  1030. spin_unlock(&pd->cdrw.active_list_lock);
  1031. }
  1032. /*
  1033. * kcdrwd is woken up when writes have been queued for one of our
  1034. * registered devices
  1035. */
  1036. static int kcdrwd(void *foobar)
  1037. {
  1038. struct pktcdvd_device *pd = foobar;
  1039. struct packet_data *pkt;
  1040. long min_sleep_time, residue;
  1041. set_user_nice(current, -20);
  1042. for (;;) {
  1043. DECLARE_WAITQUEUE(wait, current);
  1044. /*
  1045. * Wait until there is something to do
  1046. */
  1047. add_wait_queue(&pd->wqueue, &wait);
  1048. for (;;) {
  1049. set_current_state(TASK_INTERRUPTIBLE);
  1050. /* Check if we need to run pkt_handle_queue */
  1051. if (atomic_read(&pd->scan_queue) > 0)
  1052. goto work_to_do;
  1053. /* Check if we need to run the state machine for some packet */
  1054. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1055. if (atomic_read(&pkt->run_sm) > 0)
  1056. goto work_to_do;
  1057. }
  1058. /* Check if we need to process the iosched queues */
  1059. if (atomic_read(&pd->iosched.attention) != 0)
  1060. goto work_to_do;
  1061. /* Otherwise, go to sleep */
  1062. if (PACKET_DEBUG > 1) {
  1063. int states[PACKET_NUM_STATES];
  1064. pkt_count_states(pd, states);
  1065. VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  1066. states[0], states[1], states[2], states[3],
  1067. states[4], states[5]);
  1068. }
  1069. min_sleep_time = MAX_SCHEDULE_TIMEOUT;
  1070. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1071. if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
  1072. min_sleep_time = pkt->sleep_time;
  1073. }
  1074. generic_unplug_device(bdev_get_queue(pd->bdev));
  1075. VPRINTK("kcdrwd: sleeping\n");
  1076. residue = schedule_timeout(min_sleep_time);
  1077. VPRINTK("kcdrwd: wake up\n");
  1078. /* make swsusp happy with our thread */
  1079. try_to_freeze();
  1080. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1081. if (!pkt->sleep_time)
  1082. continue;
  1083. pkt->sleep_time -= min_sleep_time - residue;
  1084. if (pkt->sleep_time <= 0) {
  1085. pkt->sleep_time = 0;
  1086. atomic_inc(&pkt->run_sm);
  1087. }
  1088. }
  1089. if (signal_pending(current)) {
  1090. flush_signals(current);
  1091. }
  1092. if (kthread_should_stop())
  1093. break;
  1094. }
  1095. work_to_do:
  1096. set_current_state(TASK_RUNNING);
  1097. remove_wait_queue(&pd->wqueue, &wait);
  1098. if (kthread_should_stop())
  1099. break;
  1100. /*
  1101. * if pkt_handle_queue returns true, we can queue
  1102. * another request.
  1103. */
  1104. while (pkt_handle_queue(pd))
  1105. ;
  1106. /*
  1107. * Handle packet state machine
  1108. */
  1109. pkt_handle_packets(pd);
  1110. /*
  1111. * Handle iosched queues
  1112. */
  1113. pkt_iosched_process_queue(pd);
  1114. }
  1115. return 0;
  1116. }
  1117. static void pkt_print_settings(struct pktcdvd_device *pd)
  1118. {
  1119. printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
  1120. printk("%u blocks, ", pd->settings.size >> 2);
  1121. printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
  1122. }
  1123. static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
  1124. {
  1125. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1126. cgc->cmd[0] = GPCMD_MODE_SENSE_10;
  1127. cgc->cmd[2] = page_code | (page_control << 6);
  1128. cgc->cmd[7] = cgc->buflen >> 8;
  1129. cgc->cmd[8] = cgc->buflen & 0xff;
  1130. cgc->data_direction = CGC_DATA_READ;
  1131. return pkt_generic_packet(pd, cgc);
  1132. }
  1133. static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
  1134. {
  1135. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1136. memset(cgc->buffer, 0, 2);
  1137. cgc->cmd[0] = GPCMD_MODE_SELECT_10;
  1138. cgc->cmd[1] = 0x10; /* PF */
  1139. cgc->cmd[7] = cgc->buflen >> 8;
  1140. cgc->cmd[8] = cgc->buflen & 0xff;
  1141. cgc->data_direction = CGC_DATA_WRITE;
  1142. return pkt_generic_packet(pd, cgc);
  1143. }
  1144. static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
  1145. {
  1146. struct packet_command cgc;
  1147. int ret;
  1148. /* set up command and get the disc info */
  1149. init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
  1150. cgc.cmd[0] = GPCMD_READ_DISC_INFO;
  1151. cgc.cmd[8] = cgc.buflen = 2;
  1152. cgc.quiet = 1;
  1153. if ((ret = pkt_generic_packet(pd, &cgc)))
  1154. return ret;
  1155. /* not all drives have the same disc_info length, so requeue
  1156. * packet with the length the drive tells us it can supply
  1157. */
  1158. cgc.buflen = be16_to_cpu(di->disc_information_length) +
  1159. sizeof(di->disc_information_length);
  1160. if (cgc.buflen > sizeof(disc_information))
  1161. cgc.buflen = sizeof(disc_information);
  1162. cgc.cmd[8] = cgc.buflen;
  1163. return pkt_generic_packet(pd, &cgc);
  1164. }
  1165. static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
  1166. {
  1167. struct packet_command cgc;
  1168. int ret;
  1169. init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
  1170. cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
  1171. cgc.cmd[1] = type & 3;
  1172. cgc.cmd[4] = (track & 0xff00) >> 8;
  1173. cgc.cmd[5] = track & 0xff;
  1174. cgc.cmd[8] = 8;
  1175. cgc.quiet = 1;
  1176. if ((ret = pkt_generic_packet(pd, &cgc)))
  1177. return ret;
  1178. cgc.buflen = be16_to_cpu(ti->track_information_length) +
  1179. sizeof(ti->track_information_length);
  1180. if (cgc.buflen > sizeof(track_information))
  1181. cgc.buflen = sizeof(track_information);
  1182. cgc.cmd[8] = cgc.buflen;
  1183. return pkt_generic_packet(pd, &cgc);
  1184. }
  1185. static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
  1186. {
  1187. disc_information di;
  1188. track_information ti;
  1189. __u32 last_track;
  1190. int ret = -1;
  1191. if ((ret = pkt_get_disc_info(pd, &di)))
  1192. return ret;
  1193. last_track = (di.last_track_msb << 8) | di.last_track_lsb;
  1194. if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
  1195. return ret;
  1196. /* if this track is blank, try the previous. */
  1197. if (ti.blank) {
  1198. last_track--;
  1199. if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
  1200. return ret;
  1201. }
  1202. /* if last recorded field is valid, return it. */
  1203. if (ti.lra_v) {
  1204. *last_written = be32_to_cpu(ti.last_rec_address);
  1205. } else {
  1206. /* make it up instead */
  1207. *last_written = be32_to_cpu(ti.track_start) +
  1208. be32_to_cpu(ti.track_size);
  1209. if (ti.free_blocks)
  1210. *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
  1211. }
  1212. return 0;
  1213. }
  1214. /*
  1215. * write mode select package based on pd->settings
  1216. */
  1217. static int pkt_set_write_settings(struct pktcdvd_device *pd)
  1218. {
  1219. struct packet_command cgc;
  1220. struct request_sense sense;
  1221. write_param_page *wp;
  1222. char buffer[128];
  1223. int ret, size;
  1224. /* doesn't apply to DVD+RW or DVD-RAM */
  1225. if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
  1226. return 0;
  1227. memset(buffer, 0, sizeof(buffer));
  1228. init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
  1229. cgc.sense = &sense;
  1230. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
  1231. pkt_dump_sense(&cgc);
  1232. return ret;
  1233. }
  1234. size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
  1235. pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
  1236. if (size > sizeof(buffer))
  1237. size = sizeof(buffer);
  1238. /*
  1239. * now get it all
  1240. */
  1241. init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
  1242. cgc.sense = &sense;
  1243. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
  1244. pkt_dump_sense(&cgc);
  1245. return ret;
  1246. }
  1247. /*
  1248. * write page is offset header + block descriptor length
  1249. */
  1250. wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
  1251. wp->fp = pd->settings.fp;
  1252. wp->track_mode = pd->settings.track_mode;
  1253. wp->write_type = pd->settings.write_type;
  1254. wp->data_block_type = pd->settings.block_mode;
  1255. wp->multi_session = 0;
  1256. #ifdef PACKET_USE_LS
  1257. wp->link_size = 7;
  1258. wp->ls_v = 1;
  1259. #endif
  1260. if (wp->data_block_type == PACKET_BLOCK_MODE1) {
  1261. wp->session_format = 0;
  1262. wp->subhdr2 = 0x20;
  1263. } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
  1264. wp->session_format = 0x20;
  1265. wp->subhdr2 = 8;
  1266. #if 0
  1267. wp->mcn[0] = 0x80;
  1268. memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
  1269. #endif
  1270. } else {
  1271. /*
  1272. * paranoia
  1273. */
  1274. printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
  1275. return 1;
  1276. }
  1277. wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
  1278. cgc.buflen = cgc.cmd[8] = size;
  1279. if ((ret = pkt_mode_select(pd, &cgc))) {
  1280. pkt_dump_sense(&cgc);
  1281. return ret;
  1282. }
  1283. pkt_print_settings(pd);
  1284. return 0;
  1285. }
  1286. /*
  1287. * 1 -- we can write to this track, 0 -- we can't
  1288. */
  1289. static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
  1290. {
  1291. switch (pd->mmc3_profile) {
  1292. case 0x1a: /* DVD+RW */
  1293. case 0x12: /* DVD-RAM */
  1294. /* The track is always writable on DVD+RW/DVD-RAM */
  1295. return 1;
  1296. default:
  1297. break;
  1298. }
  1299. if (!ti->packet || !ti->fp)
  1300. return 0;
  1301. /*
  1302. * "good" settings as per Mt Fuji.
  1303. */
  1304. if (ti->rt == 0 && ti->blank == 0)
  1305. return 1;
  1306. if (ti->rt == 0 && ti->blank == 1)
  1307. return 1;
  1308. if (ti->rt == 1 && ti->blank == 0)
  1309. return 1;
  1310. printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
  1311. return 0;
  1312. }
  1313. /*
  1314. * 1 -- we can write to this disc, 0 -- we can't
  1315. */
  1316. static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
  1317. {
  1318. switch (pd->mmc3_profile) {
  1319. case 0x0a: /* CD-RW */
  1320. case 0xffff: /* MMC3 not supported */
  1321. break;
  1322. case 0x1a: /* DVD+RW */
  1323. case 0x13: /* DVD-RW */
  1324. case 0x12: /* DVD-RAM */
  1325. return 1;
  1326. default:
  1327. VPRINTK("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
  1328. return 0;
  1329. }
  1330. /*
  1331. * for disc type 0xff we should probably reserve a new track.
  1332. * but i'm not sure, should we leave this to user apps? probably.
  1333. */
  1334. if (di->disc_type == 0xff) {
  1335. printk("pktcdvd: Unknown disc. No track?\n");
  1336. return 0;
  1337. }
  1338. if (di->disc_type != 0x20 && di->disc_type != 0) {
  1339. printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
  1340. return 0;
  1341. }
  1342. if (di->erasable == 0) {
  1343. printk("pktcdvd: Disc not erasable\n");
  1344. return 0;
  1345. }
  1346. if (di->border_status == PACKET_SESSION_RESERVED) {
  1347. printk("pktcdvd: Can't write to last track (reserved)\n");
  1348. return 0;
  1349. }
  1350. return 1;
  1351. }
  1352. static int pkt_probe_settings(struct pktcdvd_device *pd)
  1353. {
  1354. struct packet_command cgc;
  1355. unsigned char buf[12];
  1356. disc_information di;
  1357. track_information ti;
  1358. int ret, track;
  1359. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1360. cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
  1361. cgc.cmd[8] = 8;
  1362. ret = pkt_generic_packet(pd, &cgc);
  1363. pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
  1364. memset(&di, 0, sizeof(disc_information));
  1365. memset(&ti, 0, sizeof(track_information));
  1366. if ((ret = pkt_get_disc_info(pd, &di))) {
  1367. printk("failed get_disc\n");
  1368. return ret;
  1369. }
  1370. if (!pkt_writable_disc(pd, &di))
  1371. return -EROFS;
  1372. pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
  1373. track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
  1374. if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
  1375. printk("pktcdvd: failed get_track\n");
  1376. return ret;
  1377. }
  1378. if (!pkt_writable_track(pd, &ti)) {
  1379. printk("pktcdvd: can't write to this track\n");
  1380. return -EROFS;
  1381. }
  1382. /*
  1383. * we keep packet size in 512 byte units, makes it easier to
  1384. * deal with request calculations.
  1385. */
  1386. pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
  1387. if (pd->settings.size == 0) {
  1388. printk("pktcdvd: detected zero packet size!\n");
  1389. return -ENXIO;
  1390. }
  1391. if (pd->settings.size > PACKET_MAX_SECTORS) {
  1392. printk("pktcdvd: packet size is too big\n");
  1393. return -EROFS;
  1394. }
  1395. pd->settings.fp = ti.fp;
  1396. pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
  1397. if (ti.nwa_v) {
  1398. pd->nwa = be32_to_cpu(ti.next_writable);
  1399. set_bit(PACKET_NWA_VALID, &pd->flags);
  1400. }
  1401. /*
  1402. * in theory we could use lra on -RW media as well and just zero
  1403. * blocks that haven't been written yet, but in practice that
  1404. * is just a no-go. we'll use that for -R, naturally.
  1405. */
  1406. if (ti.lra_v) {
  1407. pd->lra = be32_to_cpu(ti.last_rec_address);
  1408. set_bit(PACKET_LRA_VALID, &pd->flags);
  1409. } else {
  1410. pd->lra = 0xffffffff;
  1411. set_bit(PACKET_LRA_VALID, &pd->flags);
  1412. }
  1413. /*
  1414. * fine for now
  1415. */
  1416. pd->settings.link_loss = 7;
  1417. pd->settings.write_type = 0; /* packet */
  1418. pd->settings.track_mode = ti.track_mode;
  1419. /*
  1420. * mode1 or mode2 disc
  1421. */
  1422. switch (ti.data_mode) {
  1423. case PACKET_MODE1:
  1424. pd->settings.block_mode = PACKET_BLOCK_MODE1;
  1425. break;
  1426. case PACKET_MODE2:
  1427. pd->settings.block_mode = PACKET_BLOCK_MODE2;
  1428. break;
  1429. default:
  1430. printk("pktcdvd: unknown data mode\n");
  1431. return -EROFS;
  1432. }
  1433. return 0;
  1434. }
  1435. /*
  1436. * enable/disable write caching on drive
  1437. */
  1438. static int pkt_write_caching(struct pktcdvd_device *pd, int set)
  1439. {
  1440. struct packet_command cgc;
  1441. struct request_sense sense;
  1442. unsigned char buf[64];
  1443. int ret;
  1444. memset(buf, 0, sizeof(buf));
  1445. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1446. cgc.sense = &sense;
  1447. cgc.buflen = pd->mode_offset + 12;
  1448. /*
  1449. * caching mode page might not be there, so quiet this command
  1450. */
  1451. cgc.quiet = 1;
  1452. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
  1453. return ret;
  1454. buf[pd->mode_offset + 10] |= (!!set << 2);
  1455. cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
  1456. ret = pkt_mode_select(pd, &cgc);
  1457. if (ret) {
  1458. printk("pktcdvd: write caching control failed\n");
  1459. pkt_dump_sense(&cgc);
  1460. } else if (!ret && set)
  1461. printk("pktcdvd: enabled write caching on %s\n", pd->name);
  1462. return ret;
  1463. }
  1464. static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
  1465. {
  1466. struct packet_command cgc;
  1467. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1468. cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  1469. cgc.cmd[4] = lockflag ? 1 : 0;
  1470. return pkt_generic_packet(pd, &cgc);
  1471. }
  1472. /*
  1473. * Returns drive maximum write speed
  1474. */
  1475. static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
  1476. {
  1477. struct packet_command cgc;
  1478. struct request_sense sense;
  1479. unsigned char buf[256+18];
  1480. unsigned char *cap_buf;
  1481. int ret, offset;
  1482. memset(buf, 0, sizeof(buf));
  1483. cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
  1484. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
  1485. cgc.sense = &sense;
  1486. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1487. if (ret) {
  1488. cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
  1489. sizeof(struct mode_page_header);
  1490. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1491. if (ret) {
  1492. pkt_dump_sense(&cgc);
  1493. return ret;
  1494. }
  1495. }
  1496. offset = 20; /* Obsoleted field, used by older drives */
  1497. if (cap_buf[1] >= 28)
  1498. offset = 28; /* Current write speed selected */
  1499. if (cap_buf[1] >= 30) {
  1500. /* If the drive reports at least one "Logical Unit Write
  1501. * Speed Performance Descriptor Block", use the information
  1502. * in the first block. (contains the highest speed)
  1503. */
  1504. int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
  1505. if (num_spdb > 0)
  1506. offset = 34;
  1507. }
  1508. *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
  1509. return 0;
  1510. }
  1511. /* These tables from cdrecord - I don't have orange book */
  1512. /* standard speed CD-RW (1-4x) */
  1513. static char clv_to_speed[16] = {
  1514. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1515. 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1516. };
  1517. /* high speed CD-RW (-10x) */
  1518. static char hs_clv_to_speed[16] = {
  1519. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1520. 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1521. };
  1522. /* ultra high speed CD-RW */
  1523. static char us_clv_to_speed[16] = {
  1524. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1525. 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
  1526. };
  1527. /*
  1528. * reads the maximum media speed from ATIP
  1529. */
  1530. static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
  1531. {
  1532. struct packet_command cgc;
  1533. struct request_sense sense;
  1534. unsigned char buf[64];
  1535. unsigned int size, st, sp;
  1536. int ret;
  1537. init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
  1538. cgc.sense = &sense;
  1539. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1540. cgc.cmd[1] = 2;
  1541. cgc.cmd[2] = 4; /* READ ATIP */
  1542. cgc.cmd[8] = 2;
  1543. ret = pkt_generic_packet(pd, &cgc);
  1544. if (ret) {
  1545. pkt_dump_sense(&cgc);
  1546. return ret;
  1547. }
  1548. size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
  1549. if (size > sizeof(buf))
  1550. size = sizeof(buf);
  1551. init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
  1552. cgc.sense = &sense;
  1553. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1554. cgc.cmd[1] = 2;
  1555. cgc.cmd[2] = 4;
  1556. cgc.cmd[8] = size;
  1557. ret = pkt_generic_packet(pd, &cgc);
  1558. if (ret) {
  1559. pkt_dump_sense(&cgc);
  1560. return ret;
  1561. }
  1562. if (!buf[6] & 0x40) {
  1563. printk("pktcdvd: Disc type is not CD-RW\n");
  1564. return 1;
  1565. }
  1566. if (!buf[6] & 0x4) {
  1567. printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
  1568. return 1;
  1569. }
  1570. st = (buf[6] >> 3) & 0x7; /* disc sub-type */
  1571. sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
  1572. /* Info from cdrecord */
  1573. switch (st) {
  1574. case 0: /* standard speed */
  1575. *speed = clv_to_speed[sp];
  1576. break;
  1577. case 1: /* high speed */
  1578. *speed = hs_clv_to_speed[sp];
  1579. break;
  1580. case 2: /* ultra high speed */
  1581. *speed = us_clv_to_speed[sp];
  1582. break;
  1583. default:
  1584. printk("pktcdvd: Unknown disc sub-type %d\n",st);
  1585. return 1;
  1586. }
  1587. if (*speed) {
  1588. printk("pktcdvd: Max. media speed: %d\n",*speed);
  1589. return 0;
  1590. } else {
  1591. printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
  1592. return 1;
  1593. }
  1594. }
  1595. static int pkt_perform_opc(struct pktcdvd_device *pd)
  1596. {
  1597. struct packet_command cgc;
  1598. struct request_sense sense;
  1599. int ret;
  1600. VPRINTK("pktcdvd: Performing OPC\n");
  1601. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1602. cgc.sense = &sense;
  1603. cgc.timeout = 60*HZ;
  1604. cgc.cmd[0] = GPCMD_SEND_OPC;
  1605. cgc.cmd[1] = 1;
  1606. if ((ret = pkt_generic_packet(pd, &cgc)))
  1607. pkt_dump_sense(&cgc);
  1608. return ret;
  1609. }
  1610. static int pkt_open_write(struct pktcdvd_device *pd)
  1611. {
  1612. int ret;
  1613. unsigned int write_speed, media_write_speed, read_speed;
  1614. if ((ret = pkt_probe_settings(pd))) {
  1615. VPRINTK("pktcdvd: %s failed probe\n", pd->name);
  1616. return ret;
  1617. }
  1618. if ((ret = pkt_set_write_settings(pd))) {
  1619. DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
  1620. return -EIO;
  1621. }
  1622. pkt_write_caching(pd, USE_WCACHING);
  1623. if ((ret = pkt_get_max_speed(pd, &write_speed)))
  1624. write_speed = 16 * 177;
  1625. switch (pd->mmc3_profile) {
  1626. case 0x13: /* DVD-RW */
  1627. case 0x1a: /* DVD+RW */
  1628. case 0x12: /* DVD-RAM */
  1629. DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
  1630. break;
  1631. default:
  1632. if ((ret = pkt_media_speed(pd, &media_write_speed)))
  1633. media_write_speed = 16;
  1634. write_speed = min(write_speed, media_write_speed * 177);
  1635. DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
  1636. break;
  1637. }
  1638. read_speed = write_speed;
  1639. if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
  1640. DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
  1641. return -EIO;
  1642. }
  1643. pd->write_speed = write_speed;
  1644. pd->read_speed = read_speed;
  1645. if ((ret = pkt_perform_opc(pd))) {
  1646. DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
  1647. }
  1648. return 0;
  1649. }
  1650. /*
  1651. * called at open time.
  1652. */
  1653. static int pkt_open_dev(struct pktcdvd_device *pd, int write)
  1654. {
  1655. int ret;
  1656. long lba;
  1657. request_queue_t *q;
  1658. /*
  1659. * We need to re-open the cdrom device without O_NONBLOCK to be able
  1660. * to read/write from/to it. It is already opened in O_NONBLOCK mode
  1661. * so bdget() can't fail.
  1662. */
  1663. bdget(pd->bdev->bd_dev);
  1664. if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
  1665. goto out;
  1666. if ((ret = bd_claim(pd->bdev, pd)))
  1667. goto out_putdev;
  1668. if ((ret = pkt_get_last_written(pd, &lba))) {
  1669. printk("pktcdvd: pkt_get_last_written failed\n");
  1670. goto out_unclaim;
  1671. }
  1672. set_capacity(pd->disk, lba << 2);
  1673. set_capacity(pd->bdev->bd_disk, lba << 2);
  1674. bd_set_size(pd->bdev, (loff_t)lba << 11);
  1675. q = bdev_get_queue(pd->bdev);
  1676. if (write) {
  1677. if ((ret = pkt_open_write(pd)))
  1678. goto out_unclaim;
  1679. /*
  1680. * Some CDRW drives can not handle writes larger than one packet,
  1681. * even if the size is a multiple of the packet size.
  1682. */
  1683. spin_lock_irq(q->queue_lock);
  1684. blk_queue_max_sectors(q, pd->settings.size);
  1685. spin_unlock_irq(q->queue_lock);
  1686. set_bit(PACKET_WRITABLE, &pd->flags);
  1687. } else {
  1688. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  1689. clear_bit(PACKET_WRITABLE, &pd->flags);
  1690. }
  1691. if ((ret = pkt_set_segment_merging(pd, q)))
  1692. goto out_unclaim;
  1693. if (write) {
  1694. if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
  1695. printk("pktcdvd: not enough memory for buffers\n");
  1696. ret = -ENOMEM;
  1697. goto out_unclaim;
  1698. }
  1699. printk("pktcdvd: %lukB available on disc\n", lba << 1);
  1700. }
  1701. return 0;
  1702. out_unclaim:
  1703. bd_release(pd->bdev);
  1704. out_putdev:
  1705. blkdev_put(pd->bdev);
  1706. out:
  1707. return ret;
  1708. }
  1709. /*
  1710. * called when the device is closed. makes sure that the device flushes
  1711. * the internal cache before we close.
  1712. */
  1713. static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
  1714. {
  1715. if (flush && pkt_flush_cache(pd))
  1716. DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
  1717. pkt_lock_door(pd, 0);
  1718. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  1719. bd_release(pd->bdev);
  1720. blkdev_put(pd->bdev);
  1721. pkt_shrink_pktlist(pd);
  1722. }
  1723. static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
  1724. {
  1725. if (dev_minor >= MAX_WRITERS)
  1726. return NULL;
  1727. return pkt_devs[dev_minor];
  1728. }
  1729. static int pkt_open(struct inode *inode, struct file *file)
  1730. {
  1731. struct pktcdvd_device *pd = NULL;
  1732. int ret;
  1733. VPRINTK("pktcdvd: entering open\n");
  1734. down(&ctl_mutex);
  1735. pd = pkt_find_dev_from_minor(iminor(inode));
  1736. if (!pd) {
  1737. ret = -ENODEV;
  1738. goto out;
  1739. }
  1740. BUG_ON(pd->refcnt < 0);
  1741. pd->refcnt++;
  1742. if (pd->refcnt > 1) {
  1743. if ((file->f_mode & FMODE_WRITE) &&
  1744. !test_bit(PACKET_WRITABLE, &pd->flags)) {
  1745. ret = -EBUSY;
  1746. goto out_dec;
  1747. }
  1748. } else {
  1749. ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
  1750. if (ret)
  1751. goto out_dec;
  1752. /*
  1753. * needed here as well, since ext2 (among others) may change
  1754. * the blocksize at mount time
  1755. */
  1756. set_blocksize(inode->i_bdev, CD_FRAMESIZE);
  1757. }
  1758. up(&ctl_mutex);
  1759. return 0;
  1760. out_dec:
  1761. pd->refcnt--;
  1762. out:
  1763. VPRINTK("pktcdvd: failed open (%d)\n", ret);
  1764. up(&ctl_mutex);
  1765. return ret;
  1766. }
  1767. static int pkt_close(struct inode *inode, struct file *file)
  1768. {
  1769. struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
  1770. int ret = 0;
  1771. down(&ctl_mutex);
  1772. pd->refcnt--;
  1773. BUG_ON(pd->refcnt < 0);
  1774. if (pd->refcnt == 0) {
  1775. int flush = test_bit(PACKET_WRITABLE, &pd->flags);
  1776. pkt_release_dev(pd, flush);
  1777. }
  1778. up(&ctl_mutex);
  1779. return ret;
  1780. }
  1781. static void *psd_pool_alloc(gfp_t gfp_mask, void *data)
  1782. {
  1783. return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
  1784. }
  1785. static void psd_pool_free(void *ptr, void *data)
  1786. {
  1787. kfree(ptr);
  1788. }
  1789. static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
  1790. {
  1791. struct packet_stacked_data *psd = bio->bi_private;
  1792. struct pktcdvd_device *pd = psd->pd;
  1793. if (bio->bi_size)
  1794. return 1;
  1795. bio_put(bio);
  1796. bio_endio(psd->bio, psd->bio->bi_size, err);
  1797. mempool_free(psd, psd_pool);
  1798. pkt_bio_finished(pd);
  1799. return 0;
  1800. }
  1801. static int pkt_make_request(request_queue_t *q, struct bio *bio)
  1802. {
  1803. struct pktcdvd_device *pd;
  1804. char b[BDEVNAME_SIZE];
  1805. sector_t zone;
  1806. struct packet_data *pkt;
  1807. int was_empty, blocked_bio;
  1808. struct pkt_rb_node *node;
  1809. pd = q->queuedata;
  1810. if (!pd) {
  1811. printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
  1812. goto end_io;
  1813. }
  1814. /*
  1815. * Clone READ bios so we can have our own bi_end_io callback.
  1816. */
  1817. if (bio_data_dir(bio) == READ) {
  1818. struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
  1819. struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
  1820. psd->pd = pd;
  1821. psd->bio = bio;
  1822. cloned_bio->bi_bdev = pd->bdev;
  1823. cloned_bio->bi_private = psd;
  1824. cloned_bio->bi_end_io = pkt_end_io_read_cloned;
  1825. pd->stats.secs_r += bio->bi_size >> 9;
  1826. pkt_queue_bio(pd, cloned_bio);
  1827. return 0;
  1828. }
  1829. if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
  1830. printk("pktcdvd: WRITE for ro device %s (%llu)\n",
  1831. pd->name, (unsigned long long)bio->bi_sector);
  1832. goto end_io;
  1833. }
  1834. if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
  1835. printk("pktcdvd: wrong bio size\n");
  1836. goto end_io;
  1837. }
  1838. blk_queue_bounce(q, &bio);
  1839. zone = ZONE(bio->bi_sector, pd);
  1840. VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
  1841. (unsigned long long)bio->bi_sector,
  1842. (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
  1843. /* Check if we have to split the bio */
  1844. {
  1845. struct bio_pair *bp;
  1846. sector_t last_zone;
  1847. int first_sectors;
  1848. last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
  1849. if (last_zone != zone) {
  1850. BUG_ON(last_zone != zone + pd->settings.size);
  1851. first_sectors = last_zone - bio->bi_sector;
  1852. bp = bio_split(bio, bio_split_pool, first_sectors);
  1853. BUG_ON(!bp);
  1854. pkt_make_request(q, &bp->bio1);
  1855. pkt_make_request(q, &bp->bio2);
  1856. bio_pair_release(bp);
  1857. return 0;
  1858. }
  1859. }
  1860. /*
  1861. * If we find a matching packet in state WAITING or READ_WAIT, we can
  1862. * just append this bio to that packet.
  1863. */
  1864. spin_lock(&pd->cdrw.active_list_lock);
  1865. blocked_bio = 0;
  1866. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1867. if (pkt->sector == zone) {
  1868. spin_lock(&pkt->lock);
  1869. if ((pkt->state == PACKET_WAITING_STATE) ||
  1870. (pkt->state == PACKET_READ_WAIT_STATE)) {
  1871. pkt_add_list_last(bio, &pkt->orig_bios,
  1872. &pkt->orig_bios_tail);
  1873. pkt->write_size += bio->bi_size / CD_FRAMESIZE;
  1874. if ((pkt->write_size >= pkt->frames) &&
  1875. (pkt->state == PACKET_WAITING_STATE)) {
  1876. atomic_inc(&pkt->run_sm);
  1877. wake_up(&pd->wqueue);
  1878. }
  1879. spin_unlock(&pkt->lock);
  1880. spin_unlock(&pd->cdrw.active_list_lock);
  1881. return 0;
  1882. } else {
  1883. blocked_bio = 1;
  1884. }
  1885. spin_unlock(&pkt->lock);
  1886. }
  1887. }
  1888. spin_unlock(&pd->cdrw.active_list_lock);
  1889. /*
  1890. * No matching packet found. Store the bio in the work queue.
  1891. */
  1892. node = mempool_alloc(pd->rb_pool, GFP_NOIO);
  1893. node->bio = bio;
  1894. spin_lock(&pd->lock);
  1895. BUG_ON(pd->bio_queue_size < 0);
  1896. was_empty = (pd->bio_queue_size == 0);
  1897. pkt_rbtree_insert(pd, node);
  1898. spin_unlock(&pd->lock);
  1899. /*
  1900. * Wake up the worker thread.
  1901. */
  1902. atomic_set(&pd->scan_queue, 1);
  1903. if (was_empty) {
  1904. /* This wake_up is required for correct operation */
  1905. wake_up(&pd->wqueue);
  1906. } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
  1907. /*
  1908. * This wake up is not required for correct operation,
  1909. * but improves performance in some cases.
  1910. */
  1911. wake_up(&pd->wqueue);
  1912. }
  1913. return 0;
  1914. end_io:
  1915. bio_io_error(bio, bio->bi_size);
  1916. return 0;
  1917. }
  1918. static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
  1919. {
  1920. struct pktcdvd_device *pd = q->queuedata;
  1921. sector_t zone = ZONE(bio->bi_sector, pd);
  1922. int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
  1923. int remaining = (pd->settings.size << 9) - used;
  1924. int remaining2;
  1925. /*
  1926. * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
  1927. * boundary, pkt_make_request() will split the bio.
  1928. */
  1929. remaining2 = PAGE_SIZE - bio->bi_size;
  1930. remaining = max(remaining, remaining2);
  1931. BUG_ON(remaining < 0);
  1932. return remaining;
  1933. }
  1934. static void pkt_init_queue(struct pktcdvd_device *pd)
  1935. {
  1936. request_queue_t *q = pd->disk->queue;
  1937. blk_queue_make_request(q, pkt_make_request);
  1938. blk_queue_hardsect_size(q, CD_FRAMESIZE);
  1939. blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
  1940. blk_queue_merge_bvec(q, pkt_merge_bvec);
  1941. q->queuedata = pd;
  1942. }
  1943. static int pkt_seq_show(struct seq_file *m, void *p)
  1944. {
  1945. struct pktcdvd_device *pd = m->private;
  1946. char *msg;
  1947. char bdev_buf[BDEVNAME_SIZE];
  1948. int states[PACKET_NUM_STATES];
  1949. seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
  1950. bdevname(pd->bdev, bdev_buf));
  1951. seq_printf(m, "\nSettings:\n");
  1952. seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
  1953. if (pd->settings.write_type == 0)
  1954. msg = "Packet";
  1955. else
  1956. msg = "Unknown";
  1957. seq_printf(m, "\twrite type:\t\t%s\n", msg);
  1958. seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
  1959. seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
  1960. seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
  1961. if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
  1962. msg = "Mode 1";
  1963. else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
  1964. msg = "Mode 2";
  1965. else
  1966. msg = "Unknown";
  1967. seq_printf(m, "\tblock mode:\t\t%s\n", msg);
  1968. seq_printf(m, "\nStatistics:\n");
  1969. seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
  1970. seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
  1971. seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
  1972. seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
  1973. seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
  1974. seq_printf(m, "\nMisc:\n");
  1975. seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
  1976. seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
  1977. seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
  1978. seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
  1979. seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
  1980. seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
  1981. seq_printf(m, "\nQueue state:\n");
  1982. seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
  1983. seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
  1984. seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
  1985. pkt_count_states(pd, states);
  1986. seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  1987. states[0], states[1], states[2], states[3], states[4], states[5]);
  1988. return 0;
  1989. }
  1990. static int pkt_seq_open(struct inode *inode, struct file *file)
  1991. {
  1992. return single_open(file, pkt_seq_show, PDE(inode)->data);
  1993. }
  1994. static struct file_operations pkt_proc_fops = {
  1995. .open = pkt_seq_open,
  1996. .read = seq_read,
  1997. .llseek = seq_lseek,
  1998. .release = single_release
  1999. };
  2000. static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
  2001. {
  2002. int i;
  2003. int ret = 0;
  2004. char b[BDEVNAME_SIZE];
  2005. struct proc_dir_entry *proc;
  2006. struct block_device *bdev;
  2007. if (pd->pkt_dev == dev) {
  2008. printk("pktcdvd: Recursive setup not allowed\n");
  2009. return -EBUSY;
  2010. }
  2011. for (i = 0; i < MAX_WRITERS; i++) {
  2012. struct pktcdvd_device *pd2 = pkt_devs[i];
  2013. if (!pd2)
  2014. continue;
  2015. if (pd2->bdev->bd_dev == dev) {
  2016. printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
  2017. return -EBUSY;
  2018. }
  2019. if (pd2->pkt_dev == dev) {
  2020. printk("pktcdvd: Can't chain pktcdvd devices\n");
  2021. return -EBUSY;
  2022. }
  2023. }
  2024. bdev = bdget(dev);
  2025. if (!bdev)
  2026. return -ENOMEM;
  2027. ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
  2028. if (ret)
  2029. return ret;
  2030. /* This is safe, since we have a reference from open(). */
  2031. __module_get(THIS_MODULE);
  2032. pd->bdev = bdev;
  2033. set_blocksize(bdev, CD_FRAMESIZE);
  2034. pkt_init_queue(pd);
  2035. atomic_set(&pd->cdrw.pending_bios, 0);
  2036. pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
  2037. if (IS_ERR(pd->cdrw.thread)) {
  2038. printk("pktcdvd: can't start kernel thread\n");
  2039. ret = -ENOMEM;
  2040. goto out_mem;
  2041. }
  2042. proc = create_proc_entry(pd->name, 0, pkt_proc);
  2043. if (proc) {
  2044. proc->data = pd;
  2045. proc->proc_fops = &pkt_proc_fops;
  2046. }
  2047. DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
  2048. return 0;
  2049. out_mem:
  2050. blkdev_put(bdev);
  2051. /* This is safe: open() is still holding a reference. */
  2052. module_put(THIS_MODULE);
  2053. return ret;
  2054. }
  2055. static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  2056. {
  2057. struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
  2058. VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
  2059. switch (cmd) {
  2060. /*
  2061. * forward selected CDROM ioctls to CD-ROM, for UDF
  2062. */
  2063. case CDROMMULTISESSION:
  2064. case CDROMREADTOCENTRY:
  2065. case CDROM_LAST_WRITTEN:
  2066. case CDROM_SEND_PACKET:
  2067. case SCSI_IOCTL_SEND_COMMAND:
  2068. return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
  2069. case CDROMEJECT:
  2070. /*
  2071. * The door gets locked when the device is opened, so we
  2072. * have to unlock it or else the eject command fails.
  2073. */
  2074. if (pd->refcnt == 1)
  2075. pkt_lock_door(pd, 0);
  2076. return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
  2077. default:
  2078. VPRINTK("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
  2079. return -ENOTTY;
  2080. }
  2081. return 0;
  2082. }
  2083. static int pkt_media_changed(struct gendisk *disk)
  2084. {
  2085. struct pktcdvd_device *pd = disk->private_data;
  2086. struct gendisk *attached_disk;
  2087. if (!pd)
  2088. return 0;
  2089. if (!pd->bdev)
  2090. return 0;
  2091. attached_disk = pd->bdev->bd_disk;
  2092. if (!attached_disk)
  2093. return 0;
  2094. return attached_disk->fops->media_changed(attached_disk);
  2095. }
  2096. static struct block_device_operations pktcdvd_ops = {
  2097. .owner = THIS_MODULE,
  2098. .open = pkt_open,
  2099. .release = pkt_close,
  2100. .ioctl = pkt_ioctl,
  2101. .media_changed = pkt_media_changed,
  2102. };
  2103. /*
  2104. * Set up mapping from pktcdvd device to CD-ROM device.
  2105. */
  2106. static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
  2107. {
  2108. int idx;
  2109. int ret = -ENOMEM;
  2110. struct pktcdvd_device *pd;
  2111. struct gendisk *disk;
  2112. dev_t dev = new_decode_dev(ctrl_cmd->dev);
  2113. for (idx = 0; idx < MAX_WRITERS; idx++)
  2114. if (!pkt_devs[idx])
  2115. break;
  2116. if (idx == MAX_WRITERS) {
  2117. printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
  2118. return -EBUSY;
  2119. }
  2120. pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
  2121. if (!pd)
  2122. return ret;
  2123. pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
  2124. if (!pd->rb_pool)
  2125. goto out_mem;
  2126. disk = alloc_disk(1);
  2127. if (!disk)
  2128. goto out_mem;
  2129. pd->disk = disk;
  2130. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  2131. INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
  2132. spin_lock_init(&pd->cdrw.active_list_lock);
  2133. spin_lock_init(&pd->lock);
  2134. spin_lock_init(&pd->iosched.lock);
  2135. sprintf(pd->name, "pktcdvd%d", idx);
  2136. init_waitqueue_head(&pd->wqueue);
  2137. pd->bio_queue = RB_ROOT;
  2138. disk->major = pkt_major;
  2139. disk->first_minor = idx;
  2140. disk->fops = &pktcdvd_ops;
  2141. disk->flags = GENHD_FL_REMOVABLE;
  2142. sprintf(disk->disk_name, "pktcdvd%d", idx);
  2143. disk->private_data = pd;
  2144. disk->queue = blk_alloc_queue(GFP_KERNEL);
  2145. if (!disk->queue)
  2146. goto out_mem2;
  2147. pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
  2148. ret = pkt_new_dev(pd, dev);
  2149. if (ret)
  2150. goto out_new_dev;
  2151. add_disk(disk);
  2152. pkt_devs[idx] = pd;
  2153. ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
  2154. return 0;
  2155. out_new_dev:
  2156. blk_put_queue(disk->queue);
  2157. out_mem2:
  2158. put_disk(disk);
  2159. out_mem:
  2160. if (pd->rb_pool)
  2161. mempool_destroy(pd->rb_pool);
  2162. kfree(pd);
  2163. return ret;
  2164. }
  2165. /*
  2166. * Tear down mapping from pktcdvd device to CD-ROM device.
  2167. */
  2168. static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
  2169. {
  2170. struct pktcdvd_device *pd;
  2171. int idx;
  2172. dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
  2173. for (idx = 0; idx < MAX_WRITERS; idx++) {
  2174. pd = pkt_devs[idx];
  2175. if (pd && (pd->pkt_dev == pkt_dev))
  2176. break;
  2177. }
  2178. if (idx == MAX_WRITERS) {
  2179. DPRINTK("pktcdvd: dev not setup\n");
  2180. return -ENXIO;
  2181. }
  2182. if (pd->refcnt > 0)
  2183. return -EBUSY;
  2184. if (!IS_ERR(pd->cdrw.thread))
  2185. kthread_stop(pd->cdrw.thread);
  2186. blkdev_put(pd->bdev);
  2187. remove_proc_entry(pd->name, pkt_proc);
  2188. DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
  2189. del_gendisk(pd->disk);
  2190. blk_put_queue(pd->disk->queue);
  2191. put_disk(pd->disk);
  2192. pkt_devs[idx] = NULL;
  2193. mempool_destroy(pd->rb_pool);
  2194. kfree(pd);
  2195. /* This is safe: open() is still holding a reference. */
  2196. module_put(THIS_MODULE);
  2197. return 0;
  2198. }
  2199. static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
  2200. {
  2201. struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
  2202. if (pd) {
  2203. ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
  2204. ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
  2205. } else {
  2206. ctrl_cmd->dev = 0;
  2207. ctrl_cmd->pkt_dev = 0;
  2208. }
  2209. ctrl_cmd->num_devices = MAX_WRITERS;
  2210. }
  2211. static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  2212. {
  2213. void __user *argp = (void __user *)arg;
  2214. struct pkt_ctrl_command ctrl_cmd;
  2215. int ret = 0;
  2216. if (cmd != PACKET_CTRL_CMD)
  2217. return -ENOTTY;
  2218. if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
  2219. return -EFAULT;
  2220. switch (ctrl_cmd.command) {
  2221. case PKT_CTRL_CMD_SETUP:
  2222. if (!capable(CAP_SYS_ADMIN))
  2223. return -EPERM;
  2224. down(&ctl_mutex);
  2225. ret = pkt_setup_dev(&ctrl_cmd);
  2226. up(&ctl_mutex);
  2227. break;
  2228. case PKT_CTRL_CMD_TEARDOWN:
  2229. if (!capable(CAP_SYS_ADMIN))
  2230. return -EPERM;
  2231. down(&ctl_mutex);
  2232. ret = pkt_remove_dev(&ctrl_cmd);
  2233. up(&ctl_mutex);
  2234. break;
  2235. case PKT_CTRL_CMD_STATUS:
  2236. down(&ctl_mutex);
  2237. pkt_get_status(&ctrl_cmd);
  2238. up(&ctl_mutex);
  2239. break;
  2240. default:
  2241. return -ENOTTY;
  2242. }
  2243. if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
  2244. return -EFAULT;
  2245. return ret;
  2246. }
  2247. static struct file_operations pkt_ctl_fops = {
  2248. .ioctl = pkt_ctl_ioctl,
  2249. .owner = THIS_MODULE,
  2250. };
  2251. static struct miscdevice pkt_misc = {
  2252. .minor = MISC_DYNAMIC_MINOR,
  2253. .name = "pktcdvd",
  2254. .devfs_name = "pktcdvd/control",
  2255. .fops = &pkt_ctl_fops
  2256. };
  2257. static int __init pkt_init(void)
  2258. {
  2259. int ret;
  2260. psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
  2261. if (!psd_pool)
  2262. return -ENOMEM;
  2263. ret = register_blkdev(pkt_major, "pktcdvd");
  2264. if (ret < 0) {
  2265. printk("pktcdvd: Unable to register block device\n");
  2266. goto out2;
  2267. }
  2268. if (!pkt_major)
  2269. pkt_major = ret;
  2270. ret = misc_register(&pkt_misc);
  2271. if (ret) {
  2272. printk("pktcdvd: Unable to register misc device\n");
  2273. goto out;
  2274. }
  2275. init_MUTEX(&ctl_mutex);
  2276. pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
  2277. return 0;
  2278. out:
  2279. unregister_blkdev(pkt_major, "pktcdvd");
  2280. out2:
  2281. mempool_destroy(psd_pool);
  2282. return ret;
  2283. }
  2284. static void __exit pkt_exit(void)
  2285. {
  2286. remove_proc_entry("pktcdvd", proc_root_driver);
  2287. misc_deregister(&pkt_misc);
  2288. unregister_blkdev(pkt_major, "pktcdvd");
  2289. mempool_destroy(psd_pool);
  2290. }
  2291. MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
  2292. MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
  2293. MODULE_LICENSE("GPL");
  2294. module_init(pkt_init);
  2295. module_exit(pkt_exit);