aoecmd.c 23 KB

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