pktcdvd.c 71 KB

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