scsi_transport_spi.c 41 KB

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