pktcdvd.c 71 KB

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