pktcdvd.c 74 KB

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