pktcdvd.c 76 KB

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