pktcdvd.c 65 KB

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