aoecmd.c 36 KB

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