aoecmd.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. /* Copyright (c) 2012 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. page = bv->bv_page;
  801. /* Non-zero page count for non-head members of
  802. * compound pages is no longer allowed by the kernel,
  803. * but this has never been seen here.
  804. */
  805. if (unlikely(PageCompound(page)))
  806. if (compound_trans_head(page) != page) {
  807. pr_crit("page tail used for block I/O\n");
  808. BUG();
  809. }
  810. atomic_inc(&page->_count);
  811. }
  812. }
  813. static void
  814. bio_pagedec(struct bio *bio)
  815. {
  816. struct bio_vec *bv;
  817. int i;
  818. bio_for_each_segment(bv, bio, i)
  819. atomic_dec(&bv->bv_page->_count);
  820. }
  821. static void
  822. bufinit(struct buf *buf, struct request *rq, struct bio *bio)
  823. {
  824. memset(buf, 0, sizeof(*buf));
  825. buf->rq = rq;
  826. buf->bio = bio;
  827. buf->resid = bio->bi_size;
  828. buf->sector = bio->bi_sector;
  829. bio_pageinc(bio);
  830. buf->bv = bio_iovec(bio);
  831. buf->bv_resid = buf->bv->bv_len;
  832. WARN_ON(buf->bv_resid == 0);
  833. }
  834. static struct buf *
  835. nextbuf(struct aoedev *d)
  836. {
  837. struct request *rq;
  838. struct request_queue *q;
  839. struct buf *buf;
  840. struct bio *bio;
  841. q = d->blkq;
  842. if (q == NULL)
  843. return NULL; /* initializing */
  844. if (d->ip.buf)
  845. return d->ip.buf;
  846. rq = d->ip.rq;
  847. if (rq == NULL) {
  848. rq = blk_peek_request(q);
  849. if (rq == NULL)
  850. return NULL;
  851. blk_start_request(rq);
  852. d->ip.rq = rq;
  853. d->ip.nxbio = rq->bio;
  854. rq->special = (void *) rqbiocnt(rq);
  855. }
  856. buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
  857. if (buf == NULL) {
  858. pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
  859. return NULL;
  860. }
  861. bio = d->ip.nxbio;
  862. bufinit(buf, rq, bio);
  863. bio = bio->bi_next;
  864. d->ip.nxbio = bio;
  865. if (bio == NULL)
  866. d->ip.rq = NULL;
  867. return d->ip.buf = buf;
  868. }
  869. /* enters with d->lock held */
  870. void
  871. aoecmd_work(struct aoedev *d)
  872. {
  873. rexmit_deferred(d);
  874. while (aoecmd_ata_rw(d))
  875. ;
  876. }
  877. /* this function performs work that has been deferred until sleeping is OK
  878. */
  879. void
  880. aoecmd_sleepwork(struct work_struct *work)
  881. {
  882. struct aoedev *d = container_of(work, struct aoedev, work);
  883. struct block_device *bd;
  884. u64 ssize;
  885. if (d->flags & DEVFL_GDALLOC)
  886. aoeblk_gdalloc(d);
  887. if (d->flags & DEVFL_NEWSIZE) {
  888. ssize = get_capacity(d->gd);
  889. bd = bdget_disk(d->gd, 0);
  890. if (bd) {
  891. mutex_lock(&bd->bd_inode->i_mutex);
  892. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  893. mutex_unlock(&bd->bd_inode->i_mutex);
  894. bdput(bd);
  895. }
  896. spin_lock_irq(&d->lock);
  897. d->flags |= DEVFL_UP;
  898. d->flags &= ~DEVFL_NEWSIZE;
  899. spin_unlock_irq(&d->lock);
  900. }
  901. }
  902. static void
  903. ata_ident_fixstring(u16 *id, int ns)
  904. {
  905. u16 s;
  906. while (ns-- > 0) {
  907. s = *id;
  908. *id++ = s >> 8 | s << 8;
  909. }
  910. }
  911. static void
  912. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  913. {
  914. u64 ssize;
  915. u16 n;
  916. /* word 83: command set supported */
  917. n = get_unaligned_le16(&id[83 << 1]);
  918. /* word 86: command set/feature enabled */
  919. n |= get_unaligned_le16(&id[86 << 1]);
  920. if (n & (1<<10)) { /* bit 10: LBA 48 */
  921. d->flags |= DEVFL_EXT;
  922. /* word 100: number lba48 sectors */
  923. ssize = get_unaligned_le64(&id[100 << 1]);
  924. /* set as in ide-disk.c:init_idedisk_capacity */
  925. d->geo.cylinders = ssize;
  926. d->geo.cylinders /= (255 * 63);
  927. d->geo.heads = 255;
  928. d->geo.sectors = 63;
  929. } else {
  930. d->flags &= ~DEVFL_EXT;
  931. /* number lba28 sectors */
  932. ssize = get_unaligned_le32(&id[60 << 1]);
  933. /* NOTE: obsolete in ATA 6 */
  934. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  935. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  936. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  937. }
  938. ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
  939. ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
  940. ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
  941. memcpy(d->ident, id, sizeof(d->ident));
  942. if (d->ssize != ssize)
  943. printk(KERN_INFO
  944. "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
  945. t->addr,
  946. d->aoemajor, d->aoeminor,
  947. d->fw_ver, (long long)ssize);
  948. d->ssize = ssize;
  949. d->geo.start = 0;
  950. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  951. return;
  952. if (d->gd != NULL) {
  953. set_capacity(d->gd, ssize);
  954. d->flags |= DEVFL_NEWSIZE;
  955. } else
  956. d->flags |= DEVFL_GDALLOC;
  957. schedule_work(&d->work);
  958. }
  959. static void
  960. calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
  961. {
  962. register long n;
  963. n = rtt;
  964. /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
  965. n -= d->rttavg >> RTTSCALE;
  966. d->rttavg += n;
  967. if (n < 0)
  968. n = -n;
  969. n -= d->rttdev >> RTTDSCALE;
  970. d->rttdev += n;
  971. if (!t || t->maxout >= t->nframes)
  972. return;
  973. if (t->maxout < t->ssthresh)
  974. t->maxout += 1;
  975. else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
  976. t->maxout += 1;
  977. t->next_cwnd = t->maxout;
  978. }
  979. }
  980. static struct aoetgt *
  981. gettgt(struct aoedev *d, char *addr)
  982. {
  983. struct aoetgt **t, **e;
  984. t = d->targets;
  985. e = t + d->ntargets;
  986. for (; t < e && *t; t++)
  987. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  988. return *t;
  989. return NULL;
  990. }
  991. static void
  992. bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
  993. {
  994. ulong fcnt;
  995. char *p;
  996. int soff = 0;
  997. loop:
  998. fcnt = bv->bv_len - (off - bv->bv_offset);
  999. if (fcnt > cnt)
  1000. fcnt = cnt;
  1001. p = page_address(bv->bv_page) + off;
  1002. skb_copy_bits(skb, soff, p, fcnt);
  1003. soff += fcnt;
  1004. cnt -= fcnt;
  1005. if (cnt <= 0)
  1006. return;
  1007. bv++;
  1008. off = bv->bv_offset;
  1009. goto loop;
  1010. }
  1011. void
  1012. aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
  1013. {
  1014. struct bio *bio;
  1015. int bok;
  1016. struct request_queue *q;
  1017. q = d->blkq;
  1018. if (rq == d->ip.rq)
  1019. d->ip.rq = NULL;
  1020. do {
  1021. bio = rq->bio;
  1022. bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
  1023. } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
  1024. /* cf. http://lkml.org/lkml/2006/10/31/28 */
  1025. if (!fastfail)
  1026. __blk_run_queue(q);
  1027. }
  1028. static void
  1029. aoe_end_buf(struct aoedev *d, struct buf *buf)
  1030. {
  1031. struct request *rq;
  1032. unsigned long n;
  1033. if (buf == d->ip.buf)
  1034. d->ip.buf = NULL;
  1035. rq = buf->rq;
  1036. bio_pagedec(buf->bio);
  1037. mempool_free(buf, d->bufpool);
  1038. n = (unsigned long) rq->special;
  1039. rq->special = (void *) --n;
  1040. if (n == 0)
  1041. aoe_end_request(d, rq, 0);
  1042. }
  1043. static void
  1044. ktiocomplete(struct frame *f)
  1045. {
  1046. struct aoe_hdr *hin, *hout;
  1047. struct aoe_atahdr *ahin, *ahout;
  1048. struct buf *buf;
  1049. struct sk_buff *skb;
  1050. struct aoetgt *t;
  1051. struct aoeif *ifp;
  1052. struct aoedev *d;
  1053. long n;
  1054. int untainted;
  1055. if (f == NULL)
  1056. return;
  1057. t = f->t;
  1058. d = t->d;
  1059. skb = f->r_skb;
  1060. buf = f->buf;
  1061. if (f->flags & FFL_PROBE)
  1062. goto out;
  1063. if (!skb) /* just fail the buf. */
  1064. goto noskb;
  1065. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  1066. ahout = (struct aoe_atahdr *) (hout+1);
  1067. hin = (struct aoe_hdr *) skb->data;
  1068. skb_pull(skb, sizeof(*hin));
  1069. ahin = (struct aoe_atahdr *) skb->data;
  1070. skb_pull(skb, sizeof(*ahin));
  1071. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  1072. pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  1073. ahout->cmdstat, ahin->cmdstat,
  1074. d->aoemajor, d->aoeminor);
  1075. noskb: if (buf)
  1076. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1077. goto out;
  1078. }
  1079. n = ahout->scnt << 9;
  1080. switch (ahout->cmdstat) {
  1081. case ATA_CMD_PIO_READ:
  1082. case ATA_CMD_PIO_READ_EXT:
  1083. if (skb->len < n) {
  1084. pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
  1085. "aoe: runt data size in read from",
  1086. (long) d->aoemajor, d->aoeminor,
  1087. skb->len, n);
  1088. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1089. break;
  1090. }
  1091. bvcpy(f->bv, f->bv_off, skb, n);
  1092. case ATA_CMD_PIO_WRITE:
  1093. case ATA_CMD_PIO_WRITE_EXT:
  1094. spin_lock_irq(&d->lock);
  1095. ifp = getif(t, skb->dev);
  1096. if (ifp)
  1097. ifp->lost = 0;
  1098. spin_unlock_irq(&d->lock);
  1099. break;
  1100. case ATA_CMD_ID_ATA:
  1101. if (skb->len < 512) {
  1102. pr_info("%s e%ld.%d. skb->len=%d need=512\n",
  1103. "aoe: runt data size in ataid from",
  1104. (long) d->aoemajor, d->aoeminor,
  1105. skb->len);
  1106. break;
  1107. }
  1108. if (skb_linearize(skb))
  1109. break;
  1110. spin_lock_irq(&d->lock);
  1111. ataid_complete(d, t, skb->data);
  1112. spin_unlock_irq(&d->lock);
  1113. break;
  1114. default:
  1115. pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  1116. ahout->cmdstat,
  1117. be16_to_cpu(get_unaligned(&hin->major)),
  1118. hin->minor);
  1119. }
  1120. out:
  1121. spin_lock_irq(&d->lock);
  1122. if (t->taint > 0
  1123. && --t->taint > 0
  1124. && t->nout_probes == 0) {
  1125. count_targets(d, &untainted);
  1126. if (untainted > 0) {
  1127. probe(t);
  1128. t->nout_probes++;
  1129. }
  1130. }
  1131. aoe_freetframe(f);
  1132. if (buf && --buf->nframesout == 0 && buf->resid == 0)
  1133. aoe_end_buf(d, buf);
  1134. spin_unlock_irq(&d->lock);
  1135. aoedev_put(d);
  1136. dev_kfree_skb(skb);
  1137. }
  1138. /* Enters with iocq.lock held.
  1139. * Returns true iff responses needing processing remain.
  1140. */
  1141. static int
  1142. ktio(int id)
  1143. {
  1144. struct frame *f;
  1145. struct list_head *pos;
  1146. int i;
  1147. int actual_id;
  1148. for (i = 0; ; ++i) {
  1149. if (i == MAXIOC)
  1150. return 1;
  1151. if (list_empty(&iocq[id].head))
  1152. return 0;
  1153. pos = iocq[id].head.next;
  1154. list_del(pos);
  1155. f = list_entry(pos, struct frame, head);
  1156. spin_unlock_irq(&iocq[id].lock);
  1157. ktiocomplete(f);
  1158. /* Figure out if extra threads are required. */
  1159. actual_id = f->t->d->aoeminor % ncpus;
  1160. if (!kts[actual_id].active) {
  1161. BUG_ON(id != 0);
  1162. mutex_lock(&ktio_spawn_lock);
  1163. if (!kts[actual_id].active
  1164. && aoe_ktstart(&kts[actual_id]) == 0)
  1165. kts[actual_id].active = 1;
  1166. mutex_unlock(&ktio_spawn_lock);
  1167. }
  1168. spin_lock_irq(&iocq[id].lock);
  1169. }
  1170. }
  1171. static int
  1172. kthread(void *vp)
  1173. {
  1174. struct ktstate *k;
  1175. DECLARE_WAITQUEUE(wait, current);
  1176. int more;
  1177. k = vp;
  1178. current->flags |= PF_NOFREEZE;
  1179. set_user_nice(current, -10);
  1180. complete(&k->rendez); /* tell spawner we're running */
  1181. do {
  1182. spin_lock_irq(k->lock);
  1183. more = k->fn(k->id);
  1184. if (!more) {
  1185. add_wait_queue(k->waitq, &wait);
  1186. __set_current_state(TASK_INTERRUPTIBLE);
  1187. }
  1188. spin_unlock_irq(k->lock);
  1189. if (!more) {
  1190. schedule();
  1191. remove_wait_queue(k->waitq, &wait);
  1192. } else
  1193. cond_resched();
  1194. } while (!kthread_should_stop());
  1195. complete(&k->rendez); /* tell spawner we're stopping */
  1196. return 0;
  1197. }
  1198. void
  1199. aoe_ktstop(struct ktstate *k)
  1200. {
  1201. kthread_stop(k->task);
  1202. wait_for_completion(&k->rendez);
  1203. }
  1204. int
  1205. aoe_ktstart(struct ktstate *k)
  1206. {
  1207. struct task_struct *task;
  1208. init_completion(&k->rendez);
  1209. task = kthread_run(kthread, k, "%s", k->name);
  1210. if (task == NULL || IS_ERR(task))
  1211. return -ENOMEM;
  1212. k->task = task;
  1213. wait_for_completion(&k->rendez); /* allow kthread to start */
  1214. init_completion(&k->rendez); /* for waiting for exit later */
  1215. return 0;
  1216. }
  1217. /* pass it off to kthreads for processing */
  1218. static void
  1219. ktcomplete(struct frame *f, struct sk_buff *skb)
  1220. {
  1221. int id;
  1222. ulong flags;
  1223. f->r_skb = skb;
  1224. id = f->t->d->aoeminor % ncpus;
  1225. spin_lock_irqsave(&iocq[id].lock, flags);
  1226. if (!kts[id].active) {
  1227. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1228. /* The thread with id has not been spawned yet,
  1229. * so delegate the work to the main thread and
  1230. * try spawning a new thread.
  1231. */
  1232. id = 0;
  1233. spin_lock_irqsave(&iocq[id].lock, flags);
  1234. }
  1235. list_add_tail(&f->head, &iocq[id].head);
  1236. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1237. wake_up(&ktiowq[id]);
  1238. }
  1239. struct sk_buff *
  1240. aoecmd_ata_rsp(struct sk_buff *skb)
  1241. {
  1242. struct aoedev *d;
  1243. struct aoe_hdr *h;
  1244. struct frame *f;
  1245. u32 n;
  1246. ulong flags;
  1247. char ebuf[128];
  1248. u16 aoemajor;
  1249. h = (struct aoe_hdr *) skb->data;
  1250. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  1251. d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
  1252. if (d == NULL) {
  1253. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  1254. "for unknown device %d.%d\n",
  1255. aoemajor, h->minor);
  1256. aoechr_error(ebuf);
  1257. return skb;
  1258. }
  1259. spin_lock_irqsave(&d->lock, flags);
  1260. n = be32_to_cpu(get_unaligned(&h->tag));
  1261. f = getframe(d, n);
  1262. if (f) {
  1263. calc_rttavg(d, f->t, tsince_hr(f));
  1264. f->t->nout--;
  1265. if (f->flags & FFL_PROBE)
  1266. f->t->nout_probes--;
  1267. } else {
  1268. f = getframe_deferred(d, n);
  1269. if (f) {
  1270. calc_rttavg(d, NULL, tsince_hr(f));
  1271. } else {
  1272. calc_rttavg(d, NULL, tsince(n));
  1273. spin_unlock_irqrestore(&d->lock, flags);
  1274. aoedev_put(d);
  1275. snprintf(ebuf, sizeof(ebuf),
  1276. "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
  1277. "unexpected rsp",
  1278. get_unaligned_be16(&h->major),
  1279. h->minor,
  1280. get_unaligned_be32(&h->tag),
  1281. jiffies,
  1282. h->src,
  1283. h->dst);
  1284. aoechr_error(ebuf);
  1285. return skb;
  1286. }
  1287. }
  1288. aoecmd_work(d);
  1289. spin_unlock_irqrestore(&d->lock, flags);
  1290. ktcomplete(f, skb);
  1291. /*
  1292. * Note here that we do not perform an aoedev_put, as we are
  1293. * leaving this reference for the ktio to release.
  1294. */
  1295. return NULL;
  1296. }
  1297. void
  1298. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  1299. {
  1300. struct sk_buff_head queue;
  1301. __skb_queue_head_init(&queue);
  1302. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  1303. aoenet_xmit(&queue);
  1304. }
  1305. struct sk_buff *
  1306. aoecmd_ata_id(struct aoedev *d)
  1307. {
  1308. struct aoe_hdr *h;
  1309. struct aoe_atahdr *ah;
  1310. struct frame *f;
  1311. struct sk_buff *skb;
  1312. struct aoetgt *t;
  1313. f = newframe(d);
  1314. if (f == NULL)
  1315. return NULL;
  1316. t = *d->tgt;
  1317. /* initialize the headers & frame */
  1318. skb = f->skb;
  1319. h = (struct aoe_hdr *) skb_mac_header(skb);
  1320. ah = (struct aoe_atahdr *) (h+1);
  1321. skb_put(skb, sizeof *h + sizeof *ah);
  1322. memset(h, 0, skb->len);
  1323. f->tag = aoehdr_atainit(d, t, h);
  1324. fhash(f);
  1325. t->nout++;
  1326. f->waited = 0;
  1327. f->waited_total = 0;
  1328. /* set up ata header */
  1329. ah->scnt = 1;
  1330. ah->cmdstat = ATA_CMD_ID_ATA;
  1331. ah->lba3 = 0xa0;
  1332. skb->dev = t->ifp->nd;
  1333. d->rttavg = RTTAVG_INIT;
  1334. d->rttdev = RTTDEV_INIT;
  1335. d->timer.function = rexmit_timer;
  1336. skb = skb_clone(skb, GFP_ATOMIC);
  1337. if (skb) {
  1338. do_gettimeofday(&f->sent);
  1339. f->sent_jiffs = (u32) jiffies;
  1340. }
  1341. return skb;
  1342. }
  1343. static struct aoetgt **
  1344. grow_targets(struct aoedev *d)
  1345. {
  1346. ulong oldn, newn;
  1347. struct aoetgt **tt;
  1348. oldn = d->ntargets;
  1349. newn = oldn * 2;
  1350. tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
  1351. if (!tt)
  1352. return NULL;
  1353. memmove(tt, d->targets, sizeof(*d->targets) * oldn);
  1354. d->tgt = tt + (d->tgt - d->targets);
  1355. kfree(d->targets);
  1356. d->targets = tt;
  1357. d->ntargets = newn;
  1358. return &d->targets[oldn];
  1359. }
  1360. static struct aoetgt *
  1361. addtgt(struct aoedev *d, char *addr, ulong nframes)
  1362. {
  1363. struct aoetgt *t, **tt, **te;
  1364. tt = d->targets;
  1365. te = tt + d->ntargets;
  1366. for (; tt < te && *tt; tt++)
  1367. ;
  1368. if (tt == te) {
  1369. tt = grow_targets(d);
  1370. if (!tt)
  1371. goto nomem;
  1372. }
  1373. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  1374. if (!t)
  1375. goto nomem;
  1376. t->nframes = nframes;
  1377. t->d = d;
  1378. memcpy(t->addr, addr, sizeof t->addr);
  1379. t->ifp = t->ifs;
  1380. aoecmd_wreset(t);
  1381. t->maxout = t->nframes / 2;
  1382. INIT_LIST_HEAD(&t->ffree);
  1383. return *tt = t;
  1384. nomem:
  1385. pr_info("aoe: cannot allocate memory to add target\n");
  1386. return NULL;
  1387. }
  1388. static void
  1389. setdbcnt(struct aoedev *d)
  1390. {
  1391. struct aoetgt **t, **e;
  1392. int bcnt = 0;
  1393. t = d->targets;
  1394. e = t + d->ntargets;
  1395. for (; t < e && *t; t++)
  1396. if (bcnt == 0 || bcnt > (*t)->minbcnt)
  1397. bcnt = (*t)->minbcnt;
  1398. if (bcnt != d->maxbcnt) {
  1399. d->maxbcnt = bcnt;
  1400. pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
  1401. d->aoemajor, d->aoeminor, bcnt);
  1402. }
  1403. }
  1404. static void
  1405. setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
  1406. {
  1407. struct aoedev *d;
  1408. struct aoeif *p, *e;
  1409. int minbcnt;
  1410. d = t->d;
  1411. minbcnt = bcnt;
  1412. p = t->ifs;
  1413. e = p + NAOEIFS;
  1414. for (; p < e; p++) {
  1415. if (p->nd == NULL)
  1416. break; /* end of the valid interfaces */
  1417. if (p->nd == nd) {
  1418. p->bcnt = bcnt; /* we're updating */
  1419. nd = NULL;
  1420. } else if (minbcnt > p->bcnt)
  1421. minbcnt = p->bcnt; /* find the min interface */
  1422. }
  1423. if (nd) {
  1424. if (p == e) {
  1425. pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
  1426. return;
  1427. }
  1428. dev_hold(nd);
  1429. p->nd = nd;
  1430. p->bcnt = bcnt;
  1431. }
  1432. t->minbcnt = minbcnt;
  1433. setdbcnt(d);
  1434. }
  1435. void
  1436. aoecmd_cfg_rsp(struct sk_buff *skb)
  1437. {
  1438. struct aoedev *d;
  1439. struct aoe_hdr *h;
  1440. struct aoe_cfghdr *ch;
  1441. struct aoetgt *t;
  1442. ulong flags, aoemajor;
  1443. struct sk_buff *sl;
  1444. struct sk_buff_head queue;
  1445. u16 n;
  1446. sl = NULL;
  1447. h = (struct aoe_hdr *) skb_mac_header(skb);
  1448. ch = (struct aoe_cfghdr *) (h+1);
  1449. /*
  1450. * Enough people have their dip switches set backwards to
  1451. * warrant a loud message for this special case.
  1452. */
  1453. aoemajor = get_unaligned_be16(&h->major);
  1454. if (aoemajor == 0xfff) {
  1455. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  1456. "Check shelf dip switches.\n");
  1457. return;
  1458. }
  1459. if (aoemajor == 0xffff) {
  1460. pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
  1461. aoemajor, (int) h->minor);
  1462. return;
  1463. }
  1464. if (h->minor == 0xff) {
  1465. pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
  1466. aoemajor, (int) h->minor);
  1467. return;
  1468. }
  1469. n = be16_to_cpu(ch->bufcnt);
  1470. if (n > aoe_maxout) /* keep it reasonable */
  1471. n = aoe_maxout;
  1472. d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
  1473. if (d == NULL) {
  1474. pr_info("aoe: device allocation failure\n");
  1475. return;
  1476. }
  1477. spin_lock_irqsave(&d->lock, flags);
  1478. t = gettgt(d, h->src);
  1479. if (t) {
  1480. t->nframes = n;
  1481. if (n < t->maxout)
  1482. aoecmd_wreset(t);
  1483. } else {
  1484. t = addtgt(d, h->src, n);
  1485. if (!t)
  1486. goto bail;
  1487. }
  1488. n = skb->dev->mtu;
  1489. n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
  1490. n /= 512;
  1491. if (n > ch->scnt)
  1492. n = ch->scnt;
  1493. n = n ? n * 512 : DEFAULTBCNT;
  1494. setifbcnt(t, skb->dev, n);
  1495. /* don't change users' perspective */
  1496. if (d->nopen == 0) {
  1497. d->fw_ver = be16_to_cpu(ch->fwver);
  1498. sl = aoecmd_ata_id(d);
  1499. }
  1500. bail:
  1501. spin_unlock_irqrestore(&d->lock, flags);
  1502. aoedev_put(d);
  1503. if (sl) {
  1504. __skb_queue_head_init(&queue);
  1505. __skb_queue_tail(&queue, sl);
  1506. aoenet_xmit(&queue);
  1507. }
  1508. }
  1509. void
  1510. aoecmd_wreset(struct aoetgt *t)
  1511. {
  1512. t->maxout = 1;
  1513. t->ssthresh = t->nframes / 2;
  1514. t->next_cwnd = t->nframes;
  1515. }
  1516. void
  1517. aoecmd_cleanslate(struct aoedev *d)
  1518. {
  1519. struct aoetgt **t, **te;
  1520. d->rttavg = RTTAVG_INIT;
  1521. d->rttdev = RTTDEV_INIT;
  1522. d->maxbcnt = 0;
  1523. t = d->targets;
  1524. te = t + d->ntargets;
  1525. for (; t < te && *t; t++)
  1526. aoecmd_wreset(*t);
  1527. }
  1528. void
  1529. aoe_failbuf(struct aoedev *d, struct buf *buf)
  1530. {
  1531. if (buf == NULL)
  1532. return;
  1533. buf->resid = 0;
  1534. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1535. if (buf->nframesout == 0)
  1536. aoe_end_buf(d, buf);
  1537. }
  1538. void
  1539. aoe_flush_iocq(void)
  1540. {
  1541. int i;
  1542. for (i = 0; i < ncpus; i++) {
  1543. if (kts[i].active)
  1544. aoe_flush_iocq_by_index(i);
  1545. }
  1546. }
  1547. void
  1548. aoe_flush_iocq_by_index(int id)
  1549. {
  1550. struct frame *f;
  1551. struct aoedev *d;
  1552. LIST_HEAD(flist);
  1553. struct list_head *pos;
  1554. struct sk_buff *skb;
  1555. ulong flags;
  1556. spin_lock_irqsave(&iocq[id].lock, flags);
  1557. list_splice_init(&iocq[id].head, &flist);
  1558. spin_unlock_irqrestore(&iocq[id].lock, flags);
  1559. while (!list_empty(&flist)) {
  1560. pos = flist.next;
  1561. list_del(pos);
  1562. f = list_entry(pos, struct frame, head);
  1563. d = f->t->d;
  1564. skb = f->r_skb;
  1565. spin_lock_irqsave(&d->lock, flags);
  1566. if (f->buf) {
  1567. f->buf->nframesout--;
  1568. aoe_failbuf(d, f->buf);
  1569. }
  1570. aoe_freetframe(f);
  1571. spin_unlock_irqrestore(&d->lock, flags);
  1572. dev_kfree_skb(skb);
  1573. aoedev_put(d);
  1574. }
  1575. }
  1576. int __init
  1577. aoecmd_init(void)
  1578. {
  1579. void *p;
  1580. int i;
  1581. int ret;
  1582. /* get_zeroed_page returns page with ref count 1 */
  1583. p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
  1584. if (!p)
  1585. return -ENOMEM;
  1586. empty_page = virt_to_page(p);
  1587. ncpus = num_online_cpus();
  1588. iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
  1589. if (!iocq)
  1590. return -ENOMEM;
  1591. kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
  1592. if (!kts) {
  1593. ret = -ENOMEM;
  1594. goto kts_fail;
  1595. }
  1596. ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
  1597. if (!ktiowq) {
  1598. ret = -ENOMEM;
  1599. goto ktiowq_fail;
  1600. }
  1601. mutex_init(&ktio_spawn_lock);
  1602. for (i = 0; i < ncpus; i++) {
  1603. INIT_LIST_HEAD(&iocq[i].head);
  1604. spin_lock_init(&iocq[i].lock);
  1605. init_waitqueue_head(&ktiowq[i]);
  1606. snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
  1607. kts[i].fn = ktio;
  1608. kts[i].waitq = &ktiowq[i];
  1609. kts[i].lock = &iocq[i].lock;
  1610. kts[i].id = i;
  1611. kts[i].active = 0;
  1612. }
  1613. kts[0].active = 1;
  1614. if (aoe_ktstart(&kts[0])) {
  1615. ret = -ENOMEM;
  1616. goto ktstart_fail;
  1617. }
  1618. return 0;
  1619. ktstart_fail:
  1620. kfree(ktiowq);
  1621. ktiowq_fail:
  1622. kfree(kts);
  1623. kts_fail:
  1624. kfree(iocq);
  1625. return ret;
  1626. }
  1627. void
  1628. aoecmd_exit(void)
  1629. {
  1630. int i;
  1631. for (i = 0; i < ncpus; i++)
  1632. if (kts[i].active)
  1633. aoe_ktstop(&kts[i]);
  1634. aoe_flush_iocq();
  1635. /* Free up the iocq and thread speicific configuration
  1636. * allocated during startup.
  1637. */
  1638. kfree(iocq);
  1639. kfree(kts);
  1640. kfree(ktiowq);
  1641. free_page((unsigned long) page_address(empty_page));
  1642. empty_page = NULL;
  1643. }