aoecmd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/genhd.h>
  11. #include <linux/moduleparam.h>
  12. #include <net/net_namespace.h>
  13. #include <asm/unaligned.h>
  14. #include "aoe.h"
  15. static int aoe_deadsecs = 60 * 3;
  16. module_param(aoe_deadsecs, int, 0644);
  17. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  18. static int aoe_maxout = 16;
  19. module_param(aoe_maxout, int, 0644);
  20. MODULE_PARM_DESC(aoe_maxout,
  21. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  22. static struct sk_buff *
  23. new_skb(ulong len)
  24. {
  25. struct sk_buff *skb;
  26. skb = alloc_skb(len, GFP_ATOMIC);
  27. if (skb) {
  28. skb_reset_mac_header(skb);
  29. skb_reset_network_header(skb);
  30. skb->protocol = __constant_htons(ETH_P_AOE);
  31. skb->priority = 0;
  32. skb->next = skb->prev = NULL;
  33. /* tell the network layer not to perform IP checksums
  34. * or to get the NIC to do it
  35. */
  36. skb->ip_summed = CHECKSUM_NONE;
  37. }
  38. return skb;
  39. }
  40. static struct frame *
  41. getframe(struct aoetgt *t, int tag)
  42. {
  43. struct frame *f, *e;
  44. f = t->frames;
  45. e = f + t->nframes;
  46. for (; f<e; f++)
  47. if (f->tag == tag)
  48. return f;
  49. return NULL;
  50. }
  51. /*
  52. * Leave the top bit clear so we have tagspace for userland.
  53. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  54. * This driver reserves tag -1 to mean "unused frame."
  55. */
  56. static int
  57. newtag(struct aoetgt *t)
  58. {
  59. register ulong n;
  60. n = jiffies & 0xffff;
  61. return n |= (++t->lasttag & 0x7fff) << 16;
  62. }
  63. static int
  64. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  65. {
  66. u32 host_tag = newtag(t);
  67. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  68. memcpy(h->dst, t->addr, sizeof h->dst);
  69. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  70. h->verfl = AOE_HVER;
  71. h->major = cpu_to_be16(d->aoemajor);
  72. h->minor = d->aoeminor;
  73. h->cmd = AOECMD_ATA;
  74. h->tag = cpu_to_be32(host_tag);
  75. return host_tag;
  76. }
  77. static inline void
  78. put_lba(struct aoe_atahdr *ah, sector_t lba)
  79. {
  80. ah->lba0 = lba;
  81. ah->lba1 = lba >>= 8;
  82. ah->lba2 = lba >>= 8;
  83. ah->lba3 = lba >>= 8;
  84. ah->lba4 = lba >>= 8;
  85. ah->lba5 = lba >>= 8;
  86. }
  87. static void
  88. ifrotate(struct aoetgt *t)
  89. {
  90. t->ifp++;
  91. if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
  92. t->ifp = t->ifs;
  93. if (t->ifp->nd == NULL) {
  94. printk(KERN_INFO "aoe: no interface to rotate to\n");
  95. BUG();
  96. }
  97. }
  98. static void
  99. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  100. {
  101. if (!d->skbpool_hd)
  102. d->skbpool_hd = skb;
  103. else
  104. d->skbpool_tl->next = skb;
  105. d->skbpool_tl = skb;
  106. }
  107. static struct sk_buff *
  108. skb_pool_get(struct aoedev *d)
  109. {
  110. struct sk_buff *skb;
  111. skb = d->skbpool_hd;
  112. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  113. d->skbpool_hd = skb->next;
  114. skb->next = NULL;
  115. return skb;
  116. }
  117. if (d->nskbpool < NSKBPOOLMAX
  118. && (skb = new_skb(ETH_ZLEN))) {
  119. d->nskbpool++;
  120. return skb;
  121. }
  122. return NULL;
  123. }
  124. /* freeframe is where we do our load balancing so it's a little hairy. */
  125. static struct frame *
  126. freeframe(struct aoedev *d)
  127. {
  128. struct frame *f, *e, *rf;
  129. struct aoetgt **t;
  130. struct sk_buff *skb;
  131. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  132. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  133. return NULL;
  134. }
  135. t = d->tgt;
  136. t++;
  137. if (t >= &d->targets[NTARGETS] || !*t)
  138. t = d->targets;
  139. for (;;) {
  140. if ((*t)->nout < (*t)->maxout
  141. && t != d->htgt
  142. && (*t)->ifp->nd) {
  143. rf = NULL;
  144. f = (*t)->frames;
  145. e = f + (*t)->nframes;
  146. for (; f < e; f++) {
  147. if (f->tag != FREETAG)
  148. continue;
  149. skb = f->skb;
  150. if (!skb
  151. && !(f->skb = skb = new_skb(ETH_ZLEN)))
  152. continue;
  153. if (atomic_read(&skb_shinfo(skb)->dataref)
  154. != 1) {
  155. if (!rf)
  156. rf = f;
  157. continue;
  158. }
  159. gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  160. skb_trim(skb, 0);
  161. d->tgt = t;
  162. ifrotate(*t);
  163. return f;
  164. }
  165. /* Work can be done, but the network layer is
  166. holding our precious packets. Try to grab
  167. one from the pool. */
  168. f = rf;
  169. if (f == NULL) { /* more paranoia */
  170. printk(KERN_ERR
  171. "aoe: freeframe: %s.\n",
  172. "unexpected null rf");
  173. d->flags |= DEVFL_KICKME;
  174. return NULL;
  175. }
  176. skb = skb_pool_get(d);
  177. if (skb) {
  178. skb_pool_put(d, f->skb);
  179. f->skb = skb;
  180. goto gotone;
  181. }
  182. (*t)->dataref++;
  183. if ((*t)->nout == 0)
  184. d->flags |= DEVFL_KICKME;
  185. }
  186. if (t == d->tgt) /* we've looped and found nada */
  187. break;
  188. t++;
  189. if (t >= &d->targets[NTARGETS] || !*t)
  190. t = d->targets;
  191. }
  192. return NULL;
  193. }
  194. static int
  195. aoecmd_ata_rw(struct aoedev *d)
  196. {
  197. struct frame *f;
  198. struct aoe_hdr *h;
  199. struct aoe_atahdr *ah;
  200. struct buf *buf;
  201. struct bio_vec *bv;
  202. struct aoetgt *t;
  203. struct sk_buff *skb;
  204. ulong bcnt;
  205. char writebit, extbit;
  206. writebit = 0x10;
  207. extbit = 0x4;
  208. f = freeframe(d);
  209. if (f == NULL)
  210. return 0;
  211. t = *d->tgt;
  212. buf = d->inprocess;
  213. bv = buf->bv;
  214. bcnt = t->ifp->maxbcnt;
  215. if (bcnt == 0)
  216. bcnt = DEFAULTBCNT;
  217. if (bcnt > buf->bv_resid)
  218. bcnt = buf->bv_resid;
  219. /* initialize the headers & frame */
  220. skb = f->skb;
  221. h = (struct aoe_hdr *) skb_mac_header(skb);
  222. ah = (struct aoe_atahdr *) (h+1);
  223. skb_put(skb, sizeof *h + sizeof *ah);
  224. memset(h, 0, skb->len);
  225. f->tag = aoehdr_atainit(d, t, h);
  226. t->nout++;
  227. f->waited = 0;
  228. f->buf = buf;
  229. f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
  230. f->bcnt = bcnt;
  231. f->lba = buf->sector;
  232. /* set up ata header */
  233. ah->scnt = bcnt >> 9;
  234. put_lba(ah, buf->sector);
  235. if (d->flags & DEVFL_EXT) {
  236. ah->aflags |= AOEAFL_EXT;
  237. } else {
  238. extbit = 0;
  239. ah->lba3 &= 0x0f;
  240. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  241. }
  242. if (bio_data_dir(buf->bio) == WRITE) {
  243. skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
  244. ah->aflags |= AOEAFL_WRITE;
  245. skb->len += bcnt;
  246. skb->data_len = bcnt;
  247. t->wpkts++;
  248. } else {
  249. t->rpkts++;
  250. writebit = 0;
  251. }
  252. ah->cmdstat = WIN_READ | writebit | extbit;
  253. /* mark all tracking fields and load out */
  254. buf->nframesout += 1;
  255. buf->bv_off += bcnt;
  256. buf->bv_resid -= bcnt;
  257. buf->resid -= bcnt;
  258. buf->sector += bcnt >> 9;
  259. if (buf->resid == 0) {
  260. d->inprocess = NULL;
  261. } else if (buf->bv_resid == 0) {
  262. buf->bv = ++bv;
  263. buf->bv_resid = bv->bv_len;
  264. WARN_ON(buf->bv_resid == 0);
  265. buf->bv_off = bv->bv_offset;
  266. }
  267. skb->dev = t->ifp->nd;
  268. skb = skb_clone(skb, GFP_ATOMIC);
  269. if (skb) {
  270. if (d->sendq_hd)
  271. d->sendq_tl->next = skb;
  272. else
  273. d->sendq_hd = skb;
  274. d->sendq_tl = skb;
  275. }
  276. return 1;
  277. }
  278. /* some callers cannot sleep, and they can call this function,
  279. * transmitting the packets later, when interrupts are on
  280. */
  281. static struct sk_buff *
  282. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
  283. {
  284. struct aoe_hdr *h;
  285. struct aoe_cfghdr *ch;
  286. struct sk_buff *skb, *sl, *sl_tail;
  287. struct net_device *ifp;
  288. sl = sl_tail = NULL;
  289. read_lock(&dev_base_lock);
  290. for_each_netdev(&init_net, ifp) {
  291. dev_hold(ifp);
  292. if (!is_aoe_netif(ifp))
  293. goto cont;
  294. skb = new_skb(sizeof *h + sizeof *ch);
  295. if (skb == NULL) {
  296. printk(KERN_INFO "aoe: skb alloc failure\n");
  297. goto cont;
  298. }
  299. skb_put(skb, sizeof *h + sizeof *ch);
  300. skb->dev = ifp;
  301. if (sl_tail == NULL)
  302. sl_tail = skb;
  303. h = (struct aoe_hdr *) skb_mac_header(skb);
  304. memset(h, 0, sizeof *h + sizeof *ch);
  305. memset(h->dst, 0xff, sizeof h->dst);
  306. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  307. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  308. h->verfl = AOE_HVER;
  309. h->major = cpu_to_be16(aoemajor);
  310. h->minor = aoeminor;
  311. h->cmd = AOECMD_CFG;
  312. skb->next = sl;
  313. sl = skb;
  314. cont:
  315. dev_put(ifp);
  316. }
  317. read_unlock(&dev_base_lock);
  318. if (tail != NULL)
  319. *tail = sl_tail;
  320. return sl;
  321. }
  322. static void
  323. resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
  324. {
  325. struct sk_buff *skb;
  326. struct aoe_hdr *h;
  327. struct aoe_atahdr *ah;
  328. char buf[128];
  329. u32 n;
  330. ifrotate(t);
  331. n = newtag(t);
  332. skb = f->skb;
  333. h = (struct aoe_hdr *) skb_mac_header(skb);
  334. ah = (struct aoe_atahdr *) (h+1);
  335. snprintf(buf, sizeof buf,
  336. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x "
  337. "s=%012llx d=%012llx nout=%d\n",
  338. "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
  339. mac_addr(h->src),
  340. mac_addr(h->dst), t->nout);
  341. aoechr_error(buf);
  342. f->tag = n;
  343. h->tag = cpu_to_be32(n);
  344. memcpy(h->dst, t->addr, sizeof h->dst);
  345. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  346. switch (ah->cmdstat) {
  347. default:
  348. break;
  349. case WIN_READ:
  350. case WIN_READ_EXT:
  351. case WIN_WRITE:
  352. case WIN_WRITE_EXT:
  353. put_lba(ah, f->lba);
  354. n = f->bcnt;
  355. if (n > DEFAULTBCNT)
  356. n = DEFAULTBCNT;
  357. ah->scnt = n >> 9;
  358. if (ah->aflags & AOEAFL_WRITE) {
  359. skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
  360. offset_in_page(f->bufaddr), n);
  361. skb->len = sizeof *h + sizeof *ah + n;
  362. skb->data_len = n;
  363. }
  364. }
  365. skb->dev = t->ifp->nd;
  366. skb = skb_clone(skb, GFP_ATOMIC);
  367. if (skb == NULL)
  368. return;
  369. if (d->sendq_hd)
  370. d->sendq_tl->next = skb;
  371. else
  372. d->sendq_hd = skb;
  373. d->sendq_tl = skb;
  374. }
  375. static int
  376. tsince(int tag)
  377. {
  378. int n;
  379. n = jiffies & 0xffff;
  380. n -= tag & 0xffff;
  381. if (n < 0)
  382. n += 1<<16;
  383. return n;
  384. }
  385. static struct aoeif *
  386. getif(struct aoetgt *t, struct net_device *nd)
  387. {
  388. struct aoeif *p, *e;
  389. p = t->ifs;
  390. e = p + NAOEIFS;
  391. for (; p < e; p++)
  392. if (p->nd == nd)
  393. return p;
  394. return NULL;
  395. }
  396. static struct aoeif *
  397. addif(struct aoetgt *t, struct net_device *nd)
  398. {
  399. struct aoeif *p;
  400. p = getif(t, NULL);
  401. if (!p)
  402. return NULL;
  403. p->nd = nd;
  404. p->maxbcnt = DEFAULTBCNT;
  405. p->lost = 0;
  406. p->lostjumbo = 0;
  407. return p;
  408. }
  409. static void
  410. ejectif(struct aoetgt *t, struct aoeif *ifp)
  411. {
  412. struct aoeif *e;
  413. ulong n;
  414. e = t->ifs + NAOEIFS - 1;
  415. n = (e - ifp) * sizeof *ifp;
  416. memmove(ifp, ifp+1, n);
  417. e->nd = NULL;
  418. }
  419. static int
  420. sthtith(struct aoedev *d)
  421. {
  422. struct frame *f, *e, *nf;
  423. struct sk_buff *skb;
  424. struct aoetgt *ht = *d->htgt;
  425. f = ht->frames;
  426. e = f + ht->nframes;
  427. for (; f < e; f++) {
  428. if (f->tag == FREETAG)
  429. continue;
  430. nf = freeframe(d);
  431. if (!nf)
  432. return 0;
  433. skb = nf->skb;
  434. *nf = *f;
  435. f->skb = skb;
  436. f->tag = FREETAG;
  437. nf->waited = 0;
  438. ht->nout--;
  439. (*d->tgt)->nout++;
  440. resend(d, *d->tgt, nf);
  441. }
  442. /* he's clean, he's useless. take away his interfaces */
  443. memset(ht->ifs, 0, sizeof ht->ifs);
  444. d->htgt = NULL;
  445. return 1;
  446. }
  447. static inline unsigned char
  448. ata_scnt(unsigned char *packet) {
  449. struct aoe_hdr *h;
  450. struct aoe_atahdr *ah;
  451. h = (struct aoe_hdr *) packet;
  452. ah = (struct aoe_atahdr *) (h+1);
  453. return ah->scnt;
  454. }
  455. static void
  456. rexmit_timer(ulong vp)
  457. {
  458. struct aoedev *d;
  459. struct aoetgt *t, **tt, **te;
  460. struct aoeif *ifp;
  461. struct frame *f, *e;
  462. struct sk_buff *sl;
  463. register long timeout;
  464. ulong flags, n;
  465. d = (struct aoedev *) vp;
  466. sl = NULL;
  467. /* timeout is always ~150% of the moving average */
  468. timeout = d->rttavg;
  469. timeout += timeout >> 1;
  470. spin_lock_irqsave(&d->lock, flags);
  471. if (d->flags & DEVFL_TKILL) {
  472. spin_unlock_irqrestore(&d->lock, flags);
  473. return;
  474. }
  475. tt = d->targets;
  476. te = tt + NTARGETS;
  477. for (; tt < te && *tt; tt++) {
  478. t = *tt;
  479. f = t->frames;
  480. e = f + t->nframes;
  481. for (; f < e; f++) {
  482. if (f->tag == FREETAG
  483. || tsince(f->tag) < timeout)
  484. continue;
  485. n = f->waited += timeout;
  486. n /= HZ;
  487. if (n > aoe_deadsecs) {
  488. /* waited too long. device failure. */
  489. aoedev_downdev(d);
  490. break;
  491. }
  492. if (n > HELPWAIT /* see if another target can help */
  493. && (tt != d->targets || d->targets[1]))
  494. d->htgt = tt;
  495. if (t->nout == t->maxout) {
  496. if (t->maxout > 1)
  497. t->maxout--;
  498. t->lastwadj = jiffies;
  499. }
  500. ifp = getif(t, f->skb->dev);
  501. if (ifp && ++ifp->lost > (t->nframes << 1)
  502. && (ifp != t->ifs || t->ifs[1].nd)) {
  503. ejectif(t, ifp);
  504. ifp = NULL;
  505. }
  506. if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
  507. && ifp && ++ifp->lostjumbo > (t->nframes << 1)
  508. && ifp->maxbcnt != DEFAULTBCNT) {
  509. printk(KERN_INFO
  510. "aoe: e%ld.%d: "
  511. "too many lost jumbo on "
  512. "%s:%012llx - "
  513. "falling back to %d frames.\n",
  514. d->aoemajor, d->aoeminor,
  515. ifp->nd->name, mac_addr(t->addr),
  516. DEFAULTBCNT);
  517. ifp->maxbcnt = 0;
  518. }
  519. resend(d, t, f);
  520. }
  521. /* window check */
  522. if (t->nout == t->maxout
  523. && t->maxout < t->nframes
  524. && (jiffies - t->lastwadj)/HZ > 10) {
  525. t->maxout++;
  526. t->lastwadj = jiffies;
  527. }
  528. }
  529. if (d->sendq_hd) {
  530. n = d->rttavg <<= 1;
  531. if (n > MAXTIMER)
  532. d->rttavg = MAXTIMER;
  533. }
  534. if (d->flags & DEVFL_KICKME || d->htgt) {
  535. d->flags &= ~DEVFL_KICKME;
  536. aoecmd_work(d);
  537. }
  538. sl = d->sendq_hd;
  539. d->sendq_hd = d->sendq_tl = NULL;
  540. d->timer.expires = jiffies + TIMERTICK;
  541. add_timer(&d->timer);
  542. spin_unlock_irqrestore(&d->lock, flags);
  543. aoenet_xmit(sl);
  544. }
  545. /* enters with d->lock held */
  546. void
  547. aoecmd_work(struct aoedev *d)
  548. {
  549. struct buf *buf;
  550. loop:
  551. if (d->htgt && !sthtith(d))
  552. return;
  553. if (d->inprocess == NULL) {
  554. if (list_empty(&d->bufq))
  555. return;
  556. buf = container_of(d->bufq.next, struct buf, bufs);
  557. list_del(d->bufq.next);
  558. d->inprocess = buf;
  559. }
  560. if (aoecmd_ata_rw(d))
  561. goto loop;
  562. }
  563. /* this function performs work that has been deferred until sleeping is OK
  564. */
  565. void
  566. aoecmd_sleepwork(struct work_struct *work)
  567. {
  568. struct aoedev *d = container_of(work, struct aoedev, work);
  569. if (d->flags & DEVFL_GDALLOC)
  570. aoeblk_gdalloc(d);
  571. if (d->flags & DEVFL_NEWSIZE) {
  572. struct block_device *bd;
  573. unsigned long flags;
  574. u64 ssize;
  575. ssize = d->gd->capacity;
  576. bd = bdget_disk(d->gd, 0);
  577. if (bd) {
  578. mutex_lock(&bd->bd_inode->i_mutex);
  579. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  580. mutex_unlock(&bd->bd_inode->i_mutex);
  581. bdput(bd);
  582. }
  583. spin_lock_irqsave(&d->lock, flags);
  584. d->flags |= DEVFL_UP;
  585. d->flags &= ~DEVFL_NEWSIZE;
  586. spin_unlock_irqrestore(&d->lock, flags);
  587. }
  588. }
  589. static void
  590. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  591. {
  592. u64 ssize;
  593. u16 n;
  594. /* word 83: command set supported */
  595. n = get_unaligned_le16(&id[83 << 1]);
  596. /* word 86: command set/feature enabled */
  597. n |= get_unaligned_le16(&id[86 << 1]);
  598. if (n & (1<<10)) { /* bit 10: LBA 48 */
  599. d->flags |= DEVFL_EXT;
  600. /* word 100: number lba48 sectors */
  601. ssize = get_unaligned_le64(&id[100 << 1]);
  602. /* set as in ide-disk.c:init_idedisk_capacity */
  603. d->geo.cylinders = ssize;
  604. d->geo.cylinders /= (255 * 63);
  605. d->geo.heads = 255;
  606. d->geo.sectors = 63;
  607. } else {
  608. d->flags &= ~DEVFL_EXT;
  609. /* number lba28 sectors */
  610. ssize = get_unaligned_le32(&id[60 << 1]);
  611. /* NOTE: obsolete in ATA 6 */
  612. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  613. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  614. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  615. }
  616. if (d->ssize != ssize)
  617. printk(KERN_INFO
  618. "aoe: %012llx e%ld.%d v%04x has %llu sectors\n",
  619. mac_addr(t->addr),
  620. d->aoemajor, d->aoeminor,
  621. d->fw_ver, (long long)ssize);
  622. d->ssize = ssize;
  623. d->geo.start = 0;
  624. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  625. return;
  626. if (d->gd != NULL) {
  627. d->gd->capacity = ssize;
  628. d->flags |= DEVFL_NEWSIZE;
  629. } else
  630. d->flags |= DEVFL_GDALLOC;
  631. schedule_work(&d->work);
  632. }
  633. static void
  634. calc_rttavg(struct aoedev *d, int rtt)
  635. {
  636. register long n;
  637. n = rtt;
  638. if (n < 0) {
  639. n = -rtt;
  640. if (n < MINTIMER)
  641. n = MINTIMER;
  642. else if (n > MAXTIMER)
  643. n = MAXTIMER;
  644. d->mintimer += (n - d->mintimer) >> 1;
  645. } else if (n < d->mintimer)
  646. n = d->mintimer;
  647. else if (n > MAXTIMER)
  648. n = MAXTIMER;
  649. /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
  650. n -= d->rttavg;
  651. d->rttavg += n >> 2;
  652. }
  653. static struct aoetgt *
  654. gettgt(struct aoedev *d, char *addr)
  655. {
  656. struct aoetgt **t, **e;
  657. t = d->targets;
  658. e = t + NTARGETS;
  659. for (; t < e && *t; t++)
  660. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  661. return *t;
  662. return NULL;
  663. }
  664. static inline void
  665. diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
  666. {
  667. unsigned long n_sect = bio->bi_size >> 9;
  668. const int rw = bio_data_dir(bio);
  669. struct hd_struct *part;
  670. int cpu;
  671. cpu = disk_stat_lock();
  672. part = disk_map_sector_rcu(disk, sector);
  673. all_stat_inc(cpu, disk, part, ios[rw], sector);
  674. all_stat_add(cpu, disk, part, ticks[rw], duration, sector);
  675. all_stat_add(cpu, disk, part, sectors[rw], n_sect, sector);
  676. all_stat_add(cpu, disk, part, io_ticks, duration, sector);
  677. disk_stat_unlock();
  678. }
  679. void
  680. aoecmd_ata_rsp(struct sk_buff *skb)
  681. {
  682. struct aoedev *d;
  683. struct aoe_hdr *hin, *hout;
  684. struct aoe_atahdr *ahin, *ahout;
  685. struct frame *f;
  686. struct buf *buf;
  687. struct sk_buff *sl;
  688. struct aoetgt *t;
  689. struct aoeif *ifp;
  690. register long n;
  691. ulong flags;
  692. char ebuf[128];
  693. u16 aoemajor;
  694. hin = (struct aoe_hdr *) skb_mac_header(skb);
  695. aoemajor = get_unaligned_be16(&hin->major);
  696. d = aoedev_by_aoeaddr(aoemajor, hin->minor);
  697. if (d == NULL) {
  698. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  699. "for unknown device %d.%d\n",
  700. aoemajor, hin->minor);
  701. aoechr_error(ebuf);
  702. return;
  703. }
  704. spin_lock_irqsave(&d->lock, flags);
  705. n = get_unaligned_be32(&hin->tag);
  706. t = gettgt(d, hin->src);
  707. if (t == NULL) {
  708. printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n",
  709. d->aoemajor, d->aoeminor, mac_addr(hin->src));
  710. spin_unlock_irqrestore(&d->lock, flags);
  711. return;
  712. }
  713. f = getframe(t, n);
  714. if (f == NULL) {
  715. calc_rttavg(d, -tsince(n));
  716. spin_unlock_irqrestore(&d->lock, flags);
  717. snprintf(ebuf, sizeof ebuf,
  718. "%15s e%d.%d tag=%08x@%08lx\n",
  719. "unexpected rsp",
  720. get_unaligned_be16(&hin->major),
  721. hin->minor,
  722. get_unaligned_be32(&hin->tag),
  723. jiffies);
  724. aoechr_error(ebuf);
  725. return;
  726. }
  727. calc_rttavg(d, tsince(f->tag));
  728. ahin = (struct aoe_atahdr *) (hin+1);
  729. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  730. ahout = (struct aoe_atahdr *) (hout+1);
  731. buf = f->buf;
  732. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  733. printk(KERN_ERR
  734. "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  735. ahout->cmdstat, ahin->cmdstat,
  736. d->aoemajor, d->aoeminor);
  737. if (buf)
  738. buf->flags |= BUFFL_FAIL;
  739. } else {
  740. if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
  741. d->htgt = NULL;
  742. n = ahout->scnt << 9;
  743. switch (ahout->cmdstat) {
  744. case WIN_READ:
  745. case WIN_READ_EXT:
  746. if (skb->len - sizeof *hin - sizeof *ahin < n) {
  747. printk(KERN_ERR
  748. "aoe: %s. skb->len=%d need=%ld\n",
  749. "runt data size in read", skb->len, n);
  750. /* fail frame f? just returning will rexmit. */
  751. spin_unlock_irqrestore(&d->lock, flags);
  752. return;
  753. }
  754. memcpy(f->bufaddr, ahin+1, n);
  755. case WIN_WRITE:
  756. case WIN_WRITE_EXT:
  757. ifp = getif(t, skb->dev);
  758. if (ifp) {
  759. ifp->lost = 0;
  760. if (n > DEFAULTBCNT)
  761. ifp->lostjumbo = 0;
  762. }
  763. if (f->bcnt -= n) {
  764. f->lba += n >> 9;
  765. f->bufaddr += n;
  766. resend(d, t, f);
  767. goto xmit;
  768. }
  769. break;
  770. case WIN_IDENTIFY:
  771. if (skb->len - sizeof *hin - sizeof *ahin < 512) {
  772. printk(KERN_INFO
  773. "aoe: runt data size in ataid. skb->len=%d\n",
  774. skb->len);
  775. spin_unlock_irqrestore(&d->lock, flags);
  776. return;
  777. }
  778. ataid_complete(d, t, (char *) (ahin+1));
  779. break;
  780. default:
  781. printk(KERN_INFO
  782. "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  783. ahout->cmdstat,
  784. get_unaligned_be16(&hin->major),
  785. hin->minor);
  786. }
  787. }
  788. if (buf && --buf->nframesout == 0 && buf->resid == 0) {
  789. diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
  790. n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
  791. bio_endio(buf->bio, n);
  792. mempool_free(buf, d->bufpool);
  793. }
  794. f->buf = NULL;
  795. f->tag = FREETAG;
  796. t->nout--;
  797. aoecmd_work(d);
  798. xmit:
  799. sl = d->sendq_hd;
  800. d->sendq_hd = d->sendq_tl = NULL;
  801. spin_unlock_irqrestore(&d->lock, flags);
  802. aoenet_xmit(sl);
  803. }
  804. void
  805. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  806. {
  807. struct sk_buff *sl;
  808. sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
  809. aoenet_xmit(sl);
  810. }
  811. struct sk_buff *
  812. aoecmd_ata_id(struct aoedev *d)
  813. {
  814. struct aoe_hdr *h;
  815. struct aoe_atahdr *ah;
  816. struct frame *f;
  817. struct sk_buff *skb;
  818. struct aoetgt *t;
  819. f = freeframe(d);
  820. if (f == NULL)
  821. return NULL;
  822. t = *d->tgt;
  823. /* initialize the headers & frame */
  824. skb = f->skb;
  825. h = (struct aoe_hdr *) skb_mac_header(skb);
  826. ah = (struct aoe_atahdr *) (h+1);
  827. skb_put(skb, sizeof *h + sizeof *ah);
  828. memset(h, 0, skb->len);
  829. f->tag = aoehdr_atainit(d, t, h);
  830. t->nout++;
  831. f->waited = 0;
  832. /* set up ata header */
  833. ah->scnt = 1;
  834. ah->cmdstat = WIN_IDENTIFY;
  835. ah->lba3 = 0xa0;
  836. skb->dev = t->ifp->nd;
  837. d->rttavg = MAXTIMER;
  838. d->timer.function = rexmit_timer;
  839. return skb_clone(skb, GFP_ATOMIC);
  840. }
  841. static struct aoetgt *
  842. addtgt(struct aoedev *d, char *addr, ulong nframes)
  843. {
  844. struct aoetgt *t, **tt, **te;
  845. struct frame *f, *e;
  846. tt = d->targets;
  847. te = tt + NTARGETS;
  848. for (; tt < te && *tt; tt++)
  849. ;
  850. if (tt == te) {
  851. printk(KERN_INFO
  852. "aoe: device addtgt failure; too many targets\n");
  853. return NULL;
  854. }
  855. t = kcalloc(1, sizeof *t, GFP_ATOMIC);
  856. f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
  857. if (!t || !f) {
  858. kfree(f);
  859. kfree(t);
  860. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  861. return NULL;
  862. }
  863. t->nframes = nframes;
  864. t->frames = f;
  865. e = f + nframes;
  866. for (; f < e; f++)
  867. f->tag = FREETAG;
  868. memcpy(t->addr, addr, sizeof t->addr);
  869. t->ifp = t->ifs;
  870. t->maxout = t->nframes;
  871. return *tt = t;
  872. }
  873. void
  874. aoecmd_cfg_rsp(struct sk_buff *skb)
  875. {
  876. struct aoedev *d;
  877. struct aoe_hdr *h;
  878. struct aoe_cfghdr *ch;
  879. struct aoetgt *t;
  880. struct aoeif *ifp;
  881. ulong flags, sysminor, aoemajor;
  882. struct sk_buff *sl;
  883. u16 n;
  884. h = (struct aoe_hdr *) skb_mac_header(skb);
  885. ch = (struct aoe_cfghdr *) (h+1);
  886. /*
  887. * Enough people have their dip switches set backwards to
  888. * warrant a loud message for this special case.
  889. */
  890. aoemajor = get_unaligned_be16(&h->major);
  891. if (aoemajor == 0xfff) {
  892. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  893. "Check shelf dip switches.\n");
  894. return;
  895. }
  896. sysminor = SYSMINOR(aoemajor, h->minor);
  897. if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
  898. printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
  899. aoemajor, (int) h->minor);
  900. return;
  901. }
  902. n = be16_to_cpu(ch->bufcnt);
  903. if (n > aoe_maxout) /* keep it reasonable */
  904. n = aoe_maxout;
  905. d = aoedev_by_sysminor_m(sysminor);
  906. if (d == NULL) {
  907. printk(KERN_INFO "aoe: device sysminor_m failure\n");
  908. return;
  909. }
  910. spin_lock_irqsave(&d->lock, flags);
  911. t = gettgt(d, h->src);
  912. if (!t) {
  913. t = addtgt(d, h->src, n);
  914. if (!t) {
  915. spin_unlock_irqrestore(&d->lock, flags);
  916. return;
  917. }
  918. }
  919. ifp = getif(t, skb->dev);
  920. if (!ifp) {
  921. ifp = addif(t, skb->dev);
  922. if (!ifp) {
  923. printk(KERN_INFO
  924. "aoe: device addif failure; "
  925. "too many interfaces?\n");
  926. spin_unlock_irqrestore(&d->lock, flags);
  927. return;
  928. }
  929. }
  930. if (ifp->maxbcnt) {
  931. n = ifp->nd->mtu;
  932. n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
  933. n /= 512;
  934. if (n > ch->scnt)
  935. n = ch->scnt;
  936. n = n ? n * 512 : DEFAULTBCNT;
  937. if (n != ifp->maxbcnt) {
  938. printk(KERN_INFO
  939. "aoe: e%ld.%d: setting %d%s%s:%012llx\n",
  940. d->aoemajor, d->aoeminor, n,
  941. " byte data frames on ", ifp->nd->name,
  942. mac_addr(t->addr));
  943. ifp->maxbcnt = n;
  944. }
  945. }
  946. /* don't change users' perspective */
  947. if (d->nopen) {
  948. spin_unlock_irqrestore(&d->lock, flags);
  949. return;
  950. }
  951. d->fw_ver = be16_to_cpu(ch->fwver);
  952. sl = aoecmd_ata_id(d);
  953. spin_unlock_irqrestore(&d->lock, flags);
  954. aoenet_xmit(sl);
  955. }
  956. void
  957. aoecmd_cleanslate(struct aoedev *d)
  958. {
  959. struct aoetgt **t, **te;
  960. struct aoeif *p, *e;
  961. d->mintimer = MINTIMER;
  962. t = d->targets;
  963. te = t + NTARGETS;
  964. for (; t < te && *t; t++) {
  965. (*t)->maxout = (*t)->nframes;
  966. p = (*t)->ifs;
  967. e = p + NAOEIFS;
  968. for (; p < e; p++) {
  969. p->lostjumbo = 0;
  970. p->lost = 0;
  971. p->maxbcnt = DEFAULTBCNT;
  972. }
  973. }
  974. }