pktcdvd.c 74 KB

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