aoecmd.c 23 KB

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