pktcdvd.c 65 KB

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