pktcdvd.c 66 KB

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