aoecmd.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/ata.h>
  7. #include <linux/slab.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/genhd.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/kthread.h>
  16. #include <net/net_namespace.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/uio.h>
  19. #include "aoe.h"
  20. #define MAXIOC (8192) /* default meant to avoid most soft lockups */
  21. static void ktcomplete(struct frame *, struct sk_buff *);
  22. static int count_targets(struct aoedev *d, int *untainted);
  23. static struct buf *nextbuf(struct aoedev *);
  24. static int aoe_deadsecs = 60 * 3;
  25. module_param(aoe_deadsecs, int, 0644);
  26. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  27. static int aoe_maxout = 64;
  28. module_param(aoe_maxout, int, 0644);
  29. MODULE_PARM_DESC(aoe_maxout,
  30. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  31. static wait_queue_head_t ktiowq;
  32. static struct ktstate kts;
  33. /* io completion queue */
  34. static struct {
  35. struct list_head head;
  36. spinlock_t lock;
  37. } iocq;
  38. static struct page *empty_page;
  39. static struct sk_buff *
  40. new_skb(ulong len)
  41. {
  42. struct sk_buff *skb;
  43. skb = alloc_skb(len, GFP_ATOMIC);
  44. if (skb) {
  45. skb_reset_mac_header(skb);
  46. skb_reset_network_header(skb);
  47. skb->protocol = __constant_htons(ETH_P_AOE);
  48. skb_checksum_none_assert(skb);
  49. }
  50. return skb;
  51. }
  52. static struct frame *
  53. getframe_deferred(struct aoedev *d, u32 tag)
  54. {
  55. struct list_head *head, *pos, *nx;
  56. struct frame *f;
  57. head = &d->rexmitq;
  58. list_for_each_safe(pos, nx, head) {
  59. f = list_entry(pos, struct frame, head);
  60. if (f->tag == tag) {
  61. list_del(pos);
  62. return f;
  63. }
  64. }
  65. return NULL;
  66. }
  67. static struct frame *
  68. getframe(struct aoedev *d, u32 tag)
  69. {
  70. struct frame *f;
  71. struct list_head *head, *pos, *nx;
  72. u32 n;
  73. n = tag % NFACTIVE;
  74. head = &d->factive[n];
  75. list_for_each_safe(pos, nx, head) {
  76. f = list_entry(pos, struct frame, head);
  77. if (f->tag == tag) {
  78. list_del(pos);
  79. return f;
  80. }
  81. }
  82. return NULL;
  83. }
  84. /*
  85. * Leave the top bit clear so we have tagspace for userland.
  86. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  87. * This driver reserves tag -1 to mean "unused frame."
  88. */
  89. static int
  90. newtag(struct aoedev *d)
  91. {
  92. register ulong n;
  93. n = jiffies & 0xffff;
  94. return n |= (++d->lasttag & 0x7fff) << 16;
  95. }
  96. static u32
  97. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  98. {
  99. u32 host_tag = newtag(d);
  100. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  101. memcpy(h->dst, t->addr, sizeof h->dst);
  102. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  103. h->verfl = AOE_HVER;
  104. h->major = cpu_to_be16(d->aoemajor);
  105. h->minor = d->aoeminor;
  106. h->cmd = AOECMD_ATA;
  107. h->tag = cpu_to_be32(host_tag);
  108. return host_tag;
  109. }
  110. static inline void
  111. put_lba(struct aoe_atahdr *ah, sector_t lba)
  112. {
  113. ah->lba0 = lba;
  114. ah->lba1 = lba >>= 8;
  115. ah->lba2 = lba >>= 8;
  116. ah->lba3 = lba >>= 8;
  117. ah->lba4 = lba >>= 8;
  118. ah->lba5 = lba >>= 8;
  119. }
  120. static struct aoeif *
  121. ifrotate(struct aoetgt *t)
  122. {
  123. struct aoeif *ifp;
  124. ifp = t->ifp;
  125. ifp++;
  126. if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
  127. ifp = t->ifs;
  128. if (ifp->nd == NULL)
  129. return NULL;
  130. return t->ifp = ifp;
  131. }
  132. static void
  133. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  134. {
  135. __skb_queue_tail(&d->skbpool, skb);
  136. }
  137. static struct sk_buff *
  138. skb_pool_get(struct aoedev *d)
  139. {
  140. struct sk_buff *skb = skb_peek(&d->skbpool);
  141. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  142. __skb_unlink(skb, &d->skbpool);
  143. return skb;
  144. }
  145. if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
  146. (skb = new_skb(ETH_ZLEN)))
  147. return skb;
  148. return NULL;
  149. }
  150. void
  151. aoe_freetframe(struct frame *f)
  152. {
  153. struct aoetgt *t;
  154. t = f->t;
  155. f->buf = NULL;
  156. f->lba = 0;
  157. f->bv = NULL;
  158. f->r_skb = NULL;
  159. f->flags = 0;
  160. list_add(&f->head, &t->ffree);
  161. }
  162. static struct frame *
  163. newtframe(struct aoedev *d, struct aoetgt *t)
  164. {
  165. struct frame *f;
  166. struct sk_buff *skb;
  167. struct list_head *pos;
  168. if (list_empty(&t->ffree)) {
  169. if (t->falloc >= NSKBPOOLMAX*2)
  170. return NULL;
  171. f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
  172. if (f == NULL)
  173. return NULL;
  174. t->falloc++;
  175. f->t = t;
  176. } else {
  177. pos = t->ffree.next;
  178. list_del(pos);
  179. f = list_entry(pos, struct frame, head);
  180. }
  181. skb = f->skb;
  182. if (skb == NULL) {
  183. f->skb = skb = new_skb(ETH_ZLEN);
  184. if (!skb) {
  185. bail: aoe_freetframe(f);
  186. return NULL;
  187. }
  188. }
  189. if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
  190. skb = skb_pool_get(d);
  191. if (skb == NULL)
  192. goto bail;
  193. skb_pool_put(d, f->skb);
  194. f->skb = skb;
  195. }
  196. skb->truesize -= skb->data_len;
  197. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  198. skb_trim(skb, 0);
  199. return f;
  200. }
  201. static struct frame *
  202. newframe(struct aoedev *d)
  203. {
  204. struct frame *f;
  205. struct aoetgt *t, **tt;
  206. int totout = 0;
  207. int use_tainted;
  208. int has_untainted;
  209. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  210. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  211. return NULL;
  212. }
  213. tt = d->tgt; /* last used target */
  214. for (use_tainted = 0, has_untainted = 0;;) {
  215. tt++;
  216. if (tt >= &d->targets[NTARGETS] || !*tt)
  217. tt = d->targets;
  218. t = *tt;
  219. if (!t->taint) {
  220. has_untainted = 1;
  221. totout += t->nout;
  222. }
  223. if (t->nout < t->maxout
  224. && (use_tainted || !t->taint)
  225. && t->ifp->nd) {
  226. f = newtframe(d, t);
  227. if (f) {
  228. ifrotate(t);
  229. d->tgt = tt;
  230. return f;
  231. }
  232. }
  233. if (tt == d->tgt) { /* we've looped and found nada */
  234. if (!use_tainted && !has_untainted)
  235. use_tainted = 1;
  236. else
  237. break;
  238. }
  239. }
  240. if (totout == 0) {
  241. d->kicked++;
  242. d->flags |= DEVFL_KICKME;
  243. }
  244. return NULL;
  245. }
  246. static void
  247. skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
  248. {
  249. int frag = 0;
  250. ulong fcnt;
  251. loop:
  252. fcnt = bv->bv_len - (off - bv->bv_offset);
  253. if (fcnt > cnt)
  254. fcnt = cnt;
  255. skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
  256. cnt -= fcnt;
  257. if (cnt <= 0)
  258. return;
  259. bv++;
  260. off = bv->bv_offset;
  261. goto loop;
  262. }
  263. static void
  264. fhash(struct frame *f)
  265. {
  266. struct aoedev *d = f->t->d;
  267. u32 n;
  268. n = f->tag % NFACTIVE;
  269. list_add_tail(&f->head, &d->factive[n]);
  270. }
  271. static void
  272. ata_rw_frameinit(struct frame *f)
  273. {
  274. struct aoetgt *t;
  275. struct aoe_hdr *h;
  276. struct aoe_atahdr *ah;
  277. struct sk_buff *skb;
  278. char writebit, extbit;
  279. skb = f->skb;
  280. h = (struct aoe_hdr *) skb_mac_header(skb);
  281. ah = (struct aoe_atahdr *) (h + 1);
  282. skb_put(skb, sizeof(*h) + sizeof(*ah));
  283. memset(h, 0, skb->len);
  284. writebit = 0x10;
  285. extbit = 0x4;
  286. t = f->t;
  287. f->tag = aoehdr_atainit(t->d, t, h);
  288. fhash(f);
  289. t->nout++;
  290. f->waited = 0;
  291. f->waited_total = 0;
  292. if (f->buf)
  293. f->lba = f->buf->sector;
  294. /* set up ata header */
  295. ah->scnt = f->bcnt >> 9;
  296. put_lba(ah, f->lba);
  297. if (t->d->flags & DEVFL_EXT) {
  298. ah->aflags |= AOEAFL_EXT;
  299. } else {
  300. extbit = 0;
  301. ah->lba3 &= 0x0f;
  302. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  303. }
  304. if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
  305. skb_fillup(skb, f->bv, f->bv_off, f->bcnt);
  306. ah->aflags |= AOEAFL_WRITE;
  307. skb->len += f->bcnt;
  308. skb->data_len = f->bcnt;
  309. skb->truesize += f->bcnt;
  310. t->wpkts++;
  311. } else {
  312. t->rpkts++;
  313. writebit = 0;
  314. }
  315. ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
  316. skb->dev = t->ifp->nd;
  317. }
  318. static int
  319. aoecmd_ata_rw(struct aoedev *d)
  320. {
  321. struct frame *f;
  322. struct buf *buf;
  323. struct aoetgt *t;
  324. struct sk_buff *skb;
  325. struct sk_buff_head queue;
  326. ulong bcnt, fbcnt;
  327. buf = nextbuf(d);
  328. if (buf == NULL)
  329. return 0;
  330. f = newframe(d);
  331. if (f == NULL)
  332. return 0;
  333. t = *d->tgt;
  334. bcnt = d->maxbcnt;
  335. if (bcnt == 0)
  336. bcnt = DEFAULTBCNT;
  337. if (bcnt > buf->resid)
  338. bcnt = buf->resid;
  339. fbcnt = bcnt;
  340. f->bv = buf->bv;
  341. f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
  342. do {
  343. if (fbcnt < buf->bv_resid) {
  344. buf->bv_resid -= fbcnt;
  345. buf->resid -= fbcnt;
  346. break;
  347. }
  348. fbcnt -= buf->bv_resid;
  349. buf->resid -= buf->bv_resid;
  350. if (buf->resid == 0) {
  351. d->ip.buf = NULL;
  352. break;
  353. }
  354. buf->bv++;
  355. buf->bv_resid = buf->bv->bv_len;
  356. WARN_ON(buf->bv_resid == 0);
  357. } while (fbcnt);
  358. /* initialize the headers & frame */
  359. f->buf = buf;
  360. f->bcnt = bcnt;
  361. ata_rw_frameinit(f);
  362. /* mark all tracking fields and load out */
  363. buf->nframesout += 1;
  364. buf->sector += bcnt >> 9;
  365. skb = skb_clone(f->skb, GFP_ATOMIC);
  366. if (skb) {
  367. do_gettimeofday(&f->sent);
  368. f->sent_jiffs = (u32) jiffies;
  369. __skb_queue_head_init(&queue);
  370. __skb_queue_tail(&queue, skb);
  371. aoenet_xmit(&queue);
  372. }
  373. return 1;
  374. }
  375. /* some callers cannot sleep, and they can call this function,
  376. * transmitting the packets later, when interrupts are on
  377. */
  378. static void
  379. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
  380. {
  381. struct aoe_hdr *h;
  382. struct aoe_cfghdr *ch;
  383. struct sk_buff *skb;
  384. struct net_device *ifp;
  385. rcu_read_lock();
  386. for_each_netdev_rcu(&init_net, ifp) {
  387. dev_hold(ifp);
  388. if (!is_aoe_netif(ifp))
  389. goto cont;
  390. skb = new_skb(sizeof *h + sizeof *ch);
  391. if (skb == NULL) {
  392. printk(KERN_INFO "aoe: skb alloc failure\n");
  393. goto cont;
  394. }
  395. skb_put(skb, sizeof *h + sizeof *ch);
  396. skb->dev = ifp;
  397. __skb_queue_tail(queue, skb);
  398. h = (struct aoe_hdr *) skb_mac_header(skb);
  399. memset(h, 0, sizeof *h + sizeof *ch);
  400. memset(h->dst, 0xff, sizeof h->dst);
  401. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  402. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  403. h->verfl = AOE_HVER;
  404. h->major = cpu_to_be16(aoemajor);
  405. h->minor = aoeminor;
  406. h->cmd = AOECMD_CFG;
  407. cont:
  408. dev_put(ifp);
  409. }
  410. rcu_read_unlock();
  411. }
  412. static void
  413. resend(struct aoedev *d, struct frame *f)
  414. {
  415. struct sk_buff *skb;
  416. struct sk_buff_head queue;
  417. struct aoe_hdr *h;
  418. struct aoe_atahdr *ah;
  419. struct aoetgt *t;
  420. char buf[128];
  421. u32 n;
  422. t = f->t;
  423. n = newtag(d);
  424. skb = f->skb;
  425. if (ifrotate(t) == NULL) {
  426. /* probably can't happen, but set it up to fail anyway */
  427. pr_info("aoe: resend: no interfaces to rotate to.\n");
  428. ktcomplete(f, NULL);
  429. return;
  430. }
  431. h = (struct aoe_hdr *) skb_mac_header(skb);
  432. ah = (struct aoe_atahdr *) (h+1);
  433. if (!(f->flags & FFL_PROBE)) {
  434. snprintf(buf, sizeof(buf),
  435. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
  436. "retransmit", d->aoemajor, d->aoeminor,
  437. f->tag, jiffies, n,
  438. h->src, h->dst, t->nout);
  439. aoechr_error(buf);
  440. }
  441. f->tag = n;
  442. fhash(f);
  443. h->tag = cpu_to_be32(n);
  444. memcpy(h->dst, t->addr, sizeof h->dst);
  445. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  446. skb->dev = t->ifp->nd;
  447. skb = skb_clone(skb, GFP_ATOMIC);
  448. if (skb == NULL)
  449. return;
  450. do_gettimeofday(&f->sent);
  451. f->sent_jiffs = (u32) jiffies;
  452. __skb_queue_head_init(&queue);
  453. __skb_queue_tail(&queue, skb);
  454. aoenet_xmit(&queue);
  455. }
  456. static int
  457. tsince_hr(struct frame *f)
  458. {
  459. struct timeval now;
  460. int n;
  461. do_gettimeofday(&now);
  462. n = now.tv_usec - f->sent.tv_usec;
  463. n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
  464. if (n < 0)
  465. n = -n;
  466. /* For relatively long periods, use jiffies to avoid
  467. * discrepancies caused by updates to the system time.
  468. *
  469. * On system with HZ of 1000, 32-bits is over 49 days
  470. * worth of jiffies, or over 71 minutes worth of usecs.
  471. *
  472. * Jiffies overflow is handled by subtraction of unsigned ints:
  473. * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
  474. * $3 = 4
  475. * (gdb)
  476. */
  477. if (n > USEC_PER_SEC / 4) {
  478. n = ((u32) jiffies) - f->sent_jiffs;
  479. n *= USEC_PER_SEC / HZ;
  480. }
  481. return n;
  482. }
  483. static int
  484. tsince(u32 tag)
  485. {
  486. int n;
  487. n = jiffies & 0xffff;
  488. n -= tag & 0xffff;
  489. if (n < 0)
  490. n += 1<<16;
  491. return jiffies_to_usecs(n + 1);
  492. }
  493. static struct aoeif *
  494. getif(struct aoetgt *t, struct net_device *nd)
  495. {
  496. struct aoeif *p, *e;
  497. p = t->ifs;
  498. e = p + NAOEIFS;
  499. for (; p < e; p++)
  500. if (p->nd == nd)
  501. return p;
  502. return NULL;
  503. }
  504. static void
  505. ejectif(struct aoetgt *t, struct aoeif *ifp)
  506. {
  507. struct aoeif *e;
  508. struct net_device *nd;
  509. ulong n;
  510. nd = ifp->nd;
  511. e = t->ifs + NAOEIFS - 1;
  512. n = (e - ifp) * sizeof *ifp;
  513. memmove(ifp, ifp+1, n);
  514. e->nd = NULL;
  515. dev_put(nd);
  516. }
  517. static struct frame *
  518. reassign_frame(struct frame *f)
  519. {
  520. struct frame *nf;
  521. struct sk_buff *skb;
  522. nf = newframe(f->t->d);
  523. if (!nf)
  524. return NULL;
  525. if (nf->t == f->t) {
  526. aoe_freetframe(nf);
  527. return NULL;
  528. }
  529. skb = nf->skb;
  530. nf->skb = f->skb;
  531. nf->buf = f->buf;
  532. nf->bcnt = f->bcnt;
  533. nf->lba = f->lba;
  534. nf->bv = f->bv;
  535. nf->bv_off = f->bv_off;
  536. nf->waited = 0;
  537. nf->waited_total = f->waited_total;
  538. nf->sent = f->sent;
  539. nf->sent_jiffs = f->sent_jiffs;
  540. f->skb = skb;
  541. return nf;
  542. }
  543. static void
  544. probe(struct aoetgt *t)
  545. {
  546. struct aoedev *d;
  547. struct frame *f;
  548. struct sk_buff *skb;
  549. struct sk_buff_head queue;
  550. size_t n, m;
  551. int frag;
  552. d = t->d;
  553. f = newtframe(d, t);
  554. if (!f) {
  555. pr_err("%s %pm for e%ld.%d: %s\n",
  556. "aoe: cannot probe remote address",
  557. t->addr,
  558. (long) d->aoemajor, d->aoeminor,
  559. "no frame available");
  560. return;
  561. }
  562. f->flags |= FFL_PROBE;
  563. ifrotate(t);
  564. f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
  565. ata_rw_frameinit(f);
  566. skb = f->skb;
  567. for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
  568. if (n < PAGE_SIZE)
  569. m = n;
  570. else
  571. m = PAGE_SIZE;
  572. skb_fill_page_desc(skb, frag, empty_page, 0, m);
  573. }
  574. skb->len += f->bcnt;
  575. skb->data_len = f->bcnt;
  576. skb->truesize += f->bcnt;
  577. skb = skb_clone(f->skb, GFP_ATOMIC);
  578. if (skb) {
  579. do_gettimeofday(&f->sent);
  580. f->sent_jiffs = (u32) jiffies;
  581. __skb_queue_head_init(&queue);
  582. __skb_queue_tail(&queue, skb);
  583. aoenet_xmit(&queue);
  584. }
  585. }
  586. static long
  587. rto(struct aoedev *d)
  588. {
  589. long t;
  590. t = 2 * d->rttavg >> RTTSCALE;
  591. t += 8 * d->rttdev >> RTTDSCALE;
  592. if (t == 0)
  593. t = 1;
  594. return t;
  595. }
  596. static void
  597. rexmit_deferred(struct aoedev *d)
  598. {
  599. struct aoetgt *t;
  600. struct frame *f;
  601. struct frame *nf;
  602. struct list_head *pos, *nx, *head;
  603. int since;
  604. int untainted;
  605. count_targets(d, &untainted);
  606. head = &d->rexmitq;
  607. list_for_each_safe(pos, nx, head) {
  608. f = list_entry(pos, struct frame, head);
  609. t = f->t;
  610. if (t->taint) {
  611. if (!(f->flags & FFL_PROBE)) {
  612. nf = reassign_frame(f);
  613. if (nf) {
  614. if (t->nout_probes == 0
  615. && untainted > 0) {
  616. probe(t);
  617. t->nout_probes++;
  618. }
  619. list_replace(&f->head, &nf->head);
  620. pos = &nf->head;
  621. aoe_freetframe(f);
  622. f = nf;
  623. t = f->t;
  624. }
  625. } else if (untainted < 1) {
  626. /* don't probe w/o other untainted aoetgts */
  627. goto stop_probe;
  628. } else if (tsince_hr(f) < t->taint * rto(d)) {
  629. /* reprobe slowly when taint is high */
  630. continue;
  631. }
  632. } else if (f->flags & FFL_PROBE) {
  633. stop_probe: /* don't probe untainted aoetgts */
  634. list_del(pos);
  635. aoe_freetframe(f);
  636. /* leaving d->kicked, because this is routine */
  637. f->t->d->flags |= DEVFL_KICKME;
  638. continue;
  639. }
  640. if (t->nout >= t->maxout)
  641. continue;
  642. list_del(pos);
  643. t->nout++;
  644. if (f->flags & FFL_PROBE)
  645. t->nout_probes++;
  646. since = tsince_hr(f);
  647. f->waited += since;
  648. f->waited_total += since;
  649. resend(d, f);
  650. }
  651. }
  652. /* An aoetgt accumulates demerits quickly, and successful
  653. * probing redeems the aoetgt slowly.
  654. */
  655. static void
  656. scorn(struct aoetgt *t)
  657. {
  658. int n;
  659. n = t->taint++;
  660. t->taint += t->taint * 2;
  661. if (n > t->taint)
  662. t->taint = n;
  663. if (t->taint > MAX_TAINT)
  664. t->taint = MAX_TAINT;
  665. }
  666. static int
  667. count_targets(struct aoedev *d, int *untainted)
  668. {
  669. int i, good;
  670. for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
  671. if (d->targets[i]->taint == 0)
  672. good++;
  673. if (untainted)
  674. *untainted = good;
  675. return i;
  676. }
  677. static void
  678. rexmit_timer(ulong vp)
  679. {
  680. struct aoedev *d;
  681. struct aoetgt *t;
  682. struct aoeif *ifp;
  683. struct frame *f;
  684. struct list_head *head, *pos, *nx;
  685. LIST_HEAD(flist);
  686. register long timeout;
  687. ulong flags, n;
  688. int i;
  689. int utgts; /* number of aoetgt descriptors (not slots) */
  690. int since;
  691. d = (struct aoedev *) vp;
  692. spin_lock_irqsave(&d->lock, flags);
  693. /* timeout based on observed timings and variations */
  694. timeout = rto(d);
  695. utgts = count_targets(d, NULL);
  696. if (d->flags & DEVFL_TKILL) {
  697. spin_unlock_irqrestore(&d->lock, flags);
  698. return;
  699. }
  700. /* collect all frames to rexmit into flist */
  701. for (i = 0; i < NFACTIVE; i++) {
  702. head = &d->factive[i];
  703. list_for_each_safe(pos, nx, head) {
  704. f = list_entry(pos, struct frame, head);
  705. if (tsince_hr(f) < timeout)
  706. break; /* end of expired frames */
  707. /* move to flist for later processing */
  708. list_move_tail(pos, &flist);
  709. }
  710. }
  711. /* process expired frames */
  712. while (!list_empty(&flist)) {
  713. pos = flist.next;
  714. f = list_entry(pos, struct frame, head);
  715. since = tsince_hr(f);
  716. n = f->waited_total + since;
  717. n /= USEC_PER_SEC;
  718. if (n > aoe_deadsecs && !(f->flags & FFL_PROBE)) {
  719. /* Waited too long. Device failure.
  720. * Hang all frames on first hash bucket for downdev
  721. * to clean up.
  722. */
  723. list_splice(&flist, &d->factive[0]);
  724. aoedev_downdev(d);
  725. goto out;
  726. }
  727. t = f->t;
  728. n = f->waited + since;
  729. n /= USEC_PER_SEC;
  730. if (aoe_deadsecs && utgts > 0
  731. && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
  732. scorn(t); /* avoid this target */
  733. if (t->maxout != 1) {
  734. t->ssthresh = t->maxout / 2;
  735. t->maxout = 1;
  736. }
  737. if (f->flags & FFL_PROBE) {
  738. t->nout_probes--;
  739. } else {
  740. ifp = getif(t, f->skb->dev);
  741. if (ifp && ++ifp->lost > (t->nframes << 1)
  742. && (ifp != t->ifs || t->ifs[1].nd)) {
  743. ejectif(t, ifp);
  744. ifp = NULL;
  745. }
  746. }
  747. list_move_tail(pos, &d->rexmitq);
  748. t->nout--;
  749. }
  750. rexmit_deferred(d);
  751. out:
  752. if ((d->flags & DEVFL_KICKME) && d->blkq) {
  753. d->flags &= ~DEVFL_KICKME;
  754. d->blkq->request_fn(d->blkq);
  755. }
  756. d->timer.expires = jiffies + TIMERTICK;
  757. add_timer(&d->timer);
  758. spin_unlock_irqrestore(&d->lock, flags);
  759. }
  760. static unsigned long
  761. rqbiocnt(struct request *r)
  762. {
  763. struct bio *bio;
  764. unsigned long n = 0;
  765. __rq_for_each_bio(bio, r)
  766. n++;
  767. return n;
  768. }
  769. /* This can be removed if we are certain that no users of the block
  770. * layer will ever use zero-count pages in bios. Otherwise we have to
  771. * protect against the put_page sometimes done by the network layer.
  772. *
  773. * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
  774. * discussion.
  775. *
  776. * We cannot use get_page in the workaround, because it insists on a
  777. * positive page count as a precondition. So we use _count directly.
  778. */
  779. static void
  780. bio_pageinc(struct bio *bio)
  781. {
  782. struct bio_vec *bv;
  783. struct page *page;
  784. int i;
  785. bio_for_each_segment(bv, bio, i) {
  786. page = bv->bv_page;
  787. /* Non-zero page count for non-head members of
  788. * compound pages is no longer allowed by the kernel,
  789. * but this has never been seen here.
  790. */
  791. if (unlikely(PageCompound(page)))
  792. if (compound_trans_head(page) != page) {
  793. pr_crit("page tail used for block I/O\n");
  794. BUG();
  795. }
  796. atomic_inc(&page->_count);
  797. }
  798. }
  799. static void
  800. bio_pagedec(struct bio *bio)
  801. {
  802. struct bio_vec *bv;
  803. int i;
  804. bio_for_each_segment(bv, bio, i)
  805. atomic_dec(&bv->bv_page->_count);
  806. }
  807. static void
  808. bufinit(struct buf *buf, struct request *rq, struct bio *bio)
  809. {
  810. struct bio_vec *bv;
  811. memset(buf, 0, sizeof(*buf));
  812. buf->rq = rq;
  813. buf->bio = bio;
  814. buf->resid = bio->bi_size;
  815. buf->sector = bio->bi_sector;
  816. bio_pageinc(bio);
  817. buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
  818. buf->bv_resid = bv->bv_len;
  819. WARN_ON(buf->bv_resid == 0);
  820. }
  821. static struct buf *
  822. nextbuf(struct aoedev *d)
  823. {
  824. struct request *rq;
  825. struct request_queue *q;
  826. struct buf *buf;
  827. struct bio *bio;
  828. q = d->blkq;
  829. if (q == NULL)
  830. return NULL; /* initializing */
  831. if (d->ip.buf)
  832. return d->ip.buf;
  833. rq = d->ip.rq;
  834. if (rq == NULL) {
  835. rq = blk_peek_request(q);
  836. if (rq == NULL)
  837. return NULL;
  838. blk_start_request(rq);
  839. d->ip.rq = rq;
  840. d->ip.nxbio = rq->bio;
  841. rq->special = (void *) rqbiocnt(rq);
  842. }
  843. buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
  844. if (buf == NULL) {
  845. pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
  846. return NULL;
  847. }
  848. bio = d->ip.nxbio;
  849. bufinit(buf, rq, bio);
  850. bio = bio->bi_next;
  851. d->ip.nxbio = bio;
  852. if (bio == NULL)
  853. d->ip.rq = NULL;
  854. return d->ip.buf = buf;
  855. }
  856. /* enters with d->lock held */
  857. void
  858. aoecmd_work(struct aoedev *d)
  859. {
  860. rexmit_deferred(d);
  861. while (aoecmd_ata_rw(d))
  862. ;
  863. }
  864. /* this function performs work that has been deferred until sleeping is OK
  865. */
  866. void
  867. aoecmd_sleepwork(struct work_struct *work)
  868. {
  869. struct aoedev *d = container_of(work, struct aoedev, work);
  870. struct block_device *bd;
  871. u64 ssize;
  872. if (d->flags & DEVFL_GDALLOC)
  873. aoeblk_gdalloc(d);
  874. if (d->flags & DEVFL_NEWSIZE) {
  875. ssize = get_capacity(d->gd);
  876. bd = bdget_disk(d->gd, 0);
  877. if (bd) {
  878. mutex_lock(&bd->bd_inode->i_mutex);
  879. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  880. mutex_unlock(&bd->bd_inode->i_mutex);
  881. bdput(bd);
  882. }
  883. spin_lock_irq(&d->lock);
  884. d->flags |= DEVFL_UP;
  885. d->flags &= ~DEVFL_NEWSIZE;
  886. spin_unlock_irq(&d->lock);
  887. }
  888. }
  889. static void
  890. ata_ident_fixstring(u16 *id, int ns)
  891. {
  892. u16 s;
  893. while (ns-- > 0) {
  894. s = *id;
  895. *id++ = s >> 8 | s << 8;
  896. }
  897. }
  898. static void
  899. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  900. {
  901. u64 ssize;
  902. u16 n;
  903. /* word 83: command set supported */
  904. n = get_unaligned_le16(&id[83 << 1]);
  905. /* word 86: command set/feature enabled */
  906. n |= get_unaligned_le16(&id[86 << 1]);
  907. if (n & (1<<10)) { /* bit 10: LBA 48 */
  908. d->flags |= DEVFL_EXT;
  909. /* word 100: number lba48 sectors */
  910. ssize = get_unaligned_le64(&id[100 << 1]);
  911. /* set as in ide-disk.c:init_idedisk_capacity */
  912. d->geo.cylinders = ssize;
  913. d->geo.cylinders /= (255 * 63);
  914. d->geo.heads = 255;
  915. d->geo.sectors = 63;
  916. } else {
  917. d->flags &= ~DEVFL_EXT;
  918. /* number lba28 sectors */
  919. ssize = get_unaligned_le32(&id[60 << 1]);
  920. /* NOTE: obsolete in ATA 6 */
  921. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  922. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  923. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  924. }
  925. ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
  926. ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
  927. ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
  928. memcpy(d->ident, id, sizeof(d->ident));
  929. if (d->ssize != ssize)
  930. printk(KERN_INFO
  931. "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
  932. t->addr,
  933. d->aoemajor, d->aoeminor,
  934. d->fw_ver, (long long)ssize);
  935. d->ssize = ssize;
  936. d->geo.start = 0;
  937. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  938. return;
  939. if (d->gd != NULL) {
  940. set_capacity(d->gd, ssize);
  941. d->flags |= DEVFL_NEWSIZE;
  942. } else
  943. d->flags |= DEVFL_GDALLOC;
  944. schedule_work(&d->work);
  945. }
  946. static void
  947. calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
  948. {
  949. register long n;
  950. n = rtt;
  951. /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
  952. n -= d->rttavg >> RTTSCALE;
  953. d->rttavg += n;
  954. if (n < 0)
  955. n = -n;
  956. n -= d->rttdev >> RTTDSCALE;
  957. d->rttdev += n;
  958. if (!t || t->maxout >= t->nframes)
  959. return;
  960. if (t->maxout < t->ssthresh)
  961. t->maxout += 1;
  962. else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
  963. t->maxout += 1;
  964. t->next_cwnd = t->maxout;
  965. }
  966. }
  967. static struct aoetgt *
  968. gettgt(struct aoedev *d, char *addr)
  969. {
  970. struct aoetgt **t, **e;
  971. t = d->targets;
  972. e = t + NTARGETS;
  973. for (; t < e && *t; t++)
  974. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  975. return *t;
  976. return NULL;
  977. }
  978. static void
  979. bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
  980. {
  981. ulong fcnt;
  982. char *p;
  983. int soff = 0;
  984. loop:
  985. fcnt = bv->bv_len - (off - bv->bv_offset);
  986. if (fcnt > cnt)
  987. fcnt = cnt;
  988. p = page_address(bv->bv_page) + off;
  989. skb_copy_bits(skb, soff, p, fcnt);
  990. soff += fcnt;
  991. cnt -= fcnt;
  992. if (cnt <= 0)
  993. return;
  994. bv++;
  995. off = bv->bv_offset;
  996. goto loop;
  997. }
  998. void
  999. aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
  1000. {
  1001. struct bio *bio;
  1002. int bok;
  1003. struct request_queue *q;
  1004. q = d->blkq;
  1005. if (rq == d->ip.rq)
  1006. d->ip.rq = NULL;
  1007. do {
  1008. bio = rq->bio;
  1009. bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
  1010. } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
  1011. /* cf. http://lkml.org/lkml/2006/10/31/28 */
  1012. if (!fastfail)
  1013. __blk_run_queue(q);
  1014. }
  1015. static void
  1016. aoe_end_buf(struct aoedev *d, struct buf *buf)
  1017. {
  1018. struct request *rq;
  1019. unsigned long n;
  1020. if (buf == d->ip.buf)
  1021. d->ip.buf = NULL;
  1022. rq = buf->rq;
  1023. bio_pagedec(buf->bio);
  1024. mempool_free(buf, d->bufpool);
  1025. n = (unsigned long) rq->special;
  1026. rq->special = (void *) --n;
  1027. if (n == 0)
  1028. aoe_end_request(d, rq, 0);
  1029. }
  1030. static void
  1031. ktiocomplete(struct frame *f)
  1032. {
  1033. struct aoe_hdr *hin, *hout;
  1034. struct aoe_atahdr *ahin, *ahout;
  1035. struct buf *buf;
  1036. struct sk_buff *skb;
  1037. struct aoetgt *t;
  1038. struct aoeif *ifp;
  1039. struct aoedev *d;
  1040. long n;
  1041. int untainted;
  1042. if (f == NULL)
  1043. return;
  1044. t = f->t;
  1045. d = t->d;
  1046. skb = f->r_skb;
  1047. buf = f->buf;
  1048. if (f->flags & FFL_PROBE)
  1049. goto out;
  1050. if (!skb) /* just fail the buf. */
  1051. goto noskb;
  1052. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  1053. ahout = (struct aoe_atahdr *) (hout+1);
  1054. hin = (struct aoe_hdr *) skb->data;
  1055. skb_pull(skb, sizeof(*hin));
  1056. ahin = (struct aoe_atahdr *) skb->data;
  1057. skb_pull(skb, sizeof(*ahin));
  1058. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  1059. pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  1060. ahout->cmdstat, ahin->cmdstat,
  1061. d->aoemajor, d->aoeminor);
  1062. noskb: if (buf)
  1063. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1064. goto out;
  1065. }
  1066. n = ahout->scnt << 9;
  1067. switch (ahout->cmdstat) {
  1068. case ATA_CMD_PIO_READ:
  1069. case ATA_CMD_PIO_READ_EXT:
  1070. if (skb->len < n) {
  1071. pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
  1072. skb->len, n);
  1073. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1074. break;
  1075. }
  1076. bvcpy(f->bv, f->bv_off, skb, n);
  1077. case ATA_CMD_PIO_WRITE:
  1078. case ATA_CMD_PIO_WRITE_EXT:
  1079. spin_lock_irq(&d->lock);
  1080. ifp = getif(t, skb->dev);
  1081. if (ifp)
  1082. ifp->lost = 0;
  1083. spin_unlock_irq(&d->lock);
  1084. break;
  1085. case ATA_CMD_ID_ATA:
  1086. if (skb->len < 512) {
  1087. pr_info("aoe: runt data size in ataid. skb->len=%d\n",
  1088. skb->len);
  1089. break;
  1090. }
  1091. if (skb_linearize(skb))
  1092. break;
  1093. spin_lock_irq(&d->lock);
  1094. ataid_complete(d, t, skb->data);
  1095. spin_unlock_irq(&d->lock);
  1096. break;
  1097. default:
  1098. pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  1099. ahout->cmdstat,
  1100. be16_to_cpu(get_unaligned(&hin->major)),
  1101. hin->minor);
  1102. }
  1103. out:
  1104. spin_lock_irq(&d->lock);
  1105. if (t->taint > 0
  1106. && --t->taint > 0
  1107. && t->nout_probes == 0) {
  1108. count_targets(d, &untainted);
  1109. if (untainted > 0) {
  1110. probe(t);
  1111. t->nout_probes++;
  1112. }
  1113. }
  1114. aoe_freetframe(f);
  1115. if (buf && --buf->nframesout == 0 && buf->resid == 0)
  1116. aoe_end_buf(d, buf);
  1117. spin_unlock_irq(&d->lock);
  1118. aoedev_put(d);
  1119. dev_kfree_skb(skb);
  1120. }
  1121. /* Enters with iocq.lock held.
  1122. * Returns true iff responses needing processing remain.
  1123. */
  1124. static int
  1125. ktio(void)
  1126. {
  1127. struct frame *f;
  1128. struct list_head *pos;
  1129. int i;
  1130. for (i = 0; ; ++i) {
  1131. if (i == MAXIOC)
  1132. return 1;
  1133. if (list_empty(&iocq.head))
  1134. return 0;
  1135. pos = iocq.head.next;
  1136. list_del(pos);
  1137. spin_unlock_irq(&iocq.lock);
  1138. f = list_entry(pos, struct frame, head);
  1139. ktiocomplete(f);
  1140. spin_lock_irq(&iocq.lock);
  1141. }
  1142. }
  1143. static int
  1144. kthread(void *vp)
  1145. {
  1146. struct ktstate *k;
  1147. DECLARE_WAITQUEUE(wait, current);
  1148. int more;
  1149. k = vp;
  1150. current->flags |= PF_NOFREEZE;
  1151. set_user_nice(current, -10);
  1152. complete(&k->rendez); /* tell spawner we're running */
  1153. do {
  1154. spin_lock_irq(k->lock);
  1155. more = k->fn();
  1156. if (!more) {
  1157. add_wait_queue(k->waitq, &wait);
  1158. __set_current_state(TASK_INTERRUPTIBLE);
  1159. }
  1160. spin_unlock_irq(k->lock);
  1161. if (!more) {
  1162. schedule();
  1163. remove_wait_queue(k->waitq, &wait);
  1164. } else
  1165. cond_resched();
  1166. } while (!kthread_should_stop());
  1167. complete(&k->rendez); /* tell spawner we're stopping */
  1168. return 0;
  1169. }
  1170. void
  1171. aoe_ktstop(struct ktstate *k)
  1172. {
  1173. kthread_stop(k->task);
  1174. wait_for_completion(&k->rendez);
  1175. }
  1176. int
  1177. aoe_ktstart(struct ktstate *k)
  1178. {
  1179. struct task_struct *task;
  1180. init_completion(&k->rendez);
  1181. task = kthread_run(kthread, k, k->name);
  1182. if (task == NULL || IS_ERR(task))
  1183. return -ENOMEM;
  1184. k->task = task;
  1185. wait_for_completion(&k->rendez); /* allow kthread to start */
  1186. init_completion(&k->rendez); /* for waiting for exit later */
  1187. return 0;
  1188. }
  1189. /* pass it off to kthreads for processing */
  1190. static void
  1191. ktcomplete(struct frame *f, struct sk_buff *skb)
  1192. {
  1193. ulong flags;
  1194. f->r_skb = skb;
  1195. spin_lock_irqsave(&iocq.lock, flags);
  1196. list_add_tail(&f->head, &iocq.head);
  1197. spin_unlock_irqrestore(&iocq.lock, flags);
  1198. wake_up(&ktiowq);
  1199. }
  1200. struct sk_buff *
  1201. aoecmd_ata_rsp(struct sk_buff *skb)
  1202. {
  1203. struct aoedev *d;
  1204. struct aoe_hdr *h;
  1205. struct frame *f;
  1206. u32 n;
  1207. ulong flags;
  1208. char ebuf[128];
  1209. u16 aoemajor;
  1210. h = (struct aoe_hdr *) skb->data;
  1211. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  1212. d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
  1213. if (d == NULL) {
  1214. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  1215. "for unknown device %d.%d\n",
  1216. aoemajor, h->minor);
  1217. aoechr_error(ebuf);
  1218. return skb;
  1219. }
  1220. spin_lock_irqsave(&d->lock, flags);
  1221. n = be32_to_cpu(get_unaligned(&h->tag));
  1222. f = getframe(d, n);
  1223. if (f) {
  1224. calc_rttavg(d, f->t, tsince_hr(f));
  1225. f->t->nout--;
  1226. if (f->flags & FFL_PROBE)
  1227. f->t->nout_probes--;
  1228. } else {
  1229. f = getframe_deferred(d, n);
  1230. if (f) {
  1231. calc_rttavg(d, NULL, tsince_hr(f));
  1232. } else {
  1233. calc_rttavg(d, NULL, tsince(n));
  1234. spin_unlock_irqrestore(&d->lock, flags);
  1235. aoedev_put(d);
  1236. snprintf(ebuf, sizeof(ebuf),
  1237. "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
  1238. "unexpected rsp",
  1239. get_unaligned_be16(&h->major),
  1240. h->minor,
  1241. get_unaligned_be32(&h->tag),
  1242. jiffies,
  1243. h->src,
  1244. h->dst);
  1245. aoechr_error(ebuf);
  1246. return skb;
  1247. }
  1248. }
  1249. aoecmd_work(d);
  1250. spin_unlock_irqrestore(&d->lock, flags);
  1251. ktcomplete(f, skb);
  1252. /*
  1253. * Note here that we do not perform an aoedev_put, as we are
  1254. * leaving this reference for the ktio to release.
  1255. */
  1256. return NULL;
  1257. }
  1258. void
  1259. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  1260. {
  1261. struct sk_buff_head queue;
  1262. __skb_queue_head_init(&queue);
  1263. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  1264. aoenet_xmit(&queue);
  1265. }
  1266. struct sk_buff *
  1267. aoecmd_ata_id(struct aoedev *d)
  1268. {
  1269. struct aoe_hdr *h;
  1270. struct aoe_atahdr *ah;
  1271. struct frame *f;
  1272. struct sk_buff *skb;
  1273. struct aoetgt *t;
  1274. f = newframe(d);
  1275. if (f == NULL)
  1276. return NULL;
  1277. t = *d->tgt;
  1278. /* initialize the headers & frame */
  1279. skb = f->skb;
  1280. h = (struct aoe_hdr *) skb_mac_header(skb);
  1281. ah = (struct aoe_atahdr *) (h+1);
  1282. skb_put(skb, sizeof *h + sizeof *ah);
  1283. memset(h, 0, skb->len);
  1284. f->tag = aoehdr_atainit(d, t, h);
  1285. fhash(f);
  1286. t->nout++;
  1287. f->waited = 0;
  1288. f->waited_total = 0;
  1289. /* set up ata header */
  1290. ah->scnt = 1;
  1291. ah->cmdstat = ATA_CMD_ID_ATA;
  1292. ah->lba3 = 0xa0;
  1293. skb->dev = t->ifp->nd;
  1294. d->rttavg = RTTAVG_INIT;
  1295. d->rttdev = RTTDEV_INIT;
  1296. d->timer.function = rexmit_timer;
  1297. skb = skb_clone(skb, GFP_ATOMIC);
  1298. if (skb) {
  1299. do_gettimeofday(&f->sent);
  1300. f->sent_jiffs = (u32) jiffies;
  1301. }
  1302. return skb;
  1303. }
  1304. static struct aoetgt *
  1305. addtgt(struct aoedev *d, char *addr, ulong nframes)
  1306. {
  1307. struct aoetgt *t, **tt, **te;
  1308. tt = d->targets;
  1309. te = tt + NTARGETS;
  1310. for (; tt < te && *tt; tt++)
  1311. ;
  1312. if (tt == te) {
  1313. printk(KERN_INFO
  1314. "aoe: device addtgt failure; too many targets\n");
  1315. return NULL;
  1316. }
  1317. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  1318. if (!t) {
  1319. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  1320. return NULL;
  1321. }
  1322. d->ntargets++;
  1323. t->nframes = nframes;
  1324. t->d = d;
  1325. memcpy(t->addr, addr, sizeof t->addr);
  1326. t->ifp = t->ifs;
  1327. aoecmd_wreset(t);
  1328. t->maxout = t->nframes / 2;
  1329. INIT_LIST_HEAD(&t->ffree);
  1330. return *tt = t;
  1331. }
  1332. static void
  1333. setdbcnt(struct aoedev *d)
  1334. {
  1335. struct aoetgt **t, **e;
  1336. int bcnt = 0;
  1337. t = d->targets;
  1338. e = t + NTARGETS;
  1339. for (; t < e && *t; t++)
  1340. if (bcnt == 0 || bcnt > (*t)->minbcnt)
  1341. bcnt = (*t)->minbcnt;
  1342. if (bcnt != d->maxbcnt) {
  1343. d->maxbcnt = bcnt;
  1344. pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
  1345. d->aoemajor, d->aoeminor, bcnt);
  1346. }
  1347. }
  1348. static void
  1349. setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
  1350. {
  1351. struct aoedev *d;
  1352. struct aoeif *p, *e;
  1353. int minbcnt;
  1354. d = t->d;
  1355. minbcnt = bcnt;
  1356. p = t->ifs;
  1357. e = p + NAOEIFS;
  1358. for (; p < e; p++) {
  1359. if (p->nd == NULL)
  1360. break; /* end of the valid interfaces */
  1361. if (p->nd == nd) {
  1362. p->bcnt = bcnt; /* we're updating */
  1363. nd = NULL;
  1364. } else if (minbcnt > p->bcnt)
  1365. minbcnt = p->bcnt; /* find the min interface */
  1366. }
  1367. if (nd) {
  1368. if (p == e) {
  1369. pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
  1370. return;
  1371. }
  1372. dev_hold(nd);
  1373. p->nd = nd;
  1374. p->bcnt = bcnt;
  1375. }
  1376. t->minbcnt = minbcnt;
  1377. setdbcnt(d);
  1378. }
  1379. void
  1380. aoecmd_cfg_rsp(struct sk_buff *skb)
  1381. {
  1382. struct aoedev *d;
  1383. struct aoe_hdr *h;
  1384. struct aoe_cfghdr *ch;
  1385. struct aoetgt *t;
  1386. ulong flags, aoemajor;
  1387. struct sk_buff *sl;
  1388. struct sk_buff_head queue;
  1389. u16 n;
  1390. sl = NULL;
  1391. h = (struct aoe_hdr *) skb_mac_header(skb);
  1392. ch = (struct aoe_cfghdr *) (h+1);
  1393. /*
  1394. * Enough people have their dip switches set backwards to
  1395. * warrant a loud message for this special case.
  1396. */
  1397. aoemajor = get_unaligned_be16(&h->major);
  1398. if (aoemajor == 0xfff) {
  1399. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  1400. "Check shelf dip switches.\n");
  1401. return;
  1402. }
  1403. if (aoemajor == 0xffff) {
  1404. pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
  1405. aoemajor, (int) h->minor);
  1406. return;
  1407. }
  1408. if (h->minor == 0xff) {
  1409. pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
  1410. aoemajor, (int) h->minor);
  1411. return;
  1412. }
  1413. n = be16_to_cpu(ch->bufcnt);
  1414. if (n > aoe_maxout) /* keep it reasonable */
  1415. n = aoe_maxout;
  1416. d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
  1417. if (d == NULL) {
  1418. pr_info("aoe: device allocation failure\n");
  1419. return;
  1420. }
  1421. spin_lock_irqsave(&d->lock, flags);
  1422. t = gettgt(d, h->src);
  1423. if (t) {
  1424. t->nframes = n;
  1425. if (n < t->maxout)
  1426. aoecmd_wreset(t);
  1427. } else {
  1428. t = addtgt(d, h->src, n);
  1429. if (!t)
  1430. goto bail;
  1431. }
  1432. n = skb->dev->mtu;
  1433. n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
  1434. n /= 512;
  1435. if (n > ch->scnt)
  1436. n = ch->scnt;
  1437. n = n ? n * 512 : DEFAULTBCNT;
  1438. setifbcnt(t, skb->dev, n);
  1439. /* don't change users' perspective */
  1440. if (d->nopen == 0) {
  1441. d->fw_ver = be16_to_cpu(ch->fwver);
  1442. sl = aoecmd_ata_id(d);
  1443. }
  1444. bail:
  1445. spin_unlock_irqrestore(&d->lock, flags);
  1446. aoedev_put(d);
  1447. if (sl) {
  1448. __skb_queue_head_init(&queue);
  1449. __skb_queue_tail(&queue, sl);
  1450. aoenet_xmit(&queue);
  1451. }
  1452. }
  1453. void
  1454. aoecmd_wreset(struct aoetgt *t)
  1455. {
  1456. t->maxout = 1;
  1457. t->ssthresh = t->nframes / 2;
  1458. t->next_cwnd = t->nframes;
  1459. }
  1460. void
  1461. aoecmd_cleanslate(struct aoedev *d)
  1462. {
  1463. struct aoetgt **t, **te;
  1464. d->rttavg = RTTAVG_INIT;
  1465. d->rttdev = RTTDEV_INIT;
  1466. d->maxbcnt = 0;
  1467. t = d->targets;
  1468. te = t + NTARGETS;
  1469. for (; t < te && *t; t++)
  1470. aoecmd_wreset(*t);
  1471. }
  1472. void
  1473. aoe_failbuf(struct aoedev *d, struct buf *buf)
  1474. {
  1475. if (buf == NULL)
  1476. return;
  1477. buf->resid = 0;
  1478. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1479. if (buf->nframesout == 0)
  1480. aoe_end_buf(d, buf);
  1481. }
  1482. void
  1483. aoe_flush_iocq(void)
  1484. {
  1485. struct frame *f;
  1486. struct aoedev *d;
  1487. LIST_HEAD(flist);
  1488. struct list_head *pos;
  1489. struct sk_buff *skb;
  1490. ulong flags;
  1491. spin_lock_irqsave(&iocq.lock, flags);
  1492. list_splice_init(&iocq.head, &flist);
  1493. spin_unlock_irqrestore(&iocq.lock, flags);
  1494. while (!list_empty(&flist)) {
  1495. pos = flist.next;
  1496. list_del(pos);
  1497. f = list_entry(pos, struct frame, head);
  1498. d = f->t->d;
  1499. skb = f->r_skb;
  1500. spin_lock_irqsave(&d->lock, flags);
  1501. if (f->buf) {
  1502. f->buf->nframesout--;
  1503. aoe_failbuf(d, f->buf);
  1504. }
  1505. aoe_freetframe(f);
  1506. spin_unlock_irqrestore(&d->lock, flags);
  1507. dev_kfree_skb(skb);
  1508. aoedev_put(d);
  1509. }
  1510. }
  1511. int __init
  1512. aoecmd_init(void)
  1513. {
  1514. void *p;
  1515. /* get_zeroed_page returns page with ref count 1 */
  1516. p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
  1517. if (!p)
  1518. return -ENOMEM;
  1519. empty_page = virt_to_page(p);
  1520. INIT_LIST_HEAD(&iocq.head);
  1521. spin_lock_init(&iocq.lock);
  1522. init_waitqueue_head(&ktiowq);
  1523. kts.name = "aoe_ktio";
  1524. kts.fn = ktio;
  1525. kts.waitq = &ktiowq;
  1526. kts.lock = &iocq.lock;
  1527. return aoe_ktstart(&kts);
  1528. }
  1529. void
  1530. aoecmd_exit(void)
  1531. {
  1532. aoe_ktstop(&kts);
  1533. aoe_flush_iocq();
  1534. free_page((unsigned long) page_address(empty_page));
  1535. empty_page = NULL;
  1536. }