pktcdvd.c 74 KB

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