pktcdvd.c 77 KB

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