pktcdvd.c 65 KB

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