pktcdvd.c 77 KB

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