pktcdvd.c 76 KB

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