pktcdvd.c 76 KB

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