pktcdvd.c 76 KB

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