pktcdvd.c 73 KB

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