pktcdvd.c 65 KB

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