pktcdvd.c 73 KB

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