aoecmd.c 37 KB

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