scsi_transport_spi.c 44 KB

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