pktcdvd.c 71 KB

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