pktcdvd.c 65 KB

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