pktcdvd.c 74 KB

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