pktcdvd.c 65 KB

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