aoecmd.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/ata.h>
  7. #include <linux/slab.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/genhd.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/kthread.h>
  16. #include <net/net_namespace.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/uio.h>
  19. #include "aoe.h"
  20. #define MAXIOC (8192) /* default meant to avoid most soft lockups */
  21. static void ktcomplete(struct frame *, struct sk_buff *);
  22. static int count_targets(struct aoedev *d, int *untainted);
  23. static struct buf *nextbuf(struct aoedev *);
  24. static int aoe_deadsecs = 60 * 3;
  25. module_param(aoe_deadsecs, int, 0644);
  26. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  27. static int aoe_maxout = 64;
  28. module_param(aoe_maxout, int, 0644);
  29. MODULE_PARM_DESC(aoe_maxout,
  30. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  31. /* The number of online cpus during module initialization gives us a
  32. * convenient heuristic cap on the parallelism used for ktio threads
  33. * doing I/O completion. It is not important that the cap equal the
  34. * actual number of running CPUs at any given time, but because of CPU
  35. * hotplug, we take care to use ncpus instead of using
  36. * num_online_cpus() after module initialization.
  37. */
  38. static int ncpus;
  39. /* mutex lock used for synchronization while thread spawning */
  40. static DEFINE_MUTEX(ktio_spawn_lock);
  41. static wait_queue_head_t *ktiowq;
  42. static struct ktstate *kts;
  43. /* io completion queue */
  44. struct iocq_ktio {
  45. struct list_head head;
  46. spinlock_t lock;
  47. };
  48. static struct iocq_ktio *iocq;
  49. static struct page *empty_page;
  50. static struct sk_buff *
  51. new_skb(ulong len)
  52. {
  53. struct sk_buff *skb;
  54. skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC);
  55. if (skb) {
  56. skb_reserve(skb, MAX_HEADER);
  57. skb_reset_mac_header(skb);
  58. skb_reset_network_header(skb);
  59. skb->protocol = __constant_htons(ETH_P_AOE);
  60. skb_checksum_none_assert(skb);
  61. }
  62. return skb;
  63. }
  64. static struct frame *
  65. getframe_deferred(struct aoedev *d, u32 tag)
  66. {
  67. struct list_head *head, *pos, *nx;
  68. struct frame *f;
  69. head = &d->rexmitq;
  70. list_for_each_safe(pos, nx, head) {
  71. f = list_entry(pos, struct frame, head);
  72. if (f->tag == tag) {
  73. list_del(pos);
  74. return f;
  75. }
  76. }
  77. return NULL;
  78. }
  79. static struct frame *
  80. getframe(struct aoedev *d, u32 tag)
  81. {
  82. struct frame *f;
  83. struct list_head *head, *pos, *nx;
  84. u32 n;
  85. n = tag % NFACTIVE;
  86. head = &d->factive[n];
  87. list_for_each_safe(pos, nx, head) {
  88. f = list_entry(pos, struct frame, head);
  89. if (f->tag == tag) {
  90. list_del(pos);
  91. return f;
  92. }
  93. }
  94. return NULL;
  95. }
  96. /*
  97. * Leave the top bit clear so we have tagspace for userland.
  98. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  99. * This driver reserves tag -1 to mean "unused frame."
  100. */
  101. static int
  102. newtag(struct aoedev *d)
  103. {
  104. register ulong n;
  105. n = jiffies & 0xffff;
  106. return n |= (++d->lasttag & 0x7fff) << 16;
  107. }
  108. static u32
  109. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  110. {
  111. u32 host_tag = newtag(d);
  112. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  113. memcpy(h->dst, t->addr, sizeof h->dst);
  114. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  115. h->verfl = AOE_HVER;
  116. h->major = cpu_to_be16(d->aoemajor);
  117. h->minor = d->aoeminor;
  118. h->cmd = AOECMD_ATA;
  119. h->tag = cpu_to_be32(host_tag);
  120. return host_tag;
  121. }
  122. static inline void
  123. put_lba(struct aoe_atahdr *ah, sector_t lba)
  124. {
  125. ah->lba0 = lba;
  126. ah->lba1 = lba >>= 8;
  127. ah->lba2 = lba >>= 8;
  128. ah->lba3 = lba >>= 8;
  129. ah->lba4 = lba >>= 8;
  130. ah->lba5 = lba >>= 8;
  131. }
  132. static struct aoeif *
  133. ifrotate(struct aoetgt *t)
  134. {
  135. struct aoeif *ifp;
  136. ifp = t->ifp;
  137. ifp++;
  138. if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
  139. ifp = t->ifs;
  140. if (ifp->nd == NULL)
  141. return NULL;
  142. return t->ifp = ifp;
  143. }
  144. static void
  145. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  146. {
  147. __skb_queue_tail(&d->skbpool, skb);
  148. }
  149. static struct sk_buff *
  150. skb_pool_get(struct aoedev *d)
  151. {
  152. struct sk_buff *skb = skb_peek(&d->skbpool);
  153. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  154. __skb_unlink(skb, &d->skbpool);
  155. return skb;
  156. }
  157. if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
  158. (skb = new_skb(ETH_ZLEN)))
  159. return skb;
  160. return NULL;
  161. }
  162. void
  163. aoe_freetframe(struct frame *f)
  164. {
  165. struct aoetgt *t;
  166. t = f->t;
  167. f->buf = NULL;
  168. f->lba = 0;
  169. f->bv = NULL;
  170. f->r_skb = NULL;
  171. f->flags = 0;
  172. list_add(&f->head, &t->ffree);
  173. }
  174. static struct frame *
  175. newtframe(struct aoedev *d, struct aoetgt *t)
  176. {
  177. struct frame *f;
  178. struct sk_buff *skb;
  179. struct list_head *pos;
  180. if (list_empty(&t->ffree)) {
  181. if (t->falloc >= NSKBPOOLMAX*2)
  182. return NULL;
  183. f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
  184. if (f == NULL)
  185. return NULL;
  186. t->falloc++;
  187. f->t = t;
  188. } else {
  189. pos = t->ffree.next;
  190. list_del(pos);
  191. f = list_entry(pos, struct frame, head);
  192. }
  193. skb = f->skb;
  194. if (skb == NULL) {
  195. f->skb = skb = new_skb(ETH_ZLEN);
  196. if (!skb) {
  197. bail: aoe_freetframe(f);
  198. return NULL;
  199. }
  200. }
  201. if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
  202. skb = skb_pool_get(d);
  203. if (skb == NULL)
  204. goto bail;
  205. skb_pool_put(d, f->skb);
  206. f->skb = skb;
  207. }
  208. skb->truesize -= skb->data_len;
  209. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  210. skb_trim(skb, 0);
  211. return f;
  212. }
  213. static struct frame *
  214. newframe(struct aoedev *d)
  215. {
  216. struct frame *f;
  217. struct aoetgt *t, **tt;
  218. int totout = 0;
  219. int use_tainted;
  220. int has_untainted;
  221. if (!d->targets || !d->targets[0]) {
  222. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  223. return NULL;
  224. }
  225. tt = d->tgt; /* last used target */
  226. for (use_tainted = 0, has_untainted = 0;;) {
  227. tt++;
  228. if (tt >= &d->targets[d->ntargets] || !*tt)
  229. tt = d->targets;
  230. t = *tt;
  231. if (!t->taint) {
  232. has_untainted = 1;
  233. totout += t->nout;
  234. }
  235. if (t->nout < t->maxout
  236. && (use_tainted || !t->taint)
  237. && t->ifp->nd) {
  238. f = newtframe(d, t);
  239. if (f) {
  240. ifrotate(t);
  241. d->tgt = tt;
  242. return f;
  243. }
  244. }
  245. if (tt == d->tgt) { /* we've looped and found nada */
  246. if (!use_tainted && !has_untainted)
  247. use_tainted = 1;
  248. else
  249. break;
  250. }
  251. }
  252. if (totout == 0) {
  253. d->kicked++;
  254. d->flags |= DEVFL_KICKME;
  255. }
  256. return NULL;
  257. }
  258. static void
  259. skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
  260. {
  261. int frag = 0;
  262. ulong fcnt;
  263. loop:
  264. fcnt = bv->bv_len - (off - bv->bv_offset);
  265. if (fcnt > cnt)
  266. fcnt = cnt;
  267. skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
  268. cnt -= fcnt;
  269. if (cnt <= 0)
  270. return;
  271. bv++;
  272. off = bv->bv_offset;
  273. goto loop;
  274. }
  275. static void
  276. fhash(struct frame *f)
  277. {
  278. struct aoedev *d = f->t->d;
  279. u32 n;
  280. n = f->tag % NFACTIVE;
  281. list_add_tail(&f->head, &d->factive[n]);
  282. }
  283. static void
  284. ata_rw_frameinit(struct frame *f)
  285. {
  286. struct aoetgt *t;
  287. struct aoe_hdr *h;
  288. struct aoe_atahdr *ah;
  289. struct sk_buff *skb;
  290. char writebit, extbit;
  291. skb = f->skb;
  292. h = (struct aoe_hdr *) skb_mac_header(skb);
  293. ah = (struct aoe_atahdr *) (h + 1);
  294. skb_put(skb, sizeof(*h) + sizeof(*ah));
  295. memset(h, 0, skb->len);
  296. writebit = 0x10;
  297. extbit = 0x4;
  298. t = f->t;
  299. f->tag = aoehdr_atainit(t->d, t, h);
  300. fhash(f);
  301. t->nout++;
  302. f->waited = 0;
  303. f->waited_total = 0;
  304. if (f->buf)
  305. f->lba = f->buf->sector;
  306. /* set up ata header */
  307. ah->scnt = f->bcnt >> 9;
  308. put_lba(ah, f->lba);
  309. if (t->d->flags & DEVFL_EXT) {
  310. ah->aflags |= AOEAFL_EXT;
  311. } else {
  312. extbit = 0;
  313. ah->lba3 &= 0x0f;
  314. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  315. }
  316. if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
  317. skb_fillup(skb, f->bv, f->bv_off, f->bcnt);
  318. ah->aflags |= AOEAFL_WRITE;
  319. skb->len += f->bcnt;
  320. skb->data_len = f->bcnt;
  321. skb->truesize += f->bcnt;
  322. t->wpkts++;
  323. } else {
  324. t->rpkts++;
  325. writebit = 0;
  326. }
  327. ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
  328. skb->dev = t->ifp->nd;
  329. }
  330. static int
  331. aoecmd_ata_rw(struct aoedev *d)
  332. {
  333. struct frame *f;
  334. struct buf *buf;
  335. struct sk_buff *skb;
  336. struct sk_buff_head queue;
  337. ulong bcnt, fbcnt;
  338. buf = nextbuf(d);
  339. if (buf == NULL)
  340. return 0;
  341. f = newframe(d);
  342. if (f == NULL)
  343. return 0;
  344. bcnt = d->maxbcnt;
  345. if (bcnt == 0)
  346. bcnt = DEFAULTBCNT;
  347. if (bcnt > buf->resid)
  348. bcnt = buf->resid;
  349. fbcnt = bcnt;
  350. f->bv = buf->bv;
  351. f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
  352. do {
  353. if (fbcnt < buf->bv_resid) {
  354. buf->bv_resid -= fbcnt;
  355. buf->resid -= fbcnt;
  356. break;
  357. }
  358. fbcnt -= buf->bv_resid;
  359. buf->resid -= buf->bv_resid;
  360. if (buf->resid == 0) {
  361. d->ip.buf = NULL;
  362. break;
  363. }
  364. buf->bv++;
  365. buf->bv_resid = buf->bv->bv_len;
  366. WARN_ON(buf->bv_resid == 0);
  367. } while (fbcnt);
  368. /* initialize the headers & frame */
  369. f->buf = buf;
  370. f->bcnt = bcnt;
  371. ata_rw_frameinit(f);
  372. /* mark all tracking fields and load out */
  373. buf->nframesout += 1;
  374. buf->sector += bcnt >> 9;
  375. skb = skb_clone(f->skb, GFP_ATOMIC);
  376. if (skb) {
  377. do_gettimeofday(&f->sent);
  378. f->sent_jiffs = (u32) jiffies;
  379. __skb_queue_head_init(&queue);
  380. __skb_queue_tail(&queue, skb);
  381. aoenet_xmit(&queue);
  382. }
  383. return 1;
  384. }
  385. /* some callers cannot sleep, and they can call this function,
  386. * transmitting the packets later, when interrupts are on
  387. */
  388. static void
  389. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
  390. {
  391. struct aoe_hdr *h;
  392. struct aoe_cfghdr *ch;
  393. struct sk_buff *skb;
  394. struct net_device *ifp;
  395. rcu_read_lock();
  396. for_each_netdev_rcu(&init_net, ifp) {
  397. dev_hold(ifp);
  398. if (!is_aoe_netif(ifp))
  399. goto cont;
  400. skb = new_skb(sizeof *h + sizeof *ch);
  401. if (skb == NULL) {
  402. printk(KERN_INFO "aoe: skb alloc failure\n");
  403. goto cont;
  404. }
  405. skb_put(skb, sizeof *h + sizeof *ch);
  406. skb->dev = ifp;
  407. __skb_queue_tail(queue, skb);
  408. h = (struct aoe_hdr *) skb_mac_header(skb);
  409. memset(h, 0, sizeof *h + sizeof *ch);
  410. memset(h->dst, 0xff, sizeof h->dst);
  411. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  412. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  413. h->verfl = AOE_HVER;
  414. h->major = cpu_to_be16(aoemajor);
  415. h->minor = aoeminor;
  416. h->cmd = AOECMD_CFG;
  417. cont:
  418. dev_put(ifp);
  419. }
  420. rcu_read_unlock();
  421. }
  422. static void
  423. resend(struct aoedev *d, struct frame *f)
  424. {
  425. struct sk_buff *skb;
  426. struct sk_buff_head queue;
  427. struct aoe_hdr *h;
  428. struct aoetgt *t;
  429. char buf[128];
  430. u32 n;
  431. t = f->t;
  432. n = newtag(d);
  433. skb = f->skb;
  434. if (ifrotate(t) == NULL) {
  435. /* probably can't happen, but set it up to fail anyway */
  436. pr_info("aoe: resend: no interfaces to rotate to.\n");
  437. ktcomplete(f, NULL);
  438. return;
  439. }
  440. h = (struct aoe_hdr *) skb_mac_header(skb);
  441. if (!(f->flags & FFL_PROBE)) {
  442. snprintf(buf, sizeof(buf),
  443. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
  444. "retransmit", d->aoemajor, d->aoeminor,
  445. f->tag, jiffies, n,
  446. h->src, h->dst, t->nout);
  447. aoechr_error(buf);
  448. }
  449. f->tag = n;
  450. fhash(f);
  451. h->tag = cpu_to_be32(n);
  452. memcpy(h->dst, t->addr, sizeof h->dst);
  453. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  454. skb->dev = t->ifp->nd;
  455. skb = skb_clone(skb, GFP_ATOMIC);
  456. if (skb == NULL)
  457. return;
  458. do_gettimeofday(&f->sent);
  459. f->sent_jiffs = (u32) jiffies;
  460. __skb_queue_head_init(&queue);
  461. __skb_queue_tail(&queue, skb);
  462. aoenet_xmit(&queue);
  463. }
  464. static int
  465. tsince_hr(struct frame *f)
  466. {
  467. struct timeval now;
  468. int n;
  469. do_gettimeofday(&now);
  470. n = now.tv_usec - f->sent.tv_usec;
  471. n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
  472. if (n < 0)
  473. n = -n;
  474. /* For relatively long periods, use jiffies to avoid
  475. * discrepancies caused by updates to the system time.
  476. *
  477. * On system with HZ of 1000, 32-bits is over 49 days
  478. * worth of jiffies, or over 71 minutes worth of usecs.
  479. *
  480. * Jiffies overflow is handled by subtraction of unsigned ints:
  481. * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
  482. * $3 = 4
  483. * (gdb)
  484. */
  485. if (n > USEC_PER_SEC / 4) {
  486. n = ((u32) jiffies) - f->sent_jiffs;
  487. n *= USEC_PER_SEC / HZ;
  488. }
  489. return n;
  490. }
  491. static int
  492. tsince(u32 tag)
  493. {
  494. int n;
  495. n = jiffies & 0xffff;
  496. n -= tag & 0xffff;
  497. if (n < 0)
  498. n += 1<<16;
  499. return jiffies_to_usecs(n + 1);
  500. }
  501. static struct aoeif *
  502. getif(struct aoetgt *t, struct net_device *nd)
  503. {
  504. struct aoeif *p, *e;
  505. p = t->ifs;
  506. e = p + NAOEIFS;
  507. for (; p < e; p++)
  508. if (p->nd == nd)
  509. return p;
  510. return NULL;
  511. }
  512. static void
  513. ejectif(struct aoetgt *t, struct aoeif *ifp)
  514. {
  515. struct aoeif *e;
  516. struct net_device *nd;
  517. ulong n;
  518. nd = ifp->nd;
  519. e = t->ifs + NAOEIFS - 1;
  520. n = (e - ifp) * sizeof *ifp;
  521. memmove(ifp, ifp+1, n);
  522. e->nd = NULL;
  523. dev_put(nd);
  524. }
  525. static struct frame *
  526. reassign_frame(struct frame *f)
  527. {
  528. struct frame *nf;
  529. struct sk_buff *skb;
  530. nf = newframe(f->t->d);
  531. if (!nf)
  532. return NULL;
  533. if (nf->t == f->t) {
  534. aoe_freetframe(nf);
  535. return NULL;
  536. }
  537. skb = nf->skb;
  538. nf->skb = f->skb;
  539. nf->buf = f->buf;
  540. nf->bcnt = f->bcnt;
  541. nf->lba = f->lba;
  542. nf->bv = f->bv;
  543. nf->bv_off = f->bv_off;
  544. nf->waited = 0;
  545. nf->waited_total = f->waited_total;
  546. nf->sent = f->sent;
  547. nf->sent_jiffs = f->sent_jiffs;
  548. f->skb = skb;
  549. return nf;
  550. }
  551. static void
  552. probe(struct aoetgt *t)
  553. {
  554. struct aoedev *d;
  555. struct frame *f;
  556. struct sk_buff *skb;
  557. struct sk_buff_head queue;
  558. size_t n, m;
  559. int frag;
  560. d = t->d;
  561. f = newtframe(d, t);
  562. if (!f) {
  563. pr_err("%s %pm for e%ld.%d: %s\n",
  564. "aoe: cannot probe remote address",
  565. t->addr,
  566. (long) d->aoemajor, d->aoeminor,
  567. "no frame available");
  568. return;
  569. }
  570. f->flags |= FFL_PROBE;
  571. ifrotate(t);
  572. f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
  573. ata_rw_frameinit(f);
  574. skb = f->skb;
  575. for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
  576. if (n < PAGE_SIZE)
  577. m = n;
  578. else
  579. m = PAGE_SIZE;
  580. skb_fill_page_desc(skb, frag, empty_page, 0, m);
  581. }
  582. skb->len += f->bcnt;
  583. skb->data_len = f->bcnt;
  584. skb->truesize += f->bcnt;
  585. skb = skb_clone(f->skb, GFP_ATOMIC);
  586. if (skb) {
  587. do_gettimeofday(&f->sent);
  588. f->sent_jiffs = (u32) jiffies;
  589. __skb_queue_head_init(&queue);
  590. __skb_queue_tail(&queue, skb);
  591. aoenet_xmit(&queue);
  592. }
  593. }
  594. static long
  595. rto(struct aoedev *d)
  596. {
  597. long t;
  598. t = 2 * d->rttavg >> RTTSCALE;
  599. t += 8 * d->rttdev >> RTTDSCALE;
  600. if (t == 0)
  601. t = 1;
  602. return t;
  603. }
  604. static void
  605. rexmit_deferred(struct aoedev *d)
  606. {
  607. struct aoetgt *t;
  608. struct frame *f;
  609. struct frame *nf;
  610. struct list_head *pos, *nx, *head;
  611. int since;
  612. int untainted;
  613. count_targets(d, &untainted);
  614. head = &d->rexmitq;
  615. list_for_each_safe(pos, nx, head) {
  616. f = list_entry(pos, struct frame, head);
  617. t = f->t;
  618. if (t->taint) {
  619. if (!(f->flags & FFL_PROBE)) {
  620. nf = reassign_frame(f);
  621. if (nf) {
  622. if (t->nout_probes == 0
  623. && untainted > 0) {
  624. probe(t);
  625. t->nout_probes++;
  626. }
  627. list_replace(&f->head, &nf->head);
  628. pos = &nf->head;
  629. aoe_freetframe(f);
  630. f = nf;
  631. t = f->t;
  632. }
  633. } else if (untainted < 1) {
  634. /* don't probe w/o other untainted aoetgts */
  635. goto stop_probe;
  636. } else if (tsince_hr(f) < t->taint * rto(d)) {
  637. /* reprobe slowly when taint is high */
  638. continue;
  639. }
  640. } else if (f->flags & FFL_PROBE) {
  641. stop_probe: /* don't probe untainted aoetgts */
  642. list_del(pos);
  643. aoe_freetframe(f);
  644. /* leaving d->kicked, because this is routine */
  645. f->t->d->flags |= DEVFL_KICKME;
  646. continue;
  647. }
  648. if (t->nout >= t->maxout)
  649. continue;
  650. list_del(pos);
  651. t->nout++;
  652. if (f->flags & FFL_PROBE)
  653. t->nout_probes++;
  654. since = tsince_hr(f);
  655. f->waited += since;
  656. f->waited_total += since;
  657. resend(d, f);
  658. }
  659. }
  660. /* An aoetgt accumulates demerits quickly, and successful
  661. * probing redeems the aoetgt slowly.
  662. */
  663. static void
  664. scorn(struct aoetgt *t)
  665. {
  666. int n;
  667. n = t->taint++;
  668. t->taint += t->taint * 2;
  669. if (n > t->taint)
  670. t->taint = n;
  671. if (t->taint > MAX_TAINT)
  672. t->taint = MAX_TAINT;
  673. }
  674. static int
  675. count_targets(struct aoedev *d, int *untainted)
  676. {
  677. int i, good;
  678. for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
  679. if (d->targets[i]->taint == 0)
  680. good++;
  681. if (untainted)
  682. *untainted = good;
  683. return i;
  684. }
  685. static void
  686. rexmit_timer(ulong vp)
  687. {
  688. struct aoedev *d;
  689. struct aoetgt *t;
  690. struct aoeif *ifp;
  691. struct frame *f;
  692. struct list_head *head, *pos, *nx;
  693. LIST_HEAD(flist);
  694. register long timeout;
  695. ulong flags, n;
  696. int i;
  697. int utgts; /* number of aoetgt descriptors (not slots) */
  698. int since;
  699. d = (struct aoedev *) vp;
  700. spin_lock_irqsave(&d->lock, flags);
  701. /* timeout based on observed timings and variations */
  702. timeout = rto(d);
  703. utgts = count_targets(d, NULL);
  704. if (d->flags & DEVFL_TKILL) {
  705. spin_unlock_irqrestore(&d->lock, flags);
  706. return;
  707. }
  708. /* collect all frames to rexmit into flist */
  709. for (i = 0; i < NFACTIVE; i++) {
  710. head = &d->factive[i];
  711. list_for_each_safe(pos, nx, head) {
  712. f = list_entry(pos, struct frame, head);
  713. if (tsince_hr(f) < timeout)
  714. break; /* end of expired frames */
  715. /* move to flist for later processing */
  716. list_move_tail(pos, &flist);
  717. }
  718. }
  719. /* process expired frames */
  720. while (!list_empty(&flist)) {
  721. pos = flist.next;
  722. f = list_entry(pos, struct frame, head);
  723. since = tsince_hr(f);
  724. n = f->waited_total + since;
  725. n /= USEC_PER_SEC;
  726. if (aoe_deadsecs
  727. && n > aoe_deadsecs
  728. && !(f->flags & FFL_PROBE)) {
  729. /* Waited too long. Device failure.
  730. * Hang all frames on first hash bucket for downdev
  731. * to clean up.
  732. */
  733. list_splice(&flist, &d->factive[0]);
  734. aoedev_downdev(d);
  735. goto out;
  736. }
  737. t = f->t;
  738. n = f->waited + since;
  739. n /= USEC_PER_SEC;
  740. if (aoe_deadsecs && utgts > 0
  741. && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
  742. scorn(t); /* avoid this target */
  743. if (t->maxout != 1) {
  744. t->ssthresh = t->maxout / 2;
  745. t->maxout = 1;
  746. }
  747. if (f->flags & FFL_PROBE) {
  748. t->nout_probes--;
  749. } else {
  750. ifp = getif(t, f->skb->dev);
  751. if (ifp && ++ifp->lost > (t->nframes << 1)
  752. && (ifp != t->ifs || t->ifs[1].nd)) {
  753. ejectif(t, ifp);
  754. ifp = NULL;
  755. }
  756. }
  757. list_move_tail(pos, &d->rexmitq);
  758. t->nout--;
  759. }
  760. rexmit_deferred(d);
  761. out:
  762. if ((d->flags & DEVFL_KICKME) && d->blkq) {
  763. d->flags &= ~DEVFL_KICKME;
  764. d->blkq->request_fn(d->blkq);
  765. }
  766. d->timer.expires = jiffies + TIMERTICK;
  767. add_timer(&d->timer);
  768. spin_unlock_irqrestore(&d->lock, flags);
  769. }
  770. static unsigned long
  771. rqbiocnt(struct request *r)
  772. {
  773. struct bio *bio;
  774. unsigned long n = 0;
  775. __rq_for_each_bio(bio, r)
  776. n++;
  777. return n;
  778. }
  779. /* This can be removed if we are certain that no users of the block
  780. * layer will ever use zero-count pages in bios. Otherwise we have to
  781. * protect against the put_page sometimes done by the network layer.
  782. *
  783. * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
  784. * discussion.
  785. *
  786. * We cannot use get_page in the workaround, because it insists on a
  787. * positive page count as a precondition. So we use _count directly.
  788. */
  789. static void
  790. bio_pageinc(struct bio *bio)
  791. {
  792. struct bio_vec *bv;
  793. struct page *page;
  794. int i;
  795. bio_for_each_segment(bv, bio, i) {
  796. /* Non-zero page count for non-head members of
  797. * compound pages is no longer allowed by the kernel.
  798. */
  799. page = compound_trans_head(bv->bv_page);
  800. atomic_inc(&page->_count);
  801. }
  802. }
  803. static void
  804. bio_pagedec(struct bio *bio)
  805. {
  806. struct bio_vec *bv;
  807. struct page *page;
  808. int i;
  809. bio_for_each_segment(bv, bio, i) {
  810. page = compound_trans_head(bv->bv_page);
  811. atomic_dec(&page->_count);
  812. }
  813. }
  814. static void
  815. bufinit(struct buf *buf, struct request *rq, struct bio *bio)
  816. {
  817. memset(buf, 0, sizeof(*buf));
  818. buf->rq = rq;
  819. buf->bio = bio;
  820. buf->resid = bio->bi_size;
  821. buf->sector = bio->bi_sector;
  822. bio_pageinc(bio);
  823. buf->bv = bio_iovec(bio);
  824. buf->bv_resid = buf->bv->bv_len;
  825. WARN_ON(buf->bv_resid == 0);
  826. }
  827. static struct buf *
  828. nextbuf(struct aoedev *d)
  829. {
  830. struct request *rq;
  831. struct request_queue *q;
  832. struct buf *buf;
  833. struct bio *bio;
  834. q = d->blkq;
  835. if (q == NULL)
  836. return NULL; /* initializing */
  837. if (d->ip.buf)
  838. return d->ip.buf;
  839. rq = d->ip.rq;
  840. if (rq == NULL) {
  841. rq = blk_peek_request(q);
  842. if (rq == NULL)
  843. return NULL;
  844. blk_start_request(rq);
  845. d->ip.rq = rq;
  846. d->ip.nxbio = rq->bio;
  847. rq->special = (void *) rqbiocnt(rq);
  848. }
  849. buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
  850. if (buf == NULL) {
  851. pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
  852. return NULL;
  853. }
  854. bio = d->ip.nxbio;
  855. bufinit(buf, rq, bio);
  856. bio = bio->bi_next;
  857. d->ip.nxbio = bio;
  858. if (bio == NULL)
  859. d->ip.rq = NULL;
  860. return d->ip.buf = buf;
  861. }
  862. /* enters with d->lock held */
  863. void
  864. aoecmd_work(struct aoedev *d)
  865. {
  866. rexmit_deferred(d);
  867. while (aoecmd_ata_rw(d))
  868. ;
  869. }
  870. /* this function performs work that has been deferred until sleeping is OK
  871. */
  872. void
  873. aoecmd_sleepwork(struct work_struct *work)
  874. {
  875. struct aoedev *d = container_of(work, struct aoedev, work);
  876. struct block_device *bd;
  877. u64 ssize;
  878. if (d->flags & DEVFL_GDALLOC)
  879. aoeblk_gdalloc(d);
  880. if (d->flags & DEVFL_NEWSIZE) {
  881. ssize = get_capacity(d->gd);
  882. bd = bdget_disk(d->gd, 0);
  883. if (bd) {
  884. mutex_lock(&bd->bd_inode->i_mutex);
  885. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  886. mutex_unlock(&bd->bd_inode->i_mutex);
  887. bdput(bd);
  888. }
  889. spin_lock_irq(&d->lock);
  890. d->flags |= DEVFL_UP;
  891. d->flags &= ~DEVFL_NEWSIZE;
  892. spin_unlock_irq(&d->lock);
  893. }
  894. }
  895. static void
  896. ata_ident_fixstring(u16 *id, int ns)
  897. {
  898. u16 s;
  899. while (ns-- > 0) {
  900. s = *id;
  901. *id++ = s >> 8 | s << 8;
  902. }
  903. }
  904. static void
  905. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  906. {
  907. u64 ssize;
  908. u16 n;
  909. /* word 83: command set supported */
  910. n = get_unaligned_le16(&id[83 << 1]);
  911. /* word 86: command set/feature enabled */
  912. n |= get_unaligned_le16(&id[86 << 1]);
  913. if (n & (1<<10)) { /* bit 10: LBA 48 */
  914. d->flags |= DEVFL_EXT;
  915. /* word 100: number lba48 sectors */
  916. ssize = get_unaligned_le64(&id[100 << 1]);
  917. /* set as in ide-disk.c:init_idedisk_capacity */
  918. d->geo.cylinders = ssize;
  919. d->geo.cylinders /= (255 * 63);
  920. d->geo.heads = 255;
  921. d->geo.sectors = 63;
  922. } else {
  923. d->flags &= ~DEVFL_EXT;
  924. /* number lba28 sectors */
  925. ssize = get_unaligned_le32(&id[60 << 1]);
  926. /* NOTE: obsolete in ATA 6 */
  927. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  928. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  929. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  930. }
  931. ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
  932. ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
  933. ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
  934. memcpy(d->ident, id, sizeof(d->ident));
  935. if (d->ssize != ssize)
  936. printk(KERN_INFO
  937. "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
  938. t->addr,
  939. d->aoemajor, d->aoeminor,
  940. d->fw_ver, (long long)ssize);
  941. d->ssize = ssize;
  942. d->geo.start = 0;
  943. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  944. return;
  945. if (d->gd != NULL) {
  946. set_capacity(d->gd, ssize);
  947. d->flags |= DEVFL_NEWSIZE;
  948. } else
  949. d->flags |= DEVFL_GDALLOC;
  950. schedule_work(&d->work);
  951. }
  952. static void
  953. calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
  954. {
  955. register long n;
  956. n = rtt;
  957. /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
  958. n -= d->rttavg >> RTTSCALE;
  959. d->rttavg += n;
  960. if (n < 0)
  961. n = -n;
  962. n -= d->rttdev >> RTTDSCALE;
  963. d->rttdev += n;
  964. if (!t || t->maxout >= t->nframes)
  965. return;
  966. if (t->maxout < t->ssthresh)
  967. t->maxout += 1;
  968. else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
  969. t->maxout += 1;
  970. t->next_cwnd = t->maxout;
  971. }
  972. }
  973. static struct aoetgt *
  974. gettgt(struct aoedev *d, char *addr)
  975. {
  976. struct aoetgt **t, **e;
  977. t = d->targets;
  978. e = t + d->ntargets;
  979. for (; t < e && *t; t++)
  980. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  981. return *t;
  982. return NULL;
  983. }
  984. static void
  985. bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
  986. {
  987. ulong fcnt;
  988. char *p;
  989. int soff = 0;
  990. loop:
  991. fcnt = bv->bv_len - (off - bv->bv_offset);
  992. if (fcnt > cnt)
  993. fcnt = cnt;
  994. p = page_address(bv->bv_page) + off;
  995. skb_copy_bits(skb, soff, p, fcnt);
  996. soff += fcnt;
  997. cnt -= fcnt;
  998. if (cnt <= 0)
  999. return;
  1000. bv++;
  1001. off = bv->bv_offset;
  1002. goto loop;
  1003. }
  1004. void
  1005. aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
  1006. {
  1007. struct bio *bio;
  1008. int bok;
  1009. struct request_queue *q;
  1010. q = d->blkq;
  1011. if (rq == d->ip.rq)
  1012. d->ip.rq = NULL;
  1013. do {
  1014. bio = rq->bio;
  1015. bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
  1016. } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
  1017. /* cf. http://lkml.org/lkml/2006/10/31/28 */
  1018. if (!fastfail)
  1019. __blk_run_queue(q);
  1020. }
  1021. static void
  1022. aoe_end_buf(struct aoedev *d, struct buf *buf)
  1023. {
  1024. struct request *rq;
  1025. unsigned long n;
  1026. if (buf == d->ip.buf)
  1027. d->ip.buf = NULL;
  1028. rq = buf->rq;
  1029. bio_pagedec(buf->bio);
  1030. mempool_free(buf, d->bufpool);
  1031. n = (unsigned long) rq->special;
  1032. rq->special = (void *) --n;
  1033. if (n == 0)
  1034. aoe_end_request(d, rq, 0);
  1035. }
  1036. static void
  1037. ktiocomplete(struct frame *f)
  1038. {
  1039. struct aoe_hdr *hin, *hout;
  1040. struct aoe_atahdr *ahin, *ahout;
  1041. struct buf *buf;
  1042. struct sk_buff *skb;
  1043. struct aoetgt *t;
  1044. struct aoeif *ifp;
  1045. struct aoedev *d;
  1046. long n;
  1047. int untainted;
  1048. if (f == NULL)
  1049. return;
  1050. t = f->t;
  1051. d = t->d;
  1052. skb = f->r_skb;
  1053. buf = f->buf;
  1054. if (f->flags & FFL_PROBE)
  1055. goto out;
  1056. if (!skb) /* just fail the buf. */
  1057. goto noskb;
  1058. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  1059. ahout = (struct aoe_atahdr *) (hout+1);
  1060. hin = (struct aoe_hdr *) skb->data;
  1061. skb_pull(skb, sizeof(*hin));
  1062. ahin = (struct aoe_atahdr *) skb->data;
  1063. skb_pull(skb, sizeof(*ahin));
  1064. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  1065. pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  1066. ahout->cmdstat, ahin->cmdstat,
  1067. d->aoemajor, d->aoeminor);
  1068. noskb: if (buf)
  1069. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1070. goto out;
  1071. }
  1072. n = ahout->scnt << 9;
  1073. switch (ahout->cmdstat) {
  1074. case ATA_CMD_PIO_READ:
  1075. case ATA_CMD_PIO_READ_EXT:
  1076. if (skb->len < n) {
  1077. pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
  1078. "aoe: runt data size in read from",
  1079. (long) d->aoemajor, d->aoeminor,
  1080. skb->len, n);
  1081. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1082. break;
  1083. }
  1084. bvcpy(f->bv, f->bv_off, skb, n);
  1085. case ATA_CMD_PIO_WRITE:
  1086. case ATA_CMD_PIO_WRITE_EXT:
  1087. spin_lock_irq(&d->lock);
  1088. ifp = getif(t, skb->dev);
  1089. if (ifp)
  1090. ifp->lost = 0;
  1091. spin_unlock_irq(&d->lock);
  1092. break;
  1093. case ATA_CMD_ID_ATA:
  1094. if (skb->len < 512) {
  1095. pr_info("%s e%ld.%d. skb->len=%d need=512\n",
  1096. "aoe: runt data size in ataid from",
  1097. (long) d->aoemajor, d->aoeminor,
  1098. skb->len);
  1099. break;
  1100. }
  1101. if (skb_linearize(skb))
  1102. break;
  1103. spin_lock_irq(&d->lock);
  1104. ataid_complete(d, t, skb->data);
  1105. spin_unlock_irq(&d->lock);
  1106. break;
  1107. default:
  1108. pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  1109. ahout->cmdstat,
  1110. be16_to_cpu(get_unaligned(&hin->major)),
  1111. hin->minor);
  1112. }
  1113. out:
  1114. spin_lock_irq(&d->lock);
  1115. if (t->taint > 0
  1116. && --t->taint > 0
  1117. && t->nout_probes == 0) {
  1118. count_targets(d, &untainted);
  1119. if (untainted > 0) {
  1120. probe(t);
  1121. t->nout_probes++;
  1122. }
  1123. }
  1124. aoe_freetframe(f);
  1125. if (buf && --buf->nframesout == 0 && buf->resid == 0)
  1126. aoe_end_buf(d, buf);
  1127. spin_unlock_irq(&d->lock);
  1128. aoedev_put(d);
  1129. dev_kfree_skb(skb);
  1130. }
  1131. /* Enters with iocq.lock held.
  1132. * Returns true iff responses needing processing remain.
  1133. */
  1134. static int
  1135. ktio(int id)
  1136. {
  1137. struct frame *f;
  1138. struct list_head *pos;
  1139. int i;
  1140. int actual_id;
  1141. for (i = 0; ; ++i) {
  1142. if (i == MAXIOC)
  1143. return 1;
  1144. if (list_empty(&iocq[id].head))
  1145. return 0;
  1146. pos = iocq[id].head.next;
  1147. list_del(pos);
  1148. f = list_entry(pos, struct frame, head);
  1149. spin_unlock_irq(&iocq[id].lock);
  1150. ktiocomplete(f);
  1151. /* Figure out if extra threads are required. */
  1152. actual_id = f->t->d->aoeminor % ncpus;
  1153. if (!kts[actual_id].active) {
  1154. BUG_ON(id != 0);
  1155. mutex_lock(&ktio_spawn_lock);
  1156. if (!kts[actual_id].active
  1157. && aoe_ktstart(&kts[actual_id]) == 0)
  1158. kts[actual_id].active = 1;
  1159. mutex_unlock(&ktio_spawn_lock);
  1160. }
  1161. spin_lock_irq(&iocq[id].lock);
  1162. }
  1163. }
  1164. static int
  1165. kthread(void *vp)
  1166. {
  1167. struct ktstate *k;
  1168. DECLARE_WAITQUEUE(wait, current);
  1169. int more;
  1170. k = vp;
  1171. current->flags |= PF_NOFREEZE;
  1172. set_user_nice(current, -10);
  1173. complete(&k->rendez); /* tell spawner we're running */
  1174. do {
  1175. spin_lock_irq(k->lock);
  1176. more = k->fn(k->id);
  1177. if (!more) {
  1178. add_wait_queue(k->waitq, &wait);
  1179. __set_current_state(TASK_INTERRUPTIBLE);
  1180. }
  1181. spin_unlock_irq(k->lock);
  1182. if (!more) {
  1183. schedule();
  1184. remove_wait_queue(k->waitq, &wait);
  1185. } else
  1186. cond_resched();
  1187. } while (!kthread_should_stop());
  1188. complete(&k->rendez); /* tell spawner we're stopping */
  1189. return 0;
  1190. }
  1191. void
  1192. aoe_ktstop(struct ktstate *k)
  1193. {
  1194. kthread_stop(k->task);
  1195. wait_for_completion(&k->rendez);
  1196. }
  1197. int
  1198. aoe_ktstart(struct ktstate *k)
  1199. {
  1200. struct task_struct *task;
  1201. init_completion(&k->rendez);
  1202. task = kthread_run(kthread, k, "%s", k->name);
  1203. if (task == NULL || IS_ERR(task))
  1204. return -ENOMEM;
  1205. k->task = task;
  1206. wait_for_completion(&k->rendez); /* allow kthread to start */
  1207. init_completion(&k->rendez); /* for waiting for exit later */
  1208. return 0;
  1209. }
  1210. /* pass it off to kthreads for processing */
  1211. static void
  1212. ktcomplete(struct frame *f, struct sk_buff *skb)
  1213. {
  1214. int id;
  1215. ulong flags;
  1216. f->r_skb = skb;
  1217. id = f->t->d->aoeminor % ncpus;
  1218. spin_lock_irqsave(&iocq[id].lock, flags);
  1219. if (!kts[id].active) {
  1220. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1221. /* The thread with id has not been spawned yet,
  1222. * so delegate the work to the main thread and
  1223. * try spawning a new thread.
  1224. */
  1225. id = 0;
  1226. spin_lock_irqsave(&iocq[id].lock, flags);
  1227. }
  1228. list_add_tail(&f->head, &iocq[id].head);
  1229. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1230. wake_up(&ktiowq[id]);
  1231. }
  1232. struct sk_buff *
  1233. aoecmd_ata_rsp(struct sk_buff *skb)
  1234. {
  1235. struct aoedev *d;
  1236. struct aoe_hdr *h;
  1237. struct frame *f;
  1238. u32 n;
  1239. ulong flags;
  1240. char ebuf[128];
  1241. u16 aoemajor;
  1242. h = (struct aoe_hdr *) skb->data;
  1243. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  1244. d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
  1245. if (d == NULL) {
  1246. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  1247. "for unknown device %d.%d\n",
  1248. aoemajor, h->minor);
  1249. aoechr_error(ebuf);
  1250. return skb;
  1251. }
  1252. spin_lock_irqsave(&d->lock, flags);
  1253. n = be32_to_cpu(get_unaligned(&h->tag));
  1254. f = getframe(d, n);
  1255. if (f) {
  1256. calc_rttavg(d, f->t, tsince_hr(f));
  1257. f->t->nout--;
  1258. if (f->flags & FFL_PROBE)
  1259. f->t->nout_probes--;
  1260. } else {
  1261. f = getframe_deferred(d, n);
  1262. if (f) {
  1263. calc_rttavg(d, NULL, tsince_hr(f));
  1264. } else {
  1265. calc_rttavg(d, NULL, tsince(n));
  1266. spin_unlock_irqrestore(&d->lock, flags);
  1267. aoedev_put(d);
  1268. snprintf(ebuf, sizeof(ebuf),
  1269. "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
  1270. "unexpected rsp",
  1271. get_unaligned_be16(&h->major),
  1272. h->minor,
  1273. get_unaligned_be32(&h->tag),
  1274. jiffies,
  1275. h->src,
  1276. h->dst);
  1277. aoechr_error(ebuf);
  1278. return skb;
  1279. }
  1280. }
  1281. aoecmd_work(d);
  1282. spin_unlock_irqrestore(&d->lock, flags);
  1283. ktcomplete(f, skb);
  1284. /*
  1285. * Note here that we do not perform an aoedev_put, as we are
  1286. * leaving this reference for the ktio to release.
  1287. */
  1288. return NULL;
  1289. }
  1290. void
  1291. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  1292. {
  1293. struct sk_buff_head queue;
  1294. __skb_queue_head_init(&queue);
  1295. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  1296. aoenet_xmit(&queue);
  1297. }
  1298. struct sk_buff *
  1299. aoecmd_ata_id(struct aoedev *d)
  1300. {
  1301. struct aoe_hdr *h;
  1302. struct aoe_atahdr *ah;
  1303. struct frame *f;
  1304. struct sk_buff *skb;
  1305. struct aoetgt *t;
  1306. f = newframe(d);
  1307. if (f == NULL)
  1308. return NULL;
  1309. t = *d->tgt;
  1310. /* initialize the headers & frame */
  1311. skb = f->skb;
  1312. h = (struct aoe_hdr *) skb_mac_header(skb);
  1313. ah = (struct aoe_atahdr *) (h+1);
  1314. skb_put(skb, sizeof *h + sizeof *ah);
  1315. memset(h, 0, skb->len);
  1316. f->tag = aoehdr_atainit(d, t, h);
  1317. fhash(f);
  1318. t->nout++;
  1319. f->waited = 0;
  1320. f->waited_total = 0;
  1321. /* set up ata header */
  1322. ah->scnt = 1;
  1323. ah->cmdstat = ATA_CMD_ID_ATA;
  1324. ah->lba3 = 0xa0;
  1325. skb->dev = t->ifp->nd;
  1326. d->rttavg = RTTAVG_INIT;
  1327. d->rttdev = RTTDEV_INIT;
  1328. d->timer.function = rexmit_timer;
  1329. skb = skb_clone(skb, GFP_ATOMIC);
  1330. if (skb) {
  1331. do_gettimeofday(&f->sent);
  1332. f->sent_jiffs = (u32) jiffies;
  1333. }
  1334. return skb;
  1335. }
  1336. static struct aoetgt **
  1337. grow_targets(struct aoedev *d)
  1338. {
  1339. ulong oldn, newn;
  1340. struct aoetgt **tt;
  1341. oldn = d->ntargets;
  1342. newn = oldn * 2;
  1343. tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
  1344. if (!tt)
  1345. return NULL;
  1346. memmove(tt, d->targets, sizeof(*d->targets) * oldn);
  1347. d->tgt = tt + (d->tgt - d->targets);
  1348. kfree(d->targets);
  1349. d->targets = tt;
  1350. d->ntargets = newn;
  1351. return &d->targets[oldn];
  1352. }
  1353. static struct aoetgt *
  1354. addtgt(struct aoedev *d, char *addr, ulong nframes)
  1355. {
  1356. struct aoetgt *t, **tt, **te;
  1357. tt = d->targets;
  1358. te = tt + d->ntargets;
  1359. for (; tt < te && *tt; tt++)
  1360. ;
  1361. if (tt == te) {
  1362. tt = grow_targets(d);
  1363. if (!tt)
  1364. goto nomem;
  1365. }
  1366. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  1367. if (!t)
  1368. goto nomem;
  1369. t->nframes = nframes;
  1370. t->d = d;
  1371. memcpy(t->addr, addr, sizeof t->addr);
  1372. t->ifp = t->ifs;
  1373. aoecmd_wreset(t);
  1374. t->maxout = t->nframes / 2;
  1375. INIT_LIST_HEAD(&t->ffree);
  1376. return *tt = t;
  1377. nomem:
  1378. pr_info("aoe: cannot allocate memory to add target\n");
  1379. return NULL;
  1380. }
  1381. static void
  1382. setdbcnt(struct aoedev *d)
  1383. {
  1384. struct aoetgt **t, **e;
  1385. int bcnt = 0;
  1386. t = d->targets;
  1387. e = t + d->ntargets;
  1388. for (; t < e && *t; t++)
  1389. if (bcnt == 0 || bcnt > (*t)->minbcnt)
  1390. bcnt = (*t)->minbcnt;
  1391. if (bcnt != d->maxbcnt) {
  1392. d->maxbcnt = bcnt;
  1393. pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
  1394. d->aoemajor, d->aoeminor, bcnt);
  1395. }
  1396. }
  1397. static void
  1398. setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
  1399. {
  1400. struct aoedev *d;
  1401. struct aoeif *p, *e;
  1402. int minbcnt;
  1403. d = t->d;
  1404. minbcnt = bcnt;
  1405. p = t->ifs;
  1406. e = p + NAOEIFS;
  1407. for (; p < e; p++) {
  1408. if (p->nd == NULL)
  1409. break; /* end of the valid interfaces */
  1410. if (p->nd == nd) {
  1411. p->bcnt = bcnt; /* we're updating */
  1412. nd = NULL;
  1413. } else if (minbcnt > p->bcnt)
  1414. minbcnt = p->bcnt; /* find the min interface */
  1415. }
  1416. if (nd) {
  1417. if (p == e) {
  1418. pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
  1419. return;
  1420. }
  1421. dev_hold(nd);
  1422. p->nd = nd;
  1423. p->bcnt = bcnt;
  1424. }
  1425. t->minbcnt = minbcnt;
  1426. setdbcnt(d);
  1427. }
  1428. void
  1429. aoecmd_cfg_rsp(struct sk_buff *skb)
  1430. {
  1431. struct aoedev *d;
  1432. struct aoe_hdr *h;
  1433. struct aoe_cfghdr *ch;
  1434. struct aoetgt *t;
  1435. ulong flags, aoemajor;
  1436. struct sk_buff *sl;
  1437. struct sk_buff_head queue;
  1438. u16 n;
  1439. sl = NULL;
  1440. h = (struct aoe_hdr *) skb_mac_header(skb);
  1441. ch = (struct aoe_cfghdr *) (h+1);
  1442. /*
  1443. * Enough people have their dip switches set backwards to
  1444. * warrant a loud message for this special case.
  1445. */
  1446. aoemajor = get_unaligned_be16(&h->major);
  1447. if (aoemajor == 0xfff) {
  1448. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  1449. "Check shelf dip switches.\n");
  1450. return;
  1451. }
  1452. if (aoemajor == 0xffff) {
  1453. pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
  1454. aoemajor, (int) h->minor);
  1455. return;
  1456. }
  1457. if (h->minor == 0xff) {
  1458. pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
  1459. aoemajor, (int) h->minor);
  1460. return;
  1461. }
  1462. n = be16_to_cpu(ch->bufcnt);
  1463. if (n > aoe_maxout) /* keep it reasonable */
  1464. n = aoe_maxout;
  1465. d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
  1466. if (d == NULL) {
  1467. pr_info("aoe: device allocation failure\n");
  1468. return;
  1469. }
  1470. spin_lock_irqsave(&d->lock, flags);
  1471. t = gettgt(d, h->src);
  1472. if (t) {
  1473. t->nframes = n;
  1474. if (n < t->maxout)
  1475. aoecmd_wreset(t);
  1476. } else {
  1477. t = addtgt(d, h->src, n);
  1478. if (!t)
  1479. goto bail;
  1480. }
  1481. n = skb->dev->mtu;
  1482. n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
  1483. n /= 512;
  1484. if (n > ch->scnt)
  1485. n = ch->scnt;
  1486. n = n ? n * 512 : DEFAULTBCNT;
  1487. setifbcnt(t, skb->dev, n);
  1488. /* don't change users' perspective */
  1489. if (d->nopen == 0) {
  1490. d->fw_ver = be16_to_cpu(ch->fwver);
  1491. sl = aoecmd_ata_id(d);
  1492. }
  1493. bail:
  1494. spin_unlock_irqrestore(&d->lock, flags);
  1495. aoedev_put(d);
  1496. if (sl) {
  1497. __skb_queue_head_init(&queue);
  1498. __skb_queue_tail(&queue, sl);
  1499. aoenet_xmit(&queue);
  1500. }
  1501. }
  1502. void
  1503. aoecmd_wreset(struct aoetgt *t)
  1504. {
  1505. t->maxout = 1;
  1506. t->ssthresh = t->nframes / 2;
  1507. t->next_cwnd = t->nframes;
  1508. }
  1509. void
  1510. aoecmd_cleanslate(struct aoedev *d)
  1511. {
  1512. struct aoetgt **t, **te;
  1513. d->rttavg = RTTAVG_INIT;
  1514. d->rttdev = RTTDEV_INIT;
  1515. d->maxbcnt = 0;
  1516. t = d->targets;
  1517. te = t + d->ntargets;
  1518. for (; t < te && *t; t++)
  1519. aoecmd_wreset(*t);
  1520. }
  1521. void
  1522. aoe_failbuf(struct aoedev *d, struct buf *buf)
  1523. {
  1524. if (buf == NULL)
  1525. return;
  1526. buf->resid = 0;
  1527. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1528. if (buf->nframesout == 0)
  1529. aoe_end_buf(d, buf);
  1530. }
  1531. void
  1532. aoe_flush_iocq(void)
  1533. {
  1534. int i;
  1535. for (i = 0; i < ncpus; i++) {
  1536. if (kts[i].active)
  1537. aoe_flush_iocq_by_index(i);
  1538. }
  1539. }
  1540. void
  1541. aoe_flush_iocq_by_index(int id)
  1542. {
  1543. struct frame *f;
  1544. struct aoedev *d;
  1545. LIST_HEAD(flist);
  1546. struct list_head *pos;
  1547. struct sk_buff *skb;
  1548. ulong flags;
  1549. spin_lock_irqsave(&iocq[id].lock, flags);
  1550. list_splice_init(&iocq[id].head, &flist);
  1551. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1552. while (!list_empty(&flist)) {
  1553. pos = flist.next;
  1554. list_del(pos);
  1555. f = list_entry(pos, struct frame, head);
  1556. d = f->t->d;
  1557. skb = f->r_skb;
  1558. spin_lock_irqsave(&d->lock, flags);
  1559. if (f->buf) {
  1560. f->buf->nframesout--;
  1561. aoe_failbuf(d, f->buf);
  1562. }
  1563. aoe_freetframe(f);
  1564. spin_unlock_irqrestore(&d->lock, flags);
  1565. dev_kfree_skb(skb);
  1566. aoedev_put(d);
  1567. }
  1568. }
  1569. int __init
  1570. aoecmd_init(void)
  1571. {
  1572. void *p;
  1573. int i;
  1574. int ret;
  1575. /* get_zeroed_page returns page with ref count 1 */
  1576. p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
  1577. if (!p)
  1578. return -ENOMEM;
  1579. empty_page = virt_to_page(p);
  1580. ncpus = num_online_cpus();
  1581. iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
  1582. if (!iocq)
  1583. return -ENOMEM;
  1584. kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
  1585. if (!kts) {
  1586. ret = -ENOMEM;
  1587. goto kts_fail;
  1588. }
  1589. ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
  1590. if (!ktiowq) {
  1591. ret = -ENOMEM;
  1592. goto ktiowq_fail;
  1593. }
  1594. mutex_init(&ktio_spawn_lock);
  1595. for (i = 0; i < ncpus; i++) {
  1596. INIT_LIST_HEAD(&iocq[i].head);
  1597. spin_lock_init(&iocq[i].lock);
  1598. init_waitqueue_head(&ktiowq[i]);
  1599. snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
  1600. kts[i].fn = ktio;
  1601. kts[i].waitq = &ktiowq[i];
  1602. kts[i].lock = &iocq[i].lock;
  1603. kts[i].id = i;
  1604. kts[i].active = 0;
  1605. }
  1606. kts[0].active = 1;
  1607. if (aoe_ktstart(&kts[0])) {
  1608. ret = -ENOMEM;
  1609. goto ktstart_fail;
  1610. }
  1611. return 0;
  1612. ktstart_fail:
  1613. kfree(ktiowq);
  1614. ktiowq_fail:
  1615. kfree(kts);
  1616. kts_fail:
  1617. kfree(iocq);
  1618. return ret;
  1619. }
  1620. void
  1621. aoecmd_exit(void)
  1622. {
  1623. int i;
  1624. for (i = 0; i < ncpus; i++)
  1625. if (kts[i].active)
  1626. aoe_ktstop(&kts[i]);
  1627. aoe_flush_iocq();
  1628. /* Free up the iocq and thread speicific configuration
  1629. * allocated during startup.
  1630. */
  1631. kfree(iocq);
  1632. kfree(kts);
  1633. kfree(ktiowq);
  1634. free_page((unsigned long) page_address(empty_page));
  1635. empty_page = NULL;
  1636. }