scsi_transport_spi.c 44 KB

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