pktcdvd.c 76 KB

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