scsi_transport_spi.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/config.h>
  22. #include <linux/ctype.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/mutex.h>
  28. #include <scsi/scsi.h>
  29. #include "scsi_priv.h"
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_eh.h>
  34. #include <scsi/scsi_transport.h>
  35. #include <scsi/scsi_transport_spi.h>
  36. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  37. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  38. * on" attributes */
  39. #define SPI_HOST_ATTRS 1
  40. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  41. #define DV_LOOPS 3
  42. #define DV_TIMEOUT (10*HZ)
  43. #define DV_RETRIES 3 /* should only need at most
  44. * two cc/ua clears */
  45. /* Private data accessors (keep these out of the header file) */
  46. #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
  47. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  48. struct spi_internal {
  49. struct scsi_transport_template t;
  50. struct spi_function_template *f;
  51. /* The actual attributes */
  52. struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
  53. /* The array of null terminated pointers to attributes
  54. * needed by scsi_sysfs.c */
  55. struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
  56. struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
  57. struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
  58. };
  59. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  60. static const int ppr_to_ps[] = {
  61. /* The PPR values 0-6 are reserved, fill them in when
  62. * the committee defines them */
  63. -1, /* 0x00 */
  64. -1, /* 0x01 */
  65. -1, /* 0x02 */
  66. -1, /* 0x03 */
  67. -1, /* 0x04 */
  68. -1, /* 0x05 */
  69. -1, /* 0x06 */
  70. 3125, /* 0x07 */
  71. 6250, /* 0x08 */
  72. 12500, /* 0x09 */
  73. 25000, /* 0x0a */
  74. 30300, /* 0x0b */
  75. 50000, /* 0x0c */
  76. };
  77. /* The PPR values at which you calculate the period in ns by multiplying
  78. * by 4 */
  79. #define SPI_STATIC_PPR 0x0c
  80. static int sprint_frac(char *dest, int value, int denom)
  81. {
  82. int frac = value % denom;
  83. int result = sprintf(dest, "%d", value / denom);
  84. if (frac == 0)
  85. return result;
  86. dest[result++] = '.';
  87. do {
  88. denom /= 10;
  89. sprintf(dest + result, "%d", frac / denom);
  90. result++;
  91. frac %= denom;
  92. } while (frac);
  93. dest[result++] = '\0';
  94. return result;
  95. }
  96. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  97. enum dma_data_direction dir,
  98. void *buffer, unsigned bufflen,
  99. struct scsi_sense_hdr *sshdr)
  100. {
  101. int i, result;
  102. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  103. for(i = 0; i < DV_RETRIES; i++) {
  104. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  105. sense, DV_TIMEOUT, /* retries */ 1,
  106. REQ_FAILFAST);
  107. if (result & DRIVER_SENSE) {
  108. struct scsi_sense_hdr sshdr_tmp;
  109. if (!sshdr)
  110. sshdr = &sshdr_tmp;
  111. if (scsi_normalize_sense(sense, sizeof(*sense),
  112. sshdr)
  113. && sshdr->sense_key == UNIT_ATTENTION)
  114. continue;
  115. }
  116. break;
  117. }
  118. return result;
  119. }
  120. static struct {
  121. enum spi_signal_type value;
  122. char *name;
  123. } signal_types[] = {
  124. { SPI_SIGNAL_UNKNOWN, "unknown" },
  125. { SPI_SIGNAL_SE, "SE" },
  126. { SPI_SIGNAL_LVD, "LVD" },
  127. { SPI_SIGNAL_HVD, "HVD" },
  128. };
  129. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  130. {
  131. int i;
  132. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  133. if (type == signal_types[i].value)
  134. return signal_types[i].name;
  135. }
  136. return NULL;
  137. }
  138. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  139. {
  140. int i, len;
  141. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  142. len = strlen(signal_types[i].name);
  143. if (strncmp(name, signal_types[i].name, len) == 0 &&
  144. (name[len] == '\n' || name[len] == '\0'))
  145. return signal_types[i].value;
  146. }
  147. return SPI_SIGNAL_UNKNOWN;
  148. }
  149. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  150. struct class_device *cdev)
  151. {
  152. struct Scsi_Host *shost = dev_to_shost(dev);
  153. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  154. return 0;
  155. }
  156. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  157. "spi_host",
  158. spi_host_setup,
  159. NULL,
  160. NULL);
  161. static int spi_host_match(struct attribute_container *cont,
  162. struct device *dev)
  163. {
  164. struct Scsi_Host *shost;
  165. struct spi_internal *i;
  166. if (!scsi_is_host_device(dev))
  167. return 0;
  168. shost = dev_to_shost(dev);
  169. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  170. != &spi_host_class.class)
  171. return 0;
  172. i = to_spi_internal(shost->transportt);
  173. return &i->t.host_attrs.ac == cont;
  174. }
  175. static int spi_device_configure(struct transport_container *tc,
  176. struct device *dev,
  177. struct class_device *cdev)
  178. {
  179. struct scsi_device *sdev = to_scsi_device(dev);
  180. struct scsi_target *starget = sdev->sdev_target;
  181. /* Populate the target capability fields with the values
  182. * gleaned from the device inquiry */
  183. spi_support_sync(starget) = scsi_device_sync(sdev);
  184. spi_support_wide(starget) = scsi_device_wide(sdev);
  185. spi_support_dt(starget) = scsi_device_dt(sdev);
  186. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  187. spi_support_ius(starget) = scsi_device_ius(sdev);
  188. spi_support_qas(starget) = scsi_device_qas(sdev);
  189. return 0;
  190. }
  191. static int spi_setup_transport_attrs(struct transport_container *tc,
  192. struct device *dev,
  193. struct class_device *cdev)
  194. {
  195. struct scsi_target *starget = to_scsi_target(dev);
  196. spi_period(starget) = -1; /* illegal value */
  197. spi_min_period(starget) = 0;
  198. spi_offset(starget) = 0; /* async */
  199. spi_max_offset(starget) = 255;
  200. spi_width(starget) = 0; /* narrow */
  201. spi_max_width(starget) = 1;
  202. spi_iu(starget) = 0; /* no IU */
  203. spi_dt(starget) = 0; /* ST */
  204. spi_qas(starget) = 0;
  205. spi_wr_flow(starget) = 0;
  206. spi_rd_strm(starget) = 0;
  207. spi_rti(starget) = 0;
  208. spi_pcomp_en(starget) = 0;
  209. spi_hold_mcs(starget) = 0;
  210. spi_dv_pending(starget) = 0;
  211. spi_initial_dv(starget) = 0;
  212. mutex_init(&spi_dv_mutex(starget));
  213. return 0;
  214. }
  215. #define spi_transport_show_simple(field, format_string) \
  216. \
  217. static ssize_t \
  218. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  219. { \
  220. struct scsi_target *starget = transport_class_to_starget(cdev); \
  221. struct spi_transport_attrs *tp; \
  222. \
  223. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  224. return snprintf(buf, 20, format_string, tp->field); \
  225. }
  226. #define spi_transport_store_simple(field, format_string) \
  227. \
  228. static ssize_t \
  229. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  230. size_t count) \
  231. { \
  232. int val; \
  233. struct scsi_target *starget = transport_class_to_starget(cdev); \
  234. struct spi_transport_attrs *tp; \
  235. \
  236. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  237. val = simple_strtoul(buf, NULL, 0); \
  238. tp->field = val; \
  239. return count; \
  240. }
  241. #define spi_transport_show_function(field, format_string) \
  242. \
  243. static ssize_t \
  244. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  245. { \
  246. struct scsi_target *starget = transport_class_to_starget(cdev); \
  247. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  248. struct spi_transport_attrs *tp; \
  249. struct spi_internal *i = to_spi_internal(shost->transportt); \
  250. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  251. if (i->f->get_##field) \
  252. i->f->get_##field(starget); \
  253. return snprintf(buf, 20, format_string, tp->field); \
  254. }
  255. #define spi_transport_store_function(field, format_string) \
  256. static ssize_t \
  257. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  258. size_t count) \
  259. { \
  260. int val; \
  261. struct scsi_target *starget = transport_class_to_starget(cdev); \
  262. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  263. struct spi_internal *i = to_spi_internal(shost->transportt); \
  264. \
  265. val = simple_strtoul(buf, NULL, 0); \
  266. i->f->set_##field(starget, val); \
  267. return count; \
  268. }
  269. #define spi_transport_store_max(field, format_string) \
  270. static ssize_t \
  271. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  272. size_t count) \
  273. { \
  274. int val; \
  275. struct scsi_target *starget = transport_class_to_starget(cdev); \
  276. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  277. struct spi_internal *i = to_spi_internal(shost->transportt); \
  278. struct spi_transport_attrs *tp \
  279. = (struct spi_transport_attrs *)&starget->starget_data; \
  280. \
  281. val = simple_strtoul(buf, NULL, 0); \
  282. if (val > tp->max_##field) \
  283. val = tp->max_##field; \
  284. i->f->set_##field(starget, val); \
  285. return count; \
  286. }
  287. #define spi_transport_rd_attr(field, format_string) \
  288. spi_transport_show_function(field, format_string) \
  289. spi_transport_store_function(field, format_string) \
  290. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  291. show_spi_transport_##field, \
  292. store_spi_transport_##field);
  293. #define spi_transport_simple_attr(field, format_string) \
  294. spi_transport_show_simple(field, format_string) \
  295. spi_transport_store_simple(field, format_string) \
  296. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  297. show_spi_transport_##field, \
  298. store_spi_transport_##field);
  299. #define spi_transport_max_attr(field, format_string) \
  300. spi_transport_show_function(field, format_string) \
  301. spi_transport_store_max(field, format_string) \
  302. spi_transport_simple_attr(max_##field, format_string) \
  303. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  304. show_spi_transport_##field, \
  305. store_spi_transport_##field);
  306. /* The Parallel SCSI Tranport Attributes: */
  307. spi_transport_max_attr(offset, "%d\n");
  308. spi_transport_max_attr(width, "%d\n");
  309. spi_transport_rd_attr(iu, "%d\n");
  310. spi_transport_rd_attr(dt, "%d\n");
  311. spi_transport_rd_attr(qas, "%d\n");
  312. spi_transport_rd_attr(wr_flow, "%d\n");
  313. spi_transport_rd_attr(rd_strm, "%d\n");
  314. spi_transport_rd_attr(rti, "%d\n");
  315. spi_transport_rd_attr(pcomp_en, "%d\n");
  316. spi_transport_rd_attr(hold_mcs, "%d\n");
  317. /* we only care about the first child device so we return 1 */
  318. static int child_iter(struct device *dev, void *data)
  319. {
  320. struct scsi_device *sdev = to_scsi_device(dev);
  321. spi_dv_device(sdev);
  322. return 1;
  323. }
  324. static ssize_t
  325. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  326. {
  327. struct scsi_target *starget = transport_class_to_starget(cdev);
  328. device_for_each_child(&starget->dev, NULL, child_iter);
  329. return count;
  330. }
  331. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  332. /* Translate the period into ns according to the current spec
  333. * for SDTR/PPR messages */
  334. static int period_to_str(char *buf, int period)
  335. {
  336. int len, picosec;
  337. if (period < 0 || period > 0xff) {
  338. picosec = -1;
  339. } else if (period <= SPI_STATIC_PPR) {
  340. picosec = ppr_to_ps[period];
  341. } else {
  342. picosec = period * 4000;
  343. }
  344. if (picosec == -1) {
  345. len = sprintf(buf, "reserved");
  346. } else {
  347. len = sprint_frac(buf, picosec, 1000);
  348. }
  349. return len;
  350. }
  351. static ssize_t
  352. show_spi_transport_period_helper(struct class_device *cdev, char *buf,
  353. int period)
  354. {
  355. int len = period_to_str(buf, period);
  356. buf[len++] = '\n';
  357. buf[len] = '\0';
  358. return len;
  359. }
  360. static ssize_t
  361. store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
  362. size_t count, int *periodp)
  363. {
  364. int j, picosec, period = -1;
  365. char *endp;
  366. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  367. if (*endp == '.') {
  368. int mult = 100;
  369. do {
  370. endp++;
  371. if (!isdigit(*endp))
  372. break;
  373. picosec += (*endp - '0') * mult;
  374. mult /= 10;
  375. } while (mult > 0);
  376. }
  377. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  378. if (ppr_to_ps[j] < picosec)
  379. continue;
  380. period = j;
  381. break;
  382. }
  383. if (period == -1)
  384. period = picosec / 4000;
  385. if (period > 0xff)
  386. period = 0xff;
  387. *periodp = period;
  388. return count;
  389. }
  390. static ssize_t
  391. show_spi_transport_period(struct class_device *cdev, char *buf)
  392. {
  393. struct scsi_target *starget = transport_class_to_starget(cdev);
  394. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  395. struct spi_internal *i = to_spi_internal(shost->transportt);
  396. struct spi_transport_attrs *tp =
  397. (struct spi_transport_attrs *)&starget->starget_data;
  398. if (i->f->get_period)
  399. i->f->get_period(starget);
  400. return show_spi_transport_period_helper(cdev, buf, tp->period);
  401. }
  402. static ssize_t
  403. store_spi_transport_period(struct class_device *cdev, const char *buf,
  404. size_t count)
  405. {
  406. struct scsi_target *starget = transport_class_to_starget(cdev);
  407. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  408. struct spi_internal *i = to_spi_internal(shost->transportt);
  409. struct spi_transport_attrs *tp =
  410. (struct spi_transport_attrs *)&starget->starget_data;
  411. int period, retval;
  412. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  413. if (period < tp->min_period)
  414. period = tp->min_period;
  415. i->f->set_period(starget, period);
  416. return retval;
  417. }
  418. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  419. show_spi_transport_period,
  420. store_spi_transport_period);
  421. static ssize_t
  422. show_spi_transport_min_period(struct class_device *cdev, char *buf)
  423. {
  424. struct scsi_target *starget = transport_class_to_starget(cdev);
  425. struct spi_transport_attrs *tp =
  426. (struct spi_transport_attrs *)&starget->starget_data;
  427. return show_spi_transport_period_helper(cdev, buf, tp->min_period);
  428. }
  429. static ssize_t
  430. store_spi_transport_min_period(struct class_device *cdev, const char *buf,
  431. size_t count)
  432. {
  433. struct scsi_target *starget = transport_class_to_starget(cdev);
  434. struct spi_transport_attrs *tp =
  435. (struct spi_transport_attrs *)&starget->starget_data;
  436. return store_spi_transport_period_helper(cdev, buf, count,
  437. &tp->min_period);
  438. }
  439. static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR,
  440. show_spi_transport_min_period,
  441. store_spi_transport_min_period);
  442. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  443. {
  444. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  445. struct spi_internal *i = to_spi_internal(shost->transportt);
  446. if (i->f->get_signalling)
  447. i->f->get_signalling(shost);
  448. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  449. }
  450. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  451. const char *buf, size_t count)
  452. {
  453. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  454. struct spi_internal *i = to_spi_internal(shost->transportt);
  455. enum spi_signal_type type = spi_signal_to_value(buf);
  456. if (type != SPI_SIGNAL_UNKNOWN)
  457. i->f->set_signalling(shost, type);
  458. return count;
  459. }
  460. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  461. show_spi_host_signalling,
  462. store_spi_host_signalling);
  463. #define DV_SET(x, y) \
  464. if(i->f->set_##x) \
  465. i->f->set_##x(sdev->sdev_target, y)
  466. enum spi_compare_returns {
  467. SPI_COMPARE_SUCCESS,
  468. SPI_COMPARE_FAILURE,
  469. SPI_COMPARE_SKIP_TEST,
  470. };
  471. /* This is for read/write Domain Validation: If the device supports
  472. * an echo buffer, we do read/write tests to it */
  473. static enum spi_compare_returns
  474. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  475. u8 *ptr, const int retries)
  476. {
  477. int len = ptr - buffer;
  478. int j, k, r, result;
  479. unsigned int pattern = 0x0000ffff;
  480. struct scsi_sense_hdr sshdr;
  481. const char spi_write_buffer[] = {
  482. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  483. };
  484. const char spi_read_buffer[] = {
  485. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  486. };
  487. /* set up the pattern buffer. Doesn't matter if we spill
  488. * slightly beyond since that's where the read buffer is */
  489. for (j = 0; j < len; ) {
  490. /* fill the buffer with counting (test a) */
  491. for ( ; j < min(len, 32); j++)
  492. buffer[j] = j;
  493. k = j;
  494. /* fill the buffer with alternating words of 0x0 and
  495. * 0xffff (test b) */
  496. for ( ; j < min(len, k + 32); j += 2) {
  497. u16 *word = (u16 *)&buffer[j];
  498. *word = (j & 0x02) ? 0x0000 : 0xffff;
  499. }
  500. k = j;
  501. /* fill with crosstalk (alternating 0x5555 0xaaa)
  502. * (test c) */
  503. for ( ; j < min(len, k + 32); j += 2) {
  504. u16 *word = (u16 *)&buffer[j];
  505. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  506. }
  507. k = j;
  508. /* fill with shifting bits (test d) */
  509. for ( ; j < min(len, k + 32); j += 4) {
  510. u32 *word = (unsigned int *)&buffer[j];
  511. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  512. *word = pattern;
  513. pattern = (pattern << 1) | roll;
  514. }
  515. /* don't bother with random data (test e) */
  516. }
  517. for (r = 0; r < retries; r++) {
  518. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  519. buffer, len, &sshdr);
  520. if(result || !scsi_device_online(sdev)) {
  521. scsi_device_set_state(sdev, SDEV_QUIESCE);
  522. if (scsi_sense_valid(&sshdr)
  523. && sshdr.sense_key == ILLEGAL_REQUEST
  524. /* INVALID FIELD IN CDB */
  525. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  526. /* This would mean that the drive lied
  527. * to us about supporting an echo
  528. * buffer (unfortunately some Western
  529. * Digital drives do precisely this)
  530. */
  531. return SPI_COMPARE_SKIP_TEST;
  532. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  533. return SPI_COMPARE_FAILURE;
  534. }
  535. memset(ptr, 0, len);
  536. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  537. ptr, len, NULL);
  538. scsi_device_set_state(sdev, SDEV_QUIESCE);
  539. if (memcmp(buffer, ptr, len) != 0)
  540. return SPI_COMPARE_FAILURE;
  541. }
  542. return SPI_COMPARE_SUCCESS;
  543. }
  544. /* This is for the simplest form of Domain Validation: a read test
  545. * on the inquiry data from the device */
  546. static enum spi_compare_returns
  547. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  548. u8 *ptr, const int retries)
  549. {
  550. int r, result;
  551. const int len = sdev->inquiry_len;
  552. const char spi_inquiry[] = {
  553. INQUIRY, 0, 0, 0, len, 0
  554. };
  555. for (r = 0; r < retries; r++) {
  556. memset(ptr, 0, len);
  557. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  558. ptr, len, NULL);
  559. if(result || !scsi_device_online(sdev)) {
  560. scsi_device_set_state(sdev, SDEV_QUIESCE);
  561. return SPI_COMPARE_FAILURE;
  562. }
  563. /* If we don't have the inquiry data already, the
  564. * first read gets it */
  565. if (ptr == buffer) {
  566. ptr += len;
  567. --r;
  568. continue;
  569. }
  570. if (memcmp(buffer, ptr, len) != 0)
  571. /* failure */
  572. return SPI_COMPARE_FAILURE;
  573. }
  574. return SPI_COMPARE_SUCCESS;
  575. }
  576. static enum spi_compare_returns
  577. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  578. enum spi_compare_returns
  579. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  580. {
  581. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  582. struct scsi_target *starget = sdev->sdev_target;
  583. int period = 0, prevperiod = 0;
  584. enum spi_compare_returns retval;
  585. for (;;) {
  586. int newperiod;
  587. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  588. if (retval == SPI_COMPARE_SUCCESS
  589. || retval == SPI_COMPARE_SKIP_TEST)
  590. break;
  591. /* OK, retrain, fallback */
  592. if (i->f->get_iu)
  593. i->f->get_iu(starget);
  594. if (i->f->get_qas)
  595. i->f->get_qas(starget);
  596. if (i->f->get_period)
  597. i->f->get_period(sdev->sdev_target);
  598. /* Here's the fallback sequence; first try turning off
  599. * IU, then QAS (if we can control them), then finally
  600. * fall down the periods */
  601. if (i->f->set_iu && spi_iu(starget)) {
  602. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  603. DV_SET(iu, 0);
  604. } else if (i->f->set_qas && spi_qas(starget)) {
  605. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  606. DV_SET(qas, 0);
  607. } else {
  608. newperiod = spi_period(starget);
  609. period = newperiod > period ? newperiod : period;
  610. if (period < 0x0d)
  611. period++;
  612. else
  613. period += period >> 1;
  614. if (unlikely(period > 0xff || period == prevperiod)) {
  615. /* Total failure; set to async and return */
  616. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  617. DV_SET(offset, 0);
  618. return SPI_COMPARE_FAILURE;
  619. }
  620. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  621. DV_SET(period, period);
  622. prevperiod = period;
  623. }
  624. }
  625. return retval;
  626. }
  627. static int
  628. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  629. {
  630. int l, result;
  631. /* first off do a test unit ready. This can error out
  632. * because of reservations or some other reason. If it
  633. * fails, the device won't let us write to the echo buffer
  634. * so just return failure */
  635. const char spi_test_unit_ready[] = {
  636. TEST_UNIT_READY, 0, 0, 0, 0, 0
  637. };
  638. const char spi_read_buffer_descriptor[] = {
  639. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  640. };
  641. /* We send a set of three TURs to clear any outstanding
  642. * unit attention conditions if they exist (Otherwise the
  643. * buffer tests won't be happy). If the TUR still fails
  644. * (reservation conflict, device not ready, etc) just
  645. * skip the write tests */
  646. for (l = 0; ; l++) {
  647. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  648. NULL, 0, NULL);
  649. if(result) {
  650. if(l >= 3)
  651. return 0;
  652. } else {
  653. /* TUR succeeded */
  654. break;
  655. }
  656. }
  657. result = spi_execute(sdev, spi_read_buffer_descriptor,
  658. DMA_FROM_DEVICE, buffer, 4, NULL);
  659. if (result)
  660. /* Device has no echo buffer */
  661. return 0;
  662. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  663. }
  664. static void
  665. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  666. {
  667. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  668. struct scsi_target *starget = sdev->sdev_target;
  669. int len = sdev->inquiry_len;
  670. /* first set us up for narrow async */
  671. DV_SET(offset, 0);
  672. DV_SET(width, 0);
  673. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  674. != SPI_COMPARE_SUCCESS) {
  675. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  676. /* FIXME: should probably offline the device here? */
  677. return;
  678. }
  679. /* test width */
  680. if (i->f->set_width && spi_max_width(starget) &&
  681. scsi_device_wide(sdev)) {
  682. i->f->set_width(starget, 1);
  683. if (spi_dv_device_compare_inquiry(sdev, buffer,
  684. buffer + len,
  685. DV_LOOPS)
  686. != SPI_COMPARE_SUCCESS) {
  687. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  688. i->f->set_width(starget, 0);
  689. }
  690. }
  691. if (!i->f->set_period)
  692. return;
  693. /* device can't handle synchronous */
  694. if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
  695. return;
  696. /* len == -1 is the signal that we need to ascertain the
  697. * presence of an echo buffer before trying to use it. len ==
  698. * 0 means we don't have an echo buffer */
  699. len = -1;
  700. retry:
  701. /* now set up to the maximum */
  702. DV_SET(offset, spi_max_offset(starget));
  703. DV_SET(period, spi_min_period(starget));
  704. /* try QAS requests; this should be harmless to set if the
  705. * target supports it */
  706. if (scsi_device_qas(sdev))
  707. DV_SET(qas, 1);
  708. /* Also try IU transfers */
  709. if (scsi_device_ius(sdev))
  710. DV_SET(iu, 1);
  711. if (spi_min_period(starget) < 9) {
  712. /* This u320 (or u640). Ignore the coupled parameters
  713. * like DT and IU, but set the optional ones */
  714. DV_SET(rd_strm, 1);
  715. DV_SET(wr_flow, 1);
  716. DV_SET(rti, 1);
  717. if (spi_min_period(starget) == 8)
  718. DV_SET(pcomp_en, 1);
  719. }
  720. /* Do the read only INQUIRY tests */
  721. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  722. spi_dv_device_compare_inquiry);
  723. /* See if we actually managed to negotiate and sustain DT */
  724. if (i->f->get_dt)
  725. i->f->get_dt(starget);
  726. /* see if the device has an echo buffer. If it does we can do
  727. * the SPI pattern write tests. Because of some broken
  728. * devices, we *only* try this on a device that has actually
  729. * negotiated DT */
  730. if (len == -1 && spi_dt(starget))
  731. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  732. if (len <= 0) {
  733. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  734. return;
  735. }
  736. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  737. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  738. len = SPI_MAX_ECHO_BUFFER_SIZE;
  739. }
  740. if (spi_dv_retrain(sdev, buffer, buffer + len,
  741. spi_dv_device_echo_buffer)
  742. == SPI_COMPARE_SKIP_TEST) {
  743. /* OK, the stupid drive can't do a write echo buffer
  744. * test after all, fall back to the read tests */
  745. len = 0;
  746. goto retry;
  747. }
  748. }
  749. /** spi_dv_device - Do Domain Validation on the device
  750. * @sdev: scsi device to validate
  751. *
  752. * Performs the domain validation on the given device in the
  753. * current execution thread. Since DV operations may sleep,
  754. * the current thread must have user context. Also no SCSI
  755. * related locks that would deadlock I/O issued by the DV may
  756. * be held.
  757. */
  758. void
  759. spi_dv_device(struct scsi_device *sdev)
  760. {
  761. struct scsi_target *starget = sdev->sdev_target;
  762. u8 *buffer;
  763. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  764. if (unlikely(scsi_device_get(sdev)))
  765. return;
  766. buffer = kzalloc(len, GFP_KERNEL);
  767. if (unlikely(!buffer))
  768. goto out_put;
  769. /* We need to verify that the actual device will quiesce; the
  770. * later target quiesce is just a nice to have */
  771. if (unlikely(scsi_device_quiesce(sdev)))
  772. goto out_free;
  773. scsi_target_quiesce(starget);
  774. spi_dv_pending(starget) = 1;
  775. mutex_lock(&spi_dv_mutex(starget));
  776. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  777. spi_dv_device_internal(sdev, buffer);
  778. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  779. mutex_unlock(&spi_dv_mutex(starget));
  780. spi_dv_pending(starget) = 0;
  781. scsi_target_resume(starget);
  782. spi_initial_dv(starget) = 1;
  783. out_free:
  784. kfree(buffer);
  785. out_put:
  786. scsi_device_put(sdev);
  787. }
  788. EXPORT_SYMBOL(spi_dv_device);
  789. struct work_queue_wrapper {
  790. struct work_struct work;
  791. struct scsi_device *sdev;
  792. };
  793. static void
  794. spi_dv_device_work_wrapper(void *data)
  795. {
  796. struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
  797. struct scsi_device *sdev = wqw->sdev;
  798. kfree(wqw);
  799. spi_dv_device(sdev);
  800. spi_dv_pending(sdev->sdev_target) = 0;
  801. scsi_device_put(sdev);
  802. }
  803. /**
  804. * spi_schedule_dv_device - schedule domain validation to occur on the device
  805. * @sdev: The device to validate
  806. *
  807. * Identical to spi_dv_device() above, except that the DV will be
  808. * scheduled to occur in a workqueue later. All memory allocations
  809. * are atomic, so may be called from any context including those holding
  810. * SCSI locks.
  811. */
  812. void
  813. spi_schedule_dv_device(struct scsi_device *sdev)
  814. {
  815. struct work_queue_wrapper *wqw =
  816. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  817. if (unlikely(!wqw))
  818. return;
  819. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  820. kfree(wqw);
  821. return;
  822. }
  823. /* Set pending early (dv_device doesn't check it, only sets it) */
  824. spi_dv_pending(sdev->sdev_target) = 1;
  825. if (unlikely(scsi_device_get(sdev))) {
  826. kfree(wqw);
  827. spi_dv_pending(sdev->sdev_target) = 0;
  828. return;
  829. }
  830. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
  831. wqw->sdev = sdev;
  832. schedule_work(&wqw->work);
  833. }
  834. EXPORT_SYMBOL(spi_schedule_dv_device);
  835. /**
  836. * spi_display_xfer_agreement - Print the current target transfer agreement
  837. * @starget: The target for which to display the agreement
  838. *
  839. * Each SPI port is required to maintain a transfer agreement for each
  840. * other port on the bus. This function prints a one-line summary of
  841. * the current agreement; more detailed information is available in sysfs.
  842. */
  843. void spi_display_xfer_agreement(struct scsi_target *starget)
  844. {
  845. struct spi_transport_attrs *tp;
  846. tp = (struct spi_transport_attrs *)&starget->starget_data;
  847. if (tp->offset > 0 && tp->period > 0) {
  848. unsigned int picosec, kb100;
  849. char *scsi = "FAST-?";
  850. char tmp[8];
  851. if (tp->period <= SPI_STATIC_PPR) {
  852. picosec = ppr_to_ps[tp->period];
  853. switch (tp->period) {
  854. case 7: scsi = "FAST-320"; break;
  855. case 8: scsi = "FAST-160"; break;
  856. case 9: scsi = "FAST-80"; break;
  857. case 10:
  858. case 11: scsi = "FAST-40"; break;
  859. case 12: scsi = "FAST-20"; break;
  860. }
  861. } else {
  862. picosec = tp->period * 4000;
  863. if (tp->period < 25)
  864. scsi = "FAST-20";
  865. else if (tp->period < 50)
  866. scsi = "FAST-10";
  867. else
  868. scsi = "FAST-5";
  869. }
  870. kb100 = (10000000 + picosec / 2) / picosec;
  871. if (tp->width)
  872. kb100 *= 2;
  873. sprint_frac(tmp, picosec, 1000);
  874. dev_info(&starget->dev,
  875. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  876. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  877. tp->dt ? "DT" : "ST",
  878. tp->iu ? " IU" : "",
  879. tp->qas ? " QAS" : "",
  880. tp->rd_strm ? " RDSTRM" : "",
  881. tp->rti ? " RTI" : "",
  882. tp->wr_flow ? " WRFLOW" : "",
  883. tp->pcomp_en ? " PCOMP" : "",
  884. tp->hold_mcs ? " HMCS" : "",
  885. tmp, tp->offset);
  886. } else {
  887. dev_info(&starget->dev, "%sasynchronous\n",
  888. tp->width ? "wide " : "");
  889. }
  890. }
  891. EXPORT_SYMBOL(spi_display_xfer_agreement);
  892. #ifdef CONFIG_SCSI_CONSTANTS
  893. static const char * const one_byte_msgs[] = {
  894. /* 0x00 */ "Command Complete", NULL, "Save Pointers",
  895. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  896. /* 0x06 */ "Abort", "Message Reject", "Nop", "Message Parity Error",
  897. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  898. /* 0x0c */ "Bus device reset", "Abort Tag", "Clear Queue",
  899. /* 0x0f */ "Initiate Recovery", "Release Recovery"
  900. };
  901. static const char * const two_byte_msgs[] = {
  902. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  903. /* 0x23 */ "Ignore Wide Residue"
  904. };
  905. static const char * const extended_msgs[] = {
  906. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  907. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  908. /* 0x04 */ "Parallel Protocol Request"
  909. };
  910. static void print_nego(const unsigned char *msg, int per, int off, int width)
  911. {
  912. if (per) {
  913. char buf[20];
  914. period_to_str(buf, msg[per]);
  915. printk("period = %s ns ", buf);
  916. }
  917. if (off)
  918. printk("offset = %d ", msg[off]);
  919. if (width)
  920. printk("width = %d ", 8 << msg[width]);
  921. }
  922. int spi_print_msg(const unsigned char *msg)
  923. {
  924. int len = 0, i;
  925. if (msg[0] == EXTENDED_MESSAGE) {
  926. len = 3 + msg[1];
  927. if (msg[2] < ARRAY_SIZE(extended_msgs))
  928. printk ("%s ", extended_msgs[msg[2]]);
  929. else
  930. printk ("Extended Message, reserved code (0x%02x) ",
  931. (int) msg[2]);
  932. switch (msg[2]) {
  933. case EXTENDED_MODIFY_DATA_POINTER:
  934. printk("pointer = %d", (int) (msg[3] << 24) |
  935. (msg[4] << 16) | (msg[5] << 8) | msg[6]);
  936. break;
  937. case EXTENDED_SDTR:
  938. print_nego(msg, 3, 4, 0);
  939. break;
  940. case EXTENDED_WDTR:
  941. print_nego(msg, 0, 0, 3);
  942. break;
  943. case EXTENDED_PPR:
  944. print_nego(msg, 3, 5, 6);
  945. break;
  946. default:
  947. for (i = 2; i < len; ++i)
  948. printk("%02x ", msg[i]);
  949. }
  950. /* Identify */
  951. } else if (msg[0] & 0x80) {
  952. printk("Identify disconnect %sallowed %s %d ",
  953. (msg[0] & 0x40) ? "" : "not ",
  954. (msg[0] & 0x20) ? "target routine" : "lun",
  955. msg[0] & 0x7);
  956. len = 1;
  957. /* Normal One byte */
  958. } else if (msg[0] < 0x1f) {
  959. if (msg[0] < ARRAY_SIZE(one_byte_msgs))
  960. printk(one_byte_msgs[msg[0]]);
  961. else
  962. printk("reserved (%02x) ", msg[0]);
  963. len = 1;
  964. /* Two byte */
  965. } else if (msg[0] <= 0x2f) {
  966. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  967. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  968. msg[1]);
  969. else
  970. printk("reserved two byte (%02x %02x) ",
  971. msg[0], msg[1]);
  972. len = 2;
  973. } else
  974. printk("reserved");
  975. return len;
  976. }
  977. EXPORT_SYMBOL(spi_print_msg);
  978. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  979. int spi_print_msg(const unsigned char *msg)
  980. {
  981. int len = 0, i;
  982. if (msg[0] == EXTENDED_MESSAGE) {
  983. len = 3 + msg[1];
  984. for (i = 0; i < len; ++i)
  985. printk("%02x ", msg[i]);
  986. /* Identify */
  987. } else if (msg[0] & 0x80) {
  988. printk("%02x ", msg[0]);
  989. len = 1;
  990. /* Normal One byte */
  991. } else if (msg[0] < 0x1f) {
  992. printk("%02x ", msg[0]);
  993. len = 1;
  994. /* Two byte */
  995. } else if (msg[0] <= 0x2f) {
  996. printk("%02x %02x", msg[0], msg[1]);
  997. len = 2;
  998. } else
  999. printk("%02x ", msg[0]);
  1000. return len;
  1001. }
  1002. EXPORT_SYMBOL(spi_print_msg);
  1003. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1004. #define SETUP_ATTRIBUTE(field) \
  1005. i->private_attrs[count] = class_device_attr_##field; \
  1006. if (!i->f->set_##field) { \
  1007. i->private_attrs[count].attr.mode = S_IRUGO; \
  1008. i->private_attrs[count].store = NULL; \
  1009. } \
  1010. i->attrs[count] = &i->private_attrs[count]; \
  1011. if (i->f->show_##field) \
  1012. count++
  1013. #define SETUP_RELATED_ATTRIBUTE(field, rel_field) \
  1014. i->private_attrs[count] = class_device_attr_##field; \
  1015. if (!i->f->set_##rel_field) { \
  1016. i->private_attrs[count].attr.mode = S_IRUGO; \
  1017. i->private_attrs[count].store = NULL; \
  1018. } \
  1019. i->attrs[count] = &i->private_attrs[count]; \
  1020. if (i->f->show_##rel_field) \
  1021. count++
  1022. #define SETUP_HOST_ATTRIBUTE(field) \
  1023. i->private_host_attrs[count] = class_device_attr_##field; \
  1024. if (!i->f->set_##field) { \
  1025. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1026. i->private_host_attrs[count].store = NULL; \
  1027. } \
  1028. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1029. count++
  1030. static int spi_device_match(struct attribute_container *cont,
  1031. struct device *dev)
  1032. {
  1033. struct scsi_device *sdev;
  1034. struct Scsi_Host *shost;
  1035. struct spi_internal *i;
  1036. if (!scsi_is_sdev_device(dev))
  1037. return 0;
  1038. sdev = to_scsi_device(dev);
  1039. shost = sdev->host;
  1040. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1041. != &spi_host_class.class)
  1042. return 0;
  1043. /* Note: this class has no device attributes, so it has
  1044. * no per-HBA allocation and thus we don't need to distinguish
  1045. * the attribute containers for the device */
  1046. i = to_spi_internal(shost->transportt);
  1047. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1048. return 0;
  1049. return 1;
  1050. }
  1051. static int spi_target_match(struct attribute_container *cont,
  1052. struct device *dev)
  1053. {
  1054. struct Scsi_Host *shost;
  1055. struct scsi_target *starget;
  1056. struct spi_internal *i;
  1057. if (!scsi_is_target_device(dev))
  1058. return 0;
  1059. shost = dev_to_shost(dev->parent);
  1060. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1061. != &spi_host_class.class)
  1062. return 0;
  1063. i = to_spi_internal(shost->transportt);
  1064. starget = to_scsi_target(dev);
  1065. if (i->f->deny_binding && i->f->deny_binding(starget))
  1066. return 0;
  1067. return &i->t.target_attrs.ac == cont;
  1068. }
  1069. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1070. "spi_transport",
  1071. spi_setup_transport_attrs,
  1072. NULL,
  1073. NULL);
  1074. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1075. spi_device_match,
  1076. spi_device_configure);
  1077. struct scsi_transport_template *
  1078. spi_attach_transport(struct spi_function_template *ft)
  1079. {
  1080. int count = 0;
  1081. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1082. GFP_KERNEL);
  1083. if (unlikely(!i))
  1084. return NULL;
  1085. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1086. i->t.target_attrs.ac.attrs = &i->attrs[0];
  1087. i->t.target_attrs.ac.match = spi_target_match;
  1088. transport_container_register(&i->t.target_attrs);
  1089. i->t.target_size = sizeof(struct spi_transport_attrs);
  1090. i->t.host_attrs.ac.class = &spi_host_class.class;
  1091. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1092. i->t.host_attrs.ac.match = spi_host_match;
  1093. transport_container_register(&i->t.host_attrs);
  1094. i->t.host_size = sizeof(struct spi_host_attrs);
  1095. i->f = ft;
  1096. SETUP_ATTRIBUTE(period);
  1097. SETUP_RELATED_ATTRIBUTE(min_period, period);
  1098. SETUP_ATTRIBUTE(offset);
  1099. SETUP_RELATED_ATTRIBUTE(max_offset, offset);
  1100. SETUP_ATTRIBUTE(width);
  1101. SETUP_RELATED_ATTRIBUTE(max_width, width);
  1102. SETUP_ATTRIBUTE(iu);
  1103. SETUP_ATTRIBUTE(dt);
  1104. SETUP_ATTRIBUTE(qas);
  1105. SETUP_ATTRIBUTE(wr_flow);
  1106. SETUP_ATTRIBUTE(rd_strm);
  1107. SETUP_ATTRIBUTE(rti);
  1108. SETUP_ATTRIBUTE(pcomp_en);
  1109. SETUP_ATTRIBUTE(hold_mcs);
  1110. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  1111. * this bug will trigger */
  1112. BUG_ON(count > SPI_NUM_ATTRS);
  1113. i->attrs[count++] = &class_device_attr_revalidate;
  1114. i->attrs[count] = NULL;
  1115. count = 0;
  1116. SETUP_HOST_ATTRIBUTE(signalling);
  1117. BUG_ON(count > SPI_HOST_ATTRS);
  1118. i->host_attrs[count] = NULL;
  1119. return &i->t;
  1120. }
  1121. EXPORT_SYMBOL(spi_attach_transport);
  1122. void spi_release_transport(struct scsi_transport_template *t)
  1123. {
  1124. struct spi_internal *i = to_spi_internal(t);
  1125. transport_container_unregister(&i->t.target_attrs);
  1126. transport_container_unregister(&i->t.host_attrs);
  1127. kfree(i);
  1128. }
  1129. EXPORT_SYMBOL(spi_release_transport);
  1130. static __init int spi_transport_init(void)
  1131. {
  1132. int error = transport_class_register(&spi_transport_class);
  1133. if (error)
  1134. return error;
  1135. error = anon_transport_class_register(&spi_device_class);
  1136. return transport_class_register(&spi_host_class);
  1137. }
  1138. static void __exit spi_transport_exit(void)
  1139. {
  1140. transport_class_unregister(&spi_transport_class);
  1141. anon_transport_class_unregister(&spi_device_class);
  1142. transport_class_unregister(&spi_host_class);
  1143. }
  1144. MODULE_AUTHOR("Martin Hicks");
  1145. MODULE_DESCRIPTION("SPI Transport Attributes");
  1146. MODULE_LICENSE("GPL");
  1147. module_init(spi_transport_init);
  1148. module_exit(spi_transport_exit);