pktcdvd.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  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_RECLAIM);
  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_RECLAIM);
  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)
  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, bio->bi_error);
  863. if (bio->bi_error)
  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)
  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, bio->bi_error);
  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_set_op_attrs(bio, REQ_OP_READ, 0);
  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. bio_set_op_attrs(pkt->bio, REQ_OP_WRITE, 0);
  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. bio_set_op_attrs(pkt->w_bio, REQ_OP_WRITE, 0);
  1166. pkt_queue_bio(pd, pkt->w_bio);
  1167. }
  1168. static void pkt_finish_packet(struct packet_data *pkt, int error)
  1169. {
  1170. struct bio *bio;
  1171. if (error)
  1172. pkt->cache_valid = 0;
  1173. /* Finish all bios corresponding to this packet */
  1174. while ((bio = bio_list_pop(&pkt->orig_bios))) {
  1175. bio->bi_error = error;
  1176. bio_endio(bio);
  1177. }
  1178. }
  1179. static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
  1180. {
  1181. pkt_dbg(2, pd, "pkt %d\n", pkt->id);
  1182. for (;;) {
  1183. switch (pkt->state) {
  1184. case PACKET_WAITING_STATE:
  1185. if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
  1186. return;
  1187. pkt->sleep_time = 0;
  1188. pkt_gather_data(pd, pkt);
  1189. pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
  1190. break;
  1191. case PACKET_READ_WAIT_STATE:
  1192. if (atomic_read(&pkt->io_wait) > 0)
  1193. return;
  1194. if (atomic_read(&pkt->io_errors) > 0) {
  1195. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1196. } else {
  1197. pkt_start_write(pd, pkt);
  1198. }
  1199. break;
  1200. case PACKET_WRITE_WAIT_STATE:
  1201. if (atomic_read(&pkt->io_wait) > 0)
  1202. return;
  1203. if (!pkt->w_bio->bi_error) {
  1204. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1205. } else {
  1206. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1207. }
  1208. break;
  1209. case PACKET_RECOVERY_STATE:
  1210. if (pkt_start_recovery(pkt)) {
  1211. pkt_start_write(pd, pkt);
  1212. } else {
  1213. pkt_dbg(2, pd, "No recovery possible\n");
  1214. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1215. }
  1216. break;
  1217. case PACKET_FINISHED_STATE:
  1218. pkt_finish_packet(pkt, pkt->w_bio->bi_error);
  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)
  2011. {
  2012. struct packet_stacked_data *psd = bio->bi_private;
  2013. struct pktcdvd_device *pd = psd->pd;
  2014. psd->bio->bi_error = bio->bi_error;
  2015. bio_put(bio);
  2016. bio_endio(psd->bio);
  2017. mempool_free(psd, psd_pool);
  2018. pkt_bio_finished(pd);
  2019. }
  2020. static void pkt_make_request_read(struct pktcdvd_device *pd, struct bio *bio)
  2021. {
  2022. struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
  2023. struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
  2024. psd->pd = pd;
  2025. psd->bio = bio;
  2026. cloned_bio->bi_bdev = pd->bdev;
  2027. cloned_bio->bi_private = psd;
  2028. cloned_bio->bi_end_io = pkt_end_io_read_cloned;
  2029. pd->stats.secs_r += bio_sectors(bio);
  2030. pkt_queue_bio(pd, cloned_bio);
  2031. }
  2032. static void pkt_make_request_write(struct request_queue *q, struct bio *bio)
  2033. {
  2034. struct pktcdvd_device *pd = q->queuedata;
  2035. sector_t zone;
  2036. struct packet_data *pkt;
  2037. int was_empty, blocked_bio;
  2038. struct pkt_rb_node *node;
  2039. zone = get_zone(bio->bi_iter.bi_sector, pd);
  2040. /*
  2041. * If we find a matching packet in state WAITING or READ_WAIT, we can
  2042. * just append this bio to that packet.
  2043. */
  2044. spin_lock(&pd->cdrw.active_list_lock);
  2045. blocked_bio = 0;
  2046. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  2047. if (pkt->sector == zone) {
  2048. spin_lock(&pkt->lock);
  2049. if ((pkt->state == PACKET_WAITING_STATE) ||
  2050. (pkt->state == PACKET_READ_WAIT_STATE)) {
  2051. bio_list_add(&pkt->orig_bios, bio);
  2052. pkt->write_size +=
  2053. bio->bi_iter.bi_size / CD_FRAMESIZE;
  2054. if ((pkt->write_size >= pkt->frames) &&
  2055. (pkt->state == PACKET_WAITING_STATE)) {
  2056. atomic_inc(&pkt->run_sm);
  2057. wake_up(&pd->wqueue);
  2058. }
  2059. spin_unlock(&pkt->lock);
  2060. spin_unlock(&pd->cdrw.active_list_lock);
  2061. return;
  2062. } else {
  2063. blocked_bio = 1;
  2064. }
  2065. spin_unlock(&pkt->lock);
  2066. }
  2067. }
  2068. spin_unlock(&pd->cdrw.active_list_lock);
  2069. /*
  2070. * Test if there is enough room left in the bio work queue
  2071. * (queue size >= congestion on mark).
  2072. * If not, wait till the work queue size is below the congestion off mark.
  2073. */
  2074. spin_lock(&pd->lock);
  2075. if (pd->write_congestion_on > 0
  2076. && pd->bio_queue_size >= pd->write_congestion_on) {
  2077. set_bdi_congested(&q->backing_dev_info, BLK_RW_ASYNC);
  2078. do {
  2079. spin_unlock(&pd->lock);
  2080. congestion_wait(BLK_RW_ASYNC, HZ);
  2081. spin_lock(&pd->lock);
  2082. } while(pd->bio_queue_size > pd->write_congestion_off);
  2083. }
  2084. spin_unlock(&pd->lock);
  2085. /*
  2086. * No matching packet found. Store the bio in the work queue.
  2087. */
  2088. node = mempool_alloc(pd->rb_pool, GFP_NOIO);
  2089. node->bio = bio;
  2090. spin_lock(&pd->lock);
  2091. BUG_ON(pd->bio_queue_size < 0);
  2092. was_empty = (pd->bio_queue_size == 0);
  2093. pkt_rbtree_insert(pd, node);
  2094. spin_unlock(&pd->lock);
  2095. /*
  2096. * Wake up the worker thread.
  2097. */
  2098. atomic_set(&pd->scan_queue, 1);
  2099. if (was_empty) {
  2100. /* This wake_up is required for correct operation */
  2101. wake_up(&pd->wqueue);
  2102. } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
  2103. /*
  2104. * This wake up is not required for correct operation,
  2105. * but improves performance in some cases.
  2106. */
  2107. wake_up(&pd->wqueue);
  2108. }
  2109. }
  2110. static blk_qc_t pkt_make_request(struct request_queue *q, struct bio *bio)
  2111. {
  2112. struct pktcdvd_device *pd;
  2113. char b[BDEVNAME_SIZE];
  2114. struct bio *split;
  2115. blk_queue_bounce(q, &bio);
  2116. blk_queue_split(q, &bio, q->bio_split);
  2117. pd = q->queuedata;
  2118. if (!pd) {
  2119. pr_err("%s incorrect request queue\n",
  2120. bdevname(bio->bi_bdev, b));
  2121. goto end_io;
  2122. }
  2123. pkt_dbg(2, pd, "start = %6llx stop = %6llx\n",
  2124. (unsigned long long)bio->bi_iter.bi_sector,
  2125. (unsigned long long)bio_end_sector(bio));
  2126. /*
  2127. * Clone READ bios so we can have our own bi_end_io callback.
  2128. */
  2129. if (bio_data_dir(bio) == READ) {
  2130. pkt_make_request_read(pd, bio);
  2131. return BLK_QC_T_NONE;
  2132. }
  2133. if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
  2134. pkt_notice(pd, "WRITE for ro device (%llu)\n",
  2135. (unsigned long long)bio->bi_iter.bi_sector);
  2136. goto end_io;
  2137. }
  2138. if (!bio->bi_iter.bi_size || (bio->bi_iter.bi_size % CD_FRAMESIZE)) {
  2139. pkt_err(pd, "wrong bio size\n");
  2140. goto end_io;
  2141. }
  2142. do {
  2143. sector_t zone = get_zone(bio->bi_iter.bi_sector, pd);
  2144. sector_t last_zone = get_zone(bio_end_sector(bio) - 1, pd);
  2145. if (last_zone != zone) {
  2146. BUG_ON(last_zone != zone + pd->settings.size);
  2147. split = bio_split(bio, last_zone -
  2148. bio->bi_iter.bi_sector,
  2149. GFP_NOIO, fs_bio_set);
  2150. bio_chain(split, bio);
  2151. } else {
  2152. split = bio;
  2153. }
  2154. pkt_make_request_write(q, split);
  2155. } while (split != bio);
  2156. return BLK_QC_T_NONE;
  2157. end_io:
  2158. bio_io_error(bio);
  2159. return BLK_QC_T_NONE;
  2160. }
  2161. static void pkt_init_queue(struct pktcdvd_device *pd)
  2162. {
  2163. struct request_queue *q = pd->disk->queue;
  2164. blk_queue_make_request(q, pkt_make_request);
  2165. blk_queue_logical_block_size(q, CD_FRAMESIZE);
  2166. blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
  2167. q->queuedata = pd;
  2168. }
  2169. static int pkt_seq_show(struct seq_file *m, void *p)
  2170. {
  2171. struct pktcdvd_device *pd = m->private;
  2172. char *msg;
  2173. char bdev_buf[BDEVNAME_SIZE];
  2174. int states[PACKET_NUM_STATES];
  2175. seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
  2176. bdevname(pd->bdev, bdev_buf));
  2177. seq_printf(m, "\nSettings:\n");
  2178. seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
  2179. if (pd->settings.write_type == 0)
  2180. msg = "Packet";
  2181. else
  2182. msg = "Unknown";
  2183. seq_printf(m, "\twrite type:\t\t%s\n", msg);
  2184. seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
  2185. seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
  2186. seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
  2187. if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
  2188. msg = "Mode 1";
  2189. else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
  2190. msg = "Mode 2";
  2191. else
  2192. msg = "Unknown";
  2193. seq_printf(m, "\tblock mode:\t\t%s\n", msg);
  2194. seq_printf(m, "\nStatistics:\n");
  2195. seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
  2196. seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
  2197. seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
  2198. seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
  2199. seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
  2200. seq_printf(m, "\nMisc:\n");
  2201. seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
  2202. seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
  2203. seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
  2204. seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
  2205. seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
  2206. seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
  2207. seq_printf(m, "\nQueue state:\n");
  2208. seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
  2209. seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
  2210. seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
  2211. pkt_count_states(pd, states);
  2212. seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  2213. states[0], states[1], states[2], states[3], states[4], states[5]);
  2214. seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
  2215. pd->write_congestion_off,
  2216. pd->write_congestion_on);
  2217. return 0;
  2218. }
  2219. static int pkt_seq_open(struct inode *inode, struct file *file)
  2220. {
  2221. return single_open(file, pkt_seq_show, PDE_DATA(inode));
  2222. }
  2223. static const struct file_operations pkt_proc_fops = {
  2224. .open = pkt_seq_open,
  2225. .read = seq_read,
  2226. .llseek = seq_lseek,
  2227. .release = single_release
  2228. };
  2229. static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
  2230. {
  2231. int i;
  2232. int ret = 0;
  2233. char b[BDEVNAME_SIZE];
  2234. struct block_device *bdev;
  2235. if (pd->pkt_dev == dev) {
  2236. pkt_err(pd, "recursive setup not allowed\n");
  2237. return -EBUSY;
  2238. }
  2239. for (i = 0; i < MAX_WRITERS; i++) {
  2240. struct pktcdvd_device *pd2 = pkt_devs[i];
  2241. if (!pd2)
  2242. continue;
  2243. if (pd2->bdev->bd_dev == dev) {
  2244. pkt_err(pd, "%s already setup\n",
  2245. bdevname(pd2->bdev, b));
  2246. return -EBUSY;
  2247. }
  2248. if (pd2->pkt_dev == dev) {
  2249. pkt_err(pd, "can't chain pktcdvd devices\n");
  2250. return -EBUSY;
  2251. }
  2252. }
  2253. bdev = bdget(dev);
  2254. if (!bdev)
  2255. return -ENOMEM;
  2256. ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY, NULL);
  2257. if (ret)
  2258. return ret;
  2259. /* This is safe, since we have a reference from open(). */
  2260. __module_get(THIS_MODULE);
  2261. pd->bdev = bdev;
  2262. set_blocksize(bdev, CD_FRAMESIZE);
  2263. pkt_init_queue(pd);
  2264. atomic_set(&pd->cdrw.pending_bios, 0);
  2265. pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
  2266. if (IS_ERR(pd->cdrw.thread)) {
  2267. pkt_err(pd, "can't start kernel thread\n");
  2268. ret = -ENOMEM;
  2269. goto out_mem;
  2270. }
  2271. proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
  2272. pkt_dbg(1, pd, "writer mapped to %s\n", bdevname(bdev, b));
  2273. return 0;
  2274. out_mem:
  2275. blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
  2276. /* This is safe: open() is still holding a reference. */
  2277. module_put(THIS_MODULE);
  2278. return ret;
  2279. }
  2280. static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
  2281. {
  2282. struct pktcdvd_device *pd = bdev->bd_disk->private_data;
  2283. int ret;
  2284. pkt_dbg(2, pd, "cmd %x, dev %d:%d\n",
  2285. cmd, MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
  2286. mutex_lock(&pktcdvd_mutex);
  2287. switch (cmd) {
  2288. case CDROMEJECT:
  2289. /*
  2290. * The door gets locked when the device is opened, so we
  2291. * have to unlock it or else the eject command fails.
  2292. */
  2293. if (pd->refcnt == 1)
  2294. pkt_lock_door(pd, 0);
  2295. /* fallthru */
  2296. /*
  2297. * forward selected CDROM ioctls to CD-ROM, for UDF
  2298. */
  2299. case CDROMMULTISESSION:
  2300. case CDROMREADTOCENTRY:
  2301. case CDROM_LAST_WRITTEN:
  2302. case CDROM_SEND_PACKET:
  2303. case SCSI_IOCTL_SEND_COMMAND:
  2304. ret = __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
  2305. break;
  2306. default:
  2307. pkt_dbg(2, pd, "Unknown ioctl (%x)\n", cmd);
  2308. ret = -ENOTTY;
  2309. }
  2310. mutex_unlock(&pktcdvd_mutex);
  2311. return ret;
  2312. }
  2313. static unsigned int pkt_check_events(struct gendisk *disk,
  2314. unsigned int clearing)
  2315. {
  2316. struct pktcdvd_device *pd = disk->private_data;
  2317. struct gendisk *attached_disk;
  2318. if (!pd)
  2319. return 0;
  2320. if (!pd->bdev)
  2321. return 0;
  2322. attached_disk = pd->bdev->bd_disk;
  2323. if (!attached_disk || !attached_disk->fops->check_events)
  2324. return 0;
  2325. return attached_disk->fops->check_events(attached_disk, clearing);
  2326. }
  2327. static const struct block_device_operations pktcdvd_ops = {
  2328. .owner = THIS_MODULE,
  2329. .open = pkt_open,
  2330. .release = pkt_close,
  2331. .ioctl = pkt_ioctl,
  2332. .check_events = pkt_check_events,
  2333. };
  2334. static char *pktcdvd_devnode(struct gendisk *gd, umode_t *mode)
  2335. {
  2336. return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
  2337. }
  2338. /*
  2339. * Set up mapping from pktcdvd device to CD-ROM device.
  2340. */
  2341. static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
  2342. {
  2343. int idx;
  2344. int ret = -ENOMEM;
  2345. struct pktcdvd_device *pd;
  2346. struct gendisk *disk;
  2347. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2348. for (idx = 0; idx < MAX_WRITERS; idx++)
  2349. if (!pkt_devs[idx])
  2350. break;
  2351. if (idx == MAX_WRITERS) {
  2352. pr_err("max %d writers supported\n", MAX_WRITERS);
  2353. ret = -EBUSY;
  2354. goto out_mutex;
  2355. }
  2356. pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
  2357. if (!pd)
  2358. goto out_mutex;
  2359. pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
  2360. sizeof(struct pkt_rb_node));
  2361. if (!pd->rb_pool)
  2362. goto out_mem;
  2363. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  2364. INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
  2365. spin_lock_init(&pd->cdrw.active_list_lock);
  2366. spin_lock_init(&pd->lock);
  2367. spin_lock_init(&pd->iosched.lock);
  2368. bio_list_init(&pd->iosched.read_queue);
  2369. bio_list_init(&pd->iosched.write_queue);
  2370. sprintf(pd->name, DRIVER_NAME"%d", idx);
  2371. init_waitqueue_head(&pd->wqueue);
  2372. pd->bio_queue = RB_ROOT;
  2373. pd->write_congestion_on = write_congestion_on;
  2374. pd->write_congestion_off = write_congestion_off;
  2375. disk = alloc_disk(1);
  2376. if (!disk)
  2377. goto out_mem;
  2378. pd->disk = disk;
  2379. disk->major = pktdev_major;
  2380. disk->first_minor = idx;
  2381. disk->fops = &pktcdvd_ops;
  2382. disk->flags = GENHD_FL_REMOVABLE;
  2383. strcpy(disk->disk_name, pd->name);
  2384. disk->devnode = pktcdvd_devnode;
  2385. disk->private_data = pd;
  2386. disk->queue = blk_alloc_queue(GFP_KERNEL);
  2387. if (!disk->queue)
  2388. goto out_mem2;
  2389. pd->pkt_dev = MKDEV(pktdev_major, idx);
  2390. ret = pkt_new_dev(pd, dev);
  2391. if (ret)
  2392. goto out_mem2;
  2393. /* inherit events of the host device */
  2394. disk->events = pd->bdev->bd_disk->events;
  2395. disk->async_events = pd->bdev->bd_disk->async_events;
  2396. add_disk(disk);
  2397. pkt_sysfs_dev_new(pd);
  2398. pkt_debugfs_dev_new(pd);
  2399. pkt_devs[idx] = pd;
  2400. if (pkt_dev)
  2401. *pkt_dev = pd->pkt_dev;
  2402. mutex_unlock(&ctl_mutex);
  2403. return 0;
  2404. out_mem2:
  2405. put_disk(disk);
  2406. out_mem:
  2407. mempool_destroy(pd->rb_pool);
  2408. kfree(pd);
  2409. out_mutex:
  2410. mutex_unlock(&ctl_mutex);
  2411. pr_err("setup of pktcdvd device failed\n");
  2412. return ret;
  2413. }
  2414. /*
  2415. * Tear down mapping from pktcdvd device to CD-ROM device.
  2416. */
  2417. static int pkt_remove_dev(dev_t pkt_dev)
  2418. {
  2419. struct pktcdvd_device *pd;
  2420. int idx;
  2421. int ret = 0;
  2422. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2423. for (idx = 0; idx < MAX_WRITERS; idx++) {
  2424. pd = pkt_devs[idx];
  2425. if (pd && (pd->pkt_dev == pkt_dev))
  2426. break;
  2427. }
  2428. if (idx == MAX_WRITERS) {
  2429. pr_debug("dev not setup\n");
  2430. ret = -ENXIO;
  2431. goto out;
  2432. }
  2433. if (pd->refcnt > 0) {
  2434. ret = -EBUSY;
  2435. goto out;
  2436. }
  2437. if (!IS_ERR(pd->cdrw.thread))
  2438. kthread_stop(pd->cdrw.thread);
  2439. pkt_devs[idx] = NULL;
  2440. pkt_debugfs_dev_remove(pd);
  2441. pkt_sysfs_dev_remove(pd);
  2442. blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
  2443. remove_proc_entry(pd->name, pkt_proc);
  2444. pkt_dbg(1, pd, "writer unmapped\n");
  2445. del_gendisk(pd->disk);
  2446. blk_cleanup_queue(pd->disk->queue);
  2447. put_disk(pd->disk);
  2448. mempool_destroy(pd->rb_pool);
  2449. kfree(pd);
  2450. /* This is safe: open() is still holding a reference. */
  2451. module_put(THIS_MODULE);
  2452. out:
  2453. mutex_unlock(&ctl_mutex);
  2454. return ret;
  2455. }
  2456. static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
  2457. {
  2458. struct pktcdvd_device *pd;
  2459. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2460. pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
  2461. if (pd) {
  2462. ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
  2463. ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
  2464. } else {
  2465. ctrl_cmd->dev = 0;
  2466. ctrl_cmd->pkt_dev = 0;
  2467. }
  2468. ctrl_cmd->num_devices = MAX_WRITERS;
  2469. mutex_unlock(&ctl_mutex);
  2470. }
  2471. static long pkt_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2472. {
  2473. void __user *argp = (void __user *)arg;
  2474. struct pkt_ctrl_command ctrl_cmd;
  2475. int ret = 0;
  2476. dev_t pkt_dev = 0;
  2477. if (cmd != PACKET_CTRL_CMD)
  2478. return -ENOTTY;
  2479. if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
  2480. return -EFAULT;
  2481. switch (ctrl_cmd.command) {
  2482. case PKT_CTRL_CMD_SETUP:
  2483. if (!capable(CAP_SYS_ADMIN))
  2484. return -EPERM;
  2485. ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
  2486. ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
  2487. break;
  2488. case PKT_CTRL_CMD_TEARDOWN:
  2489. if (!capable(CAP_SYS_ADMIN))
  2490. return -EPERM;
  2491. ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
  2492. break;
  2493. case PKT_CTRL_CMD_STATUS:
  2494. pkt_get_status(&ctrl_cmd);
  2495. break;
  2496. default:
  2497. return -ENOTTY;
  2498. }
  2499. if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
  2500. return -EFAULT;
  2501. return ret;
  2502. }
  2503. #ifdef CONFIG_COMPAT
  2504. static long pkt_ctl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2505. {
  2506. return pkt_ctl_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2507. }
  2508. #endif
  2509. static const struct file_operations pkt_ctl_fops = {
  2510. .open = nonseekable_open,
  2511. .unlocked_ioctl = pkt_ctl_ioctl,
  2512. #ifdef CONFIG_COMPAT
  2513. .compat_ioctl = pkt_ctl_compat_ioctl,
  2514. #endif
  2515. .owner = THIS_MODULE,
  2516. .llseek = no_llseek,
  2517. };
  2518. static struct miscdevice pkt_misc = {
  2519. .minor = MISC_DYNAMIC_MINOR,
  2520. .name = DRIVER_NAME,
  2521. .nodename = "pktcdvd/control",
  2522. .fops = &pkt_ctl_fops
  2523. };
  2524. static int __init pkt_init(void)
  2525. {
  2526. int ret;
  2527. mutex_init(&ctl_mutex);
  2528. psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
  2529. sizeof(struct packet_stacked_data));
  2530. if (!psd_pool)
  2531. return -ENOMEM;
  2532. ret = register_blkdev(pktdev_major, DRIVER_NAME);
  2533. if (ret < 0) {
  2534. pr_err("unable to register block device\n");
  2535. goto out2;
  2536. }
  2537. if (!pktdev_major)
  2538. pktdev_major = ret;
  2539. ret = pkt_sysfs_init();
  2540. if (ret)
  2541. goto out;
  2542. pkt_debugfs_init();
  2543. ret = misc_register(&pkt_misc);
  2544. if (ret) {
  2545. pr_err("unable to register misc device\n");
  2546. goto out_misc;
  2547. }
  2548. pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
  2549. return 0;
  2550. out_misc:
  2551. pkt_debugfs_cleanup();
  2552. pkt_sysfs_cleanup();
  2553. out:
  2554. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2555. out2:
  2556. mempool_destroy(psd_pool);
  2557. return ret;
  2558. }
  2559. static void __exit pkt_exit(void)
  2560. {
  2561. remove_proc_entry("driver/"DRIVER_NAME, NULL);
  2562. misc_deregister(&pkt_misc);
  2563. pkt_debugfs_cleanup();
  2564. pkt_sysfs_cleanup();
  2565. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2566. mempool_destroy(psd_pool);
  2567. }
  2568. MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
  2569. MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
  2570. MODULE_LICENSE("GPL");
  2571. module_init(pkt_init);
  2572. module_exit(pkt_exit);