pktcdvd.c 65 KB

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